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" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "TreeTransform.h" |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 22 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 26 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 31 | #include "clang/Lex/LiteralSupport.h" |
| 32 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 33 | #include "clang/Sema/AnalysisBasedWarnings.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 34 | #include "clang/Sema/DeclSpec.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 35 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 36 | #include "clang/Sema/Designator.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 37 | #include "clang/Sema/Initialization.h" |
| 38 | #include "clang/Sema/Lookup.h" |
| 39 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 40 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 41 | #include "clang/Sema/ScopeInfo.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" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 45 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 47 | /// \brief Determine whether the use of this declaration is valid, without |
| 48 | /// emitting diagnostics. |
| 49 | bool Sema::CanUseDecl(NamedDecl *D) { |
| 50 | // See if this is an auto-typed variable whose initializer we are parsing. |
| 51 | if (ParsingInitForAutoVars.count(D)) |
| 52 | return false; |
| 53 | |
| 54 | // See if this is a deleted function. |
| 55 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 56 | if (FD->isDeleted()) |
| 57 | return false; |
| 58 | } |
Sebastian Redl | 28bdb14 | 2011-10-16 18:19:16 +0000 | [diff] [blame] | 59 | |
| 60 | // See if this function is unavailable. |
| 61 | if (D->getAvailability() == AR_Unavailable && |
| 62 | cast<Decl>(CurContext)->getAvailability() != AR_Unavailable) |
| 63 | return false; |
| 64 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 65 | return true; |
| 66 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 67 | |
Fariborz Jahanian | 2d40d9e | 2012-09-06 16:43:18 +0000 | [diff] [blame] | 68 | static void DiagnoseUnusedOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc) { |
| 69 | // Warn if this is used but marked unused. |
| 70 | if (D->hasAttr<UnusedAttr>()) { |
Fariborz Jahanian | 3359fa3 | 2012-09-06 18:38:58 +0000 | [diff] [blame] | 71 | const Decl *DC = cast<Decl>(S.getCurObjCLexicalContext()); |
Fariborz Jahanian | 2d40d9e | 2012-09-06 16:43:18 +0000 | [diff] [blame] | 72 | if (!DC->hasAttr<UnusedAttr>()) |
| 73 | S.Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 74 | } |
| 75 | } |
| 76 | |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 77 | static AvailabilityResult DiagnoseAvailabilityOfDecl(Sema &S, |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 78 | NamedDecl *D, SourceLocation Loc, |
| 79 | const ObjCInterfaceDecl *UnknownObjCClass) { |
| 80 | // See if this declaration is unavailable or deprecated. |
| 81 | std::string Message; |
| 82 | AvailabilityResult Result = D->getAvailability(&Message); |
Fariborz Jahanian | 39b4fc8 | 2011-11-28 19:45:58 +0000 | [diff] [blame] | 83 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 84 | if (Result == AR_Available) { |
| 85 | const DeclContext *DC = ECD->getDeclContext(); |
| 86 | if (const EnumDecl *TheEnumDecl = dyn_cast<EnumDecl>(DC)) |
| 87 | Result = TheEnumDecl->getAvailability(&Message); |
| 88 | } |
Jordan Rose | 04bec39 | 2012-10-10 16:42:54 +0000 | [diff] [blame] | 89 | |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 90 | const ObjCPropertyDecl *ObjCPDecl = 0; |
Jordan Rose | 04bec39 | 2012-10-10 16:42:54 +0000 | [diff] [blame] | 91 | if (Result == AR_Deprecated || Result == AR_Unavailable) { |
| 92 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 93 | if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) { |
| 94 | AvailabilityResult PDeclResult = PD->getAvailability(0); |
| 95 | if (PDeclResult == Result) |
| 96 | ObjCPDecl = PD; |
| 97 | } |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 98 | } |
Jordan Rose | 04bec39 | 2012-10-10 16:42:54 +0000 | [diff] [blame] | 99 | } |
Fariborz Jahanian | 39b4fc8 | 2011-11-28 19:45:58 +0000 | [diff] [blame] | 100 | |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 101 | switch (Result) { |
| 102 | case AR_Available: |
| 103 | case AR_NotYetIntroduced: |
| 104 | break; |
| 105 | |
| 106 | case AR_Deprecated: |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 107 | S.EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass, ObjCPDecl); |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 108 | break; |
| 109 | |
| 110 | case AR_Unavailable: |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 111 | if (S.getCurContextAvailability() != AR_Unavailable) { |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 112 | if (Message.empty()) { |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 113 | if (!UnknownObjCClass) { |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 114 | S.Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 115 | if (ObjCPDecl) |
| 116 | S.Diag(ObjCPDecl->getLocation(), diag::note_property_attribute) |
| 117 | << ObjCPDecl->getDeclName() << 1; |
| 118 | } |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 119 | else |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 120 | S.Diag(Loc, diag::warn_unavailable_fwdclass_message) |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 121 | << D->getDeclName(); |
| 122 | } |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 123 | else |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 124 | S.Diag(Loc, diag::err_unavailable_message) |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 125 | << D->getDeclName() << Message; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 126 | S.Diag(D->getLocation(), diag::note_unavailable_here) |
| 127 | << isa<FunctionDecl>(D) << false; |
| 128 | if (ObjCPDecl) |
| 129 | S.Diag(ObjCPDecl->getLocation(), diag::note_property_attribute) |
| 130 | << ObjCPDecl->getDeclName() << 1; |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 131 | } |
| 132 | break; |
| 133 | } |
| 134 | return Result; |
| 135 | } |
| 136 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 137 | /// \brief Emit a note explaining that this function is deleted or unavailable. |
| 138 | void Sema::NoteDeletedFunction(FunctionDecl *Decl) { |
| 139 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Decl); |
| 140 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 141 | if (Method && Method->isDeleted() && !Method->isDeletedAsWritten()) { |
| 142 | // If the method was explicitly defaulted, point at that declaration. |
| 143 | if (!Method->isImplicit()) |
| 144 | Diag(Decl->getLocation(), diag::note_implicitly_deleted); |
| 145 | |
| 146 | // Try to diagnose why this special member function was implicitly |
| 147 | // deleted. This might fail, if that reason no longer applies. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 148 | CXXSpecialMember CSM = getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 149 | if (CSM != CXXInvalid) |
| 150 | ShouldDeleteSpecialMember(Method, CSM, /*Diagnose=*/true); |
| 151 | |
| 152 | return; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | Diag(Decl->getLocation(), diag::note_unavailable_here) |
| 156 | << 1 << Decl->isDeleted(); |
| 157 | } |
| 158 | |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 159 | /// \brief Determine whether a FunctionDecl was ever declared with an |
| 160 | /// explicit storage class. |
| 161 | static bool hasAnyExplicitStorageClass(const FunctionDecl *D) { |
| 162 | for (FunctionDecl::redecl_iterator I = D->redecls_begin(), |
| 163 | E = D->redecls_end(); |
| 164 | I != E; ++I) { |
| 165 | if (I->getStorageClassAsWritten() != SC_None) |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | /// \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] | 172 | /// variable or function with internal linkage (C11 6.7.4p3). |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 173 | /// |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 174 | /// 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] | 175 | /// in many cases it will not behave correctly. This is not enabled in C++ mode |
| 176 | /// because the restriction language is a bit weaker (C++11 [basic.def.odr]p6) |
| 177 | /// and so while there may still be user mistakes, most of the time we can't |
| 178 | /// prove that there are errors. |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 179 | static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S, |
| 180 | const NamedDecl *D, |
| 181 | SourceLocation Loc) { |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 182 | // This is disabled under C++; there are too many ways for this to fire in |
| 183 | // contexts where the warning is a false positive, or where it is technically |
| 184 | // correct but benign. |
| 185 | if (S.getLangOpts().CPlusPlus) |
| 186 | return; |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 187 | |
| 188 | // Check if this is an inlined function or method. |
| 189 | FunctionDecl *Current = S.getCurFunctionDecl(); |
| 190 | if (!Current) |
| 191 | return; |
| 192 | if (!Current->isInlined()) |
| 193 | return; |
| 194 | if (Current->getLinkage() != ExternalLinkage) |
| 195 | return; |
| 196 | |
| 197 | // Check if the decl has internal linkage. |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 198 | if (D->getLinkage() != InternalLinkage) |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 199 | return; |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 200 | |
Jordan Rose | 0523327 | 2012-06-21 05:54:50 +0000 | [diff] [blame] | 201 | // Downgrade from ExtWarn to Extension if |
| 202 | // (1) the supposedly external inline function is in the main file, |
| 203 | // and probably won't be included anywhere else. |
| 204 | // (2) the thing we're referencing is a pure function. |
| 205 | // (3) the thing we're referencing is another inline function. |
| 206 | // This last can give us false negatives, but it's better than warning on |
| 207 | // wrappers for simple C library functions. |
| 208 | const FunctionDecl *UsedFn = dyn_cast<FunctionDecl>(D); |
| 209 | bool DowngradeWarning = S.getSourceManager().isFromMainFile(Loc); |
| 210 | if (!DowngradeWarning && UsedFn) |
| 211 | DowngradeWarning = UsedFn->isInlined() || UsedFn->hasAttr<ConstAttr>(); |
| 212 | |
| 213 | S.Diag(Loc, DowngradeWarning ? diag::ext_internal_in_extern_inline |
| 214 | : diag::warn_internal_in_extern_inline) |
| 215 | << /*IsVar=*/!UsedFn << D; |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 216 | |
| 217 | // Suggest "static" on the inline function, if possible. |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 218 | if (!hasAnyExplicitStorageClass(Current)) { |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 219 | const FunctionDecl *FirstDecl = Current->getCanonicalDecl(); |
| 220 | SourceLocation DeclBegin = FirstDecl->getSourceRange().getBegin(); |
| 221 | S.Diag(DeclBegin, diag::note_convert_inline_to_static) |
| 222 | << Current << FixItHint::CreateInsertion(DeclBegin, "static "); |
| 223 | } |
| 224 | |
| 225 | S.Diag(D->getCanonicalDecl()->getLocation(), |
| 226 | diag::note_internal_decl_declared_here) |
| 227 | << D; |
| 228 | } |
| 229 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 230 | /// \brief Determine whether the use of this declaration is valid, and |
| 231 | /// emit any corresponding diagnostics. |
| 232 | /// |
| 233 | /// This routine diagnoses various problems with referencing |
| 234 | /// declarations that can occur when using a declaration. For example, |
| 235 | /// it might warn if a deprecated or unavailable declaration is being |
| 236 | /// used, or produce an error (and return true) if a C++0x deleted |
| 237 | /// function is being used. |
| 238 | /// |
| 239 | /// \returns true if there was an error (this declaration cannot be |
| 240 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 241 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 242 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 243 | const ObjCInterfaceDecl *UnknownObjCClass) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 244 | if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)) { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 245 | // If there were any diagnostics suppressed by template argument deduction, |
| 246 | // emit them now. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 247 | llvm::DenseMap<Decl *, SmallVector<PartialDiagnosticAt, 1> >::iterator |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 248 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 249 | if (Pos != SuppressedDiagnostics.end()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 250 | SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 251 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 252 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 253 | |
| 254 | // 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] | 255 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 256 | // entry from the table, because we want to avoid ever emitting these |
| 257 | // diagnostics again. |
| 258 | Suppressed.clear(); |
| 259 | } |
| 260 | } |
| 261 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 262 | // 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] | 263 | if (ParsingInitForAutoVars.count(D)) { |
| 264 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 265 | << D->getDeclName(); |
| 266 | return true; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 269 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 270 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 271 | if (FD->isDeleted()) { |
| 272 | Diag(Loc, diag::err_deleted_function_use); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 273 | NoteDeletedFunction(FD); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 274 | return true; |
| 275 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 276 | } |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 277 | DiagnoseAvailabilityOfDecl(*this, D, Loc, UnknownObjCClass); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 278 | |
Fariborz Jahanian | 2d40d9e | 2012-09-06 16:43:18 +0000 | [diff] [blame] | 279 | DiagnoseUnusedOfDecl(*this, D, Loc); |
Jordan Rose | 106af9e | 2012-06-15 18:19:48 +0000 | [diff] [blame] | 280 | |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 281 | diagnoseUseOfInternalDeclInInlineFunction(*this, D, Loc); |
Jordan Rose | 106af9e | 2012-06-15 18:19:48 +0000 | [diff] [blame] | 282 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 283 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 286 | /// \brief Retrieve the message suffix that should be added to a |
| 287 | /// diagnostic complaining about the given function being deleted or |
| 288 | /// unavailable. |
| 289 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 290 | std::string Message; |
| 291 | if (FD->getAvailability(&Message)) |
| 292 | return ": " + Message; |
| 293 | |
| 294 | return std::string(); |
| 295 | } |
| 296 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 297 | /// DiagnoseSentinelCalls - This routine checks whether a call or |
| 298 | /// message-send is to a declaration with the sentinel attribute, and |
| 299 | /// if so, it checks that the requirements of the sentinel are |
| 300 | /// satisfied. |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 301 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 302 | Expr **args, unsigned numArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 303 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 305 | return; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 306 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 307 | // The number of formal parameters of the declaration. |
| 308 | unsigned numFormalParams; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 310 | // The kind of declaration. This is also an index into a %select in |
| 311 | // the diagnostic. |
| 312 | enum CalleeType { CT_Function, CT_Method, CT_Block } calleeType; |
| 313 | |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 314 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 315 | numFormalParams = MD->param_size(); |
| 316 | calleeType = CT_Method; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 317 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 318 | numFormalParams = FD->param_size(); |
| 319 | calleeType = CT_Function; |
| 320 | } else if (isa<VarDecl>(D)) { |
| 321 | QualType type = cast<ValueDecl>(D)->getType(); |
| 322 | const FunctionType *fn = 0; |
| 323 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 324 | fn = ptr->getPointeeType()->getAs<FunctionType>(); |
| 325 | if (!fn) return; |
| 326 | calleeType = CT_Function; |
| 327 | } else if (const BlockPointerType *ptr = type->getAs<BlockPointerType>()) { |
| 328 | fn = ptr->getPointeeType()->castAs<FunctionType>(); |
| 329 | calleeType = CT_Block; |
| 330 | } else { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 331 | return; |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 332 | } |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 333 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 334 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fn)) { |
| 335 | numFormalParams = proto->getNumArgs(); |
| 336 | } else { |
| 337 | numFormalParams = 0; |
| 338 | } |
| 339 | } else { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 340 | return; |
| 341 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 342 | |
| 343 | // "nullPos" is the number of formal parameters at the end which |
| 344 | // effectively count as part of the variadic arguments. This is |
| 345 | // useful if you would prefer to not have *any* formal parameters, |
| 346 | // but the language forces you to have at least one. |
| 347 | unsigned nullPos = attr->getNullPos(); |
| 348 | assert((nullPos == 0 || nullPos == 1) && "invalid null position on sentinel"); |
| 349 | numFormalParams = (nullPos > numFormalParams ? 0 : numFormalParams - nullPos); |
| 350 | |
| 351 | // The number of arguments which should follow the sentinel. |
| 352 | unsigned numArgsAfterSentinel = attr->getSentinel(); |
| 353 | |
| 354 | // If there aren't enough arguments for all the formal parameters, |
| 355 | // the sentinel, and the args after the sentinel, complain. |
| 356 | if (numArgs < numFormalParams + numArgsAfterSentinel + 1) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 357 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 358 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 359 | return; |
| 360 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 361 | |
| 362 | // Otherwise, find the sentinel expression. |
| 363 | Expr *sentinelExpr = args[numArgs - numArgsAfterSentinel - 1]; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 364 | if (!sentinelExpr) return; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 365 | if (sentinelExpr->isValueDependent()) return; |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 366 | if (Context.isSentinelNullExpr(sentinelExpr)) return; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 367 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 368 | // Pick a reasonable string to insert. Optimistically use 'nil' or |
| 369 | // 'NULL' if those are actually defined in the context. Only use |
| 370 | // 'nil' for ObjC methods, where it's much more likely that the |
| 371 | // variadic arguments form a list of object pointers. |
| 372 | SourceLocation MissingNilLoc |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 373 | = PP.getLocForEndOfToken(sentinelExpr->getLocEnd()); |
| 374 | std::string NullValue; |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 375 | if (calleeType == CT_Method && |
| 376 | PP.getIdentifierInfo("nil")->hasMacroDefinition()) |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 377 | NullValue = "nil"; |
| 378 | else if (PP.getIdentifierInfo("NULL")->hasMacroDefinition()) |
| 379 | NullValue = "NULL"; |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 380 | else |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 381 | NullValue = "(void*) 0"; |
Eli Friedman | 39834ba | 2011-09-27 23:46:37 +0000 | [diff] [blame] | 382 | |
| 383 | if (MissingNilLoc.isInvalid()) |
| 384 | Diag(Loc, diag::warn_missing_sentinel) << calleeType; |
| 385 | else |
| 386 | Diag(MissingNilLoc, diag::warn_missing_sentinel) |
| 387 | << calleeType |
| 388 | << FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue); |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 389 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 392 | SourceRange Sema::getExprRange(Expr *E) const { |
| 393 | return E ? E->getSourceRange() : SourceRange(); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 396 | //===----------------------------------------------------------------------===// |
| 397 | // Standard Promotions and Conversions |
| 398 | //===----------------------------------------------------------------------===// |
| 399 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 400 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 401 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 402 | // Handle any placeholder expressions which made it here. |
| 403 | if (E->getType()->isPlaceholderType()) { |
| 404 | ExprResult result = CheckPlaceholderExpr(E); |
| 405 | if (result.isInvalid()) return ExprError(); |
| 406 | E = result.take(); |
| 407 | } |
| 408 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 409 | QualType Ty = E->getType(); |
| 410 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 411 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 412 | if (Ty->isFunctionType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 413 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 414 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 415 | else if (Ty->isArrayType()) { |
| 416 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 417 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 418 | // type 'array of type' is converted to an expression that has type 'pointer |
| 419 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 420 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 421 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 422 | // |
| 423 | // C++ 4.2p1: |
| 424 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 425 | // T" can be converted to an rvalue of type "pointer to T". |
| 426 | // |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 427 | if (getLangOpts().C99 || getLangOpts().CPlusPlus || E->isLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 428 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 429 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 430 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 431 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 434 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
| 435 | // Check to see if we are dereferencing a null pointer. If so, |
| 436 | // and if not volatile-qualified, this is undefined behavior that the |
| 437 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 438 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 439 | // only handles the pattern "*null", which is a very syntactic check. |
| 440 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 441 | if (UO->getOpcode() == UO_Deref && |
| 442 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 443 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && |
| 444 | !UO->getType().isVolatileQualified()) { |
| 445 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 446 | S.PDiag(diag::warn_indirection_through_null) |
| 447 | << UO->getSubExpr()->getSourceRange()); |
| 448 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 449 | S.PDiag(diag::note_indirection_through_null)); |
| 450 | } |
| 451 | } |
| 452 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 453 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 454 | // Handle any placeholder expressions which made it here. |
| 455 | if (E->getType()->isPlaceholderType()) { |
| 456 | ExprResult result = CheckPlaceholderExpr(E); |
| 457 | if (result.isInvalid()) return ExprError(); |
| 458 | E = result.take(); |
| 459 | } |
| 460 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 461 | // C++ [conv.lval]p1: |
| 462 | // A glvalue of a non-function, non-array type T can be |
| 463 | // converted to a prvalue. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 464 | if (!E->isGLValue()) return Owned(E); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 465 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 466 | QualType T = E->getType(); |
| 467 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 468 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 469 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 470 | // expressions of certain types in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 471 | if (getLangOpts().CPlusPlus && |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 472 | (E->getType() == Context.OverloadTy || |
| 473 | T->isDependentType() || |
| 474 | T->isRecordType())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 475 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 476 | |
| 477 | // The C standard is actually really unclear on this point, and |
| 478 | // DR106 tells us what the result should be but not why. It's |
| 479 | // generally best to say that void types just doesn't undergo |
| 480 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 481 | // 'void' type are never l-values, but qualified void can be. |
| 482 | if (T->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 483 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 484 | |
John McCall | 9dd74c5 | 2013-02-12 01:29:43 +0000 | [diff] [blame] | 485 | // OpenCL usually rejects direct accesses to values of 'half' type. |
| 486 | if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp16 && |
| 487 | T->isHalfType()) { |
| 488 | Diag(E->getExprLoc(), diag::err_opencl_half_load_store) |
| 489 | << 0 << T; |
| 490 | return ExprError(); |
| 491 | } |
| 492 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 493 | CheckForNullPointerDereference(*this, E); |
| 494 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 495 | // C++ [conv.lval]p1: |
| 496 | // [...] If T is a non-class type, the type of the prvalue is the |
| 497 | // cv-unqualified version of T. Otherwise, the type of the |
| 498 | // rvalue is T. |
| 499 | // |
| 500 | // C99 6.3.2.1p2: |
| 501 | // If the lvalue has qualified type, the value has the unqualified |
| 502 | // version of the type of the lvalue; otherwise, the value has the |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 503 | // type of the lvalue. |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 504 | if (T.hasQualifiers()) |
| 505 | T = T.getUnqualifiedType(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 506 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 507 | UpdateMarkingForLValueToRValue(E); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 508 | |
| 509 | // Loading a __weak object implicitly retains the value, so we need a cleanup to |
| 510 | // balance that. |
| 511 | if (getLangOpts().ObjCAutoRefCount && |
| 512 | E->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 513 | ExprNeedsCleanups = true; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 514 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 515 | ExprResult Res = Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 516 | E, 0, VK_RValue)); |
| 517 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 518 | // C11 6.3.2.1p2: |
| 519 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 520 | // of the type of the lvalue ... |
| 521 | if (const AtomicType *Atomic = T->getAs<AtomicType>()) { |
| 522 | T = Atomic->getValueType().getUnqualifiedType(); |
| 523 | Res = Owned(ImplicitCastExpr::Create(Context, T, CK_AtomicToNonAtomic, |
| 524 | Res.get(), 0, VK_RValue)); |
| 525 | } |
| 526 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 527 | return Res; |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 528 | } |
| 529 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 530 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 531 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 532 | if (Res.isInvalid()) |
| 533 | return ExprError(); |
| 534 | Res = DefaultLvalueConversion(Res.take()); |
| 535 | if (Res.isInvalid()) |
| 536 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 537 | return Res; |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 541 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 542 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 543 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 544 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 545 | /// In these instances, this routine should *not* be called. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 546 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 547 | // First, convert to an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 548 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 549 | if (Res.isInvalid()) |
| 550 | return Owned(E); |
| 551 | E = Res.take(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 552 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 553 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 554 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 555 | |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 556 | // Half FP have to be promoted to float unless it is natively supported |
| 557 | if (Ty->isHalfType() && !getLangOpts().NativeHalfType) |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 558 | return ImpCastExprToType(Res.take(), Context.FloatTy, CK_FloatingCast); |
| 559 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 560 | // Try to perform integral promotions if the object has a theoretically |
| 561 | // promotable type. |
| 562 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 563 | // C99 6.3.1.1p2: |
| 564 | // |
| 565 | // The following may be used in an expression wherever an int or |
| 566 | // unsigned int may be used: |
| 567 | // - an object or expression with an integer type whose integer |
| 568 | // conversion rank is less than or equal to the rank of int |
| 569 | // and unsigned int. |
| 570 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 571 | // |
| 572 | // If an int can represent all values of the original type, the |
| 573 | // value is converted to an int; otherwise, it is converted to an |
| 574 | // unsigned int. These are called the integer promotions. All |
| 575 | // other types are unchanged by the integer promotions. |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 576 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 577 | QualType PTy = Context.isPromotableBitField(E); |
| 578 | if (!PTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 579 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 580 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 581 | } |
| 582 | if (Ty->isPromotableIntegerType()) { |
| 583 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 584 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 585 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 586 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 587 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 588 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 591 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Tim Northover | e1ac4ae | 2013-01-30 09:46:55 +0000 | [diff] [blame] | 592 | /// do not have a prototype. Arguments that have type float or __fp16 |
| 593 | /// are promoted to double. All other argument types are converted by |
| 594 | /// UsualUnaryConversions(). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 595 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 596 | QualType Ty = E->getType(); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 597 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 599 | ExprResult Res = UsualUnaryConversions(E); |
| 600 | if (Res.isInvalid()) |
| 601 | return Owned(E); |
| 602 | E = Res.take(); |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 603 | |
Tim Northover | e1ac4ae | 2013-01-30 09:46:55 +0000 | [diff] [blame] | 604 | // If this is a 'float' or '__fp16' (CVR qualified or typedef) promote to |
| 605 | // double. |
| 606 | const BuiltinType *BTy = Ty->getAs<BuiltinType>(); |
| 607 | if (BTy && (BTy->getKind() == BuiltinType::Half || |
| 608 | BTy->getKind() == BuiltinType::Float)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 609 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 610 | |
John McCall | 96a914a | 2011-08-27 22:06:17 +0000 | [diff] [blame] | 611 | // C++ performs lvalue-to-rvalue conversion as a default argument |
John McCall | 709bca8 | 2011-08-29 23:55:37 +0000 | [diff] [blame] | 612 | // promotion, even on class types, but note: |
| 613 | // C++11 [conv.lval]p2: |
| 614 | // When an lvalue-to-rvalue conversion occurs in an unevaluated |
| 615 | // operand or a subexpression thereof the value contained in the |
| 616 | // referenced object is not accessed. Otherwise, if the glvalue |
| 617 | // has a class type, the conversion copy-initializes a temporary |
| 618 | // of type T from the glvalue and the result of the conversion |
| 619 | // is a prvalue for the temporary. |
Eli Friedman | 55693fb | 2012-01-17 02:13:45 +0000 | [diff] [blame] | 620 | // FIXME: add some way to gate this entire thing for correctness in |
| 621 | // potentially potentially evaluated contexts. |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 622 | if (getLangOpts().CPlusPlus && E->isGLValue() && !isUnevaluatedContext()) { |
Eli Friedman | 55693fb | 2012-01-17 02:13:45 +0000 | [diff] [blame] | 623 | ExprResult Temp = PerformCopyInitialization( |
| 624 | InitializedEntity::InitializeTemporary(E->getType()), |
| 625 | E->getExprLoc(), |
| 626 | Owned(E)); |
| 627 | if (Temp.isInvalid()) |
| 628 | return ExprError(); |
| 629 | E = Temp.get(); |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 630 | } |
| 631 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 632 | return Owned(E); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 635 | /// Determine the degree of POD-ness for an expression. |
| 636 | /// Incomplete types are considered POD, since this check can be performed |
| 637 | /// when we're in an unevaluated context. |
| 638 | Sema::VarArgKind Sema::isValidVarArgType(const QualType &Ty) { |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 639 | if (Ty->isIncompleteType()) { |
| 640 | if (Ty->isObjCObjectType()) |
| 641 | return VAK_Invalid; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 642 | return VAK_Valid; |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | if (Ty.isCXX98PODType(Context)) |
| 646 | return VAK_Valid; |
| 647 | |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 648 | // C++11 [expr.call]p7: |
| 649 | // Passing a potentially-evaluated argument of class type (Clause 9) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 650 | // having a non-trivial copy constructor, a non-trivial move constructor, |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 651 | // or a non-trivial destructor, with no corresponding parameter, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 652 | // is conditionally-supported with implementation-defined semantics. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 653 | if (getLangOpts().CPlusPlus11 && !Ty->isDependentType()) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 654 | if (CXXRecordDecl *Record = Ty->getAsCXXRecordDecl()) |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 655 | if (!Record->hasNonTrivialCopyConstructor() && |
| 656 | !Record->hasNonTrivialMoveConstructor() && |
| 657 | !Record->hasNonTrivialDestructor()) |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 658 | return VAK_ValidInCXX11; |
| 659 | |
| 660 | if (getLangOpts().ObjCAutoRefCount && Ty->isObjCLifetimeType()) |
| 661 | return VAK_Valid; |
| 662 | return VAK_Invalid; |
| 663 | } |
| 664 | |
| 665 | bool Sema::variadicArgumentPODCheck(const Expr *E, VariadicCallType CT) { |
| 666 | // Don't allow one to pass an Objective-C interface to a vararg. |
| 667 | const QualType & Ty = E->getType(); |
| 668 | |
| 669 | // Complain about passing non-POD types through varargs. |
| 670 | switch (isValidVarArgType(Ty)) { |
| 671 | case VAK_Valid: |
| 672 | break; |
| 673 | case VAK_ValidInCXX11: |
| 674 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 675 | PDiag(diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg) |
| 676 | << E->getType() << CT); |
| 677 | break; |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 678 | case VAK_Invalid: { |
| 679 | if (Ty->isObjCObjectType()) |
| 680 | return DiagRuntimeBehavior(E->getLocStart(), 0, |
| 681 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 682 | << Ty << CT); |
| 683 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 684 | return DiagRuntimeBehavior(E->getLocStart(), 0, |
| 685 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 686 | << getLangOpts().CPlusPlus11 << Ty << CT); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 687 | } |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 688 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 689 | // c++ rules are enforced elsewhere. |
| 690 | return false; |
| 691 | } |
| 692 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 693 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 694 | /// 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] | 695 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 696 | FunctionDecl *FDecl) { |
Richard Smith | e1971a1 | 2012-06-27 20:29:39 +0000 | [diff] [blame] | 697 | if (const BuiltinType *PlaceholderTy = E->getType()->getAsPlaceholderType()) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 698 | // Strip the unbridged-cast placeholder expression off, if applicable. |
| 699 | if (PlaceholderTy->getKind() == BuiltinType::ARCUnbridgedCast && |
| 700 | (CT == VariadicMethod || |
| 701 | (FDecl && FDecl->hasAttr<CFAuditedTransferAttr>()))) { |
| 702 | E = stripARCUnbridgedCast(E); |
| 703 | |
| 704 | // Otherwise, do normal placeholder checking. |
| 705 | } else { |
| 706 | ExprResult ExprRes = CheckPlaceholderExpr(E); |
| 707 | if (ExprRes.isInvalid()) |
| 708 | return ExprError(); |
| 709 | E = ExprRes.take(); |
| 710 | } |
| 711 | } |
Douglas Gregor | 8d5e18c | 2011-06-17 00:15:10 +0000 | [diff] [blame] | 712 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 713 | ExprResult ExprRes = DefaultArgumentPromotion(E); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 714 | if (ExprRes.isInvalid()) |
| 715 | return ExprError(); |
| 716 | E = ExprRes.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 718 | // Diagnostics regarding non-POD argument types are |
| 719 | // emitted along with format string checking in Sema::CheckFunctionCall(). |
Richard Smith | 83ea530 | 2012-06-27 20:23:58 +0000 | [diff] [blame] | 720 | if (isValidVarArgType(E->getType()) == VAK_Invalid) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 721 | // Turn this into a trap. |
| 722 | CXXScopeSpec SS; |
| 723 | SourceLocation TemplateKWLoc; |
| 724 | UnqualifiedId Name; |
| 725 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 726 | E->getLocStart()); |
| 727 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, TemplateKWLoc, |
| 728 | Name, true, false); |
| 729 | if (TrapFn.isInvalid()) |
| 730 | return ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 731 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 732 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), |
| 733 | E->getLocStart(), MultiExprArg(), |
| 734 | E->getLocEnd()); |
| 735 | if (Call.isInvalid()) |
| 736 | return ExprError(); |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 737 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 738 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 739 | Call.get(), E); |
| 740 | if (Comma.isInvalid()) |
| 741 | return ExprError(); |
| 742 | return Comma.get(); |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 743 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 744 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 745 | if (!getLangOpts().CPlusPlus && |
Fariborz Jahanian | e853bb3 | 2012-03-01 23:42:00 +0000 | [diff] [blame] | 746 | RequireCompleteType(E->getExprLoc(), E->getType(), |
Fariborz Jahanian | a0e005b | 2012-03-02 17:05:03 +0000 | [diff] [blame] | 747 | diag::err_call_incomplete_argument)) |
Fariborz Jahanian | e853bb3 | 2012-03-01 23:42:00 +0000 | [diff] [blame] | 748 | return ExprError(); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 749 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 750 | return Owned(E); |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 753 | /// \brief Converts an integer to complex float type. Helper function of |
| 754 | /// UsualArithmeticConversions() |
| 755 | /// |
| 756 | /// \return false if the integer expression is an integer type and is |
| 757 | /// successfully converted to the complex type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 758 | static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr, |
| 759 | ExprResult &ComplexExpr, |
| 760 | QualType IntTy, |
| 761 | QualType ComplexTy, |
| 762 | bool SkipCast) { |
| 763 | if (IntTy->isComplexType() || IntTy->isRealFloatingType()) return true; |
| 764 | if (SkipCast) return false; |
| 765 | if (IntTy->isIntegerType()) { |
| 766 | QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType(); |
| 767 | IntExpr = S.ImpCastExprToType(IntExpr.take(), fpTy, CK_IntegralToFloating); |
| 768 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 769 | CK_FloatingRealToComplex); |
| 770 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 771 | assert(IntTy->isComplexIntegerType()); |
| 772 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 773 | CK_IntegralComplexToFloatingComplex); |
| 774 | } |
| 775 | return false; |
| 776 | } |
| 777 | |
| 778 | /// \brief Takes two complex float types and converts them to the same type. |
| 779 | /// Helper function of UsualArithmeticConversions() |
| 780 | static QualType |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 781 | handleComplexFloatToComplexFloatConverstion(Sema &S, ExprResult &LHS, |
| 782 | ExprResult &RHS, QualType LHSType, |
| 783 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 784 | bool IsCompAssign) { |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 785 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 786 | |
| 787 | if (order < 0) { |
| 788 | // _Complex float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 789 | if (!IsCompAssign) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 790 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingComplexCast); |
| 791 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 792 | } |
| 793 | if (order > 0) |
| 794 | // _Complex float -> _Complex double |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 795 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingComplexCast); |
| 796 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /// \brief Converts otherExpr to complex float and promotes complexExpr if |
| 800 | /// necessary. Helper function of UsualArithmeticConversions() |
| 801 | static QualType handleOtherComplexFloatConversion(Sema &S, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 802 | ExprResult &ComplexExpr, |
| 803 | ExprResult &OtherExpr, |
| 804 | QualType ComplexTy, |
| 805 | QualType OtherTy, |
| 806 | bool ConvertComplexExpr, |
| 807 | bool ConvertOtherExpr) { |
| 808 | int order = S.Context.getFloatingTypeOrder(ComplexTy, OtherTy); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 809 | |
| 810 | // If just the complexExpr is complex, the otherExpr needs to be converted, |
| 811 | // and the complexExpr might need to be promoted. |
| 812 | if (order > 0) { // complexExpr is wider |
| 813 | // float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 814 | if (ConvertOtherExpr) { |
| 815 | QualType fp = cast<ComplexType>(ComplexTy)->getElementType(); |
| 816 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), fp, CK_FloatingCast); |
| 817 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 818 | CK_FloatingRealToComplex); |
| 819 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 820 | return ComplexTy; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | // otherTy is at least as wide. Find its corresponding complex type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 824 | QualType result = (order == 0 ? ComplexTy : |
| 825 | S.Context.getComplexType(OtherTy)); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 826 | |
| 827 | // double -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 828 | if (ConvertOtherExpr) |
| 829 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 830 | CK_FloatingRealToComplex); |
| 831 | |
| 832 | // _Complex float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 833 | if (ConvertComplexExpr && order < 0) |
| 834 | ComplexExpr = S.ImpCastExprToType(ComplexExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 835 | CK_FloatingComplexCast); |
| 836 | |
| 837 | return result; |
| 838 | } |
| 839 | |
| 840 | /// \brief Handle arithmetic conversion with complex types. Helper function of |
| 841 | /// UsualArithmeticConversions() |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 842 | static QualType handleComplexFloatConversion(Sema &S, ExprResult &LHS, |
| 843 | ExprResult &RHS, QualType LHSType, |
| 844 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 845 | bool IsCompAssign) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 846 | // if we have an integer operand, the result is the complex type. |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 847 | if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 848 | /*skipCast*/false)) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 849 | return LHSType; |
| 850 | if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 851 | /*skipCast*/IsCompAssign)) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 852 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 853 | |
| 854 | // This handles complex/complex, complex/float, or float/complex. |
| 855 | // When both operands are complex, the shorter operand is converted to the |
| 856 | // type of the longer, and that is the type of the result. This corresponds |
| 857 | // to what is done when combining two real floating-point operands. |
| 858 | // The fun begins when size promotion occur across type domains. |
| 859 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 860 | // floating-point type, the less precise type is converted, within it's |
| 861 | // real or complex domain, to the precision of the other type. For example, |
| 862 | // when combining a "long double" with a "double _Complex", the |
| 863 | // "double _Complex" is promoted to "long double _Complex". |
| 864 | |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 865 | bool LHSComplexFloat = LHSType->isComplexType(); |
| 866 | bool RHSComplexFloat = RHSType->isComplexType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 867 | |
| 868 | // If both are complex, just cast to the more precise type. |
| 869 | if (LHSComplexFloat && RHSComplexFloat) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 870 | return handleComplexFloatToComplexFloatConverstion(S, LHS, RHS, |
| 871 | LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 872 | IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 873 | |
| 874 | // If only one operand is complex, promote it if necessary and convert the |
| 875 | // other operand to complex. |
| 876 | if (LHSComplexFloat) |
| 877 | return handleOtherComplexFloatConversion( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 878 | S, LHS, RHS, LHSType, RHSType, /*convertComplexExpr*/!IsCompAssign, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 879 | /*convertOtherExpr*/ true); |
| 880 | |
| 881 | assert(RHSComplexFloat); |
| 882 | return handleOtherComplexFloatConversion( |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 883 | S, RHS, LHS, RHSType, LHSType, /*convertComplexExpr*/true, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 884 | /*convertOtherExpr*/ !IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /// \brief Hande arithmetic conversion from integer to float. Helper function |
| 888 | /// of UsualArithmeticConversions() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 889 | static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, |
| 890 | ExprResult &IntExpr, |
| 891 | QualType FloatTy, QualType IntTy, |
| 892 | bool ConvertFloat, bool ConvertInt) { |
| 893 | if (IntTy->isIntegerType()) { |
| 894 | if (ConvertInt) |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 895 | // Convert intExpr to the lhs floating point type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 896 | IntExpr = S.ImpCastExprToType(IntExpr.take(), FloatTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 897 | CK_IntegralToFloating); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 898 | return FloatTy; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | // Convert both sides to the appropriate complex float. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 902 | assert(IntTy->isComplexIntegerType()); |
| 903 | QualType result = S.Context.getComplexType(FloatTy); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 904 | |
| 905 | // _Complex int -> _Complex float |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 906 | if (ConvertInt) |
| 907 | IntExpr = S.ImpCastExprToType(IntExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 908 | CK_IntegralComplexToFloatingComplex); |
| 909 | |
| 910 | // float -> _Complex float |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 911 | if (ConvertFloat) |
| 912 | FloatExpr = S.ImpCastExprToType(FloatExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 913 | CK_FloatingRealToComplex); |
| 914 | |
| 915 | return result; |
| 916 | } |
| 917 | |
| 918 | /// \brief Handle arithmethic conversion with floating point types. Helper |
| 919 | /// function of UsualArithmeticConversions() |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 920 | static QualType handleFloatConversion(Sema &S, ExprResult &LHS, |
| 921 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 922 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 923 | bool LHSFloat = LHSType->isRealFloatingType(); |
| 924 | bool RHSFloat = RHSType->isRealFloatingType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 925 | |
| 926 | // If we have two real floating types, convert the smaller operand |
| 927 | // to the bigger result. |
| 928 | if (LHSFloat && RHSFloat) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 929 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 930 | if (order > 0) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 931 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingCast); |
| 932 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | assert(order < 0 && "illegal float comparison"); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 936 | if (!IsCompAssign) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 937 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingCast); |
| 938 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | if (LHSFloat) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 942 | return handleIntToFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 943 | /*convertFloat=*/!IsCompAssign, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 944 | /*convertInt=*/ true); |
| 945 | assert(RHSFloat); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 946 | return handleIntToFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 947 | /*convertInt=*/ true, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 948 | /*convertFloat=*/!IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 951 | typedef ExprResult PerformCastFn(Sema &S, Expr *operand, QualType toType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 952 | |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 953 | namespace { |
| 954 | /// These helper callbacks are placed in an anonymous namespace to |
| 955 | /// permit their use as function template parameters. |
| 956 | ExprResult doIntegralCast(Sema &S, Expr *op, QualType toType) { |
| 957 | return S.ImpCastExprToType(op, toType, CK_IntegralCast); |
| 958 | } |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 959 | |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 960 | ExprResult doComplexIntegralCast(Sema &S, Expr *op, QualType toType) { |
| 961 | return S.ImpCastExprToType(op, S.Context.getComplexType(toType), |
| 962 | CK_IntegralComplexCast); |
| 963 | } |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /// \brief Handle integer arithmetic conversions. Helper function of |
| 967 | /// UsualArithmeticConversions() |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 968 | template <PerformCastFn doLHSCast, PerformCastFn doRHSCast> |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 969 | static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, |
| 970 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 971 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 972 | // The rules for this case are in C99 6.3.1.8 |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 973 | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); |
| 974 | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); |
| 975 | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); |
| 976 | if (LHSSigned == RHSSigned) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 977 | // Same signedness; use the higher-ranked type |
| 978 | if (order >= 0) { |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 979 | RHS = (*doRHSCast)(S, RHS.take(), LHSType); |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 980 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 981 | } else if (!IsCompAssign) |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 982 | LHS = (*doLHSCast)(S, LHS.take(), RHSType); |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 983 | return RHSType; |
| 984 | } else if (order != (LHSSigned ? 1 : -1)) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 985 | // The unsigned type has greater than or equal rank to the |
| 986 | // signed type, so use the unsigned type |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 987 | if (RHSSigned) { |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 988 | RHS = (*doRHSCast)(S, RHS.take(), LHSType); |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 989 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 990 | } else if (!IsCompAssign) |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 991 | LHS = (*doLHSCast)(S, LHS.take(), RHSType); |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 992 | return RHSType; |
| 993 | } else if (S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 994 | // The two types are different widths; if we are here, that |
| 995 | // means the signed type is larger than the unsigned type, so |
| 996 | // use the signed type. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 997 | if (LHSSigned) { |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 998 | RHS = (*doRHSCast)(S, RHS.take(), LHSType); |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 999 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1000 | } else if (!IsCompAssign) |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 1001 | LHS = (*doLHSCast)(S, LHS.take(), RHSType); |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1002 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 1003 | } else { |
| 1004 | // The signed type is higher-ranked than the unsigned type, |
| 1005 | // but isn't actually any bigger (like unsigned int and long |
| 1006 | // on most 32-bit systems). Use the unsigned type corresponding |
| 1007 | // to the signed type. |
| 1008 | QualType result = |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1009 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 1010 | RHS = (*doRHSCast)(S, RHS.take(), result); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1011 | if (!IsCompAssign) |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 1012 | LHS = (*doLHSCast)(S, LHS.take(), result); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 1013 | return result; |
| 1014 | } |
| 1015 | } |
| 1016 | |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 1017 | /// \brief Handle conversions with GCC complex int extension. Helper function |
| 1018 | /// of UsualArithmeticConversions() |
| 1019 | static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, |
| 1020 | ExprResult &RHS, QualType LHSType, |
| 1021 | QualType RHSType, |
| 1022 | bool IsCompAssign) { |
| 1023 | const ComplexType *LHSComplexInt = LHSType->getAsComplexIntegerType(); |
| 1024 | const ComplexType *RHSComplexInt = RHSType->getAsComplexIntegerType(); |
| 1025 | |
| 1026 | if (LHSComplexInt && RHSComplexInt) { |
| 1027 | QualType LHSEltType = LHSComplexInt->getElementType(); |
| 1028 | QualType RHSEltType = RHSComplexInt->getElementType(); |
| 1029 | QualType ScalarType = |
| 1030 | handleIntegerConversion<doComplexIntegralCast, doComplexIntegralCast> |
| 1031 | (S, LHS, RHS, LHSEltType, RHSEltType, IsCompAssign); |
| 1032 | |
| 1033 | return S.Context.getComplexType(ScalarType); |
| 1034 | } |
| 1035 | |
| 1036 | if (LHSComplexInt) { |
| 1037 | QualType LHSEltType = LHSComplexInt->getElementType(); |
| 1038 | QualType ScalarType = |
| 1039 | handleIntegerConversion<doComplexIntegralCast, doIntegralCast> |
| 1040 | (S, LHS, RHS, LHSEltType, RHSType, IsCompAssign); |
| 1041 | QualType ComplexType = S.Context.getComplexType(ScalarType); |
| 1042 | RHS = S.ImpCastExprToType(RHS.take(), ComplexType, |
| 1043 | CK_IntegralRealToComplex); |
| 1044 | |
| 1045 | return ComplexType; |
| 1046 | } |
| 1047 | |
| 1048 | assert(RHSComplexInt); |
| 1049 | |
| 1050 | QualType RHSEltType = RHSComplexInt->getElementType(); |
| 1051 | QualType ScalarType = |
| 1052 | handleIntegerConversion<doIntegralCast, doComplexIntegralCast> |
| 1053 | (S, LHS, RHS, LHSType, RHSEltType, IsCompAssign); |
| 1054 | QualType ComplexType = S.Context.getComplexType(ScalarType); |
| 1055 | |
| 1056 | if (!IsCompAssign) |
| 1057 | LHS = S.ImpCastExprToType(LHS.take(), ComplexType, |
| 1058 | CK_IntegralRealToComplex); |
| 1059 | return ComplexType; |
| 1060 | } |
| 1061 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1062 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 1063 | /// 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] | 1064 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1065 | /// responsible for emitting appropriate error diagnostics. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1066 | QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1067 | bool IsCompAssign) { |
| 1068 | if (!IsCompAssign) { |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1069 | LHS = UsualUnaryConversions(LHS.take()); |
| 1070 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1071 | return QualType(); |
| 1072 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1073 | |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1074 | RHS = UsualUnaryConversions(RHS.take()); |
| 1075 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1076 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1077 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1079 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1080 | QualType LHSType = |
| 1081 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 1082 | QualType RHSType = |
| 1083 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1084 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1085 | // For conversion purposes, we ignore any atomic qualifier on the LHS. |
| 1086 | if (const AtomicType *AtomicLHS = LHSType->getAs<AtomicType>()) |
| 1087 | LHSType = AtomicLHS->getValueType(); |
| 1088 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1089 | // If both types are identical, no conversion is needed. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1090 | if (LHSType == RHSType) |
| 1091 | return LHSType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1092 | |
| 1093 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 1094 | // The caller can deal with this (e.g. pointer + int). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1095 | if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType()) |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1096 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1097 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1098 | // Apply unary and bitfield promotions to the LHS's type. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1099 | QualType LHSUnpromotedType = LHSType; |
| 1100 | if (LHSType->isPromotableIntegerType()) |
| 1101 | LHSType = Context.getPromotedIntegerType(LHSType); |
| 1102 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(LHS.get()); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 1103 | if (!LHSBitfieldPromoteTy.isNull()) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1104 | LHSType = LHSBitfieldPromoteTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1105 | if (LHSType != LHSUnpromotedType && !IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1106 | LHS = ImpCastExprToType(LHS.take(), LHSType, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 1107 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1108 | // If both types are identical, no conversion is needed. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1109 | if (LHSType == RHSType) |
| 1110 | return LHSType; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1111 | |
| 1112 | // At this point, we have two different arithmetic types. |
| 1113 | |
| 1114 | // Handle complex types first (C99 6.3.1.8p1). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1115 | if (LHSType->isComplexType() || RHSType->isComplexType()) |
| 1116 | return handleComplexFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1117 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1118 | |
| 1119 | // Now handle "real" floating types (i.e. float, double, long double). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1120 | if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) |
| 1121 | return handleFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1122 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1123 | |
| 1124 | // Handle GCC complex int extension. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1125 | if (LHSType->isComplexIntegerType() || RHSType->isComplexIntegerType()) |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 1126 | return handleComplexIntConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1127 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1128 | |
| 1129 | // Finally, we have two differing integer types. |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 1130 | return handleIntegerConversion<doIntegralCast, doIntegralCast> |
| 1131 | (*this, LHS, RHS, LHSType, RHSType, IsCompAssign); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Bill Schmidt | 57dab71 | 2013-02-01 15:34:29 +0000 | [diff] [blame] | 1134 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1135 | //===----------------------------------------------------------------------===// |
| 1136 | // Semantic Analysis for various Expression Types |
| 1137 | //===----------------------------------------------------------------------===// |
| 1138 | |
| 1139 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1140 | ExprResult |
| 1141 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 1142 | SourceLocation DefaultLoc, |
| 1143 | SourceLocation RParenLoc, |
| 1144 | Expr *ControllingExpr, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1145 | MultiTypeArg ArgTypes, |
| 1146 | MultiExprArg ArgExprs) { |
| 1147 | unsigned NumAssocs = ArgTypes.size(); |
| 1148 | assert(NumAssocs == ArgExprs.size()); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1149 | |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1150 | ParsedType *ParsedTypes = ArgTypes.data(); |
| 1151 | Expr **Exprs = ArgExprs.data(); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1152 | |
| 1153 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 1154 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1155 | if (ParsedTypes[i]) |
| 1156 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 1157 | else |
| 1158 | Types[i] = 0; |
| 1159 | } |
| 1160 | |
| 1161 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 1162 | ControllingExpr, Types, Exprs, |
| 1163 | NumAssocs); |
Benjamin Kramer | 5bf47f7 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 1164 | delete [] Types; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1165 | return ER; |
| 1166 | } |
| 1167 | |
| 1168 | ExprResult |
| 1169 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 1170 | SourceLocation DefaultLoc, |
| 1171 | SourceLocation RParenLoc, |
| 1172 | Expr *ControllingExpr, |
| 1173 | TypeSourceInfo **Types, |
| 1174 | Expr **Exprs, |
| 1175 | unsigned NumAssocs) { |
John McCall | a2905ea | 2013-02-12 02:08:12 +0000 | [diff] [blame] | 1176 | if (ControllingExpr->getType()->isPlaceholderType()) { |
| 1177 | ExprResult result = CheckPlaceholderExpr(ControllingExpr); |
| 1178 | if (result.isInvalid()) return ExprError(); |
| 1179 | ControllingExpr = result.take(); |
| 1180 | } |
| 1181 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1182 | bool TypeErrorFound = false, |
| 1183 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 1184 | ContainsUnexpandedParameterPack |
| 1185 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 1186 | |
| 1187 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1188 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 1189 | ContainsUnexpandedParameterPack = true; |
| 1190 | |
| 1191 | if (Types[i]) { |
| 1192 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 1193 | ContainsUnexpandedParameterPack = true; |
| 1194 | |
| 1195 | if (Types[i]->getType()->isDependentType()) { |
| 1196 | IsResultDependent = true; |
| 1197 | } else { |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1198 | // 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] | 1199 | // complete object type other than a variably modified type." |
| 1200 | unsigned D = 0; |
| 1201 | if (Types[i]->getType()->isIncompleteType()) |
| 1202 | D = diag::err_assoc_type_incomplete; |
| 1203 | else if (!Types[i]->getType()->isObjectType()) |
| 1204 | D = diag::err_assoc_type_nonobject; |
| 1205 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 1206 | D = diag::err_assoc_type_variably_modified; |
| 1207 | |
| 1208 | if (D != 0) { |
| 1209 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 1210 | << Types[i]->getTypeLoc().getSourceRange() |
| 1211 | << Types[i]->getType(); |
| 1212 | TypeErrorFound = true; |
| 1213 | } |
| 1214 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1215 | // 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] | 1216 | // selection shall specify compatible types." |
| 1217 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 1218 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 1219 | Context.typesAreCompatible(Types[i]->getType(), |
| 1220 | Types[j]->getType())) { |
| 1221 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 1222 | diag::err_assoc_compatible_types) |
| 1223 | << Types[j]->getTypeLoc().getSourceRange() |
| 1224 | << Types[j]->getType() |
| 1225 | << Types[i]->getType(); |
| 1226 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 1227 | diag::note_compat_assoc) |
| 1228 | << Types[i]->getTypeLoc().getSourceRange() |
| 1229 | << Types[i]->getType(); |
| 1230 | TypeErrorFound = true; |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | if (TypeErrorFound) |
| 1236 | return ExprError(); |
| 1237 | |
| 1238 | // If we determined that the generic selection is result-dependent, don't |
| 1239 | // try to compute the result expression. |
| 1240 | if (IsResultDependent) |
| 1241 | return Owned(new (Context) GenericSelectionExpr( |
| 1242 | Context, KeyLoc, ControllingExpr, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1243 | llvm::makeArrayRef(Types, NumAssocs), |
| 1244 | llvm::makeArrayRef(Exprs, NumAssocs), |
| 1245 | DefaultLoc, RParenLoc, ContainsUnexpandedParameterPack)); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1246 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1247 | SmallVector<unsigned, 1> CompatIndices; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1248 | unsigned DefaultIndex = -1U; |
| 1249 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1250 | if (!Types[i]) |
| 1251 | DefaultIndex = i; |
| 1252 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 1253 | Types[i]->getType())) |
| 1254 | CompatIndices.push_back(i); |
| 1255 | } |
| 1256 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1257 | // 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] | 1258 | // type compatible with at most one of the types named in its generic |
| 1259 | // association list." |
| 1260 | if (CompatIndices.size() > 1) { |
| 1261 | // We strip parens here because the controlling expression is typically |
| 1262 | // parenthesized in macro definitions. |
| 1263 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1264 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 1265 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 1266 | << (unsigned) CompatIndices.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1267 | for (SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1268 | E = CompatIndices.end(); I != E; ++I) { |
| 1269 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 1270 | diag::note_compat_assoc) |
| 1271 | << Types[*I]->getTypeLoc().getSourceRange() |
| 1272 | << Types[*I]->getType(); |
| 1273 | } |
| 1274 | return ExprError(); |
| 1275 | } |
| 1276 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1277 | // 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] | 1278 | // its controlling expression shall have type compatible with exactly one of |
| 1279 | // the types named in its generic association list." |
| 1280 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 1281 | // We strip parens here because the controlling expression is typically |
| 1282 | // parenthesized in macro definitions. |
| 1283 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1284 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 1285 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 1286 | return ExprError(); |
| 1287 | } |
| 1288 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1289 | // 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] | 1290 | // type name that is compatible with the type of the controlling expression, |
| 1291 | // then the result expression of the generic selection is the expression |
| 1292 | // in that generic association. Otherwise, the result expression of the |
| 1293 | // generic selection is the expression in the default generic association." |
| 1294 | unsigned ResultIndex = |
| 1295 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 1296 | |
| 1297 | return Owned(new (Context) GenericSelectionExpr( |
| 1298 | Context, KeyLoc, ControllingExpr, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1299 | llvm::makeArrayRef(Types, NumAssocs), |
| 1300 | llvm::makeArrayRef(Exprs, NumAssocs), |
| 1301 | DefaultLoc, RParenLoc, ContainsUnexpandedParameterPack, |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1302 | ResultIndex)); |
| 1303 | } |
| 1304 | |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1305 | /// getUDSuffixLoc - Create a SourceLocation for a ud-suffix, given the |
| 1306 | /// location of the token and the offset of the ud-suffix within it. |
| 1307 | static SourceLocation getUDSuffixLoc(Sema &S, SourceLocation TokLoc, |
| 1308 | unsigned Offset) { |
| 1309 | return Lexer::AdvanceToTokenCharacter(TokLoc, Offset, S.getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1310 | S.getLangOpts()); |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1313 | /// BuildCookedLiteralOperatorCall - A user-defined literal was found. Look up |
| 1314 | /// the corresponding cooked (non-raw) literal operator, and build a call to it. |
| 1315 | static ExprResult BuildCookedLiteralOperatorCall(Sema &S, Scope *Scope, |
| 1316 | IdentifierInfo *UDSuffix, |
| 1317 | SourceLocation UDSuffixLoc, |
| 1318 | ArrayRef<Expr*> Args, |
| 1319 | SourceLocation LitEndLoc) { |
| 1320 | assert(Args.size() <= 2 && "too many arguments for literal operator"); |
| 1321 | |
| 1322 | QualType ArgTy[2]; |
| 1323 | for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { |
| 1324 | ArgTy[ArgIdx] = Args[ArgIdx]->getType(); |
| 1325 | if (ArgTy[ArgIdx]->isArrayType()) |
| 1326 | ArgTy[ArgIdx] = S.Context.getArrayDecayedType(ArgTy[ArgIdx]); |
| 1327 | } |
| 1328 | |
| 1329 | DeclarationName OpName = |
| 1330 | S.Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); |
| 1331 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); |
| 1332 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); |
| 1333 | |
| 1334 | LookupResult R(S, OpName, UDSuffixLoc, Sema::LookupOrdinaryName); |
| 1335 | if (S.LookupLiteralOperator(Scope, R, llvm::makeArrayRef(ArgTy, Args.size()), |
| 1336 | /*AllowRawAndTemplate*/false) == Sema::LOLR_Error) |
| 1337 | return ExprError(); |
| 1338 | |
| 1339 | return S.BuildLiteralOperatorCall(R, OpNameInfo, Args, LitEndLoc); |
| 1340 | } |
| 1341 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1342 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1343 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 1344 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 1345 | /// multiple tokens. However, the common case is that StringToks points to one |
| 1346 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1347 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1348 | ExprResult |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1349 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks, |
| 1350 | Scope *UDLScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1351 | assert(NumStringToks && "Must have at least one string!"); |
| 1352 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1353 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1355 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1356 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1357 | SmallVector<SourceLocation, 4> StringTokLocs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1358 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 1359 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1360 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1361 | QualType StrTy = Context.CharTy; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1362 | if (Literal.isWide()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1363 | StrTy = Context.getWCharType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1364 | else if (Literal.isUTF16()) |
| 1365 | StrTy = Context.Char16Ty; |
| 1366 | else if (Literal.isUTF32()) |
| 1367 | StrTy = Context.Char32Ty; |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 1368 | else if (Literal.isPascal()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1369 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1370 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1371 | StringLiteral::StringKind Kind = StringLiteral::Ascii; |
| 1372 | if (Literal.isWide()) |
| 1373 | Kind = StringLiteral::Wide; |
| 1374 | else if (Literal.isUTF8()) |
| 1375 | Kind = StringLiteral::UTF8; |
| 1376 | else if (Literal.isUTF16()) |
| 1377 | Kind = StringLiteral::UTF16; |
| 1378 | else if (Literal.isUTF32()) |
| 1379 | Kind = StringLiteral::UTF32; |
| 1380 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1381 | // 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] | 1382 | if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1383 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1385 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 1386 | // the nul terminator character as well as the string length for pascal |
| 1387 | // strings. |
| 1388 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1389 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1390 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1391 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1392 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1393 | StringLiteral *Lit = StringLiteral::Create(Context, Literal.GetString(), |
| 1394 | Kind, Literal.Pascal, StrTy, |
| 1395 | &StringTokLocs[0], |
| 1396 | StringTokLocs.size()); |
| 1397 | if (Literal.getUDSuffix().empty()) |
| 1398 | return Owned(Lit); |
| 1399 | |
| 1400 | // We're building a user-defined literal. |
| 1401 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1402 | SourceLocation UDSuffixLoc = |
| 1403 | getUDSuffixLoc(*this, StringTokLocs[Literal.getUDSuffixToken()], |
| 1404 | Literal.getUDSuffixOffset()); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1405 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1406 | // Make sure we're allowed user-defined literals here. |
| 1407 | if (!UDLScope) |
| 1408 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_string_udl)); |
| 1409 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1410 | // C++11 [lex.ext]p5: The literal L is treated as a call of the form |
| 1411 | // operator "" X (str, len) |
| 1412 | QualType SizeType = Context.getSizeType(); |
| 1413 | llvm::APInt Len(Context.getIntWidth(SizeType), Literal.GetNumStringChars()); |
| 1414 | IntegerLiteral *LenArg = IntegerLiteral::Create(Context, Len, SizeType, |
| 1415 | StringTokLocs[0]); |
| 1416 | Expr *Args[] = { Lit, LenArg }; |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1417 | return BuildCookedLiteralOperatorCall(*this, UDLScope, UDSuffix, UDSuffixLoc, |
| 1418 | Args, StringTokLocs.back()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1421 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1422 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1423 | SourceLocation Loc, |
| 1424 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1425 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1426 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1429 | /// BuildDeclRefExpr - Build an expression that references a |
| 1430 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1431 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1432 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1433 | const DeclarationNameInfo &NameInfo, |
| 1434 | const CXXScopeSpec *SS) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1435 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 1436 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 1437 | if (const FunctionDecl *Callee = dyn_cast<FunctionDecl>(D)) { |
| 1438 | CUDAFunctionTarget CallerTarget = IdentifyCUDATarget(Caller), |
| 1439 | CalleeTarget = IdentifyCUDATarget(Callee); |
| 1440 | if (CheckCUDATarget(CallerTarget, CalleeTarget)) { |
| 1441 | Diag(NameInfo.getLoc(), diag::err_ref_bad_target) |
| 1442 | << CalleeTarget << D->getIdentifier() << CallerTarget; |
| 1443 | Diag(D->getLocation(), diag::note_previous_decl) |
| 1444 | << D->getIdentifier(); |
| 1445 | return ExprError(); |
| 1446 | } |
| 1447 | } |
| 1448 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1449 | bool refersToEnclosingScope = |
| 1450 | (CurContext != D->getDeclContext() && |
| 1451 | D->getDeclContext()->isFunctionOrMethod()); |
| 1452 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1453 | DeclRefExpr *E = DeclRefExpr::Create(Context, |
| 1454 | SS ? SS->getWithLocInContext(Context) |
| 1455 | : NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1456 | SourceLocation(), |
| 1457 | D, refersToEnclosingScope, |
| 1458 | NameInfo, Ty, VK); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1460 | MarkDeclRefReferenced(E); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1461 | |
Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1462 | if (getLangOpts().ObjCARCWeak && isa<VarDecl>(D) && |
| 1463 | Ty.getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 1464 | DiagnosticsEngine::Level Level = |
| 1465 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, |
| 1466 | E->getLocStart()); |
| 1467 | if (Level != DiagnosticsEngine::Ignored) |
| 1468 | getCurFunction()->recordUseOfWeak(E); |
| 1469 | } |
| 1470 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1471 | // Just in case we're building an illegal pointer-to-member. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1472 | FieldDecl *FD = dyn_cast<FieldDecl>(D); |
| 1473 | if (FD && FD->isBitField()) |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1474 | E->setObjectKind(OK_BitField); |
| 1475 | |
| 1476 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1479 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1480 | /// possibly a list of template arguments. |
| 1481 | /// |
| 1482 | /// If this produces template arguments, it is permitted to call |
| 1483 | /// DecomposeTemplateName. |
| 1484 | /// |
| 1485 | /// This actually loses a lot of source location information for |
| 1486 | /// non-standard name kinds; we should consider preserving that in |
| 1487 | /// some way. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1488 | void |
| 1489 | Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, |
| 1490 | TemplateArgumentListInfo &Buffer, |
| 1491 | DeclarationNameInfo &NameInfo, |
| 1492 | const TemplateArgumentListInfo *&TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1493 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1494 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1495 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1496 | |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1497 | ASTTemplateArgsPtr TemplateArgsPtr(Id.TemplateId->getTemplateArgs(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1498 | Id.TemplateId->NumArgs); |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1499 | translateTemplateArguments(TemplateArgsPtr, Buffer); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1500 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1501 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1502 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1503 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1504 | TemplateArgs = &Buffer; |
| 1505 | } else { |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1506 | NameInfo = GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1507 | TemplateArgs = 0; |
| 1508 | } |
| 1509 | } |
| 1510 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1511 | /// Diagnose an empty lookup. |
| 1512 | /// |
| 1513 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1514 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 1515 | CorrectionCandidateCallback &CCC, |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1516 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1517 | llvm::ArrayRef<Expr *> Args) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1518 | DeclarationName Name = R.getLookupName(); |
| 1519 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1520 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1521 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1522 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1523 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1524 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1525 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1526 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1527 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1528 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1529 | // If the original lookup was an unqualified lookup, fake an |
| 1530 | // unqualified lookup. This is useful when (for example) the |
| 1531 | // original lookup would not have found something because it was a |
| 1532 | // dependent name. |
David Blaikie | 4872e10 | 2012-05-28 01:26:45 +0000 | [diff] [blame] | 1533 | DeclContext *DC = (SS.isEmpty() && !CallsUndergoingInstantiation.empty()) |
| 1534 | ? CurContext : 0; |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1535 | while (DC) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1536 | if (isa<CXXRecordDecl>(DC)) { |
| 1537 | LookupQualifiedName(R, DC); |
| 1538 | |
| 1539 | if (!R.empty()) { |
| 1540 | // Don't give errors about ambiguities in this lookup. |
| 1541 | R.suppressDiagnostics(); |
| 1542 | |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1543 | // During a default argument instantiation the CurContext points |
| 1544 | // to a CXXMethodDecl; but we can't apply a this-> fixit inside a |
| 1545 | // function parameter list, hence add an explicit check. |
| 1546 | bool isDefaultArgument = !ActiveTemplateInstantiations.empty() && |
| 1547 | ActiveTemplateInstantiations.back().Kind == |
| 1548 | ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1549 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1550 | bool isInstance = CurMethod && |
| 1551 | CurMethod->isInstance() && |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1552 | DC == CurMethod->getParent() && !isDefaultArgument; |
| 1553 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1554 | |
| 1555 | // Give a code modification hint to insert 'this->'. |
| 1556 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1557 | // Actually quite difficult! |
Nico Weber | 4b554f4 | 2012-06-20 20:21:42 +0000 | [diff] [blame] | 1558 | if (getLangOpts().MicrosoftMode) |
| 1559 | diagnostic = diag::warn_found_via_dependent_bases_lookup; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1560 | if (isInstance) { |
Nico Weber | 94c4d61 | 2012-06-22 16:39:39 +0000 | [diff] [blame] | 1561 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1562 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1563 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1564 | CallsUndergoingInstantiation.back()->getCallee()); |
Nico Weber | 94c4d61 | 2012-06-22 16:39:39 +0000 | [diff] [blame] | 1565 | |
| 1566 | |
| 1567 | CXXMethodDecl *DepMethod; |
| 1568 | if (CurMethod->getTemplatedKind() == |
| 1569 | FunctionDecl::TK_FunctionTemplateSpecialization) |
| 1570 | DepMethod = cast<CXXMethodDecl>(CurMethod->getPrimaryTemplate()-> |
| 1571 | getInstantiatedFromMemberTemplate()->getTemplatedDecl()); |
| 1572 | else |
| 1573 | DepMethod = cast<CXXMethodDecl>( |
| 1574 | CurMethod->getInstantiatedFromMemberFunction()); |
| 1575 | assert(DepMethod && "No template pattern found"); |
| 1576 | |
| 1577 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1578 | CheckCXXThisCapture(R.getNameLoc()); |
| 1579 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1580 | R.getNameLoc(), DepThisType, false); |
| 1581 | TemplateArgumentListInfo TList; |
| 1582 | if (ULE->hasExplicitTemplateArgs()) |
| 1583 | ULE->copyTemplateArgumentsInto(TList); |
| 1584 | |
| 1585 | CXXScopeSpec SS; |
| 1586 | SS.Adopt(ULE->getQualifierLoc()); |
| 1587 | CXXDependentScopeMemberExpr *DepExpr = |
| 1588 | CXXDependentScopeMemberExpr::Create( |
| 1589 | Context, DepThis, DepThisType, true, SourceLocation(), |
| 1590 | SS.getWithLocInContext(Context), |
| 1591 | ULE->getTemplateKeywordLoc(), 0, |
| 1592 | R.getLookupNameInfo(), |
| 1593 | ULE->hasExplicitTemplateArgs() ? &TList : 0); |
| 1594 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1595 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1596 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1597 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1598 | |
| 1599 | // Do we really want to note all of these? |
| 1600 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1601 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1602 | |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1603 | // Return true if we are inside a default argument instantiation |
| 1604 | // and the found name refers to an instance member function, otherwise |
| 1605 | // the function calling DiagnoseEmptyLookup will try to create an |
| 1606 | // implicit member call and this is wrong for default argument. |
| 1607 | if (isDefaultArgument && ((*R.begin())->isCXXInstanceMember())) { |
| 1608 | Diag(R.getNameLoc(), diag::err_member_call_without_object); |
| 1609 | return true; |
| 1610 | } |
| 1611 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1612 | // Tell the callee to try to recover. |
| 1613 | return false; |
| 1614 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1615 | |
| 1616 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1617 | } |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1618 | |
| 1619 | // In Microsoft mode, if we are performing lookup from within a friend |
| 1620 | // function definition declared at class scope then we must set |
| 1621 | // DC to the lexical parent to be able to search into the parent |
| 1622 | // class. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1623 | if (getLangOpts().MicrosoftMode && isa<FunctionDecl>(DC) && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1624 | cast<FunctionDecl>(DC)->getFriendObjectKind() && |
| 1625 | DC->getLexicalParent()->isRecord()) |
| 1626 | DC = DC->getLexicalParent(); |
| 1627 | else |
| 1628 | DC = DC->getParent(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1631 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1632 | TypoCorrection Corrected; |
| 1633 | if (S && (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1634 | S, &SS, CCC))) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1635 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 1636 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1637 | R.setLookupName(Corrected.getCorrection()); |
| 1638 | |
Hans Wennborg | 701d1e7 | 2011-07-12 08:45:31 +0000 | [diff] [blame] | 1639 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1640 | if (Corrected.isOverloaded()) { |
| 1641 | OverloadCandidateSet OCS(R.getNameLoc()); |
| 1642 | OverloadCandidateSet::iterator Best; |
| 1643 | for (TypoCorrection::decl_iterator CD = Corrected.begin(), |
| 1644 | CDEnd = Corrected.end(); |
| 1645 | CD != CDEnd; ++CD) { |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1646 | if (FunctionTemplateDecl *FTD = |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1647 | dyn_cast<FunctionTemplateDecl>(*CD)) |
| 1648 | AddTemplateOverloadCandidate( |
| 1649 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1650 | Args, OCS); |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1651 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*CD)) |
| 1652 | if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 0) |
| 1653 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1654 | Args, OCS); |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1655 | } |
| 1656 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { |
| 1657 | case OR_Success: |
| 1658 | ND = Best->Function; |
| 1659 | break; |
| 1660 | default: |
Kaelyn Uhrain | 844d572 | 2011-08-04 23:30:54 +0000 | [diff] [blame] | 1661 | break; |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1662 | } |
| 1663 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1664 | R.addDecl(ND); |
| 1665 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1666 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1667 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr |
| 1668 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1669 | else |
| 1670 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1671 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1672 | << SS.getRange() |
David Blaikie | 6952c01 | 2012-10-12 20:00:44 +0000 | [diff] [blame] | 1673 | << FixItHint::CreateReplacement(Corrected.getCorrectionRange(), |
| 1674 | CorrectedStr); |
Ted Kremenek | 63631bd | 2013-02-21 21:40:44 +0000 | [diff] [blame] | 1675 | |
Ted Kremenek | 94f3f54 | 2013-02-21 22:10:49 +0000 | [diff] [blame^] | 1676 | unsigned diag = isa<ImplicitParamDecl>(ND) |
| 1677 | ? diag::note_implicit_param_decl |
| 1678 | : diag::note_previous_decl; |
| 1679 | |
| 1680 | Diag(ND->getLocation(), diag) |
| 1681 | << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1682 | |
| 1683 | // Tell the callee to try to recover. |
| 1684 | return false; |
| 1685 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1686 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1687 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1688 | // FIXME: If we ended up with a typo for a type name or |
| 1689 | // Objective-C class name, we're in trouble because the parser |
| 1690 | // is in the wrong place to recover. Suggest the typo |
| 1691 | // correction, but don't make it a fix-it since we're not going |
| 1692 | // to recover well anyway. |
| 1693 | if (SS.isEmpty()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1694 | Diag(R.getNameLoc(), diagnostic_suggest) |
| 1695 | << Name << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1696 | else |
| 1697 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1698 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1699 | << SS.getRange(); |
| 1700 | |
| 1701 | // Don't try to recover; it won't work. |
| 1702 | return true; |
| 1703 | } |
| 1704 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1705 | // 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] | 1706 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1707 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1708 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1709 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1710 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1711 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1712 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1713 | return true; |
| 1714 | } |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1715 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1716 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1717 | |
| 1718 | // Emit a special diagnostic for failed member lookups. |
| 1719 | // FIXME: computing the declaration context might fail here (?) |
| 1720 | if (!SS.isEmpty()) { |
| 1721 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1722 | << Name << computeDeclContext(SS, false) |
| 1723 | << SS.getRange(); |
| 1724 | return true; |
| 1725 | } |
| 1726 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1727 | // Give up, we can't recover. |
| 1728 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1729 | return true; |
| 1730 | } |
| 1731 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1732 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1733 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1734 | SourceLocation TemplateKWLoc, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1735 | UnqualifiedId &Id, |
| 1736 | bool HasTrailingLParen, |
Kaelyn Uhrain | cd78e61 | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 1737 | bool IsAddressOfOperand, |
| 1738 | CorrectionCandidateCallback *CCC) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1739 | assert(!(IsAddressOfOperand && HasTrailingLParen) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1740 | "cannot be direct & operand and have a trailing lparen"); |
| 1741 | |
| 1742 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1743 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1744 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1745 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1746 | |
| 1747 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1748 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1749 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1750 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1751 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1752 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1753 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1754 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1755 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1756 | // C++ [temp.dep.expr]p3: |
| 1757 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1758 | // -- an identifier that was declared with a dependent type, |
| 1759 | // (note: handled after lookup) |
| 1760 | // -- a template-id that is dependent, |
| 1761 | // (note: handled in BuildTemplateIdExpr) |
| 1762 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1763 | // -- a nested-name-specifier that contains a class-name that |
| 1764 | // names a dependent type. |
| 1765 | // Determine whether this is a member of an unknown specialization; |
| 1766 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1767 | bool DependentID = false; |
| 1768 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1769 | Name.getCXXNameType()->isDependentType()) { |
| 1770 | DependentID = true; |
| 1771 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1772 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1773 | if (RequireCompleteDeclContext(SS, DC)) |
| 1774 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1775 | } else { |
| 1776 | DependentID = true; |
| 1777 | } |
| 1778 | } |
| 1779 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1780 | if (DependentID) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1781 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1782 | IsAddressOfOperand, TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1783 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1784 | // Perform the required lookup. |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1785 | LookupResult R(*this, NameInfo, |
| 1786 | (Id.getKind() == UnqualifiedId::IK_ImplicitSelfParam) |
| 1787 | ? LookupObjCImplicitSelfParam : LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1788 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1789 | // Lookup the template name again to correctly establish the context in |
| 1790 | // which it was found. This is really unfortunate as we already did the |
| 1791 | // lookup to determine that it was a template name in the first place. If |
| 1792 | // this becomes a performance hit, we can work harder to preserve those |
| 1793 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1794 | bool MemberOfUnknownSpecialization; |
| 1795 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1796 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1797 | |
| 1798 | if (MemberOfUnknownSpecialization || |
| 1799 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1800 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1801 | IsAddressOfOperand, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1802 | } else { |
Benjamin Kramer | b7ff74a | 2012-01-20 14:57:34 +0000 | [diff] [blame] | 1803 | bool IvarLookupFollowUp = II && !SS.isSet() && getCurMethodDecl(); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1804 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1806 | // If the result might be in a dependent base class, this is a dependent |
| 1807 | // id-expression. |
| 1808 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1809 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1810 | IsAddressOfOperand, TemplateArgs); |
| 1811 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1812 | // If this reference is in an Objective-C method, then we need to do |
| 1813 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1814 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1815 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1816 | if (E.isInvalid()) |
| 1817 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1819 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1820 | return Owned(Ex); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1821 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1822 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1823 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1824 | if (R.isAmbiguous()) |
| 1825 | return ExprError(); |
| 1826 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1827 | // Determine whether this name might be a candidate for |
| 1828 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1829 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1830 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1831 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1832 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1833 | // in C90, extension in C99, forbidden in C++). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1834 | if (HasTrailingLParen && II && !getLangOpts().CPlusPlus) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1835 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1836 | if (D) R.addDecl(D); |
| 1837 | } |
| 1838 | |
| 1839 | // If this name wasn't predeclared and if this is not a function |
| 1840 | // call, diagnose the problem. |
| 1841 | if (R.empty()) { |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1842 | |
| 1843 | // In Microsoft mode, if we are inside a template class member function |
| 1844 | // and we can't resolve an identifier then assume the identifier is type |
| 1845 | // dependent. The goal is to postpone name lookup to instantiation time |
| 1846 | // to be able to search into type dependent base classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1847 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1848 | isa<CXXMethodDecl>(CurContext)) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1849 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1850 | IsAddressOfOperand, TemplateArgs); |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1851 | |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 1852 | CorrectionCandidateCallback DefaultValidator; |
Kaelyn Uhrain | cd78e61 | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 1853 | if (DiagnoseEmptyLookup(S, SS, R, CCC ? *CCC : DefaultValidator)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1854 | return ExprError(); |
| 1855 | |
| 1856 | assert(!R.empty() && |
| 1857 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1858 | |
| 1859 | // If we found an Objective-C instance variable, let |
| 1860 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1861 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1862 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1863 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1864 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Fariborz Jahanian | bc2b91a | 2011-09-23 23:11:38 +0000 | [diff] [blame] | 1865 | // In a hopelessly buggy code, Objective-C instance variable |
| 1866 | // lookup fails and no expression will be built to reference it. |
| 1867 | if (!E.isInvalid() && !E.get()) |
| 1868 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1869 | return E; |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1870 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1871 | } |
| 1872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1874 | // This is guaranteed from this point on. |
| 1875 | assert(!R.empty() || ADL); |
| 1876 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1877 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1878 | // C++ [class.mfct.non-static]p3: |
| 1879 | // When an id-expression that is not part of a class member access |
| 1880 | // syntax and not used to form a pointer to member is used in the |
| 1881 | // body of a non-static member function of class X, if name lookup |
| 1882 | // resolves the name in the id-expression to a non-static non-type |
| 1883 | // member of some class C, the id-expression is transformed into a |
| 1884 | // class member access expression using (*this) as the |
| 1885 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1886 | // |
| 1887 | // But we don't actually need to do this for '&' operands if R |
| 1888 | // resolved to a function or overloaded function set, because the |
| 1889 | // expression is ill-formed if it actually works out to be a |
| 1890 | // non-static member function: |
| 1891 | // |
| 1892 | // C++ [expr.ref]p4: |
| 1893 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1894 | // [t]he expression can be used only as the left-hand operand of a |
| 1895 | // member function call. |
| 1896 | // |
| 1897 | // There are other safeguards against such uses, but it's important |
| 1898 | // to get this right here so that we don't end up making a |
| 1899 | // spuriously dependent expression if we're inside a dependent |
| 1900 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1901 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1902 | bool MightBeImplicitMember; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1903 | if (!IsAddressOfOperand) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1904 | MightBeImplicitMember = true; |
| 1905 | else if (!SS.isEmpty()) |
| 1906 | MightBeImplicitMember = false; |
| 1907 | else if (R.isOverloadedResult()) |
| 1908 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1909 | else if (R.isUnresolvableResult()) |
| 1910 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1911 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1912 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1913 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1914 | |
| 1915 | if (MightBeImplicitMember) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1916 | return BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 1917 | R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 1920 | if (TemplateArgs || TemplateKWLoc.isValid()) |
| 1921 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, ADL, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1922 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1923 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1924 | } |
| 1925 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1926 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1927 | /// declaration name, generally during template instantiation. |
| 1928 | /// There's a large number of things which don't need to be done along |
| 1929 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1930 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1931 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Richard Smith | efeeccf | 2012-10-21 03:28:35 +0000 | [diff] [blame] | 1932 | const DeclarationNameInfo &NameInfo, |
| 1933 | bool IsAddressOfOperand) { |
Richard Smith | 713c287 | 2012-10-23 19:56:01 +0000 | [diff] [blame] | 1934 | DeclContext *DC = computeDeclContext(SS, false); |
| 1935 | if (!DC) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 1936 | return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), |
| 1937 | NameInfo, /*TemplateArgs=*/0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1938 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1939 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1940 | return ExprError(); |
| 1941 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1942 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1943 | LookupQualifiedName(R, DC); |
| 1944 | |
| 1945 | if (R.isAmbiguous()) |
| 1946 | return ExprError(); |
| 1947 | |
Richard Smith | 713c287 | 2012-10-23 19:56:01 +0000 | [diff] [blame] | 1948 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1949 | return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), |
| 1950 | NameInfo, /*TemplateArgs=*/0); |
| 1951 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1952 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1953 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1954 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1955 | return ExprError(); |
| 1956 | } |
| 1957 | |
Richard Smith | efeeccf | 2012-10-21 03:28:35 +0000 | [diff] [blame] | 1958 | // Defend against this resolving to an implicit member access. We usually |
| 1959 | // won't get here if this might be a legitimate a class member (we end up in |
| 1960 | // BuildMemberReferenceExpr instead), but this can be valid if we're forming |
| 1961 | // a pointer-to-member or in an unevaluated context in C++11. |
| 1962 | if (!R.empty() && (*R.begin())->isCXXClassMember() && !IsAddressOfOperand) |
| 1963 | return BuildPossibleImplicitMemberExpr(SS, |
| 1964 | /*TemplateKWLoc=*/SourceLocation(), |
| 1965 | R, /*TemplateArgs=*/0); |
| 1966 | |
| 1967 | return BuildDeclarationNameExpr(SS, R, /* ADL */ false); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1971 | /// detected that we're currently inside an ObjC method. Perform some |
| 1972 | /// additional lookup. |
| 1973 | /// |
| 1974 | /// Ideally, most of this would be done by lookup, but there's |
| 1975 | /// actually quite a lot of extra work involved. |
| 1976 | /// |
| 1977 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1978 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1979 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1980 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1981 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1982 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Fariborz Jahanian | 0dc4ff2 | 2013-02-18 17:22:23 +0000 | [diff] [blame] | 1983 | |
| 1984 | // Check for error condition which is already reported. |
| 1985 | if (!CurMethod) |
| 1986 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1987 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1988 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1989 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1990 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1991 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1992 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1993 | |
| 1994 | // If we're in a class method, we don't normally want to look for |
| 1995 | // ivars. But if we don't find anything else, and there's an |
| 1996 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1997 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1998 | |
| 1999 | bool LookForIvars; |
| 2000 | if (Lookup.empty()) |
| 2001 | LookForIvars = true; |
| 2002 | else if (IsClassMethod) |
| 2003 | LookForIvars = false; |
| 2004 | else |
| 2005 | LookForIvars = (Lookup.isSingleResult() && |
| 2006 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 2007 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2008 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2009 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2010 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | 7c81c2a | 2011-10-19 02:25:16 +0000 | [diff] [blame] | 2011 | ObjCIvarDecl *IV = 0; |
| 2012 | if (IFace && (IV = IFace->lookupInstanceVariable(II, ClassDeclared))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2013 | // Diagnose using an ivar in a class method. |
| 2014 | if (IsClassMethod) |
| 2015 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 2016 | << IV->getDeclName()); |
| 2017 | |
| 2018 | // If we're referencing an invalid decl, just return this as a silent |
| 2019 | // error node. The error diagnostic was already emitted on the decl. |
| 2020 | if (IV->isInvalidDecl()) |
| 2021 | return ExprError(); |
| 2022 | |
| 2023 | // Check if referencing a field with __attribute__((deprecated)). |
| 2024 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 2025 | return ExprError(); |
| 2026 | |
| 2027 | // Diagnose the use of an ivar outside of the declaring class. |
| 2028 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
Fariborz Jahanian | 458a7fb | 2012-03-07 00:58:41 +0000 | [diff] [blame] | 2029 | !declaresSameEntity(ClassDeclared, IFace) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2030 | !getLangOpts().DebuggerSupport) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2031 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 2032 | |
| 2033 | // FIXME: This should use a new expr for a direct reference, don't |
| 2034 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 2035 | IdentifierInfo &II = Context.Idents.get("self"); |
| 2036 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2037 | SelfName.setIdentifier(&II, SourceLocation()); |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 2038 | SelfName.setKind(UnqualifiedId::IK_ImplicitSelfParam); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2039 | CXXScopeSpec SelfScopeSpec; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2040 | SourceLocation TemplateKWLoc; |
| 2041 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, TemplateKWLoc, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 2042 | SelfName, false, false); |
| 2043 | if (SelfExpr.isInvalid()) |
| 2044 | return ExprError(); |
| 2045 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2046 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 2047 | if (SelfExpr.isInvalid()) |
| 2048 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 2049 | |
Nick Lewycky | b7e5eec | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 2050 | MarkAnyDeclReferenced(Loc, IV, true); |
Fariborz Jahanian | ed6662d | 2012-08-08 16:41:04 +0000 | [diff] [blame] | 2051 | |
| 2052 | ObjCMethodFamily MF = CurMethod->getMethodFamily(); |
Fariborz Jahanian | 2620229 | 2013-02-14 19:07:19 +0000 | [diff] [blame] | 2053 | if (MF != OMF_init && MF != OMF_dealloc && MF != OMF_finalize && |
| 2054 | !IvarBacksCurrentMethodAccessor(IFace, CurMethod, IV)) |
Fariborz Jahanian | ed6662d | 2012-08-08 16:41:04 +0000 | [diff] [blame] | 2055 | Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName(); |
Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 2056 | |
| 2057 | ObjCIvarRefExpr *Result = new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2058 | Loc, |
| 2059 | SelfExpr.take(), |
| 2060 | true, true); |
| 2061 | |
| 2062 | if (getLangOpts().ObjCAutoRefCount) { |
| 2063 | if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 2064 | DiagnosticsEngine::Level Level = |
| 2065 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, Loc); |
| 2066 | if (Level != DiagnosticsEngine::Ignored) |
| 2067 | getCurFunction()->recordUseOfWeak(Result); |
| 2068 | } |
Fariborz Jahanian | 3f001ff | 2012-10-03 17:55:29 +0000 | [diff] [blame] | 2069 | if (CurContext->isClosure()) |
| 2070 | Diag(Loc, diag::warn_implicitly_retains_self) |
| 2071 | << FixItHint::CreateInsertion(Loc, "self->"); |
Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | return Owned(Result); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2075 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2076 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2077 | // We should warn if a local variable hides an ivar. |
Fariborz Jahanian | 90f7b62 | 2011-11-08 22:51:27 +0000 | [diff] [blame] | 2078 | if (ObjCInterfaceDecl *IFace = CurMethod->getClassInterface()) { |
| 2079 | ObjCInterfaceDecl *ClassDeclared; |
| 2080 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2081 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 2082 | declaresSameEntity(IFace, ClassDeclared)) |
Fariborz Jahanian | 90f7b62 | 2011-11-08 22:51:27 +0000 | [diff] [blame] | 2083 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 2084 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2085 | } |
Fariborz Jahanian | b5ea9db | 2011-12-20 22:21:08 +0000 | [diff] [blame] | 2086 | } else if (Lookup.isSingleResult() && |
| 2087 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()) { |
| 2088 | // If accessing a stand-alone ivar in a class method, this is an error. |
| 2089 | if (const ObjCIvarDecl *IV = dyn_cast<ObjCIvarDecl>(Lookup.getFoundDecl())) |
| 2090 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 2091 | << IV->getDeclName()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2094 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 2095 | // FIXME. Consolidate this with similar code in LookupName. |
| 2096 | if (unsigned BuiltinID = II->getBuiltinID()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2097 | if (!(getLangOpts().CPlusPlus && |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2098 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 2099 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 2100 | S, Lookup.isForRedeclaration(), |
| 2101 | Lookup.getNameLoc()); |
| 2102 | if (D) Lookup.addDecl(D); |
| 2103 | } |
| 2104 | } |
| 2105 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2106 | // Sentinel value saying that we didn't do anything special. |
| 2107 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2108 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2109 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2110 | /// \brief Cast a base object to a member's actual type. |
| 2111 | /// |
| 2112 | /// Logically this happens in three phases: |
| 2113 | /// |
| 2114 | /// * First we cast from the base type to the naming class. |
| 2115 | /// The naming class is the class into which we were looking |
| 2116 | /// when we found the member; it's the qualifier type if a |
| 2117 | /// qualifier was provided, and otherwise it's the base type. |
| 2118 | /// |
| 2119 | /// * Next we cast from the naming class to the declaring class. |
| 2120 | /// If the member we found was brought into a class's scope by |
| 2121 | /// a using declaration, this is that class; otherwise it's |
| 2122 | /// the class declaring the member. |
| 2123 | /// |
| 2124 | /// * Finally we cast from the declaring class to the "true" |
| 2125 | /// declaring class of the member. This conversion does not |
| 2126 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2127 | ExprResult |
| 2128 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2129 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2130 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2131 | NamedDecl *Member) { |
| 2132 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 2133 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2134 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2135 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2136 | QualType DestRecordType; |
| 2137 | QualType DestType; |
| 2138 | QualType FromRecordType; |
| 2139 | QualType FromType = From->getType(); |
| 2140 | bool PointerConversions = false; |
| 2141 | if (isa<FieldDecl>(Member)) { |
| 2142 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2143 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2144 | if (FromType->getAs<PointerType>()) { |
| 2145 | DestType = Context.getPointerType(DestRecordType); |
| 2146 | FromRecordType = FromType->getPointeeType(); |
| 2147 | PointerConversions = true; |
| 2148 | } else { |
| 2149 | DestType = DestRecordType; |
| 2150 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2151 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2152 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 2153 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2154 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2155 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2156 | DestType = Method->getThisType(Context); |
| 2157 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2159 | if (FromType->getAs<PointerType>()) { |
| 2160 | FromRecordType = FromType->getPointeeType(); |
| 2161 | PointerConversions = true; |
| 2162 | } else { |
| 2163 | FromRecordType = FromType; |
| 2164 | DestType = DestRecordType; |
| 2165 | } |
| 2166 | } else { |
| 2167 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2168 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2169 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2171 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2172 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2173 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2174 | // If the unqualified types are the same, no conversion is necessary. |
| 2175 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2176 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2177 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2178 | SourceRange FromRange = From->getSourceRange(); |
| 2179 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2180 | |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 2181 | ExprValueKind VK = From->getValueKind(); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2182 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2183 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2184 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2185 | // class name. |
| 2186 | // |
| 2187 | // If the member was a qualified name and the qualified referred to a |
| 2188 | // specific base subobject type, we'll cast to that intermediate type |
| 2189 | // first and then to the object in which the member is declared. That allows |
| 2190 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2191 | // |
| 2192 | // class Base { public: int x; }; |
| 2193 | // class Derived1 : public Base { }; |
| 2194 | // class Derived2 : public Base { }; |
| 2195 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2196 | // |
| 2197 | // void VeryDerived::f() { |
| 2198 | // x = 17; // error: ambiguous base subobjects |
| 2199 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2200 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2201 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2202 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2203 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2204 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2205 | |
| 2206 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2207 | |
| 2208 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2209 | // type of the object type, in which case we just ignore it. |
| 2210 | // Otherwise build the appropriate casts. |
| 2211 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2212 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2213 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2214 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2215 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2216 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2217 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2218 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2219 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2220 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2221 | |
| 2222 | FromType = QType; |
| 2223 | FromRecordType = QRecordType; |
| 2224 | |
| 2225 | // If the qualifier type was the same as the destination type, |
| 2226 | // we're done. |
| 2227 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2228 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2229 | } |
| 2230 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2231 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2232 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2233 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2234 | // If we actually found the member through a using declaration, cast |
| 2235 | // down to the using declaration's type. |
| 2236 | // |
| 2237 | // Pointer equality is fine here because only one declaration of a |
| 2238 | // class ever has member declarations. |
| 2239 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2240 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2241 | QualType URecordType = Context.getTypeDeclType( |
| 2242 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2243 | |
| 2244 | // We only need to do this if the naming-class to declaring-class |
| 2245 | // conversion is non-trivial. |
| 2246 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2247 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2248 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2249 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2250 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2251 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2252 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2253 | QualType UType = URecordType; |
| 2254 | if (PointerConversions) |
| 2255 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2256 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2257 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2258 | FromType = UType; |
| 2259 | FromRecordType = URecordType; |
| 2260 | } |
| 2261 | |
| 2262 | // We don't do access control for the conversion from the |
| 2263 | // declaring class to the true declaring class. |
| 2264 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2265 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2266 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2267 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2268 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2269 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2270 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2271 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2272 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2273 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2274 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2275 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2276 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2277 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2278 | const LookupResult &R, |
| 2279 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2280 | // Only when used directly as the postfix-expression of a call. |
| 2281 | if (!HasTrailingLParen) |
| 2282 | return false; |
| 2283 | |
| 2284 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2285 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2286 | return false; |
| 2287 | |
| 2288 | // Only in C++ or ObjC++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2289 | if (!getLangOpts().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2290 | return false; |
| 2291 | |
| 2292 | // Turn off ADL when we find certain kinds of declarations during |
| 2293 | // normal lookup: |
| 2294 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2295 | NamedDecl *D = *I; |
| 2296 | |
| 2297 | // C++0x [basic.lookup.argdep]p3: |
| 2298 | // -- a declaration of a class member |
| 2299 | // Since using decls preserve this property, we check this on the |
| 2300 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2301 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2302 | return false; |
| 2303 | |
| 2304 | // C++0x [basic.lookup.argdep]p3: |
| 2305 | // -- a block-scope function declaration that is not a |
| 2306 | // using-declaration |
| 2307 | // NOTE: we also trigger this for function templates (in fact, we |
| 2308 | // don't check the decl type at all, since all other decl types |
| 2309 | // turn off ADL anyway). |
| 2310 | if (isa<UsingShadowDecl>(D)) |
| 2311 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2312 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2313 | return false; |
| 2314 | |
| 2315 | // C++0x [basic.lookup.argdep]p3: |
| 2316 | // -- a declaration that is neither a function or a function |
| 2317 | // template |
| 2318 | // And also for builtin functions. |
| 2319 | if (isa<FunctionDecl>(D)) { |
| 2320 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2321 | |
| 2322 | // But also builtin functions. |
| 2323 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2324 | return false; |
| 2325 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2326 | return false; |
| 2327 | } |
| 2328 | |
| 2329 | return true; |
| 2330 | } |
| 2331 | |
| 2332 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2333 | /// Diagnoses obvious problems with the use of the given declaration |
| 2334 | /// as an expression. This is only actually called for lookups that |
| 2335 | /// were not overloaded, and it doesn't promise that the declaration |
| 2336 | /// will in fact be used. |
| 2337 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2338 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2339 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2340 | return true; |
| 2341 | } |
| 2342 | |
| 2343 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2344 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2345 | return true; |
| 2346 | } |
| 2347 | |
| 2348 | if (isa<NamespaceDecl>(D)) { |
| 2349 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2350 | return true; |
| 2351 | } |
| 2352 | |
| 2353 | return false; |
| 2354 | } |
| 2355 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2356 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2357 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2358 | LookupResult &R, |
| 2359 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2360 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2361 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2362 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2363 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2364 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2365 | |
| 2366 | // We only need to check the declaration if there's exactly one |
| 2367 | // result, because in the overloaded case the results can only be |
| 2368 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2369 | if (R.isSingleResult() && |
| 2370 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2371 | return ExprError(); |
| 2372 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2373 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2374 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2375 | // we've picked a target. |
| 2376 | R.suppressDiagnostics(); |
| 2377 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2378 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2379 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2380 | SS.getWithLocInContext(Context), |
| 2381 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2382 | NeedsADL, R.isOverloadedResult(), |
| 2383 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2384 | |
| 2385 | return Owned(ULE); |
| 2386 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2387 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2388 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2389 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2390 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2391 | const DeclarationNameInfo &NameInfo, |
| 2392 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2393 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2394 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2395 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2396 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2397 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2398 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2399 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2400 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2401 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2402 | // Specifically diagnose references to class templates that are missing |
| 2403 | // a template argument list. |
| 2404 | Diag(Loc, diag::err_template_decl_ref) |
| 2405 | << Template << SS.getRange(); |
| 2406 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2407 | return ExprError(); |
| 2408 | } |
| 2409 | |
| 2410 | // Make sure that we're referring to a value. |
| 2411 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2412 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2413 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2414 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2415 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2416 | return ExprError(); |
| 2417 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2418 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2419 | // Check whether this declaration can be used. Note that we suppress |
| 2420 | // this check when we're going to perform argument-dependent lookup |
| 2421 | // on this function name, because this might not be the function |
| 2422 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2423 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2424 | return ExprError(); |
| 2425 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2426 | // Only create DeclRefExpr's for valid Decl's. |
| 2427 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2428 | return ExprError(); |
| 2429 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2430 | // Handle members of anonymous structs and unions. If we got here, |
| 2431 | // and the reference is to a class member indirect field, then this |
| 2432 | // must be the subject of a pointer-to-member expression. |
| 2433 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2434 | if (!indirectField->isCXXClassMember()) |
| 2435 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2436 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2437 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2438 | { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2439 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2440 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2441 | |
| 2442 | switch (D->getKind()) { |
| 2443 | // Ignore all the non-ValueDecl kinds. |
| 2444 | #define ABSTRACT_DECL(kind) |
| 2445 | #define VALUE(type, base) |
| 2446 | #define DECL(type, base) \ |
| 2447 | case Decl::type: |
| 2448 | #include "clang/AST/DeclNodes.inc" |
| 2449 | llvm_unreachable("invalid value decl kind"); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2450 | |
| 2451 | // These shouldn't make it here. |
| 2452 | case Decl::ObjCAtDefsField: |
| 2453 | case Decl::ObjCIvar: |
| 2454 | llvm_unreachable("forming non-member reference to ivar?"); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2455 | |
| 2456 | // Enum constants are always r-values and never references. |
| 2457 | // Unresolved using declarations are dependent. |
| 2458 | case Decl::EnumConstant: |
| 2459 | case Decl::UnresolvedUsingValue: |
| 2460 | valueKind = VK_RValue; |
| 2461 | break; |
| 2462 | |
| 2463 | // Fields and indirect fields that got here must be for |
| 2464 | // pointer-to-member expressions; we just call them l-values for |
| 2465 | // internal consistency, because this subexpression doesn't really |
| 2466 | // exist in the high-level semantics. |
| 2467 | case Decl::Field: |
| 2468 | case Decl::IndirectField: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2469 | assert(getLangOpts().CPlusPlus && |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2470 | "building reference to field in C?"); |
| 2471 | |
| 2472 | // These can't have reference type in well-formed programs, but |
| 2473 | // for internal consistency we do this anyway. |
| 2474 | type = type.getNonReferenceType(); |
| 2475 | valueKind = VK_LValue; |
| 2476 | break; |
| 2477 | |
| 2478 | // Non-type template parameters are either l-values or r-values |
| 2479 | // depending on the type. |
| 2480 | case Decl::NonTypeTemplateParm: { |
| 2481 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2482 | type = reftype->getPointeeType(); |
| 2483 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2484 | break; |
| 2485 | } |
| 2486 | |
| 2487 | // For non-references, we need to strip qualifiers just in case |
| 2488 | // the template parameter was declared as 'const int' or whatever. |
| 2489 | valueKind = VK_RValue; |
| 2490 | type = type.getUnqualifiedType(); |
| 2491 | break; |
| 2492 | } |
| 2493 | |
| 2494 | case Decl::Var: |
| 2495 | // In C, "extern void blah;" is valid and is an r-value. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2496 | if (!getLangOpts().CPlusPlus && |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2497 | !type.hasQualifiers() && |
| 2498 | type->isVoidType()) { |
| 2499 | valueKind = VK_RValue; |
| 2500 | break; |
| 2501 | } |
| 2502 | // fallthrough |
| 2503 | |
| 2504 | case Decl::ImplicitParam: |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2505 | case Decl::ParmVar: { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2506 | // These are always l-values. |
| 2507 | valueKind = VK_LValue; |
| 2508 | type = type.getNonReferenceType(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2509 | |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2510 | // FIXME: Does the addition of const really only apply in |
| 2511 | // potentially-evaluated contexts? Since the variable isn't actually |
| 2512 | // captured in an unevaluated context, it seems that the answer is no. |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 2513 | if (!isUnevaluatedContext()) { |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2514 | QualType CapturedType = getCapturedDeclRefType(cast<VarDecl>(VD), Loc); |
| 2515 | if (!CapturedType.isNull()) |
| 2516 | type = CapturedType; |
| 2517 | } |
| 2518 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2519 | break; |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2522 | case Decl::Function: { |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 2523 | if (unsigned BID = cast<FunctionDecl>(VD)->getBuiltinID()) { |
| 2524 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BID)) { |
| 2525 | type = Context.BuiltinFnTy; |
| 2526 | valueKind = VK_RValue; |
| 2527 | break; |
| 2528 | } |
| 2529 | } |
| 2530 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2531 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2532 | |
| 2533 | // If we're referring to a function with an __unknown_anytype |
| 2534 | // result type, make the entire expression __unknown_anytype. |
| 2535 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2536 | type = Context.UnknownAnyTy; |
| 2537 | valueKind = VK_RValue; |
| 2538 | break; |
| 2539 | } |
| 2540 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2541 | // Functions are l-values in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2542 | if (getLangOpts().CPlusPlus) { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2543 | valueKind = VK_LValue; |
| 2544 | break; |
| 2545 | } |
| 2546 | |
| 2547 | // C99 DR 316 says that, if a function type comes from a |
| 2548 | // function definition (without a prototype), that type is only |
| 2549 | // used for checking compatibility. Therefore, when referencing |
| 2550 | // the function, we pretend that we don't have the full function |
| 2551 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2552 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2553 | isa<FunctionProtoType>(fty)) |
| 2554 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2555 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2556 | |
| 2557 | // Functions are r-values in C. |
| 2558 | valueKind = VK_RValue; |
| 2559 | break; |
| 2560 | } |
| 2561 | |
| 2562 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2563 | // If we're referring to a method with an __unknown_anytype |
| 2564 | // result type, make the entire expression __unknown_anytype. |
| 2565 | // This should only be possible with a type written directly. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 2566 | if (const FunctionProtoType *proto |
| 2567 | = dyn_cast<FunctionProtoType>(VD->getType())) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2568 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2569 | type = Context.UnknownAnyTy; |
| 2570 | valueKind = VK_RValue; |
| 2571 | break; |
| 2572 | } |
| 2573 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2574 | // C++ methods are l-values if static, r-values if non-static. |
| 2575 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2576 | valueKind = VK_LValue; |
| 2577 | break; |
| 2578 | } |
| 2579 | // fallthrough |
| 2580 | |
| 2581 | case Decl::CXXConversion: |
| 2582 | case Decl::CXXDestructor: |
| 2583 | case Decl::CXXConstructor: |
| 2584 | valueKind = VK_RValue; |
| 2585 | break; |
| 2586 | } |
| 2587 | |
| 2588 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2589 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2592 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2593 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2594 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2595 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2596 | default: llvm_unreachable("Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2597 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2598 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
Nico Weber | 28ad063 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 2599 | case tok::kw_L__FUNCTION__: IT = PredefinedExpr::LFunction; break; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2600 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2601 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2602 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2603 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2604 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2606 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Benjamin Kramer | 4242740 | 2012-12-06 15:42:21 +0000 | [diff] [blame] | 2607 | // Blocks and lambdas can occur at global scope. Don't emit a warning. |
| 2608 | if (!currentDecl) { |
| 2609 | if (const BlockScopeInfo *BSI = getCurBlock()) |
| 2610 | currentDecl = BSI->TheDecl; |
| 2611 | else if (const LambdaScopeInfo *LSI = getCurLambda()) |
| 2612 | currentDecl = LSI->CallOperator; |
| 2613 | } |
| 2614 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2615 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2616 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2617 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2618 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2619 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2620 | QualType ResTy; |
| 2621 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2622 | ResTy = Context.DependentTy; |
| 2623 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2624 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2625 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2626 | llvm::APInt LengthI(32, Length + 1); |
Nico Weber | d68615f | 2012-06-29 16:39:58 +0000 | [diff] [blame] | 2627 | if (IT == PredefinedExpr::LFunction) |
Nico Weber | 28ad063 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 2628 | ResTy = Context.WCharTy.withConst(); |
| 2629 | else |
| 2630 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2631 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2632 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2633 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2636 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2637 | SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2638 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2639 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2640 | if (Invalid) |
| 2641 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2642 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2643 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2644 | PP, Tok.getKind()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2645 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2646 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2647 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2648 | QualType Ty; |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2649 | if (Literal.isWide()) |
| 2650 | Ty = Context.WCharTy; // L'x' -> wchar_t in C and C++. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2651 | else if (Literal.isUTF16()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2652 | Ty = Context.Char16Ty; // u'x' -> char16_t in C11 and C++11. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2653 | else if (Literal.isUTF32()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2654 | Ty = Context.Char32Ty; // U'x' -> char32_t in C11 and C++11. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2655 | else if (!getLangOpts().CPlusPlus || Literal.isMultiChar()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2656 | Ty = Context.IntTy; // 'x' -> int in C, 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2657 | else |
| 2658 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2659 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2660 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
| 2661 | if (Literal.isWide()) |
| 2662 | Kind = CharacterLiteral::Wide; |
| 2663 | else if (Literal.isUTF16()) |
| 2664 | Kind = CharacterLiteral::UTF16; |
| 2665 | else if (Literal.isUTF32()) |
| 2666 | Kind = CharacterLiteral::UTF32; |
| 2667 | |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 2668 | Expr *Lit = new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
| 2669 | Tok.getLocation()); |
| 2670 | |
| 2671 | if (Literal.getUDSuffix().empty()) |
| 2672 | return Owned(Lit); |
| 2673 | |
| 2674 | // We're building a user-defined literal. |
| 2675 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
| 2676 | SourceLocation UDSuffixLoc = |
| 2677 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); |
| 2678 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2679 | // Make sure we're allowed user-defined literals here. |
| 2680 | if (!UDLScope) |
| 2681 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_character_udl)); |
| 2682 | |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 2683 | // C++11 [lex.ext]p6: The literal L is treated as a call of the form |
| 2684 | // operator "" X (ch) |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2685 | return BuildCookedLiteralOperatorCall(*this, UDLScope, UDSuffix, UDSuffixLoc, |
| 2686 | llvm::makeArrayRef(&Lit, 1), |
| 2687 | Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2690 | ExprResult Sema::ActOnIntegerConstant(SourceLocation Loc, uint64_t Val) { |
| 2691 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
| 2692 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val), |
| 2693 | Context.IntTy, Loc)); |
| 2694 | } |
| 2695 | |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2696 | static Expr *BuildFloatingLiteral(Sema &S, NumericLiteralParser &Literal, |
| 2697 | QualType Ty, SourceLocation Loc) { |
| 2698 | const llvm::fltSemantics &Format = S.Context.getFloatTypeSemantics(Ty); |
| 2699 | |
| 2700 | using llvm::APFloat; |
| 2701 | APFloat Val(Format); |
| 2702 | |
| 2703 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
| 2704 | |
| 2705 | // Overflow is always an error, but underflow is only an error if |
| 2706 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2707 | if ((result & APFloat::opOverflow) || |
| 2708 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
| 2709 | unsigned diagnostic; |
| 2710 | SmallString<20> buffer; |
| 2711 | if (result & APFloat::opOverflow) { |
| 2712 | diagnostic = diag::warn_float_overflow; |
| 2713 | APFloat::getLargest(Format).toString(buffer); |
| 2714 | } else { |
| 2715 | diagnostic = diag::warn_float_underflow; |
| 2716 | APFloat::getSmallest(Format).toString(buffer); |
| 2717 | } |
| 2718 | |
| 2719 | S.Diag(Loc, diagnostic) |
| 2720 | << Ty |
| 2721 | << StringRef(buffer.data(), buffer.size()); |
| 2722 | } |
| 2723 | |
| 2724 | bool isExact = (result == APFloat::opOK); |
| 2725 | return FloatingLiteral::Create(S.Context, Val, isExact, Ty, Loc); |
| 2726 | } |
| 2727 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2728 | ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2729 | // 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] | 2730 | // cannot have a trigraph, escaped newline, radix prefix, or suffix. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2731 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2732 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2733 | return ActOnIntegerConstant(Tok.getLocation(), Val-'0'); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2734 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2735 | |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2736 | SmallString<128> SpellingBuffer; |
| 2737 | // NumericLiteralParser wants to overread by one character. Add padding to |
| 2738 | // the buffer in case the token is copied to the buffer. If getSpelling() |
| 2739 | // returns a StringRef to the memory buffer, it should have a null char at |
| 2740 | // the EOF, so it is also safe. |
| 2741 | SpellingBuffer.resize(Tok.getLength() + 1); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2742 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2743 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2744 | bool Invalid = false; |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2745 | StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2746 | if (Invalid) |
| 2747 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2748 | |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2749 | NumericLiteralParser Literal(TokSpelling, Tok.getLocation(), PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2750 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2751 | return ExprError(); |
| 2752 | |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2753 | if (Literal.hasUDSuffix()) { |
| 2754 | // We're building a user-defined literal. |
| 2755 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
| 2756 | SourceLocation UDSuffixLoc = |
| 2757 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); |
| 2758 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2759 | // Make sure we're allowed user-defined literals here. |
| 2760 | if (!UDLScope) |
| 2761 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_numeric_udl)); |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2762 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2763 | QualType CookedTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2764 | if (Literal.isFloatingLiteral()) { |
| 2765 | // C++11 [lex.ext]p4: If S contains a literal operator with parameter type |
| 2766 | // long double, the literal is treated as a call of the form |
| 2767 | // operator "" X (f L) |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2768 | CookedTy = Context.LongDoubleTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2769 | } else { |
| 2770 | // C++11 [lex.ext]p3: If S contains a literal operator with parameter type |
| 2771 | // unsigned long long, the literal is treated as a call of the form |
| 2772 | // operator "" X (n ULL) |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2773 | CookedTy = Context.UnsignedLongLongTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2776 | DeclarationName OpName = |
| 2777 | Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); |
| 2778 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); |
| 2779 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); |
| 2780 | |
| 2781 | // Perform literal operator lookup to determine if we're building a raw |
| 2782 | // literal or a cooked one. |
| 2783 | LookupResult R(*this, OpName, UDSuffixLoc, LookupOrdinaryName); |
| 2784 | switch (LookupLiteralOperator(UDLScope, R, llvm::makeArrayRef(&CookedTy, 1), |
| 2785 | /*AllowRawAndTemplate*/true)) { |
| 2786 | case LOLR_Error: |
| 2787 | return ExprError(); |
| 2788 | |
| 2789 | case LOLR_Cooked: { |
| 2790 | Expr *Lit; |
| 2791 | if (Literal.isFloatingLiteral()) { |
| 2792 | Lit = BuildFloatingLiteral(*this, Literal, CookedTy, Tok.getLocation()); |
| 2793 | } else { |
| 2794 | llvm::APInt ResultVal(Context.getTargetInfo().getLongLongWidth(), 0); |
| 2795 | if (Literal.GetIntegerValue(ResultVal)) |
| 2796 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
| 2797 | Lit = IntegerLiteral::Create(Context, ResultVal, CookedTy, |
| 2798 | Tok.getLocation()); |
| 2799 | } |
| 2800 | return BuildLiteralOperatorCall(R, OpNameInfo, |
| 2801 | llvm::makeArrayRef(&Lit, 1), |
| 2802 | Tok.getLocation()); |
| 2803 | } |
| 2804 | |
| 2805 | case LOLR_Raw: { |
| 2806 | // C++11 [lit.ext]p3, p4: If S contains a raw literal operator, the |
| 2807 | // literal is treated as a call of the form |
| 2808 | // operator "" X ("n") |
| 2809 | SourceLocation TokLoc = Tok.getLocation(); |
| 2810 | unsigned Length = Literal.getUDSuffixOffset(); |
| 2811 | QualType StrTy = Context.getConstantArrayType( |
Richard Smith | 4ea6a64 | 2013-01-23 23:38:20 +0000 | [diff] [blame] | 2812 | Context.CharTy.withConst(), llvm::APInt(32, Length + 1), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2813 | ArrayType::Normal, 0); |
| 2814 | Expr *Lit = StringLiteral::Create( |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2815 | Context, StringRef(TokSpelling.data(), Length), StringLiteral::Ascii, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2816 | /*Pascal*/false, StrTy, &TokLoc, 1); |
| 2817 | return BuildLiteralOperatorCall(R, OpNameInfo, |
| 2818 | llvm::makeArrayRef(&Lit, 1), TokLoc); |
| 2819 | } |
| 2820 | |
| 2821 | case LOLR_Template: |
| 2822 | // C++11 [lit.ext]p3, p4: Otherwise (S contains a literal operator |
| 2823 | // template), L is treated as a call fo the form |
| 2824 | // operator "" X <'c1', 'c2', ... 'ck'>() |
| 2825 | // where n is the source character sequence c1 c2 ... ck. |
| 2826 | TemplateArgumentListInfo ExplicitArgs; |
| 2827 | unsigned CharBits = Context.getIntWidth(Context.CharTy); |
| 2828 | bool CharIsUnsigned = Context.CharTy->isUnsignedIntegerType(); |
| 2829 | llvm::APSInt Value(CharBits, CharIsUnsigned); |
| 2830 | for (unsigned I = 0, N = Literal.getUDSuffixOffset(); I != N; ++I) { |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2831 | Value = TokSpelling[I]; |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2832 | TemplateArgument Arg(Context, Value, Context.CharTy); |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2833 | TemplateArgumentLocInfo ArgInfo; |
| 2834 | ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); |
| 2835 | } |
| 2836 | return BuildLiteralOperatorCall(R, OpNameInfo, ArrayRef<Expr*>(), |
| 2837 | Tok.getLocation(), &ExplicitArgs); |
| 2838 | } |
| 2839 | |
| 2840 | llvm_unreachable("unexpected literal operator lookup result"); |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2843 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2844 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2845 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2846 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2847 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2848 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2849 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2850 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2851 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2852 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2853 | |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2854 | Res = BuildFloatingLiteral(*this, Literal, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2855 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2856 | if (Ty == Context.DoubleTy) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2857 | if (getLangOpts().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2858 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2859 | } else if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2860 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2861 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2862 | } |
| 2863 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2864 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2865 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2866 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2867 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2868 | |
Dmitri Gribenko | e3b136b | 2012-09-24 18:19:21 +0000 | [diff] [blame] | 2869 | // 'long long' is a C99 or C++11 feature. |
| 2870 | if (!getLangOpts().C99 && Literal.isLongLong) { |
| 2871 | if (getLangOpts().CPlusPlus) |
| 2872 | Diag(Tok.getLocation(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2873 | getLangOpts().CPlusPlus11 ? |
Dmitri Gribenko | e3b136b | 2012-09-24 18:19:21 +0000 | [diff] [blame] | 2874 | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong); |
| 2875 | else |
| 2876 | Diag(Tok.getLocation(), diag::ext_c99_longlong); |
| 2877 | } |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2878 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2879 | // Get the value in the widest-possible width. |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2880 | unsigned MaxWidth = Context.getTargetInfo().getIntMaxTWidth(); |
| 2881 | // The microsoft literal suffix extensions support 128-bit literals, which |
| 2882 | // may be wider than [u]intmax_t. |
Richard Smith | 8426890 | 2012-11-29 05:41:51 +0000 | [diff] [blame] | 2883 | // FIXME: Actually, they don't. We seem to have accidentally invented the |
| 2884 | // i128 suffix. |
| 2885 | if (Literal.isMicrosoftInteger && MaxWidth < 128 && |
| 2886 | PP.getTargetInfo().hasInt128Type()) |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2887 | MaxWidth = 128; |
| 2888 | llvm::APInt ResultVal(MaxWidth, 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2889 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2890 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2891 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2892 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2893 | Ty = Context.UnsignedLongLongTy; |
| 2894 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2895 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2896 | } else { |
| 2897 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2898 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2899 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2900 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2901 | // be an unsigned int. |
| 2902 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2903 | |
| 2904 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2905 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2906 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2907 | // Are int/unsigned possibilities? |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2908 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2909 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2910 | // Does it fit in a unsigned int? |
| 2911 | if (ResultVal.isIntN(IntSize)) { |
| 2912 | // Does it fit in a signed int? |
| 2913 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2914 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2915 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2916 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2917 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2918 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2919 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2920 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2921 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2922 | if (Ty.isNull() && !Literal.isLongLong) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2923 | unsigned LongSize = Context.getTargetInfo().getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2924 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2925 | // Does it fit in a unsigned long? |
| 2926 | if (ResultVal.isIntN(LongSize)) { |
| 2927 | // Does it fit in a signed long? |
| 2928 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2929 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2930 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2931 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2932 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2933 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2936 | // Check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2937 | if (Ty.isNull()) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2938 | unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2939 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2940 | // Does it fit in a unsigned long long? |
| 2941 | if (ResultVal.isIntN(LongLongSize)) { |
| 2942 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2943 | // To be compatible with MSVC, hex integer literals ending with the |
| 2944 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2945 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2946 | (getLangOpts().MicrosoftExt && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2947 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2948 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2949 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2950 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2951 | } |
| 2952 | } |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2953 | |
| 2954 | // If it doesn't fit in unsigned long long, and we're using Microsoft |
| 2955 | // extensions, then its a 128-bit integer literal. |
Richard Smith | 8426890 | 2012-11-29 05:41:51 +0000 | [diff] [blame] | 2956 | if (Ty.isNull() && Literal.isMicrosoftInteger && |
| 2957 | PP.getTargetInfo().hasInt128Type()) { |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2958 | if (Literal.isUnsigned) |
| 2959 | Ty = Context.UnsignedInt128Ty; |
| 2960 | else |
| 2961 | Ty = Context.Int128Ty; |
| 2962 | Width = 128; |
| 2963 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2964 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2965 | // If we still couldn't decide a type, we probably have something that |
| 2966 | // 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] | 2967 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2968 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2969 | Ty = Context.UnsignedLongLongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2970 | Width = Context.getTargetInfo().getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2971 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2972 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2973 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2974 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2975 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2976 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2977 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2978 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2979 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2980 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2981 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2982 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2983 | |
| 2984 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2987 | ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2988 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2989 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2992 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 2993 | SourceLocation Loc, |
| 2994 | SourceRange ArgRange) { |
| 2995 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2996 | // scalar or vector data type argument..." |
| 2997 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2998 | // type (C99 6.2.5p18) or void. |
| 2999 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 3000 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 3001 | << T << ArgRange; |
| 3002 | return true; |
| 3003 | } |
| 3004 | |
| 3005 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 3006 | "Scalar types should always be complete"); |
| 3007 | return false; |
| 3008 | } |
| 3009 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3010 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 3011 | SourceLocation Loc, |
| 3012 | SourceRange ArgRange, |
| 3013 | UnaryExprOrTypeTrait TraitKind) { |
| 3014 | // C99 6.5.3.4p1: |
| 3015 | if (T->isFunctionType()) { |
| 3016 | // alignof(function) is allowed as an extension. |
| 3017 | if (TraitKind == UETT_SizeOf) |
| 3018 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 3019 | return false; |
| 3020 | } |
| 3021 | |
| 3022 | // Allow sizeof(void)/alignof(void) as an extension. |
| 3023 | if (T->isVoidType()) { |
| 3024 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 3025 | return false; |
| 3026 | } |
| 3027 | |
| 3028 | return true; |
| 3029 | } |
| 3030 | |
| 3031 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 3032 | SourceLocation Loc, |
| 3033 | SourceRange ArgRange, |
| 3034 | UnaryExprOrTypeTrait TraitKind) { |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3035 | // Reject sizeof(interface) and sizeof(interface<proto>) if the |
| 3036 | // runtime doesn't allow it. |
| 3037 | if (!S.LangOpts.ObjCRuntime.allowsSizeofAlignof() && T->isObjCObjectType()) { |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3038 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 3039 | << T << (TraitKind == UETT_SizeOf) |
| 3040 | << ArgRange; |
| 3041 | return true; |
| 3042 | } |
| 3043 | |
| 3044 | return false; |
| 3045 | } |
| 3046 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3047 | /// \brief Check the constrains on expression operands to unary type expression |
| 3048 | /// and type traits. |
| 3049 | /// |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3050 | /// Completes any types necessary and validates the constraints on the operand |
| 3051 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 3052 | /// the expression as it completes the type for that expression through template |
| 3053 | /// instantiation, etc. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3054 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3055 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3056 | QualType ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3057 | |
| 3058 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3059 | // the result is the size of the referenced type." |
| 3060 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3061 | // result shall be the alignment of the referenced type." |
| 3062 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3063 | ExprTy = Ref->getPointeeType(); |
| 3064 | |
| 3065 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3066 | return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 3067 | E->getSourceRange()); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3068 | |
| 3069 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3070 | if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 3071 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3072 | return false; |
| 3073 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3074 | if (RequireCompleteExprType(E, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3075 | diag::err_sizeof_alignof_incomplete_type, |
| 3076 | ExprKind, E->getSourceRange())) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3077 | return true; |
| 3078 | |
| 3079 | // Completeing the expression's type may have changed it. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3080 | ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3081 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3082 | ExprTy = Ref->getPointeeType(); |
| 3083 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3084 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), |
| 3085 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3086 | return true; |
| 3087 | |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3088 | if (ExprKind == UETT_SizeOf) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3089 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3090 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 3091 | QualType OType = PVD->getOriginalType(); |
| 3092 | QualType Type = PVD->getType(); |
| 3093 | if (Type->isPointerType() && OType->isArrayType()) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3094 | Diag(E->getExprLoc(), diag::warn_sizeof_array_param) |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3095 | << Type << OType; |
| 3096 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 3097 | } |
| 3098 | } |
| 3099 | } |
| 3100 | } |
| 3101 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3102 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | /// \brief Check the constraints on operands to unary expression and type |
| 3106 | /// traits. |
| 3107 | /// |
| 3108 | /// This will complete any types necessary, and validate the various constraints |
| 3109 | /// on those operands. |
| 3110 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3111 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3112 | /// C99 6.3.2.1p[2-4] all state: |
| 3113 | /// Except when it is the operand of the sizeof operator ... |
| 3114 | /// |
| 3115 | /// C++ [expr.sizeof]p4 |
| 3116 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 3117 | /// standard conversions are not applied to the operand of sizeof. |
| 3118 | /// |
| 3119 | /// This policy is followed for all of the unary trait expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3120 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3121 | SourceLocation OpLoc, |
| 3122 | SourceRange ExprRange, |
| 3123 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3124 | if (ExprType->isDependentType()) |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3125 | return false; |
| 3126 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3127 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3128 | // the result is the size of the referenced type." |
| 3129 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3130 | // result shall be the alignment of the referenced type." |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3131 | if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) |
| 3132 | ExprType = Ref->getPointeeType(); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3133 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3134 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3135 | return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3136 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3137 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3138 | if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3139 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3140 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3141 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3142 | if (RequireCompleteType(OpLoc, ExprType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3143 | diag::err_sizeof_alignof_incomplete_type, |
| 3144 | ExprKind, ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3145 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3146 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3147 | if (CheckObjCTraitOperandConstraints(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3148 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3149 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3150 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3151 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3154 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3155 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3156 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3158 | if (isa<DeclRefExpr>(E)) |
| 3159 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3160 | |
| 3161 | // Cannot know anything else if the expression is dependent. |
| 3162 | if (E->isTypeDependent()) |
| 3163 | return false; |
| 3164 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3165 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3166 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 3167 | << 1 << E->getSourceRange(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3168 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3169 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3170 | |
| 3171 | // Alignment of a field access is always okay, so long as it isn't a |
| 3172 | // bit-field. |
| 3173 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3174 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3175 | return false; |
| 3176 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3177 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3180 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3181 | E = E->IgnoreParens(); |
| 3182 | |
| 3183 | // Cannot know anything else if the expression is dependent. |
| 3184 | if (E->isTypeDependent()) |
| 3185 | return false; |
| 3186 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3187 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3190 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3191 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3192 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 3193 | SourceLocation OpLoc, |
| 3194 | UnaryExprOrTypeTrait ExprKind, |
| 3195 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3196 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3197 | return ExprError(); |
| 3198 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3199 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 3200 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3201 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3202 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3203 | return ExprError(); |
| 3204 | |
| 3205 | // 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] | 3206 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 3207 | Context.getSizeType(), |
| 3208 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | /// \brief Build a sizeof or alignof expression given an expression |
| 3212 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3213 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3214 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 3215 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 4f0845e | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 3216 | ExprResult PE = CheckPlaceholderExpr(E); |
| 3217 | if (PE.isInvalid()) |
| 3218 | return ExprError(); |
| 3219 | |
| 3220 | E = PE.get(); |
| 3221 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3222 | // Verify that the operand is valid. |
| 3223 | bool isInvalid = false; |
| 3224 | if (E->isTypeDependent()) { |
| 3225 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3226 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3227 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3228 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3229 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3230 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3231 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3232 | isInvalid = true; |
| 3233 | } else { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3234 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
| 3237 | if (isInvalid) |
| 3238 | return ExprError(); |
| 3239 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 3240 | if (ExprKind == UETT_SizeOf && E->getType()->isVariableArrayType()) { |
Benjamin Kramer | accaf19 | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 3241 | PE = TransformToPotentiallyEvaluated(E); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 3242 | if (PE.isInvalid()) return ExprError(); |
| 3243 | E = PE.take(); |
| 3244 | } |
| 3245 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3246 | // 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] | 3247 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3248 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3249 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3252 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 3253 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3254 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3255 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3256 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3257 | UnaryExprOrTypeTrait ExprKind, bool IsType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3258 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3259 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3260 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3261 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3262 | if (IsType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3263 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3264 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3265 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3267 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3268 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3269 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3270 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3273 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3274 | bool IsReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3275 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3276 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3278 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3279 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 3280 | V = S.DefaultLvalueConversion(V.take()); |
| 3281 | if (V.isInvalid()) |
| 3282 | return QualType(); |
| 3283 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3284 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3285 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3286 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3287 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3288 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3289 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3290 | if (V.get()->getType()->isArithmeticType()) |
| 3291 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3292 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3293 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3294 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3295 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3296 | if (PR.get() != V.get()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3297 | V = PR; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3298 | return CheckRealImagOperand(S, V, Loc, IsReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3301 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3302 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3303 | << (IsReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3304 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3308 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3309 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3310 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3311 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3312 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3313 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3314 | default: llvm_unreachable("Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3315 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3316 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3317 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3318 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3319 | // Since this might is a postfix expression, get rid of ParenListExprs. |
| 3320 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Input); |
| 3321 | if (Result.isInvalid()) return ExprError(); |
| 3322 | Input = Result.take(); |
| 3323 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3324 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3327 | /// \brief Diagnose if arithmetic on the given ObjC pointer is illegal. |
| 3328 | /// |
| 3329 | /// \return true on error |
| 3330 | static bool checkArithmeticOnObjCPointer(Sema &S, |
| 3331 | SourceLocation opLoc, |
| 3332 | Expr *op) { |
| 3333 | assert(op->getType()->isObjCObjectPointerType()); |
| 3334 | if (S.LangOpts.ObjCRuntime.allowsPointerArithmetic()) |
| 3335 | return false; |
| 3336 | |
| 3337 | S.Diag(opLoc, diag::err_arithmetic_nonfragile_interface) |
| 3338 | << op->getType()->castAs<ObjCObjectPointerType>()->getPointeeType() |
| 3339 | << op->getSourceRange(); |
| 3340 | return true; |
| 3341 | } |
| 3342 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3343 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3344 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3345 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3346 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3347 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3348 | if (Result.isInvalid()) return ExprError(); |
| 3349 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3350 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3351 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3353 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3354 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3355 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3356 | Context.DependentTy, |
| 3357 | VK_LValue, OK_Ordinary, |
| 3358 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3361 | if (getLangOpts().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3362 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3363 | LHSExp->getType()->isEnumeralType() || |
| 3364 | RHSExp->getType()->isRecordType() || |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3365 | RHSExp->getType()->isEnumeralType()) && |
| 3366 | !LHSExp->getType()->isObjCObjectPointerType()) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3367 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3370 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3373 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3374 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3375 | Expr *Idx, SourceLocation RLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3376 | Expr *LHSExp = Base; |
| 3377 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3378 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3379 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3380 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3381 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3382 | if (Result.isInvalid()) |
| 3383 | return ExprError(); |
| 3384 | LHSExp = Result.take(); |
| 3385 | } |
| 3386 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3387 | if (Result.isInvalid()) |
| 3388 | return ExprError(); |
| 3389 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3390 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3391 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3392 | ExprValueKind VK = VK_LValue; |
| 3393 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3394 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3395 | // 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] | 3396 | // 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] | 3397 | // 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] | 3398 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3399 | Expr *BaseExpr, *IndexExpr; |
| 3400 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3401 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3402 | BaseExpr = LHSExp; |
| 3403 | IndexExpr = RHSExp; |
| 3404 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3405 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3406 | BaseExpr = LHSExp; |
| 3407 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3408 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3409 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3410 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3411 | BaseExpr = LHSExp; |
| 3412 | IndexExpr = RHSExp; |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3413 | |
| 3414 | // Use custom logic if this should be the pseudo-object subscript |
| 3415 | // expression. |
| 3416 | if (!LangOpts.ObjCRuntime.isSubscriptPointerArithmetic()) |
| 3417 | return BuildObjCSubscriptExpression(RLoc, BaseExpr, IndexExpr, 0, 0); |
| 3418 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3419 | ResultType = PTy->getPointeeType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3420 | if (!LangOpts.ObjCRuntime.allowsPointerArithmetic()) { |
| 3421 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3422 | << ResultType << BaseExpr->getSourceRange(); |
| 3423 | return ExprError(); |
| 3424 | } |
Fariborz Jahanian | a78eca2 | 2012-03-28 17:56:49 +0000 | [diff] [blame] | 3425 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
| 3426 | // Handle the uncommon case of "123[Ptr]". |
| 3427 | BaseExpr = RHSExp; |
| 3428 | IndexExpr = LHSExp; |
| 3429 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3430 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3431 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3432 | // Handle the uncommon case of "123[Ptr]". |
| 3433 | BaseExpr = RHSExp; |
| 3434 | IndexExpr = LHSExp; |
| 3435 | ResultType = PTy->getPointeeType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3436 | if (!LangOpts.ObjCRuntime.allowsPointerArithmetic()) { |
| 3437 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3438 | << ResultType << BaseExpr->getSourceRange(); |
| 3439 | return ExprError(); |
| 3440 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3441 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3442 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3443 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3444 | VK = LHSExp->getValueKind(); |
| 3445 | if (VK != VK_RValue) |
| 3446 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3447 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3448 | // FIXME: need to deal with const... |
| 3449 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3450 | } else if (LHSTy->isArrayType()) { |
| 3451 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3452 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3453 | // wasn't promoted because of the C90 rule that doesn't |
| 3454 | // allow promoting non-lvalue arrays. Warn, then |
| 3455 | // force the promotion here. |
| 3456 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3457 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3458 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3459 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3460 | LHSTy = LHSExp->getType(); |
| 3461 | |
| 3462 | BaseExpr = LHSExp; |
| 3463 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3464 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3465 | } else if (RHSTy->isArrayType()) { |
| 3466 | // Same as previous, except for 123[f().a] case |
| 3467 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3468 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3469 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3470 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3471 | RHSTy = RHSExp->getType(); |
| 3472 | |
| 3473 | BaseExpr = RHSExp; |
| 3474 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3475 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3476 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3477 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3478 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3479 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3480 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3481 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3482 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3483 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3484 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3485 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3486 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3487 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3488 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3489 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3490 | // 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] | 3491 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3492 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3493 | // incomplete types are not object types. |
| 3494 | if (ResultType->isFunctionType()) { |
| 3495 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3496 | << ResultType << BaseExpr->getSourceRange(); |
| 3497 | return ExprError(); |
| 3498 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3500 | if (ResultType->isVoidType() && !getLangOpts().CPlusPlus) { |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3501 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 6628969 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3502 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
| 3503 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3504 | |
| 3505 | // C forbids expressions of unqualified void type from being l-values. |
| 3506 | // See IsCForbiddenLValueType. |
| 3507 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3508 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3509 | RequireCompleteType(LLoc, ResultType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3510 | diag::err_subscript_incomplete_type, BaseExpr)) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3511 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3512 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3513 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3514 | !ResultType.isCForbiddenLValueType()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3515 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3516 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3517 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3520 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3521 | FunctionDecl *FD, |
| 3522 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3523 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3524 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3525 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3526 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3528 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3529 | return ExprError(); |
| 3530 | } |
| 3531 | |
| 3532 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3533 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3534 | |
Richard Smith | adb1d4c | 2012-07-22 23:45:10 +0000 | [diff] [blame] | 3535 | EnterExpressionEvaluationContext EvalContext(*this, PotentiallyEvaluated, |
| 3536 | Param); |
| 3537 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3538 | // Instantiate the expression. |
| 3539 | MultiLevelTemplateArgumentList ArgList |
| 3540 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3541 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3542 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3543 | = ArgList.getInnermost(); |
Richard Smith | 7e54fb5 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3544 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 3545 | ArrayRef<TemplateArgument>(Innermost.first, |
| 3546 | Innermost.second)); |
Richard Smith | ab91ef1 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3547 | if (Inst) |
| 3548 | return ExprError(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3549 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3550 | ExprResult Result; |
| 3551 | { |
| 3552 | // C++ [dcl.fct.default]p5: |
| 3553 | // The names in the [default argument] expression are bound, and |
| 3554 | // the semantic constraints are checked, at the point where the |
| 3555 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3556 | ContextRAII SavedContext(*this, FD); |
Douglas Gregor | 7bdc152 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 3557 | LocalInstantiationScope Local(*this); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3558 | Result = SubstExpr(UninstExpr, ArgList); |
| 3559 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3560 | if (Result.isInvalid()) |
| 3561 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3562 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3563 | // Check the expression as an initializer for the parameter. |
| 3564 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3565 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3566 | InitializationKind Kind |
| 3567 | = InitializationKind::CreateCopy(Param->getLocation(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3568 | /*FIXME:EqualLoc*/UninstExpr->getLocStart()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3569 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3570 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3571 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3572 | Result = InitSeq.Perform(*this, Entity, Kind, ResultE); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3573 | if (Result.isInvalid()) |
| 3574 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3575 | |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 3576 | Expr *Arg = Result.takeAs<Expr>(); |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 3577 | CheckCompletedExpr(Arg, Param->getOuterLocStart()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3578 | // Build the default argument expression. |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 3579 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, Arg)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3582 | // If the default expression creates temporaries, we need to |
| 3583 | // push them to the current stack of expression temporaries so they'll |
| 3584 | // be properly destroyed. |
| 3585 | // FIXME: We should really be rebuilding the default argument with new |
| 3586 | // bound temporaries; see the comment in PR5810. |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3587 | // We don't need to do that with block decls, though, because |
| 3588 | // blocks in default argument expression can never capture anything. |
| 3589 | if (isa<ExprWithCleanups>(Param->getInit())) { |
| 3590 | // Set the "needs cleanups" bit regardless of whether there are |
| 3591 | // any explicit objects. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3592 | ExprNeedsCleanups = true; |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3593 | |
| 3594 | // Append all the objects to the cleanup list. Right now, this |
| 3595 | // should always be a no-op, because blocks in default argument |
| 3596 | // expressions should never be able to capture anything. |
| 3597 | assert(!cast<ExprWithCleanups>(Param->getInit())->getNumObjects() && |
| 3598 | "default argument expression has capturing blocks?"); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3599 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3600 | |
| 3601 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3602 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3603 | // as being "referenced". |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 3604 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg(), |
| 3605 | /*SkipLocalVariables=*/true); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3606 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3609 | |
| 3610 | Sema::VariadicCallType |
| 3611 | Sema::getVariadicCallType(FunctionDecl *FDecl, const FunctionProtoType *Proto, |
| 3612 | Expr *Fn) { |
| 3613 | if (Proto && Proto->isVariadic()) { |
| 3614 | if (dyn_cast_or_null<CXXConstructorDecl>(FDecl)) |
| 3615 | return VariadicConstructor; |
| 3616 | else if (Fn && Fn->getType()->isBlockPointerType()) |
| 3617 | return VariadicBlock; |
| 3618 | else if (FDecl) { |
| 3619 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3620 | if (Method->isInstance()) |
| 3621 | return VariadicMethod; |
| 3622 | } |
| 3623 | return VariadicFunction; |
| 3624 | } |
| 3625 | return VariadicDoesNotApply; |
| 3626 | } |
| 3627 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3628 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3629 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3630 | /// function prototype Proto. Call is the call expression itself, and |
| 3631 | /// Fn is the function expression. For a C++ member function, this |
| 3632 | /// routine does not attempt to convert the object argument. Returns |
| 3633 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3634 | bool |
| 3635 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3636 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3637 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3638 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3639 | SourceLocation RParenLoc, |
| 3640 | bool IsExecConfig) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3641 | // Bail out early if calling a builtin with custom typechecking. |
| 3642 | // We don't need to do this in the |
| 3643 | if (FDecl) |
| 3644 | if (unsigned ID = FDecl->getBuiltinID()) |
| 3645 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 3646 | return false; |
| 3647 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3648 | // 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] | 3649 | // assignment, to the types of the corresponding parameter, ... |
| 3650 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3651 | bool Invalid = false; |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3652 | unsigned MinArgs = FDecl ? FDecl->getMinRequiredArguments() : NumArgsInProto; |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3653 | unsigned FnKind = Fn->getType()->isBlockPointerType() |
| 3654 | ? 1 /* block */ |
| 3655 | : (IsExecConfig ? 3 /* kernel function (exec config) */ |
| 3656 | : 0 /* function */); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3657 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3658 | // If too few arguments are available (and we don't have default |
| 3659 | // arguments for the remaining parameters), don't make the call. |
| 3660 | if (NumArgs < NumArgsInProto) { |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3661 | if (NumArgs < MinArgs) { |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 3662 | if (MinArgs == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) |
| 3663 | Diag(RParenLoc, MinArgs == NumArgsInProto && !Proto->isVariadic() |
| 3664 | ? diag::err_typecheck_call_too_few_args_one |
| 3665 | : diag::err_typecheck_call_too_few_args_at_least_one) |
| 3666 | << FnKind |
| 3667 | << FDecl->getParamDecl(0) << Fn->getSourceRange(); |
| 3668 | else |
| 3669 | Diag(RParenLoc, MinArgs == NumArgsInProto && !Proto->isVariadic() |
| 3670 | ? diag::err_typecheck_call_too_few_args |
| 3671 | : diag::err_typecheck_call_too_few_args_at_least) |
| 3672 | << FnKind |
| 3673 | << MinArgs << NumArgs << Fn->getSourceRange(); |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3674 | |
| 3675 | // Emit the location of the prototype. |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3676 | if (FDecl && !FDecl->getBuiltinID() && !IsExecConfig) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3677 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3678 | << FDecl; |
| 3679 | |
| 3680 | return true; |
| 3681 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3682 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | // If too many are passed and not variadic, error on the extras and drop |
| 3686 | // them. |
| 3687 | if (NumArgs > NumArgsInProto) { |
| 3688 | if (!Proto->isVariadic()) { |
Richard Smith | c608c3c | 2012-05-15 06:21:54 +0000 | [diff] [blame] | 3689 | if (NumArgsInProto == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) |
| 3690 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3691 | MinArgs == NumArgsInProto |
| 3692 | ? diag::err_typecheck_call_too_many_args_one |
| 3693 | : diag::err_typecheck_call_too_many_args_at_most_one) |
| 3694 | << FnKind |
| 3695 | << FDecl->getParamDecl(0) << NumArgs << Fn->getSourceRange() |
| 3696 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3697 | Args[NumArgs-1]->getLocEnd()); |
| 3698 | else |
| 3699 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3700 | MinArgs == NumArgsInProto |
| 3701 | ? diag::err_typecheck_call_too_many_args |
| 3702 | : diag::err_typecheck_call_too_many_args_at_most) |
| 3703 | << FnKind |
| 3704 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
| 3705 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3706 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3707 | |
| 3708 | // Emit the location of the prototype. |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3709 | if (FDecl && !FDecl->getBuiltinID() && !IsExecConfig) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3710 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3711 | << FDecl; |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3712 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3713 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3714 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3715 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3716 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3717 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3718 | SmallVector<Expr *, 8> AllArgs; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3719 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, Fn); |
| 3720 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3721 | Invalid = GatherArgumentsForCall(Call->getLocStart(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3722 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3723 | if (Invalid) |
| 3724 | return true; |
| 3725 | unsigned TotalNumArgs = AllArgs.size(); |
| 3726 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3727 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3728 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3729 | return false; |
| 3730 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3731 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3732 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3733 | FunctionDecl *FDecl, |
| 3734 | const FunctionProtoType *Proto, |
| 3735 | unsigned FirstProtoArg, |
| 3736 | Expr **Args, unsigned NumArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3737 | SmallVector<Expr *, 8> &AllArgs, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3738 | VariadicCallType CallType, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 3739 | bool AllowExplicit, |
| 3740 | bool IsListInitialization) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3741 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3742 | unsigned NumArgsToCheck = NumArgs; |
| 3743 | bool Invalid = false; |
| 3744 | if (NumArgs != NumArgsInProto) |
| 3745 | // Use default arguments for missing arguments |
| 3746 | NumArgsToCheck = NumArgsInProto; |
| 3747 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3748 | // 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] | 3749 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3750 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3751 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3752 | Expr *Arg; |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3753 | ParmVarDecl *Param; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3754 | if (ArgIx < NumArgs) { |
| 3755 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3756 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3757 | if (RequireCompleteType(Arg->getLocStart(), |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3758 | ProtoArgType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3759 | diag::err_call_incomplete_argument, Arg)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3760 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3761 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3762 | // Pass the argument |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3763 | Param = 0; |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3764 | if (FDecl && i < FDecl->getNumParams()) |
| 3765 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3766 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3767 | // Strip the unbridged-cast placeholder expression off, if applicable. |
| 3768 | if (Arg->getType() == Context.ARCUnbridgedCastTy && |
| 3769 | FDecl && FDecl->hasAttr<CFAuditedTransferAttr>() && |
| 3770 | (!Param || !Param->hasAttr<CFConsumedAttr>())) |
| 3771 | Arg = stripARCUnbridgedCast(Arg); |
| 3772 | |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 3773 | InitializedEntity Entity = Param ? |
| 3774 | InitializedEntity::InitializeParameter(Context, Param, ProtoArgType) |
| 3775 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 3776 | Proto->isArgConsumed(i)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3777 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3778 | SourceLocation(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3779 | Owned(Arg), |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 3780 | IsListInitialization, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3781 | AllowExplicit); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3782 | if (ArgE.isInvalid()) |
| 3783 | return true; |
| 3784 | |
| 3785 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3786 | } else { |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3787 | Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3788 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3789 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3790 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3791 | if (ArgExpr.isInvalid()) |
| 3792 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3793 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3794 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3795 | } |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3796 | |
| 3797 | // Check for array bounds violations for each argument to the call. This |
| 3798 | // check only triggers warnings when the argument isn't a more complex Expr |
| 3799 | // with its own checking, such as a BinaryOperator. |
| 3800 | CheckArrayAccess(Arg); |
| 3801 | |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3802 | // Check for violations of C99 static array rules (C99 6.7.5.3p7). |
| 3803 | CheckStaticArrayArgument(CallLoc, Param, Arg); |
| 3804 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3805 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3806 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3807 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3808 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3809 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3810 | // Assume that extern "C" functions with variadic arguments that |
| 3811 | // return __unknown_anytype aren't *really* variadic. |
| 3812 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 3813 | FDecl && FDecl->isExternC()) { |
| 3814 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3815 | ExprResult arg; |
| 3816 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 3817 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 3818 | else |
| 3819 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3820 | Invalid |= arg.isInvalid(); |
| 3821 | AllArgs.push_back(arg.take()); |
| 3822 | } |
| 3823 | |
| 3824 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 3825 | } else { |
| 3826 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3827 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, |
| 3828 | FDecl); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3829 | Invalid |= Arg.isInvalid(); |
| 3830 | AllArgs.push_back(Arg.take()); |
| 3831 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3832 | } |
Ted Kremenek | 615eb7c | 2011-09-26 23:36:13 +0000 | [diff] [blame] | 3833 | |
| 3834 | // Check for array bounds violations. |
| 3835 | for (unsigned i = ArgIx; i != NumArgs; ++i) |
| 3836 | CheckArrayAccess(Args[i]); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3837 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3838 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3841 | static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) { |
| 3842 | TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3843 | if (ArrayTypeLoc ATL = TL.getAs<ArrayTypeLoc>()) |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3844 | S.Diag(PVD->getLocation(), diag::note_callee_static_array) |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3845 | << ATL.getLocalSourceRange(); |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | /// CheckStaticArrayArgument - If the given argument corresponds to a static |
| 3849 | /// array parameter, check that it is non-null, and that if it is formed by |
| 3850 | /// array-to-pointer decay, the underlying array is sufficiently large. |
| 3851 | /// |
| 3852 | /// C99 6.7.5.3p7: If the keyword static also appears within the [ and ] of the |
| 3853 | /// array type derivation, then for each call to the function, the value of the |
| 3854 | /// corresponding actual argument shall provide access to the first element of |
| 3855 | /// an array with at least as many elements as specified by the size expression. |
| 3856 | void |
| 3857 | Sema::CheckStaticArrayArgument(SourceLocation CallLoc, |
| 3858 | ParmVarDecl *Param, |
| 3859 | const Expr *ArgExpr) { |
| 3860 | // Static array parameters are not supported in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3861 | if (!Param || getLangOpts().CPlusPlus) |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3862 | return; |
| 3863 | |
| 3864 | QualType OrigTy = Param->getOriginalType(); |
| 3865 | |
| 3866 | const ArrayType *AT = Context.getAsArrayType(OrigTy); |
| 3867 | if (!AT || AT->getSizeModifier() != ArrayType::Static) |
| 3868 | return; |
| 3869 | |
| 3870 | if (ArgExpr->isNullPointerConstant(Context, |
| 3871 | Expr::NPC_NeverValueDependent)) { |
| 3872 | Diag(CallLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
| 3873 | DiagnoseCalleeStaticArrayParam(*this, Param); |
| 3874 | return; |
| 3875 | } |
| 3876 | |
| 3877 | const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT); |
| 3878 | if (!CAT) |
| 3879 | return; |
| 3880 | |
| 3881 | const ConstantArrayType *ArgCAT = |
| 3882 | Context.getAsConstantArrayType(ArgExpr->IgnoreParenImpCasts()->getType()); |
| 3883 | if (!ArgCAT) |
| 3884 | return; |
| 3885 | |
| 3886 | if (ArgCAT->getSize().ult(CAT->getSize())) { |
| 3887 | Diag(CallLoc, diag::warn_static_array_too_small) |
| 3888 | << ArgExpr->getSourceRange() |
| 3889 | << (unsigned) ArgCAT->getSize().getZExtValue() |
| 3890 | << (unsigned) CAT->getSize().getZExtValue(); |
| 3891 | DiagnoseCalleeStaticArrayParam(*this, Param); |
| 3892 | } |
| 3893 | } |
| 3894 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3895 | /// Given a function expression of unknown-any type, try to rebuild it |
| 3896 | /// to have a function type. |
| 3897 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 3898 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3899 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3900 | /// This provides the location of the left/right parens and a list of comma |
| 3901 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3902 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3903 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3904 | MultiExprArg ArgExprs, SourceLocation RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3905 | Expr *ExecConfig, bool IsExecConfig) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3906 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3907 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3908 | if (Result.isInvalid()) return ExprError(); |
| 3909 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3910 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3911 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3912 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3913 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3914 | if (!ArgExprs.empty()) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3915 | // Pseudo-destructor calls should not have any arguments. |
| 3916 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3917 | << FixItHint::CreateRemoval( |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3918 | SourceRange(ArgExprs[0]->getLocStart(), |
| 3919 | ArgExprs.back()->getLocEnd())); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3920 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3921 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3922 | return Owned(new (Context) CallExpr(Context, Fn, MultiExprArg(), |
| 3923 | Context.VoidTy, VK_RValue, |
| 3924 | RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3925 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3926 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3927 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3928 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3929 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3930 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3931 | bool Dependent = false; |
| 3932 | if (Fn->isTypeDependent()) |
| 3933 | Dependent = true; |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3934 | else if (Expr::hasAnyTypeDependentArguments(ArgExprs)) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3935 | Dependent = true; |
| 3936 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3937 | if (Dependent) { |
| 3938 | if (ExecConfig) { |
| 3939 | return Owned(new (Context) CUDAKernelCallExpr( |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3940 | Context, Fn, cast<CallExpr>(ExecConfig), ArgExprs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3941 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 3942 | } else { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3943 | return Owned(new (Context) CallExpr(Context, Fn, ArgExprs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3944 | Context.DependentTy, VK_RValue, |
| 3945 | RParenLoc)); |
| 3946 | } |
| 3947 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3948 | |
| 3949 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3950 | if (Fn->getType()->isRecordType()) |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3951 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, |
| 3952 | ArgExprs.data(), |
| 3953 | ArgExprs.size(), RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3954 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3955 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3956 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3957 | if (result.isInvalid()) return ExprError(); |
| 3958 | Fn = result.take(); |
| 3959 | } |
| 3960 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3961 | if (Fn->getType() == Context.BoundMemberTy) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3962 | return BuildCallToMemberFunction(S, Fn, LParenLoc, ArgExprs.data(), |
| 3963 | ArgExprs.size(), RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3964 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3965 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3966 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3967 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 3968 | if (Fn->getType() == Context.OverloadTy) { |
| 3969 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 3970 | |
Douglas Gregor | ee697e6 | 2011-10-13 18:10:35 +0000 | [diff] [blame] | 3971 | // 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] | 3972 | if (!find.HasFormOfMemberPointer) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3973 | OverloadExpr *ovl = find.Expression; |
| 3974 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 3975 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3976 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, ArgExprs.data(), |
| 3977 | ArgExprs.size(), RParenLoc, ExecConfig); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3978 | } else { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3979 | return BuildCallToMemberFunction(S, Fn, LParenLoc, ArgExprs.data(), |
| 3980 | ArgExprs.size(), RParenLoc); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3981 | } |
| 3982 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3985 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 3986 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3987 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3988 | if (result.isInvalid()) return ExprError(); |
| 3989 | Fn = result.take(); |
| 3990 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3991 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3992 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3993 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3994 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3995 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 3996 | if (UnOp->getOpcode() == UO_AddrOf) |
| 3997 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 3998 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3999 | if (isa<DeclRefExpr>(NakedFn)) |
| 4000 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4001 | else if (isa<MemberExpr>(NakedFn)) |
| 4002 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4003 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4004 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, ArgExprs.data(), |
| 4005 | ArgExprs.size(), RParenLoc, ExecConfig, |
| 4006 | IsExecConfig); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | ExprResult |
| 4010 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4011 | MultiExprArg ExecConfig, SourceLocation GGGLoc) { |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4012 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 4013 | if (!ConfigDecl) |
| 4014 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 4015 | << "cudaConfigureCall"); |
| 4016 | QualType ConfigQTy = ConfigDecl->getType(); |
| 4017 | |
| 4018 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4019 | ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4020 | MarkFunctionReferenced(LLLLoc, ConfigDecl); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4021 | |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 4022 | return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, 0, |
| 4023 | /*IsExecConfig=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4024 | } |
| 4025 | |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 4026 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 4027 | /// |
| 4028 | /// __builtin_astype( value, dst type ) |
| 4029 | /// |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4030 | ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy, |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 4031 | SourceLocation BuiltinLoc, |
| 4032 | SourceLocation RParenLoc) { |
| 4033 | ExprValueKind VK = VK_RValue; |
| 4034 | ExprObjectKind OK = OK_Ordinary; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4035 | QualType DstTy = GetTypeFromParser(ParsedDestTy); |
| 4036 | QualType SrcTy = E->getType(); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 4037 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 4038 | return ExprError(Diag(BuiltinLoc, |
| 4039 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | af9cddf | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 4040 | << DstTy |
| 4041 | << SrcTy |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4042 | << E->getSourceRange()); |
| 4043 | return Owned(new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4044 | RParenLoc)); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4047 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 4048 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4049 | /// unary-convert to an expression of function-pointer or |
| 4050 | /// block-pointer type. |
| 4051 | /// |
| 4052 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4053 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4054 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 4055 | SourceLocation LParenLoc, |
| 4056 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4057 | SourceLocation RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 4058 | Expr *Config, bool IsExecConfig) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4059 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 4060 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4061 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4062 | // Promote the function operand. |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 4063 | // We special-case function promotion here because we only allow promoting |
| 4064 | // builtin functions to function pointers in the callee of a call. |
| 4065 | ExprResult Result; |
| 4066 | if (BuiltinID && |
| 4067 | Fn->getType()->isSpecificBuiltinType(BuiltinType::BuiltinFn)) { |
| 4068 | Result = ImpCastExprToType(Fn, Context.getPointerType(FDecl->getType()), |
| 4069 | CK_BuiltinFnToFnPtr).take(); |
| 4070 | } else { |
| 4071 | Result = UsualUnaryConversions(Fn); |
| 4072 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4073 | if (Result.isInvalid()) |
| 4074 | return ExprError(); |
| 4075 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4076 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4077 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 4078 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4079 | CallExpr *TheCall; |
Eric Christopher | a27cb25 | 2012-05-30 01:14:28 +0000 | [diff] [blame] | 4080 | if (Config) |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4081 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 4082 | cast<CallExpr>(Config), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4083 | llvm::makeArrayRef(Args,NumArgs), |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4084 | Context.BoolTy, |
| 4085 | VK_RValue, |
| 4086 | RParenLoc); |
Eric Christopher | a27cb25 | 2012-05-30 01:14:28 +0000 | [diff] [blame] | 4087 | else |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4088 | TheCall = new (Context) CallExpr(Context, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4089 | llvm::makeArrayRef(Args, NumArgs), |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4090 | Context.BoolTy, |
| 4091 | VK_RValue, |
| 4092 | RParenLoc); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4093 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4094 | // Bail out early if calling a builtin with custom typechecking. |
| 4095 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 4096 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 4097 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4098 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4099 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4100 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4101 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 4102 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4103 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4104 | if (FuncT == 0) |
| 4105 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4106 | << Fn->getType() << Fn->getSourceRange()); |
| 4107 | } else if (const BlockPointerType *BPT = |
| 4108 | Fn->getType()->getAs<BlockPointerType>()) { |
| 4109 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 4110 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4111 | // Handle calls to expressions of unknown-any type. |
| 4112 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4113 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4114 | if (rewrite.isInvalid()) return ExprError(); |
| 4115 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 4116 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4117 | goto retry; |
| 4118 | } |
| 4119 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4120 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4121 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4122 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4123 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4124 | if (getLangOpts().CUDA) { |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 4125 | if (Config) { |
| 4126 | // CUDA: Kernel calls must be to global functions |
| 4127 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 4128 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 4129 | << FDecl->getName() << Fn->getSourceRange()); |
| 4130 | |
| 4131 | // CUDA: Kernel function must have 'void' return type |
| 4132 | if (!FuncT->getResultType()->isVoidType()) |
| 4133 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 4134 | << Fn->getType() << Fn->getSourceRange()); |
Peter Collingbourne | 8591a7f | 2011-10-02 23:49:15 +0000 | [diff] [blame] | 4135 | } else { |
| 4136 | // CUDA: Calls to global functions must be configured |
| 4137 | if (FDecl && FDecl->hasAttr<CUDAGlobalAttr>()) |
| 4138 | return ExprError(Diag(LParenLoc, diag::err_global_call_not_config) |
| 4139 | << FDecl->getName() << Fn->getSourceRange()); |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 4140 | } |
| 4141 | } |
| 4142 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4143 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4144 | if (CheckCallReturnType(FuncT->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4145 | Fn->getLocStart(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4146 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4147 | return ExprError(); |
| 4148 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4149 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4150 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4151 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4152 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4153 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT); |
| 4154 | if (Proto) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4155 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 4156 | RParenLoc, IsExecConfig)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4157 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4158 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4159 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4160 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4161 | if (FDecl) { |
| 4162 | // Check if we have too few/too many template arguments, based |
| 4163 | // on our knowledge of the function definition. |
| 4164 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 4165 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4166 | Proto = Def->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4167 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4168 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 4169 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4170 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4171 | |
| 4172 | // If the function we're calling isn't a function prototype, but we have |
| 4173 | // a function prototype from a prior declaratiom, use that prototype. |
| 4174 | if (!FDecl->hasPrototype()) |
| 4175 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4176 | } |
| 4177 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4178 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4179 | for (unsigned i = 0; i != NumArgs; i++) { |
| 4180 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4181 | |
| 4182 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4183 | InitializedEntity Entity |
| 4184 | = InitializedEntity::InitializeParameter(Context, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4185 | Proto->getArgType(i), |
| 4186 | Proto->isArgConsumed(i)); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4187 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 4188 | SourceLocation(), |
| 4189 | Owned(Arg)); |
| 4190 | if (ArgE.isInvalid()) |
| 4191 | return true; |
| 4192 | |
| 4193 | Arg = ArgE.takeAs<Expr>(); |
| 4194 | |
| 4195 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4196 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 4197 | |
| 4198 | if (ArgE.isInvalid()) |
| 4199 | return true; |
| 4200 | |
| 4201 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4202 | } |
| 4203 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4204 | if (RequireCompleteType(Arg->getLocStart(), |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4205 | Arg->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4206 | diag::err_call_incomplete_argument, Arg)) |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4207 | return ExprError(); |
| 4208 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4209 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4210 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4211 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4212 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4213 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 4214 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4215 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 4216 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4217 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 4218 | // Check for sentinels |
| 4219 | if (NDecl) |
| 4220 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4221 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4222 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4223 | if (FDecl) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4224 | if (CheckFunctionCall(FDecl, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4225 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4226 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4227 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 4228 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4229 | } else if (NDecl) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4230 | if (CheckBlockCall(NDecl, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4231 | return ExprError(); |
| 4232 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4233 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4234 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4237 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4238 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4239 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4240 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 4241 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4242 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4243 | |
| 4244 | TypeSourceInfo *TInfo; |
| 4245 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 4246 | if (!TInfo) |
| 4247 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 4248 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4249 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4252 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4253 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4254 | SourceLocation RParenLoc, Expr *LiteralExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4255 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 4256 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4257 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4258 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4259 | diag::err_illegal_decl_array_incomplete_type, |
| 4260 | SourceRange(LParenLoc, |
| 4261 | LiteralExpr->getSourceRange().getEnd()))) |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4262 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4263 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4264 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4265 | << SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4266 | } else if (!literalType->isDependentType() && |
| 4267 | RequireCompleteType(LParenLoc, literalType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4268 | diag::err_typecheck_decl_incomplete_type, |
| 4269 | SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4270 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4271 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4272 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4273 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4274 | InitializationKind Kind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4275 | = InitializationKind::CreateCStyleCast(LParenLoc, |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4276 | SourceRange(LParenLoc, RParenLoc), |
| 4277 | /*InitList=*/true); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4278 | InitializationSequence InitSeq(*this, Entity, Kind, &LiteralExpr, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4279 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, LiteralExpr, |
| 4280 | &literalType); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4281 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4282 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4283 | LiteralExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4284 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 4285 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4286 | if (isFileScope) { // 6.5.2.5p3 |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4287 | if (CheckForConstantInitializer(LiteralExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4288 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 4289 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4290 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4291 | // In C, compound literals are l-values for some reason. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4292 | ExprValueKind VK = getLangOpts().CPlusPlus ? VK_RValue : VK_LValue; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4293 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 4294 | return MaybeBindToTemporary( |
| 4295 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4296 | VK, LiteralExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4299 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4300 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4301 | SourceLocation RBraceLoc) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4302 | // Immediately handle non-overload placeholders. Overloads can be |
| 4303 | // resolved contextually, but everything else here can't. |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4304 | for (unsigned I = 0, E = InitArgList.size(); I != E; ++I) { |
| 4305 | if (InitArgList[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4306 | ExprResult result = CheckPlaceholderExpr(InitArgList[I]); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4307 | |
| 4308 | // Ignore failures; dropping the entire initializer list because |
| 4309 | // of one failure would be terrible for indexing/etc. |
| 4310 | if (result.isInvalid()) continue; |
| 4311 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4312 | InitArgList[I] = result.take(); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4313 | } |
| 4314 | } |
| 4315 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 4316 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4317 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4318 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4319 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitArgList, |
| 4320 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4321 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4322 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4325 | /// Do an explicit extend of the given block pointer if we're in ARC. |
| 4326 | static void maybeExtendBlockObject(Sema &S, ExprResult &E) { |
| 4327 | assert(E.get()->getType()->isBlockPointerType()); |
| 4328 | assert(E.get()->isRValue()); |
| 4329 | |
| 4330 | // Only do this in an r-value context. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4331 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4332 | |
| 4333 | E = ImplicitCastExpr::Create(S.Context, E.get()->getType(), |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4334 | CK_ARCExtendBlockObject, E.get(), |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4335 | /*base path*/ 0, VK_RValue); |
| 4336 | S.ExprNeedsCleanups = true; |
| 4337 | } |
| 4338 | |
| 4339 | /// Prepare a conversion of the given expression to an ObjC object |
| 4340 | /// pointer type. |
| 4341 | CastKind Sema::PrepareCastToObjCObjectPointer(ExprResult &E) { |
| 4342 | QualType type = E.get()->getType(); |
| 4343 | if (type->isObjCObjectPointerType()) { |
| 4344 | return CK_BitCast; |
| 4345 | } else if (type->isBlockPointerType()) { |
| 4346 | maybeExtendBlockObject(*this, E); |
| 4347 | return CK_BlockPointerToObjCPointerCast; |
| 4348 | } else { |
| 4349 | assert(type->isPointerType()); |
| 4350 | return CK_CPointerToObjCPointerCast; |
| 4351 | } |
| 4352 | } |
| 4353 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4354 | /// Prepares for a scalar cast, performing all the necessary stages |
| 4355 | /// except the final cast and returning the kind required. |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4356 | CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4357 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 4358 | // Also, callers should have filtered out the invalid cases with |
| 4359 | // pointers. Everything else should be possible. |
| 4360 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4361 | QualType SrcTy = Src.get()->getType(); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4362 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4363 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4364 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4365 | switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4366 | case Type::STK_MemberPointer: |
| 4367 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4368 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4369 | case Type::STK_CPointer: |
| 4370 | case Type::STK_BlockPointer: |
| 4371 | case Type::STK_ObjCObjectPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4372 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4373 | case Type::STK_CPointer: |
| 4374 | return CK_BitCast; |
| 4375 | case Type::STK_BlockPointer: |
| 4376 | return (SrcKind == Type::STK_BlockPointer |
| 4377 | ? CK_BitCast : CK_AnyPointerToBlockPointerCast); |
| 4378 | case Type::STK_ObjCObjectPointer: |
| 4379 | if (SrcKind == Type::STK_ObjCObjectPointer) |
| 4380 | return CK_BitCast; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4381 | if (SrcKind == Type::STK_CPointer) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4382 | return CK_CPointerToObjCPointerCast; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4383 | maybeExtendBlockObject(*this, Src); |
| 4384 | return CK_BlockPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4385 | case Type::STK_Bool: |
| 4386 | return CK_PointerToBoolean; |
| 4387 | case Type::STK_Integral: |
| 4388 | return CK_PointerToIntegral; |
| 4389 | case Type::STK_Floating: |
| 4390 | case Type::STK_FloatingComplex: |
| 4391 | case Type::STK_IntegralComplex: |
| 4392 | case Type::STK_MemberPointer: |
| 4393 | llvm_unreachable("illegal cast from pointer"); |
| 4394 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4395 | llvm_unreachable("Should have returned before this"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4396 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4397 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 4398 | case Type::STK_Integral: |
| 4399 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4400 | case Type::STK_CPointer: |
| 4401 | case Type::STK_ObjCObjectPointer: |
| 4402 | case Type::STK_BlockPointer: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4403 | if (Src.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4404 | Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4405 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4406 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4407 | case Type::STK_Bool: |
| 4408 | return CK_IntegralToBoolean; |
| 4409 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4410 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4411 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4412 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4413 | case Type::STK_IntegralComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4414 | Src = ImpCastExprToType(Src.take(), |
| 4415 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4416 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4417 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4418 | case Type::STK_FloatingComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4419 | Src = ImpCastExprToType(Src.take(), |
| 4420 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4421 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4422 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4423 | case Type::STK_MemberPointer: |
| 4424 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4425 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4426 | llvm_unreachable("Should have returned before this"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4427 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4428 | case Type::STK_Floating: |
| 4429 | switch (DestTy->getScalarTypeKind()) { |
| 4430 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4431 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4432 | case Type::STK_Bool: |
| 4433 | return CK_FloatingToBoolean; |
| 4434 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4435 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4436 | case Type::STK_FloatingComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4437 | Src = ImpCastExprToType(Src.take(), |
| 4438 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4439 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4440 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4441 | case Type::STK_IntegralComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4442 | Src = ImpCastExprToType(Src.take(), |
| 4443 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4444 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4445 | return CK_IntegralRealToComplex; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4446 | case Type::STK_CPointer: |
| 4447 | case Type::STK_ObjCObjectPointer: |
| 4448 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4449 | llvm_unreachable("valid float->pointer cast?"); |
| 4450 | case Type::STK_MemberPointer: |
| 4451 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4452 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4453 | llvm_unreachable("Should have returned before this"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4454 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4455 | case Type::STK_FloatingComplex: |
| 4456 | switch (DestTy->getScalarTypeKind()) { |
| 4457 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4458 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4459 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4460 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4461 | case Type::STK_Floating: { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4462 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); |
| 4463 | if (Context.hasSameType(ET, DestTy)) |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4464 | return CK_FloatingComplexToReal; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4465 | Src = ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4466 | return CK_FloatingCast; |
| 4467 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4468 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4469 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4470 | case Type::STK_Integral: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4471 | Src = ImpCastExprToType(Src.take(), |
| 4472 | SrcTy->castAs<ComplexType>()->getElementType(), |
| 4473 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4474 | return CK_FloatingToIntegral; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4475 | case Type::STK_CPointer: |
| 4476 | case Type::STK_ObjCObjectPointer: |
| 4477 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4478 | llvm_unreachable("valid complex float->pointer cast?"); |
| 4479 | case Type::STK_MemberPointer: |
| 4480 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4481 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4482 | llvm_unreachable("Should have returned before this"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4483 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4484 | case Type::STK_IntegralComplex: |
| 4485 | switch (DestTy->getScalarTypeKind()) { |
| 4486 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4487 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4488 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4489 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4490 | case Type::STK_Integral: { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4491 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); |
| 4492 | if (Context.hasSameType(ET, DestTy)) |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4493 | return CK_IntegralComplexToReal; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4494 | Src = ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4495 | return CK_IntegralCast; |
| 4496 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4497 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4498 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4499 | case Type::STK_Floating: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4500 | Src = ImpCastExprToType(Src.take(), |
| 4501 | SrcTy->castAs<ComplexType>()->getElementType(), |
| 4502 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4503 | return CK_IntegralToFloating; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4504 | case Type::STK_CPointer: |
| 4505 | case Type::STK_ObjCObjectPointer: |
| 4506 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4507 | llvm_unreachable("valid complex int->pointer cast?"); |
| 4508 | case Type::STK_MemberPointer: |
| 4509 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4510 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4511 | llvm_unreachable("Should have returned before this"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4512 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4513 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4514 | llvm_unreachable("Unhandled scalar cast"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4517 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4518 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4519 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4520 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4521 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4522 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4523 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4524 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4525 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4526 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4527 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4528 | } else |
| 4529 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4530 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4531 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4532 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4533 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4534 | return false; |
| 4535 | } |
| 4536 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4537 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 4538 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4539 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4540 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4541 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4542 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4543 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4544 | // an ExtVectorType. |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4545 | // In OpenCL, casts between vectors of different types are not allowed. |
| 4546 | // (See OpenCL 6.2). |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4547 | if (SrcTy->isVectorType()) { |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4548 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4549 | || (getLangOpts().OpenCL && |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4550 | (DestTy.getCanonicalType() != SrcTy.getCanonicalType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4551 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4552 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4553 | return ExprError(); |
| 4554 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4555 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4556 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4559 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4560 | // conversion will take place first from scalar to elt type, and then |
| 4561 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4562 | if (SrcTy->isPointerType()) |
| 4563 | return Diag(R.getBegin(), |
| 4564 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4565 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4566 | |
| 4567 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4568 | ExprResult CastExprRes = Owned(CastExpr); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4569 | CastKind CK = PrepareScalarCast(CastExprRes, DestElemTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4570 | if (CastExprRes.isInvalid()) |
| 4571 | return ExprError(); |
| 4572 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4573 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4574 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4575 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4578 | ExprResult |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4579 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
| 4580 | Declarator &D, ParsedType &Ty, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4581 | SourceLocation RParenLoc, Expr *CastExpr) { |
| 4582 | assert(!D.isInvalidType() && (CastExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4583 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4584 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4585 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, CastExpr->getType()); |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4586 | if (D.isInvalidType()) |
| 4587 | return ExprError(); |
| 4588 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4589 | if (getLangOpts().CPlusPlus) { |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4590 | // Check that there are no default arguments (C++ only). |
| 4591 | CheckExtraCXXDefaultArguments(D); |
| 4592 | } |
| 4593 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4594 | checkUnusedDeclAttributes(D); |
| 4595 | |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4596 | QualType castType = castTInfo->getType(); |
| 4597 | Ty = CreateParsedType(castType, castTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4598 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4599 | bool isVectorLiteral = false; |
| 4600 | |
| 4601 | // Check for an altivec or OpenCL literal, |
| 4602 | // i.e. all the elements are integer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4603 | ParenExpr *PE = dyn_cast<ParenExpr>(CastExpr); |
| 4604 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(CastExpr); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4605 | if ((getLangOpts().AltiVec || getLangOpts().OpenCL) |
Tobias Grosser | 37c31c2 | 2011-09-21 18:28:29 +0000 | [diff] [blame] | 4606 | && castType->isVectorType() && (PE || PLE)) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4607 | if (PLE && PLE->getNumExprs() == 0) { |
| 4608 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4609 | return ExprError(); |
| 4610 | } |
| 4611 | if (PE || PLE->getNumExprs() == 1) { |
| 4612 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4613 | if (!E->getType()->isVectorType()) |
| 4614 | isVectorLiteral = true; |
| 4615 | } |
| 4616 | else |
| 4617 | isVectorLiteral = true; |
| 4618 | } |
| 4619 | |
| 4620 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4621 | // then handle it as such. |
| 4622 | if (isVectorLiteral) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4623 | return BuildVectorLiteral(LParenLoc, RParenLoc, CastExpr, castTInfo); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4624 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4625 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4626 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4627 | // sequence of BinOp comma operators. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4628 | if (isa<ParenListExpr>(CastExpr)) { |
| 4629 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4630 | if (Result.isInvalid()) return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4631 | CastExpr = Result.take(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4632 | } |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4633 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4634 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4637 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4638 | SourceLocation RParenLoc, Expr *E, |
| 4639 | TypeSourceInfo *TInfo) { |
| 4640 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4641 | "Expected paren or paren list expression"); |
| 4642 | |
| 4643 | Expr **exprs; |
| 4644 | unsigned numExprs; |
| 4645 | Expr *subExpr; |
Richard Smith | afbcab8 | 2013-02-05 05:55:57 +0000 | [diff] [blame] | 4646 | SourceLocation LiteralLParenLoc, LiteralRParenLoc; |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4647 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
Richard Smith | afbcab8 | 2013-02-05 05:55:57 +0000 | [diff] [blame] | 4648 | LiteralLParenLoc = PE->getLParenLoc(); |
| 4649 | LiteralRParenLoc = PE->getRParenLoc(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4650 | exprs = PE->getExprs(); |
| 4651 | numExprs = PE->getNumExprs(); |
Richard Smith | afbcab8 | 2013-02-05 05:55:57 +0000 | [diff] [blame] | 4652 | } else { // isa<ParenExpr> by assertion at function entrance |
| 4653 | LiteralLParenLoc = cast<ParenExpr>(E)->getLParen(); |
| 4654 | LiteralRParenLoc = cast<ParenExpr>(E)->getRParen(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4655 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4656 | exprs = &subExpr; |
| 4657 | numExprs = 1; |
| 4658 | } |
| 4659 | |
| 4660 | QualType Ty = TInfo->getType(); |
| 4661 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4662 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4663 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4664 | const VectorType *VTy = Ty->getAs<VectorType>(); |
| 4665 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4666 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4667 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4668 | // initializers must be one or must match the size of the vector. |
| 4669 | // If a single value is specified in the initializer then it will be |
| 4670 | // replicated to all the components of the vector |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4671 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4672 | // The number of initializers must be one or must match the size of the |
| 4673 | // vector. If a single value is specified in the initializer then it will |
| 4674 | // be replicated to all the components of the vector |
| 4675 | if (numExprs == 1) { |
| 4676 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
Richard Smith | 61ffd09 | 2011-10-27 23:31:58 +0000 | [diff] [blame] | 4677 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
| 4678 | if (Literal.isInvalid()) |
| 4679 | return ExprError(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4680 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4681 | PrepareScalarCast(Literal, ElemTy)); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4682 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4683 | } |
| 4684 | else if (numExprs < numElems) { |
| 4685 | Diag(E->getExprLoc(), |
| 4686 | diag::err_incorrect_number_of_vector_initializers); |
| 4687 | return ExprError(); |
| 4688 | } |
| 4689 | else |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 4690 | initExprs.append(exprs, exprs + numExprs); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4691 | } |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4692 | else { |
| 4693 | // For OpenCL, when the number of initializers is a single value, |
| 4694 | // it will be replicated to all components of the vector. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4695 | if (getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4696 | VTy->getVectorKind() == VectorType::GenericVector && |
| 4697 | numExprs == 1) { |
| 4698 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
Richard Smith | 61ffd09 | 2011-10-27 23:31:58 +0000 | [diff] [blame] | 4699 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
| 4700 | if (Literal.isInvalid()) |
| 4701 | return ExprError(); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4702 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4703 | PrepareScalarCast(Literal, ElemTy)); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4704 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4705 | } |
| 4706 | |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 4707 | initExprs.append(exprs, exprs + numExprs); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4708 | } |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4709 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4710 | // braces instead of the original commas. |
Richard Smith | afbcab8 | 2013-02-05 05:55:57 +0000 | [diff] [blame] | 4711 | InitListExpr *initE = new (Context) InitListExpr(Context, LiteralLParenLoc, |
| 4712 | initExprs, LiteralRParenLoc); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4713 | initE->setType(Ty); |
| 4714 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4715 | } |
| 4716 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4717 | /// This is not an AltiVec-style cast or or C++ direct-initialization, so turn |
| 4718 | /// the ParenListExpr into a sequence of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4719 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4720 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { |
| 4721 | ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4722 | if (!E) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4723 | return Owned(OrigExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4724 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4725 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4726 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4727 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4728 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4729 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4730 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4731 | if (Result.isInvalid()) return ExprError(); |
| 4732 | |
| 4733 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4736 | ExprResult Sema::ActOnParenListExpr(SourceLocation L, |
| 4737 | SourceLocation R, |
| 4738 | MultiExprArg Val) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4739 | Expr *expr = new (Context) ParenListExpr(Context, L, Val, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4740 | return Owned(expr); |
| 4741 | } |
| 4742 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4743 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4744 | /// constant and the other is not a pointer. Returns true if a diagnostic is |
| 4745 | /// emitted. |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4746 | bool Sema::DiagnoseConditionalForNull(Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4747 | SourceLocation QuestionLoc) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4748 | Expr *NullExpr = LHSExpr; |
| 4749 | Expr *NonPointerExpr = RHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4750 | Expr::NullPointerConstantKind NullKind = |
| 4751 | NullExpr->isNullPointerConstant(Context, |
| 4752 | Expr::NPC_ValueDependentIsNotNull); |
| 4753 | |
| 4754 | if (NullKind == Expr::NPCK_NotNull) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4755 | NullExpr = RHSExpr; |
| 4756 | NonPointerExpr = LHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4757 | NullKind = |
| 4758 | NullExpr->isNullPointerConstant(Context, |
| 4759 | Expr::NPC_ValueDependentIsNotNull); |
| 4760 | } |
| 4761 | |
| 4762 | if (NullKind == Expr::NPCK_NotNull) |
| 4763 | return false; |
| 4764 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 4765 | if (NullKind == Expr::NPCK_ZeroExpression) |
| 4766 | return false; |
| 4767 | |
| 4768 | if (NullKind == Expr::NPCK_ZeroLiteral) { |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4769 | // In this case, check to make sure that we got here from a "NULL" |
| 4770 | // string in the source code. |
| 4771 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4772 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4773 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4774 | return false; |
| 4775 | } |
| 4776 | |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4777 | int DiagType = (NullKind == Expr::NPCK_CXX11_nullptr); |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4778 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4779 | << NonPointerExpr->getType() << DiagType |
| 4780 | << NonPointerExpr->getSourceRange(); |
| 4781 | return true; |
| 4782 | } |
| 4783 | |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4784 | /// \brief Return false if the condition expression is valid, true otherwise. |
| 4785 | static bool checkCondition(Sema &S, Expr *Cond) { |
| 4786 | QualType CondTy = Cond->getType(); |
| 4787 | |
| 4788 | // C99 6.5.15p2 |
| 4789 | if (CondTy->isScalarType()) return false; |
| 4790 | |
| 4791 | // 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] | 4792 | if (S.getLangOpts().OpenCL && CondTy->isVectorType()) |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4793 | return false; |
| 4794 | |
| 4795 | // Emit the proper error message. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4796 | S.Diag(Cond->getLocStart(), S.getLangOpts().OpenCL ? |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4797 | diag::err_typecheck_cond_expect_scalar : |
| 4798 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4799 | << CondTy; |
| 4800 | return true; |
| 4801 | } |
| 4802 | |
| 4803 | /// \brief Return false if the two expressions can be converted to a vector, |
| 4804 | /// true otherwise |
| 4805 | static bool checkConditionalConvertScalarsToVectors(Sema &S, ExprResult &LHS, |
| 4806 | ExprResult &RHS, |
| 4807 | QualType CondTy) { |
| 4808 | // Both operands should be of scalar type. |
| 4809 | if (!LHS.get()->getType()->isScalarType()) { |
| 4810 | S.Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4811 | << CondTy; |
| 4812 | return true; |
| 4813 | } |
| 4814 | if (!RHS.get()->getType()->isScalarType()) { |
| 4815 | S.Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4816 | << CondTy; |
| 4817 | return true; |
| 4818 | } |
| 4819 | |
| 4820 | // Implicity convert these scalars to the type of the condition. |
| 4821 | LHS = S.ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4822 | RHS = S.ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
| 4823 | return false; |
| 4824 | } |
| 4825 | |
| 4826 | /// \brief Handle when one or both operands are void type. |
| 4827 | static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, |
| 4828 | ExprResult &RHS) { |
| 4829 | Expr *LHSExpr = LHS.get(); |
| 4830 | Expr *RHSExpr = RHS.get(); |
| 4831 | |
| 4832 | if (!LHSExpr->getType()->isVoidType()) |
| 4833 | S.Diag(RHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4834 | << RHSExpr->getSourceRange(); |
| 4835 | if (!RHSExpr->getType()->isVoidType()) |
| 4836 | S.Diag(LHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4837 | << LHSExpr->getSourceRange(); |
| 4838 | LHS = S.ImpCastExprToType(LHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4839 | RHS = S.ImpCastExprToType(RHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4840 | return S.Context.VoidTy; |
| 4841 | } |
| 4842 | |
| 4843 | /// \brief Return false if the NullExpr can be promoted to PointerTy, |
| 4844 | /// true otherwise. |
| 4845 | static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, |
| 4846 | QualType PointerTy) { |
| 4847 | if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || |
| 4848 | !NullExpr.get()->isNullPointerConstant(S.Context, |
| 4849 | Expr::NPC_ValueDependentIsNull)) |
| 4850 | return true; |
| 4851 | |
| 4852 | NullExpr = S.ImpCastExprToType(NullExpr.take(), PointerTy, CK_NullToPointer); |
| 4853 | return false; |
| 4854 | } |
| 4855 | |
| 4856 | /// \brief Checks compatibility between two pointers and return the resulting |
| 4857 | /// type. |
| 4858 | static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, |
| 4859 | ExprResult &RHS, |
| 4860 | SourceLocation Loc) { |
| 4861 | QualType LHSTy = LHS.get()->getType(); |
| 4862 | QualType RHSTy = RHS.get()->getType(); |
| 4863 | |
| 4864 | if (S.Context.hasSameType(LHSTy, RHSTy)) { |
| 4865 | // Two identical pointers types are always compatible. |
| 4866 | return LHSTy; |
| 4867 | } |
| 4868 | |
| 4869 | QualType lhptee, rhptee; |
| 4870 | |
| 4871 | // Get the pointee types. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4872 | if (const BlockPointerType *LHSBTy = LHSTy->getAs<BlockPointerType>()) { |
| 4873 | lhptee = LHSBTy->getPointeeType(); |
| 4874 | rhptee = RHSTy->castAs<BlockPointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4875 | } else { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4876 | lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); |
| 4877 | rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4878 | } |
| 4879 | |
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4880 | // C99 6.5.15p6: If both operands are pointers to compatible types or to |
| 4881 | // differently qualified versions of compatible types, the result type is |
| 4882 | // a pointer to an appropriately qualified version of the composite |
| 4883 | // type. |
| 4884 | |
| 4885 | // Only CVR-qualifiers exist in the standard, and the differently-qualified |
| 4886 | // clause doesn't make sense for our extensions. E.g. address space 2 should |
| 4887 | // be incompatible with address space 3: they may live on different devices or |
| 4888 | // anything. |
| 4889 | Qualifiers lhQual = lhptee.getQualifiers(); |
| 4890 | Qualifiers rhQual = rhptee.getQualifiers(); |
| 4891 | |
| 4892 | unsigned MergedCVRQual = lhQual.getCVRQualifiers() | rhQual.getCVRQualifiers(); |
| 4893 | lhQual.removeCVRQualifiers(); |
| 4894 | rhQual.removeCVRQualifiers(); |
| 4895 | |
| 4896 | lhptee = S.Context.getQualifiedType(lhptee.getUnqualifiedType(), lhQual); |
| 4897 | rhptee = S.Context.getQualifiedType(rhptee.getUnqualifiedType(), rhQual); |
| 4898 | |
| 4899 | QualType CompositeTy = S.Context.mergeTypes(lhptee, rhptee); |
| 4900 | |
| 4901 | if (CompositeTy.isNull()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4902 | S.Diag(Loc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4903 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4904 | << RHS.get()->getSourceRange(); |
| 4905 | // In this situation, we assume void* type. No especially good |
| 4906 | // reason, but this is what gcc does, and we do have to pick |
| 4907 | // to get a consistent AST. |
| 4908 | QualType incompatTy = S.Context.getPointerType(S.Context.VoidTy); |
| 4909 | LHS = S.ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4910 | RHS = S.ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| 4911 | return incompatTy; |
| 4912 | } |
| 4913 | |
| 4914 | // The pointer types are compatible. |
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4915 | QualType ResultTy = CompositeTy.withCVRQualifiers(MergedCVRQual); |
| 4916 | ResultTy = S.Context.getPointerType(ResultTy); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4917 | |
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4918 | LHS = S.ImpCastExprToType(LHS.take(), ResultTy, CK_BitCast); |
| 4919 | RHS = S.ImpCastExprToType(RHS.take(), ResultTy, CK_BitCast); |
| 4920 | return ResultTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | /// \brief Return the resulting type when the operands are both block pointers. |
| 4924 | static QualType checkConditionalBlockPointerCompatibility(Sema &S, |
| 4925 | ExprResult &LHS, |
| 4926 | ExprResult &RHS, |
| 4927 | SourceLocation Loc) { |
| 4928 | QualType LHSTy = LHS.get()->getType(); |
| 4929 | QualType RHSTy = RHS.get()->getType(); |
| 4930 | |
| 4931 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4932 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4933 | QualType destType = S.Context.getPointerType(S.Context.VoidTy); |
| 4934 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4935 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4936 | return destType; |
| 4937 | } |
| 4938 | S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 4939 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4940 | << RHS.get()->getSourceRange(); |
| 4941 | return QualType(); |
| 4942 | } |
| 4943 | |
| 4944 | // We have 2 block pointer types. |
| 4945 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4946 | } |
| 4947 | |
| 4948 | /// \brief Return the resulting type when the operands are both pointers. |
| 4949 | static QualType |
| 4950 | checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, |
| 4951 | ExprResult &RHS, |
| 4952 | SourceLocation Loc) { |
| 4953 | // get the pointer types |
| 4954 | QualType LHSTy = LHS.get()->getType(); |
| 4955 | QualType RHSTy = RHS.get()->getType(); |
| 4956 | |
| 4957 | // get the "pointed to" types |
| 4958 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4959 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4960 | |
| 4961 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4962 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4963 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 4964 | QualType destPointee |
| 4965 | = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4966 | QualType destType = S.Context.getPointerType(destPointee); |
| 4967 | // Add qualifiers if necessary. |
| 4968 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
| 4969 | // Promote to void*. |
| 4970 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4971 | return destType; |
| 4972 | } |
| 4973 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 4974 | QualType destPointee |
| 4975 | = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4976 | QualType destType = S.Context.getPointerType(destPointee); |
| 4977 | // Add qualifiers if necessary. |
| 4978 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
| 4979 | // Promote to void*. |
| 4980 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4981 | return destType; |
| 4982 | } |
| 4983 | |
| 4984 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4985 | } |
| 4986 | |
| 4987 | /// \brief Return false if the first expression is not an integer and the second |
| 4988 | /// expression is not a pointer, true otherwise. |
| 4989 | static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, |
| 4990 | Expr* PointerExpr, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4991 | bool IsIntFirstExpr) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4992 | if (!PointerExpr->getType()->isPointerType() || |
| 4993 | !Int.get()->getType()->isIntegerType()) |
| 4994 | return false; |
| 4995 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4996 | Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; |
| 4997 | Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4998 | |
| 4999 | S.Diag(Loc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 5000 | << Expr1->getType() << Expr2->getType() |
| 5001 | << Expr1->getSourceRange() << Expr2->getSourceRange(); |
| 5002 | Int = S.ImpCastExprToType(Int.take(), PointerExpr->getType(), |
| 5003 | CK_IntegralToPointer); |
| 5004 | return true; |
| 5005 | } |
| 5006 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5007 | /// Note that LHS is not null here, even if this is the gnu "x ?: y" extension. |
| 5008 | /// In that case, LHS = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5009 | /// C99 6.5.15 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5010 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 5011 | ExprResult &RHS, ExprValueKind &VK, |
| 5012 | ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5013 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 5014 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5015 | ExprResult LHSResult = CheckPlaceholderExpr(LHS.get()); |
| 5016 | if (!LHSResult.isUsable()) return QualType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5017 | LHS = LHSResult; |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5018 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5019 | ExprResult RHSResult = CheckPlaceholderExpr(RHS.get()); |
| 5020 | if (!RHSResult.isUsable()) return QualType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5021 | RHS = RHSResult; |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5022 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5023 | // C++ is sufficiently different to merit its own checker. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5024 | if (getLangOpts().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5025 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5026 | |
| 5027 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5028 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5029 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5030 | Cond = UsualUnaryConversions(Cond.take()); |
| 5031 | if (Cond.isInvalid()) |
| 5032 | return QualType(); |
| 5033 | LHS = UsualUnaryConversions(LHS.take()); |
| 5034 | if (LHS.isInvalid()) |
| 5035 | return QualType(); |
| 5036 | RHS = UsualUnaryConversions(RHS.take()); |
| 5037 | if (RHS.isInvalid()) |
| 5038 | return QualType(); |
| 5039 | |
| 5040 | QualType CondTy = Cond.get()->getType(); |
| 5041 | QualType LHSTy = LHS.get()->getType(); |
| 5042 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5043 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5044 | // first, check the condition. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5045 | if (checkCondition(*this, Cond.get())) |
| 5046 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5047 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5048 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5049 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5050 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5051 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5052 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 5053 | // attempt to implicity convert them to the vector type to act like the |
| 5054 | // built in select. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5055 | if (getLangOpts().OpenCL && CondTy->isVectorType()) |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5056 | if (checkConditionalConvertScalarsToVectors(*this, LHS, RHS, CondTy)) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5057 | return QualType(); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5058 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5059 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 5060 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5061 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 5062 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5063 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5064 | return QualType(); |
| 5065 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5066 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5067 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5068 | // If both operands are the same structure or union type, the result is that |
| 5069 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5070 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5071 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5072 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5073 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5074 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5075 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5076 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5077 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5078 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5079 | // 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] | 5080 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5081 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5082 | return checkConditionalVoidType(*this, LHS, RHS); |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5083 | } |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5084 | |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5085 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 5086 | // the type of the other operand." |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5087 | if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; |
| 5088 | if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5089 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5090 | // All objective-c pointer type analysis is done here. |
| 5091 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 5092 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5093 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5094 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5095 | if (!compositeType.isNull()) |
| 5096 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5097 | |
| 5098 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5099 | // Handle block pointer types. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5100 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) |
| 5101 | return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, |
| 5102 | QuestionLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5103 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5104 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5105 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) |
| 5106 | return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, |
| 5107 | QuestionLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5108 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5109 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 5110 | // null pointers have been filtered out by this point. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5111 | if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, |
| 5112 | /*isIntFirstExpr=*/true)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5113 | return RHSTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5114 | if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, |
| 5115 | /*isIntFirstExpr=*/false)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5116 | return LHSTy; |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 5117 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5118 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 5119 | // constant and the other is not a pointer type. In this case, the user most |
| 5120 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5121 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5122 | return QualType(); |
| 5123 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5124 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5125 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5126 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 5127 | << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5128 | return QualType(); |
| 5129 | } |
| 5130 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5131 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 5132 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5133 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5134 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5135 | QualType LHSTy = LHS.get()->getType(); |
| 5136 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5137 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5138 | // Handle things like Class and struct objc_class*. Here we case the result |
| 5139 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 5140 | // redefinition type if an attempt is made to access its fields. |
| 5141 | if (LHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5142 | (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5143 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5144 | return LHSTy; |
| 5145 | } |
| 5146 | if (RHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5147 | (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5148 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5149 | return RHSTy; |
| 5150 | } |
| 5151 | // And the same for struct objc_object* / id |
| 5152 | if (LHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5153 | (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5154 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5155 | return LHSTy; |
| 5156 | } |
| 5157 | if (RHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5158 | (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5159 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5160 | return RHSTy; |
| 5161 | } |
| 5162 | // And the same for struct objc_selector* / SEL |
| 5163 | if (Context.isObjCSelType(LHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5164 | (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5165 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5166 | return LHSTy; |
| 5167 | } |
| 5168 | if (Context.isObjCSelType(RHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5169 | (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5170 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5171 | return RHSTy; |
| 5172 | } |
| 5173 | // Check constraints for Objective-C object pointers types. |
| 5174 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5175 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5176 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5177 | // Two identical object pointer types are always compatible. |
| 5178 | return LHSTy; |
| 5179 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5180 | const ObjCObjectPointerType *LHSOPT = LHSTy->castAs<ObjCObjectPointerType>(); |
| 5181 | const ObjCObjectPointerType *RHSOPT = RHSTy->castAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5182 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5183 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5184 | // If both operands are interfaces and either operand can be |
| 5185 | // assigned to the other, use that type as the composite |
| 5186 | // type. This allows |
| 5187 | // xxx ? (A*) a : (B*) b |
| 5188 | // where B is a subclass of A. |
| 5189 | // |
| 5190 | // Additionally, as for assignment, if either type is 'id' |
| 5191 | // allow silent coercion. Finally, if the types are |
| 5192 | // incompatible then make sure to use 'id' as the composite |
| 5193 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5194 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5195 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 5196 | // It could return the composite type. |
| 5197 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 5198 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 5199 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 5200 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 5201 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 5202 | RHSTy->isObjCQualifiedIdType()) && |
| 5203 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 5204 | // Need to handle "id<xx>" explicitly. |
| 5205 | // GCC allows qualified id and any Objective-C type to devolve to |
| 5206 | // id. Currently localizing to here until clear this should be |
| 5207 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 5208 | compositeType = Context.getObjCIdType(); |
| 5209 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 5210 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5211 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5212 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 5213 | ; |
| 5214 | else { |
| 5215 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 5216 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5217 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5218 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5219 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 5220 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5221 | return incompatTy; |
| 5222 | } |
| 5223 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5224 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 5225 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5226 | return compositeType; |
| 5227 | } |
| 5228 | // Check Objective-C object pointer types and 'void *' |
| 5229 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5230 | if (getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | a66eccb | 2012-02-25 00:23:44 +0000 | [diff] [blame] | 5231 | // ARC forbids the implicit conversion of object pointers to 'void *', |
| 5232 | // so these types are not compatible. |
| 5233 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy |
| 5234 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5235 | LHS = RHS = true; |
| 5236 | return QualType(); |
| 5237 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5238 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5239 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5240 | QualType destPointee |
| 5241 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 5242 | QualType destType = Context.getPointerType(destPointee); |
| 5243 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5244 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5245 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5246 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5247 | return destType; |
| 5248 | } |
| 5249 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5250 | if (getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | a66eccb | 2012-02-25 00:23:44 +0000 | [diff] [blame] | 5251 | // ARC forbids the implicit conversion of object pointers to 'void *', |
| 5252 | // so these types are not compatible. |
| 5253 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy |
| 5254 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5255 | LHS = RHS = true; |
| 5256 | return QualType(); |
| 5257 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5258 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5259 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 5260 | QualType destPointee |
| 5261 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 5262 | QualType destType = Context.getPointerType(destPointee); |
| 5263 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5264 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5265 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5266 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5267 | return destType; |
| 5268 | } |
| 5269 | return QualType(); |
| 5270 | } |
| 5271 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5272 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5273 | /// ParenRange in parentheses. |
| 5274 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5275 | const PartialDiagnostic &Note, |
| 5276 | SourceRange ParenRange) { |
| 5277 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 5278 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 5279 | EndLoc.isValid()) { |
| 5280 | Self.Diag(Loc, Note) |
| 5281 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 5282 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 5283 | } else { |
| 5284 | // We can't display the parentheses, so just show the bare note. |
| 5285 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5286 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 5290 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 5291 | } |
| 5292 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5293 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 5294 | /// expression, either using a built-in or overloaded operator, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5295 | /// and sets *OpCode to the opcode and *RHSExprs to the right-hand side |
| 5296 | /// expression. |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5297 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5298 | Expr **RHSExprs) { |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 5299 | // Don't strip parenthesis: we should not warn if E is in parenthesis. |
| 5300 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5301 | E = E->IgnoreConversionOperator(); |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 5302 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5303 | |
| 5304 | // Built-in binary operator. |
| 5305 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 5306 | if (IsArithmeticOp(OP->getOpcode())) { |
| 5307 | *Opcode = OP->getOpcode(); |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5308 | *RHSExprs = OP->getRHS(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5309 | return true; |
| 5310 | } |
| 5311 | } |
| 5312 | |
| 5313 | // Overloaded operator. |
| 5314 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 5315 | if (Call->getNumArgs() != 2) |
| 5316 | return false; |
| 5317 | |
| 5318 | // Make sure this is really a binary operator that is safe to pass into |
| 5319 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 5320 | OverloadedOperatorKind OO = Call->getOperator(); |
| 5321 | if (OO < OO_Plus || OO > OO_Arrow) |
| 5322 | return false; |
| 5323 | |
| 5324 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 5325 | if (IsArithmeticOp(OpKind)) { |
| 5326 | *Opcode = OpKind; |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5327 | *RHSExprs = Call->getArg(1); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5328 | return true; |
| 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | return false; |
| 5333 | } |
| 5334 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5335 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 5336 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 5337 | } |
| 5338 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5339 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 5340 | /// or is a logical expression such as (x==y) which has int type, but is |
| 5341 | /// commonly interpreted as boolean. |
| 5342 | static bool ExprLooksBoolean(Expr *E) { |
| 5343 | E = E->IgnoreParenImpCasts(); |
| 5344 | |
| 5345 | if (E->getType()->isBooleanType()) |
| 5346 | return true; |
| 5347 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 5348 | return IsLogicOp(OP->getOpcode()); |
| 5349 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 5350 | return OP->getOpcode() == UO_LNot; |
| 5351 | |
| 5352 | return false; |
| 5353 | } |
| 5354 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5355 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 5356 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 5357 | /// the conditional operator has higher precedence, for example: |
| 5358 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 5359 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 5360 | SourceLocation OpLoc, |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5361 | Expr *Condition, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5362 | Expr *LHSExpr, |
| 5363 | Expr *RHSExpr) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5364 | BinaryOperatorKind CondOpcode; |
| 5365 | Expr *CondRHS; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5366 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5367 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5368 | return; |
| 5369 | if (!ExprLooksBoolean(CondRHS)) |
| 5370 | return; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5371 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5372 | // The condition is an arithmetic binary expression, with a right- |
| 5373 | // hand side that looks boolean, so warn. |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5374 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5375 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5376 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5377 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5378 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5379 | SuggestParentheses(Self, OpLoc, |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 5380 | Self.PDiag(diag::note_precedence_silence) |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5381 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 5382 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 5383 | |
| 5384 | SuggestParentheses(Self, OpLoc, |
| 5385 | Self.PDiag(diag::note_precedence_conditional_first), |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5386 | SourceRange(CondRHS->getLocStart(), RHSExpr->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5389 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5390 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5391 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5392 | SourceLocation ColonLoc, |
| 5393 | Expr *CondExpr, Expr *LHSExpr, |
| 5394 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5395 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 5396 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5397 | OpaqueValueExpr *opaqueValue = 0; |
| 5398 | Expr *commonExpr = 0; |
| 5399 | if (LHSExpr == 0) { |
| 5400 | commonExpr = CondExpr; |
| 5401 | |
| 5402 | // We usually want to apply unary conversions *before* saving, except |
| 5403 | // in the special case of a C++ l-value conditional. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5404 | if (!(getLangOpts().CPlusPlus |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5405 | && !commonExpr->isTypeDependent() |
| 5406 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 5407 | && commonExpr->isGLValue() |
| 5408 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 5409 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 5410 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5411 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 5412 | if (commonRes.isInvalid()) |
| 5413 | return ExprError(); |
| 5414 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 5418 | commonExpr->getType(), |
| 5419 | commonExpr->getValueKind(), |
Douglas Gregor | 97df54e | 2012-02-23 22:17:26 +0000 | [diff] [blame] | 5420 | commonExpr->getObjectKind(), |
| 5421 | commonExpr); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5422 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5423 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5424 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5425 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5426 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5427 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 5428 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5429 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5430 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 5431 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5432 | return ExprError(); |
| 5433 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5434 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 5435 | RHS.get()); |
| 5436 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5437 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5438 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 5439 | LHS.take(), ColonLoc, |
| 5440 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5441 | |
| 5442 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5443 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5444 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
| 5445 | OK)); |
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 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5449 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5450 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5451 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5452 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5453 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5454 | checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) { |
| 5455 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5456 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5457 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5458 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5459 | const Type *lhptee, *rhptee; |
| 5460 | Qualifiers lhq, rhq; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5461 | llvm::tie(lhptee, lhq) = cast<PointerType>(LHSType)->getPointeeType().split(); |
| 5462 | llvm::tie(rhptee, rhq) = cast<PointerType>(RHSType)->getPointeeType().split(); |
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 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5465 | |
| 5466 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5467 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5468 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5469 | Qualifiers lq; |
| 5470 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5471 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 5472 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 5473 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 5474 | // Ignore lifetime for further calculation. |
| 5475 | lhq.removeObjCLifetime(); |
| 5476 | rhq.removeObjCLifetime(); |
| 5477 | } |
| 5478 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5479 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5480 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5481 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5482 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5483 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5484 | // 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] | 5485 | // and from void*. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5486 | else if (lhq.withoutObjCGCAttr().withoutObjCLifetime() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5487 | .compatiblyIncludes( |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5488 | rhq.withoutObjCGCAttr().withoutObjCLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5489 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 5490 | ; // keep old |
| 5491 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5492 | // Treat lifetime mismatches as fatal. |
| 5493 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 5494 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5495 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5496 | // For GCC compatibility, other qualifier mismatches are treated |
| 5497 | // as still compatible in C. |
| 5498 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5499 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5500 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5501 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5502 | // 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] | 5503 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5504 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5505 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5506 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5507 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5508 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5509 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5510 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5511 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5512 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5513 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5514 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5515 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5516 | |
| 5517 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5518 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5519 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5520 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5521 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5522 | // 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] | 5523 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5524 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5525 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5526 | // Check if the pointee types are compatible ignoring the sign. |
| 5527 | // We explicitly check for char so that we catch "char" vs |
| 5528 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5529 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5530 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5531 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5532 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5533 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5534 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5535 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5536 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5537 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5538 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5539 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5540 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5541 | // takes priority over sign incompatibility because the sign |
| 5542 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5543 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5544 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5545 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5546 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5547 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5548 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5549 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5550 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5551 | // the eventual target type is the same and the pointers have the same |
| 5552 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5553 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5554 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5555 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5556 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5557 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5558 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5559 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5560 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5561 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5562 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5563 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5564 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5565 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5566 | if (!S.getLangOpts().CPlusPlus && |
Fariborz Jahanian | 53c8167 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 5567 | S.IsNoReturnConversion(ltrans, rtrans, ltrans)) |
| 5568 | return Sema::IncompatiblePointer; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5569 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5572 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5573 | /// block pointer types are compatible or whether a block and normal pointer |
| 5574 | /// are compatible. It is more restrict than comparing two function pointer |
| 5575 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5576 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5577 | checkBlockPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5578 | QualType RHSType) { |
| 5579 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5580 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5581 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5582 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5583 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5584 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5585 | lhptee = cast<BlockPointerType>(LHSType)->getPointeeType(); |
| 5586 | rhptee = cast<BlockPointerType>(RHSType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5587 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5588 | // In C++, the types have to match exactly. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5589 | if (S.getLangOpts().CPlusPlus) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5590 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5591 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5592 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5593 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5594 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5595 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5596 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5597 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5598 | if (!S.Context.typesAreBlockPointerCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5599 | return Sema::IncompatibleBlockPointer; |
| 5600 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5601 | return ConvTy; |
| 5602 | } |
| 5603 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5604 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5605 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5606 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5607 | checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5608 | QualType RHSType) { |
| 5609 | assert(LHSType.isCanonical() && "LHS was not canonicalized!"); |
| 5610 | assert(RHSType.isCanonical() && "RHS was not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5611 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5612 | if (LHSType->isObjCBuiltinType()) { |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5613 | // Class is not compatible with ObjC object pointers. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5614 | if (LHSType->isObjCClassType() && !RHSType->isObjCBuiltinType() && |
| 5615 | !RHSType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5616 | return Sema::IncompatiblePointer; |
| 5617 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5618 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5619 | if (RHSType->isObjCBuiltinType()) { |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5620 | if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() && |
| 5621 | !LHSType->isObjCQualifiedClassType()) |
Fariborz Jahanian | 412a496 | 2011-09-15 20:40:18 +0000 | [diff] [blame] | 5622 | return Sema::IncompatiblePointer; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5623 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5624 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5625 | QualType lhptee = LHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5626 | QualType rhptee = RHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5627 | |
Fariborz Jahanian | f2b4f7b | 2012-01-12 22:12:08 +0000 | [diff] [blame] | 5628 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee) && |
| 5629 | // make an exception for id<P> |
| 5630 | !LHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5631 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5632 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5633 | if (S.Context.typesAreCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5634 | return Sema::Compatible; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5635 | if (LHSType->isObjCQualifiedIdType() || RHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5636 | return Sema::IncompatibleObjCQualifiedId; |
| 5637 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5638 | } |
| 5639 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5640 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5641 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5642 | QualType LHSType, QualType RHSType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5643 | // Fake up an opaque expression. We don't actually care about what |
| 5644 | // cast operations are required, so if CheckAssignmentConstraints |
| 5645 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5646 | // usually happen on valid code. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5647 | OpaqueValueExpr RHSExpr(Loc, RHSType, VK_RValue); |
| 5648 | ExprResult RHSPtr = &RHSExpr; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5649 | CastKind K = CK_Invalid; |
| 5650 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5651 | return CheckAssignmentConstraints(LHSType, RHSPtr, K); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5654 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5655 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5656 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5657 | /// |
| 5658 | /// int a, *pint; |
| 5659 | /// short *pshort; |
| 5660 | /// struct foo *pfoo; |
| 5661 | /// |
| 5662 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5663 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5664 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5665 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5666 | /// |
| 5667 | /// 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] | 5668 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5669 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5670 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5671 | Sema::AssignConvertType |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5672 | Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5673 | CastKind &Kind) { |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5674 | QualType RHSType = RHS.get()->getType(); |
| 5675 | QualType OrigLHSType = LHSType; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5676 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5677 | // Get canonical types. We're not formatting these types, just comparing |
| 5678 | // them. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5679 | LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType(); |
| 5680 | RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5681 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5682 | // Common case: no conversion required. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5683 | if (LHSType == RHSType) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5684 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5685 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 5688 | // If we have an atomic type, try a non-atomic assignment, then just add an |
| 5689 | // atomic qualification step. |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5690 | if (const AtomicType *AtomicTy = dyn_cast<AtomicType>(LHSType)) { |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 5691 | Sema::AssignConvertType result = |
| 5692 | CheckAssignmentConstraints(AtomicTy->getValueType(), RHS, Kind); |
| 5693 | if (result != Compatible) |
| 5694 | return result; |
| 5695 | if (Kind != CK_NoOp) |
| 5696 | RHS = ImpCastExprToType(RHS.take(), AtomicTy->getValueType(), Kind); |
| 5697 | Kind = CK_NonAtomicToAtomic; |
| 5698 | return Compatible; |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5699 | } |
| 5700 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5701 | // If the left-hand side is a reference type, then we are in a |
| 5702 | // (rare!) case where we've allowed the use of references in C, |
| 5703 | // e.g., as a parameter type in a built-in function. In this case, |
| 5704 | // just make sure that the type referenced is compatible with the |
| 5705 | // right-hand side type. The caller is responsible for adjusting |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5706 | // LHSType so that the resulting expression does not have reference |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5707 | // type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5708 | if (const ReferenceType *LHSTypeRef = LHSType->getAs<ReferenceType>()) { |
| 5709 | if (Context.typesAreCompatible(LHSTypeRef->getPointeeType(), RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5710 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5711 | return Compatible; |
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; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5714 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5715 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5716 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5717 | // to the same ExtVector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5718 | if (LHSType->isExtVectorType()) { |
| 5719 | if (RHSType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5720 | return Incompatible; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5721 | if (RHSType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5722 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5723 | // element type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5724 | QualType elType = cast<ExtVectorType>(LHSType)->getElementType(); |
| 5725 | if (elType != RHSType) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 5726 | Kind = PrepareScalarCast(RHS, elType); |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5727 | RHS = ImpCastExprToType(RHS.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5728 | } |
| 5729 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5730 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5731 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5732 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5733 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5734 | // Conversions to or from vector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5735 | if (LHSType->isVectorType() || RHSType->isVectorType()) { |
| 5736 | if (LHSType->isVectorType() && RHSType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5737 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5738 | // vector type and vice versa |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5739 | if (Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5740 | Kind = CK_BitCast; |
| 5741 | return Compatible; |
| 5742 | } |
| 5743 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5744 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5745 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5746 | // no bits are changed but the result type is different. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5747 | if (getLangOpts().LaxVectorConversions && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5748 | (Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5749 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5750 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5751 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5752 | } |
| 5753 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5754 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5755 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5756 | // Arithmetic conversions. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5757 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5758 | !(getLangOpts().CPlusPlus && LHSType->isEnumeralType())) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 5759 | Kind = PrepareScalarCast(RHS, LHSType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5760 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5761 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5762 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5763 | // Conversions to normal pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5764 | if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5765 | // U* -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5766 | if (isa<PointerType>(RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5767 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5768 | return checkPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5769 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5770 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5771 | // int -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5772 | if (RHSType->isIntegerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5773 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5774 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5775 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5776 | |
| 5777 | // C pointers are not compatible with ObjC object pointers, |
| 5778 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5779 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5780 | // - conversions to void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5781 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5782 | Kind = CK_BitCast; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5783 | return Compatible; |
| 5784 | } |
| 5785 | |
| 5786 | // - conversions from 'Class' to the redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5787 | if (RHSType->isObjCClassType() && |
| 5788 | Context.hasSameType(LHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5789 | Context.getObjCClassRedefinitionType())) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5790 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5791 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5792 | } |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 5793 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5794 | Kind = CK_BitCast; |
| 5795 | return IncompatiblePointer; |
| 5796 | } |
| 5797 | |
| 5798 | // U^ -> void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5799 | if (RHSType->getAs<BlockPointerType>()) { |
| 5800 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5801 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5802 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5803 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5804 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5805 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5806 | return Incompatible; |
| 5807 | } |
| 5808 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5809 | // Conversions to block pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5810 | if (isa<BlockPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5811 | // U^ -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5812 | if (RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5813 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5814 | return checkBlockPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
| 5817 | // int or null -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5818 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5819 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5820 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5821 | } |
| 5822 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5823 | // id -> T^ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5824 | if (getLangOpts().ObjC1 && RHSType->isObjCIdType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5825 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5826 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5827 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5828 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5829 | // void* -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5830 | if (const PointerType *RHSPT = RHSType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5831 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5832 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5833 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5834 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5835 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5836 | return Incompatible; |
| 5837 | } |
| 5838 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5839 | // Conversions to Objective-C pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5840 | if (isa<ObjCObjectPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5841 | // A* -> B* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5842 | if (RHSType->isObjCObjectPointerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5843 | Kind = CK_BitCast; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5844 | Sema::AssignConvertType result = |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5845 | checkObjCPointerTypesForAssignment(*this, LHSType, RHSType); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5846 | if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5847 | result == Compatible && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5848 | !CheckObjCARCUnavailableWeakConversion(OrigLHSType, RHSType)) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5849 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5850 | return result; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | // int or null -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5854 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5855 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5856 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5857 | } |
| 5858 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5859 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5860 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5861 | if (isa<PointerType>(RHSType)) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5862 | Kind = CK_CPointerToObjCPointerCast; |
| 5863 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5864 | // - conversions from 'void*' |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5865 | if (RHSType->isVoidPointerType()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5866 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | // - conversions to 'Class' from its redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5870 | if (LHSType->isObjCClassType() && |
| 5871 | Context.hasSameType(RHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5872 | Context.getObjCClassRedefinitionType())) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5873 | return Compatible; |
| 5874 | } |
| 5875 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5876 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5877 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5878 | |
| 5879 | // T^ -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5880 | if (RHSType->isBlockPointerType()) { |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 5881 | maybeExtendBlockObject(*this, RHS); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5882 | Kind = CK_BlockPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5883 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5886 | return Incompatible; |
| 5887 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5888 | |
| 5889 | // Conversions from pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5890 | if (isa<PointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5891 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5892 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5893 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5894 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5895 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5896 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5897 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5898 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5899 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5900 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5901 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5902 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5903 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5904 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5905 | |
| 5906 | // Conversions from Objective-C pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5907 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5908 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5909 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5910 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5911 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5912 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5913 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5914 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5915 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5916 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5917 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5918 | } |
| 5919 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5920 | return Incompatible; |
| 5921 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5922 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5923 | // struct A -> struct B |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5924 | if (isa<TagType>(LHSType) && isa<TagType>(RHSType)) { |
| 5925 | if (Context.typesAreCompatible(LHSType, RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5926 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5927 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5928 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5929 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5930 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5931 | return Incompatible; |
| 5932 | } |
| 5933 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5934 | /// \brief Constructs a transparent union from an expression that is |
| 5935 | /// used to initialize the transparent union. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5936 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
| 5937 | ExprResult &EResult, QualType UnionType, |
| 5938 | FieldDecl *Field) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5939 | // Build an initializer list that designates the appropriate member |
| 5940 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5941 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5942 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5943 | E, SourceLocation()); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5944 | Initializer->setType(UnionType); |
| 5945 | Initializer->setInitializedFieldInUnion(Field); |
| 5946 | |
| 5947 | // Build a compound literal constructing a value of the transparent |
| 5948 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5949 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5950 | EResult = S.Owned( |
| 5951 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5952 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5953 | } |
| 5954 | |
| 5955 | Sema::AssignConvertType |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5956 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5957 | ExprResult &RHS) { |
| 5958 | QualType RHSType = RHS.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5959 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5960 | // 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] | 5961 | // transparent_union GCC extension. |
| 5962 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5963 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5964 | return Incompatible; |
| 5965 | |
| 5966 | // The field to initialize within the transparent union. |
| 5967 | RecordDecl *UD = UT->getDecl(); |
| 5968 | FieldDecl *InitField = 0; |
| 5969 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5970 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5971 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5972 | it != itend; ++it) { |
| 5973 | if (it->getType()->isPointerType()) { |
| 5974 | // If the transparent union contains a pointer type, we allow: |
| 5975 | // 1) void pointer |
| 5976 | // 2) null pointer constant |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5977 | if (RHSType->isPointerType()) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5978 | if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5979 | RHS = ImpCastExprToType(RHS.take(), it->getType(), CK_BitCast); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5980 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5981 | break; |
| 5982 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5983 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5984 | if (RHS.get()->isNullPointerConstant(Context, |
| 5985 | Expr::NPC_ValueDependentIsNull)) { |
| 5986 | RHS = ImpCastExprToType(RHS.take(), it->getType(), |
| 5987 | CK_NullToPointer); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5988 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5989 | break; |
| 5990 | } |
| 5991 | } |
| 5992 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5993 | CastKind Kind = CK_Invalid; |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5994 | if (CheckAssignmentConstraints(it->getType(), RHS, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5995 | == Compatible) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5996 | RHS = ImpCastExprToType(RHS.take(), it->getType(), Kind); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5997 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5998 | break; |
| 5999 | } |
| 6000 | } |
| 6001 | |
| 6002 | if (!InitField) |
| 6003 | return Incompatible; |
| 6004 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6005 | ConstructTransparentUnion(*this, Context, RHS, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6006 | return Compatible; |
| 6007 | } |
| 6008 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6009 | Sema::AssignConvertType |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6010 | Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
| 6011 | bool Diagnose) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6012 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6013 | if (!LHSType->isRecordType() && !LHSType->isAtomicType()) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6014 | // C++ 5.17p3: If the left operand is not of class type, the |
| 6015 | // expression is implicitly converted (C++ 4) to the |
| 6016 | // cv-unqualified type of the left operand. |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 6017 | ExprResult Res; |
| 6018 | if (Diagnose) { |
| 6019 | Res = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 6020 | AA_Assigning); |
| 6021 | } else { |
| 6022 | ImplicitConversionSequence ICS = |
| 6023 | TryImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 6024 | /*SuppressUserConversions=*/false, |
| 6025 | /*AllowExplicit=*/false, |
| 6026 | /*InOverloadResolution=*/false, |
| 6027 | /*CStyle=*/false, |
| 6028 | /*AllowObjCWritebackConversion=*/false); |
| 6029 | if (ICS.isFailure()) |
| 6030 | return Incompatible; |
| 6031 | Res = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 6032 | ICS, AA_Assigning); |
| 6033 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6034 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6035 | return Incompatible; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 6036 | Sema::AssignConvertType result = Compatible; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6037 | if (getLangOpts().ObjCAutoRefCount && |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6038 | !CheckObjCARCUnavailableWeakConversion(LHSType, |
| 6039 | RHS.get()->getType())) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 6040 | result = IncompatibleObjCWeakRef; |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6041 | RHS = Res; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 6042 | return result; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 6046 | // structures. |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6047 | // FIXME: We also fall through for atomics; not sure what should |
| 6048 | // happen there, though. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6049 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6050 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6051 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 6052 | // a null pointer constant. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6053 | if ((LHSType->isPointerType() || |
| 6054 | LHSType->isObjCObjectPointerType() || |
| 6055 | LHSType->isBlockPointerType()) |
| 6056 | && RHS.get()->isNullPointerConstant(Context, |
| 6057 | Expr::NPC_ValueDependentIsNull)) { |
| 6058 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6059 | return Compatible; |
| 6060 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6061 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6062 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6063 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 6064 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 6065 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6066 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6067 | // Suppress this for references: C++ 8.5.3p5. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6068 | if (!LHSType->isReferenceType()) { |
| 6069 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 6070 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6071 | return Incompatible; |
| 6072 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6073 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6074 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6075 | Sema::AssignConvertType result = |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6076 | CheckAssignmentConstraints(LHSType, RHS, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6077 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6078 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 6079 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6080 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 6081 | // so that we can use references in built-in functions even in C. |
| 6082 | // The getNonReferenceType() call makes sure that the resulting expression |
| 6083 | // does not have reference type. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6084 | if (result != Incompatible && RHS.get()->getType() != LHSType) |
| 6085 | RHS = ImpCastExprToType(RHS.take(), |
| 6086 | LHSType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6087 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6088 | } |
| 6089 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6090 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, |
| 6091 | ExprResult &RHS) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6092 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6093 | << LHS.get()->getType() << RHS.get()->getType() |
| 6094 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6095 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6096 | } |
| 6097 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6098 | QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6099 | SourceLocation Loc, bool IsCompAssign) { |
Richard Smith | 9c129f8 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 6100 | if (!IsCompAssign) { |
| 6101 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 6102 | if (LHS.isInvalid()) |
| 6103 | return QualType(); |
| 6104 | } |
| 6105 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 6106 | if (RHS.isInvalid()) |
| 6107 | return QualType(); |
| 6108 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6109 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6110 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6111 | QualType LHSType = |
| 6112 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 6113 | QualType RHSType = |
| 6114 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6115 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6116 | // If the vector types are identical, return. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6117 | if (LHSType == RHSType) |
| 6118 | return LHSType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6119 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6120 | // Handle the case of equivalent AltiVec and GCC vector types |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6121 | if (LHSType->isVectorType() && RHSType->isVectorType() && |
| 6122 | Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
| 6123 | if (LHSType->isExtVectorType()) { |
| 6124 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 6125 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6126 | } |
| 6127 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6128 | if (!IsCompAssign) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6129 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
| 6130 | return RHSType; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6131 | } |
| 6132 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6133 | if (getLangOpts().LaxVectorConversions && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6134 | Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType)) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6135 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6136 | // vectors, the total size only needs to be the same. This is a |
| 6137 | // bitcast; no bits are changed but the result type is different. |
| 6138 | // FIXME: Should we really be allowing this? |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6139 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 6140 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6143 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 6144 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 6145 | bool swapped = false; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6146 | if (RHSType->isExtVectorType() && !IsCompAssign) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6147 | swapped = true; |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6148 | std::swap(RHS, LHS); |
| 6149 | std::swap(RHSType, LHSType); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6150 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6151 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6152 | // Handle the case of an ext vector and scalar. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6153 | if (const ExtVectorType *LV = LHSType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6154 | QualType EltTy = LV->getElementType(); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6155 | if (EltTy->isIntegralType(Context) && RHSType->isIntegralType(Context)) { |
| 6156 | int order = Context.getIntegerTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6157 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6158 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6159 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6160 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 6161 | if (swapped) std::swap(RHS, LHS); |
| 6162 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6163 | } |
| 6164 | } |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6165 | if (EltTy->isRealFloatingType() && RHSType->isScalarType() && |
| 6166 | RHSType->isRealFloatingType()) { |
| 6167 | int order = Context.getFloatingTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6168 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6169 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6170 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6171 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 6172 | if (swapped) std::swap(RHS, LHS); |
| 6173 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6174 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6175 | } |
| 6176 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6177 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6178 | // Vectors of different size or scalar and non-ext-vector are errors. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6179 | if (swapped) std::swap(RHS, LHS); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6180 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6181 | << LHS.get()->getType() << RHS.get()->getType() |
| 6182 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6183 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6186 | // checkArithmeticNull - Detect when a NULL constant is used improperly in an |
| 6187 | // expression. These are mainly cases where the null pointer is used as an |
| 6188 | // integer instead of a pointer. |
| 6189 | static void checkArithmeticNull(Sema &S, ExprResult &LHS, ExprResult &RHS, |
| 6190 | SourceLocation Loc, bool IsCompare) { |
| 6191 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 6192 | // but we use a bit of a hack here for speed; this is a relatively |
| 6193 | // hot path, and isNullPointerConstant is slow. |
| 6194 | bool LHSNull = isa<GNUNullExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 6195 | bool RHSNull = isa<GNUNullExpr>(RHS.get()->IgnoreParenImpCasts()); |
| 6196 | |
| 6197 | QualType NonNullType = LHSNull ? RHS.get()->getType() : LHS.get()->getType(); |
| 6198 | |
| 6199 | // Avoid analyzing cases where the result will either be invalid (and |
| 6200 | // diagnosed as such) or entirely valid and not something to warn about. |
| 6201 | if ((!LHSNull && !RHSNull) || NonNullType->isBlockPointerType() || |
| 6202 | NonNullType->isMemberPointerType() || NonNullType->isFunctionType()) |
| 6203 | return; |
| 6204 | |
| 6205 | // Comparison operations would not make sense with a null pointer no matter |
| 6206 | // what the other expression is. |
| 6207 | if (!IsCompare) { |
| 6208 | S.Diag(Loc, diag::warn_null_in_arithmetic_operation) |
| 6209 | << (LHSNull ? LHS.get()->getSourceRange() : SourceRange()) |
| 6210 | << (RHSNull ? RHS.get()->getSourceRange() : SourceRange()); |
| 6211 | return; |
| 6212 | } |
| 6213 | |
| 6214 | // The rest of the operations only make sense with a null pointer |
| 6215 | // if the other expression is a pointer. |
| 6216 | if (LHSNull == RHSNull || NonNullType->isAnyPointerType() || |
| 6217 | NonNullType->canDecayToPointerType()) |
| 6218 | return; |
| 6219 | |
| 6220 | S.Diag(Loc, diag::warn_null_in_comparison_operation) |
| 6221 | << LHSNull /* LHS is NULL */ << NonNullType |
| 6222 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6223 | } |
| 6224 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6225 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6226 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6227 | bool IsCompAssign, bool IsDiv) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6228 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6229 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6230 | if (LHS.get()->getType()->isVectorType() || |
| 6231 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6232 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6233 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6234 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6235 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6236 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6237 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6238 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6239 | if (compType.isNull() || !compType->isArithmeticType()) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6240 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6241 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6242 | // Check for division by zero. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6243 | if (IsDiv && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6244 | RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6245 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6246 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_division_by_zero) |
| 6247 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6248 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6249 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6250 | } |
| 6251 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6252 | QualType Sema::CheckRemainderOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6253 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6254 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6255 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6256 | if (LHS.get()->getType()->isVectorType() || |
| 6257 | RHS.get()->getType()->isVectorType()) { |
| 6258 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 6259 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6260 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6261 | return InvalidOperands(Loc, LHS, RHS); |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6262 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6263 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6264 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6265 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6266 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6267 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6268 | if (compType.isNull() || !compType->isIntegerType()) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6269 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6270 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6271 | // Check for remainder by zero. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6272 | if (RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6273 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6274 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_remainder_by_zero) |
| 6275 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6276 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6277 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6278 | } |
| 6279 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6280 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 6281 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6282 | Expr *LHSExpr, Expr *RHSExpr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6283 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6284 | ? diag::err_typecheck_pointer_arith_void_type |
| 6285 | : diag::ext_gnu_void_ptr) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6286 | << 1 /* two pointers */ << LHSExpr->getSourceRange() |
| 6287 | << RHSExpr->getSourceRange(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6288 | } |
| 6289 | |
| 6290 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 6291 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 6292 | Expr *Pointer) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6293 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6294 | ? diag::err_typecheck_pointer_arith_void_type |
| 6295 | : diag::ext_gnu_void_ptr) |
| 6296 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 6297 | } |
| 6298 | |
| 6299 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 6300 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 6301 | Expr *LHS, Expr *RHS) { |
| 6302 | assert(LHS->getType()->isAnyPointerType()); |
| 6303 | assert(RHS->getType()->isAnyPointerType()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6304 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6305 | ? diag::err_typecheck_pointer_arith_function_type |
| 6306 | : diag::ext_gnu_ptr_func_arith) |
| 6307 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 6308 | // We only show the second type if it differs from the first. |
| 6309 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 6310 | RHS->getType()) |
| 6311 | << RHS->getType()->getPointeeType() |
| 6312 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 6313 | } |
| 6314 | |
| 6315 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 6316 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 6317 | Expr *Pointer) { |
| 6318 | assert(Pointer->getType()->isAnyPointerType()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6319 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6320 | ? diag::err_typecheck_pointer_arith_function_type |
| 6321 | : diag::ext_gnu_ptr_func_arith) |
| 6322 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 6323 | << 0 /* one pointer, so only one type */ |
| 6324 | << Pointer->getSourceRange(); |
| 6325 | } |
| 6326 | |
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 6327 | /// \brief Emit error if Operand is incomplete pointer type |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6328 | /// |
| 6329 | /// \returns True if pointer has incomplete type |
| 6330 | static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, |
| 6331 | Expr *Operand) { |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6332 | assert(Operand->getType()->isAnyPointerType() && |
| 6333 | !Operand->getType()->isDependentType()); |
| 6334 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 6335 | return S.RequireCompleteType(Loc, PointeeTy, |
| 6336 | diag::err_typecheck_arithmetic_incomplete_type, |
| 6337 | PointeeTy, Operand->getSourceRange()); |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6338 | } |
| 6339 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6340 | /// \brief Check the validity of an arithmetic pointer operand. |
| 6341 | /// |
| 6342 | /// If the operand has pointer type, this code will check for pointer types |
| 6343 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 6344 | /// appropriately, including whether or not the use is supported as an |
| 6345 | /// extension. |
| 6346 | /// |
| 6347 | /// \returns True when the operand is valid to use (even if as an extension). |
| 6348 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 6349 | Expr *Operand) { |
| 6350 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 6351 | |
| 6352 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 6353 | if (PointeeTy->isVoidType()) { |
| 6354 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6355 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6356 | } |
| 6357 | if (PointeeTy->isFunctionType()) { |
| 6358 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6359 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6360 | } |
| 6361 | |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6362 | if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6363 | |
| 6364 | return true; |
| 6365 | } |
| 6366 | |
| 6367 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 6368 | /// operands. |
| 6369 | /// |
| 6370 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 6371 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 6372 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 6373 | /// are (potentially problematic) pointers. |
| 6374 | /// |
| 6375 | /// \returns True when the operand is valid to use (even if as an extension). |
| 6376 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6377 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6378 | bool isLHSPointer = LHSExpr->getType()->isAnyPointerType(); |
| 6379 | bool isRHSPointer = RHSExpr->getType()->isAnyPointerType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6380 | if (!isLHSPointer && !isRHSPointer) return true; |
| 6381 | |
| 6382 | QualType LHSPointeeTy, RHSPointeeTy; |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6383 | if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType(); |
| 6384 | if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6385 | |
| 6386 | // Check for arithmetic on pointers to incomplete types. |
| 6387 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 6388 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 6389 | if (isLHSVoidPtr || isRHSVoidPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6390 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHSExpr); |
| 6391 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHSExpr); |
| 6392 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6393 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6394 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6395 | } |
| 6396 | |
| 6397 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 6398 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 6399 | if (isLHSFuncPtr || isRHSFuncPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6400 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHSExpr); |
| 6401 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, |
| 6402 | RHSExpr); |
| 6403 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6404 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6405 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6406 | } |
| 6407 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6408 | if (isLHSPointer && checkArithmeticIncompletePointerType(S, Loc, LHSExpr)) |
| 6409 | return false; |
| 6410 | if (isRHSPointer && checkArithmeticIncompletePointerType(S, Loc, RHSExpr)) |
| 6411 | return false; |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6412 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6413 | return true; |
| 6414 | } |
| 6415 | |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6416 | /// diagnoseStringPlusInt - Emit a warning when adding an integer to a string |
| 6417 | /// literal. |
| 6418 | static void diagnoseStringPlusInt(Sema &Self, SourceLocation OpLoc, |
| 6419 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6420 | StringLiteral* StrExpr = dyn_cast<StringLiteral>(LHSExpr->IgnoreImpCasts()); |
| 6421 | Expr* IndexExpr = RHSExpr; |
| 6422 | if (!StrExpr) { |
| 6423 | StrExpr = dyn_cast<StringLiteral>(RHSExpr->IgnoreImpCasts()); |
| 6424 | IndexExpr = LHSExpr; |
| 6425 | } |
| 6426 | |
| 6427 | bool IsStringPlusInt = StrExpr && |
| 6428 | IndexExpr->getType()->isIntegralOrUnscopedEnumerationType(); |
| 6429 | if (!IsStringPlusInt) |
| 6430 | return; |
| 6431 | |
| 6432 | llvm::APSInt index; |
| 6433 | if (IndexExpr->EvaluateAsInt(index, Self.getASTContext())) { |
| 6434 | unsigned StrLenWithNull = StrExpr->getLength() + 1; |
| 6435 | if (index.isNonNegative() && |
| 6436 | index <= llvm::APSInt(llvm::APInt(index.getBitWidth(), StrLenWithNull), |
| 6437 | index.isUnsigned())) |
| 6438 | return; |
| 6439 | } |
| 6440 | |
| 6441 | SourceRange DiagRange(LHSExpr->getLocStart(), RHSExpr->getLocEnd()); |
| 6442 | Self.Diag(OpLoc, diag::warn_string_plus_int) |
| 6443 | << DiagRange << IndexExpr->IgnoreImpCasts()->getType(); |
| 6444 | |
| 6445 | // Only print a fixit for "str" + int, not for int + "str". |
| 6446 | if (IndexExpr == RHSExpr) { |
| 6447 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(RHSExpr->getLocEnd()); |
| 6448 | Self.Diag(OpLoc, diag::note_string_plus_int_silence) |
| 6449 | << FixItHint::CreateInsertion(LHSExpr->getLocStart(), "&") |
| 6450 | << FixItHint::CreateReplacement(SourceRange(OpLoc), "[") |
| 6451 | << FixItHint::CreateInsertion(EndLoc, "]"); |
| 6452 | } else |
| 6453 | Self.Diag(OpLoc, diag::note_string_plus_int_silence); |
| 6454 | } |
| 6455 | |
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 6456 | /// \brief Emit error when two pointers are incompatible. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6457 | static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6458 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6459 | assert(LHSExpr->getType()->isAnyPointerType()); |
| 6460 | assert(RHSExpr->getType()->isAnyPointerType()); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6461 | S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6462 | << LHSExpr->getType() << RHSExpr->getType() << LHSExpr->getSourceRange() |
| 6463 | << RHSExpr->getSourceRange(); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6464 | } |
| 6465 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6466 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6467 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc, |
| 6468 | QualType* CompLHSTy) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6469 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6470 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6471 | if (LHS.get()->getType()->isVectorType() || |
| 6472 | RHS.get()->getType()->isVectorType()) { |
| 6473 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6474 | if (CompLHSTy) *CompLHSTy = compType; |
| 6475 | return compType; |
| 6476 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6477 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6478 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6479 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6480 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6481 | |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6482 | // Diagnose "string literal" '+' int. |
| 6483 | if (Opc == BO_Add) |
| 6484 | diagnoseStringPlusInt(*this, Loc, LHS.get(), RHS.get()); |
| 6485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6486 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6487 | if (!compType.isNull() && compType->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6488 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6489 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6490 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6491 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6492 | // Type-checking. Ultimately the pointer's going to be in PExp; |
| 6493 | // note that we bias towards the LHS being the pointer. |
| 6494 | Expr *PExp = LHS.get(), *IExp = RHS.get(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6495 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6496 | bool isObjCPointer; |
| 6497 | if (PExp->getType()->isPointerType()) { |
| 6498 | isObjCPointer = false; |
| 6499 | } else if (PExp->getType()->isObjCObjectPointerType()) { |
| 6500 | isObjCPointer = true; |
| 6501 | } else { |
| 6502 | std::swap(PExp, IExp); |
| 6503 | if (PExp->getType()->isPointerType()) { |
| 6504 | isObjCPointer = false; |
| 6505 | } else if (PExp->getType()->isObjCObjectPointerType()) { |
| 6506 | isObjCPointer = true; |
| 6507 | } else { |
| 6508 | return InvalidOperands(Loc, LHS, RHS); |
| 6509 | } |
| 6510 | } |
| 6511 | assert(PExp->getType()->isAnyPointerType()); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6512 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6513 | if (!IExp->getType()->isIntegerType()) |
| 6514 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6515 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6516 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 6517 | return QualType(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6518 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6519 | if (isObjCPointer && checkArithmeticOnObjCPointer(*this, Loc, PExp)) |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6520 | return QualType(); |
| 6521 | |
| 6522 | // Check array bounds for pointer arithemtic |
| 6523 | CheckArrayAccess(PExp, IExp); |
| 6524 | |
| 6525 | if (CompLHSTy) { |
| 6526 | QualType LHSTy = Context.isPromotableBitField(LHS.get()); |
| 6527 | if (LHSTy.isNull()) { |
| 6528 | LHSTy = LHS.get()->getType(); |
| 6529 | if (LHSTy->isPromotableIntegerType()) |
| 6530 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6531 | } |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6532 | *CompLHSTy = LHSTy; |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6533 | } |
| 6534 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6535 | return PExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6536 | } |
| 6537 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6538 | // C99 6.5.6 |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6539 | QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6540 | SourceLocation Loc, |
| 6541 | QualType* CompLHSTy) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6542 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6543 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6544 | if (LHS.get()->getType()->isVectorType() || |
| 6545 | RHS.get()->getType()->isVectorType()) { |
| 6546 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6547 | if (CompLHSTy) *CompLHSTy = compType; |
| 6548 | return compType; |
| 6549 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6550 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6551 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6552 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6553 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6554 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6555 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6556 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6557 | // Handle the common case first (both operands are arithmetic). |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6558 | if (!compType.isNull() && compType->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6559 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6560 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6562 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6563 | // Either ptr - int or ptr - ptr. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6564 | if (LHS.get()->getType()->isAnyPointerType()) { |
| 6565 | QualType lpointee = LHS.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6566 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6567 | // Diagnose bad cases where we step over interface counts. |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6568 | if (LHS.get()->getType()->isObjCObjectPointerType() && |
| 6569 | checkArithmeticOnObjCPointer(*this, Loc, LHS.get())) |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6570 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6571 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6572 | // The result type of a pointer-int computation is the pointer type. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6573 | if (RHS.get()->getType()->isIntegerType()) { |
| 6574 | if (!checkArithmeticOpPointerOperand(*this, Loc, LHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6575 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6576 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6577 | // Check array bounds for pointer arithemtic |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 6578 | CheckArrayAccess(LHS.get(), RHS.get(), /*ArraySubscriptExpr*/0, |
| 6579 | /*AllowOnePastEnd*/true, /*IndexNegated*/true); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6580 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6581 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
| 6582 | return LHS.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6583 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6584 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6585 | // Handle pointer-pointer subtractions. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6586 | if (const PointerType *RHSPTy |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6587 | = RHS.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6588 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6589 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6590 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6591 | // Pointee types must be the same: C++ [expr.add] |
| 6592 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6593 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6594 | } |
| 6595 | } else { |
| 6596 | // Pointee types must be compatible C99 6.5.6p3 |
| 6597 | if (!Context.typesAreCompatible( |
| 6598 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6599 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6600 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6601 | return QualType(); |
| 6602 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6603 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6604 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6605 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6606 | LHS.get(), RHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6607 | return QualType(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6608 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6609 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6610 | return Context.getPointerDiffType(); |
| 6611 | } |
| 6612 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6613 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6614 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6615 | } |
| 6616 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6617 | static bool isScopedEnumerationType(QualType T) { |
| 6618 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6619 | return ET->getDecl()->isScoped(); |
| 6620 | return false; |
| 6621 | } |
| 6622 | |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6623 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6624 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6625 | QualType LHSType) { |
David Tweed | 7a83421 | 2013-01-07 16:43:27 +0000 | [diff] [blame] | 6626 | // OpenCL 6.3j: shift values are effectively % word size of LHS (more defined), |
| 6627 | // so skip remaining warnings as we don't want to modify values within Sema. |
| 6628 | if (S.getLangOpts().OpenCL) |
| 6629 | return; |
| 6630 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6631 | llvm::APSInt Right; |
| 6632 | // Check right/shifter operand |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6633 | if (RHS.get()->isValueDependent() || |
| 6634 | !RHS.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6635 | return; |
| 6636 | |
| 6637 | if (Right.isNegative()) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6638 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6639 | S.PDiag(diag::warn_shift_negative) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6640 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6641 | return; |
| 6642 | } |
| 6643 | llvm::APInt LeftBits(Right.getBitWidth(), |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6644 | S.Context.getTypeSize(LHS.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6645 | if (Right.uge(LeftBits)) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6646 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6647 | S.PDiag(diag::warn_shift_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6648 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6649 | return; |
| 6650 | } |
| 6651 | if (Opc != BO_Shl) |
| 6652 | return; |
| 6653 | |
| 6654 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6655 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6656 | // integers have defined behavior modulo one more than the maximum value |
| 6657 | // representable in the result type, so never warn for those. |
| 6658 | llvm::APSInt Left; |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6659 | if (LHS.get()->isValueDependent() || |
| 6660 | !LHS.get()->isIntegerConstantExpr(Left, S.Context) || |
| 6661 | LHSType->hasUnsignedIntegerRepresentation()) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6662 | return; |
| 6663 | llvm::APInt ResultBits = |
| 6664 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6665 | if (LeftBits.uge(ResultBits)) |
| 6666 | return; |
| 6667 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6668 | Result = Result.shl(Right); |
| 6669 | |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6670 | // Print the bit representation of the signed integer as an unsigned |
| 6671 | // hexadecimal number. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6672 | SmallString<40> HexResult; |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6673 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 6674 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6675 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6676 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6677 | // expected value. Thus we place this behind a different warning that can be |
| 6678 | // turned off separately if needed. |
| 6679 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6680 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6681 | << HexResult.str() << LHSType |
| 6682 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6683 | return; |
| 6684 | } |
| 6685 | |
| 6686 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6687 | << HexResult.str() << Result.getMinSignedBits() << LHSType |
| 6688 | << Left.getBitWidth() << LHS.get()->getSourceRange() |
| 6689 | << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6690 | } |
| 6691 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6692 | // C99 6.5.7 |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6693 | QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6694 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6695 | bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6696 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6697 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6698 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6699 | if (!LHS.get()->getType()->hasIntegerRepresentation() || |
| 6700 | !RHS.get()->getType()->hasIntegerRepresentation()) |
| 6701 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6702 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6703 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6704 | // hasIntegerRepresentation() above instead of this. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6705 | if (isScopedEnumerationType(LHS.get()->getType()) || |
| 6706 | isScopedEnumerationType(RHS.get()->getType())) { |
| 6707 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6708 | } |
| 6709 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6710 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6711 | if (LHS.get()->getType()->isVectorType() || |
| 6712 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6713 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6714 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6715 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6716 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6717 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6718 | // For the LHS, do usual unary conversions, but then reset them away |
| 6719 | // if this is a compound assignment. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6720 | ExprResult OldLHS = LHS; |
| 6721 | LHS = UsualUnaryConversions(LHS.take()); |
| 6722 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6723 | return QualType(); |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6724 | QualType LHSType = LHS.get()->getType(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6725 | if (IsCompAssign) LHS = OldLHS; |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6726 | |
| 6727 | // The RHS is simpler. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6728 | RHS = UsualUnaryConversions(RHS.take()); |
| 6729 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6730 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6731 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6732 | // Sanity-check shift operands |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6733 | DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6734 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6735 | // "The type of the result is that of the promoted left operand." |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6736 | return LHSType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6737 | } |
| 6738 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6739 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6740 | if (DeclContext *DC = D->getDeclContext()) { |
| 6741 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6742 | return true; |
| 6743 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6744 | return FD->isFunctionTemplateSpecialization(); |
| 6745 | } |
| 6746 | return false; |
| 6747 | } |
| 6748 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6749 | /// If two different enums are compared, raise a warning. |
Ted Kremenek | 16bdd3b | 2013-01-30 19:10:21 +0000 | [diff] [blame] | 6750 | static void checkEnumComparison(Sema &S, SourceLocation Loc, Expr *LHS, |
| 6751 | Expr *RHS) { |
| 6752 | QualType LHSStrippedType = LHS->IgnoreParenImpCasts()->getType(); |
| 6753 | QualType RHSStrippedType = RHS->IgnoreParenImpCasts()->getType(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6754 | |
| 6755 | const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>(); |
| 6756 | if (!LHSEnumType) |
| 6757 | return; |
| 6758 | const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>(); |
| 6759 | if (!RHSEnumType) |
| 6760 | return; |
| 6761 | |
| 6762 | // Ignore anonymous enums. |
| 6763 | if (!LHSEnumType->getDecl()->getIdentifier()) |
| 6764 | return; |
| 6765 | if (!RHSEnumType->getDecl()->getIdentifier()) |
| 6766 | return; |
| 6767 | |
| 6768 | if (S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) |
| 6769 | return; |
| 6770 | |
| 6771 | S.Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6772 | << LHSStrippedType << RHSStrippedType |
Ted Kremenek | 16bdd3b | 2013-01-30 19:10:21 +0000 | [diff] [blame] | 6773 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6774 | } |
| 6775 | |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6776 | /// \brief Diagnose bad pointer comparisons. |
| 6777 | static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6778 | ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6779 | bool IsError) { |
| 6780 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_distinct_pointers |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6781 | : diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6782 | << LHS.get()->getType() << RHS.get()->getType() |
| 6783 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6784 | } |
| 6785 | |
| 6786 | /// \brief Returns false if the pointers are converted to a composite type, |
| 6787 | /// true otherwise. |
| 6788 | static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6789 | ExprResult &LHS, ExprResult &RHS) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6790 | // C++ [expr.rel]p2: |
| 6791 | // [...] Pointer conversions (4.10) and qualification |
| 6792 | // conversions (4.4) are performed on pointer operands (or on |
| 6793 | // a pointer operand and a null pointer constant) to bring |
| 6794 | // them to their composite pointer type. [...] |
| 6795 | // |
| 6796 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
| 6797 | // comparisons of pointers. |
| 6798 | |
| 6799 | // C++ [expr.eq]p2: |
| 6800 | // In addition, pointers to members can be compared, or a pointer to |
| 6801 | // member and a null pointer constant. Pointer to member conversions |
| 6802 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6803 | // them to a common type. If one operand is a null pointer constant, |
| 6804 | // the common type is the type of the other operand. Otherwise, the |
| 6805 | // common type is a pointer to member type similar (4.4) to the type |
| 6806 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6807 | // that is the union of the cv-qualification signatures of the operand |
| 6808 | // types. |
| 6809 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6810 | QualType LHSType = LHS.get()->getType(); |
| 6811 | QualType RHSType = RHS.get()->getType(); |
| 6812 | assert((LHSType->isPointerType() && RHSType->isPointerType()) || |
| 6813 | (LHSType->isMemberPointerType() && RHSType->isMemberPointerType())); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6814 | |
| 6815 | bool NonStandardCompositeType = false; |
Richard Trieu | 43dff1b | 2011-09-02 21:44:27 +0000 | [diff] [blame] | 6816 | bool *BoolPtr = S.isSFINAEContext() ? 0 : &NonStandardCompositeType; |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6817 | QualType T = S.FindCompositePointerType(Loc, LHS, RHS, BoolPtr); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6818 | if (T.isNull()) { |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6819 | diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6820 | return true; |
| 6821 | } |
| 6822 | |
| 6823 | if (NonStandardCompositeType) |
| 6824 | S.Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6825 | << LHSType << RHSType << T << LHS.get()->getSourceRange() |
| 6826 | << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6827 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6828 | LHS = S.ImpCastExprToType(LHS.take(), T, CK_BitCast); |
| 6829 | RHS = S.ImpCastExprToType(RHS.take(), T, CK_BitCast); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6830 | return false; |
| 6831 | } |
| 6832 | |
| 6833 | static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6834 | ExprResult &LHS, |
| 6835 | ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6836 | bool IsError) { |
| 6837 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_fptr_to_void |
| 6838 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6839 | << LHS.get()->getType() << RHS.get()->getType() |
| 6840 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6841 | } |
| 6842 | |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6843 | static bool isObjCObjectLiteral(ExprResult &E) { |
Jordan Rose | 87da0b7 | 2012-11-09 23:55:21 +0000 | [diff] [blame] | 6844 | switch (E.get()->IgnoreParenImpCasts()->getStmtClass()) { |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6845 | case Stmt::ObjCArrayLiteralClass: |
| 6846 | case Stmt::ObjCDictionaryLiteralClass: |
| 6847 | case Stmt::ObjCStringLiteralClass: |
| 6848 | case Stmt::ObjCBoxedExprClass: |
| 6849 | return true; |
| 6850 | default: |
| 6851 | // Note that ObjCBoolLiteral is NOT an object literal! |
| 6852 | return false; |
| 6853 | } |
| 6854 | } |
| 6855 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6856 | static bool hasIsEqualMethod(Sema &S, const Expr *LHS, const Expr *RHS) { |
Benjamin Kramer | 2e85e74 | 2013-02-15 15:17:50 +0000 | [diff] [blame] | 6857 | const ObjCObjectPointerType *Type = |
| 6858 | LHS->getType()->getAs<ObjCObjectPointerType>(); |
| 6859 | |
| 6860 | // If this is not actually an Objective-C object, bail out. |
| 6861 | if (!Type) |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6862 | return false; |
Benjamin Kramer | 2e85e74 | 2013-02-15 15:17:50 +0000 | [diff] [blame] | 6863 | |
| 6864 | // Get the LHS object's interface type. |
| 6865 | QualType InterfaceType = Type->getPointeeType(); |
| 6866 | if (const ObjCObjectType *iQFaceTy = |
| 6867 | InterfaceType->getAsObjCQualifiedInterfaceType()) |
| 6868 | InterfaceType = iQFaceTy->getBaseType(); |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6869 | |
| 6870 | // If the RHS isn't an Objective-C object, bail out. |
| 6871 | if (!RHS->getType()->isObjCObjectPointerType()) |
| 6872 | return false; |
| 6873 | |
| 6874 | // Try to find the -isEqual: method. |
| 6875 | Selector IsEqualSel = S.NSAPIObj->getIsEqualSelector(); |
| 6876 | ObjCMethodDecl *Method = S.LookupMethodInObjectType(IsEqualSel, |
| 6877 | InterfaceType, |
| 6878 | /*instance=*/true); |
| 6879 | if (!Method) { |
| 6880 | if (Type->isObjCIdType()) { |
| 6881 | // For 'id', just check the global pool. |
| 6882 | Method = S.LookupInstanceMethodInGlobalPool(IsEqualSel, SourceRange(), |
| 6883 | /*receiverId=*/true, |
| 6884 | /*warn=*/false); |
| 6885 | } else { |
| 6886 | // Check protocols. |
Benjamin Kramer | 2e85e74 | 2013-02-15 15:17:50 +0000 | [diff] [blame] | 6887 | Method = S.LookupMethodInQualifiedType(IsEqualSel, Type, |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6888 | /*instance=*/true); |
| 6889 | } |
| 6890 | } |
| 6891 | |
| 6892 | if (!Method) |
| 6893 | return false; |
| 6894 | |
| 6895 | QualType T = Method->param_begin()[0]->getType(); |
| 6896 | if (!T->isObjCObjectPointerType()) |
| 6897 | return false; |
| 6898 | |
| 6899 | QualType R = Method->getResultType(); |
| 6900 | if (!R->isScalarType()) |
| 6901 | return false; |
| 6902 | |
| 6903 | return true; |
| 6904 | } |
| 6905 | |
Ted Kremenek | 3ee069b | 2012-12-21 21:59:36 +0000 | [diff] [blame] | 6906 | Sema::ObjCLiteralKind Sema::CheckLiteralKind(Expr *FromE) { |
| 6907 | FromE = FromE->IgnoreParenImpCasts(); |
| 6908 | switch (FromE->getStmtClass()) { |
| 6909 | default: |
| 6910 | break; |
| 6911 | case Stmt::ObjCStringLiteralClass: |
| 6912 | // "string literal" |
| 6913 | return LK_String; |
| 6914 | case Stmt::ObjCArrayLiteralClass: |
| 6915 | // "array literal" |
| 6916 | return LK_Array; |
| 6917 | case Stmt::ObjCDictionaryLiteralClass: |
| 6918 | // "dictionary literal" |
| 6919 | return LK_Dictionary; |
Ted Kremenek | d3292c8 | 2012-12-21 22:46:35 +0000 | [diff] [blame] | 6920 | case Stmt::BlockExprClass: |
| 6921 | return LK_Block; |
Ted Kremenek | 3ee069b | 2012-12-21 21:59:36 +0000 | [diff] [blame] | 6922 | case Stmt::ObjCBoxedExprClass: { |
Ted Kremenek | f530ff7 | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 6923 | Expr *Inner = cast<ObjCBoxedExpr>(FromE)->getSubExpr()->IgnoreParens(); |
Ted Kremenek | 3ee069b | 2012-12-21 21:59:36 +0000 | [diff] [blame] | 6924 | switch (Inner->getStmtClass()) { |
| 6925 | case Stmt::IntegerLiteralClass: |
| 6926 | case Stmt::FloatingLiteralClass: |
| 6927 | case Stmt::CharacterLiteralClass: |
| 6928 | case Stmt::ObjCBoolLiteralExprClass: |
| 6929 | case Stmt::CXXBoolLiteralExprClass: |
| 6930 | // "numeric literal" |
| 6931 | return LK_Numeric; |
| 6932 | case Stmt::ImplicitCastExprClass: { |
| 6933 | CastKind CK = cast<CastExpr>(Inner)->getCastKind(); |
| 6934 | // Boolean literals can be represented by implicit casts. |
| 6935 | if (CK == CK_IntegralToBoolean || CK == CK_IntegralCast) |
| 6936 | return LK_Numeric; |
| 6937 | break; |
| 6938 | } |
| 6939 | default: |
| 6940 | break; |
| 6941 | } |
| 6942 | return LK_Boxed; |
| 6943 | } |
| 6944 | } |
| 6945 | return LK_None; |
| 6946 | } |
| 6947 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6948 | static void diagnoseObjCLiteralComparison(Sema &S, SourceLocation Loc, |
| 6949 | ExprResult &LHS, ExprResult &RHS, |
| 6950 | BinaryOperator::Opcode Opc){ |
Jordan Rose | d5209ae | 2012-07-17 17:46:48 +0000 | [diff] [blame] | 6951 | Expr *Literal; |
| 6952 | Expr *Other; |
| 6953 | if (isObjCObjectLiteral(LHS)) { |
| 6954 | Literal = LHS.get(); |
| 6955 | Other = RHS.get(); |
| 6956 | } else { |
| 6957 | Literal = RHS.get(); |
| 6958 | Other = LHS.get(); |
| 6959 | } |
| 6960 | |
| 6961 | // Don't warn on comparisons against nil. |
| 6962 | Other = Other->IgnoreParenCasts(); |
| 6963 | if (Other->isNullPointerConstant(S.getASTContext(), |
| 6964 | Expr::NPC_ValueDependentIsNotNull)) |
| 6965 | return; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6966 | |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6967 | // This should be kept in sync with warn_objc_literal_comparison. |
Ted Kremenek | 3ee069b | 2012-12-21 21:59:36 +0000 | [diff] [blame] | 6968 | // LK_String should always be after the other literals, since it has its own |
| 6969 | // warning flag. |
| 6970 | Sema::ObjCLiteralKind LiteralKind = S.CheckLiteralKind(Literal); |
Ted Kremenek | d3292c8 | 2012-12-21 22:46:35 +0000 | [diff] [blame] | 6971 | assert(LiteralKind != Sema::LK_Block); |
Ted Kremenek | 3ee069b | 2012-12-21 21:59:36 +0000 | [diff] [blame] | 6972 | if (LiteralKind == Sema::LK_None) { |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6973 | llvm_unreachable("Unknown Objective-C object literal kind"); |
| 6974 | } |
| 6975 | |
Ted Kremenek | 3ee069b | 2012-12-21 21:59:36 +0000 | [diff] [blame] | 6976 | if (LiteralKind == Sema::LK_String) |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6977 | S.Diag(Loc, diag::warn_objc_string_literal_comparison) |
| 6978 | << Literal->getSourceRange(); |
| 6979 | else |
| 6980 | S.Diag(Loc, diag::warn_objc_literal_comparison) |
| 6981 | << LiteralKind << Literal->getSourceRange(); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6982 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6983 | if (BinaryOperator::isEqualityOp(Opc) && |
| 6984 | hasIsEqualMethod(S, LHS.get(), RHS.get())) { |
| 6985 | SourceLocation Start = LHS.get()->getLocStart(); |
| 6986 | SourceLocation End = S.PP.getLocForEndOfToken(RHS.get()->getLocEnd()); |
Fariborz Jahanian | 41f7b1a | 2013-02-01 20:04:49 +0000 | [diff] [blame] | 6987 | CharSourceRange OpRange = |
| 6988 | CharSourceRange::getCharRange(Loc, S.PP.getLocForEndOfToken(Loc)); |
Jordan Rose | 6deae7c | 2012-07-09 16:54:44 +0000 | [diff] [blame] | 6989 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6990 | S.Diag(Loc, diag::note_objc_literal_comparison_isequal) |
| 6991 | << FixItHint::CreateInsertion(Start, Opc == BO_EQ ? "[" : "![") |
Fariborz Jahanian | 41f7b1a | 2013-02-01 20:04:49 +0000 | [diff] [blame] | 6992 | << FixItHint::CreateReplacement(OpRange, " isEqual:") |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6993 | << FixItHint::CreateInsertion(End, "]"); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6994 | } |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6997 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6998 | QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6999 | SourceLocation Loc, unsigned OpaqueOpc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7000 | bool IsRelational) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 7001 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/true); |
| 7002 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7003 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7004 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 7005 | // Handle vector comparisons separately. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7006 | if (LHS.get()->getType()->isVectorType() || |
| 7007 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7008 | return CheckVectorCompareOperands(LHS, RHS, Loc, IsRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7009 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7010 | QualType LHSType = LHS.get()->getType(); |
| 7011 | QualType RHSType = RHS.get()->getType(); |
Benjamin Kramer | fec0959 | 2011-09-03 08:46:20 +0000 | [diff] [blame] | 7012 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7013 | Expr *LHSStripped = LHS.get()->IgnoreParenImpCasts(); |
| 7014 | Expr *RHSStripped = RHS.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7015 | |
Ted Kremenek | 16bdd3b | 2013-01-30 19:10:21 +0000 | [diff] [blame] | 7016 | checkEnumComparison(*this, Loc, LHS.get(), RHS.get()); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7017 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7018 | if (!LHSType->hasFloatingRepresentation() && |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7019 | !(LHSType->isBlockPointerType() && IsRelational) && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7020 | !LHS.get()->getLocStart().isMacroID() && |
| 7021 | !RHS.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7022 | // For non-floating point types, check for self-comparisons of the form |
| 7023 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7024 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 7025 | // |
| 7026 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 7027 | // expansion. Also don't warn about comparisons which are only self |
| 7028 | // comparisons within a template specialization. The warnings should catch |
| 7029 | // obvious cases in the definition of the template anyways. The idea is to |
| 7030 | // warn when the typed comparison operator will always evaluate to the same |
| 7031 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7032 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7033 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7034 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7035 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7036 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7037 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7038 | << (Opc == BO_EQ |
| 7039 | || Opc == BO_LE |
| 7040 | || Opc == BO_GE)); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7041 | } else if (LHSType->isArrayType() && RHSType->isArrayType() && |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7042 | !DRL->getDecl()->getType()->isReferenceType() && |
| 7043 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 7044 | // what is it always going to eval to? |
| 7045 | char always_evals_to; |
| 7046 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7047 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7048 | always_evals_to = 0; // false |
| 7049 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7050 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7051 | always_evals_to = 1; // true |
| 7052 | break; |
| 7053 | default: |
| 7054 | // best we can say is 'a constant' |
| 7055 | always_evals_to = 2; // e.g. array1 <= array2 |
| 7056 | break; |
| 7057 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7058 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7059 | << 1 // array |
| 7060 | << always_evals_to); |
| 7061 | } |
| 7062 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7063 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7064 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7065 | if (isa<CastExpr>(LHSStripped)) |
| 7066 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 7067 | if (isa<CastExpr>(RHSStripped)) |
| 7068 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7069 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7070 | // Warn about comparisons against a string constant (unless the other |
| 7071 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7072 | Expr *literalString = 0; |
| 7073 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7074 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7075 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7076 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7077 | literalString = LHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7078 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7079 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 7080 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7081 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7082 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7083 | literalString = RHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7084 | literalStringStripped = RHSStripped; |
| 7085 | } |
| 7086 | |
| 7087 | if (literalString) { |
| 7088 | std::string resultComparison; |
| 7089 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7090 | case BO_LT: resultComparison = ") < 0"; break; |
| 7091 | case BO_GT: resultComparison = ") > 0"; break; |
| 7092 | case BO_LE: resultComparison = ") <= 0"; break; |
| 7093 | case BO_GE: resultComparison = ") >= 0"; break; |
| 7094 | case BO_EQ: resultComparison = ") == 0"; break; |
| 7095 | case BO_NE: resultComparison = ") != 0"; break; |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7096 | default: llvm_unreachable("Invalid comparison operator"); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7097 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7098 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7099 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 7100 | PDiag(diag::warn_stringcompare) |
| 7101 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 7102 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7103 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 7104 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7105 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7106 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7107 | if (LHS.get()->getType()->isArithmeticType() && |
| 7108 | RHS.get()->getType()->isArithmeticType()) { |
| 7109 | UsualArithmeticConversions(LHS, RHS); |
| 7110 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7111 | return QualType(); |
| 7112 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7113 | else { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7114 | LHS = UsualUnaryConversions(LHS.take()); |
| 7115 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7116 | return QualType(); |
| 7117 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7118 | RHS = UsualUnaryConversions(RHS.take()); |
| 7119 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7120 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7121 | } |
| 7122 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7123 | LHSType = LHS.get()->getType(); |
| 7124 | RHSType = RHS.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7125 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7126 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7127 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7128 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7129 | if (IsRelational) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7130 | if (LHSType->isRealType() && RHSType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7131 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7132 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 7133 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7134 | if (LHSType->hasFloatingRepresentation()) |
| 7135 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7136 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7137 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7138 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7139 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7140 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7141 | bool LHSIsNull = LHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7142 | Expr::NPC_ValueDependentIsNull); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7143 | bool RHSIsNull = RHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7144 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7145 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7146 | // All of the following pointer-related warnings are GCC extensions, except |
| 7147 | // when handling null pointer constants. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7148 | if (LHSType->isPointerType() && RHSType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7149 | QualType LCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7150 | LHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7151 | QualType RCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7152 | RHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7153 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7154 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7155 | if (LCanPointeeTy == RCanPointeeTy) |
| 7156 | return ResultTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7157 | if (!IsRelational && |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7158 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7159 | // Valid unless comparison between non-null pointer and function pointer |
| 7160 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7161 | // In a SFINAE context, we treat this as a hard error to maintain |
| 7162 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7163 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7164 | && !LHSIsNull && !RHSIsNull) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7165 | diagnoseFunctionPointerToVoidComparison( |
David Blaikie | 0adb175 | 2013-02-21 06:05:05 +0000 | [diff] [blame] | 7166 | *this, Loc, LHS, RHS, /*isError*/ (bool)isSFINAEContext()); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7167 | |
| 7168 | if (isSFINAEContext()) |
| 7169 | return QualType(); |
| 7170 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7171 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7172 | return ResultTy; |
| 7173 | } |
| 7174 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7175 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7176 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7177 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7178 | else |
| 7179 | return ResultTy; |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7180 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7181 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 7182 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 7183 | RCanPointeeTy.getUnqualifiedType())) { |
| 7184 | // Valid unless a relational comparison of function pointers |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7185 | if (IsRelational && LCanPointeeTy->isFunctionType()) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7186 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7187 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7188 | << RHS.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7189 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7190 | } else if (!IsRelational && |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7191 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7192 | // Valid unless comparison between non-null pointer and function pointer |
| 7193 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7194 | && !LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7195 | diagnoseFunctionPointerToVoidComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7196 | /*isError*/false); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7197 | } else { |
| 7198 | // Invalid |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7199 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7200 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7201 | if (LCanPointeeTy != RCanPointeeTy) { |
| 7202 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7203 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7204 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7205 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7206 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7207 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7208 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7209 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7210 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7211 | // Comparison of nullptr_t with itself. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7212 | if (LHSType->isNullPtrType() && RHSType->isNullPtrType()) |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7213 | return ResultTy; |
| 7214 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7215 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7216 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7217 | if (RHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7218 | ((LHSType->isAnyPointerType() || LHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7219 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7220 | (LHSType->isMemberPointerType() || LHSType->isBlockPointerType())))) { |
| 7221 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 7222 | LHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7223 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7224 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7225 | return ResultTy; |
| 7226 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7227 | if (LHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7228 | ((RHSType->isAnyPointerType() || RHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7229 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7230 | (RHSType->isMemberPointerType() || RHSType->isBlockPointerType())))) { |
| 7231 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 7232 | RHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7233 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7234 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7235 | return ResultTy; |
| 7236 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7237 | |
| 7238 | // Comparison of member pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7239 | if (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7240 | LHSType->isMemberPointerType() && RHSType->isMemberPointerType()) { |
| 7241 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7242 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7243 | else |
| 7244 | return ResultTy; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7245 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7246 | |
| 7247 | // Handle scoped enumeration types specifically, since they don't promote |
| 7248 | // to integers. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7249 | if (LHS.get()->getType()->isEnumeralType() && |
| 7250 | Context.hasSameUnqualifiedType(LHS.get()->getType(), |
| 7251 | RHS.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7252 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7253 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7254 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7255 | // Handle block pointer types. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7256 | if (!IsRelational && LHSType->isBlockPointerType() && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7257 | RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7258 | QualType lpointee = LHSType->castAs<BlockPointerType>()->getPointeeType(); |
| 7259 | QualType rpointee = RHSType->castAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7260 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7261 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7262 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7263 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7264 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7265 | << RHS.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7266 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7267 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7268 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7269 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7270 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7271 | // Allow block pointers to be compared with null pointer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7272 | if (!IsRelational |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7273 | && ((LHSType->isBlockPointerType() && RHSType->isPointerType()) |
| 7274 | || (LHSType->isPointerType() && RHSType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7275 | if (!LHSIsNull && !RHSIsNull) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7276 | if (!((RHSType->isPointerType() && RHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7277 | ->getPointeeType()->isVoidType()) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7278 | || (LHSType->isPointerType() && LHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7279 | ->getPointeeType()->isVoidType()))) |
| 7280 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7281 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7282 | << RHS.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7283 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7284 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7285 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 7286 | RHSType->isPointerType() ? CK_BitCast |
| 7287 | : CK_AnyPointerToBlockPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7288 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7289 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 7290 | LHSType->isPointerType() ? CK_BitCast |
| 7291 | : CK_AnyPointerToBlockPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7292 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7293 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7294 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7295 | if (LHSType->isObjCObjectPointerType() || |
| 7296 | RHSType->isObjCObjectPointerType()) { |
| 7297 | const PointerType *LPT = LHSType->getAs<PointerType>(); |
| 7298 | const PointerType *RPT = RHSType->getAs<PointerType>(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7299 | if (LPT || RPT) { |
| 7300 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 7301 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7302 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7303 | if (!LPtrToVoid && !RPtrToVoid && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7304 | !Context.typesAreCompatible(LHSType, RHSType)) { |
| 7305 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7306 | /*isError*/false); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7307 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7308 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7309 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 7310 | RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7311 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7312 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 7313 | LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7314 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7315 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7316 | if (LHSType->isObjCObjectPointerType() && |
| 7317 | RHSType->isObjCObjectPointerType()) { |
| 7318 | if (!Context.areComparableObjCPointerTypes(LHSType, RHSType)) |
| 7319 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7320 | /*isError*/false); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 7321 | if (isObjCObjectLiteral(LHS) || isObjCObjectLiteral(RHS)) |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 7322 | diagnoseObjCLiteralComparison(*this, Loc, LHS, RHS, Opc); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 7323 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7324 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7325 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7326 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7327 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7328 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7329 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7330 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7331 | if ((LHSType->isAnyPointerType() && RHSType->isIntegerType()) || |
| 7332 | (LHSType->isIntegerType() && RHSType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7333 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7334 | bool isError = false; |
Douglas Gregor | 6db351a | 2012-09-14 04:35:37 +0000 | [diff] [blame] | 7335 | if (LangOpts.DebuggerSupport) { |
| 7336 | // Under a debugger, allow the comparison of pointers to integers, |
| 7337 | // since users tend to want to compare addresses. |
| 7338 | } else if ((LHSIsNull && LHSType->isIntegerType()) || |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7339 | (RHSIsNull && RHSType->isIntegerType())) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7340 | if (IsRelational && !getLangOpts().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7341 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7342 | } else if (IsRelational && !getLangOpts().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7343 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7344 | else if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7345 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7346 | isError = true; |
| 7347 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7348 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7349 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7350 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7351 | Diag(Loc, DiagID) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7352 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7353 | << RHS.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7354 | if (isError) |
| 7355 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7356 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7357 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7358 | if (LHSType->isIntegerType()) |
| 7359 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7360 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7361 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7362 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7363 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7364 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7365 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7366 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7367 | // Handle block pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7368 | if (!IsRelational && RHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7369 | && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { |
| 7370 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7371 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7372 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7373 | if (!IsRelational && LHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7374 | && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { |
| 7375 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7376 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7377 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7378 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7379 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7380 | } |
| 7381 | |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7382 | |
| 7383 | // Return a signed type that is of identical size and number of elements. |
| 7384 | // For floating point vectors, return an integer type of identical size |
| 7385 | // and number of elements. |
| 7386 | QualType Sema::GetSignedVectorType(QualType V) { |
| 7387 | const VectorType *VTy = V->getAs<VectorType>(); |
| 7388 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
| 7389 | if (TypeSize == Context.getTypeSize(Context.CharTy)) |
| 7390 | return Context.getExtVectorType(Context.CharTy, VTy->getNumElements()); |
| 7391 | else if (TypeSize == Context.getTypeSize(Context.ShortTy)) |
| 7392 | return Context.getExtVectorType(Context.ShortTy, VTy->getNumElements()); |
| 7393 | else if (TypeSize == Context.getTypeSize(Context.IntTy)) |
| 7394 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
| 7395 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) |
| 7396 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 7397 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
| 7398 | "Unhandled vector element size in vector compare"); |
| 7399 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 7400 | } |
| 7401 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7402 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7403 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7404 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7405 | /// types. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7406 | QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7407 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7408 | bool IsRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7409 | // Check to make sure we're operating on vectors of the same type and width, |
| 7410 | // Allowing one side to be a scalar of element type. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7411 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, /*isCompAssign*/false); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7412 | if (vType.isNull()) |
| 7413 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7414 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7415 | QualType LHSType = LHS.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7416 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7417 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7418 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 7419 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7420 | return Context.getLogicalOperationType(); |
| 7421 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7422 | // For non-floating point types, check for self-comparisons of the form |
| 7423 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7424 | // often indicate logic errors in the program. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7425 | if (!LHSType->hasFloatingRepresentation()) { |
Richard Smith | 9c129f8 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 7426 | if (DeclRefExpr* DRL |
| 7427 | = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParenImpCasts())) |
| 7428 | if (DeclRefExpr* DRR |
| 7429 | = dyn_cast<DeclRefExpr>(RHS.get()->IgnoreParenImpCasts())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7430 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7431 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7432 | PDiag(diag::warn_comparison_always) |
| 7433 | << 0 // self- |
| 7434 | << 2 // "a constant" |
| 7435 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7436 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7437 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7438 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7439 | if (!IsRelational && LHSType->hasFloatingRepresentation()) { |
David Blaikie | 52e4c60 | 2012-01-16 05:16:03 +0000 | [diff] [blame] | 7440 | assert (RHS.get()->getType()->hasFloatingRepresentation()); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7441 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7442 | } |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7443 | |
| 7444 | // Return a signed type for the vector. |
| 7445 | return GetSignedVectorType(LHSType); |
| 7446 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7447 | |
Tanya Lattner | b0f9dd2 | 2012-01-19 01:16:16 +0000 | [diff] [blame] | 7448 | QualType Sema::CheckVectorLogicalOperands(ExprResult &LHS, ExprResult &RHS, |
| 7449 | SourceLocation Loc) { |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7450 | // Ensure that either both operands are of the same vector type, or |
| 7451 | // one operand is of a vector type and the other is of its element type. |
| 7452 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, false); |
Joey Gouly | 52e933b | 2013-02-21 11:49:56 +0000 | [diff] [blame] | 7453 | if (vType.isNull()) |
| 7454 | return InvalidOperands(Loc, LHS, RHS); |
| 7455 | if (getLangOpts().OpenCL && getLangOpts().OpenCLVersion < 120 && |
| 7456 | vType->hasFloatingRepresentation()) |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7457 | return InvalidOperands(Loc, LHS, RHS); |
| 7458 | |
| 7459 | return GetSignedVectorType(LHS.get()->getType()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7460 | } |
| 7461 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7462 | inline QualType Sema::CheckBitwiseOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7463 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 7464 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 7465 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7466 | if (LHS.get()->getType()->isVectorType() || |
| 7467 | RHS.get()->getType()->isVectorType()) { |
| 7468 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 7469 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7470 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7471 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7472 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7473 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7474 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7475 | ExprResult LHSResult = Owned(LHS), RHSResult = Owned(RHS); |
| 7476 | QualType compType = UsualArithmeticConversions(LHSResult, RHSResult, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7477 | IsCompAssign); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7478 | if (LHSResult.isInvalid() || RHSResult.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7479 | return QualType(); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7480 | LHS = LHSResult.take(); |
| 7481 | RHS = RHSResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7482 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 7483 | if (!compType.isNull() && compType->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7484 | return compType; |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7485 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7486 | } |
| 7487 | |
| 7488 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7489 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7490 | |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7491 | // Check vector operands differently. |
| 7492 | if (LHS.get()->getType()->isVectorType() || RHS.get()->getType()->isVectorType()) |
| 7493 | return CheckVectorLogicalOperands(LHS, RHS, Loc); |
| 7494 | |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7495 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 7496 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 7497 | // is a constant. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7498 | if (LHS.get()->getType()->isIntegerType() && |
| 7499 | !LHS.get()->getType()->isBooleanType() && |
| 7500 | RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() && |
Richard Trieu | e5adf59 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 7501 | // Don't warn in macros or template instantiations. |
| 7502 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7503 | // If the RHS can be constant folded, and if it constant folds to something |
| 7504 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 7505 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 7506 | // Parens on the RHS are ignored. |
Richard Smith | 909c555 | 2011-10-16 23:01:09 +0000 | [diff] [blame] | 7507 | llvm::APSInt Result; |
| 7508 | if (RHS.get()->EvaluateAsInt(Result, Context)) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7509 | if ((getLangOpts().Bool && !RHS.get()->getType()->isBooleanType()) || |
Richard Smith | 909c555 | 2011-10-16 23:01:09 +0000 | [diff] [blame] | 7510 | (Result != 0 && Result != 1)) { |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 7511 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7512 | << RHS.get()->getSourceRange() |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7513 | << (Opc == BO_LAnd ? "&&" : "||"); |
| 7514 | // Suggest replacing the logical operator with the bitwise version |
| 7515 | Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) |
| 7516 | << (Opc == BO_LAnd ? "&" : "|") |
| 7517 | << FixItHint::CreateReplacement(SourceRange( |
| 7518 | Loc, Lexer::getLocForEndOfToken(Loc, 0, getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7519 | getLangOpts())), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7520 | Opc == BO_LAnd ? "&" : "|"); |
| 7521 | if (Opc == BO_LAnd) |
| 7522 | // Suggest replacing "Foo() && kNonZero" with "Foo()" |
| 7523 | Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) |
| 7524 | << FixItHint::CreateRemoval( |
| 7525 | SourceRange( |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7526 | Lexer::getLocForEndOfToken(LHS.get()->getLocEnd(), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7527 | 0, getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7528 | getLangOpts()), |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7529 | RHS.get()->getLocEnd())); |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7530 | } |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7531 | } |
Joey Gouly | 52e933b | 2013-02-21 11:49:56 +0000 | [diff] [blame] | 7532 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7533 | if (!Context.getLangOpts().CPlusPlus) { |
Joey Gouly | 52e933b | 2013-02-21 11:49:56 +0000 | [diff] [blame] | 7534 | // OpenCL v1.1 s6.3.g: The logical operators and (&&), or (||) do |
| 7535 | // not operate on the built-in scalar and vector float types. |
| 7536 | if (Context.getLangOpts().OpenCL && |
| 7537 | Context.getLangOpts().OpenCLVersion < 120) { |
| 7538 | if (LHS.get()->getType()->isFloatingType() || |
| 7539 | RHS.get()->getType()->isFloatingType()) |
| 7540 | return InvalidOperands(Loc, LHS, RHS); |
| 7541 | } |
| 7542 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7543 | LHS = UsualUnaryConversions(LHS.take()); |
| 7544 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7545 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7546 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7547 | RHS = UsualUnaryConversions(RHS.take()); |
| 7548 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7549 | return QualType(); |
| 7550 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7551 | if (!LHS.get()->getType()->isScalarType() || |
| 7552 | !RHS.get()->getType()->isScalarType()) |
| 7553 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7554 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7555 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 7556 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7557 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7558 | // The following is safe because we only use this method for |
| 7559 | // non-overloadable operands. |
| 7560 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7561 | // C++ [expr.log.and]p1 |
| 7562 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7563 | // The operands are both contextually converted to type bool. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7564 | ExprResult LHSRes = PerformContextuallyConvertToBool(LHS.get()); |
| 7565 | if (LHSRes.isInvalid()) |
| 7566 | return InvalidOperands(Loc, LHS, RHS); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7567 | LHS = LHSRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7568 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7569 | ExprResult RHSRes = PerformContextuallyConvertToBool(RHS.get()); |
| 7570 | if (RHSRes.isInvalid()) |
| 7571 | return InvalidOperands(Loc, LHS, RHS); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7572 | RHS = RHSRes; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7573 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7574 | // C++ [expr.log.and]p2 |
| 7575 | // C++ [expr.log.or]p2 |
| 7576 | // The result is a bool. |
| 7577 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7578 | } |
| 7579 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7580 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 7581 | /// is a read-only property; return true if so. A readonly property expression |
| 7582 | /// depends on various declarations and thus must be treated specially. |
| 7583 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7584 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7585 | const ObjCPropertyRefExpr *PropExpr = dyn_cast<ObjCPropertyRefExpr>(E); |
| 7586 | if (!PropExpr) return false; |
| 7587 | if (PropExpr->isImplicitProperty()) return false; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7588 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7589 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7590 | QualType BaseType = PropExpr->isSuperReceiver() ? |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7591 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 7592 | PropExpr->getBase()->getType(); |
| 7593 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7594 | if (const ObjCObjectPointerType *OPT = |
| 7595 | BaseType->getAsObjCInterfacePointerType()) |
| 7596 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 7597 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 7598 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7599 | return false; |
| 7600 | } |
| 7601 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7602 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7603 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); |
| 7604 | if (!ME) return false; |
| 7605 | if (!isa<FieldDecl>(ME->getMemberDecl())) return false; |
| 7606 | ObjCMessageExpr *Base = |
| 7607 | dyn_cast<ObjCMessageExpr>(ME->getBase()->IgnoreParenImpCasts()); |
| 7608 | if (!Base) return false; |
| 7609 | return Base->getMethodDecl() != 0; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7610 | } |
| 7611 | |
John McCall | 78dae24 | 2012-03-13 00:37:01 +0000 | [diff] [blame] | 7612 | /// Is the given expression (which must be 'const') a reference to a |
| 7613 | /// variable which was originally non-const, but which has become |
| 7614 | /// 'const' due to being captured within a block? |
| 7615 | enum NonConstCaptureKind { NCCK_None, NCCK_Block, NCCK_Lambda }; |
| 7616 | static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) { |
| 7617 | assert(E->isLValue() && E->getType().isConstQualified()); |
| 7618 | E = E->IgnoreParens(); |
| 7619 | |
| 7620 | // Must be a reference to a declaration from an enclosing scope. |
| 7621 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
| 7622 | if (!DRE) return NCCK_None; |
| 7623 | if (!DRE->refersToEnclosingLocal()) return NCCK_None; |
| 7624 | |
| 7625 | // The declaration must be a variable which is not declared 'const'. |
| 7626 | VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7627 | if (!var) return NCCK_None; |
| 7628 | if (var->getType().isConstQualified()) return NCCK_None; |
| 7629 | assert(var->hasLocalStorage() && "capture added 'const' to non-local?"); |
| 7630 | |
| 7631 | // Decide whether the first capture was for a block or a lambda. |
| 7632 | DeclContext *DC = S.CurContext; |
| 7633 | while (DC->getParent() != var->getDeclContext()) |
| 7634 | DC = DC->getParent(); |
| 7635 | return (isa<BlockDecl>(DC) ? NCCK_Block : NCCK_Lambda); |
| 7636 | } |
| 7637 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7638 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 7639 | /// emit an error and return true. If so, return false. |
| 7640 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Fariborz Jahanian | e7ea28a | 2012-04-10 17:30:10 +0000 | [diff] [blame] | 7641 | assert(!E->hasPlaceholderType(BuiltinType::PseudoObject)); |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7642 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7643 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7644 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7645 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 7646 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7647 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 7648 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7649 | if (IsLV == Expr::MLV_Valid) |
| 7650 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7651 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7652 | unsigned Diag = 0; |
| 7653 | bool NeedType = false; |
| 7654 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7655 | case Expr::MLV_ConstQualified: |
| 7656 | Diag = diag::err_typecheck_assign_const; |
| 7657 | |
John McCall | 78dae24 | 2012-03-13 00:37:01 +0000 | [diff] [blame] | 7658 | // Use a specialized diagnostic when we're assigning to an object |
| 7659 | // from an enclosing function or block. |
| 7660 | if (NonConstCaptureKind NCCK = isReferenceToNonConstCapture(S, E)) { |
| 7661 | if (NCCK == NCCK_Block) |
| 7662 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7663 | else |
| 7664 | Diag = diag::err_lambda_decl_ref_not_modifiable_lvalue; |
| 7665 | break; |
| 7666 | } |
| 7667 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7668 | // In ARC, use some specialized diagnostics for occasions where we |
| 7669 | // infer 'const'. These are always pseudo-strong variables. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7670 | if (S.getLangOpts().ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7671 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 7672 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 7673 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 7674 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7675 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 7676 | // user actually wrote 'const'. |
| 7677 | if (var->isARCPseudoStrong() && |
| 7678 | (!var->getTypeSourceInfo() || |
| 7679 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 7680 | // There are two pseudo-strong cases: |
| 7681 | // - self |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7682 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 7683 | if (method && var == method->getSelfDecl()) |
Ted Kremenek | 2bbcd5c | 2011-11-14 21:59:25 +0000 | [diff] [blame] | 7684 | Diag = method->isClassMethod() |
| 7685 | ? diag::err_typecheck_arc_assign_self_class_method |
| 7686 | : diag::err_typecheck_arc_assign_self; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7687 | |
| 7688 | // - fast enumeration variables |
| 7689 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7690 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7691 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7692 | SourceRange Assign; |
| 7693 | if (Loc != OrigLoc) |
| 7694 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 7695 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 7696 | // We need to preserve the AST regardless, so migration tool |
| 7697 | // can do its job. |
| 7698 | return false; |
| 7699 | } |
| 7700 | } |
| 7701 | } |
| 7702 | |
| 7703 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7704 | case Expr::MLV_ArrayType: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 7705 | case Expr::MLV_ArrayTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7706 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 7707 | NeedType = true; |
| 7708 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7709 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7710 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 7711 | NeedType = true; |
| 7712 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 7713 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7714 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 7715 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7716 | case Expr::MLV_Valid: |
| 7717 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7718 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7719 | case Expr::MLV_MemberFunction: |
| 7720 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7721 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7722 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7723 | case Expr::MLV_IncompleteType: |
| 7724 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7725 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 7726 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, E); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7727 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7728 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7729 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7730 | case Expr::MLV_ReadonlyProperty: |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7731 | case Expr::MLV_NoSetterProperty: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7732 | llvm_unreachable("readonly properties should be processed differently"); |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7733 | case Expr::MLV_InvalidMessageExpression: |
| 7734 | Diag = diag::error_readonly_message_assignment; |
| 7735 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7736 | case Expr::MLV_SubObjCPropertySetting: |
| 7737 | Diag = diag::error_no_subobject_property_setting; |
| 7738 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7739 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7740 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7741 | SourceRange Assign; |
| 7742 | if (Loc != OrigLoc) |
| 7743 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7744 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7745 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7746 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7747 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7748 | return true; |
| 7749 | } |
| 7750 | |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7751 | static void CheckIdentityFieldAssignment(Expr *LHSExpr, Expr *RHSExpr, |
| 7752 | SourceLocation Loc, |
| 7753 | Sema &Sema) { |
| 7754 | // C / C++ fields |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7755 | MemberExpr *ML = dyn_cast<MemberExpr>(LHSExpr); |
| 7756 | MemberExpr *MR = dyn_cast<MemberExpr>(RHSExpr); |
| 7757 | if (ML && MR && ML->getMemberDecl() == MR->getMemberDecl()) { |
| 7758 | if (isa<CXXThisExpr>(ML->getBase()) && isa<CXXThisExpr>(MR->getBase())) |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7759 | Sema.Diag(Loc, diag::warn_identity_field_assign) << 0; |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7760 | } |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7761 | |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7762 | // Objective-C instance variables |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7763 | ObjCIvarRefExpr *OL = dyn_cast<ObjCIvarRefExpr>(LHSExpr); |
| 7764 | ObjCIvarRefExpr *OR = dyn_cast<ObjCIvarRefExpr>(RHSExpr); |
| 7765 | if (OL && OR && OL->getDecl() == OR->getDecl()) { |
| 7766 | DeclRefExpr *RL = dyn_cast<DeclRefExpr>(OL->getBase()->IgnoreImpCasts()); |
| 7767 | DeclRefExpr *RR = dyn_cast<DeclRefExpr>(OR->getBase()->IgnoreImpCasts()); |
| 7768 | if (RL && RR && RL->getDecl() == RR->getDecl()) |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7769 | Sema.Diag(Loc, diag::warn_identity_field_assign) << 1; |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7770 | } |
| 7771 | } |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7772 | |
| 7773 | // C99 6.5.16.1 |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7774 | QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7775 | SourceLocation Loc, |
| 7776 | QualType CompoundType) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7777 | assert(!LHSExpr->hasPlaceholderType(BuiltinType::PseudoObject)); |
| 7778 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7779 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7780 | if (CheckForModifiableLvalue(LHSExpr, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7781 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7782 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7783 | QualType LHSType = LHSExpr->getType(); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7784 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
| 7785 | CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7786 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7787 | if (CompoundType.isNull()) { |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7788 | Expr *RHSCheck = RHS.get(); |
| 7789 | |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7790 | CheckIdentityFieldAssignment(LHSExpr, RHSCheck, Loc, *this); |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7791 | |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7792 | QualType LHSTy(LHSType); |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7793 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7794 | if (RHS.isInvalid()) |
| 7795 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7796 | // Special case of NSObject attributes on c-style pointer types. |
| 7797 | if (ConvTy == IncompatiblePointer && |
| 7798 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7799 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7800 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7801 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7802 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7803 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7804 | if (ConvTy == Compatible && |
Fariborz Jahanian | 466f45a | 2012-01-24 19:40:13 +0000 | [diff] [blame] | 7805 | LHSType->isObjCObjectType()) |
Fariborz Jahanian | 7b383e4 | 2012-01-24 18:05:45 +0000 | [diff] [blame] | 7806 | Diag(Loc, diag::err_objc_object_assignment) |
| 7807 | << LHSType; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7808 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7809 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7810 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7811 | // instead of "x += 4". |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7812 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7813 | RHSCheck = ICE->getSubExpr(); |
| 7814 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7815 | if ((UO->getOpcode() == UO_Plus || |
| 7816 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7817 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7818 | // Only if the two operators are exactly adjacent. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7819 | Loc.getLocWithOffset(1) == UO->getOperatorLoc() && |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7820 | // And there is a space or other character before the subexpr of the |
| 7821 | // unary +/-. We don't want to warn on "x=-1". |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7822 | Loc.getLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7823 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7824 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7825 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7826 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7827 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7828 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7829 | |
| 7830 | if (ConvTy == Compatible) { |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7831 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) { |
| 7832 | // Warn about retain cycles where a block captures the LHS, but |
| 7833 | // not if the LHS is a simple variable into which the block is |
| 7834 | // being stored...unless that variable can be captured by reference! |
| 7835 | const Expr *InnerLHS = LHSExpr->IgnoreParenCasts(); |
| 7836 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InnerLHS); |
| 7837 | if (!DRE || DRE->getDecl()->hasAttr<BlocksAttr>()) |
| 7838 | checkRetainCycles(LHSExpr, RHS.get()); |
| 7839 | |
Jordan Rose | 58b6bdc | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 7840 | // It is safe to assign a weak reference into a strong variable. |
| 7841 | // Although this code can still have problems: |
| 7842 | // id x = self.weakProp; |
| 7843 | // id y = self.weakProp; |
| 7844 | // we do not warn to warn spuriously when 'x' and 'y' are on separate |
| 7845 | // paths through the function. This should be revisited if |
| 7846 | // -Wrepeated-use-of-weak is made flow-sensitive. |
| 7847 | DiagnosticsEngine::Level Level = |
| 7848 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, |
| 7849 | RHS.get()->getLocStart()); |
| 7850 | if (Level != DiagnosticsEngine::Ignored) |
| 7851 | getCurFunction()->markSafeWeakUse(RHS.get()); |
| 7852 | |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7853 | } else if (getLangOpts().ObjCAutoRefCount) { |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7854 | checkUnsafeExprAssigns(Loc, LHSExpr, RHS.get()); |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7855 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7856 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7857 | } else { |
| 7858 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7859 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7860 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7861 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7862 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7863 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7864 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7865 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7866 | CheckForNullPointerDereference(*this, LHSExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7867 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7868 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7869 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7870 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7871 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7872 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7873 | // 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] | 7874 | // operand. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7875 | return (getLangOpts().CPlusPlus |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7876 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7877 | } |
| 7878 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7879 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7880 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7881 | SourceLocation Loc) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7882 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7883 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7884 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7885 | return QualType(); |
| 7886 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7887 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7888 | // operands, but not unary promotions. |
| 7889 | // 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] | 7890 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7891 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7892 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7893 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7894 | if (LHS.isInvalid()) |
| 7895 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7896 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 7897 | S.DiagnoseUnusedExprResult(LHS.get()); |
| 7898 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7899 | if (!S.getLangOpts().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7900 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7901 | if (RHS.isInvalid()) |
| 7902 | return QualType(); |
| 7903 | if (!RHS.get()->getType()->isVoidType()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7904 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
| 7905 | diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7906 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7907 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7908 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7909 | } |
| 7910 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7911 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7912 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7913 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7914 | ExprValueKind &VK, |
| 7915 | SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7916 | bool IsInc, bool IsPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7917 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7918 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7919 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7920 | QualType ResType = Op->getType(); |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7921 | // Atomic types can be used for increment / decrement where the non-atomic |
| 7922 | // versions can, so ignore the _Atomic() specifier for the purpose of |
| 7923 | // checking. |
| 7924 | if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) |
| 7925 | ResType = ResAtomicType->getValueType(); |
| 7926 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7927 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7928 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7929 | if (S.getLangOpts().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7930 | // Decrement of bool is not allowed. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7931 | if (!IsInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7932 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7933 | return QualType(); |
| 7934 | } |
| 7935 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7936 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7937 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7938 | // OK! |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 7939 | } else if (ResType->isPointerType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7940 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7941 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7942 | return QualType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 7943 | } else if (ResType->isObjCObjectPointerType()) { |
| 7944 | // On modern runtimes, ObjC pointer arithmetic is forbidden. |
| 7945 | // Otherwise, we just need a complete type. |
| 7946 | if (checkArithmeticIncompletePointerType(S, OpLoc, Op) || |
| 7947 | checkArithmeticOnObjCPointer(S, OpLoc, Op)) |
| 7948 | return QualType(); |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7949 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7950 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7951 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7952 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7953 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7954 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7955 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7956 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7957 | IsInc, IsPrefix); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7958 | } else if (S.getLangOpts().AltiVec && ResType->isVectorType()) { |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7959 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7960 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7961 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7962 | << ResType << int(IsInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7963 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7964 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7965 | // 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] | 7966 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7967 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7968 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7969 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7970 | // (in C or with postfix), the increment is the unqualified type of the |
| 7971 | // operand. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7972 | if (IsPrefix && S.getLangOpts().CPlusPlus) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7973 | VK = VK_LValue; |
| 7974 | return ResType; |
| 7975 | } else { |
| 7976 | VK = VK_RValue; |
| 7977 | return ResType.getUnqualifiedType(); |
| 7978 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7979 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7980 | |
| 7981 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7982 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7983 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7984 | /// where the declaration is needed for type checking. We only need to |
| 7985 | /// handle cases when the expression references a function designator |
| 7986 | /// or is an lvalue. Here are some examples: |
| 7987 | /// - &(x) => x |
| 7988 | /// - &*****f => f for f a function designator. |
| 7989 | /// - &s.xx => s |
| 7990 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7991 | /// - *(x + 1) -> x, if x is an array |
| 7992 | /// - &"123"[2] -> 0 |
| 7993 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7994 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7995 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7996 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7997 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7998 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7999 | // If this is an arrow operator, the address is an offset from |
| 8000 | // the base's value, so the object the base refers to is |
| 8001 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8002 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8003 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8004 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8005 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8006 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8007 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 8008 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8009 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 8010 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 8011 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 8012 | return getPrimaryDecl(ICE->getSubExpr()); |
| 8013 | } |
| 8014 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8015 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8016 | case Stmt::UnaryOperatorClass: { |
| 8017 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8018 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8019 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8020 | case UO_Real: |
| 8021 | case UO_Imag: |
| 8022 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8023 | return getPrimaryDecl(UO->getSubExpr()); |
| 8024 | default: |
| 8025 | return 0; |
| 8026 | } |
| 8027 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8028 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8029 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8030 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8031 | // If the result of an implicit cast is an l-value, we care about |
| 8032 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8033 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8034 | default: |
| 8035 | return 0; |
| 8036 | } |
| 8037 | } |
| 8038 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8039 | namespace { |
| 8040 | enum { |
| 8041 | AO_Bit_Field = 0, |
| 8042 | AO_Vector_Element = 1, |
| 8043 | AO_Property_Expansion = 2, |
| 8044 | AO_Register_Variable = 3, |
| 8045 | AO_No_Error = 4 |
| 8046 | }; |
| 8047 | } |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 8048 | /// \brief Diagnose invalid operand for address of operations. |
| 8049 | /// |
| 8050 | /// \param Type The type of operand which cannot have its address taken. |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 8051 | static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, |
| 8052 | Expr *E, unsigned Type) { |
| 8053 | S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); |
| 8054 | } |
| 8055 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8056 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8057 | /// 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] | 8058 | /// 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] | 8059 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8060 | /// 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] | 8061 | /// 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] | 8062 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8063 | static QualType CheckAddressOfOperand(Sema &S, ExprResult &OrigOp, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8064 | SourceLocation OpLoc) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8065 | if (const BuiltinType *PTy = OrigOp.get()->getType()->getAsPlaceholderType()){ |
| 8066 | if (PTy->getKind() == BuiltinType::Overload) { |
| 8067 | if (!isa<OverloadExpr>(OrigOp.get()->IgnoreParens())) { |
Richard Smith | 3fa3fea | 2013-02-02 02:14:45 +0000 | [diff] [blame] | 8068 | assert(cast<UnaryOperator>(OrigOp.get()->IgnoreParens())->getOpcode() |
| 8069 | == UO_AddrOf); |
| 8070 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof_addrof_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8071 | << OrigOp.get()->getSourceRange(); |
| 8072 | return QualType(); |
| 8073 | } |
| 8074 | |
| 8075 | return S.Context.OverloadTy; |
| 8076 | } |
| 8077 | |
| 8078 | if (PTy->getKind() == BuiltinType::UnknownAny) |
| 8079 | return S.Context.UnknownAnyTy; |
| 8080 | |
| 8081 | if (PTy->getKind() == BuiltinType::BoundMember) { |
| 8082 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 8083 | << OrigOp.get()->getSourceRange(); |
Douglas Gregor | 44efed0 | 2011-10-09 19:10:41 +0000 | [diff] [blame] | 8084 | return QualType(); |
| 8085 | } |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8086 | |
| 8087 | OrigOp = S.CheckPlaceholderExpr(OrigOp.take()); |
| 8088 | if (OrigOp.isInvalid()) return QualType(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8089 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8090 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8091 | if (OrigOp.get()->isTypeDependent()) |
| 8092 | return S.Context.DependentTy; |
| 8093 | |
| 8094 | assert(!OrigOp.get()->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8095 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8096 | // Make sure to ignore parentheses in subsequent checks |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8097 | Expr *op = OrigOp.get()->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 8098 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8099 | if (S.getLangOpts().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8100 | // Implement C99-only parts of addressof rules. |
| 8101 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8102 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8103 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 8104 | // (assuming the deref expression is valid). |
| 8105 | return uOp->getSubExpr()->getType(); |
| 8106 | } |
| 8107 | // Technically, there should be a check for array subscript |
| 8108 | // expressions here, but the result of one is always an lvalue anyway. |
| 8109 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8110 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8111 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8112 | unsigned AddressOfError = AO_No_Error; |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 8113 | |
Richard Smith | 3fa3fea | 2013-02-02 02:14:45 +0000 | [diff] [blame] | 8114 | if (lval == Expr::LV_ClassTemporary || lval == Expr::LV_ArrayTemporary) { |
David Blaikie | 0adb175 | 2013-02-21 06:05:05 +0000 | [diff] [blame] | 8115 | bool sfinae = (bool)S.isSFINAEContext(); |
| 8116 | S.Diag(OpLoc, S.isSFINAEContext() ? diag::err_typecheck_addrof_temporary |
Richard Smith | 3fa3fea | 2013-02-02 02:14:45 +0000 | [diff] [blame] | 8117 | : diag::ext_typecheck_addrof_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8118 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8119 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8120 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8121 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8122 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8123 | } else if (lval == Expr::LV_MemberFunction) { |
| 8124 | // If it's an instance method, make a member pointer. |
| 8125 | // The expression must have exactly the form &A::foo. |
| 8126 | |
| 8127 | // If the underlying expression isn't a decl ref, give up. |
| 8128 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8129 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8130 | << OrigOp.get()->getSourceRange(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8131 | return QualType(); |
| 8132 | } |
| 8133 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 8134 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 8135 | |
| 8136 | // The id-expression was parenthesized. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8137 | if (OrigOp.get() != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8138 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8139 | << OrigOp.get()->getSourceRange(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8140 | |
| 8141 | // The method was named without a qualifier. |
| 8142 | } else if (!DRE->getQualifier()) { |
David Blaikie | abeadfb | 2012-10-11 22:55:07 +0000 | [diff] [blame] | 8143 | if (MD->getParent()->getName().empty()) |
| 8144 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
| 8145 | << op->getSourceRange(); |
| 8146 | else { |
| 8147 | SmallString<32> Str; |
| 8148 | StringRef Qual = (MD->getParent()->getName() + "::").toStringRef(Str); |
| 8149 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
| 8150 | << op->getSourceRange() |
| 8151 | << FixItHint::CreateInsertion(op->getSourceRange().getBegin(), Qual); |
| 8152 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8153 | } |
| 8154 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8155 | return S.Context.getMemberPointerType(op->getType(), |
| 8156 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8157 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8158 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8159 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8160 | if (!op->getType()->isFunctionType()) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8161 | // Use a special diagnostic for loads from property references. |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 8162 | if (isa<PseudoObjectExpr>(op)) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8163 | AddressOfError = AO_Property_Expansion; |
| 8164 | } else { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8165 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Richard Smith | 3fa3fea | 2013-02-02 02:14:45 +0000 | [diff] [blame] | 8166 | << op->getType() << op->getSourceRange(); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8167 | return QualType(); |
| 8168 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8169 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8170 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8171 | // The operand cannot be a bit-field |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8172 | AddressOfError = AO_Bit_Field; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8173 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8174 | // The operand cannot be an element of a vector |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8175 | AddressOfError = AO_Vector_Element; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8176 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8177 | // 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] | 8178 | // with the register storage-class specifier. |
| 8179 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 8180 | // in C++ it is not error to take address of a register |
| 8181 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8182 | if (vd->getStorageClass() == SC_Register && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8183 | !S.getLangOpts().CPlusPlus) { |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8184 | AddressOfError = AO_Register_Variable; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8185 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8186 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8187 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8188 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 8189 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8190 | // Could be a pointer to member, though, if there is an explicit |
| 8191 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 8192 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8193 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8194 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8195 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8196 | S.Diag(OpLoc, |
| 8197 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8198 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8199 | return QualType(); |
| 8200 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8201 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 8202 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 8203 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8204 | return S.Context.getMemberPointerType(op->getType(), |
| 8205 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8206 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8207 | } |
Eli Friedman | 7b2f51c | 2011-08-26 20:28:17 +0000 | [diff] [blame] | 8208 | } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8209 | llvm_unreachable("Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8210 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8211 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8212 | if (AddressOfError != AO_No_Error) { |
| 8213 | diagnoseAddressOfInvalidType(S, OpLoc, op, AddressOfError); |
| 8214 | return QualType(); |
| 8215 | } |
| 8216 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8217 | if (lval == Expr::LV_IncompleteVoidType) { |
| 8218 | // Taking the address of a void variable is technically illegal, but we |
| 8219 | // allow it in cases which are otherwise valid. |
| 8220 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8221 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8222 | } |
| 8223 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8224 | // 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] | 8225 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8226 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 8227 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8228 | } |
| 8229 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8230 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8231 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 8232 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8233 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8234 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8235 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8236 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 8237 | if (ConvResult.isInvalid()) |
| 8238 | return QualType(); |
| 8239 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8240 | QualType OpTy = Op->getType(); |
| 8241 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 8242 | |
| 8243 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 8244 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 8245 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 8246 | Op->getSourceRange()); |
| 8247 | } |
| 8248 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8249 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 8250 | // is an incomplete type or void. It would be possible to warn about |
| 8251 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 8252 | // warning is unlikely to catch any mistakes. |
| 8253 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 8254 | Result = PT->getPointeeType(); |
| 8255 | else if (const ObjCObjectPointerType *OPT = |
| 8256 | OpTy->getAs<ObjCObjectPointerType>()) |
| 8257 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8258 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8259 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8260 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8261 | if (PR.take() != Op) |
| 8262 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8263 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8264 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8265 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8266 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8267 | << OpTy << Op->getSourceRange(); |
| 8268 | return QualType(); |
| 8269 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8270 | |
| 8271 | // Dereferences are usually l-values... |
| 8272 | VK = VK_LValue; |
| 8273 | |
| 8274 | // ...except that certain expressions are never l-values in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8275 | if (!S.getLangOpts().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8276 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8277 | |
| 8278 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8279 | } |
| 8280 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8281 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8282 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8283 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8284 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8285 | default: llvm_unreachable("Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8286 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 8287 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 8288 | case tok::star: Opc = BO_Mul; break; |
| 8289 | case tok::slash: Opc = BO_Div; break; |
| 8290 | case tok::percent: Opc = BO_Rem; break; |
| 8291 | case tok::plus: Opc = BO_Add; break; |
| 8292 | case tok::minus: Opc = BO_Sub; break; |
| 8293 | case tok::lessless: Opc = BO_Shl; break; |
| 8294 | case tok::greatergreater: Opc = BO_Shr; break; |
| 8295 | case tok::lessequal: Opc = BO_LE; break; |
| 8296 | case tok::less: Opc = BO_LT; break; |
| 8297 | case tok::greaterequal: Opc = BO_GE; break; |
| 8298 | case tok::greater: Opc = BO_GT; break; |
| 8299 | case tok::exclaimequal: Opc = BO_NE; break; |
| 8300 | case tok::equalequal: Opc = BO_EQ; break; |
| 8301 | case tok::amp: Opc = BO_And; break; |
| 8302 | case tok::caret: Opc = BO_Xor; break; |
| 8303 | case tok::pipe: Opc = BO_Or; break; |
| 8304 | case tok::ampamp: Opc = BO_LAnd; break; |
| 8305 | case tok::pipepipe: Opc = BO_LOr; break; |
| 8306 | case tok::equal: Opc = BO_Assign; break; |
| 8307 | case tok::starequal: Opc = BO_MulAssign; break; |
| 8308 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 8309 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 8310 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 8311 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 8312 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 8313 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 8314 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 8315 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 8316 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 8317 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8318 | } |
| 8319 | return Opc; |
| 8320 | } |
| 8321 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8322 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8323 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8324 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8325 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8326 | default: llvm_unreachable("Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8327 | case tok::plusplus: Opc = UO_PreInc; break; |
| 8328 | case tok::minusminus: Opc = UO_PreDec; break; |
| 8329 | case tok::amp: Opc = UO_AddrOf; break; |
| 8330 | case tok::star: Opc = UO_Deref; break; |
| 8331 | case tok::plus: Opc = UO_Plus; break; |
| 8332 | case tok::minus: Opc = UO_Minus; break; |
| 8333 | case tok::tilde: Opc = UO_Not; break; |
| 8334 | case tok::exclaim: Opc = UO_LNot; break; |
| 8335 | case tok::kw___real: Opc = UO_Real; break; |
| 8336 | case tok::kw___imag: Opc = UO_Imag; break; |
| 8337 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8338 | } |
| 8339 | return Opc; |
| 8340 | } |
| 8341 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8342 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 8343 | /// This warning is only emitted for builtin assignment operations. It is also |
| 8344 | /// suppressed in the event of macro expansions. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8345 | static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8346 | SourceLocation OpLoc) { |
| 8347 | if (!S.ActiveTemplateInstantiations.empty()) |
| 8348 | return; |
| 8349 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 8350 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8351 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
| 8352 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); |
| 8353 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); |
| 8354 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); |
| 8355 | if (!LHSDeclRef || !RHSDeclRef || |
| 8356 | LHSDeclRef->getLocation().isMacroID() || |
| 8357 | RHSDeclRef->getLocation().isMacroID()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8358 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8359 | const ValueDecl *LHSDecl = |
| 8360 | cast<ValueDecl>(LHSDeclRef->getDecl()->getCanonicalDecl()); |
| 8361 | const ValueDecl *RHSDecl = |
| 8362 | cast<ValueDecl>(RHSDeclRef->getDecl()->getCanonicalDecl()); |
| 8363 | if (LHSDecl != RHSDecl) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8364 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8365 | if (LHSDecl->getType().isVolatileQualified()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8366 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8367 | if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8368 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 8369 | return; |
| 8370 | |
| 8371 | S.Diag(OpLoc, diag::warn_self_assignment) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8372 | << LHSDeclRef->getType() |
| 8373 | << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8374 | } |
| 8375 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8376 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 8377 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 8378 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8379 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8380 | BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8381 | Expr *LHSExpr, Expr *RHSExpr) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8382 | if (getLangOpts().CPlusPlus11 && isa<InitListExpr>(RHSExpr)) { |
Sebastian Redl | 0d8ab2e | 2012-02-27 20:34:02 +0000 | [diff] [blame] | 8383 | // The syntax only allows initializer lists on the RHS of assignment, |
| 8384 | // so we don't need to worry about accepting invalid code for |
| 8385 | // non-assignment operators. |
| 8386 | // C++11 5.17p9: |
| 8387 | // The meaning of x = {v} [...] is that of x = T(v) [...]. The meaning |
| 8388 | // of x = {} is x = T(). |
| 8389 | InitializationKind Kind = |
| 8390 | InitializationKind::CreateDirectList(RHSExpr->getLocStart()); |
| 8391 | InitializedEntity Entity = |
| 8392 | InitializedEntity::InitializeTemporary(LHSExpr->getType()); |
| 8393 | InitializationSequence InitSeq(*this, Entity, Kind, &RHSExpr, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8394 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, RHSExpr); |
Sebastian Redl | 0d8ab2e | 2012-02-27 20:34:02 +0000 | [diff] [blame] | 8395 | if (Init.isInvalid()) |
| 8396 | return Init; |
| 8397 | RHSExpr = Init.take(); |
| 8398 | } |
| 8399 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8400 | ExprResult LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8401 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8402 | // The following two variables are used for compound assignment operators |
| 8403 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 8404 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8405 | ExprValueKind VK = VK_RValue; |
| 8406 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8407 | |
| 8408 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8409 | case BO_Assign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8410 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8411 | if (getLangOpts().CPlusPlus && |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8412 | LHS.get()->getObjectKind() != OK_ObjCProperty) { |
| 8413 | VK = LHS.get()->getValueKind(); |
| 8414 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8415 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8416 | if (!ResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8417 | DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8418 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8419 | case BO_PtrMemD: |
| 8420 | case BO_PtrMemI: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8421 | ResultTy = CheckPointerToMemberOperands(LHS, RHS, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8422 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 8423 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8424 | case BO_Mul: |
| 8425 | case BO_Div: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8426 | ResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8427 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8428 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8429 | case BO_Rem: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8430 | ResultTy = CheckRemainderOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8431 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8432 | case BO_Add: |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 8433 | ResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8434 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8435 | case BO_Sub: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8436 | ResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8437 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8438 | case BO_Shl: |
| 8439 | case BO_Shr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8440 | ResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8441 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8442 | case BO_LE: |
| 8443 | case BO_LT: |
| 8444 | case BO_GE: |
| 8445 | case BO_GT: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8446 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8447 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8448 | case BO_EQ: |
| 8449 | case BO_NE: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8450 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8451 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8452 | case BO_And: |
| 8453 | case BO_Xor: |
| 8454 | case BO_Or: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8455 | ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8456 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8457 | case BO_LAnd: |
| 8458 | case BO_LOr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8459 | ResultTy = CheckLogicalOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8460 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8461 | case BO_MulAssign: |
| 8462 | case BO_DivAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8463 | CompResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8464 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8465 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8466 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8467 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8468 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8469 | case BO_RemAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8470 | CompResultTy = CheckRemainderOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8471 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8472 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8473 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8474 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8475 | case BO_AddAssign: |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 8476 | CompResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc, &CompLHSTy); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8477 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8478 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8479 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8480 | case BO_SubAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8481 | CompResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 8482 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8483 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8484 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8485 | case BO_ShlAssign: |
| 8486 | case BO_ShrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8487 | CompResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8488 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8489 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8490 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8491 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8492 | case BO_AndAssign: |
| 8493 | case BO_XorAssign: |
| 8494 | case BO_OrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8495 | CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8496 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8497 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8498 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8499 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8500 | case BO_Comma: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8501 | ResultTy = CheckCommaOperands(*this, LHS, RHS, OpLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8502 | if (getLangOpts().CPlusPlus && !RHS.isInvalid()) { |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8503 | VK = RHS.get()->getValueKind(); |
| 8504 | OK = RHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8505 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8506 | break; |
| 8507 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8508 | if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8509 | return ExprError(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8510 | |
| 8511 | // Check for array bounds violations for both sides of the BinaryOperator |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8512 | CheckArrayAccess(LHS.get()); |
| 8513 | CheckArrayAccess(RHS.get()); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8514 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8515 | if (CompResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8516 | return Owned(new (Context) BinaryOperator(LHS.take(), RHS.take(), Opc, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 8517 | ResultTy, VK, OK, OpLoc, |
| 8518 | FPFeatures.fp_contract)); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8519 | if (getLangOpts().CPlusPlus && LHS.get()->getObjectKind() != |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 8520 | OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8521 | VK = VK_LValue; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8522 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8523 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8524 | return Owned(new (Context) CompoundAssignOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8525 | ResultTy, VK, OK, CompLHSTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 8526 | CompResultTy, OpLoc, |
| 8527 | FPFeatures.fp_contract)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8528 | } |
| 8529 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8530 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 8531 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 8532 | /// comparison operators have higher precedence. The most typical example of |
| 8533 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8534 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8535 | SourceLocation OpLoc, Expr *LHSExpr, |
| 8536 | Expr *RHSExpr) { |
Eli Friedman | ebbcd1d | 2012-11-15 00:29:07 +0000 | [diff] [blame] | 8537 | BinaryOperator *LHSBO = dyn_cast<BinaryOperator>(LHSExpr); |
| 8538 | BinaryOperator *RHSBO = dyn_cast<BinaryOperator>(RHSExpr); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8539 | |
Eli Friedman | ebbcd1d | 2012-11-15 00:29:07 +0000 | [diff] [blame] | 8540 | // Check that one of the sides is a comparison operator. |
| 8541 | bool isLeftComp = LHSBO && LHSBO->isComparisonOp(); |
| 8542 | bool isRightComp = RHSBO && RHSBO->isComparisonOp(); |
| 8543 | if (!isLeftComp && !isRightComp) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8544 | return; |
| 8545 | |
| 8546 | // Bitwise operations are sometimes used as eager logical ops. |
| 8547 | // Don't diagnose this. |
Eli Friedman | ebbcd1d | 2012-11-15 00:29:07 +0000 | [diff] [blame] | 8548 | bool isLeftBitwise = LHSBO && LHSBO->isBitwiseOp(); |
| 8549 | bool isRightBitwise = RHSBO && RHSBO->isBitwiseOp(); |
| 8550 | if ((isLeftComp || isLeftBitwise) && (isRightComp || isRightBitwise)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8551 | return; |
| 8552 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8553 | SourceRange DiagRange = isLeftComp ? SourceRange(LHSExpr->getLocStart(), |
| 8554 | OpLoc) |
| 8555 | : SourceRange(OpLoc, RHSExpr->getLocEnd()); |
Eli Friedman | ebbcd1d | 2012-11-15 00:29:07 +0000 | [diff] [blame] | 8556 | StringRef OpStr = isLeftComp ? LHSBO->getOpcodeStr() : RHSBO->getOpcodeStr(); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8557 | SourceRange ParensRange = isLeftComp ? |
Eli Friedman | ebbcd1d | 2012-11-15 00:29:07 +0000 | [diff] [blame] | 8558 | SourceRange(LHSBO->getRHS()->getLocStart(), RHSExpr->getLocEnd()) |
| 8559 | : SourceRange(LHSExpr->getLocStart(), RHSBO->getLHS()->getLocStart()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8560 | |
| 8561 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
Eli Friedman | ebbcd1d | 2012-11-15 00:29:07 +0000 | [diff] [blame] | 8562 | << DiagRange << BinaryOperator::getOpcodeStr(Opc) << OpStr; |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8563 | SuggestParentheses(Self, OpLoc, |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8564 | Self.PDiag(diag::note_precedence_silence) << OpStr, |
Nico Weber | 40e2999 | 2012-06-03 07:07:00 +0000 | [diff] [blame] | 8565 | (isLeftComp ? LHSExpr : RHSExpr)->getSourceRange()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8566 | SuggestParentheses(Self, OpLoc, |
Eli Friedman | ebbcd1d | 2012-11-15 00:29:07 +0000 | [diff] [blame] | 8567 | Self.PDiag(diag::note_precedence_bitwise_first) |
| 8568 | << BinaryOperator::getOpcodeStr(Opc), |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8569 | ParensRange); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8570 | } |
| 8571 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8572 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 8573 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 8574 | /// in parentheses. |
| 8575 | static void |
| 8576 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 8577 | BinaryOperator *Bop) { |
| 8578 | assert(Bop->getOpcode() == BO_And); |
| 8579 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 8580 | << Bop->getSourceRange() << OpLoc; |
| 8581 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8582 | Self.PDiag(diag::note_precedence_silence) |
| 8583 | << Bop->getOpcodeStr(), |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8584 | Bop->getSourceRange()); |
| 8585 | } |
| 8586 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8587 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 8588 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 8589 | /// in parentheses. |
| 8590 | static void |
| 8591 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8592 | BinaryOperator *Bop) { |
| 8593 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 8594 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 8595 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8596 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8597 | Self.PDiag(diag::note_precedence_silence) |
| 8598 | << Bop->getOpcodeStr(), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 8599 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8600 | } |
| 8601 | |
| 8602 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8603 | /// 'true'. |
| 8604 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 8605 | bool Res; |
| 8606 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 8607 | } |
| 8608 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8609 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8610 | /// 'false'. |
| 8611 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 8612 | bool Res; |
| 8613 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 8614 | } |
| 8615 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8616 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 8617 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8618 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8619 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(LHSExpr)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8620 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8621 | // 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] | 8622 | if (EvaluatesAsFalse(S, RHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8623 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8624 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 8625 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 8626 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 8627 | } else if (Bop->getOpcode() == BO_LOr) { |
| 8628 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 8629 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 8630 | // "a || b && 1", but warn now. |
| 8631 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8632 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8633 | } |
| 8634 | } |
| 8635 | } |
| 8636 | } |
| 8637 | |
| 8638 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8639 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8640 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8641 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(RHSExpr)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8642 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8643 | // 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] | 8644 | if (EvaluatesAsFalse(S, LHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8645 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8646 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8647 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8648 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8649 | } |
| 8650 | } |
| 8651 | } |
| 8652 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8653 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 8654 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 8655 | Expr *OrArg) { |
| 8656 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 8657 | if (Bop->getOpcode() == BO_And) |
| 8658 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 8659 | } |
| 8660 | } |
| 8661 | |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8662 | static void DiagnoseAdditionInShift(Sema &S, SourceLocation OpLoc, |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 8663 | Expr *SubExpr, StringRef Shift) { |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8664 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(SubExpr)) { |
| 8665 | if (Bop->getOpcode() == BO_Add || Bop->getOpcode() == BO_Sub) { |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8666 | StringRef Op = Bop->getOpcodeStr(); |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8667 | S.Diag(Bop->getOperatorLoc(), diag::warn_addition_in_bitshift) |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 8668 | << Bop->getSourceRange() << OpLoc << Shift << Op; |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8669 | SuggestParentheses(S, Bop->getOperatorLoc(), |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8670 | S.PDiag(diag::note_precedence_silence) << Op, |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8671 | Bop->getSourceRange()); |
| 8672 | } |
| 8673 | } |
| 8674 | } |
| 8675 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8676 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8677 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8678 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8679 | SourceLocation OpLoc, Expr *LHSExpr, |
| 8680 | Expr *RHSExpr){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8681 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8682 | if (BinaryOperator::isBitwiseOp(Opc)) |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8683 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8684 | |
| 8685 | // Diagnose "arg1 & arg2 | arg3" |
| 8686 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8687 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, LHSExpr); |
| 8688 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8689 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8690 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8691 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8692 | // 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] | 8693 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8694 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); |
| 8695 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8696 | } |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8697 | |
| 8698 | if ((Opc == BO_Shl && LHSExpr->getType()->isIntegralType(Self.getASTContext())) |
| 8699 | || Opc == BO_Shr) { |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 8700 | StringRef Shift = BinaryOperator::getOpcodeStr(Opc); |
| 8701 | DiagnoseAdditionInShift(Self, OpLoc, LHSExpr, Shift); |
| 8702 | DiagnoseAdditionInShift(Self, OpLoc, RHSExpr, Shift); |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8703 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8704 | } |
| 8705 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8706 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8707 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8708 | tok::TokenKind Kind, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8709 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8710 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8711 | assert((LHSExpr != 0) && "ActOnBinOp(): missing left expression"); |
| 8712 | assert((RHSExpr != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8713 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8714 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8715 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8716 | |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8717 | return BuildBinOp(S, TokLoc, Opc, LHSExpr, RHSExpr); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8718 | } |
| 8719 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8720 | /// Build an overloaded binary operator expression in the given scope. |
| 8721 | static ExprResult BuildOverloadedBinOp(Sema &S, Scope *Sc, SourceLocation OpLoc, |
| 8722 | BinaryOperatorKind Opc, |
| 8723 | Expr *LHS, Expr *RHS) { |
| 8724 | // Find all of the overloaded operators visible from this |
| 8725 | // point. We perform both an operator-name lookup from the local |
| 8726 | // scope and an argument-dependent lookup based on the types of |
| 8727 | // the arguments. |
| 8728 | UnresolvedSet<16> Functions; |
| 8729 | OverloadedOperatorKind OverOp |
| 8730 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8731 | if (Sc && OverOp != OO_None) |
| 8732 | S.LookupOverloadedOperatorName(OverOp, Sc, LHS->getType(), |
| 8733 | RHS->getType(), Functions); |
| 8734 | |
| 8735 | // Build the (potentially-overloaded, potentially-dependent) |
| 8736 | // binary operation. |
| 8737 | return S.CreateOverloadedBinOp(OpLoc, Opc, Functions, LHS, RHS); |
| 8738 | } |
| 8739 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8740 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8741 | BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8742 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8743 | // We want to end up calling one of checkPseudoObjectAssignment |
| 8744 | // (if the LHS is a pseudo-object), BuildOverloadedBinOp (if |
| 8745 | // both expressions are overloadable or either is type-dependent), |
| 8746 | // or CreateBuiltinBinOp (in any other case). We also want to get |
| 8747 | // any placeholder types out of the way. |
| 8748 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8749 | // Handle pseudo-objects in the LHS. |
| 8750 | if (const BuiltinType *pty = LHSExpr->getType()->getAsPlaceholderType()) { |
| 8751 | // Assignments with a pseudo-object l-value need special analysis. |
| 8752 | if (pty->getKind() == BuiltinType::PseudoObject && |
| 8753 | BinaryOperator::isAssignmentOp(Opc)) |
| 8754 | return checkPseudoObjectAssignment(S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8755 | |
| 8756 | // Don't resolve overloads if the other type is overloadable. |
| 8757 | if (pty->getKind() == BuiltinType::Overload) { |
| 8758 | // We can't actually test that if we still have a placeholder, |
| 8759 | // though. Fortunately, none of the exceptions we see in that |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8760 | // code below are valid when the LHS is an overload set. Note |
| 8761 | // that an overload set can be dependently-typed, but it never |
| 8762 | // instantiates to having an overloadable type. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8763 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); |
| 8764 | if (resolvedRHS.isInvalid()) return ExprError(); |
| 8765 | RHSExpr = resolvedRHS.take(); |
| 8766 | |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8767 | if (RHSExpr->isTypeDependent() || |
| 8768 | RHSExpr->getType()->isOverloadableType()) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8769 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8770 | } |
| 8771 | |
| 8772 | ExprResult LHS = CheckPlaceholderExpr(LHSExpr); |
| 8773 | if (LHS.isInvalid()) return ExprError(); |
| 8774 | LHSExpr = LHS.take(); |
| 8775 | } |
| 8776 | |
| 8777 | // Handle pseudo-objects in the RHS. |
| 8778 | if (const BuiltinType *pty = RHSExpr->getType()->getAsPlaceholderType()) { |
| 8779 | // An overload in the RHS can potentially be resolved by the type |
| 8780 | // being assigned to. |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8781 | if (Opc == BO_Assign && pty->getKind() == BuiltinType::Overload) { |
| 8782 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) |
| 8783 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8784 | |
Eli Friedman | 8788491 | 2012-01-17 21:27:43 +0000 | [diff] [blame] | 8785 | if (LHSExpr->getType()->isOverloadableType()) |
| 8786 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8787 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8788 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8789 | } |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8790 | |
| 8791 | // Don't resolve overloads if the other type is overloadable. |
| 8792 | if (pty->getKind() == BuiltinType::Overload && |
| 8793 | LHSExpr->getType()->isOverloadableType()) |
| 8794 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8795 | |
| 8796 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); |
| 8797 | if (!resolvedRHS.isUsable()) return ExprError(); |
| 8798 | RHSExpr = resolvedRHS.take(); |
| 8799 | } |
| 8800 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8801 | if (getLangOpts().CPlusPlus) { |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8802 | // If either expression is type-dependent, always build an |
| 8803 | // overloaded op. |
| 8804 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) |
| 8805 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8806 | |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8807 | // Otherwise, build an overloaded op if either expression has an |
| 8808 | // overloadable type. |
| 8809 | if (LHSExpr->getType()->isOverloadableType() || |
| 8810 | RHSExpr->getType()->isOverloadableType()) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8811 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8812 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8813 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8814 | // Build a built-in binary operation. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8815 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8816 | } |
| 8817 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8818 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8819 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8820 | Expr *InputExpr) { |
| 8821 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8822 | ExprValueKind VK = VK_RValue; |
| 8823 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8824 | QualType resultType; |
| 8825 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8826 | case UO_PreInc: |
| 8827 | case UO_PreDec: |
| 8828 | case UO_PostInc: |
| 8829 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8830 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8831 | Opc == UO_PreInc || |
| 8832 | Opc == UO_PostInc, |
| 8833 | Opc == UO_PreInc || |
| 8834 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8835 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8836 | case UO_AddrOf: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8837 | resultType = CheckAddressOfOperand(*this, Input, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8838 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8839 | case UO_Deref: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8840 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 8841 | if (Input.isInvalid()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8842 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8843 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8844 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8845 | case UO_Plus: |
| 8846 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8847 | Input = UsualUnaryConversions(Input.take()); |
| 8848 | if (Input.isInvalid()) return ExprError(); |
| 8849 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8850 | if (resultType->isDependentType()) |
| 8851 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8852 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8853 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8854 | break; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8855 | else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6-7 |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8856 | resultType->isEnumeralType()) |
| 8857 | break; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8858 | else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8859 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8860 | resultType->isPointerType()) |
| 8861 | break; |
| 8862 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8863 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8864 | << resultType << Input.get()->getSourceRange()); |
| 8865 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8866 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8867 | Input = UsualUnaryConversions(Input.take()); |
Joey Gouly | 52e933b | 2013-02-21 11:49:56 +0000 | [diff] [blame] | 8868 | if (Input.isInvalid()) |
| 8869 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8870 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8871 | if (resultType->isDependentType()) |
| 8872 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8873 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8874 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8875 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8876 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Joey Gouly | 52e933b | 2013-02-21 11:49:56 +0000 | [diff] [blame] | 8877 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8878 | else if (resultType->hasIntegerRepresentation()) |
| 8879 | break; |
Joey Gouly | 52e933b | 2013-02-21 11:49:56 +0000 | [diff] [blame] | 8880 | else if (resultType->isExtVectorType()) { |
| 8881 | if (Context.getLangOpts().OpenCL) { |
| 8882 | // OpenCL v1.1 s6.3.f: The bitwise operator not (~) does not operate |
| 8883 | // on vector float types. |
| 8884 | QualType T = resultType->getAs<ExtVectorType>()->getElementType(); |
| 8885 | if (!T->isIntegerType()) |
| 8886 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 8887 | << resultType << Input.get()->getSourceRange()); |
| 8888 | } |
| 8889 | break; |
| 8890 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8891 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
Joey Gouly | 52e933b | 2013-02-21 11:49:56 +0000 | [diff] [blame] | 8892 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8893 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8894 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8895 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8896 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8897 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8898 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8899 | if (Input.isInvalid()) return ExprError(); |
| 8900 | resultType = Input.get()->getType(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 8901 | |
| 8902 | // Though we still have to promote half FP to float... |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 8903 | if (resultType->isHalfType() && !Context.getLangOpts().NativeHalfType) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 8904 | Input = ImpCastExprToType(Input.take(), Context.FloatTy, CK_FloatingCast).take(); |
| 8905 | resultType = Context.FloatTy; |
| 8906 | } |
| 8907 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8908 | if (resultType->isDependentType()) |
| 8909 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8910 | if (resultType->isScalarType()) { |
| 8911 | // C99 6.5.3.3p1: ok, fallthrough; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8912 | if (Context.getLangOpts().CPlusPlus) { |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8913 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8914 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8915 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8916 | ScalarTypeToBooleanCastKind(resultType)); |
Joey Gouly | 52e933b | 2013-02-21 11:49:56 +0000 | [diff] [blame] | 8917 | } else if (Context.getLangOpts().OpenCL && |
| 8918 | Context.getLangOpts().OpenCLVersion < 120) { |
| 8919 | // OpenCL v1.1 6.3.h: The logical operator not (!) does not |
| 8920 | // operate on scalar float types. |
| 8921 | if (!resultType->isIntegerType()) |
| 8922 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 8923 | << resultType << Input.get()->getSourceRange()); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8924 | } |
Tanya Lattner | b0f9dd2 | 2012-01-19 01:16:16 +0000 | [diff] [blame] | 8925 | } else if (resultType->isExtVectorType()) { |
Joey Gouly | 52e933b | 2013-02-21 11:49:56 +0000 | [diff] [blame] | 8926 | if (Context.getLangOpts().OpenCL && |
| 8927 | Context.getLangOpts().OpenCLVersion < 120) { |
| 8928 | // OpenCL v1.1 6.3.h: The logical operator not (!) does not |
| 8929 | // operate on vector float types. |
| 8930 | QualType T = resultType->getAs<ExtVectorType>()->getElementType(); |
| 8931 | if (!T->isIntegerType()) |
| 8932 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 8933 | << resultType << Input.get()->getSourceRange()); |
| 8934 | } |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 8935 | // Vector logical not returns the signed variant of the operand type. |
| 8936 | resultType = GetSignedVectorType(resultType); |
| 8937 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8938 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8939 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8940 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8941 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8942 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8943 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8944 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8945 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8946 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8947 | case UO_Real: |
| 8948 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8949 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8950 | // _Real maps ordinary l-values into ordinary l-values. _Imag maps ordinary |
| 8951 | // 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] | 8952 | if (Input.isInvalid()) return ExprError(); |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8953 | if (Opc == UO_Real || Input.get()->getType()->isAnyComplexType()) { |
| 8954 | if (Input.get()->getValueKind() != VK_RValue && |
| 8955 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8956 | VK = Input.get()->getValueKind(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8957 | } else if (!getLangOpts().CPlusPlus) { |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8958 | // In C, a volatile scalar is read by __imag. In C++, it is not. |
| 8959 | Input = DefaultLvalueConversion(Input.take()); |
| 8960 | } |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8961 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8962 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8963 | resultType = Input.get()->getType(); |
| 8964 | VK = Input.get()->getValueKind(); |
| 8965 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8966 | break; |
| 8967 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8968 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8969 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8970 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8971 | // Check for array bounds violations in the operand of the UnaryOperator, |
| 8972 | // except for the '*' and '&' operators that have to be handled specially |
| 8973 | // by CheckArrayAccess (as there are special cases like &array[arraysize] |
| 8974 | // that are explicitly defined as valid by the standard). |
| 8975 | if (Opc != UO_AddrOf && Opc != UO_Deref) |
| 8976 | CheckArrayAccess(Input.get()); |
| 8977 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8978 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8979 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8980 | } |
| 8981 | |
Douglas Gregor | d3d0853 | 2011-12-14 21:23:13 +0000 | [diff] [blame] | 8982 | /// \brief Determine whether the given expression is a qualified member |
| 8983 | /// access expression, of a form that could be turned into a pointer to member |
| 8984 | /// with the address-of operator. |
| 8985 | static bool isQualifiedMemberAccess(Expr *E) { |
| 8986 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 8987 | if (!DRE->getQualifier()) |
| 8988 | return false; |
| 8989 | |
| 8990 | ValueDecl *VD = DRE->getDecl(); |
| 8991 | if (!VD->isCXXClassMember()) |
| 8992 | return false; |
| 8993 | |
| 8994 | if (isa<FieldDecl>(VD) || isa<IndirectFieldDecl>(VD)) |
| 8995 | return true; |
| 8996 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(VD)) |
| 8997 | return Method->isInstance(); |
| 8998 | |
| 8999 | return false; |
| 9000 | } |
| 9001 | |
| 9002 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 9003 | if (!ULE->getQualifier()) |
| 9004 | return false; |
| 9005 | |
| 9006 | for (UnresolvedLookupExpr::decls_iterator D = ULE->decls_begin(), |
| 9007 | DEnd = ULE->decls_end(); |
| 9008 | D != DEnd; ++D) { |
| 9009 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*D)) { |
| 9010 | if (Method->isInstance()) |
| 9011 | return true; |
| 9012 | } else { |
| 9013 | // Overload set does not contain methods. |
| 9014 | break; |
| 9015 | } |
| 9016 | } |
| 9017 | |
| 9018 | return false; |
| 9019 | } |
| 9020 | |
| 9021 | return false; |
| 9022 | } |
| 9023 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9024 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9025 | UnaryOperatorKind Opc, Expr *Input) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 9026 | // First things first: handle placeholders so that the |
| 9027 | // overloaded-operator check considers the right type. |
| 9028 | if (const BuiltinType *pty = Input->getType()->getAsPlaceholderType()) { |
| 9029 | // Increment and decrement of pseudo-object references. |
| 9030 | if (pty->getKind() == BuiltinType::PseudoObject && |
| 9031 | UnaryOperator::isIncrementDecrementOp(Opc)) |
| 9032 | return checkPseudoObjectIncDec(S, OpLoc, Opc, Input); |
| 9033 | |
| 9034 | // extension is always a builtin operator. |
| 9035 | if (Opc == UO_Extension) |
| 9036 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
| 9037 | |
| 9038 | // & gets special logic for several kinds of placeholder. |
| 9039 | // The builtin code knows what to do. |
| 9040 | if (Opc == UO_AddrOf && |
| 9041 | (pty->getKind() == BuiltinType::Overload || |
| 9042 | pty->getKind() == BuiltinType::UnknownAny || |
| 9043 | pty->getKind() == BuiltinType::BoundMember)) |
| 9044 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
| 9045 | |
| 9046 | // Anything else needs to be handled now. |
| 9047 | ExprResult Result = CheckPlaceholderExpr(Input); |
| 9048 | if (Result.isInvalid()) return ExprError(); |
| 9049 | Input = Result.take(); |
| 9050 | } |
| 9051 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9052 | if (getLangOpts().CPlusPlus && Input->getType()->isOverloadableType() && |
Douglas Gregor | d3d0853 | 2011-12-14 21:23:13 +0000 | [diff] [blame] | 9053 | UnaryOperator::getOverloadedOperator(Opc) != OO_None && |
| 9054 | !(Opc == UO_AddrOf && isQualifiedMemberAccess(Input))) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9055 | // Find all of the overloaded operators visible from this |
| 9056 | // point. We perform both an operator-name lookup from the local |
| 9057 | // scope and an argument-dependent lookup based on the types of |
| 9058 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9059 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9060 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9061 | if (S && OverOp != OO_None) |
| 9062 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 9063 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9064 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9065 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9066 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9067 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9068 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9069 | } |
| 9070 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9071 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9072 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 9073 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9074 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9075 | } |
| 9076 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 9077 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9078 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 9079 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9080 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9081 | // 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] | 9082 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9083 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9084 | } |
| 9085 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9086 | /// Given the last statement in a statement-expression, check whether |
| 9087 | /// the result is a producing expression (like a call to an |
| 9088 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 9089 | /// release out of the full-expression. Otherwise, return null. |
| 9090 | /// Cannot fail. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9091 | static Expr *maybeRebuildARCConsumingStmt(Stmt *Statement) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9092 | // Should always be wrapped with one of these. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9093 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(Statement); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9094 | if (!cleanups) return 0; |
| 9095 | |
| 9096 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 9097 | if (!cast || cast->getCastKind() != CK_ARCConsumeObject) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9098 | return 0; |
| 9099 | |
| 9100 | // Splice out the cast. This shouldn't modify any interesting |
| 9101 | // features of the statement. |
| 9102 | Expr *producer = cast->getSubExpr(); |
| 9103 | assert(producer->getType() == cast->getType()); |
| 9104 | assert(producer->getValueKind() == cast->getValueKind()); |
| 9105 | cleanups->setSubExpr(producer); |
| 9106 | return cleanups; |
| 9107 | } |
| 9108 | |
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 9109 | void Sema::ActOnStartStmtExpr() { |
| 9110 | PushExpressionEvaluationContext(ExprEvalContexts.back().Context); |
| 9111 | } |
| 9112 | |
| 9113 | void Sema::ActOnStmtExprError() { |
John McCall | 7f39d51 | 2012-04-06 18:20:53 +0000 | [diff] [blame] | 9114 | // Note that function is also called by TreeTransform when leaving a |
| 9115 | // StmtExpr scope without rebuilding anything. |
| 9116 | |
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 9117 | DiscardCleanupsInEvaluationContext(); |
| 9118 | PopExpressionEvaluationContext(); |
| 9119 | } |
| 9120 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9121 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9122 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9123 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9124 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 9125 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 9126 | |
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 9127 | if (hasAnyUnrecoverableErrorsInThisFunction()) |
| 9128 | DiscardCleanupsInEvaluationContext(); |
| 9129 | assert(!ExprNeedsCleanups && "cleanups within StmtExpr not correctly bound!"); |
| 9130 | PopExpressionEvaluationContext(); |
| 9131 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 9132 | bool isFileScope |
| 9133 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 9134 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9135 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 9136 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9137 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 9138 | // example, it is not possible to goto into a stmt expression apparently. |
| 9139 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9140 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9141 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 9142 | // as the type of the stmtexpr. |
| 9143 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9144 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9145 | if (!Compound->body_empty()) { |
| 9146 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9147 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9148 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9149 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 9150 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9151 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9152 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9153 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9154 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9155 | // Do function/array conversion on the last expression, but not |
| 9156 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9157 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 9158 | if (LastExpr.isInvalid()) |
| 9159 | return ExprError(); |
| 9160 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9161 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9162 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9163 | // In ARC, if the final expression ends in a consume, splice |
| 9164 | // the consume out and bind it later. In the alternate case |
| 9165 | // (when dealing with a retainable type), the result |
| 9166 | // initialization will create a produce. In both cases the |
| 9167 | // result will be +1, and we'll need to balance that out with |
| 9168 | // a bind. |
| 9169 | if (Expr *rebuiltLastStmt |
| 9170 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 9171 | LastExpr = rebuiltLastStmt; |
| 9172 | } else { |
| 9173 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9174 | InitializedEntity::InitializeResult(LPLoc, |
| 9175 | Ty, |
| 9176 | false), |
| 9177 | SourceLocation(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9178 | LastExpr); |
| 9179 | } |
| 9180 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9181 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9182 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9183 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9184 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9185 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9186 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9187 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9188 | StmtExprMayBindToTemp = true; |
| 9189 | } |
| 9190 | } |
| 9191 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9192 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9193 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9194 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 9195 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9196 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 9197 | if (StmtExprMayBindToTemp) |
| 9198 | return MaybeBindToTemporary(ResStmtExpr); |
| 9199 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9200 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 9201 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9202 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9203 | TypeSourceInfo *TInfo, |
| 9204 | OffsetOfComponent *CompPtr, |
| 9205 | unsigned NumComponents, |
| 9206 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9207 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9208 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9209 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9210 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9211 | // We must have at least one component that refers to the type, and the first |
| 9212 | // one is known to be a field designator. Verify that the ArgTy represents |
| 9213 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9214 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9215 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 9216 | << ArgTy << TypeRange); |
| 9217 | |
| 9218 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 9219 | // with an incomplete type would be ill-formed. |
| 9220 | if (!Dependent |
| 9221 | && RequireCompleteType(BuiltinLoc, ArgTy, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 9222 | diag::err_offsetof_incomplete_type, TypeRange)) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9223 | return ExprError(); |
| 9224 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9225 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 9226 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 9227 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 9228 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9229 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 9230 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 9231 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9232 | |
| 9233 | bool DidWarnAboutNonPOD = false; |
| 9234 | QualType CurrentType = ArgTy; |
| 9235 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9236 | SmallVector<OffsetOfNode, 4> Comps; |
| 9237 | SmallVector<Expr*, 4> Exprs; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9238 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 9239 | const OffsetOfComponent &OC = CompPtr[i]; |
| 9240 | if (OC.isBrackets) { |
| 9241 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 9242 | if (!CurrentType->isDependentType()) { |
| 9243 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 9244 | if(!AT) |
| 9245 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 9246 | << CurrentType); |
| 9247 | CurrentType = AT->getElementType(); |
| 9248 | } else |
| 9249 | CurrentType = Context.DependentTy; |
| 9250 | |
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 9251 | ExprResult IdxRval = DefaultLvalueConversion(static_cast<Expr*>(OC.U.E)); |
| 9252 | if (IdxRval.isInvalid()) |
| 9253 | return ExprError(); |
| 9254 | Expr *Idx = IdxRval.take(); |
| 9255 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9256 | // The expression must be an integral expression. |
| 9257 | // FIXME: An integral constant expression? |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9258 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 9259 | !Idx->getType()->isIntegerType()) |
| 9260 | return ExprError(Diag(Idx->getLocStart(), |
| 9261 | diag::err_typecheck_subscript_not_integer) |
| 9262 | << Idx->getSourceRange()); |
Richard Smith | d82e5d3 | 2011-10-17 05:48:07 +0000 | [diff] [blame] | 9263 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9264 | // Record this array index. |
| 9265 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 9266 | Exprs.push_back(Idx); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9267 | continue; |
| 9268 | } |
| 9269 | |
| 9270 | // Offset of a field. |
| 9271 | if (CurrentType->isDependentType()) { |
| 9272 | // We have the offset of a field, but we can't look into the dependent |
| 9273 | // type. Just record the identifier of the field. |
| 9274 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 9275 | CurrentType = Context.DependentTy; |
| 9276 | continue; |
| 9277 | } |
| 9278 | |
| 9279 | // We need to have a complete type to look into. |
| 9280 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 9281 | diag::err_offsetof_incomplete_type)) |
| 9282 | return ExprError(); |
| 9283 | |
| 9284 | // Look for the designated field. |
| 9285 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 9286 | if (!RC) |
| 9287 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 9288 | << CurrentType); |
| 9289 | RecordDecl *RD = RC->getDecl(); |
| 9290 | |
| 9291 | // C++ [lib.support.types]p5: |
| 9292 | // The macro offsetof accepts a restricted set of type arguments in this |
| 9293 | // International Standard. type shall be a POD structure or a POD union |
| 9294 | // (clause 9). |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9295 | // C++11 [support.types]p4: |
| 9296 | // If type is not a standard-layout class (Clause 9), the results are |
| 9297 | // undefined. |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9298 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9299 | bool IsSafe = LangOpts.CPlusPlus11? CRD->isStandardLayout() : CRD->isPOD(); |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9300 | unsigned DiagID = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9301 | LangOpts.CPlusPlus11? diag::warn_offsetof_non_standardlayout_type |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9302 | : diag::warn_offsetof_non_pod_type; |
| 9303 | |
| 9304 | if (!IsSafe && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9305 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9306 | PDiag(DiagID) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9307 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 9308 | << CurrentType)) |
| 9309 | DidWarnAboutNonPOD = true; |
| 9310 | } |
| 9311 | |
| 9312 | // Look for the field. |
| 9313 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 9314 | LookupQualifiedName(R, RD); |
| 9315 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9316 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 9317 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 9318 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9319 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 9320 | } |
| 9321 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9322 | if (!MemberDecl) |
| 9323 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 9324 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 9325 | OC.LocEnd)); |
| 9326 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9327 | // C99 7.17p3: |
| 9328 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 9329 | // |
| 9330 | // We diagnose this as an error. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9331 | if (MemberDecl->isBitField()) { |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9332 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 9333 | << MemberDecl->getDeclName() |
| 9334 | << SourceRange(BuiltinLoc, RParenLoc); |
| 9335 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 9336 | return ExprError(); |
| 9337 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9338 | |
| 9339 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9340 | if (IndirectMemberDecl) |
| 9341 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9342 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9343 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 9344 | // the base class indirections. |
| 9345 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 9346 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9347 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9348 | CXXBasePath &Path = Paths.front(); |
| 9349 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 9350 | B != BEnd; ++B) |
| 9351 | Comps.push_back(OffsetOfNode(B->Base)); |
| 9352 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9353 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9354 | if (IndirectMemberDecl) { |
| 9355 | for (IndirectFieldDecl::chain_iterator FI = |
| 9356 | IndirectMemberDecl->chain_begin(), |
| 9357 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 9358 | assert(isa<FieldDecl>(*FI)); |
| 9359 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 9360 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 9361 | } |
| 9362 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9363 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9364 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9365 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9366 | } |
| 9367 | |
| 9368 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 9369 | TInfo, Comps, Exprs, RParenLoc)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9370 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9371 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9372 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9373 | SourceLocation BuiltinLoc, |
| 9374 | SourceLocation TypeLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9375 | ParsedType ParsedArgTy, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9376 | OffsetOfComponent *CompPtr, |
| 9377 | unsigned NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9378 | SourceLocation RParenLoc) { |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9379 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9380 | TypeSourceInfo *ArgTInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9381 | QualType ArgTy = GetTypeFromParser(ParsedArgTy, &ArgTInfo); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9382 | if (ArgTy.isNull()) |
| 9383 | return ExprError(); |
| 9384 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 9385 | if (!ArgTInfo) |
| 9386 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 9387 | |
| 9388 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9389 | RParenLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9390 | } |
| 9391 | |
| 9392 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9393 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9394 | Expr *CondExpr, |
| 9395 | Expr *LHSExpr, Expr *RHSExpr, |
| 9396 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9397 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 9398 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9399 | ExprValueKind VK = VK_RValue; |
| 9400 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9401 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9402 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 9403 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9404 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9405 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9406 | } else { |
| 9407 | // The conditional expression is required to be a constant expression. |
| 9408 | llvm::APSInt condEval(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9409 | ExprResult CondICE |
| 9410 | = VerifyIntegerConstantExpression(CondExpr, &condEval, |
| 9411 | diag::err_typecheck_choose_expr_requires_constant, false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9412 | if (CondICE.isInvalid()) |
| 9413 | return ExprError(); |
| 9414 | CondExpr = CondICE.take(); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9415 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9416 | // 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] | 9417 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 9418 | |
| 9419 | resType = ActiveExpr->getType(); |
| 9420 | ValueDependent = ActiveExpr->isValueDependent(); |
| 9421 | VK = ActiveExpr->getValueKind(); |
| 9422 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9423 | } |
| 9424 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9425 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9426 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9427 | resType->isDependentType(), |
| 9428 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9429 | } |
| 9430 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9431 | //===----------------------------------------------------------------------===// |
| 9432 | // Clang Extensions. |
| 9433 | //===----------------------------------------------------------------------===// |
| 9434 | |
| 9435 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9436 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9437 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9438 | PushBlockScope(CurScope, Block); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9439 | CurContext->addDecl(Block); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9440 | if (CurScope) |
| 9441 | PushDeclContext(CurScope, Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9442 | else |
| 9443 | CurContext = Block; |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9444 | |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 9445 | getCurBlock()->HasImplicitReturnType = true; |
| 9446 | |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9447 | // Enter a new evaluation context to insulate the block from any |
| 9448 | // cleanups from the enclosing full-expression. |
| 9449 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9450 | } |
| 9451 | |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 9452 | void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, |
| 9453 | Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 9454 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9455 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9456 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9457 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9458 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9459 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9460 | |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 9461 | // FIXME: We should allow unexpanded parameter packs here, but that would, |
| 9462 | // in turn, make the block expression contain unexpanded parameter packs. |
| 9463 | if (DiagnoseUnexpandedParameterPack(CaretLoc, Sig, UPPC_Block)) { |
| 9464 | // Drop the parameters. |
| 9465 | FunctionProtoType::ExtProtoInfo EPI; |
| 9466 | EPI.HasTrailingReturn = false; |
| 9467 | EPI.TypeQuals |= DeclSpec::TQ_const; |
| 9468 | T = Context.getFunctionType(Context.DependentTy, /*Args=*/0, /*NumArgs=*/0, |
| 9469 | EPI); |
| 9470 | Sig = Context.getTrivialTypeSourceInfo(T); |
| 9471 | } |
| 9472 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9473 | // GetTypeForDeclarator always produces a function type for a block |
| 9474 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 9475 | // unless the function was written with a typedef. |
| 9476 | assert(T->isFunctionType() && |
| 9477 | "GetTypeForDeclarator made a non-function block signature"); |
| 9478 | |
| 9479 | // Look for an explicit signature in that function type. |
| 9480 | FunctionProtoTypeLoc ExplicitSignature; |
| 9481 | |
| 9482 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 9483 | if ((ExplicitSignature = tmp.getAs<FunctionProtoTypeLoc>())) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9484 | |
| 9485 | // Check whether that explicit signature was synthesized by |
| 9486 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 9487 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 9488 | if (ExplicitSignature.getLocalRangeBegin() == |
| 9489 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9490 | // This would be much cheaper if we stored TypeLocs instead of |
| 9491 | // TypeSourceInfos. |
| 9492 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 9493 | unsigned Size = Result.getFullDataSize(); |
| 9494 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 9495 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 9496 | |
| 9497 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 9498 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9500 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9501 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 9502 | CurBlock->FunctionType = T; |
| 9503 | |
| 9504 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 9505 | QualType RetTy = Fn->getResultType(); |
| 9506 | bool isVariadic = |
| 9507 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 9508 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9509 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 9510 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9511 | // Don't allow returning a objc interface by value. |
| 9512 | if (RetTy->isObjCObjectType()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9513 | Diag(ParamInfo.getLocStart(), |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9514 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 9515 | return; |
| 9516 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9517 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9518 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9519 | // return type. TODO: what should we do with declarators like: |
| 9520 | // ^ * { ... } |
| 9521 | // If the answer is "apply template argument deduction".... |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9522 | if (RetTy != Context.DependentTy) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9523 | CurBlock->ReturnType = RetTy; |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9524 | CurBlock->TheDecl->setBlockMissingReturnType(false); |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 9525 | CurBlock->HasImplicitReturnType = false; |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9526 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9527 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9528 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9529 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9530 | if (ExplicitSignature) { |
| 9531 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 9532 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9533 | if (Param->getIdentifier() == 0 && |
| 9534 | !Param->isImplicit() && |
| 9535 | !Param->isInvalidDecl() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9536 | !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9537 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9538 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9539 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9540 | |
| 9541 | // Fake up parameter variables if we have a typedef, like |
| 9542 | // ^ fntype { ... } |
| 9543 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 9544 | for (FunctionProtoType::arg_type_iterator |
| 9545 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 9546 | ParmVarDecl *Param = |
| 9547 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9548 | ParamInfo.getLocStart(), |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9549 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9550 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9551 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9552 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9553 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9554 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9555 | if (!Params.empty()) { |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9556 | CurBlock->TheDecl->setParams(Params); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9557 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 9558 | CurBlock->TheDecl->param_end(), |
| 9559 | /*CheckParameterNames=*/false); |
| 9560 | } |
| 9561 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9562 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9563 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9564 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9565 | // Put the parameter variables in scope. We can bail out immediately |
| 9566 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9567 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9568 | return; |
| 9569 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9570 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9571 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 9572 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 9573 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9574 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9575 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9576 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9577 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9578 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9579 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9580 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9581 | } |
| 9582 | |
| 9583 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 9584 | /// is invoked to pop the information about the block from the action impl. |
| 9585 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9586 | // Leave the expression-evaluation context. |
| 9587 | DiscardCleanupsInEvaluationContext(); |
| 9588 | PopExpressionEvaluationContext(); |
| 9589 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9590 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9591 | PopDeclContext(); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9592 | PopFunctionScopeInfo(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9593 | } |
| 9594 | |
| 9595 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 9596 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9597 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9598 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9599 | // If blocks are disabled, emit an error. |
| 9600 | if (!LangOpts.Blocks) |
| 9601 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9602 | |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9603 | // Leave the expression-evaluation context. |
John McCall | 1e5bc4f | 2012-03-08 22:00:17 +0000 | [diff] [blame] | 9604 | if (hasAnyUnrecoverableErrorsInThisFunction()) |
| 9605 | DiscardCleanupsInEvaluationContext(); |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9606 | assert(!ExprNeedsCleanups && "cleanups within block not correctly bound!"); |
| 9607 | PopExpressionEvaluationContext(); |
| 9608 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9609 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9610 | |
| 9611 | if (BSI->HasImplicitReturnType) |
| 9612 | deduceClosureReturnType(*BSI); |
| 9613 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9614 | PopDeclContext(); |
| 9615 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9616 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9617 | if (!BSI->ReturnType.isNull()) |
| 9618 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9619 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9620 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9621 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9622 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9623 | // Set the captured variables on the block. |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 9624 | // FIXME: Share capture structure between BlockDecl and CapturingScopeInfo! |
| 9625 | SmallVector<BlockDecl::Capture, 4> Captures; |
| 9626 | for (unsigned i = 0, e = BSI->Captures.size(); i != e; i++) { |
| 9627 | CapturingScopeInfo::Capture &Cap = BSI->Captures[i]; |
| 9628 | if (Cap.isThisCapture()) |
| 9629 | continue; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9630 | BlockDecl::Capture NewCap(Cap.getVariable(), Cap.isBlockCapture(), |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 9631 | Cap.isNested(), Cap.getCopyExpr()); |
| 9632 | Captures.push_back(NewCap); |
| 9633 | } |
| 9634 | BSI->TheDecl->setCaptures(Context, Captures.begin(), Captures.end(), |
| 9635 | BSI->CXXThisCaptureIndex != 0); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9636 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9637 | // If the user wrote a function type in some form, try to use that. |
| 9638 | if (!BSI->FunctionType.isNull()) { |
| 9639 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 9640 | |
| 9641 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 9642 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 9643 | |
| 9644 | // Turn protoless block types into nullary block types. |
| 9645 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9646 | FunctionProtoType::ExtProtoInfo EPI; |
| 9647 | EPI.ExtInfo = Ext; |
| 9648 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9649 | |
| 9650 | // Otherwise, if we don't need to change anything about the function type, |
| 9651 | // preserve its sugar structure. |
| 9652 | } else if (FTy->getResultType() == RetTy && |
| 9653 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 9654 | BlockTy = BSI->FunctionType; |
| 9655 | |
| 9656 | // Otherwise, make the minimal modifications to the function type. |
| 9657 | } else { |
| 9658 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9659 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9660 | EPI.TypeQuals = 0; // FIXME: silently? |
| 9661 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9662 | BlockTy = Context.getFunctionType(RetTy, |
| 9663 | FPT->arg_type_begin(), |
| 9664 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9665 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9666 | } |
| 9667 | |
| 9668 | // If we don't have a function type, just build one from nothing. |
| 9669 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9670 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9671 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9672 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9673 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9674 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9675 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 9676 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9677 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9678 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 9679 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9680 | if (getCurFunction()->NeedsScopeChecking() && |
Douglas Gregor | 27bec77 | 2012-08-17 05:12:08 +0000 | [diff] [blame] | 9681 | !hasAnyUnrecoverableErrorsInThisFunction() && |
| 9682 | !PP.isCodeCompletionEnabled()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9683 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9684 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9685 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9686 | |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9687 | // Try to apply the named return value optimization. We have to check again |
| 9688 | // if we can do this, though, because blocks keep return statements around |
| 9689 | // to deduce an implicit return type. |
| 9690 | if (getLangOpts().CPlusPlus && RetTy->isRecordType() && |
| 9691 | !BSI->TheDecl->isDependentContext()) |
| 9692 | computeNRVO(Body, getCurBlock()); |
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9693 | |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 9694 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| 9695 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9696 | PopFunctionScopeInfo(&WP, Result->getBlockDecl(), Result); |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 9697 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9698 | // 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] | 9699 | // 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] | 9700 | if (Result->getBlockDecl()->hasCaptures()) { |
John McCall | 97b57a2 | 2012-04-13 01:08:17 +0000 | [diff] [blame] | 9701 | // First, this expression has a new cleanup object. |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9702 | ExprCleanupObjects.push_back(Result->getBlockDecl()); |
| 9703 | ExprNeedsCleanups = true; |
John McCall | 97b57a2 | 2012-04-13 01:08:17 +0000 | [diff] [blame] | 9704 | |
| 9705 | // It also gets a branch-protected scope if any of the captured |
| 9706 | // variables needs destruction. |
| 9707 | for (BlockDecl::capture_const_iterator |
| 9708 | ci = Result->getBlockDecl()->capture_begin(), |
| 9709 | ce = Result->getBlockDecl()->capture_end(); ci != ce; ++ci) { |
| 9710 | const VarDecl *var = ci->getVariable(); |
| 9711 | if (var->getType().isDestructedType() != QualType::DK_none) { |
| 9712 | getCurFunction()->setHasBranchProtectedScope(); |
| 9713 | break; |
| 9714 | } |
| 9715 | } |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9716 | } |
Fariborz Jahanian | 27949f6 | 2012-03-06 18:41:35 +0000 | [diff] [blame] | 9717 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9718 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9719 | } |
| 9720 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9721 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9722 | Expr *E, ParsedType Ty, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9723 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9724 | TypeSourceInfo *TInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9725 | GetTypeFromParser(Ty, &TInfo); |
| 9726 | return BuildVAArgExpr(BuiltinLoc, E, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9727 | } |
| 9728 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9729 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9730 | Expr *E, TypeSourceInfo *TInfo, |
| 9731 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9732 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9733 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9734 | // Get the va_list type |
| 9735 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9736 | if (VaListType->isArrayType()) { |
| 9737 | // Deal with implicit array decay; for example, on x86-64, |
| 9738 | // va_list is an array, but it's supposed to decay to |
| 9739 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9740 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9741 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9742 | ExprResult Result = UsualUnaryConversions(E); |
| 9743 | if (Result.isInvalid()) |
| 9744 | return ExprError(); |
| 9745 | E = Result.take(); |
Logan Chien | b687f3b | 2012-10-20 06:11:33 +0000 | [diff] [blame] | 9746 | } else if (VaListType->isRecordType() && getLangOpts().CPlusPlus) { |
| 9747 | // If va_list is a record type and we are compiling in C++ mode, |
| 9748 | // check the argument using reference binding. |
| 9749 | InitializedEntity Entity |
| 9750 | = InitializedEntity::InitializeParameter(Context, |
| 9751 | Context.getLValueReferenceType(VaListType), false); |
| 9752 | ExprResult Init = PerformCopyInitialization(Entity, SourceLocation(), E); |
| 9753 | if (Init.isInvalid()) |
| 9754 | return ExprError(); |
| 9755 | E = Init.takeAs<Expr>(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9756 | } else { |
| 9757 | // Otherwise, the va_list argument must be an l-value because |
| 9758 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9759 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9760 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9761 | return ExprError(); |
| 9762 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9763 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9764 | if (!E->isTypeDependent() && |
| 9765 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9766 | return ExprError(Diag(E->getLocStart(), |
| 9767 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9768 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 9769 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9770 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9771 | if (!TInfo->getType()->isDependentType()) { |
| 9772 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 9773 | diag::err_second_parameter_to_va_arg_incomplete, |
| 9774 | TInfo->getTypeLoc())) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9775 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 9776 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9777 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 9778 | TInfo->getType(), |
| 9779 | diag::err_second_parameter_to_va_arg_abstract, |
| 9780 | TInfo->getTypeLoc())) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9781 | return ExprError(); |
| 9782 | |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9783 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9784 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9785 | TInfo->getType()->isObjCLifetimeType() |
| 9786 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified |
| 9787 | : diag::warn_second_parameter_to_va_arg_not_pod) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9788 | << TInfo->getType() |
| 9789 | << TInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9790 | } |
Eli Friedman | 46d37c1 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 9791 | |
| 9792 | // Check for va_arg where arguments of the given type will be promoted |
| 9793 | // (i.e. this va_arg is guaranteed to have undefined behavior). |
| 9794 | QualType PromoteType; |
| 9795 | if (TInfo->getType()->isPromotableIntegerType()) { |
| 9796 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); |
| 9797 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) |
| 9798 | PromoteType = QualType(); |
| 9799 | } |
| 9800 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) |
| 9801 | PromoteType = Context.DoubleTy; |
| 9802 | if (!PromoteType.isNull()) |
Ted Kremenek | cbb99ef | 2013-01-08 01:50:40 +0000 | [diff] [blame] | 9803 | DiagRuntimeBehavior(TInfo->getTypeLoc().getBeginLoc(), E, |
| 9804 | PDiag(diag::warn_second_parameter_to_va_arg_never_compatible) |
| 9805 | << TInfo->getType() |
| 9806 | << PromoteType |
| 9807 | << TInfo->getTypeLoc().getSourceRange()); |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9808 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9809 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9810 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 9811 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9812 | } |
| 9813 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9814 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9815 | // The type of __null will be int or long, depending on the size of |
| 9816 | // pointers on the target. |
| 9817 | QualType Ty; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9818 | unsigned pw = Context.getTargetInfo().getPointerWidth(0); |
| 9819 | if (pw == Context.getTargetInfo().getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9820 | Ty = Context.IntTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9821 | else if (pw == Context.getTargetInfo().getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9822 | Ty = Context.LongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9823 | else if (pw == Context.getTargetInfo().getLongLongWidth()) |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9824 | Ty = Context.LongLongTy; |
| 9825 | else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9826 | llvm_unreachable("I don't know size of pointer!"); |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9827 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9828 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9829 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9830 | } |
| 9831 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9832 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9833 | Expr *SrcExpr, FixItHint &Hint) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9834 | if (!SemaRef.getLangOpts().ObjC1) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9835 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9836 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9837 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 9838 | if (!PT) |
| 9839 | return; |
| 9840 | |
| 9841 | // Check if the destination is of type 'id'. |
| 9842 | if (!PT->isObjCIdType()) { |
| 9843 | // Check if the destination is the 'NSString' interface. |
| 9844 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 9845 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 9846 | return; |
| 9847 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9848 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 9849 | // Ignore any parens, implicit casts (should only be |
| 9850 | // array-to-pointer decays), and not-so-opaque values. The last is |
| 9851 | // important for making this trigger for property assignments. |
| 9852 | SrcExpr = SrcExpr->IgnoreParenImpCasts(); |
| 9853 | if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(SrcExpr)) |
| 9854 | if (OV->getSourceExpr()) |
| 9855 | SrcExpr = OV->getSourceExpr()->IgnoreParenImpCasts(); |
| 9856 | |
| 9857 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 9858 | if (!SL || !SL->isAscii()) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9859 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9860 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9861 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9862 | } |
| 9863 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9864 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 9865 | SourceLocation Loc, |
| 9866 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9867 | Expr *SrcExpr, AssignmentAction Action, |
| 9868 | bool *Complained) { |
| 9869 | if (Complained) |
| 9870 | *Complained = false; |
| 9871 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9872 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9873 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9874 | bool isInvalid = false; |
Eli Friedman | fd81978 | 2012-02-29 20:59:56 +0000 | [diff] [blame] | 9875 | unsigned DiagKind = 0; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9876 | FixItHint Hint; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9877 | ConversionFixItGenerator ConvHints; |
| 9878 | bool MayHaveConvFixit = false; |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9879 | bool MayHaveFunctionDiff = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9880 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9881 | switch (ConvTy) { |
Fariborz Jahanian | 379b281 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 9882 | case Compatible: |
Daniel Dunbar | 7a0c064 | 2012-10-15 22:23:53 +0000 | [diff] [blame] | 9883 | DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr); |
| 9884 | return false; |
Fariborz Jahanian | 379b281 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 9885 | |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9886 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9887 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9888 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9889 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9890 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9891 | case IntToPointer: |
| 9892 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9893 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9894 | MayHaveConvFixit = true; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9895 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9896 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9897 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9898 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9899 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 9900 | SrcType->isObjCObjectPointerType(); |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9901 | if (Hint.isNull() && !CheckInferredResultType) { |
| 9902 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9903 | } |
| 9904 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9905 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9906 | case IncompatiblePointerSign: |
| 9907 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9908 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9909 | case FunctionVoidPointer: |
| 9910 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9911 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9912 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9913 | // Perform array-to-pointer decay if necessary. |
| 9914 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9915 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9916 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9917 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9918 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9919 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9920 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9921 | |
| 9922 | |
| 9923 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 9924 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9925 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9926 | } |
| 9927 | |
| 9928 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9929 | // fallthrough |
| 9930 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9931 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9932 | // If the qualifiers lost were because we were applying the |
| 9933 | // (deprecated) C++ conversion from a string literal to a char* |
| 9934 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9935 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9936 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9937 | // bit of refactoring (so that the second argument is an |
| 9938 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9939 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9940 | // C++ semantics. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9941 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9942 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9943 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9944 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9945 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9946 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9947 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9948 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9949 | case IntToBlockPointer: |
| 9950 | DiagKind = diag::err_int_to_block_pointer; |
| 9951 | break; |
| 9952 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9953 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9954 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9955 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9956 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9957 | // it can give a more specific diagnostic. |
| 9958 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9959 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9960 | case IncompatibleVectors: |
| 9961 | DiagKind = diag::warn_incompatible_vectors; |
| 9962 | break; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 9963 | case IncompatibleObjCWeakRef: |
| 9964 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 9965 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9966 | case Incompatible: |
| 9967 | DiagKind = diag::err_typecheck_convert_incompatible; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9968 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9969 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9970 | isInvalid = true; |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9971 | MayHaveFunctionDiff = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9972 | break; |
| 9973 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9974 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9975 | QualType FirstType, SecondType; |
| 9976 | switch (Action) { |
| 9977 | case AA_Assigning: |
| 9978 | case AA_Initializing: |
| 9979 | // The destination type comes first. |
| 9980 | FirstType = DstType; |
| 9981 | SecondType = SrcType; |
| 9982 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9983 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9984 | case AA_Returning: |
| 9985 | case AA_Passing: |
| 9986 | case AA_Converting: |
| 9987 | case AA_Sending: |
| 9988 | case AA_Casting: |
| 9989 | // The source type comes first. |
| 9990 | FirstType = SrcType; |
| 9991 | SecondType = DstType; |
| 9992 | break; |
| 9993 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9994 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9995 | PartialDiagnostic FDiag = PDiag(DiagKind); |
| 9996 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); |
| 9997 | |
| 9998 | // If we can fix the conversion, suggest the FixIts. |
| 9999 | assert(ConvHints.isNull() || Hint.isNull()); |
| 10000 | if (!ConvHints.isNull()) { |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 10001 | for (std::vector<FixItHint>::iterator HI = ConvHints.Hints.begin(), |
| 10002 | HE = ConvHints.Hints.end(); HI != HE; ++HI) |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 10003 | FDiag << *HI; |
| 10004 | } else { |
| 10005 | FDiag << Hint; |
| 10006 | } |
| 10007 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } |
| 10008 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 10009 | if (MayHaveFunctionDiff) |
| 10010 | HandleFunctionTypeMismatch(FDiag, SecondType, FirstType); |
| 10011 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 10012 | Diag(Loc, FDiag); |
| 10013 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 10014 | if (SecondType == Context.OverloadTy) |
| 10015 | NoteAllOverloadCandidates(OverloadExpr::find(SrcExpr).Expression, |
| 10016 | FirstType); |
| 10017 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 10018 | if (CheckInferredResultType) |
| 10019 | EmitRelatedResultTypeNote(SrcExpr); |
| 10020 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 10021 | if (Complained) |
| 10022 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10023 | return isInvalid; |
| 10024 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10025 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10026 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, |
| 10027 | llvm::APSInt *Result) { |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10028 | class SimpleICEDiagnoser : public VerifyICEDiagnoser { |
| 10029 | public: |
| 10030 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { |
| 10031 | S.Diag(Loc, diag::err_expr_not_ice) << S.LangOpts.CPlusPlus << SR; |
| 10032 | } |
| 10033 | } Diagnoser; |
| 10034 | |
| 10035 | return VerifyIntegerConstantExpression(E, Result, Diagnoser); |
| 10036 | } |
| 10037 | |
| 10038 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, |
| 10039 | llvm::APSInt *Result, |
| 10040 | unsigned DiagID, |
| 10041 | bool AllowFold) { |
| 10042 | class IDDiagnoser : public VerifyICEDiagnoser { |
| 10043 | unsigned DiagID; |
| 10044 | |
| 10045 | public: |
| 10046 | IDDiagnoser(unsigned DiagID) |
| 10047 | : VerifyICEDiagnoser(DiagID == 0), DiagID(DiagID) { } |
| 10048 | |
| 10049 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { |
| 10050 | S.Diag(Loc, DiagID) << SR; |
| 10051 | } |
| 10052 | } Diagnoser(DiagID); |
| 10053 | |
| 10054 | return VerifyIntegerConstantExpression(E, Result, Diagnoser, AllowFold); |
| 10055 | } |
| 10056 | |
| 10057 | void Sema::VerifyICEDiagnoser::diagnoseFold(Sema &S, SourceLocation Loc, |
| 10058 | SourceRange SR) { |
| 10059 | S.Diag(Loc, diag::ext_expr_not_ice) << SR << S.LangOpts.CPlusPlus; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10060 | } |
| 10061 | |
Benjamin Kramer | d448ce0 | 2012-04-18 14:22:41 +0000 | [diff] [blame] | 10062 | ExprResult |
| 10063 | Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10064 | VerifyICEDiagnoser &Diagnoser, |
| 10065 | bool AllowFold) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10066 | SourceLocation DiagLoc = E->getLocStart(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10067 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10068 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10069 | // C++11 [expr.const]p5: |
| 10070 | // If an expression of literal class type is used in a context where an |
| 10071 | // integral constant expression is required, then that class type shall |
| 10072 | // have a single non-explicit conversion function to an integral or |
| 10073 | // unscoped enumeration type |
| 10074 | ExprResult Converted; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10075 | if (!Diagnoser.Suppress) { |
| 10076 | class CXX11ConvertDiagnoser : public ICEConvertDiagnoser { |
| 10077 | public: |
| 10078 | CXX11ConvertDiagnoser() : ICEConvertDiagnoser(false, true) { } |
| 10079 | |
| 10080 | virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 10081 | QualType T) { |
| 10082 | return S.Diag(Loc, diag::err_ice_not_integral) << T; |
| 10083 | } |
| 10084 | |
| 10085 | virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, |
| 10086 | SourceLocation Loc, |
| 10087 | QualType T) { |
| 10088 | return S.Diag(Loc, diag::err_ice_incomplete_type) << T; |
| 10089 | } |
| 10090 | |
| 10091 | virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, |
| 10092 | SourceLocation Loc, |
| 10093 | QualType T, |
| 10094 | QualType ConvTy) { |
| 10095 | return S.Diag(Loc, diag::err_ice_explicit_conversion) << T << ConvTy; |
| 10096 | } |
| 10097 | |
| 10098 | virtual DiagnosticBuilder noteExplicitConv(Sema &S, |
| 10099 | CXXConversionDecl *Conv, |
| 10100 | QualType ConvTy) { |
| 10101 | return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) |
| 10102 | << ConvTy->isEnumeralType() << ConvTy; |
| 10103 | } |
| 10104 | |
| 10105 | virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 10106 | QualType T) { |
| 10107 | return S.Diag(Loc, diag::err_ice_ambiguous_conversion) << T; |
| 10108 | } |
| 10109 | |
| 10110 | virtual DiagnosticBuilder noteAmbiguous(Sema &S, |
| 10111 | CXXConversionDecl *Conv, |
| 10112 | QualType ConvTy) { |
| 10113 | return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) |
| 10114 | << ConvTy->isEnumeralType() << ConvTy; |
| 10115 | } |
| 10116 | |
| 10117 | virtual DiagnosticBuilder diagnoseConversion(Sema &S, |
| 10118 | SourceLocation Loc, |
| 10119 | QualType T, |
| 10120 | QualType ConvTy) { |
| 10121 | return DiagnosticBuilder::getEmpty(); |
| 10122 | } |
| 10123 | } ConvertDiagnoser; |
| 10124 | |
| 10125 | Converted = ConvertToIntegralOrEnumerationType(DiagLoc, E, |
| 10126 | ConvertDiagnoser, |
| 10127 | /*AllowScopedEnumerations*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10128 | } else { |
| 10129 | // The caller wants to silently enquire whether this is an ICE. Don't |
| 10130 | // produce any diagnostics if it isn't. |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10131 | class SilentICEConvertDiagnoser : public ICEConvertDiagnoser { |
| 10132 | public: |
| 10133 | SilentICEConvertDiagnoser() : ICEConvertDiagnoser(true, true) { } |
| 10134 | |
| 10135 | virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 10136 | QualType T) { |
| 10137 | return DiagnosticBuilder::getEmpty(); |
| 10138 | } |
| 10139 | |
| 10140 | virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, |
| 10141 | SourceLocation Loc, |
| 10142 | QualType T) { |
| 10143 | return DiagnosticBuilder::getEmpty(); |
| 10144 | } |
| 10145 | |
| 10146 | virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, |
| 10147 | SourceLocation Loc, |
| 10148 | QualType T, |
| 10149 | QualType ConvTy) { |
| 10150 | return DiagnosticBuilder::getEmpty(); |
| 10151 | } |
| 10152 | |
| 10153 | virtual DiagnosticBuilder noteExplicitConv(Sema &S, |
| 10154 | CXXConversionDecl *Conv, |
| 10155 | QualType ConvTy) { |
| 10156 | return DiagnosticBuilder::getEmpty(); |
| 10157 | } |
| 10158 | |
| 10159 | virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 10160 | QualType T) { |
| 10161 | return DiagnosticBuilder::getEmpty(); |
| 10162 | } |
| 10163 | |
| 10164 | virtual DiagnosticBuilder noteAmbiguous(Sema &S, |
| 10165 | CXXConversionDecl *Conv, |
| 10166 | QualType ConvTy) { |
| 10167 | return DiagnosticBuilder::getEmpty(); |
| 10168 | } |
| 10169 | |
| 10170 | virtual DiagnosticBuilder diagnoseConversion(Sema &S, |
| 10171 | SourceLocation Loc, |
| 10172 | QualType T, |
| 10173 | QualType ConvTy) { |
| 10174 | return DiagnosticBuilder::getEmpty(); |
| 10175 | } |
| 10176 | } ConvertDiagnoser; |
| 10177 | |
| 10178 | Converted = ConvertToIntegralOrEnumerationType(DiagLoc, E, |
| 10179 | ConvertDiagnoser, false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10180 | } |
| 10181 | if (Converted.isInvalid()) |
| 10182 | return Converted; |
| 10183 | E = Converted.take(); |
| 10184 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) |
| 10185 | return ExprError(); |
| 10186 | } else if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
| 10187 | // An ICE must be of integral or unscoped enumeration type. |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10188 | if (!Diagnoser.Suppress) |
| 10189 | Diagnoser.diagnoseNotICE(*this, DiagLoc, E->getSourceRange()); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10190 | return ExprError(); |
| 10191 | } |
| 10192 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10193 | // Circumvent ICE checking in C++11 to avoid evaluating the expression twice |
| 10194 | // in the non-ICE case. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10195 | if (!getLangOpts().CPlusPlus11 && E->isIntegerConstantExpr(Context)) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10196 | if (Result) |
| 10197 | *Result = E->EvaluateKnownConstInt(Context); |
| 10198 | return Owned(E); |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10199 | } |
| 10200 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10201 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10202 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 10203 | EvalResult.Diag = &Notes; |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10204 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10205 | // Try to evaluate the expression, and produce diagnostics explaining why it's |
| 10206 | // not a constant expression as a side-effect. |
| 10207 | bool Folded = E->EvaluateAsRValue(EvalResult, Context) && |
| 10208 | EvalResult.Val.isInt() && !EvalResult.HasSideEffects; |
| 10209 | |
| 10210 | // In C++11, we can rely on diagnostics being produced for any expression |
| 10211 | // which is not a constant expression. If no diagnostics were produced, then |
| 10212 | // this is a constant expression. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10213 | if (Folded && getLangOpts().CPlusPlus11 && Notes.empty()) { |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10214 | if (Result) |
| 10215 | *Result = EvalResult.Val.getInt(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10216 | return Owned(E); |
| 10217 | } |
| 10218 | |
| 10219 | // If our only note is the usual "invalid subexpression" note, just point |
| 10220 | // the caret at its location rather than producing an essentially |
| 10221 | // redundant note. |
| 10222 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 10223 | diag::note_invalid_subexpr_in_const_expr) { |
| 10224 | DiagLoc = Notes[0].first; |
| 10225 | Notes.clear(); |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10226 | } |
| 10227 | |
| 10228 | if (!Folded || !AllowFold) { |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10229 | if (!Diagnoser.Suppress) { |
| 10230 | Diagnoser.diagnoseNotICE(*this, DiagLoc, E->getSourceRange()); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 10231 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 10232 | Diag(Notes[I].first, Notes[I].second); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10233 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10234 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10235 | return ExprError(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10236 | } |
| 10237 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10238 | Diagnoser.diagnoseFold(*this, DiagLoc, E->getSourceRange()); |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 10239 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 10240 | Diag(Notes[I].first, Notes[I].second); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10241 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10242 | if (Result) |
| 10243 | *Result = EvalResult.Val.getInt(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10244 | return Owned(E); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10245 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10246 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10247 | namespace { |
| 10248 | // Handle the case where we conclude a expression which we speculatively |
| 10249 | // considered to be unevaluated is actually evaluated. |
| 10250 | class TransformToPE : public TreeTransform<TransformToPE> { |
| 10251 | typedef TreeTransform<TransformToPE> BaseTransform; |
| 10252 | |
| 10253 | public: |
| 10254 | TransformToPE(Sema &SemaRef) : BaseTransform(SemaRef) { } |
| 10255 | |
| 10256 | // Make sure we redo semantic analysis |
| 10257 | bool AlwaysRebuild() { return true; } |
| 10258 | |
Eli Friedman | 56ff283 | 2012-02-06 23:29:57 +0000 | [diff] [blame] | 10259 | // Make sure we handle LabelStmts correctly. |
| 10260 | // FIXME: This does the right thing, but maybe we need a more general |
| 10261 | // fix to TreeTransform? |
| 10262 | StmtResult TransformLabelStmt(LabelStmt *S) { |
| 10263 | S->getDecl()->setStmt(0); |
| 10264 | return BaseTransform::TransformLabelStmt(S); |
| 10265 | } |
| 10266 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10267 | // We need to special-case DeclRefExprs referring to FieldDecls which |
| 10268 | // are not part of a member pointer formation; normal TreeTransforming |
| 10269 | // doesn't catch this case because of the way we represent them in the AST. |
| 10270 | // FIXME: This is a bit ugly; is it really the best way to handle this |
| 10271 | // case? |
| 10272 | // |
| 10273 | // Error on DeclRefExprs referring to FieldDecls. |
| 10274 | ExprResult TransformDeclRefExpr(DeclRefExpr *E) { |
| 10275 | if (isa<FieldDecl>(E->getDecl()) && |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 10276 | !SemaRef.isUnevaluatedContext()) |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10277 | return SemaRef.Diag(E->getLocation(), |
| 10278 | diag::err_invalid_non_static_member_use) |
| 10279 | << E->getDecl() << E->getSourceRange(); |
| 10280 | |
| 10281 | return BaseTransform::TransformDeclRefExpr(E); |
| 10282 | } |
| 10283 | |
| 10284 | // Exception: filter out member pointer formation |
| 10285 | ExprResult TransformUnaryOperator(UnaryOperator *E) { |
| 10286 | if (E->getOpcode() == UO_AddrOf && E->getType()->isMemberPointerType()) |
| 10287 | return E; |
| 10288 | |
| 10289 | return BaseTransform::TransformUnaryOperator(E); |
| 10290 | } |
| 10291 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10292 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 10293 | // Lambdas never need to be transformed. |
| 10294 | return E; |
| 10295 | } |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10296 | }; |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 10297 | } |
| 10298 | |
Benjamin Kramer | accaf19 | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 10299 | ExprResult Sema::TransformToPotentiallyEvaluated(Expr *E) { |
Eli Friedman | 72b8b1e | 2012-02-29 04:03:55 +0000 | [diff] [blame] | 10300 | assert(ExprEvalContexts.back().Context == Unevaluated && |
| 10301 | "Should only transform unevaluated expressions"); |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10302 | ExprEvalContexts.back().Context = |
| 10303 | ExprEvalContexts[ExprEvalContexts.size()-2].Context; |
| 10304 | if (ExprEvalContexts.back().Context == Unevaluated) |
| 10305 | return E; |
| 10306 | return TransformToPE(*this).TransformExpr(E); |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 10307 | } |
| 10308 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10309 | void |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 10310 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext, |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 10311 | Decl *LambdaContextDecl, |
| 10312 | bool IsDecltype) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10313 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10314 | ExpressionEvaluationContextRecord(NewContext, |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10315 | ExprCleanupObjects.size(), |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 10316 | ExprNeedsCleanups, |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 10317 | LambdaContextDecl, |
| 10318 | IsDecltype)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10319 | ExprNeedsCleanups = false; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10320 | if (!MaybeODRUseExprs.empty()) |
| 10321 | std::swap(MaybeODRUseExprs, ExprEvalContexts.back().SavedMaybeODRUseExprs); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10322 | } |
| 10323 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 10324 | void |
| 10325 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext, |
| 10326 | ReuseLambdaContextDecl_t, |
| 10327 | bool IsDecltype) { |
| 10328 | Decl *LambdaContextDecl = ExprEvalContexts.back().LambdaContextDecl; |
| 10329 | PushExpressionEvaluationContext(NewContext, LambdaContextDecl, IsDecltype); |
| 10330 | } |
| 10331 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 10332 | void Sema::PopExpressionEvaluationContext() { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10333 | ExpressionEvaluationContextRecord& Rec = ExprEvalContexts.back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10334 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10335 | if (!Rec.Lambdas.empty()) { |
| 10336 | if (Rec.Context == Unevaluated) { |
| 10337 | // C++11 [expr.prim.lambda]p2: |
| 10338 | // A lambda-expression shall not appear in an unevaluated operand |
| 10339 | // (Clause 5). |
| 10340 | for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) |
| 10341 | Diag(Rec.Lambdas[I]->getLocStart(), |
| 10342 | diag::err_lambda_unevaluated_operand); |
| 10343 | } else { |
| 10344 | // Mark the capture expressions odr-used. This was deferred |
| 10345 | // during lambda expression creation. |
| 10346 | for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) { |
| 10347 | LambdaExpr *Lambda = Rec.Lambdas[I]; |
| 10348 | for (LambdaExpr::capture_init_iterator |
| 10349 | C = Lambda->capture_init_begin(), |
| 10350 | CEnd = Lambda->capture_init_end(); |
| 10351 | C != CEnd; ++C) { |
| 10352 | MarkDeclarationsReferencedInExpr(*C); |
| 10353 | } |
| 10354 | } |
| 10355 | } |
| 10356 | } |
| 10357 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10358 | // When are coming out of an unevaluated context, clear out any |
| 10359 | // temporaries that we may have created as part of the evaluation of |
| 10360 | // the expression in that context: they aren't relevant because they |
| 10361 | // will never be constructed. |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 10362 | if (Rec.Context == Unevaluated || Rec.Context == ConstantEvaluated) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10363 | ExprCleanupObjects.erase(ExprCleanupObjects.begin() + Rec.NumCleanupObjects, |
| 10364 | ExprCleanupObjects.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10365 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10366 | CleanupVarDeclMarking(); |
| 10367 | std::swap(MaybeODRUseExprs, Rec.SavedMaybeODRUseExprs); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10368 | // Otherwise, merge the contexts together. |
| 10369 | } else { |
| 10370 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10371 | MaybeODRUseExprs.insert(Rec.SavedMaybeODRUseExprs.begin(), |
| 10372 | Rec.SavedMaybeODRUseExprs.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10373 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10374 | |
| 10375 | // Pop the current expression evaluation context off the stack. |
| 10376 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10377 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10378 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10379 | void Sema::DiscardCleanupsInEvaluationContext() { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10380 | ExprCleanupObjects.erase( |
| 10381 | ExprCleanupObjects.begin() + ExprEvalContexts.back().NumCleanupObjects, |
| 10382 | ExprCleanupObjects.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10383 | ExprNeedsCleanups = false; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10384 | MaybeODRUseExprs.clear(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10385 | } |
| 10386 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 10387 | ExprResult Sema::HandleExprEvaluationContextForTypeof(Expr *E) { |
| 10388 | if (!E->getType()->isVariablyModifiedType()) |
| 10389 | return E; |
Benjamin Kramer | accaf19 | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 10390 | return TransformToPotentiallyEvaluated(E); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 10391 | } |
| 10392 | |
Benjamin Kramer | 5bbc385 | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 10393 | static bool IsPotentiallyEvaluatedContext(Sema &SemaRef) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10394 | // Do not mark anything as "used" within a dependent context; wait for |
| 10395 | // an instantiation. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10396 | if (SemaRef.CurContext->isDependentContext()) |
| 10397 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10398 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10399 | switch (SemaRef.ExprEvalContexts.back().Context) { |
| 10400 | case Sema::Unevaluated: |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10401 | // We are in an expression that is not potentially evaluated; do nothing. |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 10402 | // (Depending on how you read the standard, we actually do need to do |
| 10403 | // something here for null pointer constants, but the standard's |
| 10404 | // definition of a null pointer constant is completely crazy.) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10405 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10406 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10407 | case Sema::ConstantEvaluated: |
| 10408 | case Sema::PotentiallyEvaluated: |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 10409 | // We are in a potentially evaluated expression (or a constant-expression |
| 10410 | // in C++03); we need to do implicit template instantiation, implicitly |
| 10411 | // define class members, and mark most declarations as used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10412 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10413 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10414 | case Sema::PotentiallyEvaluatedIfUsed: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10415 | // Referenced declarations will only be used if the construct in the |
| 10416 | // containing expression is used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10417 | return false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10418 | } |
Matt Beaumont-Gay | 4f7dcdb | 2012-02-02 18:35:35 +0000 | [diff] [blame] | 10419 | llvm_unreachable("Invalid context"); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10420 | } |
| 10421 | |
| 10422 | /// \brief Mark a function referenced, and check whether it is odr-used |
| 10423 | /// (C++ [basic.def.odr]p2, C99 6.9p3) |
| 10424 | void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func) { |
| 10425 | assert(Func && "No function?"); |
| 10426 | |
| 10427 | Func->setReferenced(); |
| 10428 | |
Richard Smith | ce2661f | 2012-11-07 01:14:25 +0000 | [diff] [blame] | 10429 | // C++11 [basic.def.odr]p3: |
| 10430 | // A function whose name appears as a potentially-evaluated expression is |
| 10431 | // odr-used if it is the unique lookup result or the selected member of a |
| 10432 | // set of overloaded functions [...]. |
| 10433 | // |
| 10434 | // We (incorrectly) mark overload resolution as an unevaluated context, so we |
| 10435 | // can just check that here. Skip the rest of this function if we've already |
| 10436 | // marked the function as used. |
| 10437 | if (Func->isUsed(false) || !IsPotentiallyEvaluatedContext(*this)) { |
| 10438 | // C++11 [temp.inst]p3: |
| 10439 | // Unless a function template specialization has been explicitly |
| 10440 | // instantiated or explicitly specialized, the function template |
| 10441 | // specialization is implicitly instantiated when the specialization is |
| 10442 | // referenced in a context that requires a function definition to exist. |
| 10443 | // |
| 10444 | // We consider constexpr function templates to be referenced in a context |
| 10445 | // that requires a definition to exist whenever they are referenced. |
| 10446 | // |
| 10447 | // FIXME: This instantiates constexpr functions too frequently. If this is |
| 10448 | // really an unevaluated context (and we're not just in the definition of a |
| 10449 | // function template or overload resolution or other cases which we |
| 10450 | // incorrectly consider to be unevaluated contexts), and we're not in a |
| 10451 | // subexpression which we actually need to evaluate (for instance, a |
| 10452 | // template argument, array bound or an expression in a braced-init-list), |
| 10453 | // we are not permitted to instantiate this constexpr function definition. |
| 10454 | // |
| 10455 | // FIXME: This also implicitly defines special members too frequently. They |
| 10456 | // are only supposed to be implicitly defined if they are odr-used, but they |
| 10457 | // are not odr-used from constant expressions in unevaluated contexts. |
| 10458 | // However, they cannot be referenced if they are deleted, and they are |
| 10459 | // deleted whenever the implicit definition of the special member would |
| 10460 | // fail. |
| 10461 | if (!Func->isConstexpr() || Func->getBody()) |
| 10462 | return; |
| 10463 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Func); |
| 10464 | if (!Func->isImplicitlyInstantiable() && (!MD || MD->isUserProvided())) |
| 10465 | return; |
| 10466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10467 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10468 | // Note that this declaration has been used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10469 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10470 | if (Constructor->isDefaulted() && !Constructor->isDeleted()) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10471 | if (Constructor->isDefaultConstructor()) { |
| 10472 | if (Constructor->isTrivial()) |
| 10473 | return; |
| 10474 | if (!Constructor->isUsed(false)) |
| 10475 | DefineImplicitDefaultConstructor(Loc, Constructor); |
| 10476 | } else if (Constructor->isCopyConstructor()) { |
| 10477 | if (!Constructor->isUsed(false)) |
| 10478 | DefineImplicitCopyConstructor(Loc, Constructor); |
| 10479 | } else if (Constructor->isMoveConstructor()) { |
| 10480 | if (!Constructor->isUsed(false)) |
| 10481 | DefineImplicitMoveConstructor(Loc, Constructor); |
| 10482 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10483 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10484 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10485 | MarkVTableUsed(Loc, Constructor->getParent()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10486 | } else if (CXXDestructorDecl *Destructor = |
| 10487 | dyn_cast<CXXDestructorDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10488 | if (Destructor->isDefaulted() && !Destructor->isDeleted() && |
| 10489 | !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10490 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10491 | if (Destructor->isVirtual()) |
| 10492 | MarkVTableUsed(Loc, Destructor->getParent()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10493 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10494 | if (MethodDecl->isDefaulted() && !MethodDecl->isDeleted() && |
| 10495 | MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10496 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10497 | if (!MethodDecl->isUsed(false)) { |
| 10498 | if (MethodDecl->isCopyAssignmentOperator()) |
| 10499 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
| 10500 | else |
| 10501 | DefineImplicitMoveAssignment(Loc, MethodDecl); |
| 10502 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10503 | } else if (isa<CXXConversionDecl>(MethodDecl) && |
| 10504 | MethodDecl->getParent()->isLambda()) { |
| 10505 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(MethodDecl); |
| 10506 | if (Conversion->isLambdaToBlockPointerConversion()) |
| 10507 | DefineImplicitLambdaToBlockPointerConversion(Loc, Conversion); |
| 10508 | else |
| 10509 | DefineImplicitLambdaToFunctionPointerConversion(Loc, Conversion); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10510 | } else if (MethodDecl->isVirtual()) |
| 10511 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10512 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10513 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10514 | // Recursive functions should be marked when used from another function. |
| 10515 | // FIXME: Is this really right? |
| 10516 | if (CurContext == Func) return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10517 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10518 | // Resolve the exception specification for any function which is |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10519 | // used: CodeGen will need it. |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 10520 | const FunctionProtoType *FPT = Func->getType()->getAs<FunctionProtoType>(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10521 | if (FPT && isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) |
| 10522 | ResolveExceptionSpec(Loc, FPT); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10523 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10524 | // Implicit instantiation of function templates and member functions of |
| 10525 | // class templates. |
| 10526 | if (Func->isImplicitlyInstantiable()) { |
| 10527 | bool AlreadyInstantiated = false; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10528 | SourceLocation PointOfInstantiation = Loc; |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10529 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 10530 | = Func->getTemplateSpecializationInfo()) { |
| 10531 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 10532 | SpecInfo->setPointOfInstantiation(Loc); |
| 10533 | else if (SpecInfo->getTemplateSpecializationKind() |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10534 | == TSK_ImplicitInstantiation) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10535 | AlreadyInstantiated = true; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10536 | PointOfInstantiation = SpecInfo->getPointOfInstantiation(); |
| 10537 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10538 | } else if (MemberSpecializationInfo *MSInfo |
| 10539 | = Func->getMemberSpecializationInfo()) { |
| 10540 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10541 | MSInfo->setPointOfInstantiation(Loc); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10542 | else if (MSInfo->getTemplateSpecializationKind() |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10543 | == TSK_ImplicitInstantiation) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10544 | AlreadyInstantiated = true; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10545 | PointOfInstantiation = MSInfo->getPointOfInstantiation(); |
| 10546 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10547 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10548 | |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10549 | if (!AlreadyInstantiated || Func->isConstexpr()) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10550 | if (isa<CXXRecordDecl>(Func->getDeclContext()) && |
| 10551 | cast<CXXRecordDecl>(Func->getDeclContext())->isLocalClass()) |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10552 | PendingLocalImplicitInstantiations.push_back( |
| 10553 | std::make_pair(Func, PointOfInstantiation)); |
| 10554 | else if (Func->isConstexpr()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10555 | // Do not defer instantiations of constexpr functions, to avoid the |
| 10556 | // expression evaluator needing to call back into Sema if it sees a |
| 10557 | // call to such a function. |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10558 | InstantiateFunctionDefinition(PointOfInstantiation, Func); |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 10559 | else { |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10560 | PendingInstantiations.push_back(std::make_pair(Func, |
| 10561 | PointOfInstantiation)); |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 10562 | // Notify the consumer that a function was implicitly instantiated. |
| 10563 | Consumer.HandleCXXImplicitFunctionInstantiation(Func); |
| 10564 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10565 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10566 | } else { |
| 10567 | // Walk redefinitions, as some of them may be instantiable. |
| 10568 | for (FunctionDecl::redecl_iterator i(Func->redecls_begin()), |
| 10569 | e(Func->redecls_end()); i != e; ++i) { |
| 10570 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
| 10571 | MarkFunctionReferenced(Loc, *i); |
| 10572 | } |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 10573 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10574 | |
| 10575 | // Keep track of used but undefined functions. |
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 10576 | if (!Func->isDefined()) { |
| 10577 | if (Func->getLinkage() != ExternalLinkage) |
| 10578 | UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc)); |
| 10579 | else if (Func->getMostRecentDecl()->isInlined() && |
| 10580 | (LangOpts.CPlusPlus || !LangOpts.GNUInline) && |
| 10581 | !Func->getMostRecentDecl()->hasAttr<GNUInlineAttr>()) |
| 10582 | UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc)); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10583 | } |
| 10584 | |
Rafael Espindola | b9725cf | 2013-01-08 19:43:34 +0000 | [diff] [blame] | 10585 | // Normally the must current decl is marked used while processing the use and |
| 10586 | // any subsequent decls are marked used by decl merging. This fails with |
| 10587 | // template instantiation since marking can happen at the end of the file |
| 10588 | // and, because of the two phase lookup, this function is called with at |
| 10589 | // decl in the middle of a decl chain. We loop to maintain the invariant |
| 10590 | // that once a decl is used, all decls after it are also used. |
Rafael Espindola | a6d2020 | 2013-01-08 19:58:34 +0000 | [diff] [blame] | 10591 | for (FunctionDecl *F = Func->getMostRecentDecl();; F = F->getPreviousDecl()) { |
Rafael Espindola | b9725cf | 2013-01-08 19:43:34 +0000 | [diff] [blame] | 10592 | F->setUsed(true); |
| 10593 | if (F == Func) |
| 10594 | break; |
Rafael Espindola | b9725cf | 2013-01-08 19:43:34 +0000 | [diff] [blame] | 10595 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10596 | } |
| 10597 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10598 | static void |
| 10599 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
| 10600 | VarDecl *var, DeclContext *DC) { |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10601 | DeclContext *VarDC = var->getDeclContext(); |
| 10602 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10603 | // If the parameter still belongs to the translation unit, then |
| 10604 | // we're actually just using one parameter in the declaration of |
| 10605 | // the next. |
| 10606 | if (isa<ParmVarDecl>(var) && |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10607 | isa<TranslationUnitDecl>(VarDC)) |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10608 | return; |
| 10609 | |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10610 | // For C code, don't diagnose about capture if we're not actually in code |
| 10611 | // right now; it's impossible to write a non-constant expression outside of |
| 10612 | // function context, so we'll get other (more useful) diagnostics later. |
| 10613 | // |
| 10614 | // For C++, things get a bit more nasty... it would be nice to suppress this |
| 10615 | // diagnostic for certain cases like using a local variable in an array bound |
| 10616 | // 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] | 10617 | if (!S.getLangOpts().CPlusPlus && !S.CurContext->isFunctionOrMethod()) |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10618 | return; |
| 10619 | |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10620 | if (isa<CXXMethodDecl>(VarDC) && |
| 10621 | cast<CXXRecordDecl>(VarDC->getParent())->isLambda()) { |
| 10622 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_lambda) |
| 10623 | << var->getIdentifier(); |
| 10624 | } else if (FunctionDecl *fn = dyn_cast<FunctionDecl>(VarDC)) { |
| 10625 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 10626 | << var->getIdentifier() << fn->getDeclName(); |
| 10627 | } else if (isa<BlockDecl>(VarDC)) { |
| 10628 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_block) |
| 10629 | << var->getIdentifier(); |
| 10630 | } else { |
| 10631 | // FIXME: Is there any other context where a local variable can be |
| 10632 | // declared? |
| 10633 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_context) |
| 10634 | << var->getIdentifier(); |
| 10635 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10636 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10637 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 10638 | << var->getIdentifier(); |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10639 | |
| 10640 | // FIXME: Add additional diagnostic info about class etc. which prevents |
| 10641 | // capture. |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10642 | } |
| 10643 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10644 | /// \brief Capture the given variable in the given lambda expression. |
| 10645 | static ExprResult captureInLambda(Sema &S, LambdaScopeInfo *LSI, |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10646 | VarDecl *Var, QualType FieldType, |
| 10647 | QualType DeclRefType, |
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10648 | SourceLocation Loc, |
| 10649 | bool RefersToEnclosingLocal) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10650 | CXXRecordDecl *Lambda = LSI->Lambda; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10651 | |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10652 | // Build the non-static data member. |
| 10653 | FieldDecl *Field |
| 10654 | = FieldDecl::Create(S.Context, Lambda, Loc, Loc, 0, FieldType, |
| 10655 | S.Context.getTrivialTypeSourceInfo(FieldType, Loc), |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 10656 | 0, false, ICIS_NoInit); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10657 | Field->setImplicit(true); |
| 10658 | Field->setAccess(AS_private); |
Douglas Gregor | 20f87a4 | 2012-02-09 02:12:34 +0000 | [diff] [blame] | 10659 | Lambda->addDecl(Field); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10660 | |
| 10661 | // C++11 [expr.prim.lambda]p21: |
| 10662 | // When the lambda-expression is evaluated, the entities that |
| 10663 | // are captured by copy are used to direct-initialize each |
| 10664 | // corresponding non-static data member of the resulting closure |
| 10665 | // object. (For array members, the array elements are |
| 10666 | // direct-initialized in increasing subscript order.) These |
| 10667 | // initializations are performed in the (unspecified) order in |
| 10668 | // which the non-static data members are declared. |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10669 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10670 | // Introduce a new evaluation context for the initialization, so |
| 10671 | // that temporaries introduced as part of the capture are retained |
| 10672 | // to be re-"exported" from the lambda expression itself. |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10673 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 10674 | |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 10675 | // C++ [expr.prim.labda]p12: |
| 10676 | // An entity captured by a lambda-expression is odr-used (3.2) in |
| 10677 | // the scope containing the lambda-expression. |
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10678 | Expr *Ref = new (S.Context) DeclRefExpr(Var, RefersToEnclosingLocal, |
| 10679 | DeclRefType, VK_LValue, Loc); |
Eli Friedman | 88530d5 | 2012-03-01 21:32:56 +0000 | [diff] [blame] | 10680 | Var->setReferenced(true); |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 10681 | Var->setUsed(true); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10682 | |
| 10683 | // When the field has array type, create index variables for each |
| 10684 | // dimension of the array. We use these index variables to subscript |
| 10685 | // the source array, and other clients (e.g., CodeGen) will perform |
| 10686 | // the necessary iteration with these index variables. |
| 10687 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10688 | QualType BaseType = FieldType; |
| 10689 | QualType SizeType = S.Context.getSizeType(); |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 10690 | LSI->ArrayIndexStarts.push_back(LSI->ArrayIndexVars.size()); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10691 | while (const ConstantArrayType *Array |
| 10692 | = S.Context.getAsConstantArrayType(BaseType)) { |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10693 | // Create the iteration variable for this array index. |
| 10694 | IdentifierInfo *IterationVarName = 0; |
| 10695 | { |
| 10696 | SmallString<8> Str; |
| 10697 | llvm::raw_svector_ostream OS(Str); |
| 10698 | OS << "__i" << IndexVariables.size(); |
| 10699 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 10700 | } |
| 10701 | VarDecl *IterationVar |
| 10702 | = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
| 10703 | IterationVarName, SizeType, |
| 10704 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
| 10705 | SC_None, SC_None); |
| 10706 | IndexVariables.push_back(IterationVar); |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 10707 | LSI->ArrayIndexVars.push_back(IterationVar); |
| 10708 | |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10709 | // Create a reference to the iteration variable. |
| 10710 | ExprResult IterationVarRef |
| 10711 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
| 10712 | assert(!IterationVarRef.isInvalid() && |
| 10713 | "Reference to invented variable cannot fail!"); |
| 10714 | IterationVarRef = S.DefaultLvalueConversion(IterationVarRef.take()); |
| 10715 | assert(!IterationVarRef.isInvalid() && |
| 10716 | "Conversion of invented variable cannot fail!"); |
| 10717 | |
| 10718 | // Subscript the array with this iteration variable. |
| 10719 | ExprResult Subscript = S.CreateBuiltinArraySubscriptExpr( |
| 10720 | Ref, Loc, IterationVarRef.take(), Loc); |
| 10721 | if (Subscript.isInvalid()) { |
| 10722 | S.CleanupVarDeclMarking(); |
| 10723 | S.DiscardCleanupsInEvaluationContext(); |
| 10724 | S.PopExpressionEvaluationContext(); |
| 10725 | return ExprError(); |
| 10726 | } |
| 10727 | |
| 10728 | Ref = Subscript.take(); |
| 10729 | BaseType = Array->getElementType(); |
| 10730 | } |
| 10731 | |
| 10732 | // Construct the entity that we will be initializing. For an array, this |
| 10733 | // will be first element in the array, which may require several levels |
| 10734 | // of array-subscript entities. |
| 10735 | SmallVector<InitializedEntity, 4> Entities; |
| 10736 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 10737 | Entities.push_back( |
| 10738 | InitializedEntity::InitializeLambdaCapture(Var, Field, Loc)); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10739 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 10740 | Entities.push_back(InitializedEntity::InitializeElement(S.Context, |
| 10741 | 0, |
| 10742 | Entities.back())); |
| 10743 | |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10744 | InitializationKind InitKind |
| 10745 | = InitializationKind::CreateDirect(Loc, Loc, Loc); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10746 | InitializationSequence Init(S, Entities.back(), InitKind, &Ref, 1); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10747 | ExprResult Result(true); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10748 | if (!Init.Diagnose(S, Entities.back(), InitKind, &Ref, 1)) |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10749 | Result = Init.Perform(S, Entities.back(), InitKind, Ref); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10750 | |
| 10751 | // If this initialization requires any cleanups (e.g., due to a |
| 10752 | // default argument to a copy constructor), note that for the |
| 10753 | // lambda. |
| 10754 | if (S.ExprNeedsCleanups) |
| 10755 | LSI->ExprNeedsCleanups = true; |
| 10756 | |
| 10757 | // Exit the expression evaluation context used for the capture. |
| 10758 | S.CleanupVarDeclMarking(); |
| 10759 | S.DiscardCleanupsInEvaluationContext(); |
| 10760 | S.PopExpressionEvaluationContext(); |
| 10761 | return Result; |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10762 | } |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10763 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10764 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, |
| 10765 | TryCaptureKind Kind, SourceLocation EllipsisLoc, |
| 10766 | bool BuildAndDiagnose, |
| 10767 | QualType &CaptureType, |
| 10768 | QualType &DeclRefType) { |
| 10769 | bool Nested = false; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10770 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10771 | DeclContext *DC = CurContext; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10772 | if (Var->getDeclContext() == DC) return true; |
| 10773 | if (!Var->hasLocalStorage()) return true; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10774 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10775 | bool HasBlocksAttr = Var->hasAttr<BlocksAttr>(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10776 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10777 | // Walk up the stack to determine whether we can capture the variable, |
| 10778 | // performing the "simple" checks that don't depend on type. We stop when |
| 10779 | // we've either hit the declared scope of the variable or find an existing |
| 10780 | // capture of that variable. |
| 10781 | CaptureType = Var->getType(); |
| 10782 | DeclRefType = CaptureType.getNonReferenceType(); |
| 10783 | bool Explicit = (Kind != TryCapture_Implicit); |
| 10784 | unsigned FunctionScopesIndex = FunctionScopes.size() - 1; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10785 | do { |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10786 | // Only block literals and lambda expressions can capture; other |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10787 | // scopes don't work. |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10788 | DeclContext *ParentDC; |
| 10789 | if (isa<BlockDecl>(DC)) |
| 10790 | ParentDC = DC->getParent(); |
| 10791 | else if (isa<CXXMethodDecl>(DC) && |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10792 | cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call && |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10793 | cast<CXXRecordDecl>(DC->getParent())->isLambda()) |
| 10794 | ParentDC = DC->getParent()->getParent(); |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10795 | else { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10796 | if (BuildAndDiagnose) |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10797 | diagnoseUncapturableValueReference(*this, Loc, Var, DC); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10798 | return true; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10799 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10800 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10801 | CapturingScopeInfo *CSI = |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10802 | cast<CapturingScopeInfo>(FunctionScopes[FunctionScopesIndex]); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10803 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10804 | // Check whether we've already captured it. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10805 | if (CSI->CaptureMap.count(Var)) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10806 | // If we found a capture, any subcaptures are nested. |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10807 | Nested = true; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10808 | |
| 10809 | // Retrieve the capture type for this variable. |
| 10810 | CaptureType = CSI->getCapture(Var).getCaptureType(); |
| 10811 | |
| 10812 | // Compute the type of an expression that refers to this variable. |
| 10813 | DeclRefType = CaptureType.getNonReferenceType(); |
| 10814 | |
| 10815 | const CapturingScopeInfo::Capture &Cap = CSI->getCapture(Var); |
| 10816 | if (Cap.isCopyCapture() && |
| 10817 | !(isa<LambdaScopeInfo>(CSI) && cast<LambdaScopeInfo>(CSI)->Mutable)) |
| 10818 | DeclRefType.addConst(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10819 | break; |
| 10820 | } |
| 10821 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10822 | bool IsBlock = isa<BlockScopeInfo>(CSI); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10823 | bool IsLambda = !IsBlock; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10824 | |
| 10825 | // Lambdas are not allowed to capture unnamed variables |
| 10826 | // (e.g. anonymous unions). |
| 10827 | // FIXME: The C++11 rule don't actually state this explicitly, but I'm |
| 10828 | // assuming that's the intent. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10829 | if (IsLambda && !Var->getDeclName()) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10830 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10831 | Diag(Loc, diag::err_lambda_capture_anonymous_var); |
| 10832 | Diag(Var->getLocation(), diag::note_declared_at); |
| 10833 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10834 | return true; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10835 | } |
| 10836 | |
| 10837 | // Prohibit variably-modified types; they're difficult to deal with. |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10838 | if (Var->getType()->isVariablyModifiedType()) { |
| 10839 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10840 | if (IsBlock) |
| 10841 | Diag(Loc, diag::err_ref_vm_type); |
| 10842 | else |
| 10843 | Diag(Loc, diag::err_lambda_capture_vm_type) << Var->getDeclName(); |
| 10844 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10845 | << Var->getDeclName(); |
| 10846 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10847 | return true; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10848 | } |
Fariborz Jahanian | b20eb10 | 2013-01-08 23:48:48 +0000 | [diff] [blame] | 10849 | // Prohibit structs with flexible array members too. |
Fariborz Jahanian | 9c0816f | 2013-01-08 23:17:51 +0000 | [diff] [blame] | 10850 | // We cannot capture what is in the tail end of the struct. |
| 10851 | if (const RecordType *VTTy = Var->getType()->getAs<RecordType>()) { |
Fariborz Jahanian | e178e70 | 2013-01-09 00:09:15 +0000 | [diff] [blame] | 10852 | if (VTTy->getDecl()->hasFlexibleArrayMember()) { |
Fariborz Jahanian | 9c0816f | 2013-01-08 23:17:51 +0000 | [diff] [blame] | 10853 | if (BuildAndDiagnose) { |
| 10854 | if (IsBlock) |
| 10855 | Diag(Loc, diag::err_ref_flexarray_type); |
Fariborz Jahanian | e178e70 | 2013-01-09 00:09:15 +0000 | [diff] [blame] | 10856 | else |
| 10857 | Diag(Loc, diag::err_lambda_capture_flexarray_type) |
| 10858 | << Var->getDeclName(); |
Fariborz Jahanian | 9c0816f | 2013-01-08 23:17:51 +0000 | [diff] [blame] | 10859 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10860 | << Var->getDeclName(); |
| 10861 | } |
| 10862 | return true; |
| 10863 | } |
| 10864 | } |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10865 | // Lambdas are not allowed to capture __block variables; they don't |
| 10866 | // support the expected semantics. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10867 | if (IsLambda && HasBlocksAttr) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10868 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10869 | Diag(Loc, diag::err_lambda_capture_block) |
| 10870 | << Var->getDeclName(); |
| 10871 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10872 | << Var->getDeclName(); |
| 10873 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10874 | return true; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10875 | } |
| 10876 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10877 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None && !Explicit) { |
| 10878 | // No capture-default |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10879 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10880 | Diag(Loc, diag::err_lambda_impcap) << Var->getDeclName(); |
| 10881 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10882 | << Var->getDeclName(); |
| 10883 | Diag(cast<LambdaScopeInfo>(CSI)->Lambda->getLocStart(), |
| 10884 | diag::note_lambda_decl); |
| 10885 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10886 | return true; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10887 | } |
| 10888 | |
| 10889 | FunctionScopesIndex--; |
| 10890 | DC = ParentDC; |
| 10891 | Explicit = false; |
| 10892 | } while (!Var->getDeclContext()->Equals(DC)); |
| 10893 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10894 | // Walk back down the scope stack, computing the type of the capture at |
| 10895 | // each step, checking type-specific requirements, and adding captures if |
| 10896 | // requested. |
| 10897 | for (unsigned I = ++FunctionScopesIndex, N = FunctionScopes.size(); I != N; |
| 10898 | ++I) { |
| 10899 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[I]); |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 10900 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10901 | // Compute the type of the capture and of a reference to the capture within |
| 10902 | // this scope. |
| 10903 | if (isa<BlockScopeInfo>(CSI)) { |
| 10904 | Expr *CopyExpr = 0; |
| 10905 | bool ByRef = false; |
| 10906 | |
| 10907 | // Blocks are not allowed to capture arrays. |
| 10908 | if (CaptureType->isArrayType()) { |
| 10909 | if (BuildAndDiagnose) { |
| 10910 | Diag(Loc, diag::err_ref_array_type); |
| 10911 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10912 | << Var->getDeclName(); |
| 10913 | } |
| 10914 | return true; |
| 10915 | } |
| 10916 | |
John McCall | 100c649 | 2012-03-30 05:23:48 +0000 | [diff] [blame] | 10917 | // Forbid the block-capture of autoreleasing variables. |
| 10918 | if (CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { |
| 10919 | if (BuildAndDiagnose) { |
| 10920 | Diag(Loc, diag::err_arc_autoreleasing_capture) |
| 10921 | << /*block*/ 0; |
| 10922 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10923 | << Var->getDeclName(); |
| 10924 | } |
| 10925 | return true; |
| 10926 | } |
| 10927 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10928 | if (HasBlocksAttr || CaptureType->isReferenceType()) { |
| 10929 | // Block capture by reference does not change the capture or |
| 10930 | // declaration reference types. |
| 10931 | ByRef = true; |
| 10932 | } else { |
| 10933 | // Block capture by copy introduces 'const'. |
| 10934 | CaptureType = CaptureType.getNonReferenceType().withConst(); |
| 10935 | DeclRefType = CaptureType; |
| 10936 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10937 | if (getLangOpts().CPlusPlus && BuildAndDiagnose) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10938 | if (const RecordType *Record = DeclRefType->getAs<RecordType>()) { |
| 10939 | // The capture logic needs the destructor, so make sure we mark it. |
| 10940 | // Usually this is unnecessary because most local variables have |
| 10941 | // their destructors marked at declaration time, but parameters are |
| 10942 | // an exception because it's technically only the call site that |
| 10943 | // actually requires the destructor. |
| 10944 | if (isa<ParmVarDecl>(Var)) |
| 10945 | FinalizeVarWithDestructor(Var, Record); |
Douglas Gregor | 464a01a | 2012-12-01 01:01:09 +0000 | [diff] [blame] | 10946 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10947 | // According to the blocks spec, the capture of a variable from |
| 10948 | // the stack requires a const copy constructor. This is not true |
| 10949 | // of the copy/move done to move a __block variable to the heap. |
Douglas Gregor | 464a01a | 2012-12-01 01:01:09 +0000 | [diff] [blame] | 10950 | Expr *DeclRef = new (Context) DeclRefExpr(Var, Nested, |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10951 | DeclRefType.withConst(), |
| 10952 | VK_LValue, Loc); |
Douglas Gregor | 464a01a | 2012-12-01 01:01:09 +0000 | [diff] [blame] | 10953 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10954 | ExprResult Result |
| 10955 | = PerformCopyInitialization( |
| 10956 | InitializedEntity::InitializeBlock(Var->getLocation(), |
| 10957 | CaptureType, false), |
| 10958 | Loc, Owned(DeclRef)); |
| 10959 | |
| 10960 | // Build a full-expression copy expression if initialization |
| 10961 | // succeeded and used a non-trivial constructor. Recover from |
| 10962 | // errors by pretending that the copy isn't necessary. |
| 10963 | if (!Result.isInvalid() && |
| 10964 | !cast<CXXConstructExpr>(Result.get())->getConstructor() |
| 10965 | ->isTrivial()) { |
| 10966 | Result = MaybeCreateExprWithCleanups(Result); |
| 10967 | CopyExpr = Result.take(); |
| 10968 | } |
| 10969 | } |
| 10970 | } |
| 10971 | } |
| 10972 | |
| 10973 | // Actually capture the variable. |
| 10974 | if (BuildAndDiagnose) |
| 10975 | CSI->addCapture(Var, HasBlocksAttr, ByRef, Nested, Loc, |
| 10976 | SourceLocation(), CaptureType, CopyExpr); |
| 10977 | Nested = true; |
| 10978 | continue; |
| 10979 | } |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 10980 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10981 | LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CSI); |
| 10982 | |
| 10983 | // Determine whether we are capturing by reference or by value. |
| 10984 | bool ByRef = false; |
| 10985 | if (I == N - 1 && Kind != TryCapture_Implicit) { |
| 10986 | ByRef = (Kind == TryCapture_ExplicitByRef); |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10987 | } else { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10988 | ByRef = (LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByref); |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10989 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10990 | |
| 10991 | // Compute the type of the field that will capture this variable. |
| 10992 | if (ByRef) { |
| 10993 | // C++11 [expr.prim.lambda]p15: |
| 10994 | // An entity is captured by reference if it is implicitly or |
| 10995 | // explicitly captured but not captured by copy. It is |
| 10996 | // unspecified whether additional unnamed non-static data |
| 10997 | // members are declared in the closure type for entities |
| 10998 | // captured by reference. |
| 10999 | // |
| 11000 | // FIXME: It is not clear whether we want to build an lvalue reference |
| 11001 | // to the DeclRefType or to CaptureType.getNonReferenceType(). GCC appears |
| 11002 | // to do the former, while EDG does the latter. Core issue 1249 will |
| 11003 | // clarify, but for now we follow GCC because it's a more permissive and |
| 11004 | // easily defensible position. |
| 11005 | CaptureType = Context.getLValueReferenceType(DeclRefType); |
| 11006 | } else { |
| 11007 | // C++11 [expr.prim.lambda]p14: |
| 11008 | // For each entity captured by copy, an unnamed non-static |
| 11009 | // data member is declared in the closure type. The |
| 11010 | // declaration order of these members is unspecified. The type |
| 11011 | // of such a data member is the type of the corresponding |
| 11012 | // captured entity if the entity is not a reference to an |
| 11013 | // object, or the referenced type otherwise. [Note: If the |
| 11014 | // captured entity is a reference to a function, the |
| 11015 | // corresponding data member is also a reference to a |
| 11016 | // function. - end note ] |
| 11017 | if (const ReferenceType *RefType = CaptureType->getAs<ReferenceType>()){ |
| 11018 | if (!RefType->getPointeeType()->isFunctionType()) |
| 11019 | CaptureType = RefType->getPointeeType(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 11020 | } |
John McCall | 100c649 | 2012-03-30 05:23:48 +0000 | [diff] [blame] | 11021 | |
| 11022 | // Forbid the lambda copy-capture of autoreleasing variables. |
| 11023 | if (CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { |
| 11024 | if (BuildAndDiagnose) { |
| 11025 | Diag(Loc, diag::err_arc_autoreleasing_capture) << /*lambda*/ 1; |
| 11026 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 11027 | << Var->getDeclName(); |
| 11028 | } |
| 11029 | return true; |
| 11030 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 11031 | } |
| 11032 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 11033 | // Capture this variable in the lambda. |
| 11034 | Expr *CopyExpr = 0; |
| 11035 | if (BuildAndDiagnose) { |
| 11036 | ExprResult Result = captureInLambda(*this, LSI, Var, CaptureType, |
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 11037 | DeclRefType, Loc, |
Douglas Gregor | 464a01a | 2012-12-01 01:01:09 +0000 | [diff] [blame] | 11038 | Nested); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 11039 | if (!Result.isInvalid()) |
| 11040 | CopyExpr = Result.take(); |
| 11041 | } |
| 11042 | |
| 11043 | // Compute the type of a reference to this captured variable. |
| 11044 | if (ByRef) |
| 11045 | DeclRefType = CaptureType.getNonReferenceType(); |
| 11046 | else { |
| 11047 | // C++ [expr.prim.lambda]p5: |
| 11048 | // The closure type for a lambda-expression has a public inline |
| 11049 | // function call operator [...]. This function call operator is |
| 11050 | // declared const (9.3.1) if and only if the lambda-expression’s |
| 11051 | // parameter-declaration-clause is not followed by mutable. |
| 11052 | DeclRefType = CaptureType.getNonReferenceType(); |
| 11053 | if (!LSI->Mutable && !CaptureType->isReferenceType()) |
| 11054 | DeclRefType.addConst(); |
| 11055 | } |
| 11056 | |
| 11057 | // Add the capture. |
| 11058 | if (BuildAndDiagnose) |
| 11059 | CSI->addCapture(Var, /*IsBlock=*/false, ByRef, Nested, Loc, |
| 11060 | EllipsisLoc, CaptureType, CopyExpr); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 11061 | Nested = true; |
| 11062 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 11063 | |
| 11064 | return false; |
| 11065 | } |
| 11066 | |
| 11067 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, |
| 11068 | TryCaptureKind Kind, SourceLocation EllipsisLoc) { |
| 11069 | QualType CaptureType; |
| 11070 | QualType DeclRefType; |
| 11071 | return tryCaptureVariable(Var, Loc, Kind, EllipsisLoc, |
| 11072 | /*BuildAndDiagnose=*/true, CaptureType, |
| 11073 | DeclRefType); |
| 11074 | } |
| 11075 | |
| 11076 | QualType Sema::getCapturedDeclRefType(VarDecl *Var, SourceLocation Loc) { |
| 11077 | QualType CaptureType; |
| 11078 | QualType DeclRefType; |
| 11079 | |
| 11080 | // Determine whether we can capture this variable. |
| 11081 | if (tryCaptureVariable(Var, Loc, TryCapture_Implicit, SourceLocation(), |
| 11082 | /*BuildAndDiagnose=*/false, CaptureType, DeclRefType)) |
| 11083 | return QualType(); |
| 11084 | |
| 11085 | return DeclRefType; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 11086 | } |
| 11087 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11088 | static void MarkVarDeclODRUsed(Sema &SemaRef, VarDecl *Var, |
| 11089 | SourceLocation Loc) { |
| 11090 | // Keep track of used but undefined variables. |
Eli Friedman | 0cc5d40 | 2012-02-04 00:54:05 +0000 | [diff] [blame] | 11091 | // FIXME: We shouldn't suppress this warning for static data members. |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 11092 | if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly && |
Eli Friedman | 0cc5d40 | 2012-02-04 00:54:05 +0000 | [diff] [blame] | 11093 | Var->getLinkage() != ExternalLinkage && |
| 11094 | !(Var->isStaticDataMember() && Var->hasInit())) { |
Nick Lewycky | cd0655b | 2013-02-01 08:13:20 +0000 | [diff] [blame] | 11095 | SourceLocation &old = SemaRef.UndefinedButUsed[Var->getCanonicalDecl()]; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11096 | if (old.isInvalid()) old = Loc; |
| 11097 | } |
| 11098 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 11099 | SemaRef.tryCaptureVariable(Var, Loc); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 11100 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11101 | Var->setUsed(true); |
| 11102 | } |
| 11103 | |
| 11104 | void Sema::UpdateMarkingForLValueToRValue(Expr *E) { |
| 11105 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it is |
| 11106 | // an object that satisfies the requirements for appearing in a |
| 11107 | // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) |
| 11108 | // is immediately applied." This function handles the lvalue-to-rvalue |
| 11109 | // conversion part. |
| 11110 | MaybeODRUseExprs.erase(E->IgnoreParens()); |
| 11111 | } |
| 11112 | |
Eli Friedman | ac62601 | 2012-02-29 03:16:56 +0000 | [diff] [blame] | 11113 | ExprResult Sema::ActOnConstantExpression(ExprResult Res) { |
| 11114 | if (!Res.isUsable()) |
| 11115 | return Res; |
| 11116 | |
| 11117 | // If a constant-expression is a reference to a variable where we delay |
| 11118 | // deciding whether it is an odr-use, just assume we will apply the |
| 11119 | // lvalue-to-rvalue conversion. In the one case where this doesn't happen |
| 11120 | // (a non-type template argument), we have special handling anyway. |
| 11121 | UpdateMarkingForLValueToRValue(Res.get()); |
| 11122 | return Res; |
| 11123 | } |
| 11124 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11125 | void Sema::CleanupVarDeclMarking() { |
| 11126 | for (llvm::SmallPtrSetIterator<Expr*> i = MaybeODRUseExprs.begin(), |
| 11127 | e = MaybeODRUseExprs.end(); |
| 11128 | i != e; ++i) { |
| 11129 | VarDecl *Var; |
| 11130 | SourceLocation Loc; |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11131 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*i)) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11132 | Var = cast<VarDecl>(DRE->getDecl()); |
| 11133 | Loc = DRE->getLocation(); |
| 11134 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(*i)) { |
| 11135 | Var = cast<VarDecl>(ME->getMemberDecl()); |
| 11136 | Loc = ME->getMemberLoc(); |
| 11137 | } else { |
| 11138 | llvm_unreachable("Unexpcted expression"); |
| 11139 | } |
| 11140 | |
| 11141 | MarkVarDeclODRUsed(*this, Var, Loc); |
| 11142 | } |
| 11143 | |
| 11144 | MaybeODRUseExprs.clear(); |
| 11145 | } |
| 11146 | |
| 11147 | // Mark a VarDecl referenced, and perform the necessary handling to compute |
| 11148 | // odr-uses. |
| 11149 | static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, |
| 11150 | VarDecl *Var, Expr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11151 | Var->setReferenced(); |
| 11152 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11153 | if (!IsPotentiallyEvaluatedContext(SemaRef)) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11154 | return; |
| 11155 | |
| 11156 | // Implicit instantiation of static data members of class templates. |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 11157 | if (Var->isStaticDataMember() && Var->getInstantiatedFromStaticDataMember()) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11158 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 11159 | assert(MSInfo && "Missing member specialization information?"); |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 11160 | bool AlreadyInstantiated = !MSInfo->getPointOfInstantiation().isInvalid(); |
| 11161 | if (MSInfo->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 11162 | (!AlreadyInstantiated || |
| 11163 | Var->isUsableInConstantExpressions(SemaRef.Context))) { |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 11164 | if (!AlreadyInstantiated) { |
| 11165 | // This is a modification of an existing AST node. Notify listeners. |
| 11166 | if (ASTMutationListener *L = SemaRef.getASTMutationListener()) |
| 11167 | L->StaticDataMemberInstantiated(Var); |
| 11168 | MSInfo->setPointOfInstantiation(Loc); |
| 11169 | } |
| 11170 | SourceLocation PointOfInstantiation = MSInfo->getPointOfInstantiation(); |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 11171 | if (Var->isUsableInConstantExpressions(SemaRef.Context)) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11172 | // Do not defer instantiations of variables which could be used in a |
| 11173 | // constant expression. |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 11174 | SemaRef.InstantiateStaticDataMemberDefinition(PointOfInstantiation,Var); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11175 | else |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 11176 | SemaRef.PendingInstantiations.push_back( |
| 11177 | std::make_pair(Var, PointOfInstantiation)); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11178 | } |
| 11179 | } |
| 11180 | |
Richard Smith | 5016a70 | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 11181 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it satisfies |
| 11182 | // the requirements for appearing in a constant expression (5.19) and, if |
| 11183 | // it is an object, the lvalue-to-rvalue conversion (4.1) |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11184 | // is immediately applied." We check the first part here, and |
| 11185 | // Sema::UpdateMarkingForLValueToRValue deals with the second part. |
| 11186 | // Note that we use the C++11 definition everywhere because nothing in |
Richard Smith | 5016a70 | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 11187 | // C++03 depends on whether we get the C++03 version correct. The second |
| 11188 | // part does not apply to references, since they are not objects. |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11189 | const VarDecl *DefVD; |
Richard Smith | 5016a70 | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 11190 | if (E && !isa<ParmVarDecl>(Var) && |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 11191 | Var->isUsableInConstantExpressions(SemaRef.Context) && |
Richard Smith | 5016a70 | 2012-10-20 01:38:33 +0000 | [diff] [blame] | 11192 | Var->getAnyInitializer(DefVD) && DefVD->checkInitIsICE()) { |
| 11193 | if (!Var->getType()->isReferenceType()) |
| 11194 | SemaRef.MaybeODRUseExprs.insert(E); |
| 11195 | } else |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11196 | MarkVarDeclODRUsed(SemaRef, Var, Loc); |
| 11197 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11198 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11199 | /// \brief Mark a variable referenced, and check whether it is odr-used |
| 11200 | /// (C++ [basic.def.odr]p2, C99 6.9p3). Note that this should not be |
| 11201 | /// used directly for normal expressions referring to VarDecl. |
| 11202 | void Sema::MarkVariableReferenced(SourceLocation Loc, VarDecl *Var) { |
| 11203 | DoMarkVarDeclReferenced(*this, Loc, Var, 0); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11204 | } |
| 11205 | |
| 11206 | static void MarkExprReferenced(Sema &SemaRef, SourceLocation Loc, |
Nick Lewycky | b7e5eec | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 11207 | Decl *D, Expr *E, bool OdrUse) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11208 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 11209 | DoMarkVarDeclReferenced(SemaRef, Loc, Var, E); |
| 11210 | return; |
| 11211 | } |
| 11212 | |
Nick Lewycky | b7e5eec | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 11213 | SemaRef.MarkAnyDeclReferenced(Loc, D, OdrUse); |
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 11214 | |
| 11215 | // If this is a call to a method via a cast, also mark the method in the |
| 11216 | // derived class used in case codegen can devirtualize the call. |
| 11217 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); |
| 11218 | if (!ME) |
| 11219 | return; |
| 11220 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 11221 | if (!MD) |
| 11222 | return; |
| 11223 | const Expr *Base = ME->getBase(); |
Rafael Espindola | 8d852e3 | 2012-06-27 18:18:05 +0000 | [diff] [blame] | 11224 | const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType(); |
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 11225 | if (!MostDerivedClassDecl) |
| 11226 | return; |
| 11227 | CXXMethodDecl *DM = MD->getCorrespondingMethodInClass(MostDerivedClassDecl); |
Nick Lewycky | d3b4f0e | 2013-02-14 00:55:17 +0000 | [diff] [blame] | 11228 | if (!DM || DM->isPure()) |
Rafael Espindola | 0713d99 | 2012-06-27 17:44:39 +0000 | [diff] [blame] | 11229 | return; |
Nick Lewycky | b7e5eec | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 11230 | SemaRef.MarkAnyDeclReferenced(Loc, DM, OdrUse); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11231 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11232 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11233 | /// \brief Perform reference-marking and odr-use handling for a DeclRefExpr. |
| 11234 | void Sema::MarkDeclRefReferenced(DeclRefExpr *E) { |
Nick Lewycky | b7e5eec | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 11235 | // TODO: update this with DR# once a defect report is filed. |
| 11236 | // C++11 defect. The address of a pure member should not be an ODR use, even |
| 11237 | // if it's a qualified reference. |
| 11238 | bool OdrUse = true; |
| 11239 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(E->getDecl())) |
Nick Lewycky | 7cea148 | 2013-02-05 06:20:31 +0000 | [diff] [blame] | 11240 | if (Method->isVirtual()) |
Nick Lewycky | b7e5eec | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 11241 | OdrUse = false; |
| 11242 | MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E, OdrUse); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11243 | } |
| 11244 | |
| 11245 | /// \brief Perform reference-marking and odr-use handling for a MemberExpr. |
| 11246 | void Sema::MarkMemberReferenced(MemberExpr *E) { |
Nick Lewycky | 869709c | 2013-01-31 03:15:20 +0000 | [diff] [blame] | 11247 | // C++11 [basic.def.odr]p2: |
Nick Lewycky | 4ceaf33 | 2013-01-31 01:34:31 +0000 | [diff] [blame] | 11248 | // A non-overloaded function whose name appears as a potentially-evaluated |
| 11249 | // expression or a member of a set of candidate functions, if selected by |
| 11250 | // overload resolution when referred to from a potentially-evaluated |
| 11251 | // expression, is odr-used, unless it is a pure virtual function and its |
| 11252 | // name is not explicitly qualified. |
Nick Lewycky | b7e5eec | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 11253 | bool OdrUse = true; |
Nick Lewycky | 4ceaf33 | 2013-01-31 01:34:31 +0000 | [diff] [blame] | 11254 | if (!E->hasQualifier()) { |
| 11255 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) |
| 11256 | if (Method->isPure()) |
Nick Lewycky | b7e5eec | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 11257 | OdrUse = false; |
Nick Lewycky | 4ceaf33 | 2013-01-31 01:34:31 +0000 | [diff] [blame] | 11258 | } |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 11259 | SourceLocation Loc = E->getMemberLoc().isValid() ? |
| 11260 | E->getMemberLoc() : E->getLocStart(); |
| 11261 | MarkExprReferenced(*this, Loc, E->getMemberDecl(), E, OdrUse); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11262 | } |
| 11263 | |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 11264 | /// \brief Perform marking for a reference to an arbitrary declaration. It |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11265 | /// marks the declaration referenced, and performs odr-use checking for functions |
| 11266 | /// and variables. This method should not be used when building an normal |
| 11267 | /// expression which refers to a variable. |
Nick Lewycky | b7e5eec | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 11268 | void Sema::MarkAnyDeclReferenced(SourceLocation Loc, Decl *D, bool OdrUse) { |
| 11269 | if (OdrUse) { |
| 11270 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 11271 | MarkVariableReferenced(Loc, VD); |
| 11272 | return; |
| 11273 | } |
| 11274 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 11275 | MarkFunctionReferenced(Loc, FD); |
| 11276 | return; |
| 11277 | } |
| 11278 | } |
| 11279 | D->setReferenced(); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 11280 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11281 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11282 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11283 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11284 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11285 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11286 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 11287 | Sema &S; |
| 11288 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11289 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11290 | public: |
| 11291 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11292 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11293 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11294 | |
| 11295 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 11296 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11297 | }; |
| 11298 | } |
| 11299 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11300 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 11301 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11302 | if (Arg.getKind() == TemplateArgument::Declaration) { |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 11303 | if (Decl *D = Arg.getAsDecl()) |
Nick Lewycky | b7e5eec | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 11304 | S.MarkAnyDeclReferenced(Loc, D, true); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11305 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11306 | |
| 11307 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11308 | } |
| 11309 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11310 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11311 | if (ClassTemplateSpecializationDecl *Spec |
| 11312 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 11313 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 11314 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11315 | } |
| 11316 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 11317 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11318 | } |
| 11319 | |
| 11320 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 11321 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11322 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11323 | } |
| 11324 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11325 | namespace { |
| 11326 | /// \brief Helper class that marks all of the declarations referenced by |
| 11327 | /// potentially-evaluated subexpressions as "referenced". |
| 11328 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 11329 | Sema &S; |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11330 | bool SkipLocalVariables; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11331 | |
| 11332 | public: |
| 11333 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 11334 | |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11335 | EvaluatedExprMarker(Sema &S, bool SkipLocalVariables) |
| 11336 | : Inherited(S.Context), S(S), SkipLocalVariables(SkipLocalVariables) { } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11337 | |
| 11338 | void VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11339 | // If we were asked not to visit local variables, don't. |
| 11340 | if (SkipLocalVariables) { |
| 11341 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
| 11342 | if (VD->hasLocalStorage()) |
| 11343 | return; |
| 11344 | } |
| 11345 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11346 | S.MarkDeclRefReferenced(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11347 | } |
| 11348 | |
| 11349 | void VisitMemberExpr(MemberExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11350 | S.MarkMemberReferenced(E); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11351 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11352 | } |
| 11353 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 11354 | void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11355 | S.MarkFunctionReferenced(E->getLocStart(), |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 11356 | const_cast<CXXDestructorDecl*>(E->getTemporary()->getDestructor())); |
| 11357 | Visit(E->getSubExpr()); |
| 11358 | } |
| 11359 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11360 | void VisitCXXNewExpr(CXXNewExpr *E) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11361 | if (E->getOperatorNew()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11362 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorNew()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11363 | if (E->getOperatorDelete()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11364 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11365 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11366 | } |
Sebastian Redl | 2aed8b8 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 11367 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11368 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 11369 | if (E->getOperatorDelete()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11370 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 11371 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 11372 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 11373 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11374 | S.MarkFunctionReferenced(E->getLocStart(), |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 11375 | S.LookupDestructor(Record)); |
| 11376 | } |
| 11377 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11378 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11379 | } |
| 11380 | |
| 11381 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11382 | S.MarkFunctionReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11383 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11384 | } |
| 11385 | |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 11386 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 11387 | Visit(E->getExpr()); |
| 11388 | } |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11389 | |
| 11390 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 11391 | Inherited::VisitImplicitCastExpr(E); |
| 11392 | |
| 11393 | if (E->getCastKind() == CK_LValueToRValue) |
| 11394 | S.UpdateMarkingForLValueToRValue(E->getSubExpr()); |
| 11395 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11396 | }; |
| 11397 | } |
| 11398 | |
| 11399 | /// \brief Mark any declarations that appear within this expression or any |
| 11400 | /// potentially-evaluated subexpressions as "referenced". |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11401 | /// |
| 11402 | /// \param SkipLocalVariables If true, don't mark local variables as |
| 11403 | /// 'referenced'. |
| 11404 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E, |
| 11405 | bool SkipLocalVariables) { |
| 11406 | EvaluatedExprMarker(*this, SkipLocalVariables).Visit(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11407 | } |
| 11408 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11409 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 11410 | /// of the program being compiled. |
| 11411 | /// |
| 11412 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11413 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11414 | /// possibility that the code will actually be executable. Code in sizeof() |
| 11415 | /// expressions, code used only during overload resolution, etc., are not |
| 11416 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 11417 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11418 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11419 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11420 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11421 | /// This routine should be used for all diagnostics that describe the run-time |
| 11422 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 11423 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 11424 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11425 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *Statement, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11426 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11427 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11428 | case Unevaluated: |
| 11429 | // The argument will never be evaluated, so don't complain. |
| 11430 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11431 | |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 11432 | case ConstantEvaluated: |
| 11433 | // Relevant diagnostics should be produced by constant evaluation. |
| 11434 | break; |
| 11435 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11436 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11437 | case PotentiallyEvaluatedIfUsed: |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11438 | if (Statement && getCurFunctionOrMethodDecl()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 11439 | FunctionScopes.back()->PossiblyUnreachableDiags. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11440 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, Statement)); |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 11441 | } |
| 11442 | else |
| 11443 | Diag(Loc, PD); |
| 11444 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11445 | return true; |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11446 | } |
| 11447 | |
| 11448 | return false; |
| 11449 | } |
| 11450 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11451 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 11452 | CallExpr *CE, FunctionDecl *FD) { |
| 11453 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 11454 | return false; |
| 11455 | |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 11456 | // If we're inside a decltype's expression, don't check for a valid return |
| 11457 | // type or construct temporaries until we know whether this is the last call. |
| 11458 | if (ExprEvalContexts.back().IsDecltype) { |
| 11459 | ExprEvalContexts.back().DelayedDecltypeCalls.push_back(CE); |
| 11460 | return false; |
| 11461 | } |
| 11462 | |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 11463 | class CallReturnIncompleteDiagnoser : public TypeDiagnoser { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11464 | FunctionDecl *FD; |
| 11465 | CallExpr *CE; |
| 11466 | |
| 11467 | public: |
| 11468 | CallReturnIncompleteDiagnoser(FunctionDecl *FD, CallExpr *CE) |
| 11469 | : FD(FD), CE(CE) { } |
| 11470 | |
| 11471 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
| 11472 | if (!FD) { |
| 11473 | S.Diag(Loc, diag::err_call_incomplete_return) |
| 11474 | << T << CE->getSourceRange(); |
| 11475 | return; |
| 11476 | } |
| 11477 | |
| 11478 | S.Diag(Loc, diag::err_call_function_incomplete_return) |
| 11479 | << CE->getSourceRange() << FD->getDeclName() << T; |
| 11480 | S.Diag(FD->getLocation(), |
| 11481 | diag::note_function_with_incomplete_return_type_declared_here) |
| 11482 | << FD->getDeclName(); |
| 11483 | } |
| 11484 | } Diagnoser(FD, CE); |
| 11485 | |
| 11486 | if (RequireCompleteType(Loc, ReturnType, Diagnoser)) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11487 | return true; |
| 11488 | |
| 11489 | return false; |
| 11490 | } |
| 11491 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11492 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11493 | // will prevent this condition from triggering, which is what we want. |
| 11494 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 11495 | SourceLocation Loc; |
| 11496 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11497 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11498 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11499 | |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 11500 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11501 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11502 | return; |
| 11503 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11504 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 11505 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11506 | // Greylist some idioms by putting them into a warning subcategory. |
| 11507 | if (ObjCMessageExpr *ME |
| 11508 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 11509 | Selector Sel = ME->getSelector(); |
| 11510 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11511 | // self = [<foo> init...] |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 11512 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11513 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 11514 | |
| 11515 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 11516 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11517 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 11518 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11519 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11520 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 11521 | } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11522 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11523 | return; |
| 11524 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11525 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11526 | Loc = Op->getOperatorLoc(); |
Fariborz Jahanian | a414a2f | 2012-08-29 17:17:11 +0000 | [diff] [blame] | 11527 | } else if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
| 11528 | return DiagnoseAssignmentAsCondition(POE->getSyntacticForm()); |
| 11529 | else { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11530 | // Not an assignment. |
| 11531 | return; |
| 11532 | } |
| 11533 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 11534 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11535 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11536 | SourceLocation Open = E->getLocStart(); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 11537 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 11538 | Diag(Loc, diag::note_condition_assign_silence) |
| 11539 | << FixItHint::CreateInsertion(Open, "(") |
| 11540 | << FixItHint::CreateInsertion(Close, ")"); |
| 11541 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11542 | if (IsOrAssign) |
| 11543 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 11544 | << FixItHint::CreateReplacement(Loc, "!="); |
| 11545 | else |
| 11546 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 11547 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11548 | } |
| 11549 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11550 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 11551 | /// that the user intended an assignment used as condition. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11552 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *ParenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11553 | // Don't warn if the parens came from a macro. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11554 | SourceLocation parenLoc = ParenE->getLocStart(); |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11555 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 11556 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 11557 | // Don't warn for dependent expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11558 | if (ParenE->isTypeDependent()) |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 11559 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11560 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11561 | Expr *E = ParenE->IgnoreParens(); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11562 | |
| 11563 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 11564 | if (opE->getOpcode() == BO_EQ && |
| 11565 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 11566 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11567 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 11568 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 11569 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11570 | SourceRange ParenERange = ParenE->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 11571 | Diag(Loc, diag::note_equality_comparison_silence) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11572 | << FixItHint::CreateRemoval(ParenERange.getBegin()) |
| 11573 | << FixItHint::CreateRemoval(ParenERange.getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 11574 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 11575 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11576 | } |
| 11577 | } |
| 11578 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11579 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11580 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11581 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 11582 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11583 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11584 | ExprResult result = CheckPlaceholderExpr(E); |
| 11585 | if (result.isInvalid()) return ExprError(); |
| 11586 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 11587 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11588 | if (!E->isTypeDependent()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11589 | if (getLangOpts().CPlusPlus) |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 11590 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 11591 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11592 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 11593 | if (ERes.isInvalid()) |
| 11594 | return ExprError(); |
| 11595 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 11596 | |
| 11597 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11598 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 11599 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 11600 | << T << E->getSourceRange(); |
| 11601 | return ExprError(); |
| 11602 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11603 | } |
| 11604 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11605 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11606 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11607 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11608 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11609 | Expr *SubExpr) { |
| 11610 | if (!SubExpr) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11611 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11612 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11613 | return CheckBooleanCondition(SubExpr, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11614 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11615 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11616 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11617 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 11618 | /// to have an appropriate type. |
| 11619 | struct RebuildUnknownAnyFunction |
| 11620 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 11621 | |
| 11622 | Sema &S; |
| 11623 | |
| 11624 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 11625 | |
| 11626 | ExprResult VisitStmt(Stmt *S) { |
| 11627 | llvm_unreachable("unexpected statement!"); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11628 | } |
| 11629 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11630 | ExprResult VisitExpr(Expr *E) { |
| 11631 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 11632 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11633 | return ExprError(); |
| 11634 | } |
| 11635 | |
| 11636 | /// Rebuild an expression which simply semantically wraps another |
| 11637 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11638 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 11639 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11640 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11641 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11642 | Expr *SubExpr = SubResult.take(); |
| 11643 | E->setSubExpr(SubExpr); |
| 11644 | E->setType(SubExpr->getType()); |
| 11645 | E->setValueKind(SubExpr->getValueKind()); |
| 11646 | assert(E->getObjectKind() == OK_Ordinary); |
| 11647 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11648 | } |
| 11649 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11650 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 11651 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11652 | } |
| 11653 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11654 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 11655 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11656 | } |
| 11657 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11658 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 11659 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11660 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11661 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11662 | Expr *SubExpr = SubResult.take(); |
| 11663 | E->setSubExpr(SubExpr); |
| 11664 | E->setType(S.Context.getPointerType(SubExpr->getType())); |
| 11665 | assert(E->getValueKind() == VK_RValue); |
| 11666 | assert(E->getObjectKind() == OK_Ordinary); |
| 11667 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11668 | } |
| 11669 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11670 | ExprResult resolveDecl(Expr *E, ValueDecl *VD) { |
| 11671 | if (!isa<FunctionDecl>(VD)) return VisitExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11672 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11673 | E->setType(VD->getType()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11674 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11675 | assert(E->getValueKind() == VK_RValue); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11676 | if (S.getLangOpts().CPlusPlus && |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11677 | !(isa<CXXMethodDecl>(VD) && |
| 11678 | cast<CXXMethodDecl>(VD)->isInstance())) |
| 11679 | E->setValueKind(VK_LValue); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11680 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11681 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11682 | } |
| 11683 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11684 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 11685 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11686 | } |
| 11687 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11688 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 11689 | return resolveDecl(E, E->getDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11690 | } |
| 11691 | }; |
| 11692 | } |
| 11693 | |
| 11694 | /// Given a function expression of unknown-any type, try to rebuild it |
| 11695 | /// to have a function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11696 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { |
| 11697 | ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); |
| 11698 | if (Result.isInvalid()) return ExprError(); |
| 11699 | return S.DefaultFunctionArrayConversion(Result.take()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11700 | } |
| 11701 | |
| 11702 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11703 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 11704 | /// into one which resolves the type directly on the referring |
| 11705 | /// expression. Strict preservation of the original source |
| 11706 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11707 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11708 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11709 | |
| 11710 | Sema &S; |
| 11711 | |
| 11712 | /// The current destination type. |
| 11713 | QualType DestType; |
| 11714 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11715 | RebuildUnknownAnyExpr(Sema &S, QualType CastType) |
| 11716 | : S(S), DestType(CastType) {} |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11717 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11718 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11719 | llvm_unreachable("unexpected statement!"); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11720 | } |
| 11721 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11722 | ExprResult VisitExpr(Expr *E) { |
| 11723 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 11724 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11725 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11726 | } |
| 11727 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11728 | ExprResult VisitCallExpr(CallExpr *E); |
| 11729 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11730 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11731 | /// Rebuild an expression which simply semantically wraps another |
| 11732 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11733 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 11734 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11735 | if (SubResult.isInvalid()) return ExprError(); |
| 11736 | Expr *SubExpr = SubResult.take(); |
| 11737 | E->setSubExpr(SubExpr); |
| 11738 | E->setType(SubExpr->getType()); |
| 11739 | E->setValueKind(SubExpr->getValueKind()); |
| 11740 | assert(E->getObjectKind() == OK_Ordinary); |
| 11741 | return E; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11742 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11743 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11744 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 11745 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11746 | } |
| 11747 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11748 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 11749 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11750 | } |
| 11751 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11752 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 11753 | const PointerType *Ptr = DestType->getAs<PointerType>(); |
| 11754 | if (!Ptr) { |
| 11755 | S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 11756 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11757 | return ExprError(); |
| 11758 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11759 | assert(E->getValueKind() == VK_RValue); |
| 11760 | assert(E->getObjectKind() == OK_Ordinary); |
| 11761 | E->setType(DestType); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11762 | |
| 11763 | // 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] | 11764 | DestType = Ptr->getPointeeType(); |
| 11765 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11766 | if (SubResult.isInvalid()) return ExprError(); |
| 11767 | E->setSubExpr(SubResult.take()); |
| 11768 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11769 | } |
| 11770 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11771 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11772 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11773 | ExprResult resolveDecl(Expr *E, ValueDecl *VD); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11774 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11775 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 11776 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11777 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11778 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11779 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 11780 | return resolveDecl(E, E->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11781 | } |
| 11782 | }; |
| 11783 | } |
| 11784 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11785 | /// Rebuilds a call expression which yielded __unknown_anytype. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11786 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { |
| 11787 | Expr *CalleeExpr = E->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11788 | |
| 11789 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11790 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11791 | FK_FunctionPointer, |
| 11792 | FK_BlockPointer |
| 11793 | }; |
| 11794 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11795 | FnKind Kind; |
| 11796 | QualType CalleeType = CalleeExpr->getType(); |
| 11797 | if (CalleeType == S.Context.BoundMemberTy) { |
| 11798 | assert(isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E)); |
| 11799 | Kind = FK_MemberFunction; |
| 11800 | CalleeType = Expr::findBoundMemberType(CalleeExpr); |
| 11801 | } else if (const PointerType *Ptr = CalleeType->getAs<PointerType>()) { |
| 11802 | CalleeType = Ptr->getPointeeType(); |
| 11803 | Kind = FK_FunctionPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11804 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11805 | CalleeType = CalleeType->castAs<BlockPointerType>()->getPointeeType(); |
| 11806 | Kind = FK_BlockPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11807 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11808 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11809 | |
| 11810 | // Verify that this is a legal result type of a function. |
| 11811 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 11812 | unsigned diagID = diag::err_func_returning_array_function; |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11813 | if (Kind == FK_BlockPointer) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11814 | diagID = diag::err_block_returning_array_function; |
| 11815 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11816 | S.Diag(E->getExprLoc(), diagID) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11817 | << DestType->isFunctionType() << DestType; |
| 11818 | return ExprError(); |
| 11819 | } |
| 11820 | |
| 11821 | // Otherwise, go ahead and set DestType as the call's result. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11822 | E->setType(DestType.getNonLValueExprType(S.Context)); |
| 11823 | E->setValueKind(Expr::getValueKindForType(DestType)); |
| 11824 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11825 | |
| 11826 | // Rebuild the function type, replacing the result type with DestType. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11827 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType)) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11828 | DestType = S.Context.getFunctionType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11829 | Proto->arg_type_begin(), |
| 11830 | Proto->getNumArgs(), |
| 11831 | Proto->getExtProtoInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11832 | else |
| 11833 | DestType = S.Context.getFunctionNoProtoType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11834 | FnType->getExtInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11835 | |
| 11836 | // Rebuild the appropriate pointer-to-function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11837 | switch (Kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11838 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11839 | // Nothing to do. |
| 11840 | break; |
| 11841 | |
| 11842 | case FK_FunctionPointer: |
| 11843 | DestType = S.Context.getPointerType(DestType); |
| 11844 | break; |
| 11845 | |
| 11846 | case FK_BlockPointer: |
| 11847 | DestType = S.Context.getBlockPointerType(DestType); |
| 11848 | break; |
| 11849 | } |
| 11850 | |
| 11851 | // Finally, we can recurse. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11852 | ExprResult CalleeResult = Visit(CalleeExpr); |
| 11853 | if (!CalleeResult.isUsable()) return ExprError(); |
| 11854 | E->setCallee(CalleeResult.take()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11855 | |
| 11856 | // Bind a temporary if necessary. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11857 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11858 | } |
| 11859 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11860 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11861 | // Verify that this is a legal result type of a call. |
| 11862 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11863 | S.Diag(E->getExprLoc(), diag::err_func_returning_array_function) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11864 | << DestType->isFunctionType() << DestType; |
| 11865 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11866 | } |
| 11867 | |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 11868 | // Rewrite the method result type if available. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11869 | if (ObjCMethodDecl *Method = E->getMethodDecl()) { |
| 11870 | assert(Method->getResultType() == S.Context.UnknownAnyTy); |
| 11871 | Method->setResultType(DestType); |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 11872 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11873 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11874 | // Change the type of the message. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11875 | E->setType(DestType.getNonReferenceType()); |
| 11876 | E->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11877 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11878 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11879 | } |
| 11880 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11881 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11882 | // 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] | 11883 | if (E->getCastKind() == CK_FunctionToPointerDecay) { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11884 | assert(E->getValueKind() == VK_RValue); |
| 11885 | assert(E->getObjectKind() == OK_Ordinary); |
| 11886 | |
| 11887 | E->setType(DestType); |
| 11888 | |
| 11889 | // Rebuild the sub-expression as the pointee (function) type. |
| 11890 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 11891 | |
| 11892 | ExprResult Result = Visit(E->getSubExpr()); |
| 11893 | if (!Result.isUsable()) return ExprError(); |
| 11894 | |
| 11895 | E->setSubExpr(Result.take()); |
| 11896 | return S.Owned(E); |
Sean Callanan | ba66c6c | 2012-03-06 23:12:57 +0000 | [diff] [blame] | 11897 | } else if (E->getCastKind() == CK_LValueToRValue) { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11898 | assert(E->getValueKind() == VK_RValue); |
| 11899 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11900 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11901 | assert(isa<BlockPointerType>(E->getType())); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11902 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11903 | E->setType(DestType); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11904 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11905 | // The sub-expression has to be a lvalue reference, so rebuild it as such. |
| 11906 | DestType = S.Context.getLValueReferenceType(DestType); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11907 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11908 | ExprResult Result = Visit(E->getSubExpr()); |
| 11909 | if (!Result.isUsable()) return ExprError(); |
| 11910 | |
| 11911 | E->setSubExpr(Result.take()); |
| 11912 | return S.Owned(E); |
Sean Callanan | ba66c6c | 2012-03-06 23:12:57 +0000 | [diff] [blame] | 11913 | } else { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11914 | llvm_unreachable("Unhandled cast type!"); |
| 11915 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11916 | } |
| 11917 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11918 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { |
| 11919 | ExprValueKind ValueKind = VK_LValue; |
| 11920 | QualType Type = DestType; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11921 | |
| 11922 | // We know how to make this work for certain kinds of decls: |
| 11923 | |
| 11924 | // - functions |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11925 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(VD)) { |
| 11926 | if (const PointerType *Ptr = Type->getAs<PointerType>()) { |
| 11927 | DestType = Ptr->getPointeeType(); |
| 11928 | ExprResult Result = resolveDecl(E, VD); |
| 11929 | if (Result.isInvalid()) return ExprError(); |
| 11930 | return S.ImpCastExprToType(Result.take(), Type, |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 11931 | CK_FunctionToPointerDecay, VK_RValue); |
| 11932 | } |
| 11933 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11934 | if (!Type->isFunctionType()) { |
| 11935 | S.Diag(E->getExprLoc(), diag::err_unknown_any_function) |
| 11936 | << VD << E->getSourceRange(); |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 11937 | return ExprError(); |
| 11938 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11939 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11940 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 11941 | if (MD->isInstance()) { |
| 11942 | ValueKind = VK_RValue; |
| 11943 | Type = S.Context.BoundMemberTy; |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11944 | } |
| 11945 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11946 | // Function references aren't l-values in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11947 | if (!S.getLangOpts().CPlusPlus) |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11948 | ValueKind = VK_RValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11949 | |
| 11950 | // - variables |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11951 | } else if (isa<VarDecl>(VD)) { |
| 11952 | if (const ReferenceType *RefTy = Type->getAs<ReferenceType>()) { |
| 11953 | Type = RefTy->getPointeeType(); |
| 11954 | } else if (Type->isFunctionType()) { |
| 11955 | S.Diag(E->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 11956 | << VD << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11957 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11958 | } |
| 11959 | |
| 11960 | // - nothing else |
| 11961 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11962 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 11963 | << VD << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11964 | return ExprError(); |
| 11965 | } |
| 11966 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11967 | VD->setType(DestType); |
| 11968 | E->setType(Type); |
| 11969 | E->setValueKind(ValueKind); |
| 11970 | return S.Owned(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11971 | } |
| 11972 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11973 | /// Check a cast of an unknown-any type. We intentionally only |
| 11974 | /// trigger this for C-style casts. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11975 | ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, |
| 11976 | Expr *CastExpr, CastKind &CastKind, |
| 11977 | ExprValueKind &VK, CXXCastPath &Path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11978 | // Rewrite the casted expression from scratch. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11979 | ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11980 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11981 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11982 | CastExpr = result.take(); |
| 11983 | VK = CastExpr->getValueKind(); |
| 11984 | CastKind = CK_NoOp; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11985 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11986 | return CastExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11987 | } |
| 11988 | |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 11989 | ExprResult Sema::forceUnknownAnyToType(Expr *E, QualType ToType) { |
| 11990 | return RebuildUnknownAnyExpr(*this, ToType).Visit(E); |
| 11991 | } |
| 11992 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 11993 | QualType Sema::checkUnknownAnyArg(Expr *&arg) { |
| 11994 | // Filter out placeholders. |
| 11995 | ExprResult argR = CheckPlaceholderExpr(arg); |
| 11996 | if (argR.isInvalid()) return QualType(); |
| 11997 | arg = argR.take(); |
| 11998 | |
| 11999 | // If the argument is an explicit cast, use that exact type as the |
| 12000 | // effective parameter type. |
| 12001 | if (ExplicitCastExpr *castArg = dyn_cast<ExplicitCastExpr>(arg)) { |
| 12002 | return castArg->getTypeAsWritten(); |
| 12003 | } |
| 12004 | |
| 12005 | // Otherwise, try to pass by value. |
| 12006 | return arg->getType().getUnqualifiedType(); |
| 12007 | } |
| 12008 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 12009 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { |
| 12010 | Expr *orig = E; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 12011 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 12012 | while (true) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 12013 | E = E->IgnoreParenImpCasts(); |
| 12014 | if (CallExpr *call = dyn_cast<CallExpr>(E)) { |
| 12015 | E = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 12016 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 12017 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 12018 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 12019 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 12020 | } |
| 12021 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 12022 | SourceLocation loc; |
| 12023 | NamedDecl *d; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 12024 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 12025 | loc = ref->getLocation(); |
| 12026 | d = ref->getDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 12027 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 12028 | loc = mem->getMemberLoc(); |
| 12029 | d = mem->getMemberDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 12030 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 12031 | diagID = diag::err_uncasted_call_of_unknown_any; |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 12032 | loc = msg->getSelectorStartLoc(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 12033 | d = msg->getMethodDecl(); |
John McCall | 819e745 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 12034 | if (!d) { |
| 12035 | S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) |
| 12036 | << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() |
| 12037 | << orig->getSourceRange(); |
| 12038 | return ExprError(); |
| 12039 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 12040 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 12041 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 12042 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 12043 | return ExprError(); |
| 12044 | } |
| 12045 | |
| 12046 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 12047 | |
| 12048 | // Never recoverable. |
| 12049 | return ExprError(); |
| 12050 | } |
| 12051 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 12052 | /// Check for operands with placeholder types and complain if found. |
| 12053 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 12054 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12055 | const BuiltinType *placeholderType = E->getType()->getAsPlaceholderType(); |
| 12056 | if (!placeholderType) return Owned(E); |
| 12057 | |
| 12058 | switch (placeholderType->getKind()) { |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 12059 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 12060 | // Overloaded expressions. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12061 | case BuiltinType::Overload: { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 12062 | // Try to resolve a single function template specialization. |
| 12063 | // This is obligatory. |
| 12064 | ExprResult result = Owned(E); |
| 12065 | if (ResolveAndFixSingleFunctionTemplateSpecialization(result, false)) { |
| 12066 | return result; |
| 12067 | |
| 12068 | // If that failed, try to recover with a call. |
| 12069 | } else { |
| 12070 | tryToRecoverWithCall(result, PDiag(diag::err_ovl_unresolvable), |
| 12071 | /*complain*/ true); |
| 12072 | return result; |
| 12073 | } |
| 12074 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 12075 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12076 | // Bound member functions. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12077 | case BuiltinType::BoundMember: { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 12078 | ExprResult result = Owned(E); |
| 12079 | tryToRecoverWithCall(result, PDiag(diag::err_bound_member_function), |
| 12080 | /*complain*/ true); |
| 12081 | return result; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12082 | } |
| 12083 | |
| 12084 | // ARC unbridged casts. |
| 12085 | case BuiltinType::ARCUnbridgedCast: { |
| 12086 | Expr *realCast = stripARCUnbridgedCast(E); |
| 12087 | diagnoseARCUnbridgedCast(realCast); |
| 12088 | return Owned(realCast); |
| 12089 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12090 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 12091 | // Expressions of unknown type. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12092 | case BuiltinType::UnknownAny: |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 12093 | return diagnoseUnknownAnyExpr(*this, E); |
| 12094 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 12095 | // Pseudo-objects. |
| 12096 | case BuiltinType::PseudoObject: |
| 12097 | return checkPseudoObjectRValue(E); |
| 12098 | |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 12099 | case BuiltinType::BuiltinFn: |
| 12100 | Diag(E->getLocStart(), diag::err_builtin_fn_use); |
| 12101 | return ExprError(); |
| 12102 | |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 12103 | // Everything else should be impossible. |
| 12104 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 12105 | case BuiltinType::Id: |
| 12106 | #define PLACEHOLDER_TYPE(Id, SingletonId) |
| 12107 | #include "clang/AST/BuiltinTypes.def" |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12108 | break; |
| 12109 | } |
| 12110 | |
| 12111 | llvm_unreachable("invalid placeholder type!"); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 12112 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 12113 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 12114 | bool Sema::CheckCaseExpression(Expr *E) { |
| 12115 | if (E->isTypeDependent()) |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 12116 | return true; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 12117 | if (E->isValueDependent() || E->isIntegerConstantExpr(Context)) |
| 12118 | return E->getType()->isIntegralOrEnumerationType(); |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 12119 | return false; |
| 12120 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 12121 | |
| 12122 | /// ActOnObjCBoolLiteral - Parse {__objc_yes,__objc_no} literals. |
| 12123 | ExprResult |
| 12124 | Sema::ActOnObjCBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
| 12125 | assert((Kind == tok::kw___objc_yes || Kind == tok::kw___objc_no) && |
| 12126 | "Unknown Objective-C Boolean value!"); |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 12127 | QualType BoolT = Context.ObjCBuiltinBoolTy; |
| 12128 | if (!Context.getBOOLDecl()) { |
Fariborz Jahanian | 1c9a2da | 2012-10-16 17:08:11 +0000 | [diff] [blame] | 12129 | LookupResult Result(*this, &Context.Idents.get("BOOL"), OpLoc, |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 12130 | Sema::LookupOrdinaryName); |
Fariborz Jahanian | 9f5933a | 2012-10-16 16:21:20 +0000 | [diff] [blame] | 12131 | if (LookupName(Result, getCurScope()) && Result.isSingleResult()) { |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 12132 | NamedDecl *ND = Result.getFoundDecl(); |
| 12133 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 12134 | Context.setBOOLDecl(TD); |
| 12135 | } |
| 12136 | } |
| 12137 | if (Context.getBOOLDecl()) |
| 12138 | BoolT = Context.getBOOLType(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 12139 | return Owned(new (Context) ObjCBoolLiteralExpr(Kind == tok::kw___objc_yes, |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 12140 | BoolT, OpLoc)); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 12141 | } |