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() |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1643 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
1644 | if (ND) | ||||
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1645 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1646 | << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1647 | |
1648 | // Tell the callee to try to recover. | ||||
1649 | return false; | ||||
1650 | } | ||||
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1651 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1652 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1653 | // FIXME: If we ended up with a typo for a type name or |
1654 | // Objective-C class name, we're in trouble because the parser | ||||
1655 | // is in the wrong place to recover. Suggest the typo | ||||
1656 | // correction, but don't make it a fix-it since we're not going | ||||
1657 | // to recover well anyway. | ||||
1658 | if (SS.isEmpty()) | ||||
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1659 | Diag(R.getNameLoc(), diagnostic_suggest) |
1660 | << Name << CorrectedQuotedStr; | ||||
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1661 | else |
1662 | Diag(R.getNameLoc(), diag::err_no_member_suggest) | ||||
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1663 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1664 | << SS.getRange(); |
1665 | |||||
1666 | // Don't try to recover; it won't work. | ||||
1667 | return true; | ||||
1668 | } | ||||
1669 | } else { | ||||
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1670 | // 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] | 1671 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1672 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1673 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1674 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1675 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1676 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1677 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1678 | return true; |
1679 | } | ||||
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1680 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1681 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1682 | |
1683 | // Emit a special diagnostic for failed member lookups. | ||||
1684 | // FIXME: computing the declaration context might fail here (?) | ||||
1685 | if (!SS.isEmpty()) { | ||||
1686 | Diag(R.getNameLoc(), diag::err_no_member) | ||||
1687 | << Name << computeDeclContext(SS, false) | ||||
1688 | << SS.getRange(); | ||||
1689 | return true; | ||||
1690 | } | ||||
1691 | |||||
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1692 | // Give up, we can't recover. |
1693 | Diag(R.getNameLoc(), diagnostic) << Name; | ||||
1694 | return true; | ||||
1695 | } | ||||
1696 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1697 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1698 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1699 | SourceLocation TemplateKWLoc, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1700 | UnqualifiedId &Id, |
1701 | bool HasTrailingLParen, | ||||
Kaelyn Uhrain | cd78e61 | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 1702 | bool IsAddressOfOperand, |
1703 | CorrectionCandidateCallback *CCC) { | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1704 | assert(!(IsAddressOfOperand && HasTrailingLParen) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1705 | "cannot be direct & operand and have a trailing lparen"); |
1706 | |||||
1707 | if (SS.isInvalid()) | ||||
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1708 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1709 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1710 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1711 | |
1712 | // Decompose the UnqualifiedId into the following data. | ||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1713 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1714 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1715 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1716 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1717 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1718 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1719 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1720 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1721 | // C++ [temp.dep.expr]p3: |
1722 | // An id-expression is type-dependent if it contains: | ||||
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1723 | // -- an identifier that was declared with a dependent type, |
1724 | // (note: handled after lookup) | ||||
1725 | // -- a template-id that is dependent, | ||||
1726 | // (note: handled in BuildTemplateIdExpr) | ||||
1727 | // -- a conversion-function-id that specifies a dependent type, | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1728 | // -- a nested-name-specifier that contains a class-name that |
1729 | // names a dependent type. | ||||
1730 | // Determine whether this is a member of an unknown specialization; | ||||
1731 | // we need to handle these differently. | ||||
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1732 | bool DependentID = false; |
1733 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && | ||||
1734 | Name.getCXXNameType()->isDependentType()) { | ||||
1735 | DependentID = true; | ||||
1736 | } else if (SS.isSet()) { | ||||
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1737 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1738 | if (RequireCompleteDeclContext(SS, DC)) |
1739 | return ExprError(); | ||||
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1740 | } else { |
1741 | DependentID = true; | ||||
1742 | } | ||||
1743 | } | ||||
1744 | |||||
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1745 | if (DependentID) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1746 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
1747 | IsAddressOfOperand, TemplateArgs); | ||||
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1748 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1749 | // Perform the required lookup. |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1750 | LookupResult R(*this, NameInfo, |
1751 | (Id.getKind() == UnqualifiedId::IK_ImplicitSelfParam) | ||||
1752 | ? LookupObjCImplicitSelfParam : LookupOrdinaryName); | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1753 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1754 | // Lookup the template name again to correctly establish the context in |
1755 | // which it was found. This is really unfortunate as we already did the | ||||
1756 | // lookup to determine that it was a template name in the first place. If | ||||
1757 | // this becomes a performance hit, we can work harder to preserve those | ||||
1758 | // results until we get here but it's likely not worth it. | ||||
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1759 | bool MemberOfUnknownSpecialization; |
1760 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, | ||||
1761 | MemberOfUnknownSpecialization); | ||||
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1762 | |
1763 | if (MemberOfUnknownSpecialization || | ||||
1764 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) | ||||
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1765 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
1766 | IsAddressOfOperand, TemplateArgs); | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1767 | } else { |
Benjamin Kramer | b7ff74a | 2012-01-20 14:57:34 +0000 | [diff] [blame] | 1768 | bool IvarLookupFollowUp = II && !SS.isSet() && getCurMethodDecl(); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1769 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1771 | // If the result might be in a dependent base class, this is a dependent |
1772 | // id-expression. | ||||
1773 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) | ||||
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1774 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
1775 | IsAddressOfOperand, TemplateArgs); | ||||
1776 | |||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1777 | // If this reference is in an Objective-C method, then we need to do |
1778 | // some special Objective-C lookup, too. | ||||
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1779 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1780 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1781 | if (E.isInvalid()) |
1782 | return ExprError(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1784 | if (Expr *Ex = E.takeAs<Expr>()) |
1785 | return Owned(Ex); | ||||
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1786 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1787 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1788 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1789 | if (R.isAmbiguous()) |
1790 | return ExprError(); | ||||
1791 | |||||
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1792 | // Determine whether this name might be a candidate for |
1793 | // argument-dependent lookup. | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1794 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1795 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1796 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1797 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1798 | // in C90, extension in C99, forbidden in C++). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1799 | if (HasTrailingLParen && II && !getLangOpts().CPlusPlus) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1800 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
1801 | if (D) R.addDecl(D); | ||||
1802 | } | ||||
1803 | |||||
1804 | // If this name wasn't predeclared and if this is not a function | ||||
1805 | // call, diagnose the problem. | ||||
1806 | if (R.empty()) { | ||||
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1807 | |
1808 | // In Microsoft mode, if we are inside a template class member function | ||||
1809 | // and we can't resolve an identifier then assume the identifier is type | ||||
1810 | // dependent. The goal is to postpone name lookup to instantiation time | ||||
1811 | // to be able to search into type dependent base classes. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1812 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1813 | isa<CXXMethodDecl>(CurContext)) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1814 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
1815 | IsAddressOfOperand, TemplateArgs); | ||||
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1816 | |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 1817 | CorrectionCandidateCallback DefaultValidator; |
Kaelyn Uhrain | cd78e61 | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 1818 | if (DiagnoseEmptyLookup(S, SS, R, CCC ? *CCC : DefaultValidator)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1819 | return ExprError(); |
1820 | |||||
1821 | assert(!R.empty() && | ||||
1822 | "DiagnoseEmptyLookup returned false but added no results"); | ||||
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1823 | |
1824 | // If we found an Objective-C instance variable, let | ||||
1825 | // LookupInObjCMethod build the appropriate expression to | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1826 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1827 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
1828 | R.clear(); | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1829 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Fariborz Jahanian | bc2b91a | 2011-09-23 23:11:38 +0000 | [diff] [blame] | 1830 | // In a hopelessly buggy code, Objective-C instance variable |
1831 | // lookup fails and no expression will be built to reference it. | ||||
1832 | if (!E.isInvalid() && !E.get()) | ||||
1833 | return ExprError(); | ||||
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1834 | return E; |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1835 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1836 | } |
1837 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1839 | // This is guaranteed from this point on. |
1840 | assert(!R.empty() || ADL); | ||||
1841 | |||||
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1842 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1843 | // C++ [class.mfct.non-static]p3: |
1844 | // When an id-expression that is not part of a class member access | ||||
1845 | // syntax and not used to form a pointer to member is used in the | ||||
1846 | // body of a non-static member function of class X, if name lookup | ||||
1847 | // resolves the name in the id-expression to a non-static non-type | ||||
1848 | // member of some class C, the id-expression is transformed into a | ||||
1849 | // class member access expression using (*this) as the | ||||
1850 | // postfix-expression to the left of the . operator. | ||||
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1851 | // |
1852 | // But we don't actually need to do this for '&' operands if R | ||||
1853 | // resolved to a function or overloaded function set, because the | ||||
1854 | // expression is ill-formed if it actually works out to be a | ||||
1855 | // non-static member function: | ||||
1856 | // | ||||
1857 | // C++ [expr.ref]p4: | ||||
1858 | // Otherwise, if E1.E2 refers to a non-static member function. . . | ||||
1859 | // [t]he expression can be used only as the left-hand operand of a | ||||
1860 | // member function call. | ||||
1861 | // | ||||
1862 | // There are other safeguards against such uses, but it's important | ||||
1863 | // to get this right here so that we don't end up making a | ||||
1864 | // spuriously dependent expression if we're inside a dependent | ||||
1865 | // instance method. | ||||
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1866 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1867 | bool MightBeImplicitMember; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1868 | if (!IsAddressOfOperand) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1869 | MightBeImplicitMember = true; |
1870 | else if (!SS.isEmpty()) | ||||
1871 | MightBeImplicitMember = false; | ||||
1872 | else if (R.isOverloadedResult()) | ||||
1873 | MightBeImplicitMember = false; | ||||
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1874 | else if (R.isUnresolvableResult()) |
1875 | MightBeImplicitMember = true; | ||||
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1876 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1877 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
1878 | isa<IndirectFieldDecl>(R.getFoundDecl()); | ||||
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1879 | |
1880 | if (MightBeImplicitMember) | ||||
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1881 | return BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
1882 | R, TemplateArgs); | ||||
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1883 | } |
1884 | |||||
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 1885 | if (TemplateArgs || TemplateKWLoc.isValid()) |
1886 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, ADL, TemplateArgs); | ||||
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1887 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1888 | return BuildDeclarationNameExpr(SS, R, ADL); |
1889 | } | ||||
1890 | |||||
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1891 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
1892 | /// declaration name, generally during template instantiation. | ||||
1893 | /// There's a large number of things which don't need to be done along | ||||
1894 | /// this path. | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1895 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1896 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1897 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1898 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1899 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 1900 | return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), |
1901 | NameInfo, /*TemplateArgs=*/0); | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1902 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1903 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1904 | return ExprError(); |
1905 | |||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1906 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1907 | LookupQualifiedName(R, DC); |
1908 | |||||
1909 | if (R.isAmbiguous()) | ||||
1910 | return ExprError(); | ||||
1911 | |||||
1912 | if (R.empty()) { | ||||
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1913 | Diag(NameInfo.getLoc(), diag::err_no_member) |
1914 | << NameInfo.getName() << DC << SS.getRange(); | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1915 | return ExprError(); |
1916 | } | ||||
1917 | |||||
1918 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); | ||||
1919 | } | ||||
1920 | |||||
1921 | /// LookupInObjCMethod - The parser has read a name in, and Sema has | ||||
1922 | /// detected that we're currently inside an ObjC method. Perform some | ||||
1923 | /// additional lookup. | ||||
1924 | /// | ||||
1925 | /// Ideally, most of this would be done by lookup, but there's | ||||
1926 | /// actually quite a lot of extra work involved. | ||||
1927 | /// | ||||
1928 | /// Returns a null sentinel to indicate trivial success. | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1929 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1930 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1931 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1932 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1933 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1934 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1935 | // There are two cases to handle here. 1) scoped lookup could have failed, |
1936 | // in which case we should look for an ivar. 2) scoped lookup could have | ||||
1937 | // found a decl, but that decl is outside the current instance method (i.e. | ||||
1938 | // a global variable). In these two cases, we do a lookup for an ivar with | ||||
1939 | // this name, if the lookup sucedes, we replace it our current decl. | ||||
1940 | |||||
1941 | // If we're in a class method, we don't normally want to look for | ||||
1942 | // ivars. But if we don't find anything else, and there's an | ||||
1943 | // ivar, that's an error. | ||||
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1944 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1945 | |
1946 | bool LookForIvars; | ||||
1947 | if (Lookup.empty()) | ||||
1948 | LookForIvars = true; | ||||
1949 | else if (IsClassMethod) | ||||
1950 | LookForIvars = false; | ||||
1951 | else | ||||
1952 | LookForIvars = (Lookup.isSingleResult() && | ||||
1953 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); | ||||
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1954 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1955 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1956 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1957 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | 7c81c2a | 2011-10-19 02:25:16 +0000 | [diff] [blame] | 1958 | ObjCIvarDecl *IV = 0; |
1959 | if (IFace && (IV = IFace->lookupInstanceVariable(II, ClassDeclared))) { | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1960 | // Diagnose using an ivar in a class method. |
1961 | if (IsClassMethod) | ||||
1962 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) | ||||
1963 | << IV->getDeclName()); | ||||
1964 | |||||
1965 | // If we're referencing an invalid decl, just return this as a silent | ||||
1966 | // error node. The error diagnostic was already emitted on the decl. | ||||
1967 | if (IV->isInvalidDecl()) | ||||
1968 | return ExprError(); | ||||
1969 | |||||
1970 | // Check if referencing a field with __attribute__((deprecated)). | ||||
1971 | if (DiagnoseUseOfDecl(IV, Loc)) | ||||
1972 | return ExprError(); | ||||
1973 | |||||
1974 | // Diagnose the use of an ivar outside of the declaring class. | ||||
1975 | if (IV->getAccessControl() == ObjCIvarDecl::Private && | ||||
Fariborz Jahanian | 458a7fb | 2012-03-07 00:58:41 +0000 | [diff] [blame] | 1976 | !declaresSameEntity(ClassDeclared, IFace) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1977 | !getLangOpts().DebuggerSupport) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1978 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
1979 | |||||
1980 | // FIXME: This should use a new expr for a direct reference, don't | ||||
1981 | // turn this into Self->ivar, just return a BareIVarExpr or something. | ||||
1982 | IdentifierInfo &II = Context.Idents.get("self"); | ||||
1983 | UnqualifiedId SelfName; | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1984 | SelfName.setIdentifier(&II, SourceLocation()); |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1985 | SelfName.setKind(UnqualifiedId::IK_ImplicitSelfParam); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1986 | CXXScopeSpec SelfScopeSpec; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1987 | SourceLocation TemplateKWLoc; |
1988 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, TemplateKWLoc, | ||||
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1989 | SelfName, false, false); |
1990 | if (SelfExpr.isInvalid()) | ||||
1991 | return ExprError(); | ||||
1992 | |||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1993 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
1994 | if (SelfExpr.isInvalid()) | ||||
1995 | return ExprError(); | ||||
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1996 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1997 | MarkAnyDeclReferenced(Loc, IV); |
Fariborz Jahanian | ed6662d | 2012-08-08 16:41:04 +0000 | [diff] [blame] | 1998 | |
1999 | ObjCMethodFamily MF = CurMethod->getMethodFamily(); | ||||
2000 | if (MF != OMF_init && MF != OMF_dealloc && MF != OMF_finalize) | ||||
2001 | Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName(); | ||||
Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 2002 | |
2003 | ObjCIvarRefExpr *Result = new (Context) ObjCIvarRefExpr(IV, IV->getType(), | ||||
2004 | Loc, | ||||
2005 | SelfExpr.take(), | ||||
2006 | true, true); | ||||
2007 | |||||
2008 | if (getLangOpts().ObjCAutoRefCount) { | ||||
2009 | if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { | ||||
2010 | DiagnosticsEngine::Level Level = | ||||
2011 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, Loc); | ||||
2012 | if (Level != DiagnosticsEngine::Ignored) | ||||
2013 | getCurFunction()->recordUseOfWeak(Result); | ||||
2014 | } | ||||
Fariborz Jahanian | 3f001ff | 2012-10-03 17:55:29 +0000 | [diff] [blame] | 2015 | if (CurContext->isClosure()) |
2016 | Diag(Loc, diag::warn_implicitly_retains_self) | ||||
2017 | << FixItHint::CreateInsertion(Loc, "self->"); | ||||
Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 2018 | } |
2019 | |||||
2020 | return Owned(Result); | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2021 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2022 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2023 | // We should warn if a local variable hides an ivar. |
Fariborz Jahanian | 90f7b62 | 2011-11-08 22:51:27 +0000 | [diff] [blame] | 2024 | if (ObjCInterfaceDecl *IFace = CurMethod->getClassInterface()) { |
2025 | ObjCInterfaceDecl *ClassDeclared; | ||||
2026 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { | ||||
2027 | if (IV->getAccessControl() != ObjCIvarDecl::Private || | ||||
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 2028 | declaresSameEntity(IFace, ClassDeclared)) |
Fariborz Jahanian | 90f7b62 | 2011-11-08 22:51:27 +0000 | [diff] [blame] | 2029 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
2030 | } | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2031 | } |
Fariborz Jahanian | b5ea9db | 2011-12-20 22:21:08 +0000 | [diff] [blame] | 2032 | } else if (Lookup.isSingleResult() && |
2033 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()) { | ||||
2034 | // If accessing a stand-alone ivar in a class method, this is an error. | ||||
2035 | if (const ObjCIvarDecl *IV = dyn_cast<ObjCIvarDecl>(Lookup.getFoundDecl())) | ||||
2036 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) | ||||
2037 | << IV->getDeclName()); | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2038 | } |
2039 | |||||
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2040 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
2041 | // FIXME. Consolidate this with similar code in LookupName. | ||||
2042 | if (unsigned BuiltinID = II->getBuiltinID()) { | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2043 | if (!(getLangOpts().CPlusPlus && |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2044 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
2045 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, | ||||
2046 | S, Lookup.isForRedeclaration(), | ||||
2047 | Lookup.getNameLoc()); | ||||
2048 | if (D) Lookup.addDecl(D); | ||||
2049 | } | ||||
2050 | } | ||||
2051 | } | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2052 | // Sentinel value saying that we didn't do anything special. |
2053 | return Owned((Expr*) 0); | ||||
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2054 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2055 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2056 | /// \brief Cast a base object to a member's actual type. |
2057 | /// | ||||
2058 | /// Logically this happens in three phases: | ||||
2059 | /// | ||||
2060 | /// * First we cast from the base type to the naming class. | ||||
2061 | /// The naming class is the class into which we were looking | ||||
2062 | /// when we found the member; it's the qualifier type if a | ||||
2063 | /// qualifier was provided, and otherwise it's the base type. | ||||
2064 | /// | ||||
2065 | /// * Next we cast from the naming class to the declaring class. | ||||
2066 | /// If the member we found was brought into a class's scope by | ||||
2067 | /// a using declaration, this is that class; otherwise it's | ||||
2068 | /// the class declaring the member. | ||||
2069 | /// | ||||
2070 | /// * Finally we cast from the declaring class to the "true" | ||||
2071 | /// declaring class of the member. This conversion does not | ||||
2072 | /// obey access control. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2073 | ExprResult |
2074 | Sema::PerformObjectMemberConversion(Expr *From, | ||||
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2075 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2076 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2077 | NamedDecl *Member) { |
2078 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); | ||||
2079 | if (!RD) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2080 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2081 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2082 | QualType DestRecordType; |
2083 | QualType DestType; | ||||
2084 | QualType FromRecordType; | ||||
2085 | QualType FromType = From->getType(); | ||||
2086 | bool PointerConversions = false; | ||||
2087 | if (isa<FieldDecl>(Member)) { | ||||
2088 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2089 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2090 | if (FromType->getAs<PointerType>()) { |
2091 | DestType = Context.getPointerType(DestRecordType); | ||||
2092 | FromRecordType = FromType->getPointeeType(); | ||||
2093 | PointerConversions = true; | ||||
2094 | } else { | ||||
2095 | DestType = DestRecordType; | ||||
2096 | FromRecordType = FromType; | ||||
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2097 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2098 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
2099 | if (Method->isStatic()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2100 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2102 | DestType = Method->getThisType(Context); |
2103 | DestRecordType = DestType->getPointeeType(); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2104 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2105 | if (FromType->getAs<PointerType>()) { |
2106 | FromRecordType = FromType->getPointeeType(); | ||||
2107 | PointerConversions = true; | ||||
2108 | } else { | ||||
2109 | FromRecordType = FromType; | ||||
2110 | DestType = DestRecordType; | ||||
2111 | } | ||||
2112 | } else { | ||||
2113 | // No conversion necessary. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2114 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2115 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2117 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2118 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2120 | // If the unqualified types are the same, no conversion is necessary. |
2121 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2122 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2123 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2124 | SourceRange FromRange = From->getSourceRange(); |
2125 | SourceLocation FromLoc = FromRange.getBegin(); | ||||
2126 | |||||
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 2127 | ExprValueKind VK = From->getValueKind(); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2128 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2129 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2130 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2131 | // class name. |
2132 | // | ||||
2133 | // If the member was a qualified name and the qualified referred to a | ||||
2134 | // specific base subobject type, we'll cast to that intermediate type | ||||
2135 | // first and then to the object in which the member is declared. That allows | ||||
2136 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: | ||||
2137 | // | ||||
2138 | // class Base { public: int x; }; | ||||
2139 | // class Derived1 : public Base { }; | ||||
2140 | // class Derived2 : public Base { }; | ||||
2141 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; | ||||
2142 | // | ||||
2143 | // void VeryDerived::f() { | ||||
2144 | // x = 17; // error: ambiguous base subobjects | ||||
2145 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 | ||||
2146 | // } | ||||
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2147 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2148 | QualType QType = QualType(Qualifier->getAsType(), 0); |
2149 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); | ||||
2150 | assert(QType->isRecordType() && "lookup done with non-record type"); | ||||
2151 | |||||
2152 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); | ||||
2153 | |||||
2154 | // In C++98, the qualifier type doesn't actually have to be a base | ||||
2155 | // type of the object type, in which case we just ignore it. | ||||
2156 | // Otherwise build the appropriate casts. | ||||
2157 | if (IsDerivedFrom(FromRecordType, QRecordType)) { | ||||
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2158 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2159 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2160 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2161 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2162 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2163 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2164 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2165 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
2166 | VK, &BasePath).take(); | ||||
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2167 | |
2168 | FromType = QType; | ||||
2169 | FromRecordType = QRecordType; | ||||
2170 | |||||
2171 | // If the qualifier type was the same as the destination type, | ||||
2172 | // we're done. | ||||
2173 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2174 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2175 | } |
2176 | } | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2177 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2178 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2179 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2180 | // If we actually found the member through a using declaration, cast |
2181 | // down to the using declaration's type. | ||||
2182 | // | ||||
2183 | // Pointer equality is fine here because only one declaration of a | ||||
2184 | // class ever has member declarations. | ||||
2185 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { | ||||
2186 | assert(isa<UsingShadowDecl>(FoundDecl)); | ||||
2187 | QualType URecordType = Context.getTypeDeclType( | ||||
2188 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); | ||||
2189 | |||||
2190 | // We only need to do this if the naming-class to declaring-class | ||||
2191 | // conversion is non-trivial. | ||||
2192 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { | ||||
2193 | assert(IsDerivedFrom(FromRecordType, URecordType)); | ||||
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2194 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2195 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2196 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2197 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2198 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2199 | QualType UType = URecordType; |
2200 | if (PointerConversions) | ||||
2201 | UType = Context.getPointerType(UType); | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2202 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
2203 | VK, &BasePath).take(); | ||||
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2204 | FromType = UType; |
2205 | FromRecordType = URecordType; | ||||
2206 | } | ||||
2207 | |||||
2208 | // We don't do access control for the conversion from the | ||||
2209 | // declaring class to the true declaring class. | ||||
2210 | IgnoreAccess = true; | ||||
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2211 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2212 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2213 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2214 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
2215 | FromLoc, FromRange, &BasePath, | ||||
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2216 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2217 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2218 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2219 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
2220 | VK, &BasePath); | ||||
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2221 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2222 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2223 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2224 | const LookupResult &R, |
2225 | bool HasTrailingLParen) { | ||||
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2226 | // Only when used directly as the postfix-expression of a call. |
2227 | if (!HasTrailingLParen) | ||||
2228 | return false; | ||||
2229 | |||||
2230 | // Never if a scope specifier was provided. | ||||
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2231 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2232 | return false; |
2233 | |||||
2234 | // Only in C++ or ObjC++. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2235 | if (!getLangOpts().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2236 | return false; |
2237 | |||||
2238 | // Turn off ADL when we find certain kinds of declarations during | ||||
2239 | // normal lookup: | ||||
2240 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { | ||||
2241 | NamedDecl *D = *I; | ||||
2242 | |||||
2243 | // C++0x [basic.lookup.argdep]p3: | ||||
2244 | // -- a declaration of a class member | ||||
2245 | // Since using decls preserve this property, we check this on the | ||||
2246 | // original decl. | ||||
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2247 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2248 | return false; |
2249 | |||||
2250 | // C++0x [basic.lookup.argdep]p3: | ||||
2251 | // -- a block-scope function declaration that is not a | ||||
2252 | // using-declaration | ||||
2253 | // NOTE: we also trigger this for function templates (in fact, we | ||||
2254 | // don't check the decl type at all, since all other decl types | ||||
2255 | // turn off ADL anyway). | ||||
2256 | if (isa<UsingShadowDecl>(D)) | ||||
2257 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); | ||||
2258 | else if (D->getDeclContext()->isFunctionOrMethod()) | ||||
2259 | return false; | ||||
2260 | |||||
2261 | // C++0x [basic.lookup.argdep]p3: | ||||
2262 | // -- a declaration that is neither a function or a function | ||||
2263 | // template | ||||
2264 | // And also for builtin functions. | ||||
2265 | if (isa<FunctionDecl>(D)) { | ||||
2266 | FunctionDecl *FDecl = cast<FunctionDecl>(D); | ||||
2267 | |||||
2268 | // But also builtin functions. | ||||
2269 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) | ||||
2270 | return false; | ||||
2271 | } else if (!isa<FunctionTemplateDecl>(D)) | ||||
2272 | return false; | ||||
2273 | } | ||||
2274 | |||||
2275 | return true; | ||||
2276 | } | ||||
2277 | |||||
2278 | |||||
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2279 | /// Diagnoses obvious problems with the use of the given declaration |
2280 | /// as an expression. This is only actually called for lookups that | ||||
2281 | /// were not overloaded, and it doesn't promise that the declaration | ||||
2282 | /// will in fact be used. | ||||
2283 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { | ||||
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2284 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2285 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
2286 | return true; | ||||
2287 | } | ||||
2288 | |||||
2289 | if (isa<ObjCInterfaceDecl>(D)) { | ||||
2290 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); | ||||
2291 | return true; | ||||
2292 | } | ||||
2293 | |||||
2294 | if (isa<NamespaceDecl>(D)) { | ||||
2295 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); | ||||
2296 | return true; | ||||
2297 | } | ||||
2298 | |||||
2299 | return false; | ||||
2300 | } | ||||
2301 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2302 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2303 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2304 | LookupResult &R, |
2305 | bool NeedsADL) { | ||||
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2306 | // If this is a single, fully-resolved result and we don't need ADL, |
2307 | // just build an ordinary singleton decl ref. | ||||
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2308 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2309 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
2310 | R.getFoundDecl()); | ||||
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2311 | |
2312 | // We only need to check the declaration if there's exactly one | ||||
2313 | // result, because in the overloaded case the results can only be | ||||
2314 | // functions and function templates. | ||||
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2315 | if (R.isSingleResult() && |
2316 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) | ||||
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2317 | return ExprError(); |
2318 | |||||
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2319 | // Otherwise, just build an unresolved lookup expression. Suppress |
2320 | // any lookup-related diagnostics; we'll hash these out later, when | ||||
2321 | // we've picked a target. | ||||
2322 | R.suppressDiagnostics(); | ||||
2323 | |||||
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2324 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2325 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2326 | SS.getWithLocInContext(Context), |
2327 | R.getLookupNameInfo(), | ||||
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2328 | NeedsADL, R.isOverloadedResult(), |
2329 | R.begin(), R.end()); | ||||
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2330 | |
2331 | return Owned(ULE); | ||||
2332 | } | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2333 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2334 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2335 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2336 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2337 | const DeclarationNameInfo &NameInfo, |
2338 | NamedDecl *D) { | ||||
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2339 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2340 | assert(!isa<FunctionTemplateDecl>(D) && |
2341 | "Cannot refer unambiguously to a function template"); | ||||
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2342 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2343 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2344 | if (CheckDeclInExpr(*this, Loc, D)) |
2345 | return ExprError(); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2346 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2347 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
2348 | // Specifically diagnose references to class templates that are missing | ||||
2349 | // a template argument list. | ||||
2350 | Diag(Loc, diag::err_template_decl_ref) | ||||
2351 | << Template << SS.getRange(); | ||||
2352 | Diag(Template->getLocation(), diag::note_template_decl_here); | ||||
2353 | return ExprError(); | ||||
2354 | } | ||||
2355 | |||||
2356 | // Make sure that we're referring to a value. | ||||
2357 | ValueDecl *VD = dyn_cast<ValueDecl>(D); | ||||
2358 | if (!VD) { | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2359 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2360 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2361 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2362 | return ExprError(); |
2363 | } | ||||
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2364 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2365 | // Check whether this declaration can be used. Note that we suppress |
2366 | // this check when we're going to perform argument-dependent lookup | ||||
2367 | // on this function name, because this might not be the function | ||||
2368 | // that overload resolution actually selects. | ||||
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2369 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2370 | return ExprError(); |
2371 | |||||
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2372 | // Only create DeclRefExpr's for valid Decl's. |
2373 | if (VD->isInvalidDecl()) | ||||
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2374 | return ExprError(); |
2375 | |||||
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2376 | // Handle members of anonymous structs and unions. If we got here, |
2377 | // and the reference is to a class member indirect field, then this | ||||
2378 | // must be the subject of a pointer-to-member expression. | ||||
2379 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) | ||||
2380 | if (!indirectField->isCXXClassMember()) | ||||
2381 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), | ||||
2382 | indirectField); | ||||
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2383 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2384 | { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2385 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2386 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2387 | |
2388 | switch (D->getKind()) { | ||||
2389 | // Ignore all the non-ValueDecl kinds. | ||||
2390 | #define ABSTRACT_DECL(kind) | ||||
2391 | #define VALUE(type, base) | ||||
2392 | #define DECL(type, base) \ | ||||
2393 | case Decl::type: | ||||
2394 | #include "clang/AST/DeclNodes.inc" | ||||
2395 | llvm_unreachable("invalid value decl kind"); | ||||
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2396 | |
2397 | // These shouldn't make it here. | ||||
2398 | case Decl::ObjCAtDefsField: | ||||
2399 | case Decl::ObjCIvar: | ||||
2400 | llvm_unreachable("forming non-member reference to ivar?"); | ||||
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2401 | |
2402 | // Enum constants are always r-values and never references. | ||||
2403 | // Unresolved using declarations are dependent. | ||||
2404 | case Decl::EnumConstant: | ||||
2405 | case Decl::UnresolvedUsingValue: | ||||
2406 | valueKind = VK_RValue; | ||||
2407 | break; | ||||
2408 | |||||
2409 | // Fields and indirect fields that got here must be for | ||||
2410 | // pointer-to-member expressions; we just call them l-values for | ||||
2411 | // internal consistency, because this subexpression doesn't really | ||||
2412 | // exist in the high-level semantics. | ||||
2413 | case Decl::Field: | ||||
2414 | case Decl::IndirectField: | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2415 | assert(getLangOpts().CPlusPlus && |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2416 | "building reference to field in C?"); |
2417 | |||||
2418 | // These can't have reference type in well-formed programs, but | ||||
2419 | // for internal consistency we do this anyway. | ||||
2420 | type = type.getNonReferenceType(); | ||||
2421 | valueKind = VK_LValue; | ||||
2422 | break; | ||||
2423 | |||||
2424 | // Non-type template parameters are either l-values or r-values | ||||
2425 | // depending on the type. | ||||
2426 | case Decl::NonTypeTemplateParm: { | ||||
2427 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { | ||||
2428 | type = reftype->getPointeeType(); | ||||
2429 | valueKind = VK_LValue; // even if the parameter is an r-value reference | ||||
2430 | break; | ||||
2431 | } | ||||
2432 | |||||
2433 | // For non-references, we need to strip qualifiers just in case | ||||
2434 | // the template parameter was declared as 'const int' or whatever. | ||||
2435 | valueKind = VK_RValue; | ||||
2436 | type = type.getUnqualifiedType(); | ||||
2437 | break; | ||||
2438 | } | ||||
2439 | |||||
2440 | case Decl::Var: | ||||
2441 | // In C, "extern void blah;" is valid and is an r-value. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2442 | if (!getLangOpts().CPlusPlus && |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2443 | !type.hasQualifiers() && |
2444 | type->isVoidType()) { | ||||
2445 | valueKind = VK_RValue; | ||||
2446 | break; | ||||
2447 | } | ||||
2448 | // fallthrough | ||||
2449 | |||||
2450 | case Decl::ImplicitParam: | ||||
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2451 | case Decl::ParmVar: { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2452 | // These are always l-values. |
2453 | valueKind = VK_LValue; | ||||
2454 | type = type.getNonReferenceType(); | ||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2455 | |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2456 | // FIXME: Does the addition of const really only apply in |
2457 | // potentially-evaluated contexts? Since the variable isn't actually | ||||
2458 | // captured in an unevaluated context, it seems that the answer is no. | ||||
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 2459 | if (!isUnevaluatedContext()) { |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2460 | QualType CapturedType = getCapturedDeclRefType(cast<VarDecl>(VD), Loc); |
2461 | if (!CapturedType.isNull()) | ||||
2462 | type = CapturedType; | ||||
2463 | } | ||||
2464 | |||||
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2465 | break; |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2466 | } |
2467 | |||||
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2468 | case Decl::Function: { |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 2469 | if (unsigned BID = cast<FunctionDecl>(VD)->getBuiltinID()) { |
2470 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BID)) { | ||||
2471 | type = Context.BuiltinFnTy; | ||||
2472 | valueKind = VK_RValue; | ||||
2473 | break; | ||||
2474 | } | ||||
2475 | } | ||||
2476 | |||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2477 | const FunctionType *fty = type->castAs<FunctionType>(); |
2478 | |||||
2479 | // If we're referring to a function with an __unknown_anytype | ||||
2480 | // result type, make the entire expression __unknown_anytype. | ||||
2481 | if (fty->getResultType() == Context.UnknownAnyTy) { | ||||
2482 | type = Context.UnknownAnyTy; | ||||
2483 | valueKind = VK_RValue; | ||||
2484 | break; | ||||
2485 | } | ||||
2486 | |||||
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2487 | // Functions are l-values in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2488 | if (getLangOpts().CPlusPlus) { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2489 | valueKind = VK_LValue; |
2490 | break; | ||||
2491 | } | ||||
2492 | |||||
2493 | // C99 DR 316 says that, if a function type comes from a | ||||
2494 | // function definition (without a prototype), that type is only | ||||
2495 | // used for checking compatibility. Therefore, when referencing | ||||
2496 | // the function, we pretend that we don't have the full function | ||||
2497 | // type. | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2498 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
2499 | isa<FunctionProtoType>(fty)) | ||||
2500 | type = Context.getFunctionNoProtoType(fty->getResultType(), | ||||
2501 | fty->getExtInfo()); | ||||
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2502 | |
2503 | // Functions are r-values in C. | ||||
2504 | valueKind = VK_RValue; | ||||
2505 | break; | ||||
2506 | } | ||||
2507 | |||||
2508 | case Decl::CXXMethod: | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2509 | // If we're referring to a method with an __unknown_anytype |
2510 | // result type, make the entire expression __unknown_anytype. | ||||
2511 | // This should only be possible with a type written directly. | ||||
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 2512 | if (const FunctionProtoType *proto |
2513 | = dyn_cast<FunctionProtoType>(VD->getType())) | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2514 | if (proto->getResultType() == Context.UnknownAnyTy) { |
2515 | type = Context.UnknownAnyTy; | ||||
2516 | valueKind = VK_RValue; | ||||
2517 | break; | ||||
2518 | } | ||||
2519 | |||||
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2520 | // C++ methods are l-values if static, r-values if non-static. |
2521 | if (cast<CXXMethodDecl>(VD)->isStatic()) { | ||||
2522 | valueKind = VK_LValue; | ||||
2523 | break; | ||||
2524 | } | ||||
2525 | // fallthrough | ||||
2526 | |||||
2527 | case Decl::CXXConversion: | ||||
2528 | case Decl::CXXDestructor: | ||||
2529 | case Decl::CXXConstructor: | ||||
2530 | valueKind = VK_RValue; | ||||
2531 | break; | ||||
2532 | } | ||||
2533 | |||||
2534 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); | ||||
2535 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2536 | } |
2537 | |||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2538 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2539 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2540 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2541 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2542 | default: llvm_unreachable("Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2543 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
2544 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; | ||||
Nico Weber | 28ad063 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 2545 | case tok::kw_L__FUNCTION__: IT = PredefinedExpr::LFunction; break; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2546 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2547 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2548 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2549 | // Pre-defined identifiers are of type char[x], where x is the length of the |
2550 | // string. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2551 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2552 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2553 | if (!currentDecl && getCurBlock()) |
2554 | currentDecl = getCurBlock()->TheDecl; | ||||
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2555 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2556 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2557 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2558 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2559 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2560 | QualType ResTy; |
2561 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { | ||||
2562 | ResTy = Context.DependentTy; | ||||
2563 | } else { | ||||
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2564 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2565 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2566 | llvm::APInt LengthI(32, Length + 1); |
Nico Weber | d68615f | 2012-06-29 16:39:58 +0000 | [diff] [blame] | 2567 | if (IT == PredefinedExpr::LFunction) |
Nico Weber | 28ad063 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 2568 | ResTy = Context.WCharTy.withConst(); |
2569 | else | ||||
2570 | ResTy = Context.CharTy.withConst(); | ||||
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2571 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
2572 | } | ||||
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2573 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2574 | } |
2575 | |||||
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2576 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2577 | SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2578 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2579 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2580 | if (Invalid) |
2581 | return ExprError(); | ||||
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2582 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2583 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2584 | PP, Tok.getKind()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2585 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2586 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2587 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2588 | QualType Ty; |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2589 | if (Literal.isWide()) |
2590 | Ty = Context.WCharTy; // L'x' -> wchar_t in C and C++. | ||||
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2591 | else if (Literal.isUTF16()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2592 | Ty = Context.Char16Ty; // u'x' -> char16_t in C11 and C++11. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2593 | else if (Literal.isUTF32()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2594 | Ty = Context.Char32Ty; // U'x' -> char32_t in C11 and C++11. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2595 | else if (!getLangOpts().CPlusPlus || Literal.isMultiChar()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2596 | Ty = Context.IntTy; // 'x' -> int in C, 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2597 | else |
2598 | Ty = Context.CharTy; // 'x' -> char in C++ | ||||
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2599 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2600 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
2601 | if (Literal.isWide()) | ||||
2602 | Kind = CharacterLiteral::Wide; | ||||
2603 | else if (Literal.isUTF16()) | ||||
2604 | Kind = CharacterLiteral::UTF16; | ||||
2605 | else if (Literal.isUTF32()) | ||||
2606 | Kind = CharacterLiteral::UTF32; | ||||
2607 | |||||
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 2608 | Expr *Lit = new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
2609 | Tok.getLocation()); | ||||
2610 | |||||
2611 | if (Literal.getUDSuffix().empty()) | ||||
2612 | return Owned(Lit); | ||||
2613 | |||||
2614 | // We're building a user-defined literal. | ||||
2615 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); | ||||
2616 | SourceLocation UDSuffixLoc = | ||||
2617 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); | ||||
2618 | |||||
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2619 | // Make sure we're allowed user-defined literals here. |
2620 | if (!UDLScope) | ||||
2621 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_character_udl)); | ||||
2622 | |||||
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 2623 | // C++11 [lex.ext]p6: The literal L is treated as a call of the form |
2624 | // operator "" X (ch) | ||||
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2625 | return BuildCookedLiteralOperatorCall(*this, UDLScope, UDSuffix, UDSuffixLoc, |
2626 | llvm::makeArrayRef(&Lit, 1), | ||||
2627 | Tok.getLocation()); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2628 | } |
2629 | |||||
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2630 | ExprResult Sema::ActOnIntegerConstant(SourceLocation Loc, uint64_t Val) { |
2631 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); | ||||
2632 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val), | ||||
2633 | Context.IntTy, Loc)); | ||||
2634 | } | ||||
2635 | |||||
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2636 | static Expr *BuildFloatingLiteral(Sema &S, NumericLiteralParser &Literal, |
2637 | QualType Ty, SourceLocation Loc) { | ||||
2638 | const llvm::fltSemantics &Format = S.Context.getFloatTypeSemantics(Ty); | ||||
2639 | |||||
2640 | using llvm::APFloat; | ||||
2641 | APFloat Val(Format); | ||||
2642 | |||||
2643 | APFloat::opStatus result = Literal.GetFloatValue(Val); | ||||
2644 | |||||
2645 | // Overflow is always an error, but underflow is only an error if | ||||
2646 | // we underflowed to zero (APFloat reports denormals as underflow). | ||||
2647 | if ((result & APFloat::opOverflow) || | ||||
2648 | ((result & APFloat::opUnderflow) && Val.isZero())) { | ||||
2649 | unsigned diagnostic; | ||||
2650 | SmallString<20> buffer; | ||||
2651 | if (result & APFloat::opOverflow) { | ||||
2652 | diagnostic = diag::warn_float_overflow; | ||||
2653 | APFloat::getLargest(Format).toString(buffer); | ||||
2654 | } else { | ||||
2655 | diagnostic = diag::warn_float_underflow; | ||||
2656 | APFloat::getSmallest(Format).toString(buffer); | ||||
2657 | } | ||||
2658 | |||||
2659 | S.Diag(Loc, diagnostic) | ||||
2660 | << Ty | ||||
2661 | << StringRef(buffer.data(), buffer.size()); | ||||
2662 | } | ||||
2663 | |||||
2664 | bool isExact = (result == APFloat::opOK); | ||||
2665 | return FloatingLiteral::Create(S.Context, Val, isExact, Ty, Loc); | ||||
2666 | } | ||||
2667 | |||||
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2668 | ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2669 | // 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] | 2670 | // cannot have a trigraph, escaped newline, radix prefix, or suffix. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2671 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2672 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2673 | return ActOnIntegerConstant(Tok.getLocation(), Val-'0'); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2674 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2675 | |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2676 | SmallString<128> SpellingBuffer; |
2677 | // NumericLiteralParser wants to overread by one character. Add padding to | ||||
2678 | // the buffer in case the token is copied to the buffer. If getSpelling() | ||||
2679 | // returns a StringRef to the memory buffer, it should have a null char at | ||||
2680 | // the EOF, so it is also safe. | ||||
2681 | SpellingBuffer.resize(Tok.getLength() + 1); | ||||
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2682 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2683 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2684 | bool Invalid = false; |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2685 | StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2686 | if (Invalid) |
2687 | return ExprError(); | ||||
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2688 | |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2689 | NumericLiteralParser Literal(TokSpelling, Tok.getLocation(), PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2690 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2691 | return ExprError(); |
2692 | |||||
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2693 | if (Literal.hasUDSuffix()) { |
2694 | // We're building a user-defined literal. | ||||
2695 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); | ||||
2696 | SourceLocation UDSuffixLoc = | ||||
2697 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); | ||||
2698 | |||||
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2699 | // Make sure we're allowed user-defined literals here. |
2700 | if (!UDLScope) | ||||
2701 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_numeric_udl)); | ||||
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2702 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2703 | QualType CookedTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2704 | if (Literal.isFloatingLiteral()) { |
2705 | // C++11 [lex.ext]p4: If S contains a literal operator with parameter type | ||||
2706 | // long double, the literal is treated as a call of the form | ||||
2707 | // operator "" X (f L) | ||||
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2708 | CookedTy = Context.LongDoubleTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2709 | } else { |
2710 | // C++11 [lex.ext]p3: If S contains a literal operator with parameter type | ||||
2711 | // unsigned long long, the literal is treated as a call of the form | ||||
2712 | // operator "" X (n ULL) | ||||
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2713 | CookedTy = Context.UnsignedLongLongTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2714 | } |
2715 | |||||
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2716 | DeclarationName OpName = |
2717 | Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); | ||||
2718 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); | ||||
2719 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); | ||||
2720 | |||||
2721 | // Perform literal operator lookup to determine if we're building a raw | ||||
2722 | // literal or a cooked one. | ||||
2723 | LookupResult R(*this, OpName, UDSuffixLoc, LookupOrdinaryName); | ||||
2724 | switch (LookupLiteralOperator(UDLScope, R, llvm::makeArrayRef(&CookedTy, 1), | ||||
2725 | /*AllowRawAndTemplate*/true)) { | ||||
2726 | case LOLR_Error: | ||||
2727 | return ExprError(); | ||||
2728 | |||||
2729 | case LOLR_Cooked: { | ||||
2730 | Expr *Lit; | ||||
2731 | if (Literal.isFloatingLiteral()) { | ||||
2732 | Lit = BuildFloatingLiteral(*this, Literal, CookedTy, Tok.getLocation()); | ||||
2733 | } else { | ||||
2734 | llvm::APInt ResultVal(Context.getTargetInfo().getLongLongWidth(), 0); | ||||
2735 | if (Literal.GetIntegerValue(ResultVal)) | ||||
2736 | Diag(Tok.getLocation(), diag::warn_integer_too_large); | ||||
2737 | Lit = IntegerLiteral::Create(Context, ResultVal, CookedTy, | ||||
2738 | Tok.getLocation()); | ||||
2739 | } | ||||
2740 | return BuildLiteralOperatorCall(R, OpNameInfo, | ||||
2741 | llvm::makeArrayRef(&Lit, 1), | ||||
2742 | Tok.getLocation()); | ||||
2743 | } | ||||
2744 | |||||
2745 | case LOLR_Raw: { | ||||
2746 | // C++11 [lit.ext]p3, p4: If S contains a raw literal operator, the | ||||
2747 | // literal is treated as a call of the form | ||||
2748 | // operator "" X ("n") | ||||
2749 | SourceLocation TokLoc = Tok.getLocation(); | ||||
2750 | unsigned Length = Literal.getUDSuffixOffset(); | ||||
2751 | QualType StrTy = Context.getConstantArrayType( | ||||
2752 | Context.CharTy, llvm::APInt(32, Length + 1), | ||||
2753 | ArrayType::Normal, 0); | ||||
2754 | Expr *Lit = StringLiteral::Create( | ||||
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2755 | Context, StringRef(TokSpelling.data(), Length), StringLiteral::Ascii, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2756 | /*Pascal*/false, StrTy, &TokLoc, 1); |
2757 | return BuildLiteralOperatorCall(R, OpNameInfo, | ||||
2758 | llvm::makeArrayRef(&Lit, 1), TokLoc); | ||||
2759 | } | ||||
2760 | |||||
2761 | case LOLR_Template: | ||||
2762 | // C++11 [lit.ext]p3, p4: Otherwise (S contains a literal operator | ||||
2763 | // template), L is treated as a call fo the form | ||||
2764 | // operator "" X <'c1', 'c2', ... 'ck'>() | ||||
2765 | // where n is the source character sequence c1 c2 ... ck. | ||||
2766 | TemplateArgumentListInfo ExplicitArgs; | ||||
2767 | unsigned CharBits = Context.getIntWidth(Context.CharTy); | ||||
2768 | bool CharIsUnsigned = Context.CharTy->isUnsignedIntegerType(); | ||||
2769 | llvm::APSInt Value(CharBits, CharIsUnsigned); | ||||
2770 | for (unsigned I = 0, N = Literal.getUDSuffixOffset(); I != N; ++I) { | ||||
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 2771 | Value = TokSpelling[I]; |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2772 | TemplateArgument Arg(Context, Value, Context.CharTy); |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2773 | TemplateArgumentLocInfo ArgInfo; |
2774 | ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); | ||||
2775 | } | ||||
2776 | return BuildLiteralOperatorCall(R, OpNameInfo, ArrayRef<Expr*>(), | ||||
2777 | Tok.getLocation(), &ExplicitArgs); | ||||
2778 | } | ||||
2779 | |||||
2780 | llvm_unreachable("unexpected literal operator lookup result"); | ||||
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2781 | } |
2782 | |||||
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2783 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2784 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2785 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2786 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2787 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2788 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2789 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2790 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2791 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2792 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2793 | |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2794 | Res = BuildFloatingLiteral(*this, Literal, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2795 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2796 | if (Ty == Context.DoubleTy) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2797 | if (getLangOpts().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2798 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2799 | } else if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2800 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2801 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2802 | } |
2803 | } | ||||
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2804 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2805 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2806 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2807 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2808 | |
Dmitri Gribenko | e3b136b | 2012-09-24 18:19:21 +0000 | [diff] [blame] | 2809 | // 'long long' is a C99 or C++11 feature. |
2810 | if (!getLangOpts().C99 && Literal.isLongLong) { | ||||
2811 | if (getLangOpts().CPlusPlus) | ||||
2812 | Diag(Tok.getLocation(), | ||||
2813 | getLangOpts().CPlusPlus0x ? | ||||
2814 | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong); | ||||
2815 | else | ||||
2816 | Diag(Tok.getLocation(), diag::ext_c99_longlong); | ||||
2817 | } | ||||
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2818 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2819 | // Get the value in the widest-possible width. |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2820 | unsigned MaxWidth = Context.getTargetInfo().getIntMaxTWidth(); |
2821 | // The microsoft literal suffix extensions support 128-bit literals, which | ||||
2822 | // may be wider than [u]intmax_t. | ||||
2823 | if (Literal.isMicrosoftInteger && MaxWidth < 128) | ||||
2824 | MaxWidth = 128; | ||||
2825 | llvm::APInt ResultVal(MaxWidth, 0); | ||||
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2826 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2827 | if (Literal.GetIntegerValue(ResultVal)) { |
2828 | // If this value didn't fit into uintmax_t, warn and force to ull. | ||||
2829 | Diag(Tok.getLocation(), diag::warn_integer_too_large); | ||||
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2830 | Ty = Context.UnsignedLongLongTy; |
2831 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && | ||||
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2832 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2833 | } else { |
2834 | // If this value fits into a ULL, try to figure out what else it fits into | ||||
2835 | // according to the rules of C99 6.4.4.1p5. | ||||
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2836 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2837 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
2838 | // be an unsigned int. | ||||
2839 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; | ||||
2840 | |||||
2841 | // Check from smallest to largest, picking the smallest type we can. | ||||
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2842 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2843 | if (!Literal.isLong && !Literal.isLongLong) { |
2844 | // Are int/unsigned possibilities? | ||||
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2845 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2846 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2847 | // Does it fit in a unsigned int? |
2848 | if (ResultVal.isIntN(IntSize)) { | ||||
2849 | // Does it fit in a signed int? | ||||
2850 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) | ||||
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2851 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2852 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2853 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2854 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2855 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2856 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2857 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2858 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2859 | if (Ty.isNull() && !Literal.isLongLong) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2860 | unsigned LongSize = Context.getTargetInfo().getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2861 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2862 | // Does it fit in a unsigned long? |
2863 | if (ResultVal.isIntN(LongSize)) { | ||||
2864 | // Does it fit in a signed long? | ||||
2865 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) | ||||
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2866 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2867 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2868 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2869 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2870 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2871 | } |
2872 | |||||
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2873 | // Check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2874 | if (Ty.isNull()) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2875 | unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2876 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2877 | // Does it fit in a unsigned long long? |
2878 | if (ResultVal.isIntN(LongLongSize)) { | ||||
2879 | // Does it fit in a signed long long? | ||||
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2880 | // To be compatible with MSVC, hex integer literals ending with the |
2881 | // LL or i64 suffix are always signed in Microsoft mode. | ||||
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2882 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2883 | (getLangOpts().MicrosoftExt && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2884 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2885 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2886 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2887 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2888 | } |
2889 | } | ||||
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2890 | |
2891 | // If it doesn't fit in unsigned long long, and we're using Microsoft | ||||
2892 | // extensions, then its a 128-bit integer literal. | ||||
2893 | if (Ty.isNull() && Literal.isMicrosoftInteger) { | ||||
2894 | if (Literal.isUnsigned) | ||||
2895 | Ty = Context.UnsignedInt128Ty; | ||||
2896 | else | ||||
2897 | Ty = Context.Int128Ty; | ||||
2898 | Width = 128; | ||||
2899 | } | ||||
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2900 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2901 | // If we still couldn't decide a type, we probably have something that |
2902 | // 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] | 2903 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2904 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2905 | Ty = Context.UnsignedLongLongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2906 | Width = Context.getTargetInfo().getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2907 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2908 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2909 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2910 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2911 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2912 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2913 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2914 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2915 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
2916 | if (Literal.isImaginary) | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2918 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2919 | |
2920 | return Owned(Res); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2921 | } |
2922 | |||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2923 | ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2924 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2925 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2926 | } |
2927 | |||||
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2928 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
2929 | SourceLocation Loc, | ||||
2930 | SourceRange ArgRange) { | ||||
2931 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in | ||||
2932 | // scalar or vector data type argument..." | ||||
2933 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic | ||||
2934 | // type (C99 6.2.5p18) or void. | ||||
2935 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { | ||||
2936 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) | ||||
2937 | << T << ArgRange; | ||||
2938 | return true; | ||||
2939 | } | ||||
2940 | |||||
2941 | assert((T->isVoidType() || !T->isIncompleteType()) && | ||||
2942 | "Scalar types should always be complete"); | ||||
2943 | return false; | ||||
2944 | } | ||||
2945 | |||||
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2946 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
2947 | SourceLocation Loc, | ||||
2948 | SourceRange ArgRange, | ||||
2949 | UnaryExprOrTypeTrait TraitKind) { | ||||
2950 | // C99 6.5.3.4p1: | ||||
2951 | if (T->isFunctionType()) { | ||||
2952 | // alignof(function) is allowed as an extension. | ||||
2953 | if (TraitKind == UETT_SizeOf) | ||||
2954 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; | ||||
2955 | return false; | ||||
2956 | } | ||||
2957 | |||||
2958 | // Allow sizeof(void)/alignof(void) as an extension. | ||||
2959 | if (T->isVoidType()) { | ||||
2960 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; | ||||
2961 | return false; | ||||
2962 | } | ||||
2963 | |||||
2964 | return true; | ||||
2965 | } | ||||
2966 | |||||
2967 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, | ||||
2968 | SourceLocation Loc, | ||||
2969 | SourceRange ArgRange, | ||||
2970 | UnaryExprOrTypeTrait TraitKind) { | ||||
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 2971 | // Reject sizeof(interface) and sizeof(interface<proto>) if the |
2972 | // runtime doesn't allow it. | ||||
2973 | if (!S.LangOpts.ObjCRuntime.allowsSizeofAlignof() && T->isObjCObjectType()) { | ||||
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2974 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
2975 | << T << (TraitKind == UETT_SizeOf) | ||||
2976 | << ArgRange; | ||||
2977 | return true; | ||||
2978 | } | ||||
2979 | |||||
2980 | return false; | ||||
2981 | } | ||||
2982 | |||||
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2983 | /// \brief Check the constrains on expression operands to unary type expression |
2984 | /// and type traits. | ||||
2985 | /// | ||||
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2986 | /// Completes any types necessary and validates the constraints on the operand |
2987 | /// expression. The logic mostly mirrors the type-based overload, but may modify | ||||
2988 | /// the expression as it completes the type for that expression through template | ||||
2989 | /// instantiation, etc. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2990 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2991 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2992 | QualType ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2993 | |
2994 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, | ||||
2995 | // the result is the size of the referenced type." | ||||
2996 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the | ||||
2997 | // result shall be the alignment of the referenced type." | ||||
2998 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) | ||||
2999 | ExprTy = Ref->getPointeeType(); | ||||
3000 | |||||
3001 | if (ExprKind == UETT_VecStep) | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3002 | return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(), |
3003 | E->getSourceRange()); | ||||
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3004 | |
3005 | // Whitelist some types as extensions | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3006 | if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(), |
3007 | E->getSourceRange(), ExprKind)) | ||||
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3008 | return false; |
3009 | |||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3010 | if (RequireCompleteExprType(E, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3011 | diag::err_sizeof_alignof_incomplete_type, |
3012 | ExprKind, E->getSourceRange())) | ||||
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3013 | return true; |
3014 | |||||
3015 | // Completeing the expression's type may have changed it. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3016 | ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3017 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
3018 | ExprTy = Ref->getPointeeType(); | ||||
3019 | |||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3020 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), |
3021 | E->getSourceRange(), ExprKind)) | ||||
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3022 | return true; |
3023 | |||||
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3024 | if (ExprKind == UETT_SizeOf) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3025 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3026 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
3027 | QualType OType = PVD->getOriginalType(); | ||||
3028 | QualType Type = PVD->getType(); | ||||
3029 | if (Type->isPointerType() && OType->isArrayType()) { | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3030 | Diag(E->getExprLoc(), diag::warn_sizeof_array_param) |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3031 | << Type << OType; |
3032 | Diag(PVD->getLocation(), diag::note_declared_at); | ||||
3033 | } | ||||
3034 | } | ||||
3035 | } | ||||
3036 | } | ||||
3037 | |||||
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3038 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3039 | } |
3040 | |||||
3041 | /// \brief Check the constraints on operands to unary expression and type | ||||
3042 | /// traits. | ||||
3043 | /// | ||||
3044 | /// This will complete any types necessary, and validate the various constraints | ||||
3045 | /// on those operands. | ||||
3046 | /// | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3047 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3048 | /// C99 6.3.2.1p[2-4] all state: |
3049 | /// Except when it is the operand of the sizeof operator ... | ||||
3050 | /// | ||||
3051 | /// C++ [expr.sizeof]p4 | ||||
3052 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer | ||||
3053 | /// standard conversions are not applied to the operand of sizeof. | ||||
3054 | /// | ||||
3055 | /// This policy is followed for all of the unary trait expressions. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3056 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3057 | SourceLocation OpLoc, |
3058 | SourceRange ExprRange, | ||||
3059 | UnaryExprOrTypeTrait ExprKind) { | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3060 | if (ExprType->isDependentType()) |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3061 | return false; |
3062 | |||||
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3063 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
3064 | // the result is the size of the referenced type." | ||||
3065 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the | ||||
3066 | // result shall be the alignment of the referenced type." | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3067 | if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) |
3068 | ExprType = Ref->getPointeeType(); | ||||
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3069 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3070 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3071 | return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3072 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3073 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3074 | if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3075 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3076 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3078 | if (RequireCompleteType(OpLoc, ExprType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3079 | diag::err_sizeof_alignof_incomplete_type, |
3080 | ExprKind, ExprRange)) | ||||
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3081 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3083 | if (CheckObjCTraitOperandConstraints(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3084 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3085 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3086 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3087 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3088 | } |
3089 | |||||
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3090 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3091 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3092 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3093 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3094 | if (isa<DeclRefExpr>(E)) |
3095 | return false; | ||||
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3096 | |
3097 | // Cannot know anything else if the expression is dependent. | ||||
3098 | if (E->isTypeDependent()) | ||||
3099 | return false; | ||||
3100 | |||||
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3101 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3102 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
3103 | << 1 << E->getSourceRange(); | ||||
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3104 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3105 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3106 | |
3107 | // Alignment of a field access is always okay, so long as it isn't a | ||||
3108 | // bit-field. | ||||
3109 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) | ||||
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3110 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3111 | return false; |
3112 | |||||
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3113 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3114 | } |
3115 | |||||
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3116 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3117 | E = E->IgnoreParens(); |
3118 | |||||
3119 | // Cannot know anything else if the expression is dependent. | ||||
3120 | if (E->isTypeDependent()) | ||||
3121 | return false; | ||||
3122 | |||||
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3123 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3124 | } |
3125 | |||||
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3126 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3127 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3128 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
3129 | SourceLocation OpLoc, | ||||
3130 | UnaryExprOrTypeTrait ExprKind, | ||||
3131 | SourceRange R) { | ||||
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3132 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3133 | return ExprError(); |
3134 | |||||
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3135 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 3136 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3137 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3138 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3139 | return ExprError(); |
3140 | |||||
3141 | // 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] | 3142 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
3143 | Context.getSizeType(), | ||||
3144 | OpLoc, R.getEnd())); | ||||
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3145 | } |
3146 | |||||
3147 | /// \brief Build a sizeof or alignof expression given an expression | ||||
3148 | /// operand. | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3149 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3150 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
3151 | UnaryExprOrTypeTrait ExprKind) { | ||||
Douglas Gregor | 4f0845e | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 3152 | ExprResult PE = CheckPlaceholderExpr(E); |
3153 | if (PE.isInvalid()) | ||||
3154 | return ExprError(); | ||||
3155 | |||||
3156 | E = PE.get(); | ||||
3157 | |||||
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3158 | // Verify that the operand is valid. |
3159 | bool isInvalid = false; | ||||
3160 | if (E->isTypeDependent()) { | ||||
3161 | // Delay type-checking for type-dependent expressions. | ||||
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3162 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3163 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3164 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3165 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3166 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3167 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3168 | isInvalid = true; |
3169 | } else { | ||||
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3170 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3171 | } |
3172 | |||||
3173 | if (isInvalid) | ||||
3174 | return ExprError(); | ||||
3175 | |||||
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 3176 | if (ExprKind == UETT_SizeOf && E->getType()->isVariableArrayType()) { |
3177 | PE = TranformToPotentiallyEvaluated(E); | ||||
3178 | if (PE.isInvalid()) return ExprError(); | ||||
3179 | E = PE.take(); | ||||
3180 | } | ||||
3181 | |||||
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3182 | // 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] | 3183 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3184 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3185 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3186 | } |
3187 | |||||
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3188 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
3189 | /// expr and the same for @c alignof and @c __alignof | ||||
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3190 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3191 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3192 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3193 | UnaryExprOrTypeTrait ExprKind, bool IsType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3194 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3195 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3196 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3197 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3198 | if (IsType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3199 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3200 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3201 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3202 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3203 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3204 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3205 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3206 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3207 | } |
3208 | |||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3209 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3210 | bool IsReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3211 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3212 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3213 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3214 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3215 | if (V.get()->getObjectKind() != OK_Ordinary) { |
3216 | V = S.DefaultLvalueConversion(V.take()); | ||||
3217 | if (V.isInvalid()) | ||||
3218 | return QualType(); | ||||
3219 | } | ||||
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3220 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3221 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3222 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3223 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3225 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3226 | if (V.get()->getType()->isArithmeticType()) |
3227 | return V.get()->getType(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3228 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3229 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3230 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3231 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3232 | if (PR.get() != V.get()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3233 | V = PR; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3234 | return CheckRealImagOperand(S, V, Loc, IsReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3235 | } |
3236 | |||||
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3237 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3238 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3239 | << (IsReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3240 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3241 | } |
3242 | |||||
3243 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3244 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3245 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3246 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3247 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3248 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3249 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3250 | default: llvm_unreachable("Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3251 | case tok::plusplus: Opc = UO_PostInc; break; |
3252 | case tok::minusminus: Opc = UO_PostDec; break; | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3253 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3254 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3255 | // Since this might is a postfix expression, get rid of ParenListExprs. |
3256 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Input); | ||||
3257 | if (Result.isInvalid()) return ExprError(); | ||||
3258 | Input = Result.take(); | ||||
3259 | |||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3260 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3261 | } |
3262 | |||||
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3263 | /// \brief Diagnose if arithmetic on the given ObjC pointer is illegal. |
3264 | /// | ||||
3265 | /// \return true on error | ||||
3266 | static bool checkArithmeticOnObjCPointer(Sema &S, | ||||
3267 | SourceLocation opLoc, | ||||
3268 | Expr *op) { | ||||
3269 | assert(op->getType()->isObjCObjectPointerType()); | ||||
3270 | if (S.LangOpts.ObjCRuntime.allowsPointerArithmetic()) | ||||
3271 | return false; | ||||
3272 | |||||
3273 | S.Diag(opLoc, diag::err_arithmetic_nonfragile_interface) | ||||
3274 | << op->getType()->castAs<ObjCObjectPointerType>()->getPointeeType() | ||||
3275 | << op->getSourceRange(); | ||||
3276 | return true; | ||||
3277 | } | ||||
3278 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3279 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3280 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
3281 | Expr *Idx, SourceLocation RLoc) { | ||||
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3282 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3283 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3284 | if (Result.isInvalid()) return ExprError(); |
3285 | Base = Result.take(); | ||||
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3286 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3287 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3288 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3289 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3290 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3291 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3292 | Context.DependentTy, |
3293 | VK_LValue, OK_Ordinary, | ||||
3294 | RLoc)); | ||||
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3295 | } |
3296 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3297 | if (getLangOpts().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3298 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3299 | LHSExp->getType()->isEnumeralType() || |
3300 | RHSExp->getType()->isRecordType() || | ||||
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3301 | RHSExp->getType()->isEnumeralType()) && |
3302 | !LHSExp->getType()->isObjCObjectPointerType()) { | ||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3303 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3304 | } |
3305 | |||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3306 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3307 | } |
3308 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3309 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3310 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3311 | Expr *Idx, SourceLocation RLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3312 | Expr *LHSExp = Base; |
3313 | Expr *RHSExp = Idx; | ||||
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3314 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3315 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3316 | if (!LHSExp->getType()->getAs<VectorType>()) { |
3317 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); | ||||
3318 | if (Result.isInvalid()) | ||||
3319 | return ExprError(); | ||||
3320 | LHSExp = Result.take(); | ||||
3321 | } | ||||
3322 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); | ||||
3323 | if (Result.isInvalid()) | ||||
3324 | return ExprError(); | ||||
3325 | RHSExp = Result.take(); | ||||
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3326 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3327 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3328 | ExprValueKind VK = VK_LValue; |
3329 | ExprObjectKind OK = OK_Ordinary; | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3330 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3331 | // 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] | 3332 | // 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] | 3333 | // 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] | 3334 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3335 | Expr *BaseExpr, *IndexExpr; |
3336 | QualType ResultType; | ||||
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3337 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
3338 | BaseExpr = LHSExp; | ||||
3339 | IndexExpr = RHSExp; | ||||
3340 | ResultType = Context.DependentTy; | ||||
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3341 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3342 | BaseExpr = LHSExp; |
3343 | IndexExpr = RHSExp; | ||||
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3344 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3345 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3346 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3347 | BaseExpr = LHSExp; |
3348 | IndexExpr = RHSExp; | ||||
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3349 | |
3350 | // Use custom logic if this should be the pseudo-object subscript | ||||
3351 | // expression. | ||||
3352 | if (!LangOpts.ObjCRuntime.isSubscriptPointerArithmetic()) | ||||
3353 | return BuildObjCSubscriptExpression(RLoc, BaseExpr, IndexExpr, 0, 0); | ||||
3354 | |||||
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3355 | ResultType = PTy->getPointeeType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3356 | if (!LangOpts.ObjCRuntime.allowsPointerArithmetic()) { |
3357 | Diag(LLoc, diag::err_subscript_nonfragile_interface) | ||||
3358 | << ResultType << BaseExpr->getSourceRange(); | ||||
3359 | return ExprError(); | ||||
3360 | } | ||||
Fariborz Jahanian | a78eca2 | 2012-03-28 17:56:49 +0000 | [diff] [blame] | 3361 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
3362 | // Handle the uncommon case of "123[Ptr]". | ||||
3363 | BaseExpr = RHSExp; | ||||
3364 | IndexExpr = LHSExp; | ||||
3365 | ResultType = PTy->getPointeeType(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3366 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3367 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3368 | // Handle the uncommon case of "123[Ptr]". |
3369 | BaseExpr = RHSExp; | ||||
3370 | IndexExpr = LHSExp; | ||||
3371 | ResultType = PTy->getPointeeType(); | ||||
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3372 | if (!LangOpts.ObjCRuntime.allowsPointerArithmetic()) { |
3373 | Diag(LLoc, diag::err_subscript_nonfragile_interface) | ||||
3374 | << ResultType << BaseExpr->getSourceRange(); | ||||
3375 | return ExprError(); | ||||
3376 | } | ||||
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3377 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3378 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3379 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3380 | VK = LHSExp->getValueKind(); |
3381 | if (VK != VK_RValue) | ||||
3382 | OK = OK_VectorComponent; | ||||
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3383 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3384 | // FIXME: need to deal with const... |
3385 | ResultType = VTy->getElementType(); | ||||
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3386 | } else if (LHSTy->isArrayType()) { |
3387 | // If we see an array that wasn't promoted by | ||||
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3388 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3389 | // wasn't promoted because of the C90 rule that doesn't |
3390 | // allow promoting non-lvalue arrays. Warn, then | ||||
3391 | // force the promotion here. | ||||
3392 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << | ||||
3393 | LHSExp->getSourceRange(); | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3394 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
3395 | CK_ArrayToPointerDecay).take(); | ||||
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3396 | LHSTy = LHSExp->getType(); |
3397 | |||||
3398 | BaseExpr = LHSExp; | ||||
3399 | IndexExpr = RHSExp; | ||||
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3400 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3401 | } else if (RHSTy->isArrayType()) { |
3402 | // Same as previous, except for 123[f().a] case | ||||
3403 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << | ||||
3404 | RHSExp->getSourceRange(); | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3405 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
3406 | CK_ArrayToPointerDecay).take(); | ||||
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3407 | RHSTy = RHSExp->getType(); |
3408 | |||||
3409 | BaseExpr = RHSExp; | ||||
3410 | IndexExpr = LHSExp; | ||||
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3411 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3412 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3413 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
3414 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); | ||||
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3415 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3416 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3417 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3418 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
3419 | << IndexExpr->getSourceRange()); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3420 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3421 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3422 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
3423 | && !IndexExpr->isTypeDependent()) | ||||
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3424 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
3425 | |||||
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3426 | // 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] | 3427 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
3428 | // type. Note that Functions are not objects, and that (in C99 parlance) | ||||
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3429 | // incomplete types are not object types. |
3430 | if (ResultType->isFunctionType()) { | ||||
3431 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) | ||||
3432 | << ResultType << BaseExpr->getSourceRange(); | ||||
3433 | return ExprError(); | ||||
3434 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3436 | if (ResultType->isVoidType() && !getLangOpts().CPlusPlus) { |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3437 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 6628969 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3438 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
3439 | << BaseExpr->getSourceRange(); | ||||
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3440 | |
3441 | // C forbids expressions of unqualified void type from being l-values. | ||||
3442 | // See IsCForbiddenLValueType. | ||||
3443 | if (!ResultType.hasQualifiers()) VK = VK_RValue; | ||||
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3444 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3445 | RequireCompleteType(LLoc, ResultType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3446 | diag::err_subscript_incomplete_type, BaseExpr)) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3447 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3448 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3449 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3450 | !ResultType.isCForbiddenLValueType()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3451 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3452 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3453 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3454 | } |
3455 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3456 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3457 | FunctionDecl *FD, |
3458 | ParmVarDecl *Param) { | ||||
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3459 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3460 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3461 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3462 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3463 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3464 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3465 | return ExprError(); |
3466 | } | ||||
3467 | |||||
3468 | if (Param->hasUninstantiatedDefaultArg()) { | ||||
3469 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); | ||||
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3470 | |
Richard Smith | adb1d4c | 2012-07-22 23:45:10 +0000 | [diff] [blame] | 3471 | EnterExpressionEvaluationContext EvalContext(*this, PotentiallyEvaluated, |
3472 | Param); | ||||
3473 | |||||
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3474 | // Instantiate the expression. |
3475 | MultiLevelTemplateArgumentList ArgList | ||||
3476 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); | ||||
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3477 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3478 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3479 | = ArgList.getInnermost(); |
Richard Smith | 7e54fb5 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3480 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
3481 | ArrayRef<TemplateArgument>(Innermost.first, | ||||
3482 | Innermost.second)); | ||||
Richard Smith | ab91ef1 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3483 | if (Inst) |
3484 | return ExprError(); | ||||
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3485 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3486 | ExprResult Result; |
3487 | { | ||||
3488 | // C++ [dcl.fct.default]p5: | ||||
3489 | // The names in the [default argument] expression are bound, and | ||||
3490 | // the semantic constraints are checked, at the point where the | ||||
3491 | // default argument expression appears. | ||||
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3492 | ContextRAII SavedContext(*this, FD); |
Douglas Gregor | 7bdc152 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 3493 | LocalInstantiationScope Local(*this); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3494 | Result = SubstExpr(UninstExpr, ArgList); |
3495 | } | ||||
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3496 | if (Result.isInvalid()) |
3497 | return ExprError(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3499 | // Check the expression as an initializer for the parameter. |
3500 | InitializedEntity Entity | ||||
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3501 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3502 | InitializationKind Kind |
3503 | = InitializationKind::CreateCopy(Param->getLocation(), | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3504 | /*FIXME:EqualLoc*/UninstExpr->getLocStart()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3505 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3507 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3508 | Result = InitSeq.Perform(*this, Entity, Kind, ResultE); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3509 | if (Result.isInvalid()) |
3510 | return ExprError(); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3511 | |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 3512 | Expr *Arg = Result.takeAs<Expr>(); |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 3513 | CheckImplicitConversions(Arg, Param->getOuterLocStart()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3514 | // Build the default argument expression. |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 3515 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, Arg)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3516 | } |
3517 | |||||
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3518 | // If the default expression creates temporaries, we need to |
3519 | // push them to the current stack of expression temporaries so they'll | ||||
3520 | // be properly destroyed. | ||||
3521 | // FIXME: We should really be rebuilding the default argument with new | ||||
3522 | // bound temporaries; see the comment in PR5810. | ||||
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3523 | // We don't need to do that with block decls, though, because |
3524 | // blocks in default argument expression can never capture anything. | ||||
3525 | if (isa<ExprWithCleanups>(Param->getInit())) { | ||||
3526 | // Set the "needs cleanups" bit regardless of whether there are | ||||
3527 | // any explicit objects. | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3528 | ExprNeedsCleanups = true; |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3529 | |
3530 | // Append all the objects to the cleanup list. Right now, this | ||||
3531 | // should always be a no-op, because blocks in default argument | ||||
3532 | // expressions should never be able to capture anything. | ||||
3533 | assert(!cast<ExprWithCleanups>(Param->getInit())->getNumObjects() && | ||||
3534 | "default argument expression has capturing blocks?"); | ||||
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3535 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3536 | |
3537 | // We already type-checked the argument, so we know it works. | ||||
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3538 | // Just mark all of the declarations in this potentially-evaluated expression |
3539 | // as being "referenced". | ||||
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 3540 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg(), |
3541 | /*SkipLocalVariables=*/true); | ||||
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3542 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3543 | } |
3544 | |||||
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3545 | |
3546 | Sema::VariadicCallType | ||||
3547 | Sema::getVariadicCallType(FunctionDecl *FDecl, const FunctionProtoType *Proto, | ||||
3548 | Expr *Fn) { | ||||
3549 | if (Proto && Proto->isVariadic()) { | ||||
3550 | if (dyn_cast_or_null<CXXConstructorDecl>(FDecl)) | ||||
3551 | return VariadicConstructor; | ||||
3552 | else if (Fn && Fn->getType()->isBlockPointerType()) | ||||
3553 | return VariadicBlock; | ||||
3554 | else if (FDecl) { | ||||
3555 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) | ||||
3556 | if (Method->isInstance()) | ||||
3557 | return VariadicMethod; | ||||
3558 | } | ||||
3559 | return VariadicFunction; | ||||
3560 | } | ||||
3561 | return VariadicDoesNotApply; | ||||
3562 | } | ||||
3563 | |||||
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3564 | /// ConvertArgumentsForCall - Converts the arguments specified in |
3565 | /// Args/NumArgs to the parameter types of the function FDecl with | ||||
3566 | /// function prototype Proto. Call is the call expression itself, and | ||||
3567 | /// Fn is the function expression. For a C++ member function, this | ||||
3568 | /// routine does not attempt to convert the object argument. Returns | ||||
3569 | /// true if the call is ill-formed. | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3570 | bool |
3571 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, | ||||
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3572 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3573 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3574 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3575 | SourceLocation RParenLoc, |
3576 | bool IsExecConfig) { | ||||
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3577 | // Bail out early if calling a builtin with custom typechecking. |
3578 | // We don't need to do this in the | ||||
3579 | if (FDecl) | ||||
3580 | if (unsigned ID = FDecl->getBuiltinID()) | ||||
3581 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) | ||||
3582 | return false; | ||||
3583 | |||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3584 | // 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] | 3585 | // assignment, to the types of the corresponding parameter, ... |
3586 | unsigned NumArgsInProto = Proto->getNumArgs(); | ||||
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3587 | bool Invalid = false; |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3588 | unsigned MinArgs = FDecl ? FDecl->getMinRequiredArguments() : NumArgsInProto; |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3589 | unsigned FnKind = Fn->getType()->isBlockPointerType() |
3590 | ? 1 /* block */ | ||||
3591 | : (IsExecConfig ? 3 /* kernel function (exec config) */ | ||||
3592 | : 0 /* function */); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3593 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3594 | // If too few arguments are available (and we don't have default |
3595 | // arguments for the remaining parameters), don't make the call. | ||||
3596 | if (NumArgs < NumArgsInProto) { | ||||
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3597 | if (NumArgs < MinArgs) { |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 3598 | if (MinArgs == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) |
3599 | Diag(RParenLoc, MinArgs == NumArgsInProto && !Proto->isVariadic() | ||||
3600 | ? diag::err_typecheck_call_too_few_args_one | ||||
3601 | : diag::err_typecheck_call_too_few_args_at_least_one) | ||||
3602 | << FnKind | ||||
3603 | << FDecl->getParamDecl(0) << Fn->getSourceRange(); | ||||
3604 | else | ||||
3605 | Diag(RParenLoc, MinArgs == NumArgsInProto && !Proto->isVariadic() | ||||
3606 | ? diag::err_typecheck_call_too_few_args | ||||
3607 | : diag::err_typecheck_call_too_few_args_at_least) | ||||
3608 | << FnKind | ||||
3609 | << MinArgs << NumArgs << Fn->getSourceRange(); | ||||
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3610 | |
3611 | // Emit the location of the prototype. | ||||
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3612 | if (FDecl && !FDecl->getBuiltinID() && !IsExecConfig) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3613 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
3614 | << FDecl; | ||||
3615 | |||||
3616 | return true; | ||||
3617 | } | ||||
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3618 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3619 | } |
3620 | |||||
3621 | // If too many are passed and not variadic, error on the extras and drop | ||||
3622 | // them. | ||||
3623 | if (NumArgs > NumArgsInProto) { | ||||
3624 | if (!Proto->isVariadic()) { | ||||
Richard Smith | c608c3c | 2012-05-15 06:21:54 +0000 | [diff] [blame] | 3625 | if (NumArgsInProto == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) |
3626 | Diag(Args[NumArgsInProto]->getLocStart(), | ||||
3627 | MinArgs == NumArgsInProto | ||||
3628 | ? diag::err_typecheck_call_too_many_args_one | ||||
3629 | : diag::err_typecheck_call_too_many_args_at_most_one) | ||||
3630 | << FnKind | ||||
3631 | << FDecl->getParamDecl(0) << NumArgs << Fn->getSourceRange() | ||||
3632 | << SourceRange(Args[NumArgsInProto]->getLocStart(), | ||||
3633 | Args[NumArgs-1]->getLocEnd()); | ||||
3634 | else | ||||
3635 | Diag(Args[NumArgsInProto]->getLocStart(), | ||||
3636 | MinArgs == NumArgsInProto | ||||
3637 | ? diag::err_typecheck_call_too_many_args | ||||
3638 | : diag::err_typecheck_call_too_many_args_at_most) | ||||
3639 | << FnKind | ||||
3640 | << NumArgsInProto << NumArgs << Fn->getSourceRange() | ||||
3641 | << SourceRange(Args[NumArgsInProto]->getLocStart(), | ||||
3642 | Args[NumArgs-1]->getLocEnd()); | ||||
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3643 | |
3644 | // Emit the location of the prototype. | ||||
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3645 | if (FDecl && !FDecl->getBuiltinID() && !IsExecConfig) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3646 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
3647 | << FDecl; | ||||
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3648 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3649 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3650 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3651 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3652 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3653 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3654 | SmallVector<Expr *, 8> AllArgs; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3655 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, Fn); |
3656 | |||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3657 | Invalid = GatherArgumentsForCall(Call->getLocStart(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3658 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3659 | if (Invalid) |
3660 | return true; | ||||
3661 | unsigned TotalNumArgs = AllArgs.size(); | ||||
3662 | for (unsigned i = 0; i < TotalNumArgs; ++i) | ||||
3663 | Call->setArg(i, AllArgs[i]); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3664 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3665 | return false; |
3666 | } | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3667 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3668 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
3669 | FunctionDecl *FDecl, | ||||
3670 | const FunctionProtoType *Proto, | ||||
3671 | unsigned FirstProtoArg, | ||||
3672 | Expr **Args, unsigned NumArgs, | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3673 | SmallVector<Expr *, 8> &AllArgs, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3674 | VariadicCallType CallType, |
3675 | bool AllowExplicit) { | ||||
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3676 | unsigned NumArgsInProto = Proto->getNumArgs(); |
3677 | unsigned NumArgsToCheck = NumArgs; | ||||
3678 | bool Invalid = false; | ||||
3679 | if (NumArgs != NumArgsInProto) | ||||
3680 | // Use default arguments for missing arguments | ||||
3681 | NumArgsToCheck = NumArgsInProto; | ||||
3682 | unsigned ArgIx = 0; | ||||
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3683 | // 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] | 3684 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3685 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3687 | Expr *Arg; |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3688 | ParmVarDecl *Param; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3689 | if (ArgIx < NumArgs) { |
3690 | Arg = Args[ArgIx++]; | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3691 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3692 | if (RequireCompleteType(Arg->getLocStart(), |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3693 | ProtoArgType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3694 | diag::err_call_incomplete_argument, Arg)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3695 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3696 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3697 | // Pass the argument |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3698 | Param = 0; |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3699 | if (FDecl && i < FDecl->getNumParams()) |
3700 | Param = FDecl->getParamDecl(i); | ||||
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3701 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3702 | // Strip the unbridged-cast placeholder expression off, if applicable. |
3703 | if (Arg->getType() == Context.ARCUnbridgedCastTy && | ||||
3704 | FDecl && FDecl->hasAttr<CFAuditedTransferAttr>() && | ||||
3705 | (!Param || !Param->hasAttr<CFConsumedAttr>())) | ||||
3706 | Arg = stripARCUnbridgedCast(Arg); | ||||
3707 | |||||
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3708 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3709 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3710 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
3711 | Proto->isArgConsumed(i)); | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3712 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3713 | SourceLocation(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3714 | Owned(Arg), |
3715 | /*TopLevelOfInitList=*/false, | ||||
3716 | AllowExplicit); | ||||
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3717 | if (ArgE.isInvalid()) |
3718 | return true; | ||||
3719 | |||||
3720 | Arg = ArgE.takeAs<Expr>(); | ||||
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3721 | } else { |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3722 | Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3723 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3724 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3725 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3726 | if (ArgExpr.isInvalid()) |
3727 | return true; | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3728 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3729 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3730 | } |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3731 | |
3732 | // Check for array bounds violations for each argument to the call. This | ||||
3733 | // check only triggers warnings when the argument isn't a more complex Expr | ||||
3734 | // with its own checking, such as a BinaryOperator. | ||||
3735 | CheckArrayAccess(Arg); | ||||
3736 | |||||
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3737 | // Check for violations of C99 static array rules (C99 6.7.5.3p7). |
3738 | CheckStaticArrayArgument(CallLoc, Param, Arg); | ||||
3739 | |||||
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3740 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3741 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3742 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3743 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3744 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3745 | // Assume that extern "C" functions with variadic arguments that |
3746 | // return __unknown_anytype aren't *really* variadic. | ||||
3747 | if (Proto->getResultType() == Context.UnknownAnyTy && | ||||
3748 | FDecl && FDecl->isExternC()) { | ||||
3749 | for (unsigned i = ArgIx; i != NumArgs; ++i) { | ||||
3750 | ExprResult arg; | ||||
3751 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) | ||||
3752 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); | ||||
3753 | else | ||||
3754 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); | ||||
3755 | Invalid |= arg.isInvalid(); | ||||
3756 | AllArgs.push_back(arg.take()); | ||||
3757 | } | ||||
3758 | |||||
3759 | // Otherwise do argument promotion, (C99 6.5.2.2p7). | ||||
3760 | } else { | ||||
3761 | for (unsigned i = ArgIx; i != NumArgs; ++i) { | ||||
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3762 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, |
3763 | FDecl); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3764 | Invalid |= Arg.isInvalid(); |
3765 | AllArgs.push_back(Arg.take()); | ||||
3766 | } | ||||
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3767 | } |
Ted Kremenek | 615eb7c | 2011-09-26 23:36:13 +0000 | [diff] [blame] | 3768 | |
3769 | // Check for array bounds violations. | ||||
3770 | for (unsigned i = ArgIx; i != NumArgs; ++i) | ||||
3771 | CheckArrayAccess(Args[i]); | ||||
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3772 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3773 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3774 | } |
3775 | |||||
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3776 | static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) { |
3777 | TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc(); | ||||
3778 | if (ArrayTypeLoc *ATL = dyn_cast<ArrayTypeLoc>(&TL)) | ||||
3779 | S.Diag(PVD->getLocation(), diag::note_callee_static_array) | ||||
3780 | << ATL->getLocalSourceRange(); | ||||
3781 | } | ||||
3782 | |||||
3783 | /// CheckStaticArrayArgument - If the given argument corresponds to a static | ||||
3784 | /// array parameter, check that it is non-null, and that if it is formed by | ||||
3785 | /// array-to-pointer decay, the underlying array is sufficiently large. | ||||
3786 | /// | ||||
3787 | /// C99 6.7.5.3p7: If the keyword static also appears within the [ and ] of the | ||||
3788 | /// array type derivation, then for each call to the function, the value of the | ||||
3789 | /// corresponding actual argument shall provide access to the first element of | ||||
3790 | /// an array with at least as many elements as specified by the size expression. | ||||
3791 | void | ||||
3792 | Sema::CheckStaticArrayArgument(SourceLocation CallLoc, | ||||
3793 | ParmVarDecl *Param, | ||||
3794 | const Expr *ArgExpr) { | ||||
3795 | // Static array parameters are not supported in C++. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3796 | if (!Param || getLangOpts().CPlusPlus) |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3797 | return; |
3798 | |||||
3799 | QualType OrigTy = Param->getOriginalType(); | ||||
3800 | |||||
3801 | const ArrayType *AT = Context.getAsArrayType(OrigTy); | ||||
3802 | if (!AT || AT->getSizeModifier() != ArrayType::Static) | ||||
3803 | return; | ||||
3804 | |||||
3805 | if (ArgExpr->isNullPointerConstant(Context, | ||||
3806 | Expr::NPC_NeverValueDependent)) { | ||||
3807 | Diag(CallLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); | ||||
3808 | DiagnoseCalleeStaticArrayParam(*this, Param); | ||||
3809 | return; | ||||
3810 | } | ||||
3811 | |||||
3812 | const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT); | ||||
3813 | if (!CAT) | ||||
3814 | return; | ||||
3815 | |||||
3816 | const ConstantArrayType *ArgCAT = | ||||
3817 | Context.getAsConstantArrayType(ArgExpr->IgnoreParenImpCasts()->getType()); | ||||
3818 | if (!ArgCAT) | ||||
3819 | return; | ||||
3820 | |||||
3821 | if (ArgCAT->getSize().ult(CAT->getSize())) { | ||||
3822 | Diag(CallLoc, diag::warn_static_array_too_small) | ||||
3823 | << ArgExpr->getSourceRange() | ||||
3824 | << (unsigned) ArgCAT->getSize().getZExtValue() | ||||
3825 | << (unsigned) CAT->getSize().getZExtValue(); | ||||
3826 | DiagnoseCalleeStaticArrayParam(*this, Param); | ||||
3827 | } | ||||
3828 | } | ||||
3829 | |||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3830 | /// Given a function expression of unknown-any type, try to rebuild it |
3831 | /// to have a function type. | ||||
3832 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); | ||||
3833 | |||||
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3834 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3835 | /// This provides the location of the left/right parens and a list of comma |
3836 | /// locations. | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3837 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3838 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3839 | MultiExprArg ArgExprs, SourceLocation RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3840 | Expr *ExecConfig, bool IsExecConfig) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3841 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3842 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3843 | if (Result.isInvalid()) return ExprError(); |
3844 | Fn = Result.take(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3845 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3846 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3847 | // If this is a pseudo-destructor expression, build the call immediately. |
3848 | if (isa<CXXPseudoDestructorExpr>(Fn)) { | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3849 | if (!ArgExprs.empty()) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3850 | // Pseudo-destructor calls should not have any arguments. |
3851 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) | ||||
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3852 | << FixItHint::CreateRemoval( |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3853 | SourceRange(ArgExprs[0]->getLocStart(), |
3854 | ArgExprs.back()->getLocEnd())); | ||||
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3855 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3856 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3857 | return Owned(new (Context) CallExpr(Context, Fn, MultiExprArg(), |
3858 | Context.VoidTy, VK_RValue, | ||||
3859 | RParenLoc)); | ||||
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3860 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3862 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3863 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3864 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
3865 | // Fn. | ||||
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3866 | bool Dependent = false; |
3867 | if (Fn->isTypeDependent()) | ||||
3868 | Dependent = true; | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3869 | else if (Expr::hasAnyTypeDependentArguments(ArgExprs)) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3870 | Dependent = true; |
3871 | |||||
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3872 | if (Dependent) { |
3873 | if (ExecConfig) { | ||||
3874 | return Owned(new (Context) CUDAKernelCallExpr( | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3875 | Context, Fn, cast<CallExpr>(ExecConfig), ArgExprs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3876 | Context.DependentTy, VK_RValue, RParenLoc)); |
3877 | } else { | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3878 | return Owned(new (Context) CallExpr(Context, Fn, ArgExprs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3879 | Context.DependentTy, VK_RValue, |
3880 | RParenLoc)); | ||||
3881 | } | ||||
3882 | } | ||||
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3883 | |
3884 | // Determine whether this is a call to an object (C++ [over.call.object]). | ||||
3885 | if (Fn->getType()->isRecordType()) | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3886 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, |
3887 | ArgExprs.data(), | ||||
3888 | ArgExprs.size(), RParenLoc)); | ||||
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3889 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3890 | if (Fn->getType() == Context.UnknownAnyTy) { |
3891 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); | ||||
3892 | if (result.isInvalid()) return ExprError(); | ||||
3893 | Fn = result.take(); | ||||
3894 | } | ||||
3895 | |||||
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3896 | if (Fn->getType() == Context.BoundMemberTy) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3897 | return BuildCallToMemberFunction(S, Fn, LParenLoc, ArgExprs.data(), |
3898 | ArgExprs.size(), RParenLoc); | ||||
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3899 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3900 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3901 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3902 | // Check for overloaded calls. This can happen even in C due to extensions. |
3903 | if (Fn->getType() == Context.OverloadTy) { | ||||
3904 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); | ||||
3905 | |||||
Douglas Gregor | ee697e6 | 2011-10-13 18:10:35 +0000 | [diff] [blame] | 3906 | // 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] | 3907 | if (!find.HasFormOfMemberPointer) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3908 | OverloadExpr *ovl = find.Expression; |
3909 | if (isa<UnresolvedLookupExpr>(ovl)) { | ||||
3910 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3911 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, ArgExprs.data(), |
3912 | ArgExprs.size(), RParenLoc, ExecConfig); | ||||
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3913 | } else { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3914 | return BuildCallToMemberFunction(S, Fn, LParenLoc, ArgExprs.data(), |
3915 | ArgExprs.size(), RParenLoc); | ||||
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3916 | } |
3917 | } | ||||
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3918 | } |
3919 | |||||
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3920 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 3921 | if (Fn->getType() == Context.UnknownAnyTy) { |
3922 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); | ||||
3923 | if (result.isInvalid()) return ExprError(); | ||||
3924 | Fn = result.take(); | ||||
3925 | } | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3926 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3927 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3928 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3929 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3930 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
3931 | if (UnOp->getOpcode() == UO_AddrOf) | ||||
3932 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); | ||||
3933 | |||||
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3934 | if (isa<DeclRefExpr>(NakedFn)) |
3935 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); | ||||
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3936 | else if (isa<MemberExpr>(NakedFn)) |
3937 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); | ||||
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3938 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3939 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, ArgExprs.data(), |
3940 | ArgExprs.size(), RParenLoc, ExecConfig, | ||||
3941 | IsExecConfig); | ||||
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3942 | } |
3943 | |||||
3944 | ExprResult | ||||
3945 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3946 | MultiExprArg ExecConfig, SourceLocation GGGLoc) { |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3947 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
3948 | if (!ConfigDecl) | ||||
3949 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) | ||||
3950 | << "cudaConfigureCall"); | ||||
3951 | QualType ConfigQTy = ConfigDecl->getType(); | ||||
3952 | |||||
3953 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( | ||||
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3954 | ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3955 | MarkFunctionReferenced(LLLLoc, ConfigDecl); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3956 | |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3957 | return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, 0, |
3958 | /*IsExecConfig=*/true); | ||||
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3959 | } |
3960 | |||||
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3961 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
3962 | /// | ||||
3963 | /// __builtin_astype( value, dst type ) | ||||
3964 | /// | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3965 | ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy, |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3966 | SourceLocation BuiltinLoc, |
3967 | SourceLocation RParenLoc) { | ||||
3968 | ExprValueKind VK = VK_RValue; | ||||
3969 | ExprObjectKind OK = OK_Ordinary; | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3970 | QualType DstTy = GetTypeFromParser(ParsedDestTy); |
3971 | QualType SrcTy = E->getType(); | ||||
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3972 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
3973 | return ExprError(Diag(BuiltinLoc, | ||||
3974 | diag::err_invalid_astype_of_different_size) | ||||
Peter Collingbourne | af9cddf | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 3975 | << DstTy |
3976 | << SrcTy | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3977 | << E->getSourceRange()); |
3978 | return Owned(new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, | ||||
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3979 | RParenLoc)); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3980 | } |
3981 | |||||
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3982 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
3983 | /// i.e. an expression not of \p OverloadTy. The expression should | ||||
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3984 | /// unary-convert to an expression of function-pointer or |
3985 | /// block-pointer type. | ||||
3986 | /// | ||||
3987 | /// \param NDecl the declaration being called, if available | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3988 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3989 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
3990 | SourceLocation LParenLoc, | ||||
3991 | Expr **Args, unsigned NumArgs, | ||||
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3992 | SourceLocation RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3993 | Expr *Config, bool IsExecConfig) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3994 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 3995 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3996 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3997 | // Promote the function operand. |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 3998 | // We special-case function promotion here because we only allow promoting |
3999 | // builtin functions to function pointers in the callee of a call. | ||||
4000 | ExprResult Result; | ||||
4001 | if (BuiltinID && | ||||
4002 | Fn->getType()->isSpecificBuiltinType(BuiltinType::BuiltinFn)) { | ||||
4003 | Result = ImpCastExprToType(Fn, Context.getPointerType(FDecl->getType()), | ||||
4004 | CK_BuiltinFnToFnPtr).take(); | ||||
4005 | } else { | ||||
4006 | Result = UsualUnaryConversions(Fn); | ||||
4007 | } | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4008 | if (Result.isInvalid()) |
4009 | return ExprError(); | ||||
4010 | Fn = Result.take(); | ||||
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4011 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4012 | // Make the call expr early, before semantic checks. This guarantees cleanup |
4013 | // of arguments and function on error. | ||||
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4014 | CallExpr *TheCall; |
Eric Christopher | a27cb25 | 2012-05-30 01:14:28 +0000 | [diff] [blame] | 4015 | if (Config) |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4016 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
4017 | cast<CallExpr>(Config), | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4018 | llvm::makeArrayRef(Args,NumArgs), |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4019 | Context.BoolTy, |
4020 | VK_RValue, | ||||
4021 | RParenLoc); | ||||
Eric Christopher | a27cb25 | 2012-05-30 01:14:28 +0000 | [diff] [blame] | 4022 | else |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4023 | TheCall = new (Context) CallExpr(Context, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4024 | llvm::makeArrayRef(Args, NumArgs), |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4025 | Context.BoolTy, |
4026 | VK_RValue, | ||||
4027 | RParenLoc); | ||||
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4028 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4029 | // Bail out early if calling a builtin with custom typechecking. |
4030 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) | ||||
4031 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); | ||||
4032 | |||||
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4033 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4034 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4035 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4036 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
4037 | // have type pointer to function". | ||||
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4038 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4039 | if (FuncT == 0) |
4040 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) | ||||
4041 | << Fn->getType() << Fn->getSourceRange()); | ||||
4042 | } else if (const BlockPointerType *BPT = | ||||
4043 | Fn->getType()->getAs<BlockPointerType>()) { | ||||
4044 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); | ||||
4045 | } else { | ||||
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4046 | // Handle calls to expressions of unknown-any type. |
4047 | if (Fn->getType() == Context.UnknownAnyTy) { | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4048 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4049 | if (rewrite.isInvalid()) return ExprError(); |
4050 | Fn = rewrite.take(); | ||||
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 4051 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4052 | goto retry; |
4053 | } | ||||
4054 | |||||
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4055 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
4056 | << Fn->getType() << Fn->getSourceRange()); | ||||
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4057 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4058 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4059 | if (getLangOpts().CUDA) { |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 4060 | if (Config) { |
4061 | // CUDA: Kernel calls must be to global functions | ||||
4062 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) | ||||
4063 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) | ||||
4064 | << FDecl->getName() << Fn->getSourceRange()); | ||||
4065 | |||||
4066 | // CUDA: Kernel function must have 'void' return type | ||||
4067 | if (!FuncT->getResultType()->isVoidType()) | ||||
4068 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) | ||||
4069 | << Fn->getType() << Fn->getSourceRange()); | ||||
Peter Collingbourne | 8591a7f | 2011-10-02 23:49:15 +0000 | [diff] [blame] | 4070 | } else { |
4071 | // CUDA: Calls to global functions must be configured | ||||
4072 | if (FDecl && FDecl->hasAttr<CUDAGlobalAttr>()) | ||||
4073 | return ExprError(Diag(LParenLoc, diag::err_global_call_not_config) | ||||
4074 | << FDecl->getName() << Fn->getSourceRange()); | ||||
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 4075 | } |
4076 | } | ||||
4077 | |||||
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4078 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4079 | if (CheckCallReturnType(FuncT->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4080 | Fn->getLocStart(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4081 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4082 | return ExprError(); |
4083 | |||||
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4084 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4085 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4086 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4087 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4088 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT); |
4089 | if (Proto) { | ||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4090 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 4091 | RParenLoc, IsExecConfig)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4092 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4093 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4094 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4095 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4096 | if (FDecl) { |
4097 | // Check if we have too few/too many template arguments, based | ||||
4098 | // on our knowledge of the function definition. | ||||
4099 | const FunctionDecl *Def = 0; | ||||
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 4100 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4101 | Proto = Def->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4102 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4103 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
4104 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); | ||||
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4105 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4106 | |
4107 | // If the function we're calling isn't a function prototype, but we have | ||||
4108 | // a function prototype from a prior declaratiom, use that prototype. | ||||
4109 | if (!FDecl->hasPrototype()) | ||||
4110 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); | ||||
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4111 | } |
4112 | |||||
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4113 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4114 | for (unsigned i = 0; i != NumArgs; i++) { |
4115 | Expr *Arg = Args[i]; | ||||
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4116 | |
4117 | if (Proto && i < Proto->getNumArgs()) { | ||||
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4118 | InitializedEntity Entity |
4119 | = InitializedEntity::InitializeParameter(Context, | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4120 | Proto->getArgType(i), |
4121 | Proto->isArgConsumed(i)); | ||||
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4122 | ExprResult ArgE = PerformCopyInitialization(Entity, |
4123 | SourceLocation(), | ||||
4124 | Owned(Arg)); | ||||
4125 | if (ArgE.isInvalid()) | ||||
4126 | return true; | ||||
4127 | |||||
4128 | Arg = ArgE.takeAs<Expr>(); | ||||
4129 | |||||
4130 | } else { | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4131 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
4132 | |||||
4133 | if (ArgE.isInvalid()) | ||||
4134 | return true; | ||||
4135 | |||||
4136 | Arg = ArgE.takeAs<Expr>(); | ||||
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4137 | } |
4138 | |||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4139 | if (RequireCompleteType(Arg->getLocStart(), |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4140 | Arg->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4141 | diag::err_call_incomplete_argument, Arg)) |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4142 | return ExprError(); |
4143 | |||||
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4144 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4145 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4146 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4147 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4148 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
4149 | if (!Method->isStatic()) | ||||
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4150 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
4151 | << Fn->getSourceRange()); | ||||
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4152 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 4153 | // Check for sentinels |
4154 | if (NDecl) | ||||
4155 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4156 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4157 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4158 | if (FDecl) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4159 | if (CheckFunctionCall(FDecl, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4160 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4161 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4162 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 4163 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4164 | } else if (NDecl) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4165 | if (CheckBlockCall(NDecl, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4166 | return ExprError(); |
4167 | } | ||||
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4168 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4169 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4170 | } |
4171 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4172 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4173 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4174 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4175 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 4176 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4177 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4178 | |
4179 | TypeSourceInfo *TInfo; | ||||
4180 | QualType literalType = GetTypeFromParser(Ty, &TInfo); | ||||
4181 | if (!TInfo) | ||||
4182 | TInfo = Context.getTrivialTypeSourceInfo(literalType); | ||||
4183 | |||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4184 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4185 | } |
4186 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4187 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4188 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4189 | SourceLocation RParenLoc, Expr *LiteralExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4190 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 4191 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4192 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4193 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4194 | diag::err_illegal_decl_array_incomplete_type, |
4195 | SourceRange(LParenLoc, | ||||
4196 | LiteralExpr->getSourceRange().getEnd()))) | ||||
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4197 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4198 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4199 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4200 | << SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4201 | } else if (!literalType->isDependentType() && |
4202 | RequireCompleteType(LParenLoc, literalType, | ||||
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4203 | diag::err_typecheck_decl_incomplete_type, |
4204 | SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd()))) | ||||
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4205 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4206 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4207 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4208 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4209 | InitializationKind Kind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4210 | = InitializationKind::CreateCStyleCast(LParenLoc, |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4211 | SourceRange(LParenLoc, RParenLoc), |
4212 | /*InitList=*/true); | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4213 | InitializationSequence InitSeq(*this, Entity, Kind, &LiteralExpr, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4214 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, LiteralExpr, |
4215 | &literalType); | ||||
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4216 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4217 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4218 | LiteralExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4219 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 4220 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4221 | if (isFileScope) { // 6.5.2.5p3 |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4222 | if (CheckForConstantInitializer(LiteralExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4223 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 4224 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4225 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4226 | // In C, compound literals are l-values for some reason. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4227 | ExprValueKind VK = getLangOpts().CPlusPlus ? VK_RValue : VK_LValue; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4228 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 4229 | return MaybeBindToTemporary( |
4230 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4231 | VK, LiteralExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4232 | } |
4233 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4234 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4235 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4236 | SourceLocation RBraceLoc) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4237 | // Immediately handle non-overload placeholders. Overloads can be |
4238 | // resolved contextually, but everything else here can't. | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4239 | for (unsigned I = 0, E = InitArgList.size(); I != E; ++I) { |
4240 | if (InitArgList[I]->getType()->isNonOverloadPlaceholderType()) { | ||||
4241 | ExprResult result = CheckPlaceholderExpr(InitArgList[I]); | ||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4242 | |
4243 | // Ignore failures; dropping the entire initializer list because | ||||
4244 | // of one failure would be terrible for indexing/etc. | ||||
4245 | if (result.isInvalid()) continue; | ||||
4246 | |||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4247 | InitArgList[I] = result.take(); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4248 | } |
4249 | } | ||||
4250 | |||||
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 4251 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4252 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4253 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4254 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitArgList, |
4255 | RBraceLoc); | ||||
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4256 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4257 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4258 | } |
4259 | |||||
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4260 | /// Do an explicit extend of the given block pointer if we're in ARC. |
4261 | static void maybeExtendBlockObject(Sema &S, ExprResult &E) { | ||||
4262 | assert(E.get()->getType()->isBlockPointerType()); | ||||
4263 | assert(E.get()->isRValue()); | ||||
4264 | |||||
4265 | // Only do this in an r-value context. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4266 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4267 | |
4268 | E = ImplicitCastExpr::Create(S.Context, E.get()->getType(), | ||||
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4269 | CK_ARCExtendBlockObject, E.get(), |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4270 | /*base path*/ 0, VK_RValue); |
4271 | S.ExprNeedsCleanups = true; | ||||
4272 | } | ||||
4273 | |||||
4274 | /// Prepare a conversion of the given expression to an ObjC object | ||||
4275 | /// pointer type. | ||||
4276 | CastKind Sema::PrepareCastToObjCObjectPointer(ExprResult &E) { | ||||
4277 | QualType type = E.get()->getType(); | ||||
4278 | if (type->isObjCObjectPointerType()) { | ||||
4279 | return CK_BitCast; | ||||
4280 | } else if (type->isBlockPointerType()) { | ||||
4281 | maybeExtendBlockObject(*this, E); | ||||
4282 | return CK_BlockPointerToObjCPointerCast; | ||||
4283 | } else { | ||||
4284 | assert(type->isPointerType()); | ||||
4285 | return CK_CPointerToObjCPointerCast; | ||||
4286 | } | ||||
4287 | } | ||||
4288 | |||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4289 | /// Prepares for a scalar cast, performing all the necessary stages |
4290 | /// except the final cast and returning the kind required. | ||||
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4291 | CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4292 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
4293 | // Also, callers should have filtered out the invalid cases with | ||||
4294 | // pointers. Everything else should be possible. | ||||
4295 | |||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4296 | QualType SrcTy = Src.get()->getType(); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4297 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4298 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4299 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4300 | switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4301 | case Type::STK_MemberPointer: |
4302 | llvm_unreachable("member pointer type in C"); | ||||
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4303 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4304 | case Type::STK_CPointer: |
4305 | case Type::STK_BlockPointer: | ||||
4306 | case Type::STK_ObjCObjectPointer: | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4307 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4308 | case Type::STK_CPointer: |
4309 | return CK_BitCast; | ||||
4310 | case Type::STK_BlockPointer: | ||||
4311 | return (SrcKind == Type::STK_BlockPointer | ||||
4312 | ? CK_BitCast : CK_AnyPointerToBlockPointerCast); | ||||
4313 | case Type::STK_ObjCObjectPointer: | ||||
4314 | if (SrcKind == Type::STK_ObjCObjectPointer) | ||||
4315 | return CK_BitCast; | ||||
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4316 | if (SrcKind == Type::STK_CPointer) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4317 | return CK_CPointerToObjCPointerCast; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4318 | maybeExtendBlockObject(*this, Src); |
4319 | return CK_BlockPointerToObjCPointerCast; | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4320 | case Type::STK_Bool: |
4321 | return CK_PointerToBoolean; | ||||
4322 | case Type::STK_Integral: | ||||
4323 | return CK_PointerToIntegral; | ||||
4324 | case Type::STK_Floating: | ||||
4325 | case Type::STK_FloatingComplex: | ||||
4326 | case Type::STK_IntegralComplex: | ||||
4327 | case Type::STK_MemberPointer: | ||||
4328 | llvm_unreachable("illegal cast from pointer"); | ||||
4329 | } | ||||
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4330 | llvm_unreachable("Should have returned before this"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4331 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4332 | case Type::STK_Bool: // casting from bool is like casting from an integer |
4333 | case Type::STK_Integral: | ||||
4334 | switch (DestTy->getScalarTypeKind()) { | ||||
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4335 | case Type::STK_CPointer: |
4336 | case Type::STK_ObjCObjectPointer: | ||||
4337 | case Type::STK_BlockPointer: | ||||
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4338 | if (Src.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4339 | Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4340 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4341 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4342 | case Type::STK_Bool: |
4343 | return CK_IntegralToBoolean; | ||||
4344 | case Type::STK_Integral: | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4345 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4346 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4347 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4348 | case Type::STK_IntegralComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4349 | Src = ImpCastExprToType(Src.take(), |
4350 | DestTy->castAs<ComplexType>()->getElementType(), | ||||
4351 | CK_IntegralCast); | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4352 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4353 | case Type::STK_FloatingComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4354 | Src = ImpCastExprToType(Src.take(), |
4355 | DestTy->castAs<ComplexType>()->getElementType(), | ||||
4356 | CK_IntegralToFloating); | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4357 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4358 | case Type::STK_MemberPointer: |
4359 | llvm_unreachable("member pointer type in C"); | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4360 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4361 | llvm_unreachable("Should have returned before this"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4362 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4363 | case Type::STK_Floating: |
4364 | switch (DestTy->getScalarTypeKind()) { | ||||
4365 | case Type::STK_Floating: | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4366 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4367 | case Type::STK_Bool: |
4368 | return CK_FloatingToBoolean; | ||||
4369 | case Type::STK_Integral: | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4370 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4371 | case Type::STK_FloatingComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4372 | Src = ImpCastExprToType(Src.take(), |
4373 | DestTy->castAs<ComplexType>()->getElementType(), | ||||
4374 | CK_FloatingCast); | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4375 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4376 | case Type::STK_IntegralComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4377 | Src = ImpCastExprToType(Src.take(), |
4378 | DestTy->castAs<ComplexType>()->getElementType(), | ||||
4379 | CK_FloatingToIntegral); | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4380 | return CK_IntegralRealToComplex; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4381 | case Type::STK_CPointer: |
4382 | case Type::STK_ObjCObjectPointer: | ||||
4383 | case Type::STK_BlockPointer: | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4384 | llvm_unreachable("valid float->pointer cast?"); |
4385 | case Type::STK_MemberPointer: | ||||
4386 | llvm_unreachable("member pointer type in C"); | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4387 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4388 | llvm_unreachable("Should have returned before this"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4389 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4390 | case Type::STK_FloatingComplex: |
4391 | switch (DestTy->getScalarTypeKind()) { | ||||
4392 | case Type::STK_FloatingComplex: | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4393 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4394 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4395 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4396 | case Type::STK_Floating: { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4397 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); |
4398 | if (Context.hasSameType(ET, DestTy)) | ||||
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4399 | return CK_FloatingComplexToReal; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4400 | Src = ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4401 | return CK_FloatingCast; |
4402 | } | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4403 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4404 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4405 | case Type::STK_Integral: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4406 | Src = ImpCastExprToType(Src.take(), |
4407 | SrcTy->castAs<ComplexType>()->getElementType(), | ||||
4408 | CK_FloatingComplexToReal); | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4409 | return CK_FloatingToIntegral; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4410 | case Type::STK_CPointer: |
4411 | case Type::STK_ObjCObjectPointer: | ||||
4412 | case Type::STK_BlockPointer: | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4413 | llvm_unreachable("valid complex float->pointer cast?"); |
4414 | case Type::STK_MemberPointer: | ||||
4415 | llvm_unreachable("member pointer type in C"); | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4416 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4417 | llvm_unreachable("Should have returned before this"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4418 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4419 | case Type::STK_IntegralComplex: |
4420 | switch (DestTy->getScalarTypeKind()) { | ||||
4421 | case Type::STK_FloatingComplex: | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4422 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4423 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4424 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4425 | case Type::STK_Integral: { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4426 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); |
4427 | if (Context.hasSameType(ET, DestTy)) | ||||
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4428 | return CK_IntegralComplexToReal; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4429 | Src = ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4430 | return CK_IntegralCast; |
4431 | } | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4432 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4433 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4434 | case Type::STK_Floating: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4435 | Src = ImpCastExprToType(Src.take(), |
4436 | SrcTy->castAs<ComplexType>()->getElementType(), | ||||
4437 | CK_IntegralComplexToReal); | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4438 | return CK_IntegralToFloating; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4439 | case Type::STK_CPointer: |
4440 | case Type::STK_ObjCObjectPointer: | ||||
4441 | case Type::STK_BlockPointer: | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4442 | llvm_unreachable("valid complex int->pointer cast?"); |
4443 | case Type::STK_MemberPointer: | ||||
4444 | llvm_unreachable("member pointer type in C"); | ||||
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4445 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4446 | llvm_unreachable("Should have returned before this"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4447 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4448 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4449 | llvm_unreachable("Unhandled scalar cast"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4450 | } |
4451 | |||||
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4452 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4453 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4454 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4455 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4456 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4457 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4458 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4459 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4460 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4461 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4462 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4463 | } else |
4464 | return Diag(R.getBegin(), | ||||
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4465 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4466 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4467 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4468 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4469 | return false; |
4470 | } | ||||
4471 | |||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4472 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
4473 | Expr *CastExpr, CastKind &Kind) { | ||||
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4474 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4475 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4476 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4477 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4478 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
4479 | // an ExtVectorType. | ||||
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4480 | // In OpenCL, casts between vectors of different types are not allowed. |
4481 | // (See OpenCL 6.2). | ||||
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4482 | if (SrcTy->isVectorType()) { |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4483 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4484 | || (getLangOpts().OpenCL && |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4485 | (DestTy.getCanonicalType() != SrcTy.getCanonicalType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4486 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4487 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4488 | return ExprError(); |
4489 | } | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4490 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4491 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4492 | } |
4493 | |||||
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4494 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4495 | // conversion will take place first from scalar to elt type, and then |
4496 | // splat from elt type to vector. | ||||
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4497 | if (SrcTy->isPointerType()) |
4498 | return Diag(R.getBegin(), | ||||
4499 | diag::err_invalid_conversion_between_vector_and_scalar) | ||||
4500 | << DestTy << SrcTy << R; | ||||
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4501 | |
4502 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4503 | ExprResult CastExprRes = Owned(CastExpr); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4504 | CastKind CK = PrepareScalarCast(CastExprRes, DestElemTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4505 | if (CastExprRes.isInvalid()) |
4506 | return ExprError(); | ||||
4507 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4508 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4509 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4510 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4511 | } |
4512 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4513 | ExprResult |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4514 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
4515 | Declarator &D, ParsedType &Ty, | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4516 | SourceLocation RParenLoc, Expr *CastExpr) { |
4517 | assert(!D.isInvalidType() && (CastExpr != 0) && | ||||
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4518 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4519 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4520 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, CastExpr->getType()); |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4521 | if (D.isInvalidType()) |
4522 | return ExprError(); | ||||
4523 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4524 | if (getLangOpts().CPlusPlus) { |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4525 | // Check that there are no default arguments (C++ only). |
4526 | CheckExtraCXXDefaultArguments(D); | ||||
4527 | } | ||||
4528 | |||||
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4529 | checkUnusedDeclAttributes(D); |
4530 | |||||
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4531 | QualType castType = castTInfo->getType(); |
4532 | Ty = CreateParsedType(castType, castTInfo); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4534 | bool isVectorLiteral = false; |
4535 | |||||
4536 | // Check for an altivec or OpenCL literal, | ||||
4537 | // i.e. all the elements are integer constants. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4538 | ParenExpr *PE = dyn_cast<ParenExpr>(CastExpr); |
4539 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(CastExpr); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4540 | if ((getLangOpts().AltiVec || getLangOpts().OpenCL) |
Tobias Grosser | 37c31c2 | 2011-09-21 18:28:29 +0000 | [diff] [blame] | 4541 | && castType->isVectorType() && (PE || PLE)) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4542 | if (PLE && PLE->getNumExprs() == 0) { |
4543 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); | ||||
4544 | return ExprError(); | ||||
4545 | } | ||||
4546 | if (PE || PLE->getNumExprs() == 1) { | ||||
4547 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); | ||||
4548 | if (!E->getType()->isVectorType()) | ||||
4549 | isVectorLiteral = true; | ||||
4550 | } | ||||
4551 | else | ||||
4552 | isVectorLiteral = true; | ||||
4553 | } | ||||
4554 | |||||
4555 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' | ||||
4556 | // then handle it as such. | ||||
4557 | if (isVectorLiteral) | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4558 | return BuildVectorLiteral(LParenLoc, RParenLoc, CastExpr, castTInfo); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4559 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4560 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4561 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
4562 | // sequence of BinOp comma operators. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4563 | if (isa<ParenListExpr>(CastExpr)) { |
4564 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); | ||||
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4565 | if (Result.isInvalid()) return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4566 | CastExpr = Result.take(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4567 | } |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4568 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4569 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4570 | } |
4571 | |||||
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4572 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
4573 | SourceLocation RParenLoc, Expr *E, | ||||
4574 | TypeSourceInfo *TInfo) { | ||||
4575 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && | ||||
4576 | "Expected paren or paren list expression"); | ||||
4577 | |||||
4578 | Expr **exprs; | ||||
4579 | unsigned numExprs; | ||||
4580 | Expr *subExpr; | ||||
4581 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { | ||||
4582 | exprs = PE->getExprs(); | ||||
4583 | numExprs = PE->getNumExprs(); | ||||
4584 | } else { | ||||
4585 | subExpr = cast<ParenExpr>(E)->getSubExpr(); | ||||
4586 | exprs = &subExpr; | ||||
4587 | numExprs = 1; | ||||
4588 | } | ||||
4589 | |||||
4590 | QualType Ty = TInfo->getType(); | ||||
4591 | assert(Ty->isVectorType() && "Expected vector type"); | ||||
4592 | |||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4593 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4594 | const VectorType *VTy = Ty->getAs<VectorType>(); |
4595 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); | ||||
4596 | |||||
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4597 | // '(...)' form of vector initialization in AltiVec: the number of |
4598 | // initializers must be one or must match the size of the vector. | ||||
4599 | // If a single value is specified in the initializer then it will be | ||||
4600 | // replicated to all the components of the vector | ||||
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4601 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4602 | // The number of initializers must be one or must match the size of the |
4603 | // vector. If a single value is specified in the initializer then it will | ||||
4604 | // be replicated to all the components of the vector | ||||
4605 | if (numExprs == 1) { | ||||
4606 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); | ||||
Richard Smith | 61ffd09 | 2011-10-27 23:31:58 +0000 | [diff] [blame] | 4607 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
4608 | if (Literal.isInvalid()) | ||||
4609 | return ExprError(); | ||||
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4610 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4611 | PrepareScalarCast(Literal, ElemTy)); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4612 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
4613 | } | ||||
4614 | else if (numExprs < numElems) { | ||||
4615 | Diag(E->getExprLoc(), | ||||
4616 | diag::err_incorrect_number_of_vector_initializers); | ||||
4617 | return ExprError(); | ||||
4618 | } | ||||
4619 | else | ||||
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 4620 | initExprs.append(exprs, exprs + numExprs); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4621 | } |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4622 | else { |
4623 | // For OpenCL, when the number of initializers is a single value, | ||||
4624 | // it will be replicated to all components of the vector. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4625 | if (getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4626 | VTy->getVectorKind() == VectorType::GenericVector && |
4627 | numExprs == 1) { | ||||
4628 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); | ||||
Richard Smith | 61ffd09 | 2011-10-27 23:31:58 +0000 | [diff] [blame] | 4629 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
4630 | if (Literal.isInvalid()) | ||||
4631 | return ExprError(); | ||||
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4632 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4633 | PrepareScalarCast(Literal, ElemTy)); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4634 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
4635 | } | ||||
4636 | |||||
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 4637 | initExprs.append(exprs, exprs + numExprs); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4638 | } |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4639 | // FIXME: This means that pretty-printing the final AST will produce curly |
4640 | // braces instead of the original commas. | ||||
4641 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4642 | initExprs, RParenLoc); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4643 | initE->setType(Ty); |
4644 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); | ||||
4645 | } | ||||
4646 | |||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4647 | /// This is not an AltiVec-style cast or or C++ direct-initialization, so turn |
4648 | /// the ParenListExpr into a sequence of comma binary operators. | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4649 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4650 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { |
4651 | ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); | ||||
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4652 | if (!E) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4653 | return Owned(OrigExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4655 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4656 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4657 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4658 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
4659 | E->getExpr(i)); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4660 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4661 | if (Result.isInvalid()) return ExprError(); |
4662 | |||||
4663 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); | ||||
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4664 | } |
4665 | |||||
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4666 | ExprResult Sema::ActOnParenListExpr(SourceLocation L, |
4667 | SourceLocation R, | ||||
4668 | MultiExprArg Val) { | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4669 | assert(Val.data() != 0 && "ActOnParenOrParenListExpr() missing expr list"); |
4670 | Expr *expr = new (Context) ParenListExpr(Context, L, Val, R); | ||||
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4671 | return Owned(expr); |
4672 | } | ||||
4673 | |||||
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4674 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4675 | /// constant and the other is not a pointer. Returns true if a diagnostic is |
4676 | /// emitted. | ||||
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4677 | bool Sema::DiagnoseConditionalForNull(Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4678 | SourceLocation QuestionLoc) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4679 | Expr *NullExpr = LHSExpr; |
4680 | Expr *NonPointerExpr = RHSExpr; | ||||
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4681 | Expr::NullPointerConstantKind NullKind = |
4682 | NullExpr->isNullPointerConstant(Context, | ||||
4683 | Expr::NPC_ValueDependentIsNotNull); | ||||
4684 | |||||
4685 | if (NullKind == Expr::NPCK_NotNull) { | ||||
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4686 | NullExpr = RHSExpr; |
4687 | NonPointerExpr = LHSExpr; | ||||
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4688 | NullKind = |
4689 | NullExpr->isNullPointerConstant(Context, | ||||
4690 | Expr::NPC_ValueDependentIsNotNull); | ||||
4691 | } | ||||
4692 | |||||
4693 | if (NullKind == Expr::NPCK_NotNull) | ||||
4694 | return false; | ||||
4695 | |||||
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 4696 | if (NullKind == Expr::NPCK_ZeroExpression) |
4697 | return false; | ||||
4698 | |||||
4699 | if (NullKind == Expr::NPCK_ZeroLiteral) { | ||||
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4700 | // In this case, check to make sure that we got here from a "NULL" |
4701 | // string in the source code. | ||||
4702 | NullExpr = NullExpr->IgnoreParenImpCasts(); | ||||
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4703 | SourceLocation loc = NullExpr->getExprLoc(); |
4704 | if (!findMacroSpelling(loc, "NULL")) | ||||
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4705 | return false; |
4706 | } | ||||
4707 | |||||
4708 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); | ||||
4709 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) | ||||
4710 | << NonPointerExpr->getType() << DiagType | ||||
4711 | << NonPointerExpr->getSourceRange(); | ||||
4712 | return true; | ||||
4713 | } | ||||
4714 | |||||
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4715 | /// \brief Return false if the condition expression is valid, true otherwise. |
4716 | static bool checkCondition(Sema &S, Expr *Cond) { | ||||
4717 | QualType CondTy = Cond->getType(); | ||||
4718 | |||||
4719 | // C99 6.5.15p2 | ||||
4720 | if (CondTy->isScalarType()) return false; | ||||
4721 | |||||
4722 | // 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] | 4723 | if (S.getLangOpts().OpenCL && CondTy->isVectorType()) |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4724 | return false; |
4725 | |||||
4726 | // Emit the proper error message. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4727 | S.Diag(Cond->getLocStart(), S.getLangOpts().OpenCL ? |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4728 | diag::err_typecheck_cond_expect_scalar : |
4729 | diag::err_typecheck_cond_expect_scalar_or_vector) | ||||
4730 | << CondTy; | ||||
4731 | return true; | ||||
4732 | } | ||||
4733 | |||||
4734 | /// \brief Return false if the two expressions can be converted to a vector, | ||||
4735 | /// true otherwise | ||||
4736 | static bool checkConditionalConvertScalarsToVectors(Sema &S, ExprResult &LHS, | ||||
4737 | ExprResult &RHS, | ||||
4738 | QualType CondTy) { | ||||
4739 | // Both operands should be of scalar type. | ||||
4740 | if (!LHS.get()->getType()->isScalarType()) { | ||||
4741 | S.Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) | ||||
4742 | << CondTy; | ||||
4743 | return true; | ||||
4744 | } | ||||
4745 | if (!RHS.get()->getType()->isScalarType()) { | ||||
4746 | S.Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) | ||||
4747 | << CondTy; | ||||
4748 | return true; | ||||
4749 | } | ||||
4750 | |||||
4751 | // Implicity convert these scalars to the type of the condition. | ||||
4752 | LHS = S.ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); | ||||
4753 | RHS = S.ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); | ||||
4754 | return false; | ||||
4755 | } | ||||
4756 | |||||
4757 | /// \brief Handle when one or both operands are void type. | ||||
4758 | static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, | ||||
4759 | ExprResult &RHS) { | ||||
4760 | Expr *LHSExpr = LHS.get(); | ||||
4761 | Expr *RHSExpr = RHS.get(); | ||||
4762 | |||||
4763 | if (!LHSExpr->getType()->isVoidType()) | ||||
4764 | S.Diag(RHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) | ||||
4765 | << RHSExpr->getSourceRange(); | ||||
4766 | if (!RHSExpr->getType()->isVoidType()) | ||||
4767 | S.Diag(LHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) | ||||
4768 | << LHSExpr->getSourceRange(); | ||||
4769 | LHS = S.ImpCastExprToType(LHS.take(), S.Context.VoidTy, CK_ToVoid); | ||||
4770 | RHS = S.ImpCastExprToType(RHS.take(), S.Context.VoidTy, CK_ToVoid); | ||||
4771 | return S.Context.VoidTy; | ||||
4772 | } | ||||
4773 | |||||
4774 | /// \brief Return false if the NullExpr can be promoted to PointerTy, | ||||
4775 | /// true otherwise. | ||||
4776 | static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, | ||||
4777 | QualType PointerTy) { | ||||
4778 | if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || | ||||
4779 | !NullExpr.get()->isNullPointerConstant(S.Context, | ||||
4780 | Expr::NPC_ValueDependentIsNull)) | ||||
4781 | return true; | ||||
4782 | |||||
4783 | NullExpr = S.ImpCastExprToType(NullExpr.take(), PointerTy, CK_NullToPointer); | ||||
4784 | return false; | ||||
4785 | } | ||||
4786 | |||||
4787 | /// \brief Checks compatibility between two pointers and return the resulting | ||||
4788 | /// type. | ||||
4789 | static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, | ||||
4790 | ExprResult &RHS, | ||||
4791 | SourceLocation Loc) { | ||||
4792 | QualType LHSTy = LHS.get()->getType(); | ||||
4793 | QualType RHSTy = RHS.get()->getType(); | ||||
4794 | |||||
4795 | if (S.Context.hasSameType(LHSTy, RHSTy)) { | ||||
4796 | // Two identical pointers types are always compatible. | ||||
4797 | return LHSTy; | ||||
4798 | } | ||||
4799 | |||||
4800 | QualType lhptee, rhptee; | ||||
4801 | |||||
4802 | // Get the pointee types. | ||||
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4803 | if (const BlockPointerType *LHSBTy = LHSTy->getAs<BlockPointerType>()) { |
4804 | lhptee = LHSBTy->getPointeeType(); | ||||
4805 | rhptee = RHSTy->castAs<BlockPointerType>()->getPointeeType(); | ||||
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4806 | } else { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4807 | lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); |
4808 | rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); | ||||
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4809 | } |
4810 | |||||
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4811 | // C99 6.5.15p6: If both operands are pointers to compatible types or to |
4812 | // differently qualified versions of compatible types, the result type is | ||||
4813 | // a pointer to an appropriately qualified version of the composite | ||||
4814 | // type. | ||||
4815 | |||||
4816 | // Only CVR-qualifiers exist in the standard, and the differently-qualified | ||||
4817 | // clause doesn't make sense for our extensions. E.g. address space 2 should | ||||
4818 | // be incompatible with address space 3: they may live on different devices or | ||||
4819 | // anything. | ||||
4820 | Qualifiers lhQual = lhptee.getQualifiers(); | ||||
4821 | Qualifiers rhQual = rhptee.getQualifiers(); | ||||
4822 | |||||
4823 | unsigned MergedCVRQual = lhQual.getCVRQualifiers() | rhQual.getCVRQualifiers(); | ||||
4824 | lhQual.removeCVRQualifiers(); | ||||
4825 | rhQual.removeCVRQualifiers(); | ||||
4826 | |||||
4827 | lhptee = S.Context.getQualifiedType(lhptee.getUnqualifiedType(), lhQual); | ||||
4828 | rhptee = S.Context.getQualifiedType(rhptee.getUnqualifiedType(), rhQual); | ||||
4829 | |||||
4830 | QualType CompositeTy = S.Context.mergeTypes(lhptee, rhptee); | ||||
4831 | |||||
4832 | if (CompositeTy.isNull()) { | ||||
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4833 | S.Diag(Loc, diag::warn_typecheck_cond_incompatible_pointers) |
4834 | << LHSTy << RHSTy << LHS.get()->getSourceRange() | ||||
4835 | << RHS.get()->getSourceRange(); | ||||
4836 | // In this situation, we assume void* type. No especially good | ||||
4837 | // reason, but this is what gcc does, and we do have to pick | ||||
4838 | // to get a consistent AST. | ||||
4839 | QualType incompatTy = S.Context.getPointerType(S.Context.VoidTy); | ||||
4840 | LHS = S.ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); | ||||
4841 | RHS = S.ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); | ||||
4842 | return incompatTy; | ||||
4843 | } | ||||
4844 | |||||
4845 | // The pointer types are compatible. | ||||
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4846 | QualType ResultTy = CompositeTy.withCVRQualifiers(MergedCVRQual); |
4847 | ResultTy = S.Context.getPointerType(ResultTy); | ||||
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4848 | |
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4849 | LHS = S.ImpCastExprToType(LHS.take(), ResultTy, CK_BitCast); |
4850 | RHS = S.ImpCastExprToType(RHS.take(), ResultTy, CK_BitCast); | ||||
4851 | return ResultTy; | ||||
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4852 | } |
4853 | |||||
4854 | /// \brief Return the resulting type when the operands are both block pointers. | ||||
4855 | static QualType checkConditionalBlockPointerCompatibility(Sema &S, | ||||
4856 | ExprResult &LHS, | ||||
4857 | ExprResult &RHS, | ||||
4858 | SourceLocation Loc) { | ||||
4859 | QualType LHSTy = LHS.get()->getType(); | ||||
4860 | QualType RHSTy = RHS.get()->getType(); | ||||
4861 | |||||
4862 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { | ||||
4863 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { | ||||
4864 | QualType destType = S.Context.getPointerType(S.Context.VoidTy); | ||||
4865 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); | ||||
4866 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); | ||||
4867 | return destType; | ||||
4868 | } | ||||
4869 | S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) | ||||
4870 | << LHSTy << RHSTy << LHS.get()->getSourceRange() | ||||
4871 | << RHS.get()->getSourceRange(); | ||||
4872 | return QualType(); | ||||
4873 | } | ||||
4874 | |||||
4875 | // We have 2 block pointer types. | ||||
4876 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); | ||||
4877 | } | ||||
4878 | |||||
4879 | /// \brief Return the resulting type when the operands are both pointers. | ||||
4880 | static QualType | ||||
4881 | checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, | ||||
4882 | ExprResult &RHS, | ||||
4883 | SourceLocation Loc) { | ||||
4884 | // get the pointer types | ||||
4885 | QualType LHSTy = LHS.get()->getType(); | ||||
4886 | QualType RHSTy = RHS.get()->getType(); | ||||
4887 | |||||
4888 | // get the "pointed to" types | ||||
4889 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); | ||||
4890 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); | ||||
4891 | |||||
4892 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) | ||||
4893 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { | ||||
4894 | // Figure out necessary qualifiers (C99 6.5.15p6) | ||||
4895 | QualType destPointee | ||||
4896 | = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); | ||||
4897 | QualType destType = S.Context.getPointerType(destPointee); | ||||
4898 | // Add qualifiers if necessary. | ||||
4899 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_NoOp); | ||||
4900 | // Promote to void*. | ||||
4901 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); | ||||
4902 | return destType; | ||||
4903 | } | ||||
4904 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { | ||||
4905 | QualType destPointee | ||||
4906 | = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); | ||||
4907 | QualType destType = S.Context.getPointerType(destPointee); | ||||
4908 | // Add qualifiers if necessary. | ||||
4909 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_NoOp); | ||||
4910 | // Promote to void*. | ||||
4911 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); | ||||
4912 | return destType; | ||||
4913 | } | ||||
4914 | |||||
4915 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); | ||||
4916 | } | ||||
4917 | |||||
4918 | /// \brief Return false if the first expression is not an integer and the second | ||||
4919 | /// expression is not a pointer, true otherwise. | ||||
4920 | static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, | ||||
4921 | Expr* PointerExpr, SourceLocation Loc, | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4922 | bool IsIntFirstExpr) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4923 | if (!PointerExpr->getType()->isPointerType() || |
4924 | !Int.get()->getType()->isIntegerType()) | ||||
4925 | return false; | ||||
4926 | |||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4927 | Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; |
4928 | Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); | ||||
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4929 | |
4930 | S.Diag(Loc, diag::warn_typecheck_cond_pointer_integer_mismatch) | ||||
4931 | << Expr1->getType() << Expr2->getType() | ||||
4932 | << Expr1->getSourceRange() << Expr2->getSourceRange(); | ||||
4933 | Int = S.ImpCastExprToType(Int.take(), PointerExpr->getType(), | ||||
4934 | CK_IntegralToPointer); | ||||
4935 | return true; | ||||
4936 | } | ||||
4937 | |||||
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4938 | /// Note that LHS is not null here, even if this is the gnu "x ?: y" extension. |
4939 | /// In that case, LHS = cond. | ||||
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4940 | /// C99 6.5.15 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4941 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
4942 | ExprResult &RHS, ExprValueKind &VK, | ||||
4943 | ExprObjectKind &OK, | ||||
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4944 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4945 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4946 | ExprResult LHSResult = CheckPlaceholderExpr(LHS.get()); |
4947 | if (!LHSResult.isUsable()) return QualType(); | ||||
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4948 | LHS = LHSResult; |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4949 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4950 | ExprResult RHSResult = CheckPlaceholderExpr(RHS.get()); |
4951 | if (!RHSResult.isUsable()) return QualType(); | ||||
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4952 | RHS = RHSResult; |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4953 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4954 | // C++ is sufficiently different to merit its own checker. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4955 | if (getLangOpts().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4956 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4957 | |
4958 | VK = VK_RValue; | ||||
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4959 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4960 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4961 | Cond = UsualUnaryConversions(Cond.take()); |
4962 | if (Cond.isInvalid()) | ||||
4963 | return QualType(); | ||||
4964 | LHS = UsualUnaryConversions(LHS.take()); | ||||
4965 | if (LHS.isInvalid()) | ||||
4966 | return QualType(); | ||||
4967 | RHS = UsualUnaryConversions(RHS.take()); | ||||
4968 | if (RHS.isInvalid()) | ||||
4969 | return QualType(); | ||||
4970 | |||||
4971 | QualType CondTy = Cond.get()->getType(); | ||||
4972 | QualType LHSTy = LHS.get()->getType(); | ||||
4973 | QualType RHSTy = RHS.get()->getType(); | ||||
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4974 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4975 | // first, check the condition. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4976 | if (checkCondition(*this, Cond.get())) |
4977 | return QualType(); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4978 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4979 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4980 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4981 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4982 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4983 | // OpenCL: If the condition is a vector, and both operands are scalar, |
4984 | // attempt to implicity convert them to the vector type to act like the | ||||
4985 | // built in select. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4986 | if (getLangOpts().OpenCL && CondTy->isVectorType()) |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4987 | if (checkConditionalConvertScalarsToVectors(*this, LHS, RHS, CondTy)) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4988 | return QualType(); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4989 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4990 | // If both operands have arithmetic type, do the usual arithmetic conversions |
4991 | // to find a common type: C99 6.5.15p3,5. | ||||
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4992 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
4993 | UsualArithmeticConversions(LHS, RHS); | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4994 | if (LHS.isInvalid() || RHS.isInvalid()) |
4995 | return QualType(); | ||||
4996 | return LHS.get()->getType(); | ||||
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4997 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4998 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4999 | // If both operands are the same structure or union type, the result is that |
5000 | // type. | ||||
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5001 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
5002 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) | ||||
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5003 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5004 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5005 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5006 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5007 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5008 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5009 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5010 | // 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] | 5011 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5012 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5013 | return checkConditionalVoidType(*this, LHS, RHS); |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5014 | } |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5015 | |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5016 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
5017 | // the type of the other operand." | ||||
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5018 | if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; |
5019 | if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5020 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5021 | // All objective-c pointer type analysis is done here. |
5022 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, | ||||
5023 | QuestionLoc); | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5024 | if (LHS.isInvalid() || RHS.isInvalid()) |
5025 | return QualType(); | ||||
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5026 | if (!compositeType.isNull()) |
5027 | return compositeType; | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5028 | |
5029 | |||||
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5030 | // Handle block pointer types. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5031 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) |
5032 | return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, | ||||
5033 | QuestionLoc); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5034 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5035 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5036 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) |
5037 | return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, | ||||
5038 | QuestionLoc); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5039 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5040 | // GCC compatibility: soften pointer/integer mismatch. Note that |
5041 | // null pointers have been filtered out by this point. | ||||
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5042 | if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, |
5043 | /*isIntFirstExpr=*/true)) | ||||
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5044 | return RHSTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 5045 | if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, |
5046 | /*isIntFirstExpr=*/false)) | ||||
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5047 | return LHSTy; |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 5048 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5049 | // Emit a better diagnostic if one of the expressions is a null pointer |
5050 | // constant and the other is not a pointer type. In this case, the user most | ||||
5051 | // likely forgot to take the address of the other expression. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5052 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5053 | return QualType(); |
5054 | |||||
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5055 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5056 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5057 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
5058 | << RHS.get()->getSourceRange(); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5059 | return QualType(); |
5060 | } | ||||
5061 | |||||
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5062 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
5063 | /// two objective-c pointer types of the two input expressions. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5064 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5065 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5066 | QualType LHSTy = LHS.get()->getType(); |
5067 | QualType RHSTy = RHS.get()->getType(); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5068 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5069 | // Handle things like Class and struct objc_class*. Here we case the result |
5070 | // to the pseudo-builtin, because that will be implicitly cast back to the | ||||
5071 | // redefinition type if an attempt is made to access its fields. | ||||
5072 | if (LHSTy->isObjCClassType() && | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5073 | (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5074 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5075 | return LHSTy; |
5076 | } | ||||
5077 | if (RHSTy->isObjCClassType() && | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5078 | (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5079 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5080 | return RHSTy; |
5081 | } | ||||
5082 | // And the same for struct objc_object* / id | ||||
5083 | if (LHSTy->isObjCIdType() && | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5084 | (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5085 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5086 | return LHSTy; |
5087 | } | ||||
5088 | if (RHSTy->isObjCIdType() && | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5089 | (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5090 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5091 | return RHSTy; |
5092 | } | ||||
5093 | // And the same for struct objc_selector* / SEL | ||||
5094 | if (Context.isObjCSelType(LHSTy) && | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5095 | (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5096 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5097 | return LHSTy; |
5098 | } | ||||
5099 | if (Context.isObjCSelType(RHSTy) && | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5100 | (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5101 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5102 | return RHSTy; |
5103 | } | ||||
5104 | // Check constraints for Objective-C object pointers types. | ||||
5105 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5106 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5107 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
5108 | // Two identical object pointer types are always compatible. | ||||
5109 | return LHSTy; | ||||
5110 | } | ||||
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5111 | const ObjCObjectPointerType *LHSOPT = LHSTy->castAs<ObjCObjectPointerType>(); |
5112 | const ObjCObjectPointerType *RHSOPT = RHSTy->castAs<ObjCObjectPointerType>(); | ||||
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5113 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5114 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5115 | // If both operands are interfaces and either operand can be |
5116 | // assigned to the other, use that type as the composite | ||||
5117 | // type. This allows | ||||
5118 | // xxx ? (A*) a : (B*) b | ||||
5119 | // where B is a subclass of A. | ||||
5120 | // | ||||
5121 | // Additionally, as for assignment, if either type is 'id' | ||||
5122 | // allow silent coercion. Finally, if the types are | ||||
5123 | // incompatible then make sure to use 'id' as the composite | ||||
5124 | // type so the result is acceptable for sending messages to. | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5125 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5126 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
5127 | // It could return the composite type. | ||||
5128 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { | ||||
5129 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; | ||||
5130 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { | ||||
5131 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; | ||||
5132 | } else if ((LHSTy->isObjCQualifiedIdType() || | ||||
5133 | RHSTy->isObjCQualifiedIdType()) && | ||||
5134 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { | ||||
5135 | // Need to handle "id<xx>" explicitly. | ||||
5136 | // GCC allows qualified id and any Objective-C type to devolve to | ||||
5137 | // id. Currently localizing to here until clear this should be | ||||
5138 | // part of ObjCQualifiedIdTypesAreCompatible. | ||||
5139 | compositeType = Context.getObjCIdType(); | ||||
5140 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { | ||||
5141 | compositeType = Context.getObjCIdType(); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5142 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5143 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
5144 | ; | ||||
5145 | else { | ||||
5146 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) | ||||
5147 | << LHSTy << RHSTy | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5148 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5149 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5150 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
5151 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); | ||||
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5152 | return incompatTy; |
5153 | } | ||||
5154 | // The object pointer types are compatible. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5155 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
5156 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); | ||||
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5157 | return compositeType; |
5158 | } | ||||
5159 | // Check Objective-C object pointer types and 'void *' | ||||
5160 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5161 | if (getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | a66eccb | 2012-02-25 00:23:44 +0000 | [diff] [blame] | 5162 | // ARC forbids the implicit conversion of object pointers to 'void *', |
5163 | // so these types are not compatible. | ||||
5164 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy | ||||
5165 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | ||||
5166 | LHS = RHS = true; | ||||
5167 | return QualType(); | ||||
5168 | } | ||||
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5169 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
5170 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); | ||||
5171 | QualType destPointee | ||||
5172 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); | ||||
5173 | QualType destType = Context.getPointerType(destPointee); | ||||
5174 | // Add qualifiers if necessary. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5175 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5176 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5177 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5178 | return destType; |
5179 | } | ||||
5180 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5181 | if (getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | a66eccb | 2012-02-25 00:23:44 +0000 | [diff] [blame] | 5182 | // ARC forbids the implicit conversion of object pointers to 'void *', |
5183 | // so these types are not compatible. | ||||
5184 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy | ||||
5185 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | ||||
5186 | LHS = RHS = true; | ||||
5187 | return QualType(); | ||||
5188 | } | ||||
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5189 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
5190 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); | ||||
5191 | QualType destPointee | ||||
5192 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); | ||||
5193 | QualType destType = Context.getPointerType(destPointee); | ||||
5194 | // Add qualifiers if necessary. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5195 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5196 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5197 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5198 | return destType; |
5199 | } | ||||
5200 | return QualType(); | ||||
5201 | } | ||||
5202 | |||||
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5203 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5204 | /// ParenRange in parentheses. |
5205 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, | ||||
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5206 | const PartialDiagnostic &Note, |
5207 | SourceRange ParenRange) { | ||||
5208 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); | ||||
5209 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && | ||||
5210 | EndLoc.isValid()) { | ||||
5211 | Self.Diag(Loc, Note) | ||||
5212 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") | ||||
5213 | << FixItHint::CreateInsertion(EndLoc, ")"); | ||||
5214 | } else { | ||||
5215 | // We can't display the parentheses, so just show the bare note. | ||||
5216 | Self.Diag(Loc, Note) << ParenRange; | ||||
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5217 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5218 | } |
5219 | |||||
5220 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { | ||||
5221 | return Opc >= BO_Mul && Opc <= BO_Shr; | ||||
5222 | } | ||||
5223 | |||||
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5224 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
5225 | /// expression, either using a built-in or overloaded operator, | ||||
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5226 | /// and sets *OpCode to the opcode and *RHSExprs to the right-hand side |
5227 | /// expression. | ||||
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5228 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5229 | Expr **RHSExprs) { |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 5230 | // Don't strip parenthesis: we should not warn if E is in parenthesis. |
5231 | E = E->IgnoreImpCasts(); | ||||
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5232 | E = E->IgnoreConversionOperator(); |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 5233 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5234 | |
5235 | // Built-in binary operator. | ||||
5236 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { | ||||
5237 | if (IsArithmeticOp(OP->getOpcode())) { | ||||
5238 | *Opcode = OP->getOpcode(); | ||||
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5239 | *RHSExprs = OP->getRHS(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5240 | return true; |
5241 | } | ||||
5242 | } | ||||
5243 | |||||
5244 | // Overloaded operator. | ||||
5245 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { | ||||
5246 | if (Call->getNumArgs() != 2) | ||||
5247 | return false; | ||||
5248 | |||||
5249 | // Make sure this is really a binary operator that is safe to pass into | ||||
5250 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. | ||||
5251 | OverloadedOperatorKind OO = Call->getOperator(); | ||||
5252 | if (OO < OO_Plus || OO > OO_Arrow) | ||||
5253 | return false; | ||||
5254 | |||||
5255 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); | ||||
5256 | if (IsArithmeticOp(OpKind)) { | ||||
5257 | *Opcode = OpKind; | ||||
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5258 | *RHSExprs = Call->getArg(1); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5259 | return true; |
5260 | } | ||||
5261 | } | ||||
5262 | |||||
5263 | return false; | ||||
5264 | } | ||||
5265 | |||||
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5266 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
5267 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); | ||||
5268 | } | ||||
5269 | |||||
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5270 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
5271 | /// or is a logical expression such as (x==y) which has int type, but is | ||||
5272 | /// commonly interpreted as boolean. | ||||
5273 | static bool ExprLooksBoolean(Expr *E) { | ||||
5274 | E = E->IgnoreParenImpCasts(); | ||||
5275 | |||||
5276 | if (E->getType()->isBooleanType()) | ||||
5277 | return true; | ||||
5278 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) | ||||
5279 | return IsLogicOp(OP->getOpcode()); | ||||
5280 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) | ||||
5281 | return OP->getOpcode() == UO_LNot; | ||||
5282 | |||||
5283 | return false; | ||||
5284 | } | ||||
5285 | |||||
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5286 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
5287 | /// and binary operator are mixed in a way that suggests the programmer assumed | ||||
5288 | /// the conditional operator has higher precedence, for example: | ||||
5289 | /// "int x = a + someBinaryCondition ? 1 : 2". | ||||
5290 | static void DiagnoseConditionalPrecedence(Sema &Self, | ||||
5291 | SourceLocation OpLoc, | ||||
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5292 | Expr *Condition, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5293 | Expr *LHSExpr, |
5294 | Expr *RHSExpr) { | ||||
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5295 | BinaryOperatorKind CondOpcode; |
5296 | Expr *CondRHS; | ||||
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5297 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5298 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5299 | return; |
5300 | if (!ExprLooksBoolean(CondRHS)) | ||||
5301 | return; | ||||
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5302 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5303 | // The condition is an arithmetic binary expression, with a right- |
5304 | // hand side that looks boolean, so warn. | ||||
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5305 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5306 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5307 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5308 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5309 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5310 | SuggestParentheses(Self, OpLoc, |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 5311 | Self.PDiag(diag::note_precedence_silence) |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5312 | << BinaryOperator::getOpcodeStr(CondOpcode), |
5313 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); | ||||
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 5314 | |
5315 | SuggestParentheses(Self, OpLoc, | ||||
5316 | Self.PDiag(diag::note_precedence_conditional_first), | ||||
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5317 | SourceRange(CondRHS->getLocStart(), RHSExpr->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5318 | } |
5319 | |||||
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5320 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5321 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5322 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5323 | SourceLocation ColonLoc, |
5324 | Expr *CondExpr, Expr *LHSExpr, | ||||
5325 | Expr *RHSExpr) { | ||||
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5326 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
5327 | // was the condition. | ||||
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5328 | OpaqueValueExpr *opaqueValue = 0; |
5329 | Expr *commonExpr = 0; | ||||
5330 | if (LHSExpr == 0) { | ||||
5331 | commonExpr = CondExpr; | ||||
5332 | |||||
5333 | // We usually want to apply unary conversions *before* saving, except | ||||
5334 | // in the special case of a C++ l-value conditional. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5335 | if (!(getLangOpts().CPlusPlus |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5336 | && !commonExpr->isTypeDependent() |
5337 | && commonExpr->getValueKind() == RHSExpr->getValueKind() | ||||
5338 | && commonExpr->isGLValue() | ||||
5339 | && commonExpr->isOrdinaryOrBitFieldObject() | ||||
5340 | && RHSExpr->isOrdinaryOrBitFieldObject() | ||||
5341 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5342 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
5343 | if (commonRes.isInvalid()) | ||||
5344 | return ExprError(); | ||||
5345 | commonExpr = commonRes.take(); | ||||
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5346 | } |
5347 | |||||
5348 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), | ||||
5349 | commonExpr->getType(), | ||||
5350 | commonExpr->getValueKind(), | ||||
Douglas Gregor | 97df54e | 2012-02-23 22:17:26 +0000 | [diff] [blame] | 5351 | commonExpr->getObjectKind(), |
5352 | commonExpr); | ||||
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5353 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5354 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5355 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5356 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5357 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5358 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
5359 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, | ||||
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5360 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5361 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
5362 | RHS.isInvalid()) | ||||
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5363 | return ExprError(); |
5364 | |||||
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5365 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
5366 | RHS.get()); | ||||
5367 | |||||
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5368 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5369 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
5370 | LHS.take(), ColonLoc, | ||||
5371 | RHS.take(), result, VK, OK)); | ||||
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5372 | |
5373 | return Owned(new (Context) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5374 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5375 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
5376 | OK)); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5377 | } |
5378 | |||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5379 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5380 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5381 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
5382 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. | ||||
5383 | // FIXME: add a couple examples in this comment. | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5384 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5385 | checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) { |
5386 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); | ||||
5387 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5388 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5389 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5390 | const Type *lhptee, *rhptee; |
5391 | Qualifiers lhq, rhq; | ||||
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5392 | llvm::tie(lhptee, lhq) = cast<PointerType>(LHSType)->getPointeeType().split(); |
5393 | llvm::tie(rhptee, rhq) = cast<PointerType>(RHSType)->getPointeeType().split(); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5394 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5395 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5396 | |
5397 | // C99 6.5.16.1p1: This following citation is common to constraints | ||||
5398 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the | ||||
5399 | // qualifiers of the type *pointed to* by the right; | ||||
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5400 | Qualifiers lq; |
5401 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5402 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
5403 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && | ||||
5404 | lhq.compatiblyIncludesObjCLifetime(rhq)) { | ||||
5405 | // Ignore lifetime for further calculation. | ||||
5406 | lhq.removeObjCLifetime(); | ||||
5407 | rhq.removeObjCLifetime(); | ||||
5408 | } | ||||
5409 | |||||
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5410 | if (!lhq.compatiblyIncludes(rhq)) { |
5411 | // Treat address-space mismatches as fatal. TODO: address subspaces | ||||
5412 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) | ||||
5413 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; | ||||
5414 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5415 | // 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] | 5416 | // and from void*. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5417 | else if (lhq.withoutObjCGCAttr().withoutObjCLifetime() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5418 | .compatiblyIncludes( |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5419 | rhq.withoutObjCGCAttr().withoutObjCLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5420 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
5421 | ; // keep old | ||||
5422 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5423 | // Treat lifetime mismatches as fatal. |
5424 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) | ||||
5425 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; | ||||
5426 | |||||
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5427 | // For GCC compatibility, other qualifier mismatches are treated |
5428 | // as still compatible in C. | ||||
5429 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; | ||||
5430 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5431 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5432 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
5433 | // 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] | 5434 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5435 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5436 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5437 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5438 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5439 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5440 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5441 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5442 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5443 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5444 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5445 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5446 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5447 | |
5448 | // As an extension, we allow cast to/from void* to function pointer. | ||||
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5449 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5450 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5451 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5452 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5453 | // 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] | 5454 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5455 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
5456 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { | ||||
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5457 | // Check if the pointee types are compatible ignoring the sign. |
5458 | // We explicitly check for char so that we catch "char" vs | ||||
5459 | // "unsigned char" on systems where "char" is unsigned. | ||||
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5460 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5461 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5462 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5463 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5464 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5465 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5466 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5467 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5468 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5469 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5470 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5471 | // Types are compatible ignoring the sign. Qualifier incompatibility |
5472 | // takes priority over sign incompatibility because the sign | ||||
5473 | // warning can be disabled. | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5474 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5475 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5476 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5477 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5478 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5479 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5480 | // If we are a multi-level pointer, it's possible that our issue is simply |
5481 | // one of qualification - e.g. char ** -> const char ** is not allowed. If | ||||
5482 | // the eventual target type is the same and the pointers have the same | ||||
5483 | // level of indirection, this must be the issue. | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5484 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5485 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5486 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
5487 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5488 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5489 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5490 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5491 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5492 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5493 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5494 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5495 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5496 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5497 | if (!S.getLangOpts().CPlusPlus && |
Fariborz Jahanian | 53c8167 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 5498 | S.IsNoReturnConversion(ltrans, rtrans, ltrans)) |
5499 | return Sema::IncompatiblePointer; | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5500 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5501 | } |
5502 | |||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5503 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5504 | /// block pointer types are compatible or whether a block and normal pointer |
5505 | /// are compatible. It is more restrict than comparing two function pointer | ||||
5506 | // types. | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5507 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5508 | checkBlockPointerTypesForAssignment(Sema &S, QualType LHSType, |
5509 | QualType RHSType) { | ||||
5510 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); | ||||
5511 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5512 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5513 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5514 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5515 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5516 | lhptee = cast<BlockPointerType>(LHSType)->getPointeeType(); |
5517 | rhptee = cast<BlockPointerType>(RHSType)->getPointeeType(); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5518 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5519 | // In C++, the types have to match exactly. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5520 | if (S.getLangOpts().CPlusPlus) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5521 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5522 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5523 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5524 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5525 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5526 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
5527 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5528 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5529 | if (!S.Context.typesAreBlockPointerCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5530 | return Sema::IncompatibleBlockPointer; |
5531 | |||||
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5532 | return ConvTy; |
5533 | } | ||||
5534 | |||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5535 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5536 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5537 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5538 | checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType, |
5539 | QualType RHSType) { | ||||
5540 | assert(LHSType.isCanonical() && "LHS was not canonicalized!"); | ||||
5541 | assert(RHSType.isCanonical() && "RHS was not canonicalized!"); | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5542 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5543 | if (LHSType->isObjCBuiltinType()) { |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5544 | // Class is not compatible with ObjC object pointers. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5545 | if (LHSType->isObjCClassType() && !RHSType->isObjCBuiltinType() && |
5546 | !RHSType->isObjCQualifiedClassType()) | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5547 | return Sema::IncompatiblePointer; |
5548 | return Sema::Compatible; | ||||
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5549 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5550 | if (RHSType->isObjCBuiltinType()) { |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5551 | if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() && |
5552 | !LHSType->isObjCQualifiedClassType()) | ||||
Fariborz Jahanian | 412a496 | 2011-09-15 20:40:18 +0000 | [diff] [blame] | 5553 | return Sema::IncompatiblePointer; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5554 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5555 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5556 | QualType lhptee = LHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
5557 | QualType rhptee = RHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5558 | |
Fariborz Jahanian | f2b4f7b | 2012-01-12 22:12:08 +0000 | [diff] [blame] | 5559 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee) && |
5560 | // make an exception for id<P> | ||||
5561 | !LHSType->isObjCQualifiedIdType()) | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5562 | return Sema::CompatiblePointerDiscardsQualifiers; |
5563 | |||||
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5564 | if (S.Context.typesAreCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5565 | return Sema::Compatible; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5566 | if (LHSType->isObjCQualifiedIdType() || RHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5567 | return Sema::IncompatibleObjCQualifiedId; |
5568 | return Sema::IncompatiblePointer; | ||||
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5569 | } |
5570 | |||||
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5571 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5572 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5573 | QualType LHSType, QualType RHSType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5574 | // Fake up an opaque expression. We don't actually care about what |
5575 | // cast operations are required, so if CheckAssignmentConstraints | ||||
5576 | // adds casts to this they'll be wasted, but fortunately that doesn't | ||||
5577 | // usually happen on valid code. | ||||
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5578 | OpaqueValueExpr RHSExpr(Loc, RHSType, VK_RValue); |
5579 | ExprResult RHSPtr = &RHSExpr; | ||||
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5580 | CastKind K = CK_Invalid; |
5581 | |||||
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5582 | return CheckAssignmentConstraints(LHSType, RHSPtr, K); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5583 | } |
5584 | |||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5585 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
5586 | /// has code to accommodate several GCC extensions when type checking | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5587 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
5588 | /// | ||||
5589 | /// int a, *pint; | ||||
5590 | /// short *pshort; | ||||
5591 | /// struct foo *pfoo; | ||||
5592 | /// | ||||
5593 | /// pint = pshort; // warning: assignment from incompatible pointer type | ||||
5594 | /// a = pint; // warning: assignment makes integer from pointer without a cast | ||||
5595 | /// pint = a; // warning: assignment makes pointer from integer without a cast | ||||
5596 | /// pint = pfoo; // warning: assignment from incompatible pointer type | ||||
5597 | /// | ||||
5598 | /// 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] | 5599 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5600 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5601 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5602 | Sema::AssignConvertType |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5603 | Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5604 | CastKind &Kind) { |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5605 | QualType RHSType = RHS.get()->getType(); |
5606 | QualType OrigLHSType = LHSType; | ||||
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5607 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5608 | // Get canonical types. We're not formatting these types, just comparing |
5609 | // them. | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5610 | LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType(); |
5611 | RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType(); | ||||
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5612 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5613 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5614 | // Common case: no conversion required. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5615 | if (LHSType == RHSType) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5616 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5617 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5618 | } |
5619 | |||||
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 5620 | // If we have an atomic type, try a non-atomic assignment, then just add an |
5621 | // atomic qualification step. | ||||
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5622 | if (const AtomicType *AtomicTy = dyn_cast<AtomicType>(LHSType)) { |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 5623 | Sema::AssignConvertType result = |
5624 | CheckAssignmentConstraints(AtomicTy->getValueType(), RHS, Kind); | ||||
5625 | if (result != Compatible) | ||||
5626 | return result; | ||||
5627 | if (Kind != CK_NoOp) | ||||
5628 | RHS = ImpCastExprToType(RHS.take(), AtomicTy->getValueType(), Kind); | ||||
5629 | Kind = CK_NonAtomicToAtomic; | ||||
5630 | return Compatible; | ||||
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5631 | } |
5632 | |||||
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5633 | // If the left-hand side is a reference type, then we are in a |
5634 | // (rare!) case where we've allowed the use of references in C, | ||||
5635 | // e.g., as a parameter type in a built-in function. In this case, | ||||
5636 | // just make sure that the type referenced is compatible with the | ||||
5637 | // right-hand side type. The caller is responsible for adjusting | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5638 | // LHSType so that the resulting expression does not have reference |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5639 | // type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5640 | if (const ReferenceType *LHSTypeRef = LHSType->getAs<ReferenceType>()) { |
5641 | if (Context.typesAreCompatible(LHSTypeRef->getPointeeType(), RHSType)) { | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5642 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5643 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5644 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5645 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5646 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5647 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5648 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
5649 | // to the same ExtVector type. | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5650 | if (LHSType->isExtVectorType()) { |
5651 | if (RHSType->isExtVectorType()) | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5652 | return Incompatible; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5653 | if (RHSType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5654 | // CK_VectorSplat does T -> vector T, so first cast to the |
5655 | // element type. | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5656 | QualType elType = cast<ExtVectorType>(LHSType)->getElementType(); |
5657 | if (elType != RHSType) { | ||||
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 5658 | Kind = PrepareScalarCast(RHS, elType); |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5659 | RHS = ImpCastExprToType(RHS.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5660 | } |
5661 | Kind = CK_VectorSplat; | ||||
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5662 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5663 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5664 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5665 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5666 | // Conversions to or from vector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5667 | if (LHSType->isVectorType() || RHSType->isVectorType()) { |
5668 | if (LHSType->isVectorType() && RHSType->isVectorType()) { | ||||
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5669 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
5670 | // vector type and vice versa | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5671 | if (Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5672 | Kind = CK_BitCast; |
5673 | return Compatible; | ||||
5674 | } | ||||
5675 | |||||
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5676 | // If we are allowing lax vector conversions, and LHS and RHS are both |
5677 | // vectors, the total size only needs to be the same. This is a bitcast; | ||||
5678 | // no bits are changed but the result type is different. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5679 | if (getLangOpts().LaxVectorConversions && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5680 | (Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5681 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5682 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5683 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5684 | } |
5685 | return Incompatible; | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5686 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5687 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5688 | // Arithmetic conversions. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5689 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5690 | !(getLangOpts().CPlusPlus && LHSType->isEnumeralType())) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 5691 | Kind = PrepareScalarCast(RHS, LHSType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5692 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5693 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5694 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5695 | // Conversions to normal pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5696 | if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5697 | // U* -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5698 | if (isa<PointerType>(RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5699 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5700 | return checkPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5701 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5702 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5703 | // int -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5704 | if (RHSType->isIntegerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5705 | Kind = CK_IntegralToPointer; // FIXME: null? |
5706 | return IntToPointer; | ||||
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5707 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5708 | |
5709 | // C pointers are not compatible with ObjC object pointers, | ||||
5710 | // with two exceptions: | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5711 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5712 | // - conversions to void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5713 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5714 | Kind = CK_BitCast; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5715 | return Compatible; |
5716 | } | ||||
5717 | |||||
5718 | // - conversions from 'Class' to the redefinition type | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5719 | if (RHSType->isObjCClassType() && |
5720 | Context.hasSameType(LHSType, | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5721 | Context.getObjCClassRedefinitionType())) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5722 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5723 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5724 | } |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 5725 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5726 | Kind = CK_BitCast; |
5727 | return IncompatiblePointer; | ||||
5728 | } | ||||
5729 | |||||
5730 | // U^ -> void* | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5731 | if (RHSType->getAs<BlockPointerType>()) { |
5732 | if (LHSPointer->getPointeeType()->isVoidType()) { | ||||
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5733 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5734 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5735 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5736 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5737 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5738 | return Incompatible; |
5739 | } | ||||
5740 | |||||
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5741 | // Conversions to block pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5742 | if (isa<BlockPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5743 | // U^ -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5744 | if (RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5745 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5746 | return checkBlockPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5747 | } |
5748 | |||||
5749 | // int or null -> T^ | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5750 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5751 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5752 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5753 | } |
5754 | |||||
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5755 | // id -> T^ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5756 | if (getLangOpts().ObjC1 && RHSType->isObjCIdType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5757 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5758 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5759 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5760 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5761 | // void* -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5762 | if (const PointerType *RHSPT = RHSType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5763 | if (RHSPT->getPointeeType()->isVoidType()) { |
5764 | Kind = CK_AnyPointerToBlockPointerCast; | ||||
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5765 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5766 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5767 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5768 | return Incompatible; |
5769 | } | ||||
5770 | |||||
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5771 | // Conversions to Objective-C pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5772 | if (isa<ObjCObjectPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5773 | // A* -> B* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5774 | if (RHSType->isObjCObjectPointerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5775 | Kind = CK_BitCast; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5776 | Sema::AssignConvertType result = |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5777 | checkObjCPointerTypesForAssignment(*this, LHSType, RHSType); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5778 | if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5779 | result == Compatible && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5780 | !CheckObjCARCUnavailableWeakConversion(OrigLHSType, RHSType)) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5781 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5782 | return result; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5783 | } |
5784 | |||||
5785 | // int or null -> A* | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5786 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5787 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5788 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5789 | } |
5790 | |||||
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5791 | // In general, C pointers are not compatible with ObjC object pointers, |
5792 | // with two exceptions: | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5793 | if (isa<PointerType>(RHSType)) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5794 | Kind = CK_CPointerToObjCPointerCast; |
5795 | |||||
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5796 | // - conversions from 'void*' |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5797 | if (RHSType->isVoidPointerType()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5798 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5799 | } |
5800 | |||||
5801 | // - conversions to 'Class' from its redefinition type | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5802 | if (LHSType->isObjCClassType() && |
5803 | Context.hasSameType(RHSType, | ||||
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5804 | Context.getObjCClassRedefinitionType())) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5805 | return Compatible; |
5806 | } | ||||
5807 | |||||
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5808 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5809 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5810 | |
5811 | // T^ -> A* | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5812 | if (RHSType->isBlockPointerType()) { |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 5813 | maybeExtendBlockObject(*this, RHS); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5814 | Kind = CK_BlockPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5815 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5816 | } |
5817 | |||||
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5818 | return Incompatible; |
5819 | } | ||||
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5820 | |
5821 | // Conversions from pointers that are not covered by the above. | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5822 | if (isa<PointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5823 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5824 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5825 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5826 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5827 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5828 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5829 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5830 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5831 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5832 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5833 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5834 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5835 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5836 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5837 | |
5838 | // Conversions from Objective-C pointers that are not covered by the above. | ||||
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5839 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5840 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5841 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5842 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5843 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5844 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5845 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5846 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5847 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5848 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5849 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5850 | } |
5851 | |||||
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5852 | return Incompatible; |
5853 | } | ||||
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5854 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5855 | // struct A -> struct B |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5856 | if (isa<TagType>(LHSType) && isa<TagType>(RHSType)) { |
5857 | if (Context.typesAreCompatible(LHSType, RHSType)) { | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5858 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5859 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5860 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5861 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5862 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5863 | return Incompatible; |
5864 | } | ||||
5865 | |||||
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5866 | /// \brief Constructs a transparent union from an expression that is |
5867 | /// used to initialize the transparent union. | ||||
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5868 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
5869 | ExprResult &EResult, QualType UnionType, | ||||
5870 | FieldDecl *Field) { | ||||
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5871 | // Build an initializer list that designates the appropriate member |
5872 | // of the transparent union. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5873 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5874 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5875 | E, SourceLocation()); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5876 | Initializer->setType(UnionType); |
5877 | Initializer->setInitializedFieldInUnion(Field); | ||||
5878 | |||||
5879 | // Build a compound literal constructing a value of the transparent | ||||
5880 | // union type from this initializer list. | ||||
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5881 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5882 | EResult = S.Owned( |
5883 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, | ||||
5884 | VK_RValue, Initializer, false)); | ||||
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5885 | } |
5886 | |||||
5887 | Sema::AssignConvertType | ||||
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5888 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5889 | ExprResult &RHS) { |
5890 | QualType RHSType = RHS.get()->getType(); | ||||
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5891 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5892 | // 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] | 5893 | // transparent_union GCC extension. |
5894 | const RecordType *UT = ArgType->getAsUnionType(); | ||||
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5895 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5896 | return Incompatible; |
5897 | |||||
5898 | // The field to initialize within the transparent union. | ||||
5899 | RecordDecl *UD = UT->getDecl(); | ||||
5900 | FieldDecl *InitField = 0; | ||||
5901 | // It's compatible if the expression matches any of the fields. | ||||
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5902 | for (RecordDecl::field_iterator it = UD->field_begin(), |
5903 | itend = UD->field_end(); | ||||
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5904 | it != itend; ++it) { |
5905 | if (it->getType()->isPointerType()) { | ||||
5906 | // If the transparent union contains a pointer type, we allow: | ||||
5907 | // 1) void pointer | ||||
5908 | // 2) null pointer constant | ||||
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5909 | if (RHSType->isPointerType()) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5910 | if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5911 | RHS = ImpCastExprToType(RHS.take(), it->getType(), CK_BitCast); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5912 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5913 | break; |
5914 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5915 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5916 | if (RHS.get()->isNullPointerConstant(Context, |
5917 | Expr::NPC_ValueDependentIsNull)) { | ||||
5918 | RHS = ImpCastExprToType(RHS.take(), it->getType(), | ||||
5919 | CK_NullToPointer); | ||||
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5920 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5921 | break; |
5922 | } | ||||
5923 | } | ||||
5924 | |||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5925 | CastKind Kind = CK_Invalid; |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5926 | if (CheckAssignmentConstraints(it->getType(), RHS, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5927 | == Compatible) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5928 | RHS = ImpCastExprToType(RHS.take(), it->getType(), Kind); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5929 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5930 | break; |
5931 | } | ||||
5932 | } | ||||
5933 | |||||
5934 | if (!InitField) | ||||
5935 | return Incompatible; | ||||
5936 | |||||
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5937 | ConstructTransparentUnion(*this, Context, RHS, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5938 | return Compatible; |
5939 | } | ||||
5940 | |||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5941 | Sema::AssignConvertType |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5942 | Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
5943 | bool Diagnose) { | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5944 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5945 | if (!LHSType->isRecordType() && !LHSType->isAtomicType()) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5946 | // C++ 5.17p3: If the left operand is not of class type, the |
5947 | // expression is implicitly converted (C++ 4) to the | ||||
5948 | // cv-unqualified type of the left operand. | ||||
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 5949 | ExprResult Res; |
5950 | if (Diagnose) { | ||||
5951 | Res = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), | ||||
5952 | AA_Assigning); | ||||
5953 | } else { | ||||
5954 | ImplicitConversionSequence ICS = | ||||
5955 | TryImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), | ||||
5956 | /*SuppressUserConversions=*/false, | ||||
5957 | /*AllowExplicit=*/false, | ||||
5958 | /*InOverloadResolution=*/false, | ||||
5959 | /*CStyle=*/false, | ||||
5960 | /*AllowObjCWritebackConversion=*/false); | ||||
5961 | if (ICS.isFailure()) | ||||
5962 | return Incompatible; | ||||
5963 | Res = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), | ||||
5964 | ICS, AA_Assigning); | ||||
5965 | } | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5966 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5967 | return Incompatible; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5968 | Sema::AssignConvertType result = Compatible; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5969 | if (getLangOpts().ObjCAutoRefCount && |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5970 | !CheckObjCARCUnavailableWeakConversion(LHSType, |
5971 | RHS.get()->getType())) | ||||
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5972 | result = IncompatibleObjCWeakRef; |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5973 | RHS = Res; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5974 | return result; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5975 | } |
5976 | |||||
5977 | // FIXME: Currently, we fall through and treat C++ classes like C | ||||
5978 | // structures. | ||||
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5979 | // FIXME: We also fall through for atomics; not sure what should |
5980 | // happen there, though. | ||||
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5981 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5982 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5983 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
5984 | // a null pointer constant. | ||||
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5985 | if ((LHSType->isPointerType() || |
5986 | LHSType->isObjCObjectPointerType() || | ||||
5987 | LHSType->isBlockPointerType()) | ||||
5988 | && RHS.get()->isNullPointerConstant(Context, | ||||
5989 | Expr::NPC_ValueDependentIsNull)) { | ||||
5990 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); | ||||
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5991 | return Compatible; |
5992 | } | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5993 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5994 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5995 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5996 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5997 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5998 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5999 | // Suppress this for references: C++ 8.5.3p5. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6000 | if (!LHSType->isReferenceType()) { |
6001 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); | ||||
6002 | if (RHS.isInvalid()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6003 | return Incompatible; |
6004 | } | ||||
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6005 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6006 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6007 | Sema::AssignConvertType result = |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6008 | CheckAssignmentConstraints(LHSType, RHS, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6009 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6010 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
6011 | // type of the assignment expression. | ||||
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6012 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
6013 | // so that we can use references in built-in functions even in C. | ||||
6014 | // The getNonReferenceType() call makes sure that the resulting expression | ||||
6015 | // does not have reference type. | ||||
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6016 | if (result != Incompatible && RHS.get()->getType() != LHSType) |
6017 | RHS = ImpCastExprToType(RHS.take(), | ||||
6018 | LHSType.getNonLValueExprType(Context), Kind); | ||||
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6019 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6020 | } |
6021 | |||||
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6022 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, |
6023 | ExprResult &RHS) { | ||||
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6024 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 6025 | << LHS.get()->getType() << RHS.get()->getType() |
6026 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | ||||
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6027 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6028 | } |
6029 | |||||
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6030 | QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6031 | SourceLocation Loc, bool IsCompAssign) { |
Richard Smith | 9c129f8 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 6032 | if (!IsCompAssign) { |
6033 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); | ||||
6034 | if (LHS.isInvalid()) | ||||
6035 | return QualType(); | ||||
6036 | } | ||||
6037 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); | ||||
6038 | if (RHS.isInvalid()) | ||||
6039 | return QualType(); | ||||
6040 | |||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6041 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6042 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6043 | QualType LHSType = |
6044 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); | ||||
6045 | QualType RHSType = | ||||
6046 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6047 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6048 | // If the vector types are identical, return. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6049 | if (LHSType == RHSType) |
6050 | return LHSType; | ||||
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6051 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6052 | // Handle the case of equivalent AltiVec and GCC vector types |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6053 | if (LHSType->isVectorType() && RHSType->isVectorType() && |
6054 | Context.areCompatibleVectorTypes(LHSType, RHSType)) { | ||||
6055 | if (LHSType->isExtVectorType()) { | ||||
6056 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); | ||||
6057 | return LHSType; | ||||
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6058 | } |
6059 | |||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6060 | if (!IsCompAssign) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6061 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
6062 | return RHSType; | ||||
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6063 | } |
6064 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6065 | if (getLangOpts().LaxVectorConversions && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6066 | Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType)) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6067 | // If we are allowing lax vector conversions, and LHS and RHS are both |
6068 | // vectors, the total size only needs to be the same. This is a | ||||
6069 | // bitcast; no bits are changed but the result type is different. | ||||
6070 | // FIXME: Should we really be allowing this? | ||||
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6071 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
6072 | return LHSType; | ||||
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6073 | } |
6074 | |||||
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6075 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
6076 | // swap back (so that we don't reverse the inputs to a subtract, for instance. | ||||
6077 | bool swapped = false; | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6078 | if (RHSType->isExtVectorType() && !IsCompAssign) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6079 | swapped = true; |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6080 | std::swap(RHS, LHS); |
6081 | std::swap(RHSType, LHSType); | ||||
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6082 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6083 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6084 | // Handle the case of an ext vector and scalar. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6085 | if (const ExtVectorType *LV = LHSType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6086 | QualType EltTy = LV->getElementType(); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6087 | if (EltTy->isIntegralType(Context) && RHSType->isIntegralType(Context)) { |
6088 | int order = Context.getIntegerTypeOrder(EltTy, RHSType); | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6089 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6090 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6091 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6092 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
6093 | if (swapped) std::swap(RHS, LHS); | ||||
6094 | return LHSType; | ||||
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6095 | } |
6096 | } | ||||
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6097 | if (EltTy->isRealFloatingType() && RHSType->isScalarType() && |
6098 | RHSType->isRealFloatingType()) { | ||||
6099 | int order = Context.getFloatingTypeOrder(EltTy, RHSType); | ||||
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6100 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6101 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6102 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6103 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
6104 | if (swapped) std::swap(RHS, LHS); | ||||
6105 | return LHSType; | ||||
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6106 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6107 | } |
6108 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6109 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6110 | // Vectors of different size or scalar and non-ext-vector are errors. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6111 | if (swapped) std::swap(RHS, LHS); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6112 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6113 | << LHS.get()->getType() << RHS.get()->getType() |
6114 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6115 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6116 | } |
6117 | |||||
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6118 | // checkArithmeticNull - Detect when a NULL constant is used improperly in an |
6119 | // expression. These are mainly cases where the null pointer is used as an | ||||
6120 | // integer instead of a pointer. | ||||
6121 | static void checkArithmeticNull(Sema &S, ExprResult &LHS, ExprResult &RHS, | ||||
6122 | SourceLocation Loc, bool IsCompare) { | ||||
6123 | // The canonical way to check for a GNU null is with isNullPointerConstant, | ||||
6124 | // but we use a bit of a hack here for speed; this is a relatively | ||||
6125 | // hot path, and isNullPointerConstant is slow. | ||||
6126 | bool LHSNull = isa<GNUNullExpr>(LHS.get()->IgnoreParenImpCasts()); | ||||
6127 | bool RHSNull = isa<GNUNullExpr>(RHS.get()->IgnoreParenImpCasts()); | ||||
6128 | |||||
6129 | QualType NonNullType = LHSNull ? RHS.get()->getType() : LHS.get()->getType(); | ||||
6130 | |||||
6131 | // Avoid analyzing cases where the result will either be invalid (and | ||||
6132 | // diagnosed as such) or entirely valid and not something to warn about. | ||||
6133 | if ((!LHSNull && !RHSNull) || NonNullType->isBlockPointerType() || | ||||
6134 | NonNullType->isMemberPointerType() || NonNullType->isFunctionType()) | ||||
6135 | return; | ||||
6136 | |||||
6137 | // Comparison operations would not make sense with a null pointer no matter | ||||
6138 | // what the other expression is. | ||||
6139 | if (!IsCompare) { | ||||
6140 | S.Diag(Loc, diag::warn_null_in_arithmetic_operation) | ||||
6141 | << (LHSNull ? LHS.get()->getSourceRange() : SourceRange()) | ||||
6142 | << (RHSNull ? RHS.get()->getSourceRange() : SourceRange()); | ||||
6143 | return; | ||||
6144 | } | ||||
6145 | |||||
6146 | // The rest of the operations only make sense with a null pointer | ||||
6147 | // if the other expression is a pointer. | ||||
6148 | if (LHSNull == RHSNull || NonNullType->isAnyPointerType() || | ||||
6149 | NonNullType->canDecayToPointerType()) | ||||
6150 | return; | ||||
6151 | |||||
6152 | S.Diag(Loc, diag::warn_null_in_comparison_operation) | ||||
6153 | << LHSNull /* LHS is NULL */ << NonNullType | ||||
6154 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | ||||
6155 | } | ||||
6156 | |||||
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6157 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6158 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6159 | bool IsCompAssign, bool IsDiv) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6160 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
6161 | |||||
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6162 | if (LHS.get()->getType()->isVectorType() || |
6163 | RHS.get()->getType()->isVectorType()) | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6164 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6165 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6166 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6167 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6168 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6169 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6170 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6171 | if (compType.isNull() || !compType->isArithmeticType()) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6172 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6173 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6174 | // Check for division by zero. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6175 | if (IsDiv && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6176 | RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6177 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6178 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_division_by_zero) |
6179 | << RHS.get()->getSourceRange()); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6180 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6181 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6182 | } |
6183 | |||||
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6184 | QualType Sema::CheckRemainderOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6185 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6186 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
6187 | |||||
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6188 | if (LHS.get()->getType()->isVectorType() || |
6189 | RHS.get()->getType()->isVectorType()) { | ||||
6190 | if (LHS.get()->getType()->hasIntegerRepresentation() && | ||||
6191 | RHS.get()->getType()->hasIntegerRepresentation()) | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6192 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6193 | return InvalidOperands(Loc, LHS, RHS); |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6194 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6195 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6196 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6197 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6198 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6199 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6200 | if (compType.isNull() || !compType->isIntegerType()) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6201 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6202 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6203 | // Check for remainder by zero. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6204 | if (RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6205 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6206 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_remainder_by_zero) |
6207 | << RHS.get()->getSourceRange()); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6208 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6209 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6210 | } |
6211 | |||||
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6212 | /// \brief Diagnose invalid arithmetic on two void pointers. |
6213 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, | ||||
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6214 | Expr *LHSExpr, Expr *RHSExpr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6215 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6216 | ? diag::err_typecheck_pointer_arith_void_type |
6217 | : diag::ext_gnu_void_ptr) | ||||
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6218 | << 1 /* two pointers */ << LHSExpr->getSourceRange() |
6219 | << RHSExpr->getSourceRange(); | ||||
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6220 | } |
6221 | |||||
6222 | /// \brief Diagnose invalid arithmetic on a void pointer. | ||||
6223 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, | ||||
6224 | Expr *Pointer) { | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6225 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6226 | ? diag::err_typecheck_pointer_arith_void_type |
6227 | : diag::ext_gnu_void_ptr) | ||||
6228 | << 0 /* one pointer */ << Pointer->getSourceRange(); | ||||
6229 | } | ||||
6230 | |||||
6231 | /// \brief Diagnose invalid arithmetic on two function pointers. | ||||
6232 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, | ||||
6233 | Expr *LHS, Expr *RHS) { | ||||
6234 | assert(LHS->getType()->isAnyPointerType()); | ||||
6235 | assert(RHS->getType()->isAnyPointerType()); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6236 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6237 | ? diag::err_typecheck_pointer_arith_function_type |
6238 | : diag::ext_gnu_ptr_func_arith) | ||||
6239 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() | ||||
6240 | // We only show the second type if it differs from the first. | ||||
6241 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), | ||||
6242 | RHS->getType()) | ||||
6243 | << RHS->getType()->getPointeeType() | ||||
6244 | << LHS->getSourceRange() << RHS->getSourceRange(); | ||||
6245 | } | ||||
6246 | |||||
6247 | /// \brief Diagnose invalid arithmetic on a function pointer. | ||||
6248 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, | ||||
6249 | Expr *Pointer) { | ||||
6250 | assert(Pointer->getType()->isAnyPointerType()); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6251 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6252 | ? diag::err_typecheck_pointer_arith_function_type |
6253 | : diag::ext_gnu_ptr_func_arith) | ||||
6254 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() | ||||
6255 | << 0 /* one pointer, so only one type */ | ||||
6256 | << Pointer->getSourceRange(); | ||||
6257 | } | ||||
6258 | |||||
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 6259 | /// \brief Emit error if Operand is incomplete pointer type |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6260 | /// |
6261 | /// \returns True if pointer has incomplete type | ||||
6262 | static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, | ||||
6263 | Expr *Operand) { | ||||
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6264 | assert(Operand->getType()->isAnyPointerType() && |
6265 | !Operand->getType()->isDependentType()); | ||||
6266 | QualType PointeeTy = Operand->getType()->getPointeeType(); | ||||
6267 | return S.RequireCompleteType(Loc, PointeeTy, | ||||
6268 | diag::err_typecheck_arithmetic_incomplete_type, | ||||
6269 | PointeeTy, Operand->getSourceRange()); | ||||
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6270 | } |
6271 | |||||
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6272 | /// \brief Check the validity of an arithmetic pointer operand. |
6273 | /// | ||||
6274 | /// If the operand has pointer type, this code will check for pointer types | ||||
6275 | /// which are invalid in arithmetic operations. These will be diagnosed | ||||
6276 | /// appropriately, including whether or not the use is supported as an | ||||
6277 | /// extension. | ||||
6278 | /// | ||||
6279 | /// \returns True when the operand is valid to use (even if as an extension). | ||||
6280 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, | ||||
6281 | Expr *Operand) { | ||||
6282 | if (!Operand->getType()->isAnyPointerType()) return true; | ||||
6283 | |||||
6284 | QualType PointeeTy = Operand->getType()->getPointeeType(); | ||||
6285 | if (PointeeTy->isVoidType()) { | ||||
6286 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6287 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6288 | } |
6289 | if (PointeeTy->isFunctionType()) { | ||||
6290 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6291 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6292 | } |
6293 | |||||
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6294 | if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6295 | |
6296 | return true; | ||||
6297 | } | ||||
6298 | |||||
6299 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer | ||||
6300 | /// operands. | ||||
6301 | /// | ||||
6302 | /// This routine will diagnose any invalid arithmetic on pointer operands much | ||||
6303 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic | ||||
6304 | /// for emitting a single diagnostic even for operations where both LHS and RHS | ||||
6305 | /// are (potentially problematic) pointers. | ||||
6306 | /// | ||||
6307 | /// \returns True when the operand is valid to use (even if as an extension). | ||||
6308 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, | ||||
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6309 | Expr *LHSExpr, Expr *RHSExpr) { |
6310 | bool isLHSPointer = LHSExpr->getType()->isAnyPointerType(); | ||||
6311 | bool isRHSPointer = RHSExpr->getType()->isAnyPointerType(); | ||||
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6312 | if (!isLHSPointer && !isRHSPointer) return true; |
6313 | |||||
6314 | QualType LHSPointeeTy, RHSPointeeTy; | ||||
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6315 | if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType(); |
6316 | if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType(); | ||||
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6317 | |
6318 | // Check for arithmetic on pointers to incomplete types. | ||||
6319 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); | ||||
6320 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); | ||||
6321 | if (isLHSVoidPtr || isRHSVoidPtr) { | ||||
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6322 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHSExpr); |
6323 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHSExpr); | ||||
6324 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHSExpr, RHSExpr); | ||||
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6325 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6326 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6327 | } |
6328 | |||||
6329 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); | ||||
6330 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); | ||||
6331 | if (isLHSFuncPtr || isRHSFuncPtr) { | ||||
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6332 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHSExpr); |
6333 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, | ||||
6334 | RHSExpr); | ||||
6335 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHSExpr, RHSExpr); | ||||
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6336 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6337 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6338 | } |
6339 | |||||
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6340 | if (isLHSPointer && checkArithmeticIncompletePointerType(S, Loc, LHSExpr)) |
6341 | return false; | ||||
6342 | if (isRHSPointer && checkArithmeticIncompletePointerType(S, Loc, RHSExpr)) | ||||
6343 | return false; | ||||
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6344 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6345 | return true; |
6346 | } | ||||
6347 | |||||
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6348 | /// diagnoseStringPlusInt - Emit a warning when adding an integer to a string |
6349 | /// literal. | ||||
6350 | static void diagnoseStringPlusInt(Sema &Self, SourceLocation OpLoc, | ||||
6351 | Expr *LHSExpr, Expr *RHSExpr) { | ||||
6352 | StringLiteral* StrExpr = dyn_cast<StringLiteral>(LHSExpr->IgnoreImpCasts()); | ||||
6353 | Expr* IndexExpr = RHSExpr; | ||||
6354 | if (!StrExpr) { | ||||
6355 | StrExpr = dyn_cast<StringLiteral>(RHSExpr->IgnoreImpCasts()); | ||||
6356 | IndexExpr = LHSExpr; | ||||
6357 | } | ||||
6358 | |||||
6359 | bool IsStringPlusInt = StrExpr && | ||||
6360 | IndexExpr->getType()->isIntegralOrUnscopedEnumerationType(); | ||||
6361 | if (!IsStringPlusInt) | ||||
6362 | return; | ||||
6363 | |||||
6364 | llvm::APSInt index; | ||||
6365 | if (IndexExpr->EvaluateAsInt(index, Self.getASTContext())) { | ||||
6366 | unsigned StrLenWithNull = StrExpr->getLength() + 1; | ||||
6367 | if (index.isNonNegative() && | ||||
6368 | index <= llvm::APSInt(llvm::APInt(index.getBitWidth(), StrLenWithNull), | ||||
6369 | index.isUnsigned())) | ||||
6370 | return; | ||||
6371 | } | ||||
6372 | |||||
6373 | SourceRange DiagRange(LHSExpr->getLocStart(), RHSExpr->getLocEnd()); | ||||
6374 | Self.Diag(OpLoc, diag::warn_string_plus_int) | ||||
6375 | << DiagRange << IndexExpr->IgnoreImpCasts()->getType(); | ||||
6376 | |||||
6377 | // Only print a fixit for "str" + int, not for int + "str". | ||||
6378 | if (IndexExpr == RHSExpr) { | ||||
6379 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(RHSExpr->getLocEnd()); | ||||
6380 | Self.Diag(OpLoc, diag::note_string_plus_int_silence) | ||||
6381 | << FixItHint::CreateInsertion(LHSExpr->getLocStart(), "&") | ||||
6382 | << FixItHint::CreateReplacement(SourceRange(OpLoc), "[") | ||||
6383 | << FixItHint::CreateInsertion(EndLoc, "]"); | ||||
6384 | } else | ||||
6385 | Self.Diag(OpLoc, diag::note_string_plus_int_silence); | ||||
6386 | } | ||||
6387 | |||||
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 6388 | /// \brief Emit error when two pointers are incompatible. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6389 | static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6390 | Expr *LHSExpr, Expr *RHSExpr) { |
6391 | assert(LHSExpr->getType()->isAnyPointerType()); | ||||
6392 | assert(RHSExpr->getType()->isAnyPointerType()); | ||||
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6393 | S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6394 | << LHSExpr->getType() << RHSExpr->getType() << LHSExpr->getSourceRange() |
6395 | << RHSExpr->getSourceRange(); | ||||
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6396 | } |
6397 | |||||
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6398 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6399 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc, |
6400 | QualType* CompLHSTy) { | ||||
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6401 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
6402 | |||||
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6403 | if (LHS.get()->getType()->isVectorType() || |
6404 | RHS.get()->getType()->isVectorType()) { | ||||
6405 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); | ||||
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6406 | if (CompLHSTy) *CompLHSTy = compType; |
6407 | return compType; | ||||
6408 | } | ||||
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6409 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6410 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
6411 | if (LHS.isInvalid() || RHS.isInvalid()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6412 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6413 | |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6414 | // Diagnose "string literal" '+' int. |
6415 | if (Opc == BO_Add) | ||||
6416 | diagnoseStringPlusInt(*this, Loc, LHS.get(), RHS.get()); | ||||
6417 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6418 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6419 | if (!compType.isNull() && compType->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6420 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6421 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6422 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6423 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6424 | // Type-checking. Ultimately the pointer's going to be in PExp; |
6425 | // note that we bias towards the LHS being the pointer. | ||||
6426 | Expr *PExp = LHS.get(), *IExp = RHS.get(); | ||||
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6427 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6428 | bool isObjCPointer; |
6429 | if (PExp->getType()->isPointerType()) { | ||||
6430 | isObjCPointer = false; | ||||
6431 | } else if (PExp->getType()->isObjCObjectPointerType()) { | ||||
6432 | isObjCPointer = true; | ||||
6433 | } else { | ||||
6434 | std::swap(PExp, IExp); | ||||
6435 | if (PExp->getType()->isPointerType()) { | ||||
6436 | isObjCPointer = false; | ||||
6437 | } else if (PExp->getType()->isObjCObjectPointerType()) { | ||||
6438 | isObjCPointer = true; | ||||
6439 | } else { | ||||
6440 | return InvalidOperands(Loc, LHS, RHS); | ||||
6441 | } | ||||
6442 | } | ||||
6443 | assert(PExp->getType()->isAnyPointerType()); | ||||
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6444 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6445 | if (!IExp->getType()->isIntegerType()) |
6446 | return InvalidOperands(Loc, LHS, RHS); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6447 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6448 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
6449 | return QualType(); | ||||
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6450 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6451 | if (isObjCPointer && checkArithmeticOnObjCPointer(*this, Loc, PExp)) |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6452 | return QualType(); |
6453 | |||||
6454 | // Check array bounds for pointer arithemtic | ||||
6455 | CheckArrayAccess(PExp, IExp); | ||||
6456 | |||||
6457 | if (CompLHSTy) { | ||||
6458 | QualType LHSTy = Context.isPromotableBitField(LHS.get()); | ||||
6459 | if (LHSTy.isNull()) { | ||||
6460 | LHSTy = LHS.get()->getType(); | ||||
6461 | if (LHSTy->isPromotableIntegerType()) | ||||
6462 | LHSTy = Context.getPromotedIntegerType(LHSTy); | ||||
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6463 | } |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6464 | *CompLHSTy = LHSTy; |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6465 | } |
6466 | |||||
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6467 | return PExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6468 | } |
6469 | |||||
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6470 | // C99 6.5.6 |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6471 | QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6472 | SourceLocation Loc, |
6473 | QualType* CompLHSTy) { | ||||
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6474 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
6475 | |||||
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6476 | if (LHS.get()->getType()->isVectorType() || |
6477 | RHS.get()->getType()->isVectorType()) { | ||||
6478 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); | ||||
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6479 | if (CompLHSTy) *CompLHSTy = compType; |
6480 | return compType; | ||||
6481 | } | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6482 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6483 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
6484 | if (LHS.isInvalid() || RHS.isInvalid()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6485 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6486 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6487 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6488 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6489 | // Handle the common case first (both operands are arithmetic). |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6490 | if (!compType.isNull() && compType->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6491 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6492 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6493 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6494 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6495 | // Either ptr - int or ptr - ptr. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6496 | if (LHS.get()->getType()->isAnyPointerType()) { |
6497 | QualType lpointee = LHS.get()->getType()->getPointeeType(); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6498 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6499 | // Diagnose bad cases where we step over interface counts. |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6500 | if (LHS.get()->getType()->isObjCObjectPointerType() && |
6501 | checkArithmeticOnObjCPointer(*this, Loc, LHS.get())) | ||||
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6502 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6503 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6504 | // The result type of a pointer-int computation is the pointer type. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6505 | if (RHS.get()->getType()->isIntegerType()) { |
6506 | if (!checkArithmeticOpPointerOperand(*this, Loc, LHS.get())) | ||||
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6507 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6508 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6509 | // Check array bounds for pointer arithemtic |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 6510 | CheckArrayAccess(LHS.get(), RHS.get(), /*ArraySubscriptExpr*/0, |
6511 | /*AllowOnePastEnd*/true, /*IndexNegated*/true); | ||||
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6512 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6513 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
6514 | return LHS.get()->getType(); | ||||
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6515 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6516 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6517 | // Handle pointer-pointer subtractions. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6518 | if (const PointerType *RHSPTy |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6519 | = RHS.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6520 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6521 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6522 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6523 | // Pointee types must be the same: C++ [expr.add] |
6524 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { | ||||
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6525 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6526 | } |
6527 | } else { | ||||
6528 | // Pointee types must be compatible C99 6.5.6p3 | ||||
6529 | if (!Context.typesAreCompatible( | ||||
6530 | Context.getCanonicalType(lpointee).getUnqualifiedType(), | ||||
6531 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { | ||||
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6532 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6533 | return QualType(); |
6534 | } | ||||
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6535 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6536 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6537 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6538 | LHS.get(), RHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6539 | return QualType(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6540 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6541 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6542 | return Context.getPointerDiffType(); |
6543 | } | ||||
6544 | } | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6545 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6546 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6547 | } |
6548 | |||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6549 | static bool isScopedEnumerationType(QualType T) { |
6550 | if (const EnumType *ET = dyn_cast<EnumType>(T)) | ||||
6551 | return ET->getDecl()->isScoped(); | ||||
6552 | return false; | ||||
6553 | } | ||||
6554 | |||||
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6555 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6556 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6557 | QualType LHSType) { |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6558 | llvm::APSInt Right; |
6559 | // Check right/shifter operand | ||||
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6560 | if (RHS.get()->isValueDependent() || |
6561 | !RHS.get()->isIntegerConstantExpr(Right, S.Context)) | ||||
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6562 | return; |
6563 | |||||
6564 | if (Right.isNegative()) { | ||||
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6565 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6566 | S.PDiag(diag::warn_shift_negative) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6567 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6568 | return; |
6569 | } | ||||
6570 | llvm::APInt LeftBits(Right.getBitWidth(), | ||||
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6571 | S.Context.getTypeSize(LHS.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6572 | if (Right.uge(LeftBits)) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6573 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6574 | S.PDiag(diag::warn_shift_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6575 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6576 | return; |
6577 | } | ||||
6578 | if (Opc != BO_Shl) | ||||
6579 | return; | ||||
6580 | |||||
6581 | // When left shifting an ICE which is signed, we can check for overflow which | ||||
6582 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned | ||||
6583 | // integers have defined behavior modulo one more than the maximum value | ||||
6584 | // representable in the result type, so never warn for those. | ||||
6585 | llvm::APSInt Left; | ||||
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6586 | if (LHS.get()->isValueDependent() || |
6587 | !LHS.get()->isIntegerConstantExpr(Left, S.Context) || | ||||
6588 | LHSType->hasUnsignedIntegerRepresentation()) | ||||
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6589 | return; |
6590 | llvm::APInt ResultBits = | ||||
6591 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); | ||||
6592 | if (LeftBits.uge(ResultBits)) | ||||
6593 | return; | ||||
6594 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); | ||||
6595 | Result = Result.shl(Right); | ||||
6596 | |||||
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6597 | // Print the bit representation of the signed integer as an unsigned |
6598 | // hexadecimal number. | ||||
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6599 | SmallString<40> HexResult; |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6600 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
6601 | |||||
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6602 | // If we are only missing a sign bit, this is less likely to result in actual |
6603 | // bugs -- if the result is cast back to an unsigned type, it will have the | ||||
6604 | // expected value. Thus we place this behind a different warning that can be | ||||
6605 | // turned off separately if needed. | ||||
6606 | if (LeftBits == ResultBits - 1) { | ||||
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6607 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6608 | << HexResult.str() << LHSType |
6609 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | ||||
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6610 | return; |
6611 | } | ||||
6612 | |||||
6613 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) | ||||
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6614 | << HexResult.str() << Result.getMinSignedBits() << LHSType |
6615 | << Left.getBitWidth() << LHS.get()->getSourceRange() | ||||
6616 | << RHS.get()->getSourceRange(); | ||||
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6617 | } |
6618 | |||||
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6619 | // C99 6.5.7 |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6620 | QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6621 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6622 | bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6623 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
6624 | |||||
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6625 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6626 | if (!LHS.get()->getType()->hasIntegerRepresentation() || |
6627 | !RHS.get()->getType()->hasIntegerRepresentation()) | ||||
6628 | return InvalidOperands(Loc, LHS, RHS); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6629 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6630 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
6631 | // hasIntegerRepresentation() above instead of this. | ||||
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6632 | if (isScopedEnumerationType(LHS.get()->getType()) || |
6633 | isScopedEnumerationType(RHS.get()->getType())) { | ||||
6634 | return InvalidOperands(Loc, LHS, RHS); | ||||
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6635 | } |
6636 | |||||
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6637 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6638 | if (LHS.get()->getType()->isVectorType() || |
6639 | RHS.get()->getType()->isVectorType()) | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6640 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6641 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6642 | // Shifts don't perform usual arithmetic conversions, they just do integer |
6643 | // promotions on each operand. C99 6.5.7p3 | ||||
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6644 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6645 | // For the LHS, do usual unary conversions, but then reset them away |
6646 | // if this is a compound assignment. | ||||
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6647 | ExprResult OldLHS = LHS; |
6648 | LHS = UsualUnaryConversions(LHS.take()); | ||||
6649 | if (LHS.isInvalid()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6650 | return QualType(); |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6651 | QualType LHSType = LHS.get()->getType(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6652 | if (IsCompAssign) LHS = OldLHS; |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6653 | |
6654 | // The RHS is simpler. | ||||
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6655 | RHS = UsualUnaryConversions(RHS.take()); |
6656 | if (RHS.isInvalid()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6657 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6658 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6659 | // Sanity-check shift operands |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6660 | DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6661 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6662 | // "The type of the result is that of the promoted left operand." |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6663 | return LHSType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6664 | } |
6665 | |||||
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6666 | static bool IsWithinTemplateSpecialization(Decl *D) { |
6667 | if (DeclContext *DC = D->getDeclContext()) { | ||||
6668 | if (isa<ClassTemplateSpecializationDecl>(DC)) | ||||
6669 | return true; | ||||
6670 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) | ||||
6671 | return FD->isFunctionTemplateSpecialization(); | ||||
6672 | } | ||||
6673 | return false; | ||||
6674 | } | ||||
6675 | |||||
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6676 | /// If two different enums are compared, raise a warning. |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6677 | static void checkEnumComparison(Sema &S, SourceLocation Loc, ExprResult &LHS, |
6678 | ExprResult &RHS) { | ||||
6679 | QualType LHSStrippedType = LHS.get()->IgnoreParenImpCasts()->getType(); | ||||
6680 | QualType RHSStrippedType = RHS.get()->IgnoreParenImpCasts()->getType(); | ||||
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6681 | |
6682 | const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>(); | ||||
6683 | if (!LHSEnumType) | ||||
6684 | return; | ||||
6685 | const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>(); | ||||
6686 | if (!RHSEnumType) | ||||
6687 | return; | ||||
6688 | |||||
6689 | // Ignore anonymous enums. | ||||
6690 | if (!LHSEnumType->getDecl()->getIdentifier()) | ||||
6691 | return; | ||||
6692 | if (!RHSEnumType->getDecl()->getIdentifier()) | ||||
6693 | return; | ||||
6694 | |||||
6695 | if (S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) | ||||
6696 | return; | ||||
6697 | |||||
6698 | S.Diag(Loc, diag::warn_comparison_of_mixed_enum_types) | ||||
6699 | << LHSStrippedType << RHSStrippedType | ||||
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6700 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6701 | } |
6702 | |||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6703 | /// \brief Diagnose bad pointer comparisons. |
6704 | static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, | ||||
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6705 | ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6706 | bool IsError) { |
6707 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_distinct_pointers | ||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6708 | : diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6709 | << LHS.get()->getType() << RHS.get()->getType() |
6710 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | ||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6711 | } |
6712 | |||||
6713 | /// \brief Returns false if the pointers are converted to a composite type, | ||||
6714 | /// true otherwise. | ||||
6715 | static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, | ||||
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6716 | ExprResult &LHS, ExprResult &RHS) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6717 | // C++ [expr.rel]p2: |
6718 | // [...] Pointer conversions (4.10) and qualification | ||||
6719 | // conversions (4.4) are performed on pointer operands (or on | ||||
6720 | // a pointer operand and a null pointer constant) to bring | ||||
6721 | // them to their composite pointer type. [...] | ||||
6722 | // | ||||
6723 | // C++ [expr.eq]p1 uses the same notion for (in)equality | ||||
6724 | // comparisons of pointers. | ||||
6725 | |||||
6726 | // C++ [expr.eq]p2: | ||||
6727 | // In addition, pointers to members can be compared, or a pointer to | ||||
6728 | // member and a null pointer constant. Pointer to member conversions | ||||
6729 | // (4.11) and qualification conversions (4.4) are performed to bring | ||||
6730 | // them to a common type. If one operand is a null pointer constant, | ||||
6731 | // the common type is the type of the other operand. Otherwise, the | ||||
6732 | // common type is a pointer to member type similar (4.4) to the type | ||||
6733 | // of one of the operands, with a cv-qualification signature (4.4) | ||||
6734 | // that is the union of the cv-qualification signatures of the operand | ||||
6735 | // types. | ||||
6736 | |||||
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6737 | QualType LHSType = LHS.get()->getType(); |
6738 | QualType RHSType = RHS.get()->getType(); | ||||
6739 | assert((LHSType->isPointerType() && RHSType->isPointerType()) || | ||||
6740 | (LHSType->isMemberPointerType() && RHSType->isMemberPointerType())); | ||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6741 | |
6742 | bool NonStandardCompositeType = false; | ||||
Richard Trieu | 43dff1b | 2011-09-02 21:44:27 +0000 | [diff] [blame] | 6743 | bool *BoolPtr = S.isSFINAEContext() ? 0 : &NonStandardCompositeType; |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6744 | QualType T = S.FindCompositePointerType(Loc, LHS, RHS, BoolPtr); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6745 | if (T.isNull()) { |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6746 | diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6747 | return true; |
6748 | } | ||||
6749 | |||||
6750 | if (NonStandardCompositeType) | ||||
6751 | S.Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) | ||||
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6752 | << LHSType << RHSType << T << LHS.get()->getSourceRange() |
6753 | << RHS.get()->getSourceRange(); | ||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6754 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6755 | LHS = S.ImpCastExprToType(LHS.take(), T, CK_BitCast); |
6756 | RHS = S.ImpCastExprToType(RHS.take(), T, CK_BitCast); | ||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6757 | return false; |
6758 | } | ||||
6759 | |||||
6760 | static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, | ||||
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6761 | ExprResult &LHS, |
6762 | ExprResult &RHS, | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6763 | bool IsError) { |
6764 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_fptr_to_void | ||||
6765 | : diag::ext_typecheck_comparison_of_fptr_to_void) | ||||
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6766 | << LHS.get()->getType() << RHS.get()->getType() |
6767 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | ||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6768 | } |
6769 | |||||
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6770 | static bool isObjCObjectLiteral(ExprResult &E) { |
6771 | switch (E.get()->getStmtClass()) { | ||||
6772 | case Stmt::ObjCArrayLiteralClass: | ||||
6773 | case Stmt::ObjCDictionaryLiteralClass: | ||||
6774 | case Stmt::ObjCStringLiteralClass: | ||||
6775 | case Stmt::ObjCBoxedExprClass: | ||||
6776 | return true; | ||||
6777 | default: | ||||
6778 | // Note that ObjCBoolLiteral is NOT an object literal! | ||||
6779 | return false; | ||||
6780 | } | ||||
6781 | } | ||||
6782 | |||||
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6783 | static bool hasIsEqualMethod(Sema &S, const Expr *LHS, const Expr *RHS) { |
6784 | // Get the LHS object's interface type. | ||||
6785 | QualType Type = LHS->getType(); | ||||
6786 | QualType InterfaceType; | ||||
6787 | if (const ObjCObjectPointerType *PTy = Type->getAs<ObjCObjectPointerType>()) { | ||||
6788 | InterfaceType = PTy->getPointeeType(); | ||||
6789 | if (const ObjCObjectType *iQFaceTy = | ||||
6790 | InterfaceType->getAsObjCQualifiedInterfaceType()) | ||||
6791 | InterfaceType = iQFaceTy->getBaseType(); | ||||
6792 | } else { | ||||
6793 | // If this is not actually an Objective-C object, bail out. | ||||
6794 | return false; | ||||
6795 | } | ||||
6796 | |||||
6797 | // If the RHS isn't an Objective-C object, bail out. | ||||
6798 | if (!RHS->getType()->isObjCObjectPointerType()) | ||||
6799 | return false; | ||||
6800 | |||||
6801 | // Try to find the -isEqual: method. | ||||
6802 | Selector IsEqualSel = S.NSAPIObj->getIsEqualSelector(); | ||||
6803 | ObjCMethodDecl *Method = S.LookupMethodInObjectType(IsEqualSel, | ||||
6804 | InterfaceType, | ||||
6805 | /*instance=*/true); | ||||
6806 | if (!Method) { | ||||
6807 | if (Type->isObjCIdType()) { | ||||
6808 | // For 'id', just check the global pool. | ||||
6809 | Method = S.LookupInstanceMethodInGlobalPool(IsEqualSel, SourceRange(), | ||||
6810 | /*receiverId=*/true, | ||||
6811 | /*warn=*/false); | ||||
6812 | } else { | ||||
6813 | // Check protocols. | ||||
6814 | Method = S.LookupMethodInQualifiedType(IsEqualSel, | ||||
6815 | cast<ObjCObjectPointerType>(Type), | ||||
6816 | /*instance=*/true); | ||||
6817 | } | ||||
6818 | } | ||||
6819 | |||||
6820 | if (!Method) | ||||
6821 | return false; | ||||
6822 | |||||
6823 | QualType T = Method->param_begin()[0]->getType(); | ||||
6824 | if (!T->isObjCObjectPointerType()) | ||||
6825 | return false; | ||||
6826 | |||||
6827 | QualType R = Method->getResultType(); | ||||
6828 | if (!R->isScalarType()) | ||||
6829 | return false; | ||||
6830 | |||||
6831 | return true; | ||||
6832 | } | ||||
6833 | |||||
6834 | static void diagnoseObjCLiteralComparison(Sema &S, SourceLocation Loc, | ||||
6835 | ExprResult &LHS, ExprResult &RHS, | ||||
6836 | BinaryOperator::Opcode Opc){ | ||||
Jordan Rose | d5209ae | 2012-07-17 17:46:48 +0000 | [diff] [blame] | 6837 | Expr *Literal; |
6838 | Expr *Other; | ||||
6839 | if (isObjCObjectLiteral(LHS)) { | ||||
6840 | Literal = LHS.get(); | ||||
6841 | Other = RHS.get(); | ||||
6842 | } else { | ||||
6843 | Literal = RHS.get(); | ||||
6844 | Other = LHS.get(); | ||||
6845 | } | ||||
6846 | |||||
6847 | // Don't warn on comparisons against nil. | ||||
6848 | Other = Other->IgnoreParenCasts(); | ||||
6849 | if (Other->isNullPointerConstant(S.getASTContext(), | ||||
6850 | Expr::NPC_ValueDependentIsNotNull)) | ||||
6851 | return; | ||||
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6852 | |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6853 | // This should be kept in sync with warn_objc_literal_comparison. |
Jordan Rose | d5209ae | 2012-07-17 17:46:48 +0000 | [diff] [blame] | 6854 | // 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] | 6855 | enum { |
6856 | LK_Array, | ||||
6857 | LK_Dictionary, | ||||
6858 | LK_Numeric, | ||||
6859 | LK_Boxed, | ||||
6860 | LK_String | ||||
6861 | } LiteralKind; | ||||
6862 | |||||
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6863 | switch (Literal->getStmtClass()) { |
6864 | case Stmt::ObjCStringLiteralClass: | ||||
6865 | // "string literal" | ||||
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6866 | LiteralKind = LK_String; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6867 | break; |
6868 | case Stmt::ObjCArrayLiteralClass: | ||||
6869 | // "array literal" | ||||
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6870 | LiteralKind = LK_Array; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6871 | break; |
6872 | case Stmt::ObjCDictionaryLiteralClass: | ||||
6873 | // "dictionary literal" | ||||
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6874 | LiteralKind = LK_Dictionary; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6875 | break; |
6876 | case Stmt::ObjCBoxedExprClass: { | ||||
6877 | Expr *Inner = cast<ObjCBoxedExpr>(Literal)->getSubExpr(); | ||||
6878 | switch (Inner->getStmtClass()) { | ||||
6879 | case Stmt::IntegerLiteralClass: | ||||
6880 | case Stmt::FloatingLiteralClass: | ||||
6881 | case Stmt::CharacterLiteralClass: | ||||
6882 | case Stmt::ObjCBoolLiteralExprClass: | ||||
6883 | case Stmt::CXXBoolLiteralExprClass: | ||||
6884 | // "numeric literal" | ||||
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6885 | LiteralKind = LK_Numeric; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6886 | break; |
6887 | case Stmt::ImplicitCastExprClass: { | ||||
6888 | CastKind CK = cast<CastExpr>(Inner)->getCastKind(); | ||||
6889 | // Boolean literals can be represented by implicit casts. | ||||
6890 | if (CK == CK_IntegralToBoolean || CK == CK_IntegralCast) { | ||||
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6891 | LiteralKind = LK_Numeric; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6892 | break; |
6893 | } | ||||
6894 | // FALLTHROUGH | ||||
6895 | } | ||||
6896 | default: | ||||
6897 | // "boxed expression" | ||||
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6898 | LiteralKind = LK_Boxed; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6899 | break; |
6900 | } | ||||
6901 | break; | ||||
6902 | } | ||||
6903 | default: | ||||
6904 | llvm_unreachable("Unknown Objective-C object literal kind"); | ||||
6905 | } | ||||
6906 | |||||
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6907 | if (LiteralKind == LK_String) |
6908 | S.Diag(Loc, diag::warn_objc_string_literal_comparison) | ||||
6909 | << Literal->getSourceRange(); | ||||
6910 | else | ||||
6911 | S.Diag(Loc, diag::warn_objc_literal_comparison) | ||||
6912 | << LiteralKind << Literal->getSourceRange(); | ||||
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6913 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6914 | if (BinaryOperator::isEqualityOp(Opc) && |
6915 | hasIsEqualMethod(S, LHS.get(), RHS.get())) { | ||||
6916 | SourceLocation Start = LHS.get()->getLocStart(); | ||||
6917 | SourceLocation End = S.PP.getLocForEndOfToken(RHS.get()->getLocEnd()); | ||||
6918 | SourceRange OpRange(Loc, S.PP.getLocForEndOfToken(Loc)); | ||||
Jordan Rose | 6deae7c | 2012-07-09 16:54:44 +0000 | [diff] [blame] | 6919 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6920 | S.Diag(Loc, diag::note_objc_literal_comparison_isequal) |
6921 | << FixItHint::CreateInsertion(Start, Opc == BO_EQ ? "[" : "![") | ||||
6922 | << FixItHint::CreateReplacement(OpRange, "isEqual:") | ||||
6923 | << FixItHint::CreateInsertion(End, "]"); | ||||
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6924 | } |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6925 | } |
6926 | |||||
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6927 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6928 | QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6929 | SourceLocation Loc, unsigned OpaqueOpc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6930 | bool IsRelational) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6931 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/true); |
6932 | |||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6933 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6934 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6935 | // Handle vector comparisons separately. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6936 | if (LHS.get()->getType()->isVectorType() || |
6937 | RHS.get()->getType()->isVectorType()) | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6938 | return CheckVectorCompareOperands(LHS, RHS, Loc, IsRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6939 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6940 | QualType LHSType = LHS.get()->getType(); |
6941 | QualType RHSType = RHS.get()->getType(); | ||||
Benjamin Kramer | fec0959 | 2011-09-03 08:46:20 +0000 | [diff] [blame] | 6942 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6943 | Expr *LHSStripped = LHS.get()->IgnoreParenImpCasts(); |
6944 | Expr *RHSStripped = RHS.get()->IgnoreParenImpCasts(); | ||||
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6945 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6946 | checkEnumComparison(*this, Loc, LHS, RHS); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6947 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6948 | if (!LHSType->hasFloatingRepresentation() && |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6949 | !(LHSType->isBlockPointerType() && IsRelational) && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6950 | !LHS.get()->getLocStart().isMacroID() && |
6951 | !RHS.get()->getLocStart().isMacroID()) { | ||||
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6952 | // For non-floating point types, check for self-comparisons of the form |
6953 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and | ||||
6954 | // often indicate logic errors in the program. | ||||
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6955 | // |
6956 | // NOTE: Don't warn about comparison expressions resulting from macro | ||||
6957 | // expansion. Also don't warn about comparisons which are only self | ||||
6958 | // comparisons within a template specialization. The warnings should catch | ||||
6959 | // obvious cases in the definition of the template anyways. The idea is to | ||||
6960 | // warn when the typed comparison operator will always evaluate to the same | ||||
6961 | // result. | ||||
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6962 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6963 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6964 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6965 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6966 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6967 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6968 | << (Opc == BO_EQ |
6969 | || Opc == BO_LE | ||||
6970 | || Opc == BO_GE)); | ||||
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6971 | } else if (LHSType->isArrayType() && RHSType->isArrayType() && |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6972 | !DRL->getDecl()->getType()->isReferenceType() && |
6973 | !DRR->getDecl()->getType()->isReferenceType()) { | ||||
6974 | // what is it always going to eval to? | ||||
6975 | char always_evals_to; | ||||
6976 | switch(Opc) { | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6977 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6978 | always_evals_to = 0; // false |
6979 | break; | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6980 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6981 | always_evals_to = 1; // true |
6982 | break; | ||||
6983 | default: | ||||
6984 | // best we can say is 'a constant' | ||||
6985 | always_evals_to = 2; // e.g. array1 <= array2 | ||||
6986 | break; | ||||
6987 | } | ||||
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6988 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6989 | << 1 // array |
6990 | << always_evals_to); | ||||
6991 | } | ||||
6992 | } | ||||
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6993 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6994 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6995 | if (isa<CastExpr>(LHSStripped)) |
6996 | LHSStripped = LHSStripped->IgnoreParenCasts(); | ||||
6997 | if (isa<CastExpr>(RHSStripped)) | ||||
6998 | RHSStripped = RHSStripped->IgnoreParenCasts(); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6999 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7000 | // Warn about comparisons against a string constant (unless the other |
7001 | // operand is null), the user probably wants strcmp. | ||||
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7002 | Expr *literalString = 0; |
7003 | Expr *literalStringStripped = 0; | ||||
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7004 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7005 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7006 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7007 | literalString = LHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7008 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7009 | } else if ((isa<StringLiteral>(RHSStripped) || |
7010 | isa<ObjCEncodeExpr>(RHSStripped)) && | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7011 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7012 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7013 | literalString = RHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7014 | literalStringStripped = RHSStripped; |
7015 | } | ||||
7016 | |||||
7017 | if (literalString) { | ||||
7018 | std::string resultComparison; | ||||
7019 | switch (Opc) { | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7020 | case BO_LT: resultComparison = ") < 0"; break; |
7021 | case BO_GT: resultComparison = ") > 0"; break; | ||||
7022 | case BO_LE: resultComparison = ") <= 0"; break; | ||||
7023 | case BO_GE: resultComparison = ") >= 0"; break; | ||||
7024 | case BO_EQ: resultComparison = ") == 0"; break; | ||||
7025 | case BO_NE: resultComparison = ") != 0"; break; | ||||
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7026 | default: llvm_unreachable("Invalid comparison operator"); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7027 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7028 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7029 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 7030 | PDiag(diag::warn_stringcompare) |
7031 | << isa<ObjCEncodeExpr>(literalStringStripped) | ||||
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 7032 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7033 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 7034 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7035 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7036 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7037 | if (LHS.get()->getType()->isArithmeticType() && |
7038 | RHS.get()->getType()->isArithmeticType()) { | ||||
7039 | UsualArithmeticConversions(LHS, RHS); | ||||
7040 | if (LHS.isInvalid() || RHS.isInvalid()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7041 | return QualType(); |
7042 | } | ||||
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7043 | else { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7044 | LHS = UsualUnaryConversions(LHS.take()); |
7045 | if (LHS.isInvalid()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7046 | return QualType(); |
7047 | |||||
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7048 | RHS = UsualUnaryConversions(RHS.take()); |
7049 | if (RHS.isInvalid()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7050 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7051 | } |
7052 | |||||
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7053 | LHSType = LHS.get()->getType(); |
7054 | RHSType = RHS.get()->getType(); | ||||
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7055 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7056 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7057 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7058 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7059 | if (IsRelational) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7060 | if (LHSType->isRealType() && RHSType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7061 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7062 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 7063 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7064 | if (LHSType->hasFloatingRepresentation()) |
7065 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7066 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7067 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7068 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7069 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7070 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7071 | bool LHSIsNull = LHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7072 | Expr::NPC_ValueDependentIsNull); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7073 | bool RHSIsNull = RHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7074 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7075 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7076 | // All of the following pointer-related warnings are GCC extensions, except |
7077 | // when handling null pointer constants. | ||||
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7078 | if (LHSType->isPointerType() && RHSType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7079 | QualType LCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7080 | LHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7081 | QualType RCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7082 | RHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7083 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7084 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7085 | if (LCanPointeeTy == RCanPointeeTy) |
7086 | return ResultTy; | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7087 | if (!IsRelational && |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7088 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
7089 | // Valid unless comparison between non-null pointer and function pointer | ||||
7090 | // This is a gcc extension compatibility comparison. | ||||
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7091 | // In a SFINAE context, we treat this as a hard error to maintain |
7092 | // conformance with the C++ standard. | ||||
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7093 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
7094 | && !LHSIsNull && !RHSIsNull) { | ||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7095 | diagnoseFunctionPointerToVoidComparison( |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7096 | *this, Loc, LHS, RHS, /*isError*/ isSFINAEContext()); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7097 | |
7098 | if (isSFINAEContext()) | ||||
7099 | return QualType(); | ||||
7100 | |||||
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7101 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7102 | return ResultTy; |
7103 | } | ||||
7104 | } | ||||
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7105 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7106 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7107 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7108 | else |
7109 | return ResultTy; | ||||
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7110 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7111 | // C99 6.5.9p2 and C99 6.5.8p2 |
7112 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), | ||||
7113 | RCanPointeeTy.getUnqualifiedType())) { | ||||
7114 | // Valid unless a relational comparison of function pointers | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7115 | if (IsRelational && LCanPointeeTy->isFunctionType()) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7116 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7117 | << LHSType << RHSType << LHS.get()->getSourceRange() |
7118 | << RHS.get()->getSourceRange(); | ||||
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7119 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7120 | } else if (!IsRelational && |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7121 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
7122 | // Valid unless comparison between non-null pointer and function pointer | ||||
7123 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) | ||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7124 | && !LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7125 | diagnoseFunctionPointerToVoidComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7126 | /*isError*/false); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7127 | } else { |
7128 | // Invalid | ||||
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7129 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7130 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7131 | if (LCanPointeeTy != RCanPointeeTy) { |
7132 | if (LHSIsNull && !RHSIsNull) | ||||
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7133 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7134 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7135 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7136 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7137 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7138 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7139 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7140 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7141 | // Comparison of nullptr_t with itself. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7142 | if (LHSType->isNullPtrType() && RHSType->isNullPtrType()) |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7143 | return ResultTy; |
7144 | |||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7145 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7146 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7147 | if (RHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7148 | ((LHSType->isAnyPointerType() || LHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7149 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7150 | (LHSType->isMemberPointerType() || LHSType->isBlockPointerType())))) { |
7151 | RHS = ImpCastExprToType(RHS.take(), LHSType, | ||||
7152 | LHSType->isMemberPointerType() | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7153 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7154 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7155 | return ResultTy; |
7156 | } | ||||
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7157 | if (LHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7158 | ((RHSType->isAnyPointerType() || RHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7159 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7160 | (RHSType->isMemberPointerType() || RHSType->isBlockPointerType())))) { |
7161 | LHS = ImpCastExprToType(LHS.take(), RHSType, | ||||
7162 | RHSType->isMemberPointerType() | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7163 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7164 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7165 | return ResultTy; |
7166 | } | ||||
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7167 | |
7168 | // Comparison of member pointers. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7169 | if (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7170 | LHSType->isMemberPointerType() && RHSType->isMemberPointerType()) { |
7171 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) | ||||
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7172 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7173 | else |
7174 | return ResultTy; | ||||
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7175 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7176 | |
7177 | // Handle scoped enumeration types specifically, since they don't promote | ||||
7178 | // to integers. | ||||
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7179 | if (LHS.get()->getType()->isEnumeralType() && |
7180 | Context.hasSameUnqualifiedType(LHS.get()->getType(), | ||||
7181 | RHS.get()->getType())) | ||||
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7182 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7184 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7185 | // Handle block pointer types. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7186 | if (!IsRelational && LHSType->isBlockPointerType() && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7187 | RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7188 | QualType lpointee = LHSType->castAs<BlockPointerType>()->getPointeeType(); |
7189 | QualType rpointee = RHSType->castAs<BlockPointerType>()->getPointeeType(); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7190 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7191 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7192 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7193 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7194 | << LHSType << RHSType << LHS.get()->getSourceRange() |
7195 | << RHS.get()->getSourceRange(); | ||||
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7196 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7197 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7198 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7199 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7200 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7201 | // Allow block pointers to be compared with null pointer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7202 | if (!IsRelational |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7203 | && ((LHSType->isBlockPointerType() && RHSType->isPointerType()) |
7204 | || (LHSType->isPointerType() && RHSType->isBlockPointerType()))) { | ||||
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7205 | if (!LHSIsNull && !RHSIsNull) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7206 | if (!((RHSType->isPointerType() && RHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7207 | ->getPointeeType()->isVoidType()) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7208 | || (LHSType->isPointerType() && LHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7209 | ->getPointeeType()->isVoidType()))) |
7210 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) | ||||
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7211 | << LHSType << RHSType << LHS.get()->getSourceRange() |
7212 | << RHS.get()->getSourceRange(); | ||||
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7213 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7214 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7215 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
7216 | RHSType->isPointerType() ? CK_BitCast | ||||
7217 | : CK_AnyPointerToBlockPointerCast); | ||||
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7218 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7219 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
7220 | LHSType->isPointerType() ? CK_BitCast | ||||
7221 | : CK_AnyPointerToBlockPointerCast); | ||||
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7222 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7223 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7224 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7225 | if (LHSType->isObjCObjectPointerType() || |
7226 | RHSType->isObjCObjectPointerType()) { | ||||
7227 | const PointerType *LPT = LHSType->getAs<PointerType>(); | ||||
7228 | const PointerType *RPT = RHSType->getAs<PointerType>(); | ||||
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7229 | if (LPT || RPT) { |
7230 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; | ||||
7231 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7232 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7233 | if (!LPtrToVoid && !RPtrToVoid && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7234 | !Context.typesAreCompatible(LHSType, RHSType)) { |
7235 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, | ||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7236 | /*isError*/false); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7237 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7238 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7239 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
7240 | RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); | ||||
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7241 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7242 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
7243 | LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); | ||||
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7244 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7245 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7246 | if (LHSType->isObjCObjectPointerType() && |
7247 | RHSType->isObjCObjectPointerType()) { | ||||
7248 | if (!Context.areComparableObjCPointerTypes(LHSType, RHSType)) | ||||
7249 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, | ||||
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7250 | /*isError*/false); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 7251 | if (isObjCObjectLiteral(LHS) || isObjCObjectLiteral(RHS)) |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 7252 | diagnoseObjCLiteralComparison(*this, Loc, LHS, RHS, Opc); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 7253 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7254 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7255 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7256 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7257 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7258 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7259 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7260 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7261 | if ((LHSType->isAnyPointerType() && RHSType->isIntegerType()) || |
7262 | (LHSType->isIntegerType() && RHSType->isAnyPointerType())) { | ||||
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7263 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7264 | bool isError = false; |
Douglas Gregor | 6db351a | 2012-09-14 04:35:37 +0000 | [diff] [blame] | 7265 | if (LangOpts.DebuggerSupport) { |
7266 | // Under a debugger, allow the comparison of pointers to integers, | ||||
7267 | // since users tend to want to compare addresses. | ||||
7268 | } else if ((LHSIsNull && LHSType->isIntegerType()) || | ||||
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7269 | (RHSIsNull && RHSType->isIntegerType())) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7270 | if (IsRelational && !getLangOpts().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7271 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7272 | } else if (IsRelational && !getLangOpts().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7273 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7274 | else if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7275 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
7276 | isError = true; | ||||
7277 | } else | ||||
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7278 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7279 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7280 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7281 | Diag(Loc, DiagID) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7282 | << LHSType << RHSType << LHS.get()->getSourceRange() |
7283 | << RHS.get()->getSourceRange(); | ||||
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7284 | if (isError) |
7285 | return QualType(); | ||||
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7286 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7287 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7288 | if (LHSType->isIntegerType()) |
7289 | LHS = ImpCastExprToType(LHS.take(), RHSType, | ||||
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7290 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7291 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7292 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7293 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7294 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7295 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7296 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7297 | // Handle block pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7298 | if (!IsRelational && RHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7299 | && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { |
7300 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); | ||||
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7301 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7302 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7303 | if (!IsRelational && LHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7304 | && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { |
7305 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_NullToPointer); | ||||
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7306 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7307 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7308 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7309 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7310 | } |
7311 | |||||
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7312 | |
7313 | // Return a signed type that is of identical size and number of elements. | ||||
7314 | // For floating point vectors, return an integer type of identical size | ||||
7315 | // and number of elements. | ||||
7316 | QualType Sema::GetSignedVectorType(QualType V) { | ||||
7317 | const VectorType *VTy = V->getAs<VectorType>(); | ||||
7318 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); | ||||
7319 | if (TypeSize == Context.getTypeSize(Context.CharTy)) | ||||
7320 | return Context.getExtVectorType(Context.CharTy, VTy->getNumElements()); | ||||
7321 | else if (TypeSize == Context.getTypeSize(Context.ShortTy)) | ||||
7322 | return Context.getExtVectorType(Context.ShortTy, VTy->getNumElements()); | ||||
7323 | else if (TypeSize == Context.getTypeSize(Context.IntTy)) | ||||
7324 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); | ||||
7325 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) | ||||
7326 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); | ||||
7327 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && | ||||
7328 | "Unhandled vector element size in vector compare"); | ||||
7329 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); | ||||
7330 | } | ||||
7331 | |||||
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7332 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7333 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7334 | /// like a scalar comparison, a vector comparison produces a vector of integer |
7335 | /// types. | ||||
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7336 | QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7337 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7338 | bool IsRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7339 | // Check to make sure we're operating on vectors of the same type and width, |
7340 | // Allowing one side to be a scalar of element type. | ||||
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7341 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, /*isCompAssign*/false); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7342 | if (vType.isNull()) |
7343 | return vType; | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7344 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7345 | QualType LHSType = LHS.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7346 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7347 | // If AltiVec, the comparison results in a numeric type, i.e. |
7348 | // bool for C++, int for C | ||||
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 7349 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7350 | return Context.getLogicalOperationType(); |
7351 | |||||
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7352 | // For non-floating point types, check for self-comparisons of the form |
7353 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and | ||||
7354 | // often indicate logic errors in the program. | ||||
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7355 | if (!LHSType->hasFloatingRepresentation()) { |
Richard Smith | 9c129f8 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 7356 | if (DeclRefExpr* DRL |
7357 | = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParenImpCasts())) | ||||
7358 | if (DeclRefExpr* DRR | ||||
7359 | = dyn_cast<DeclRefExpr>(RHS.get()->IgnoreParenImpCasts())) | ||||
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7360 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7361 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7362 | PDiag(diag::warn_comparison_always) |
7363 | << 0 // self- | ||||
7364 | << 2 // "a constant" | ||||
7365 | ); | ||||
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7366 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7367 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7368 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7369 | if (!IsRelational && LHSType->hasFloatingRepresentation()) { |
David Blaikie | 52e4c60 | 2012-01-16 05:16:03 +0000 | [diff] [blame] | 7370 | assert (RHS.get()->getType()->hasFloatingRepresentation()); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7371 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7372 | } |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7373 | |
7374 | // Return a signed type for the vector. | ||||
7375 | return GetSignedVectorType(LHSType); | ||||
7376 | } | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7377 | |
Tanya Lattner | b0f9dd2 | 2012-01-19 01:16:16 +0000 | [diff] [blame] | 7378 | QualType Sema::CheckVectorLogicalOperands(ExprResult &LHS, ExprResult &RHS, |
7379 | SourceLocation Loc) { | ||||
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7380 | // Ensure that either both operands are of the same vector type, or |
7381 | // one operand is of a vector type and the other is of its element type. | ||||
7382 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, false); | ||||
7383 | if (vType.isNull() || vType->isFloatingType()) | ||||
7384 | return InvalidOperands(Loc, LHS, RHS); | ||||
7385 | |||||
7386 | return GetSignedVectorType(LHS.get()->getType()); | ||||
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7387 | } |
7388 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7389 | inline QualType Sema::CheckBitwiseOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7390 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 7391 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
7392 | |||||
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7393 | if (LHS.get()->getType()->isVectorType() || |
7394 | RHS.get()->getType()->isVectorType()) { | ||||
7395 | if (LHS.get()->getType()->hasIntegerRepresentation() && | ||||
7396 | RHS.get()->getType()->hasIntegerRepresentation()) | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7397 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7398 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7399 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7400 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7401 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7402 | ExprResult LHSResult = Owned(LHS), RHSResult = Owned(RHS); |
7403 | QualType compType = UsualArithmeticConversions(LHSResult, RHSResult, | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7404 | IsCompAssign); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7405 | if (LHSResult.isInvalid() || RHSResult.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7406 | return QualType(); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7407 | LHS = LHSResult.take(); |
7408 | RHS = RHSResult.take(); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7409 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 7410 | if (!compType.isNull() && compType->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7411 | return compType; |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7412 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7413 | } |
7414 | |||||
7415 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] | ||||
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7416 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7417 | |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7418 | // Check vector operands differently. |
7419 | if (LHS.get()->getType()->isVectorType() || RHS.get()->getType()->isVectorType()) | ||||
7420 | return CheckVectorLogicalOperands(LHS, RHS, Loc); | ||||
7421 | |||||
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7422 | // Diagnose cases where the user write a logical and/or but probably meant a |
7423 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS | ||||
7424 | // is a constant. | ||||
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7425 | if (LHS.get()->getType()->isIntegerType() && |
7426 | !LHS.get()->getType()->isBooleanType() && | ||||
7427 | RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() && | ||||
Richard Trieu | e5adf59 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 7428 | // Don't warn in macros or template instantiations. |
7429 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { | ||||
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7430 | // If the RHS can be constant folded, and if it constant folds to something |
7431 | // that isn't 0 or 1 (which indicate a potential logical operation that | ||||
7432 | // happened to fold to true/false) then warn. | ||||
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 7433 | // Parens on the RHS are ignored. |
Richard Smith | 909c555 | 2011-10-16 23:01:09 +0000 | [diff] [blame] | 7434 | llvm::APSInt Result; |
7435 | if (RHS.get()->EvaluateAsInt(Result, Context)) | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7436 | if ((getLangOpts().Bool && !RHS.get()->getType()->isBooleanType()) || |
Richard Smith | 909c555 | 2011-10-16 23:01:09 +0000 | [diff] [blame] | 7437 | (Result != 0 && Result != 1)) { |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 7438 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7439 | << RHS.get()->getSourceRange() |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7440 | << (Opc == BO_LAnd ? "&&" : "||"); |
7441 | // Suggest replacing the logical operator with the bitwise version | ||||
7442 | Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) | ||||
7443 | << (Opc == BO_LAnd ? "&" : "|") | ||||
7444 | << FixItHint::CreateReplacement(SourceRange( | ||||
7445 | Loc, Lexer::getLocForEndOfToken(Loc, 0, getSourceManager(), | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7446 | getLangOpts())), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7447 | Opc == BO_LAnd ? "&" : "|"); |
7448 | if (Opc == BO_LAnd) | ||||
7449 | // Suggest replacing "Foo() && kNonZero" with "Foo()" | ||||
7450 | Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) | ||||
7451 | << FixItHint::CreateRemoval( | ||||
7452 | SourceRange( | ||||
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7453 | Lexer::getLocForEndOfToken(LHS.get()->getLocEnd(), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7454 | 0, getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7455 | getLangOpts()), |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7456 | RHS.get()->getLocEnd())); |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7457 | } |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7458 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7459 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7460 | if (!Context.getLangOpts().CPlusPlus) { |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7461 | LHS = UsualUnaryConversions(LHS.take()); |
7462 | if (LHS.isInvalid()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7463 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7464 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7465 | RHS = UsualUnaryConversions(RHS.take()); |
7466 | if (RHS.isInvalid()) | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7467 | return QualType(); |
7468 | |||||
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7469 | if (!LHS.get()->getType()->isScalarType() || |
7470 | !RHS.get()->getType()->isScalarType()) | ||||
7471 | return InvalidOperands(Loc, LHS, RHS); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7472 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7473 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 7474 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7475 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7476 | // The following is safe because we only use this method for |
7477 | // non-overloadable operands. | ||||
7478 | |||||
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7479 | // C++ [expr.log.and]p1 |
7480 | // C++ [expr.log.or]p1 | ||||
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7481 | // The operands are both contextually converted to type bool. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7482 | ExprResult LHSRes = PerformContextuallyConvertToBool(LHS.get()); |
7483 | if (LHSRes.isInvalid()) | ||||
7484 | return InvalidOperands(Loc, LHS, RHS); | ||||
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7485 | LHS = LHSRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7486 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7487 | ExprResult RHSRes = PerformContextuallyConvertToBool(RHS.get()); |
7488 | if (RHSRes.isInvalid()) | ||||
7489 | return InvalidOperands(Loc, LHS, RHS); | ||||
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7490 | RHS = RHSRes; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7491 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7492 | // C++ [expr.log.and]p2 |
7493 | // C++ [expr.log.or]p2 | ||||
7494 | // The result is a bool. | ||||
7495 | return Context.BoolTy; | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7496 | } |
7497 | |||||
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7498 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
7499 | /// is a read-only property; return true if so. A readonly property expression | ||||
7500 | /// depends on various declarations and thus must be treated specially. | ||||
7501 | /// | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7502 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7503 | const ObjCPropertyRefExpr *PropExpr = dyn_cast<ObjCPropertyRefExpr>(E); |
7504 | if (!PropExpr) return false; | ||||
7505 | if (PropExpr->isImplicitProperty()) return false; | ||||
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7506 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7507 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
7508 | QualType BaseType = PropExpr->isSuperReceiver() ? | ||||
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7509 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 7510 | PropExpr->getBase()->getType(); |
7511 | |||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7512 | if (const ObjCObjectPointerType *OPT = |
7513 | BaseType->getAsObjCInterfacePointerType()) | ||||
7514 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) | ||||
7515 | if (S.isPropertyReadonly(PDecl, IFace)) | ||||
7516 | return true; | ||||
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7517 | return false; |
7518 | } | ||||
7519 | |||||
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7520 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7521 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); |
7522 | if (!ME) return false; | ||||
7523 | if (!isa<FieldDecl>(ME->getMemberDecl())) return false; | ||||
7524 | ObjCMessageExpr *Base = | ||||
7525 | dyn_cast<ObjCMessageExpr>(ME->getBase()->IgnoreParenImpCasts()); | ||||
7526 | if (!Base) return false; | ||||
7527 | return Base->getMethodDecl() != 0; | ||||
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7528 | } |
7529 | |||||
John McCall | 78dae24 | 2012-03-13 00:37:01 +0000 | [diff] [blame] | 7530 | /// Is the given expression (which must be 'const') a reference to a |
7531 | /// variable which was originally non-const, but which has become | ||||
7532 | /// 'const' due to being captured within a block? | ||||
7533 | enum NonConstCaptureKind { NCCK_None, NCCK_Block, NCCK_Lambda }; | ||||
7534 | static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) { | ||||
7535 | assert(E->isLValue() && E->getType().isConstQualified()); | ||||
7536 | E = E->IgnoreParens(); | ||||
7537 | |||||
7538 | // Must be a reference to a declaration from an enclosing scope. | ||||
7539 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); | ||||
7540 | if (!DRE) return NCCK_None; | ||||
7541 | if (!DRE->refersToEnclosingLocal()) return NCCK_None; | ||||
7542 | |||||
7543 | // The declaration must be a variable which is not declared 'const'. | ||||
7544 | VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl()); | ||||
7545 | if (!var) return NCCK_None; | ||||
7546 | if (var->getType().isConstQualified()) return NCCK_None; | ||||
7547 | assert(var->hasLocalStorage() && "capture added 'const' to non-local?"); | ||||
7548 | |||||
7549 | // Decide whether the first capture was for a block or a lambda. | ||||
7550 | DeclContext *DC = S.CurContext; | ||||
7551 | while (DC->getParent() != var->getDeclContext()) | ||||
7552 | DC = DC->getParent(); | ||||
7553 | return (isa<BlockDecl>(DC) ? NCCK_Block : NCCK_Lambda); | ||||
7554 | } | ||||
7555 | |||||
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7556 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
7557 | /// emit an error and return true. If so, return false. | ||||
7558 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { | ||||
Fariborz Jahanian | e7ea28a | 2012-04-10 17:30:10 +0000 | [diff] [blame] | 7559 | assert(!E->hasPlaceholderType(BuiltinType::PseudoObject)); |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7560 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7561 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7562 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7563 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
7564 | IsLV = Expr::MLV_ReadonlyProperty; | ||||
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7565 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
7566 | IsLV = Expr::MLV_InvalidMessageExpression; | ||||
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7567 | if (IsLV == Expr::MLV_Valid) |
7568 | return false; | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7569 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7570 | unsigned Diag = 0; |
7571 | bool NeedType = false; | ||||
7572 | switch (IsLV) { // C99 6.5.16p2 | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7573 | case Expr::MLV_ConstQualified: |
7574 | Diag = diag::err_typecheck_assign_const; | ||||
7575 | |||||
John McCall | 78dae24 | 2012-03-13 00:37:01 +0000 | [diff] [blame] | 7576 | // Use a specialized diagnostic when we're assigning to an object |
7577 | // from an enclosing function or block. | ||||
7578 | if (NonConstCaptureKind NCCK = isReferenceToNonConstCapture(S, E)) { | ||||
7579 | if (NCCK == NCCK_Block) | ||||
7580 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; | ||||
7581 | else | ||||
7582 | Diag = diag::err_lambda_decl_ref_not_modifiable_lvalue; | ||||
7583 | break; | ||||
7584 | } | ||||
7585 | |||||
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7586 | // In ARC, use some specialized diagnostics for occasions where we |
7587 | // infer 'const'. These are always pseudo-strong variables. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7588 | if (S.getLangOpts().ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7589 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
7590 | if (declRef && isa<VarDecl>(declRef->getDecl())) { | ||||
7591 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); | ||||
7592 | |||||
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7593 | // Use the normal diagnostic if it's pseudo-__strong but the |
7594 | // user actually wrote 'const'. | ||||
7595 | if (var->isARCPseudoStrong() && | ||||
7596 | (!var->getTypeSourceInfo() || | ||||
7597 | !var->getTypeSourceInfo()->getType().isConstQualified())) { | ||||
7598 | // There are two pseudo-strong cases: | ||||
7599 | // - self | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7600 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
7601 | if (method && var == method->getSelfDecl()) | ||||
Ted Kremenek | 2bbcd5c | 2011-11-14 21:59:25 +0000 | [diff] [blame] | 7602 | Diag = method->isClassMethod() |
7603 | ? diag::err_typecheck_arc_assign_self_class_method | ||||
7604 | : diag::err_typecheck_arc_assign_self; | ||||
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7605 | |
7606 | // - fast enumeration variables | ||||
7607 | else | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7608 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7609 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7610 | SourceRange Assign; |
7611 | if (Loc != OrigLoc) | ||||
7612 | Assign = SourceRange(OrigLoc, OrigLoc); | ||||
7613 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; | ||||
7614 | // We need to preserve the AST regardless, so migration tool | ||||
7615 | // can do its job. | ||||
7616 | return false; | ||||
7617 | } | ||||
7618 | } | ||||
7619 | } | ||||
7620 | |||||
7621 | break; | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7622 | case Expr::MLV_ArrayType: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 7623 | case Expr::MLV_ArrayTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7624 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
7625 | NeedType = true; | ||||
7626 | break; | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7627 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7628 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
7629 | NeedType = true; | ||||
7630 | break; | ||||
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 7631 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7632 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
7633 | break; | ||||
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7634 | case Expr::MLV_Valid: |
7635 | llvm_unreachable("did not take early return for MLV_Valid"); | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7636 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7637 | case Expr::MLV_MemberFunction: |
7638 | case Expr::MLV_ClassTemporary: | ||||
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7639 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
7640 | break; | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7641 | case Expr::MLV_IncompleteType: |
7642 | case Expr::MLV_IncompleteVoidType: | ||||
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7643 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 7644 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, E); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7645 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7646 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
7647 | break; | ||||
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7648 | case Expr::MLV_ReadonlyProperty: |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7649 | case Expr::MLV_NoSetterProperty: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7650 | llvm_unreachable("readonly properties should be processed differently"); |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7651 | case Expr::MLV_InvalidMessageExpression: |
7652 | Diag = diag::error_readonly_message_assignment; | ||||
7653 | break; | ||||
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7654 | case Expr::MLV_SubObjCPropertySetting: |
7655 | Diag = diag::error_no_subobject_property_setting; | ||||
7656 | break; | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7657 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7658 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7659 | SourceRange Assign; |
7660 | if (Loc != OrigLoc) | ||||
7661 | Assign = SourceRange(OrigLoc, OrigLoc); | ||||
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7662 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7663 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7664 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7665 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7666 | return true; |
7667 | } | ||||
7668 | |||||
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7669 | static void CheckIdentityFieldAssignment(Expr *LHSExpr, Expr *RHSExpr, |
7670 | SourceLocation Loc, | ||||
7671 | Sema &Sema) { | ||||
7672 | // C / C++ fields | ||||
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7673 | MemberExpr *ML = dyn_cast<MemberExpr>(LHSExpr); |
7674 | MemberExpr *MR = dyn_cast<MemberExpr>(RHSExpr); | ||||
7675 | if (ML && MR && ML->getMemberDecl() == MR->getMemberDecl()) { | ||||
7676 | if (isa<CXXThisExpr>(ML->getBase()) && isa<CXXThisExpr>(MR->getBase())) | ||||
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7677 | Sema.Diag(Loc, diag::warn_identity_field_assign) << 0; |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7678 | } |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7679 | |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7680 | // Objective-C instance variables |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7681 | ObjCIvarRefExpr *OL = dyn_cast<ObjCIvarRefExpr>(LHSExpr); |
7682 | ObjCIvarRefExpr *OR = dyn_cast<ObjCIvarRefExpr>(RHSExpr); | ||||
7683 | if (OL && OR && OL->getDecl() == OR->getDecl()) { | ||||
7684 | DeclRefExpr *RL = dyn_cast<DeclRefExpr>(OL->getBase()->IgnoreImpCasts()); | ||||
7685 | DeclRefExpr *RR = dyn_cast<DeclRefExpr>(OR->getBase()->IgnoreImpCasts()); | ||||
7686 | if (RL && RR && RL->getDecl() == RR->getDecl()) | ||||
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7687 | Sema.Diag(Loc, diag::warn_identity_field_assign) << 1; |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7688 | } |
7689 | } | ||||
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7690 | |
7691 | // C99 6.5.16.1 | ||||
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7692 | QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7693 | SourceLocation Loc, |
7694 | QualType CompoundType) { | ||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7695 | assert(!LHSExpr->hasPlaceholderType(BuiltinType::PseudoObject)); |
7696 | |||||
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7697 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7698 | if (CheckForModifiableLvalue(LHSExpr, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7699 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7700 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7701 | QualType LHSType = LHSExpr->getType(); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7702 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
7703 | CompoundType; | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7704 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7705 | if (CompoundType.isNull()) { |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7706 | Expr *RHSCheck = RHS.get(); |
7707 | |||||
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7708 | CheckIdentityFieldAssignment(LHSExpr, RHSCheck, Loc, *this); |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7709 | |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7710 | QualType LHSTy(LHSType); |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7711 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7712 | if (RHS.isInvalid()) |
7713 | return QualType(); | ||||
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7714 | // Special case of NSObject attributes on c-style pointer types. |
7715 | if (ConvTy == IncompatiblePointer && | ||||
7716 | ((Context.isObjCNSObjectType(LHSType) && | ||||
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7717 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7718 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7719 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7720 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7721 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7722 | if (ConvTy == Compatible && |
Fariborz Jahanian | 466f45a | 2012-01-24 19:40:13 +0000 | [diff] [blame] | 7723 | LHSType->isObjCObjectType()) |
Fariborz Jahanian | 7b383e4 | 2012-01-24 18:05:45 +0000 | [diff] [blame] | 7724 | Diag(Loc, diag::err_objc_object_assignment) |
7725 | << LHSType; | ||||
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7726 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7727 | // If the RHS is a unary plus or minus, check to see if they = and + are |
7728 | // right next to each other. If so, the user may have typo'd "x =+ 4" | ||||
7729 | // instead of "x += 4". | ||||
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7730 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
7731 | RHSCheck = ICE->getSubExpr(); | ||||
7732 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7733 | if ((UO->getOpcode() == UO_Plus || |
7734 | UO->getOpcode() == UO_Minus) && | ||||
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7735 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7736 | // Only if the two operators are exactly adjacent. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7737 | Loc.getLocWithOffset(1) == UO->getOperatorLoc() && |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7738 | // And there is a space or other character before the subexpr of the |
7739 | // unary +/-. We don't want to warn on "x=-1". | ||||
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7740 | Loc.getLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7741 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7742 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7743 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7744 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7745 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7746 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7747 | |
7748 | if (ConvTy == Compatible) { | ||||
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7749 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) { |
7750 | // Warn about retain cycles where a block captures the LHS, but | ||||
7751 | // not if the LHS is a simple variable into which the block is | ||||
7752 | // being stored...unless that variable can be captured by reference! | ||||
7753 | const Expr *InnerLHS = LHSExpr->IgnoreParenCasts(); | ||||
7754 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InnerLHS); | ||||
7755 | if (!DRE || DRE->getDecl()->hasAttr<BlocksAttr>()) | ||||
7756 | checkRetainCycles(LHSExpr, RHS.get()); | ||||
7757 | |||||
Jordan Rose | 58b6bdc | 2012-09-28 22:21:30 +0000 | [diff] [blame] | 7758 | // It is safe to assign a weak reference into a strong variable. |
7759 | // Although this code can still have problems: | ||||
7760 | // id x = self.weakProp; | ||||
7761 | // id y = self.weakProp; | ||||
7762 | // we do not warn to warn spuriously when 'x' and 'y' are on separate | ||||
7763 | // paths through the function. This should be revisited if | ||||
7764 | // -Wrepeated-use-of-weak is made flow-sensitive. | ||||
7765 | DiagnosticsEngine::Level Level = | ||||
7766 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, | ||||
7767 | RHS.get()->getLocStart()); | ||||
7768 | if (Level != DiagnosticsEngine::Ignored) | ||||
7769 | getCurFunction()->markSafeWeakUse(RHS.get()); | ||||
7770 | |||||
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7771 | } else if (getLangOpts().ObjCAutoRefCount) { |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7772 | checkUnsafeExprAssigns(Loc, LHSExpr, RHS.get()); |
Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 7773 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7774 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7775 | } else { |
7776 | // Compound assignment "x += y" | ||||
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7777 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7778 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7779 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7780 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7781 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7782 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7783 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7784 | CheckForNullPointerDereference(*this, LHSExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7786 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
7787 | // left operand unless the left operand has qualified type, in which case | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7788 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7789 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
7790 | // is converted to the type of the assignment expression (above). | ||||
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7791 | // 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] | 7792 | // operand. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7793 | return (getLangOpts().CPlusPlus |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7794 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7795 | } |
7796 | |||||
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7797 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7798 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7799 | SourceLocation Loc) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7800 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
7801 | RHS = S.CheckPlaceholderExpr(RHS.take()); | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7802 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7803 | return QualType(); |
7804 | |||||
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7805 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
7806 | // operands, but not unary promotions. | ||||
7807 | // 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] | 7808 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7809 | // So we treat the LHS as a ignored value, and in C++ we allow the |
7810 | // containing site to determine what should be done with the RHS. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7811 | LHS = S.IgnoredValueConversions(LHS.take()); |
7812 | if (LHS.isInvalid()) | ||||
7813 | return QualType(); | ||||
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7814 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 7815 | S.DiagnoseUnusedExprResult(LHS.get()); |
7816 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7817 | if (!S.getLangOpts().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7818 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
7819 | if (RHS.isInvalid()) | ||||
7820 | return QualType(); | ||||
7821 | if (!RHS.get()->getType()->isVoidType()) | ||||
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7822 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
7823 | diag::err_incomplete_type); | ||||
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7824 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7825 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7826 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7827 | } |
7828 | |||||
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7829 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
7830 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. | ||||
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7831 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
7832 | ExprValueKind &VK, | ||||
7833 | SourceLocation OpLoc, | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7834 | bool IsInc, bool IsPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7835 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7836 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7837 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7838 | QualType ResType = Op->getType(); |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7839 | // Atomic types can be used for increment / decrement where the non-atomic |
7840 | // versions can, so ignore the _Atomic() specifier for the purpose of | ||||
7841 | // checking. | ||||
7842 | if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) | ||||
7843 | ResType = ResAtomicType->getValueType(); | ||||
7844 | |||||
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7845 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7846 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7847 | if (S.getLangOpts().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7848 | // Decrement of bool is not allowed. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7849 | if (!IsInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7850 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7851 | return QualType(); |
7852 | } | ||||
7853 | // Increment of bool sets it to true, but is deprecated. | ||||
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7854 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7855 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7856 | // OK! |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 7857 | } else if (ResType->isPointerType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7858 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7859 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7860 | return QualType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 7861 | } else if (ResType->isObjCObjectPointerType()) { |
7862 | // On modern runtimes, ObjC pointer arithmetic is forbidden. | ||||
7863 | // Otherwise, we just need a complete type. | ||||
7864 | if (checkArithmeticIncompletePointerType(S, OpLoc, Op) || | ||||
7865 | checkArithmeticOnObjCPointer(S, OpLoc, Op)) | ||||
7866 | return QualType(); | ||||
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7867 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7868 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7869 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7870 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7871 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7872 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7873 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7874 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7875 | IsInc, IsPrefix); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7876 | } else if (S.getLangOpts().AltiVec && ResType->isVectorType()) { |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7877 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7878 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7879 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7880 | << ResType << int(IsInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7881 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7882 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7883 | // 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] | 7884 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7885 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7886 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7887 | // In C++, a prefix increment is the same type as the operand. Otherwise |
7888 | // (in C or with postfix), the increment is the unqualified type of the | ||||
7889 | // operand. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7890 | if (IsPrefix && S.getLangOpts().CPlusPlus) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7891 | VK = VK_LValue; |
7892 | return ResType; | ||||
7893 | } else { | ||||
7894 | VK = VK_RValue; | ||||
7895 | return ResType.getUnqualifiedType(); | ||||
7896 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7897 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7898 | |
7899 | |||||
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7900 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7901 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7902 | /// where the declaration is needed for type checking. We only need to |
7903 | /// handle cases when the expression references a function designator | ||||
7904 | /// or is an lvalue. Here are some examples: | ||||
7905 | /// - &(x) => x | ||||
7906 | /// - &*****f => f for f a function designator. | ||||
7907 | /// - &s.xx => s | ||||
7908 | /// - &s.zz[1].yy -> s, if zz is an array | ||||
7909 | /// - *(x + 1) -> x, if x is an array | ||||
7910 | /// - &"123"[2] -> 0 | ||||
7911 | /// - & __real__ x -> x | ||||
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7912 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7913 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7914 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7915 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7916 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7917 | // If this is an arrow operator, the address is an offset from |
7918 | // the base's value, so the object the base refers to is | ||||
7919 | // irrelevant. | ||||
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7920 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7921 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7922 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7923 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7924 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7925 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
7926 | // promotion of register arrays earlier. | ||||
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7927 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
7928 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { | ||||
7929 | if (ICE->getSubExpr()->getType()->isArrayType()) | ||||
7930 | return getPrimaryDecl(ICE->getSubExpr()); | ||||
7931 | } | ||||
7932 | return 0; | ||||
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7933 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7934 | case Stmt::UnaryOperatorClass: { |
7935 | UnaryOperator *UO = cast<UnaryOperator>(E); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7936 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7937 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7938 | case UO_Real: |
7939 | case UO_Imag: | ||||
7940 | case UO_Extension: | ||||
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7941 | return getPrimaryDecl(UO->getSubExpr()); |
7942 | default: | ||||
7943 | return 0; | ||||
7944 | } | ||||
7945 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7946 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7947 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7948 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7949 | // If the result of an implicit cast is an l-value, we care about |
7950 | // the sub-expression; otherwise, the result here doesn't matter. | ||||
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7951 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7952 | default: |
7953 | return 0; | ||||
7954 | } | ||||
7955 | } | ||||
7956 | |||||
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7957 | namespace { |
7958 | enum { | ||||
7959 | AO_Bit_Field = 0, | ||||
7960 | AO_Vector_Element = 1, | ||||
7961 | AO_Property_Expansion = 2, | ||||
7962 | AO_Register_Variable = 3, | ||||
7963 | AO_No_Error = 4 | ||||
7964 | }; | ||||
7965 | } | ||||
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7966 | /// \brief Diagnose invalid operand for address of operations. |
7967 | /// | ||||
7968 | /// \param Type The type of operand which cannot have its address taken. | ||||
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7969 | static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, |
7970 | Expr *E, unsigned Type) { | ||||
7971 | S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); | ||||
7972 | } | ||||
7973 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7974 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7975 | /// 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] | 7976 | /// 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] | 7977 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7978 | /// 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] | 7979 | /// 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] | 7980 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7981 | static QualType CheckAddressOfOperand(Sema &S, ExprResult &OrigOp, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7982 | SourceLocation OpLoc) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7983 | if (const BuiltinType *PTy = OrigOp.get()->getType()->getAsPlaceholderType()){ |
7984 | if (PTy->getKind() == BuiltinType::Overload) { | ||||
7985 | if (!isa<OverloadExpr>(OrigOp.get()->IgnoreParens())) { | ||||
7986 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) | ||||
7987 | << OrigOp.get()->getSourceRange(); | ||||
7988 | return QualType(); | ||||
7989 | } | ||||
7990 | |||||
7991 | return S.Context.OverloadTy; | ||||
7992 | } | ||||
7993 | |||||
7994 | if (PTy->getKind() == BuiltinType::UnknownAny) | ||||
7995 | return S.Context.UnknownAnyTy; | ||||
7996 | |||||
7997 | if (PTy->getKind() == BuiltinType::BoundMember) { | ||||
7998 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) | ||||
7999 | << OrigOp.get()->getSourceRange(); | ||||
Douglas Gregor | 44efed0 | 2011-10-09 19:10:41 +0000 | [diff] [blame] | 8000 | return QualType(); |
8001 | } | ||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8002 | |
8003 | OrigOp = S.CheckPlaceholderExpr(OrigOp.take()); | ||||
8004 | if (OrigOp.isInvalid()) return QualType(); | ||||
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8005 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8006 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8007 | if (OrigOp.get()->isTypeDependent()) |
8008 | return S.Context.DependentTy; | ||||
8009 | |||||
8010 | assert(!OrigOp.get()->getType()->isPlaceholderType()); | ||||
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8011 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8012 | // Make sure to ignore parentheses in subsequent checks |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8013 | Expr *op = OrigOp.get()->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 8014 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8015 | if (S.getLangOpts().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8016 | // Implement C99-only parts of addressof rules. |
8017 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8018 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8019 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
8020 | // (assuming the deref expression is valid). | ||||
8021 | return uOp->getSubExpr()->getType(); | ||||
8022 | } | ||||
8023 | // Technically, there should be a check for array subscript | ||||
8024 | // expressions here, but the result of one is always an lvalue anyway. | ||||
8025 | } | ||||
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8026 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8027 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8028 | unsigned AddressOfError = AO_No_Error; |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 8029 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8030 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8031 | bool sfinae = S.isSFINAEContext(); |
8032 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary | ||||
8033 | : diag::ext_typecheck_addrof_class_temporary) | ||||
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8034 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8035 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8036 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8037 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8038 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8039 | } else if (lval == Expr::LV_MemberFunction) { |
8040 | // If it's an instance method, make a member pointer. | ||||
8041 | // The expression must have exactly the form &A::foo. | ||||
8042 | |||||
8043 | // If the underlying expression isn't a decl ref, give up. | ||||
8044 | if (!isa<DeclRefExpr>(op)) { | ||||
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8045 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8046 | << OrigOp.get()->getSourceRange(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8047 | return QualType(); |
8048 | } | ||||
8049 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); | ||||
8050 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); | ||||
8051 | |||||
8052 | // The id-expression was parenthesized. | ||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8053 | if (OrigOp.get() != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8054 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8055 | << OrigOp.get()->getSourceRange(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8056 | |
8057 | // The method was named without a qualifier. | ||||
8058 | } else if (!DRE->getQualifier()) { | ||||
David Blaikie | abeadfb | 2012-10-11 22:55:07 +0000 | [diff] [blame^] | 8059 | if (MD->getParent()->getName().empty()) |
8060 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) | ||||
8061 | << op->getSourceRange(); | ||||
8062 | else { | ||||
8063 | SmallString<32> Str; | ||||
8064 | StringRef Qual = (MD->getParent()->getName() + "::").toStringRef(Str); | ||||
8065 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) | ||||
8066 | << op->getSourceRange() | ||||
8067 | << FixItHint::CreateInsertion(op->getSourceRange().getBegin(), Qual); | ||||
8068 | } | ||||
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8069 | } |
8070 | |||||
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8071 | return S.Context.getMemberPointerType(op->getType(), |
8072 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); | ||||
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8073 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8074 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8075 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8076 | if (!op->getType()->isFunctionType()) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8077 | // Use a special diagnostic for loads from property references. |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 8078 | if (isa<PseudoObjectExpr>(op)) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8079 | AddressOfError = AO_Property_Expansion; |
8080 | } else { | ||||
8081 | // FIXME: emit more specific diag... | ||||
8082 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) | ||||
8083 | << op->getSourceRange(); | ||||
8084 | return QualType(); | ||||
8085 | } | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8086 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8087 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8088 | // The operand cannot be a bit-field |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8089 | AddressOfError = AO_Bit_Field; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8090 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8091 | // The operand cannot be an element of a vector |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8092 | AddressOfError = AO_Vector_Element; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8093 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8094 | // 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] | 8095 | // with the register storage-class specifier. |
8096 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { | ||||
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 8097 | // in C++ it is not error to take address of a register |
8098 | // variable (c++03 7.1.1P3) | ||||
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8099 | if (vd->getStorageClass() == SC_Register && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8100 | !S.getLangOpts().CPlusPlus) { |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8101 | AddressOfError = AO_Register_Variable; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8102 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8103 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8104 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8105 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 8106 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8107 | // Could be a pointer to member, though, if there is an explicit |
8108 | // scope qualifier for the class. | ||||
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 8109 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8110 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8111 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8112 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8113 | S.Diag(OpLoc, |
8114 | diag::err_cannot_form_pointer_to_member_of_reference_type) | ||||
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8115 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8116 | return QualType(); |
8117 | } | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8118 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 8119 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
8120 | Ctx = Ctx->getParent(); | ||||
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8121 | return S.Context.getMemberPointerType(op->getType(), |
8122 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); | ||||
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8123 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8124 | } |
Eli Friedman | 7b2f51c | 2011-08-26 20:28:17 +0000 | [diff] [blame] | 8125 | } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8126 | llvm_unreachable("Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8127 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8128 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8129 | if (AddressOfError != AO_No_Error) { |
8130 | diagnoseAddressOfInvalidType(S, OpLoc, op, AddressOfError); | ||||
8131 | return QualType(); | ||||
8132 | } | ||||
8133 | |||||
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8134 | if (lval == Expr::LV_IncompleteVoidType) { |
8135 | // Taking the address of a void variable is technically illegal, but we | ||||
8136 | // allow it in cases which are otherwise valid. | ||||
8137 | // Example: "extern void x; void* y = &x;". | ||||
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8138 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8139 | } |
8140 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8141 | // 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] | 8142 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8143 | return S.Context.getObjCObjectPointerType(op->getType()); |
8144 | return S.Context.getPointerType(op->getType()); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8145 | } |
8146 | |||||
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8147 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8148 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
8149 | SourceLocation OpLoc) { | ||||
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8150 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8151 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8152 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8153 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
8154 | if (ConvResult.isInvalid()) | ||||
8155 | return QualType(); | ||||
8156 | Op = ConvResult.take(); | ||||
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8157 | QualType OpTy = Op->getType(); |
8158 | QualType Result; | ||||
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 8159 | |
8160 | if (isa<CXXReinterpretCastExpr>(Op)) { | ||||
8161 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); | ||||
8162 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, | ||||
8163 | Op->getSourceRange()); | ||||
8164 | } | ||||
8165 | |||||
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8166 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
8167 | // is an incomplete type or void. It would be possible to warn about | ||||
8168 | // dereferencing a void pointer, but it's completely well-defined, and such a | ||||
8169 | // warning is unlikely to catch any mistakes. | ||||
8170 | if (const PointerType *PT = OpTy->getAs<PointerType>()) | ||||
8171 | Result = PT->getPointeeType(); | ||||
8172 | else if (const ObjCObjectPointerType *OPT = | ||||
8173 | OpTy->getAs<ObjCObjectPointerType>()) | ||||
8174 | Result = OPT->getPointeeType(); | ||||
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8175 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8176 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8177 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8178 | if (PR.take() != Op) |
8179 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); | ||||
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8180 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8181 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8182 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8183 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8184 | << OpTy << Op->getSourceRange(); |
8185 | return QualType(); | ||||
8186 | } | ||||
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8187 | |
8188 | // Dereferences are usually l-values... | ||||
8189 | VK = VK_LValue; | ||||
8190 | |||||
8191 | // ...except that certain expressions are never l-values in C. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8192 | if (!S.getLangOpts().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8193 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8194 | |
8195 | return Result; | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8196 | } |
8197 | |||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8198 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8199 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8200 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8201 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8202 | default: llvm_unreachable("Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8203 | case tok::periodstar: Opc = BO_PtrMemD; break; |
8204 | case tok::arrowstar: Opc = BO_PtrMemI; break; | ||||
8205 | case tok::star: Opc = BO_Mul; break; | ||||
8206 | case tok::slash: Opc = BO_Div; break; | ||||
8207 | case tok::percent: Opc = BO_Rem; break; | ||||
8208 | case tok::plus: Opc = BO_Add; break; | ||||
8209 | case tok::minus: Opc = BO_Sub; break; | ||||
8210 | case tok::lessless: Opc = BO_Shl; break; | ||||
8211 | case tok::greatergreater: Opc = BO_Shr; break; | ||||
8212 | case tok::lessequal: Opc = BO_LE; break; | ||||
8213 | case tok::less: Opc = BO_LT; break; | ||||
8214 | case tok::greaterequal: Opc = BO_GE; break; | ||||
8215 | case tok::greater: Opc = BO_GT; break; | ||||
8216 | case tok::exclaimequal: Opc = BO_NE; break; | ||||
8217 | case tok::equalequal: Opc = BO_EQ; break; | ||||
8218 | case tok::amp: Opc = BO_And; break; | ||||
8219 | case tok::caret: Opc = BO_Xor; break; | ||||
8220 | case tok::pipe: Opc = BO_Or; break; | ||||
8221 | case tok::ampamp: Opc = BO_LAnd; break; | ||||
8222 | case tok::pipepipe: Opc = BO_LOr; break; | ||||
8223 | case tok::equal: Opc = BO_Assign; break; | ||||
8224 | case tok::starequal: Opc = BO_MulAssign; break; | ||||
8225 | case tok::slashequal: Opc = BO_DivAssign; break; | ||||
8226 | case tok::percentequal: Opc = BO_RemAssign; break; | ||||
8227 | case tok::plusequal: Opc = BO_AddAssign; break; | ||||
8228 | case tok::minusequal: Opc = BO_SubAssign; break; | ||||
8229 | case tok::lesslessequal: Opc = BO_ShlAssign; break; | ||||
8230 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; | ||||
8231 | case tok::ampequal: Opc = BO_AndAssign; break; | ||||
8232 | case tok::caretequal: Opc = BO_XorAssign; break; | ||||
8233 | case tok::pipeequal: Opc = BO_OrAssign; break; | ||||
8234 | case tok::comma: Opc = BO_Comma; break; | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8235 | } |
8236 | return Opc; | ||||
8237 | } | ||||
8238 | |||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8239 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8240 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8241 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8242 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8243 | default: llvm_unreachable("Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8244 | case tok::plusplus: Opc = UO_PreInc; break; |
8245 | case tok::minusminus: Opc = UO_PreDec; break; | ||||
8246 | case tok::amp: Opc = UO_AddrOf; break; | ||||
8247 | case tok::star: Opc = UO_Deref; break; | ||||
8248 | case tok::plus: Opc = UO_Plus; break; | ||||
8249 | case tok::minus: Opc = UO_Minus; break; | ||||
8250 | case tok::tilde: Opc = UO_Not; break; | ||||
8251 | case tok::exclaim: Opc = UO_LNot; break; | ||||
8252 | case tok::kw___real: Opc = UO_Real; break; | ||||
8253 | case tok::kw___imag: Opc = UO_Imag; break; | ||||
8254 | case tok::kw___extension__: Opc = UO_Extension; break; | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8255 | } |
8256 | return Opc; | ||||
8257 | } | ||||
8258 | |||||
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8259 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
8260 | /// This warning is only emitted for builtin assignment operations. It is also | ||||
8261 | /// suppressed in the event of macro expansions. | ||||
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8262 | static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8263 | SourceLocation OpLoc) { |
8264 | if (!S.ActiveTemplateInstantiations.empty()) | ||||
8265 | return; | ||||
8266 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) | ||||
8267 | return; | ||||
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8268 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
8269 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); | ||||
8270 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); | ||||
8271 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); | ||||
8272 | if (!LHSDeclRef || !RHSDeclRef || | ||||
8273 | LHSDeclRef->getLocation().isMacroID() || | ||||
8274 | RHSDeclRef->getLocation().isMacroID()) | ||||
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8275 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8276 | const ValueDecl *LHSDecl = |
8277 | cast<ValueDecl>(LHSDeclRef->getDecl()->getCanonicalDecl()); | ||||
8278 | const ValueDecl *RHSDecl = | ||||
8279 | cast<ValueDecl>(RHSDeclRef->getDecl()->getCanonicalDecl()); | ||||
8280 | if (LHSDecl != RHSDecl) | ||||
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8281 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8282 | if (LHSDecl->getType().isVolatileQualified()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8283 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8284 | if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8285 | if (RefTy->getPointeeType().isVolatileQualified()) |
8286 | return; | ||||
8287 | |||||
8288 | S.Diag(OpLoc, diag::warn_self_assignment) | ||||
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8289 | << LHSDeclRef->getType() |
8290 | << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); | ||||
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8291 | } |
8292 | |||||
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8293 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
8294 | /// operator @p Opc at location @c TokLoc. This routine only supports | ||||
8295 | /// built-in operations; ActOnBinOp handles overloaded operators. | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8296 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8297 | BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8298 | Expr *LHSExpr, Expr *RHSExpr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8299 | if (getLangOpts().CPlusPlus0x && isa<InitListExpr>(RHSExpr)) { |
Sebastian Redl | 0d8ab2e | 2012-02-27 20:34:02 +0000 | [diff] [blame] | 8300 | // The syntax only allows initializer lists on the RHS of assignment, |
8301 | // so we don't need to worry about accepting invalid code for | ||||
8302 | // non-assignment operators. | ||||
8303 | // C++11 5.17p9: | ||||
8304 | // The meaning of x = {v} [...] is that of x = T(v) [...]. The meaning | ||||
8305 | // of x = {} is x = T(). | ||||
8306 | InitializationKind Kind = | ||||
8307 | InitializationKind::CreateDirectList(RHSExpr->getLocStart()); | ||||
8308 | InitializedEntity Entity = | ||||
8309 | InitializedEntity::InitializeTemporary(LHSExpr->getType()); | ||||
8310 | InitializationSequence InitSeq(*this, Entity, Kind, &RHSExpr, 1); | ||||
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8311 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, RHSExpr); |
Sebastian Redl | 0d8ab2e | 2012-02-27 20:34:02 +0000 | [diff] [blame] | 8312 | if (Init.isInvalid()) |
8313 | return Init; | ||||
8314 | RHSExpr = Init.take(); | ||||
8315 | } | ||||
8316 | |||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8317 | ExprResult LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8318 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8319 | // The following two variables are used for compound assignment operators |
8320 | QualType CompLHSTy; // Type of LHS after promotions for computation | ||||
8321 | QualType CompResultTy; // Type of computation result | ||||
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8322 | ExprValueKind VK = VK_RValue; |
8323 | ExprObjectKind OK = OK_Ordinary; | ||||
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8324 | |
8325 | switch (Opc) { | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8326 | case BO_Assign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8327 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8328 | if (getLangOpts().CPlusPlus && |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8329 | LHS.get()->getObjectKind() != OK_ObjCProperty) { |
8330 | VK = LHS.get()->getValueKind(); | ||||
8331 | OK = LHS.get()->getObjectKind(); | ||||
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8332 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8333 | if (!ResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8334 | DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8335 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8336 | case BO_PtrMemD: |
8337 | case BO_PtrMemI: | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8338 | ResultTy = CheckPointerToMemberOperands(LHS, RHS, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8339 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 8340 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8341 | case BO_Mul: |
8342 | case BO_Div: | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8343 | ResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8344 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8345 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8346 | case BO_Rem: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8347 | ResultTy = CheckRemainderOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8348 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8349 | case BO_Add: |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 8350 | ResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8351 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8352 | case BO_Sub: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8353 | ResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8354 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8355 | case BO_Shl: |
8356 | case BO_Shr: | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8357 | ResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8358 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8359 | case BO_LE: |
8360 | case BO_LT: | ||||
8361 | case BO_GE: | ||||
8362 | case BO_GT: | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8363 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8364 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8365 | case BO_EQ: |
8366 | case BO_NE: | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8367 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8368 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8369 | case BO_And: |
8370 | case BO_Xor: | ||||
8371 | case BO_Or: | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8372 | ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8373 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8374 | case BO_LAnd: |
8375 | case BO_LOr: | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8376 | ResultTy = CheckLogicalOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8377 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8378 | case BO_MulAssign: |
8379 | case BO_DivAssign: | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8380 | CompResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8381 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8382 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8383 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
8384 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | ||||
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8385 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8386 | case BO_RemAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8387 | CompResultTy = CheckRemainderOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8388 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8389 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
8390 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | ||||
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8391 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8392 | case BO_AddAssign: |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 8393 | CompResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc, &CompLHSTy); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8394 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
8395 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | ||||
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8396 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8397 | case BO_SubAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8398 | CompResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
8399 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) | ||||
8400 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | ||||
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8401 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8402 | case BO_ShlAssign: |
8403 | case BO_ShrAssign: | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8404 | CompResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8405 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8406 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
8407 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | ||||
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8408 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8409 | case BO_AndAssign: |
8410 | case BO_XorAssign: | ||||
8411 | case BO_OrAssign: | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8412 | CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8413 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8414 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
8415 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); | ||||
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8416 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8417 | case BO_Comma: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8418 | ResultTy = CheckCommaOperands(*this, LHS, RHS, OpLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8419 | if (getLangOpts().CPlusPlus && !RHS.isInvalid()) { |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8420 | VK = RHS.get()->getValueKind(); |
8421 | OK = RHS.get()->getObjectKind(); | ||||
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8422 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8423 | break; |
8424 | } | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8425 | if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8426 | return ExprError(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8427 | |
8428 | // Check for array bounds violations for both sides of the BinaryOperator | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8429 | CheckArrayAccess(LHS.get()); |
8430 | CheckArrayAccess(RHS.get()); | ||||
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8431 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8432 | if (CompResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8433 | return Owned(new (Context) BinaryOperator(LHS.take(), RHS.take(), Opc, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 8434 | ResultTy, VK, OK, OpLoc, |
8435 | FPFeatures.fp_contract)); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8436 | if (getLangOpts().CPlusPlus && LHS.get()->getObjectKind() != |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 8437 | OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8438 | VK = VK_LValue; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8439 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8440 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8441 | return Owned(new (Context) CompoundAssignOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8442 | ResultTy, VK, OK, CompLHSTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 8443 | CompResultTy, OpLoc, |
8444 | FPFeatures.fp_contract)); | ||||
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8445 | } |
8446 | |||||
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8447 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
8448 | /// operators are mixed in a way that suggests that the programmer forgot that | ||||
8449 | /// comparison operators have higher precedence. The most typical example of | ||||
8450 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8451 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8452 | SourceLocation OpLoc, Expr *LHSExpr, |
8453 | Expr *RHSExpr) { | ||||
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8454 | typedef BinaryOperator BinOp; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8455 | BinOp::Opcode LHSopc = static_cast<BinOp::Opcode>(-1), |
8456 | RHSopc = static_cast<BinOp::Opcode>(-1); | ||||
8457 | if (BinOp *BO = dyn_cast<BinOp>(LHSExpr)) | ||||
8458 | LHSopc = BO->getOpcode(); | ||||
8459 | if (BinOp *BO = dyn_cast<BinOp>(RHSExpr)) | ||||
8460 | RHSopc = BO->getOpcode(); | ||||
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8461 | |
8462 | // Subs are not binary operators. | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8463 | if (LHSopc == -1 && RHSopc == -1) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8464 | return; |
8465 | |||||
8466 | // Bitwise operations are sometimes used as eager logical ops. | ||||
8467 | // Don't diagnose this. | ||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8468 | if ((BinOp::isComparisonOp(LHSopc) || BinOp::isBitwiseOp(LHSopc)) && |
8469 | (BinOp::isComparisonOp(RHSopc) || BinOp::isBitwiseOp(RHSopc))) | ||||
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8470 | return; |
8471 | |||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8472 | bool isLeftComp = BinOp::isComparisonOp(LHSopc); |
8473 | bool isRightComp = BinOp::isComparisonOp(RHSopc); | ||||
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8474 | if (!isLeftComp && !isRightComp) return; |
8475 | |||||
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8476 | SourceRange DiagRange = isLeftComp ? SourceRange(LHSExpr->getLocStart(), |
8477 | OpLoc) | ||||
8478 | : SourceRange(OpLoc, RHSExpr->getLocEnd()); | ||||
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8479 | StringRef OpStr = isLeftComp ? BinOp::getOpcodeStr(LHSopc) |
8480 | : BinOp::getOpcodeStr(RHSopc); | ||||
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8481 | SourceRange ParensRange = isLeftComp ? |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8482 | SourceRange(cast<BinOp>(LHSExpr)->getRHS()->getLocStart(), |
8483 | RHSExpr->getLocEnd()) | ||||
8484 | : SourceRange(LHSExpr->getLocStart(), | ||||
8485 | cast<BinOp>(RHSExpr)->getLHS()->getLocStart()); | ||||
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8486 | |
8487 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) | ||||
8488 | << DiagRange << BinOp::getOpcodeStr(Opc) << OpStr; | ||||
8489 | SuggestParentheses(Self, OpLoc, | ||||
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8490 | Self.PDiag(diag::note_precedence_silence) << OpStr, |
Nico Weber | 40e2999 | 2012-06-03 07:07:00 +0000 | [diff] [blame] | 8491 | (isLeftComp ? LHSExpr : RHSExpr)->getSourceRange()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8492 | SuggestParentheses(Self, OpLoc, |
8493 | Self.PDiag(diag::note_precedence_bitwise_first) << BinOp::getOpcodeStr(Opc), | ||||
8494 | ParensRange); | ||||
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8495 | } |
8496 | |||||
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8497 | /// \brief It accepts a '&' expr that is inside a '|' one. |
8498 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression | ||||
8499 | /// in parentheses. | ||||
8500 | static void | ||||
8501 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, | ||||
8502 | BinaryOperator *Bop) { | ||||
8503 | assert(Bop->getOpcode() == BO_And); | ||||
8504 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) | ||||
8505 | << Bop->getSourceRange() << OpLoc; | ||||
8506 | SuggestParentheses(Self, Bop->getOperatorLoc(), | ||||
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8507 | Self.PDiag(diag::note_precedence_silence) |
8508 | << Bop->getOpcodeStr(), | ||||
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8509 | Bop->getSourceRange()); |
8510 | } | ||||
8511 | |||||
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8512 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
8513 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression | ||||
8514 | /// in parentheses. | ||||
8515 | static void | ||||
8516 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, | ||||
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8517 | BinaryOperator *Bop) { |
8518 | assert(Bop->getOpcode() == BO_LAnd); | ||||
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 8519 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
8520 | << Bop->getSourceRange() << OpLoc; | ||||
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8521 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8522 | Self.PDiag(diag::note_precedence_silence) |
8523 | << Bop->getOpcodeStr(), | ||||
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 8524 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8525 | } |
8526 | |||||
8527 | /// \brief Returns true if the given expression can be evaluated as a constant | ||||
8528 | /// 'true'. | ||||
8529 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { | ||||
8530 | bool Res; | ||||
8531 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; | ||||
8532 | } | ||||
8533 | |||||
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8534 | /// \brief Returns true if the given expression can be evaluated as a constant |
8535 | /// 'false'. | ||||
8536 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { | ||||
8537 | bool Res; | ||||
8538 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; | ||||
8539 | } | ||||
8540 | |||||
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8541 | /// \brief Look for '&&' in the left hand of a '||' expr. |
8542 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, | ||||
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8543 | Expr *LHSExpr, Expr *RHSExpr) { |
8544 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(LHSExpr)) { | ||||
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8545 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8546 | // 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] | 8547 | if (EvaluatesAsFalse(S, RHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8548 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8549 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
8550 | if (!EvaluatesAsTrue(S, Bop->getLHS())) | ||||
8551 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); | ||||
8552 | } else if (Bop->getOpcode() == BO_LOr) { | ||||
8553 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { | ||||
8554 | // If it's "a || b && 1 || c" we didn't warn earlier for | ||||
8555 | // "a || b && 1", but warn now. | ||||
8556 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) | ||||
8557 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); | ||||
8558 | } | ||||
8559 | } | ||||
8560 | } | ||||
8561 | } | ||||
8562 | |||||
8563 | /// \brief Look for '&&' in the right hand of a '||' expr. | ||||
8564 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, | ||||
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8565 | Expr *LHSExpr, Expr *RHSExpr) { |
8566 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(RHSExpr)) { | ||||
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8567 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8568 | // 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] | 8569 | if (EvaluatesAsFalse(S, LHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8570 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8571 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
8572 | if (!EvaluatesAsTrue(S, Bop->getRHS())) | ||||
8573 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); | ||||
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8574 | } |
8575 | } | ||||
8576 | } | ||||
8577 | |||||
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8578 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
8579 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, | ||||
8580 | Expr *OrArg) { | ||||
8581 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { | ||||
8582 | if (Bop->getOpcode() == BO_And) | ||||
8583 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); | ||||
8584 | } | ||||
8585 | } | ||||
8586 | |||||
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8587 | static void DiagnoseAdditionInShift(Sema &S, SourceLocation OpLoc, |
8588 | Expr *SubExpr, StringRef shift) { | ||||
8589 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(SubExpr)) { | ||||
8590 | if (Bop->getOpcode() == BO_Add || Bop->getOpcode() == BO_Sub) { | ||||
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8591 | StringRef Op = Bop->getOpcodeStr(); |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8592 | S.Diag(Bop->getOperatorLoc(), diag::warn_addition_in_bitshift) |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8593 | << Bop->getSourceRange() << OpLoc << Op << shift; |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8594 | SuggestParentheses(S, Bop->getOperatorLoc(), |
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8595 | S.PDiag(diag::note_precedence_silence) << Op, |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8596 | Bop->getSourceRange()); |
8597 | } | ||||
8598 | } | ||||
8599 | } | ||||
8600 | |||||
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8601 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8602 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8603 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8604 | SourceLocation OpLoc, Expr *LHSExpr, |
8605 | Expr *RHSExpr){ | ||||
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8606 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8607 | if (BinaryOperator::isBitwiseOp(Opc)) |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8608 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8609 | |
8610 | // Diagnose "arg1 & arg2 | arg3" | ||||
8611 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { | ||||
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8612 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, LHSExpr); |
8613 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, RHSExpr); | ||||
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8614 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8615 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8616 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
8617 | // 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] | 8618 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8619 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); |
8620 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); | ||||
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8621 | } |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8622 | |
8623 | if ((Opc == BO_Shl && LHSExpr->getType()->isIntegralType(Self.getASTContext())) | ||||
8624 | || Opc == BO_Shr) { | ||||
David Blaikie | 6b34c17 | 2012-10-08 01:19:49 +0000 | [diff] [blame] | 8625 | StringRef shift = BinaryOperator::getOpcodeStr(Opc); |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 8626 | DiagnoseAdditionInShift(Self, OpLoc, LHSExpr, shift); |
8627 | DiagnoseAdditionInShift(Self, OpLoc, RHSExpr, shift); | ||||
8628 | } | ||||
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8629 | } |
8630 | |||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8631 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8632 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8633 | tok::TokenKind Kind, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8634 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8635 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8636 | assert((LHSExpr != 0) && "ActOnBinOp(): missing left expression"); |
8637 | assert((RHSExpr != 0) && "ActOnBinOp(): missing right expression"); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8638 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8639 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8640 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8641 | |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8642 | return BuildBinOp(S, TokLoc, Opc, LHSExpr, RHSExpr); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8643 | } |
8644 | |||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8645 | /// Build an overloaded binary operator expression in the given scope. |
8646 | static ExprResult BuildOverloadedBinOp(Sema &S, Scope *Sc, SourceLocation OpLoc, | ||||
8647 | BinaryOperatorKind Opc, | ||||
8648 | Expr *LHS, Expr *RHS) { | ||||
8649 | // Find all of the overloaded operators visible from this | ||||
8650 | // point. We perform both an operator-name lookup from the local | ||||
8651 | // scope and an argument-dependent lookup based on the types of | ||||
8652 | // the arguments. | ||||
8653 | UnresolvedSet<16> Functions; | ||||
8654 | OverloadedOperatorKind OverOp | ||||
8655 | = BinaryOperator::getOverloadedOperator(Opc); | ||||
8656 | if (Sc && OverOp != OO_None) | ||||
8657 | S.LookupOverloadedOperatorName(OverOp, Sc, LHS->getType(), | ||||
8658 | RHS->getType(), Functions); | ||||
8659 | |||||
8660 | // Build the (potentially-overloaded, potentially-dependent) | ||||
8661 | // binary operation. | ||||
8662 | return S.CreateOverloadedBinOp(OpLoc, Opc, Functions, LHS, RHS); | ||||
8663 | } | ||||
8664 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8665 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8666 | BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8667 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8668 | // We want to end up calling one of checkPseudoObjectAssignment |
8669 | // (if the LHS is a pseudo-object), BuildOverloadedBinOp (if | ||||
8670 | // both expressions are overloadable or either is type-dependent), | ||||
8671 | // or CreateBuiltinBinOp (in any other case). We also want to get | ||||
8672 | // any placeholder types out of the way. | ||||
8673 | |||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8674 | // Handle pseudo-objects in the LHS. |
8675 | if (const BuiltinType *pty = LHSExpr->getType()->getAsPlaceholderType()) { | ||||
8676 | // Assignments with a pseudo-object l-value need special analysis. | ||||
8677 | if (pty->getKind() == BuiltinType::PseudoObject && | ||||
8678 | BinaryOperator::isAssignmentOp(Opc)) | ||||
8679 | return checkPseudoObjectAssignment(S, OpLoc, Opc, LHSExpr, RHSExpr); | ||||
8680 | |||||
8681 | // Don't resolve overloads if the other type is overloadable. | ||||
8682 | if (pty->getKind() == BuiltinType::Overload) { | ||||
8683 | // We can't actually test that if we still have a placeholder, | ||||
8684 | // though. Fortunately, none of the exceptions we see in that | ||||
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8685 | // code below are valid when the LHS is an overload set. Note |
8686 | // that an overload set can be dependently-typed, but it never | ||||
8687 | // instantiates to having an overloadable type. | ||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8688 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); |
8689 | if (resolvedRHS.isInvalid()) return ExprError(); | ||||
8690 | RHSExpr = resolvedRHS.take(); | ||||
8691 | |||||
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8692 | if (RHSExpr->isTypeDependent() || |
8693 | RHSExpr->getType()->isOverloadableType()) | ||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8694 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
8695 | } | ||||
8696 | |||||
8697 | ExprResult LHS = CheckPlaceholderExpr(LHSExpr); | ||||
8698 | if (LHS.isInvalid()) return ExprError(); | ||||
8699 | LHSExpr = LHS.take(); | ||||
8700 | } | ||||
8701 | |||||
8702 | // Handle pseudo-objects in the RHS. | ||||
8703 | if (const BuiltinType *pty = RHSExpr->getType()->getAsPlaceholderType()) { | ||||
8704 | // An overload in the RHS can potentially be resolved by the type | ||||
8705 | // being assigned to. | ||||
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8706 | if (Opc == BO_Assign && pty->getKind() == BuiltinType::Overload) { |
8707 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) | ||||
8708 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); | ||||
8709 | |||||
Eli Friedman | 8788491 | 2012-01-17 21:27:43 +0000 | [diff] [blame] | 8710 | if (LHSExpr->getType()->isOverloadableType()) |
8711 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); | ||||
8712 | |||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8713 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8714 | } |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8715 | |
8716 | // Don't resolve overloads if the other type is overloadable. | ||||
8717 | if (pty->getKind() == BuiltinType::Overload && | ||||
8718 | LHSExpr->getType()->isOverloadableType()) | ||||
8719 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); | ||||
8720 | |||||
8721 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); | ||||
8722 | if (!resolvedRHS.isUsable()) return ExprError(); | ||||
8723 | RHSExpr = resolvedRHS.take(); | ||||
8724 | } | ||||
8725 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8726 | if (getLangOpts().CPlusPlus) { |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8727 | // If either expression is type-dependent, always build an |
8728 | // overloaded op. | ||||
8729 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) | ||||
8730 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8731 | |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8732 | // Otherwise, build an overloaded op if either expression has an |
8733 | // overloadable type. | ||||
8734 | if (LHSExpr->getType()->isOverloadableType() || | ||||
8735 | RHSExpr->getType()->isOverloadableType()) | ||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8736 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8737 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8738 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8739 | // Build a built-in binary operation. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8740 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8741 | } |
8742 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8743 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8744 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8745 | Expr *InputExpr) { |
8746 | ExprResult Input = Owned(InputExpr); | ||||
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8747 | ExprValueKind VK = VK_RValue; |
8748 | ExprObjectKind OK = OK_Ordinary; | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8749 | QualType resultType; |
8750 | switch (Opc) { | ||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8751 | case UO_PreInc: |
8752 | case UO_PreDec: | ||||
8753 | case UO_PostInc: | ||||
8754 | case UO_PostDec: | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8755 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8756 | Opc == UO_PreInc || |
8757 | Opc == UO_PostInc, | ||||
8758 | Opc == UO_PreInc || | ||||
8759 | Opc == UO_PreDec); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8760 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8761 | case UO_AddrOf: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8762 | resultType = CheckAddressOfOperand(*this, Input, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8763 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8764 | case UO_Deref: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8765 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 8766 | if (Input.isInvalid()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8767 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8768 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8769 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8770 | case UO_Plus: |
8771 | case UO_Minus: | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8772 | Input = UsualUnaryConversions(Input.take()); |
8773 | if (Input.isInvalid()) return ExprError(); | ||||
8774 | resultType = Input.get()->getType(); | ||||
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8775 | if (resultType->isDependentType()) |
8776 | break; | ||||
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8777 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
8778 | resultType->isVectorType()) | ||||
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8779 | break; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8780 | else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6-7 |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8781 | resultType->isEnumeralType()) |
8782 | break; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8783 | else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8784 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8785 | resultType->isPointerType()) |
8786 | break; | ||||
8787 | |||||
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8788 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8789 | << resultType << Input.get()->getSourceRange()); |
8790 | |||||
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8791 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8792 | Input = UsualUnaryConversions(Input.take()); |
8793 | if (Input.isInvalid()) return ExprError(); | ||||
8794 | resultType = Input.get()->getType(); | ||||
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8795 | if (resultType->isDependentType()) |
8796 | break; | ||||
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8797 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
8798 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) | ||||
8799 | // C99 does not support '~' for complex conjugation. | ||||
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8800 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8801 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8802 | else if (resultType->hasIntegerRepresentation()) |
8803 | break; | ||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8804 | else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8805 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8806 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8807 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8808 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8809 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8810 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8811 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8812 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
8813 | if (Input.isInvalid()) return ExprError(); | ||||
8814 | resultType = Input.get()->getType(); | ||||
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 8815 | |
8816 | // Though we still have to promote half FP to float... | ||||
8817 | if (resultType->isHalfType()) { | ||||
8818 | Input = ImpCastExprToType(Input.take(), Context.FloatTy, CK_FloatingCast).take(); | ||||
8819 | resultType = Context.FloatTy; | ||||
8820 | } | ||||
8821 | |||||
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8822 | if (resultType->isDependentType()) |
8823 | break; | ||||
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8824 | if (resultType->isScalarType()) { |
8825 | // C99 6.5.3.3p1: ok, fallthrough; | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8826 | if (Context.getLangOpts().CPlusPlus) { |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8827 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
8828 | // operand contextually converted to bool. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8829 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
8830 | ScalarTypeToBooleanCastKind(resultType)); | ||||
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8831 | } |
Tanya Lattner | b0f9dd2 | 2012-01-19 01:16:16 +0000 | [diff] [blame] | 8832 | } else if (resultType->isExtVectorType()) { |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 8833 | // Vector logical not returns the signed variant of the operand type. |
8834 | resultType = GetSignedVectorType(resultType); | ||||
8835 | break; | ||||
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8836 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8837 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8838 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8839 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8840 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8841 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8842 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8843 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8844 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8845 | case UO_Real: |
8846 | case UO_Imag: | ||||
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8847 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8848 | // _Real maps ordinary l-values into ordinary l-values. _Imag maps ordinary |
8849 | // 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] | 8850 | if (Input.isInvalid()) return ExprError(); |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8851 | if (Opc == UO_Real || Input.get()->getType()->isAnyComplexType()) { |
8852 | if (Input.get()->getValueKind() != VK_RValue && | ||||
8853 | Input.get()->getObjectKind() == OK_Ordinary) | ||||
8854 | VK = Input.get()->getValueKind(); | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8855 | } else if (!getLangOpts().CPlusPlus) { |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8856 | // In C, a volatile scalar is read by __imag. In C++, it is not. |
8857 | Input = DefaultLvalueConversion(Input.take()); | ||||
8858 | } | ||||
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8859 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8860 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8861 | resultType = Input.get()->getType(); |
8862 | VK = Input.get()->getValueKind(); | ||||
8863 | OK = Input.get()->getObjectKind(); | ||||
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8864 | break; |
8865 | } | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8866 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8867 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8868 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8869 | // Check for array bounds violations in the operand of the UnaryOperator, |
8870 | // except for the '*' and '&' operators that have to be handled specially | ||||
8871 | // by CheckArrayAccess (as there are special cases like &array[arraysize] | ||||
8872 | // that are explicitly defined as valid by the standard). | ||||
8873 | if (Opc != UO_AddrOf && Opc != UO_Deref) | ||||
8874 | CheckArrayAccess(Input.get()); | ||||
8875 | |||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8876 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8877 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8878 | } |
8879 | |||||
Douglas Gregor | d3d0853 | 2011-12-14 21:23:13 +0000 | [diff] [blame] | 8880 | /// \brief Determine whether the given expression is a qualified member |
8881 | /// access expression, of a form that could be turned into a pointer to member | ||||
8882 | /// with the address-of operator. | ||||
8883 | static bool isQualifiedMemberAccess(Expr *E) { | ||||
8884 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { | ||||
8885 | if (!DRE->getQualifier()) | ||||
8886 | return false; | ||||
8887 | |||||
8888 | ValueDecl *VD = DRE->getDecl(); | ||||
8889 | if (!VD->isCXXClassMember()) | ||||
8890 | return false; | ||||
8891 | |||||
8892 | if (isa<FieldDecl>(VD) || isa<IndirectFieldDecl>(VD)) | ||||
8893 | return true; | ||||
8894 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(VD)) | ||||
8895 | return Method->isInstance(); | ||||
8896 | |||||
8897 | return false; | ||||
8898 | } | ||||
8899 | |||||
8900 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { | ||||
8901 | if (!ULE->getQualifier()) | ||||
8902 | return false; | ||||
8903 | |||||
8904 | for (UnresolvedLookupExpr::decls_iterator D = ULE->decls_begin(), | ||||
8905 | DEnd = ULE->decls_end(); | ||||
8906 | D != DEnd; ++D) { | ||||
8907 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*D)) { | ||||
8908 | if (Method->isInstance()) | ||||
8909 | return true; | ||||
8910 | } else { | ||||
8911 | // Overload set does not contain methods. | ||||
8912 | break; | ||||
8913 | } | ||||
8914 | } | ||||
8915 | |||||
8916 | return false; | ||||
8917 | } | ||||
8918 | |||||
8919 | return false; | ||||
8920 | } | ||||
8921 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8922 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8923 | UnaryOperatorKind Opc, Expr *Input) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8924 | // First things first: handle placeholders so that the |
8925 | // overloaded-operator check considers the right type. | ||||
8926 | if (const BuiltinType *pty = Input->getType()->getAsPlaceholderType()) { | ||||
8927 | // Increment and decrement of pseudo-object references. | ||||
8928 | if (pty->getKind() == BuiltinType::PseudoObject && | ||||
8929 | UnaryOperator::isIncrementDecrementOp(Opc)) | ||||
8930 | return checkPseudoObjectIncDec(S, OpLoc, Opc, Input); | ||||
8931 | |||||
8932 | // extension is always a builtin operator. | ||||
8933 | if (Opc == UO_Extension) | ||||
8934 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); | ||||
8935 | |||||
8936 | // & gets special logic for several kinds of placeholder. | ||||
8937 | // The builtin code knows what to do. | ||||
8938 | if (Opc == UO_AddrOf && | ||||
8939 | (pty->getKind() == BuiltinType::Overload || | ||||
8940 | pty->getKind() == BuiltinType::UnknownAny || | ||||
8941 | pty->getKind() == BuiltinType::BoundMember)) | ||||
8942 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); | ||||
8943 | |||||
8944 | // Anything else needs to be handled now. | ||||
8945 | ExprResult Result = CheckPlaceholderExpr(Input); | ||||
8946 | if (Result.isInvalid()) return ExprError(); | ||||
8947 | Input = Result.take(); | ||||
8948 | } | ||||
8949 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8950 | if (getLangOpts().CPlusPlus && Input->getType()->isOverloadableType() && |
Douglas Gregor | d3d0853 | 2011-12-14 21:23:13 +0000 | [diff] [blame] | 8951 | UnaryOperator::getOverloadedOperator(Opc) != OO_None && |
8952 | !(Opc == UO_AddrOf && isQualifiedMemberAccess(Input))) { | ||||
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8953 | // Find all of the overloaded operators visible from this |
8954 | // point. We perform both an operator-name lookup from the local | ||||
8955 | // scope and an argument-dependent lookup based on the types of | ||||
8956 | // the arguments. | ||||
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8957 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8958 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8959 | if (S && OverOp != OO_None) |
8960 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), | ||||
8961 | Functions); | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8962 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8963 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8964 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8965 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8966 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8967 | } |
8968 | |||||
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8969 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8970 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8971 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8972 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8973 | } |
8974 | |||||
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8975 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8976 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8977 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8978 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8979 | // 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] | 8980 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8981 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8982 | } |
8983 | |||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8984 | /// Given the last statement in a statement-expression, check whether |
8985 | /// the result is a producing expression (like a call to an | ||||
8986 | /// ns_returns_retained function) and, if so, rebuild it to hoist the | ||||
8987 | /// release out of the full-expression. Otherwise, return null. | ||||
8988 | /// Cannot fail. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8989 | static Expr *maybeRebuildARCConsumingStmt(Stmt *Statement) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8990 | // Should always be wrapped with one of these. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8991 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(Statement); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8992 | if (!cleanups) return 0; |
8993 | |||||
8994 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); | ||||
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 8995 | if (!cast || cast->getCastKind() != CK_ARCConsumeObject) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8996 | return 0; |
8997 | |||||
8998 | // Splice out the cast. This shouldn't modify any interesting | ||||
8999 | // features of the statement. | ||||
9000 | Expr *producer = cast->getSubExpr(); | ||||
9001 | assert(producer->getType() == cast->getType()); | ||||
9002 | assert(producer->getValueKind() == cast->getValueKind()); | ||||
9003 | cleanups->setSubExpr(producer); | ||||
9004 | return cleanups; | ||||
9005 | } | ||||
9006 | |||||
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 9007 | void Sema::ActOnStartStmtExpr() { |
9008 | PushExpressionEvaluationContext(ExprEvalContexts.back().Context); | ||||
9009 | } | ||||
9010 | |||||
9011 | void Sema::ActOnStmtExprError() { | ||||
John McCall | 7f39d51 | 2012-04-06 18:20:53 +0000 | [diff] [blame] | 9012 | // Note that function is also called by TreeTransform when leaving a |
9013 | // StmtExpr scope without rebuilding anything. | ||||
9014 | |||||
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 9015 | DiscardCleanupsInEvaluationContext(); |
9016 | PopExpressionEvaluationContext(); | ||||
9017 | } | ||||
9018 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9019 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9020 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9021 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9022 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
9023 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); | ||||
9024 | |||||
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 9025 | if (hasAnyUnrecoverableErrorsInThisFunction()) |
9026 | DiscardCleanupsInEvaluationContext(); | ||||
9027 | assert(!ExprNeedsCleanups && "cleanups within StmtExpr not correctly bound!"); | ||||
9028 | PopExpressionEvaluationContext(); | ||||
9029 | |||||
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 9030 | bool isFileScope |
9031 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); | ||||
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 9032 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9033 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 9034 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9035 | // FIXME: there are a variety of strange constraints to enforce here, for |
9036 | // example, it is not possible to goto into a stmt expression apparently. | ||||
9037 | // More semantic analysis is needed. | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9038 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9039 | // If there are sub stmts in the compound stmt, take the type of the last one |
9040 | // as the type of the stmtexpr. | ||||
9041 | QualType Ty = Context.VoidTy; | ||||
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9042 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9043 | if (!Compound->body_empty()) { |
9044 | Stmt *LastStmt = Compound->body_back(); | ||||
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9045 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9046 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9047 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
9048 | LastLabelStmt = Label; | ||||
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9049 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9050 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9051 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9052 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9053 | // Do function/array conversion on the last expression, but not |
9054 | // lvalue-to-rvalue. However, initialize an unqualified type. | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9055 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
9056 | if (LastExpr.isInvalid()) | ||||
9057 | return ExprError(); | ||||
9058 | Ty = LastExpr.get()->getType().getUnqualifiedType(); | ||||
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9059 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9060 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9061 | // In ARC, if the final expression ends in a consume, splice |
9062 | // the consume out and bind it later. In the alternate case | ||||
9063 | // (when dealing with a retainable type), the result | ||||
9064 | // initialization will create a produce. In both cases the | ||||
9065 | // result will be +1, and we'll need to balance that out with | ||||
9066 | // a bind. | ||||
9067 | if (Expr *rebuiltLastStmt | ||||
9068 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { | ||||
9069 | LastExpr = rebuiltLastStmt; | ||||
9070 | } else { | ||||
9071 | LastExpr = PerformCopyInitialization( | ||||
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9072 | InitializedEntity::InitializeResult(LPLoc, |
9073 | Ty, | ||||
9074 | false), | ||||
9075 | SourceLocation(), | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9076 | LastExpr); |
9077 | } | ||||
9078 | |||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9079 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9080 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9081 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9082 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9083 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9084 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9085 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9086 | StmtExprMayBindToTemp = true; |
9087 | } | ||||
9088 | } | ||||
9089 | } | ||||
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9090 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9091 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9092 | // FIXME: Check that expression type is complete/non-abstract; statement |
9093 | // expressions are not lvalues. | ||||
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9094 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
9095 | if (StmtExprMayBindToTemp) | ||||
9096 | return MaybeBindToTemporary(ResStmtExpr); | ||||
9097 | return Owned(ResStmtExpr); | ||||
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9098 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 9099 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9100 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9101 | TypeSourceInfo *TInfo, |
9102 | OffsetOfComponent *CompPtr, | ||||
9103 | unsigned NumComponents, | ||||
9104 | SourceLocation RParenLoc) { | ||||
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9105 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9106 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9107 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9108 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9109 | // We must have at least one component that refers to the type, and the first |
9110 | // one is known to be a field designator. Verify that the ArgTy represents | ||||
9111 | // a struct/union/class. | ||||
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9112 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9113 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
9114 | << ArgTy << TypeRange); | ||||
9115 | |||||
9116 | // Type must be complete per C99 7.17p3 because a declaring a variable | ||||
9117 | // with an incomplete type would be ill-formed. | ||||
9118 | if (!Dependent | ||||
9119 | && RequireCompleteType(BuiltinLoc, ArgTy, | ||||
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 9120 | diag::err_offsetof_incomplete_type, TypeRange)) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9121 | return ExprError(); |
9122 | |||||
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9123 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
9124 | // GCC extension, diagnose them. | ||||
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 9125 | // FIXME: This diagnostic isn't actually visible because the location is in |
9126 | // a system header! | ||||
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9127 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 9128 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
9129 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); | ||||
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9130 | |
9131 | bool DidWarnAboutNonPOD = false; | ||||
9132 | QualType CurrentType = ArgTy; | ||||
9133 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; | ||||
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9134 | SmallVector<OffsetOfNode, 4> Comps; |
9135 | SmallVector<Expr*, 4> Exprs; | ||||
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9136 | for (unsigned i = 0; i != NumComponents; ++i) { |
9137 | const OffsetOfComponent &OC = CompPtr[i]; | ||||
9138 | if (OC.isBrackets) { | ||||
9139 | // Offset of an array sub-field. TODO: Should we allow vector elements? | ||||
9140 | if (!CurrentType->isDependentType()) { | ||||
9141 | const ArrayType *AT = Context.getAsArrayType(CurrentType); | ||||
9142 | if(!AT) | ||||
9143 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) | ||||
9144 | << CurrentType); | ||||
9145 | CurrentType = AT->getElementType(); | ||||
9146 | } else | ||||
9147 | CurrentType = Context.DependentTy; | ||||
9148 | |||||
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 9149 | ExprResult IdxRval = DefaultLvalueConversion(static_cast<Expr*>(OC.U.E)); |
9150 | if (IdxRval.isInvalid()) | ||||
9151 | return ExprError(); | ||||
9152 | Expr *Idx = IdxRval.take(); | ||||
9153 | |||||
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9154 | // The expression must be an integral expression. |
9155 | // FIXME: An integral constant expression? | ||||
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9156 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
9157 | !Idx->getType()->isIntegerType()) | ||||
9158 | return ExprError(Diag(Idx->getLocStart(), | ||||
9159 | diag::err_typecheck_subscript_not_integer) | ||||
9160 | << Idx->getSourceRange()); | ||||
Richard Smith | d82e5d3 | 2011-10-17 05:48:07 +0000 | [diff] [blame] | 9161 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9162 | // Record this array index. |
9163 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); | ||||
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 9164 | Exprs.push_back(Idx); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9165 | continue; |
9166 | } | ||||
9167 | |||||
9168 | // Offset of a field. | ||||
9169 | if (CurrentType->isDependentType()) { | ||||
9170 | // We have the offset of a field, but we can't look into the dependent | ||||
9171 | // type. Just record the identifier of the field. | ||||
9172 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); | ||||
9173 | CurrentType = Context.DependentTy; | ||||
9174 | continue; | ||||
9175 | } | ||||
9176 | |||||
9177 | // We need to have a complete type to look into. | ||||
9178 | if (RequireCompleteType(OC.LocStart, CurrentType, | ||||
9179 | diag::err_offsetof_incomplete_type)) | ||||
9180 | return ExprError(); | ||||
9181 | |||||
9182 | // Look for the designated field. | ||||
9183 | const RecordType *RC = CurrentType->getAs<RecordType>(); | ||||
9184 | if (!RC) | ||||
9185 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) | ||||
9186 | << CurrentType); | ||||
9187 | RecordDecl *RD = RC->getDecl(); | ||||
9188 | |||||
9189 | // C++ [lib.support.types]p5: | ||||
9190 | // The macro offsetof accepts a restricted set of type arguments in this | ||||
9191 | // International Standard. type shall be a POD structure or a POD union | ||||
9192 | // (clause 9). | ||||
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9193 | // C++11 [support.types]p4: |
9194 | // If type is not a standard-layout class (Clause 9), the results are | ||||
9195 | // undefined. | ||||
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9196 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9197 | bool IsSafe = LangOpts.CPlusPlus0x? CRD->isStandardLayout() : CRD->isPOD(); |
9198 | unsigned DiagID = | ||||
9199 | LangOpts.CPlusPlus0x? diag::warn_offsetof_non_standardlayout_type | ||||
9200 | : diag::warn_offsetof_non_pod_type; | ||||
9201 | |||||
9202 | if (!IsSafe && !DidWarnAboutNonPOD && | ||||
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9203 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9204 | PDiag(DiagID) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9205 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
9206 | << CurrentType)) | ||||
9207 | DidWarnAboutNonPOD = true; | ||||
9208 | } | ||||
9209 | |||||
9210 | // Look for the field. | ||||
9211 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); | ||||
9212 | LookupQualifiedName(R, RD); | ||||
9213 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); | ||||
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9214 | IndirectFieldDecl *IndirectMemberDecl = 0; |
9215 | if (!MemberDecl) { | ||||
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 9216 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9217 | MemberDecl = IndirectMemberDecl->getAnonField(); |
9218 | } | ||||
9219 | |||||
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9220 | if (!MemberDecl) |
9221 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) | ||||
9222 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, | ||||
9223 | OC.LocEnd)); | ||||
9224 | |||||
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9225 | // C99 7.17p3: |
9226 | // (If the specified member is a bit-field, the behavior is undefined.) | ||||
9227 | // | ||||
9228 | // We diagnose this as an error. | ||||
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9229 | if (MemberDecl->isBitField()) { |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9230 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
9231 | << MemberDecl->getDeclName() | ||||
9232 | << SourceRange(BuiltinLoc, RParenLoc); | ||||
9233 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); | ||||
9234 | return ExprError(); | ||||
9235 | } | ||||
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9236 | |
9237 | RecordDecl *Parent = MemberDecl->getParent(); | ||||
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9238 | if (IndirectMemberDecl) |
9239 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); | ||||
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9240 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9241 | // If the member was found in a base class, introduce OffsetOfNodes for |
9242 | // the base class indirections. | ||||
9243 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, | ||||
9244 | /*DetectVirtual=*/false); | ||||
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9245 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9246 | CXXBasePath &Path = Paths.front(); |
9247 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); | ||||
9248 | B != BEnd; ++B) | ||||
9249 | Comps.push_back(OffsetOfNode(B->Base)); | ||||
9250 | } | ||||
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9251 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9252 | if (IndirectMemberDecl) { |
9253 | for (IndirectFieldDecl::chain_iterator FI = | ||||
9254 | IndirectMemberDecl->chain_begin(), | ||||
9255 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { | ||||
9256 | assert(isa<FieldDecl>(*FI)); | ||||
9257 | Comps.push_back(OffsetOfNode(OC.LocStart, | ||||
9258 | cast<FieldDecl>(*FI), OC.LocEnd)); | ||||
9259 | } | ||||
9260 | } else | ||||
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9261 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9262 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9263 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
9264 | } | ||||
9265 | |||||
9266 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, | ||||
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 9267 | TInfo, Comps, Exprs, RParenLoc)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9268 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9269 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9270 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9271 | SourceLocation BuiltinLoc, |
9272 | SourceLocation TypeLoc, | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9273 | ParsedType ParsedArgTy, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9274 | OffsetOfComponent *CompPtr, |
9275 | unsigned NumComponents, | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9276 | SourceLocation RParenLoc) { |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9277 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9278 | TypeSourceInfo *ArgTInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9279 | QualType ArgTy = GetTypeFromParser(ParsedArgTy, &ArgTInfo); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9280 | if (ArgTy.isNull()) |
9281 | return ExprError(); | ||||
9282 | |||||
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 9283 | if (!ArgTInfo) |
9284 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); | ||||
9285 | |||||
9286 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9287 | RParenLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9288 | } |
9289 | |||||
9290 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9291 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9292 | Expr *CondExpr, |
9293 | Expr *LHSExpr, Expr *RHSExpr, | ||||
9294 | SourceLocation RPLoc) { | ||||
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9295 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
9296 | |||||
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9297 | ExprValueKind VK = VK_RValue; |
9298 | ExprObjectKind OK = OK_Ordinary; | ||||
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9299 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9300 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 9301 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9302 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9303 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9304 | } else { |
9305 | // The conditional expression is required to be a constant expression. | ||||
9306 | llvm::APSInt condEval(32); | ||||
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9307 | ExprResult CondICE |
9308 | = VerifyIntegerConstantExpression(CondExpr, &condEval, | ||||
9309 | diag::err_typecheck_choose_expr_requires_constant, false); | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9310 | if (CondICE.isInvalid()) |
9311 | return ExprError(); | ||||
9312 | CondExpr = CondICE.take(); | ||||
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9313 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9314 | // 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] | 9315 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
9316 | |||||
9317 | resType = ActiveExpr->getType(); | ||||
9318 | ValueDependent = ActiveExpr->isValueDependent(); | ||||
9319 | VK = ActiveExpr->getValueKind(); | ||||
9320 | OK = ActiveExpr->getObjectKind(); | ||||
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9321 | } |
9322 | |||||
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9323 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9324 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9325 | resType->isDependentType(), |
9326 | ValueDependent)); | ||||
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9327 | } |
9328 | |||||
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9329 | //===----------------------------------------------------------------------===// |
9330 | // Clang Extensions. | ||||
9331 | //===----------------------------------------------------------------------===// | ||||
9332 | |||||
9333 | /// ActOnBlockStart - This callback is invoked when a block literal is started. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9334 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9335 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9336 | PushBlockScope(CurScope, Block); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9337 | CurContext->addDecl(Block); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9338 | if (CurScope) |
9339 | PushDeclContext(CurScope, Block); | ||||
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9340 | else |
9341 | CurContext = Block; | ||||
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9342 | |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 9343 | getCurBlock()->HasImplicitReturnType = true; |
9344 | |||||
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9345 | // Enter a new evaluation context to insulate the block from any |
9346 | // cleanups from the enclosing full-expression. | ||||
9347 | PushExpressionEvaluationContext(PotentiallyEvaluated); | ||||
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9348 | } |
9349 | |||||
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 9350 | void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, |
9351 | Scope *CurScope) { | ||||
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 9352 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9353 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9354 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9355 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9356 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9357 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9358 | |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 9359 | // FIXME: We should allow unexpanded parameter packs here, but that would, |
9360 | // in turn, make the block expression contain unexpanded parameter packs. | ||||
9361 | if (DiagnoseUnexpandedParameterPack(CaretLoc, Sig, UPPC_Block)) { | ||||
9362 | // Drop the parameters. | ||||
9363 | FunctionProtoType::ExtProtoInfo EPI; | ||||
9364 | EPI.HasTrailingReturn = false; | ||||
9365 | EPI.TypeQuals |= DeclSpec::TQ_const; | ||||
9366 | T = Context.getFunctionType(Context.DependentTy, /*Args=*/0, /*NumArgs=*/0, | ||||
9367 | EPI); | ||||
9368 | Sig = Context.getTrivialTypeSourceInfo(T); | ||||
9369 | } | ||||
9370 | |||||
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9371 | // GetTypeForDeclarator always produces a function type for a block |
9372 | // literal signature. Furthermore, it is always a FunctionProtoType | ||||
9373 | // unless the function was written with a typedef. | ||||
9374 | assert(T->isFunctionType() && | ||||
9375 | "GetTypeForDeclarator made a non-function block signature"); | ||||
9376 | |||||
9377 | // Look for an explicit signature in that function type. | ||||
9378 | FunctionProtoTypeLoc ExplicitSignature; | ||||
9379 | |||||
9380 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); | ||||
9381 | if (isa<FunctionProtoTypeLoc>(tmp)) { | ||||
9382 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); | ||||
9383 | |||||
9384 | // Check whether that explicit signature was synthesized by | ||||
9385 | // GetTypeForDeclarator. If so, don't save that as part of the | ||||
9386 | // written signature. | ||||
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 9387 | if (ExplicitSignature.getLocalRangeBegin() == |
9388 | ExplicitSignature.getLocalRangeEnd()) { | ||||
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9389 | // This would be much cheaper if we stored TypeLocs instead of |
9390 | // TypeSourceInfos. | ||||
9391 | TypeLoc Result = ExplicitSignature.getResultLoc(); | ||||
9392 | unsigned Size = Result.getFullDataSize(); | ||||
9393 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); | ||||
9394 | Sig->getTypeLoc().initializeFullCopy(Result, Size); | ||||
9395 | |||||
9396 | ExplicitSignature = FunctionProtoTypeLoc(); | ||||
9397 | } | ||||
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9398 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9399 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9400 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
9401 | CurBlock->FunctionType = T; | ||||
9402 | |||||
9403 | const FunctionType *Fn = T->getAs<FunctionType>(); | ||||
9404 | QualType RetTy = Fn->getResultType(); | ||||
9405 | bool isVariadic = | ||||
9406 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); | ||||
9407 | |||||
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9408 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 9409 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9410 | // Don't allow returning a objc interface by value. |
9411 | if (RetTy->isObjCObjectType()) { | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9412 | Diag(ParamInfo.getLocStart(), |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9413 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
9414 | return; | ||||
9415 | } | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9416 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9417 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9418 | // return type. TODO: what should we do with declarators like: |
9419 | // ^ * { ... } | ||||
9420 | // If the answer is "apply template argument deduction".... | ||||
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9421 | if (RetTy != Context.DependentTy) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9422 | CurBlock->ReturnType = RetTy; |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9423 | CurBlock->TheDecl->setBlockMissingReturnType(false); |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 9424 | CurBlock->HasImplicitReturnType = false; |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9425 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9426 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9427 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9428 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9429 | if (ExplicitSignature) { |
9430 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { | ||||
9431 | ParmVarDecl *Param = ExplicitSignature.getArg(I); | ||||
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9432 | if (Param->getIdentifier() == 0 && |
9433 | !Param->isImplicit() && | ||||
9434 | !Param->isInvalidDecl() && | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9435 | !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9436 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9437 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9438 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9439 | |
9440 | // Fake up parameter variables if we have a typedef, like | ||||
9441 | // ^ fntype { ... } | ||||
9442 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { | ||||
9443 | for (FunctionProtoType::arg_type_iterator | ||||
9444 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { | ||||
9445 | ParmVarDecl *Param = | ||||
9446 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9447 | ParamInfo.getLocStart(), |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9448 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9449 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9450 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9451 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9452 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9453 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9454 | if (!Params.empty()) { |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9455 | CurBlock->TheDecl->setParams(Params); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9456 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
9457 | CurBlock->TheDecl->param_end(), | ||||
9458 | /*CheckParameterNames=*/false); | ||||
9459 | } | ||||
9460 | |||||
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9461 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9462 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9463 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9464 | // Put the parameter variables in scope. We can bail out immediately |
9465 | // if we don't have any. | ||||
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9466 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9467 | return; |
9468 | |||||
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9469 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9470 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
9471 | (*AI)->setOwningFunction(CurBlock->TheDecl); | ||||
9472 | |||||
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9473 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9474 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9475 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9476 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9477 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9478 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9479 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9480 | } |
9481 | |||||
9482 | /// ActOnBlockError - If there is an error parsing a block, this callback | ||||
9483 | /// is invoked to pop the information about the block from the action impl. | ||||
9484 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { | ||||
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9485 | // Leave the expression-evaluation context. |
9486 | DiscardCleanupsInEvaluationContext(); | ||||
9487 | PopExpressionEvaluationContext(); | ||||
9488 | |||||
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9489 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9490 | PopDeclContext(); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9491 | PopFunctionScopeInfo(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9492 | } |
9493 | |||||
9494 | /// ActOnBlockStmtExpr - This is called when the body of a block statement | ||||
9495 | /// literal was successfully completed. ^(int x){...} | ||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9496 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9497 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9498 | // If blocks are disabled, emit an error. |
9499 | if (!LangOpts.Blocks) | ||||
9500 | Diag(CaretLoc, diag::err_blocks_disable); | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9501 | |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9502 | // Leave the expression-evaluation context. |
John McCall | 1e5bc4f | 2012-03-08 22:00:17 +0000 | [diff] [blame] | 9503 | if (hasAnyUnrecoverableErrorsInThisFunction()) |
9504 | DiscardCleanupsInEvaluationContext(); | ||||
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9505 | assert(!ExprNeedsCleanups && "cleanups within block not correctly bound!"); |
9506 | PopExpressionEvaluationContext(); | ||||
9507 | |||||
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9508 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9509 | |
9510 | if (BSI->HasImplicitReturnType) | ||||
9511 | deduceClosureReturnType(*BSI); | ||||
9512 | |||||
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9513 | PopDeclContext(); |
9514 | |||||
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9515 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9516 | if (!BSI->ReturnType.isNull()) |
9517 | RetTy = BSI->ReturnType; | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9518 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9519 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9520 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9521 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9522 | // Set the captured variables on the block. |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 9523 | // FIXME: Share capture structure between BlockDecl and CapturingScopeInfo! |
9524 | SmallVector<BlockDecl::Capture, 4> Captures; | ||||
9525 | for (unsigned i = 0, e = BSI->Captures.size(); i != e; i++) { | ||||
9526 | CapturingScopeInfo::Capture &Cap = BSI->Captures[i]; | ||||
9527 | if (Cap.isThisCapture()) | ||||
9528 | continue; | ||||
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9529 | BlockDecl::Capture NewCap(Cap.getVariable(), Cap.isBlockCapture(), |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 9530 | Cap.isNested(), Cap.getCopyExpr()); |
9531 | Captures.push_back(NewCap); | ||||
9532 | } | ||||
9533 | BSI->TheDecl->setCaptures(Context, Captures.begin(), Captures.end(), | ||||
9534 | BSI->CXXThisCaptureIndex != 0); | ||||
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9535 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9536 | // If the user wrote a function type in some form, try to use that. |
9537 | if (!BSI->FunctionType.isNull()) { | ||||
9538 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); | ||||
9539 | |||||
9540 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); | ||||
9541 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); | ||||
9542 | |||||
9543 | // Turn protoless block types into nullary block types. | ||||
9544 | if (isa<FunctionNoProtoType>(FTy)) { | ||||
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9545 | FunctionProtoType::ExtProtoInfo EPI; |
9546 | EPI.ExtInfo = Ext; | ||||
9547 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); | ||||
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9548 | |
9549 | // Otherwise, if we don't need to change anything about the function type, | ||||
9550 | // preserve its sugar structure. | ||||
9551 | } else if (FTy->getResultType() == RetTy && | ||||
9552 | (!NoReturn || FTy->getNoReturnAttr())) { | ||||
9553 | BlockTy = BSI->FunctionType; | ||||
9554 | |||||
9555 | // Otherwise, make the minimal modifications to the function type. | ||||
9556 | } else { | ||||
9557 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); | ||||
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9558 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
9559 | EPI.TypeQuals = 0; // FIXME: silently? | ||||
9560 | EPI.ExtInfo = Ext; | ||||
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9561 | BlockTy = Context.getFunctionType(RetTy, |
9562 | FPT->arg_type_begin(), | ||||
9563 | FPT->getNumArgs(), | ||||
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9564 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9565 | } |
9566 | |||||
9567 | // If we don't have a function type, just build one from nothing. | ||||
9568 | } else { | ||||
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9569 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9570 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9571 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9572 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9573 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9574 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
9575 | BSI->TheDecl->param_end()); | ||||
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9576 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9577 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 9578 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9579 | if (getCurFunction()->NeedsScopeChecking() && |
Douglas Gregor | 27bec77 | 2012-08-17 05:12:08 +0000 | [diff] [blame] | 9580 | !hasAnyUnrecoverableErrorsInThisFunction() && |
9581 | !PP.isCodeCompletionEnabled()) | ||||
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9582 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9583 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9584 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9585 | |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9586 | // Try to apply the named return value optimization. We have to check again |
9587 | // if we can do this, though, because blocks keep return statements around | ||||
9588 | // to deduce an implicit return type. | ||||
9589 | if (getLangOpts().CPlusPlus && RetTy->isRecordType() && | ||||
9590 | !BSI->TheDecl->isDependentContext()) | ||||
9591 | computeNRVO(Body, getCurBlock()); | ||||
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9592 | |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 9593 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
9594 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); | ||||
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9595 | PopFunctionScopeInfo(&WP, Result->getBlockDecl(), Result); |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 9596 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9597 | // 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] | 9598 | // 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] | 9599 | if (Result->getBlockDecl()->hasCaptures()) { |
John McCall | 97b57a2 | 2012-04-13 01:08:17 +0000 | [diff] [blame] | 9600 | // First, this expression has a new cleanup object. |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9601 | ExprCleanupObjects.push_back(Result->getBlockDecl()); |
9602 | ExprNeedsCleanups = true; | ||||
John McCall | 97b57a2 | 2012-04-13 01:08:17 +0000 | [diff] [blame] | 9603 | |
9604 | // It also gets a branch-protected scope if any of the captured | ||||
9605 | // variables needs destruction. | ||||
9606 | for (BlockDecl::capture_const_iterator | ||||
9607 | ci = Result->getBlockDecl()->capture_begin(), | ||||
9608 | ce = Result->getBlockDecl()->capture_end(); ci != ce; ++ci) { | ||||
9609 | const VarDecl *var = ci->getVariable(); | ||||
9610 | if (var->getType().isDestructedType() != QualType::DK_none) { | ||||
9611 | getCurFunction()->setHasBranchProtectedScope(); | ||||
9612 | break; | ||||
9613 | } | ||||
9614 | } | ||||
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9615 | } |
Fariborz Jahanian | 27949f6 | 2012-03-06 18:41:35 +0000 | [diff] [blame] | 9616 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9617 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9618 | } |
9619 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9620 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9621 | Expr *E, ParsedType Ty, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9622 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9623 | TypeSourceInfo *TInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9624 | GetTypeFromParser(Ty, &TInfo); |
9625 | return BuildVAArgExpr(BuiltinLoc, E, TInfo, RPLoc); | ||||
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9626 | } |
9627 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9628 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9629 | Expr *E, TypeSourceInfo *TInfo, |
9630 | SourceLocation RPLoc) { | ||||
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9631 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9632 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9633 | // Get the va_list type |
9634 | QualType VaListType = Context.getBuiltinVaListType(); | ||||
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9635 | if (VaListType->isArrayType()) { |
9636 | // Deal with implicit array decay; for example, on x86-64, | ||||
9637 | // va_list is an array, but it's supposed to decay to | ||||
9638 | // a pointer for va_arg. | ||||
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9639 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9640 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9641 | ExprResult Result = UsualUnaryConversions(E); |
9642 | if (Result.isInvalid()) | ||||
9643 | return ExprError(); | ||||
9644 | E = Result.take(); | ||||
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9645 | } else { |
9646 | // Otherwise, the va_list argument must be an l-value because | ||||
9647 | // it is modified by va_arg. | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9648 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9649 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9650 | return ExprError(); |
9651 | } | ||||
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9652 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9653 | if (!E->isTypeDependent() && |
9654 | !Context.hasSameType(VaListType, E->getType())) { | ||||
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9655 | return ExprError(Diag(E->getLocStart(), |
9656 | diag::err_first_argument_to_va_arg_not_of_type_va_list) | ||||
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9657 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 9658 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9659 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9660 | if (!TInfo->getType()->isDependentType()) { |
9661 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), | ||||
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 9662 | diag::err_second_parameter_to_va_arg_incomplete, |
9663 | TInfo->getTypeLoc())) | ||||
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9664 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 9665 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9666 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 9667 | TInfo->getType(), |
9668 | diag::err_second_parameter_to_va_arg_abstract, | ||||
9669 | TInfo->getTypeLoc())) | ||||
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9670 | return ExprError(); |
9671 | |||||
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9672 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9673 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9674 | TInfo->getType()->isObjCLifetimeType() |
9675 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified | ||||
9676 | : diag::warn_second_parameter_to_va_arg_not_pod) | ||||
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9677 | << TInfo->getType() |
9678 | << TInfo->getTypeLoc().getSourceRange(); | ||||
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9679 | } |
Eli Friedman | 46d37c1 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 9680 | |
9681 | // Check for va_arg where arguments of the given type will be promoted | ||||
9682 | // (i.e. this va_arg is guaranteed to have undefined behavior). | ||||
9683 | QualType PromoteType; | ||||
9684 | if (TInfo->getType()->isPromotableIntegerType()) { | ||||
9685 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); | ||||
9686 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) | ||||
9687 | PromoteType = QualType(); | ||||
9688 | } | ||||
9689 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) | ||||
9690 | PromoteType = Context.DoubleTy; | ||||
9691 | if (!PromoteType.isNull()) | ||||
9692 | Diag(TInfo->getTypeLoc().getBeginLoc(), | ||||
9693 | diag::warn_second_parameter_to_va_arg_never_compatible) | ||||
9694 | << TInfo->getType() | ||||
9695 | << PromoteType | ||||
9696 | << TInfo->getTypeLoc().getSourceRange(); | ||||
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9697 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9698 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9699 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
9700 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); | ||||
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9701 | } |
9702 | |||||
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9703 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9704 | // The type of __null will be int or long, depending on the size of |
9705 | // pointers on the target. | ||||
9706 | QualType Ty; | ||||
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9707 | unsigned pw = Context.getTargetInfo().getPointerWidth(0); |
9708 | if (pw == Context.getTargetInfo().getIntWidth()) | ||||
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9709 | Ty = Context.IntTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9710 | else if (pw == Context.getTargetInfo().getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9711 | Ty = Context.LongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9712 | else if (pw == Context.getTargetInfo().getLongLongWidth()) |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9713 | Ty = Context.LongLongTy; |
9714 | else { | ||||
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9715 | llvm_unreachable("I don't know size of pointer!"); |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9716 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9717 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9718 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9719 | } |
9720 | |||||
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9721 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9722 | Expr *SrcExpr, FixItHint &Hint) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9723 | if (!SemaRef.getLangOpts().ObjC1) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9724 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9725 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9726 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
9727 | if (!PT) | ||||
9728 | return; | ||||
9729 | |||||
9730 | // Check if the destination is of type 'id'. | ||||
9731 | if (!PT->isObjCIdType()) { | ||||
9732 | // Check if the destination is the 'NSString' interface. | ||||
9733 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); | ||||
9734 | if (!ID || !ID->getIdentifier()->isStr("NSString")) | ||||
9735 | return; | ||||
9736 | } | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9737 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 9738 | // Ignore any parens, implicit casts (should only be |
9739 | // array-to-pointer decays), and not-so-opaque values. The last is | ||||
9740 | // important for making this trigger for property assignments. | ||||
9741 | SrcExpr = SrcExpr->IgnoreParenImpCasts(); | ||||
9742 | if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(SrcExpr)) | ||||
9743 | if (OV->getSourceExpr()) | ||||
9744 | SrcExpr = OV->getSourceExpr()->IgnoreParenImpCasts(); | ||||
9745 | |||||
9746 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr); | ||||
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 9747 | if (!SL || !SL->isAscii()) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9748 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9749 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9750 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9751 | } |
9752 | |||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9753 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
9754 | SourceLocation Loc, | ||||
9755 | QualType DstType, QualType SrcType, | ||||
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9756 | Expr *SrcExpr, AssignmentAction Action, |
9757 | bool *Complained) { | ||||
9758 | if (Complained) | ||||
9759 | *Complained = false; | ||||
9760 | |||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9761 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9762 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9763 | bool isInvalid = false; |
Eli Friedman | fd81978 | 2012-02-29 20:59:56 +0000 | [diff] [blame] | 9764 | unsigned DiagKind = 0; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9765 | FixItHint Hint; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9766 | ConversionFixItGenerator ConvHints; |
9767 | bool MayHaveConvFixit = false; | ||||
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9768 | bool MayHaveFunctionDiff = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9769 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9770 | switch (ConvTy) { |
Fariborz Jahanian | 379b281 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 9771 | case Compatible: |
9772 | DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr); | ||||
9773 | return false; | ||||
9774 | |||||
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9775 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9776 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9777 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
9778 | MayHaveConvFixit = true; | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9779 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9780 | case IntToPointer: |
9781 | DiagKind = diag::ext_typecheck_convert_int_pointer; | ||||
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9782 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
9783 | MayHaveConvFixit = true; | ||||
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9784 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9785 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9786 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9787 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9788 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
9789 | SrcType->isObjCObjectPointerType(); | ||||
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9790 | if (Hint.isNull() && !CheckInferredResultType) { |
9791 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); | ||||
9792 | } | ||||
9793 | MayHaveConvFixit = true; | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9794 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9795 | case IncompatiblePointerSign: |
9796 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; | ||||
9797 | break; | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9798 | case FunctionVoidPointer: |
9799 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; | ||||
9800 | break; | ||||
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9801 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9802 | // Perform array-to-pointer decay if necessary. |
9803 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); | ||||
9804 | |||||
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9805 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
9806 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); | ||||
9807 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { | ||||
9808 | DiagKind = diag::err_typecheck_incompatible_address_space; | ||||
9809 | break; | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9810 | |
9811 | |||||
9812 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { | ||||
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 9813 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9814 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9815 | } |
9816 | |||||
9817 | llvm_unreachable("unknown error case for discarding qualifiers!"); | ||||
9818 | // fallthrough | ||||
9819 | } | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9820 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9821 | // If the qualifiers lost were because we were applying the |
9822 | // (deprecated) C++ conversion from a string literal to a char* | ||||
9823 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: | ||||
9824 | // Ideally, this check would be performed in | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9825 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9826 | // bit of refactoring (so that the second argument is an |
9827 | // expression, rather than a type), which should be done as part | ||||
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9828 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9829 | // C++ semantics. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9830 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9831 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
9832 | return false; | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9833 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
9834 | break; | ||||
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9835 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9836 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9837 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9838 | case IntToBlockPointer: |
9839 | DiagKind = diag::err_int_to_block_pointer; | ||||
9840 | break; | ||||
9841 | case IncompatibleBlockPointer: | ||||
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9842 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9843 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9844 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9845 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9846 | // it can give a more specific diagnostic. |
9847 | DiagKind = diag::warn_incompatible_qualified_id; | ||||
9848 | break; | ||||
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9849 | case IncompatibleVectors: |
9850 | DiagKind = diag::warn_incompatible_vectors; | ||||
9851 | break; | ||||
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 9852 | case IncompatibleObjCWeakRef: |
9853 | DiagKind = diag::err_arc_weak_unavailable_assign; | ||||
9854 | break; | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9855 | case Incompatible: |
9856 | DiagKind = diag::err_typecheck_convert_incompatible; | ||||
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9857 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
9858 | MayHaveConvFixit = true; | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9859 | isInvalid = true; |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9860 | MayHaveFunctionDiff = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9861 | break; |
9862 | } | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9863 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9864 | QualType FirstType, SecondType; |
9865 | switch (Action) { | ||||
9866 | case AA_Assigning: | ||||
9867 | case AA_Initializing: | ||||
9868 | // The destination type comes first. | ||||
9869 | FirstType = DstType; | ||||
9870 | SecondType = SrcType; | ||||
9871 | break; | ||||
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9872 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9873 | case AA_Returning: |
9874 | case AA_Passing: | ||||
9875 | case AA_Converting: | ||||
9876 | case AA_Sending: | ||||
9877 | case AA_Casting: | ||||
9878 | // The source type comes first. | ||||
9879 | FirstType = SrcType; | ||||
9880 | SecondType = DstType; | ||||
9881 | break; | ||||
9882 | } | ||||
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9883 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9884 | PartialDiagnostic FDiag = PDiag(DiagKind); |
9885 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); | ||||
9886 | |||||
9887 | // If we can fix the conversion, suggest the FixIts. | ||||
9888 | assert(ConvHints.isNull() || Hint.isNull()); | ||||
9889 | if (!ConvHints.isNull()) { | ||||
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 9890 | for (std::vector<FixItHint>::iterator HI = ConvHints.Hints.begin(), |
9891 | HE = ConvHints.Hints.end(); HI != HE; ++HI) | ||||
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9892 | FDiag << *HI; |
9893 | } else { | ||||
9894 | FDiag << Hint; | ||||
9895 | } | ||||
9896 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } | ||||
9897 | |||||
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9898 | if (MayHaveFunctionDiff) |
9899 | HandleFunctionTypeMismatch(FDiag, SecondType, FirstType); | ||||
9900 | |||||
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9901 | Diag(Loc, FDiag); |
9902 | |||||
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9903 | if (SecondType == Context.OverloadTy) |
9904 | NoteAllOverloadCandidates(OverloadExpr::find(SrcExpr).Expression, | ||||
9905 | FirstType); | ||||
9906 | |||||
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9907 | if (CheckInferredResultType) |
9908 | EmitRelatedResultTypeNote(SrcExpr); | ||||
9909 | |||||
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9910 | if (Complained) |
9911 | *Complained = true; | ||||
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9912 | return isInvalid; |
9913 | } | ||||
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9914 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9915 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, |
9916 | llvm::APSInt *Result) { | ||||
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9917 | class SimpleICEDiagnoser : public VerifyICEDiagnoser { |
9918 | public: | ||||
9919 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { | ||||
9920 | S.Diag(Loc, diag::err_expr_not_ice) << S.LangOpts.CPlusPlus << SR; | ||||
9921 | } | ||||
9922 | } Diagnoser; | ||||
9923 | |||||
9924 | return VerifyIntegerConstantExpression(E, Result, Diagnoser); | ||||
9925 | } | ||||
9926 | |||||
9927 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, | ||||
9928 | llvm::APSInt *Result, | ||||
9929 | unsigned DiagID, | ||||
9930 | bool AllowFold) { | ||||
9931 | class IDDiagnoser : public VerifyICEDiagnoser { | ||||
9932 | unsigned DiagID; | ||||
9933 | |||||
9934 | public: | ||||
9935 | IDDiagnoser(unsigned DiagID) | ||||
9936 | : VerifyICEDiagnoser(DiagID == 0), DiagID(DiagID) { } | ||||
9937 | |||||
9938 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { | ||||
9939 | S.Diag(Loc, DiagID) << SR; | ||||
9940 | } | ||||
9941 | } Diagnoser(DiagID); | ||||
9942 | |||||
9943 | return VerifyIntegerConstantExpression(E, Result, Diagnoser, AllowFold); | ||||
9944 | } | ||||
9945 | |||||
9946 | void Sema::VerifyICEDiagnoser::diagnoseFold(Sema &S, SourceLocation Loc, | ||||
9947 | SourceRange SR) { | ||||
9948 | S.Diag(Loc, diag::ext_expr_not_ice) << SR << S.LangOpts.CPlusPlus; | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9949 | } |
9950 | |||||
Benjamin Kramer | d448ce0 | 2012-04-18 14:22:41 +0000 | [diff] [blame] | 9951 | ExprResult |
9952 | Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, | ||||
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9953 | VerifyICEDiagnoser &Diagnoser, |
9954 | bool AllowFold) { | ||||
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9955 | SourceLocation DiagLoc = E->getLocStart(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9956 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9957 | if (getLangOpts().CPlusPlus0x) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9958 | // C++11 [expr.const]p5: |
9959 | // If an expression of literal class type is used in a context where an | ||||
9960 | // integral constant expression is required, then that class type shall | ||||
9961 | // have a single non-explicit conversion function to an integral or | ||||
9962 | // unscoped enumeration type | ||||
9963 | ExprResult Converted; | ||||
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9964 | if (!Diagnoser.Suppress) { |
9965 | class CXX11ConvertDiagnoser : public ICEConvertDiagnoser { | ||||
9966 | public: | ||||
9967 | CXX11ConvertDiagnoser() : ICEConvertDiagnoser(false, true) { } | ||||
9968 | |||||
9969 | virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, | ||||
9970 | QualType T) { | ||||
9971 | return S.Diag(Loc, diag::err_ice_not_integral) << T; | ||||
9972 | } | ||||
9973 | |||||
9974 | virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, | ||||
9975 | SourceLocation Loc, | ||||
9976 | QualType T) { | ||||
9977 | return S.Diag(Loc, diag::err_ice_incomplete_type) << T; | ||||
9978 | } | ||||
9979 | |||||
9980 | virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, | ||||
9981 | SourceLocation Loc, | ||||
9982 | QualType T, | ||||
9983 | QualType ConvTy) { | ||||
9984 | return S.Diag(Loc, diag::err_ice_explicit_conversion) << T << ConvTy; | ||||
9985 | } | ||||
9986 | |||||
9987 | virtual DiagnosticBuilder noteExplicitConv(Sema &S, | ||||
9988 | CXXConversionDecl *Conv, | ||||
9989 | QualType ConvTy) { | ||||
9990 | return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) | ||||
9991 | << ConvTy->isEnumeralType() << ConvTy; | ||||
9992 | } | ||||
9993 | |||||
9994 | virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, | ||||
9995 | QualType T) { | ||||
9996 | return S.Diag(Loc, diag::err_ice_ambiguous_conversion) << T; | ||||
9997 | } | ||||
9998 | |||||
9999 | virtual DiagnosticBuilder noteAmbiguous(Sema &S, | ||||
10000 | CXXConversionDecl *Conv, | ||||
10001 | QualType ConvTy) { | ||||
10002 | return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) | ||||
10003 | << ConvTy->isEnumeralType() << ConvTy; | ||||
10004 | } | ||||
10005 | |||||
10006 | virtual DiagnosticBuilder diagnoseConversion(Sema &S, | ||||
10007 | SourceLocation Loc, | ||||
10008 | QualType T, | ||||
10009 | QualType ConvTy) { | ||||
10010 | return DiagnosticBuilder::getEmpty(); | ||||
10011 | } | ||||
10012 | } ConvertDiagnoser; | ||||
10013 | |||||
10014 | Converted = ConvertToIntegralOrEnumerationType(DiagLoc, E, | ||||
10015 | ConvertDiagnoser, | ||||
10016 | /*AllowScopedEnumerations*/ false); | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10017 | } else { |
10018 | // The caller wants to silently enquire whether this is an ICE. Don't | ||||
10019 | // produce any diagnostics if it isn't. | ||||
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10020 | class SilentICEConvertDiagnoser : public ICEConvertDiagnoser { |
10021 | public: | ||||
10022 | SilentICEConvertDiagnoser() : ICEConvertDiagnoser(true, true) { } | ||||
10023 | |||||
10024 | virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, | ||||
10025 | QualType T) { | ||||
10026 | return DiagnosticBuilder::getEmpty(); | ||||
10027 | } | ||||
10028 | |||||
10029 | virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, | ||||
10030 | SourceLocation Loc, | ||||
10031 | QualType T) { | ||||
10032 | return DiagnosticBuilder::getEmpty(); | ||||
10033 | } | ||||
10034 | |||||
10035 | virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, | ||||
10036 | SourceLocation Loc, | ||||
10037 | QualType T, | ||||
10038 | QualType ConvTy) { | ||||
10039 | return DiagnosticBuilder::getEmpty(); | ||||
10040 | } | ||||
10041 | |||||
10042 | virtual DiagnosticBuilder noteExplicitConv(Sema &S, | ||||
10043 | CXXConversionDecl *Conv, | ||||
10044 | QualType ConvTy) { | ||||
10045 | return DiagnosticBuilder::getEmpty(); | ||||
10046 | } | ||||
10047 | |||||
10048 | virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, | ||||
10049 | QualType T) { | ||||
10050 | return DiagnosticBuilder::getEmpty(); | ||||
10051 | } | ||||
10052 | |||||
10053 | virtual DiagnosticBuilder noteAmbiguous(Sema &S, | ||||
10054 | CXXConversionDecl *Conv, | ||||
10055 | QualType ConvTy) { | ||||
10056 | return DiagnosticBuilder::getEmpty(); | ||||
10057 | } | ||||
10058 | |||||
10059 | virtual DiagnosticBuilder diagnoseConversion(Sema &S, | ||||
10060 | SourceLocation Loc, | ||||
10061 | QualType T, | ||||
10062 | QualType ConvTy) { | ||||
10063 | return DiagnosticBuilder::getEmpty(); | ||||
10064 | } | ||||
10065 | } ConvertDiagnoser; | ||||
10066 | |||||
10067 | Converted = ConvertToIntegralOrEnumerationType(DiagLoc, E, | ||||
10068 | ConvertDiagnoser, false); | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10069 | } |
10070 | if (Converted.isInvalid()) | ||||
10071 | return Converted; | ||||
10072 | E = Converted.take(); | ||||
10073 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) | ||||
10074 | return ExprError(); | ||||
10075 | } else if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { | ||||
10076 | // An ICE must be of integral or unscoped enumeration type. | ||||
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10077 | if (!Diagnoser.Suppress) |
10078 | Diagnoser.diagnoseNotICE(*this, DiagLoc, E->getSourceRange()); | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10079 | return ExprError(); |
10080 | } | ||||
10081 | |||||
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10082 | // Circumvent ICE checking in C++11 to avoid evaluating the expression twice |
10083 | // in the non-ICE case. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10084 | if (!getLangOpts().CPlusPlus0x && E->isIntegerConstantExpr(Context)) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10085 | if (Result) |
10086 | *Result = E->EvaluateKnownConstInt(Context); | ||||
10087 | return Owned(E); | ||||
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10088 | } |
10089 | |||||
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10090 | Expr::EvalResult EvalResult; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 10091 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
10092 | EvalResult.Diag = &Notes; | ||||
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10093 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10094 | // Try to evaluate the expression, and produce diagnostics explaining why it's |
10095 | // not a constant expression as a side-effect. | ||||
10096 | bool Folded = E->EvaluateAsRValue(EvalResult, Context) && | ||||
10097 | EvalResult.Val.isInt() && !EvalResult.HasSideEffects; | ||||
10098 | |||||
10099 | // In C++11, we can rely on diagnostics being produced for any expression | ||||
10100 | // which is not a constant expression. If no diagnostics were produced, then | ||||
10101 | // this is a constant expression. | ||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10102 | if (Folded && getLangOpts().CPlusPlus0x && Notes.empty()) { |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10103 | if (Result) |
10104 | *Result = EvalResult.Val.getInt(); | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10105 | return Owned(E); |
10106 | } | ||||
10107 | |||||
10108 | // If our only note is the usual "invalid subexpression" note, just point | ||||
10109 | // the caret at its location rather than producing an essentially | ||||
10110 | // redundant note. | ||||
10111 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == | ||||
10112 | diag::note_invalid_subexpr_in_const_expr) { | ||||
10113 | DiagLoc = Notes[0].first; | ||||
10114 | Notes.clear(); | ||||
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10115 | } |
10116 | |||||
10117 | if (!Folded || !AllowFold) { | ||||
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10118 | if (!Diagnoser.Suppress) { |
10119 | Diagnoser.diagnoseNotICE(*this, DiagLoc, E->getSourceRange()); | ||||
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 10120 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
10121 | Diag(Notes[I].first, Notes[I].second); | ||||
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10122 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10123 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10124 | return ExprError(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10125 | } |
10126 | |||||
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10127 | Diagnoser.diagnoseFold(*this, DiagLoc, E->getSourceRange()); |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 10128 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
10129 | Diag(Notes[I].first, Notes[I].second); | ||||
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10130 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10131 | if (Result) |
10132 | *Result = EvalResult.Val.getInt(); | ||||
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10133 | return Owned(E); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10134 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10135 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10136 | namespace { |
10137 | // Handle the case where we conclude a expression which we speculatively | ||||
10138 | // considered to be unevaluated is actually evaluated. | ||||
10139 | class TransformToPE : public TreeTransform<TransformToPE> { | ||||
10140 | typedef TreeTransform<TransformToPE> BaseTransform; | ||||
10141 | |||||
10142 | public: | ||||
10143 | TransformToPE(Sema &SemaRef) : BaseTransform(SemaRef) { } | ||||
10144 | |||||
10145 | // Make sure we redo semantic analysis | ||||
10146 | bool AlwaysRebuild() { return true; } | ||||
10147 | |||||
Eli Friedman | 56ff283 | 2012-02-06 23:29:57 +0000 | [diff] [blame] | 10148 | // Make sure we handle LabelStmts correctly. |
10149 | // FIXME: This does the right thing, but maybe we need a more general | ||||
10150 | // fix to TreeTransform? | ||||
10151 | StmtResult TransformLabelStmt(LabelStmt *S) { | ||||
10152 | S->getDecl()->setStmt(0); | ||||
10153 | return BaseTransform::TransformLabelStmt(S); | ||||
10154 | } | ||||
10155 | |||||
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10156 | // We need to special-case DeclRefExprs referring to FieldDecls which |
10157 | // are not part of a member pointer formation; normal TreeTransforming | ||||
10158 | // doesn't catch this case because of the way we represent them in the AST. | ||||
10159 | // FIXME: This is a bit ugly; is it really the best way to handle this | ||||
10160 | // case? | ||||
10161 | // | ||||
10162 | // Error on DeclRefExprs referring to FieldDecls. | ||||
10163 | ExprResult TransformDeclRefExpr(DeclRefExpr *E) { | ||||
10164 | if (isa<FieldDecl>(E->getDecl()) && | ||||
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 10165 | !SemaRef.isUnevaluatedContext()) |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10166 | return SemaRef.Diag(E->getLocation(), |
10167 | diag::err_invalid_non_static_member_use) | ||||
10168 | << E->getDecl() << E->getSourceRange(); | ||||
10169 | |||||
10170 | return BaseTransform::TransformDeclRefExpr(E); | ||||
10171 | } | ||||
10172 | |||||
10173 | // Exception: filter out member pointer formation | ||||
10174 | ExprResult TransformUnaryOperator(UnaryOperator *E) { | ||||
10175 | if (E->getOpcode() == UO_AddrOf && E->getType()->isMemberPointerType()) | ||||
10176 | return E; | ||||
10177 | |||||
10178 | return BaseTransform::TransformUnaryOperator(E); | ||||
10179 | } | ||||
10180 | |||||
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10181 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
10182 | // Lambdas never need to be transformed. | ||||
10183 | return E; | ||||
10184 | } | ||||
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10185 | }; |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 10186 | } |
10187 | |||||
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10188 | ExprResult Sema::TranformToPotentiallyEvaluated(Expr *E) { |
Eli Friedman | 72b8b1e | 2012-02-29 04:03:55 +0000 | [diff] [blame] | 10189 | assert(ExprEvalContexts.back().Context == Unevaluated && |
10190 | "Should only transform unevaluated expressions"); | ||||
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10191 | ExprEvalContexts.back().Context = |
10192 | ExprEvalContexts[ExprEvalContexts.size()-2].Context; | ||||
10193 | if (ExprEvalContexts.back().Context == Unevaluated) | ||||
10194 | return E; | ||||
10195 | return TransformToPE(*this).TransformExpr(E); | ||||
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 10196 | } |
10197 | |||||
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10198 | void |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 10199 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext, |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 10200 | Decl *LambdaContextDecl, |
10201 | bool IsDecltype) { | ||||
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10202 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10203 | ExpressionEvaluationContextRecord(NewContext, |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10204 | ExprCleanupObjects.size(), |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 10205 | ExprNeedsCleanups, |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 10206 | LambdaContextDecl, |
10207 | IsDecltype)); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10208 | ExprNeedsCleanups = false; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10209 | if (!MaybeODRUseExprs.empty()) |
10210 | std::swap(MaybeODRUseExprs, ExprEvalContexts.back().SavedMaybeODRUseExprs); | ||||
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10211 | } |
10212 | |||||
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 10213 | void |
10214 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext, | ||||
10215 | ReuseLambdaContextDecl_t, | ||||
10216 | bool IsDecltype) { | ||||
10217 | Decl *LambdaContextDecl = ExprEvalContexts.back().LambdaContextDecl; | ||||
10218 | PushExpressionEvaluationContext(NewContext, LambdaContextDecl, IsDecltype); | ||||
10219 | } | ||||
10220 | |||||
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 10221 | void Sema::PopExpressionEvaluationContext() { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10222 | ExpressionEvaluationContextRecord& Rec = ExprEvalContexts.back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10223 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10224 | if (!Rec.Lambdas.empty()) { |
10225 | if (Rec.Context == Unevaluated) { | ||||
10226 | // C++11 [expr.prim.lambda]p2: | ||||
10227 | // A lambda-expression shall not appear in an unevaluated operand | ||||
10228 | // (Clause 5). | ||||
10229 | for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) | ||||
10230 | Diag(Rec.Lambdas[I]->getLocStart(), | ||||
10231 | diag::err_lambda_unevaluated_operand); | ||||
10232 | } else { | ||||
10233 | // Mark the capture expressions odr-used. This was deferred | ||||
10234 | // during lambda expression creation. | ||||
10235 | for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) { | ||||
10236 | LambdaExpr *Lambda = Rec.Lambdas[I]; | ||||
10237 | for (LambdaExpr::capture_init_iterator | ||||
10238 | C = Lambda->capture_init_begin(), | ||||
10239 | CEnd = Lambda->capture_init_end(); | ||||
10240 | C != CEnd; ++C) { | ||||
10241 | MarkDeclarationsReferencedInExpr(*C); | ||||
10242 | } | ||||
10243 | } | ||||
10244 | } | ||||
10245 | } | ||||
10246 | |||||
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10247 | // When are coming out of an unevaluated context, clear out any |
10248 | // temporaries that we may have created as part of the evaluation of | ||||
10249 | // the expression in that context: they aren't relevant because they | ||||
10250 | // will never be constructed. | ||||
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 10251 | if (Rec.Context == Unevaluated || Rec.Context == ConstantEvaluated) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10252 | ExprCleanupObjects.erase(ExprCleanupObjects.begin() + Rec.NumCleanupObjects, |
10253 | ExprCleanupObjects.end()); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10254 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10255 | CleanupVarDeclMarking(); |
10256 | std::swap(MaybeODRUseExprs, Rec.SavedMaybeODRUseExprs); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10257 | // Otherwise, merge the contexts together. |
10258 | } else { | ||||
10259 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; | ||||
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10260 | MaybeODRUseExprs.insert(Rec.SavedMaybeODRUseExprs.begin(), |
10261 | Rec.SavedMaybeODRUseExprs.end()); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10262 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10263 | |
10264 | // Pop the current expression evaluation context off the stack. | ||||
10265 | ExprEvalContexts.pop_back(); | ||||
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10266 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10267 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10268 | void Sema::DiscardCleanupsInEvaluationContext() { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10269 | ExprCleanupObjects.erase( |
10270 | ExprCleanupObjects.begin() + ExprEvalContexts.back().NumCleanupObjects, | ||||
10271 | ExprCleanupObjects.end()); | ||||
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10272 | ExprNeedsCleanups = false; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10273 | MaybeODRUseExprs.clear(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10274 | } |
10275 | |||||
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 10276 | ExprResult Sema::HandleExprEvaluationContextForTypeof(Expr *E) { |
10277 | if (!E->getType()->isVariablyModifiedType()) | ||||
10278 | return E; | ||||
10279 | return TranformToPotentiallyEvaluated(E); | ||||
10280 | } | ||||
10281 | |||||
Benjamin Kramer | 5bbc385 | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 10282 | static bool IsPotentiallyEvaluatedContext(Sema &SemaRef) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10283 | // Do not mark anything as "used" within a dependent context; wait for |
10284 | // an instantiation. | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10285 | if (SemaRef.CurContext->isDependentContext()) |
10286 | return false; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10287 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10288 | switch (SemaRef.ExprEvalContexts.back().Context) { |
10289 | case Sema::Unevaluated: | ||||
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10290 | // We are in an expression that is not potentially evaluated; do nothing. |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 10291 | // (Depending on how you read the standard, we actually do need to do |
10292 | // something here for null pointer constants, but the standard's | ||||
10293 | // definition of a null pointer constant is completely crazy.) | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10294 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10295 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10296 | case Sema::ConstantEvaluated: |
10297 | case Sema::PotentiallyEvaluated: | ||||
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 10298 | // We are in a potentially evaluated expression (or a constant-expression |
10299 | // in C++03); we need to do implicit template instantiation, implicitly | ||||
10300 | // define class members, and mark most declarations as used. | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10301 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10302 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10303 | case Sema::PotentiallyEvaluatedIfUsed: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10304 | // Referenced declarations will only be used if the construct in the |
10305 | // containing expression is used. | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10306 | return false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10307 | } |
Matt Beaumont-Gay | 4f7dcdb | 2012-02-02 18:35:35 +0000 | [diff] [blame] | 10308 | llvm_unreachable("Invalid context"); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10309 | } |
10310 | |||||
10311 | /// \brief Mark a function referenced, and check whether it is odr-used | ||||
10312 | /// (C++ [basic.def.odr]p2, C99 6.9p3) | ||||
10313 | void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func) { | ||||
10314 | assert(Func && "No function?"); | ||||
10315 | |||||
10316 | Func->setReferenced(); | ||||
10317 | |||||
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10318 | // Don't mark this function as used multiple times, unless it's a constexpr |
10319 | // function which we need to instantiate. | ||||
10320 | if (Func->isUsed(false) && | ||||
10321 | !(Func->isConstexpr() && !Func->getBody() && | ||||
10322 | Func->isImplicitlyInstantiable())) | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10323 | return; |
10324 | |||||
10325 | if (!IsPotentiallyEvaluatedContext(*this)) | ||||
10326 | return; | ||||
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10327 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10328 | // Note that this declaration has been used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10329 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10330 | if (Constructor->isDefaulted() && !Constructor->isDeleted()) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10331 | if (Constructor->isDefaultConstructor()) { |
10332 | if (Constructor->isTrivial()) | ||||
10333 | return; | ||||
10334 | if (!Constructor->isUsed(false)) | ||||
10335 | DefineImplicitDefaultConstructor(Loc, Constructor); | ||||
10336 | } else if (Constructor->isCopyConstructor()) { | ||||
10337 | if (!Constructor->isUsed(false)) | ||||
10338 | DefineImplicitCopyConstructor(Loc, Constructor); | ||||
10339 | } else if (Constructor->isMoveConstructor()) { | ||||
10340 | if (!Constructor->isUsed(false)) | ||||
10341 | DefineImplicitMoveConstructor(Loc, Constructor); | ||||
10342 | } | ||||
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10343 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10344 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10345 | MarkVTableUsed(Loc, Constructor->getParent()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10346 | } else if (CXXDestructorDecl *Destructor = |
10347 | dyn_cast<CXXDestructorDecl>(Func)) { | ||||
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10348 | if (Destructor->isDefaulted() && !Destructor->isDeleted() && |
10349 | !Destructor->isUsed(false)) | ||||
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10350 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10351 | if (Destructor->isVirtual()) |
10352 | MarkVTableUsed(Loc, Destructor->getParent()); | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10353 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10354 | if (MethodDecl->isDefaulted() && !MethodDecl->isDeleted() && |
10355 | MethodDecl->isOverloadedOperator() && | ||||
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10356 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10357 | if (!MethodDecl->isUsed(false)) { |
10358 | if (MethodDecl->isCopyAssignmentOperator()) | ||||
10359 | DefineImplicitCopyAssignment(Loc, MethodDecl); | ||||
10360 | else | ||||
10361 | DefineImplicitMoveAssignment(Loc, MethodDecl); | ||||
10362 | } | ||||
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10363 | } else if (isa<CXXConversionDecl>(MethodDecl) && |
10364 | MethodDecl->getParent()->isLambda()) { | ||||
10365 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(MethodDecl); | ||||
10366 | if (Conversion->isLambdaToBlockPointerConversion()) | ||||
10367 | DefineImplicitLambdaToBlockPointerConversion(Loc, Conversion); | ||||
10368 | else | ||||
10369 | DefineImplicitLambdaToFunctionPointerConversion(Loc, Conversion); | ||||
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10370 | } else if (MethodDecl->isVirtual()) |
10371 | MarkVTableUsed(Loc, MethodDecl->getParent()); | ||||
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10372 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10373 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10374 | // Recursive functions should be marked when used from another function. |
10375 | // FIXME: Is this really right? | ||||
10376 | if (CurContext == Func) return; | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10377 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10378 | // Resolve the exception specification for any function which is |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10379 | // used: CodeGen will need it. |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 10380 | const FunctionProtoType *FPT = Func->getType()->getAs<FunctionProtoType>(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10381 | if (FPT && isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) |
10382 | ResolveExceptionSpec(Loc, FPT); | ||||
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10383 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10384 | // Implicit instantiation of function templates and member functions of |
10385 | // class templates. | ||||
10386 | if (Func->isImplicitlyInstantiable()) { | ||||
10387 | bool AlreadyInstantiated = false; | ||||
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10388 | SourceLocation PointOfInstantiation = Loc; |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10389 | if (FunctionTemplateSpecializationInfo *SpecInfo |
10390 | = Func->getTemplateSpecializationInfo()) { | ||||
10391 | if (SpecInfo->getPointOfInstantiation().isInvalid()) | ||||
10392 | SpecInfo->setPointOfInstantiation(Loc); | ||||
10393 | else if (SpecInfo->getTemplateSpecializationKind() | ||||
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10394 | == TSK_ImplicitInstantiation) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10395 | AlreadyInstantiated = true; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10396 | PointOfInstantiation = SpecInfo->getPointOfInstantiation(); |
10397 | } | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10398 | } else if (MemberSpecializationInfo *MSInfo |
10399 | = Func->getMemberSpecializationInfo()) { | ||||
10400 | if (MSInfo->getPointOfInstantiation().isInvalid()) | ||||
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10401 | MSInfo->setPointOfInstantiation(Loc); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10402 | else if (MSInfo->getTemplateSpecializationKind() |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10403 | == TSK_ImplicitInstantiation) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10404 | AlreadyInstantiated = true; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10405 | PointOfInstantiation = MSInfo->getPointOfInstantiation(); |
10406 | } | ||||
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10408 | |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10409 | if (!AlreadyInstantiated || Func->isConstexpr()) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10410 | if (isa<CXXRecordDecl>(Func->getDeclContext()) && |
10411 | cast<CXXRecordDecl>(Func->getDeclContext())->isLocalClass()) | ||||
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10412 | PendingLocalImplicitInstantiations.push_back( |
10413 | std::make_pair(Func, PointOfInstantiation)); | ||||
10414 | else if (Func->isConstexpr()) | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10415 | // Do not defer instantiations of constexpr functions, to avoid the |
10416 | // expression evaluator needing to call back into Sema if it sees a | ||||
10417 | // call to such a function. | ||||
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10418 | InstantiateFunctionDefinition(PointOfInstantiation, Func); |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 10419 | else { |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10420 | PendingInstantiations.push_back(std::make_pair(Func, |
10421 | PointOfInstantiation)); | ||||
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 10422 | // Notify the consumer that a function was implicitly instantiated. |
10423 | Consumer.HandleCXXImplicitFunctionInstantiation(Func); | ||||
10424 | } | ||||
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10425 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10426 | } else { |
10427 | // Walk redefinitions, as some of them may be instantiable. | ||||
10428 | for (FunctionDecl::redecl_iterator i(Func->redecls_begin()), | ||||
10429 | e(Func->redecls_end()); i != e; ++i) { | ||||
10430 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) | ||||
10431 | MarkFunctionReferenced(Loc, *i); | ||||
10432 | } | ||||
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 10433 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10434 | |
10435 | // Keep track of used but undefined functions. | ||||
10436 | if (!Func->isPure() && !Func->hasBody() && | ||||
10437 | Func->getLinkage() != ExternalLinkage) { | ||||
10438 | SourceLocation &old = UndefinedInternals[Func->getCanonicalDecl()]; | ||||
10439 | if (old.isInvalid()) old = Loc; | ||||
10440 | } | ||||
10441 | |||||
10442 | Func->setUsed(true); | ||||
10443 | } | ||||
10444 | |||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10445 | static void |
10446 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, | ||||
10447 | VarDecl *var, DeclContext *DC) { | ||||
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10448 | DeclContext *VarDC = var->getDeclContext(); |
10449 | |||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10450 | // If the parameter still belongs to the translation unit, then |
10451 | // we're actually just using one parameter in the declaration of | ||||
10452 | // the next. | ||||
10453 | if (isa<ParmVarDecl>(var) && | ||||
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10454 | isa<TranslationUnitDecl>(VarDC)) |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10455 | return; |
10456 | |||||
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10457 | // For C code, don't diagnose about capture if we're not actually in code |
10458 | // right now; it's impossible to write a non-constant expression outside of | ||||
10459 | // function context, so we'll get other (more useful) diagnostics later. | ||||
10460 | // | ||||
10461 | // For C++, things get a bit more nasty... it would be nice to suppress this | ||||
10462 | // diagnostic for certain cases like using a local variable in an array bound | ||||
10463 | // 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] | 10464 | if (!S.getLangOpts().CPlusPlus && !S.CurContext->isFunctionOrMethod()) |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10465 | return; |
10466 | |||||
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10467 | if (isa<CXXMethodDecl>(VarDC) && |
10468 | cast<CXXRecordDecl>(VarDC->getParent())->isLambda()) { | ||||
10469 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_lambda) | ||||
10470 | << var->getIdentifier(); | ||||
10471 | } else if (FunctionDecl *fn = dyn_cast<FunctionDecl>(VarDC)) { | ||||
10472 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) | ||||
10473 | << var->getIdentifier() << fn->getDeclName(); | ||||
10474 | } else if (isa<BlockDecl>(VarDC)) { | ||||
10475 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_block) | ||||
10476 | << var->getIdentifier(); | ||||
10477 | } else { | ||||
10478 | // FIXME: Is there any other context where a local variable can be | ||||
10479 | // declared? | ||||
10480 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_context) | ||||
10481 | << var->getIdentifier(); | ||||
10482 | } | ||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10483 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10484 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
10485 | << var->getIdentifier(); | ||||
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10486 | |
10487 | // FIXME: Add additional diagnostic info about class etc. which prevents | ||||
10488 | // capture. | ||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10489 | } |
10490 | |||||
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10491 | /// \brief Capture the given variable in the given lambda expression. |
10492 | static ExprResult captureInLambda(Sema &S, LambdaScopeInfo *LSI, | ||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10493 | VarDecl *Var, QualType FieldType, |
10494 | QualType DeclRefType, | ||||
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10495 | SourceLocation Loc, |
10496 | bool RefersToEnclosingLocal) { | ||||
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10497 | CXXRecordDecl *Lambda = LSI->Lambda; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10498 | |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10499 | // Build the non-static data member. |
10500 | FieldDecl *Field | ||||
10501 | = FieldDecl::Create(S.Context, Lambda, Loc, Loc, 0, FieldType, | ||||
10502 | S.Context.getTrivialTypeSourceInfo(FieldType, Loc), | ||||
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 10503 | 0, false, ICIS_NoInit); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10504 | Field->setImplicit(true); |
10505 | Field->setAccess(AS_private); | ||||
Douglas Gregor | 20f87a4 | 2012-02-09 02:12:34 +0000 | [diff] [blame] | 10506 | Lambda->addDecl(Field); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10507 | |
10508 | // C++11 [expr.prim.lambda]p21: | ||||
10509 | // When the lambda-expression is evaluated, the entities that | ||||
10510 | // are captured by copy are used to direct-initialize each | ||||
10511 | // corresponding non-static data member of the resulting closure | ||||
10512 | // object. (For array members, the array elements are | ||||
10513 | // direct-initialized in increasing subscript order.) These | ||||
10514 | // initializations are performed in the (unspecified) order in | ||||
10515 | // which the non-static data members are declared. | ||||
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10516 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10517 | // Introduce a new evaluation context for the initialization, so |
10518 | // that temporaries introduced as part of the capture are retained | ||||
10519 | // to be re-"exported" from the lambda expression itself. | ||||
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10520 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
10521 | |||||
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 10522 | // C++ [expr.prim.labda]p12: |
10523 | // An entity captured by a lambda-expression is odr-used (3.2) in | ||||
10524 | // the scope containing the lambda-expression. | ||||
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10525 | Expr *Ref = new (S.Context) DeclRefExpr(Var, RefersToEnclosingLocal, |
10526 | DeclRefType, VK_LValue, Loc); | ||||
Eli Friedman | 88530d5 | 2012-03-01 21:32:56 +0000 | [diff] [blame] | 10527 | Var->setReferenced(true); |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 10528 | Var->setUsed(true); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10529 | |
10530 | // When the field has array type, create index variables for each | ||||
10531 | // dimension of the array. We use these index variables to subscript | ||||
10532 | // the source array, and other clients (e.g., CodeGen) will perform | ||||
10533 | // the necessary iteration with these index variables. | ||||
10534 | SmallVector<VarDecl *, 4> IndexVariables; | ||||
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10535 | QualType BaseType = FieldType; |
10536 | QualType SizeType = S.Context.getSizeType(); | ||||
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 10537 | LSI->ArrayIndexStarts.push_back(LSI->ArrayIndexVars.size()); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10538 | while (const ConstantArrayType *Array |
10539 | = S.Context.getAsConstantArrayType(BaseType)) { | ||||
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10540 | // Create the iteration variable for this array index. |
10541 | IdentifierInfo *IterationVarName = 0; | ||||
10542 | { | ||||
10543 | SmallString<8> Str; | ||||
10544 | llvm::raw_svector_ostream OS(Str); | ||||
10545 | OS << "__i" << IndexVariables.size(); | ||||
10546 | IterationVarName = &S.Context.Idents.get(OS.str()); | ||||
10547 | } | ||||
10548 | VarDecl *IterationVar | ||||
10549 | = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, | ||||
10550 | IterationVarName, SizeType, | ||||
10551 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), | ||||
10552 | SC_None, SC_None); | ||||
10553 | IndexVariables.push_back(IterationVar); | ||||
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 10554 | LSI->ArrayIndexVars.push_back(IterationVar); |
10555 | |||||
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10556 | // Create a reference to the iteration variable. |
10557 | ExprResult IterationVarRef | ||||
10558 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); | ||||
10559 | assert(!IterationVarRef.isInvalid() && | ||||
10560 | "Reference to invented variable cannot fail!"); | ||||
10561 | IterationVarRef = S.DefaultLvalueConversion(IterationVarRef.take()); | ||||
10562 | assert(!IterationVarRef.isInvalid() && | ||||
10563 | "Conversion of invented variable cannot fail!"); | ||||
10564 | |||||
10565 | // Subscript the array with this iteration variable. | ||||
10566 | ExprResult Subscript = S.CreateBuiltinArraySubscriptExpr( | ||||
10567 | Ref, Loc, IterationVarRef.take(), Loc); | ||||
10568 | if (Subscript.isInvalid()) { | ||||
10569 | S.CleanupVarDeclMarking(); | ||||
10570 | S.DiscardCleanupsInEvaluationContext(); | ||||
10571 | S.PopExpressionEvaluationContext(); | ||||
10572 | return ExprError(); | ||||
10573 | } | ||||
10574 | |||||
10575 | Ref = Subscript.take(); | ||||
10576 | BaseType = Array->getElementType(); | ||||
10577 | } | ||||
10578 | |||||
10579 | // Construct the entity that we will be initializing. For an array, this | ||||
10580 | // will be first element in the array, which may require several levels | ||||
10581 | // of array-subscript entities. | ||||
10582 | SmallVector<InitializedEntity, 4> Entities; | ||||
10583 | Entities.reserve(1 + IndexVariables.size()); | ||||
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 10584 | Entities.push_back( |
10585 | InitializedEntity::InitializeLambdaCapture(Var, Field, Loc)); | ||||
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10586 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
10587 | Entities.push_back(InitializedEntity::InitializeElement(S.Context, | ||||
10588 | 0, | ||||
10589 | Entities.back())); | ||||
10590 | |||||
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10591 | InitializationKind InitKind |
10592 | = InitializationKind::CreateDirect(Loc, Loc, Loc); | ||||
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10593 | InitializationSequence Init(S, Entities.back(), InitKind, &Ref, 1); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10594 | ExprResult Result(true); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10595 | if (!Init.Diagnose(S, Entities.back(), InitKind, &Ref, 1)) |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10596 | Result = Init.Perform(S, Entities.back(), InitKind, Ref); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10597 | |
10598 | // If this initialization requires any cleanups (e.g., due to a | ||||
10599 | // default argument to a copy constructor), note that for the | ||||
10600 | // lambda. | ||||
10601 | if (S.ExprNeedsCleanups) | ||||
10602 | LSI->ExprNeedsCleanups = true; | ||||
10603 | |||||
10604 | // Exit the expression evaluation context used for the capture. | ||||
10605 | S.CleanupVarDeclMarking(); | ||||
10606 | S.DiscardCleanupsInEvaluationContext(); | ||||
10607 | S.PopExpressionEvaluationContext(); | ||||
10608 | return Result; | ||||
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10609 | } |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10610 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10611 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, |
10612 | TryCaptureKind Kind, SourceLocation EllipsisLoc, | ||||
10613 | bool BuildAndDiagnose, | ||||
10614 | QualType &CaptureType, | ||||
10615 | QualType &DeclRefType) { | ||||
10616 | bool Nested = false; | ||||
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10617 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10618 | DeclContext *DC = CurContext; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10619 | if (Var->getDeclContext() == DC) return true; |
10620 | if (!Var->hasLocalStorage()) return true; | ||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10621 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10622 | bool HasBlocksAttr = Var->hasAttr<BlocksAttr>(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10623 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10624 | // Walk up the stack to determine whether we can capture the variable, |
10625 | // performing the "simple" checks that don't depend on type. We stop when | ||||
10626 | // we've either hit the declared scope of the variable or find an existing | ||||
10627 | // capture of that variable. | ||||
10628 | CaptureType = Var->getType(); | ||||
10629 | DeclRefType = CaptureType.getNonReferenceType(); | ||||
10630 | bool Explicit = (Kind != TryCapture_Implicit); | ||||
10631 | unsigned FunctionScopesIndex = FunctionScopes.size() - 1; | ||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10632 | do { |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10633 | // Only block literals and lambda expressions can capture; other |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10634 | // scopes don't work. |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10635 | DeclContext *ParentDC; |
10636 | if (isa<BlockDecl>(DC)) | ||||
10637 | ParentDC = DC->getParent(); | ||||
10638 | else if (isa<CXXMethodDecl>(DC) && | ||||
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10639 | cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call && |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10640 | cast<CXXRecordDecl>(DC->getParent())->isLambda()) |
10641 | ParentDC = DC->getParent()->getParent(); | ||||
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10642 | else { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10643 | if (BuildAndDiagnose) |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10644 | diagnoseUncapturableValueReference(*this, Loc, Var, DC); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10645 | return true; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10646 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10647 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10648 | CapturingScopeInfo *CSI = |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10649 | cast<CapturingScopeInfo>(FunctionScopes[FunctionScopesIndex]); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10650 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10651 | // Check whether we've already captured it. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10652 | if (CSI->CaptureMap.count(Var)) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10653 | // If we found a capture, any subcaptures are nested. |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10654 | Nested = true; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10655 | |
10656 | // Retrieve the capture type for this variable. | ||||
10657 | CaptureType = CSI->getCapture(Var).getCaptureType(); | ||||
10658 | |||||
10659 | // Compute the type of an expression that refers to this variable. | ||||
10660 | DeclRefType = CaptureType.getNonReferenceType(); | ||||
10661 | |||||
10662 | const CapturingScopeInfo::Capture &Cap = CSI->getCapture(Var); | ||||
10663 | if (Cap.isCopyCapture() && | ||||
10664 | !(isa<LambdaScopeInfo>(CSI) && cast<LambdaScopeInfo>(CSI)->Mutable)) | ||||
10665 | DeclRefType.addConst(); | ||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10666 | break; |
10667 | } | ||||
10668 | |||||
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10669 | bool IsBlock = isa<BlockScopeInfo>(CSI); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10670 | bool IsLambda = !IsBlock; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10671 | |
10672 | // Lambdas are not allowed to capture unnamed variables | ||||
10673 | // (e.g. anonymous unions). | ||||
10674 | // FIXME: The C++11 rule don't actually state this explicitly, but I'm | ||||
10675 | // assuming that's the intent. | ||||
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10676 | if (IsLambda && !Var->getDeclName()) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10677 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10678 | Diag(Loc, diag::err_lambda_capture_anonymous_var); |
10679 | Diag(Var->getLocation(), diag::note_declared_at); | ||||
10680 | } | ||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10681 | return true; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10682 | } |
10683 | |||||
10684 | // Prohibit variably-modified types; they're difficult to deal with. | ||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10685 | if (Var->getType()->isVariablyModifiedType()) { |
10686 | if (BuildAndDiagnose) { | ||||
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10687 | if (IsBlock) |
10688 | Diag(Loc, diag::err_ref_vm_type); | ||||
10689 | else | ||||
10690 | Diag(Loc, diag::err_lambda_capture_vm_type) << Var->getDeclName(); | ||||
10691 | Diag(Var->getLocation(), diag::note_previous_decl) | ||||
10692 | << Var->getDeclName(); | ||||
10693 | } | ||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10694 | return true; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10695 | } |
10696 | |||||
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10697 | // Lambdas are not allowed to capture __block variables; they don't |
10698 | // support the expected semantics. | ||||
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10699 | if (IsLambda && HasBlocksAttr) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10700 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10701 | Diag(Loc, diag::err_lambda_capture_block) |
10702 | << Var->getDeclName(); | ||||
10703 | Diag(Var->getLocation(), diag::note_previous_decl) | ||||
10704 | << Var->getDeclName(); | ||||
10705 | } | ||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10706 | return true; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10707 | } |
10708 | |||||
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10709 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None && !Explicit) { |
10710 | // No capture-default | ||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10711 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10712 | Diag(Loc, diag::err_lambda_impcap) << Var->getDeclName(); |
10713 | Diag(Var->getLocation(), diag::note_previous_decl) | ||||
10714 | << Var->getDeclName(); | ||||
10715 | Diag(cast<LambdaScopeInfo>(CSI)->Lambda->getLocStart(), | ||||
10716 | diag::note_lambda_decl); | ||||
10717 | } | ||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10718 | return true; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10719 | } |
10720 | |||||
10721 | FunctionScopesIndex--; | ||||
10722 | DC = ParentDC; | ||||
10723 | Explicit = false; | ||||
10724 | } while (!Var->getDeclContext()->Equals(DC)); | ||||
10725 | |||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10726 | // Walk back down the scope stack, computing the type of the capture at |
10727 | // each step, checking type-specific requirements, and adding captures if | ||||
10728 | // requested. | ||||
10729 | for (unsigned I = ++FunctionScopesIndex, N = FunctionScopes.size(); I != N; | ||||
10730 | ++I) { | ||||
10731 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[I]); | ||||
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 10732 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10733 | // Compute the type of the capture and of a reference to the capture within |
10734 | // this scope. | ||||
10735 | if (isa<BlockScopeInfo>(CSI)) { | ||||
10736 | Expr *CopyExpr = 0; | ||||
10737 | bool ByRef = false; | ||||
10738 | |||||
10739 | // Blocks are not allowed to capture arrays. | ||||
10740 | if (CaptureType->isArrayType()) { | ||||
10741 | if (BuildAndDiagnose) { | ||||
10742 | Diag(Loc, diag::err_ref_array_type); | ||||
10743 | Diag(Var->getLocation(), diag::note_previous_decl) | ||||
10744 | << Var->getDeclName(); | ||||
10745 | } | ||||
10746 | return true; | ||||
10747 | } | ||||
10748 | |||||
John McCall | 100c649 | 2012-03-30 05:23:48 +0000 | [diff] [blame] | 10749 | // Forbid the block-capture of autoreleasing variables. |
10750 | if (CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { | ||||
10751 | if (BuildAndDiagnose) { | ||||
10752 | Diag(Loc, diag::err_arc_autoreleasing_capture) | ||||
10753 | << /*block*/ 0; | ||||
10754 | Diag(Var->getLocation(), diag::note_previous_decl) | ||||
10755 | << Var->getDeclName(); | ||||
10756 | } | ||||
10757 | return true; | ||||
10758 | } | ||||
10759 | |||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10760 | if (HasBlocksAttr || CaptureType->isReferenceType()) { |
10761 | // Block capture by reference does not change the capture or | ||||
10762 | // declaration reference types. | ||||
10763 | ByRef = true; | ||||
10764 | } else { | ||||
10765 | // Block capture by copy introduces 'const'. | ||||
10766 | CaptureType = CaptureType.getNonReferenceType().withConst(); | ||||
10767 | DeclRefType = CaptureType; | ||||
10768 | |||||
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10769 | if (getLangOpts().CPlusPlus && BuildAndDiagnose) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10770 | if (const RecordType *Record = DeclRefType->getAs<RecordType>()) { |
10771 | // The capture logic needs the destructor, so make sure we mark it. | ||||
10772 | // Usually this is unnecessary because most local variables have | ||||
10773 | // their destructors marked at declaration time, but parameters are | ||||
10774 | // an exception because it's technically only the call site that | ||||
10775 | // actually requires the destructor. | ||||
10776 | if (isa<ParmVarDecl>(Var)) | ||||
10777 | FinalizeVarWithDestructor(Var, Record); | ||||
10778 | |||||
10779 | // According to the blocks spec, the capture of a variable from | ||||
10780 | // the stack requires a const copy constructor. This is not true | ||||
10781 | // 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] | 10782 | Expr *DeclRef = new (Context) DeclRefExpr(Var, false, |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10783 | DeclRefType.withConst(), |
10784 | VK_LValue, Loc); | ||||
10785 | ExprResult Result | ||||
10786 | = PerformCopyInitialization( | ||||
10787 | InitializedEntity::InitializeBlock(Var->getLocation(), | ||||
10788 | CaptureType, false), | ||||
10789 | Loc, Owned(DeclRef)); | ||||
10790 | |||||
10791 | // Build a full-expression copy expression if initialization | ||||
10792 | // succeeded and used a non-trivial constructor. Recover from | ||||
10793 | // errors by pretending that the copy isn't necessary. | ||||
10794 | if (!Result.isInvalid() && | ||||
10795 | !cast<CXXConstructExpr>(Result.get())->getConstructor() | ||||
10796 | ->isTrivial()) { | ||||
10797 | Result = MaybeCreateExprWithCleanups(Result); | ||||
10798 | CopyExpr = Result.take(); | ||||
10799 | } | ||||
10800 | } | ||||
10801 | } | ||||
10802 | } | ||||
10803 | |||||
10804 | // Actually capture the variable. | ||||
10805 | if (BuildAndDiagnose) | ||||
10806 | CSI->addCapture(Var, HasBlocksAttr, ByRef, Nested, Loc, | ||||
10807 | SourceLocation(), CaptureType, CopyExpr); | ||||
10808 | Nested = true; | ||||
10809 | continue; | ||||
10810 | } | ||||
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 10811 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10812 | LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CSI); |
10813 | |||||
10814 | // Determine whether we are capturing by reference or by value. | ||||
10815 | bool ByRef = false; | ||||
10816 | if (I == N - 1 && Kind != TryCapture_Implicit) { | ||||
10817 | ByRef = (Kind == TryCapture_ExplicitByRef); | ||||
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10818 | } else { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10819 | ByRef = (LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByref); |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10820 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10821 | |
10822 | // Compute the type of the field that will capture this variable. | ||||
10823 | if (ByRef) { | ||||
10824 | // C++11 [expr.prim.lambda]p15: | ||||
10825 | // An entity is captured by reference if it is implicitly or | ||||
10826 | // explicitly captured but not captured by copy. It is | ||||
10827 | // unspecified whether additional unnamed non-static data | ||||
10828 | // members are declared in the closure type for entities | ||||
10829 | // captured by reference. | ||||
10830 | // | ||||
10831 | // FIXME: It is not clear whether we want to build an lvalue reference | ||||
10832 | // to the DeclRefType or to CaptureType.getNonReferenceType(). GCC appears | ||||
10833 | // to do the former, while EDG does the latter. Core issue 1249 will | ||||
10834 | // clarify, but for now we follow GCC because it's a more permissive and | ||||
10835 | // easily defensible position. | ||||
10836 | CaptureType = Context.getLValueReferenceType(DeclRefType); | ||||
10837 | } else { | ||||
10838 | // C++11 [expr.prim.lambda]p14: | ||||
10839 | // For each entity captured by copy, an unnamed non-static | ||||
10840 | // data member is declared in the closure type. The | ||||
10841 | // declaration order of these members is unspecified. The type | ||||
10842 | // of such a data member is the type of the corresponding | ||||
10843 | // captured entity if the entity is not a reference to an | ||||
10844 | // object, or the referenced type otherwise. [Note: If the | ||||
10845 | // captured entity is a reference to a function, the | ||||
10846 | // corresponding data member is also a reference to a | ||||
10847 | // function. - end note ] | ||||
10848 | if (const ReferenceType *RefType = CaptureType->getAs<ReferenceType>()){ | ||||
10849 | if (!RefType->getPointeeType()->isFunctionType()) | ||||
10850 | CaptureType = RefType->getPointeeType(); | ||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10851 | } |
John McCall | 100c649 | 2012-03-30 05:23:48 +0000 | [diff] [blame] | 10852 | |
10853 | // Forbid the lambda copy-capture of autoreleasing variables. | ||||
10854 | if (CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { | ||||
10855 | if (BuildAndDiagnose) { | ||||
10856 | Diag(Loc, diag::err_arc_autoreleasing_capture) << /*lambda*/ 1; | ||||
10857 | Diag(Var->getLocation(), diag::note_previous_decl) | ||||
10858 | << Var->getDeclName(); | ||||
10859 | } | ||||
10860 | return true; | ||||
10861 | } | ||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10862 | } |
10863 | |||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10864 | // Capture this variable in the lambda. |
10865 | Expr *CopyExpr = 0; | ||||
10866 | if (BuildAndDiagnose) { | ||||
10867 | ExprResult Result = captureInLambda(*this, LSI, Var, CaptureType, | ||||
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10868 | DeclRefType, Loc, |
10869 | I == N-1); | ||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10870 | if (!Result.isInvalid()) |
10871 | CopyExpr = Result.take(); | ||||
10872 | } | ||||
10873 | |||||
10874 | // Compute the type of a reference to this captured variable. | ||||
10875 | if (ByRef) | ||||
10876 | DeclRefType = CaptureType.getNonReferenceType(); | ||||
10877 | else { | ||||
10878 | // C++ [expr.prim.lambda]p5: | ||||
10879 | // The closure type for a lambda-expression has a public inline | ||||
10880 | // function call operator [...]. This function call operator is | ||||
10881 | // declared const (9.3.1) if and only if the lambda-expression’s | ||||
10882 | // parameter-declaration-clause is not followed by mutable. | ||||
10883 | DeclRefType = CaptureType.getNonReferenceType(); | ||||
10884 | if (!LSI->Mutable && !CaptureType->isReferenceType()) | ||||
10885 | DeclRefType.addConst(); | ||||
10886 | } | ||||
10887 | |||||
10888 | // Add the capture. | ||||
10889 | if (BuildAndDiagnose) | ||||
10890 | CSI->addCapture(Var, /*IsBlock=*/false, ByRef, Nested, Loc, | ||||
10891 | EllipsisLoc, CaptureType, CopyExpr); | ||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10892 | Nested = true; |
10893 | } | ||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10894 | |
10895 | return false; | ||||
10896 | } | ||||
10897 | |||||
10898 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, | ||||
10899 | TryCaptureKind Kind, SourceLocation EllipsisLoc) { | ||||
10900 | QualType CaptureType; | ||||
10901 | QualType DeclRefType; | ||||
10902 | return tryCaptureVariable(Var, Loc, Kind, EllipsisLoc, | ||||
10903 | /*BuildAndDiagnose=*/true, CaptureType, | ||||
10904 | DeclRefType); | ||||
10905 | } | ||||
10906 | |||||
10907 | QualType Sema::getCapturedDeclRefType(VarDecl *Var, SourceLocation Loc) { | ||||
10908 | QualType CaptureType; | ||||
10909 | QualType DeclRefType; | ||||
10910 | |||||
10911 | // Determine whether we can capture this variable. | ||||
10912 | if (tryCaptureVariable(Var, Loc, TryCapture_Implicit, SourceLocation(), | ||||
10913 | /*BuildAndDiagnose=*/false, CaptureType, DeclRefType)) | ||||
10914 | return QualType(); | ||||
10915 | |||||
10916 | return DeclRefType; | ||||
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10917 | } |
10918 | |||||
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10919 | static void MarkVarDeclODRUsed(Sema &SemaRef, VarDecl *Var, |
10920 | SourceLocation Loc) { | ||||
10921 | // Keep track of used but undefined variables. | ||||
Eli Friedman | 0cc5d40 | 2012-02-04 00:54:05 +0000 | [diff] [blame] | 10922 | // FIXME: We shouldn't suppress this warning for static data members. |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 10923 | if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly && |
Eli Friedman | 0cc5d40 | 2012-02-04 00:54:05 +0000 | [diff] [blame] | 10924 | Var->getLinkage() != ExternalLinkage && |
10925 | !(Var->isStaticDataMember() && Var->hasInit())) { | ||||
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10926 | SourceLocation &old = SemaRef.UndefinedInternals[Var->getCanonicalDecl()]; |
10927 | if (old.isInvalid()) old = Loc; | ||||
10928 | } | ||||
10929 | |||||
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10930 | SemaRef.tryCaptureVariable(Var, Loc); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10931 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10932 | Var->setUsed(true); |
10933 | } | ||||
10934 | |||||
10935 | void Sema::UpdateMarkingForLValueToRValue(Expr *E) { | ||||
10936 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it is | ||||
10937 | // an object that satisfies the requirements for appearing in a | ||||
10938 | // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) | ||||
10939 | // is immediately applied." This function handles the lvalue-to-rvalue | ||||
10940 | // conversion part. | ||||
10941 | MaybeODRUseExprs.erase(E->IgnoreParens()); | ||||
10942 | } | ||||
10943 | |||||
Eli Friedman | ac62601 | 2012-02-29 03:16:56 +0000 | [diff] [blame] | 10944 | ExprResult Sema::ActOnConstantExpression(ExprResult Res) { |
10945 | if (!Res.isUsable()) | ||||
10946 | return Res; | ||||
10947 | |||||
10948 | // If a constant-expression is a reference to a variable where we delay | ||||
10949 | // deciding whether it is an odr-use, just assume we will apply the | ||||
10950 | // lvalue-to-rvalue conversion. In the one case where this doesn't happen | ||||
10951 | // (a non-type template argument), we have special handling anyway. | ||||
10952 | UpdateMarkingForLValueToRValue(Res.get()); | ||||
10953 | return Res; | ||||
10954 | } | ||||
10955 | |||||
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10956 | void Sema::CleanupVarDeclMarking() { |
10957 | for (llvm::SmallPtrSetIterator<Expr*> i = MaybeODRUseExprs.begin(), | ||||
10958 | e = MaybeODRUseExprs.end(); | ||||
10959 | i != e; ++i) { | ||||
10960 | VarDecl *Var; | ||||
10961 | SourceLocation Loc; | ||||
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 10962 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*i)) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10963 | Var = cast<VarDecl>(DRE->getDecl()); |
10964 | Loc = DRE->getLocation(); | ||||
10965 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(*i)) { | ||||
10966 | Var = cast<VarDecl>(ME->getMemberDecl()); | ||||
10967 | Loc = ME->getMemberLoc(); | ||||
10968 | } else { | ||||
10969 | llvm_unreachable("Unexpcted expression"); | ||||
10970 | } | ||||
10971 | |||||
10972 | MarkVarDeclODRUsed(*this, Var, Loc); | ||||
10973 | } | ||||
10974 | |||||
10975 | MaybeODRUseExprs.clear(); | ||||
10976 | } | ||||
10977 | |||||
10978 | // Mark a VarDecl referenced, and perform the necessary handling to compute | ||||
10979 | // odr-uses. | ||||
10980 | static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, | ||||
10981 | VarDecl *Var, Expr *E) { | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10982 | Var->setReferenced(); |
10983 | |||||
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10984 | if (!IsPotentiallyEvaluatedContext(SemaRef)) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10985 | return; |
10986 | |||||
10987 | // Implicit instantiation of static data members of class templates. | ||||
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10988 | if (Var->isStaticDataMember() && Var->getInstantiatedFromStaticDataMember()) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10989 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
10990 | assert(MSInfo && "Missing member specialization information?"); | ||||
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10991 | bool AlreadyInstantiated = !MSInfo->getPointOfInstantiation().isInvalid(); |
10992 | if (MSInfo->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && | ||||
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 10993 | (!AlreadyInstantiated || |
10994 | Var->isUsableInConstantExpressions(SemaRef.Context))) { | ||||
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10995 | if (!AlreadyInstantiated) { |
10996 | // This is a modification of an existing AST node. Notify listeners. | ||||
10997 | if (ASTMutationListener *L = SemaRef.getASTMutationListener()) | ||||
10998 | L->StaticDataMemberInstantiated(Var); | ||||
10999 | MSInfo->setPointOfInstantiation(Loc); | ||||
11000 | } | ||||
11001 | SourceLocation PointOfInstantiation = MSInfo->getPointOfInstantiation(); | ||||
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 11002 | if (Var->isUsableInConstantExpressions(SemaRef.Context)) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11003 | // Do not defer instantiations of variables which could be used in a |
11004 | // constant expression. | ||||
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 11005 | SemaRef.InstantiateStaticDataMemberDefinition(PointOfInstantiation,Var); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11006 | else |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 11007 | SemaRef.PendingInstantiations.push_back( |
11008 | std::make_pair(Var, PointOfInstantiation)); | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11009 | } |
11010 | } | ||||
11011 | |||||
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11012 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it is |
11013 | // an object that satisfies the requirements for appearing in a | ||||
11014 | // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) | ||||
11015 | // is immediately applied." We check the first part here, and | ||||
11016 | // Sema::UpdateMarkingForLValueToRValue deals with the second part. | ||||
11017 | // Note that we use the C++11 definition everywhere because nothing in | ||||
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 11018 | // C++03 depends on whether we get the C++03 version correct. This does not |
11019 | // apply to references, since they are not objects. | ||||
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11020 | const VarDecl *DefVD; |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 11021 | if (E && !isa<ParmVarDecl>(Var) && !Var->getType()->isReferenceType() && |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 11022 | Var->isUsableInConstantExpressions(SemaRef.Context) && |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11023 | Var->getAnyInitializer(DefVD) && DefVD->checkInitIsICE()) |
11024 | SemaRef.MaybeODRUseExprs.insert(E); | ||||
11025 | else | ||||
11026 | MarkVarDeclODRUsed(SemaRef, Var, Loc); | ||||
11027 | } | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11028 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11029 | /// \brief Mark a variable referenced, and check whether it is odr-used |
11030 | /// (C++ [basic.def.odr]p2, C99 6.9p3). Note that this should not be | ||||
11031 | /// used directly for normal expressions referring to VarDecl. | ||||
11032 | void Sema::MarkVariableReferenced(SourceLocation Loc, VarDecl *Var) { | ||||
11033 | DoMarkVarDeclReferenced(*this, Loc, Var, 0); | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11034 | } |
11035 | |||||
11036 | static void MarkExprReferenced(Sema &SemaRef, SourceLocation Loc, | ||||
11037 | Decl *D, Expr *E) { | ||||
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11038 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
11039 | DoMarkVarDeclReferenced(SemaRef, Loc, Var, E); | ||||
11040 | return; | ||||
11041 | } | ||||
11042 | |||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11043 | SemaRef.MarkAnyDeclReferenced(Loc, D); |
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 11044 | |
11045 | // If this is a call to a method via a cast, also mark the method in the | ||||
11046 | // derived class used in case codegen can devirtualize the call. | ||||
11047 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); | ||||
11048 | if (!ME) | ||||
11049 | return; | ||||
11050 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); | ||||
11051 | if (!MD) | ||||
11052 | return; | ||||
11053 | const Expr *Base = ME->getBase(); | ||||
Rafael Espindola | 8d852e3 | 2012-06-27 18:18:05 +0000 | [diff] [blame] | 11054 | const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType(); |
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 11055 | if (!MostDerivedClassDecl) |
11056 | return; | ||||
11057 | CXXMethodDecl *DM = MD->getCorrespondingMethodInClass(MostDerivedClassDecl); | ||||
Rafael Espindola | 0713d99 | 2012-06-27 17:44:39 +0000 | [diff] [blame] | 11058 | if (!DM) |
11059 | return; | ||||
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 11060 | SemaRef.MarkAnyDeclReferenced(Loc, DM); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11061 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11062 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11063 | /// \brief Perform reference-marking and odr-use handling for a DeclRefExpr. |
11064 | void Sema::MarkDeclRefReferenced(DeclRefExpr *E) { | ||||
11065 | MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E); | ||||
11066 | } | ||||
11067 | |||||
11068 | /// \brief Perform reference-marking and odr-use handling for a MemberExpr. | ||||
11069 | void Sema::MarkMemberReferenced(MemberExpr *E) { | ||||
11070 | MarkExprReferenced(*this, E->getMemberLoc(), E->getMemberDecl(), E); | ||||
11071 | } | ||||
11072 | |||||
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 11073 | /// \brief Perform marking for a reference to an arbitrary declaration. It |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11074 | /// marks the declaration referenced, and performs odr-use checking for functions |
11075 | /// and variables. This method should not be used when building an normal | ||||
11076 | /// expression which refers to a variable. | ||||
11077 | void Sema::MarkAnyDeclReferenced(SourceLocation Loc, Decl *D) { | ||||
11078 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) | ||||
11079 | MarkVariableReferenced(Loc, VD); | ||||
11080 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) | ||||
11081 | MarkFunctionReferenced(Loc, FD); | ||||
11082 | else | ||||
11083 | D->setReferenced(); | ||||
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 11084 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11085 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11086 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11087 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11088 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11089 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11090 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
11091 | Sema &S; | ||||
11092 | SourceLocation Loc; | ||||
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11093 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11094 | public: |
11095 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; | ||||
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11096 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11097 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11098 | |
11099 | bool TraverseTemplateArgument(const TemplateArgument &Arg); | ||||
11100 | bool TraverseRecordType(RecordType *T); | ||||
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11101 | }; |
11102 | } | ||||
11103 | |||||
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11104 | bool MarkReferencedDecls::TraverseTemplateArgument( |
11105 | const TemplateArgument &Arg) { | ||||
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11106 | if (Arg.getKind() == TemplateArgument::Declaration) { |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 11107 | if (Decl *D = Arg.getAsDecl()) |
11108 | S.MarkAnyDeclReferenced(Loc, D); | ||||
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11109 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11110 | |
11111 | return Inherited::TraverseTemplateArgument(Arg); | ||||
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11112 | } |
11113 | |||||
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11114 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11115 | if (ClassTemplateSpecializationDecl *Spec |
11116 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { | ||||
11117 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); | ||||
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 11118 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11119 | } |
11120 | |||||
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 11121 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11122 | } |
11123 | |||||
11124 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { | ||||
11125 | MarkReferencedDecls Marker(*this, Loc); | ||||
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11126 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11127 | } |
11128 | |||||
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11129 | namespace { |
11130 | /// \brief Helper class that marks all of the declarations referenced by | ||||
11131 | /// potentially-evaluated subexpressions as "referenced". | ||||
11132 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { | ||||
11133 | Sema &S; | ||||
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11134 | bool SkipLocalVariables; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11135 | |
11136 | public: | ||||
11137 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; | ||||
11138 | |||||
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11139 | EvaluatedExprMarker(Sema &S, bool SkipLocalVariables) |
11140 | : Inherited(S.Context), S(S), SkipLocalVariables(SkipLocalVariables) { } | ||||
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11141 | |
11142 | void VisitDeclRefExpr(DeclRefExpr *E) { | ||||
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11143 | // If we were asked not to visit local variables, don't. |
11144 | if (SkipLocalVariables) { | ||||
11145 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) | ||||
11146 | if (VD->hasLocalStorage()) | ||||
11147 | return; | ||||
11148 | } | ||||
11149 | |||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11150 | S.MarkDeclRefReferenced(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11151 | } |
11152 | |||||
11153 | void VisitMemberExpr(MemberExpr *E) { | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11154 | S.MarkMemberReferenced(E); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11155 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11156 | } |
11157 | |||||
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 11158 | void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11159 | S.MarkFunctionReferenced(E->getLocStart(), |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 11160 | const_cast<CXXDestructorDecl*>(E->getTemporary()->getDestructor())); |
11161 | Visit(E->getSubExpr()); | ||||
11162 | } | ||||
11163 | |||||
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11164 | void VisitCXXNewExpr(CXXNewExpr *E) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11165 | if (E->getOperatorNew()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11166 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorNew()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11167 | if (E->getOperatorDelete()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11168 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11169 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11170 | } |
Sebastian Redl | 2aed8b8 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 11171 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11172 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
11173 | if (E->getOperatorDelete()) | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11174 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 11175 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
11176 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { | ||||
11177 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11178 | S.MarkFunctionReferenced(E->getLocStart(), |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 11179 | S.LookupDestructor(Record)); |
11180 | } | ||||
11181 | |||||
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11182 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11183 | } |
11184 | |||||
11185 | void VisitCXXConstructExpr(CXXConstructExpr *E) { | ||||
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11186 | S.MarkFunctionReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11187 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11188 | } |
11189 | |||||
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 11190 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
11191 | Visit(E->getExpr()); | ||||
11192 | } | ||||
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11193 | |
11194 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { | ||||
11195 | Inherited::VisitImplicitCastExpr(E); | ||||
11196 | |||||
11197 | if (E->getCastKind() == CK_LValueToRValue) | ||||
11198 | S.UpdateMarkingForLValueToRValue(E->getSubExpr()); | ||||
11199 | } | ||||
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11200 | }; |
11201 | } | ||||
11202 | |||||
11203 | /// \brief Mark any declarations that appear within this expression or any | ||||
11204 | /// potentially-evaluated subexpressions as "referenced". | ||||
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11205 | /// |
11206 | /// \param SkipLocalVariables If true, don't mark local variables as | ||||
11207 | /// 'referenced'. | ||||
11208 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E, | ||||
11209 | bool SkipLocalVariables) { | ||||
11210 | EvaluatedExprMarker(*this, SkipLocalVariables).Visit(E); | ||||
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11211 | } |
11212 | |||||
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11213 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
11214 | /// of the program being compiled. | ||||
11215 | /// | ||||
11216 | /// This routine emits the given diagnostic when the code currently being | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11217 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11218 | /// possibility that the code will actually be executable. Code in sizeof() |
11219 | /// expressions, code used only during overload resolution, etc., are not | ||||
11220 | /// potentially evaluated. This routine will suppress such diagnostics or, | ||||
11221 | /// in the absolutely nutty case of potentially potentially evaluated | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11222 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11223 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11224 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11225 | /// This routine should be used for all diagnostics that describe the run-time |
11226 | /// behavior of a program, such as passing a non-POD value through an ellipsis. | ||||
11227 | /// Failure to do so will likely result in spurious diagnostics or failures | ||||
11228 | /// during overload resolution or within sizeof/alignof/typeof/typeid. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11229 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *Statement, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11230 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11231 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11232 | case Unevaluated: |
11233 | // The argument will never be evaluated, so don't complain. | ||||
11234 | break; | ||||
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11235 | |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 11236 | case ConstantEvaluated: |
11237 | // Relevant diagnostics should be produced by constant evaluation. | ||||
11238 | break; | ||||
11239 | |||||
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11240 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11241 | case PotentiallyEvaluatedIfUsed: |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11242 | if (Statement && getCurFunctionOrMethodDecl()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 11243 | FunctionScopes.back()->PossiblyUnreachableDiags. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11244 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, Statement)); |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 11245 | } |
11246 | else | ||||
11247 | Diag(Loc, PD); | ||||
11248 | |||||
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11249 | return true; |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11250 | } |
11251 | |||||
11252 | return false; | ||||
11253 | } | ||||
11254 | |||||
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11255 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
11256 | CallExpr *CE, FunctionDecl *FD) { | ||||
11257 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) | ||||
11258 | return false; | ||||
11259 | |||||
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 11260 | // If we're inside a decltype's expression, don't check for a valid return |
11261 | // type or construct temporaries until we know whether this is the last call. | ||||
11262 | if (ExprEvalContexts.back().IsDecltype) { | ||||
11263 | ExprEvalContexts.back().DelayedDecltypeCalls.push_back(CE); | ||||
11264 | return false; | ||||
11265 | } | ||||
11266 | |||||
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 11267 | class CallReturnIncompleteDiagnoser : public TypeDiagnoser { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11268 | FunctionDecl *FD; |
11269 | CallExpr *CE; | ||||
11270 | |||||
11271 | public: | ||||
11272 | CallReturnIncompleteDiagnoser(FunctionDecl *FD, CallExpr *CE) | ||||
11273 | : FD(FD), CE(CE) { } | ||||
11274 | |||||
11275 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { | ||||
11276 | if (!FD) { | ||||
11277 | S.Diag(Loc, diag::err_call_incomplete_return) | ||||
11278 | << T << CE->getSourceRange(); | ||||
11279 | return; | ||||
11280 | } | ||||
11281 | |||||
11282 | S.Diag(Loc, diag::err_call_function_incomplete_return) | ||||
11283 | << CE->getSourceRange() << FD->getDeclName() << T; | ||||
11284 | S.Diag(FD->getLocation(), | ||||
11285 | diag::note_function_with_incomplete_return_type_declared_here) | ||||
11286 | << FD->getDeclName(); | ||||
11287 | } | ||||
11288 | } Diagnoser(FD, CE); | ||||
11289 | |||||
11290 | if (RequireCompleteType(Loc, ReturnType, Diagnoser)) | ||||
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11291 | return true; |
11292 | |||||
11293 | return false; | ||||
11294 | } | ||||
11295 | |||||
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11296 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11297 | // will prevent this condition from triggering, which is what we want. |
11298 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { | ||||
11299 | SourceLocation Loc; | ||||
11300 | |||||
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11301 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11302 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11303 | |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 11304 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11305 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11306 | return; |
11307 | |||||
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11308 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
11309 | |||||
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11310 | // Greylist some idioms by putting them into a warning subcategory. |
11311 | if (ObjCMessageExpr *ME | ||||
11312 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { | ||||
11313 | Selector Sel = ME->getSelector(); | ||||
11314 | |||||
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11315 | // self = [<foo> init...] |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 11316 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11317 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
11318 | |||||
11319 | // <foo> = [<bar> nextObject] | ||||
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 11320 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11321 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
11322 | } | ||||
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11323 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11324 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 11325 | } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11326 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11327 | return; |
11328 | |||||
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11329 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11330 | Loc = Op->getOperatorLoc(); |
Fariborz Jahanian | a414a2f | 2012-08-29 17:17:11 +0000 | [diff] [blame] | 11331 | } else if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
11332 | return DiagnoseAssignmentAsCondition(POE->getSyntacticForm()); | ||||
11333 | else { | ||||
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11334 | // Not an assignment. |
11335 | return; | ||||
11336 | } | ||||
11337 | |||||
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 11338 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11339 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11340 | SourceLocation Open = E->getLocStart(); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 11341 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
11342 | Diag(Loc, diag::note_condition_assign_silence) | ||||
11343 | << FixItHint::CreateInsertion(Open, "(") | ||||
11344 | << FixItHint::CreateInsertion(Close, ")"); | ||||
11345 | |||||
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11346 | if (IsOrAssign) |
11347 | Diag(Loc, diag::note_condition_or_assign_to_comparison) | ||||
11348 | << FixItHint::CreateReplacement(Loc, "!="); | ||||
11349 | else | ||||
11350 | Diag(Loc, diag::note_condition_assign_to_comparison) | ||||
11351 | << FixItHint::CreateReplacement(Loc, "=="); | ||||
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11352 | } |
11353 | |||||
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11354 | /// \brief Redundant parentheses over an equality comparison can indicate |
11355 | /// that the user intended an assignment used as condition. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11356 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *ParenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11357 | // Don't warn if the parens came from a macro. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11358 | SourceLocation parenLoc = ParenE->getLocStart(); |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11359 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
11360 | return; | ||||
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 11361 | // Don't warn for dependent expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11362 | if (ParenE->isTypeDependent()) |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 11363 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11364 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11365 | Expr *E = ParenE->IgnoreParens(); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11366 | |
11367 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) | ||||
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 11368 | if (opE->getOpcode() == BO_EQ && |
11369 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) | ||||
11370 | == Expr::MLV_Valid) { | ||||
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11371 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 11372 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 11373 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11374 | SourceRange ParenERange = ParenE->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 11375 | Diag(Loc, diag::note_equality_comparison_silence) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11376 | << FixItHint::CreateRemoval(ParenERange.getBegin()) |
11377 | << FixItHint::CreateRemoval(ParenERange.getEnd()); | ||||
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 11378 | Diag(Loc, diag::note_equality_comparison_to_assign) |
11379 | << FixItHint::CreateReplacement(Loc, "="); | ||||
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11380 | } |
11381 | } | ||||
11382 | |||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11383 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11384 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11385 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
11386 | DiagnoseEqualityWithExtraParens(parenE); | ||||
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11387 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11388 | ExprResult result = CheckPlaceholderExpr(E); |
11389 | if (result.isInvalid()) return ExprError(); | ||||
11390 | E = result.take(); | ||||
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 11391 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11392 | if (!E->isTypeDependent()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11393 | if (getLangOpts().CPlusPlus) |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 11394 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
11395 | |||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11396 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
11397 | if (ERes.isInvalid()) | ||||
11398 | return ExprError(); | ||||
11399 | E = ERes.take(); | ||||
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 11400 | |
11401 | QualType T = E->getType(); | ||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11402 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
11403 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) | ||||
11404 | << T << E->getSourceRange(); | ||||
11405 | return ExprError(); | ||||
11406 | } | ||||
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11407 | } |
11408 | |||||
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11409 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11410 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11411 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11412 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11413 | Expr *SubExpr) { |
11414 | if (!SubExpr) | ||||
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11415 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11416 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11417 | return CheckBooleanCondition(SubExpr, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11418 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11419 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11420 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11421 | /// A visitor for rebuilding a call to an __unknown_any expression |
11422 | /// to have an appropriate type. | ||||
11423 | struct RebuildUnknownAnyFunction | ||||
11424 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { | ||||
11425 | |||||
11426 | Sema &S; | ||||
11427 | |||||
11428 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} | ||||
11429 | |||||
11430 | ExprResult VisitStmt(Stmt *S) { | ||||
11431 | llvm_unreachable("unexpected statement!"); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11432 | } |
11433 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11434 | ExprResult VisitExpr(Expr *E) { |
11435 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_call) | ||||
11436 | << E->getSourceRange(); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11437 | return ExprError(); |
11438 | } | ||||
11439 | |||||
11440 | /// Rebuild an expression which simply semantically wraps another | ||||
11441 | /// expression which it shares the type and value kind of. | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11442 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
11443 | ExprResult SubResult = Visit(E->getSubExpr()); | ||||
11444 | if (SubResult.isInvalid()) return ExprError(); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11445 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11446 | Expr *SubExpr = SubResult.take(); |
11447 | E->setSubExpr(SubExpr); | ||||
11448 | E->setType(SubExpr->getType()); | ||||
11449 | E->setValueKind(SubExpr->getValueKind()); | ||||
11450 | assert(E->getObjectKind() == OK_Ordinary); | ||||
11451 | return E; | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11452 | } |
11453 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11454 | ExprResult VisitParenExpr(ParenExpr *E) { |
11455 | return rebuildSugarExpr(E); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11456 | } |
11457 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11458 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
11459 | return rebuildSugarExpr(E); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11460 | } |
11461 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11462 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
11463 | ExprResult SubResult = Visit(E->getSubExpr()); | ||||
11464 | if (SubResult.isInvalid()) return ExprError(); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11465 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11466 | Expr *SubExpr = SubResult.take(); |
11467 | E->setSubExpr(SubExpr); | ||||
11468 | E->setType(S.Context.getPointerType(SubExpr->getType())); | ||||
11469 | assert(E->getValueKind() == VK_RValue); | ||||
11470 | assert(E->getObjectKind() == OK_Ordinary); | ||||
11471 | return E; | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11472 | } |
11473 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11474 | ExprResult resolveDecl(Expr *E, ValueDecl *VD) { |
11475 | if (!isa<FunctionDecl>(VD)) return VisitExpr(E); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11476 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11477 | E->setType(VD->getType()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11478 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11479 | assert(E->getValueKind() == VK_RValue); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11480 | if (S.getLangOpts().CPlusPlus && |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11481 | !(isa<CXXMethodDecl>(VD) && |
11482 | cast<CXXMethodDecl>(VD)->isInstance())) | ||||
11483 | E->setValueKind(VK_LValue); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11484 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11485 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11486 | } |
11487 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11488 | ExprResult VisitMemberExpr(MemberExpr *E) { |
11489 | return resolveDecl(E, E->getMemberDecl()); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11490 | } |
11491 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11492 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
11493 | return resolveDecl(E, E->getDecl()); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11494 | } |
11495 | }; | ||||
11496 | } | ||||
11497 | |||||
11498 | /// Given a function expression of unknown-any type, try to rebuild it | ||||
11499 | /// to have a function type. | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11500 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { |
11501 | ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); | ||||
11502 | if (Result.isInvalid()) return ExprError(); | ||||
11503 | return S.DefaultFunctionArrayConversion(Result.take()); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11504 | } |
11505 | |||||
11506 | namespace { | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11507 | /// A visitor for rebuilding an expression of type __unknown_anytype |
11508 | /// into one which resolves the type directly on the referring | ||||
11509 | /// expression. Strict preservation of the original source | ||||
11510 | /// structure is not a goal. | ||||
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11511 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11512 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11513 | |
11514 | Sema &S; | ||||
11515 | |||||
11516 | /// The current destination type. | ||||
11517 | QualType DestType; | ||||
11518 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11519 | RebuildUnknownAnyExpr(Sema &S, QualType CastType) |
11520 | : S(S), DestType(CastType) {} | ||||
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11521 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11522 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11523 | llvm_unreachable("unexpected statement!"); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11524 | } |
11525 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11526 | ExprResult VisitExpr(Expr *E) { |
11527 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) | ||||
11528 | << E->getSourceRange(); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11529 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11530 | } |
11531 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11532 | ExprResult VisitCallExpr(CallExpr *E); |
11533 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *E); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11534 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11535 | /// Rebuild an expression which simply semantically wraps another |
11536 | /// expression which it shares the type and value kind of. | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11537 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
11538 | ExprResult SubResult = Visit(E->getSubExpr()); | ||||
11539 | if (SubResult.isInvalid()) return ExprError(); | ||||
11540 | Expr *SubExpr = SubResult.take(); | ||||
11541 | E->setSubExpr(SubExpr); | ||||
11542 | E->setType(SubExpr->getType()); | ||||
11543 | E->setValueKind(SubExpr->getValueKind()); | ||||
11544 | assert(E->getObjectKind() == OK_Ordinary); | ||||
11545 | return E; | ||||
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11546 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11547 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11548 | ExprResult VisitParenExpr(ParenExpr *E) { |
11549 | return rebuildSugarExpr(E); | ||||
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11550 | } |
11551 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11552 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
11553 | return rebuildSugarExpr(E); | ||||
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11554 | } |
11555 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11556 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
11557 | const PointerType *Ptr = DestType->getAs<PointerType>(); | ||||
11558 | if (!Ptr) { | ||||
11559 | S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof) | ||||
11560 | << E->getSourceRange(); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11561 | return ExprError(); |
11562 | } | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11563 | assert(E->getValueKind() == VK_RValue); |
11564 | assert(E->getObjectKind() == OK_Ordinary); | ||||
11565 | E->setType(DestType); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11566 | |
11567 | // 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] | 11568 | DestType = Ptr->getPointeeType(); |
11569 | ExprResult SubResult = Visit(E->getSubExpr()); | ||||
11570 | if (SubResult.isInvalid()) return ExprError(); | ||||
11571 | E->setSubExpr(SubResult.take()); | ||||
11572 | return E; | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11573 | } |
11574 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11575 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11576 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11577 | ExprResult resolveDecl(Expr *E, ValueDecl *VD); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11578 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11579 | ExprResult VisitMemberExpr(MemberExpr *E) { |
11580 | return resolveDecl(E, E->getMemberDecl()); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11581 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11582 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11583 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
11584 | return resolveDecl(E, E->getDecl()); | ||||
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11585 | } |
11586 | }; | ||||
11587 | } | ||||
11588 | |||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11589 | /// Rebuilds a call expression which yielded __unknown_anytype. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11590 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { |
11591 | Expr *CalleeExpr = E->getCallee(); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11592 | |
11593 | enum FnKind { | ||||
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11594 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11595 | FK_FunctionPointer, |
11596 | FK_BlockPointer | ||||
11597 | }; | ||||
11598 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11599 | FnKind Kind; |
11600 | QualType CalleeType = CalleeExpr->getType(); | ||||
11601 | if (CalleeType == S.Context.BoundMemberTy) { | ||||
11602 | assert(isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E)); | ||||
11603 | Kind = FK_MemberFunction; | ||||
11604 | CalleeType = Expr::findBoundMemberType(CalleeExpr); | ||||
11605 | } else if (const PointerType *Ptr = CalleeType->getAs<PointerType>()) { | ||||
11606 | CalleeType = Ptr->getPointeeType(); | ||||
11607 | Kind = FK_FunctionPointer; | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11608 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11609 | CalleeType = CalleeType->castAs<BlockPointerType>()->getPointeeType(); |
11610 | Kind = FK_BlockPointer; | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11611 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11612 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11613 | |
11614 | // Verify that this is a legal result type of a function. | ||||
11615 | if (DestType->isArrayType() || DestType->isFunctionType()) { | ||||
11616 | unsigned diagID = diag::err_func_returning_array_function; | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11617 | if (Kind == FK_BlockPointer) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11618 | diagID = diag::err_block_returning_array_function; |
11619 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11620 | S.Diag(E->getExprLoc(), diagID) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11621 | << DestType->isFunctionType() << DestType; |
11622 | return ExprError(); | ||||
11623 | } | ||||
11624 | |||||
11625 | // Otherwise, go ahead and set DestType as the call's result. | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11626 | E->setType(DestType.getNonLValueExprType(S.Context)); |
11627 | E->setValueKind(Expr::getValueKindForType(DestType)); | ||||
11628 | assert(E->getObjectKind() == OK_Ordinary); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11629 | |
11630 | // Rebuild the function type, replacing the result type with DestType. | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11631 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType)) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11632 | DestType = S.Context.getFunctionType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11633 | Proto->arg_type_begin(), |
11634 | Proto->getNumArgs(), | ||||
11635 | Proto->getExtProtoInfo()); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11636 | else |
11637 | DestType = S.Context.getFunctionNoProtoType(DestType, | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11638 | FnType->getExtInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11639 | |
11640 | // Rebuild the appropriate pointer-to-function type. | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11641 | switch (Kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11642 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11643 | // Nothing to do. |
11644 | break; | ||||
11645 | |||||
11646 | case FK_FunctionPointer: | ||||
11647 | DestType = S.Context.getPointerType(DestType); | ||||
11648 | break; | ||||
11649 | |||||
11650 | case FK_BlockPointer: | ||||
11651 | DestType = S.Context.getBlockPointerType(DestType); | ||||
11652 | break; | ||||
11653 | } | ||||
11654 | |||||
11655 | // Finally, we can recurse. | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11656 | ExprResult CalleeResult = Visit(CalleeExpr); |
11657 | if (!CalleeResult.isUsable()) return ExprError(); | ||||
11658 | E->setCallee(CalleeResult.take()); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11659 | |
11660 | // Bind a temporary if necessary. | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11661 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11662 | } |
11663 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11664 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11665 | // Verify that this is a legal result type of a call. |
11666 | if (DestType->isArrayType() || DestType->isFunctionType()) { | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11667 | S.Diag(E->getExprLoc(), diag::err_func_returning_array_function) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11668 | << DestType->isFunctionType() << DestType; |
11669 | return ExprError(); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11670 | } |
11671 | |||||
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 11672 | // Rewrite the method result type if available. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11673 | if (ObjCMethodDecl *Method = E->getMethodDecl()) { |
11674 | assert(Method->getResultType() == S.Context.UnknownAnyTy); | ||||
11675 | Method->setResultType(DestType); | ||||
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 11676 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11677 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11678 | // Change the type of the message. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11679 | E->setType(DestType.getNonReferenceType()); |
11680 | E->setValueKind(Expr::getValueKindForType(DestType)); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11681 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11682 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11683 | } |
11684 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11685 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11686 | // 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] | 11687 | if (E->getCastKind() == CK_FunctionToPointerDecay) { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11688 | assert(E->getValueKind() == VK_RValue); |
11689 | assert(E->getObjectKind() == OK_Ordinary); | ||||
11690 | |||||
11691 | E->setType(DestType); | ||||
11692 | |||||
11693 | // Rebuild the sub-expression as the pointee (function) type. | ||||
11694 | DestType = DestType->castAs<PointerType>()->getPointeeType(); | ||||
11695 | |||||
11696 | ExprResult Result = Visit(E->getSubExpr()); | ||||
11697 | if (!Result.isUsable()) return ExprError(); | ||||
11698 | |||||
11699 | E->setSubExpr(Result.take()); | ||||
11700 | return S.Owned(E); | ||||
Sean Callanan | ba66c6c | 2012-03-06 23:12:57 +0000 | [diff] [blame] | 11701 | } else if (E->getCastKind() == CK_LValueToRValue) { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11702 | assert(E->getValueKind() == VK_RValue); |
11703 | assert(E->getObjectKind() == OK_Ordinary); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11704 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11705 | assert(isa<BlockPointerType>(E->getType())); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11706 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11707 | E->setType(DestType); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11708 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11709 | // The sub-expression has to be a lvalue reference, so rebuild it as such. |
11710 | DestType = S.Context.getLValueReferenceType(DestType); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11711 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11712 | ExprResult Result = Visit(E->getSubExpr()); |
11713 | if (!Result.isUsable()) return ExprError(); | ||||
11714 | |||||
11715 | E->setSubExpr(Result.take()); | ||||
11716 | return S.Owned(E); | ||||
Sean Callanan | ba66c6c | 2012-03-06 23:12:57 +0000 | [diff] [blame] | 11717 | } else { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11718 | llvm_unreachable("Unhandled cast type!"); |
11719 | } | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11720 | } |
11721 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11722 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { |
11723 | ExprValueKind ValueKind = VK_LValue; | ||||
11724 | QualType Type = DestType; | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11725 | |
11726 | // We know how to make this work for certain kinds of decls: | ||||
11727 | |||||
11728 | // - functions | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11729 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(VD)) { |
11730 | if (const PointerType *Ptr = Type->getAs<PointerType>()) { | ||||
11731 | DestType = Ptr->getPointeeType(); | ||||
11732 | ExprResult Result = resolveDecl(E, VD); | ||||
11733 | if (Result.isInvalid()) return ExprError(); | ||||
11734 | return S.ImpCastExprToType(Result.take(), Type, | ||||
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 11735 | CK_FunctionToPointerDecay, VK_RValue); |
11736 | } | ||||
11737 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11738 | if (!Type->isFunctionType()) { |
11739 | S.Diag(E->getExprLoc(), diag::err_unknown_any_function) | ||||
11740 | << VD << E->getSourceRange(); | ||||
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 11741 | return ExprError(); |
11742 | } | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11743 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11744 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
11745 | if (MD->isInstance()) { | ||||
11746 | ValueKind = VK_RValue; | ||||
11747 | Type = S.Context.BoundMemberTy; | ||||
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11748 | } |
11749 | |||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11750 | // Function references aren't l-values in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11751 | if (!S.getLangOpts().CPlusPlus) |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11752 | ValueKind = VK_RValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11753 | |
11754 | // - variables | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11755 | } else if (isa<VarDecl>(VD)) { |
11756 | if (const ReferenceType *RefTy = Type->getAs<ReferenceType>()) { | ||||
11757 | Type = RefTy->getPointeeType(); | ||||
11758 | } else if (Type->isFunctionType()) { | ||||
11759 | S.Diag(E->getExprLoc(), diag::err_unknown_any_var_function_type) | ||||
11760 | << VD << E->getSourceRange(); | ||||
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11761 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11762 | } |
11763 | |||||
11764 | // - nothing else | ||||
11765 | } else { | ||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11766 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
11767 | << VD << E->getSourceRange(); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11768 | return ExprError(); |
11769 | } | ||||
11770 | |||||
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11771 | VD->setType(DestType); |
11772 | E->setType(Type); | ||||
11773 | E->setValueKind(ValueKind); | ||||
11774 | return S.Owned(E); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11775 | } |
11776 | |||||
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11777 | /// Check a cast of an unknown-any type. We intentionally only |
11778 | /// trigger this for C-style casts. | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11779 | ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, |
11780 | Expr *CastExpr, CastKind &CastKind, | ||||
11781 | ExprValueKind &VK, CXXCastPath &Path) { | ||||
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11782 | // Rewrite the casted expression from scratch. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11783 | ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11784 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11785 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11786 | CastExpr = result.take(); |
11787 | VK = CastExpr->getValueKind(); | ||||
11788 | CastKind = CK_NoOp; | ||||
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11789 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11790 | return CastExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11791 | } |
11792 | |||||
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 11793 | ExprResult Sema::forceUnknownAnyToType(Expr *E, QualType ToType) { |
11794 | return RebuildUnknownAnyExpr(*this, ToType).Visit(E); | ||||
11795 | } | ||||
11796 | |||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11797 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { |
11798 | Expr *orig = E; | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11799 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11800 | while (true) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11801 | E = E->IgnoreParenImpCasts(); |
11802 | if (CallExpr *call = dyn_cast<CallExpr>(E)) { | ||||
11803 | E = call->getCallee(); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11804 | diagID = diag::err_uncasted_call_of_unknown_any; |
11805 | } else { | ||||
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11806 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11807 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11808 | } |
11809 | |||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11810 | SourceLocation loc; |
11811 | NamedDecl *d; | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11812 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11813 | loc = ref->getLocation(); |
11814 | d = ref->getDecl(); | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11815 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11816 | loc = mem->getMemberLoc(); |
11817 | d = mem->getMemberDecl(); | ||||
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11818 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11819 | diagID = diag::err_uncasted_call_of_unknown_any; |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 11820 | loc = msg->getSelectorStartLoc(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11821 | d = msg->getMethodDecl(); |
John McCall | 819e745 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 11822 | if (!d) { |
11823 | S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) | ||||
11824 | << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() | ||||
11825 | << orig->getSourceRange(); | ||||
11826 | return ExprError(); | ||||
11827 | } | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11828 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11829 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
11830 | << E->getSourceRange(); | ||||
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11831 | return ExprError(); |
11832 | } | ||||
11833 | |||||
11834 | S.Diag(loc, diagID) << d << orig->getSourceRange(); | ||||
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11835 | |
11836 | // Never recoverable. | ||||
11837 | return ExprError(); | ||||
11838 | } | ||||
11839 | |||||
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11840 | /// Check for operands with placeholder types and complain if found. |
11841 | /// Returns true if there was an error and no recovery was possible. | ||||
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 11842 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11843 | const BuiltinType *placeholderType = E->getType()->getAsPlaceholderType(); |
11844 | if (!placeholderType) return Owned(E); | ||||
11845 | |||||
11846 | switch (placeholderType->getKind()) { | ||||
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11847 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11848 | // Overloaded expressions. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11849 | case BuiltinType::Overload: { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11850 | // Try to resolve a single function template specialization. |
11851 | // This is obligatory. | ||||
11852 | ExprResult result = Owned(E); | ||||
11853 | if (ResolveAndFixSingleFunctionTemplateSpecialization(result, false)) { | ||||
11854 | return result; | ||||
11855 | |||||
11856 | // If that failed, try to recover with a call. | ||||
11857 | } else { | ||||
11858 | tryToRecoverWithCall(result, PDiag(diag::err_ovl_unresolvable), | ||||
11859 | /*complain*/ true); | ||||
11860 | return result; | ||||
11861 | } | ||||
11862 | } | ||||
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11863 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11864 | // Bound member functions. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11865 | case BuiltinType::BoundMember: { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11866 | ExprResult result = Owned(E); |
11867 | tryToRecoverWithCall(result, PDiag(diag::err_bound_member_function), | ||||
11868 | /*complain*/ true); | ||||
11869 | return result; | ||||
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11870 | } |
11871 | |||||
11872 | // ARC unbridged casts. | ||||
11873 | case BuiltinType::ARCUnbridgedCast: { | ||||
11874 | Expr *realCast = stripARCUnbridgedCast(E); | ||||
11875 | diagnoseARCUnbridgedCast(realCast); | ||||
11876 | return Owned(realCast); | ||||
11877 | } | ||||
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11878 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11879 | // Expressions of unknown type. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11880 | case BuiltinType::UnknownAny: |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11881 | return diagnoseUnknownAnyExpr(*this, E); |
11882 | |||||
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 11883 | // Pseudo-objects. |
11884 | case BuiltinType::PseudoObject: | ||||
11885 | return checkPseudoObjectRValue(E); | ||||
11886 | |||||
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 11887 | case BuiltinType::BuiltinFn: |
11888 | Diag(E->getLocStart(), diag::err_builtin_fn_use); | ||||
11889 | return ExprError(); | ||||
11890 | |||||
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 11891 | // Everything else should be impossible. |
11892 | #define BUILTIN_TYPE(Id, SingletonId) \ | ||||
11893 | case BuiltinType::Id: | ||||
11894 | #define PLACEHOLDER_TYPE(Id, SingletonId) | ||||
11895 | #include "clang/AST/BuiltinTypes.def" | ||||
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11896 | break; |
11897 | } | ||||
11898 | |||||
11899 | llvm_unreachable("invalid placeholder type!"); | ||||
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11900 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11901 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11902 | bool Sema::CheckCaseExpression(Expr *E) { |
11903 | if (E->isTypeDependent()) | ||||
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11904 | return true; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11905 | if (E->isValueDependent() || E->isIntegerConstantExpr(Context)) |
11906 | return E->getType()->isIntegralOrEnumerationType(); | ||||
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11907 | return false; |
11908 | } | ||||
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11909 | |
11910 | /// ActOnObjCBoolLiteral - Parse {__objc_yes,__objc_no} literals. | ||||
11911 | ExprResult | ||||
11912 | Sema::ActOnObjCBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { | ||||
11913 | assert((Kind == tok::kw___objc_yes || Kind == tok::kw___objc_no) && | ||||
11914 | "Unknown Objective-C Boolean value!"); | ||||
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 11915 | QualType BoolT = Context.ObjCBuiltinBoolTy; |
11916 | if (!Context.getBOOLDecl()) { | ||||
11917 | LookupResult Result(*this, &Context.Idents.get("BOOL"), SourceLocation(), | ||||
11918 | Sema::LookupOrdinaryName); | ||||
11919 | if (LookupName(Result, getCurScope())) { | ||||
11920 | NamedDecl *ND = Result.getFoundDecl(); | ||||
11921 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) | ||||
11922 | Context.setBOOLDecl(TD); | ||||
11923 | } | ||||
11924 | } | ||||
11925 | if (Context.getBOOLDecl()) | ||||
11926 | BoolT = Context.getBOOLType(); | ||||
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11927 | return Owned(new (Context) ObjCBoolLiteralExpr(Kind == tok::kw___objc_yes, |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 11928 | BoolT, OpLoc)); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11929 | } |