Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/DelayedDiagnostic.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Initialization.h" |
| 17 | #include "clang/Sema/Lookup.h" |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 19 | #include "clang/Sema/AnalysisBasedWarnings.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTConsumer.h" |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 23 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 26 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 27 | #include "clang/AST/Expr.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 28 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 29 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 30 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 31 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 32 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 35 | #include "clang/Lex/LiteralSupport.h" |
| 36 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 37 | #include "clang/Sema/DeclSpec.h" |
| 38 | #include "clang/Sema/Designator.h" |
| 39 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 40 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 41 | #include "clang/Sema/ParsedTemplate.h" |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 42 | #include "clang/Sema/SemaFixItUtils.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 43 | #include "clang/Sema/Template.h" |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 44 | #include "TreeTransform.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 46 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 48 | /// \brief Determine whether the use of this declaration is valid, without |
| 49 | /// emitting diagnostics. |
| 50 | bool Sema::CanUseDecl(NamedDecl *D) { |
| 51 | // See if this is an auto-typed variable whose initializer we are parsing. |
| 52 | if (ParsingInitForAutoVars.count(D)) |
| 53 | return false; |
| 54 | |
| 55 | // See if this is a deleted function. |
| 56 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 57 | if (FD->isDeleted()) |
| 58 | return false; |
| 59 | } |
Sebastian Redl | 28bdb14 | 2011-10-16 18:19:16 +0000 | [diff] [blame] | 60 | |
| 61 | // See if this function is unavailable. |
| 62 | if (D->getAvailability() == AR_Unavailable && |
| 63 | cast<Decl>(CurContext)->getAvailability() != AR_Unavailable) |
| 64 | return false; |
| 65 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 66 | return true; |
| 67 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 68 | |
Fariborz Jahanian | 2d40d9e | 2012-09-06 16:43:18 +0000 | [diff] [blame] | 69 | static void DiagnoseUnusedOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc) { |
| 70 | // Warn if this is used but marked unused. |
| 71 | if (D->hasAttr<UnusedAttr>()) { |
Fariborz Jahanian | 3359fa3 | 2012-09-06 18:38:58 +0000 | [diff] [blame] | 72 | const Decl *DC = cast<Decl>(S.getCurObjCLexicalContext()); |
Fariborz Jahanian | 2d40d9e | 2012-09-06 16:43:18 +0000 | [diff] [blame] | 73 | if (!DC->hasAttr<UnusedAttr>()) |
| 74 | S.Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 75 | } |
| 76 | } |
| 77 | |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 78 | static AvailabilityResult DiagnoseAvailabilityOfDecl(Sema &S, |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 79 | NamedDecl *D, SourceLocation Loc, |
| 80 | const ObjCInterfaceDecl *UnknownObjCClass) { |
| 81 | // See if this declaration is unavailable or deprecated. |
| 82 | std::string Message; |
| 83 | AvailabilityResult Result = D->getAvailability(&Message); |
Fariborz Jahanian | 39b4fc8 | 2011-11-28 19:45:58 +0000 | [diff] [blame] | 84 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 85 | if (Result == AR_Available) { |
| 86 | const DeclContext *DC = ECD->getDeclContext(); |
| 87 | if (const EnumDecl *TheEnumDecl = dyn_cast<EnumDecl>(DC)) |
| 88 | Result = TheEnumDecl->getAvailability(&Message); |
| 89 | } |
Jordan Rose | 04bec39 | 2012-10-10 16:42:54 +0000 | [diff] [blame] | 90 | |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 91 | const ObjCPropertyDecl *ObjCPDecl = 0; |
Jordan Rose | 04bec39 | 2012-10-10 16:42:54 +0000 | [diff] [blame] | 92 | if (Result == AR_Deprecated || Result == AR_Unavailable) { |
| 93 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 94 | if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) { |
| 95 | AvailabilityResult PDeclResult = PD->getAvailability(0); |
| 96 | if (PDeclResult == Result) |
| 97 | ObjCPDecl = PD; |
| 98 | } |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 99 | } |
Jordan Rose | 04bec39 | 2012-10-10 16:42:54 +0000 | [diff] [blame] | 100 | } |
Fariborz Jahanian | 39b4fc8 | 2011-11-28 19:45:58 +0000 | [diff] [blame] | 101 | |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 102 | switch (Result) { |
| 103 | case AR_Available: |
| 104 | case AR_NotYetIntroduced: |
| 105 | break; |
| 106 | |
| 107 | case AR_Deprecated: |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 108 | S.EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass, ObjCPDecl); |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 109 | break; |
| 110 | |
| 111 | case AR_Unavailable: |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 112 | if (S.getCurContextAvailability() != AR_Unavailable) { |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 113 | if (Message.empty()) { |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 114 | if (!UnknownObjCClass) { |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 115 | S.Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 116 | if (ObjCPDecl) |
| 117 | S.Diag(ObjCPDecl->getLocation(), diag::note_property_attribute) |
| 118 | << ObjCPDecl->getDeclName() << 1; |
| 119 | } |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 120 | else |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 121 | S.Diag(Loc, diag::warn_unavailable_fwdclass_message) |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 122 | << D->getDeclName(); |
| 123 | } |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 124 | else |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 125 | S.Diag(Loc, diag::err_unavailable_message) |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 126 | << D->getDeclName() << Message; |
Fariborz Jahanian | fd09088 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 127 | S.Diag(D->getLocation(), diag::note_unavailable_here) |
| 128 | << isa<FunctionDecl>(D) << false; |
| 129 | if (ObjCPDecl) |
| 130 | S.Diag(ObjCPDecl->getLocation(), diag::note_property_attribute) |
| 131 | << ObjCPDecl->getDeclName() << 1; |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 132 | } |
| 133 | break; |
| 134 | } |
| 135 | return Result; |
| 136 | } |
| 137 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 138 | /// \brief Emit a note explaining that this function is deleted or unavailable. |
| 139 | void Sema::NoteDeletedFunction(FunctionDecl *Decl) { |
| 140 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Decl); |
| 141 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 142 | if (Method && Method->isDeleted() && !Method->isDeletedAsWritten()) { |
| 143 | // If the method was explicitly defaulted, point at that declaration. |
| 144 | if (!Method->isImplicit()) |
| 145 | Diag(Decl->getLocation(), diag::note_implicitly_deleted); |
| 146 | |
| 147 | // Try to diagnose why this special member function was implicitly |
| 148 | // deleted. This might fail, if that reason no longer applies. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 149 | CXXSpecialMember CSM = getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 150 | if (CSM != CXXInvalid) |
| 151 | ShouldDeleteSpecialMember(Method, CSM, /*Diagnose=*/true); |
| 152 | |
| 153 | return; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | Diag(Decl->getLocation(), diag::note_unavailable_here) |
| 157 | << 1 << Decl->isDeleted(); |
| 158 | } |
| 159 | |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 160 | /// \brief Determine whether a FunctionDecl was ever declared with an |
| 161 | /// explicit storage class. |
| 162 | static bool hasAnyExplicitStorageClass(const FunctionDecl *D) { |
| 163 | for (FunctionDecl::redecl_iterator I = D->redecls_begin(), |
| 164 | E = D->redecls_end(); |
| 165 | I != E; ++I) { |
| 166 | if (I->getStorageClassAsWritten() != SC_None) |
| 167 | return true; |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | /// \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] | 173 | /// variable or function with internal linkage (C11 6.7.4p3). |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 174 | /// |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 175 | /// 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] | 176 | /// in many cases it will not behave correctly. This is not enabled in C++ mode |
| 177 | /// because the restriction language is a bit weaker (C++11 [basic.def.odr]p6) |
| 178 | /// and so while there may still be user mistakes, most of the time we can't |
| 179 | /// prove that there are errors. |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 180 | static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S, |
| 181 | const NamedDecl *D, |
| 182 | SourceLocation Loc) { |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 183 | // This is disabled under C++; there are too many ways for this to fire in |
| 184 | // contexts where the warning is a false positive, or where it is technically |
| 185 | // correct but benign. |
| 186 | if (S.getLangOpts().CPlusPlus) |
| 187 | return; |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 188 | |
| 189 | // Check if this is an inlined function or method. |
| 190 | FunctionDecl *Current = S.getCurFunctionDecl(); |
| 191 | if (!Current) |
| 192 | return; |
| 193 | if (!Current->isInlined()) |
| 194 | return; |
| 195 | if (Current->getLinkage() != ExternalLinkage) |
| 196 | return; |
| 197 | |
| 198 | // Check if the decl has internal linkage. |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 199 | if (D->getLinkage() != InternalLinkage) |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 200 | return; |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 201 | |
Jordan Rose | 0523327 | 2012-06-21 05:54:50 +0000 | [diff] [blame] | 202 | // Downgrade from ExtWarn to Extension if |
| 203 | // (1) the supposedly external inline function is in the main file, |
| 204 | // and probably won't be included anywhere else. |
| 205 | // (2) the thing we're referencing is a pure function. |
| 206 | // (3) the thing we're referencing is another inline function. |
| 207 | // This last can give us false negatives, but it's better than warning on |
| 208 | // wrappers for simple C library functions. |
| 209 | const FunctionDecl *UsedFn = dyn_cast<FunctionDecl>(D); |
| 210 | bool DowngradeWarning = S.getSourceManager().isFromMainFile(Loc); |
| 211 | if (!DowngradeWarning && UsedFn) |
| 212 | DowngradeWarning = UsedFn->isInlined() || UsedFn->hasAttr<ConstAttr>(); |
| 213 | |
| 214 | S.Diag(Loc, DowngradeWarning ? diag::ext_internal_in_extern_inline |
| 215 | : diag::warn_internal_in_extern_inline) |
| 216 | << /*IsVar=*/!UsedFn << D; |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 217 | |
| 218 | // Suggest "static" on the inline function, if possible. |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 219 | if (!hasAnyExplicitStorageClass(Current)) { |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 220 | const FunctionDecl *FirstDecl = Current->getCanonicalDecl(); |
| 221 | SourceLocation DeclBegin = FirstDecl->getSourceRange().getBegin(); |
| 222 | S.Diag(DeclBegin, diag::note_convert_inline_to_static) |
| 223 | << Current << FixItHint::CreateInsertion(DeclBegin, "static "); |
| 224 | } |
| 225 | |
| 226 | S.Diag(D->getCanonicalDecl()->getLocation(), |
| 227 | diag::note_internal_decl_declared_here) |
| 228 | << D; |
| 229 | } |
| 230 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 231 | /// \brief Determine whether the use of this declaration is valid, and |
| 232 | /// emit any corresponding diagnostics. |
| 233 | /// |
| 234 | /// This routine diagnoses various problems with referencing |
| 235 | /// declarations that can occur when using a declaration. For example, |
| 236 | /// it might warn if a deprecated or unavailable declaration is being |
| 237 | /// used, or produce an error (and return true) if a C++0x deleted |
| 238 | /// function is being used. |
| 239 | /// |
| 240 | /// \returns true if there was an error (this declaration cannot be |
| 241 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 242 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 243 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 244 | const ObjCInterfaceDecl *UnknownObjCClass) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 245 | if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)) { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 246 | // If there were any diagnostics suppressed by template argument deduction, |
| 247 | // emit them now. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 248 | llvm::DenseMap<Decl *, SmallVector<PartialDiagnosticAt, 1> >::iterator |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 249 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 250 | if (Pos != SuppressedDiagnostics.end()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 251 | SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 252 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 253 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 254 | |
| 255 | // 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] | 256 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 257 | // entry from the table, because we want to avoid ever emitting these |
| 258 | // diagnostics again. |
| 259 | Suppressed.clear(); |
| 260 | } |
| 261 | } |
| 262 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 263 | // 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] | 264 | if (ParsingInitForAutoVars.count(D)) { |
| 265 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 266 | << D->getDeclName(); |
| 267 | return true; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 270 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 271 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 272 | if (FD->isDeleted()) { |
| 273 | Diag(Loc, diag::err_deleted_function_use); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 274 | NoteDeletedFunction(FD); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 275 | return true; |
| 276 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 277 | } |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 278 | DiagnoseAvailabilityOfDecl(*this, D, Loc, UnknownObjCClass); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 279 | |
Fariborz Jahanian | 2d40d9e | 2012-09-06 16:43:18 +0000 | [diff] [blame] | 280 | DiagnoseUnusedOfDecl(*this, D, Loc); |
Jordan Rose | 106af9e | 2012-06-15 18:19:48 +0000 | [diff] [blame] | 281 | |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 282 | diagnoseUseOfInternalDeclInInlineFunction(*this, D, Loc); |
Jordan Rose | 106af9e | 2012-06-15 18:19:48 +0000 | [diff] [blame] | 283 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 284 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 287 | /// \brief Retrieve the message suffix that should be added to a |
| 288 | /// diagnostic complaining about the given function being deleted or |
| 289 | /// unavailable. |
| 290 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 291 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 292 | // detected here so that we could improve diagnostics to say, e.g., |
| 293 | // "base class 'A' had a deleted copy constructor". |
| 294 | if (FD->isDeleted()) |
| 295 | return std::string(); |
| 296 | |
| 297 | std::string Message; |
| 298 | if (FD->getAvailability(&Message)) |
| 299 | return ": " + Message; |
| 300 | |
| 301 | return std::string(); |
| 302 | } |
| 303 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 304 | /// DiagnoseSentinelCalls - This routine checks whether a call or |
| 305 | /// message-send is to a declaration with the sentinel attribute, and |
| 306 | /// if so, it checks that the requirements of the sentinel are |
| 307 | /// satisfied. |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 308 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 309 | Expr **args, unsigned numArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 310 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 312 | return; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 313 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 314 | // The number of formal parameters of the declaration. |
| 315 | unsigned numFormalParams; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 317 | // The kind of declaration. This is also an index into a %select in |
| 318 | // the diagnostic. |
| 319 | enum CalleeType { CT_Function, CT_Method, CT_Block } calleeType; |
| 320 | |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 321 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 322 | numFormalParams = MD->param_size(); |
| 323 | calleeType = CT_Method; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 324 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 325 | numFormalParams = FD->param_size(); |
| 326 | calleeType = CT_Function; |
| 327 | } else if (isa<VarDecl>(D)) { |
| 328 | QualType type = cast<ValueDecl>(D)->getType(); |
| 329 | const FunctionType *fn = 0; |
| 330 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 331 | fn = ptr->getPointeeType()->getAs<FunctionType>(); |
| 332 | if (!fn) return; |
| 333 | calleeType = CT_Function; |
| 334 | } else if (const BlockPointerType *ptr = type->getAs<BlockPointerType>()) { |
| 335 | fn = ptr->getPointeeType()->castAs<FunctionType>(); |
| 336 | calleeType = CT_Block; |
| 337 | } else { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 338 | return; |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 339 | } |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 340 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 341 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fn)) { |
| 342 | numFormalParams = proto->getNumArgs(); |
| 343 | } else { |
| 344 | numFormalParams = 0; |
| 345 | } |
| 346 | } else { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 347 | return; |
| 348 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 349 | |
| 350 | // "nullPos" is the number of formal parameters at the end which |
| 351 | // effectively count as part of the variadic arguments. This is |
| 352 | // useful if you would prefer to not have *any* formal parameters, |
| 353 | // but the language forces you to have at least one. |
| 354 | unsigned nullPos = attr->getNullPos(); |
| 355 | assert((nullPos == 0 || nullPos == 1) && "invalid null position on sentinel"); |
| 356 | numFormalParams = (nullPos > numFormalParams ? 0 : numFormalParams - nullPos); |
| 357 | |
| 358 | // The number of arguments which should follow the sentinel. |
| 359 | unsigned numArgsAfterSentinel = attr->getSentinel(); |
| 360 | |
| 361 | // If there aren't enough arguments for all the formal parameters, |
| 362 | // the sentinel, and the args after the sentinel, complain. |
| 363 | if (numArgs < numFormalParams + numArgsAfterSentinel + 1) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 364 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 365 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 366 | return; |
| 367 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 368 | |
| 369 | // Otherwise, find the sentinel expression. |
| 370 | Expr *sentinelExpr = args[numArgs - numArgsAfterSentinel - 1]; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 371 | if (!sentinelExpr) return; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 372 | if (sentinelExpr->isValueDependent()) return; |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 373 | if (Context.isSentinelNullExpr(sentinelExpr)) return; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 374 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 375 | // Pick a reasonable string to insert. Optimistically use 'nil' or |
| 376 | // 'NULL' if those are actually defined in the context. Only use |
| 377 | // 'nil' for ObjC methods, where it's much more likely that the |
| 378 | // variadic arguments form a list of object pointers. |
| 379 | SourceLocation MissingNilLoc |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 380 | = PP.getLocForEndOfToken(sentinelExpr->getLocEnd()); |
| 381 | std::string NullValue; |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 382 | if (calleeType == CT_Method && |
| 383 | PP.getIdentifierInfo("nil")->hasMacroDefinition()) |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 384 | NullValue = "nil"; |
| 385 | else if (PP.getIdentifierInfo("NULL")->hasMacroDefinition()) |
| 386 | NullValue = "NULL"; |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 387 | else |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 388 | NullValue = "(void*) 0"; |
Eli Friedman | 39834ba | 2011-09-27 23:46:37 +0000 | [diff] [blame] | 389 | |
| 390 | if (MissingNilLoc.isInvalid()) |
| 391 | Diag(Loc, diag::warn_missing_sentinel) << calleeType; |
| 392 | else |
| 393 | Diag(MissingNilLoc, diag::warn_missing_sentinel) |
| 394 | << calleeType |
| 395 | << FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue); |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 396 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 399 | SourceRange Sema::getExprRange(Expr *E) const { |
| 400 | return E ? E->getSourceRange() : SourceRange(); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 403 | //===----------------------------------------------------------------------===// |
| 404 | // Standard Promotions and Conversions |
| 405 | //===----------------------------------------------------------------------===// |
| 406 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 407 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 408 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 409 | // Handle any placeholder expressions which made it here. |
| 410 | if (E->getType()->isPlaceholderType()) { |
| 411 | ExprResult result = CheckPlaceholderExpr(E); |
| 412 | if (result.isInvalid()) return ExprError(); |
| 413 | E = result.take(); |
| 414 | } |
| 415 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 416 | QualType Ty = E->getType(); |
| 417 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 418 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 419 | if (Ty->isFunctionType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 420 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 421 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 422 | else if (Ty->isArrayType()) { |
| 423 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 424 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 425 | // type 'array of type' is converted to an expression that has type 'pointer |
| 426 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 427 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 428 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 429 | // |
| 430 | // C++ 4.2p1: |
| 431 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 432 | // T" can be converted to an rvalue of type "pointer to T". |
| 433 | // |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 434 | if (getLangOpts().C99 || getLangOpts().CPlusPlus || E->isLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 435 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 436 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 437 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 438 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 441 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
| 442 | // Check to see if we are dereferencing a null pointer. If so, |
| 443 | // and if not volatile-qualified, this is undefined behavior that the |
| 444 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 445 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 446 | // only handles the pattern "*null", which is a very syntactic check. |
| 447 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 448 | if (UO->getOpcode() == UO_Deref && |
| 449 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 450 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && |
| 451 | !UO->getType().isVolatileQualified()) { |
| 452 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 453 | S.PDiag(diag::warn_indirection_through_null) |
| 454 | << UO->getSubExpr()->getSourceRange()); |
| 455 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 456 | S.PDiag(diag::note_indirection_through_null)); |
| 457 | } |
| 458 | } |
| 459 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 460 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 461 | // Handle any placeholder expressions which made it here. |
| 462 | if (E->getType()->isPlaceholderType()) { |
| 463 | ExprResult result = CheckPlaceholderExpr(E); |
| 464 | if (result.isInvalid()) return ExprError(); |
| 465 | E = result.take(); |
| 466 | } |
| 467 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 468 | // C++ [conv.lval]p1: |
| 469 | // A glvalue of a non-function, non-array type T can be |
| 470 | // converted to a prvalue. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 471 | if (!E->isGLValue()) return Owned(E); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 472 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 473 | QualType T = E->getType(); |
| 474 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 475 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 476 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 477 | // expressions of certain types in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 478 | if (getLangOpts().CPlusPlus && |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 479 | (E->getType() == Context.OverloadTy || |
| 480 | T->isDependentType() || |
| 481 | T->isRecordType())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 482 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 483 | |
| 484 | // The C standard is actually really unclear on this point, and |
| 485 | // DR106 tells us what the result should be but not why. It's |
| 486 | // generally best to say that void types just doesn't undergo |
| 487 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 488 | // 'void' type are never l-values, but qualified void can be. |
| 489 | if (T->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 490 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 491 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 492 | CheckForNullPointerDereference(*this, E); |
| 493 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 494 | // C++ [conv.lval]p1: |
| 495 | // [...] If T is a non-class type, the type of the prvalue is the |
| 496 | // cv-unqualified version of T. Otherwise, the type of the |
| 497 | // rvalue is T. |
| 498 | // |
| 499 | // C99 6.3.2.1p2: |
| 500 | // If the lvalue has qualified type, the value has the unqualified |
| 501 | // version of the type of the lvalue; otherwise, the value has the |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 502 | // type of the lvalue. |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 503 | if (T.hasQualifiers()) |
| 504 | T = T.getUnqualifiedType(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 505 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 506 | UpdateMarkingForLValueToRValue(E); |
| 507 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 508 | ExprResult Res = Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 509 | E, 0, VK_RValue)); |
| 510 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 511 | // C11 6.3.2.1p2: |
| 512 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 513 | // of the type of the lvalue ... |
| 514 | if (const AtomicType *Atomic = T->getAs<AtomicType>()) { |
| 515 | T = Atomic->getValueType().getUnqualifiedType(); |
| 516 | Res = Owned(ImplicitCastExpr::Create(Context, T, CK_AtomicToNonAtomic, |
| 517 | Res.get(), 0, VK_RValue)); |
| 518 | } |
| 519 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 520 | return Res; |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 521 | } |
| 522 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 523 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 524 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 525 | if (Res.isInvalid()) |
| 526 | return ExprError(); |
| 527 | Res = DefaultLvalueConversion(Res.take()); |
| 528 | if (Res.isInvalid()) |
| 529 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 530 | return Res; |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 534 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 536 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 537 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 538 | /// In these instances, this routine should *not* be called. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 539 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 540 | // First, convert to an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 541 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 542 | if (Res.isInvalid()) |
| 543 | return Owned(E); |
| 544 | E = Res.take(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 545 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 546 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 547 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 548 | |
| 549 | // Half FP is a bit different: it's a storage-only type, meaning that any |
| 550 | // "use" of it should be promoted to float. |
| 551 | if (Ty->isHalfType()) |
| 552 | return ImpCastExprToType(Res.take(), Context.FloatTy, CK_FloatingCast); |
| 553 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 554 | // Try to perform integral promotions if the object has a theoretically |
| 555 | // promotable type. |
| 556 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 557 | // C99 6.3.1.1p2: |
| 558 | // |
| 559 | // The following may be used in an expression wherever an int or |
| 560 | // unsigned int may be used: |
| 561 | // - an object or expression with an integer type whose integer |
| 562 | // conversion rank is less than or equal to the rank of int |
| 563 | // and unsigned int. |
| 564 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 565 | // |
| 566 | // If an int can represent all values of the original type, the |
| 567 | // value is converted to an int; otherwise, it is converted to an |
| 568 | // unsigned int. These are called the integer promotions. All |
| 569 | // other types are unchanged by the integer promotions. |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 570 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 571 | QualType PTy = Context.isPromotableBitField(E); |
| 572 | if (!PTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 573 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 574 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 575 | } |
| 576 | if (Ty->isPromotableIntegerType()) { |
| 577 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 578 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 579 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 580 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 581 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 582 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 585 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | /// do not have a prototype. Arguments that have type float are promoted to |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 587 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 588 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 589 | QualType Ty = E->getType(); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 590 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 592 | ExprResult Res = UsualUnaryConversions(E); |
| 593 | if (Res.isInvalid()) |
| 594 | return Owned(E); |
| 595 | E = Res.take(); |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 597 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 598 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 599 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 600 | |
John McCall | 96a914a | 2011-08-27 22:06:17 +0000 | [diff] [blame] | 601 | // C++ performs lvalue-to-rvalue conversion as a default argument |
John McCall | 709bca8 | 2011-08-29 23:55:37 +0000 | [diff] [blame] | 602 | // promotion, even on class types, but note: |
| 603 | // C++11 [conv.lval]p2: |
| 604 | // When an lvalue-to-rvalue conversion occurs in an unevaluated |
| 605 | // operand or a subexpression thereof the value contained in the |
| 606 | // referenced object is not accessed. Otherwise, if the glvalue |
| 607 | // has a class type, the conversion copy-initializes a temporary |
| 608 | // of type T from the glvalue and the result of the conversion |
| 609 | // is a prvalue for the temporary. |
Eli Friedman | 55693fb | 2012-01-17 02:13:45 +0000 | [diff] [blame] | 610 | // FIXME: add some way to gate this entire thing for correctness in |
| 611 | // potentially potentially evaluated contexts. |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 612 | if (getLangOpts().CPlusPlus && E->isGLValue() && !isUnevaluatedContext()) { |
Eli Friedman | 55693fb | 2012-01-17 02:13:45 +0000 | [diff] [blame] | 613 | ExprResult Temp = PerformCopyInitialization( |
| 614 | InitializedEntity::InitializeTemporary(E->getType()), |
| 615 | E->getExprLoc(), |
| 616 | Owned(E)); |
| 617 | if (Temp.isInvalid()) |
| 618 | return ExprError(); |
| 619 | E = Temp.get(); |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 620 | } |
| 621 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 622 | return Owned(E); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 625 | /// Determine the degree of POD-ness for an expression. |
| 626 | /// Incomplete types are considered POD, since this check can be performed |
| 627 | /// when we're in an unevaluated context. |
| 628 | Sema::VarArgKind Sema::isValidVarArgType(const QualType &Ty) { |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 629 | if (Ty->isIncompleteType()) { |
| 630 | if (Ty->isObjCObjectType()) |
| 631 | return VAK_Invalid; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 632 | return VAK_Valid; |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | if (Ty.isCXX98PODType(Context)) |
| 636 | return VAK_Valid; |
| 637 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 638 | // C++0x [expr.call]p7: |
| 639 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 640 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 641 | // or a non-trivial destructor, with no corresponding parameter, |
| 642 | // is conditionally-supported with implementation-defined semantics. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 643 | if (getLangOpts().CPlusPlus0x && !Ty->isDependentType()) |
| 644 | if (CXXRecordDecl *Record = Ty->getAsCXXRecordDecl()) |
| 645 | if (Record->hasTrivialCopyConstructor() && |
| 646 | Record->hasTrivialMoveConstructor() && |
| 647 | Record->hasTrivialDestructor()) |
| 648 | return VAK_ValidInCXX11; |
| 649 | |
| 650 | if (getLangOpts().ObjCAutoRefCount && Ty->isObjCLifetimeType()) |
| 651 | return VAK_Valid; |
| 652 | return VAK_Invalid; |
| 653 | } |
| 654 | |
| 655 | bool Sema::variadicArgumentPODCheck(const Expr *E, VariadicCallType CT) { |
| 656 | // Don't allow one to pass an Objective-C interface to a vararg. |
| 657 | const QualType & Ty = E->getType(); |
| 658 | |
| 659 | // Complain about passing non-POD types through varargs. |
| 660 | switch (isValidVarArgType(Ty)) { |
| 661 | case VAK_Valid: |
| 662 | break; |
| 663 | case VAK_ValidInCXX11: |
| 664 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 665 | PDiag(diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg) |
| 666 | << E->getType() << CT); |
| 667 | break; |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 668 | case VAK_Invalid: { |
| 669 | if (Ty->isObjCObjectType()) |
| 670 | return DiagRuntimeBehavior(E->getLocStart(), 0, |
| 671 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 672 | << Ty << CT); |
| 673 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 674 | return DiagRuntimeBehavior(E->getLocStart(), 0, |
| 675 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 676 | << getLangOpts().CPlusPlus0x << Ty << CT); |
| 677 | } |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 678 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 679 | // c++ rules are enforced elsewhere. |
| 680 | return false; |
| 681 | } |
| 682 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 683 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 684 | /// 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] | 685 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 686 | FunctionDecl *FDecl) { |
Richard Smith | e1971a1 | 2012-06-27 20:29:39 +0000 | [diff] [blame] | 687 | if (const BuiltinType *PlaceholderTy = E->getType()->getAsPlaceholderType()) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 688 | // Strip the unbridged-cast placeholder expression off, if applicable. |
| 689 | if (PlaceholderTy->getKind() == BuiltinType::ARCUnbridgedCast && |
| 690 | (CT == VariadicMethod || |
| 691 | (FDecl && FDecl->hasAttr<CFAuditedTransferAttr>()))) { |
| 692 | E = stripARCUnbridgedCast(E); |
| 693 | |
| 694 | // Otherwise, do normal placeholder checking. |
| 695 | } else { |
| 696 | ExprResult ExprRes = CheckPlaceholderExpr(E); |
| 697 | if (ExprRes.isInvalid()) |
| 698 | return ExprError(); |
| 699 | E = ExprRes.take(); |
| 700 | } |
| 701 | } |
Douglas Gregor | 8d5e18c | 2011-06-17 00:15:10 +0000 | [diff] [blame] | 702 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 703 | ExprResult ExprRes = DefaultArgumentPromotion(E); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 704 | if (ExprRes.isInvalid()) |
| 705 | return ExprError(); |
| 706 | E = ExprRes.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 707 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 708 | // Diagnostics regarding non-POD argument types are |
| 709 | // emitted along with format string checking in Sema::CheckFunctionCall(). |
Richard Smith | 83ea530 | 2012-06-27 20:23:58 +0000 | [diff] [blame] | 710 | if (isValidVarArgType(E->getType()) == VAK_Invalid) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 711 | // Turn this into a trap. |
| 712 | CXXScopeSpec SS; |
| 713 | SourceLocation TemplateKWLoc; |
| 714 | UnqualifiedId Name; |
| 715 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 716 | E->getLocStart()); |
| 717 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, TemplateKWLoc, |
| 718 | Name, true, false); |
| 719 | if (TrapFn.isInvalid()) |
| 720 | return ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 721 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 722 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), |
| 723 | E->getLocStart(), MultiExprArg(), |
| 724 | E->getLocEnd()); |
| 725 | if (Call.isInvalid()) |
| 726 | return ExprError(); |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 727 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 728 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 729 | Call.get(), E); |
| 730 | if (Comma.isInvalid()) |
| 731 | return ExprError(); |
| 732 | return Comma.get(); |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 733 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 734 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 735 | if (!getLangOpts().CPlusPlus && |
Fariborz Jahanian | e853bb3 | 2012-03-01 23:42:00 +0000 | [diff] [blame] | 736 | RequireCompleteType(E->getExprLoc(), E->getType(), |
Fariborz Jahanian | a0e005b | 2012-03-02 17:05:03 +0000 | [diff] [blame] | 737 | diag::err_call_incomplete_argument)) |
Fariborz Jahanian | e853bb3 | 2012-03-01 23:42:00 +0000 | [diff] [blame] | 738 | return ExprError(); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 739 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 740 | return Owned(E); |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 743 | /// \brief Converts an integer to complex float type. Helper function of |
| 744 | /// UsualArithmeticConversions() |
| 745 | /// |
| 746 | /// \return false if the integer expression is an integer type and is |
| 747 | /// successfully converted to the complex type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 748 | static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr, |
| 749 | ExprResult &ComplexExpr, |
| 750 | QualType IntTy, |
| 751 | QualType ComplexTy, |
| 752 | bool SkipCast) { |
| 753 | if (IntTy->isComplexType() || IntTy->isRealFloatingType()) return true; |
| 754 | if (SkipCast) return false; |
| 755 | if (IntTy->isIntegerType()) { |
| 756 | QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType(); |
| 757 | IntExpr = S.ImpCastExprToType(IntExpr.take(), fpTy, CK_IntegralToFloating); |
| 758 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 759 | CK_FloatingRealToComplex); |
| 760 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 761 | assert(IntTy->isComplexIntegerType()); |
| 762 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 763 | CK_IntegralComplexToFloatingComplex); |
| 764 | } |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | /// \brief Takes two complex float types and converts them to the same type. |
| 769 | /// Helper function of UsualArithmeticConversions() |
| 770 | static QualType |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 771 | handleComplexFloatToComplexFloatConverstion(Sema &S, ExprResult &LHS, |
| 772 | ExprResult &RHS, QualType LHSType, |
| 773 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 774 | bool IsCompAssign) { |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 775 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 776 | |
| 777 | if (order < 0) { |
| 778 | // _Complex float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 779 | if (!IsCompAssign) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 780 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingComplexCast); |
| 781 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 782 | } |
| 783 | if (order > 0) |
| 784 | // _Complex float -> _Complex double |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 785 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingComplexCast); |
| 786 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /// \brief Converts otherExpr to complex float and promotes complexExpr if |
| 790 | /// necessary. Helper function of UsualArithmeticConversions() |
| 791 | static QualType handleOtherComplexFloatConversion(Sema &S, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 792 | ExprResult &ComplexExpr, |
| 793 | ExprResult &OtherExpr, |
| 794 | QualType ComplexTy, |
| 795 | QualType OtherTy, |
| 796 | bool ConvertComplexExpr, |
| 797 | bool ConvertOtherExpr) { |
| 798 | int order = S.Context.getFloatingTypeOrder(ComplexTy, OtherTy); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 799 | |
| 800 | // If just the complexExpr is complex, the otherExpr needs to be converted, |
| 801 | // and the complexExpr might need to be promoted. |
| 802 | if (order > 0) { // complexExpr is wider |
| 803 | // float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 804 | if (ConvertOtherExpr) { |
| 805 | QualType fp = cast<ComplexType>(ComplexTy)->getElementType(); |
| 806 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), fp, CK_FloatingCast); |
| 807 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 808 | CK_FloatingRealToComplex); |
| 809 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 810 | return ComplexTy; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | // otherTy is at least as wide. Find its corresponding complex type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 814 | QualType result = (order == 0 ? ComplexTy : |
| 815 | S.Context.getComplexType(OtherTy)); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 816 | |
| 817 | // double -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 818 | if (ConvertOtherExpr) |
| 819 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 820 | CK_FloatingRealToComplex); |
| 821 | |
| 822 | // _Complex float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 823 | if (ConvertComplexExpr && order < 0) |
| 824 | ComplexExpr = S.ImpCastExprToType(ComplexExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 825 | CK_FloatingComplexCast); |
| 826 | |
| 827 | return result; |
| 828 | } |
| 829 | |
| 830 | /// \brief Handle arithmetic conversion with complex types. Helper function of |
| 831 | /// UsualArithmeticConversions() |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 832 | static QualType handleComplexFloatConversion(Sema &S, ExprResult &LHS, |
| 833 | ExprResult &RHS, QualType LHSType, |
| 834 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 835 | bool IsCompAssign) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 836 | // if we have an integer operand, the result is the complex type. |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 837 | if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 838 | /*skipCast*/false)) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 839 | return LHSType; |
| 840 | if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 841 | /*skipCast*/IsCompAssign)) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 842 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 843 | |
| 844 | // This handles complex/complex, complex/float, or float/complex. |
| 845 | // When both operands are complex, the shorter operand is converted to the |
| 846 | // type of the longer, and that is the type of the result. This corresponds |
| 847 | // to what is done when combining two real floating-point operands. |
| 848 | // The fun begins when size promotion occur across type domains. |
| 849 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 850 | // floating-point type, the less precise type is converted, within it's |
| 851 | // real or complex domain, to the precision of the other type. For example, |
| 852 | // when combining a "long double" with a "double _Complex", the |
| 853 | // "double _Complex" is promoted to "long double _Complex". |
| 854 | |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 855 | bool LHSComplexFloat = LHSType->isComplexType(); |
| 856 | bool RHSComplexFloat = RHSType->isComplexType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 857 | |
| 858 | // If both are complex, just cast to the more precise type. |
| 859 | if (LHSComplexFloat && RHSComplexFloat) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 860 | return handleComplexFloatToComplexFloatConverstion(S, LHS, RHS, |
| 861 | LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 862 | IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 863 | |
| 864 | // If only one operand is complex, promote it if necessary and convert the |
| 865 | // other operand to complex. |
| 866 | if (LHSComplexFloat) |
| 867 | return handleOtherComplexFloatConversion( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 868 | S, LHS, RHS, LHSType, RHSType, /*convertComplexExpr*/!IsCompAssign, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 869 | /*convertOtherExpr*/ true); |
| 870 | |
| 871 | assert(RHSComplexFloat); |
| 872 | return handleOtherComplexFloatConversion( |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 873 | S, RHS, LHS, RHSType, LHSType, /*convertComplexExpr*/true, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 874 | /*convertOtherExpr*/ !IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /// \brief Hande arithmetic conversion from integer to float. Helper function |
| 878 | /// of UsualArithmeticConversions() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 879 | static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, |
| 880 | ExprResult &IntExpr, |
| 881 | QualType FloatTy, QualType IntTy, |
| 882 | bool ConvertFloat, bool ConvertInt) { |
| 883 | if (IntTy->isIntegerType()) { |
| 884 | if (ConvertInt) |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 885 | // Convert intExpr to the lhs floating point type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 886 | IntExpr = S.ImpCastExprToType(IntExpr.take(), FloatTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 887 | CK_IntegralToFloating); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 888 | return FloatTy; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | // Convert both sides to the appropriate complex float. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 892 | assert(IntTy->isComplexIntegerType()); |
| 893 | QualType result = S.Context.getComplexType(FloatTy); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 894 | |
| 895 | // _Complex int -> _Complex float |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 896 | if (ConvertInt) |
| 897 | IntExpr = S.ImpCastExprToType(IntExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 898 | CK_IntegralComplexToFloatingComplex); |
| 899 | |
| 900 | // float -> _Complex float |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 901 | if (ConvertFloat) |
| 902 | FloatExpr = S.ImpCastExprToType(FloatExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 903 | CK_FloatingRealToComplex); |
| 904 | |
| 905 | return result; |
| 906 | } |
| 907 | |
| 908 | /// \brief Handle arithmethic conversion with floating point types. Helper |
| 909 | /// function of UsualArithmeticConversions() |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 910 | static QualType handleFloatConversion(Sema &S, ExprResult &LHS, |
| 911 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 912 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 913 | bool LHSFloat = LHSType->isRealFloatingType(); |
| 914 | bool RHSFloat = RHSType->isRealFloatingType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 915 | |
| 916 | // If we have two real floating types, convert the smaller operand |
| 917 | // to the bigger result. |
| 918 | if (LHSFloat && RHSFloat) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 919 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 920 | if (order > 0) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 921 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingCast); |
| 922 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | assert(order < 0 && "illegal float comparison"); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 926 | if (!IsCompAssign) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 927 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingCast); |
| 928 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | if (LHSFloat) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 932 | return handleIntToFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 933 | /*convertFloat=*/!IsCompAssign, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 934 | /*convertInt=*/ true); |
| 935 | assert(RHSFloat); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 936 | return handleIntToFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 937 | /*convertInt=*/ true, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 938 | /*convertFloat=*/!IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /// \brief Handle conversions with GCC complex int extension. Helper function |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 942 | /// of UsualArithmeticConversions() |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 943 | // FIXME: if the operands are (int, _Complex long), we currently |
| 944 | // don't promote the complex. Also, signedness? |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 945 | static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, |
| 946 | ExprResult &RHS, QualType LHSType, |
| 947 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 948 | bool IsCompAssign) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 949 | const ComplexType *LHSComplexInt = LHSType->getAsComplexIntegerType(); |
| 950 | const ComplexType *RHSComplexInt = RHSType->getAsComplexIntegerType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 951 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 952 | if (LHSComplexInt && RHSComplexInt) { |
| 953 | int order = S.Context.getIntegerTypeOrder(LHSComplexInt->getElementType(), |
| 954 | RHSComplexInt->getElementType()); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 955 | assert(order && "inequal types with equal element ordering"); |
| 956 | if (order > 0) { |
| 957 | // _Complex int -> _Complex long |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 958 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralComplexCast); |
| 959 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 962 | if (!IsCompAssign) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 963 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralComplexCast); |
| 964 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 967 | if (LHSComplexInt) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 968 | // int -> _Complex int |
Eli Friedman | ddadaa4 | 2011-11-12 03:56:23 +0000 | [diff] [blame] | 969 | // FIXME: This needs to take integer ranks into account |
| 970 | RHS = S.ImpCastExprToType(RHS.take(), LHSComplexInt->getElementType(), |
| 971 | CK_IntegralCast); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 972 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralRealToComplex); |
| 973 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 976 | assert(RHSComplexInt); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 977 | // int -> _Complex int |
Eli Friedman | ddadaa4 | 2011-11-12 03:56:23 +0000 | [diff] [blame] | 978 | // FIXME: This needs to take integer ranks into account |
| 979 | if (!IsCompAssign) { |
| 980 | LHS = S.ImpCastExprToType(LHS.take(), RHSComplexInt->getElementType(), |
| 981 | CK_IntegralCast); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 982 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralRealToComplex); |
Eli Friedman | ddadaa4 | 2011-11-12 03:56:23 +0000 | [diff] [blame] | 983 | } |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 984 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /// \brief Handle integer arithmetic conversions. Helper function of |
| 988 | /// UsualArithmeticConversions() |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 989 | static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, |
| 990 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 991 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 992 | // The rules for this case are in C99 6.3.1.8 |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 993 | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); |
| 994 | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); |
| 995 | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); |
| 996 | if (LHSSigned == RHSSigned) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 997 | // Same signedness; use the higher-ranked type |
| 998 | if (order >= 0) { |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 999 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 1000 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1001 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1002 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 1003 | return RHSType; |
| 1004 | } else if (order != (LHSSigned ? 1 : -1)) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 1005 | // The unsigned type has greater than or equal rank to the |
| 1006 | // signed type, so use the unsigned type |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1007 | if (RHSSigned) { |
| 1008 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 1009 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1010 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1011 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 1012 | return RHSType; |
| 1013 | } else if (S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 1014 | // The two types are different widths; if we are here, that |
| 1015 | // means the signed type is larger than the unsigned type, so |
| 1016 | // use the signed type. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1017 | if (LHSSigned) { |
| 1018 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 1019 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1020 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1021 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 1022 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 1023 | } else { |
| 1024 | // The signed type is higher-ranked than the unsigned type, |
| 1025 | // but isn't actually any bigger (like unsigned int and long |
| 1026 | // on most 32-bit systems). Use the unsigned type corresponding |
| 1027 | // to the signed type. |
| 1028 | QualType result = |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1029 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); |
| 1030 | RHS = S.ImpCastExprToType(RHS.take(), result, CK_IntegralCast); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1031 | if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1032 | LHS = S.ImpCastExprToType(LHS.take(), result, CK_IntegralCast); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 1033 | return result; |
| 1034 | } |
| 1035 | } |
| 1036 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1037 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 1038 | /// 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] | 1039 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1040 | /// responsible for emitting appropriate error diagnostics. |
| 1041 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 1042 | /// GCC. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1043 | QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1044 | bool IsCompAssign) { |
| 1045 | if (!IsCompAssign) { |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1046 | LHS = UsualUnaryConversions(LHS.take()); |
| 1047 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1048 | return QualType(); |
| 1049 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1050 | |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1051 | RHS = UsualUnaryConversions(RHS.take()); |
| 1052 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1053 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1054 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1056 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1057 | QualType LHSType = |
| 1058 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 1059 | QualType RHSType = |
| 1060 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1061 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1062 | // For conversion purposes, we ignore any atomic qualifier on the LHS. |
| 1063 | if (const AtomicType *AtomicLHS = LHSType->getAs<AtomicType>()) |
| 1064 | LHSType = AtomicLHS->getValueType(); |
| 1065 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1066 | // If both types are identical, no conversion is needed. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1067 | if (LHSType == RHSType) |
| 1068 | return LHSType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1069 | |
| 1070 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 1071 | // The caller can deal with this (e.g. pointer + int). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1072 | if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType()) |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1073 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1074 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1075 | // Apply unary and bitfield promotions to the LHS's type. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1076 | QualType LHSUnpromotedType = LHSType; |
| 1077 | if (LHSType->isPromotableIntegerType()) |
| 1078 | LHSType = Context.getPromotedIntegerType(LHSType); |
| 1079 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(LHS.get()); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 1080 | if (!LHSBitfieldPromoteTy.isNull()) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1081 | LHSType = LHSBitfieldPromoteTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1082 | if (LHSType != LHSUnpromotedType && !IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1083 | LHS = ImpCastExprToType(LHS.take(), LHSType, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 1084 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1085 | // If both types are identical, no conversion is needed. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1086 | if (LHSType == RHSType) |
| 1087 | return LHSType; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1088 | |
| 1089 | // At this point, we have two different arithmetic types. |
| 1090 | |
| 1091 | // Handle complex types first (C99 6.3.1.8p1). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1092 | if (LHSType->isComplexType() || RHSType->isComplexType()) |
| 1093 | return handleComplexFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1094 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1095 | |
| 1096 | // Now handle "real" floating types (i.e. float, double, long double). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1097 | if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) |
| 1098 | return handleFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1099 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1100 | |
| 1101 | // Handle GCC complex int extension. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1102 | if (LHSType->isComplexIntegerType() || RHSType->isComplexIntegerType()) |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 1103 | return handleComplexIntConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1104 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1105 | |
| 1106 | // Finally, we have two differing integer types. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1107 | return handleIntegerConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1108 | IsCompAssign); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1111 | //===----------------------------------------------------------------------===// |
| 1112 | // Semantic Analysis for various Expression Types |
| 1113 | //===----------------------------------------------------------------------===// |
| 1114 | |
| 1115 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1116 | ExprResult |
| 1117 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 1118 | SourceLocation DefaultLoc, |
| 1119 | SourceLocation RParenLoc, |
| 1120 | Expr *ControllingExpr, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1121 | MultiTypeArg ArgTypes, |
| 1122 | MultiExprArg ArgExprs) { |
| 1123 | unsigned NumAssocs = ArgTypes.size(); |
| 1124 | assert(NumAssocs == ArgExprs.size()); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1125 | |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1126 | ParsedType *ParsedTypes = ArgTypes.data(); |
| 1127 | Expr **Exprs = ArgExprs.data(); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1128 | |
| 1129 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 1130 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1131 | if (ParsedTypes[i]) |
| 1132 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 1133 | else |
| 1134 | Types[i] = 0; |
| 1135 | } |
| 1136 | |
| 1137 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 1138 | ControllingExpr, Types, Exprs, |
| 1139 | NumAssocs); |
Benjamin Kramer | 5bf47f7 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 1140 | delete [] Types; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1141 | return ER; |
| 1142 | } |
| 1143 | |
| 1144 | ExprResult |
| 1145 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 1146 | SourceLocation DefaultLoc, |
| 1147 | SourceLocation RParenLoc, |
| 1148 | Expr *ControllingExpr, |
| 1149 | TypeSourceInfo **Types, |
| 1150 | Expr **Exprs, |
| 1151 | unsigned NumAssocs) { |
| 1152 | bool TypeErrorFound = false, |
| 1153 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 1154 | ContainsUnexpandedParameterPack |
| 1155 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 1156 | |
| 1157 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1158 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 1159 | ContainsUnexpandedParameterPack = true; |
| 1160 | |
| 1161 | if (Types[i]) { |
| 1162 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 1163 | ContainsUnexpandedParameterPack = true; |
| 1164 | |
| 1165 | if (Types[i]->getType()->isDependentType()) { |
| 1166 | IsResultDependent = true; |
| 1167 | } else { |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1168 | // 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] | 1169 | // complete object type other than a variably modified type." |
| 1170 | unsigned D = 0; |
| 1171 | if (Types[i]->getType()->isIncompleteType()) |
| 1172 | D = diag::err_assoc_type_incomplete; |
| 1173 | else if (!Types[i]->getType()->isObjectType()) |
| 1174 | D = diag::err_assoc_type_nonobject; |
| 1175 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 1176 | D = diag::err_assoc_type_variably_modified; |
| 1177 | |
| 1178 | if (D != 0) { |
| 1179 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 1180 | << Types[i]->getTypeLoc().getSourceRange() |
| 1181 | << Types[i]->getType(); |
| 1182 | TypeErrorFound = true; |
| 1183 | } |
| 1184 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1185 | // 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] | 1186 | // selection shall specify compatible types." |
| 1187 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 1188 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 1189 | Context.typesAreCompatible(Types[i]->getType(), |
| 1190 | Types[j]->getType())) { |
| 1191 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 1192 | diag::err_assoc_compatible_types) |
| 1193 | << Types[j]->getTypeLoc().getSourceRange() |
| 1194 | << Types[j]->getType() |
| 1195 | << Types[i]->getType(); |
| 1196 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 1197 | diag::note_compat_assoc) |
| 1198 | << Types[i]->getTypeLoc().getSourceRange() |
| 1199 | << Types[i]->getType(); |
| 1200 | TypeErrorFound = true; |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | if (TypeErrorFound) |
| 1206 | return ExprError(); |
| 1207 | |
| 1208 | // If we determined that the generic selection is result-dependent, don't |
| 1209 | // try to compute the result expression. |
| 1210 | if (IsResultDependent) |
| 1211 | return Owned(new (Context) GenericSelectionExpr( |
| 1212 | Context, KeyLoc, ControllingExpr, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1213 | llvm::makeArrayRef(Types, NumAssocs), |
| 1214 | llvm::makeArrayRef(Exprs, NumAssocs), |
| 1215 | DefaultLoc, RParenLoc, ContainsUnexpandedParameterPack)); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1217 | SmallVector<unsigned, 1> CompatIndices; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1218 | unsigned DefaultIndex = -1U; |
| 1219 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1220 | if (!Types[i]) |
| 1221 | DefaultIndex = i; |
| 1222 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 1223 | Types[i]->getType())) |
| 1224 | CompatIndices.push_back(i); |
| 1225 | } |
| 1226 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1227 | // 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] | 1228 | // type compatible with at most one of the types named in its generic |
| 1229 | // association list." |
| 1230 | if (CompatIndices.size() > 1) { |
| 1231 | // We strip parens here because the controlling expression is typically |
| 1232 | // parenthesized in macro definitions. |
| 1233 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1234 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 1235 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 1236 | << (unsigned) CompatIndices.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1237 | for (SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1238 | E = CompatIndices.end(); I != E; ++I) { |
| 1239 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 1240 | diag::note_compat_assoc) |
| 1241 | << Types[*I]->getTypeLoc().getSourceRange() |
| 1242 | << Types[*I]->getType(); |
| 1243 | } |
| 1244 | return ExprError(); |
| 1245 | } |
| 1246 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1247 | // 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] | 1248 | // its controlling expression shall have type compatible with exactly one of |
| 1249 | // the types named in its generic association list." |
| 1250 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 1251 | // We strip parens here because the controlling expression is typically |
| 1252 | // parenthesized in macro definitions. |
| 1253 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1254 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 1255 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 1256 | return ExprError(); |
| 1257 | } |
| 1258 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1259 | // 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] | 1260 | // type name that is compatible with the type of the controlling expression, |
| 1261 | // then the result expression of the generic selection is the expression |
| 1262 | // in that generic association. Otherwise, the result expression of the |
| 1263 | // generic selection is the expression in the default generic association." |
| 1264 | unsigned ResultIndex = |
| 1265 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 1266 | |
| 1267 | return Owned(new (Context) GenericSelectionExpr( |
| 1268 | Context, KeyLoc, ControllingExpr, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1269 | llvm::makeArrayRef(Types, NumAssocs), |
| 1270 | llvm::makeArrayRef(Exprs, NumAssocs), |
| 1271 | DefaultLoc, RParenLoc, ContainsUnexpandedParameterPack, |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1272 | ResultIndex)); |
| 1273 | } |
| 1274 | |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1275 | /// getUDSuffixLoc - Create a SourceLocation for a ud-suffix, given the |
| 1276 | /// location of the token and the offset of the ud-suffix within it. |
| 1277 | static SourceLocation getUDSuffixLoc(Sema &S, SourceLocation TokLoc, |
| 1278 | unsigned Offset) { |
| 1279 | return Lexer::AdvanceToTokenCharacter(TokLoc, Offset, S.getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1280 | S.getLangOpts()); |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1283 | /// BuildCookedLiteralOperatorCall - A user-defined literal was found. Look up |
| 1284 | /// the corresponding cooked (non-raw) literal operator, and build a call to it. |
| 1285 | static ExprResult BuildCookedLiteralOperatorCall(Sema &S, Scope *Scope, |
| 1286 | IdentifierInfo *UDSuffix, |
| 1287 | SourceLocation UDSuffixLoc, |
| 1288 | ArrayRef<Expr*> Args, |
| 1289 | SourceLocation LitEndLoc) { |
| 1290 | assert(Args.size() <= 2 && "too many arguments for literal operator"); |
| 1291 | |
| 1292 | QualType ArgTy[2]; |
| 1293 | for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { |
| 1294 | ArgTy[ArgIdx] = Args[ArgIdx]->getType(); |
| 1295 | if (ArgTy[ArgIdx]->isArrayType()) |
| 1296 | ArgTy[ArgIdx] = S.Context.getArrayDecayedType(ArgTy[ArgIdx]); |
| 1297 | } |
| 1298 | |
| 1299 | DeclarationName OpName = |
| 1300 | S.Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); |
| 1301 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); |
| 1302 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); |
| 1303 | |
| 1304 | LookupResult R(S, OpName, UDSuffixLoc, Sema::LookupOrdinaryName); |
| 1305 | if (S.LookupLiteralOperator(Scope, R, llvm::makeArrayRef(ArgTy, Args.size()), |
| 1306 | /*AllowRawAndTemplate*/false) == Sema::LOLR_Error) |
| 1307 | return ExprError(); |
| 1308 | |
| 1309 | return S.BuildLiteralOperatorCall(R, OpNameInfo, Args, LitEndLoc); |
| 1310 | } |
| 1311 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1312 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1313 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 1314 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 1315 | /// multiple tokens. However, the common case is that StringToks points to one |
| 1316 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1317 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1318 | ExprResult |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1319 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks, |
| 1320 | Scope *UDLScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1321 | assert(NumStringToks && "Must have at least one string!"); |
| 1322 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1323 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1324 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1325 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1326 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1327 | SmallVector<SourceLocation, 4> StringTokLocs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1328 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 1329 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1330 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1331 | QualType StrTy = Context.CharTy; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1332 | if (Literal.isWide()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1333 | StrTy = Context.getWCharType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1334 | else if (Literal.isUTF16()) |
| 1335 | StrTy = Context.Char16Ty; |
| 1336 | else if (Literal.isUTF32()) |
| 1337 | StrTy = Context.Char32Ty; |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 1338 | else if (Literal.isPascal()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1339 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1340 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1341 | StringLiteral::StringKind Kind = StringLiteral::Ascii; |
| 1342 | if (Literal.isWide()) |
| 1343 | Kind = StringLiteral::Wide; |
| 1344 | else if (Literal.isUTF8()) |
| 1345 | Kind = StringLiteral::UTF8; |
| 1346 | else if (Literal.isUTF16()) |
| 1347 | Kind = StringLiteral::UTF16; |
| 1348 | else if (Literal.isUTF32()) |
| 1349 | Kind = StringLiteral::UTF32; |
| 1350 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1351 | // 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] | 1352 | if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1353 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1355 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 1356 | // the nul terminator character as well as the string length for pascal |
| 1357 | // strings. |
| 1358 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1359 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1360 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1362 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1363 | StringLiteral *Lit = StringLiteral::Create(Context, Literal.GetString(), |
| 1364 | Kind, Literal.Pascal, StrTy, |
| 1365 | &StringTokLocs[0], |
| 1366 | StringTokLocs.size()); |
| 1367 | if (Literal.getUDSuffix().empty()) |
| 1368 | return Owned(Lit); |
| 1369 | |
| 1370 | // We're building a user-defined literal. |
| 1371 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1372 | SourceLocation UDSuffixLoc = |
| 1373 | getUDSuffixLoc(*this, StringTokLocs[Literal.getUDSuffixToken()], |
| 1374 | Literal.getUDSuffixOffset()); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1375 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1376 | // Make sure we're allowed user-defined literals here. |
| 1377 | if (!UDLScope) |
| 1378 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_string_udl)); |
| 1379 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1380 | // C++11 [lex.ext]p5: The literal L is treated as a call of the form |
| 1381 | // operator "" X (str, len) |
| 1382 | QualType SizeType = Context.getSizeType(); |
| 1383 | llvm::APInt Len(Context.getIntWidth(SizeType), Literal.GetNumStringChars()); |
| 1384 | IntegerLiteral *LenArg = IntegerLiteral::Create(Context, Len, SizeType, |
| 1385 | StringTokLocs[0]); |
| 1386 | Expr *Args[] = { Lit, LenArg }; |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1387 | return BuildCookedLiteralOperatorCall(*this, UDLScope, UDSuffix, UDSuffixLoc, |
| 1388 | Args, StringTokLocs.back()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1391 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1392 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1393 | SourceLocation Loc, |
| 1394 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1395 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1396 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1399 | /// BuildDeclRefExpr - Build an expression that references a |
| 1400 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1401 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1402 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1403 | const DeclarationNameInfo &NameInfo, |
| 1404 | const CXXScopeSpec *SS) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1405 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 1406 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 1407 | if (const FunctionDecl *Callee = dyn_cast<FunctionDecl>(D)) { |
| 1408 | CUDAFunctionTarget CallerTarget = IdentifyCUDATarget(Caller), |
| 1409 | CalleeTarget = IdentifyCUDATarget(Callee); |
| 1410 | if (CheckCUDATarget(CallerTarget, CalleeTarget)) { |
| 1411 | Diag(NameInfo.getLoc(), diag::err_ref_bad_target) |
| 1412 | << CalleeTarget << D->getIdentifier() << CallerTarget; |
| 1413 | Diag(D->getLocation(), diag::note_previous_decl) |
| 1414 | << D->getIdentifier(); |
| 1415 | return ExprError(); |
| 1416 | } |
| 1417 | } |
| 1418 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1419 | bool refersToEnclosingScope = |
| 1420 | (CurContext != D->getDeclContext() && |
| 1421 | D->getDeclContext()->isFunctionOrMethod()); |
| 1422 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1423 | DeclRefExpr *E = DeclRefExpr::Create(Context, |
| 1424 | SS ? SS->getWithLocInContext(Context) |
| 1425 | : NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1426 | SourceLocation(), |
| 1427 | D, refersToEnclosingScope, |
| 1428 | NameInfo, Ty, VK); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1430 | MarkDeclRefReferenced(E); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1431 | |
Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 1432 | if (getLangOpts().ObjCARCWeak && isa<VarDecl>(D) && |
| 1433 | Ty.getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 1434 | DiagnosticsEngine::Level Level = |
| 1435 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, |
| 1436 | E->getLocStart()); |
| 1437 | if (Level != DiagnosticsEngine::Ignored) |
| 1438 | getCurFunction()->recordUseOfWeak(E); |
| 1439 | } |
| 1440 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1441 | // Just in case we're building an illegal pointer-to-member. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1442 | FieldDecl *FD = dyn_cast<FieldDecl>(D); |
| 1443 | if (FD && FD->isBitField()) |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1444 | E->setObjectKind(OK_BitField); |
| 1445 | |
| 1446 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1449 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1450 | /// possibly a list of template arguments. |
| 1451 | /// |
| 1452 | /// If this produces template arguments, it is permitted to call |
| 1453 | /// DecomposeTemplateName. |
| 1454 | /// |
| 1455 | /// This actually loses a lot of source location information for |
| 1456 | /// non-standard name kinds; we should consider preserving that in |
| 1457 | /// some way. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1458 | void |
| 1459 | Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, |
| 1460 | TemplateArgumentListInfo &Buffer, |
| 1461 | DeclarationNameInfo &NameInfo, |
| 1462 | const TemplateArgumentListInfo *&TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1463 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1464 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1465 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1466 | |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1467 | ASTTemplateArgsPtr TemplateArgsPtr(Id.TemplateId->getTemplateArgs(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1468 | Id.TemplateId->NumArgs); |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1469 | translateTemplateArguments(TemplateArgsPtr, Buffer); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1470 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1471 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1472 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1473 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1474 | TemplateArgs = &Buffer; |
| 1475 | } else { |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1476 | NameInfo = GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1477 | TemplateArgs = 0; |
| 1478 | } |
| 1479 | } |
| 1480 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1481 | /// Diagnose an empty lookup. |
| 1482 | /// |
| 1483 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1484 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 1485 | CorrectionCandidateCallback &CCC, |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1486 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1487 | llvm::ArrayRef<Expr *> Args) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1488 | DeclarationName Name = R.getLookupName(); |
| 1489 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1490 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1491 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1492 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1493 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1494 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1495 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1496 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1497 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1498 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1499 | // If the original lookup was an unqualified lookup, fake an |
| 1500 | // unqualified lookup. This is useful when (for example) the |
| 1501 | // original lookup would not have found something because it was a |
| 1502 | // dependent name. |
David Blaikie | 4872e10 | 2012-05-28 01:26:45 +0000 | [diff] [blame] | 1503 | DeclContext *DC = (SS.isEmpty() && !CallsUndergoingInstantiation.empty()) |
| 1504 | ? CurContext : 0; |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1505 | while (DC) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1506 | if (isa<CXXRecordDecl>(DC)) { |
| 1507 | LookupQualifiedName(R, DC); |
| 1508 | |
| 1509 | if (!R.empty()) { |
| 1510 | // Don't give errors about ambiguities in this lookup. |
| 1511 | R.suppressDiagnostics(); |
| 1512 | |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1513 | // During a default argument instantiation the CurContext points |
| 1514 | // to a CXXMethodDecl; but we can't apply a this-> fixit inside a |
| 1515 | // function parameter list, hence add an explicit check. |
| 1516 | bool isDefaultArgument = !ActiveTemplateInstantiations.empty() && |
| 1517 | ActiveTemplateInstantiations.back().Kind == |
| 1518 | ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1519 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1520 | bool isInstance = CurMethod && |
| 1521 | CurMethod->isInstance() && |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1522 | DC == CurMethod->getParent() && !isDefaultArgument; |
| 1523 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1524 | |
| 1525 | // Give a code modification hint to insert 'this->'. |
| 1526 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1527 | // Actually quite difficult! |
Nico Weber | 4b554f4 | 2012-06-20 20:21:42 +0000 | [diff] [blame] | 1528 | if (getLangOpts().MicrosoftMode) |
| 1529 | diagnostic = diag::warn_found_via_dependent_bases_lookup; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1530 | if (isInstance) { |
Nico Weber | 94c4d61 | 2012-06-22 16:39:39 +0000 | [diff] [blame] | 1531 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1532 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1533 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1534 | CallsUndergoingInstantiation.back()->getCallee()); |
Nico Weber | 94c4d61 | 2012-06-22 16:39:39 +0000 | [diff] [blame] | 1535 | |
| 1536 | |
| 1537 | CXXMethodDecl *DepMethod; |
| 1538 | if (CurMethod->getTemplatedKind() == |
| 1539 | FunctionDecl::TK_FunctionTemplateSpecialization) |
| 1540 | DepMethod = cast<CXXMethodDecl>(CurMethod->getPrimaryTemplate()-> |
| 1541 | getInstantiatedFromMemberTemplate()->getTemplatedDecl()); |
| 1542 | else |
| 1543 | DepMethod = cast<CXXMethodDecl>( |
| 1544 | CurMethod->getInstantiatedFromMemberFunction()); |
| 1545 | assert(DepMethod && "No template pattern found"); |
| 1546 | |
| 1547 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1548 | CheckCXXThisCapture(R.getNameLoc()); |
| 1549 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1550 | R.getNameLoc(), DepThisType, false); |
| 1551 | TemplateArgumentListInfo TList; |
| 1552 | if (ULE->hasExplicitTemplateArgs()) |
| 1553 | ULE->copyTemplateArgumentsInto(TList); |
| 1554 | |
| 1555 | CXXScopeSpec SS; |
| 1556 | SS.Adopt(ULE->getQualifierLoc()); |
| 1557 | CXXDependentScopeMemberExpr *DepExpr = |
| 1558 | CXXDependentScopeMemberExpr::Create( |
| 1559 | Context, DepThis, DepThisType, true, SourceLocation(), |
| 1560 | SS.getWithLocInContext(Context), |
| 1561 | ULE->getTemplateKeywordLoc(), 0, |
| 1562 | R.getLookupNameInfo(), |
| 1563 | ULE->hasExplicitTemplateArgs() ? &TList : 0); |
| 1564 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1565 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1566 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1567 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1568 | |
| 1569 | // Do we really want to note all of these? |
| 1570 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1571 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1572 | |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1573 | // Return true if we are inside a default argument instantiation |
| 1574 | // and the found name refers to an instance member function, otherwise |
| 1575 | // the function calling DiagnoseEmptyLookup will try to create an |
| 1576 | // implicit member call and this is wrong for default argument. |
| 1577 | if (isDefaultArgument && ((*R.begin())->isCXXInstanceMember())) { |
| 1578 | Diag(R.getNameLoc(), diag::err_member_call_without_object); |
| 1579 | return true; |
| 1580 | } |
| 1581 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1582 | // Tell the callee to try to recover. |
| 1583 | return false; |
| 1584 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1585 | |
| 1586 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1587 | } |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1588 | |
| 1589 | // In Microsoft mode, if we are performing lookup from within a friend |
| 1590 | // function definition declared at class scope then we must set |
| 1591 | // DC to the lexical parent to be able to search into the parent |
| 1592 | // class. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1593 | if (getLangOpts().MicrosoftMode && isa<FunctionDecl>(DC) && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1594 | cast<FunctionDecl>(DC)->getFriendObjectKind() && |
| 1595 | DC->getLexicalParent()->isRecord()) |
| 1596 | DC = DC->getLexicalParent(); |
| 1597 | else |
| 1598 | DC = DC->getParent(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1601 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1602 | TypoCorrection Corrected; |
| 1603 | if (S && (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1604 | S, &SS, CCC))) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1605 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 1606 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1607 | R.setLookupName(Corrected.getCorrection()); |
| 1608 | |
Hans Wennborg | 701d1e7 | 2011-07-12 08:45:31 +0000 | [diff] [blame] | 1609 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1610 | if (Corrected.isOverloaded()) { |
| 1611 | OverloadCandidateSet OCS(R.getNameLoc()); |
| 1612 | OverloadCandidateSet::iterator Best; |
| 1613 | for (TypoCorrection::decl_iterator CD = Corrected.begin(), |
| 1614 | CDEnd = Corrected.end(); |
| 1615 | CD != CDEnd; ++CD) { |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1616 | if (FunctionTemplateDecl *FTD = |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1617 | dyn_cast<FunctionTemplateDecl>(*CD)) |
| 1618 | AddTemplateOverloadCandidate( |
| 1619 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1620 | Args, OCS); |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1621 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*CD)) |
| 1622 | if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 0) |
| 1623 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1624 | Args, OCS); |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1625 | } |
| 1626 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { |
| 1627 | case OR_Success: |
| 1628 | ND = Best->Function; |
| 1629 | break; |
| 1630 | default: |
Kaelyn Uhrain | 844d572 | 2011-08-04 23:30:54 +0000 | [diff] [blame] | 1631 | break; |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1632 | } |
| 1633 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1634 | R.addDecl(ND); |
| 1635 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1636 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1637 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr |
| 1638 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1639 | else |
| 1640 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1641 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1642 | << SS.getRange() |
David Blaikie | 6952c01 | 2012-10-12 20:00:44 +0000 | [diff] [blame^] | 1643 | << FixItHint::CreateReplacement(Corrected.getCorrectionRange(), |
| 1644 | CorrectedStr); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1645 | if (ND) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1646 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1647 | << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1648 | |
| 1649 | // Tell the callee to try to recover. |
| 1650 | return false; |
| 1651 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1652 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1653 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1654 | // FIXME: If we ended up with a typo for a type name or |
| 1655 | // Objective-C class name, we're in trouble because the parser |
| 1656 | // is in the wrong place to recover. Suggest the typo |
| 1657 | // correction, but don't make it a fix-it since we're not going |
| 1658 | // to recover well anyway. |
| 1659 | if (SS.isEmpty()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1660 | Diag(R.getNameLoc(), diagnostic_suggest) |
| 1661 | << Name << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1662 | else |
| 1663 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1664 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1665 | << SS.getRange(); |
| 1666 | |
| 1667 | // Don't try to recover; it won't work. |
| 1668 | return true; |
| 1669 | } |
| 1670 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1671 | // 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] | 1672 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1673 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1674 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1675 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1676 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1677 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1678 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1679 | return true; |
| 1680 | } |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1681 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1682 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1683 | |
| 1684 | // Emit a special diagnostic for failed member lookups. |
| 1685 | // FIXME: computing the declaration context might fail here (?) |
| 1686 | if (!SS.isEmpty()) { |
| 1687 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1688 | << Name << computeDeclContext(SS, false) |
| 1689 | << SS.getRange(); |
| 1690 | return true; |
| 1691 | } |
| 1692 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1693 | // Give up, we can't recover. |
| 1694 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1695 | return true; |
| 1696 | } |
| 1697 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1698 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1699 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1700 | SourceLocation TemplateKWLoc, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1701 | UnqualifiedId &Id, |
| 1702 | bool HasTrailingLParen, |
Kaelyn Uhrain | cd78e61 | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 1703 | bool IsAddressOfOperand, |
| 1704 | CorrectionCandidateCallback *CCC) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1705 | assert(!(IsAddressOfOperand && HasTrailingLParen) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1706 | "cannot be direct & operand and have a trailing lparen"); |
| 1707 | |
| 1708 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1709 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1710 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1711 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1712 | |
| 1713 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1714 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1715 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1716 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1717 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1718 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1719 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1720 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1721 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1722 | // C++ [temp.dep.expr]p3: |
| 1723 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1724 | // -- an identifier that was declared with a dependent type, |
| 1725 | // (note: handled after lookup) |
| 1726 | // -- a template-id that is dependent, |
| 1727 | // (note: handled in BuildTemplateIdExpr) |
| 1728 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1729 | // -- a nested-name-specifier that contains a class-name that |
| 1730 | // names a dependent type. |
| 1731 | // Determine whether this is a member of an unknown specialization; |
| 1732 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1733 | bool DependentID = false; |
| 1734 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1735 | Name.getCXXNameType()->isDependentType()) { |
| 1736 | DependentID = true; |
| 1737 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1738 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1739 | if (RequireCompleteDeclContext(SS, DC)) |
| 1740 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1741 | } else { |
| 1742 | DependentID = true; |
| 1743 | } |
| 1744 | } |
| 1745 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1746 | if (DependentID) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1747 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1748 | IsAddressOfOperand, TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1749 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1750 | // Perform the required lookup. |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1751 | LookupResult R(*this, NameInfo, |
| 1752 | (Id.getKind() == UnqualifiedId::IK_ImplicitSelfParam) |
| 1753 | ? LookupObjCImplicitSelfParam : LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1754 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1755 | // Lookup the template name again to correctly establish the context in |
| 1756 | // which it was found. This is really unfortunate as we already did the |
| 1757 | // lookup to determine that it was a template name in the first place. If |
| 1758 | // this becomes a performance hit, we can work harder to preserve those |
| 1759 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1760 | bool MemberOfUnknownSpecialization; |
| 1761 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1762 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1763 | |
| 1764 | if (MemberOfUnknownSpecialization || |
| 1765 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1766 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1767 | IsAddressOfOperand, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1768 | } else { |
Benjamin Kramer | b7ff74a | 2012-01-20 14:57:34 +0000 | [diff] [blame] | 1769 | bool IvarLookupFollowUp = II && !SS.isSet() && getCurMethodDecl(); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1770 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1772 | // If the result might be in a dependent base class, this is a dependent |
| 1773 | // id-expression. |
| 1774 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1775 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1776 | IsAddressOfOperand, TemplateArgs); |
| 1777 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1778 | // If this reference is in an Objective-C method, then we need to do |
| 1779 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1780 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1781 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1782 | if (E.isInvalid()) |
| 1783 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1785 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1786 | return Owned(Ex); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1787 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1788 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1789 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1790 | if (R.isAmbiguous()) |
| 1791 | return ExprError(); |
| 1792 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1793 | // Determine whether this name might be a candidate for |
| 1794 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1795 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1796 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1797 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1798 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1799 | // in C90, extension in C99, forbidden in C++). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1800 | if (HasTrailingLParen && II && !getLangOpts().CPlusPlus) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1801 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1802 | if (D) R.addDecl(D); |
| 1803 | } |
| 1804 | |
| 1805 | // If this name wasn't predeclared and if this is not a function |
| 1806 | // call, diagnose the problem. |
| 1807 | if (R.empty()) { |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1808 | |
| 1809 | // In Microsoft mode, if we are inside a template class member function |
| 1810 | // and we can't resolve an identifier then assume the identifier is type |
| 1811 | // dependent. The goal is to postpone name lookup to instantiation time |
| 1812 | // to be able to search into type dependent base classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1813 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1814 | isa<CXXMethodDecl>(CurContext)) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1815 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1816 | IsAddressOfOperand, TemplateArgs); |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1817 | |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 1818 | CorrectionCandidateCallback DefaultValidator; |
Kaelyn Uhrain | cd78e61 | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 1819 | if (DiagnoseEmptyLookup(S, SS, R, CCC ? *CCC : DefaultValidator)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1820 | return ExprError(); |
| 1821 | |
| 1822 | assert(!R.empty() && |
| 1823 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1824 | |
| 1825 | // If we found an Objective-C instance variable, let |
| 1826 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1827 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1828 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1829 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1830 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Fariborz Jahanian | bc2b91a | 2011-09-23 23:11:38 +0000 | [diff] [blame] | 1831 | // In a hopelessly buggy code, Objective-C instance variable |
| 1832 | // lookup fails and no expression will be built to reference it. |
| 1833 | if (!E.isInvalid() && !E.get()) |
| 1834 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1835 | return E; |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1836 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1837 | } |
| 1838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1840 | // This is guaranteed from this point on. |
| 1841 | assert(!R.empty() || ADL); |
| 1842 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1843 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1844 | // C++ [class.mfct.non-static]p3: |
| 1845 | // When an id-expression that is not part of a class member access |
| 1846 | // syntax and not used to form a pointer to member is used in the |
| 1847 | // body of a non-static member function of class X, if name lookup |
| 1848 | // resolves the name in the id-expression to a non-static non-type |
| 1849 | // member of some class C, the id-expression is transformed into a |
| 1850 | // class member access expression using (*this) as the |
| 1851 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1852 | // |
| 1853 | // But we don't actually need to do this for '&' operands if R |
| 1854 | // resolved to a function or overloaded function set, because the |
| 1855 | // expression is ill-formed if it actually works out to be a |
| 1856 | // non-static member function: |
| 1857 | // |
| 1858 | // C++ [expr.ref]p4: |
| 1859 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1860 | // [t]he expression can be used only as the left-hand operand of a |
| 1861 | // member function call. |
| 1862 | // |
| 1863 | // There are other safeguards against such uses, but it's important |
| 1864 | // to get this right here so that we don't end up making a |
| 1865 | // spuriously dependent expression if we're inside a dependent |
| 1866 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1867 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1868 | bool MightBeImplicitMember; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1869 | if (!IsAddressOfOperand) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1870 | MightBeImplicitMember = true; |
| 1871 | else if (!SS.isEmpty()) |
| 1872 | MightBeImplicitMember = false; |
| 1873 | else if (R.isOverloadedResult()) |
| 1874 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1875 | else if (R.isUnresolvableResult()) |
| 1876 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1877 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1878 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1879 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1880 | |
| 1881 | if (MightBeImplicitMember) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1882 | return BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 1883 | R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 1886 | if (TemplateArgs || TemplateKWLoc.isValid()) |
| 1887 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, ADL, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1888 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1889 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1890 | } |
| 1891 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1892 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1893 | /// declaration name, generally during template instantiation. |
| 1894 | /// There's a large number of things which don't need to be done along |
| 1895 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1896 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1897 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1898 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1899 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1900 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 1901 | return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), |
| 1902 | NameInfo, /*TemplateArgs=*/0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1903 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1904 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1905 | return ExprError(); |
| 1906 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1907 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1908 | LookupQualifiedName(R, DC); |
| 1909 | |
| 1910 | if (R.isAmbiguous()) |
| 1911 | return ExprError(); |
| 1912 | |
| 1913 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1914 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1915 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1916 | return ExprError(); |
| 1917 | } |
| 1918 | |
| 1919 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1920 | } |
| 1921 | |
| 1922 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1923 | /// detected that we're currently inside an ObjC method. Perform some |
| 1924 | /// additional lookup. |
| 1925 | /// |
| 1926 | /// Ideally, most of this would be done by lookup, but there's |
| 1927 | /// actually quite a lot of extra work involved. |
| 1928 | /// |
| 1929 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1930 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1931 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1932 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1933 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1934 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1935 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1936 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1937 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1938 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1939 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1940 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1941 | |
| 1942 | // If we're in a class method, we don't normally want to look for |
| 1943 | // ivars. But if we don't find anything else, and there's an |
| 1944 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1945 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1946 | |
| 1947 | bool LookForIvars; |
| 1948 | if (Lookup.empty()) |
| 1949 | LookForIvars = true; |
| 1950 | else if (IsClassMethod) |
| 1951 | LookForIvars = false; |
| 1952 | else |
| 1953 | LookForIvars = (Lookup.isSingleResult() && |
| 1954 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1955 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1956 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1957 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1958 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | 7c81c2a | 2011-10-19 02:25:16 +0000 | [diff] [blame] | 1959 | ObjCIvarDecl *IV = 0; |
| 1960 | if (IFace && (IV = IFace->lookupInstanceVariable(II, ClassDeclared))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1961 | // Diagnose using an ivar in a class method. |
| 1962 | if (IsClassMethod) |
| 1963 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1964 | << IV->getDeclName()); |
| 1965 | |
| 1966 | // If we're referencing an invalid decl, just return this as a silent |
| 1967 | // error node. The error diagnostic was already emitted on the decl. |
| 1968 | if (IV->isInvalidDecl()) |
| 1969 | return ExprError(); |
| 1970 | |
| 1971 | // Check if referencing a field with __attribute__((deprecated)). |
| 1972 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1973 | return ExprError(); |
| 1974 | |
| 1975 | // Diagnose the use of an ivar outside of the declaring class. |
| 1976 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
Fariborz Jahanian | 458a7fb | 2012-03-07 00:58:41 +0000 | [diff] [blame] | 1977 | !declaresSameEntity(ClassDeclared, IFace) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1978 | !getLangOpts().DebuggerSupport) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1979 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1980 | |
| 1981 | // FIXME: This should use a new expr for a direct reference, don't |
| 1982 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1983 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1984 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1985 | SelfName.setIdentifier(&II, SourceLocation()); |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1986 | SelfName.setKind(UnqualifiedId::IK_ImplicitSelfParam); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1987 | CXXScopeSpec SelfScopeSpec; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1988 | SourceLocation TemplateKWLoc; |
| 1989 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, TemplateKWLoc, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1990 | SelfName, false, false); |
| 1991 | if (SelfExpr.isInvalid()) |
| 1992 | return ExprError(); |
| 1993 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1994 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 1995 | if (SelfExpr.isInvalid()) |
| 1996 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1997 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1998 | MarkAnyDeclReferenced(Loc, IV); |
Fariborz Jahanian | ed6662d | 2012-08-08 16:41:04 +0000 | [diff] [blame] | 1999 | |
| 2000 | ObjCMethodFamily MF = CurMethod->getMethodFamily(); |
| 2001 | if (MF != OMF_init && MF != OMF_dealloc && MF != OMF_finalize) |
| 2002 | Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName(); |
Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 2003 | |
| 2004 | ObjCIvarRefExpr *Result = new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2005 | Loc, |
| 2006 | SelfExpr.take(), |
| 2007 | true, true); |
| 2008 | |
| 2009 | if (getLangOpts().ObjCAutoRefCount) { |
| 2010 | if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 2011 | DiagnosticsEngine::Level Level = |
| 2012 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, Loc); |
| 2013 | if (Level != DiagnosticsEngine::Ignored) |
| 2014 | getCurFunction()->recordUseOfWeak(Result); |
| 2015 | } |
Fariborz Jahanian | 3f001ff | 2012-10-03 17:55:29 +0000 | [diff] [blame] | 2016 | if (CurContext->isClosure()) |
| 2017 | Diag(Loc, diag::warn_implicitly_retains_self) |
| 2018 | << FixItHint::CreateInsertion(Loc, "self->"); |
Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | return Owned(Result); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2022 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2023 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2024 | // We should warn if a local variable hides an ivar. |
Fariborz Jahanian | 90f7b62 | 2011-11-08 22:51:27 +0000 | [diff] [blame] | 2025 | if (ObjCInterfaceDecl *IFace = CurMethod->getClassInterface()) { |
| 2026 | ObjCInterfaceDecl *ClassDeclared; |
| 2027 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2028 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 2029 | declaresSameEntity(IFace, ClassDeclared)) |
Fariborz Jahanian | 90f7b62 | 2011-11-08 22:51:27 +0000 | [diff] [blame] | 2030 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 2031 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2032 | } |
Fariborz Jahanian | b5ea9db | 2011-12-20 22:21:08 +0000 | [diff] [blame] | 2033 | } else if (Lookup.isSingleResult() && |
| 2034 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()) { |
| 2035 | // If accessing a stand-alone ivar in a class method, this is an error. |
| 2036 | if (const ObjCIvarDecl *IV = dyn_cast<ObjCIvarDecl>(Lookup.getFoundDecl())) |
| 2037 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 2038 | << IV->getDeclName()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2041 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 2042 | // FIXME. Consolidate this with similar code in LookupName. |
| 2043 | if (unsigned BuiltinID = II->getBuiltinID()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2044 | if (!(getLangOpts().CPlusPlus && |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2045 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 2046 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 2047 | S, Lookup.isForRedeclaration(), |
| 2048 | Lookup.getNameLoc()); |
| 2049 | if (D) Lookup.addDecl(D); |
| 2050 | } |
| 2051 | } |
| 2052 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2053 | // Sentinel value saying that we didn't do anything special. |
| 2054 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2055 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2056 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2057 | /// \brief Cast a base object to a member's actual type. |
| 2058 | /// |
| 2059 | /// Logically this happens in three phases: |
| 2060 | /// |
| 2061 | /// * First we cast from the base type to the naming class. |
| 2062 | /// The naming class is the class into which we were looking |
| 2063 | /// when we found the member; it's the qualifier type if a |
| 2064 | /// qualifier was provided, and otherwise it's the base type. |
| 2065 | /// |
| 2066 | /// * Next we cast from the naming class to the declaring class. |
| 2067 | /// If the member we found was brought into a class's scope by |
| 2068 | /// a using declaration, this is that class; otherwise it's |
| 2069 | /// the class declaring the member. |
| 2070 | /// |
| 2071 | /// * Finally we cast from the declaring class to the "true" |
| 2072 | /// declaring class of the member. This conversion does not |
| 2073 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2074 | ExprResult |
| 2075 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2076 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2077 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2078 | NamedDecl *Member) { |
| 2079 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 2080 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2081 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2082 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2083 | QualType DestRecordType; |
| 2084 | QualType DestType; |
| 2085 | QualType FromRecordType; |
| 2086 | QualType FromType = From->getType(); |
| 2087 | bool PointerConversions = false; |
| 2088 | if (isa<FieldDecl>(Member)) { |
| 2089 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2091 | if (FromType->getAs<PointerType>()) { |
| 2092 | DestType = Context.getPointerType(DestRecordType); |
| 2093 | FromRecordType = FromType->getPointeeType(); |
| 2094 | PointerConversions = true; |
| 2095 | } else { |
| 2096 | DestType = DestRecordType; |
| 2097 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2098 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2099 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 2100 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2101 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2102 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2103 | DestType = Method->getThisType(Context); |
| 2104 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2105 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2106 | if (FromType->getAs<PointerType>()) { |
| 2107 | FromRecordType = FromType->getPointeeType(); |
| 2108 | PointerConversions = true; |
| 2109 | } else { |
| 2110 | FromRecordType = FromType; |
| 2111 | DestType = DestRecordType; |
| 2112 | } |
| 2113 | } else { |
| 2114 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2115 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2116 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2117 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2118 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2119 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2120 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2121 | // If the unqualified types are the same, no conversion is necessary. |
| 2122 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2123 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2124 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2125 | SourceRange FromRange = From->getSourceRange(); |
| 2126 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2127 | |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 2128 | ExprValueKind VK = From->getValueKind(); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2129 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2130 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2131 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2132 | // class name. |
| 2133 | // |
| 2134 | // If the member was a qualified name and the qualified referred to a |
| 2135 | // specific base subobject type, we'll cast to that intermediate type |
| 2136 | // first and then to the object in which the member is declared. That allows |
| 2137 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2138 | // |
| 2139 | // class Base { public: int x; }; |
| 2140 | // class Derived1 : public Base { }; |
| 2141 | // class Derived2 : public Base { }; |
| 2142 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2143 | // |
| 2144 | // void VeryDerived::f() { |
| 2145 | // x = 17; // error: ambiguous base subobjects |
| 2146 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2147 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2148 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2149 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2150 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2151 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2152 | |
| 2153 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2154 | |
| 2155 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2156 | // type of the object type, in which case we just ignore it. |
| 2157 | // Otherwise build the appropriate casts. |
| 2158 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2159 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2160 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2161 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2162 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2163 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2164 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2165 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2166 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2167 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2168 | |
| 2169 | FromType = QType; |
| 2170 | FromRecordType = QRecordType; |
| 2171 | |
| 2172 | // If the qualifier type was the same as the destination type, |
| 2173 | // we're done. |
| 2174 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2175 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2176 | } |
| 2177 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2178 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2179 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2180 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2181 | // If we actually found the member through a using declaration, cast |
| 2182 | // down to the using declaration's type. |
| 2183 | // |
| 2184 | // Pointer equality is fine here because only one declaration of a |
| 2185 | // class ever has member declarations. |
| 2186 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2187 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2188 | QualType URecordType = Context.getTypeDeclType( |
| 2189 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2190 | |
| 2191 | // We only need to do this if the naming-class to declaring-class |
| 2192 | // conversion is non-trivial. |
| 2193 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2194 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2195 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2196 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2197 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2198 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2199 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2200 | QualType UType = URecordType; |
| 2201 | if (PointerConversions) |
| 2202 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2203 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2204 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2205 | FromType = UType; |
| 2206 | FromRecordType = URecordType; |
| 2207 | } |
| 2208 | |
| 2209 | // We don't do access control for the conversion from the |
| 2210 | // declaring class to the true declaring class. |
| 2211 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2212 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2213 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2214 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2215 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2216 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2217 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2218 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2219 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2220 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2221 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2222 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2223 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2224 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2225 | const LookupResult &R, |
| 2226 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2227 | // Only when used directly as the postfix-expression of a call. |
| 2228 | if (!HasTrailingLParen) |
| 2229 | return false; |
| 2230 | |
| 2231 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2232 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2233 | return false; |
| 2234 | |
| 2235 | // Only in C++ or ObjC++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2236 | if (!getLangOpts().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2237 | return false; |
| 2238 | |
| 2239 | // Turn off ADL when we find certain kinds of declarations during |
| 2240 | // normal lookup: |
| 2241 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2242 | NamedDecl *D = *I; |
| 2243 | |
| 2244 | // C++0x [basic.lookup.argdep]p3: |
| 2245 | // -- a declaration of a class member |
| 2246 | // Since using decls preserve this property, we check this on the |
| 2247 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2248 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2249 | return false; |
| 2250 | |
| 2251 | // C++0x [basic.lookup.argdep]p3: |
| 2252 | // -- a block-scope function declaration that is not a |
| 2253 | // using-declaration |
| 2254 | // NOTE: we also trigger this for function templates (in fact, we |
| 2255 | // don't check the decl type at all, since all other decl types |
| 2256 | // turn off ADL anyway). |
| 2257 | if (isa<UsingShadowDecl>(D)) |
| 2258 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2259 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2260 | return false; |
| 2261 | |
| 2262 | // C++0x [basic.lookup.argdep]p3: |
| 2263 | // -- a declaration that is neither a function or a function |
| 2264 | // template |
| 2265 | // And also for builtin functions. |
| 2266 | if (isa<FunctionDecl>(D)) { |
| 2267 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2268 | |
| 2269 | // But also builtin functions. |
| 2270 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2271 | return false; |
| 2272 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2273 | return false; |
| 2274 | } |
| 2275 | |
| 2276 | return true; |
| 2277 | } |
| 2278 | |
| 2279 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2280 | /// Diagnoses obvious problems with the use of the given declaration |
| 2281 | /// as an expression. This is only actually called for lookups that |
| 2282 | /// were not overloaded, and it doesn't promise that the declaration |
| 2283 | /// will in fact be used. |
| 2284 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2285 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2286 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2287 | return true; |
| 2288 | } |
| 2289 | |
| 2290 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2291 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2292 | return true; |
| 2293 | } |
| 2294 | |
| 2295 | if (isa<NamespaceDecl>(D)) { |
| 2296 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2297 | return true; |
| 2298 | } |
| 2299 | |
| 2300 | return false; |
| 2301 | } |
| 2302 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2303 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2304 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2305 | LookupResult &R, |
| 2306 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2307 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2308 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2309 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2310 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2311 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2312 | |
| 2313 | // We only need to check the declaration if there's exactly one |
| 2314 | // result, because in the overloaded case the results can only be |
| 2315 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2316 | if (R.isSingleResult() && |
| 2317 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2318 | return ExprError(); |
| 2319 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2320 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2321 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2322 | // we've picked a target. |
| 2323 | R.suppressDiagnostics(); |
| 2324 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2325 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2326 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2327 | SS.getWithLocInContext(Context), |
| 2328 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2329 | NeedsADL, R.isOverloadedResult(), |
| 2330 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2331 | |
| 2332 | return Owned(ULE); |
| 2333 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2334 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2335 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2336 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2337 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2338 | const DeclarationNameInfo &NameInfo, |
| 2339 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2340 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2341 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2342 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2343 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2344 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2345 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2346 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2347 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2348 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2349 | // Specifically diagnose references to class templates that are missing |
| 2350 | // a template argument list. |
| 2351 | Diag(Loc, diag::err_template_decl_ref) |
| 2352 | << Template << SS.getRange(); |
| 2353 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2354 | return ExprError(); |
| 2355 | } |
| 2356 | |
| 2357 | // Make sure that we're referring to a value. |
| 2358 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2359 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2360 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2361 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2362 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2363 | return ExprError(); |
| 2364 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2365 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2366 | // Check whether this declaration can be used. Note that we suppress |
| 2367 | // this check when we're going to perform argument-dependent lookup |
| 2368 | // on this function name, because this might not be the function |
| 2369 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2370 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2371 | return ExprError(); |
| 2372 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2373 | // Only create DeclRefExpr's for valid Decl's. |
| 2374 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2375 | return ExprError(); |
| 2376 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2377 | // Handle members of anonymous structs and unions. If we got here, |
| 2378 | // and the reference is to a class member indirect field, then this |
| 2379 | // must be the subject of a pointer-to-member expression. |
| 2380 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2381 | if (!indirectField->isCXXClassMember()) |
| 2382 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2383 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2384 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2385 | { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2386 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2387 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2388 | |
| 2389 | switch (D->getKind()) { |
| 2390 | // Ignore all the non-ValueDecl kinds. |
| 2391 | #define ABSTRACT_DECL(kind) |
| 2392 | #define VALUE(type, base) |
| 2393 | #define DECL(type, base) \ |
| 2394 | case Decl::type: |
| 2395 | #include "clang/AST/DeclNodes.inc" |
| 2396 | llvm_unreachable("invalid value decl kind"); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2397 | |
| 2398 | // These shouldn't make it here. |
| 2399 | case Decl::ObjCAtDefsField: |
| 2400 | case Decl::ObjCIvar: |
| 2401 | llvm_unreachable("forming non-member reference to ivar?"); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2402 | |
| 2403 | // Enum constants are always r-values and never references. |
| 2404 | // Unresolved using declarations are dependent. |
| 2405 | case Decl::EnumConstant: |
| 2406 | case Decl::UnresolvedUsingValue: |
| 2407 | valueKind = VK_RValue; |
| 2408 | break; |
| 2409 | |
| 2410 | // Fields and indirect fields that got here must be for |
| 2411 | // pointer-to-member expressions; we just call them l-values for |
| 2412 | // internal consistency, because this subexpression doesn't really |
| 2413 | // exist in the high-level semantics. |
| 2414 | case Decl::Field: |
| 2415 | case Decl::IndirectField: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2416 | assert(getLangOpts().CPlusPlus && |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2417 | "building reference to field in C?"); |
| 2418 | |
| 2419 | // These can't have reference type in well-formed programs, but |
| 2420 | // for internal consistency we do this anyway. |
| 2421 | type = type.getNonReferenceType(); |
| 2422 | valueKind = VK_LValue; |
| 2423 | break; |
| 2424 | |
| 2425 | // Non-type template parameters are either l-values or r-values |
| 2426 | // depending on the type. |
| 2427 | case Decl::NonTypeTemplateParm: { |
| 2428 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2429 | type = reftype->getPointeeType(); |
| 2430 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2431 | break; |
| 2432 | } |
| 2433 | |
| 2434 | // For non-references, we need to strip qualifiers just in case |
| 2435 | // the template parameter was declared as 'const int' or whatever. |
| 2436 | valueKind = VK_RValue; |
| 2437 | type = type.getUnqualifiedType(); |
| 2438 | break; |
| 2439 | } |
| 2440 | |
| 2441 | case Decl::Var: |
| 2442 | // In C, "extern void blah;" is valid and is an r-value. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2443 | if (!getLangOpts().CPlusPlus && |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2444 | !type.hasQualifiers() && |
| 2445 | type->isVoidType()) { |
| 2446 | valueKind = VK_RValue; |
| 2447 | break; |
| 2448 | } |
| 2449 | // fallthrough |
| 2450 | |
| 2451 | case Decl::ImplicitParam: |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2452 | case Decl::ParmVar: { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2453 | // These are always l-values. |
| 2454 | valueKind = VK_LValue; |
| 2455 | type = type.getNonReferenceType(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2456 | |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2457 | // FIXME: Does the addition of const really only apply in |
| 2458 | // potentially-evaluated contexts? Since the variable isn't actually |
| 2459 | // captured in an unevaluated context, it seems that the answer is no. |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 2460 | if (!isUnevaluatedContext()) { |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2461 | QualType CapturedType = getCapturedDeclRefType(cast<VarDecl>(VD), Loc); |
| 2462 | if (!CapturedType.isNull()) |
| 2463 | type = CapturedType; |
| 2464 | } |
| 2465 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2466 | break; |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2469 | case Decl::Function: { |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 2470 | if (unsigned BID = cast<FunctionDecl>(VD)->getBuiltinID()) { |
| 2471 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BID)) { |
| 2472 | type = Context.BuiltinFnTy; |
| 2473 | valueKind = VK_RValue; |
| 2474 | break; |
| 2475 | } |
| 2476 | } |
| 2477 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2478 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2479 | |
| 2480 | // If we're referring to a function with an __unknown_anytype |
| 2481 | // result type, make the entire expression __unknown_anytype. |
| 2482 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2483 | type = Context.UnknownAnyTy; |
| 2484 | valueKind = VK_RValue; |
| 2485 | break; |
| 2486 | } |
| 2487 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2488 | // Functions are l-values in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2489 | if (getLangOpts().CPlusPlus) { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2490 | valueKind = VK_LValue; |
| 2491 | break; |
| 2492 | } |
| 2493 | |
| 2494 | // C99 DR 316 says that, if a function type comes from a |
| 2495 | // function definition (without a prototype), that type is only |
| 2496 | // used for checking compatibility. Therefore, when referencing |
| 2497 | // the function, we pretend that we don't have the full function |
| 2498 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2499 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2500 | isa<FunctionProtoType>(fty)) |
| 2501 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2502 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2503 | |
| 2504 | // Functions are r-values in C. |
| 2505 | valueKind = VK_RValue; |
| 2506 | break; |
| 2507 | } |
| 2508 | |
| 2509 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2510 | // If we're referring to a method with an __unknown_anytype |
| 2511 | // result type, make the entire expression __unknown_anytype. |
| 2512 | // This should only be possible with a type written directly. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 2513 | if (const FunctionProtoType *proto |
| 2514 | = dyn_cast<FunctionProtoType>(VD->getType())) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2515 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2516 | type = Context.UnknownAnyTy; |
| 2517 | valueKind = VK_RValue; |
| 2518 | break; |
| 2519 | } |
| 2520 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2521 | // C++ methods are l-values if static, r-values if non-static. |
| 2522 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2523 | valueKind = VK_LValue; |
| 2524 | break; |
| 2525 | } |
| 2526 | // fallthrough |
| 2527 | |
| 2528 | case Decl::CXXConversion: |
| 2529 | case Decl::CXXDestructor: |
| 2530 | case Decl::CXXConstructor: |
| 2531 | valueKind = VK_RValue; |
| 2532 | break; |
| 2533 | } |
| 2534 | |
| 2535 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2536 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2539 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2540 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2541 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2542 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2543 | default: llvm_unreachable("Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2544 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2545 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
Nico Weber | 28ad063 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 2546 | case tok::kw_L__FUNCTION__: IT = PredefinedExpr::LFunction; break; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2547 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2548 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2549 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2550 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2551 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2552 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2553 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2554 | if (!currentDecl && getCurBlock()) |
| 2555 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2556 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2557 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2558 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2559 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2560 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2561 | QualType ResTy; |
| 2562 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2563 | ResTy = Context.DependentTy; |
| 2564 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2565 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2566 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2567 | llvm::APInt LengthI(32, Length + 1); |
Nico Weber | d68615f | 2012-06-29 16:39:58 +0000 | [diff] [blame] | 2568 | if (IT == PredefinedExpr::LFunction) |
Nico Weber | 28ad063 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 2569 | ResTy = Context.WCharTy.withConst(); |
| 2570 | else |
| 2571 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2572 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2573 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2574 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2577 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2578 | SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2579 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2580 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2581 | if (Invalid) |
| 2582 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2583 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2584 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2585 | PP, Tok.getKind()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2586 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2587 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2588 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2589 | QualType Ty; |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2590 | if (Literal.isWide()) |
| 2591 | Ty = Context.WCharTy; // L'x' -> wchar_t in C and C++. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2592 | else if (Literal.isUTF16()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2593 | Ty = Context.Char16Ty; // u'x' -> char16_t in C11 and C++11. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2594 | else if (Literal.isUTF32()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2595 | Ty = Context.Char32Ty; // U'x' -> char32_t in C11 and C++11. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2596 | else if (!getLangOpts().CPlusPlus || Literal.isMultiChar()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2597 | Ty = Context.IntTy; // 'x' -> int in C, 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2598 | else |
| 2599 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2600 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2601 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
| 2602 | if (Literal.isWide()) |
| 2603 | Kind = CharacterLiteral::Wide; |
| 2604 | else if (Literal.isUTF16()) |
| 2605 | Kind = CharacterLiteral::UTF16; |
| 2606 | else if (Literal.isUTF32()) |
| 2607 | Kind = CharacterLiteral::UTF32; |
| 2608 | |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 2609 | Expr *Lit = new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
| 2610 | Tok.getLocation()); |
| 2611 | |
| 2612 | if (Literal.getUDSuffix().empty()) |
| 2613 | return Owned(Lit); |
| 2614 | |
| 2615 | // We're building a user-defined literal. |
| 2616 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
| 2617 | SourceLocation UDSuffixLoc = |
| 2618 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); |
| 2619 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2620 | // Make sure we're allowed user-defined literals here. |
| 2621 | if (!UDLScope) |
| 2622 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_character_udl)); |
| 2623 | |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 2624 | // C++11 [lex.ext]p6: The literal L is treated as a call of the form |
| 2625 | // operator "" X (ch) |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2626 | return BuildCookedLiteralOperatorCall(*this, UDLScope, UDSuffix, UDSuffixLoc, |
| 2627 | llvm::makeArrayRef(&Lit, 1), |
| 2628 | Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2631 | ExprResult Sema::ActOnIntegerConstant(SourceLocation Loc, uint64_t Val) { |
| 2632 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
| 2633 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val), |
| 2634 | Context.IntTy, Loc)); |
| 2635 | } |
| 2636 | |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2637 | static Expr *BuildFloatingLiteral(Sema &S, NumericLiteralParser &Literal, |
| 2638 | QualType Ty, SourceLocation Loc) { |
| 2639 | const llvm::fltSemantics &Format = S.Context.getFloatTypeSemantics(Ty); |
| 2640 | |
| 2641 | using llvm::APFloat; |
| 2642 | APFloat Val(Format); |
| 2643 | |
| 2644 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
| 2645 | |
| 2646 | // Overflow is always an error, but underflow is only an error if |
| 2647 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2648 | if ((result & APFloat::opOverflow) || |
| 2649 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
| 2650 | unsigned diagnostic; |
| 2651 | SmallString<20> buffer; |
| 2652 | if (result & APFloat::opOverflow) { |
| 2653 | diagnostic = diag::warn_float_overflow; |
| 2654 | APFloat::getLargest(Format).toString(buffer); |
| 2655 | } else { |
| 2656 | diagnostic = diag::warn_float_underflow; |
| 2657 | APFloat::getSmallest(Format).toString(buffer); |
| 2658 | } |
| 2659 | |
| 2660 | S.Diag(Loc, diagnostic) |
| 2661 | << Ty |
| 2662 | << StringRef(buffer.data(), buffer.size()); |
| 2663 | } |
| 2664 | |
| 2665 | bool isExact = (result == APFloat::opOK); |
| 2666 | return FloatingLiteral::Create(S.Context, Val, isExact, Ty, Loc); |
| 2667 | } |
| 2668 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2669 | ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2670 | // 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] | 2671 | // cannot have a trigraph, escaped newline, radix prefix, or suffix. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2672 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2673 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2674 | return ActOnIntegerConstant(Tok.getLocation(), Val-'0'); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2675 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2676 | |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2677 | SmallString<128> SpellingBuffer; |
| 2678 | // NumericLiteralParser wants to overread by one character. Add padding to |
| 2679 | // the buffer in case the token is copied to the buffer. If getSpelling() |
| 2680 | // returns a StringRef to the memory buffer, it should have a null char at |
| 2681 | // the EOF, so it is also safe. |
| 2682 | SpellingBuffer.resize(Tok.getLength() + 1); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2684 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2685 | bool Invalid = false; |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2686 | StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2687 | if (Invalid) |
| 2688 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2689 | |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2690 | NumericLiteralParser Literal(TokSpelling, Tok.getLocation(), PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2691 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2692 | return ExprError(); |
| 2693 | |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2694 | if (Literal.hasUDSuffix()) { |
| 2695 | // We're building a user-defined literal. |
| 2696 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
| 2697 | SourceLocation UDSuffixLoc = |
| 2698 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); |
| 2699 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2700 | // Make sure we're allowed user-defined literals here. |
| 2701 | if (!UDLScope) |
| 2702 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_numeric_udl)); |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2703 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2704 | QualType CookedTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2705 | if (Literal.isFloatingLiteral()) { |
| 2706 | // C++11 [lex.ext]p4: If S contains a literal operator with parameter type |
| 2707 | // long double, the literal is treated as a call of the form |
| 2708 | // operator "" X (f L) |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2709 | CookedTy = Context.LongDoubleTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2710 | } else { |
| 2711 | // C++11 [lex.ext]p3: If S contains a literal operator with parameter type |
| 2712 | // unsigned long long, the literal is treated as a call of the form |
| 2713 | // operator "" X (n ULL) |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2714 | CookedTy = Context.UnsignedLongLongTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2717 | DeclarationName OpName = |
| 2718 | Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); |
| 2719 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); |
| 2720 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); |
| 2721 | |
| 2722 | // Perform literal operator lookup to determine if we're building a raw |
| 2723 | // literal or a cooked one. |
| 2724 | LookupResult R(*this, OpName, UDSuffixLoc, LookupOrdinaryName); |
| 2725 | switch (LookupLiteralOperator(UDLScope, R, llvm::makeArrayRef(&CookedTy, 1), |
| 2726 | /*AllowRawAndTemplate*/true)) { |
| 2727 | case LOLR_Error: |
| 2728 | return ExprError(); |
| 2729 | |
| 2730 | case LOLR_Cooked: { |
| 2731 | Expr *Lit; |
| 2732 | if (Literal.isFloatingLiteral()) { |
| 2733 | Lit = BuildFloatingLiteral(*this, Literal, CookedTy, Tok.getLocation()); |
| 2734 | } else { |
| 2735 | llvm::APInt ResultVal(Context.getTargetInfo().getLongLongWidth(), 0); |
| 2736 | if (Literal.GetIntegerValue(ResultVal)) |
| 2737 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
| 2738 | Lit = IntegerLiteral::Create(Context, ResultVal, CookedTy, |
| 2739 | Tok.getLocation()); |
| 2740 | } |
| 2741 | return BuildLiteralOperatorCall(R, OpNameInfo, |
| 2742 | llvm::makeArrayRef(&Lit, 1), |
| 2743 | Tok.getLocation()); |
| 2744 | } |
| 2745 | |
| 2746 | case LOLR_Raw: { |
| 2747 | // C++11 [lit.ext]p3, p4: If S contains a raw literal operator, the |
| 2748 | // literal is treated as a call of the form |
| 2749 | // operator "" X ("n") |
| 2750 | SourceLocation TokLoc = Tok.getLocation(); |
| 2751 | unsigned Length = Literal.getUDSuffixOffset(); |
| 2752 | QualType StrTy = Context.getConstantArrayType( |
| 2753 | Context.CharTy, llvm::APInt(32, Length + 1), |
| 2754 | ArrayType::Normal, 0); |
| 2755 | Expr *Lit = StringLiteral::Create( |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2756 | Context, StringRef(TokSpelling.data(), Length), StringLiteral::Ascii, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2757 | /*Pascal*/false, StrTy, &TokLoc, 1); |
| 2758 | return BuildLiteralOperatorCall(R, OpNameInfo, |
| 2759 | llvm::makeArrayRef(&Lit, 1), TokLoc); |
| 2760 | } |
| 2761 | |
| 2762 | case LOLR_Template: |
| 2763 | // C++11 [lit.ext]p3, p4: Otherwise (S contains a literal operator |
| 2764 | // template), L is treated as a call fo the form |
| 2765 | // operator "" X <'c1', 'c2', ... 'ck'>() |
| 2766 | // where n is the source character sequence c1 c2 ... ck. |
| 2767 | TemplateArgumentListInfo ExplicitArgs; |
| 2768 | unsigned CharBits = Context.getIntWidth(Context.CharTy); |
| 2769 | bool CharIsUnsigned = Context.CharTy->isUnsignedIntegerType(); |
| 2770 | llvm::APSInt Value(CharBits, CharIsUnsigned); |
| 2771 | for (unsigned I = 0, N = Literal.getUDSuffixOffset(); I != N; ++I) { |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2772 | Value = TokSpelling[I]; |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2773 | TemplateArgument Arg(Context, Value, Context.CharTy); |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2774 | TemplateArgumentLocInfo ArgInfo; |
| 2775 | ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); |
| 2776 | } |
| 2777 | return BuildLiteralOperatorCall(R, OpNameInfo, ArrayRef<Expr*>(), |
| 2778 | Tok.getLocation(), &ExplicitArgs); |
| 2779 | } |
| 2780 | |
| 2781 | llvm_unreachable("unexpected literal operator lookup result"); |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2784 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2785 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2786 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2787 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2788 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2789 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2790 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2791 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2792 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2793 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2794 | |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2795 | Res = BuildFloatingLiteral(*this, Literal, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2796 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2797 | if (Ty == Context.DoubleTy) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2798 | if (getLangOpts().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2799 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2800 | } else if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2801 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2802 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2803 | } |
| 2804 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2805 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2806 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2807 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2808 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2809 | |
Dmitri Gribenko | e3b136b | 2012-09-24 18:19:21 +0000 | [diff] [blame] | 2810 | // 'long long' is a C99 or C++11 feature. |
| 2811 | if (!getLangOpts().C99 && Literal.isLongLong) { |
| 2812 | if (getLangOpts().CPlusPlus) |
| 2813 | Diag(Tok.getLocation(), |
| 2814 | getLangOpts().CPlusPlus0x ? |
| 2815 | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong); |
| 2816 | else |
| 2817 | Diag(Tok.getLocation(), diag::ext_c99_longlong); |
| 2818 | } |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2819 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2820 | // Get the value in the widest-possible width. |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2821 | unsigned MaxWidth = Context.getTargetInfo().getIntMaxTWidth(); |
| 2822 | // The microsoft literal suffix extensions support 128-bit literals, which |
| 2823 | // may be wider than [u]intmax_t. |
| 2824 | if (Literal.isMicrosoftInteger && MaxWidth < 128) |
| 2825 | MaxWidth = 128; |
| 2826 | llvm::APInt ResultVal(MaxWidth, 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2827 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2828 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2829 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2830 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2831 | Ty = Context.UnsignedLongLongTy; |
| 2832 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2833 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2834 | } else { |
| 2835 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2836 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2837 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2838 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2839 | // be an unsigned int. |
| 2840 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2841 | |
| 2842 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2843 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2844 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2845 | // Are int/unsigned possibilities? |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2846 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2847 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2848 | // Does it fit in a unsigned int? |
| 2849 | if (ResultVal.isIntN(IntSize)) { |
| 2850 | // Does it fit in a signed int? |
| 2851 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2852 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2853 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2854 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2855 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2856 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2857 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2858 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2859 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2860 | if (Ty.isNull() && !Literal.isLongLong) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2861 | unsigned LongSize = Context.getTargetInfo().getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2862 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2863 | // Does it fit in a unsigned long? |
| 2864 | if (ResultVal.isIntN(LongSize)) { |
| 2865 | // Does it fit in a signed long? |
| 2866 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2867 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2868 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2869 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2870 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2871 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2874 | // Check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2875 | if (Ty.isNull()) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2876 | unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2877 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2878 | // Does it fit in a unsigned long long? |
| 2879 | if (ResultVal.isIntN(LongLongSize)) { |
| 2880 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2881 | // To be compatible with MSVC, hex integer literals ending with the |
| 2882 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2883 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2884 | (getLangOpts().MicrosoftExt && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2885 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2886 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2887 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2888 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2889 | } |
| 2890 | } |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2891 | |
| 2892 | // If it doesn't fit in unsigned long long, and we're using Microsoft |
| 2893 | // extensions, then its a 128-bit integer literal. |
| 2894 | if (Ty.isNull() && Literal.isMicrosoftInteger) { |
| 2895 | if (Literal.isUnsigned) |
| 2896 | Ty = Context.UnsignedInt128Ty; |
| 2897 | else |
| 2898 | Ty = Context.Int128Ty; |
| 2899 | Width = 128; |
| 2900 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2901 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2902 | // If we still couldn't decide a type, we probably have something that |
| 2903 | // 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] | 2904 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2905 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2906 | Ty = Context.UnsignedLongLongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2907 | Width = Context.getTargetInfo().getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2908 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2909 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2910 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2911 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2912 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2913 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2914 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2915 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2916 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2917 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2918 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2919 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2920 | |
| 2921 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2924 | ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2925 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2926 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2929 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 2930 | SourceLocation Loc, |
| 2931 | SourceRange ArgRange) { |
| 2932 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2933 | // scalar or vector data type argument..." |
| 2934 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2935 | // type (C99 6.2.5p18) or void. |
| 2936 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 2937 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 2938 | << T << ArgRange; |
| 2939 | return true; |
| 2940 | } |
| 2941 | |
| 2942 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 2943 | "Scalar types should always be complete"); |
| 2944 | return false; |
| 2945 | } |
| 2946 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2947 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 2948 | SourceLocation Loc, |
| 2949 | SourceRange ArgRange, |
| 2950 | UnaryExprOrTypeTrait TraitKind) { |
| 2951 | // C99 6.5.3.4p1: |
| 2952 | if (T->isFunctionType()) { |
| 2953 | // alignof(function) is allowed as an extension. |
| 2954 | if (TraitKind == UETT_SizeOf) |
| 2955 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 2956 | return false; |
| 2957 | } |
| 2958 | |
| 2959 | // Allow sizeof(void)/alignof(void) as an extension. |
| 2960 | if (T->isVoidType()) { |
| 2961 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 2962 | return false; |
| 2963 | } |
| 2964 | |
| 2965 | return true; |
| 2966 | } |
| 2967 | |
| 2968 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 2969 | SourceLocation Loc, |
| 2970 | SourceRange ArgRange, |
| 2971 | UnaryExprOrTypeTrait TraitKind) { |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 2972 | // Reject sizeof(interface) and sizeof(interface<proto>) if the |
| 2973 | // runtime doesn't allow it. |
| 2974 | if (!S.LangOpts.ObjCRuntime.allowsSizeofAlignof() && T->isObjCObjectType()) { |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2975 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 2976 | << T << (TraitKind == UETT_SizeOf) |
| 2977 | << ArgRange; |
| 2978 | return true; |
| 2979 | } |
| 2980 | |
| 2981 | return false; |
| 2982 | } |
| 2983 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2984 | /// \brief Check the constrains on expression operands to unary type expression |
| 2985 | /// and type traits. |
| 2986 | /// |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2987 | /// Completes any types necessary and validates the constraints on the operand |
| 2988 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 2989 | /// the expression as it completes the type for that expression through template |
| 2990 | /// instantiation, etc. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2991 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2992 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2993 | QualType ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2994 | |
| 2995 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2996 | // the result is the size of the referenced type." |
| 2997 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2998 | // result shall be the alignment of the referenced type." |
| 2999 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3000 | ExprTy = Ref->getPointeeType(); |
| 3001 | |
| 3002 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3003 | return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 3004 | E->getSourceRange()); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3005 | |
| 3006 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3007 | if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 3008 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3009 | return false; |
| 3010 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3011 | if (RequireCompleteExprType(E, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3012 | diag::err_sizeof_alignof_incomplete_type, |
| 3013 | ExprKind, E->getSourceRange())) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3014 | return true; |
| 3015 | |
| 3016 | // Completeing the expression's type may have changed it. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3017 | ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3018 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3019 | ExprTy = Ref->getPointeeType(); |
| 3020 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3021 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), |
| 3022 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3023 | return true; |
| 3024 | |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3025 | if (ExprKind == UETT_SizeOf) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3026 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3027 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 3028 | QualType OType = PVD->getOriginalType(); |
| 3029 | QualType Type = PVD->getType(); |
| 3030 | if (Type->isPointerType() && OType->isArrayType()) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3031 | Diag(E->getExprLoc(), diag::warn_sizeof_array_param) |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3032 | << Type << OType; |
| 3033 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 3034 | } |
| 3035 | } |
| 3036 | } |
| 3037 | } |
| 3038 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3039 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | /// \brief Check the constraints on operands to unary expression and type |
| 3043 | /// traits. |
| 3044 | /// |
| 3045 | /// This will complete any types necessary, and validate the various constraints |
| 3046 | /// on those operands. |
| 3047 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3048 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3049 | /// C99 6.3.2.1p[2-4] all state: |
| 3050 | /// Except when it is the operand of the sizeof operator ... |
| 3051 | /// |
| 3052 | /// C++ [expr.sizeof]p4 |
| 3053 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 3054 | /// standard conversions are not applied to the operand of sizeof. |
| 3055 | /// |
| 3056 | /// This policy is followed for all of the unary trait expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3057 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3058 | SourceLocation OpLoc, |
| 3059 | SourceRange ExprRange, |
| 3060 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3061 | if (ExprType->isDependentType()) |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3062 | return false; |
| 3063 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3064 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3065 | // the result is the size of the referenced type." |
| 3066 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3067 | // result shall be the alignment of the referenced type." |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3068 | if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) |
| 3069 | ExprType = Ref->getPointeeType(); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3070 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3071 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3072 | return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3073 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3074 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3075 | if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3076 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3077 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3079 | if (RequireCompleteType(OpLoc, ExprType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3080 | diag::err_sizeof_alignof_incomplete_type, |
| 3081 | ExprKind, ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3082 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3084 | if (CheckObjCTraitOperandConstraints(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3085 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3086 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3088 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3091 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3092 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3093 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3095 | if (isa<DeclRefExpr>(E)) |
| 3096 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3097 | |
| 3098 | // Cannot know anything else if the expression is dependent. |
| 3099 | if (E->isTypeDependent()) |
| 3100 | return false; |
| 3101 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3102 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3103 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 3104 | << 1 << E->getSourceRange(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3105 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3106 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3107 | |
| 3108 | // Alignment of a field access is always okay, so long as it isn't a |
| 3109 | // bit-field. |
| 3110 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3111 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3112 | return false; |
| 3113 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3114 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3117 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3118 | E = E->IgnoreParens(); |
| 3119 | |
| 3120 | // Cannot know anything else if the expression is dependent. |
| 3121 | if (E->isTypeDependent()) |
| 3122 | return false; |
| 3123 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3124 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3127 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3128 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3129 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 3130 | SourceLocation OpLoc, |
| 3131 | UnaryExprOrTypeTrait ExprKind, |
| 3132 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3133 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3134 | return ExprError(); |
| 3135 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3136 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 3137 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3138 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3139 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3140 | return ExprError(); |
| 3141 | |
| 3142 | // 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] | 3143 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 3144 | Context.getSizeType(), |
| 3145 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | /// \brief Build a sizeof or alignof expression given an expression |
| 3149 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3150 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3151 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 3152 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 4f0845e | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 3153 | ExprResult PE = CheckPlaceholderExpr(E); |
| 3154 | if (PE.isInvalid()) |
| 3155 | return ExprError(); |
| 3156 | |
| 3157 | E = PE.get(); |
| 3158 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3159 | // Verify that the operand is valid. |
| 3160 | bool isInvalid = false; |
| 3161 | if (E->isTypeDependent()) { |
| 3162 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3163 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3164 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3165 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3166 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3167 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3168 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3169 | isInvalid = true; |
| 3170 | } else { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3171 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | if (isInvalid) |
| 3175 | return ExprError(); |
| 3176 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 3177 | if (ExprKind == UETT_SizeOf && E->getType()->isVariableArrayType()) { |
| 3178 | PE = TranformToPotentiallyEvaluated(E); |
| 3179 | if (PE.isInvalid()) return ExprError(); |
| 3180 | E = PE.take(); |
| 3181 | } |
| 3182 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3183 | // 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] | 3184 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3185 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3186 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3189 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 3190 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3191 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3192 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3193 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3194 | UnaryExprOrTypeTrait ExprKind, bool IsType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3195 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3196 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3197 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3198 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3199 | if (IsType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3200 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3201 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3202 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3203 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3204 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3205 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3206 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3207 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3210 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3211 | bool IsReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3212 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3213 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3214 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3215 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3216 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 3217 | V = S.DefaultLvalueConversion(V.take()); |
| 3218 | if (V.isInvalid()) |
| 3219 | return QualType(); |
| 3220 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3221 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3222 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3223 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3224 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3225 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3226 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3227 | if (V.get()->getType()->isArithmeticType()) |
| 3228 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3229 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3230 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3231 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3232 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3233 | if (PR.get() != V.get()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3234 | V = PR; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3235 | return CheckRealImagOperand(S, V, Loc, IsReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3238 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3239 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3240 | << (IsReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3241 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3245 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3246 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3247 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3248 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3249 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3250 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3251 | default: llvm_unreachable("Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3252 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3253 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3254 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3255 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3256 | // Since this might is a postfix expression, get rid of ParenListExprs. |
| 3257 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Input); |
| 3258 | if (Result.isInvalid()) return ExprError(); |
| 3259 | Input = Result.take(); |
| 3260 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3261 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3264 | /// \brief Diagnose if arithmetic on the given ObjC pointer is illegal. |
| 3265 | /// |
| 3266 | /// \return true on error |
| 3267 | static bool checkArithmeticOnObjCPointer(Sema &S, |
| 3268 | SourceLocation opLoc, |
| 3269 | Expr *op) { |
| 3270 | assert(op->getType()->isObjCObjectPointerType()); |
| 3271 | if (S.LangOpts.ObjCRuntime.allowsPointerArithmetic()) |
| 3272 | return false; |
| 3273 | |
| 3274 | S.Diag(opLoc, diag::err_arithmetic_nonfragile_interface) |
| 3275 | << op->getType()->castAs<ObjCObjectPointerType>()->getPointeeType() |
| 3276 | << op->getSourceRange(); |
| 3277 | return true; |
| 3278 | } |
| 3279 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3280 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3281 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3282 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3283 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3284 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3285 | if (Result.isInvalid()) return ExprError(); |
| 3286 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3287 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3288 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3290 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3291 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3292 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3293 | Context.DependentTy, |
| 3294 | VK_LValue, OK_Ordinary, |
| 3295 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3298 | if (getLangOpts().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3299 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3300 | LHSExp->getType()->isEnumeralType() || |
| 3301 | RHSExp->getType()->isRecordType() || |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3302 | RHSExp->getType()->isEnumeralType()) && |
| 3303 | !LHSExp->getType()->isObjCObjectPointerType()) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3304 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3307 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3310 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3311 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3312 | Expr *Idx, SourceLocation RLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3313 | Expr *LHSExp = Base; |
| 3314 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3315 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3316 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3317 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3318 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3319 | if (Result.isInvalid()) |
| 3320 | return ExprError(); |
| 3321 | LHSExp = Result.take(); |
| 3322 | } |
| 3323 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3324 | if (Result.isInvalid()) |
| 3325 | return ExprError(); |
| 3326 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3327 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3328 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3329 | ExprValueKind VK = VK_LValue; |
| 3330 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3331 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3332 | // 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] | 3333 | // 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] | 3334 | // 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] | 3335 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3336 | Expr *BaseExpr, *IndexExpr; |
| 3337 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3338 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3339 | BaseExpr = LHSExp; |
| 3340 | IndexExpr = RHSExp; |
| 3341 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3342 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3343 | BaseExpr = LHSExp; |
| 3344 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3345 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3347 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3348 | BaseExpr = LHSExp; |
| 3349 | IndexExpr = RHSExp; |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3350 | |
| 3351 | // Use custom logic if this should be the pseudo-object subscript |
| 3352 | // expression. |
| 3353 | if (!LangOpts.ObjCRuntime.isSubscriptPointerArithmetic()) |
| 3354 | return BuildObjCSubscriptExpression(RLoc, BaseExpr, IndexExpr, 0, 0); |
| 3355 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3356 | ResultType = PTy->getPointeeType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3357 | if (!LangOpts.ObjCRuntime.allowsPointerArithmetic()) { |
| 3358 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3359 | << ResultType << BaseExpr->getSourceRange(); |
| 3360 | return ExprError(); |
| 3361 | } |
Fariborz Jahanian | a78eca2 | 2012-03-28 17:56:49 +0000 | [diff] [blame] | 3362 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
| 3363 | // Handle the uncommon case of "123[Ptr]". |
| 3364 | BaseExpr = RHSExp; |
| 3365 | IndexExpr = LHSExp; |
| 3366 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3368 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3369 | // Handle the uncommon case of "123[Ptr]". |
| 3370 | BaseExpr = RHSExp; |
| 3371 | IndexExpr = LHSExp; |
| 3372 | ResultType = PTy->getPointeeType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3373 | if (!LangOpts.ObjCRuntime.allowsPointerArithmetic()) { |
| 3374 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3375 | << ResultType << BaseExpr->getSourceRange(); |
| 3376 | return ExprError(); |
| 3377 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3378 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3379 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3380 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3381 | VK = LHSExp->getValueKind(); |
| 3382 | if (VK != VK_RValue) |
| 3383 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3384 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3385 | // FIXME: need to deal with const... |
| 3386 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3387 | } else if (LHSTy->isArrayType()) { |
| 3388 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3389 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3390 | // wasn't promoted because of the C90 rule that doesn't |
| 3391 | // allow promoting non-lvalue arrays. Warn, then |
| 3392 | // force the promotion here. |
| 3393 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3394 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3395 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3396 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3397 | LHSTy = LHSExp->getType(); |
| 3398 | |
| 3399 | BaseExpr = LHSExp; |
| 3400 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3401 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3402 | } else if (RHSTy->isArrayType()) { |
| 3403 | // Same as previous, except for 123[f().a] case |
| 3404 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3405 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3406 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3407 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3408 | RHSTy = RHSExp->getType(); |
| 3409 | |
| 3410 | BaseExpr = RHSExp; |
| 3411 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3412 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3413 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3414 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3415 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3416 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3417 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3418 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3419 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3420 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3421 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3422 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3423 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3424 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3425 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3426 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3427 | // 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] | 3428 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3429 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3430 | // incomplete types are not object types. |
| 3431 | if (ResultType->isFunctionType()) { |
| 3432 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3433 | << ResultType << BaseExpr->getSourceRange(); |
| 3434 | return ExprError(); |
| 3435 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3437 | if (ResultType->isVoidType() && !getLangOpts().CPlusPlus) { |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3438 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 6628969 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3439 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
| 3440 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3441 | |
| 3442 | // C forbids expressions of unqualified void type from being l-values. |
| 3443 | // See IsCForbiddenLValueType. |
| 3444 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3445 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | RequireCompleteType(LLoc, ResultType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3447 | diag::err_subscript_incomplete_type, BaseExpr)) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3448 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3450 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3451 | !ResultType.isCForbiddenLValueType()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3452 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3453 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3454 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3457 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3458 | FunctionDecl *FD, |
| 3459 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3460 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3461 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3462 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3463 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3465 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3466 | return ExprError(); |
| 3467 | } |
| 3468 | |
| 3469 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3470 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3471 | |
Richard Smith | adb1d4c | 2012-07-22 23:45:10 +0000 | [diff] [blame] | 3472 | EnterExpressionEvaluationContext EvalContext(*this, PotentiallyEvaluated, |
| 3473 | Param); |
| 3474 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3475 | // Instantiate the expression. |
| 3476 | MultiLevelTemplateArgumentList ArgList |
| 3477 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3478 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3479 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3480 | = ArgList.getInnermost(); |
Richard Smith | 7e54fb5 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3481 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 3482 | ArrayRef<TemplateArgument>(Innermost.first, |
| 3483 | Innermost.second)); |
Richard Smith | ab91ef1 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3484 | if (Inst) |
| 3485 | return ExprError(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3486 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3487 | ExprResult Result; |
| 3488 | { |
| 3489 | // C++ [dcl.fct.default]p5: |
| 3490 | // The names in the [default argument] expression are bound, and |
| 3491 | // the semantic constraints are checked, at the point where the |
| 3492 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3493 | ContextRAII SavedContext(*this, FD); |
Douglas Gregor | 7bdc152 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 3494 | LocalInstantiationScope Local(*this); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3495 | Result = SubstExpr(UninstExpr, ArgList); |
| 3496 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3497 | if (Result.isInvalid()) |
| 3498 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3500 | // Check the expression as an initializer for the parameter. |
| 3501 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3502 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3503 | InitializationKind Kind |
| 3504 | = InitializationKind::CreateCopy(Param->getLocation(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3505 | /*FIXME:EqualLoc*/UninstExpr->getLocStart()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3506 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3507 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3508 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3509 | Result = InitSeq.Perform(*this, Entity, Kind, ResultE); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3510 | if (Result.isInvalid()) |
| 3511 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3512 | |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 3513 | Expr *Arg = Result.takeAs<Expr>(); |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 3514 | CheckImplicitConversions(Arg, Param->getOuterLocStart()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3515 | // Build the default argument expression. |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 3516 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, Arg)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3519 | // If the default expression creates temporaries, we need to |
| 3520 | // push them to the current stack of expression temporaries so they'll |
| 3521 | // be properly destroyed. |
| 3522 | // FIXME: We should really be rebuilding the default argument with new |
| 3523 | // bound temporaries; see the comment in PR5810. |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3524 | // We don't need to do that with block decls, though, because |
| 3525 | // blocks in default argument expression can never capture anything. |
| 3526 | if (isa<ExprWithCleanups>(Param->getInit())) { |
| 3527 | // Set the "needs cleanups" bit regardless of whether there are |
| 3528 | // any explicit objects. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3529 | ExprNeedsCleanups = true; |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3530 | |
| 3531 | // Append all the objects to the cleanup list. Right now, this |
| 3532 | // should always be a no-op, because blocks in default argument |
| 3533 | // expressions should never be able to capture anything. |
| 3534 | assert(!cast<ExprWithCleanups>(Param->getInit())->getNumObjects() && |
| 3535 | "default argument expression has capturing blocks?"); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3536 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3537 | |
| 3538 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3539 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3540 | // as being "referenced". |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 3541 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg(), |
| 3542 | /*SkipLocalVariables=*/true); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3543 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3546 | |
| 3547 | Sema::VariadicCallType |
| 3548 | Sema::getVariadicCallType(FunctionDecl *FDecl, const FunctionProtoType *Proto, |
| 3549 | Expr *Fn) { |
| 3550 | if (Proto && Proto->isVariadic()) { |
| 3551 | if (dyn_cast_or_null<CXXConstructorDecl>(FDecl)) |
| 3552 | return VariadicConstructor; |
| 3553 | else if (Fn && Fn->getType()->isBlockPointerType()) |
| 3554 | return VariadicBlock; |
| 3555 | else if (FDecl) { |
| 3556 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3557 | if (Method->isInstance()) |
| 3558 | return VariadicMethod; |
| 3559 | } |
| 3560 | return VariadicFunction; |
| 3561 | } |
| 3562 | return VariadicDoesNotApply; |
| 3563 | } |
| 3564 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3565 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3566 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3567 | /// function prototype Proto. Call is the call expression itself, and |
| 3568 | /// Fn is the function expression. For a C++ member function, this |
| 3569 | /// routine does not attempt to convert the object argument. Returns |
| 3570 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3571 | bool |
| 3572 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3573 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3574 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3575 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3576 | SourceLocation RParenLoc, |
| 3577 | bool IsExecConfig) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3578 | // Bail out early if calling a builtin with custom typechecking. |
| 3579 | // We don't need to do this in the |
| 3580 | if (FDecl) |
| 3581 | if (unsigned ID = FDecl->getBuiltinID()) |
| 3582 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 3583 | return false; |
| 3584 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3585 | // 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] | 3586 | // assignment, to the types of the corresponding parameter, ... |
| 3587 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3588 | bool Invalid = false; |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3589 | unsigned MinArgs = FDecl ? FDecl->getMinRequiredArguments() : NumArgsInProto; |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3590 | unsigned FnKind = Fn->getType()->isBlockPointerType() |
| 3591 | ? 1 /* block */ |
| 3592 | : (IsExecConfig ? 3 /* kernel function (exec config) */ |
| 3593 | : 0 /* function */); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3594 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3595 | // If too few arguments are available (and we don't have default |
| 3596 | // arguments for the remaining parameters), don't make the call. |
| 3597 | if (NumArgs < NumArgsInProto) { |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3598 | if (NumArgs < MinArgs) { |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 3599 | if (MinArgs == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) |
| 3600 | Diag(RParenLoc, MinArgs == NumArgsInProto && !Proto->isVariadic() |
| 3601 | ? diag::err_typecheck_call_too_few_args_one |
| 3602 | : diag::err_typecheck_call_too_few_args_at_least_one) |
| 3603 | << FnKind |
| 3604 | << FDecl->getParamDecl(0) << Fn->getSourceRange(); |
| 3605 | else |
| 3606 | Diag(RParenLoc, MinArgs == NumArgsInProto && !Proto->isVariadic() |
| 3607 | ? diag::err_typecheck_call_too_few_args |
| 3608 | : diag::err_typecheck_call_too_few_args_at_least) |
| 3609 | << FnKind |
| 3610 | << MinArgs << NumArgs << Fn->getSourceRange(); |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3611 | |
| 3612 | // Emit the location of the prototype. |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3613 | if (FDecl && !FDecl->getBuiltinID() && !IsExecConfig) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3614 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3615 | << FDecl; |
| 3616 | |
| 3617 | return true; |
| 3618 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3619 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | // If too many are passed and not variadic, error on the extras and drop |
| 3623 | // them. |
| 3624 | if (NumArgs > NumArgsInProto) { |
| 3625 | if (!Proto->isVariadic()) { |
Richard Smith | c608c3c | 2012-05-15 06:21:54 +0000 | [diff] [blame] | 3626 | if (NumArgsInProto == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) |
| 3627 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3628 | MinArgs == NumArgsInProto |
| 3629 | ? diag::err_typecheck_call_too_many_args_one |
| 3630 | : diag::err_typecheck_call_too_many_args_at_most_one) |
| 3631 | << FnKind |
| 3632 | << FDecl->getParamDecl(0) << NumArgs << Fn->getSourceRange() |
| 3633 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3634 | Args[NumArgs-1]->getLocEnd()); |
| 3635 | else |
| 3636 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3637 | MinArgs == NumArgsInProto |
| 3638 | ? diag::err_typecheck_call_too_many_args |
| 3639 | : diag::err_typecheck_call_too_many_args_at_most) |
| 3640 | << FnKind |
| 3641 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
| 3642 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3643 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3644 | |
| 3645 | // Emit the location of the prototype. |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3646 | if (FDecl && !FDecl->getBuiltinID() && !IsExecConfig) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3647 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3648 | << FDecl; |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3649 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3650 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3651 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3652 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3653 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3654 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3655 | SmallVector<Expr *, 8> AllArgs; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3656 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, Fn); |
| 3657 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3658 | Invalid = GatherArgumentsForCall(Call->getLocStart(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3659 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3660 | if (Invalid) |
| 3661 | return true; |
| 3662 | unsigned TotalNumArgs = AllArgs.size(); |
| 3663 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3664 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3665 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3666 | return false; |
| 3667 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3668 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3669 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3670 | FunctionDecl *FDecl, |
| 3671 | const FunctionProtoType *Proto, |
| 3672 | unsigned FirstProtoArg, |
| 3673 | Expr **Args, unsigned NumArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3674 | SmallVector<Expr *, 8> &AllArgs, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3675 | VariadicCallType CallType, |
| 3676 | bool AllowExplicit) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3677 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3678 | unsigned NumArgsToCheck = NumArgs; |
| 3679 | bool Invalid = false; |
| 3680 | if (NumArgs != NumArgsInProto) |
| 3681 | // Use default arguments for missing arguments |
| 3682 | NumArgsToCheck = NumArgsInProto; |
| 3683 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3684 | // 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] | 3685 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3686 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3687 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3688 | Expr *Arg; |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3689 | ParmVarDecl *Param; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3690 | if (ArgIx < NumArgs) { |
| 3691 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3692 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3693 | if (RequireCompleteType(Arg->getLocStart(), |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3694 | ProtoArgType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3695 | diag::err_call_incomplete_argument, Arg)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3696 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3697 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3698 | // Pass the argument |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3699 | Param = 0; |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3700 | if (FDecl && i < FDecl->getNumParams()) |
| 3701 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3702 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3703 | // Strip the unbridged-cast placeholder expression off, if applicable. |
| 3704 | if (Arg->getType() == Context.ARCUnbridgedCastTy && |
| 3705 | FDecl && FDecl->hasAttr<CFAuditedTransferAttr>() && |
| 3706 | (!Param || !Param->hasAttr<CFConsumedAttr>())) |
| 3707 | Arg = stripARCUnbridgedCast(Arg); |
| 3708 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3709 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3710 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3711 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 3712 | Proto->isArgConsumed(i)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3713 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3714 | SourceLocation(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3715 | Owned(Arg), |
| 3716 | /*TopLevelOfInitList=*/false, |
| 3717 | AllowExplicit); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3718 | if (ArgE.isInvalid()) |
| 3719 | return true; |
| 3720 | |
| 3721 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3722 | } else { |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3723 | Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3724 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3725 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3726 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3727 | if (ArgExpr.isInvalid()) |
| 3728 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3729 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3730 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3731 | } |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3732 | |
| 3733 | // Check for array bounds violations for each argument to the call. This |
| 3734 | // check only triggers warnings when the argument isn't a more complex Expr |
| 3735 | // with its own checking, such as a BinaryOperator. |
| 3736 | CheckArrayAccess(Arg); |
| 3737 | |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3738 | // Check for violations of C99 static array rules (C99 6.7.5.3p7). |
| 3739 | CheckStaticArrayArgument(CallLoc, Param, Arg); |
| 3740 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3741 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3742 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3743 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3744 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3745 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3746 | // Assume that extern "C" functions with variadic arguments that |
| 3747 | // return __unknown_anytype aren't *really* variadic. |
| 3748 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 3749 | FDecl && FDecl->isExternC()) { |
| 3750 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3751 | ExprResult arg; |
| 3752 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 3753 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 3754 | else |
| 3755 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3756 | Invalid |= arg.isInvalid(); |
| 3757 | AllArgs.push_back(arg.take()); |
| 3758 | } |
| 3759 | |
| 3760 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 3761 | } else { |
| 3762 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3763 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, |
| 3764 | FDecl); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3765 | Invalid |= Arg.isInvalid(); |
| 3766 | AllArgs.push_back(Arg.take()); |
| 3767 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3768 | } |
Ted Kremenek | 615eb7c | 2011-09-26 23:36:13 +0000 | [diff] [blame] | 3769 | |
| 3770 | // Check for array bounds violations. |
| 3771 | for (unsigned i = ArgIx; i != NumArgs; ++i) |
| 3772 | CheckArrayAccess(Args[i]); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3773 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3774 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3777 | static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) { |
| 3778 | TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc(); |
| 3779 | if (ArrayTypeLoc *ATL = dyn_cast<ArrayTypeLoc>(&TL)) |
| 3780 | S.Diag(PVD->getLocation(), diag::note_callee_static_array) |
| 3781 | << ATL->getLocalSourceRange(); |
| 3782 | } |
| 3783 | |
| 3784 | /// CheckStaticArrayArgument - If the given argument corresponds to a static |
| 3785 | /// array parameter, check that it is non-null, and that if it is formed by |
| 3786 | /// array-to-pointer decay, the underlying array is sufficiently large. |
| 3787 | /// |
| 3788 | /// C99 6.7.5.3p7: If the keyword static also appears within the [ and ] of the |
| 3789 | /// array type derivation, then for each call to the function, the value of the |
| 3790 | /// corresponding actual argument shall provide access to the first element of |
| 3791 | /// an array with at least as many elements as specified by the size expression. |
| 3792 | void |
| 3793 | Sema::CheckStaticArrayArgument(SourceLocation CallLoc, |
| 3794 | ParmVarDecl *Param, |
| 3795 | const Expr *ArgExpr) { |
| 3796 | // Static array parameters are not supported in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3797 | if (!Param || getLangOpts().CPlusPlus) |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3798 | return; |
| 3799 | |
| 3800 | QualType OrigTy = Param->getOriginalType(); |
| 3801 | |
| 3802 | const ArrayType *AT = Context.getAsArrayType(OrigTy); |
| 3803 | if (!AT || AT->getSizeModifier() != ArrayType::Static) |
| 3804 | return; |
| 3805 | |
| 3806 | if (ArgExpr->isNullPointerConstant(Context, |
| 3807 | Expr::NPC_NeverValueDependent)) { |
| 3808 | Diag(CallLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
| 3809 | DiagnoseCalleeStaticArrayParam(*this, Param); |
| 3810 | return; |
| 3811 | } |
| 3812 | |
| 3813 | const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT); |
| 3814 | if (!CAT) |
| 3815 | return; |
| 3816 | |
| 3817 | const ConstantArrayType *ArgCAT = |
| 3818 | Context.getAsConstantArrayType(ArgExpr->IgnoreParenImpCasts()->getType()); |
| 3819 | if (!ArgCAT) |
| 3820 | return; |
| 3821 | |
| 3822 | if (ArgCAT->getSize().ult(CAT->getSize())) { |
| 3823 | Diag(CallLoc, diag::warn_static_array_too_small) |
| 3824 | << ArgExpr->getSourceRange() |
| 3825 | << (unsigned) ArgCAT->getSize().getZExtValue() |
| 3826 | << (unsigned) CAT->getSize().getZExtValue(); |
| 3827 | DiagnoseCalleeStaticArrayParam(*this, Param); |
| 3828 | } |
| 3829 | } |
| 3830 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3831 | /// Given a function expression of unknown-any type, try to rebuild it |
| 3832 | /// to have a function type. |
| 3833 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 3834 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3835 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3836 | /// This provides the location of the left/right parens and a list of comma |
| 3837 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3838 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3839 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3840 | MultiExprArg ArgExprs, SourceLocation RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3841 | Expr *ExecConfig, bool IsExecConfig) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3842 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3843 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3844 | if (Result.isInvalid()) return ExprError(); |
| 3845 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3846 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3847 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3848 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3849 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3850 | if (!ArgExprs.empty()) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3851 | // Pseudo-destructor calls should not have any arguments. |
| 3852 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3853 | << FixItHint::CreateRemoval( |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3854 | SourceRange(ArgExprs[0]->getLocStart(), |
| 3855 | ArgExprs.back()->getLocEnd())); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3856 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3858 | return Owned(new (Context) CallExpr(Context, Fn, MultiExprArg(), |
| 3859 | Context.VoidTy, VK_RValue, |
| 3860 | RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3861 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3863 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3864 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3865 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3866 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3867 | bool Dependent = false; |
| 3868 | if (Fn->isTypeDependent()) |
| 3869 | Dependent = true; |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3870 | else if (Expr::hasAnyTypeDependentArguments(ArgExprs)) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3871 | Dependent = true; |
| 3872 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3873 | if (Dependent) { |
| 3874 | if (ExecConfig) { |
| 3875 | return Owned(new (Context) CUDAKernelCallExpr( |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3876 | Context, Fn, cast<CallExpr>(ExecConfig), ArgExprs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3877 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 3878 | } else { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3879 | return Owned(new (Context) CallExpr(Context, Fn, ArgExprs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3880 | Context.DependentTy, VK_RValue, |
| 3881 | RParenLoc)); |
| 3882 | } |
| 3883 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3884 | |
| 3885 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3886 | if (Fn->getType()->isRecordType()) |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3887 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, |
| 3888 | ArgExprs.data(), |
| 3889 | ArgExprs.size(), RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3890 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3891 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3892 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3893 | if (result.isInvalid()) return ExprError(); |
| 3894 | Fn = result.take(); |
| 3895 | } |
| 3896 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3897 | if (Fn->getType() == Context.BoundMemberTy) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3898 | return BuildCallToMemberFunction(S, Fn, LParenLoc, ArgExprs.data(), |
| 3899 | ArgExprs.size(), RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3900 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3901 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3902 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3903 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 3904 | if (Fn->getType() == Context.OverloadTy) { |
| 3905 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 3906 | |
Douglas Gregor | ee697e6 | 2011-10-13 18:10:35 +0000 | [diff] [blame] | 3907 | // 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] | 3908 | if (!find.HasFormOfMemberPointer) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3909 | OverloadExpr *ovl = find.Expression; |
| 3910 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 3911 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3912 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, ArgExprs.data(), |
| 3913 | ArgExprs.size(), RParenLoc, ExecConfig); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3914 | } else { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3915 | return BuildCallToMemberFunction(S, Fn, LParenLoc, ArgExprs.data(), |
| 3916 | ArgExprs.size(), RParenLoc); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3917 | } |
| 3918 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3921 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 3922 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3923 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3924 | if (result.isInvalid()) return ExprError(); |
| 3925 | Fn = result.take(); |
| 3926 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3927 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3928 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3929 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3930 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3931 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 3932 | if (UnOp->getOpcode() == UO_AddrOf) |
| 3933 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 3934 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3935 | if (isa<DeclRefExpr>(NakedFn)) |
| 3936 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3937 | else if (isa<MemberExpr>(NakedFn)) |
| 3938 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3939 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3940 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, ArgExprs.data(), |
| 3941 | ArgExprs.size(), RParenLoc, ExecConfig, |
| 3942 | IsExecConfig); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | ExprResult |
| 3946 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3947 | MultiExprArg ExecConfig, SourceLocation GGGLoc) { |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3948 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 3949 | if (!ConfigDecl) |
| 3950 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 3951 | << "cudaConfigureCall"); |
| 3952 | QualType ConfigQTy = ConfigDecl->getType(); |
| 3953 | |
| 3954 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3955 | ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3956 | MarkFunctionReferenced(LLLLoc, ConfigDecl); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3957 | |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3958 | return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, 0, |
| 3959 | /*IsExecConfig=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3962 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 3963 | /// |
| 3964 | /// __builtin_astype( value, dst type ) |
| 3965 | /// |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3966 | ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy, |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3967 | SourceLocation BuiltinLoc, |
| 3968 | SourceLocation RParenLoc) { |
| 3969 | ExprValueKind VK = VK_RValue; |
| 3970 | ExprObjectKind OK = OK_Ordinary; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3971 | QualType DstTy = GetTypeFromParser(ParsedDestTy); |
| 3972 | QualType SrcTy = E->getType(); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3973 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 3974 | return ExprError(Diag(BuiltinLoc, |
| 3975 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | af9cddf | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 3976 | << DstTy |
| 3977 | << SrcTy |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3978 | << E->getSourceRange()); |
| 3979 | return Owned(new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3980 | RParenLoc)); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3983 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3984 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3985 | /// unary-convert to an expression of function-pointer or |
| 3986 | /// block-pointer type. |
| 3987 | /// |
| 3988 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3989 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3990 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3991 | SourceLocation LParenLoc, |
| 3992 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3993 | SourceLocation RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3994 | Expr *Config, bool IsExecConfig) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3995 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 3996 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3997 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3998 | // Promote the function operand. |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 3999 | // We special-case function promotion here because we only allow promoting |
| 4000 | // builtin functions to function pointers in the callee of a call. |
| 4001 | ExprResult Result; |
| 4002 | if (BuiltinID && |
| 4003 | Fn->getType()->isSpecificBuiltinType(BuiltinType::BuiltinFn)) { |
| 4004 | Result = ImpCastExprToType(Fn, Context.getPointerType(FDecl->getType()), |
| 4005 | CK_BuiltinFnToFnPtr).take(); |
| 4006 | } else { |
| 4007 | Result = UsualUnaryConversions(Fn); |
| 4008 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4009 | if (Result.isInvalid()) |
| 4010 | return ExprError(); |
| 4011 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4012 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4013 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 4014 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4015 | CallExpr *TheCall; |
Eric Christopher | a27cb25 | 2012-05-30 01:14:28 +0000 | [diff] [blame] | 4016 | if (Config) |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4017 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 4018 | cast<CallExpr>(Config), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4019 | llvm::makeArrayRef(Args,NumArgs), |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4020 | Context.BoolTy, |
| 4021 | VK_RValue, |
| 4022 | RParenLoc); |
Eric Christopher | a27cb25 | 2012-05-30 01:14:28 +0000 | [diff] [blame] | 4023 | else |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4024 | TheCall = new (Context) CallExpr(Context, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4025 | llvm::makeArrayRef(Args, NumArgs), |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4026 | Context.BoolTy, |
| 4027 | VK_RValue, |
| 4028 | RParenLoc); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4029 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4030 | // Bail out early if calling a builtin with custom typechecking. |
| 4031 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 4032 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 4033 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4034 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4035 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4036 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4037 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 4038 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4039 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4040 | if (FuncT == 0) |
| 4041 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4042 | << Fn->getType() << Fn->getSourceRange()); |
| 4043 | } else if (const BlockPointerType *BPT = |
| 4044 | Fn->getType()->getAs<BlockPointerType>()) { |
| 4045 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 4046 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4047 | // Handle calls to expressions of unknown-any type. |
| 4048 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4049 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4050 | if (rewrite.isInvalid()) return ExprError(); |
| 4051 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 4052 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4053 | goto retry; |
| 4054 | } |
| 4055 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4056 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4057 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4058 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4059 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4060 | if (getLangOpts().CUDA) { |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 4061 | if (Config) { |
| 4062 | // CUDA: Kernel calls must be to global functions |
| 4063 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 4064 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 4065 | << FDecl->getName() << Fn->getSourceRange()); |
| 4066 | |
| 4067 | // CUDA: Kernel function must have 'void' return type |
| 4068 | if (!FuncT->getResultType()->isVoidType()) |
| 4069 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 4070 | << Fn->getType() << Fn->getSourceRange()); |
Peter Collingbourne | 8591a7f | 2011-10-02 23:49:15 +0000 | [diff] [blame] | 4071 | } else { |
| 4072 | // CUDA: Calls to global functions must be configured |
| 4073 | if (FDecl && FDecl->hasAttr<CUDAGlobalAttr>()) |
| 4074 | return ExprError(Diag(LParenLoc, diag::err_global_call_not_config) |
| 4075 | << FDecl->getName() << Fn->getSourceRange()); |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 4076 | } |
| 4077 | } |
| 4078 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4079 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4080 | if (CheckCallReturnType(FuncT->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4081 | Fn->getLocStart(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4082 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4083 | return ExprError(); |
| 4084 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4085 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4086 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4087 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4088 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4089 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT); |
| 4090 | if (Proto) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4091 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 4092 | RParenLoc, IsExecConfig)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4093 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4094 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4095 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4096 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4097 | if (FDecl) { |
| 4098 | // Check if we have too few/too many template arguments, based |
| 4099 | // on our knowledge of the function definition. |
| 4100 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 4101 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4102 | Proto = Def->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4103 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4104 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 4105 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4106 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4107 | |
| 4108 | // If the function we're calling isn't a function prototype, but we have |
| 4109 | // a function prototype from a prior declaratiom, use that prototype. |
| 4110 | if (!FDecl->hasPrototype()) |
| 4111 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4114 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4115 | for (unsigned i = 0; i != NumArgs; i++) { |
| 4116 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4117 | |
| 4118 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4119 | InitializedEntity Entity |
| 4120 | = InitializedEntity::InitializeParameter(Context, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4121 | Proto->getArgType(i), |
| 4122 | Proto->isArgConsumed(i)); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4123 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 4124 | SourceLocation(), |
| 4125 | Owned(Arg)); |
| 4126 | if (ArgE.isInvalid()) |
| 4127 | return true; |
| 4128 | |
| 4129 | Arg = ArgE.takeAs<Expr>(); |
| 4130 | |
| 4131 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4132 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 4133 | |
| 4134 | if (ArgE.isInvalid()) |
| 4135 | return true; |
| 4136 | |
| 4137 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4140 | if (RequireCompleteType(Arg->getLocStart(), |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4141 | Arg->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4142 | diag::err_call_incomplete_argument, Arg)) |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4143 | return ExprError(); |
| 4144 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4145 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4146 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4147 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4148 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4149 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 4150 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4151 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 4152 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4153 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 4154 | // Check for sentinels |
| 4155 | if (NDecl) |
| 4156 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4158 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4159 | if (FDecl) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4160 | if (CheckFunctionCall(FDecl, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4161 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4162 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4163 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 4164 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4165 | } else if (NDecl) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4166 | if (CheckBlockCall(NDecl, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4167 | return ExprError(); |
| 4168 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4169 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4170 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4173 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4174 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4175 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4176 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 4177 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4178 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4179 | |
| 4180 | TypeSourceInfo *TInfo; |
| 4181 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 4182 | if (!TInfo) |
| 4183 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 4184 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4185 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4188 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4189 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4190 | SourceLocation RParenLoc, Expr *LiteralExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4191 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 4192 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4193 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4194 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4195 | diag::err_illegal_decl_array_incomplete_type, |
| 4196 | SourceRange(LParenLoc, |
| 4197 | LiteralExpr->getSourceRange().getEnd()))) |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4198 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4199 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4200 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4201 | << SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4202 | } else if (!literalType->isDependentType() && |
| 4203 | RequireCompleteType(LParenLoc, literalType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4204 | diag::err_typecheck_decl_incomplete_type, |
| 4205 | SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4206 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4207 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4208 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4209 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4210 | InitializationKind Kind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4211 | = InitializationKind::CreateCStyleCast(LParenLoc, |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4212 | SourceRange(LParenLoc, RParenLoc), |
| 4213 | /*InitList=*/true); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4214 | InitializationSequence InitSeq(*this, Entity, Kind, &LiteralExpr, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4215 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, LiteralExpr, |
| 4216 | &literalType); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4217 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4218 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4219 | LiteralExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4220 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 4221 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4222 | if (isFileScope) { // 6.5.2.5p3 |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4223 | if (CheckForConstantInitializer(LiteralExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4224 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 4225 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4226 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4227 | // In C, compound literals are l-values for some reason. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4228 | ExprValueKind VK = getLangOpts().CPlusPlus ? VK_RValue : VK_LValue; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4229 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 4230 | return MaybeBindToTemporary( |
| 4231 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4232 | VK, LiteralExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4235 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4236 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4237 | SourceLocation RBraceLoc) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4238 | // Immediately handle non-overload placeholders. Overloads can be |
| 4239 | // resolved contextually, but everything else here can't. |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4240 | for (unsigned I = 0, E = InitArgList.size(); I != E; ++I) { |
| 4241 | if (InitArgList[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4242 | ExprResult result = CheckPlaceholderExpr(InitArgList[I]); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4243 | |
| 4244 | // Ignore failures; dropping the entire initializer list because |
| 4245 | // of one failure would be terrible for indexing/etc. |
| 4246 | if (result.isInvalid()) continue; |
| 4247 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4248 | InitArgList[I] = result.take(); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4249 | } |
| 4250 | } |
| 4251 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 4252 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4253 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4254 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4255 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitArgList, |
| 4256 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4257 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4258 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4261 | /// Do an explicit extend of the given block pointer if we're in ARC. |
| 4262 | static void maybeExtendBlockObject(Sema &S, ExprResult &E) { |
| 4263 | assert(E.get()->getType()->isBlockPointerType()); |
| 4264 | assert(E.get()->isRValue()); |
| 4265 | |
| 4266 | // Only do this in an r-value context. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4267 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4268 | |
| 4269 | E = ImplicitCastExpr::Create(S.Context, E.get()->getType(), |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4270 | CK_ARCExtendBlockObject, E.get(), |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4271 | /*base path*/ 0, VK_RValue); |
| 4272 | S.ExprNeedsCleanups = true; |
| 4273 | } |
| 4274 | |
| 4275 | /// Prepare a conversion of the given expression to an ObjC object |
| 4276 | /// pointer type. |
| 4277 | CastKind Sema::PrepareCastToObjCObjectPointer(ExprResult &E) { |
| 4278 | QualType type = E.get()->getType(); |
| 4279 | if (type->isObjCObjectPointerType()) { |
| 4280 | return CK_BitCast; |
| 4281 | } else if (type->isBlockPointerType()) { |
| 4282 | maybeExtendBlockObject(*this, E); |
| 4283 | return CK_BlockPointerToObjCPointerCast; |
| 4284 | } else { |
| 4285 | assert(type->isPointerType()); |
| 4286 | return CK_CPointerToObjCPointerCast; |
| 4287 | } |
| 4288 | } |
| 4289 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4290 | /// Prepares for a scalar cast, performing all the necessary stages |
| 4291 | /// except the final cast and returning the kind required. |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4292 | CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4293 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 4294 | // Also, callers should have filtered out the invalid cases with |
| 4295 | // pointers. Everything else should be possible. |
| 4296 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4297 | QualType SrcTy = Src.get()->getType(); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4298 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4299 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4300 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4301 | switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4302 | case Type::STK_MemberPointer: |
| 4303 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4304 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4305 | case Type::STK_CPointer: |
| 4306 | case Type::STK_BlockPointer: |
| 4307 | case Type::STK_ObjCObjectPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4308 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4309 | case Type::STK_CPointer: |
| 4310 | return CK_BitCast; |
| 4311 | case Type::STK_BlockPointer: |
| 4312 | return (SrcKind == Type::STK_BlockPointer |
| 4313 | ? CK_BitCast : CK_AnyPointerToBlockPointerCast); |
| 4314 | case Type::STK_ObjCObjectPointer: |
| 4315 | if (SrcKind == Type::STK_ObjCObjectPointer) |
| 4316 | return CK_BitCast; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4317 | if (SrcKind == Type::STK_CPointer) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4318 | return CK_CPointerToObjCPointerCast; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4319 | maybeExtendBlockObject(*this, Src); |
| 4320 | return CK_BlockPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4321 | case Type::STK_Bool: |
| 4322 | return CK_PointerToBoolean; |
| 4323 | case Type::STK_Integral: |
| 4324 | return CK_PointerToIntegral; |
| 4325 | case Type::STK_Floating: |
| 4326 | case Type::STK_FloatingComplex: |
| 4327 | case Type::STK_IntegralComplex: |
| 4328 | case Type::STK_MemberPointer: |
| 4329 | llvm_unreachable("illegal cast from pointer"); |
| 4330 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4331 | llvm_unreachable("Should have returned before this"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4332 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4333 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 4334 | case Type::STK_Integral: |
| 4335 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4336 | case Type::STK_CPointer: |
| 4337 | case Type::STK_ObjCObjectPointer: |
| 4338 | case Type::STK_BlockPointer: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4339 | if (Src.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4340 | Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4341 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4342 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4343 | case Type::STK_Bool: |
| 4344 | return CK_IntegralToBoolean; |
| 4345 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4346 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4347 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4348 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4349 | case Type::STK_IntegralComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4350 | Src = ImpCastExprToType(Src.take(), |
| 4351 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4352 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4353 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4354 | case Type::STK_FloatingComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4355 | Src = ImpCastExprToType(Src.take(), |
| 4356 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4357 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4358 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4359 | case Type::STK_MemberPointer: |
| 4360 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4361 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4362 | llvm_unreachable("Should have returned before this"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4363 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4364 | case Type::STK_Floating: |
| 4365 | switch (DestTy->getScalarTypeKind()) { |
| 4366 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4367 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4368 | case Type::STK_Bool: |
| 4369 | return CK_FloatingToBoolean; |
| 4370 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4371 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4372 | case Type::STK_FloatingComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4373 | Src = ImpCastExprToType(Src.take(), |
| 4374 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4375 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4376 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4377 | case Type::STK_IntegralComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4378 | Src = ImpCastExprToType(Src.take(), |
| 4379 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4380 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4381 | return CK_IntegralRealToComplex; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4382 | case Type::STK_CPointer: |
| 4383 | case Type::STK_ObjCObjectPointer: |
| 4384 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4385 | llvm_unreachable("valid float->pointer cast?"); |
| 4386 | case Type::STK_MemberPointer: |
| 4387 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4388 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4389 | llvm_unreachable("Should have returned before this"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4390 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4391 | case Type::STK_FloatingComplex: |
| 4392 | switch (DestTy->getScalarTypeKind()) { |
| 4393 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4394 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4395 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4396 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4397 | case Type::STK_Floating: { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4398 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); |
| 4399 | if (Context.hasSameType(ET, DestTy)) |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4400 | return CK_FloatingComplexToReal; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4401 | Src = ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4402 | return CK_FloatingCast; |
| 4403 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4404 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4405 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4406 | case Type::STK_Integral: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4407 | Src = ImpCastExprToType(Src.take(), |
| 4408 | SrcTy->castAs<ComplexType>()->getElementType(), |
| 4409 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4410 | return CK_FloatingToIntegral; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4411 | case Type::STK_CPointer: |
| 4412 | case Type::STK_ObjCObjectPointer: |
| 4413 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4414 | llvm_unreachable("valid complex float->pointer cast?"); |
| 4415 | case Type::STK_MemberPointer: |
| 4416 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4417 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4418 | llvm_unreachable("Should have returned before this"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4419 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4420 | case Type::STK_IntegralComplex: |
| 4421 | switch (DestTy->getScalarTypeKind()) { |
| 4422 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4423 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4424 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4425 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4426 | case Type::STK_Integral: { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4427 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); |
| 4428 | if (Context.hasSameType(ET, DestTy)) |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4429 | return CK_IntegralComplexToReal; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4430 | Src = ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4431 | return CK_IntegralCast; |
| 4432 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4433 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4434 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4435 | case Type::STK_Floating: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4436 | Src = ImpCastExprToType(Src.take(), |
| 4437 | SrcTy->castAs<ComplexType>()->getElementType(), |
| 4438 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4439 | return CK_IntegralToFloating; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4440 | case Type::STK_CPointer: |
| 4441 | case Type::STK_ObjCObjectPointer: |
| 4442 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4443 | llvm_unreachable("valid complex int->pointer cast?"); |
| 4444 | case Type::STK_MemberPointer: |
| 4445 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4446 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4447 | llvm_unreachable("Should have returned before this"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4448 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4449 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4450 | llvm_unreachable("Unhandled scalar cast"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4453 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4454 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4455 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4456 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4457 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4458 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4459 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4460 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4461 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4462 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4463 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4464 | } else |
| 4465 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4466 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4467 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4468 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4469 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4470 | return false; |
| 4471 | } |
| 4472 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4473 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 4474 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4475 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4476 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4477 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4478 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4479 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4480 | // an ExtVectorType. |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4481 | // In OpenCL, casts between vectors of different types are not allowed. |
| 4482 | // (See OpenCL 6.2). |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4483 | if (SrcTy->isVectorType()) { |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4484 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4485 | || (getLangOpts().OpenCL && |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4486 | (DestTy.getCanonicalType() != SrcTy.getCanonicalType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4487 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4488 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4489 | return ExprError(); |
| 4490 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4491 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4492 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4495 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4496 | // conversion will take place first from scalar to elt type, and then |
| 4497 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4498 | if (SrcTy->isPointerType()) |
| 4499 | return Diag(R.getBegin(), |
| 4500 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4501 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4502 | |
| 4503 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4504 | ExprResult CastExprRes = Owned(CastExpr); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4505 | CastKind CK = PrepareScalarCast(CastExprRes, DestElemTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4506 | if (CastExprRes.isInvalid()) |
| 4507 | return ExprError(); |
| 4508 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4509 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4510 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4511 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4514 | ExprResult |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4515 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
| 4516 | Declarator &D, ParsedType &Ty, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4517 | SourceLocation RParenLoc, Expr *CastExpr) { |
| 4518 | assert(!D.isInvalidType() && (CastExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4519 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4520 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4521 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, CastExpr->getType()); |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4522 | if (D.isInvalidType()) |
| 4523 | return ExprError(); |
| 4524 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4525 | if (getLangOpts().CPlusPlus) { |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4526 | // Check that there are no default arguments (C++ only). |
| 4527 | CheckExtraCXXDefaultArguments(D); |
| 4528 | } |
| 4529 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4530 | checkUnusedDeclAttributes(D); |
| 4531 | |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4532 | QualType castType = castTInfo->getType(); |
| 4533 | Ty = CreateParsedType(castType, castTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4534 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4535 | bool isVectorLiteral = false; |
| 4536 | |
| 4537 | // Check for an altivec or OpenCL literal, |
| 4538 | // i.e. all the elements are integer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4539 | ParenExpr *PE = dyn_cast<ParenExpr>(CastExpr); |
| 4540 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(CastExpr); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4541 | if ((getLangOpts().AltiVec || getLangOpts().OpenCL) |
Tobias Grosser | 37c31c2 | 2011-09-21 18:28:29 +0000 | [diff] [blame] | 4542 | && castType->isVectorType() && (PE || PLE)) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4543 | if (PLE && PLE->getNumExprs() == 0) { |
| 4544 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4545 | return ExprError(); |
| 4546 | } |
| 4547 | if (PE || PLE->getNumExprs() == 1) { |
| 4548 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4549 | if (!E->getType()->isVectorType()) |
| 4550 | isVectorLiteral = true; |
| 4551 | } |
| 4552 | else |
| 4553 | isVectorLiteral = true; |
| 4554 | } |
| 4555 | |
| 4556 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4557 | // then handle it as such. |
| 4558 | if (isVectorLiteral) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4559 | return BuildVectorLiteral(LParenLoc, RParenLoc, CastExpr, castTInfo); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4560 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4561 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4562 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4563 | // sequence of BinOp comma operators. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4564 | if (isa<ParenListExpr>(CastExpr)) { |
| 4565 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4566 | if (Result.isInvalid()) return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4567 | CastExpr = Result.take(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4568 | } |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4569 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4570 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4573 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4574 | SourceLocation RParenLoc, Expr *E, |
| 4575 | TypeSourceInfo *TInfo) { |
| 4576 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4577 | "Expected paren or paren list expression"); |
| 4578 | |
| 4579 | Expr **exprs; |
| 4580 | unsigned numExprs; |
| 4581 | Expr *subExpr; |
| 4582 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
| 4583 | exprs = PE->getExprs(); |
| 4584 | numExprs = PE->getNumExprs(); |
| 4585 | } else { |
| 4586 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4587 | exprs = &subExpr; |
| 4588 | numExprs = 1; |
| 4589 | } |
| 4590 | |
| 4591 | QualType Ty = TInfo->getType(); |
| 4592 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4593 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4594 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4595 | const VectorType *VTy = Ty->getAs<VectorType>(); |
| 4596 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4597 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4598 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4599 | // initializers must be one or must match the size of the vector. |
| 4600 | // If a single value is specified in the initializer then it will be |
| 4601 | // replicated to all the components of the vector |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4602 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4603 | // The number of initializers must be one or must match the size of the |
| 4604 | // vector. If a single value is specified in the initializer then it will |
| 4605 | // be replicated to all the components of the vector |
| 4606 | if (numExprs == 1) { |
| 4607 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
Richard Smith | 61ffd09 | 2011-10-27 23:31:58 +0000 | [diff] [blame] | 4608 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
| 4609 | if (Literal.isInvalid()) |
| 4610 | return ExprError(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4611 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4612 | PrepareScalarCast(Literal, ElemTy)); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4613 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4614 | } |
| 4615 | else if (numExprs < numElems) { |
| 4616 | Diag(E->getExprLoc(), |
| 4617 | diag::err_incorrect_number_of_vector_initializers); |
| 4618 | return ExprError(); |
| 4619 | } |
| 4620 | else |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 4621 | initExprs.append(exprs, exprs + numExprs); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4622 | } |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4623 | else { |
| 4624 | // For OpenCL, when the number of initializers is a single value, |
| 4625 | // it will be replicated to all components of the vector. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4626 | if (getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4627 | VTy->getVectorKind() == VectorType::GenericVector && |
| 4628 | numExprs == 1) { |
| 4629 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
Richard Smith | 61ffd09 | 2011-10-27 23:31:58 +0000 | [diff] [blame] | 4630 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
| 4631 | if (Literal.isInvalid()) |
| 4632 | return ExprError(); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4633 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4634 | PrepareScalarCast(Literal, ElemTy)); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4635 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4636 | } |
| 4637 | |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 4638 | initExprs.append(exprs, exprs + numExprs); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4639 | } |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4640 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4641 | // braces instead of the original commas. |
| 4642 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4643 | initExprs, RParenLoc); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4644 | initE->setType(Ty); |
| 4645 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4646 | } |
| 4647 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4648 | /// This is not an AltiVec-style cast or or C++ direct-initialization, so turn |
| 4649 | /// the ParenListExpr into a sequence of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4650 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4651 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { |
| 4652 | ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4653 | if (!E) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4654 | return Owned(OrigExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4656 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4658 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4659 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4660 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4661 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4662 | if (Result.isInvalid()) return ExprError(); |
| 4663 | |
| 4664 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4667 | ExprResult Sema::ActOnParenListExpr(SourceLocation L, |
| 4668 | SourceLocation R, |
| 4669 | MultiExprArg Val) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4670 | assert(Val.data() != 0 && "ActOnParenOrParenListExpr() missing expr list"); |
| 4671 | Expr *expr = new (Context) ParenListExpr(Context, L, Val, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4672 | return Owned(expr); |
| 4673 | } |
| 4674 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4675 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4676 | /// constant and the other is not a pointer. Returns true if a diagnostic is |
| 4677 | /// emitted. |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4678 | bool Sema::DiagnoseConditionalForNull(Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4679 | SourceLocation QuestionLoc) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4680 | Expr *NullExpr = LHSExpr; |
| 4681 | Expr *NonPointerExpr = RHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4682 | Expr::NullPointerConstantKind NullKind = |
| 4683 | NullExpr->isNullPointerConstant(Context, |
| 4684 | Expr::NPC_ValueDependentIsNotNull); |
| 4685 | |
| 4686 | if (NullKind == Expr::NPCK_NotNull) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4687 | NullExpr = RHSExpr; |
| 4688 | NonPointerExpr = LHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4689 | NullKind = |
| 4690 | NullExpr->isNullPointerConstant(Context, |
| 4691 | Expr::NPC_ValueDependentIsNotNull); |
| 4692 | } |
| 4693 | |
| 4694 | if (NullKind == Expr::NPCK_NotNull) |
| 4695 | return false; |
| 4696 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 4697 | if (NullKind == Expr::NPCK_ZeroExpression) |
| 4698 | return false; |
| 4699 | |
| 4700 | if (NullKind == Expr::NPCK_ZeroLiteral) { |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4701 | // In this case, check to make sure that we got here from a "NULL" |
| 4702 | // string in the source code. |
| 4703 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4704 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4705 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4706 | return false; |
| 4707 | } |
| 4708 | |
| 4709 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 4710 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4711 | << NonPointerExpr->getType() << DiagType |
| 4712 | << NonPointerExpr->getSourceRange(); |
| 4713 | return true; |
| 4714 | } |
| 4715 | |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4716 | /// \brief Return false if the condition expression is valid, true otherwise. |
| 4717 | static bool checkCondition(Sema &S, Expr *Cond) { |
| 4718 | QualType CondTy = Cond->getType(); |
| 4719 | |
| 4720 | // C99 6.5.15p2 |
| 4721 | if (CondTy->isScalarType()) return false; |
| 4722 | |
| 4723 | // 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] | 4724 | if (S.getLangOpts().OpenCL && CondTy->isVectorType()) |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4725 | return false; |
| 4726 | |
| 4727 | // Emit the proper error message. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4728 | S.Diag(Cond->getLocStart(), S.getLangOpts().OpenCL ? |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4729 | diag::err_typecheck_cond_expect_scalar : |
| 4730 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4731 | << CondTy; |
| 4732 | return true; |
| 4733 | } |
| 4734 | |
| 4735 | /// \brief Return false if the two expressions can be converted to a vector, |
| 4736 | /// true otherwise |
| 4737 | static bool checkConditionalConvertScalarsToVectors(Sema &S, ExprResult &LHS, |
| 4738 | ExprResult &RHS, |
| 4739 | QualType CondTy) { |
| 4740 | // Both operands should be of scalar type. |
| 4741 | if (!LHS.get()->getType()->isScalarType()) { |
| 4742 | S.Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4743 | << CondTy; |
| 4744 | return true; |
| 4745 | } |
| 4746 | if (!RHS.get()->getType()->isScalarType()) { |
| 4747 | S.Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4748 | << CondTy; |
| 4749 | return true; |
| 4750 | } |
| 4751 | |
| 4752 | // Implicity convert these scalars to the type of the condition. |
| 4753 | LHS = S.ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4754 | RHS = S.ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
| 4755 | return false; |
| 4756 | } |
| 4757 | |
| 4758 | /// \brief Handle when one or both operands are void type. |
| 4759 | static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, |
| 4760 | ExprResult &RHS) { |
| 4761 | Expr *LHSExpr = LHS.get(); |
| 4762 | Expr *RHSExpr = RHS.get(); |
| 4763 | |
| 4764 | if (!LHSExpr->getType()->isVoidType()) |
| 4765 | S.Diag(RHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4766 | << RHSExpr->getSourceRange(); |
| 4767 | if (!RHSExpr->getType()->isVoidType()) |
| 4768 | S.Diag(LHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4769 | << LHSExpr->getSourceRange(); |
| 4770 | LHS = S.ImpCastExprToType(LHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4771 | RHS = S.ImpCastExprToType(RHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4772 | return S.Context.VoidTy; |
| 4773 | } |
| 4774 | |
| 4775 | /// \brief Return false if the NullExpr can be promoted to PointerTy, |
| 4776 | /// true otherwise. |
| 4777 | static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, |
| 4778 | QualType PointerTy) { |
| 4779 | if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || |
| 4780 | !NullExpr.get()->isNullPointerConstant(S.Context, |
| 4781 | Expr::NPC_ValueDependentIsNull)) |
| 4782 | return true; |
| 4783 | |
| 4784 | NullExpr = S.ImpCastExprToType(NullExpr.take(), PointerTy, CK_NullToPointer); |
| 4785 | return false; |
| 4786 | } |
| 4787 | |
| 4788 | /// \brief Checks compatibility between two pointers and return the resulting |
| 4789 | /// type. |
| 4790 | static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, |
| 4791 | ExprResult &RHS, |
| 4792 | SourceLocation Loc) { |
| 4793 | QualType LHSTy = LHS.get()->getType(); |
| 4794 | QualType RHSTy = RHS.get()->getType(); |
| 4795 | |
| 4796 | if (S.Context.hasSameType(LHSTy, RHSTy)) { |
| 4797 | // Two identical pointers types are always compatible. |
| 4798 | return LHSTy; |
| 4799 | } |
| 4800 | |
| 4801 | QualType lhptee, rhptee; |
| 4802 | |
| 4803 | // Get the pointee types. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4804 | if (const BlockPointerType *LHSBTy = LHSTy->getAs<BlockPointerType>()) { |
| 4805 | lhptee = LHSBTy->getPointeeType(); |
| 4806 | rhptee = RHSTy->castAs<BlockPointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4807 | } else { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4808 | lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); |
| 4809 | rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4812 | // C99 6.5.15p6: If both operands are pointers to compatible types or to |
| 4813 | // differently qualified versions of compatible types, the result type is |
| 4814 | // a pointer to an appropriately qualified version of the composite |
| 4815 | // type. |
| 4816 | |
| 4817 | // Only CVR-qualifiers exist in the standard, and the differently-qualified |
| 4818 | // clause doesn't make sense for our extensions. E.g. address space 2 should |
| 4819 | // be incompatible with address space 3: they may live on different devices or |
| 4820 | // anything. |
| 4821 | Qualifiers lhQual = lhptee.getQualifiers(); |
| 4822 | Qualifiers rhQual = rhptee.getQualifiers(); |
| 4823 | |
| 4824 | unsigned MergedCVRQual = lhQual.getCVRQualifiers() | rhQual.getCVRQualifiers(); |
| 4825 | lhQual.removeCVRQualifiers(); |
| 4826 | rhQual.removeCVRQualifiers(); |
| 4827 | |
| 4828 | lhptee = S.Context.getQualifiedType(lhptee.getUnqualifiedType(), lhQual); |
| 4829 | rhptee = S.Context.getQualifiedType(rhptee.getUnqualifiedType(), rhQual); |
| 4830 | |
| 4831 | QualType CompositeTy = S.Context.mergeTypes(lhptee, rhptee); |
| 4832 | |
| 4833 | if (CompositeTy.isNull()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4834 | S.Diag(Loc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4835 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4836 | << RHS.get()->getSourceRange(); |
| 4837 | // In this situation, we assume void* type. No especially good |
| 4838 | // reason, but this is what gcc does, and we do have to pick |
| 4839 | // to get a consistent AST. |
| 4840 | QualType incompatTy = S.Context.getPointerType(S.Context.VoidTy); |
| 4841 | LHS = S.ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4842 | RHS = S.ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| 4843 | return incompatTy; |
| 4844 | } |
| 4845 | |
| 4846 | // The pointer types are compatible. |
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4847 | QualType ResultTy = CompositeTy.withCVRQualifiers(MergedCVRQual); |
| 4848 | ResultTy = S.Context.getPointerType(ResultTy); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4849 | |
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4850 | LHS = S.ImpCastExprToType(LHS.take(), ResultTy, CK_BitCast); |
| 4851 | RHS = S.ImpCastExprToType(RHS.take(), ResultTy, CK_BitCast); |
| 4852 | return ResultTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | /// \brief Return the resulting type when the operands are both block pointers. |
| 4856 | static QualType checkConditionalBlockPointerCompatibility(Sema &S, |
| 4857 | ExprResult &LHS, |
| 4858 | ExprResult &RHS, |
| 4859 | SourceLocation Loc) { |
| 4860 | QualType LHSTy = LHS.get()->getType(); |
| 4861 | QualType RHSTy = RHS.get()->getType(); |
| 4862 | |
| 4863 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4864 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4865 | QualType destType = S.Context.getPointerType(S.Context.VoidTy); |
| 4866 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4867 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4868 | return destType; |
| 4869 | } |
| 4870 | S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 4871 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4872 | << RHS.get()->getSourceRange(); |
| 4873 | return QualType(); |
| 4874 | } |
| 4875 | |
| 4876 | // We have 2 block pointer types. |
| 4877 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4878 | } |
| 4879 | |
| 4880 | /// \brief Return the resulting type when the operands are both pointers. |
| 4881 | static QualType |
| 4882 | checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, |
| 4883 | ExprResult &RHS, |
| 4884 | SourceLocation Loc) { |
| 4885 | // get the pointer types |
| 4886 | QualType LHSTy = LHS.get()->getType(); |
| 4887 | QualType RHSTy = RHS.get()->getType(); |
| 4888 | |
| 4889 | // get the "pointed to" types |
| 4890 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4891 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4892 | |
| 4893 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4894 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4895 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 4896 | QualType destPointee |
| 4897 | = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4898 | QualType destType = S.Context.getPointerType(destPointee); |
| 4899 | // Add qualifiers if necessary. |
| 4900 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
| 4901 | // Promote to void*. |
| 4902 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4903 | return destType; |
| 4904 | } |
| 4905 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 4906 | QualType destPointee |
| 4907 | = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4908 | QualType destType = S.Context.getPointerType(destPointee); |
| 4909 | // Add qualifiers if necessary. |
| 4910 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
| 4911 | // Promote to void*. |
| 4912 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4913 | return destType; |
| 4914 | } |
| 4915 | |
| 4916 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4917 | } |
| 4918 | |
| 4919 | /// \brief Return false if the first expression is not an integer and the second |
| 4920 | /// expression is not a pointer, true otherwise. |
| 4921 | static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, |
| 4922 | Expr* PointerExpr, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4923 | bool IsIntFirstExpr) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4924 | if (!PointerExpr->getType()->isPointerType() || |
| 4925 | !Int.get()->getType()->isIntegerType()) |
| 4926 | return false; |
| 4927 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4928 | Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; |
| 4929 | Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4930 | |
| 4931 | S.Diag(Loc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4932 | << Expr1->getType() << Expr2->getType() |
| 4933 | << Expr1->getSourceRange() << Expr2->getSourceRange(); |
| 4934 | Int = S.ImpCastExprToType(Int.take(), PointerExpr->getType(), |
| 4935 | CK_IntegralToPointer); |
| 4936 | return true; |
| 4937 | } |
| 4938 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4939 | /// Note that LHS is not null here, even if this is the gnu "x ?: y" extension. |
| 4940 | /// In that case, LHS = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4941 | /// C99 6.5.15 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4942 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 4943 | ExprResult &RHS, ExprValueKind &VK, |
| 4944 | ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4945 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4946 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4947 | ExprResult LHSResult = CheckPlaceholderExpr(LHS.get()); |
| 4948 | if (!LHSResult.isUsable()) return QualType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4949 | LHS = LHSResult; |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4950 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4951 | ExprResult RHSResult = CheckPlaceholderExpr(RHS.get()); |
| 4952 | if (!RHSResult.isUsable()) return QualType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4953 | RHS = RHSResult; |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4954 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4955 | // C++ is sufficiently different to merit its own checker. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4956 | if (getLangOpts().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4957 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4958 | |
| 4959 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4960 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4961 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4962 | Cond = UsualUnaryConversions(Cond.take()); |
| 4963 | if (Cond.isInvalid()) |
| 4964 | return QualType(); |
| 4965 | LHS = UsualUnaryConversions(LHS.take()); |
| 4966 | if (LHS.isInvalid()) |
| 4967 | return QualType(); |
| 4968 | RHS = UsualUnaryConversions(RHS.take()); |
| 4969 | if (RHS.isInvalid()) |
| 4970 | return QualType(); |
| 4971 | |
| 4972 | QualType CondTy = Cond.get()->getType(); |
| 4973 | QualType LHSTy = LHS.get()->getType(); |
| 4974 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4975 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4976 | // first, check the condition. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4977 | if (checkCondition(*this, Cond.get())) |
| 4978 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4979 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4980 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4981 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4982 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4983 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4984 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4985 | // attempt to implicity convert them to the vector type to act like the |
| 4986 | // built in select. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4987 | if (getLangOpts().OpenCL && CondTy->isVectorType()) |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4988 | if (checkConditionalConvertScalarsToVectors(*this, LHS, RHS, CondTy)) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4989 | return QualType(); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4990 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4991 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4992 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4993 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4994 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4995 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4996 | return QualType(); |
| 4997 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4998 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4999 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5000 | // If both operands are the same structure or union type, the result is that |
| 5001 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5002 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5003 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5004 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5005 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5006 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5007 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5008 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5009 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5010 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5011 | // 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] | 5012 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5013 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5014 | return checkConditionalVoidType(*this, LHS, RHS); |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5015 | } |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5016 | |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5017 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 5018 | // the type of the other operand." |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5019 | if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; |
| 5020 | if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5021 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5022 | // All objective-c pointer type analysis is done here. |
| 5023 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 5024 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5025 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5026 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5027 | if (!compositeType.isNull()) |
| 5028 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5029 | |
| 5030 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5031 | // Handle block pointer types. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5032 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) |
| 5033 | return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, |
| 5034 | QuestionLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5035 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5036 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5037 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) |
| 5038 | return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, |
| 5039 | QuestionLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5040 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5041 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 5042 | // null pointers have been filtered out by this point. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5043 | if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, |
| 5044 | /*isIntFirstExpr=*/true)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5045 | return RHSTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5046 | if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, |
| 5047 | /*isIntFirstExpr=*/false)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5048 | return LHSTy; |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 5049 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5050 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 5051 | // constant and the other is not a pointer type. In this case, the user most |
| 5052 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5053 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5054 | return QualType(); |
| 5055 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5056 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5057 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5058 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 5059 | << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5060 | return QualType(); |
| 5061 | } |
| 5062 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5063 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 5064 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5065 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5066 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5067 | QualType LHSTy = LHS.get()->getType(); |
| 5068 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5069 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5070 | // Handle things like Class and struct objc_class*. Here we case the result |
| 5071 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 5072 | // redefinition type if an attempt is made to access its fields. |
| 5073 | if (LHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5074 | (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5075 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5076 | return LHSTy; |
| 5077 | } |
| 5078 | if (RHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5079 | (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5080 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5081 | return RHSTy; |
| 5082 | } |
| 5083 | // And the same for struct objc_object* / id |
| 5084 | if (LHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5085 | (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5086 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5087 | return LHSTy; |
| 5088 | } |
| 5089 | if (RHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5090 | (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5091 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5092 | return RHSTy; |
| 5093 | } |
| 5094 | // And the same for struct objc_selector* / SEL |
| 5095 | if (Context.isObjCSelType(LHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5096 | (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5097 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5098 | return LHSTy; |
| 5099 | } |
| 5100 | if (Context.isObjCSelType(RHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5101 | (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5102 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5103 | return RHSTy; |
| 5104 | } |
| 5105 | // Check constraints for Objective-C object pointers types. |
| 5106 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5107 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5108 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5109 | // Two identical object pointer types are always compatible. |
| 5110 | return LHSTy; |
| 5111 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5112 | const ObjCObjectPointerType *LHSOPT = LHSTy->castAs<ObjCObjectPointerType>(); |
| 5113 | const ObjCObjectPointerType *RHSOPT = RHSTy->castAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5114 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5115 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5116 | // If both operands are interfaces and either operand can be |
| 5117 | // assigned to the other, use that type as the composite |
| 5118 | // type. This allows |
| 5119 | // xxx ? (A*) a : (B*) b |
| 5120 | // where B is a subclass of A. |
| 5121 | // |
| 5122 | // Additionally, as for assignment, if either type is 'id' |
| 5123 | // allow silent coercion. Finally, if the types are |
| 5124 | // incompatible then make sure to use 'id' as the composite |
| 5125 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5126 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5127 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 5128 | // It could return the composite type. |
| 5129 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 5130 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 5131 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 5132 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 5133 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 5134 | RHSTy->isObjCQualifiedIdType()) && |
| 5135 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 5136 | // Need to handle "id<xx>" explicitly. |
| 5137 | // GCC allows qualified id and any Objective-C type to devolve to |
| 5138 | // id. Currently localizing to here until clear this should be |
| 5139 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 5140 | compositeType = Context.getObjCIdType(); |
| 5141 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 5142 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5143 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5144 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 5145 | ; |
| 5146 | else { |
| 5147 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 5148 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5149 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5150 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5151 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 5152 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5153 | return incompatTy; |
| 5154 | } |
| 5155 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5156 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 5157 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5158 | return compositeType; |
| 5159 | } |
| 5160 | // Check Objective-C object pointer types and 'void *' |
| 5161 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5162 | if (getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | a66eccb | 2012-02-25 00:23:44 +0000 | [diff] [blame] | 5163 | // ARC forbids the implicit conversion of object pointers to 'void *', |
| 5164 | // so these types are not compatible. |
| 5165 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy |
| 5166 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5167 | LHS = RHS = true; |
| 5168 | return QualType(); |
| 5169 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5170 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5171 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5172 | QualType destPointee |
| 5173 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 5174 | QualType destType = Context.getPointerType(destPointee); |
| 5175 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5176 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5177 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5178 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5179 | return destType; |
| 5180 | } |
| 5181 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5182 | if (getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | a66eccb | 2012-02-25 00:23:44 +0000 | [diff] [blame] | 5183 | // ARC forbids the implicit conversion of object pointers to 'void *', |
| 5184 | // so these types are not compatible. |
| 5185 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy |
| 5186 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5187 | LHS = RHS = true; |
| 5188 | return QualType(); |
| 5189 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5190 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5191 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 5192 | QualType destPointee |
| 5193 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 5194 | QualType destType = Context.getPointerType(destPointee); |
| 5195 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5196 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5197 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5198 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5199 | return destType; |
| 5200 | } |
| 5201 | return QualType(); |
| 5202 | } |
| 5203 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5204 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5205 | /// ParenRange in parentheses. |
| 5206 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5207 | const PartialDiagnostic &Note, |
| 5208 | SourceRange ParenRange) { |
| 5209 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 5210 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 5211 | EndLoc.isValid()) { |
| 5212 | Self.Diag(Loc, Note) |
| 5213 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 5214 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 5215 | } else { |
| 5216 | // We can't display the parentheses, so just show the bare note. |
| 5217 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5218 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 5222 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 5223 | } |
| 5224 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5225 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 5226 | /// expression, either using a built-in or overloaded operator, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5227 | /// and sets *OpCode to the opcode and *RHSExprs to the right-hand side |
| 5228 | /// expression. |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5229 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5230 | Expr **RHSExprs) { |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 5231 | // Don't strip parenthesis: we should not warn if E is in parenthesis. |
| 5232 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5233 | E = E->IgnoreConversionOperator(); |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 5234 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5235 | |
| 5236 | // Built-in binary operator. |
| 5237 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 5238 | if (IsArithmeticOp(OP->getOpcode())) { |
| 5239 | *Opcode = OP->getOpcode(); |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5240 | *RHSExprs = OP->getRHS(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5241 | return true; |
| 5242 | } |
| 5243 | } |
| 5244 | |
| 5245 | // Overloaded operator. |
| 5246 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 5247 | if (Call->getNumArgs() != 2) |
| 5248 | return false; |
| 5249 | |
| 5250 | // Make sure this is really a binary operator that is safe to pass into |
| 5251 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 5252 | OverloadedOperatorKind OO = Call->getOperator(); |
| 5253 | if (OO < OO_Plus || OO > OO_Arrow) |
| 5254 | return false; |
| 5255 | |
| 5256 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 5257 | if (IsArithmeticOp(OpKind)) { |
| 5258 | *Opcode = OpKind; |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5259 | *RHSExprs = Call->getArg(1); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5260 | return true; |
| 5261 | } |
| 5262 | } |
| 5263 | |
| 5264 | return false; |
| 5265 | } |
| 5266 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5267 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 5268 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 5269 | } |
| 5270 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5271 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 5272 | /// or is a logical expression such as (x==y) which has int type, but is |
| 5273 | /// commonly interpreted as boolean. |
| 5274 | static bool ExprLooksBoolean(Expr *E) { |
| 5275 | E = E->IgnoreParenImpCasts(); |
| 5276 | |
| 5277 | if (E->getType()->isBooleanType()) |
| 5278 | return true; |
| 5279 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 5280 | return IsLogicOp(OP->getOpcode()); |
| 5281 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 5282 | return OP->getOpcode() == UO_LNot; |
| 5283 | |
| 5284 | return false; |
| 5285 | } |
| 5286 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5287 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 5288 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 5289 | /// the conditional operator has higher precedence, for example: |
| 5290 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 5291 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 5292 | SourceLocation OpLoc, |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5293 | Expr *Condition, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5294 | Expr *LHSExpr, |
| 5295 | Expr *RHSExpr) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5296 | BinaryOperatorKind CondOpcode; |
| 5297 | Expr *CondRHS; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5298 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5299 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5300 | return; |
| 5301 | if (!ExprLooksBoolean(CondRHS)) |
| 5302 | return; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5303 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5304 | // The condition is an arithmetic binary expression, with a right- |
| 5305 | // hand side that looks boolean, so warn. |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5306 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5307 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5308 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5309 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5310 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5311 | SuggestParentheses(Self, OpLoc, |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 5312 | Self.PDiag(diag::note_precedence_silence) |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5313 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 5314 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 5315 | |
| 5316 | SuggestParentheses(Self, OpLoc, |
| 5317 | Self.PDiag(diag::note_precedence_conditional_first), |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5318 | SourceRange(CondRHS->getLocStart(), RHSExpr->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5319 | } |
| 5320 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5321 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5322 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5323 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5324 | SourceLocation ColonLoc, |
| 5325 | Expr *CondExpr, Expr *LHSExpr, |
| 5326 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5327 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 5328 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5329 | OpaqueValueExpr *opaqueValue = 0; |
| 5330 | Expr *commonExpr = 0; |
| 5331 | if (LHSExpr == 0) { |
| 5332 | commonExpr = CondExpr; |
| 5333 | |
| 5334 | // We usually want to apply unary conversions *before* saving, except |
| 5335 | // in the special case of a C++ l-value conditional. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5336 | if (!(getLangOpts().CPlusPlus |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5337 | && !commonExpr->isTypeDependent() |
| 5338 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 5339 | && commonExpr->isGLValue() |
| 5340 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 5341 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 5342 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5343 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 5344 | if (commonRes.isInvalid()) |
| 5345 | return ExprError(); |
| 5346 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 5350 | commonExpr->getType(), |
| 5351 | commonExpr->getValueKind(), |
Douglas Gregor | 97df54e | 2012-02-23 22:17:26 +0000 | [diff] [blame] | 5352 | commonExpr->getObjectKind(), |
| 5353 | commonExpr); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5354 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5355 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5356 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5357 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5358 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5359 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 5360 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5361 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5362 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 5363 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5364 | return ExprError(); |
| 5365 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5366 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 5367 | RHS.get()); |
| 5368 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5369 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5370 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 5371 | LHS.take(), ColonLoc, |
| 5372 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5373 | |
| 5374 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5375 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5376 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
| 5377 | OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5378 | } |
| 5379 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5380 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5381 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5382 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5383 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5384 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5385 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5386 | checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) { |
| 5387 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5388 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5390 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5391 | const Type *lhptee, *rhptee; |
| 5392 | Qualifiers lhq, rhq; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5393 | llvm::tie(lhptee, lhq) = cast<PointerType>(LHSType)->getPointeeType().split(); |
| 5394 | llvm::tie(rhptee, rhq) = cast<PointerType>(RHSType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5395 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5396 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5397 | |
| 5398 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5399 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5400 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5401 | Qualifiers lq; |
| 5402 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5403 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 5404 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 5405 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 5406 | // Ignore lifetime for further calculation. |
| 5407 | lhq.removeObjCLifetime(); |
| 5408 | rhq.removeObjCLifetime(); |
| 5409 | } |
| 5410 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5411 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5412 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5413 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5414 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5415 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5416 | // 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] | 5417 | // and from void*. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5418 | else if (lhq.withoutObjCGCAttr().withoutObjCLifetime() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5419 | .compatiblyIncludes( |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5420 | rhq.withoutObjCGCAttr().withoutObjCLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5421 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 5422 | ; // keep old |
| 5423 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5424 | // Treat lifetime mismatches as fatal. |
| 5425 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 5426 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5427 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5428 | // For GCC compatibility, other qualifier mismatches are treated |
| 5429 | // as still compatible in C. |
| 5430 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5431 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5432 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5433 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5434 | // 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] | 5435 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5436 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5437 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5438 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5439 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5440 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5441 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5442 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5443 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5444 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5445 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5446 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5447 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5448 | |
| 5449 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5450 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5451 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5452 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5453 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5454 | // 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] | 5455 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5456 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5457 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5458 | // Check if the pointee types are compatible ignoring the sign. |
| 5459 | // We explicitly check for char so that we catch "char" vs |
| 5460 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5461 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5462 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5463 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5464 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5465 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5466 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5467 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5468 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5469 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5470 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5471 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5472 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5473 | // takes priority over sign incompatibility because the sign |
| 5474 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5475 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5476 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5477 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5478 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5479 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5480 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5481 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5482 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5483 | // the eventual target type is the same and the pointers have the same |
| 5484 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5485 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5486 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5487 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5488 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5489 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5490 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5491 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5492 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5493 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5494 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5495 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5496 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5497 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5498 | if (!S.getLangOpts().CPlusPlus && |
Fariborz Jahanian | 53c8167 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 5499 | S.IsNoReturnConversion(ltrans, rtrans, ltrans)) |
| 5500 | return Sema::IncompatiblePointer; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5501 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5502 | } |
| 5503 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5504 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5505 | /// block pointer types are compatible or whether a block and normal pointer |
| 5506 | /// are compatible. It is more restrict than comparing two function pointer |
| 5507 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5508 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5509 | checkBlockPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5510 | QualType RHSType) { |
| 5511 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5512 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5513 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5514 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5515 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5516 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5517 | lhptee = cast<BlockPointerType>(LHSType)->getPointeeType(); |
| 5518 | rhptee = cast<BlockPointerType>(RHSType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5519 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5520 | // In C++, the types have to match exactly. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5521 | if (S.getLangOpts().CPlusPlus) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5522 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5523 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5524 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5525 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5526 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5527 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5528 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5529 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5530 | if (!S.Context.typesAreBlockPointerCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5531 | return Sema::IncompatibleBlockPointer; |
| 5532 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5533 | return ConvTy; |
| 5534 | } |
| 5535 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5536 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5537 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5538 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5539 | checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5540 | QualType RHSType) { |
| 5541 | assert(LHSType.isCanonical() && "LHS was not canonicalized!"); |
| 5542 | assert(RHSType.isCanonical() && "RHS was not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5543 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5544 | if (LHSType->isObjCBuiltinType()) { |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5545 | // Class is not compatible with ObjC object pointers. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5546 | if (LHSType->isObjCClassType() && !RHSType->isObjCBuiltinType() && |
| 5547 | !RHSType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5548 | return Sema::IncompatiblePointer; |
| 5549 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5550 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5551 | if (RHSType->isObjCBuiltinType()) { |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5552 | if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() && |
| 5553 | !LHSType->isObjCQualifiedClassType()) |
Fariborz Jahanian | 412a496 | 2011-09-15 20:40:18 +0000 | [diff] [blame] | 5554 | return Sema::IncompatiblePointer; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5555 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5556 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5557 | QualType lhptee = LHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5558 | QualType rhptee = RHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5559 | |
Fariborz Jahanian | f2b4f7b | 2012-01-12 22:12:08 +0000 | [diff] [blame] | 5560 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee) && |
| 5561 | // make an exception for id<P> |
| 5562 | !LHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5563 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5564 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5565 | if (S.Context.typesAreCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5566 | return Sema::Compatible; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5567 | if (LHSType->isObjCQualifiedIdType() || RHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5568 | return Sema::IncompatibleObjCQualifiedId; |
| 5569 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5572 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5573 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5574 | QualType LHSType, QualType RHSType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5575 | // Fake up an opaque expression. We don't actually care about what |
| 5576 | // cast operations are required, so if CheckAssignmentConstraints |
| 5577 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5578 | // usually happen on valid code. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5579 | OpaqueValueExpr RHSExpr(Loc, RHSType, VK_RValue); |
| 5580 | ExprResult RHSPtr = &RHSExpr; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5581 | CastKind K = CK_Invalid; |
| 5582 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5583 | return CheckAssignmentConstraints(LHSType, RHSPtr, K); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5586 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5587 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5588 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5589 | /// |
| 5590 | /// int a, *pint; |
| 5591 | /// short *pshort; |
| 5592 | /// struct foo *pfoo; |
| 5593 | /// |
| 5594 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5595 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5596 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5597 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5598 | /// |
| 5599 | /// 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] | 5600 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5601 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5602 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5603 | Sema::AssignConvertType |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5604 | Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5605 | CastKind &Kind) { |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5606 | QualType RHSType = RHS.get()->getType(); |
| 5607 | QualType OrigLHSType = LHSType; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5608 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5609 | // Get canonical types. We're not formatting these types, just comparing |
| 5610 | // them. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5611 | LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType(); |
| 5612 | RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5613 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5614 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5615 | // Common case: no conversion required. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5616 | if (LHSType == RHSType) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5617 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5618 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5619 | } |
| 5620 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 5621 | // If we have an atomic type, try a non-atomic assignment, then just add an |
| 5622 | // atomic qualification step. |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5623 | if (const AtomicType *AtomicTy = dyn_cast<AtomicType>(LHSType)) { |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 5624 | Sema::AssignConvertType result = |
| 5625 | CheckAssignmentConstraints(AtomicTy->getValueType(), RHS, Kind); |
| 5626 | if (result != Compatible) |
| 5627 | return result; |
| 5628 | if (Kind != CK_NoOp) |
| 5629 | RHS = ImpCastExprToType(RHS.take(), AtomicTy->getValueType(), Kind); |
| 5630 | Kind = CK_NonAtomicToAtomic; |
| 5631 | return Compatible; |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5634 | // If the left-hand side is a reference type, then we are in a |
| 5635 | // (rare!) case where we've allowed the use of references in C, |
| 5636 | // e.g., as a parameter type in a built-in function. In this case, |
| 5637 | // just make sure that the type referenced is compatible with the |
| 5638 | // right-hand side type. The caller is responsible for adjusting |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5639 | // LHSType so that the resulting expression does not have reference |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5640 | // type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5641 | if (const ReferenceType *LHSTypeRef = LHSType->getAs<ReferenceType>()) { |
| 5642 | if (Context.typesAreCompatible(LHSTypeRef->getPointeeType(), RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5643 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5644 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5645 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5646 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5647 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5648 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5649 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5650 | // to the same ExtVector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5651 | if (LHSType->isExtVectorType()) { |
| 5652 | if (RHSType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5653 | return Incompatible; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5654 | if (RHSType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5655 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5656 | // element type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5657 | QualType elType = cast<ExtVectorType>(LHSType)->getElementType(); |
| 5658 | if (elType != RHSType) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 5659 | Kind = PrepareScalarCast(RHS, elType); |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5660 | RHS = ImpCastExprToType(RHS.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5661 | } |
| 5662 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5663 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5664 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5665 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5666 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5667 | // Conversions to or from vector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5668 | if (LHSType->isVectorType() || RHSType->isVectorType()) { |
| 5669 | if (LHSType->isVectorType() && RHSType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5670 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5671 | // vector type and vice versa |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5672 | if (Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5673 | Kind = CK_BitCast; |
| 5674 | return Compatible; |
| 5675 | } |
| 5676 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5677 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5678 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5679 | // no bits are changed but the result type is different. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5680 | if (getLangOpts().LaxVectorConversions && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5681 | (Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5682 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5683 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5684 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5685 | } |
| 5686 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5687 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5688 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5689 | // Arithmetic conversions. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5690 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5691 | !(getLangOpts().CPlusPlus && LHSType->isEnumeralType())) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 5692 | Kind = PrepareScalarCast(RHS, LHSType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5693 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5694 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5695 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5696 | // Conversions to normal pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5697 | if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5698 | // U* -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5699 | if (isa<PointerType>(RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5700 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5701 | return checkPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5702 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5703 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5704 | // int -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5705 | if (RHSType->isIntegerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5706 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5707 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5708 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5709 | |
| 5710 | // C pointers are not compatible with ObjC object pointers, |
| 5711 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5712 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5713 | // - conversions to void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5714 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5715 | Kind = CK_BitCast; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5716 | return Compatible; |
| 5717 | } |
| 5718 | |
| 5719 | // - conversions from 'Class' to the redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5720 | if (RHSType->isObjCClassType() && |
| 5721 | Context.hasSameType(LHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5722 | Context.getObjCClassRedefinitionType())) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5723 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5724 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5725 | } |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 5726 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5727 | Kind = CK_BitCast; |
| 5728 | return IncompatiblePointer; |
| 5729 | } |
| 5730 | |
| 5731 | // U^ -> void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5732 | if (RHSType->getAs<BlockPointerType>()) { |
| 5733 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5734 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5735 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5736 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5737 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5738 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5739 | return Incompatible; |
| 5740 | } |
| 5741 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5742 | // Conversions to block pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5743 | if (isa<BlockPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5744 | // U^ -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5745 | if (RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5746 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5747 | return checkBlockPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5748 | } |
| 5749 | |
| 5750 | // int or null -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5751 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5752 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5753 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5754 | } |
| 5755 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5756 | // id -> T^ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5757 | if (getLangOpts().ObjC1 && RHSType->isObjCIdType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5758 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5759 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5760 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5761 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5762 | // void* -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5763 | if (const PointerType *RHSPT = RHSType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5764 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5765 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5766 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5767 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5768 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5769 | return Incompatible; |
| 5770 | } |
| 5771 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5772 | // Conversions to Objective-C pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5773 | if (isa<ObjCObjectPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5774 | // A* -> B* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5775 | if (RHSType->isObjCObjectPointerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5776 | Kind = CK_BitCast; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5777 | Sema::AssignConvertType result = |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5778 | checkObjCPointerTypesForAssignment(*this, LHSType, RHSType); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5779 | if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5780 | result == Compatible && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5781 | !CheckObjCARCUnavailableWeakConversion(OrigLHSType, RHSType)) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5782 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5783 | return result; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5784 | } |
| 5785 | |
| 5786 | // int or null -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5787 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5788 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5789 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5792 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5793 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5794 | if (isa<PointerType>(RHSType)) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5795 | Kind = CK_CPointerToObjCPointerCast; |
| 5796 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5797 | // - conversions from 'void*' |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5798 | if (RHSType->isVoidPointerType()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5799 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5800 | } |
| 5801 | |
| 5802 | // - conversions to 'Class' from its redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5803 | if (LHSType->isObjCClassType() && |
| 5804 | Context.hasSameType(RHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5805 | Context.getObjCClassRedefinitionType())) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5806 | return Compatible; |
| 5807 | } |
| 5808 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5809 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5810 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5811 | |
| 5812 | // T^ -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5813 | if (RHSType->isBlockPointerType()) { |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 5814 | maybeExtendBlockObject(*this, RHS); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5815 | Kind = CK_BlockPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5816 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5817 | } |
| 5818 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5819 | return Incompatible; |
| 5820 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5821 | |
| 5822 | // Conversions from pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5823 | if (isa<PointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5824 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5825 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5826 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5827 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5828 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5829 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5830 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5831 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5832 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5833 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5834 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5835 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5836 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5837 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5838 | |
| 5839 | // Conversions from Objective-C pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5840 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5841 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5842 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5843 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5844 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5845 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5846 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5847 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5848 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5849 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5850 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5851 | } |
| 5852 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5853 | return Incompatible; |
| 5854 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5855 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5856 | // struct A -> struct B |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5857 | if (isa<TagType>(LHSType) && isa<TagType>(RHSType)) { |
| 5858 | if (Context.typesAreCompatible(LHSType, RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5859 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5860 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5861 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5862 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5863 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5864 | return Incompatible; |
| 5865 | } |
| 5866 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5867 | /// \brief Constructs a transparent union from an expression that is |
| 5868 | /// used to initialize the transparent union. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5869 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
| 5870 | ExprResult &EResult, QualType UnionType, |
| 5871 | FieldDecl *Field) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5872 | // Build an initializer list that designates the appropriate member |
| 5873 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5874 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5875 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5876 | E, SourceLocation()); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5877 | Initializer->setType(UnionType); |
| 5878 | Initializer->setInitializedFieldInUnion(Field); |
| 5879 | |
| 5880 | // Build a compound literal constructing a value of the transparent |
| 5881 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5882 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5883 | EResult = S.Owned( |
| 5884 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5885 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | Sema::AssignConvertType |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5889 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5890 | ExprResult &RHS) { |
| 5891 | QualType RHSType = RHS.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5892 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5893 | // 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] | 5894 | // transparent_union GCC extension. |
| 5895 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5896 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5897 | return Incompatible; |
| 5898 | |
| 5899 | // The field to initialize within the transparent union. |
| 5900 | RecordDecl *UD = UT->getDecl(); |
| 5901 | FieldDecl *InitField = 0; |
| 5902 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5903 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5904 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5905 | it != itend; ++it) { |
| 5906 | if (it->getType()->isPointerType()) { |
| 5907 | // If the transparent union contains a pointer type, we allow: |
| 5908 | // 1) void pointer |
| 5909 | // 2) null pointer constant |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5910 | if (RHSType->isPointerType()) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5911 | if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5912 | RHS = ImpCastExprToType(RHS.take(), it->getType(), CK_BitCast); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5913 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5914 | break; |
| 5915 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5916 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5917 | if (RHS.get()->isNullPointerConstant(Context, |
| 5918 | Expr::NPC_ValueDependentIsNull)) { |
| 5919 | RHS = ImpCastExprToType(RHS.take(), it->getType(), |
| 5920 | CK_NullToPointer); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5921 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5922 | break; |
| 5923 | } |
| 5924 | } |
| 5925 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5926 | CastKind Kind = CK_Invalid; |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5927 | if (CheckAssignmentConstraints(it->getType(), RHS, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5928 | == Compatible) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5929 | RHS = ImpCastExprToType(RHS.take(), it->getType(), Kind); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5930 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5931 | break; |
| 5932 | } |
| 5933 | } |
| 5934 | |
| 5935 | if (!InitField) |
| 5936 | return Incompatible; |
| 5937 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5938 | ConstructTransparentUnion(*this, Context, RHS, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5939 | return Compatible; |
| 5940 | } |
| 5941 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5942 | Sema::AssignConvertType |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5943 | Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
| 5944 | bool Diagnose) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5945 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5946 | if (!LHSType->isRecordType() && !LHSType->isAtomicType()) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5947 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5948 | // expression is implicitly converted (C++ 4) to the |
| 5949 | // cv-unqualified type of the left operand. |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 5950 | ExprResult Res; |
| 5951 | if (Diagnose) { |
| 5952 | Res = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 5953 | AA_Assigning); |
| 5954 | } else { |
| 5955 | ImplicitConversionSequence ICS = |
| 5956 | TryImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 5957 | /*SuppressUserConversions=*/false, |
| 5958 | /*AllowExplicit=*/false, |
| 5959 | /*InOverloadResolution=*/false, |
| 5960 | /*CStyle=*/false, |
| 5961 | /*AllowObjCWritebackConversion=*/false); |
| 5962 | if (ICS.isFailure()) |
| 5963 | return Incompatible; |
| 5964 | Res = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 5965 | ICS, AA_Assigning); |
| 5966 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5967 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5968 | return Incompatible; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5969 | Sema::AssignConvertType result = Compatible; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5970 | if (getLangOpts().ObjCAutoRefCount && |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5971 | !CheckObjCARCUnavailableWeakConversion(LHSType, |
| 5972 | RHS.get()->getType())) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5973 | result = IncompatibleObjCWeakRef; |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5974 | RHS = Res; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5975 | return result; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5976 | } |
| 5977 | |
| 5978 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5979 | // structures. |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5980 | // FIXME: We also fall through for atomics; not sure what should |
| 5981 | // happen there, though. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5982 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5983 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5984 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5985 | // a null pointer constant. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5986 | if ((LHSType->isPointerType() || |
| 5987 | LHSType->isObjCObjectPointerType() || |
| 5988 | LHSType->isBlockPointerType()) |
| 5989 | && RHS.get()->isNullPointerConstant(Context, |
| 5990 | Expr::NPC_ValueDependentIsNull)) { |
| 5991 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5992 | return Compatible; |
| 5993 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5994 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5995 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5996 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5997 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5998 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5999 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6000 | // Suppress this for references: C++ 8.5.3p5. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6001 | if (!LHSType->isReferenceType()) { |
| 6002 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 6003 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6004 | return Incompatible; |
| 6005 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6006 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6007 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6008 | Sema::AssignConvertType result = |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6009 | CheckAssignmentConstraints(LHSType, RHS, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6010 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6011 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 6012 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6013 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 6014 | // so that we can use references in built-in functions even in C. |
| 6015 | // The getNonReferenceType() call makes sure that the resulting expression |
| 6016 | // does not have reference type. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6017 | if (result != Incompatible && RHS.get()->getType() != LHSType) |
| 6018 | RHS = ImpCastExprToType(RHS.take(), |
| 6019 | LHSType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6020 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6023 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, |
| 6024 | ExprResult &RHS) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6025 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6026 | << LHS.get()->getType() << RHS.get()->getType() |
| 6027 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6028 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6029 | } |
| 6030 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6031 | QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6032 | SourceLocation Loc, bool IsCompAssign) { |
Richard Smith | 9c129f8 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 6033 | if (!IsCompAssign) { |
| 6034 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 6035 | if (LHS.isInvalid()) |
| 6036 | return QualType(); |
| 6037 | } |
| 6038 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 6039 | if (RHS.isInvalid()) |
| 6040 | return QualType(); |
| 6041 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6042 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6043 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6044 | QualType LHSType = |
| 6045 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 6046 | QualType RHSType = |
| 6047 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6048 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6049 | // If the vector types are identical, return. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6050 | if (LHSType == RHSType) |
| 6051 | return LHSType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6052 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6053 | // Handle the case of equivalent AltiVec and GCC vector types |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6054 | if (LHSType->isVectorType() && RHSType->isVectorType() && |
| 6055 | Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
| 6056 | if (LHSType->isExtVectorType()) { |
| 6057 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 6058 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6061 | if (!IsCompAssign) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6062 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
| 6063 | return RHSType; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6064 | } |
| 6065 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6066 | if (getLangOpts().LaxVectorConversions && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6067 | Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType)) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6068 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6069 | // vectors, the total size only needs to be the same. This is a |
| 6070 | // bitcast; no bits are changed but the result type is different. |
| 6071 | // FIXME: Should we really be allowing this? |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6072 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 6073 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6074 | } |
| 6075 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6076 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 6077 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 6078 | bool swapped = false; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6079 | if (RHSType->isExtVectorType() && !IsCompAssign) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6080 | swapped = true; |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6081 | std::swap(RHS, LHS); |
| 6082 | std::swap(RHSType, LHSType); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6083 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6084 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6085 | // Handle the case of an ext vector and scalar. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6086 | if (const ExtVectorType *LV = LHSType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6087 | QualType EltTy = LV->getElementType(); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6088 | if (EltTy->isIntegralType(Context) && RHSType->isIntegralType(Context)) { |
| 6089 | int order = Context.getIntegerTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6090 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6091 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6092 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6093 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 6094 | if (swapped) std::swap(RHS, LHS); |
| 6095 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6096 | } |
| 6097 | } |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6098 | if (EltTy->isRealFloatingType() && RHSType->isScalarType() && |
| 6099 | RHSType->isRealFloatingType()) { |
| 6100 | int order = Context.getFloatingTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6101 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6102 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6103 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6104 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 6105 | if (swapped) std::swap(RHS, LHS); |
| 6106 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6107 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6108 | } |
| 6109 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6110 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6111 | // Vectors of different size or scalar and non-ext-vector are errors. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6112 | if (swapped) std::swap(RHS, LHS); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6113 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6114 | << LHS.get()->getType() << RHS.get()->getType() |
| 6115 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6116 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6119 | // checkArithmeticNull - Detect when a NULL constant is used improperly in an |
| 6120 | // expression. These are mainly cases where the null pointer is used as an |
| 6121 | // integer instead of a pointer. |
| 6122 | static void checkArithmeticNull(Sema &S, ExprResult &LHS, ExprResult &RHS, |
| 6123 | SourceLocation Loc, bool IsCompare) { |
| 6124 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 6125 | // but we use a bit of a hack here for speed; this is a relatively |
| 6126 | // hot path, and isNullPointerConstant is slow. |
| 6127 | bool LHSNull = isa<GNUNullExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 6128 | bool RHSNull = isa<GNUNullExpr>(RHS.get()->IgnoreParenImpCasts()); |
| 6129 | |
| 6130 | QualType NonNullType = LHSNull ? RHS.get()->getType() : LHS.get()->getType(); |
| 6131 | |
| 6132 | // Avoid analyzing cases where the result will either be invalid (and |
| 6133 | // diagnosed as such) or entirely valid and not something to warn about. |
| 6134 | if ((!LHSNull && !RHSNull) || NonNullType->isBlockPointerType() || |
| 6135 | NonNullType->isMemberPointerType() || NonNullType->isFunctionType()) |
| 6136 | return; |
| 6137 | |
| 6138 | // Comparison operations would not make sense with a null pointer no matter |
| 6139 | // what the other expression is. |
| 6140 | if (!IsCompare) { |
| 6141 | S.Diag(Loc, diag::warn_null_in_arithmetic_operation) |
| 6142 | << (LHSNull ? LHS.get()->getSourceRange() : SourceRange()) |
| 6143 | << (RHSNull ? RHS.get()->getSourceRange() : SourceRange()); |
| 6144 | return; |
| 6145 | } |
| 6146 | |
| 6147 | // The rest of the operations only make sense with a null pointer |
| 6148 | // if the other expression is a pointer. |
| 6149 | if (LHSNull == RHSNull || NonNullType->isAnyPointerType() || |
| 6150 | NonNullType->canDecayToPointerType()) |
| 6151 | return; |
| 6152 | |
| 6153 | S.Diag(Loc, diag::warn_null_in_comparison_operation) |
| 6154 | << LHSNull /* LHS is NULL */ << NonNullType |
| 6155 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6156 | } |
| 6157 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6158 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6159 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6160 | bool IsCompAssign, bool IsDiv) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6161 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6162 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6163 | if (LHS.get()->getType()->isVectorType() || |
| 6164 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6165 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6166 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6167 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6168 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6169 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6170 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6171 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6172 | if (compType.isNull() || !compType->isArithmeticType()) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6173 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6174 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6175 | // Check for division by zero. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6176 | if (IsDiv && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6177 | RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6178 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6179 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_division_by_zero) |
| 6180 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6181 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6182 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6183 | } |
| 6184 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6185 | QualType Sema::CheckRemainderOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6186 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6187 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6188 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6189 | if (LHS.get()->getType()->isVectorType() || |
| 6190 | RHS.get()->getType()->isVectorType()) { |
| 6191 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 6192 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6193 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6194 | return InvalidOperands(Loc, LHS, RHS); |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6195 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6196 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6197 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6198 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6199 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6200 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6201 | if (compType.isNull() || !compType->isIntegerType()) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6202 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6203 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6204 | // Check for remainder by zero. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6205 | if (RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6206 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6207 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_remainder_by_zero) |
| 6208 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6209 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6210 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6211 | } |
| 6212 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6213 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 6214 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6215 | Expr *LHSExpr, Expr *RHSExpr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6216 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6217 | ? diag::err_typecheck_pointer_arith_void_type |
| 6218 | : diag::ext_gnu_void_ptr) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6219 | << 1 /* two pointers */ << LHSExpr->getSourceRange() |
| 6220 | << RHSExpr->getSourceRange(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 6224 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 6225 | Expr *Pointer) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6226 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6227 | ? diag::err_typecheck_pointer_arith_void_type |
| 6228 | : diag::ext_gnu_void_ptr) |
| 6229 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 6230 | } |
| 6231 | |
| 6232 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 6233 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 6234 | Expr *LHS, Expr *RHS) { |
| 6235 | assert(LHS->getType()->isAnyPointerType()); |
| 6236 | assert(RHS->getType()->isAnyPointerType()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6237 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6238 | ? diag::err_typecheck_pointer_arith_function_type |
| 6239 | : diag::ext_gnu_ptr_func_arith) |
| 6240 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 6241 | // We only show the second type if it differs from the first. |
| 6242 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 6243 | RHS->getType()) |
| 6244 | << RHS->getType()->getPointeeType() |
| 6245 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 6246 | } |
| 6247 | |
| 6248 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 6249 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 6250 | Expr *Pointer) { |
| 6251 | assert(Pointer->getType()->isAnyPointerType()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6252 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6253 | ? diag::err_typecheck_pointer_arith_function_type |
| 6254 | : diag::ext_gnu_ptr_func_arith) |
| 6255 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 6256 | << 0 /* one pointer, so only one type */ |
| 6257 | << Pointer->getSourceRange(); |
| 6258 | } |
| 6259 | |
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 6260 | /// \brief Emit error if Operand is incomplete pointer type |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6261 | /// |
| 6262 | /// \returns True if pointer has incomplete type |
| 6263 | static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, |
| 6264 | Expr *Operand) { |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6265 | assert(Operand->getType()->isAnyPointerType() && |
| 6266 | !Operand->getType()->isDependentType()); |
| 6267 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 6268 | return S.RequireCompleteType(Loc, PointeeTy, |
| 6269 | diag::err_typecheck_arithmetic_incomplete_type, |
| 6270 | PointeeTy, Operand->getSourceRange()); |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6271 | } |
| 6272 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6273 | /// \brief Check the validity of an arithmetic pointer operand. |
| 6274 | /// |
| 6275 | /// If the operand has pointer type, this code will check for pointer types |
| 6276 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 6277 | /// appropriately, including whether or not the use is supported as an |
| 6278 | /// extension. |
| 6279 | /// |
| 6280 | /// \returns True when the operand is valid to use (even if as an extension). |
| 6281 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 6282 | Expr *Operand) { |
| 6283 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 6284 | |
| 6285 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 6286 | if (PointeeTy->isVoidType()) { |
| 6287 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6288 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6289 | } |
| 6290 | if (PointeeTy->isFunctionType()) { |
| 6291 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6292 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6293 | } |
| 6294 | |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6295 | if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6296 | |
| 6297 | return true; |
| 6298 | } |
| 6299 | |
| 6300 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 6301 | /// operands. |
| 6302 | /// |
| 6303 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 6304 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 6305 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 6306 | /// are (potentially problematic) pointers. |
| 6307 | /// |
| 6308 | /// \returns True when the operand is valid to use (even if as an extension). |
| 6309 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6310 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6311 | bool isLHSPointer = LHSExpr->getType()->isAnyPointerType(); |
| 6312 | bool isRHSPointer = RHSExpr->getType()->isAnyPointerType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6313 | if (!isLHSPointer && !isRHSPointer) return true; |
| 6314 | |
| 6315 | QualType LHSPointeeTy, RHSPointeeTy; |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6316 | if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType(); |
| 6317 | if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6318 | |
| 6319 | // Check for arithmetic on pointers to incomplete types. |
| 6320 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 6321 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 6322 | if (isLHSVoidPtr || isRHSVoidPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6323 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHSExpr); |
| 6324 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHSExpr); |
| 6325 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6326 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6327 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6328 | } |
| 6329 | |
| 6330 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 6331 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 6332 | if (isLHSFuncPtr || isRHSFuncPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6333 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHSExpr); |
| 6334 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, |
| 6335 | RHSExpr); |
| 6336 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6337 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6338 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6339 | } |
| 6340 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6341 | if (isLHSPointer && checkArithmeticIncompletePointerType(S, Loc, LHSExpr)) |
| 6342 | return false; |
| 6343 | if (isRHSPointer && checkArithmeticIncompletePointerType(S, Loc, RHSExpr)) |
| 6344 | return false; |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6345 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6346 | return true; |
| 6347 | } |
| 6348 | |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6349 | /// diagnoseStringPlusInt - Emit a warning when adding an integer to a string |
| 6350 | /// literal. |
| 6351 | static void diagnoseStringPlusInt(Sema &Self, SourceLocation OpLoc, |
| 6352 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6353 | StringLiteral* StrExpr = dyn_cast<StringLiteral>(LHSExpr->IgnoreImpCasts()); |
| 6354 | Expr* IndexExpr = RHSExpr; |
| 6355 | if (!StrExpr) { |
| 6356 | StrExpr = dyn_cast<StringLiteral>(RHSExpr->IgnoreImpCasts()); |
| 6357 | IndexExpr = LHSExpr; |
| 6358 | } |
| 6359 | |
| 6360 | bool IsStringPlusInt = StrExpr && |
| 6361 | IndexExpr->getType()->isIntegralOrUnscopedEnumerationType(); |
| 6362 | if (!IsStringPlusInt) |
| 6363 | return; |
| 6364 | |
| 6365 | llvm::APSInt index; |
| 6366 | if (IndexExpr->EvaluateAsInt(index, Self.getASTContext())) { |
| 6367 | unsigned StrLenWithNull = StrExpr->getLength() + 1; |
| 6368 | if (index.isNonNegative() && |
| 6369 | index <= llvm::APSInt(llvm::APInt(index.getBitWidth(), StrLenWithNull), |
| 6370 | index.isUnsigned())) |
| 6371 | return; |
| 6372 | } |
| 6373 | |
| 6374 | SourceRange DiagRange(LHSExpr->getLocStart(), RHSExpr->getLocEnd()); |
| 6375 | Self.Diag(OpLoc, diag::warn_string_plus_int) |
| 6376 | << DiagRange << IndexExpr->IgnoreImpCasts()->getType(); |
| 6377 | |
| 6378 | // Only print a fixit for "str" + int, not for int + "str". |
| 6379 | if (IndexExpr == RHSExpr) { |
| 6380 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(RHSExpr->getLocEnd()); |
| 6381 | Self.Diag(OpLoc, diag::note_string_plus_int_silence) |
| 6382 | << FixItHint::CreateInsertion(LHSExpr->getLocStart(), "&") |
| 6383 | << FixItHint::CreateReplacement(SourceRange(OpLoc), "[") |
| 6384 | << FixItHint::CreateInsertion(EndLoc, "]"); |
| 6385 | } else |
| 6386 | Self.Diag(OpLoc, diag::note_string_plus_int_silence); |
| 6387 | } |
| 6388 | |
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 6389 | /// \brief Emit error when two pointers are incompatible. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6390 | static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6391 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6392 | assert(LHSExpr->getType()->isAnyPointerType()); |
| 6393 | assert(RHSExpr->getType()->isAnyPointerType()); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6394 | S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6395 | << LHSExpr->getType() << RHSExpr->getType() << LHSExpr->getSourceRange() |
| 6396 | << RHSExpr->getSourceRange(); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6399 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6400 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc, |
| 6401 | QualType* CompLHSTy) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6402 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6403 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6404 | if (LHS.get()->getType()->isVectorType() || |
| 6405 | RHS.get()->getType()->isVectorType()) { |
| 6406 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6407 | if (CompLHSTy) *CompLHSTy = compType; |
| 6408 | return compType; |
| 6409 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6410 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6411 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6412 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6413 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6414 | |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6415 | // Diagnose "string literal" '+' int. |
| 6416 | if (Opc == BO_Add) |
| 6417 | diagnoseStringPlusInt(*this, Loc, LHS.get(), RHS.get()); |
| 6418 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6419 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6420 | if (!compType.isNull() && compType->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6421 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6422 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6423 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6424 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6425 | // Type-checking. Ultimately the pointer's going to be in PExp; |
| 6426 | // note that we bias towards the LHS being the pointer. |
| 6427 | Expr *PExp = LHS.get(), *IExp = RHS.get(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6428 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6429 | bool isObjCPointer; |
| 6430 | if (PExp->getType()->isPointerType()) { |
| 6431 | isObjCPointer = false; |
| 6432 | } else if (PExp->getType()->isObjCObjectPointerType()) { |
| 6433 | isObjCPointer = true; |
| 6434 | } else { |
| 6435 | std::swap(PExp, IExp); |
| 6436 | if (PExp->getType()->isPointerType()) { |
| 6437 | isObjCPointer = false; |
| 6438 | } else if (PExp->getType()->isObjCObjectPointerType()) { |
| 6439 | isObjCPointer = true; |
| 6440 | } else { |
| 6441 | return InvalidOperands(Loc, LHS, RHS); |
| 6442 | } |
| 6443 | } |
| 6444 | assert(PExp->getType()->isAnyPointerType()); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6445 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6446 | if (!IExp->getType()->isIntegerType()) |
| 6447 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6448 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6449 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 6450 | return QualType(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6451 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6452 | if (isObjCPointer && checkArithmeticOnObjCPointer(*this, Loc, PExp)) |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6453 | return QualType(); |
| 6454 | |
| 6455 | // Check array bounds for pointer arithemtic |
| 6456 | CheckArrayAccess(PExp, IExp); |
| 6457 | |
| 6458 | if (CompLHSTy) { |
| 6459 | QualType LHSTy = Context.isPromotableBitField(LHS.get()); |
| 6460 | if (LHSTy.isNull()) { |
| 6461 | LHSTy = LHS.get()->getType(); |
| 6462 | if (LHSTy->isPromotableIntegerType()) |
| 6463 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6464 | } |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6465 | *CompLHSTy = LHSTy; |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6466 | } |
| 6467 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6468 | return PExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6469 | } |
| 6470 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6471 | // C99 6.5.6 |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6472 | QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6473 | SourceLocation Loc, |
| 6474 | QualType* CompLHSTy) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6475 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6476 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6477 | if (LHS.get()->getType()->isVectorType() || |
| 6478 | RHS.get()->getType()->isVectorType()) { |
| 6479 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6480 | if (CompLHSTy) *CompLHSTy = compType; |
| 6481 | return compType; |
| 6482 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6483 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6484 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6485 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6486 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6487 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6488 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6489 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6490 | // Handle the common case first (both operands are arithmetic). |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6491 | if (!compType.isNull() && compType->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6492 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6493 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6494 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6495 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6496 | // Either ptr - int or ptr - ptr. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6497 | if (LHS.get()->getType()->isAnyPointerType()) { |
| 6498 | QualType lpointee = LHS.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6499 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6500 | // Diagnose bad cases where we step over interface counts. |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6501 | if (LHS.get()->getType()->isObjCObjectPointerType() && |
| 6502 | checkArithmeticOnObjCPointer(*this, Loc, LHS.get())) |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6503 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6504 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6505 | // The result type of a pointer-int computation is the pointer type. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6506 | if (RHS.get()->getType()->isIntegerType()) { |
| 6507 | if (!checkArithmeticOpPointerOperand(*this, Loc, LHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6508 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6509 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6510 | // Check array bounds for pointer arithemtic |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 6511 | CheckArrayAccess(LHS.get(), RHS.get(), /*ArraySubscriptExpr*/0, |
| 6512 | /*AllowOnePastEnd*/true, /*IndexNegated*/true); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6513 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6514 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
| 6515 | return LHS.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6516 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6517 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6518 | // Handle pointer-pointer subtractions. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6519 | if (const PointerType *RHSPTy |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6520 | = RHS.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6521 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6522 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6523 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6524 | // Pointee types must be the same: C++ [expr.add] |
| 6525 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6526 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6527 | } |
| 6528 | } else { |
| 6529 | // Pointee types must be compatible C99 6.5.6p3 |
| 6530 | if (!Context.typesAreCompatible( |
| 6531 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6532 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6533 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6534 | return QualType(); |
| 6535 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6536 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6537 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6538 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6539 | LHS.get(), RHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6540 | return QualType(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6541 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6542 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6543 | return Context.getPointerDiffType(); |
| 6544 | } |
| 6545 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6546 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6547 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6548 | } |
| 6549 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6550 | static bool isScopedEnumerationType(QualType T) { |
| 6551 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6552 | return ET->getDecl()->isScoped(); |
| 6553 | return false; |
| 6554 | } |
| 6555 | |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6556 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6557 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6558 | QualType LHSType) { |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6559 | llvm::APSInt Right; |
| 6560 | // Check right/shifter operand |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6561 | if (RHS.get()->isValueDependent() || |
| 6562 | !RHS.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6563 | return; |
| 6564 | |
| 6565 | if (Right.isNegative()) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6566 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6567 | S.PDiag(diag::warn_shift_negative) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6568 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6569 | return; |
| 6570 | } |
| 6571 | llvm::APInt LeftBits(Right.getBitWidth(), |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6572 | S.Context.getTypeSize(LHS.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6573 | if (Right.uge(LeftBits)) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6574 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6575 | S.PDiag(diag::warn_shift_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6576 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6577 | return; |
| 6578 | } |
| 6579 | if (Opc != BO_Shl) |
| 6580 | return; |
| 6581 | |
| 6582 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6583 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6584 | // integers have defined behavior modulo one more than the maximum value |
| 6585 | // representable in the result type, so never warn for those. |
| 6586 | llvm::APSInt Left; |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6587 | if (LHS.get()->isValueDependent() || |
| 6588 | !LHS.get()->isIntegerConstantExpr(Left, S.Context) || |
| 6589 | LHSType->hasUnsignedIntegerRepresentation()) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6590 | return; |
| 6591 | llvm::APInt ResultBits = |
| 6592 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6593 | if (LeftBits.uge(ResultBits)) |
| 6594 | return; |
| 6595 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6596 | Result = Result.shl(Right); |
| 6597 | |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6598 | // Print the bit representation of the signed integer as an unsigned |
| 6599 | // hexadecimal number. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6600 | SmallString<40> HexResult; |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6601 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 6602 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6603 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6604 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6605 | // expected value. Thus we place this behind a different warning that can be |
| 6606 | // turned off separately if needed. |
| 6607 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6608 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6609 | << HexResult.str() << LHSType |
| 6610 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6611 | return; |
| 6612 | } |
| 6613 | |
| 6614 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6615 | << HexResult.str() << Result.getMinSignedBits() << LHSType |
| 6616 | << Left.getBitWidth() << LHS.get()->getSourceRange() |
| 6617 | << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6618 | } |
| 6619 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6620 | // C99 6.5.7 |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6621 | QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6622 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6623 | bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6624 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6625 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6626 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6627 | if (!LHS.get()->getType()->hasIntegerRepresentation() || |
| 6628 | !RHS.get()->getType()->hasIntegerRepresentation()) |
| 6629 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6630 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6631 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6632 | // hasIntegerRepresentation() above instead of this. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6633 | if (isScopedEnumerationType(LHS.get()->getType()) || |
| 6634 | isScopedEnumerationType(RHS.get()->getType())) { |
| 6635 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6636 | } |
| 6637 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6638 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6639 | if (LHS.get()->getType()->isVectorType() || |
| 6640 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6641 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6642 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6643 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6644 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6645 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6646 | // For the LHS, do usual unary conversions, but then reset them away |
| 6647 | // if this is a compound assignment. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6648 | ExprResult OldLHS = LHS; |
| 6649 | LHS = UsualUnaryConversions(LHS.take()); |
| 6650 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6651 | return QualType(); |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6652 | QualType LHSType = LHS.get()->getType(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6653 | if (IsCompAssign) LHS = OldLHS; |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6654 | |
| 6655 | // The RHS is simpler. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6656 | RHS = UsualUnaryConversions(RHS.take()); |
| 6657 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6658 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6659 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6660 | // Sanity-check shift operands |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6661 | DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6662 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6663 | // "The type of the result is that of the promoted left operand." |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6664 | return LHSType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6665 | } |
| 6666 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6667 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6668 | if (DeclContext *DC = D->getDeclContext()) { |
| 6669 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6670 | return true; |
| 6671 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6672 | return FD->isFunctionTemplateSpecialization(); |
| 6673 | } |
| 6674 | return false; |
| 6675 | } |
| 6676 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6677 | /// If two different enums are compared, raise a warning. |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6678 | static void checkEnumComparison(Sema &S, SourceLocation Loc, ExprResult &LHS, |
| 6679 | ExprResult &RHS) { |
| 6680 | QualType LHSStrippedType = LHS.get()->IgnoreParenImpCasts()->getType(); |
| 6681 | QualType RHSStrippedType = RHS.get()->IgnoreParenImpCasts()->getType(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6682 | |
| 6683 | const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>(); |
| 6684 | if (!LHSEnumType) |
| 6685 | return; |
| 6686 | const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>(); |
| 6687 | if (!RHSEnumType) |
| 6688 | return; |
| 6689 | |
| 6690 | // Ignore anonymous enums. |
| 6691 | if (!LHSEnumType->getDecl()->getIdentifier()) |
| 6692 | return; |
| 6693 | if (!RHSEnumType->getDecl()->getIdentifier()) |
| 6694 | return; |
| 6695 | |
| 6696 | if (S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) |
| 6697 | return; |
| 6698 | |
| 6699 | S.Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6700 | << LHSStrippedType << RHSStrippedType |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6701 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6702 | } |
| 6703 | |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6704 | /// \brief Diagnose bad pointer comparisons. |
| 6705 | static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6706 | ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6707 | bool IsError) { |
| 6708 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_distinct_pointers |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6709 | : diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6710 | << LHS.get()->getType() << RHS.get()->getType() |
| 6711 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6712 | } |
| 6713 | |
| 6714 | /// \brief Returns false if the pointers are converted to a composite type, |
| 6715 | /// true otherwise. |
| 6716 | static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6717 | ExprResult &LHS, ExprResult &RHS) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6718 | // C++ [expr.rel]p2: |
| 6719 | // [...] Pointer conversions (4.10) and qualification |
| 6720 | // conversions (4.4) are performed on pointer operands (or on |
| 6721 | // a pointer operand and a null pointer constant) to bring |
| 6722 | // them to their composite pointer type. [...] |
| 6723 | // |
| 6724 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
| 6725 | // comparisons of pointers. |
| 6726 | |
| 6727 | // C++ [expr.eq]p2: |
| 6728 | // In addition, pointers to members can be compared, or a pointer to |
| 6729 | // member and a null pointer constant. Pointer to member conversions |
| 6730 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6731 | // them to a common type. If one operand is a null pointer constant, |
| 6732 | // the common type is the type of the other operand. Otherwise, the |
| 6733 | // common type is a pointer to member type similar (4.4) to the type |
| 6734 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6735 | // that is the union of the cv-qualification signatures of the operand |
| 6736 | // types. |
| 6737 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6738 | QualType LHSType = LHS.get()->getType(); |
| 6739 | QualType RHSType = RHS.get()->getType(); |
| 6740 | assert((LHSType->isPointerType() && RHSType->isPointerType()) || |
| 6741 | (LHSType->isMemberPointerType() && RHSType->isMemberPointerType())); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6742 | |
| 6743 | bool NonStandardCompositeType = false; |
Richard Trieu | 43dff1b | 2011-09-02 21:44:27 +0000 | [diff] [blame] | 6744 | bool *BoolPtr = S.isSFINAEContext() ? 0 : &NonStandardCompositeType; |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6745 | QualType T = S.FindCompositePointerType(Loc, LHS, RHS, BoolPtr); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6746 | if (T.isNull()) { |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6747 | diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6748 | return true; |
| 6749 | } |
| 6750 | |
| 6751 | if (NonStandardCompositeType) |
| 6752 | S.Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6753 | << LHSType << RHSType << T << LHS.get()->getSourceRange() |
| 6754 | << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6755 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6756 | LHS = S.ImpCastExprToType(LHS.take(), T, CK_BitCast); |
| 6757 | RHS = S.ImpCastExprToType(RHS.take(), T, CK_BitCast); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6758 | return false; |
| 6759 | } |
| 6760 | |
| 6761 | static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6762 | ExprResult &LHS, |
| 6763 | ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6764 | bool IsError) { |
| 6765 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_fptr_to_void |
| 6766 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6767 | << LHS.get()->getType() << RHS.get()->getType() |
| 6768 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6769 | } |
| 6770 | |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6771 | static bool isObjCObjectLiteral(ExprResult &E) { |
| 6772 | switch (E.get()->getStmtClass()) { |
| 6773 | case Stmt::ObjCArrayLiteralClass: |
| 6774 | case Stmt::ObjCDictionaryLiteralClass: |
| 6775 | case Stmt::ObjCStringLiteralClass: |
| 6776 | case Stmt::ObjCBoxedExprClass: |
| 6777 | return true; |
| 6778 | default: |
| 6779 | // Note that ObjCBoolLiteral is NOT an object literal! |
| 6780 | return false; |
| 6781 | } |
| 6782 | } |
| 6783 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6784 | static bool hasIsEqualMethod(Sema &S, const Expr *LHS, const Expr *RHS) { |
| 6785 | // Get the LHS object's interface type. |
| 6786 | QualType Type = LHS->getType(); |
| 6787 | QualType InterfaceType; |
| 6788 | if (const ObjCObjectPointerType *PTy = Type->getAs<ObjCObjectPointerType>()) { |
| 6789 | InterfaceType = PTy->getPointeeType(); |
| 6790 | if (const ObjCObjectType *iQFaceTy = |
| 6791 | InterfaceType->getAsObjCQualifiedInterfaceType()) |
| 6792 | InterfaceType = iQFaceTy->getBaseType(); |
| 6793 | } else { |
| 6794 | // If this is not actually an Objective-C object, bail out. |
| 6795 | return false; |
| 6796 | } |
| 6797 | |
| 6798 | // If the RHS isn't an Objective-C object, bail out. |
| 6799 | if (!RHS->getType()->isObjCObjectPointerType()) |
| 6800 | return false; |
| 6801 | |
| 6802 | // Try to find the -isEqual: method. |
| 6803 | Selector IsEqualSel = S.NSAPIObj->getIsEqualSelector(); |
| 6804 | ObjCMethodDecl *Method = S.LookupMethodInObjectType(IsEqualSel, |
| 6805 | InterfaceType, |
| 6806 | /*instance=*/true); |
| 6807 | if (!Method) { |
| 6808 | if (Type->isObjCIdType()) { |
| 6809 | // For 'id', just check the global pool. |
| 6810 | Method = S.LookupInstanceMethodInGlobalPool(IsEqualSel, SourceRange(), |
| 6811 | /*receiverId=*/true, |
| 6812 | /*warn=*/false); |
| 6813 | } else { |
| 6814 | // Check protocols. |
| 6815 | Method = S.LookupMethodInQualifiedType(IsEqualSel, |
| 6816 | cast<ObjCObjectPointerType>(Type), |
| 6817 | /*instance=*/true); |
| 6818 | } |
| 6819 | } |
| 6820 | |
| 6821 | if (!Method) |
| 6822 | return false; |
| 6823 | |
| 6824 | QualType T = Method->param_begin()[0]->getType(); |
| 6825 | if (!T->isObjCObjectPointerType()) |
| 6826 | return false; |
| 6827 | |
| 6828 | QualType R = Method->getResultType(); |
| 6829 | if (!R->isScalarType()) |
| 6830 | return false; |
| 6831 | |
| 6832 | return true; |
| 6833 | } |
| 6834 | |
| 6835 | static void diagnoseObjCLiteralComparison(Sema &S, SourceLocation Loc, |
| 6836 | ExprResult &LHS, ExprResult &RHS, |
| 6837 | BinaryOperator::Opcode Opc){ |
Jordan Rose | d5209ae | 2012-07-17 17:46:48 +0000 | [diff] [blame] | 6838 | Expr *Literal; |
| 6839 | Expr *Other; |
| 6840 | if (isObjCObjectLiteral(LHS)) { |
| 6841 | Literal = LHS.get(); |
| 6842 | Other = RHS.get(); |
| 6843 | } else { |
| 6844 | Literal = RHS.get(); |
| 6845 | Other = LHS.get(); |
| 6846 | } |
| 6847 | |
| 6848 | // Don't warn on comparisons against nil. |
| 6849 | Other = Other->IgnoreParenCasts(); |
| 6850 | if (Other->isNullPointerConstant(S.getASTContext(), |
| 6851 | Expr::NPC_ValueDependentIsNotNull)) |
| 6852 | return; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6853 | |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6854 | // This should be kept in sync with warn_objc_literal_comparison. |
Jordan Rose | d5209ae | 2012-07-17 17:46:48 +0000 | [diff] [blame] | 6855 | // LK_String should always be last, since it has its own warning flag. |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6856 | enum { |
| 6857 | LK_Array, |
| 6858 | LK_Dictionary, |
| 6859 | LK_Numeric, |
| 6860 | LK_Boxed, |
| 6861 | LK_String |
| 6862 | } LiteralKind; |
| 6863 | |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6864 | switch (Literal->getStmtClass()) { |
| 6865 | case Stmt::ObjCStringLiteralClass: |
| 6866 | // "string literal" |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6867 | LiteralKind = LK_String; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6868 | break; |
| 6869 | case Stmt::ObjCArrayLiteralClass: |
| 6870 | // "array literal" |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6871 | LiteralKind = LK_Array; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6872 | break; |
| 6873 | case Stmt::ObjCDictionaryLiteralClass: |
| 6874 | // "dictionary literal" |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6875 | LiteralKind = LK_Dictionary; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6876 | break; |
| 6877 | case Stmt::ObjCBoxedExprClass: { |
| 6878 | Expr *Inner = cast<ObjCBoxedExpr>(Literal)->getSubExpr(); |
| 6879 | switch (Inner->getStmtClass()) { |
| 6880 | case Stmt::IntegerLiteralClass: |
| 6881 | case Stmt::FloatingLiteralClass: |
| 6882 | case Stmt::CharacterLiteralClass: |
| 6883 | case Stmt::ObjCBoolLiteralExprClass: |
| 6884 | case Stmt::CXXBoolLiteralExprClass: |
| 6885 | // "numeric literal" |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6886 | LiteralKind = LK_Numeric; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6887 | break; |
| 6888 | case Stmt::ImplicitCastExprClass: { |
| 6889 | CastKind CK = cast<CastExpr>(Inner)->getCastKind(); |
| 6890 | // Boolean literals can be represented by implicit casts. |
| 6891 | if (CK == CK_IntegralToBoolean || CK == CK_IntegralCast) { |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6892 | LiteralKind = LK_Numeric; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6893 | break; |
| 6894 | } |
| 6895 | // FALLTHROUGH |
| 6896 | } |
| 6897 | default: |
| 6898 | // "boxed expression" |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6899 | LiteralKind = LK_Boxed; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6900 | break; |
| 6901 | } |
| 6902 | break; |
| 6903 | } |
| 6904 | default: |
| 6905 | llvm_unreachable("Unknown Objective-C object literal kind"); |
| 6906 | } |
| 6907 | |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6908 | if (LiteralKind == LK_String) |
| 6909 | S.Diag(Loc, diag::warn_objc_string_literal_comparison) |
| 6910 | << Literal->getSourceRange(); |
| 6911 | else |
| 6912 | S.Diag(Loc, diag::warn_objc_literal_comparison) |
| 6913 | << LiteralKind << Literal->getSourceRange(); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6914 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6915 | if (BinaryOperator::isEqualityOp(Opc) && |
| 6916 | hasIsEqualMethod(S, LHS.get(), RHS.get())) { |
| 6917 | SourceLocation Start = LHS.get()->getLocStart(); |
| 6918 | SourceLocation End = S.PP.getLocForEndOfToken(RHS.get()->getLocEnd()); |
| 6919 | SourceRange OpRange(Loc, S.PP.getLocForEndOfToken(Loc)); |
Jordan Rose | 6deae7c | 2012-07-09 16:54:44 +0000 | [diff] [blame] | 6920 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6921 | S.Diag(Loc, diag::note_objc_literal_comparison_isequal) |
| 6922 | << FixItHint::CreateInsertion(Start, Opc == BO_EQ ? "[" : "![") |
| 6923 | << FixItHint::CreateReplacement(OpRange, "isEqual:") |
| 6924 | << FixItHint::CreateInsertion(End, "]"); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6925 | } |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6926 | } |
| 6927 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6928 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6929 | QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6930 | SourceLocation Loc, unsigned OpaqueOpc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6931 | bool IsRelational) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6932 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/true); |
| 6933 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6934 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6935 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6936 | // Handle vector comparisons separately. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6937 | if (LHS.get()->getType()->isVectorType() || |
| 6938 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6939 | return CheckVectorCompareOperands(LHS, RHS, Loc, IsRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6940 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6941 | QualType LHSType = LHS.get()->getType(); |
| 6942 | QualType RHSType = RHS.get()->getType(); |
Benjamin Kramer | fec0959 | 2011-09-03 08:46:20 +0000 | [diff] [blame] | 6943 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6944 | Expr *LHSStripped = LHS.get()->IgnoreParenImpCasts(); |
| 6945 | Expr *RHSStripped = RHS.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6946 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6947 | checkEnumComparison(*this, Loc, LHS, RHS); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6948 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6949 | if (!LHSType->hasFloatingRepresentation() && |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6950 | !(LHSType->isBlockPointerType() && IsRelational) && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6951 | !LHS.get()->getLocStart().isMacroID() && |
| 6952 | !RHS.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6953 | // For non-floating point types, check for self-comparisons of the form |
| 6954 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6955 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6956 | // |
| 6957 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6958 | // expansion. Also don't warn about comparisons which are only self |
| 6959 | // comparisons within a template specialization. The warnings should catch |
| 6960 | // obvious cases in the definition of the template anyways. The idea is to |
| 6961 | // warn when the typed comparison operator will always evaluate to the same |
| 6962 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6963 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6964 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6965 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6966 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6967 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6968 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6969 | << (Opc == BO_EQ |
| 6970 | || Opc == BO_LE |
| 6971 | || Opc == BO_GE)); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6972 | } else if (LHSType->isArrayType() && RHSType->isArrayType() && |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6973 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6974 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6975 | // what is it always going to eval to? |
| 6976 | char always_evals_to; |
| 6977 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6978 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6979 | always_evals_to = 0; // false |
| 6980 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6981 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6982 | always_evals_to = 1; // true |
| 6983 | break; |
| 6984 | default: |
| 6985 | // best we can say is 'a constant' |
| 6986 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6987 | break; |
| 6988 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6989 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6990 | << 1 // array |
| 6991 | << always_evals_to); |
| 6992 | } |
| 6993 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6994 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6995 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6996 | if (isa<CastExpr>(LHSStripped)) |
| 6997 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6998 | if (isa<CastExpr>(RHSStripped)) |
| 6999 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7000 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7001 | // Warn about comparisons against a string constant (unless the other |
| 7002 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7003 | Expr *literalString = 0; |
| 7004 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7005 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7006 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7007 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7008 | literalString = LHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7009 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7010 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 7011 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7012 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7013 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7014 | literalString = RHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7015 | literalStringStripped = RHSStripped; |
| 7016 | } |
| 7017 | |
| 7018 | if (literalString) { |
| 7019 | std::string resultComparison; |
| 7020 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7021 | case BO_LT: resultComparison = ") < 0"; break; |
| 7022 | case BO_GT: resultComparison = ") > 0"; break; |
| 7023 | case BO_LE: resultComparison = ") <= 0"; break; |
| 7024 | case BO_GE: resultComparison = ") >= 0"; break; |
| 7025 | case BO_EQ: resultComparison = ") == 0"; break; |
| 7026 | case BO_NE: resultComparison = ") != 0"; break; |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7027 | default: llvm_unreachable("Invalid comparison operator"); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7028 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7029 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7030 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 7031 | PDiag(diag::warn_stringcompare) |
| 7032 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 7033 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7034 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 7035 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7036 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7037 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7038 | if (LHS.get()->getType()->isArithmeticType() && |
| 7039 | RHS.get()->getType()->isArithmeticType()) { |
| 7040 | UsualArithmeticConversions(LHS, RHS); |
| 7041 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7042 | return QualType(); |
| 7043 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7044 | else { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7045 | LHS = UsualUnaryConversions(LHS.take()); |
| 7046 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7047 | return QualType(); |
| 7048 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7049 | RHS = UsualUnaryConversions(RHS.take()); |
| 7050 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7051 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7052 | } |
| 7053 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7054 | LHSType = LHS.get()->getType(); |
| 7055 | RHSType = RHS.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7056 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7057 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7058 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7059 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7060 | if (IsRelational) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7061 | if (LHSType->isRealType() && RHSType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7062 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7063 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 7064 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7065 | if (LHSType->hasFloatingRepresentation()) |
| 7066 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7067 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7068 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7069 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7070 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7071 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7072 | bool LHSIsNull = LHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7073 | Expr::NPC_ValueDependentIsNull); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7074 | bool RHSIsNull = RHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7075 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7076 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7077 | // All of the following pointer-related warnings are GCC extensions, except |
| 7078 | // when handling null pointer constants. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7079 | if (LHSType->isPointerType() && RHSType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7080 | QualType LCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7081 | LHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7082 | QualType RCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7083 | RHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7084 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7085 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7086 | if (LCanPointeeTy == RCanPointeeTy) |
| 7087 | return ResultTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7088 | if (!IsRelational && |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7089 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7090 | // Valid unless comparison between non-null pointer and function pointer |
| 7091 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7092 | // In a SFINAE context, we treat this as a hard error to maintain |
| 7093 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7094 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7095 | && !LHSIsNull && !RHSIsNull) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7096 | diagnoseFunctionPointerToVoidComparison( |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7097 | *this, Loc, LHS, RHS, /*isError*/ isSFINAEContext()); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7098 | |
| 7099 | if (isSFINAEContext()) |
| 7100 | return QualType(); |
| 7101 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7102 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7103 | return ResultTy; |
| 7104 | } |
| 7105 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7106 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7107 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7108 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7109 | else |
| 7110 | return ResultTy; |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7111 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7112 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 7113 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 7114 | RCanPointeeTy.getUnqualifiedType())) { |
| 7115 | // Valid unless a relational comparison of function pointers |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7116 | if (IsRelational && LCanPointeeTy->isFunctionType()) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7117 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7118 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7119 | << RHS.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7120 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7121 | } else if (!IsRelational && |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7122 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7123 | // Valid unless comparison between non-null pointer and function pointer |
| 7124 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7125 | && !LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7126 | diagnoseFunctionPointerToVoidComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7127 | /*isError*/false); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7128 | } else { |
| 7129 | // Invalid |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7130 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7131 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7132 | if (LCanPointeeTy != RCanPointeeTy) { |
| 7133 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7134 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7135 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7136 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7137 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7138 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7139 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7140 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7141 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7142 | // Comparison of nullptr_t with itself. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7143 | if (LHSType->isNullPtrType() && RHSType->isNullPtrType()) |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7144 | return ResultTy; |
| 7145 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7146 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7147 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7148 | if (RHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7149 | ((LHSType->isAnyPointerType() || LHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7150 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7151 | (LHSType->isMemberPointerType() || LHSType->isBlockPointerType())))) { |
| 7152 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 7153 | LHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7154 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7155 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7156 | return ResultTy; |
| 7157 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7158 | if (LHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7159 | ((RHSType->isAnyPointerType() || RHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7160 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7161 | (RHSType->isMemberPointerType() || RHSType->isBlockPointerType())))) { |
| 7162 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 7163 | RHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7164 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7165 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7166 | return ResultTy; |
| 7167 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7168 | |
| 7169 | // Comparison of member pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7170 | if (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7171 | LHSType->isMemberPointerType() && RHSType->isMemberPointerType()) { |
| 7172 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7173 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7174 | else |
| 7175 | return ResultTy; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7176 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7177 | |
| 7178 | // Handle scoped enumeration types specifically, since they don't promote |
| 7179 | // to integers. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7180 | if (LHS.get()->getType()->isEnumeralType() && |
| 7181 | Context.hasSameUnqualifiedType(LHS.get()->getType(), |
| 7182 | RHS.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7183 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7184 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7185 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7186 | // Handle block pointer types. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7187 | if (!IsRelational && LHSType->isBlockPointerType() && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7188 | RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7189 | QualType lpointee = LHSType->castAs<BlockPointerType>()->getPointeeType(); |
| 7190 | QualType rpointee = RHSType->castAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7191 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7192 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7193 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7194 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7195 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7196 | << RHS.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7197 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7198 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7199 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7200 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7201 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7202 | // Allow block pointers to be compared with null pointer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7203 | if (!IsRelational |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7204 | && ((LHSType->isBlockPointerType() && RHSType->isPointerType()) |
| 7205 | || (LHSType->isPointerType() && RHSType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7206 | if (!LHSIsNull && !RHSIsNull) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7207 | if (!((RHSType->isPointerType() && RHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7208 | ->getPointeeType()->isVoidType()) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7209 | || (LHSType->isPointerType() && LHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7210 | ->getPointeeType()->isVoidType()))) |
| 7211 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7212 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7213 | << RHS.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7214 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7215 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7216 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 7217 | RHSType->isPointerType() ? CK_BitCast |
| 7218 | : CK_AnyPointerToBlockPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7219 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7220 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 7221 | LHSType->isPointerType() ? CK_BitCast |
| 7222 | : CK_AnyPointerToBlockPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7223 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7224 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7225 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7226 | if (LHSType->isObjCObjectPointerType() || |
| 7227 | RHSType->isObjCObjectPointerType()) { |
| 7228 | const PointerType *LPT = LHSType->getAs<PointerType>(); |
| 7229 | const PointerType *RPT = RHSType->getAs<PointerType>(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7230 | if (LPT || RPT) { |
| 7231 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 7232 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7233 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7234 | if (!LPtrToVoid && !RPtrToVoid && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7235 | !Context.typesAreCompatible(LHSType, RHSType)) { |
| 7236 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7237 | /*isError*/false); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7238 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7239 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7240 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 7241 | RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7242 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7243 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 7244 | LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7245 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7246 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7247 | if (LHSType->isObjCObjectPointerType() && |
| 7248 | RHSType->isObjCObjectPointerType()) { |
| 7249 | if (!Context.areComparableObjCPointerTypes(LHSType, RHSType)) |
| 7250 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7251 | /*isError*/false); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 7252 | if (isObjCObjectLiteral(LHS) || isObjCObjectLiteral(RHS)) |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 7253 | diagnoseObjCLiteralComparison(*this, Loc, LHS, RHS, Opc); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 7254 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7255 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7256 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7257 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7258 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7259 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7260 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7261 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7262 | if ((LHSType->isAnyPointerType() && RHSType->isIntegerType()) || |
| 7263 | (LHSType->isIntegerType() && RHSType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7264 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7265 | bool isError = false; |
Douglas Gregor | 6db351a | 2012-09-14 04:35:37 +0000 | [diff] [blame] | 7266 | if (LangOpts.DebuggerSupport) { |
| 7267 | // Under a debugger, allow the comparison of pointers to integers, |
| 7268 | // since users tend to want to compare addresses. |
| 7269 | } else if ((LHSIsNull && LHSType->isIntegerType()) || |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7270 | (RHSIsNull && RHSType->isIntegerType())) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7271 | if (IsRelational && !getLangOpts().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7272 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7273 | } else if (IsRelational && !getLangOpts().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7274 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7275 | else if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7276 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7277 | isError = true; |
| 7278 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7279 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7280 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7281 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7282 | Diag(Loc, DiagID) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7283 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7284 | << RHS.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7285 | if (isError) |
| 7286 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7287 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7288 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7289 | if (LHSType->isIntegerType()) |
| 7290 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7291 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7292 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7293 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7294 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7295 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7296 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7297 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7298 | // Handle block pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7299 | if (!IsRelational && RHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7300 | && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { |
| 7301 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7302 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7303 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7304 | if (!IsRelational && LHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7305 | && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { |
| 7306 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7307 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7308 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7309 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7310 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7311 | } |
| 7312 | |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7313 | |
| 7314 | // Return a signed type that is of identical size and number of elements. |
| 7315 | // For floating point vectors, return an integer type of identical size |
| 7316 | // and number of elements. |
| 7317 | QualType Sema::GetSignedVectorType(QualType V) { |
| 7318 | const VectorType *VTy = V->getAs<VectorType>(); |
| 7319 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
| 7320 | if (TypeSize == Context.getTypeSize(Context.CharTy)) |
| 7321 | return Context.getExtVectorType(Context.CharTy, VTy->getNumElements()); |
| 7322 | else if (TypeSize == Context.getTypeSize(Context.ShortTy)) |
| 7323 | return Context.getExtVectorType(Context.ShortTy, VTy->getNumElements()); |
| 7324 | else if (TypeSize == Context.getTypeSize(Context.IntTy)) |
| 7325 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
| 7326 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) |
| 7327 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 7328 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
| 7329 | "Unhandled vector element size in vector compare"); |
| 7330 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 7331 | } |
| 7332 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7333 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7334 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7335 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7336 | /// types. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7337 | QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7338 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7339 | bool IsRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7340 | // Check to make sure we're operating on vectors of the same type and width, |
| 7341 | // Allowing one side to be a scalar of element type. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7342 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, /*isCompAssign*/false); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7343 | if (vType.isNull()) |
| 7344 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7345 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7346 | QualType LHSType = LHS.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7347 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7348 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7349 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 7350 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7351 | return Context.getLogicalOperationType(); |
| 7352 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7353 | // For non-floating point types, check for self-comparisons of the form |
| 7354 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7355 | // often indicate logic errors in the program. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7356 | if (!LHSType->hasFloatingRepresentation()) { |
Richard Smith | 9c129f8 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 7357 | if (DeclRefExpr* DRL |
| 7358 | = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParenImpCasts())) |
| 7359 | if (DeclRefExpr* DRR |
| 7360 | = dyn_cast<DeclRefExpr>(RHS.get()->IgnoreParenImpCasts())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7361 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7362 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7363 | PDiag(diag::warn_comparison_always) |
| 7364 | << 0 // self- |
| 7365 | << 2 // "a constant" |
| 7366 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7367 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7368 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7369 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7370 | if (!IsRelational && LHSType->hasFloatingRepresentation()) { |
David Blaikie | 52e4c60 | 2012-01-16 05:16:03 +0000 | [diff] [blame] | 7371 | assert (RHS.get()->getType()->hasFloatingRepresentation()); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7372 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7373 | } |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7374 | |
| 7375 | // Return a signed type for the vector. |
| 7376 | return GetSignedVectorType(LHSType); |
| 7377 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7378 | |
Tanya Lattner | b0f9dd2 | 2012-01-19 01:16:16 +0000 | [diff] [blame] | 7379 | QualType Sema::CheckVectorLogicalOperands(ExprResult &LHS, ExprResult &RHS, |
| 7380 | SourceLocation Loc) { |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7381 | // Ensure that either both operands are of the same vector type, or |
| 7382 | // one operand is of a vector type and the other is of its element type. |
| 7383 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, false); |
| 7384 | if (vType.isNull() || vType->isFloatingType()) |
| 7385 | return InvalidOperands(Loc, LHS, RHS); |
| 7386 | |
| 7387 | return GetSignedVectorType(LHS.get()->getType()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7388 | } |
| 7389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7390 | inline QualType Sema::CheckBitwiseOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7391 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 7392 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 7393 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7394 | if (LHS.get()->getType()->isVectorType() || |
| 7395 | RHS.get()->getType()->isVectorType()) { |
| 7396 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 7397 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7398 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7399 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7400 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7401 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7402 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7403 | ExprResult LHSResult = Owned(LHS), RHSResult = Owned(RHS); |
| 7404 | QualType compType = UsualArithmeticConversions(LHSResult, RHSResult, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7405 | IsCompAssign); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7406 | if (LHSResult.isInvalid() || RHSResult.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7407 | return QualType(); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7408 | LHS = LHSResult.take(); |
| 7409 | RHS = RHSResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7410 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 7411 | if (!compType.isNull() && compType->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7412 | return compType; |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7413 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7414 | } |
| 7415 | |
| 7416 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7417 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7418 | |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7419 | // Check vector operands differently. |
| 7420 | if (LHS.get()->getType()->isVectorType() || RHS.get()->getType()->isVectorType()) |
| 7421 | return CheckVectorLogicalOperands(LHS, RHS, Loc); |
| 7422 | |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7423 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 7424 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 7425 | // is a constant. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7426 | if (LHS.get()->getType()->isIntegerType() && |
| 7427 | !LHS.get()->getType()->isBooleanType() && |
| 7428 | RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() && |
Richard Trieu | e5adf59 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 7429 | // Don't warn in macros or template instantiations. |
| 7430 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7431 | // If the RHS can be constant folded, and if it constant folds to something |
| 7432 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 7433 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 7434 | // Parens on the RHS are ignored. |
Richard Smith | 909c555 | 2011-10-16 23:01:09 +0000 | [diff] [blame] | 7435 | llvm::APSInt Result; |
| 7436 | if (RHS.get()->EvaluateAsInt(Result, Context)) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7437 | if ((getLangOpts().Bool && !RHS.get()->getType()->isBooleanType()) || |
Richard Smith | 909c555 | 2011-10-16 23:01:09 +0000 | [diff] [blame] | 7438 | (Result != 0 && Result != 1)) { |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 7439 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7440 | << RHS.get()->getSourceRange() |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7441 | << (Opc == BO_LAnd ? "&&" : "||"); |
| 7442 | // Suggest replacing the logical operator with the bitwise version |
| 7443 | Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) |
| 7444 | << (Opc == BO_LAnd ? "&" : "|") |
| 7445 | << FixItHint::CreateReplacement(SourceRange( |
| 7446 | Loc, Lexer::getLocForEndOfToken(Loc, 0, getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7447 | getLangOpts())), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7448 | Opc == BO_LAnd ? "&" : "|"); |
| 7449 | if (Opc == BO_LAnd) |
| 7450 | // Suggest replacing "Foo() && kNonZero" with "Foo()" |
| 7451 | Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) |
| 7452 | << FixItHint::CreateRemoval( |
| 7453 | SourceRange( |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7454 | Lexer::getLocForEndOfToken(LHS.get()->getLocEnd(), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7455 | 0, getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7456 | getLangOpts()), |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7457 | RHS.get()->getLocEnd())); |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7458 | } |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7459 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7460 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7461 | if (!Context.getLangOpts().CPlusPlus) { |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7462 | LHS = UsualUnaryConversions(LHS.take()); |
| 7463 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7464 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7465 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7466 | RHS = UsualUnaryConversions(RHS.take()); |
| 7467 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7468 | return QualType(); |
| 7469 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7470 | if (!LHS.get()->getType()->isScalarType() || |
| 7471 | !RHS.get()->getType()->isScalarType()) |
| 7472 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7473 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7474 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 7475 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7476 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7477 | // The following is safe because we only use this method for |
| 7478 | // non-overloadable operands. |
| 7479 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7480 | // C++ [expr.log.and]p1 |
| 7481 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7482 | // The operands are both contextually converted to type bool. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7483 | ExprResult LHSRes = PerformContextuallyConvertToBool(LHS.get()); |
| 7484 | if (LHSRes.isInvalid()) |
| 7485 | return InvalidOperands(Loc, LHS, RHS); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7486 | LHS = LHSRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7487 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7488 | ExprResult RHSRes = PerformContextuallyConvertToBool(RHS.get()); |
| 7489 | if (RHSRes.isInvalid()) |
| 7490 | return InvalidOperands(Loc, LHS, RHS); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7491 | RHS = RHSRes; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7492 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7493 | // C++ [expr.log.and]p2 |
| 7494 | // C++ [expr.log.or]p2 |
| 7495 | // The result is a bool. |
| 7496 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7497 | } |
| 7498 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7499 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 7500 | /// is a read-only property; return true if so. A readonly property expression |
| 7501 | /// depends on various declarations and thus must be treated specially. |
| 7502 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7503 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7504 | const ObjCPropertyRefExpr *PropExpr = dyn_cast<ObjCPropertyRefExpr>(E); |
| 7505 | if (!PropExpr) return false; |
| 7506 | if (PropExpr->isImplicitProperty()) return false; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7507 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7508 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7509 | QualType BaseType = PropExpr->isSuperReceiver() ? |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7510 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 7511 | PropExpr->getBase()->getType(); |
| 7512 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7513 | if (const ObjCObjectPointerType *OPT = |
| 7514 | BaseType->getAsObjCInterfacePointerType()) |
| 7515 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 7516 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 7517 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7518 | return false; |
| 7519 | } |
| 7520 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7521 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7522 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); |
| 7523 | if (!ME) return false; |
| 7524 | if (!isa<FieldDecl>(ME->getMemberDecl())) return false; |
| 7525 | ObjCMessageExpr *Base = |
| 7526 | dyn_cast<ObjCMessageExpr>(ME->getBase()->IgnoreParenImpCasts()); |
| 7527 | if (!Base) return false; |
| 7528 | return Base->getMethodDecl() != 0; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7529 | } |
| 7530 | |
John McCall | 78dae24 | 2012-03-13 00:37:01 +0000 | [diff] [blame] | 7531 | /// Is the given expression (which must be 'const') a reference to a |
| 7532 | /// variable which was originally non-const, but which has become |
| 7533 | /// 'const' due to being captured within a block? |
| 7534 | enum NonConstCaptureKind { NCCK_None, NCCK_Block, NCCK_Lambda }; |
| 7535 | static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) { |
| 7536 | assert(E->isLValue() && E->getType().isConstQualified()); |
| 7537 | E = E->IgnoreParens(); |
| 7538 | |
| 7539 | // Must be a reference to a declaration from an enclosing scope. |
| 7540 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
| 7541 | if (!DRE) return NCCK_None; |
| 7542 | if (!DRE->refersToEnclosingLocal()) return NCCK_None; |
| 7543 | |
| 7544 | // The declaration must be a variable which is not declared 'const'. |
| 7545 | VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7546 | if (!var) return NCCK_None; |
| 7547 | if (var->getType().isConstQualified()) return NCCK_None; |
| 7548 | assert(var->hasLocalStorage() && "capture added 'const' to non-local?"); |
| 7549 | |
| 7550 | // Decide whether the first capture was for a block or a lambda. |
| 7551 | DeclContext *DC = S.CurContext; |
| 7552 | while (DC->getParent() != var->getDeclContext()) |
| 7553 | DC = DC->getParent(); |
| 7554 | return (isa<BlockDecl>(DC) ? NCCK_Block : NCCK_Lambda); |
| 7555 | } |
| 7556 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7557 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 7558 | /// emit an error and return true. If so, return false. |
| 7559 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Fariborz Jahanian | e7ea28a | 2012-04-10 17:30:10 +0000 | [diff] [blame] | 7560 | assert(!E->hasPlaceholderType(BuiltinType::PseudoObject)); |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7561 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7562 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7563 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7564 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 7565 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7566 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 7567 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7568 | if (IsLV == Expr::MLV_Valid) |
| 7569 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7570 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7571 | unsigned Diag = 0; |
| 7572 | bool NeedType = false; |
| 7573 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7574 | case Expr::MLV_ConstQualified: |
| 7575 | Diag = diag::err_typecheck_assign_const; |
| 7576 | |
John McCall | 78dae24 | 2012-03-13 00:37:01 +0000 | [diff] [blame] | 7577 | // Use a specialized diagnostic when we're assigning to an object |
| 7578 | // from an enclosing function or block. |
| 7579 | if (NonConstCaptureKind NCCK = isReferenceToNonConstCapture(S, E)) { |
| 7580 | if (NCCK == NCCK_Block) |
| 7581 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7582 | else |
| 7583 | Diag = diag::err_lambda_decl_ref_not_modifiable_lvalue; |
| 7584 | break; |
| 7585 | } |
| 7586 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7587 | // In ARC, use some specialized diagnostics for occasions where we |
| 7588 | // infer 'const'. These are always pseudo-strong variables. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7589 | if (S.getLangOpts().ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7590 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 7591 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 7592 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 7593 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7594 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 7595 | // user actually wrote 'const'. |
| 7596 | if (var->isARCPseudoStrong() && |
| 7597 | (!var->getTypeSourceInfo() || |
| 7598 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 7599 | // There are two pseudo-strong cases: |
| 7600 | // - self |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7601 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 7602 | if (method && var == method->getSelfDecl()) |
Ted Kremenek | 2bbcd5c | 2011-11-14 21:59:25 +0000 | [diff] [blame] | 7603 | Diag = method->isClassMethod() |
| 7604 | ? diag::err_typecheck_arc_assign_self_class_method |
| 7605 | : diag::err_typecheck_arc_assign_self; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7606 | |
| 7607 | // - fast enumeration variables |
| 7608 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7609 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7610 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7611 | SourceRange Assign; |
| 7612 | if (Loc != OrigLoc) |
| 7613 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 7614 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 7615 | // We need to preserve the AST regardless, so migration tool |
| 7616 | // can do its job. |
| 7617 | return false; |
| 7618 | } |
| 7619 | } |
| 7620 | } |
| 7621 | |
| 7622 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7623 | case Expr::MLV_ArrayType: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 7624 | case Expr::MLV_ArrayTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7625 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 7626 | NeedType = true; |
| 7627 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7628 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7629 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 7630 | NeedType = true; |
| 7631 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 7632 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7633 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 7634 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7635 | case Expr::MLV_Valid: |
| 7636 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7637 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7638 | case Expr::MLV_MemberFunction: |
| 7639 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7640 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7641 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7642 | case Expr::MLV_IncompleteType: |
| 7643 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7644 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 7645 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, E); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7646 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7647 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7648 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7649 | case Expr::MLV_ReadonlyProperty: |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7650 | case Expr::MLV_NoSetterProperty: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7651 | llvm_unreachable("readonly properties should be processed differently"); |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7652 | case Expr::MLV_InvalidMessageExpression: |
| 7653 | Diag = diag::error_readonly_message_assignment; |
| 7654 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7655 | case Expr::MLV_SubObjCPropertySetting: |
| 7656 | Diag = diag::error_no_subobject_property_setting; |
| 7657 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7658 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7659 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7660 | SourceRange Assign; |
| 7661 | if (Loc != OrigLoc) |
| 7662 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7663 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7664 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7665 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7666 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7667 | return true; |
| 7668 | } |
| 7669 | |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7670 | static void CheckIdentityFieldAssignment(Expr *LHSExpr, Expr *RHSExpr, |
| 7671 | SourceLocation Loc, |
| 7672 | Sema &Sema) { |
| 7673 | // C / C++ fields |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7674 | MemberExpr *ML = dyn_cast<MemberExpr>(LHSExpr); |
| 7675 | MemberExpr *MR = dyn_cast<MemberExpr>(RHSExpr); |
| 7676 | if (ML && MR && ML->getMemberDecl() == MR->getMemberDecl()) { |
| 7677 | if (isa<CXXThisExpr>(ML->getBase()) && isa<CXXThisExpr>(MR->getBase())) |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7678 | Sema.Diag(Loc, diag::warn_identity_field_assign) << 0; |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7679 | } |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7680 | |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7681 | // Objective-C instance variables |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7682 | ObjCIvarRefExpr *OL = dyn_cast<ObjCIvarRefExpr>(LHSExpr); |
| 7683 | ObjCIvarRefExpr *OR = dyn_cast<ObjCIvarRefExpr>(RHSExpr); |
| 7684 | if (OL && OR && OL->getDecl() == OR->getDecl()) { |
| 7685 | DeclRefExpr *RL = dyn_cast<DeclRefExpr>(OL->getBase()->IgnoreImpCasts()); |
| 7686 | DeclRefExpr *RR = dyn_cast<DeclRefExpr>(OR->getBase()->IgnoreImpCasts()); |
| 7687 | if (RL && RR && RL->getDecl() == RR->getDecl()) |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7688 | Sema.Diag(Loc, diag::warn_identity_field_assign) << 1; |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7689 | } |
| 7690 | } |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7691 | |
| 7692 | // C99 6.5.16.1 |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7693 | QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7694 | SourceLocation Loc, |
| 7695 | QualType CompoundType) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7696 | assert(!LHSExpr->hasPlaceholderType(BuiltinType::PseudoObject)); |
| 7697 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7698 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7699 | if (CheckForModifiableLvalue(LHSExpr, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7700 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7701 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7702 | QualType LHSType = LHSExpr->getType(); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7703 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
| 7704 | CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7705 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7706 | if (CompoundType.isNull()) { |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7707 | Expr *RHSCheck = RHS.get(); |
| 7708 | |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7709 | CheckIdentityFieldAssignment(LHSExpr, RHSCheck, Loc, *this); |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7710 | |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7711 | QualType LHSTy(LHSType); |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7712 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7713 | if (RHS.isInvalid()) |
| 7714 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7715 | // Special case of NSObject attributes on c-style pointer types. |
| 7716 | if (ConvTy == IncompatiblePointer && |
| 7717 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7718 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7719 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7720 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7721 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7722 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7723 | if (ConvTy == Compatible && |
Fariborz Jahanian | 466f45a | 2012-01-24 19:40:13 +0000 | [diff] [blame] | 7724 | LHSType->isObjCObjectType()) |
Fariborz Jahanian | 7b383e4 | 2012-01-24 18:05:45 +0000 | [diff] [blame] | 7725 | Diag(Loc, diag::err_objc_object_assignment) |
| 7726 | << LHSType; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7727 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7728 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7729 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7730 | // instead of "x += 4". |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7731 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7732 | RHSCheck = ICE->getSubExpr(); |
| 7733 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7734 | if ((UO->getOpcode() == UO_Plus || |
| 7735 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7736 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7737 | // Only if the two operators are exactly adjacent. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7738 | Loc.getLocWithOffset(1) == UO->getOperatorLoc() && |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7739 | // And there is a space or other character before the subexpr of the |
| 7740 | // unary +/-. We don't want to warn on "x=-1". |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7741 | Loc.getLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7742 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7743 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7744 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7745 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7746 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7747 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7748 | |
| 7749 | if (ConvTy == Compatible) { |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7750 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) { |
| 7751 | // Warn about retain cycles where a block captures the LHS, but |
| 7752 | // not if the LHS is a simple variable into which the block is |
| 7753 | // being stored...unless that variable can be captured by reference! |
| 7754 | const Expr *InnerLHS = LHSExpr->IgnoreParenCasts(); |
| 7755 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InnerLHS); |
| 7756 | if (!DRE || DRE->getDecl()->hasAttr<BlocksAttr>()) |
| 7757 | checkRetainCycles(LHSExpr, RHS.get()); |
| 7758 | |
Jordan Rose | 58b6bdc | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 7759 | // It is safe to assign a weak reference into a strong variable. |
| 7760 | // Although this code can still have problems: |
| 7761 | // id x = self.weakProp; |
| 7762 | // id y = self.weakProp; |
| 7763 | // we do not warn to warn spuriously when 'x' and 'y' are on separate |
| 7764 | // paths through the function. This should be revisited if |
| 7765 | // -Wrepeated-use-of-weak is made flow-sensitive. |
| 7766 | DiagnosticsEngine::Level Level = |
| 7767 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, |
| 7768 | RHS.get()->getLocStart()); |
| 7769 | if (Level != DiagnosticsEngine::Ignored) |
| 7770 | getCurFunction()->markSafeWeakUse(RHS.get()); |
| 7771 | |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7772 | } else if (getLangOpts().ObjCAutoRefCount) { |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7773 | checkUnsafeExprAssigns(Loc, LHSExpr, RHS.get()); |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7774 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7775 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7776 | } else { |
| 7777 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7778 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7779 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7780 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7781 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7782 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7783 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7784 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7785 | CheckForNullPointerDereference(*this, LHSExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7786 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7787 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7788 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7789 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7790 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7791 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7792 | // 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] | 7793 | // operand. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7794 | return (getLangOpts().CPlusPlus |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7795 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7796 | } |
| 7797 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7798 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7799 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7800 | SourceLocation Loc) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7801 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7802 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7803 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7804 | return QualType(); |
| 7805 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7806 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7807 | // operands, but not unary promotions. |
| 7808 | // 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] | 7809 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7810 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7811 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7812 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7813 | if (LHS.isInvalid()) |
| 7814 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7815 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 7816 | S.DiagnoseUnusedExprResult(LHS.get()); |
| 7817 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7818 | if (!S.getLangOpts().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7819 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7820 | if (RHS.isInvalid()) |
| 7821 | return QualType(); |
| 7822 | if (!RHS.get()->getType()->isVoidType()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7823 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
| 7824 | diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7825 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7826 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7827 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7828 | } |
| 7829 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7830 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7831 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7832 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7833 | ExprValueKind &VK, |
| 7834 | SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7835 | bool IsInc, bool IsPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7836 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7837 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7838 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7839 | QualType ResType = Op->getType(); |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7840 | // Atomic types can be used for increment / decrement where the non-atomic |
| 7841 | // versions can, so ignore the _Atomic() specifier for the purpose of |
| 7842 | // checking. |
| 7843 | if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) |
| 7844 | ResType = ResAtomicType->getValueType(); |
| 7845 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7846 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7847 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7848 | if (S.getLangOpts().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7849 | // Decrement of bool is not allowed. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7850 | if (!IsInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7851 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7852 | return QualType(); |
| 7853 | } |
| 7854 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7855 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7856 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7857 | // OK! |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 7858 | } else if (ResType->isPointerType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7859 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7860 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7861 | return QualType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 7862 | } else if (ResType->isObjCObjectPointerType()) { |
| 7863 | // On modern runtimes, ObjC pointer arithmetic is forbidden. |
| 7864 | // Otherwise, we just need a complete type. |
| 7865 | if (checkArithmeticIncompletePointerType(S, OpLoc, Op) || |
| 7866 | checkArithmeticOnObjCPointer(S, OpLoc, Op)) |
| 7867 | return QualType(); |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7868 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7869 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7870 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7871 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7872 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7873 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7874 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7875 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7876 | IsInc, IsPrefix); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7877 | } else if (S.getLangOpts().AltiVec && ResType->isVectorType()) { |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7878 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7879 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7880 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7881 | << ResType << int(IsInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7882 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7883 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7884 | // 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] | 7885 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7886 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7887 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7888 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7889 | // (in C or with postfix), the increment is the unqualified type of the |
| 7890 | // operand. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7891 | if (IsPrefix && S.getLangOpts().CPlusPlus) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7892 | VK = VK_LValue; |
| 7893 | return ResType; |
| 7894 | } else { |
| 7895 | VK = VK_RValue; |
| 7896 | return ResType.getUnqualifiedType(); |
| 7897 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7898 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7899 | |
| 7900 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7901 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7902 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7903 | /// where the declaration is needed for type checking. We only need to |
| 7904 | /// handle cases when the expression references a function designator |
| 7905 | /// or is an lvalue. Here are some examples: |
| 7906 | /// - &(x) => x |
| 7907 | /// - &*****f => f for f a function designator. |
| 7908 | /// - &s.xx => s |
| 7909 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7910 | /// - *(x + 1) -> x, if x is an array |
| 7911 | /// - &"123"[2] -> 0 |
| 7912 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7913 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7914 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7915 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7916 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7917 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7918 | // If this is an arrow operator, the address is an offset from |
| 7919 | // the base's value, so the object the base refers to is |
| 7920 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7921 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7922 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7923 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7924 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7925 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7926 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7927 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7928 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7929 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7930 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7931 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7932 | } |
| 7933 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7934 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7935 | case Stmt::UnaryOperatorClass: { |
| 7936 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7937 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7938 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7939 | case UO_Real: |
| 7940 | case UO_Imag: |
| 7941 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7942 | return getPrimaryDecl(UO->getSubExpr()); |
| 7943 | default: |
| 7944 | return 0; |
| 7945 | } |
| 7946 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7947 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7948 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7949 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7950 | // If the result of an implicit cast is an l-value, we care about |
| 7951 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7952 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7953 | default: |
| 7954 | return 0; |
| 7955 | } |
| 7956 | } |
| 7957 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7958 | namespace { |
| 7959 | enum { |
| 7960 | AO_Bit_Field = 0, |
| 7961 | AO_Vector_Element = 1, |
| 7962 | AO_Property_Expansion = 2, |
| 7963 | AO_Register_Variable = 3, |
| 7964 | AO_No_Error = 4 |
| 7965 | }; |
| 7966 | } |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7967 | /// \brief Diagnose invalid operand for address of operations. |
| 7968 | /// |
| 7969 | /// \param Type The type of operand which cannot have its address taken. |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7970 | static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, |
| 7971 | Expr *E, unsigned Type) { |
| 7972 | S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); |
| 7973 | } |
| 7974 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7975 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7976 | /// 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] | 7977 | /// 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] | 7978 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7979 | /// 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] | 7980 | /// 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] | 7981 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7982 | static QualType CheckAddressOfOperand(Sema &S, ExprResult &OrigOp, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7983 | SourceLocation OpLoc) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7984 | if (const BuiltinType *PTy = OrigOp.get()->getType()->getAsPlaceholderType()){ |
| 7985 | if (PTy->getKind() == BuiltinType::Overload) { |
| 7986 | if (!isa<OverloadExpr>(OrigOp.get()->IgnoreParens())) { |
| 7987 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 7988 | << OrigOp.get()->getSourceRange(); |
| 7989 | return QualType(); |
| 7990 | } |
| 7991 | |
| 7992 | return S.Context.OverloadTy; |
| 7993 | } |
| 7994 | |
| 7995 | if (PTy->getKind() == BuiltinType::UnknownAny) |
| 7996 | return S.Context.UnknownAnyTy; |
| 7997 | |
| 7998 | if (PTy->getKind() == BuiltinType::BoundMember) { |
| 7999 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 8000 | << OrigOp.get()->getSourceRange(); |
Douglas Gregor | 44efed0 | 2011-10-09 19:10:41 +0000 | [diff] [blame] | 8001 | return QualType(); |
| 8002 | } |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8003 | |
| 8004 | OrigOp = S.CheckPlaceholderExpr(OrigOp.take()); |
| 8005 | if (OrigOp.isInvalid()) return QualType(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8006 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8007 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8008 | if (OrigOp.get()->isTypeDependent()) |
| 8009 | return S.Context.DependentTy; |
| 8010 | |
| 8011 | assert(!OrigOp.get()->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8012 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8013 | // Make sure to ignore parentheses in subsequent checks |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8014 | Expr *op = OrigOp.get()->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 8015 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8016 | if (S.getLangOpts().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8017 | // Implement C99-only parts of addressof rules. |
| 8018 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8019 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8020 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 8021 | // (assuming the deref expression is valid). |
| 8022 | return uOp->getSubExpr()->getType(); |
| 8023 | } |
| 8024 | // Technically, there should be a check for array subscript |
| 8025 | // expressions here, but the result of one is always an lvalue anyway. |
| 8026 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8027 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8028 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8029 | unsigned AddressOfError = AO_No_Error; |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 8030 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8031 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8032 | bool sfinae = S.isSFINAEContext(); |
| 8033 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 8034 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8035 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8036 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8037 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8038 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8039 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8040 | } else if (lval == Expr::LV_MemberFunction) { |
| 8041 | // If it's an instance method, make a member pointer. |
| 8042 | // The expression must have exactly the form &A::foo. |
| 8043 | |
| 8044 | // If the underlying expression isn't a decl ref, give up. |
| 8045 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8046 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8047 | << OrigOp.get()->getSourceRange(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8048 | return QualType(); |
| 8049 | } |
| 8050 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 8051 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 8052 | |
| 8053 | // The id-expression was parenthesized. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8054 | if (OrigOp.get() != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8055 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8056 | << OrigOp.get()->getSourceRange(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8057 | |
| 8058 | // The method was named without a qualifier. |
| 8059 | } else if (!DRE->getQualifier()) { |
David Blaikie | abeadfb | 2012-10-11 22:55:07 +0000 | [diff] [blame] | 8060 | if (MD->getParent()->getName().empty()) |
| 8061 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
| 8062 | << op->getSourceRange(); |
| 8063 | else { |
| 8064 | SmallString<32> Str; |
| 8065 | StringRef Qual = (MD->getParent()->getName() + "::").toStringRef(Str); |
| 8066 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
| 8067 | << op->getSourceRange() |
| 8068 | << FixItHint::CreateInsertion(op->getSourceRange().getBegin(), Qual); |
| 8069 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8070 | } |
| 8071 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8072 | return S.Context.getMemberPointerType(op->getType(), |
| 8073 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8074 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8075 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8076 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8077 | if (!op->getType()->isFunctionType()) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8078 | // Use a special diagnostic for loads from property references. |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 8079 | if (isa<PseudoObjectExpr>(op)) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8080 | AddressOfError = AO_Property_Expansion; |
| 8081 | } else { |
| 8082 | // FIXME: emit more specific diag... |
| 8083 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 8084 | << op->getSourceRange(); |
| 8085 | return QualType(); |
| 8086 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8087 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8088 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8089 | // The operand cannot be a bit-field |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8090 | AddressOfError = AO_Bit_Field; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8091 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8092 | // The operand cannot be an element of a vector |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8093 | AddressOfError = AO_Vector_Element; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8094 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8095 | // 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] | 8096 | // with the register storage-class specifier. |
| 8097 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 8098 | // in C++ it is not error to take address of a register |
| 8099 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8100 | if (vd->getStorageClass() == SC_Register && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8101 | !S.getLangOpts().CPlusPlus) { |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8102 | AddressOfError = AO_Register_Variable; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8103 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8104 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8105 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8106 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 8107 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8108 | // Could be a pointer to member, though, if there is an explicit |
| 8109 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 8110 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8111 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8112 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8113 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8114 | S.Diag(OpLoc, |
| 8115 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8116 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8117 | return QualType(); |
| 8118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8119 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 8120 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 8121 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8122 | return S.Context.getMemberPointerType(op->getType(), |
| 8123 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8124 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8125 | } |
Eli Friedman | 7b2f51c | 2011-08-26 20:28:17 +0000 | [diff] [blame] | 8126 | } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8127 | llvm_unreachable("Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8128 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8129 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8130 | if (AddressOfError != AO_No_Error) { |
| 8131 | diagnoseAddressOfInvalidType(S, OpLoc, op, AddressOfError); |
| 8132 | return QualType(); |
| 8133 | } |
| 8134 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8135 | if (lval == Expr::LV_IncompleteVoidType) { |
| 8136 | // Taking the address of a void variable is technically illegal, but we |
| 8137 | // allow it in cases which are otherwise valid. |
| 8138 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8139 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8140 | } |
| 8141 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8142 | // 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] | 8143 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8144 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 8145 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8146 | } |
| 8147 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8148 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8149 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 8150 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8151 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8152 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8153 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8154 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 8155 | if (ConvResult.isInvalid()) |
| 8156 | return QualType(); |
| 8157 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8158 | QualType OpTy = Op->getType(); |
| 8159 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 8160 | |
| 8161 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 8162 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 8163 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 8164 | Op->getSourceRange()); |
| 8165 | } |
| 8166 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8167 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 8168 | // is an incomplete type or void. It would be possible to warn about |
| 8169 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 8170 | // warning is unlikely to catch any mistakes. |
| 8171 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 8172 | Result = PT->getPointeeType(); |
| 8173 | else if (const ObjCObjectPointerType *OPT = |
| 8174 | OpTy->getAs<ObjCObjectPointerType>()) |
| 8175 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8176 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8177 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8178 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8179 | if (PR.take() != Op) |
| 8180 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8181 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8182 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8183 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8184 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8185 | << OpTy << Op->getSourceRange(); |
| 8186 | return QualType(); |
| 8187 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8188 | |
| 8189 | // Dereferences are usually l-values... |
| 8190 | VK = VK_LValue; |
| 8191 | |
| 8192 | // ...except that certain expressions are never l-values in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8193 | if (!S.getLangOpts().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8194 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8195 | |
| 8196 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8197 | } |
| 8198 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8199 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8200 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8201 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8202 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8203 | default: llvm_unreachable("Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8204 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 8205 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 8206 | case tok::star: Opc = BO_Mul; break; |
| 8207 | case tok::slash: Opc = BO_Div; break; |
| 8208 | case tok::percent: Opc = BO_Rem; break; |
| 8209 | case tok::plus: Opc = BO_Add; break; |
| 8210 | case tok::minus: Opc = BO_Sub; break; |
| 8211 | case tok::lessless: Opc = BO_Shl; break; |
| 8212 | case tok::greatergreater: Opc = BO_Shr; break; |
| 8213 | case tok::lessequal: Opc = BO_LE; break; |
| 8214 | case tok::less: Opc = BO_LT; break; |
| 8215 | case tok::greaterequal: Opc = BO_GE; break; |
| 8216 | case tok::greater: Opc = BO_GT; break; |
| 8217 | case tok::exclaimequal: Opc = BO_NE; break; |
| 8218 | case tok::equalequal: Opc = BO_EQ; break; |
| 8219 | case tok::amp: Opc = BO_And; break; |
| 8220 | case tok::caret: Opc = BO_Xor; break; |
| 8221 | case tok::pipe: Opc = BO_Or; break; |
| 8222 | case tok::ampamp: Opc = BO_LAnd; break; |
| 8223 | case tok::pipepipe: Opc = BO_LOr; break; |
| 8224 | case tok::equal: Opc = BO_Assign; break; |
| 8225 | case tok::starequal: Opc = BO_MulAssign; break; |
| 8226 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 8227 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 8228 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 8229 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 8230 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 8231 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 8232 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 8233 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 8234 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 8235 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8236 | } |
| 8237 | return Opc; |
| 8238 | } |
| 8239 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8240 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8241 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8242 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8243 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8244 | default: llvm_unreachable("Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8245 | case tok::plusplus: Opc = UO_PreInc; break; |
| 8246 | case tok::minusminus: Opc = UO_PreDec; break; |
| 8247 | case tok::amp: Opc = UO_AddrOf; break; |
| 8248 | case tok::star: Opc = UO_Deref; break; |
| 8249 | case tok::plus: Opc = UO_Plus; break; |
| 8250 | case tok::minus: Opc = UO_Minus; break; |
| 8251 | case tok::tilde: Opc = UO_Not; break; |
| 8252 | case tok::exclaim: Opc = UO_LNot; break; |
| 8253 | case tok::kw___real: Opc = UO_Real; break; |
| 8254 | case tok::kw___imag: Opc = UO_Imag; break; |
| 8255 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8256 | } |
| 8257 | return Opc; |
| 8258 | } |
| 8259 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8260 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 8261 | /// This warning is only emitted for builtin assignment operations. It is also |
| 8262 | /// suppressed in the event of macro expansions. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8263 | static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8264 | SourceLocation OpLoc) { |
| 8265 | if (!S.ActiveTemplateInstantiations.empty()) |
| 8266 | return; |
| 8267 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 8268 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8269 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
| 8270 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); |
| 8271 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); |
| 8272 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); |
| 8273 | if (!LHSDeclRef || !RHSDeclRef || |
| 8274 | LHSDeclRef->getLocation().isMacroID() || |
| 8275 | RHSDeclRef->getLocation().isMacroID()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8276 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8277 | const ValueDecl *LHSDecl = |
| 8278 | cast<ValueDecl>(LHSDeclRef->getDecl()->getCanonicalDecl()); |
| 8279 | const ValueDecl *RHSDecl = |
| 8280 | cast<ValueDecl>(RHSDeclRef->getDecl()->getCanonicalDecl()); |
| 8281 | if (LHSDecl != RHSDecl) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8282 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8283 | if (LHSDecl->getType().isVolatileQualified()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8284 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8285 | if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8286 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 8287 | return; |
| 8288 | |
| 8289 | S.Diag(OpLoc, diag::warn_self_assignment) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8290 | << LHSDeclRef->getType() |
| 8291 | << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8292 | } |
| 8293 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8294 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 8295 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 8296 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8297 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8298 | BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8299 | Expr *LHSExpr, Expr *RHSExpr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8300 | if (getLangOpts().CPlusPlus0x && isa<InitListExpr>(RHSExpr)) { |
Sebastian Redl | 0d8ab2e | 2012-02-27 20:34:02 +0000 | [diff] [blame] | 8301 | // The syntax only allows initializer lists on the RHS of assignment, |
| 8302 | // so we don't need to worry about accepting invalid code for |
| 8303 | // non-assignment operators. |
| 8304 | // C++11 5.17p9: |
| 8305 | // The meaning of x = {v} [...] is that of x = T(v) [...]. The meaning |
| 8306 | // of x = {} is x = T(). |
| 8307 | InitializationKind Kind = |
| 8308 | InitializationKind::CreateDirectList(RHSExpr->getLocStart()); |
| 8309 | InitializedEntity Entity = |
| 8310 | InitializedEntity::InitializeTemporary(LHSExpr->getType()); |
| 8311 | InitializationSequence InitSeq(*this, Entity, Kind, &RHSExpr, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8312 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, RHSExpr); |
Sebastian Redl | 0d8ab2e | 2012-02-27 20:34:02 +0000 | [diff] [blame] | 8313 | if (Init.isInvalid()) |
| 8314 | return Init; |
| 8315 | RHSExpr = Init.take(); |
| 8316 | } |
| 8317 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8318 | ExprResult LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8319 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8320 | // The following two variables are used for compound assignment operators |
| 8321 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 8322 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8323 | ExprValueKind VK = VK_RValue; |
| 8324 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8325 | |
| 8326 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8327 | case BO_Assign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8328 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8329 | if (getLangOpts().CPlusPlus && |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8330 | LHS.get()->getObjectKind() != OK_ObjCProperty) { |
| 8331 | VK = LHS.get()->getValueKind(); |
| 8332 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8333 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8334 | if (!ResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8335 | DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8336 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8337 | case BO_PtrMemD: |
| 8338 | case BO_PtrMemI: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8339 | ResultTy = CheckPointerToMemberOperands(LHS, RHS, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8340 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 8341 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8342 | case BO_Mul: |
| 8343 | case BO_Div: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8344 | ResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8345 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8346 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8347 | case BO_Rem: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8348 | ResultTy = CheckRemainderOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8349 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8350 | case BO_Add: |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 8351 | ResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8352 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8353 | case BO_Sub: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8354 | ResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8355 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8356 | case BO_Shl: |
| 8357 | case BO_Shr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8358 | ResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8359 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8360 | case BO_LE: |
| 8361 | case BO_LT: |
| 8362 | case BO_GE: |
| 8363 | case BO_GT: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8364 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8365 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8366 | case BO_EQ: |
| 8367 | case BO_NE: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8368 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8369 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8370 | case BO_And: |
| 8371 | case BO_Xor: |
| 8372 | case BO_Or: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8373 | ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8374 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8375 | case BO_LAnd: |
| 8376 | case BO_LOr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8377 | ResultTy = CheckLogicalOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8378 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8379 | case BO_MulAssign: |
| 8380 | case BO_DivAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8381 | CompResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8382 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8383 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8384 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8385 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8386 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8387 | case BO_RemAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8388 | CompResultTy = CheckRemainderOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8389 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8390 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8391 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8392 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8393 | case BO_AddAssign: |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 8394 | CompResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc, &CompLHSTy); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8395 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8396 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8397 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8398 | case BO_SubAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8399 | CompResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 8400 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8401 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8402 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8403 | case BO_ShlAssign: |
| 8404 | case BO_ShrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8405 | CompResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8406 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8407 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8408 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8409 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8410 | case BO_AndAssign: |
| 8411 | case BO_XorAssign: |
| 8412 | case BO_OrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8413 | CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8414 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8415 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8416 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8417 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8418 | case BO_Comma: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8419 | ResultTy = CheckCommaOperands(*this, LHS, RHS, OpLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8420 | if (getLangOpts().CPlusPlus && !RHS.isInvalid()) { |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8421 | VK = RHS.get()->getValueKind(); |
| 8422 | OK = RHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8423 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8424 | break; |
| 8425 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8426 | if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8427 | return ExprError(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8428 | |
| 8429 | // Check for array bounds violations for both sides of the BinaryOperator |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8430 | CheckArrayAccess(LHS.get()); |
| 8431 | CheckArrayAccess(RHS.get()); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8432 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8433 | if (CompResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8434 | return Owned(new (Context) BinaryOperator(LHS.take(), RHS.take(), Opc, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 8435 | ResultTy, VK, OK, OpLoc, |
| 8436 | FPFeatures.fp_contract)); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8437 | if (getLangOpts().CPlusPlus && LHS.get()->getObjectKind() != |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 8438 | OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8439 | VK = VK_LValue; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8440 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8441 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8442 | return Owned(new (Context) CompoundAssignOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8443 | ResultTy, VK, OK, CompLHSTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 8444 | CompResultTy, OpLoc, |
| 8445 | FPFeatures.fp_contract)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8446 | } |
| 8447 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8448 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 8449 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 8450 | /// comparison operators have higher precedence. The most typical example of |
| 8451 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8452 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8453 | SourceLocation OpLoc, Expr *LHSExpr, |
| 8454 | Expr *RHSExpr) { |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8455 | typedef BinaryOperator BinOp; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8456 | BinOp::Opcode LHSopc = static_cast<BinOp::Opcode>(-1), |
| 8457 | RHSopc = static_cast<BinOp::Opcode>(-1); |
| 8458 | if (BinOp *BO = dyn_cast<BinOp>(LHSExpr)) |
| 8459 | LHSopc = BO->getOpcode(); |
| 8460 | if (BinOp *BO = dyn_cast<BinOp>(RHSExpr)) |
| 8461 | RHSopc = BO->getOpcode(); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8462 | |
| 8463 | // Subs are not binary operators. |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8464 | if (LHSopc == -1 && RHSopc == -1) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8465 | return; |
| 8466 | |
| 8467 | // Bitwise operations are sometimes used as eager logical ops. |
| 8468 | // Don't diagnose this. |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8469 | if ((BinOp::isComparisonOp(LHSopc) || BinOp::isBitwiseOp(LHSopc)) && |
| 8470 | (BinOp::isComparisonOp(RHSopc) || BinOp::isBitwiseOp(RHSopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8471 | return; |
| 8472 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8473 | bool isLeftComp = BinOp::isComparisonOp(LHSopc); |
| 8474 | bool isRightComp = BinOp::isComparisonOp(RHSopc); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8475 | if (!isLeftComp && !isRightComp) return; |
| 8476 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8477 | SourceRange DiagRange = isLeftComp ? SourceRange(LHSExpr->getLocStart(), |
| 8478 | OpLoc) |
| 8479 | : SourceRange(OpLoc, RHSExpr->getLocEnd()); |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8480 | StringRef OpStr = isLeftComp ? BinOp::getOpcodeStr(LHSopc) |
| 8481 | : BinOp::getOpcodeStr(RHSopc); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8482 | SourceRange ParensRange = isLeftComp ? |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8483 | SourceRange(cast<BinOp>(LHSExpr)->getRHS()->getLocStart(), |
| 8484 | RHSExpr->getLocEnd()) |
| 8485 | : SourceRange(LHSExpr->getLocStart(), |
| 8486 | cast<BinOp>(RHSExpr)->getLHS()->getLocStart()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8487 | |
| 8488 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 8489 | << DiagRange << BinOp::getOpcodeStr(Opc) << OpStr; |
| 8490 | SuggestParentheses(Self, OpLoc, |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8491 | Self.PDiag(diag::note_precedence_silence) << OpStr, |
Nico Weber | 40e2999 | 2012-06-03 07:07:00 +0000 | [diff] [blame] | 8492 | (isLeftComp ? LHSExpr : RHSExpr)->getSourceRange()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8493 | SuggestParentheses(Self, OpLoc, |
| 8494 | Self.PDiag(diag::note_precedence_bitwise_first) << BinOp::getOpcodeStr(Opc), |
| 8495 | ParensRange); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8496 | } |
| 8497 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8498 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 8499 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 8500 | /// in parentheses. |
| 8501 | static void |
| 8502 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 8503 | BinaryOperator *Bop) { |
| 8504 | assert(Bop->getOpcode() == BO_And); |
| 8505 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 8506 | << Bop->getSourceRange() << OpLoc; |
| 8507 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8508 | Self.PDiag(diag::note_precedence_silence) |
| 8509 | << Bop->getOpcodeStr(), |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8510 | Bop->getSourceRange()); |
| 8511 | } |
| 8512 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8513 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 8514 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 8515 | /// in parentheses. |
| 8516 | static void |
| 8517 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8518 | BinaryOperator *Bop) { |
| 8519 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 8520 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 8521 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8522 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8523 | Self.PDiag(diag::note_precedence_silence) |
| 8524 | << Bop->getOpcodeStr(), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 8525 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8526 | } |
| 8527 | |
| 8528 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8529 | /// 'true'. |
| 8530 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 8531 | bool Res; |
| 8532 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 8533 | } |
| 8534 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8535 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8536 | /// 'false'. |
| 8537 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 8538 | bool Res; |
| 8539 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 8540 | } |
| 8541 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8542 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 8543 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8544 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8545 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(LHSExpr)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8546 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8547 | // 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] | 8548 | if (EvaluatesAsFalse(S, RHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8549 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8550 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 8551 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 8552 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 8553 | } else if (Bop->getOpcode() == BO_LOr) { |
| 8554 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 8555 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 8556 | // "a || b && 1", but warn now. |
| 8557 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8558 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8559 | } |
| 8560 | } |
| 8561 | } |
| 8562 | } |
| 8563 | |
| 8564 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8565 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8566 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8567 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(RHSExpr)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8568 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8569 | // 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] | 8570 | if (EvaluatesAsFalse(S, LHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8571 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8572 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8573 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8574 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8575 | } |
| 8576 | } |
| 8577 | } |
| 8578 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8579 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 8580 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 8581 | Expr *OrArg) { |
| 8582 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 8583 | if (Bop->getOpcode() == BO_And) |
| 8584 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 8585 | } |
| 8586 | } |
| 8587 | |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8588 | static void DiagnoseAdditionInShift(Sema &S, SourceLocation OpLoc, |
| 8589 | Expr *SubExpr, StringRef shift) { |
| 8590 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(SubExpr)) { |
| 8591 | if (Bop->getOpcode() == BO_Add || Bop->getOpcode() == BO_Sub) { |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8592 | StringRef Op = Bop->getOpcodeStr(); |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8593 | S.Diag(Bop->getOperatorLoc(), diag::warn_addition_in_bitshift) |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8594 | << Bop->getSourceRange() << OpLoc << Op << shift; |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8595 | SuggestParentheses(S, Bop->getOperatorLoc(), |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8596 | S.PDiag(diag::note_precedence_silence) << Op, |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8597 | Bop->getSourceRange()); |
| 8598 | } |
| 8599 | } |
| 8600 | } |
| 8601 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8602 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8603 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8604 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8605 | SourceLocation OpLoc, Expr *LHSExpr, |
| 8606 | Expr *RHSExpr){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8607 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8608 | if (BinaryOperator::isBitwiseOp(Opc)) |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8609 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8610 | |
| 8611 | // Diagnose "arg1 & arg2 | arg3" |
| 8612 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8613 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, LHSExpr); |
| 8614 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8615 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8616 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8617 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8618 | // 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] | 8619 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8620 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); |
| 8621 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8622 | } |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8623 | |
| 8624 | if ((Opc == BO_Shl && LHSExpr->getType()->isIntegralType(Self.getASTContext())) |
| 8625 | || Opc == BO_Shr) { |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8626 | StringRef shift = BinaryOperator::getOpcodeStr(Opc); |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8627 | DiagnoseAdditionInShift(Self, OpLoc, LHSExpr, shift); |
| 8628 | DiagnoseAdditionInShift(Self, OpLoc, RHSExpr, shift); |
| 8629 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8630 | } |
| 8631 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8632 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8633 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8634 | tok::TokenKind Kind, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8635 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8636 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8637 | assert((LHSExpr != 0) && "ActOnBinOp(): missing left expression"); |
| 8638 | assert((RHSExpr != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8639 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8640 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8641 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8642 | |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8643 | return BuildBinOp(S, TokLoc, Opc, LHSExpr, RHSExpr); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8644 | } |
| 8645 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8646 | /// Build an overloaded binary operator expression in the given scope. |
| 8647 | static ExprResult BuildOverloadedBinOp(Sema &S, Scope *Sc, SourceLocation OpLoc, |
| 8648 | BinaryOperatorKind Opc, |
| 8649 | Expr *LHS, Expr *RHS) { |
| 8650 | // Find all of the overloaded operators visible from this |
| 8651 | // point. We perform both an operator-name lookup from the local |
| 8652 | // scope and an argument-dependent lookup based on the types of |
| 8653 | // the arguments. |
| 8654 | UnresolvedSet<16> Functions; |
| 8655 | OverloadedOperatorKind OverOp |
| 8656 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8657 | if (Sc && OverOp != OO_None) |
| 8658 | S.LookupOverloadedOperatorName(OverOp, Sc, LHS->getType(), |
| 8659 | RHS->getType(), Functions); |
| 8660 | |
| 8661 | // Build the (potentially-overloaded, potentially-dependent) |
| 8662 | // binary operation. |
| 8663 | return S.CreateOverloadedBinOp(OpLoc, Opc, Functions, LHS, RHS); |
| 8664 | } |
| 8665 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8666 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8667 | BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8668 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8669 | // We want to end up calling one of checkPseudoObjectAssignment |
| 8670 | // (if the LHS is a pseudo-object), BuildOverloadedBinOp (if |
| 8671 | // both expressions are overloadable or either is type-dependent), |
| 8672 | // or CreateBuiltinBinOp (in any other case). We also want to get |
| 8673 | // any placeholder types out of the way. |
| 8674 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8675 | // Handle pseudo-objects in the LHS. |
| 8676 | if (const BuiltinType *pty = LHSExpr->getType()->getAsPlaceholderType()) { |
| 8677 | // Assignments with a pseudo-object l-value need special analysis. |
| 8678 | if (pty->getKind() == BuiltinType::PseudoObject && |
| 8679 | BinaryOperator::isAssignmentOp(Opc)) |
| 8680 | return checkPseudoObjectAssignment(S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8681 | |
| 8682 | // Don't resolve overloads if the other type is overloadable. |
| 8683 | if (pty->getKind() == BuiltinType::Overload) { |
| 8684 | // We can't actually test that if we still have a placeholder, |
| 8685 | // though. Fortunately, none of the exceptions we see in that |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8686 | // code below are valid when the LHS is an overload set. Note |
| 8687 | // that an overload set can be dependently-typed, but it never |
| 8688 | // instantiates to having an overloadable type. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8689 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); |
| 8690 | if (resolvedRHS.isInvalid()) return ExprError(); |
| 8691 | RHSExpr = resolvedRHS.take(); |
| 8692 | |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8693 | if (RHSExpr->isTypeDependent() || |
| 8694 | RHSExpr->getType()->isOverloadableType()) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8695 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8696 | } |
| 8697 | |
| 8698 | ExprResult LHS = CheckPlaceholderExpr(LHSExpr); |
| 8699 | if (LHS.isInvalid()) return ExprError(); |
| 8700 | LHSExpr = LHS.take(); |
| 8701 | } |
| 8702 | |
| 8703 | // Handle pseudo-objects in the RHS. |
| 8704 | if (const BuiltinType *pty = RHSExpr->getType()->getAsPlaceholderType()) { |
| 8705 | // An overload in the RHS can potentially be resolved by the type |
| 8706 | // being assigned to. |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8707 | if (Opc == BO_Assign && pty->getKind() == BuiltinType::Overload) { |
| 8708 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) |
| 8709 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8710 | |
Eli Friedman | 8788491 | 2012-01-17 21:27:43 +0000 | [diff] [blame] | 8711 | if (LHSExpr->getType()->isOverloadableType()) |
| 8712 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8713 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8714 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8715 | } |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8716 | |
| 8717 | // Don't resolve overloads if the other type is overloadable. |
| 8718 | if (pty->getKind() == BuiltinType::Overload && |
| 8719 | LHSExpr->getType()->isOverloadableType()) |
| 8720 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8721 | |
| 8722 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); |
| 8723 | if (!resolvedRHS.isUsable()) return ExprError(); |
| 8724 | RHSExpr = resolvedRHS.take(); |
| 8725 | } |
| 8726 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8727 | if (getLangOpts().CPlusPlus) { |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8728 | // If either expression is type-dependent, always build an |
| 8729 | // overloaded op. |
| 8730 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) |
| 8731 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8732 | |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8733 | // Otherwise, build an overloaded op if either expression has an |
| 8734 | // overloadable type. |
| 8735 | if (LHSExpr->getType()->isOverloadableType() || |
| 8736 | RHSExpr->getType()->isOverloadableType()) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8737 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8738 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8739 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8740 | // Build a built-in binary operation. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8741 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8742 | } |
| 8743 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8744 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8745 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8746 | Expr *InputExpr) { |
| 8747 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8748 | ExprValueKind VK = VK_RValue; |
| 8749 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8750 | QualType resultType; |
| 8751 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8752 | case UO_PreInc: |
| 8753 | case UO_PreDec: |
| 8754 | case UO_PostInc: |
| 8755 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8756 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8757 | Opc == UO_PreInc || |
| 8758 | Opc == UO_PostInc, |
| 8759 | Opc == UO_PreInc || |
| 8760 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8761 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8762 | case UO_AddrOf: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8763 | resultType = CheckAddressOfOperand(*this, Input, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8764 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8765 | case UO_Deref: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8766 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 8767 | if (Input.isInvalid()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8768 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8769 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8770 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8771 | case UO_Plus: |
| 8772 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8773 | Input = UsualUnaryConversions(Input.take()); |
| 8774 | if (Input.isInvalid()) return ExprError(); |
| 8775 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8776 | if (resultType->isDependentType()) |
| 8777 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8778 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8779 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8780 | break; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8781 | else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6-7 |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8782 | resultType->isEnumeralType()) |
| 8783 | break; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8784 | else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8785 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8786 | resultType->isPointerType()) |
| 8787 | break; |
| 8788 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8789 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8790 | << resultType << Input.get()->getSourceRange()); |
| 8791 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8792 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8793 | Input = UsualUnaryConversions(Input.take()); |
| 8794 | if (Input.isInvalid()) return ExprError(); |
| 8795 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8796 | if (resultType->isDependentType()) |
| 8797 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8798 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8799 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8800 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8801 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8802 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8803 | else if (resultType->hasIntegerRepresentation()) |
| 8804 | break; |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8805 | else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8806 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8807 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8808 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8809 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8810 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8811 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8812 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8813 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8814 | if (Input.isInvalid()) return ExprError(); |
| 8815 | resultType = Input.get()->getType(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 8816 | |
| 8817 | // Though we still have to promote half FP to float... |
| 8818 | if (resultType->isHalfType()) { |
| 8819 | Input = ImpCastExprToType(Input.take(), Context.FloatTy, CK_FloatingCast).take(); |
| 8820 | resultType = Context.FloatTy; |
| 8821 | } |
| 8822 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8823 | if (resultType->isDependentType()) |
| 8824 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8825 | if (resultType->isScalarType()) { |
| 8826 | // C99 6.5.3.3p1: ok, fallthrough; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8827 | if (Context.getLangOpts().CPlusPlus) { |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8828 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8829 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8830 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8831 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8832 | } |
Tanya Lattner | b0f9dd2 | 2012-01-19 01:16:16 +0000 | [diff] [blame] | 8833 | } else if (resultType->isExtVectorType()) { |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 8834 | // Vector logical not returns the signed variant of the operand type. |
| 8835 | resultType = GetSignedVectorType(resultType); |
| 8836 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8837 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8838 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8839 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8840 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8841 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8842 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8843 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8844 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8845 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8846 | case UO_Real: |
| 8847 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8848 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8849 | // _Real maps ordinary l-values into ordinary l-values. _Imag maps ordinary |
| 8850 | // 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] | 8851 | if (Input.isInvalid()) return ExprError(); |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8852 | if (Opc == UO_Real || Input.get()->getType()->isAnyComplexType()) { |
| 8853 | if (Input.get()->getValueKind() != VK_RValue && |
| 8854 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8855 | VK = Input.get()->getValueKind(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8856 | } else if (!getLangOpts().CPlusPlus) { |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8857 | // In C, a volatile scalar is read by __imag. In C++, it is not. |
| 8858 | Input = DefaultLvalueConversion(Input.take()); |
| 8859 | } |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8860 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8861 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8862 | resultType = Input.get()->getType(); |
| 8863 | VK = Input.get()->getValueKind(); |
| 8864 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8865 | break; |
| 8866 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8867 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8868 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8869 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8870 | // Check for array bounds violations in the operand of the UnaryOperator, |
| 8871 | // except for the '*' and '&' operators that have to be handled specially |
| 8872 | // by CheckArrayAccess (as there are special cases like &array[arraysize] |
| 8873 | // that are explicitly defined as valid by the standard). |
| 8874 | if (Opc != UO_AddrOf && Opc != UO_Deref) |
| 8875 | CheckArrayAccess(Input.get()); |
| 8876 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8877 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8878 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8879 | } |
| 8880 | |
Douglas Gregor | d3d0853 | 2011-12-14 21:23:13 +0000 | [diff] [blame] | 8881 | /// \brief Determine whether the given expression is a qualified member |
| 8882 | /// access expression, of a form that could be turned into a pointer to member |
| 8883 | /// with the address-of operator. |
| 8884 | static bool isQualifiedMemberAccess(Expr *E) { |
| 8885 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 8886 | if (!DRE->getQualifier()) |
| 8887 | return false; |
| 8888 | |
| 8889 | ValueDecl *VD = DRE->getDecl(); |
| 8890 | if (!VD->isCXXClassMember()) |
| 8891 | return false; |
| 8892 | |
| 8893 | if (isa<FieldDecl>(VD) || isa<IndirectFieldDecl>(VD)) |
| 8894 | return true; |
| 8895 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(VD)) |
| 8896 | return Method->isInstance(); |
| 8897 | |
| 8898 | return false; |
| 8899 | } |
| 8900 | |
| 8901 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 8902 | if (!ULE->getQualifier()) |
| 8903 | return false; |
| 8904 | |
| 8905 | for (UnresolvedLookupExpr::decls_iterator D = ULE->decls_begin(), |
| 8906 | DEnd = ULE->decls_end(); |
| 8907 | D != DEnd; ++D) { |
| 8908 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*D)) { |
| 8909 | if (Method->isInstance()) |
| 8910 | return true; |
| 8911 | } else { |
| 8912 | // Overload set does not contain methods. |
| 8913 | break; |
| 8914 | } |
| 8915 | } |
| 8916 | |
| 8917 | return false; |
| 8918 | } |
| 8919 | |
| 8920 | return false; |
| 8921 | } |
| 8922 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8923 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8924 | UnaryOperatorKind Opc, Expr *Input) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8925 | // First things first: handle placeholders so that the |
| 8926 | // overloaded-operator check considers the right type. |
| 8927 | if (const BuiltinType *pty = Input->getType()->getAsPlaceholderType()) { |
| 8928 | // Increment and decrement of pseudo-object references. |
| 8929 | if (pty->getKind() == BuiltinType::PseudoObject && |
| 8930 | UnaryOperator::isIncrementDecrementOp(Opc)) |
| 8931 | return checkPseudoObjectIncDec(S, OpLoc, Opc, Input); |
| 8932 | |
| 8933 | // extension is always a builtin operator. |
| 8934 | if (Opc == UO_Extension) |
| 8935 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
| 8936 | |
| 8937 | // & gets special logic for several kinds of placeholder. |
| 8938 | // The builtin code knows what to do. |
| 8939 | if (Opc == UO_AddrOf && |
| 8940 | (pty->getKind() == BuiltinType::Overload || |
| 8941 | pty->getKind() == BuiltinType::UnknownAny || |
| 8942 | pty->getKind() == BuiltinType::BoundMember)) |
| 8943 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
| 8944 | |
| 8945 | // Anything else needs to be handled now. |
| 8946 | ExprResult Result = CheckPlaceholderExpr(Input); |
| 8947 | if (Result.isInvalid()) return ExprError(); |
| 8948 | Input = Result.take(); |
| 8949 | } |
| 8950 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8951 | if (getLangOpts().CPlusPlus && Input->getType()->isOverloadableType() && |
Douglas Gregor | d3d0853 | 2011-12-14 21:23:13 +0000 | [diff] [blame] | 8952 | UnaryOperator::getOverloadedOperator(Opc) != OO_None && |
| 8953 | !(Opc == UO_AddrOf && isQualifiedMemberAccess(Input))) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8954 | // Find all of the overloaded operators visible from this |
| 8955 | // point. We perform both an operator-name lookup from the local |
| 8956 | // scope and an argument-dependent lookup based on the types of |
| 8957 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8958 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8959 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8960 | if (S && OverOp != OO_None) |
| 8961 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8962 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8963 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8964 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8965 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8966 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8967 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8968 | } |
| 8969 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8970 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8971 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8972 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8973 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8974 | } |
| 8975 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8976 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8977 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8978 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8979 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8980 | // 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] | 8981 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8982 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8983 | } |
| 8984 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8985 | /// Given the last statement in a statement-expression, check whether |
| 8986 | /// the result is a producing expression (like a call to an |
| 8987 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 8988 | /// release out of the full-expression. Otherwise, return null. |
| 8989 | /// Cannot fail. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8990 | static Expr *maybeRebuildARCConsumingStmt(Stmt *Statement) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8991 | // Should always be wrapped with one of these. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8992 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(Statement); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8993 | if (!cleanups) return 0; |
| 8994 | |
| 8995 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 8996 | if (!cast || cast->getCastKind() != CK_ARCConsumeObject) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8997 | return 0; |
| 8998 | |
| 8999 | // Splice out the cast. This shouldn't modify any interesting |
| 9000 | // features of the statement. |
| 9001 | Expr *producer = cast->getSubExpr(); |
| 9002 | assert(producer->getType() == cast->getType()); |
| 9003 | assert(producer->getValueKind() == cast->getValueKind()); |
| 9004 | cleanups->setSubExpr(producer); |
| 9005 | return cleanups; |
| 9006 | } |
| 9007 | |
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 9008 | void Sema::ActOnStartStmtExpr() { |
| 9009 | PushExpressionEvaluationContext(ExprEvalContexts.back().Context); |
| 9010 | } |
| 9011 | |
| 9012 | void Sema::ActOnStmtExprError() { |
John McCall | 7f39d51 | 2012-04-06 18:20:53 +0000 | [diff] [blame] | 9013 | // Note that function is also called by TreeTransform when leaving a |
| 9014 | // StmtExpr scope without rebuilding anything. |
| 9015 | |
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 9016 | DiscardCleanupsInEvaluationContext(); |
| 9017 | PopExpressionEvaluationContext(); |
| 9018 | } |
| 9019 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9020 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9021 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9022 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9023 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 9024 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 9025 | |
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 9026 | if (hasAnyUnrecoverableErrorsInThisFunction()) |
| 9027 | DiscardCleanupsInEvaluationContext(); |
| 9028 | assert(!ExprNeedsCleanups && "cleanups within StmtExpr not correctly bound!"); |
| 9029 | PopExpressionEvaluationContext(); |
| 9030 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 9031 | bool isFileScope |
| 9032 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 9033 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9034 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 9035 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9036 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 9037 | // example, it is not possible to goto into a stmt expression apparently. |
| 9038 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9039 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9040 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 9041 | // as the type of the stmtexpr. |
| 9042 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9043 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9044 | if (!Compound->body_empty()) { |
| 9045 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9046 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9047 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9048 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 9049 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9050 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9051 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9052 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9053 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9054 | // Do function/array conversion on the last expression, but not |
| 9055 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9056 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 9057 | if (LastExpr.isInvalid()) |
| 9058 | return ExprError(); |
| 9059 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9060 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9061 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9062 | // In ARC, if the final expression ends in a consume, splice |
| 9063 | // the consume out and bind it later. In the alternate case |
| 9064 | // (when dealing with a retainable type), the result |
| 9065 | // initialization will create a produce. In both cases the |
| 9066 | // result will be +1, and we'll need to balance that out with |
| 9067 | // a bind. |
| 9068 | if (Expr *rebuiltLastStmt |
| 9069 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 9070 | LastExpr = rebuiltLastStmt; |
| 9071 | } else { |
| 9072 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9073 | InitializedEntity::InitializeResult(LPLoc, |
| 9074 | Ty, |
| 9075 | false), |
| 9076 | SourceLocation(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9077 | LastExpr); |
| 9078 | } |
| 9079 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9080 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9081 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9082 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9083 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9084 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9085 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9086 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9087 | StmtExprMayBindToTemp = true; |
| 9088 | } |
| 9089 | } |
| 9090 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9091 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9092 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9093 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 9094 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9095 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 9096 | if (StmtExprMayBindToTemp) |
| 9097 | return MaybeBindToTemporary(ResStmtExpr); |
| 9098 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9099 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 9100 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9101 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9102 | TypeSourceInfo *TInfo, |
| 9103 | OffsetOfComponent *CompPtr, |
| 9104 | unsigned NumComponents, |
| 9105 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9106 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9107 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9108 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9109 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9110 | // We must have at least one component that refers to the type, and the first |
| 9111 | // one is known to be a field designator. Verify that the ArgTy represents |
| 9112 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9113 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9114 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 9115 | << ArgTy << TypeRange); |
| 9116 | |
| 9117 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 9118 | // with an incomplete type would be ill-formed. |
| 9119 | if (!Dependent |
| 9120 | && RequireCompleteType(BuiltinLoc, ArgTy, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 9121 | diag::err_offsetof_incomplete_type, TypeRange)) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9122 | return ExprError(); |
| 9123 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9124 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 9125 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 9126 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 9127 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9128 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 9129 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 9130 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9131 | |
| 9132 | bool DidWarnAboutNonPOD = false; |
| 9133 | QualType CurrentType = ArgTy; |
| 9134 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9135 | SmallVector<OffsetOfNode, 4> Comps; |
| 9136 | SmallVector<Expr*, 4> Exprs; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9137 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 9138 | const OffsetOfComponent &OC = CompPtr[i]; |
| 9139 | if (OC.isBrackets) { |
| 9140 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 9141 | if (!CurrentType->isDependentType()) { |
| 9142 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 9143 | if(!AT) |
| 9144 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 9145 | << CurrentType); |
| 9146 | CurrentType = AT->getElementType(); |
| 9147 | } else |
| 9148 | CurrentType = Context.DependentTy; |
| 9149 | |
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 9150 | ExprResult IdxRval = DefaultLvalueConversion(static_cast<Expr*>(OC.U.E)); |
| 9151 | if (IdxRval.isInvalid()) |
| 9152 | return ExprError(); |
| 9153 | Expr *Idx = IdxRval.take(); |
| 9154 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9155 | // The expression must be an integral expression. |
| 9156 | // FIXME: An integral constant expression? |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9157 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 9158 | !Idx->getType()->isIntegerType()) |
| 9159 | return ExprError(Diag(Idx->getLocStart(), |
| 9160 | diag::err_typecheck_subscript_not_integer) |
| 9161 | << Idx->getSourceRange()); |
Richard Smith | d82e5d3 | 2011-10-17 05:48:07 +0000 | [diff] [blame] | 9162 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9163 | // Record this array index. |
| 9164 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 9165 | Exprs.push_back(Idx); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9166 | continue; |
| 9167 | } |
| 9168 | |
| 9169 | // Offset of a field. |
| 9170 | if (CurrentType->isDependentType()) { |
| 9171 | // We have the offset of a field, but we can't look into the dependent |
| 9172 | // type. Just record the identifier of the field. |
| 9173 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 9174 | CurrentType = Context.DependentTy; |
| 9175 | continue; |
| 9176 | } |
| 9177 | |
| 9178 | // We need to have a complete type to look into. |
| 9179 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 9180 | diag::err_offsetof_incomplete_type)) |
| 9181 | return ExprError(); |
| 9182 | |
| 9183 | // Look for the designated field. |
| 9184 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 9185 | if (!RC) |
| 9186 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 9187 | << CurrentType); |
| 9188 | RecordDecl *RD = RC->getDecl(); |
| 9189 | |
| 9190 | // C++ [lib.support.types]p5: |
| 9191 | // The macro offsetof accepts a restricted set of type arguments in this |
| 9192 | // International Standard. type shall be a POD structure or a POD union |
| 9193 | // (clause 9). |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9194 | // C++11 [support.types]p4: |
| 9195 | // If type is not a standard-layout class (Clause 9), the results are |
| 9196 | // undefined. |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9197 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9198 | bool IsSafe = LangOpts.CPlusPlus0x? CRD->isStandardLayout() : CRD->isPOD(); |
| 9199 | unsigned DiagID = |
| 9200 | LangOpts.CPlusPlus0x? diag::warn_offsetof_non_standardlayout_type |
| 9201 | : diag::warn_offsetof_non_pod_type; |
| 9202 | |
| 9203 | if (!IsSafe && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9204 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9205 | PDiag(DiagID) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9206 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 9207 | << CurrentType)) |
| 9208 | DidWarnAboutNonPOD = true; |
| 9209 | } |
| 9210 | |
| 9211 | // Look for the field. |
| 9212 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 9213 | LookupQualifiedName(R, RD); |
| 9214 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9215 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 9216 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 9217 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9218 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 9219 | } |
| 9220 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9221 | if (!MemberDecl) |
| 9222 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 9223 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 9224 | OC.LocEnd)); |
| 9225 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9226 | // C99 7.17p3: |
| 9227 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 9228 | // |
| 9229 | // We diagnose this as an error. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9230 | if (MemberDecl->isBitField()) { |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9231 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 9232 | << MemberDecl->getDeclName() |
| 9233 | << SourceRange(BuiltinLoc, RParenLoc); |
| 9234 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 9235 | return ExprError(); |
| 9236 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9237 | |
| 9238 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9239 | if (IndirectMemberDecl) |
| 9240 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9241 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9242 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 9243 | // the base class indirections. |
| 9244 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 9245 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9246 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9247 | CXXBasePath &Path = Paths.front(); |
| 9248 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 9249 | B != BEnd; ++B) |
| 9250 | Comps.push_back(OffsetOfNode(B->Base)); |
| 9251 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9252 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9253 | if (IndirectMemberDecl) { |
| 9254 | for (IndirectFieldDecl::chain_iterator FI = |
| 9255 | IndirectMemberDecl->chain_begin(), |
| 9256 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 9257 | assert(isa<FieldDecl>(*FI)); |
| 9258 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 9259 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 9260 | } |
| 9261 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9262 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9263 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9264 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9265 | } |
| 9266 | |
| 9267 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 9268 | TInfo, Comps, Exprs, RParenLoc)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9269 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9270 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9271 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9272 | SourceLocation BuiltinLoc, |
| 9273 | SourceLocation TypeLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9274 | ParsedType ParsedArgTy, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9275 | OffsetOfComponent *CompPtr, |
| 9276 | unsigned NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9277 | SourceLocation RParenLoc) { |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9278 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9279 | TypeSourceInfo *ArgTInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9280 | QualType ArgTy = GetTypeFromParser(ParsedArgTy, &ArgTInfo); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9281 | if (ArgTy.isNull()) |
| 9282 | return ExprError(); |
| 9283 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 9284 | if (!ArgTInfo) |
| 9285 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 9286 | |
| 9287 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9288 | RParenLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9289 | } |
| 9290 | |
| 9291 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9292 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9293 | Expr *CondExpr, |
| 9294 | Expr *LHSExpr, Expr *RHSExpr, |
| 9295 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9296 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 9297 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9298 | ExprValueKind VK = VK_RValue; |
| 9299 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9300 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9301 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 9302 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9303 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9304 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9305 | } else { |
| 9306 | // The conditional expression is required to be a constant expression. |
| 9307 | llvm::APSInt condEval(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9308 | ExprResult CondICE |
| 9309 | = VerifyIntegerConstantExpression(CondExpr, &condEval, |
| 9310 | diag::err_typecheck_choose_expr_requires_constant, false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9311 | if (CondICE.isInvalid()) |
| 9312 | return ExprError(); |
| 9313 | CondExpr = CondICE.take(); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9314 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9315 | // 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] | 9316 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 9317 | |
| 9318 | resType = ActiveExpr->getType(); |
| 9319 | ValueDependent = ActiveExpr->isValueDependent(); |
| 9320 | VK = ActiveExpr->getValueKind(); |
| 9321 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9322 | } |
| 9323 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9324 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9325 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9326 | resType->isDependentType(), |
| 9327 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9328 | } |
| 9329 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9330 | //===----------------------------------------------------------------------===// |
| 9331 | // Clang Extensions. |
| 9332 | //===----------------------------------------------------------------------===// |
| 9333 | |
| 9334 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9335 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9336 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9337 | PushBlockScope(CurScope, Block); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9338 | CurContext->addDecl(Block); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9339 | if (CurScope) |
| 9340 | PushDeclContext(CurScope, Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9341 | else |
| 9342 | CurContext = Block; |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9343 | |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 9344 | getCurBlock()->HasImplicitReturnType = true; |
| 9345 | |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9346 | // Enter a new evaluation context to insulate the block from any |
| 9347 | // cleanups from the enclosing full-expression. |
| 9348 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9349 | } |
| 9350 | |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 9351 | void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, |
| 9352 | Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 9353 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9354 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9355 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9356 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9357 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9358 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9359 | |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 9360 | // FIXME: We should allow unexpanded parameter packs here, but that would, |
| 9361 | // in turn, make the block expression contain unexpanded parameter packs. |
| 9362 | if (DiagnoseUnexpandedParameterPack(CaretLoc, Sig, UPPC_Block)) { |
| 9363 | // Drop the parameters. |
| 9364 | FunctionProtoType::ExtProtoInfo EPI; |
| 9365 | EPI.HasTrailingReturn = false; |
| 9366 | EPI.TypeQuals |= DeclSpec::TQ_const; |
| 9367 | T = Context.getFunctionType(Context.DependentTy, /*Args=*/0, /*NumArgs=*/0, |
| 9368 | EPI); |
| 9369 | Sig = Context.getTrivialTypeSourceInfo(T); |
| 9370 | } |
| 9371 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9372 | // GetTypeForDeclarator always produces a function type for a block |
| 9373 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 9374 | // unless the function was written with a typedef. |
| 9375 | assert(T->isFunctionType() && |
| 9376 | "GetTypeForDeclarator made a non-function block signature"); |
| 9377 | |
| 9378 | // Look for an explicit signature in that function type. |
| 9379 | FunctionProtoTypeLoc ExplicitSignature; |
| 9380 | |
| 9381 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 9382 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 9383 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 9384 | |
| 9385 | // Check whether that explicit signature was synthesized by |
| 9386 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 9387 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 9388 | if (ExplicitSignature.getLocalRangeBegin() == |
| 9389 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9390 | // This would be much cheaper if we stored TypeLocs instead of |
| 9391 | // TypeSourceInfos. |
| 9392 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 9393 | unsigned Size = Result.getFullDataSize(); |
| 9394 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 9395 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 9396 | |
| 9397 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 9398 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9399 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9400 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9401 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 9402 | CurBlock->FunctionType = T; |
| 9403 | |
| 9404 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 9405 | QualType RetTy = Fn->getResultType(); |
| 9406 | bool isVariadic = |
| 9407 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 9408 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9409 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 9410 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9411 | // Don't allow returning a objc interface by value. |
| 9412 | if (RetTy->isObjCObjectType()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9413 | Diag(ParamInfo.getLocStart(), |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9414 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 9415 | return; |
| 9416 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9417 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9418 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9419 | // return type. TODO: what should we do with declarators like: |
| 9420 | // ^ * { ... } |
| 9421 | // If the answer is "apply template argument deduction".... |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9422 | if (RetTy != Context.DependentTy) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9423 | CurBlock->ReturnType = RetTy; |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9424 | CurBlock->TheDecl->setBlockMissingReturnType(false); |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 9425 | CurBlock->HasImplicitReturnType = false; |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9426 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9427 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9428 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9429 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9430 | if (ExplicitSignature) { |
| 9431 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 9432 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9433 | if (Param->getIdentifier() == 0 && |
| 9434 | !Param->isImplicit() && |
| 9435 | !Param->isInvalidDecl() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9436 | !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9437 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9438 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9439 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9440 | |
| 9441 | // Fake up parameter variables if we have a typedef, like |
| 9442 | // ^ fntype { ... } |
| 9443 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 9444 | for (FunctionProtoType::arg_type_iterator |
| 9445 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 9446 | ParmVarDecl *Param = |
| 9447 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9448 | ParamInfo.getLocStart(), |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9449 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9450 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9451 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9452 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9453 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9454 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9455 | if (!Params.empty()) { |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9456 | CurBlock->TheDecl->setParams(Params); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9457 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 9458 | CurBlock->TheDecl->param_end(), |
| 9459 | /*CheckParameterNames=*/false); |
| 9460 | } |
| 9461 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9462 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9463 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9464 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9465 | // Put the parameter variables in scope. We can bail out immediately |
| 9466 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9467 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9468 | return; |
| 9469 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9470 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9471 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 9472 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 9473 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9474 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9475 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9476 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9477 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9478 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9479 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9480 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9481 | } |
| 9482 | |
| 9483 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 9484 | /// is invoked to pop the information about the block from the action impl. |
| 9485 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9486 | // Leave the expression-evaluation context. |
| 9487 | DiscardCleanupsInEvaluationContext(); |
| 9488 | PopExpressionEvaluationContext(); |
| 9489 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9490 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9491 | PopDeclContext(); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9492 | PopFunctionScopeInfo(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9493 | } |
| 9494 | |
| 9495 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 9496 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9497 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9498 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9499 | // If blocks are disabled, emit an error. |
| 9500 | if (!LangOpts.Blocks) |
| 9501 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9502 | |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9503 | // Leave the expression-evaluation context. |
John McCall | 1e5bc4f | 2012-03-08 22:00:17 +0000 | [diff] [blame] | 9504 | if (hasAnyUnrecoverableErrorsInThisFunction()) |
| 9505 | DiscardCleanupsInEvaluationContext(); |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9506 | assert(!ExprNeedsCleanups && "cleanups within block not correctly bound!"); |
| 9507 | PopExpressionEvaluationContext(); |
| 9508 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9509 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9510 | |
| 9511 | if (BSI->HasImplicitReturnType) |
| 9512 | deduceClosureReturnType(*BSI); |
| 9513 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9514 | PopDeclContext(); |
| 9515 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9516 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9517 | if (!BSI->ReturnType.isNull()) |
| 9518 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9519 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9520 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9521 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9522 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9523 | // Set the captured variables on the block. |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 9524 | // FIXME: Share capture structure between BlockDecl and CapturingScopeInfo! |
| 9525 | SmallVector<BlockDecl::Capture, 4> Captures; |
| 9526 | for (unsigned i = 0, e = BSI->Captures.size(); i != e; i++) { |
| 9527 | CapturingScopeInfo::Capture &Cap = BSI->Captures[i]; |
| 9528 | if (Cap.isThisCapture()) |
| 9529 | continue; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9530 | BlockDecl::Capture NewCap(Cap.getVariable(), Cap.isBlockCapture(), |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 9531 | Cap.isNested(), Cap.getCopyExpr()); |
| 9532 | Captures.push_back(NewCap); |
| 9533 | } |
| 9534 | BSI->TheDecl->setCaptures(Context, Captures.begin(), Captures.end(), |
| 9535 | BSI->CXXThisCaptureIndex != 0); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9536 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9537 | // If the user wrote a function type in some form, try to use that. |
| 9538 | if (!BSI->FunctionType.isNull()) { |
| 9539 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 9540 | |
| 9541 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 9542 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 9543 | |
| 9544 | // Turn protoless block types into nullary block types. |
| 9545 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9546 | FunctionProtoType::ExtProtoInfo EPI; |
| 9547 | EPI.ExtInfo = Ext; |
| 9548 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9549 | |
| 9550 | // Otherwise, if we don't need to change anything about the function type, |
| 9551 | // preserve its sugar structure. |
| 9552 | } else if (FTy->getResultType() == RetTy && |
| 9553 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 9554 | BlockTy = BSI->FunctionType; |
| 9555 | |
| 9556 | // Otherwise, make the minimal modifications to the function type. |
| 9557 | } else { |
| 9558 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9559 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9560 | EPI.TypeQuals = 0; // FIXME: silently? |
| 9561 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9562 | BlockTy = Context.getFunctionType(RetTy, |
| 9563 | FPT->arg_type_begin(), |
| 9564 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9565 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9566 | } |
| 9567 | |
| 9568 | // If we don't have a function type, just build one from nothing. |
| 9569 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9570 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9571 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9572 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9573 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9574 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9575 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 9576 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9577 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9578 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 9579 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9580 | if (getCurFunction()->NeedsScopeChecking() && |
Douglas Gregor | 27bec77 | 2012-08-17 05:12:08 +0000 | [diff] [blame] | 9581 | !hasAnyUnrecoverableErrorsInThisFunction() && |
| 9582 | !PP.isCodeCompletionEnabled()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9583 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9584 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9585 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9586 | |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9587 | // Try to apply the named return value optimization. We have to check again |
| 9588 | // if we can do this, though, because blocks keep return statements around |
| 9589 | // to deduce an implicit return type. |
| 9590 | if (getLangOpts().CPlusPlus && RetTy->isRecordType() && |
| 9591 | !BSI->TheDecl->isDependentContext()) |
| 9592 | computeNRVO(Body, getCurBlock()); |
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9593 | |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 9594 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| 9595 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9596 | PopFunctionScopeInfo(&WP, Result->getBlockDecl(), Result); |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 9597 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9598 | // 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] | 9599 | // 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] | 9600 | if (Result->getBlockDecl()->hasCaptures()) { |
John McCall | 97b57a2 | 2012-04-13 01:08:17 +0000 | [diff] [blame] | 9601 | // First, this expression has a new cleanup object. |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9602 | ExprCleanupObjects.push_back(Result->getBlockDecl()); |
| 9603 | ExprNeedsCleanups = true; |
John McCall | 97b57a2 | 2012-04-13 01:08:17 +0000 | [diff] [blame] | 9604 | |
| 9605 | // It also gets a branch-protected scope if any of the captured |
| 9606 | // variables needs destruction. |
| 9607 | for (BlockDecl::capture_const_iterator |
| 9608 | ci = Result->getBlockDecl()->capture_begin(), |
| 9609 | ce = Result->getBlockDecl()->capture_end(); ci != ce; ++ci) { |
| 9610 | const VarDecl *var = ci->getVariable(); |
| 9611 | if (var->getType().isDestructedType() != QualType::DK_none) { |
| 9612 | getCurFunction()->setHasBranchProtectedScope(); |
| 9613 | break; |
| 9614 | } |
| 9615 | } |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9616 | } |
Fariborz Jahanian | 27949f6 | 2012-03-06 18:41:35 +0000 | [diff] [blame] | 9617 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9618 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9619 | } |
| 9620 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9621 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9622 | Expr *E, ParsedType Ty, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9623 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9624 | TypeSourceInfo *TInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9625 | GetTypeFromParser(Ty, &TInfo); |
| 9626 | return BuildVAArgExpr(BuiltinLoc, E, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9627 | } |
| 9628 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9629 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9630 | Expr *E, TypeSourceInfo *TInfo, |
| 9631 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9632 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9633 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9634 | // Get the va_list type |
| 9635 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9636 | if (VaListType->isArrayType()) { |
| 9637 | // Deal with implicit array decay; for example, on x86-64, |
| 9638 | // va_list is an array, but it's supposed to decay to |
| 9639 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9640 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9641 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9642 | ExprResult Result = UsualUnaryConversions(E); |
| 9643 | if (Result.isInvalid()) |
| 9644 | return ExprError(); |
| 9645 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9646 | } else { |
| 9647 | // Otherwise, the va_list argument must be an l-value because |
| 9648 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9649 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9650 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9651 | return ExprError(); |
| 9652 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9653 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9654 | if (!E->isTypeDependent() && |
| 9655 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9656 | return ExprError(Diag(E->getLocStart(), |
| 9657 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9658 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 9659 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9660 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9661 | if (!TInfo->getType()->isDependentType()) { |
| 9662 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 9663 | diag::err_second_parameter_to_va_arg_incomplete, |
| 9664 | TInfo->getTypeLoc())) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9665 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 9666 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9667 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 9668 | TInfo->getType(), |
| 9669 | diag::err_second_parameter_to_va_arg_abstract, |
| 9670 | TInfo->getTypeLoc())) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9671 | return ExprError(); |
| 9672 | |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9673 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9674 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9675 | TInfo->getType()->isObjCLifetimeType() |
| 9676 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified |
| 9677 | : diag::warn_second_parameter_to_va_arg_not_pod) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9678 | << TInfo->getType() |
| 9679 | << TInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9680 | } |
Eli Friedman | 46d37c1 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 9681 | |
| 9682 | // Check for va_arg where arguments of the given type will be promoted |
| 9683 | // (i.e. this va_arg is guaranteed to have undefined behavior). |
| 9684 | QualType PromoteType; |
| 9685 | if (TInfo->getType()->isPromotableIntegerType()) { |
| 9686 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); |
| 9687 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) |
| 9688 | PromoteType = QualType(); |
| 9689 | } |
| 9690 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) |
| 9691 | PromoteType = Context.DoubleTy; |
| 9692 | if (!PromoteType.isNull()) |
| 9693 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 9694 | diag::warn_second_parameter_to_va_arg_never_compatible) |
| 9695 | << TInfo->getType() |
| 9696 | << PromoteType |
| 9697 | << TInfo->getTypeLoc().getSourceRange(); |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9698 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9699 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9700 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 9701 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9702 | } |
| 9703 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9704 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9705 | // The type of __null will be int or long, depending on the size of |
| 9706 | // pointers on the target. |
| 9707 | QualType Ty; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9708 | unsigned pw = Context.getTargetInfo().getPointerWidth(0); |
| 9709 | if (pw == Context.getTargetInfo().getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9710 | Ty = Context.IntTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9711 | else if (pw == Context.getTargetInfo().getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9712 | Ty = Context.LongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9713 | else if (pw == Context.getTargetInfo().getLongLongWidth()) |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9714 | Ty = Context.LongLongTy; |
| 9715 | else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9716 | llvm_unreachable("I don't know size of pointer!"); |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9717 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9718 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9719 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9720 | } |
| 9721 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9722 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9723 | Expr *SrcExpr, FixItHint &Hint) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9724 | if (!SemaRef.getLangOpts().ObjC1) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9725 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9726 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9727 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 9728 | if (!PT) |
| 9729 | return; |
| 9730 | |
| 9731 | // Check if the destination is of type 'id'. |
| 9732 | if (!PT->isObjCIdType()) { |
| 9733 | // Check if the destination is the 'NSString' interface. |
| 9734 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 9735 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 9736 | return; |
| 9737 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9738 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 9739 | // Ignore any parens, implicit casts (should only be |
| 9740 | // array-to-pointer decays), and not-so-opaque values. The last is |
| 9741 | // important for making this trigger for property assignments. |
| 9742 | SrcExpr = SrcExpr->IgnoreParenImpCasts(); |
| 9743 | if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(SrcExpr)) |
| 9744 | if (OV->getSourceExpr()) |
| 9745 | SrcExpr = OV->getSourceExpr()->IgnoreParenImpCasts(); |
| 9746 | |
| 9747 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 9748 | if (!SL || !SL->isAscii()) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9749 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9750 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9751 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9752 | } |
| 9753 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9754 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 9755 | SourceLocation Loc, |
| 9756 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9757 | Expr *SrcExpr, AssignmentAction Action, |
| 9758 | bool *Complained) { |
| 9759 | if (Complained) |
| 9760 | *Complained = false; |
| 9761 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9762 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9763 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9764 | bool isInvalid = false; |
Eli Friedman | fd81978 | 2012-02-29 20:59:56 +0000 | [diff] [blame] | 9765 | unsigned DiagKind = 0; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9766 | FixItHint Hint; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9767 | ConversionFixItGenerator ConvHints; |
| 9768 | bool MayHaveConvFixit = false; |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9769 | bool MayHaveFunctionDiff = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9770 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9771 | switch (ConvTy) { |
Fariborz Jahanian | 379b281 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 9772 | case Compatible: |
| 9773 | DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr); |
| 9774 | return false; |
| 9775 | |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9776 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9777 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9778 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9779 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9780 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9781 | case IntToPointer: |
| 9782 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9783 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9784 | MayHaveConvFixit = true; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9785 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9786 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9787 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9788 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9789 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 9790 | SrcType->isObjCObjectPointerType(); |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9791 | if (Hint.isNull() && !CheckInferredResultType) { |
| 9792 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9793 | } |
| 9794 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9795 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9796 | case IncompatiblePointerSign: |
| 9797 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9798 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9799 | case FunctionVoidPointer: |
| 9800 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9801 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9802 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9803 | // Perform array-to-pointer decay if necessary. |
| 9804 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9805 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9806 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9807 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9808 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9809 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9810 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9811 | |
| 9812 | |
| 9813 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 9814 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9815 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9816 | } |
| 9817 | |
| 9818 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9819 | // fallthrough |
| 9820 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9821 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9822 | // If the qualifiers lost were because we were applying the |
| 9823 | // (deprecated) C++ conversion from a string literal to a char* |
| 9824 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9825 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9826 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9827 | // bit of refactoring (so that the second argument is an |
| 9828 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9829 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9830 | // C++ semantics. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9831 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9832 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9833 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9834 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9835 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9836 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9837 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9838 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9839 | case IntToBlockPointer: |
| 9840 | DiagKind = diag::err_int_to_block_pointer; |
| 9841 | break; |
| 9842 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9843 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9844 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9845 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9846 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9847 | // it can give a more specific diagnostic. |
| 9848 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9849 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9850 | case IncompatibleVectors: |
| 9851 | DiagKind = diag::warn_incompatible_vectors; |
| 9852 | break; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 9853 | case IncompatibleObjCWeakRef: |
| 9854 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 9855 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9856 | case Incompatible: |
| 9857 | DiagKind = diag::err_typecheck_convert_incompatible; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9858 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9859 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9860 | isInvalid = true; |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9861 | MayHaveFunctionDiff = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9862 | break; |
| 9863 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9864 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9865 | QualType FirstType, SecondType; |
| 9866 | switch (Action) { |
| 9867 | case AA_Assigning: |
| 9868 | case AA_Initializing: |
| 9869 | // The destination type comes first. |
| 9870 | FirstType = DstType; |
| 9871 | SecondType = SrcType; |
| 9872 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9873 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9874 | case AA_Returning: |
| 9875 | case AA_Passing: |
| 9876 | case AA_Converting: |
| 9877 | case AA_Sending: |
| 9878 | case AA_Casting: |
| 9879 | // The source type comes first. |
| 9880 | FirstType = SrcType; |
| 9881 | SecondType = DstType; |
| 9882 | break; |
| 9883 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9884 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9885 | PartialDiagnostic FDiag = PDiag(DiagKind); |
| 9886 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); |
| 9887 | |
| 9888 | // If we can fix the conversion, suggest the FixIts. |
| 9889 | assert(ConvHints.isNull() || Hint.isNull()); |
| 9890 | if (!ConvHints.isNull()) { |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 9891 | for (std::vector<FixItHint>::iterator HI = ConvHints.Hints.begin(), |
| 9892 | HE = ConvHints.Hints.end(); HI != HE; ++HI) |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9893 | FDiag << *HI; |
| 9894 | } else { |
| 9895 | FDiag << Hint; |
| 9896 | } |
| 9897 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } |
| 9898 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9899 | if (MayHaveFunctionDiff) |
| 9900 | HandleFunctionTypeMismatch(FDiag, SecondType, FirstType); |
| 9901 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9902 | Diag(Loc, FDiag); |
| 9903 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9904 | if (SecondType == Context.OverloadTy) |
| 9905 | NoteAllOverloadCandidates(OverloadExpr::find(SrcExpr).Expression, |
| 9906 | FirstType); |
| 9907 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9908 | if (CheckInferredResultType) |
| 9909 | EmitRelatedResultTypeNote(SrcExpr); |
| 9910 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9911 | if (Complained) |
| 9912 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9913 | return isInvalid; |
| 9914 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9915 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9916 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, |
| 9917 | llvm::APSInt *Result) { |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9918 | class SimpleICEDiagnoser : public VerifyICEDiagnoser { |
| 9919 | public: |
| 9920 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { |
| 9921 | S.Diag(Loc, diag::err_expr_not_ice) << S.LangOpts.CPlusPlus << SR; |
| 9922 | } |
| 9923 | } Diagnoser; |
| 9924 | |
| 9925 | return VerifyIntegerConstantExpression(E, Result, Diagnoser); |
| 9926 | } |
| 9927 | |
| 9928 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, |
| 9929 | llvm::APSInt *Result, |
| 9930 | unsigned DiagID, |
| 9931 | bool AllowFold) { |
| 9932 | class IDDiagnoser : public VerifyICEDiagnoser { |
| 9933 | unsigned DiagID; |
| 9934 | |
| 9935 | public: |
| 9936 | IDDiagnoser(unsigned DiagID) |
| 9937 | : VerifyICEDiagnoser(DiagID == 0), DiagID(DiagID) { } |
| 9938 | |
| 9939 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { |
| 9940 | S.Diag(Loc, DiagID) << SR; |
| 9941 | } |
| 9942 | } Diagnoser(DiagID); |
| 9943 | |
| 9944 | return VerifyIntegerConstantExpression(E, Result, Diagnoser, AllowFold); |
| 9945 | } |
| 9946 | |
| 9947 | void Sema::VerifyICEDiagnoser::diagnoseFold(Sema &S, SourceLocation Loc, |
| 9948 | SourceRange SR) { |
| 9949 | S.Diag(Loc, diag::ext_expr_not_ice) << SR << S.LangOpts.CPlusPlus; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9950 | } |
| 9951 | |
Benjamin Kramer | d448ce0 | 2012-04-18 14:22:41 +0000 | [diff] [blame] | 9952 | ExprResult |
| 9953 | Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9954 | VerifyICEDiagnoser &Diagnoser, |
| 9955 | bool AllowFold) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9956 | SourceLocation DiagLoc = E->getLocStart(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9957 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9958 | if (getLangOpts().CPlusPlus0x) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9959 | // C++11 [expr.const]p5: |
| 9960 | // If an expression of literal class type is used in a context where an |
| 9961 | // integral constant expression is required, then that class type shall |
| 9962 | // have a single non-explicit conversion function to an integral or |
| 9963 | // unscoped enumeration type |
| 9964 | ExprResult Converted; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9965 | if (!Diagnoser.Suppress) { |
| 9966 | class CXX11ConvertDiagnoser : public ICEConvertDiagnoser { |
| 9967 | public: |
| 9968 | CXX11ConvertDiagnoser() : ICEConvertDiagnoser(false, true) { } |
| 9969 | |
| 9970 | virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 9971 | QualType T) { |
| 9972 | return S.Diag(Loc, diag::err_ice_not_integral) << T; |
| 9973 | } |
| 9974 | |
| 9975 | virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, |
| 9976 | SourceLocation Loc, |
| 9977 | QualType T) { |
| 9978 | return S.Diag(Loc, diag::err_ice_incomplete_type) << T; |
| 9979 | } |
| 9980 | |
| 9981 | virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, |
| 9982 | SourceLocation Loc, |
| 9983 | QualType T, |
| 9984 | QualType ConvTy) { |
| 9985 | return S.Diag(Loc, diag::err_ice_explicit_conversion) << T << ConvTy; |
| 9986 | } |
| 9987 | |
| 9988 | virtual DiagnosticBuilder noteExplicitConv(Sema &S, |
| 9989 | CXXConversionDecl *Conv, |
| 9990 | QualType ConvTy) { |
| 9991 | return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) |
| 9992 | << ConvTy->isEnumeralType() << ConvTy; |
| 9993 | } |
| 9994 | |
| 9995 | virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 9996 | QualType T) { |
| 9997 | return S.Diag(Loc, diag::err_ice_ambiguous_conversion) << T; |
| 9998 | } |
| 9999 | |
| 10000 | virtual DiagnosticBuilder noteAmbiguous(Sema &S, |
| 10001 | CXXConversionDecl *Conv, |
| 10002 | QualType ConvTy) { |
| 10003 | return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) |
| 10004 | << ConvTy->isEnumeralType() << ConvTy; |
| 10005 | } |
| 10006 | |
| 10007 | virtual DiagnosticBuilder diagnoseConversion(Sema &S, |
| 10008 | SourceLocation Loc, |
| 10009 | QualType T, |
| 10010 | QualType ConvTy) { |
| 10011 | return DiagnosticBuilder::getEmpty(); |
| 10012 | } |
| 10013 | } ConvertDiagnoser; |
| 10014 | |
| 10015 | Converted = ConvertToIntegralOrEnumerationType(DiagLoc, E, |
| 10016 | ConvertDiagnoser, |
| 10017 | /*AllowScopedEnumerations*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10018 | } else { |
| 10019 | // The caller wants to silently enquire whether this is an ICE. Don't |
| 10020 | // produce any diagnostics if it isn't. |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10021 | class SilentICEConvertDiagnoser : public ICEConvertDiagnoser { |
| 10022 | public: |
| 10023 | SilentICEConvertDiagnoser() : ICEConvertDiagnoser(true, true) { } |
| 10024 | |
| 10025 | virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 10026 | QualType T) { |
| 10027 | return DiagnosticBuilder::getEmpty(); |
| 10028 | } |
| 10029 | |
| 10030 | virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, |
| 10031 | SourceLocation Loc, |
| 10032 | QualType T) { |
| 10033 | return DiagnosticBuilder::getEmpty(); |
| 10034 | } |
| 10035 | |
| 10036 | virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, |
| 10037 | SourceLocation Loc, |
| 10038 | QualType T, |
| 10039 | QualType ConvTy) { |
| 10040 | return DiagnosticBuilder::getEmpty(); |
| 10041 | } |
| 10042 | |
| 10043 | virtual DiagnosticBuilder noteExplicitConv(Sema &S, |
| 10044 | CXXConversionDecl *Conv, |
| 10045 | QualType ConvTy) { |
| 10046 | return DiagnosticBuilder::getEmpty(); |
| 10047 | } |
| 10048 | |
| 10049 | virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 10050 | QualType T) { |
| 10051 | return DiagnosticBuilder::getEmpty(); |
| 10052 | } |
| 10053 | |
| 10054 | virtual DiagnosticBuilder noteAmbiguous(Sema &S, |
| 10055 | CXXConversionDecl *Conv, |
| 10056 | QualType ConvTy) { |
| 10057 | return DiagnosticBuilder::getEmpty(); |
| 10058 | } |
| 10059 | |
| 10060 | virtual DiagnosticBuilder diagnoseConversion(Sema &S, |
| 10061 | SourceLocation Loc, |
| 10062 | QualType T, |
| 10063 | QualType ConvTy) { |
| 10064 | return DiagnosticBuilder::getEmpty(); |
| 10065 | } |
| 10066 | } ConvertDiagnoser; |
| 10067 | |
| 10068 | Converted = ConvertToIntegralOrEnumerationType(DiagLoc, E, |
| 10069 | ConvertDiagnoser, false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10070 | } |
| 10071 | if (Converted.isInvalid()) |
| 10072 | return Converted; |
| 10073 | E = Converted.take(); |
| 10074 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) |
| 10075 | return ExprError(); |
| 10076 | } else if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
| 10077 | // An ICE must be of integral or unscoped enumeration type. |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10078 | if (!Diagnoser.Suppress) |
| 10079 | Diagnoser.diagnoseNotICE(*this, DiagLoc, E->getSourceRange()); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10080 | return ExprError(); |
| 10081 | } |
| 10082 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10083 | // Circumvent ICE checking in C++11 to avoid evaluating the expression twice |
| 10084 | // in the non-ICE case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10085 | if (!getLangOpts().CPlusPlus0x && E->isIntegerConstantExpr(Context)) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10086 | if (Result) |
| 10087 | *Result = E->EvaluateKnownConstInt(Context); |
| 10088 | return Owned(E); |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10089 | } |
| 10090 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10091 | Expr::EvalResult EvalResult; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 10092 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 10093 | EvalResult.Diag = &Notes; |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10094 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10095 | // Try to evaluate the expression, and produce diagnostics explaining why it's |
| 10096 | // not a constant expression as a side-effect. |
| 10097 | bool Folded = E->EvaluateAsRValue(EvalResult, Context) && |
| 10098 | EvalResult.Val.isInt() && !EvalResult.HasSideEffects; |
| 10099 | |
| 10100 | // In C++11, we can rely on diagnostics being produced for any expression |
| 10101 | // which is not a constant expression. If no diagnostics were produced, then |
| 10102 | // this is a constant expression. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10103 | if (Folded && getLangOpts().CPlusPlus0x && Notes.empty()) { |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10104 | if (Result) |
| 10105 | *Result = EvalResult.Val.getInt(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10106 | return Owned(E); |
| 10107 | } |
| 10108 | |
| 10109 | // If our only note is the usual "invalid subexpression" note, just point |
| 10110 | // the caret at its location rather than producing an essentially |
| 10111 | // redundant note. |
| 10112 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 10113 | diag::note_invalid_subexpr_in_const_expr) { |
| 10114 | DiagLoc = Notes[0].first; |
| 10115 | Notes.clear(); |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10116 | } |
| 10117 | |
| 10118 | if (!Folded || !AllowFold) { |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10119 | if (!Diagnoser.Suppress) { |
| 10120 | Diagnoser.diagnoseNotICE(*this, DiagLoc, E->getSourceRange()); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 10121 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 10122 | Diag(Notes[I].first, Notes[I].second); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10123 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10124 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10125 | return ExprError(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10126 | } |
| 10127 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10128 | Diagnoser.diagnoseFold(*this, DiagLoc, E->getSourceRange()); |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 10129 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 10130 | Diag(Notes[I].first, Notes[I].second); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10131 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10132 | if (Result) |
| 10133 | *Result = EvalResult.Val.getInt(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10134 | return Owned(E); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10135 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10136 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10137 | namespace { |
| 10138 | // Handle the case where we conclude a expression which we speculatively |
| 10139 | // considered to be unevaluated is actually evaluated. |
| 10140 | class TransformToPE : public TreeTransform<TransformToPE> { |
| 10141 | typedef TreeTransform<TransformToPE> BaseTransform; |
| 10142 | |
| 10143 | public: |
| 10144 | TransformToPE(Sema &SemaRef) : BaseTransform(SemaRef) { } |
| 10145 | |
| 10146 | // Make sure we redo semantic analysis |
| 10147 | bool AlwaysRebuild() { return true; } |
| 10148 | |
Eli Friedman | 56ff283 | 2012-02-06 23:29:57 +0000 | [diff] [blame] | 10149 | // Make sure we handle LabelStmts correctly. |
| 10150 | // FIXME: This does the right thing, but maybe we need a more general |
| 10151 | // fix to TreeTransform? |
| 10152 | StmtResult TransformLabelStmt(LabelStmt *S) { |
| 10153 | S->getDecl()->setStmt(0); |
| 10154 | return BaseTransform::TransformLabelStmt(S); |
| 10155 | } |
| 10156 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10157 | // We need to special-case DeclRefExprs referring to FieldDecls which |
| 10158 | // are not part of a member pointer formation; normal TreeTransforming |
| 10159 | // doesn't catch this case because of the way we represent them in the AST. |
| 10160 | // FIXME: This is a bit ugly; is it really the best way to handle this |
| 10161 | // case? |
| 10162 | // |
| 10163 | // Error on DeclRefExprs referring to FieldDecls. |
| 10164 | ExprResult TransformDeclRefExpr(DeclRefExpr *E) { |
| 10165 | if (isa<FieldDecl>(E->getDecl()) && |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 10166 | !SemaRef.isUnevaluatedContext()) |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10167 | return SemaRef.Diag(E->getLocation(), |
| 10168 | diag::err_invalid_non_static_member_use) |
| 10169 | << E->getDecl() << E->getSourceRange(); |
| 10170 | |
| 10171 | return BaseTransform::TransformDeclRefExpr(E); |
| 10172 | } |
| 10173 | |
| 10174 | // Exception: filter out member pointer formation |
| 10175 | ExprResult TransformUnaryOperator(UnaryOperator *E) { |
| 10176 | if (E->getOpcode() == UO_AddrOf && E->getType()->isMemberPointerType()) |
| 10177 | return E; |
| 10178 | |
| 10179 | return BaseTransform::TransformUnaryOperator(E); |
| 10180 | } |
| 10181 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10182 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 10183 | // Lambdas never need to be transformed. |
| 10184 | return E; |
| 10185 | } |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10186 | }; |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 10187 | } |
| 10188 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10189 | ExprResult Sema::TranformToPotentiallyEvaluated(Expr *E) { |
Eli Friedman | 72b8b1e | 2012-02-29 04:03:55 +0000 | [diff] [blame] | 10190 | assert(ExprEvalContexts.back().Context == Unevaluated && |
| 10191 | "Should only transform unevaluated expressions"); |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10192 | ExprEvalContexts.back().Context = |
| 10193 | ExprEvalContexts[ExprEvalContexts.size()-2].Context; |
| 10194 | if (ExprEvalContexts.back().Context == Unevaluated) |
| 10195 | return E; |
| 10196 | return TransformToPE(*this).TransformExpr(E); |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 10197 | } |
| 10198 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10199 | void |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 10200 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext, |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 10201 | Decl *LambdaContextDecl, |
| 10202 | bool IsDecltype) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10203 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10204 | ExpressionEvaluationContextRecord(NewContext, |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10205 | ExprCleanupObjects.size(), |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 10206 | ExprNeedsCleanups, |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 10207 | LambdaContextDecl, |
| 10208 | IsDecltype)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10209 | ExprNeedsCleanups = false; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10210 | if (!MaybeODRUseExprs.empty()) |
| 10211 | std::swap(MaybeODRUseExprs, ExprEvalContexts.back().SavedMaybeODRUseExprs); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10212 | } |
| 10213 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 10214 | void |
| 10215 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext, |
| 10216 | ReuseLambdaContextDecl_t, |
| 10217 | bool IsDecltype) { |
| 10218 | Decl *LambdaContextDecl = ExprEvalContexts.back().LambdaContextDecl; |
| 10219 | PushExpressionEvaluationContext(NewContext, LambdaContextDecl, IsDecltype); |
| 10220 | } |
| 10221 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 10222 | void Sema::PopExpressionEvaluationContext() { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10223 | ExpressionEvaluationContextRecord& Rec = ExprEvalContexts.back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10224 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10225 | if (!Rec.Lambdas.empty()) { |
| 10226 | if (Rec.Context == Unevaluated) { |
| 10227 | // C++11 [expr.prim.lambda]p2: |
| 10228 | // A lambda-expression shall not appear in an unevaluated operand |
| 10229 | // (Clause 5). |
| 10230 | for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) |
| 10231 | Diag(Rec.Lambdas[I]->getLocStart(), |
| 10232 | diag::err_lambda_unevaluated_operand); |
| 10233 | } else { |
| 10234 | // Mark the capture expressions odr-used. This was deferred |
| 10235 | // during lambda expression creation. |
| 10236 | for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) { |
| 10237 | LambdaExpr *Lambda = Rec.Lambdas[I]; |
| 10238 | for (LambdaExpr::capture_init_iterator |
| 10239 | C = Lambda->capture_init_begin(), |
| 10240 | CEnd = Lambda->capture_init_end(); |
| 10241 | C != CEnd; ++C) { |
| 10242 | MarkDeclarationsReferencedInExpr(*C); |
| 10243 | } |
| 10244 | } |
| 10245 | } |
| 10246 | } |
| 10247 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10248 | // When are coming out of an unevaluated context, clear out any |
| 10249 | // temporaries that we may have created as part of the evaluation of |
| 10250 | // the expression in that context: they aren't relevant because they |
| 10251 | // will never be constructed. |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 10252 | if (Rec.Context == Unevaluated || Rec.Context == ConstantEvaluated) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10253 | ExprCleanupObjects.erase(ExprCleanupObjects.begin() + Rec.NumCleanupObjects, |
| 10254 | ExprCleanupObjects.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10255 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10256 | CleanupVarDeclMarking(); |
| 10257 | std::swap(MaybeODRUseExprs, Rec.SavedMaybeODRUseExprs); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10258 | // Otherwise, merge the contexts together. |
| 10259 | } else { |
| 10260 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10261 | MaybeODRUseExprs.insert(Rec.SavedMaybeODRUseExprs.begin(), |
| 10262 | Rec.SavedMaybeODRUseExprs.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10263 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10264 | |
| 10265 | // Pop the current expression evaluation context off the stack. |
| 10266 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10267 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10268 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10269 | void Sema::DiscardCleanupsInEvaluationContext() { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10270 | ExprCleanupObjects.erase( |
| 10271 | ExprCleanupObjects.begin() + ExprEvalContexts.back().NumCleanupObjects, |
| 10272 | ExprCleanupObjects.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10273 | ExprNeedsCleanups = false; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10274 | MaybeODRUseExprs.clear(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10275 | } |
| 10276 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 10277 | ExprResult Sema::HandleExprEvaluationContextForTypeof(Expr *E) { |
| 10278 | if (!E->getType()->isVariablyModifiedType()) |
| 10279 | return E; |
| 10280 | return TranformToPotentiallyEvaluated(E); |
| 10281 | } |
| 10282 | |
Benjamin Kramer | 5bbc385 | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 10283 | static bool IsPotentiallyEvaluatedContext(Sema &SemaRef) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10284 | // Do not mark anything as "used" within a dependent context; wait for |
| 10285 | // an instantiation. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10286 | if (SemaRef.CurContext->isDependentContext()) |
| 10287 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10288 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10289 | switch (SemaRef.ExprEvalContexts.back().Context) { |
| 10290 | case Sema::Unevaluated: |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10291 | // We are in an expression that is not potentially evaluated; do nothing. |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 10292 | // (Depending on how you read the standard, we actually do need to do |
| 10293 | // something here for null pointer constants, but the standard's |
| 10294 | // definition of a null pointer constant is completely crazy.) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10295 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10296 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10297 | case Sema::ConstantEvaluated: |
| 10298 | case Sema::PotentiallyEvaluated: |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 10299 | // We are in a potentially evaluated expression (or a constant-expression |
| 10300 | // in C++03); we need to do implicit template instantiation, implicitly |
| 10301 | // define class members, and mark most declarations as used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10302 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10303 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10304 | case Sema::PotentiallyEvaluatedIfUsed: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10305 | // Referenced declarations will only be used if the construct in the |
| 10306 | // containing expression is used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10307 | return false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10308 | } |
Matt Beaumont-Gay | 4f7dcdb | 2012-02-02 18:35:35 +0000 | [diff] [blame] | 10309 | llvm_unreachable("Invalid context"); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10310 | } |
| 10311 | |
| 10312 | /// \brief Mark a function referenced, and check whether it is odr-used |
| 10313 | /// (C++ [basic.def.odr]p2, C99 6.9p3) |
| 10314 | void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func) { |
| 10315 | assert(Func && "No function?"); |
| 10316 | |
| 10317 | Func->setReferenced(); |
| 10318 | |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10319 | // Don't mark this function as used multiple times, unless it's a constexpr |
| 10320 | // function which we need to instantiate. |
| 10321 | if (Func->isUsed(false) && |
| 10322 | !(Func->isConstexpr() && !Func->getBody() && |
| 10323 | Func->isImplicitlyInstantiable())) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10324 | return; |
| 10325 | |
| 10326 | if (!IsPotentiallyEvaluatedContext(*this)) |
| 10327 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10328 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10329 | // Note that this declaration has been used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10330 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10331 | if (Constructor->isDefaulted() && !Constructor->isDeleted()) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10332 | if (Constructor->isDefaultConstructor()) { |
| 10333 | if (Constructor->isTrivial()) |
| 10334 | return; |
| 10335 | if (!Constructor->isUsed(false)) |
| 10336 | DefineImplicitDefaultConstructor(Loc, Constructor); |
| 10337 | } else if (Constructor->isCopyConstructor()) { |
| 10338 | if (!Constructor->isUsed(false)) |
| 10339 | DefineImplicitCopyConstructor(Loc, Constructor); |
| 10340 | } else if (Constructor->isMoveConstructor()) { |
| 10341 | if (!Constructor->isUsed(false)) |
| 10342 | DefineImplicitMoveConstructor(Loc, Constructor); |
| 10343 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10344 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10345 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10346 | MarkVTableUsed(Loc, Constructor->getParent()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10347 | } else if (CXXDestructorDecl *Destructor = |
| 10348 | dyn_cast<CXXDestructorDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10349 | if (Destructor->isDefaulted() && !Destructor->isDeleted() && |
| 10350 | !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10351 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10352 | if (Destructor->isVirtual()) |
| 10353 | MarkVTableUsed(Loc, Destructor->getParent()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10354 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10355 | if (MethodDecl->isDefaulted() && !MethodDecl->isDeleted() && |
| 10356 | MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10357 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10358 | if (!MethodDecl->isUsed(false)) { |
| 10359 | if (MethodDecl->isCopyAssignmentOperator()) |
| 10360 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
| 10361 | else |
| 10362 | DefineImplicitMoveAssignment(Loc, MethodDecl); |
| 10363 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10364 | } else if (isa<CXXConversionDecl>(MethodDecl) && |
| 10365 | MethodDecl->getParent()->isLambda()) { |
| 10366 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(MethodDecl); |
| 10367 | if (Conversion->isLambdaToBlockPointerConversion()) |
| 10368 | DefineImplicitLambdaToBlockPointerConversion(Loc, Conversion); |
| 10369 | else |
| 10370 | DefineImplicitLambdaToFunctionPointerConversion(Loc, Conversion); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10371 | } else if (MethodDecl->isVirtual()) |
| 10372 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10373 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10374 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10375 | // Recursive functions should be marked when used from another function. |
| 10376 | // FIXME: Is this really right? |
| 10377 | if (CurContext == Func) return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10378 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10379 | // Resolve the exception specification for any function which is |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10380 | // used: CodeGen will need it. |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 10381 | const FunctionProtoType *FPT = Func->getType()->getAs<FunctionProtoType>(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10382 | if (FPT && isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) |
| 10383 | ResolveExceptionSpec(Loc, FPT); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10384 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10385 | // Implicit instantiation of function templates and member functions of |
| 10386 | // class templates. |
| 10387 | if (Func->isImplicitlyInstantiable()) { |
| 10388 | bool AlreadyInstantiated = false; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10389 | SourceLocation PointOfInstantiation = Loc; |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10390 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 10391 | = Func->getTemplateSpecializationInfo()) { |
| 10392 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 10393 | SpecInfo->setPointOfInstantiation(Loc); |
| 10394 | else if (SpecInfo->getTemplateSpecializationKind() |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10395 | == TSK_ImplicitInstantiation) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10396 | AlreadyInstantiated = true; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10397 | PointOfInstantiation = SpecInfo->getPointOfInstantiation(); |
| 10398 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10399 | } else if (MemberSpecializationInfo *MSInfo |
| 10400 | = Func->getMemberSpecializationInfo()) { |
| 10401 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10402 | MSInfo->setPointOfInstantiation(Loc); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10403 | else if (MSInfo->getTemplateSpecializationKind() |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10404 | == TSK_ImplicitInstantiation) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10405 | AlreadyInstantiated = true; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10406 | PointOfInstantiation = MSInfo->getPointOfInstantiation(); |
| 10407 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10408 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10409 | |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10410 | if (!AlreadyInstantiated || Func->isConstexpr()) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10411 | if (isa<CXXRecordDecl>(Func->getDeclContext()) && |
| 10412 | cast<CXXRecordDecl>(Func->getDeclContext())->isLocalClass()) |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10413 | PendingLocalImplicitInstantiations.push_back( |
| 10414 | std::make_pair(Func, PointOfInstantiation)); |
| 10415 | else if (Func->isConstexpr()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10416 | // Do not defer instantiations of constexpr functions, to avoid the |
| 10417 | // expression evaluator needing to call back into Sema if it sees a |
| 10418 | // call to such a function. |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10419 | InstantiateFunctionDefinition(PointOfInstantiation, Func); |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 10420 | else { |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10421 | PendingInstantiations.push_back(std::make_pair(Func, |
| 10422 | PointOfInstantiation)); |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 10423 | // Notify the consumer that a function was implicitly instantiated. |
| 10424 | Consumer.HandleCXXImplicitFunctionInstantiation(Func); |
| 10425 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10426 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10427 | } else { |
| 10428 | // Walk redefinitions, as some of them may be instantiable. |
| 10429 | for (FunctionDecl::redecl_iterator i(Func->redecls_begin()), |
| 10430 | e(Func->redecls_end()); i != e; ++i) { |
| 10431 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
| 10432 | MarkFunctionReferenced(Loc, *i); |
| 10433 | } |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 10434 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10435 | |
| 10436 | // Keep track of used but undefined functions. |
| 10437 | if (!Func->isPure() && !Func->hasBody() && |
| 10438 | Func->getLinkage() != ExternalLinkage) { |
| 10439 | SourceLocation &old = UndefinedInternals[Func->getCanonicalDecl()]; |
| 10440 | if (old.isInvalid()) old = Loc; |
| 10441 | } |
| 10442 | |
| 10443 | Func->setUsed(true); |
| 10444 | } |
| 10445 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10446 | static void |
| 10447 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
| 10448 | VarDecl *var, DeclContext *DC) { |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10449 | DeclContext *VarDC = var->getDeclContext(); |
| 10450 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10451 | // If the parameter still belongs to the translation unit, then |
| 10452 | // we're actually just using one parameter in the declaration of |
| 10453 | // the next. |
| 10454 | if (isa<ParmVarDecl>(var) && |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10455 | isa<TranslationUnitDecl>(VarDC)) |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10456 | return; |
| 10457 | |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10458 | // For C code, don't diagnose about capture if we're not actually in code |
| 10459 | // right now; it's impossible to write a non-constant expression outside of |
| 10460 | // function context, so we'll get other (more useful) diagnostics later. |
| 10461 | // |
| 10462 | // For C++, things get a bit more nasty... it would be nice to suppress this |
| 10463 | // diagnostic for certain cases like using a local variable in an array bound |
| 10464 | // 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] | 10465 | if (!S.getLangOpts().CPlusPlus && !S.CurContext->isFunctionOrMethod()) |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10466 | return; |
| 10467 | |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10468 | if (isa<CXXMethodDecl>(VarDC) && |
| 10469 | cast<CXXRecordDecl>(VarDC->getParent())->isLambda()) { |
| 10470 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_lambda) |
| 10471 | << var->getIdentifier(); |
| 10472 | } else if (FunctionDecl *fn = dyn_cast<FunctionDecl>(VarDC)) { |
| 10473 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 10474 | << var->getIdentifier() << fn->getDeclName(); |
| 10475 | } else if (isa<BlockDecl>(VarDC)) { |
| 10476 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_block) |
| 10477 | << var->getIdentifier(); |
| 10478 | } else { |
| 10479 | // FIXME: Is there any other context where a local variable can be |
| 10480 | // declared? |
| 10481 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_context) |
| 10482 | << var->getIdentifier(); |
| 10483 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10484 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10485 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 10486 | << var->getIdentifier(); |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10487 | |
| 10488 | // FIXME: Add additional diagnostic info about class etc. which prevents |
| 10489 | // capture. |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10490 | } |
| 10491 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10492 | /// \brief Capture the given variable in the given lambda expression. |
| 10493 | static ExprResult captureInLambda(Sema &S, LambdaScopeInfo *LSI, |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10494 | VarDecl *Var, QualType FieldType, |
| 10495 | QualType DeclRefType, |
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10496 | SourceLocation Loc, |
| 10497 | bool RefersToEnclosingLocal) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10498 | CXXRecordDecl *Lambda = LSI->Lambda; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10499 | |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10500 | // Build the non-static data member. |
| 10501 | FieldDecl *Field |
| 10502 | = FieldDecl::Create(S.Context, Lambda, Loc, Loc, 0, FieldType, |
| 10503 | S.Context.getTrivialTypeSourceInfo(FieldType, Loc), |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 10504 | 0, false, ICIS_NoInit); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10505 | Field->setImplicit(true); |
| 10506 | Field->setAccess(AS_private); |
Douglas Gregor | 20f87a4 | 2012-02-09 02:12:34 +0000 | [diff] [blame] | 10507 | Lambda->addDecl(Field); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10508 | |
| 10509 | // C++11 [expr.prim.lambda]p21: |
| 10510 | // When the lambda-expression is evaluated, the entities that |
| 10511 | // are captured by copy are used to direct-initialize each |
| 10512 | // corresponding non-static data member of the resulting closure |
| 10513 | // object. (For array members, the array elements are |
| 10514 | // direct-initialized in increasing subscript order.) These |
| 10515 | // initializations are performed in the (unspecified) order in |
| 10516 | // which the non-static data members are declared. |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10517 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10518 | // Introduce a new evaluation context for the initialization, so |
| 10519 | // that temporaries introduced as part of the capture are retained |
| 10520 | // to be re-"exported" from the lambda expression itself. |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10521 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 10522 | |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 10523 | // C++ [expr.prim.labda]p12: |
| 10524 | // An entity captured by a lambda-expression is odr-used (3.2) in |
| 10525 | // the scope containing the lambda-expression. |
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10526 | Expr *Ref = new (S.Context) DeclRefExpr(Var, RefersToEnclosingLocal, |
| 10527 | DeclRefType, VK_LValue, Loc); |
Eli Friedman | 88530d5 | 2012-03-01 21:32:56 +0000 | [diff] [blame] | 10528 | Var->setReferenced(true); |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 10529 | Var->setUsed(true); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10530 | |
| 10531 | // When the field has array type, create index variables for each |
| 10532 | // dimension of the array. We use these index variables to subscript |
| 10533 | // the source array, and other clients (e.g., CodeGen) will perform |
| 10534 | // the necessary iteration with these index variables. |
| 10535 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10536 | QualType BaseType = FieldType; |
| 10537 | QualType SizeType = S.Context.getSizeType(); |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 10538 | LSI->ArrayIndexStarts.push_back(LSI->ArrayIndexVars.size()); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10539 | while (const ConstantArrayType *Array |
| 10540 | = S.Context.getAsConstantArrayType(BaseType)) { |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10541 | // Create the iteration variable for this array index. |
| 10542 | IdentifierInfo *IterationVarName = 0; |
| 10543 | { |
| 10544 | SmallString<8> Str; |
| 10545 | llvm::raw_svector_ostream OS(Str); |
| 10546 | OS << "__i" << IndexVariables.size(); |
| 10547 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 10548 | } |
| 10549 | VarDecl *IterationVar |
| 10550 | = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
| 10551 | IterationVarName, SizeType, |
| 10552 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
| 10553 | SC_None, SC_None); |
| 10554 | IndexVariables.push_back(IterationVar); |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 10555 | LSI->ArrayIndexVars.push_back(IterationVar); |
| 10556 | |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10557 | // Create a reference to the iteration variable. |
| 10558 | ExprResult IterationVarRef |
| 10559 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
| 10560 | assert(!IterationVarRef.isInvalid() && |
| 10561 | "Reference to invented variable cannot fail!"); |
| 10562 | IterationVarRef = S.DefaultLvalueConversion(IterationVarRef.take()); |
| 10563 | assert(!IterationVarRef.isInvalid() && |
| 10564 | "Conversion of invented variable cannot fail!"); |
| 10565 | |
| 10566 | // Subscript the array with this iteration variable. |
| 10567 | ExprResult Subscript = S.CreateBuiltinArraySubscriptExpr( |
| 10568 | Ref, Loc, IterationVarRef.take(), Loc); |
| 10569 | if (Subscript.isInvalid()) { |
| 10570 | S.CleanupVarDeclMarking(); |
| 10571 | S.DiscardCleanupsInEvaluationContext(); |
| 10572 | S.PopExpressionEvaluationContext(); |
| 10573 | return ExprError(); |
| 10574 | } |
| 10575 | |
| 10576 | Ref = Subscript.take(); |
| 10577 | BaseType = Array->getElementType(); |
| 10578 | } |
| 10579 | |
| 10580 | // Construct the entity that we will be initializing. For an array, this |
| 10581 | // will be first element in the array, which may require several levels |
| 10582 | // of array-subscript entities. |
| 10583 | SmallVector<InitializedEntity, 4> Entities; |
| 10584 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 10585 | Entities.push_back( |
| 10586 | InitializedEntity::InitializeLambdaCapture(Var, Field, Loc)); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10587 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 10588 | Entities.push_back(InitializedEntity::InitializeElement(S.Context, |
| 10589 | 0, |
| 10590 | Entities.back())); |
| 10591 | |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10592 | InitializationKind InitKind |
| 10593 | = InitializationKind::CreateDirect(Loc, Loc, Loc); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10594 | InitializationSequence Init(S, Entities.back(), InitKind, &Ref, 1); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10595 | ExprResult Result(true); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10596 | if (!Init.Diagnose(S, Entities.back(), InitKind, &Ref, 1)) |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10597 | Result = Init.Perform(S, Entities.back(), InitKind, Ref); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10598 | |
| 10599 | // If this initialization requires any cleanups (e.g., due to a |
| 10600 | // default argument to a copy constructor), note that for the |
| 10601 | // lambda. |
| 10602 | if (S.ExprNeedsCleanups) |
| 10603 | LSI->ExprNeedsCleanups = true; |
| 10604 | |
| 10605 | // Exit the expression evaluation context used for the capture. |
| 10606 | S.CleanupVarDeclMarking(); |
| 10607 | S.DiscardCleanupsInEvaluationContext(); |
| 10608 | S.PopExpressionEvaluationContext(); |
| 10609 | return Result; |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10610 | } |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10611 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10612 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, |
| 10613 | TryCaptureKind Kind, SourceLocation EllipsisLoc, |
| 10614 | bool BuildAndDiagnose, |
| 10615 | QualType &CaptureType, |
| 10616 | QualType &DeclRefType) { |
| 10617 | bool Nested = false; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10618 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10619 | DeclContext *DC = CurContext; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10620 | if (Var->getDeclContext() == DC) return true; |
| 10621 | if (!Var->hasLocalStorage()) return true; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10622 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10623 | bool HasBlocksAttr = Var->hasAttr<BlocksAttr>(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10624 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10625 | // Walk up the stack to determine whether we can capture the variable, |
| 10626 | // performing the "simple" checks that don't depend on type. We stop when |
| 10627 | // we've either hit the declared scope of the variable or find an existing |
| 10628 | // capture of that variable. |
| 10629 | CaptureType = Var->getType(); |
| 10630 | DeclRefType = CaptureType.getNonReferenceType(); |
| 10631 | bool Explicit = (Kind != TryCapture_Implicit); |
| 10632 | unsigned FunctionScopesIndex = FunctionScopes.size() - 1; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10633 | do { |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10634 | // Only block literals and lambda expressions can capture; other |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10635 | // scopes don't work. |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10636 | DeclContext *ParentDC; |
| 10637 | if (isa<BlockDecl>(DC)) |
| 10638 | ParentDC = DC->getParent(); |
| 10639 | else if (isa<CXXMethodDecl>(DC) && |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10640 | cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call && |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10641 | cast<CXXRecordDecl>(DC->getParent())->isLambda()) |
| 10642 | ParentDC = DC->getParent()->getParent(); |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10643 | else { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10644 | if (BuildAndDiagnose) |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10645 | diagnoseUncapturableValueReference(*this, Loc, Var, DC); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10646 | return true; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10647 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10648 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10649 | CapturingScopeInfo *CSI = |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10650 | cast<CapturingScopeInfo>(FunctionScopes[FunctionScopesIndex]); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10651 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10652 | // Check whether we've already captured it. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10653 | if (CSI->CaptureMap.count(Var)) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10654 | // If we found a capture, any subcaptures are nested. |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10655 | Nested = true; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10656 | |
| 10657 | // Retrieve the capture type for this variable. |
| 10658 | CaptureType = CSI->getCapture(Var).getCaptureType(); |
| 10659 | |
| 10660 | // Compute the type of an expression that refers to this variable. |
| 10661 | DeclRefType = CaptureType.getNonReferenceType(); |
| 10662 | |
| 10663 | const CapturingScopeInfo::Capture &Cap = CSI->getCapture(Var); |
| 10664 | if (Cap.isCopyCapture() && |
| 10665 | !(isa<LambdaScopeInfo>(CSI) && cast<LambdaScopeInfo>(CSI)->Mutable)) |
| 10666 | DeclRefType.addConst(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10667 | break; |
| 10668 | } |
| 10669 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10670 | bool IsBlock = isa<BlockScopeInfo>(CSI); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10671 | bool IsLambda = !IsBlock; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10672 | |
| 10673 | // Lambdas are not allowed to capture unnamed variables |
| 10674 | // (e.g. anonymous unions). |
| 10675 | // FIXME: The C++11 rule don't actually state this explicitly, but I'm |
| 10676 | // assuming that's the intent. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10677 | if (IsLambda && !Var->getDeclName()) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10678 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10679 | Diag(Loc, diag::err_lambda_capture_anonymous_var); |
| 10680 | Diag(Var->getLocation(), diag::note_declared_at); |
| 10681 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10682 | return true; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10683 | } |
| 10684 | |
| 10685 | // Prohibit variably-modified types; they're difficult to deal with. |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10686 | if (Var->getType()->isVariablyModifiedType()) { |
| 10687 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10688 | if (IsBlock) |
| 10689 | Diag(Loc, diag::err_ref_vm_type); |
| 10690 | else |
| 10691 | Diag(Loc, diag::err_lambda_capture_vm_type) << Var->getDeclName(); |
| 10692 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10693 | << Var->getDeclName(); |
| 10694 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10695 | return true; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10696 | } |
| 10697 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10698 | // Lambdas are not allowed to capture __block variables; they don't |
| 10699 | // support the expected semantics. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10700 | if (IsLambda && HasBlocksAttr) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10701 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10702 | Diag(Loc, diag::err_lambda_capture_block) |
| 10703 | << Var->getDeclName(); |
| 10704 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10705 | << Var->getDeclName(); |
| 10706 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10707 | return true; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10708 | } |
| 10709 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10710 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None && !Explicit) { |
| 10711 | // No capture-default |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10712 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10713 | Diag(Loc, diag::err_lambda_impcap) << Var->getDeclName(); |
| 10714 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10715 | << Var->getDeclName(); |
| 10716 | Diag(cast<LambdaScopeInfo>(CSI)->Lambda->getLocStart(), |
| 10717 | diag::note_lambda_decl); |
| 10718 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10719 | return true; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10720 | } |
| 10721 | |
| 10722 | FunctionScopesIndex--; |
| 10723 | DC = ParentDC; |
| 10724 | Explicit = false; |
| 10725 | } while (!Var->getDeclContext()->Equals(DC)); |
| 10726 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10727 | // Walk back down the scope stack, computing the type of the capture at |
| 10728 | // each step, checking type-specific requirements, and adding captures if |
| 10729 | // requested. |
| 10730 | for (unsigned I = ++FunctionScopesIndex, N = FunctionScopes.size(); I != N; |
| 10731 | ++I) { |
| 10732 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[I]); |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 10733 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10734 | // Compute the type of the capture and of a reference to the capture within |
| 10735 | // this scope. |
| 10736 | if (isa<BlockScopeInfo>(CSI)) { |
| 10737 | Expr *CopyExpr = 0; |
| 10738 | bool ByRef = false; |
| 10739 | |
| 10740 | // Blocks are not allowed to capture arrays. |
| 10741 | if (CaptureType->isArrayType()) { |
| 10742 | if (BuildAndDiagnose) { |
| 10743 | Diag(Loc, diag::err_ref_array_type); |
| 10744 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10745 | << Var->getDeclName(); |
| 10746 | } |
| 10747 | return true; |
| 10748 | } |
| 10749 | |
John McCall | 100c649 | 2012-03-30 05:23:48 +0000 | [diff] [blame] | 10750 | // Forbid the block-capture of autoreleasing variables. |
| 10751 | if (CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { |
| 10752 | if (BuildAndDiagnose) { |
| 10753 | Diag(Loc, diag::err_arc_autoreleasing_capture) |
| 10754 | << /*block*/ 0; |
| 10755 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10756 | << Var->getDeclName(); |
| 10757 | } |
| 10758 | return true; |
| 10759 | } |
| 10760 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10761 | if (HasBlocksAttr || CaptureType->isReferenceType()) { |
| 10762 | // Block capture by reference does not change the capture or |
| 10763 | // declaration reference types. |
| 10764 | ByRef = true; |
| 10765 | } else { |
| 10766 | // Block capture by copy introduces 'const'. |
| 10767 | CaptureType = CaptureType.getNonReferenceType().withConst(); |
| 10768 | DeclRefType = CaptureType; |
| 10769 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10770 | if (getLangOpts().CPlusPlus && BuildAndDiagnose) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10771 | if (const RecordType *Record = DeclRefType->getAs<RecordType>()) { |
| 10772 | // The capture logic needs the destructor, so make sure we mark it. |
| 10773 | // Usually this is unnecessary because most local variables have |
| 10774 | // their destructors marked at declaration time, but parameters are |
| 10775 | // an exception because it's technically only the call site that |
| 10776 | // actually requires the destructor. |
| 10777 | if (isa<ParmVarDecl>(Var)) |
| 10778 | FinalizeVarWithDestructor(Var, Record); |
| 10779 | |
| 10780 | // According to the blocks spec, the capture of a variable from |
| 10781 | // the stack requires a const copy constructor. This is not true |
| 10782 | // of the copy/move done to move a __block variable to the heap. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 10783 | Expr *DeclRef = new (Context) DeclRefExpr(Var, false, |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10784 | DeclRefType.withConst(), |
| 10785 | VK_LValue, Loc); |
| 10786 | ExprResult Result |
| 10787 | = PerformCopyInitialization( |
| 10788 | InitializedEntity::InitializeBlock(Var->getLocation(), |
| 10789 | CaptureType, false), |
| 10790 | Loc, Owned(DeclRef)); |
| 10791 | |
| 10792 | // Build a full-expression copy expression if initialization |
| 10793 | // succeeded and used a non-trivial constructor. Recover from |
| 10794 | // errors by pretending that the copy isn't necessary. |
| 10795 | if (!Result.isInvalid() && |
| 10796 | !cast<CXXConstructExpr>(Result.get())->getConstructor() |
| 10797 | ->isTrivial()) { |
| 10798 | Result = MaybeCreateExprWithCleanups(Result); |
| 10799 | CopyExpr = Result.take(); |
| 10800 | } |
| 10801 | } |
| 10802 | } |
| 10803 | } |
| 10804 | |
| 10805 | // Actually capture the variable. |
| 10806 | if (BuildAndDiagnose) |
| 10807 | CSI->addCapture(Var, HasBlocksAttr, ByRef, Nested, Loc, |
| 10808 | SourceLocation(), CaptureType, CopyExpr); |
| 10809 | Nested = true; |
| 10810 | continue; |
| 10811 | } |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 10812 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10813 | LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CSI); |
| 10814 | |
| 10815 | // Determine whether we are capturing by reference or by value. |
| 10816 | bool ByRef = false; |
| 10817 | if (I == N - 1 && Kind != TryCapture_Implicit) { |
| 10818 | ByRef = (Kind == TryCapture_ExplicitByRef); |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10819 | } else { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10820 | ByRef = (LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByref); |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10821 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10822 | |
| 10823 | // Compute the type of the field that will capture this variable. |
| 10824 | if (ByRef) { |
| 10825 | // C++11 [expr.prim.lambda]p15: |
| 10826 | // An entity is captured by reference if it is implicitly or |
| 10827 | // explicitly captured but not captured by copy. It is |
| 10828 | // unspecified whether additional unnamed non-static data |
| 10829 | // members are declared in the closure type for entities |
| 10830 | // captured by reference. |
| 10831 | // |
| 10832 | // FIXME: It is not clear whether we want to build an lvalue reference |
| 10833 | // to the DeclRefType or to CaptureType.getNonReferenceType(). GCC appears |
| 10834 | // to do the former, while EDG does the latter. Core issue 1249 will |
| 10835 | // clarify, but for now we follow GCC because it's a more permissive and |
| 10836 | // easily defensible position. |
| 10837 | CaptureType = Context.getLValueReferenceType(DeclRefType); |
| 10838 | } else { |
| 10839 | // C++11 [expr.prim.lambda]p14: |
| 10840 | // For each entity captured by copy, an unnamed non-static |
| 10841 | // data member is declared in the closure type. The |
| 10842 | // declaration order of these members is unspecified. The type |
| 10843 | // of such a data member is the type of the corresponding |
| 10844 | // captured entity if the entity is not a reference to an |
| 10845 | // object, or the referenced type otherwise. [Note: If the |
| 10846 | // captured entity is a reference to a function, the |
| 10847 | // corresponding data member is also a reference to a |
| 10848 | // function. - end note ] |
| 10849 | if (const ReferenceType *RefType = CaptureType->getAs<ReferenceType>()){ |
| 10850 | if (!RefType->getPointeeType()->isFunctionType()) |
| 10851 | CaptureType = RefType->getPointeeType(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10852 | } |
John McCall | 100c649 | 2012-03-30 05:23:48 +0000 | [diff] [blame] | 10853 | |
| 10854 | // Forbid the lambda copy-capture of autoreleasing variables. |
| 10855 | if (CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { |
| 10856 | if (BuildAndDiagnose) { |
| 10857 | Diag(Loc, diag::err_arc_autoreleasing_capture) << /*lambda*/ 1; |
| 10858 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10859 | << Var->getDeclName(); |
| 10860 | } |
| 10861 | return true; |
| 10862 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10863 | } |
| 10864 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10865 | // Capture this variable in the lambda. |
| 10866 | Expr *CopyExpr = 0; |
| 10867 | if (BuildAndDiagnose) { |
| 10868 | ExprResult Result = captureInLambda(*this, LSI, Var, CaptureType, |
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10869 | DeclRefType, Loc, |
| 10870 | I == N-1); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10871 | if (!Result.isInvalid()) |
| 10872 | CopyExpr = Result.take(); |
| 10873 | } |
| 10874 | |
| 10875 | // Compute the type of a reference to this captured variable. |
| 10876 | if (ByRef) |
| 10877 | DeclRefType = CaptureType.getNonReferenceType(); |
| 10878 | else { |
| 10879 | // C++ [expr.prim.lambda]p5: |
| 10880 | // The closure type for a lambda-expression has a public inline |
| 10881 | // function call operator [...]. This function call operator is |
| 10882 | // declared const (9.3.1) if and only if the lambda-expression’s |
| 10883 | // parameter-declaration-clause is not followed by mutable. |
| 10884 | DeclRefType = CaptureType.getNonReferenceType(); |
| 10885 | if (!LSI->Mutable && !CaptureType->isReferenceType()) |
| 10886 | DeclRefType.addConst(); |
| 10887 | } |
| 10888 | |
| 10889 | // Add the capture. |
| 10890 | if (BuildAndDiagnose) |
| 10891 | CSI->addCapture(Var, /*IsBlock=*/false, ByRef, Nested, Loc, |
| 10892 | EllipsisLoc, CaptureType, CopyExpr); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10893 | Nested = true; |
| 10894 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10895 | |
| 10896 | return false; |
| 10897 | } |
| 10898 | |
| 10899 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, |
| 10900 | TryCaptureKind Kind, SourceLocation EllipsisLoc) { |
| 10901 | QualType CaptureType; |
| 10902 | QualType DeclRefType; |
| 10903 | return tryCaptureVariable(Var, Loc, Kind, EllipsisLoc, |
| 10904 | /*BuildAndDiagnose=*/true, CaptureType, |
| 10905 | DeclRefType); |
| 10906 | } |
| 10907 | |
| 10908 | QualType Sema::getCapturedDeclRefType(VarDecl *Var, SourceLocation Loc) { |
| 10909 | QualType CaptureType; |
| 10910 | QualType DeclRefType; |
| 10911 | |
| 10912 | // Determine whether we can capture this variable. |
| 10913 | if (tryCaptureVariable(Var, Loc, TryCapture_Implicit, SourceLocation(), |
| 10914 | /*BuildAndDiagnose=*/false, CaptureType, DeclRefType)) |
| 10915 | return QualType(); |
| 10916 | |
| 10917 | return DeclRefType; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10918 | } |
| 10919 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10920 | static void MarkVarDeclODRUsed(Sema &SemaRef, VarDecl *Var, |
| 10921 | SourceLocation Loc) { |
| 10922 | // Keep track of used but undefined variables. |
Eli Friedman | 0cc5d40 | 2012-02-04 00:54:05 +0000 | [diff] [blame] | 10923 | // FIXME: We shouldn't suppress this warning for static data members. |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 10924 | if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly && |
Eli Friedman | 0cc5d40 | 2012-02-04 00:54:05 +0000 | [diff] [blame] | 10925 | Var->getLinkage() != ExternalLinkage && |
| 10926 | !(Var->isStaticDataMember() && Var->hasInit())) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10927 | SourceLocation &old = SemaRef.UndefinedInternals[Var->getCanonicalDecl()]; |
| 10928 | if (old.isInvalid()) old = Loc; |
| 10929 | } |
| 10930 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10931 | SemaRef.tryCaptureVariable(Var, Loc); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10932 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10933 | Var->setUsed(true); |
| 10934 | } |
| 10935 | |
| 10936 | void Sema::UpdateMarkingForLValueToRValue(Expr *E) { |
| 10937 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it is |
| 10938 | // an object that satisfies the requirements for appearing in a |
| 10939 | // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) |
| 10940 | // is immediately applied." This function handles the lvalue-to-rvalue |
| 10941 | // conversion part. |
| 10942 | MaybeODRUseExprs.erase(E->IgnoreParens()); |
| 10943 | } |
| 10944 | |
Eli Friedman | ac62601 | 2012-02-29 03:16:56 +0000 | [diff] [blame] | 10945 | ExprResult Sema::ActOnConstantExpression(ExprResult Res) { |
| 10946 | if (!Res.isUsable()) |
| 10947 | return Res; |
| 10948 | |
| 10949 | // If a constant-expression is a reference to a variable where we delay |
| 10950 | // deciding whether it is an odr-use, just assume we will apply the |
| 10951 | // lvalue-to-rvalue conversion. In the one case where this doesn't happen |
| 10952 | // (a non-type template argument), we have special handling anyway. |
| 10953 | UpdateMarkingForLValueToRValue(Res.get()); |
| 10954 | return Res; |
| 10955 | } |
| 10956 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10957 | void Sema::CleanupVarDeclMarking() { |
| 10958 | for (llvm::SmallPtrSetIterator<Expr*> i = MaybeODRUseExprs.begin(), |
| 10959 | e = MaybeODRUseExprs.end(); |
| 10960 | i != e; ++i) { |
| 10961 | VarDecl *Var; |
| 10962 | SourceLocation Loc; |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 10963 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*i)) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10964 | Var = cast<VarDecl>(DRE->getDecl()); |
| 10965 | Loc = DRE->getLocation(); |
| 10966 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(*i)) { |
| 10967 | Var = cast<VarDecl>(ME->getMemberDecl()); |
| 10968 | Loc = ME->getMemberLoc(); |
| 10969 | } else { |
| 10970 | llvm_unreachable("Unexpcted expression"); |
| 10971 | } |
| 10972 | |
| 10973 | MarkVarDeclODRUsed(*this, Var, Loc); |
| 10974 | } |
| 10975 | |
| 10976 | MaybeODRUseExprs.clear(); |
| 10977 | } |
| 10978 | |
| 10979 | // Mark a VarDecl referenced, and perform the necessary handling to compute |
| 10980 | // odr-uses. |
| 10981 | static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, |
| 10982 | VarDecl *Var, Expr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10983 | Var->setReferenced(); |
| 10984 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10985 | if (!IsPotentiallyEvaluatedContext(SemaRef)) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10986 | return; |
| 10987 | |
| 10988 | // Implicit instantiation of static data members of class templates. |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10989 | if (Var->isStaticDataMember() && Var->getInstantiatedFromStaticDataMember()) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10990 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 10991 | assert(MSInfo && "Missing member specialization information?"); |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10992 | bool AlreadyInstantiated = !MSInfo->getPointOfInstantiation().isInvalid(); |
| 10993 | if (MSInfo->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 10994 | (!AlreadyInstantiated || |
| 10995 | Var->isUsableInConstantExpressions(SemaRef.Context))) { |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10996 | if (!AlreadyInstantiated) { |
| 10997 | // This is a modification of an existing AST node. Notify listeners. |
| 10998 | if (ASTMutationListener *L = SemaRef.getASTMutationListener()) |
| 10999 | L->StaticDataMemberInstantiated(Var); |
| 11000 | MSInfo->setPointOfInstantiation(Loc); |
| 11001 | } |
| 11002 | SourceLocation PointOfInstantiation = MSInfo->getPointOfInstantiation(); |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 11003 | if (Var->isUsableInConstantExpressions(SemaRef.Context)) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11004 | // Do not defer instantiations of variables which could be used in a |
| 11005 | // constant expression. |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 11006 | SemaRef.InstantiateStaticDataMemberDefinition(PointOfInstantiation,Var); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11007 | else |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 11008 | SemaRef.PendingInstantiations.push_back( |
| 11009 | std::make_pair(Var, PointOfInstantiation)); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11010 | } |
| 11011 | } |
| 11012 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11013 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it is |
| 11014 | // an object that satisfies the requirements for appearing in a |
| 11015 | // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) |
| 11016 | // is immediately applied." We check the first part here, and |
| 11017 | // Sema::UpdateMarkingForLValueToRValue deals with the second part. |
| 11018 | // Note that we use the C++11 definition everywhere because nothing in |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 11019 | // C++03 depends on whether we get the C++03 version correct. This does not |
| 11020 | // apply to references, since they are not objects. |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11021 | const VarDecl *DefVD; |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 11022 | if (E && !isa<ParmVarDecl>(Var) && !Var->getType()->isReferenceType() && |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 11023 | Var->isUsableInConstantExpressions(SemaRef.Context) && |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11024 | Var->getAnyInitializer(DefVD) && DefVD->checkInitIsICE()) |
| 11025 | SemaRef.MaybeODRUseExprs.insert(E); |
| 11026 | else |
| 11027 | MarkVarDeclODRUsed(SemaRef, Var, Loc); |
| 11028 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11029 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11030 | /// \brief Mark a variable referenced, and check whether it is odr-used |
| 11031 | /// (C++ [basic.def.odr]p2, C99 6.9p3). Note that this should not be |
| 11032 | /// used directly for normal expressions referring to VarDecl. |
| 11033 | void Sema::MarkVariableReferenced(SourceLocation Loc, VarDecl *Var) { |
| 11034 | DoMarkVarDeclReferenced(*this, Loc, Var, 0); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11035 | } |
| 11036 | |
| 11037 | static void MarkExprReferenced(Sema &SemaRef, SourceLocation Loc, |
| 11038 | Decl *D, Expr *E) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11039 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 11040 | DoMarkVarDeclReferenced(SemaRef, Loc, Var, E); |
| 11041 | return; |
| 11042 | } |
| 11043 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11044 | SemaRef.MarkAnyDeclReferenced(Loc, D); |
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 11045 | |
| 11046 | // If this is a call to a method via a cast, also mark the method in the |
| 11047 | // derived class used in case codegen can devirtualize the call. |
| 11048 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); |
| 11049 | if (!ME) |
| 11050 | return; |
| 11051 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 11052 | if (!MD) |
| 11053 | return; |
| 11054 | const Expr *Base = ME->getBase(); |
Rafael Espindola | 8d852e3 | 2012-06-27 18:18:05 +0000 | [diff] [blame] | 11055 | const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType(); |
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 11056 | if (!MostDerivedClassDecl) |
| 11057 | return; |
| 11058 | CXXMethodDecl *DM = MD->getCorrespondingMethodInClass(MostDerivedClassDecl); |
Rafael Espindola | 0713d99 | 2012-06-27 17:44:39 +0000 | [diff] [blame] | 11059 | if (!DM) |
| 11060 | return; |
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 11061 | SemaRef.MarkAnyDeclReferenced(Loc, DM); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11062 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11063 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11064 | /// \brief Perform reference-marking and odr-use handling for a DeclRefExpr. |
| 11065 | void Sema::MarkDeclRefReferenced(DeclRefExpr *E) { |
| 11066 | MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E); |
| 11067 | } |
| 11068 | |
| 11069 | /// \brief Perform reference-marking and odr-use handling for a MemberExpr. |
| 11070 | void Sema::MarkMemberReferenced(MemberExpr *E) { |
| 11071 | MarkExprReferenced(*this, E->getMemberLoc(), E->getMemberDecl(), E); |
| 11072 | } |
| 11073 | |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 11074 | /// \brief Perform marking for a reference to an arbitrary declaration. It |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11075 | /// marks the declaration referenced, and performs odr-use checking for functions |
| 11076 | /// and variables. This method should not be used when building an normal |
| 11077 | /// expression which refers to a variable. |
| 11078 | void Sema::MarkAnyDeclReferenced(SourceLocation Loc, Decl *D) { |
| 11079 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 11080 | MarkVariableReferenced(Loc, VD); |
| 11081 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 11082 | MarkFunctionReferenced(Loc, FD); |
| 11083 | else |
| 11084 | D->setReferenced(); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 11085 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11086 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11087 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11088 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11089 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11090 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11091 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 11092 | Sema &S; |
| 11093 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11094 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11095 | public: |
| 11096 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11097 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11098 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11099 | |
| 11100 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 11101 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11102 | }; |
| 11103 | } |
| 11104 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11105 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 11106 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11107 | if (Arg.getKind() == TemplateArgument::Declaration) { |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 11108 | if (Decl *D = Arg.getAsDecl()) |
| 11109 | S.MarkAnyDeclReferenced(Loc, D); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11110 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11111 | |
| 11112 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11113 | } |
| 11114 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11115 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11116 | if (ClassTemplateSpecializationDecl *Spec |
| 11117 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 11118 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 11119 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11120 | } |
| 11121 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 11122 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11123 | } |
| 11124 | |
| 11125 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 11126 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11127 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11128 | } |
| 11129 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11130 | namespace { |
| 11131 | /// \brief Helper class that marks all of the declarations referenced by |
| 11132 | /// potentially-evaluated subexpressions as "referenced". |
| 11133 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 11134 | Sema &S; |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11135 | bool SkipLocalVariables; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11136 | |
| 11137 | public: |
| 11138 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 11139 | |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11140 | EvaluatedExprMarker(Sema &S, bool SkipLocalVariables) |
| 11141 | : Inherited(S.Context), S(S), SkipLocalVariables(SkipLocalVariables) { } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11142 | |
| 11143 | void VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11144 | // If we were asked not to visit local variables, don't. |
| 11145 | if (SkipLocalVariables) { |
| 11146 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
| 11147 | if (VD->hasLocalStorage()) |
| 11148 | return; |
| 11149 | } |
| 11150 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11151 | S.MarkDeclRefReferenced(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11152 | } |
| 11153 | |
| 11154 | void VisitMemberExpr(MemberExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11155 | S.MarkMemberReferenced(E); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11156 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11157 | } |
| 11158 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 11159 | void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11160 | S.MarkFunctionReferenced(E->getLocStart(), |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 11161 | const_cast<CXXDestructorDecl*>(E->getTemporary()->getDestructor())); |
| 11162 | Visit(E->getSubExpr()); |
| 11163 | } |
| 11164 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11165 | void VisitCXXNewExpr(CXXNewExpr *E) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11166 | if (E->getOperatorNew()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11167 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorNew()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11168 | if (E->getOperatorDelete()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11169 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11170 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11171 | } |
Sebastian Redl | 2aed8b8 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 11172 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11173 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 11174 | if (E->getOperatorDelete()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11175 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 11176 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 11177 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 11178 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11179 | S.MarkFunctionReferenced(E->getLocStart(), |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 11180 | S.LookupDestructor(Record)); |
| 11181 | } |
| 11182 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11183 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11184 | } |
| 11185 | |
| 11186 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11187 | S.MarkFunctionReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11188 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11189 | } |
| 11190 | |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 11191 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 11192 | Visit(E->getExpr()); |
| 11193 | } |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11194 | |
| 11195 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 11196 | Inherited::VisitImplicitCastExpr(E); |
| 11197 | |
| 11198 | if (E->getCastKind() == CK_LValueToRValue) |
| 11199 | S.UpdateMarkingForLValueToRValue(E->getSubExpr()); |
| 11200 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11201 | }; |
| 11202 | } |
| 11203 | |
| 11204 | /// \brief Mark any declarations that appear within this expression or any |
| 11205 | /// potentially-evaluated subexpressions as "referenced". |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11206 | /// |
| 11207 | /// \param SkipLocalVariables If true, don't mark local variables as |
| 11208 | /// 'referenced'. |
| 11209 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E, |
| 11210 | bool SkipLocalVariables) { |
| 11211 | EvaluatedExprMarker(*this, SkipLocalVariables).Visit(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11212 | } |
| 11213 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11214 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 11215 | /// of the program being compiled. |
| 11216 | /// |
| 11217 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11218 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11219 | /// possibility that the code will actually be executable. Code in sizeof() |
| 11220 | /// expressions, code used only during overload resolution, etc., are not |
| 11221 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 11222 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11223 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11224 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11225 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11226 | /// This routine should be used for all diagnostics that describe the run-time |
| 11227 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 11228 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 11229 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11230 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *Statement, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11231 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11232 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11233 | case Unevaluated: |
| 11234 | // The argument will never be evaluated, so don't complain. |
| 11235 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11236 | |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 11237 | case ConstantEvaluated: |
| 11238 | // Relevant diagnostics should be produced by constant evaluation. |
| 11239 | break; |
| 11240 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11241 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11242 | case PotentiallyEvaluatedIfUsed: |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11243 | if (Statement && getCurFunctionOrMethodDecl()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 11244 | FunctionScopes.back()->PossiblyUnreachableDiags. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11245 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, Statement)); |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 11246 | } |
| 11247 | else |
| 11248 | Diag(Loc, PD); |
| 11249 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11250 | return true; |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11251 | } |
| 11252 | |
| 11253 | return false; |
| 11254 | } |
| 11255 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11256 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 11257 | CallExpr *CE, FunctionDecl *FD) { |
| 11258 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 11259 | return false; |
| 11260 | |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 11261 | // If we're inside a decltype's expression, don't check for a valid return |
| 11262 | // type or construct temporaries until we know whether this is the last call. |
| 11263 | if (ExprEvalContexts.back().IsDecltype) { |
| 11264 | ExprEvalContexts.back().DelayedDecltypeCalls.push_back(CE); |
| 11265 | return false; |
| 11266 | } |
| 11267 | |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 11268 | class CallReturnIncompleteDiagnoser : public TypeDiagnoser { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11269 | FunctionDecl *FD; |
| 11270 | CallExpr *CE; |
| 11271 | |
| 11272 | public: |
| 11273 | CallReturnIncompleteDiagnoser(FunctionDecl *FD, CallExpr *CE) |
| 11274 | : FD(FD), CE(CE) { } |
| 11275 | |
| 11276 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
| 11277 | if (!FD) { |
| 11278 | S.Diag(Loc, diag::err_call_incomplete_return) |
| 11279 | << T << CE->getSourceRange(); |
| 11280 | return; |
| 11281 | } |
| 11282 | |
| 11283 | S.Diag(Loc, diag::err_call_function_incomplete_return) |
| 11284 | << CE->getSourceRange() << FD->getDeclName() << T; |
| 11285 | S.Diag(FD->getLocation(), |
| 11286 | diag::note_function_with_incomplete_return_type_declared_here) |
| 11287 | << FD->getDeclName(); |
| 11288 | } |
| 11289 | } Diagnoser(FD, CE); |
| 11290 | |
| 11291 | if (RequireCompleteType(Loc, ReturnType, Diagnoser)) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11292 | return true; |
| 11293 | |
| 11294 | return false; |
| 11295 | } |
| 11296 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11297 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11298 | // will prevent this condition from triggering, which is what we want. |
| 11299 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 11300 | SourceLocation Loc; |
| 11301 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11302 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11303 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11304 | |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 11305 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11306 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11307 | return; |
| 11308 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11309 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 11310 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11311 | // Greylist some idioms by putting them into a warning subcategory. |
| 11312 | if (ObjCMessageExpr *ME |
| 11313 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 11314 | Selector Sel = ME->getSelector(); |
| 11315 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11316 | // self = [<foo> init...] |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 11317 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11318 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 11319 | |
| 11320 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 11321 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11322 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 11323 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11324 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11325 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 11326 | } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11327 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11328 | return; |
| 11329 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11330 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11331 | Loc = Op->getOperatorLoc(); |
Fariborz Jahanian | a414a2f | 2012-08-29 17:17:11 +0000 | [diff] [blame] | 11332 | } else if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
| 11333 | return DiagnoseAssignmentAsCondition(POE->getSyntacticForm()); |
| 11334 | else { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11335 | // Not an assignment. |
| 11336 | return; |
| 11337 | } |
| 11338 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 11339 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11340 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11341 | SourceLocation Open = E->getLocStart(); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 11342 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 11343 | Diag(Loc, diag::note_condition_assign_silence) |
| 11344 | << FixItHint::CreateInsertion(Open, "(") |
| 11345 | << FixItHint::CreateInsertion(Close, ")"); |
| 11346 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11347 | if (IsOrAssign) |
| 11348 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 11349 | << FixItHint::CreateReplacement(Loc, "!="); |
| 11350 | else |
| 11351 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 11352 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11353 | } |
| 11354 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11355 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 11356 | /// that the user intended an assignment used as condition. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11357 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *ParenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11358 | // Don't warn if the parens came from a macro. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11359 | SourceLocation parenLoc = ParenE->getLocStart(); |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11360 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 11361 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 11362 | // Don't warn for dependent expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11363 | if (ParenE->isTypeDependent()) |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 11364 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11365 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11366 | Expr *E = ParenE->IgnoreParens(); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11367 | |
| 11368 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 11369 | if (opE->getOpcode() == BO_EQ && |
| 11370 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 11371 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11372 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 11373 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 11374 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11375 | SourceRange ParenERange = ParenE->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 11376 | Diag(Loc, diag::note_equality_comparison_silence) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11377 | << FixItHint::CreateRemoval(ParenERange.getBegin()) |
| 11378 | << FixItHint::CreateRemoval(ParenERange.getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 11379 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 11380 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11381 | } |
| 11382 | } |
| 11383 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11384 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11385 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11386 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 11387 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11388 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11389 | ExprResult result = CheckPlaceholderExpr(E); |
| 11390 | if (result.isInvalid()) return ExprError(); |
| 11391 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 11392 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11393 | if (!E->isTypeDependent()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11394 | if (getLangOpts().CPlusPlus) |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 11395 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 11396 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11397 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 11398 | if (ERes.isInvalid()) |
| 11399 | return ExprError(); |
| 11400 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 11401 | |
| 11402 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11403 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 11404 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 11405 | << T << E->getSourceRange(); |
| 11406 | return ExprError(); |
| 11407 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11408 | } |
| 11409 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11410 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11411 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11412 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11413 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11414 | Expr *SubExpr) { |
| 11415 | if (!SubExpr) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11416 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11417 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11418 | return CheckBooleanCondition(SubExpr, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11419 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11420 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11421 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11422 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 11423 | /// to have an appropriate type. |
| 11424 | struct RebuildUnknownAnyFunction |
| 11425 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 11426 | |
| 11427 | Sema &S; |
| 11428 | |
| 11429 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 11430 | |
| 11431 | ExprResult VisitStmt(Stmt *S) { |
| 11432 | llvm_unreachable("unexpected statement!"); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11433 | } |
| 11434 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11435 | ExprResult VisitExpr(Expr *E) { |
| 11436 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 11437 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11438 | return ExprError(); |
| 11439 | } |
| 11440 | |
| 11441 | /// Rebuild an expression which simply semantically wraps another |
| 11442 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11443 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 11444 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11445 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11446 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11447 | Expr *SubExpr = SubResult.take(); |
| 11448 | E->setSubExpr(SubExpr); |
| 11449 | E->setType(SubExpr->getType()); |
| 11450 | E->setValueKind(SubExpr->getValueKind()); |
| 11451 | assert(E->getObjectKind() == OK_Ordinary); |
| 11452 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11453 | } |
| 11454 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11455 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 11456 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11457 | } |
| 11458 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11459 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 11460 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11461 | } |
| 11462 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11463 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 11464 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11465 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11466 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11467 | Expr *SubExpr = SubResult.take(); |
| 11468 | E->setSubExpr(SubExpr); |
| 11469 | E->setType(S.Context.getPointerType(SubExpr->getType())); |
| 11470 | assert(E->getValueKind() == VK_RValue); |
| 11471 | assert(E->getObjectKind() == OK_Ordinary); |
| 11472 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11473 | } |
| 11474 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11475 | ExprResult resolveDecl(Expr *E, ValueDecl *VD) { |
| 11476 | if (!isa<FunctionDecl>(VD)) return VisitExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11477 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11478 | E->setType(VD->getType()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11479 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11480 | assert(E->getValueKind() == VK_RValue); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11481 | if (S.getLangOpts().CPlusPlus && |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11482 | !(isa<CXXMethodDecl>(VD) && |
| 11483 | cast<CXXMethodDecl>(VD)->isInstance())) |
| 11484 | E->setValueKind(VK_LValue); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11485 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11486 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11487 | } |
| 11488 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11489 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 11490 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11491 | } |
| 11492 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11493 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 11494 | return resolveDecl(E, E->getDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11495 | } |
| 11496 | }; |
| 11497 | } |
| 11498 | |
| 11499 | /// Given a function expression of unknown-any type, try to rebuild it |
| 11500 | /// to have a function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11501 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { |
| 11502 | ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); |
| 11503 | if (Result.isInvalid()) return ExprError(); |
| 11504 | return S.DefaultFunctionArrayConversion(Result.take()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11505 | } |
| 11506 | |
| 11507 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11508 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 11509 | /// into one which resolves the type directly on the referring |
| 11510 | /// expression. Strict preservation of the original source |
| 11511 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11512 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11513 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11514 | |
| 11515 | Sema &S; |
| 11516 | |
| 11517 | /// The current destination type. |
| 11518 | QualType DestType; |
| 11519 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11520 | RebuildUnknownAnyExpr(Sema &S, QualType CastType) |
| 11521 | : S(S), DestType(CastType) {} |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11522 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11523 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11524 | llvm_unreachable("unexpected statement!"); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11525 | } |
| 11526 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11527 | ExprResult VisitExpr(Expr *E) { |
| 11528 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 11529 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11530 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11531 | } |
| 11532 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11533 | ExprResult VisitCallExpr(CallExpr *E); |
| 11534 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11535 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11536 | /// Rebuild an expression which simply semantically wraps another |
| 11537 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11538 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 11539 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11540 | if (SubResult.isInvalid()) return ExprError(); |
| 11541 | Expr *SubExpr = SubResult.take(); |
| 11542 | E->setSubExpr(SubExpr); |
| 11543 | E->setType(SubExpr->getType()); |
| 11544 | E->setValueKind(SubExpr->getValueKind()); |
| 11545 | assert(E->getObjectKind() == OK_Ordinary); |
| 11546 | return E; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11547 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11548 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11549 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 11550 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11551 | } |
| 11552 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11553 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 11554 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11555 | } |
| 11556 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11557 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 11558 | const PointerType *Ptr = DestType->getAs<PointerType>(); |
| 11559 | if (!Ptr) { |
| 11560 | S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 11561 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11562 | return ExprError(); |
| 11563 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11564 | assert(E->getValueKind() == VK_RValue); |
| 11565 | assert(E->getObjectKind() == OK_Ordinary); |
| 11566 | E->setType(DestType); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11567 | |
| 11568 | // 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] | 11569 | DestType = Ptr->getPointeeType(); |
| 11570 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11571 | if (SubResult.isInvalid()) return ExprError(); |
| 11572 | E->setSubExpr(SubResult.take()); |
| 11573 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11574 | } |
| 11575 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11576 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11577 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11578 | ExprResult resolveDecl(Expr *E, ValueDecl *VD); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11579 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11580 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 11581 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11582 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11583 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11584 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 11585 | return resolveDecl(E, E->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11586 | } |
| 11587 | }; |
| 11588 | } |
| 11589 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11590 | /// Rebuilds a call expression which yielded __unknown_anytype. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11591 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { |
| 11592 | Expr *CalleeExpr = E->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11593 | |
| 11594 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11595 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11596 | FK_FunctionPointer, |
| 11597 | FK_BlockPointer |
| 11598 | }; |
| 11599 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11600 | FnKind Kind; |
| 11601 | QualType CalleeType = CalleeExpr->getType(); |
| 11602 | if (CalleeType == S.Context.BoundMemberTy) { |
| 11603 | assert(isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E)); |
| 11604 | Kind = FK_MemberFunction; |
| 11605 | CalleeType = Expr::findBoundMemberType(CalleeExpr); |
| 11606 | } else if (const PointerType *Ptr = CalleeType->getAs<PointerType>()) { |
| 11607 | CalleeType = Ptr->getPointeeType(); |
| 11608 | Kind = FK_FunctionPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11609 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11610 | CalleeType = CalleeType->castAs<BlockPointerType>()->getPointeeType(); |
| 11611 | Kind = FK_BlockPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11612 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11613 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11614 | |
| 11615 | // Verify that this is a legal result type of a function. |
| 11616 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 11617 | unsigned diagID = diag::err_func_returning_array_function; |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11618 | if (Kind == FK_BlockPointer) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11619 | diagID = diag::err_block_returning_array_function; |
| 11620 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11621 | S.Diag(E->getExprLoc(), diagID) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11622 | << DestType->isFunctionType() << DestType; |
| 11623 | return ExprError(); |
| 11624 | } |
| 11625 | |
| 11626 | // Otherwise, go ahead and set DestType as the call's result. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11627 | E->setType(DestType.getNonLValueExprType(S.Context)); |
| 11628 | E->setValueKind(Expr::getValueKindForType(DestType)); |
| 11629 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11630 | |
| 11631 | // Rebuild the function type, replacing the result type with DestType. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11632 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType)) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11633 | DestType = S.Context.getFunctionType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11634 | Proto->arg_type_begin(), |
| 11635 | Proto->getNumArgs(), |
| 11636 | Proto->getExtProtoInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11637 | else |
| 11638 | DestType = S.Context.getFunctionNoProtoType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11639 | FnType->getExtInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11640 | |
| 11641 | // Rebuild the appropriate pointer-to-function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11642 | switch (Kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11643 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11644 | // Nothing to do. |
| 11645 | break; |
| 11646 | |
| 11647 | case FK_FunctionPointer: |
| 11648 | DestType = S.Context.getPointerType(DestType); |
| 11649 | break; |
| 11650 | |
| 11651 | case FK_BlockPointer: |
| 11652 | DestType = S.Context.getBlockPointerType(DestType); |
| 11653 | break; |
| 11654 | } |
| 11655 | |
| 11656 | // Finally, we can recurse. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11657 | ExprResult CalleeResult = Visit(CalleeExpr); |
| 11658 | if (!CalleeResult.isUsable()) return ExprError(); |
| 11659 | E->setCallee(CalleeResult.take()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11660 | |
| 11661 | // Bind a temporary if necessary. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11662 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11663 | } |
| 11664 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11665 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11666 | // Verify that this is a legal result type of a call. |
| 11667 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11668 | S.Diag(E->getExprLoc(), diag::err_func_returning_array_function) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11669 | << DestType->isFunctionType() << DestType; |
| 11670 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11671 | } |
| 11672 | |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 11673 | // Rewrite the method result type if available. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11674 | if (ObjCMethodDecl *Method = E->getMethodDecl()) { |
| 11675 | assert(Method->getResultType() == S.Context.UnknownAnyTy); |
| 11676 | Method->setResultType(DestType); |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 11677 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11678 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11679 | // Change the type of the message. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11680 | E->setType(DestType.getNonReferenceType()); |
| 11681 | E->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11682 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11683 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11684 | } |
| 11685 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11686 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11687 | // 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] | 11688 | if (E->getCastKind() == CK_FunctionToPointerDecay) { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11689 | assert(E->getValueKind() == VK_RValue); |
| 11690 | assert(E->getObjectKind() == OK_Ordinary); |
| 11691 | |
| 11692 | E->setType(DestType); |
| 11693 | |
| 11694 | // Rebuild the sub-expression as the pointee (function) type. |
| 11695 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 11696 | |
| 11697 | ExprResult Result = Visit(E->getSubExpr()); |
| 11698 | if (!Result.isUsable()) return ExprError(); |
| 11699 | |
| 11700 | E->setSubExpr(Result.take()); |
| 11701 | return S.Owned(E); |
Sean Callanan | ba66c6c | 2012-03-06 23:12:57 +0000 | [diff] [blame] | 11702 | } else if (E->getCastKind() == CK_LValueToRValue) { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11703 | assert(E->getValueKind() == VK_RValue); |
| 11704 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11705 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11706 | assert(isa<BlockPointerType>(E->getType())); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11707 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11708 | E->setType(DestType); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11709 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11710 | // The sub-expression has to be a lvalue reference, so rebuild it as such. |
| 11711 | DestType = S.Context.getLValueReferenceType(DestType); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11712 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11713 | ExprResult Result = Visit(E->getSubExpr()); |
| 11714 | if (!Result.isUsable()) return ExprError(); |
| 11715 | |
| 11716 | E->setSubExpr(Result.take()); |
| 11717 | return S.Owned(E); |
Sean Callanan | ba66c6c | 2012-03-06 23:12:57 +0000 | [diff] [blame] | 11718 | } else { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11719 | llvm_unreachable("Unhandled cast type!"); |
| 11720 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11721 | } |
| 11722 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11723 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { |
| 11724 | ExprValueKind ValueKind = VK_LValue; |
| 11725 | QualType Type = DestType; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11726 | |
| 11727 | // We know how to make this work for certain kinds of decls: |
| 11728 | |
| 11729 | // - functions |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11730 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(VD)) { |
| 11731 | if (const PointerType *Ptr = Type->getAs<PointerType>()) { |
| 11732 | DestType = Ptr->getPointeeType(); |
| 11733 | ExprResult Result = resolveDecl(E, VD); |
| 11734 | if (Result.isInvalid()) return ExprError(); |
| 11735 | return S.ImpCastExprToType(Result.take(), Type, |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 11736 | CK_FunctionToPointerDecay, VK_RValue); |
| 11737 | } |
| 11738 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11739 | if (!Type->isFunctionType()) { |
| 11740 | S.Diag(E->getExprLoc(), diag::err_unknown_any_function) |
| 11741 | << VD << E->getSourceRange(); |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 11742 | return ExprError(); |
| 11743 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11744 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11745 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 11746 | if (MD->isInstance()) { |
| 11747 | ValueKind = VK_RValue; |
| 11748 | Type = S.Context.BoundMemberTy; |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11749 | } |
| 11750 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11751 | // Function references aren't l-values in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11752 | if (!S.getLangOpts().CPlusPlus) |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11753 | ValueKind = VK_RValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11754 | |
| 11755 | // - variables |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11756 | } else if (isa<VarDecl>(VD)) { |
| 11757 | if (const ReferenceType *RefTy = Type->getAs<ReferenceType>()) { |
| 11758 | Type = RefTy->getPointeeType(); |
| 11759 | } else if (Type->isFunctionType()) { |
| 11760 | S.Diag(E->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 11761 | << VD << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11762 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11763 | } |
| 11764 | |
| 11765 | // - nothing else |
| 11766 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11767 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 11768 | << VD << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11769 | return ExprError(); |
| 11770 | } |
| 11771 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11772 | VD->setType(DestType); |
| 11773 | E->setType(Type); |
| 11774 | E->setValueKind(ValueKind); |
| 11775 | return S.Owned(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11776 | } |
| 11777 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11778 | /// Check a cast of an unknown-any type. We intentionally only |
| 11779 | /// trigger this for C-style casts. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11780 | ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, |
| 11781 | Expr *CastExpr, CastKind &CastKind, |
| 11782 | ExprValueKind &VK, CXXCastPath &Path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11783 | // Rewrite the casted expression from scratch. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11784 | ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11785 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11786 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11787 | CastExpr = result.take(); |
| 11788 | VK = CastExpr->getValueKind(); |
| 11789 | CastKind = CK_NoOp; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11790 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11791 | return CastExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11792 | } |
| 11793 | |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 11794 | ExprResult Sema::forceUnknownAnyToType(Expr *E, QualType ToType) { |
| 11795 | return RebuildUnknownAnyExpr(*this, ToType).Visit(E); |
| 11796 | } |
| 11797 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11798 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { |
| 11799 | Expr *orig = E; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11800 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11801 | while (true) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11802 | E = E->IgnoreParenImpCasts(); |
| 11803 | if (CallExpr *call = dyn_cast<CallExpr>(E)) { |
| 11804 | E = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11805 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 11806 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11807 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11808 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11809 | } |
| 11810 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11811 | SourceLocation loc; |
| 11812 | NamedDecl *d; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11813 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11814 | loc = ref->getLocation(); |
| 11815 | d = ref->getDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11816 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11817 | loc = mem->getMemberLoc(); |
| 11818 | d = mem->getMemberDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11819 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11820 | diagID = diag::err_uncasted_call_of_unknown_any; |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 11821 | loc = msg->getSelectorStartLoc(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11822 | d = msg->getMethodDecl(); |
John McCall | 819e745 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 11823 | if (!d) { |
| 11824 | S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) |
| 11825 | << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() |
| 11826 | << orig->getSourceRange(); |
| 11827 | return ExprError(); |
| 11828 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11829 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11830 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 11831 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11832 | return ExprError(); |
| 11833 | } |
| 11834 | |
| 11835 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11836 | |
| 11837 | // Never recoverable. |
| 11838 | return ExprError(); |
| 11839 | } |
| 11840 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11841 | /// Check for operands with placeholder types and complain if found. |
| 11842 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 11843 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11844 | const BuiltinType *placeholderType = E->getType()->getAsPlaceholderType(); |
| 11845 | if (!placeholderType) return Owned(E); |
| 11846 | |
| 11847 | switch (placeholderType->getKind()) { |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11848 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11849 | // Overloaded expressions. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11850 | case BuiltinType::Overload: { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11851 | // Try to resolve a single function template specialization. |
| 11852 | // This is obligatory. |
| 11853 | ExprResult result = Owned(E); |
| 11854 | if (ResolveAndFixSingleFunctionTemplateSpecialization(result, false)) { |
| 11855 | return result; |
| 11856 | |
| 11857 | // If that failed, try to recover with a call. |
| 11858 | } else { |
| 11859 | tryToRecoverWithCall(result, PDiag(diag::err_ovl_unresolvable), |
| 11860 | /*complain*/ true); |
| 11861 | return result; |
| 11862 | } |
| 11863 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11864 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11865 | // Bound member functions. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11866 | case BuiltinType::BoundMember: { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11867 | ExprResult result = Owned(E); |
| 11868 | tryToRecoverWithCall(result, PDiag(diag::err_bound_member_function), |
| 11869 | /*complain*/ true); |
| 11870 | return result; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11871 | } |
| 11872 | |
| 11873 | // ARC unbridged casts. |
| 11874 | case BuiltinType::ARCUnbridgedCast: { |
| 11875 | Expr *realCast = stripARCUnbridgedCast(E); |
| 11876 | diagnoseARCUnbridgedCast(realCast); |
| 11877 | return Owned(realCast); |
| 11878 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11879 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11880 | // Expressions of unknown type. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11881 | case BuiltinType::UnknownAny: |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11882 | return diagnoseUnknownAnyExpr(*this, E); |
| 11883 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 11884 | // Pseudo-objects. |
| 11885 | case BuiltinType::PseudoObject: |
| 11886 | return checkPseudoObjectRValue(E); |
| 11887 | |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 11888 | case BuiltinType::BuiltinFn: |
| 11889 | Diag(E->getLocStart(), diag::err_builtin_fn_use); |
| 11890 | return ExprError(); |
| 11891 | |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 11892 | // Everything else should be impossible. |
| 11893 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 11894 | case BuiltinType::Id: |
| 11895 | #define PLACEHOLDER_TYPE(Id, SingletonId) |
| 11896 | #include "clang/AST/BuiltinTypes.def" |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11897 | break; |
| 11898 | } |
| 11899 | |
| 11900 | llvm_unreachable("invalid placeholder type!"); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11901 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11902 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11903 | bool Sema::CheckCaseExpression(Expr *E) { |
| 11904 | if (E->isTypeDependent()) |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11905 | return true; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11906 | if (E->isValueDependent() || E->isIntegerConstantExpr(Context)) |
| 11907 | return E->getType()->isIntegralOrEnumerationType(); |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11908 | return false; |
| 11909 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11910 | |
| 11911 | /// ActOnObjCBoolLiteral - Parse {__objc_yes,__objc_no} literals. |
| 11912 | ExprResult |
| 11913 | Sema::ActOnObjCBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
| 11914 | assert((Kind == tok::kw___objc_yes || Kind == tok::kw___objc_no) && |
| 11915 | "Unknown Objective-C Boolean value!"); |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 11916 | QualType BoolT = Context.ObjCBuiltinBoolTy; |
| 11917 | if (!Context.getBOOLDecl()) { |
| 11918 | LookupResult Result(*this, &Context.Idents.get("BOOL"), SourceLocation(), |
| 11919 | Sema::LookupOrdinaryName); |
| 11920 | if (LookupName(Result, getCurScope())) { |
| 11921 | NamedDecl *ND = Result.getFoundDecl(); |
| 11922 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 11923 | Context.setBOOLDecl(TD); |
| 11924 | } |
| 11925 | } |
| 11926 | if (Context.getBOOLDecl()) |
| 11927 | BoolT = Context.getBOOLType(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11928 | return Owned(new (Context) ObjCBoolLiteralExpr(Kind == tok::kw___objc_yes, |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 11929 | BoolT, OpLoc)); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11930 | } |