/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaExpr.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements semantic analysis for expressions. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "TreeTransform.h" |
14 | | #include "UsedDeclVisitor.h" |
15 | | #include "clang/AST/ASTConsumer.h" |
16 | | #include "clang/AST/ASTContext.h" |
17 | | #include "clang/AST/ASTLambda.h" |
18 | | #include "clang/AST/ASTMutationListener.h" |
19 | | #include "clang/AST/CXXInheritance.h" |
20 | | #include "clang/AST/DeclObjC.h" |
21 | | #include "clang/AST/DeclTemplate.h" |
22 | | #include "clang/AST/EvaluatedExprVisitor.h" |
23 | | #include "clang/AST/Expr.h" |
24 | | #include "clang/AST/ExprCXX.h" |
25 | | #include "clang/AST/ExprObjC.h" |
26 | | #include "clang/AST/ExprOpenMP.h" |
27 | | #include "clang/AST/OperationKinds.h" |
28 | | #include "clang/AST/ParentMapContext.h" |
29 | | #include "clang/AST/RecursiveASTVisitor.h" |
30 | | #include "clang/AST/Type.h" |
31 | | #include "clang/AST/TypeLoc.h" |
32 | | #include "clang/Basic/Builtins.h" |
33 | | #include "clang/Basic/DiagnosticSema.h" |
34 | | #include "clang/Basic/PartialDiagnostic.h" |
35 | | #include "clang/Basic/SourceManager.h" |
36 | | #include "clang/Basic/Specifiers.h" |
37 | | #include "clang/Basic/TargetInfo.h" |
38 | | #include "clang/Lex/LiteralSupport.h" |
39 | | #include "clang/Lex/Preprocessor.h" |
40 | | #include "clang/Sema/AnalysisBasedWarnings.h" |
41 | | #include "clang/Sema/DeclSpec.h" |
42 | | #include "clang/Sema/DelayedDiagnostic.h" |
43 | | #include "clang/Sema/Designator.h" |
44 | | #include "clang/Sema/Initialization.h" |
45 | | #include "clang/Sema/Lookup.h" |
46 | | #include "clang/Sema/Overload.h" |
47 | | #include "clang/Sema/ParsedTemplate.h" |
48 | | #include "clang/Sema/Scope.h" |
49 | | #include "clang/Sema/ScopeInfo.h" |
50 | | #include "clang/Sema/SemaFixItUtils.h" |
51 | | #include "clang/Sema/SemaInternal.h" |
52 | | #include "clang/Sema/Template.h" |
53 | | #include "llvm/ADT/STLExtras.h" |
54 | | #include "llvm/ADT/StringExtras.h" |
55 | | #include "llvm/Support/Casting.h" |
56 | | #include "llvm/Support/ConvertUTF.h" |
57 | | #include "llvm/Support/SaveAndRestore.h" |
58 | | #include "llvm/Support/TypeSize.h" |
59 | | |
60 | | using namespace clang; |
61 | | using namespace sema; |
62 | | |
63 | | /// Determine whether the use of this declaration is valid, without |
64 | | /// emitting diagnostics. |
65 | 40.4k | bool Sema::CanUseDecl(NamedDecl *D, bool TreatUnavailableAsInvalid) { |
66 | | // See if this is an auto-typed variable whose initializer we are parsing. |
67 | 40.4k | if (ParsingInitForAutoVars.count(D)) |
68 | 0 | return false; |
69 | | |
70 | | // See if this is a deleted function. |
71 | 40.4k | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
72 | 8 | if (FD->isDeleted()) |
73 | 1 | return false; |
74 | | |
75 | | // If the function has a deduced return type, and we can't deduce it, |
76 | | // then we can't use it either. |
77 | 7 | if (getLangOpts().CPlusPlus14 && FD->getReturnType()->isUndeducedType()0 && |
78 | 7 | DeduceReturnType(FD, SourceLocation(), /*Diagnose*/ false)0 ) |
79 | 0 | return false; |
80 | | |
81 | | // See if this is an aligned allocation/deallocation function that is |
82 | | // unavailable. |
83 | 7 | if (TreatUnavailableAsInvalid && |
84 | 7 | isUnavailableAlignedAllocationFunction(*FD)0 ) |
85 | 0 | return false; |
86 | 7 | } |
87 | | |
88 | | // See if this function is unavailable. |
89 | 40.4k | if (TreatUnavailableAsInvalid && D->getAvailability() == AR_Unavailable19.3k && |
90 | 40.4k | cast<Decl>(CurContext)->getAvailability() != AR_Unavailable0 ) |
91 | 0 | return false; |
92 | | |
93 | 40.4k | if (isa<UnresolvedUsingIfExistsDecl>(D)) |
94 | 0 | return false; |
95 | | |
96 | 40.4k | return true; |
97 | 40.4k | } |
98 | | |
99 | 126M | static void DiagnoseUnusedOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc) { |
100 | | // Warn if this is used but marked unused. |
101 | 126M | if (const auto *A = D->getAttr<UnusedAttr>()) { |
102 | | // [[maybe_unused]] should not diagnose uses, but __attribute__((unused)) |
103 | | // should diagnose them. |
104 | 110k | if (A->getSemanticSpelling() != UnusedAttr::CXX11_maybe_unused && |
105 | 110k | A->getSemanticSpelling() != UnusedAttr::C2x_maybe_unused110k ) { |
106 | 110k | const Decl *DC = cast_or_null<Decl>(S.getCurObjCLexicalContext()); |
107 | 110k | if (DC && !DC->hasAttr<UnusedAttr>()) |
108 | 110k | S.Diag(Loc, diag::warn_used_but_marked_unused) << D; |
109 | 110k | } |
110 | 110k | } |
111 | 126M | } |
112 | | |
113 | | /// Emit a note explaining that this function is deleted. |
114 | 1.19k | void Sema::NoteDeletedFunction(FunctionDecl *Decl) { |
115 | 1.19k | assert(Decl && Decl->isDeleted()); |
116 | | |
117 | 1.19k | if (Decl->isDefaulted()) { |
118 | | // If the method was explicitly defaulted, point at that declaration. |
119 | 614 | if (!Decl->isImplicit()) |
120 | 51 | Diag(Decl->getLocation(), diag::note_implicitly_deleted); |
121 | | |
122 | | // Try to diagnose why this special member function was implicitly |
123 | | // deleted. This might fail, if that reason no longer applies. |
124 | 614 | DiagnoseDeletedDefaultedFunction(Decl); |
125 | 614 | return; |
126 | 614 | } |
127 | | |
128 | 576 | auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl); |
129 | 576 | if (Ctor && Ctor->isInheritingConstructor()351 ) |
130 | 8 | return NoteDeletedInheritingConstructor(Ctor); |
131 | | |
132 | 568 | Diag(Decl->getLocation(), diag::note_availability_specified_here) |
133 | 568 | << Decl << 1; |
134 | 568 | } |
135 | | |
136 | | /// Determine whether a FunctionDecl was ever declared with an |
137 | | /// explicit storage class. |
138 | 35 | static bool hasAnyExplicitStorageClass(const FunctionDecl *D) { |
139 | 35 | for (auto I : D->redecls()) { |
140 | 35 | if (I->getStorageClass() != SC_None) |
141 | 5 | return true; |
142 | 35 | } |
143 | 30 | return false; |
144 | 35 | } |
145 | | |
146 | | /// Check whether we're in an extern inline function and referring to a |
147 | | /// variable or function with internal linkage (C11 6.7.4p3). |
148 | | /// |
149 | | /// This is only a warning because we used to silently accept this code, but |
150 | | /// in many cases it will not behave correctly. This is not enabled in C++ mode |
151 | | /// because the restriction language is a bit weaker (C++11 [basic.def.odr]p6) |
152 | | /// and so while there may still be user mistakes, most of the time we can't |
153 | | /// prove that there are errors. |
154 | | static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S, |
155 | | const NamedDecl *D, |
156 | 126M | SourceLocation Loc) { |
157 | | // This is disabled under C++; there are too many ways for this to fire in |
158 | | // contexts where the warning is a false positive, or where it is technically |
159 | | // correct but benign. |
160 | 126M | if (S.getLangOpts().CPlusPlus) |
161 | 60.4M | return; |
162 | | |
163 | | // Check if this is an inlined function or method. |
164 | 66.1M | FunctionDecl *Current = S.getCurFunctionDecl(); |
165 | 66.1M | if (!Current) |
166 | 53.9M | return; |
167 | 12.1M | if (!Current->isInlined()) |
168 | 674k | return; |
169 | 11.5M | if (!Current->isExternallyVisible()) |
170 | 11.4M | return; |
171 | | |
172 | | // Check if the decl has internal linkage. |
173 | 56.0k | if (D->getFormalLinkage() != InternalLinkage) |
174 | 56.0k | return; |
175 | | |
176 | | // Downgrade from ExtWarn to Extension if |
177 | | // (1) the supposedly external inline function is in the main file, |
178 | | // and probably won't be included anywhere else. |
179 | | // (2) the thing we're referencing is a pure function. |
180 | | // (3) the thing we're referencing is another inline function. |
181 | | // This last can give us false negatives, but it's better than warning on |
182 | | // wrappers for simple C library functions. |
183 | 21 | const FunctionDecl *UsedFn = dyn_cast<FunctionDecl>(D); |
184 | 21 | bool DowngradeWarning = S.getSourceManager().isInMainFile(Loc); |
185 | 21 | if (!DowngradeWarning && UsedFn10 ) |
186 | 6 | DowngradeWarning = UsedFn->isInlined() || UsedFn->hasAttr<ConstAttr>()4 ; |
187 | | |
188 | 21 | S.Diag(Loc, DowngradeWarning ? diag::ext_internal_in_extern_inline_quiet14 |
189 | 21 | : diag::ext_internal_in_extern_inline7 ) |
190 | 21 | << /*IsVar=*/!UsedFn << D; |
191 | | |
192 | 21 | S.MaybeSuggestAddingStaticToDecl(Current); |
193 | | |
194 | 21 | S.Diag(D->getCanonicalDecl()->getLocation(), diag::note_entity_declared_at) |
195 | 21 | << D; |
196 | 21 | } |
197 | | |
198 | 35 | void Sema::MaybeSuggestAddingStaticToDecl(const FunctionDecl *Cur) { |
199 | 35 | const FunctionDecl *First = Cur->getFirstDecl(); |
200 | | |
201 | | // Suggest "static" on the function, if possible. |
202 | 35 | if (!hasAnyExplicitStorageClass(First)) { |
203 | 30 | SourceLocation DeclBegin = First->getSourceRange().getBegin(); |
204 | 30 | Diag(DeclBegin, diag::note_convert_inline_to_static) |
205 | 30 | << Cur << FixItHint::CreateInsertion(DeclBegin, "static "); |
206 | 30 | } |
207 | 35 | } |
208 | | |
209 | | /// Determine whether the use of this declaration is valid, and |
210 | | /// emit any corresponding diagnostics. |
211 | | /// |
212 | | /// This routine diagnoses various problems with referencing |
213 | | /// declarations that can occur when using a declaration. For example, |
214 | | /// it might warn if a deprecated or unavailable declaration is being |
215 | | /// used, or produce an error (and return true) if a C++0x deleted |
216 | | /// function is being used. |
217 | | /// |
218 | | /// \returns true if there was an error (this declaration cannot be |
219 | | /// referenced), false otherwise. |
220 | | /// |
221 | | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs, |
222 | | const ObjCInterfaceDecl *UnknownObjCClass, |
223 | | bool ObjCPropertyAccess, |
224 | | bool AvoidPartialAvailabilityChecks, |
225 | 126M | ObjCInterfaceDecl *ClassReceiver) { |
226 | 126M | SourceLocation Loc = Locs.front(); |
227 | 126M | if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)60.4M ) { |
228 | | // If there were any diagnostics suppressed by template argument deduction, |
229 | | // emit them now. |
230 | 2.71M | auto Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
231 | 2.71M | if (Pos != SuppressedDiagnostics.end()) { |
232 | 5.89k | for (const PartialDiagnosticAt &Suppressed : Pos->second) |
233 | 4.59k | Diag(Suppressed.first, Suppressed.second); |
234 | | |
235 | | // Clear out the list of suppressed diagnostics, so that we don't emit |
236 | | // them again for this specialization. However, we don't obsolete this |
237 | | // entry from the table, because we want to avoid ever emitting these |
238 | | // diagnostics again. |
239 | 5.89k | Pos->second.clear(); |
240 | 5.89k | } |
241 | | |
242 | | // C++ [basic.start.main]p3: |
243 | | // The function 'main' shall not be used within a program. |
244 | 2.71M | if (cast<FunctionDecl>(D)->isMain()) |
245 | 104 | Diag(Loc, diag::ext_main_used); |
246 | | |
247 | 2.71M | diagnoseUnavailableAlignedAllocation(*cast<FunctionDecl>(D), Loc); |
248 | 2.71M | } |
249 | | |
250 | | // See if this is an auto-typed variable whose initializer we are parsing. |
251 | 126M | if (ParsingInitForAutoVars.count(D)) { |
252 | 27 | if (isa<BindingDecl>(D)) { |
253 | 1 | Diag(Loc, diag::err_binding_cannot_appear_in_own_initializer) |
254 | 1 | << D->getDeclName(); |
255 | 26 | } else { |
256 | 26 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
257 | 26 | << D->getDeclName() << cast<VarDecl>(D)->getType(); |
258 | 26 | } |
259 | 27 | return true; |
260 | 27 | } |
261 | | |
262 | 126M | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
263 | | // See if this is a deleted function. |
264 | 5.02M | if (FD->isDeleted()) { |
265 | 177 | auto *Ctor = dyn_cast<CXXConstructorDecl>(FD); |
266 | 177 | if (Ctor && Ctor->isInheritingConstructor()10 ) |
267 | 8 | Diag(Loc, diag::err_deleted_inherited_ctor_use) |
268 | 8 | << Ctor->getParent() |
269 | 8 | << Ctor->getInheritedConstructor().getConstructor()->getParent(); |
270 | 169 | else |
271 | 169 | Diag(Loc, diag::err_deleted_function_use); |
272 | 177 | NoteDeletedFunction(FD); |
273 | 177 | return true; |
274 | 177 | } |
275 | | |
276 | | // [expr.prim.id]p4 |
277 | | // A program that refers explicitly or implicitly to a function with a |
278 | | // trailing requires-clause whose constraint-expression is not satisfied, |
279 | | // other than to declare it, is ill-formed. [...] |
280 | | // |
281 | | // See if this is a function with constraints that need to be satisfied. |
282 | | // Check this before deducing the return type, as it might instantiate the |
283 | | // definition. |
284 | 5.02M | if (FD->getTrailingRequiresClause()) { |
285 | 135 | ConstraintSatisfaction Satisfaction; |
286 | 135 | if (CheckFunctionConstraints(FD, Satisfaction, Loc)) |
287 | | // A diagnostic will have already been generated (non-constant |
288 | | // constraint expression, for example) |
289 | 1 | return true; |
290 | 134 | if (!Satisfaction.IsSatisfied) { |
291 | 18 | Diag(Loc, |
292 | 18 | diag::err_reference_to_function_with_unsatisfied_constraints) |
293 | 18 | << D; |
294 | 18 | DiagnoseUnsatisfiedConstraint(Satisfaction); |
295 | 18 | return true; |
296 | 18 | } |
297 | 134 | } |
298 | | |
299 | | // If the function has a deduced return type, and we can't deduce it, |
300 | | // then we can't use it either. |
301 | 5.02M | if (getLangOpts().CPlusPlus14 && FD->getReturnType()->isUndeducedType()683k && |
302 | 5.02M | DeduceReturnType(FD, Loc)2.53k ) |
303 | 57 | return true; |
304 | | |
305 | 5.02M | if (getLangOpts().CUDA && !CheckCUDACall(Loc, FD)6.84k ) |
306 | 38 | return true; |
307 | | |
308 | 5.02M | if (getLangOpts().SYCLIsDevice && !checkSYCLDeviceFunction(Loc, FD)504 ) |
309 | 0 | return true; |
310 | 5.02M | } |
311 | | |
312 | 126M | if (auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
313 | | // Lambdas are only default-constructible or assignable in C++2a onwards. |
314 | 1.54M | if (MD->getParent()->isLambda() && |
315 | 1.54M | (21.0k (21.0k isa<CXXConstructorDecl>(MD)21.0k && |
316 | 21.0k | cast<CXXConstructorDecl>(MD)->isDefaultConstructor()4.44k ) || |
317 | 21.0k | MD->isCopyAssignmentOperator()20.8k || MD->isMoveAssignmentOperator()20.8k )) { |
318 | 142 | Diag(Loc, diag::warn_cxx17_compat_lambda_def_ctor_assign) |
319 | 142 | << !isa<CXXConstructorDecl>(MD); |
320 | 142 | } |
321 | 1.54M | } |
322 | | |
323 | 126M | auto getReferencedObjCProp = [](const NamedDecl *D) -> |
324 | 126M | const ObjCPropertyDecl * { |
325 | 126M | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
326 | 22.9k | return MD->findPropertyDecl(); |
327 | 126M | return nullptr; |
328 | 126M | }; |
329 | 126M | if (const ObjCPropertyDecl *ObjCPDecl = getReferencedObjCProp(D)) { |
330 | 1.92k | if (diagnoseArgIndependentDiagnoseIfAttrs(ObjCPDecl, Loc)) |
331 | 0 | return true; |
332 | 126M | } else if (diagnoseArgIndependentDiagnoseIfAttrs(D, Loc)) { |
333 | 21 | return true; |
334 | 21 | } |
335 | | |
336 | | // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions |
337 | | // Only the variables omp_in and omp_out are allowed in the combiner. |
338 | | // Only the variables omp_priv and omp_orig are allowed in the |
339 | | // initializer-clause. |
340 | 126M | auto *DRD = dyn_cast<OMPDeclareReductionDecl>(CurContext); |
341 | 126M | if (LangOpts.OpenMP && DRD1.94M && !CurContext->containsDecl(D)1.96k && |
342 | 126M | isa<VarDecl>(D)349 ) { |
343 | 54 | Diag(Loc, diag::err_omp_wrong_var_in_declare_reduction) |
344 | 54 | << getCurFunction()->HasOMPDeclareReductionCombiner; |
345 | 54 | Diag(D->getLocation(), diag::note_entity_declared_at) << D; |
346 | 54 | return true; |
347 | 54 | } |
348 | | |
349 | | // [OpenMP 5.0], 2.19.7.3. declare mapper Directive, Restrictions |
350 | | // List-items in map clauses on this construct may only refer to the declared |
351 | | // variable var and entities that could be referenced by a procedure defined |
352 | | // at the same location |
353 | 126M | if (LangOpts.OpenMP && isa<VarDecl>(D)1.94M && |
354 | 126M | !isOpenMPDeclareMapperVarDeclAllowed(cast<VarDecl>(D))1.49M ) { |
355 | 8 | Diag(Loc, diag::err_omp_declare_mapper_wrong_var) |
356 | 8 | << getOpenMPDeclareMapperVarName(); |
357 | 8 | Diag(D->getLocation(), diag::note_entity_declared_at) << D; |
358 | 8 | return true; |
359 | 8 | } |
360 | | |
361 | 126M | if (const auto *EmptyD = dyn_cast<UnresolvedUsingIfExistsDecl>(D)) { |
362 | 732 | Diag(Loc, diag::err_use_of_empty_using_if_exists); |
363 | 732 | Diag(EmptyD->getLocation(), diag::note_empty_using_if_exists_here); |
364 | 732 | return true; |
365 | 732 | } |
366 | | |
367 | 126M | DiagnoseAvailabilityOfDecl(D, Locs, UnknownObjCClass, ObjCPropertyAccess, |
368 | 126M | AvoidPartialAvailabilityChecks, ClassReceiver); |
369 | | |
370 | 126M | DiagnoseUnusedOfDecl(*this, D, Loc); |
371 | | |
372 | 126M | diagnoseUseOfInternalDeclInInlineFunction(*this, D, Loc); |
373 | | |
374 | 126M | if (auto *VD = dyn_cast<ValueDecl>(D)) |
375 | 22.3M | checkTypeSupport(VD->getType(), Loc, VD); |
376 | | |
377 | 126M | if (LangOpts.SYCLIsDevice || (126M LangOpts.OpenMP126M && LangOpts.OpenMPIsDevice1.94M )) { |
378 | 71.6k | if (!Context.getTargetInfo().isTLSSupported()) |
379 | 23.7k | if (const auto *VD = dyn_cast<VarDecl>(D)) |
380 | 16.0k | if (VD->getTLSKind() != VarDecl::TLS_None) |
381 | 8 | targetDiag(*Locs.begin(), diag::err_thread_unsupported); |
382 | 71.6k | } |
383 | | |
384 | 126M | if (isa<ParmVarDecl>(D) && isa<RequiresExprBodyDecl>(D->getDeclContext())9.47M && |
385 | 126M | !isUnevaluatedContext()4.59k ) { |
386 | | // C++ [expr.prim.req.nested] p3 |
387 | | // A local parameter shall only appear as an unevaluated operand |
388 | | // (Clause 8) within the constraint-expression. |
389 | 2 | Diag(Loc, diag::err_requires_expr_parameter_referenced_in_evaluated_context) |
390 | 2 | << D; |
391 | 2 | Diag(D->getLocation(), diag::note_entity_declared_at) << D; |
392 | 2 | return true; |
393 | 2 | } |
394 | | |
395 | 126M | return false; |
396 | 126M | } |
397 | | |
398 | | /// DiagnoseSentinelCalls - This routine checks whether a call or |
399 | | /// message-send is to a declaration with the sentinel attribute, and |
400 | | /// if so, it checks that the requirements of the sentinel are |
401 | | /// satisfied. |
402 | | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
403 | 4.09M | ArrayRef<Expr *> Args) { |
404 | 4.09M | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
405 | 4.09M | if (!attr) |
406 | 4.09M | return; |
407 | | |
408 | | // The number of formal parameters of the declaration. |
409 | 272 | unsigned numFormalParams; |
410 | | |
411 | | // The kind of declaration. This is also an index into a %select in |
412 | | // the diagnostic. |
413 | 272 | enum CalleeType { CT_Function, CT_Method, CT_Block } calleeType; |
414 | | |
415 | 272 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
416 | 233 | numFormalParams = MD->param_size(); |
417 | 233 | calleeType = CT_Method; |
418 | 233 | } else if (FunctionDecl *39 FD39 = dyn_cast<FunctionDecl>(D)) { |
419 | 27 | numFormalParams = FD->param_size(); |
420 | 27 | calleeType = CT_Function; |
421 | 27 | } else if (12 isa<VarDecl>(D)12 ) { |
422 | 12 | QualType type = cast<ValueDecl>(D)->getType(); |
423 | 12 | const FunctionType *fn = nullptr; |
424 | 12 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
425 | 6 | fn = ptr->getPointeeType()->getAs<FunctionType>(); |
426 | 6 | if (!fn) return0 ; |
427 | 6 | calleeType = CT_Function; |
428 | 6 | } else if (const BlockPointerType *ptr = type->getAs<BlockPointerType>()) { |
429 | 6 | fn = ptr->getPointeeType()->castAs<FunctionType>(); |
430 | 6 | calleeType = CT_Block; |
431 | 6 | } else { |
432 | 0 | return; |
433 | 0 | } |
434 | | |
435 | 12 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fn)) { |
436 | 12 | numFormalParams = proto->getNumParams(); |
437 | 12 | } else { |
438 | 0 | numFormalParams = 0; |
439 | 0 | } |
440 | 12 | } else { |
441 | 0 | return; |
442 | 0 | } |
443 | | |
444 | | // "nullPos" is the number of formal parameters at the end which |
445 | | // effectively count as part of the variadic arguments. This is |
446 | | // useful if you would prefer to not have *any* formal parameters, |
447 | | // but the language forces you to have at least one. |
448 | 272 | unsigned nullPos = attr->getNullPos(); |
449 | 272 | assert((nullPos == 0 || nullPos == 1) && "invalid null position on sentinel"); |
450 | 272 | numFormalParams = (nullPos > numFormalParams ? 00 : numFormalParams - nullPos); |
451 | | |
452 | | // The number of arguments which should follow the sentinel. |
453 | 272 | unsigned numArgsAfterSentinel = attr->getSentinel(); |
454 | | |
455 | | // If there aren't enough arguments for all the formal parameters, |
456 | | // the sentinel, and the args after the sentinel, complain. |
457 | 272 | if (Args.size() < numFormalParams + numArgsAfterSentinel + 1) { |
458 | 4 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
459 | 4 | Diag(D->getLocation(), diag::note_sentinel_here) << int(calleeType); |
460 | 4 | return; |
461 | 4 | } |
462 | | |
463 | | // Otherwise, find the sentinel expression. |
464 | 268 | Expr *sentinelExpr = Args[Args.size() - numArgsAfterSentinel - 1]; |
465 | 268 | if (!sentinelExpr) return0 ; |
466 | 268 | if (sentinelExpr->isValueDependent()) return0 ; |
467 | 268 | if (Context.isSentinelNullExpr(sentinelExpr)) return243 ; |
468 | | |
469 | | // Pick a reasonable string to insert. Optimistically use 'nil', 'nullptr', |
470 | | // or 'NULL' if those are actually defined in the context. Only use |
471 | | // 'nil' for ObjC methods, where it's much more likely that the |
472 | | // variadic arguments form a list of object pointers. |
473 | 25 | SourceLocation MissingNilLoc = getLocForEndOfToken(sentinelExpr->getEndLoc()); |
474 | 25 | std::string NullValue; |
475 | 25 | if (calleeType == CT_Method && PP.isMacroDefined("nil")5 ) |
476 | 2 | NullValue = "nil"; |
477 | 23 | else if (getLangOpts().CPlusPlus11) |
478 | 5 | NullValue = "nullptr"; |
479 | 18 | else if (PP.isMacroDefined("NULL")) |
480 | 13 | NullValue = "NULL"; |
481 | 5 | else |
482 | 5 | NullValue = "(void*) 0"; |
483 | | |
484 | 25 | if (MissingNilLoc.isInvalid()) |
485 | 1 | Diag(Loc, diag::warn_missing_sentinel) << int(calleeType); |
486 | 24 | else |
487 | 24 | Diag(MissingNilLoc, diag::warn_missing_sentinel) |
488 | 24 | << int(calleeType) |
489 | 24 | << FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue); |
490 | 25 | Diag(D->getLocation(), diag::note_sentinel_here) << int(calleeType); |
491 | 25 | } |
492 | | |
493 | 96 | SourceRange Sema::getExprRange(Expr *E) const { |
494 | 96 | return E ? E->getSourceRange() : SourceRange()0 ; |
495 | 96 | } |
496 | | |
497 | | //===----------------------------------------------------------------------===// |
498 | | // Standard Promotions and Conversions |
499 | | //===----------------------------------------------------------------------===// |
500 | | |
501 | | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
502 | 27.1M | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E, bool Diagnose) { |
503 | | // Handle any placeholder expressions which made it here. |
504 | 27.1M | if (E->hasPlaceholderType()) { |
505 | 27 | ExprResult result = CheckPlaceholderExpr(E); |
506 | 27 | if (result.isInvalid()) return ExprError()4 ; |
507 | 23 | E = result.get(); |
508 | 23 | } |
509 | | |
510 | 27.1M | QualType Ty = E->getType(); |
511 | 27.1M | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
512 | | |
513 | 27.1M | if (Ty->isFunctionType()) { |
514 | 2.52k | if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts())) |
515 | 2.50k | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) |
516 | 2.50k | if (!checkAddressOfFunctionIsAvailable(FD, Diagnose, E->getExprLoc())) |
517 | 11 | return ExprError(); |
518 | | |
519 | 2.51k | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
520 | 2.51k | CK_FunctionToPointerDecay).get(); |
521 | 27.1M | } else if (Ty->isArrayType()) { |
522 | | // In C90 mode, arrays only promote to pointers if the array expression is |
523 | | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
524 | | // type 'array of type' is converted to an expression that has type 'pointer |
525 | | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
526 | | // that has type 'array of type' ...". The relevant change is "an lvalue" |
527 | | // (C90) to "an expression" (C99). |
528 | | // |
529 | | // C++ 4.2p1: |
530 | | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
531 | | // T" can be converted to an rvalue of type "pointer to T". |
532 | | // |
533 | 142k | if (getLangOpts().C99 || getLangOpts().CPlusPlus82.9k || E->isLValue()1.26k ) { |
534 | 142k | ExprResult Res = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
535 | 142k | CK_ArrayToPointerDecay); |
536 | 142k | if (Res.isInvalid()) |
537 | 24 | return ExprError(); |
538 | 142k | E = Res.get(); |
539 | 142k | } |
540 | 142k | } |
541 | 27.1M | return E; |
542 | 27.1M | } |
543 | | |
544 | 13.7M | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
545 | | // Check to see if we are dereferencing a null pointer. If so, |
546 | | // and if not volatile-qualified, this is undefined behavior that the |
547 | | // optimizer will delete, so warn about it. People sometimes try to use this |
548 | | // to get a deterministic trap and are surprised by clang's behavior. This |
549 | | // only handles the pattern "*null", which is a very syntactic check. |
550 | 13.7M | const auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts()); |
551 | 13.7M | if (UO && UO->getOpcode() == UO_Deref109k && |
552 | 13.7M | UO->getSubExpr()->getType()->isPointerType()98.9k ) { |
553 | 98.9k | const LangAS AS = |
554 | 98.9k | UO->getSubExpr()->getType()->getPointeeType().getAddressSpace(); |
555 | 98.9k | if ((!isTargetAddressSpace(AS) || |
556 | 98.9k | (15 isTargetAddressSpace(AS)15 && toTargetAddressSpace(AS) == 015 )) && |
557 | 98.9k | UO->getSubExpr()->IgnoreParenCasts()->isNullPointerConstant( |
558 | 98.8k | S.Context, Expr::NPC_ValueDependentIsNotNull) && |
559 | 98.9k | !UO->getType().isVolatileQualified()1.13k ) { |
560 | 36 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
561 | 36 | S.PDiag(diag::warn_indirection_through_null) |
562 | 36 | << UO->getSubExpr()->getSourceRange()); |
563 | 36 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
564 | 36 | S.PDiag(diag::note_indirection_through_null)); |
565 | 36 | } |
566 | 98.9k | } |
567 | 13.7M | } |
568 | | |
569 | | static void DiagnoseDirectIsaAccess(Sema &S, const ObjCIvarRefExpr *OIRE, |
570 | | SourceLocation AssignLoc, |
571 | 3.04k | const Expr* RHS) { |
572 | 3.04k | const ObjCIvarDecl *IV = OIRE->getDecl(); |
573 | 3.04k | if (!IV) |
574 | 0 | return; |
575 | | |
576 | 3.04k | DeclarationName MemberName = IV->getDeclName(); |
577 | 3.04k | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
578 | 3.04k | if (!Member || !Member->isStr("isa")) |
579 | 3.02k | return; |
580 | | |
581 | 19 | const Expr *Base = OIRE->getBase(); |
582 | 19 | QualType BaseType = Base->getType(); |
583 | 19 | if (OIRE->isArrow()) |
584 | 18 | BaseType = BaseType->getPointeeType(); |
585 | 19 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) |
586 | 19 | if (ObjCInterfaceDecl *IDecl = OTy->getInterface()) { |
587 | 19 | ObjCInterfaceDecl *ClassDeclared = nullptr; |
588 | 19 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
589 | 19 | if (!ClassDeclared->getSuperClass() |
590 | 19 | && (*ClassDeclared->ivar_begin()) == IV16 ) { |
591 | 11 | if (RHS) { |
592 | 4 | NamedDecl *ObjectSetClass = |
593 | 4 | S.LookupSingleName(S.TUScope, |
594 | 4 | &S.Context.Idents.get("object_setClass"), |
595 | 4 | SourceLocation(), S.LookupOrdinaryName); |
596 | 4 | if (ObjectSetClass) { |
597 | 3 | SourceLocation RHSLocEnd = S.getLocForEndOfToken(RHS->getEndLoc()); |
598 | 3 | S.Diag(OIRE->getExprLoc(), diag::warn_objc_isa_assign) |
599 | 3 | << FixItHint::CreateInsertion(OIRE->getBeginLoc(), |
600 | 3 | "object_setClass(") |
601 | 3 | << FixItHint::CreateReplacement( |
602 | 3 | SourceRange(OIRE->getOpLoc(), AssignLoc), ",") |
603 | 3 | << FixItHint::CreateInsertion(RHSLocEnd, ")"); |
604 | 3 | } |
605 | 1 | else |
606 | 1 | S.Diag(OIRE->getLocation(), diag::warn_objc_isa_assign); |
607 | 7 | } else { |
608 | 7 | NamedDecl *ObjectGetClass = |
609 | 7 | S.LookupSingleName(S.TUScope, |
610 | 7 | &S.Context.Idents.get("object_getClass"), |
611 | 7 | SourceLocation(), S.LookupOrdinaryName); |
612 | 7 | if (ObjectGetClass) |
613 | 4 | S.Diag(OIRE->getExprLoc(), diag::warn_objc_isa_use) |
614 | 4 | << FixItHint::CreateInsertion(OIRE->getBeginLoc(), |
615 | 4 | "object_getClass(") |
616 | 4 | << FixItHint::CreateReplacement( |
617 | 4 | SourceRange(OIRE->getOpLoc(), OIRE->getEndLoc()), ")"); |
618 | 3 | else |
619 | 3 | S.Diag(OIRE->getLocation(), diag::warn_objc_isa_use); |
620 | 7 | } |
621 | 11 | S.Diag(IV->getLocation(), diag::note_ivar_decl); |
622 | 11 | } |
623 | 19 | } |
624 | 19 | } |
625 | | |
626 | 39.9M | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
627 | | // Handle any placeholder expressions which made it here. |
628 | 39.9M | if (E->hasPlaceholderType()) { |
629 | 63 | ExprResult result = CheckPlaceholderExpr(E); |
630 | 63 | if (result.isInvalid()) return ExprError()20 ; |
631 | 43 | E = result.get(); |
632 | 43 | } |
633 | | |
634 | | // C++ [conv.lval]p1: |
635 | | // A glvalue of a non-function, non-array type T can be |
636 | | // converted to a prvalue. |
637 | 39.9M | if (!E->isGLValue()) return E28.1M ; |
638 | | |
639 | 11.8M | QualType T = E->getType(); |
640 | 11.8M | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
641 | | |
642 | | // lvalue-to-rvalue conversion cannot be applied to function or array types. |
643 | 11.8M | if (T->isFunctionType() || T->isArrayType()11.8M ) |
644 | 39 | return E; |
645 | | |
646 | | // We don't want to throw lvalue-to-rvalue casts on top of |
647 | | // expressions of certain types in C++. |
648 | 11.8M | if (getLangOpts().CPlusPlus && |
649 | 11.8M | (6.23M E->getType() == Context.OverloadTy6.23M || |
650 | 6.23M | T->isDependentType() || |
651 | 6.23M | T->isRecordType()6.22M )) |
652 | 38.3k | return E; |
653 | | |
654 | | // The C standard is actually really unclear on this point, and |
655 | | // DR106 tells us what the result should be but not why. It's |
656 | | // generally best to say that void types just doesn't undergo |
657 | | // lvalue-to-rvalue at all. Note that expressions of unqualified |
658 | | // 'void' type are never l-values, but qualified void can be. |
659 | 11.7M | if (T->isVoidType()) |
660 | 11 | return E; |
661 | | |
662 | | // OpenCL usually rejects direct accesses to values of 'half' type. |
663 | 11.7M | if (getLangOpts().OpenCL && |
664 | 11.7M | !getOpenCLOptions().isAvailableOption("cl_khr_fp16", getLangOpts())676k && |
665 | 11.7M | T->isHalfType()7.14k ) { |
666 | 6 | Diag(E->getExprLoc(), diag::err_opencl_half_load_store) |
667 | 6 | << 0 << T; |
668 | 6 | return ExprError(); |
669 | 6 | } |
670 | | |
671 | 11.7M | CheckForNullPointerDereference(*this, E); |
672 | 11.7M | if (const ObjCIsaExpr *OISA = dyn_cast<ObjCIsaExpr>(E->IgnoreParenCasts())) { |
673 | 45 | NamedDecl *ObjectGetClass = LookupSingleName(TUScope, |
674 | 45 | &Context.Idents.get("object_getClass"), |
675 | 45 | SourceLocation(), LookupOrdinaryName); |
676 | 45 | if (ObjectGetClass) |
677 | 6 | Diag(E->getExprLoc(), diag::warn_objc_isa_use) |
678 | 6 | << FixItHint::CreateInsertion(OISA->getBeginLoc(), "object_getClass(") |
679 | 6 | << FixItHint::CreateReplacement( |
680 | 6 | SourceRange(OISA->getOpLoc(), OISA->getIsaMemberLoc()), ")"); |
681 | 39 | else |
682 | 39 | Diag(E->getExprLoc(), diag::warn_objc_isa_use); |
683 | 45 | } |
684 | 11.7M | else if (const ObjCIvarRefExpr *OIRE = |
685 | 11.7M | dyn_cast<ObjCIvarRefExpr>(E->IgnoreParenCasts())) |
686 | 1.61k | DiagnoseDirectIsaAccess(*this, OIRE, SourceLocation(), /* Expr*/nullptr); |
687 | | |
688 | | // C++ [conv.lval]p1: |
689 | | // [...] If T is a non-class type, the type of the prvalue is the |
690 | | // cv-unqualified version of T. Otherwise, the type of the |
691 | | // rvalue is T. |
692 | | // |
693 | | // C99 6.3.2.1p2: |
694 | | // If the lvalue has qualified type, the value has the unqualified |
695 | | // version of the type of the lvalue; otherwise, the value has the |
696 | | // type of the lvalue. |
697 | 11.7M | if (T.hasQualifiers()) |
698 | 2.17M | T = T.getUnqualifiedType(); |
699 | | |
700 | | // Under the MS ABI, lock down the inheritance model now. |
701 | 11.7M | if (T->isMemberPointerType() && |
702 | 11.7M | Context.getTargetInfo().getCXXABI().isMicrosoft()1.14k ) |
703 | 119 | (void)isCompleteType(E->getExprLoc(), T); |
704 | | |
705 | 11.7M | ExprResult Res = CheckLValueToRValueConversionOperand(E); |
706 | 11.7M | if (Res.isInvalid()) |
707 | 0 | return Res; |
708 | 11.7M | E = Res.get(); |
709 | | |
710 | | // Loading a __weak object implicitly retains the value, so we need a cleanup to |
711 | | // balance that. |
712 | 11.7M | if (E->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
713 | 292 | Cleanup.setExprNeedsCleanups(true); |
714 | | |
715 | 11.7M | if (E->getType().isDestructedType() == QualType::DK_nontrivial_c_struct) |
716 | 126 | Cleanup.setExprNeedsCleanups(true); |
717 | | |
718 | | // C++ [conv.lval]p3: |
719 | | // If T is cv std::nullptr_t, the result is a null pointer constant. |
720 | 11.7M | CastKind CK = T->isNullPtrType() ? CK_NullToPointer410 : CK_LValueToRValue11.7M ; |
721 | 11.7M | Res = ImplicitCastExpr::Create(Context, T, CK, E, nullptr, VK_PRValue, |
722 | 11.7M | CurFPFeatureOverrides()); |
723 | | |
724 | | // C11 6.3.2.1p2: |
725 | | // ... if the lvalue has atomic type, the value has the non-atomic version |
726 | | // of the type of the lvalue ... |
727 | 11.7M | if (const AtomicType *Atomic = T->getAs<AtomicType>()) { |
728 | 212 | T = Atomic->getValueType().getUnqualifiedType(); |
729 | 212 | Res = ImplicitCastExpr::Create(Context, T, CK_AtomicToNonAtomic, Res.get(), |
730 | 212 | nullptr, VK_PRValue, FPOptionsOverride()); |
731 | 212 | } |
732 | | |
733 | 11.7M | return Res; |
734 | 11.7M | } |
735 | | |
736 | 27.1M | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E, bool Diagnose) { |
737 | 27.1M | ExprResult Res = DefaultFunctionArrayConversion(E, Diagnose); |
738 | 27.1M | if (Res.isInvalid()) |
739 | 39 | return ExprError(); |
740 | 27.1M | Res = DefaultLvalueConversion(Res.get()); |
741 | 27.1M | if (Res.isInvalid()) |
742 | 5 | return ExprError(); |
743 | 27.1M | return Res; |
744 | 27.1M | } |
745 | | |
746 | | /// CallExprUnaryConversions - a special case of an unary conversion |
747 | | /// performed on a function designator of a call expression. |
748 | 1.79M | ExprResult Sema::CallExprUnaryConversions(Expr *E) { |
749 | 1.79M | QualType Ty = E->getType(); |
750 | 1.79M | ExprResult Res = E; |
751 | | // Only do implicit cast for a function type, but not for a pointer |
752 | | // to function type. |
753 | 1.79M | if (Ty->isFunctionType()) { |
754 | 1.78M | Res = ImpCastExprToType(E, Context.getPointerType(Ty), |
755 | 1.78M | CK_FunctionToPointerDecay); |
756 | 1.78M | if (Res.isInvalid()) |
757 | 0 | return ExprError(); |
758 | 1.78M | } |
759 | 1.79M | Res = DefaultLvalueConversion(Res.get()); |
760 | 1.79M | if (Res.isInvalid()) |
761 | 16 | return ExprError(); |
762 | 1.79M | return Res.get(); |
763 | 1.79M | } |
764 | | |
765 | | /// UsualUnaryConversions - Performs various conversions that are common to most |
766 | | /// operators (C99 6.3). The conversions of array and function types are |
767 | | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
768 | | /// apply if the array is an argument to the sizeof or address (&) operators. |
769 | | /// In these instances, this routine should *not* be called. |
770 | 10.1M | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
771 | | // First, convert to an r-value. |
772 | 10.1M | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
773 | 10.1M | if (Res.isInvalid()) |
774 | 9 | return ExprError(); |
775 | 10.1M | E = Res.get(); |
776 | | |
777 | 10.1M | QualType Ty = E->getType(); |
778 | 10.1M | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
779 | | |
780 | 0 | LangOptions::FPEvalMethodKind EvalMethod = CurFPFeatures.getFPEvalMethod(); |
781 | 10.1M | if (EvalMethod != LangOptions::FEM_Source && Ty->isFloatingType()302k && |
782 | 10.1M | (4.49k getLangOpts().getFPEvalMethod() != |
783 | 4.49k | LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine || |
784 | 4.49k | PP.getLastFPEvalPragmaLocation().isValid()4.35k )) { |
785 | 249 | switch (EvalMethod) { |
786 | 0 | default: |
787 | 0 | llvm_unreachable("Unrecognized float evaluation method"); |
788 | 0 | break; |
789 | 0 | case LangOptions::FEM_UnsetOnCommandLine: |
790 | 0 | llvm_unreachable("Float evaluation method should be set by now"); |
791 | 0 | break; |
792 | 145 | case LangOptions::FEM_Double: |
793 | 145 | if (Context.getFloatingTypeOrder(Context.DoubleTy, Ty) > 0) |
794 | | // Widen the expression to double. |
795 | 111 | return Ty->isComplexType() |
796 | 111 | ? ImpCastExprToType(E, |
797 | 0 | Context.getComplexType(Context.DoubleTy), |
798 | 0 | CK_FloatingComplexCast) |
799 | 111 | : ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast); |
800 | 34 | break; |
801 | 104 | case LangOptions::FEM_Extended: |
802 | 104 | if (Context.getFloatingTypeOrder(Context.LongDoubleTy, Ty) > 0) |
803 | | // Widen the expression to long double. |
804 | 84 | return Ty->isComplexType() |
805 | 84 | ? ImpCastExprToType( |
806 | 0 | E, Context.getComplexType(Context.LongDoubleTy), |
807 | 0 | CK_FloatingComplexCast) |
808 | 84 | : ImpCastExprToType(E, Context.LongDoubleTy, |
809 | 84 | CK_FloatingCast); |
810 | 20 | break; |
811 | 249 | } |
812 | 249 | } |
813 | | |
814 | | // Half FP have to be promoted to float unless it is natively supported |
815 | 10.1M | if (Ty->isHalfType() && !getLangOpts().NativeHalfType1.48k ) |
816 | 1.00k | return ImpCastExprToType(Res.get(), Context.FloatTy, CK_FloatingCast); |
817 | | |
818 | | // Try to perform integral promotions if the object has a theoretically |
819 | | // promotable type. |
820 | 10.1M | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
821 | | // C99 6.3.1.1p2: |
822 | | // |
823 | | // The following may be used in an expression wherever an int or |
824 | | // unsigned int may be used: |
825 | | // - an object or expression with an integer type whose integer |
826 | | // conversion rank is less than or equal to the rank of int |
827 | | // and unsigned int. |
828 | | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
829 | | // |
830 | | // If an int can represent all values of the original type, the |
831 | | // value is converted to an int; otherwise, it is converted to an |
832 | | // unsigned int. These are called the integer promotions. All |
833 | | // other types are unchanged by the integer promotions. |
834 | | |
835 | 9.68M | QualType PTy = Context.isPromotableBitField(E); |
836 | 9.68M | if (!PTy.isNull()) { |
837 | 1.45k | E = ImpCastExprToType(E, PTy, CK_IntegralCast).get(); |
838 | 1.45k | return E; |
839 | 1.45k | } |
840 | 9.67M | if (Ty->isPromotableIntegerType()) { |
841 | 194k | QualType PT = Context.getPromotedIntegerType(Ty); |
842 | 194k | E = ImpCastExprToType(E, PT, CK_IntegralCast).get(); |
843 | 194k | return E; |
844 | 194k | } |
845 | 9.67M | } |
846 | 10.0M | return E; |
847 | 10.1M | } |
848 | | |
849 | | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
850 | | /// do not have a prototype. Arguments that have type float or __fp16 |
851 | | /// are promoted to double. All other argument types are converted by |
852 | | /// UsualUnaryConversions(). |
853 | 115k | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
854 | 115k | QualType Ty = E->getType(); |
855 | 115k | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
856 | | |
857 | 0 | ExprResult Res = UsualUnaryConversions(E); |
858 | 115k | if (Res.isInvalid()) |
859 | 0 | return ExprError(); |
860 | 115k | E = Res.get(); |
861 | | |
862 | | // If this is a 'float' or '__fp16' (CVR qualified or typedef) |
863 | | // promote to double. |
864 | | // Note that default argument promotion applies only to float (and |
865 | | // half/fp16); it does not apply to _Float16. |
866 | 115k | const BuiltinType *BTy = Ty->getAs<BuiltinType>(); |
867 | 115k | if (BTy && (78.5k BTy->getKind() == BuiltinType::Half78.5k || |
868 | 78.5k | BTy->getKind() == BuiltinType::Float78.5k )) { |
869 | 804 | if (getLangOpts().OpenCL && |
870 | 804 | !getOpenCLOptions().isAvailableOption("cl_khr_fp64", getLangOpts())6 ) { |
871 | 2 | if (BTy->getKind() == BuiltinType::Half) { |
872 | 0 | E = ImpCastExprToType(E, Context.FloatTy, CK_FloatingCast).get(); |
873 | 0 | } |
874 | 802 | } else { |
875 | 802 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).get(); |
876 | 802 | } |
877 | 804 | } |
878 | 115k | if (BTy && |
879 | 115k | getLangOpts().getExtendIntArgs() == |
880 | 78.5k | LangOptions::ExtendArgsKind::ExtendTo64 && |
881 | 115k | Context.getTargetInfo().supportsExtendIntArgs()62 && Ty->isIntegerType()16 && |
882 | 115k | Context.getTypeSizeInChars(BTy) < |
883 | 14 | Context.getTypeSizeInChars(Context.LongLongTy)) { |
884 | 12 | E = (Ty->isUnsignedIntegerType()) |
885 | 12 | ? ImpCastExprToType(E, Context.UnsignedLongLongTy, CK_IntegralCast) |
886 | 6 | .get() |
887 | 12 | : ImpCastExprToType(E, Context.LongLongTy, CK_IntegralCast).get()6 ; |
888 | 12 | assert(8 == Context.getTypeSizeInChars(Context.LongLongTy).getQuantity() && |
889 | 12 | "Unexpected typesize for LongLongTy"); |
890 | 12 | } |
891 | | |
892 | | // C++ performs lvalue-to-rvalue conversion as a default argument |
893 | | // promotion, even on class types, but note: |
894 | | // C++11 [conv.lval]p2: |
895 | | // When an lvalue-to-rvalue conversion occurs in an unevaluated |
896 | | // operand or a subexpression thereof the value contained in the |
897 | | // referenced object is not accessed. Otherwise, if the glvalue |
898 | | // has a class type, the conversion copy-initializes a temporary |
899 | | // of type T from the glvalue and the result of the conversion |
900 | | // is a prvalue for the temporary. |
901 | | // FIXME: add some way to gate this entire thing for correctness in |
902 | | // potentially potentially evaluated contexts. |
903 | 115k | if (getLangOpts().CPlusPlus && E->isGLValue()104k && !isUnevaluatedContext()250 ) { |
904 | 231 | ExprResult Temp = PerformCopyInitialization( |
905 | 231 | InitializedEntity::InitializeTemporary(E->getType()), |
906 | 231 | E->getExprLoc(), E); |
907 | 231 | if (Temp.isInvalid()) |
908 | 8 | return ExprError(); |
909 | 223 | E = Temp.get(); |
910 | 223 | } |
911 | | |
912 | 115k | return E; |
913 | 115k | } |
914 | | |
915 | | /// Determine the degree of POD-ness for an expression. |
916 | | /// Incomplete types are considered POD, since this check can be performed |
917 | | /// when we're in an unevaluated context. |
918 | 218k | Sema::VarArgKind Sema::isValidVarArgType(const QualType &Ty) { |
919 | 218k | if (Ty->isIncompleteType()) { |
920 | | // C++11 [expr.call]p7: |
921 | | // After these conversions, if the argument does not have arithmetic, |
922 | | // enumeration, pointer, pointer to member, or class type, the program |
923 | | // is ill-formed. |
924 | | // |
925 | | // Since we've already performed array-to-pointer and function-to-pointer |
926 | | // decay, the only such type in C++ is cv void. This also handles |
927 | | // initializer lists as variadic arguments. |
928 | 14 | if (Ty->isVoidType()) |
929 | 11 | return VAK_Invalid; |
930 | | |
931 | 3 | if (Ty->isObjCObjectType()) |
932 | 0 | return VAK_Invalid; |
933 | 3 | return VAK_Valid; |
934 | 3 | } |
935 | | |
936 | 218k | if (Ty.isDestructedType() == QualType::DK_nontrivial_c_struct) |
937 | 2 | return VAK_Invalid; |
938 | | |
939 | 218k | if (Ty.isCXX98PODType(Context)) |
940 | 218k | return VAK_Valid; |
941 | | |
942 | | // C++11 [expr.call]p7: |
943 | | // Passing a potentially-evaluated argument of class type (Clause 9) |
944 | | // having a non-trivial copy constructor, a non-trivial move constructor, |
945 | | // or a non-trivial destructor, with no corresponding parameter, |
946 | | // is conditionally-supported with implementation-defined semantics. |
947 | 483 | if (getLangOpts().CPlusPlus11 && !Ty->isDependentType()344 ) |
948 | 344 | if (CXXRecordDecl *Record = Ty->getAsCXXRecordDecl()) |
949 | 344 | if (!Record->hasNonTrivialCopyConstructor() && |
950 | 344 | !Record->hasNonTrivialMoveConstructor()284 && |
951 | 344 | !Record->hasNonTrivialDestructor()284 ) |
952 | 266 | return VAK_ValidInCXX11; |
953 | | |
954 | 217 | if (getLangOpts().ObjCAutoRefCount && Ty->isObjCLifetimeType()0 ) |
955 | 0 | return VAK_Valid; |
956 | | |
957 | 217 | if (Ty->isObjCObjectType()) |
958 | 10 | return VAK_Invalid; |
959 | | |
960 | 207 | if (getLangOpts().MSVCCompat) |
961 | 60 | return VAK_MSVCUndefined; |
962 | | |
963 | | // FIXME: In C++11, these cases are conditionally-supported, meaning we're |
964 | | // permitted to reject them. We should consider doing so. |
965 | 147 | return VAK_Undefined; |
966 | 207 | } |
967 | | |
968 | 100k | void Sema::checkVariadicArgument(const Expr *E, VariadicCallType CT) { |
969 | | // Don't allow one to pass an Objective-C interface to a vararg. |
970 | 100k | const QualType &Ty = E->getType(); |
971 | 100k | VarArgKind VAK = isValidVarArgType(Ty); |
972 | | |
973 | | // Complain about passing non-POD types through varargs. |
974 | 100k | switch (VAK) { |
975 | 121 | case VAK_ValidInCXX11: |
976 | 121 | DiagRuntimeBehavior( |
977 | 121 | E->getBeginLoc(), nullptr, |
978 | 121 | PDiag(diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg) << Ty << CT); |
979 | 121 | LLVM_FALLTHROUGH; |
980 | 100k | case VAK_Valid: |
981 | 100k | if (Ty->isRecordType()) { |
982 | | // This is unlikely to be what the user intended. If the class has a |
983 | | // 'c_str' member function, the user probably meant to call that. |
984 | 411 | DiagRuntimeBehavior(E->getBeginLoc(), nullptr, |
985 | 411 | PDiag(diag::warn_pass_class_arg_to_vararg) |
986 | 411 | << Ty << CT << hasCStrMethod(E) << ".c_str()"); |
987 | 411 | } |
988 | 100k | break; |
989 | | |
990 | 61 | case VAK_Undefined: |
991 | 91 | case VAK_MSVCUndefined: |
992 | 91 | DiagRuntimeBehavior(E->getBeginLoc(), nullptr, |
993 | 91 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
994 | 91 | << getLangOpts().CPlusPlus11 << Ty << CT); |
995 | 91 | break; |
996 | | |
997 | 7 | case VAK_Invalid: |
998 | 7 | if (Ty.isDestructedType() == QualType::DK_nontrivial_c_struct) |
999 | 1 | Diag(E->getBeginLoc(), |
1000 | 1 | diag::err_cannot_pass_non_trivial_c_struct_to_vararg) |
1001 | 1 | << Ty << CT; |
1002 | 6 | else if (Ty->isObjCObjectType()) |
1003 | 3 | DiagRuntimeBehavior(E->getBeginLoc(), nullptr, |
1004 | 3 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
1005 | 3 | << Ty << CT); |
1006 | 3 | else |
1007 | 3 | Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg) |
1008 | 3 | << isa<InitListExpr>(E) << Ty << CT; |
1009 | 7 | break; |
1010 | 100k | } |
1011 | 100k | } |
1012 | | |
1013 | | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
1014 | | /// will create a trap if the resulting type is not a POD type. |
1015 | | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
1016 | 114k | FunctionDecl *FDecl) { |
1017 | 114k | if (const BuiltinType *PlaceholderTy = E->getType()->getAsPlaceholderType()) { |
1018 | | // Strip the unbridged-cast placeholder expression off, if applicable. |
1019 | 15 | if (PlaceholderTy->getKind() == BuiltinType::ARCUnbridgedCast && |
1020 | 15 | (9 CT == VariadicMethod9 || |
1021 | 9 | (7 FDecl7 && FDecl->hasAttr<CFAuditedTransferAttr>()7 ))) { |
1022 | 5 | E = stripARCUnbridgedCast(E); |
1023 | | |
1024 | | // Otherwise, do normal placeholder checking. |
1025 | 10 | } else { |
1026 | 10 | ExprResult ExprRes = CheckPlaceholderExpr(E); |
1027 | 10 | if (ExprRes.isInvalid()) |
1028 | 3 | return ExprError(); |
1029 | 7 | E = ExprRes.get(); |
1030 | 7 | } |
1031 | 15 | } |
1032 | | |
1033 | 114k | ExprResult ExprRes = DefaultArgumentPromotion(E); |
1034 | 114k | if (ExprRes.isInvalid()) |
1035 | 8 | return ExprError(); |
1036 | | |
1037 | | // Copy blocks to the heap. |
1038 | 114k | if (ExprRes.get()->getType()->isBlockPointerType()) |
1039 | 21 | maybeExtendBlockObject(ExprRes); |
1040 | | |
1041 | 114k | E = ExprRes.get(); |
1042 | | |
1043 | | // Diagnostics regarding non-POD argument types are |
1044 | | // emitted along with format string checking in Sema::CheckFunctionCall(). |
1045 | 114k | if (isValidVarArgType(E->getType()) == VAK_Undefined) { |
1046 | | // Turn this into a trap. |
1047 | 76 | CXXScopeSpec SS; |
1048 | 76 | SourceLocation TemplateKWLoc; |
1049 | 76 | UnqualifiedId Name; |
1050 | 76 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
1051 | 76 | E->getBeginLoc()); |
1052 | 76 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, TemplateKWLoc, Name, |
1053 | 76 | /*HasTrailingLParen=*/true, |
1054 | 76 | /*IsAddressOfOperand=*/false); |
1055 | 76 | if (TrapFn.isInvalid()) |
1056 | 0 | return ExprError(); |
1057 | | |
1058 | 76 | ExprResult Call = BuildCallExpr(TUScope, TrapFn.get(), E->getBeginLoc(), |
1059 | 76 | None, E->getEndLoc()); |
1060 | 76 | if (Call.isInvalid()) |
1061 | 0 | return ExprError(); |
1062 | | |
1063 | 76 | ExprResult Comma = |
1064 | 76 | ActOnBinOp(TUScope, E->getBeginLoc(), tok::comma, Call.get(), E); |
1065 | 76 | if (Comma.isInvalid()) |
1066 | 0 | return ExprError(); |
1067 | 76 | return Comma.get(); |
1068 | 76 | } |
1069 | | |
1070 | 113k | if (!getLangOpts().CPlusPlus && |
1071 | 113k | RequireCompleteType(E->getExprLoc(), E->getType(), |
1072 | 9.52k | diag::err_call_incomplete_argument)) |
1073 | 2 | return ExprError(); |
1074 | | |
1075 | 113k | return E; |
1076 | 113k | } |
1077 | | |
1078 | | /// Converts an integer to complex float type. Helper function of |
1079 | | /// UsualArithmeticConversions() |
1080 | | /// |
1081 | | /// \return false if the integer expression is an integer type and is |
1082 | | /// successfully converted to the complex type. |
1083 | | static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr, |
1084 | | ExprResult &ComplexExpr, |
1085 | | QualType IntTy, |
1086 | | QualType ComplexTy, |
1087 | 1.31k | bool SkipCast) { |
1088 | 1.31k | if (IntTy->isComplexType() || IntTy->isRealFloatingType()657 ) return true1.23k ; |
1089 | 74 | if (SkipCast) return false0 ; |
1090 | 74 | if (IntTy->isIntegerType()) { |
1091 | 43 | QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType(); |
1092 | 43 | IntExpr = S.ImpCastExprToType(IntExpr.get(), fpTy, CK_IntegralToFloating); |
1093 | 43 | IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy, |
1094 | 43 | CK_FloatingRealToComplex); |
1095 | 43 | } else { |
1096 | 31 | assert(IntTy->isComplexIntegerType()); |
1097 | 0 | IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy, |
1098 | 31 | CK_IntegralComplexToFloatingComplex); |
1099 | 31 | } |
1100 | 0 | return false; |
1101 | 74 | } |
1102 | | |
1103 | | /// Handle arithmetic conversion with complex types. Helper function of |
1104 | | /// UsualArithmeticConversions() |
1105 | | static QualType handleComplexFloatConversion(Sema &S, ExprResult &LHS, |
1106 | | ExprResult &RHS, QualType LHSType, |
1107 | | QualType RHSType, |
1108 | 669 | bool IsCompAssign) { |
1109 | | // if we have an integer operand, the result is the complex type. |
1110 | 669 | if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType, |
1111 | 669 | /*skipCast*/false)) |
1112 | 28 | return LHSType; |
1113 | 641 | if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType, |
1114 | 641 | /*skipCast*/IsCompAssign)) |
1115 | 46 | return RHSType; |
1116 | | |
1117 | | // This handles complex/complex, complex/float, or float/complex. |
1118 | | // When both operands are complex, the shorter operand is converted to the |
1119 | | // type of the longer, and that is the type of the result. This corresponds |
1120 | | // to what is done when combining two real floating-point operands. |
1121 | | // The fun begins when size promotion occur across type domains. |
1122 | | // From H&S 6.3.4: When one operand is complex and the other is a real |
1123 | | // floating-point type, the less precise type is converted, within it's |
1124 | | // real or complex domain, to the precision of the other type. For example, |
1125 | | // when combining a "long double" with a "double _Complex", the |
1126 | | // "double _Complex" is promoted to "long double _Complex". |
1127 | | |
1128 | | // Compute the rank of the two types, regardless of whether they are complex. |
1129 | 595 | int Order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
1130 | | |
1131 | 595 | auto *LHSComplexType = dyn_cast<ComplexType>(LHSType); |
1132 | 595 | auto *RHSComplexType = dyn_cast<ComplexType>(RHSType); |
1133 | 595 | QualType LHSElementType = |
1134 | 595 | LHSComplexType ? LHSComplexType->getElementType()184 : LHSType411 ; |
1135 | 595 | QualType RHSElementType = |
1136 | 595 | RHSComplexType ? RHSComplexType->getElementType()423 : RHSType172 ; |
1137 | | |
1138 | 595 | QualType ResultType = S.Context.getComplexType(LHSElementType); |
1139 | 595 | if (Order < 0) { |
1140 | | // Promote the precision of the LHS if not an assignment. |
1141 | 21 | ResultType = S.Context.getComplexType(RHSElementType); |
1142 | 21 | if (!IsCompAssign) { |
1143 | 17 | if (LHSComplexType) |
1144 | 10 | LHS = |
1145 | 10 | S.ImpCastExprToType(LHS.get(), ResultType, CK_FloatingComplexCast); |
1146 | 7 | else |
1147 | 7 | LHS = S.ImpCastExprToType(LHS.get(), RHSElementType, CK_FloatingCast); |
1148 | 17 | } |
1149 | 574 | } else if (Order > 0) { |
1150 | | // Promote the precision of the RHS. |
1151 | 35 | if (RHSComplexType) |
1152 | 29 | RHS = S.ImpCastExprToType(RHS.get(), ResultType, CK_FloatingComplexCast); |
1153 | 6 | else |
1154 | 6 | RHS = S.ImpCastExprToType(RHS.get(), LHSElementType, CK_FloatingCast); |
1155 | 35 | } |
1156 | 595 | return ResultType; |
1157 | 641 | } |
1158 | | |
1159 | | /// Handle arithmetic conversion from integer to float. Helper function |
1160 | | /// of UsualArithmeticConversions() |
1161 | | static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, |
1162 | | ExprResult &IntExpr, |
1163 | | QualType FloatTy, QualType IntTy, |
1164 | 10.1k | bool ConvertFloat, bool ConvertInt) { |
1165 | 10.1k | if (IntTy->isIntegerType()) { |
1166 | 10.1k | if (ConvertInt) |
1167 | | // Convert intExpr to the lhs floating point type. |
1168 | 9.82k | IntExpr = S.ImpCastExprToType(IntExpr.get(), FloatTy, |
1169 | 9.82k | CK_IntegralToFloating); |
1170 | 10.1k | return FloatTy; |
1171 | 10.1k | } |
1172 | | |
1173 | | // Convert both sides to the appropriate complex float. |
1174 | 27 | assert(IntTy->isComplexIntegerType()); |
1175 | 0 | QualType result = S.Context.getComplexType(FloatTy); |
1176 | | |
1177 | | // _Complex int -> _Complex float |
1178 | 27 | if (ConvertInt) |
1179 | 27 | IntExpr = S.ImpCastExprToType(IntExpr.get(), result, |
1180 | 27 | CK_IntegralComplexToFloatingComplex); |
1181 | | |
1182 | | // float -> _Complex float |
1183 | 27 | if (ConvertFloat) |
1184 | 27 | FloatExpr = S.ImpCastExprToType(FloatExpr.get(), result, |
1185 | 27 | CK_FloatingRealToComplex); |
1186 | | |
1187 | 27 | return result; |
1188 | 10.1k | } |
1189 | | |
1190 | | /// Handle arithmethic conversion with floating point types. Helper |
1191 | | /// function of UsualArithmeticConversions() |
1192 | | static QualType handleFloatConversion(Sema &S, ExprResult &LHS, |
1193 | | ExprResult &RHS, QualType LHSType, |
1194 | 11.9k | QualType RHSType, bool IsCompAssign) { |
1195 | 11.9k | bool LHSFloat = LHSType->isRealFloatingType(); |
1196 | 11.9k | bool RHSFloat = RHSType->isRealFloatingType(); |
1197 | | |
1198 | | // N1169 4.1.4: If one of the operands has a floating type and the other |
1199 | | // operand has a fixed-point type, the fixed-point operand |
1200 | | // is converted to the floating type [...] |
1201 | 11.9k | if (LHSType->isFixedPointType() || RHSType->isFixedPointType()11.8k ) { |
1202 | 14 | if (LHSFloat) |
1203 | 4 | RHS = S.ImpCastExprToType(RHS.get(), LHSType, CK_FixedPointToFloating); |
1204 | 10 | else if (!IsCompAssign) |
1205 | 0 | LHS = S.ImpCastExprToType(LHS.get(), RHSType, CK_FixedPointToFloating); |
1206 | 14 | return LHSFloat ? LHSType4 : RHSType10 ; |
1207 | 14 | } |
1208 | | |
1209 | | // If we have two real floating types, convert the smaller operand |
1210 | | // to the bigger result. |
1211 | 11.8k | if (LHSFloat && RHSFloat7.02k ) { |
1212 | 1.73k | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
1213 | 1.73k | if (order > 0) { |
1214 | 151 | RHS = S.ImpCastExprToType(RHS.get(), LHSType, CK_FloatingCast); |
1215 | 151 | return LHSType; |
1216 | 151 | } |
1217 | | |
1218 | 1.58k | assert(order < 0 && "illegal float comparison"); |
1219 | 1.58k | if (!IsCompAssign) |
1220 | 635 | LHS = S.ImpCastExprToType(LHS.get(), RHSType, CK_FloatingCast); |
1221 | 1.58k | return RHSType; |
1222 | 1.73k | } |
1223 | | |
1224 | 10.1k | if (LHSFloat) { |
1225 | | // Half FP has to be promoted to float unless it is natively supported |
1226 | 5.29k | if (LHSType->isHalfType() && !S.getLangOpts().NativeHalfType110 ) |
1227 | 40 | LHSType = S.Context.FloatTy; |
1228 | | |
1229 | 5.29k | return handleIntToFloatConversion(S, LHS, RHS, LHSType, RHSType, |
1230 | 5.29k | /*ConvertFloat=*/!IsCompAssign, |
1231 | 5.29k | /*ConvertInt=*/ true); |
1232 | 5.29k | } |
1233 | 4.86k | assert(RHSFloat); |
1234 | 0 | return handleIntToFloatConversion(S, RHS, LHS, RHSType, LHSType, |
1235 | 4.86k | /*ConvertFloat=*/ true, |
1236 | 4.86k | /*ConvertInt=*/!IsCompAssign); |
1237 | 10.1k | } |
1238 | | |
1239 | | /// Diagnose attempts to convert between __float128, __ibm128 and |
1240 | | /// long double if there is no support for such conversion. |
1241 | | /// Helper function of UsualArithmeticConversions(). |
1242 | | static bool unsupportedTypeConversion(const Sema &S, QualType LHSType, |
1243 | 924k | QualType RHSType) { |
1244 | | // No issue if either is not a floating point type. |
1245 | 924k | if (!LHSType->isFloatingType() || !RHSType->isFloatingType()34.2k ) |
1246 | 911k | return false; |
1247 | | |
1248 | | // No issue if both have the same 128-bit float semantics. |
1249 | 12.0k | auto *LHSComplex = LHSType->getAs<ComplexType>(); |
1250 | 12.0k | auto *RHSComplex = RHSType->getAs<ComplexType>(); |
1251 | | |
1252 | 12.0k | QualType LHSElem = LHSComplex ? LHSComplex->getElementType()813 : LHSType11.2k ; |
1253 | 12.0k | QualType RHSElem = RHSComplex ? RHSComplex->getElementType()558 : RHSType11.5k ; |
1254 | | |
1255 | 12.0k | const llvm::fltSemantics &LHSSem = S.Context.getFloatTypeSemantics(LHSElem); |
1256 | 12.0k | const llvm::fltSemantics &RHSSem = S.Context.getFloatTypeSemantics(RHSElem); |
1257 | | |
1258 | 12.0k | if ((&LHSSem != &llvm::APFloat::PPCDoubleDouble() || |
1259 | 12.0k | &RHSSem != &llvm::APFloat::IEEEquad()49 ) && |
1260 | 12.0k | (12.0k &LHSSem != &llvm::APFloat::IEEEquad()12.0k || |
1261 | 12.0k | &RHSSem != &llvm::APFloat::PPCDoubleDouble()357 )) |
1262 | 12.0k | return false; |
1263 | | |
1264 | 45 | return true; |
1265 | 12.0k | } |
1266 | | |
1267 | | typedef ExprResult PerformCastFn(Sema &S, Expr *operand, QualType toType); |
1268 | | |
1269 | | namespace { |
1270 | | /// These helper callbacks are placed in an anonymous namespace to |
1271 | | /// permit their use as function template parameters. |
1272 | 504k | ExprResult doIntegralCast(Sema &S, Expr *op, QualType toType) { |
1273 | 504k | return S.ImpCastExprToType(op, toType, CK_IntegralCast); |
1274 | 504k | } |
1275 | | |
1276 | 106 | ExprResult doComplexIntegralCast(Sema &S, Expr *op, QualType toType) { |
1277 | 106 | return S.ImpCastExprToType(op, S.Context.getComplexType(toType), |
1278 | 106 | CK_IntegralComplexCast); |
1279 | 106 | } |
1280 | | } |
1281 | | |
1282 | | /// Handle integer arithmetic conversions. Helper function of |
1283 | | /// UsualArithmeticConversions() |
1284 | | template <PerformCastFn doLHSCast, PerformCastFn doRHSCast> |
1285 | | static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, |
1286 | | ExprResult &RHS, QualType LHSType, |
1287 | 504k | QualType RHSType, bool IsCompAssign) { |
1288 | | // The rules for this case are in C99 6.3.1.8 |
1289 | 504k | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); |
1290 | 504k | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); |
1291 | 504k | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); |
1292 | 504k | if (LHSSigned == RHSSigned) { |
1293 | | // Same signedness; use the higher-ranked type |
1294 | 103k | if (order >= 0) { |
1295 | 80.6k | RHS = (*doRHSCast)(S, RHS.get(), LHSType); |
1296 | 80.6k | return LHSType; |
1297 | 80.6k | } else if (23.1k !IsCompAssign23.1k ) |
1298 | 22.9k | LHS = (*doLHSCast)(S, LHS.get(), RHSType); |
1299 | 23.1k | return RHSType; |
1300 | 401k | } else if (order != (LHSSigned ? 199.9k : -1301k )) { |
1301 | | // The unsigned type has greater than or equal rank to the |
1302 | | // signed type, so use the unsigned type |
1303 | 396k | if (RHSSigned) { |
1304 | 298k | RHS = (*doRHSCast)(S, RHS.get(), LHSType); |
1305 | 298k | return LHSType; |
1306 | 298k | } else if (97.8k !IsCompAssign97.8k ) |
1307 | 97.6k | LHS = (*doLHSCast)(S, LHS.get(), RHSType); |
1308 | 97.8k | return RHSType; |
1309 | 396k | } else if (4.67k S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)4.67k ) { |
1310 | | // The two types are different widths; if we are here, that |
1311 | | // means the signed type is larger than the unsigned type, so |
1312 | | // use the signed type. |
1313 | 4.35k | if (LHSSigned) { |
1314 | 1.80k | RHS = (*doRHSCast)(S, RHS.get(), LHSType); |
1315 | 1.80k | return LHSType; |
1316 | 2.55k | } else if (!IsCompAssign) |
1317 | 2.54k | LHS = (*doLHSCast)(S, LHS.get(), RHSType); |
1318 | 2.55k | return RHSType; |
1319 | 4.35k | } else { |
1320 | | // The signed type is higher-ranked than the unsigned type, |
1321 | | // but isn't actually any bigger (like unsigned int and long |
1322 | | // on most 32-bit systems). Use the unsigned type corresponding |
1323 | | // to the signed type. |
1324 | 325 | QualType result = |
1325 | 325 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType241 : RHSType84 ); |
1326 | 325 | RHS = (*doRHSCast)(S, RHS.get(), result); |
1327 | 325 | if (!IsCompAssign) |
1328 | 325 | LHS = (*doLHSCast)(S, LHS.get(), result); |
1329 | 325 | return result; |
1330 | 325 | } |
1331 | 504k | } SemaExpr.cpp:clang::QualType handleIntegerConversion<&((anonymous namespace)::doComplexIntegralCast(clang::Sema&, clang::Expr*, clang::QualType)), &((anonymous namespace)::doComplexIntegralCast(clang::Sema&, clang::Expr*, clang::QualType))>(clang::Sema&, clang::ActionResult<clang::Expr*, true>&, clang::ActionResult<clang::Expr*, true>&, clang::QualType, clang::QualType, bool) Line | Count | Source | 1287 | 25 | QualType RHSType, bool IsCompAssign) { | 1288 | | // The rules for this case are in C99 6.3.1.8 | 1289 | 25 | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); | 1290 | 25 | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); | 1291 | 25 | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); | 1292 | 25 | if (LHSSigned == RHSSigned) { | 1293 | | // Same signedness; use the higher-ranked type | 1294 | 24 | if (order >= 0) { | 1295 | 14 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1296 | 14 | return LHSType; | 1297 | 14 | } else if (10 !IsCompAssign10 ) | 1298 | 9 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1299 | 10 | return RHSType; | 1300 | 24 | } else if (1 order != (1 LHSSigned1 ? 10 : -11 )) { | 1301 | | // The unsigned type has greater than or equal rank to the | 1302 | | // signed type, so use the unsigned type | 1303 | 1 | if (RHSSigned) { | 1304 | 1 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1305 | 1 | return LHSType; | 1306 | 1 | } else if (0 !IsCompAssign0 ) | 1307 | 0 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1308 | 0 | return RHSType; | 1309 | 1 | } else if (0 S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)0 ) { | 1310 | | // The two types are different widths; if we are here, that | 1311 | | // means the signed type is larger than the unsigned type, so | 1312 | | // use the signed type. | 1313 | 0 | if (LHSSigned) { | 1314 | 0 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1315 | 0 | return LHSType; | 1316 | 0 | } else if (!IsCompAssign) | 1317 | 0 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1318 | 0 | return RHSType; | 1319 | 0 | } else { | 1320 | | // The signed type is higher-ranked than the unsigned type, | 1321 | | // but isn't actually any bigger (like unsigned int and long | 1322 | | // on most 32-bit systems). Use the unsigned type corresponding | 1323 | | // to the signed type. | 1324 | 0 | QualType result = | 1325 | 0 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); | 1326 | 0 | RHS = (*doRHSCast)(S, RHS.get(), result); | 1327 | 0 | if (!IsCompAssign) | 1328 | 0 | LHS = (*doLHSCast)(S, LHS.get(), result); | 1329 | 0 | return result; | 1330 | 0 | } | 1331 | 25 | } |
SemaExpr.cpp:clang::QualType handleIntegerConversion<&((anonymous namespace)::doComplexIntegralCast(clang::Sema&, clang::Expr*, clang::QualType)), &((anonymous namespace)::doIntegralCast(clang::Sema&, clang::Expr*, clang::QualType))>(clang::Sema&, clang::ActionResult<clang::Expr*, true>&, clang::ActionResult<clang::Expr*, true>&, clang::QualType, clang::QualType, bool) Line | Count | Source | 1287 | 61 | QualType RHSType, bool IsCompAssign) { | 1288 | | // The rules for this case are in C99 6.3.1.8 | 1289 | 61 | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); | 1290 | 61 | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); | 1291 | 61 | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); | 1292 | 61 | if (LHSSigned == RHSSigned) { | 1293 | | // Same signedness; use the higher-ranked type | 1294 | 53 | if (order >= 0) { | 1295 | 46 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1296 | 46 | return LHSType; | 1297 | 46 | } else if (7 !IsCompAssign7 ) | 1298 | 6 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1299 | 7 | return RHSType; | 1300 | 53 | } else if (8 order != (8 LHSSigned8 ? 12 : -16 )) { | 1301 | | // The unsigned type has greater than or equal rank to the | 1302 | | // signed type, so use the unsigned type | 1303 | 5 | if (RHSSigned) { | 1304 | 3 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1305 | 3 | return LHSType; | 1306 | 3 | } else if (2 !IsCompAssign2 ) | 1307 | 2 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1308 | 2 | return RHSType; | 1309 | 5 | } else if (3 S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)3 ) { | 1310 | | // The two types are different widths; if we are here, that | 1311 | | // means the signed type is larger than the unsigned type, so | 1312 | | // use the signed type. | 1313 | 3 | if (LHSSigned) { | 1314 | 0 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1315 | 0 | return LHSType; | 1316 | 3 | } else if (!IsCompAssign) | 1317 | 3 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1318 | 3 | return RHSType; | 1319 | 3 | } else { | 1320 | | // The signed type is higher-ranked than the unsigned type, | 1321 | | // but isn't actually any bigger (like unsigned int and long | 1322 | | // on most 32-bit systems). Use the unsigned type corresponding | 1323 | | // to the signed type. | 1324 | 0 | QualType result = | 1325 | 0 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); | 1326 | 0 | RHS = (*doRHSCast)(S, RHS.get(), result); | 1327 | 0 | if (!IsCompAssign) | 1328 | 0 | LHS = (*doLHSCast)(S, LHS.get(), result); | 1329 | 0 | return result; | 1330 | 0 | } | 1331 | 61 | } |
SemaExpr.cpp:clang::QualType handleIntegerConversion<&((anonymous namespace)::doIntegralCast(clang::Sema&, clang::Expr*, clang::QualType)), &((anonymous namespace)::doComplexIntegralCast(clang::Sema&, clang::Expr*, clang::QualType))>(clang::Sema&, clang::ActionResult<clang::Expr*, true>&, clang::ActionResult<clang::Expr*, true>&, clang::QualType, clang::QualType, bool) Line | Count | Source | 1287 | 78 | QualType RHSType, bool IsCompAssign) { | 1288 | | // The rules for this case are in C99 6.3.1.8 | 1289 | 78 | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); | 1290 | 78 | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); | 1291 | 78 | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); | 1292 | 78 | if (LHSSigned == RHSSigned) { | 1293 | | // Same signedness; use the higher-ranked type | 1294 | 68 | if (order >= 0) { | 1295 | 66 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1296 | 66 | return LHSType; | 1297 | 66 | } else if (2 !IsCompAssign2 ) | 1298 | 2 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1299 | 2 | return RHSType; | 1300 | 68 | } else if (10 order != (10 LHSSigned10 ? 18 : -12 )) { | 1301 | | // The unsigned type has greater than or equal rank to the | 1302 | | // signed type, so use the unsigned type | 1303 | 7 | if (RHSSigned) { | 1304 | 2 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1305 | 2 | return LHSType; | 1306 | 5 | } else if (!IsCompAssign) | 1307 | 5 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1308 | 5 | return RHSType; | 1309 | 7 | } else if (3 S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)3 ) { | 1310 | | // The two types are different widths; if we are here, that | 1311 | | // means the signed type is larger than the unsigned type, so | 1312 | | // use the signed type. | 1313 | 3 | if (LHSSigned) { | 1314 | 3 | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1315 | 3 | return LHSType; | 1316 | 3 | } else if (0 !IsCompAssign0 ) | 1317 | 0 | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1318 | 0 | return RHSType; | 1319 | 3 | } else { | 1320 | | // The signed type is higher-ranked than the unsigned type, | 1321 | | // but isn't actually any bigger (like unsigned int and long | 1322 | | // on most 32-bit systems). Use the unsigned type corresponding | 1323 | | // to the signed type. | 1324 | 0 | QualType result = | 1325 | 0 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); | 1326 | 0 | RHS = (*doRHSCast)(S, RHS.get(), result); | 1327 | 0 | if (!IsCompAssign) | 1328 | 0 | LHS = (*doLHSCast)(S, LHS.get(), result); | 1329 | 0 | return result; | 1330 | 0 | } | 1331 | 78 | } |
SemaExpr.cpp:clang::QualType handleIntegerConversion<&((anonymous namespace)::doIntegralCast(clang::Sema&, clang::Expr*, clang::QualType)), &((anonymous namespace)::doIntegralCast(clang::Sema&, clang::Expr*, clang::QualType))>(clang::Sema&, clang::ActionResult<clang::Expr*, true>&, clang::ActionResult<clang::Expr*, true>&, clang::QualType, clang::QualType, bool) Line | Count | Source | 1287 | 504k | QualType RHSType, bool IsCompAssign) { | 1288 | | // The rules for this case are in C99 6.3.1.8 | 1289 | 504k | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); | 1290 | 504k | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); | 1291 | 504k | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); | 1292 | 504k | if (LHSSigned == RHSSigned) { | 1293 | | // Same signedness; use the higher-ranked type | 1294 | 103k | if (order >= 0) { | 1295 | 80.5k | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1296 | 80.5k | return LHSType; | 1297 | 80.5k | } else if (23.1k !IsCompAssign23.1k ) | 1298 | 22.9k | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1299 | 23.1k | return RHSType; | 1300 | 401k | } else if (order != (LHSSigned ? 199.9k : -1301k )) { | 1301 | | // The unsigned type has greater than or equal rank to the | 1302 | | // signed type, so use the unsigned type | 1303 | 396k | if (RHSSigned) { | 1304 | 298k | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1305 | 298k | return LHSType; | 1306 | 298k | } else if (97.8k !IsCompAssign97.8k ) | 1307 | 97.6k | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1308 | 97.8k | return RHSType; | 1309 | 396k | } else if (4.67k S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)4.67k ) { | 1310 | | // The two types are different widths; if we are here, that | 1311 | | // means the signed type is larger than the unsigned type, so | 1312 | | // use the signed type. | 1313 | 4.34k | if (LHSSigned) { | 1314 | 1.79k | RHS = (*doRHSCast)(S, RHS.get(), LHSType); | 1315 | 1.79k | return LHSType; | 1316 | 2.54k | } else if (!IsCompAssign) | 1317 | 2.54k | LHS = (*doLHSCast)(S, LHS.get(), RHSType); | 1318 | 2.54k | return RHSType; | 1319 | 4.34k | } else { | 1320 | | // The signed type is higher-ranked than the unsigned type, | 1321 | | // but isn't actually any bigger (like unsigned int and long | 1322 | | // on most 32-bit systems). Use the unsigned type corresponding | 1323 | | // to the signed type. | 1324 | 325 | QualType result = | 1325 | 325 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType241 : RHSType84 ); | 1326 | 325 | RHS = (*doRHSCast)(S, RHS.get(), result); | 1327 | 325 | if (!IsCompAssign) | 1328 | 325 | LHS = (*doLHSCast)(S, LHS.get(), result); | 1329 | 325 | return result; | 1330 | 325 | } | 1331 | 504k | } |
|
1332 | | |
1333 | | /// Handle conversions with GCC complex int extension. Helper function |
1334 | | /// of UsualArithmeticConversions() |
1335 | | static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, |
1336 | | ExprResult &RHS, QualType LHSType, |
1337 | | QualType RHSType, |
1338 | 164 | bool IsCompAssign) { |
1339 | 164 | const ComplexType *LHSComplexInt = LHSType->getAsComplexIntegerType(); |
1340 | 164 | const ComplexType *RHSComplexInt = RHSType->getAsComplexIntegerType(); |
1341 | | |
1342 | 164 | if (LHSComplexInt && RHSComplexInt86 ) { |
1343 | 25 | QualType LHSEltType = LHSComplexInt->getElementType(); |
1344 | 25 | QualType RHSEltType = RHSComplexInt->getElementType(); |
1345 | 25 | QualType ScalarType = |
1346 | 25 | handleIntegerConversion<doComplexIntegralCast, doComplexIntegralCast> |
1347 | 25 | (S, LHS, RHS, LHSEltType, RHSEltType, IsCompAssign); |
1348 | | |
1349 | 25 | return S.Context.getComplexType(ScalarType); |
1350 | 25 | } |
1351 | | |
1352 | 139 | if (LHSComplexInt) { |
1353 | 61 | QualType LHSEltType = LHSComplexInt->getElementType(); |
1354 | 61 | QualType ScalarType = |
1355 | 61 | handleIntegerConversion<doComplexIntegralCast, doIntegralCast> |
1356 | 61 | (S, LHS, RHS, LHSEltType, RHSType, IsCompAssign); |
1357 | 61 | QualType ComplexType = S.Context.getComplexType(ScalarType); |
1358 | 61 | RHS = S.ImpCastExprToType(RHS.get(), ComplexType, |
1359 | 61 | CK_IntegralRealToComplex); |
1360 | | |
1361 | 61 | return ComplexType; |
1362 | 61 | } |
1363 | | |
1364 | 78 | assert(RHSComplexInt); |
1365 | | |
1366 | 0 | QualType RHSEltType = RHSComplexInt->getElementType(); |
1367 | 78 | QualType ScalarType = |
1368 | 78 | handleIntegerConversion<doIntegralCast, doComplexIntegralCast> |
1369 | 78 | (S, LHS, RHS, LHSType, RHSEltType, IsCompAssign); |
1370 | 78 | QualType ComplexType = S.Context.getComplexType(ScalarType); |
1371 | | |
1372 | 78 | if (!IsCompAssign) |
1373 | 76 | LHS = S.ImpCastExprToType(LHS.get(), ComplexType, |
1374 | 76 | CK_IntegralRealToComplex); |
1375 | 78 | return ComplexType; |
1376 | 139 | } |
1377 | | |
1378 | | /// Return the rank of a given fixed point or integer type. The value itself |
1379 | | /// doesn't matter, but the values must be increasing with proper increasing |
1380 | | /// rank as described in N1169 4.1.1. |
1381 | 1.05k | static unsigned GetFixedPointRank(QualType Ty) { |
1382 | 1.05k | const auto *BTy = Ty->getAs<BuiltinType>(); |
1383 | 1.05k | assert(BTy && "Expected a builtin type."); |
1384 | | |
1385 | 0 | switch (BTy->getKind()) { |
1386 | 53 | case BuiltinType::ShortFract: |
1387 | 61 | case BuiltinType::UShortFract: |
1388 | 65 | case BuiltinType::SatShortFract: |
1389 | 65 | case BuiltinType::SatUShortFract: |
1390 | 65 | return 1; |
1391 | 26 | case BuiltinType::Fract: |
1392 | 36 | case BuiltinType::UFract: |
1393 | 38 | case BuiltinType::SatFract: |
1394 | 38 | case BuiltinType::SatUFract: |
1395 | 38 | return 2; |
1396 | 45 | case BuiltinType::LongFract: |
1397 | 45 | case BuiltinType::ULongFract: |
1398 | 45 | case BuiltinType::SatLongFract: |
1399 | 45 | case BuiltinType::SatULongFract: |
1400 | 45 | return 3; |
1401 | 321 | case BuiltinType::ShortAccum: |
1402 | 411 | case BuiltinType::UShortAccum: |
1403 | 475 | case BuiltinType::SatShortAccum: |
1404 | 523 | case BuiltinType::SatUShortAccum: |
1405 | 523 | return 4; |
1406 | 93 | case BuiltinType::Accum: |
1407 | 117 | case BuiltinType::UAccum: |
1408 | 129 | case BuiltinType::SatAccum: |
1409 | 129 | case BuiltinType::SatUAccum: |
1410 | 129 | return 5; |
1411 | 14 | case BuiltinType::LongAccum: |
1412 | 20 | case BuiltinType::ULongAccum: |
1413 | 20 | case BuiltinType::SatLongAccum: |
1414 | 22 | case BuiltinType::SatULongAccum: |
1415 | 22 | return 6; |
1416 | 236 | default: |
1417 | 236 | if (BTy->isInteger()) |
1418 | 236 | return 0; |
1419 | 1.05k | llvm_unreachable0 ("Unexpected fixed point or integer type"); |
1420 | 1.05k | } |
1421 | 1.05k | } |
1422 | | |
1423 | | /// handleFixedPointConversion - Fixed point operations between fixed |
1424 | | /// point types and integers or other fixed point types do not fall under |
1425 | | /// usual arithmetic conversion since these conversions could result in loss |
1426 | | /// of precsision (N1169 4.1.4). These operations should be calculated with |
1427 | | /// the full precision of their result type (N1169 4.1.6.2.1). |
1428 | | static QualType handleFixedPointConversion(Sema &S, QualType LHSTy, |
1429 | 529 | QualType RHSTy) { |
1430 | 529 | assert((LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) && |
1431 | 529 | "Expected at least one of the operands to be a fixed point type"); |
1432 | 0 | assert((LHSTy->isFixedPointOrIntegerType() || |
1433 | 529 | RHSTy->isFixedPointOrIntegerType()) && |
1434 | 529 | "Special fixed point arithmetic operation conversions are only " |
1435 | 529 | "applied to ints or other fixed point types"); |
1436 | | |
1437 | | // If one operand has signed fixed-point type and the other operand has |
1438 | | // unsigned fixed-point type, then the unsigned fixed-point operand is |
1439 | | // converted to its corresponding signed fixed-point type and the resulting |
1440 | | // type is the type of the converted operand. |
1441 | 529 | if (RHSTy->isSignedFixedPointType() && LHSTy->isUnsignedFixedPointType()194 ) |
1442 | 12 | LHSTy = S.Context.getCorrespondingSignedFixedPointType(LHSTy); |
1443 | 517 | else if (RHSTy->isUnsignedFixedPointType() && LHSTy->isSignedFixedPointType()148 ) |
1444 | 76 | RHSTy = S.Context.getCorrespondingSignedFixedPointType(RHSTy); |
1445 | | |
1446 | | // The result type is the type with the highest rank, whereby a fixed-point |
1447 | | // conversion rank is always greater than an integer conversion rank; if the |
1448 | | // type of either of the operands is a saturating fixedpoint type, the result |
1449 | | // type shall be the saturating fixed-point type corresponding to the type |
1450 | | // with the highest rank; the resulting value is converted (taking into |
1451 | | // account rounding and overflow) to the precision of the resulting type. |
1452 | | // Same ranks between signed and unsigned types are resolved earlier, so both |
1453 | | // types are either signed or both unsigned at this point. |
1454 | 529 | unsigned LHSTyRank = GetFixedPointRank(LHSTy); |
1455 | 529 | unsigned RHSTyRank = GetFixedPointRank(RHSTy); |
1456 | | |
1457 | 529 | QualType ResultTy = LHSTyRank > RHSTyRank ? LHSTy280 : RHSTy249 ; |
1458 | | |
1459 | 529 | if (LHSTy->isSaturatedFixedPointType() || RHSTy->isSaturatedFixedPointType()435 ) |
1460 | 126 | ResultTy = S.Context.getCorrespondingSaturatedType(ResultTy); |
1461 | | |
1462 | 529 | return ResultTy; |
1463 | 529 | } |
1464 | | |
1465 | | /// Check that the usual arithmetic conversions can be performed on this pair of |
1466 | | /// expressions that might be of enumeration type. |
1467 | | static void checkEnumArithmeticConversions(Sema &S, Expr *LHS, Expr *RHS, |
1468 | | SourceLocation Loc, |
1469 | 4.19M | Sema::ArithConvKind ACK) { |
1470 | | // C++2a [expr.arith.conv]p1: |
1471 | | // If one operand is of enumeration type and the other operand is of a |
1472 | | // different enumeration type or a floating-point type, this behavior is |
1473 | | // deprecated ([depr.arith.conv.enum]). |
1474 | | // |
1475 | | // Warn on this in all language modes. Produce a deprecation warning in C++20. |
1476 | | // Eventually we will presumably reject these cases (in C++23 onwards?). |
1477 | 4.19M | QualType L = LHS->getType(), R = RHS->getType(); |
1478 | 4.19M | bool LEnum = L->isUnscopedEnumerationType(), |
1479 | 4.19M | REnum = R->isUnscopedEnumerationType(); |
1480 | 4.19M | bool IsCompAssign = ACK == Sema::ACK_CompAssign; |
1481 | 4.19M | if ((!IsCompAssign && LEnum4.13M && R->isFloatingType()33.3k ) || |
1482 | 4.19M | (4.19M REnum4.19M && L->isFloatingType()35.3k )) { |
1483 | 40 | S.Diag(Loc, S.getLangOpts().CPlusPlus20 |
1484 | 40 | ? diag::warn_arith_conv_enum_float_cxx2014 |
1485 | 40 | : diag::warn_arith_conv_enum_float26 ) |
1486 | 40 | << LHS->getSourceRange() << RHS->getSourceRange() |
1487 | 40 | << (int)ACK << LEnum << L << R; |
1488 | 4.19M | } else if (!IsCompAssign && LEnum4.13M && REnum33.3k && |
1489 | 4.19M | !S.Context.hasSameUnqualifiedType(L, R)28.6k ) { |
1490 | 1.92k | unsigned DiagID; |
1491 | 1.92k | if (!L->castAs<EnumType>()->getDecl()->hasNameForLinkage() || |
1492 | 1.92k | !R->castAs<EnumType>()->getDecl()->hasNameForLinkage()307 ) { |
1493 | | // If either enumeration type is unnamed, it's less likely that the |
1494 | | // user cares about this, but this situation is still deprecated in |
1495 | | // C++2a. Use a different warning group. |
1496 | 1.62k | DiagID = S.getLangOpts().CPlusPlus20 |
1497 | 1.62k | ? diag::warn_arith_conv_mixed_anon_enum_types_cxx200 |
1498 | 1.62k | : diag::warn_arith_conv_mixed_anon_enum_types; |
1499 | 1.62k | } else if (301 ACK == Sema::ACK_Conditional301 ) { |
1500 | | // Conditional expressions are separated out because they have |
1501 | | // historically had a different warning flag. |
1502 | 13 | DiagID = S.getLangOpts().CPlusPlus20 |
1503 | 13 | ? diag::warn_conditional_mixed_enum_types_cxx202 |
1504 | 13 | : diag::warn_conditional_mixed_enum_types11 ; |
1505 | 288 | } else if (ACK == Sema::ACK_Comparison) { |
1506 | | // Comparison expressions are separated out because they have |
1507 | | // historically had a different warning flag. |
1508 | 284 | DiagID = S.getLangOpts().CPlusPlus20 |
1509 | 284 | ? diag::warn_comparison_mixed_enum_types_cxx203 |
1510 | 284 | : diag::warn_comparison_mixed_enum_types281 ; |
1511 | 284 | } else { |
1512 | 4 | DiagID = S.getLangOpts().CPlusPlus20 |
1513 | 4 | ? diag::warn_arith_conv_mixed_enum_types_cxx202 |
1514 | 4 | : diag::warn_arith_conv_mixed_enum_types2 ; |
1515 | 4 | } |
1516 | 1.92k | S.Diag(Loc, DiagID) << LHS->getSourceRange() << RHS->getSourceRange() |
1517 | 1.92k | << (int)ACK << L << R; |
1518 | 1.92k | } |
1519 | 4.19M | } |
1520 | | |
1521 | | /// UsualArithmeticConversions - Performs various conversions that are common to |
1522 | | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
1523 | | /// routine returns the first non-arithmetic type found. The client is |
1524 | | /// responsible for emitting appropriate error diagnostics. |
1525 | | QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, |
1526 | | SourceLocation Loc, |
1527 | 4.19M | ArithConvKind ACK) { |
1528 | 4.19M | checkEnumArithmeticConversions(*this, LHS.get(), RHS.get(), Loc, ACK); |
1529 | | |
1530 | 4.19M | if (ACK != ACK_CompAssign) { |
1531 | 4.13M | LHS = UsualUnaryConversions(LHS.get()); |
1532 | 4.13M | if (LHS.isInvalid()) |
1533 | 7 | return QualType(); |
1534 | 4.13M | } |
1535 | | |
1536 | 4.19M | RHS = UsualUnaryConversions(RHS.get()); |
1537 | 4.19M | if (RHS.isInvalid()) |
1538 | 2 | return QualType(); |
1539 | | |
1540 | | // For conversion purposes, we ignore any qualifiers. |
1541 | | // For example, "const float" and "float" are equivalent. |
1542 | 4.19M | QualType LHSType = |
1543 | 4.19M | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
1544 | 4.19M | QualType RHSType = |
1545 | 4.19M | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
1546 | | |
1547 | | // For conversion purposes, we ignore any atomic qualifier on the LHS. |
1548 | 4.19M | if (const AtomicType *AtomicLHS = LHSType->getAs<AtomicType>()) |
1549 | 70 | LHSType = AtomicLHS->getValueType(); |
1550 | | |
1551 | | // If both types are identical, no conversion is needed. |
1552 | 4.19M | if (LHSType == RHSType) |
1553 | 3.60M | return LHSType; |
1554 | | |
1555 | | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
1556 | | // The caller can deal with this (e.g. pointer + int). |
1557 | 595k | if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType()522k ) |
1558 | 73.6k | return QualType(); |
1559 | | |
1560 | | // Apply unary and bitfield promotions to the LHS's type. |
1561 | 522k | QualType LHSUnpromotedType = LHSType; |
1562 | 522k | if (LHSType->isPromotableIntegerType()) |
1563 | 4.15k | LHSType = Context.getPromotedIntegerType(LHSType); |
1564 | 522k | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(LHS.get()); |
1565 | 522k | if (!LHSBitfieldPromoteTy.isNull()) |
1566 | 151 | LHSType = LHSBitfieldPromoteTy; |
1567 | 522k | if (LHSType != LHSUnpromotedType && ACK != ACK_CompAssign4.20k ) |
1568 | 0 | LHS = ImpCastExprToType(LHS.get(), LHSType, CK_IntegralCast); |
1569 | | |
1570 | | // If both types are identical, no conversion is needed. |
1571 | 522k | if (LHSType == RHSType) |
1572 | 3.96k | return LHSType; |
1573 | | |
1574 | | // At this point, we have two different arithmetic types. |
1575 | | |
1576 | | // Diagnose attempts to convert between __ibm128, __float128 and long double |
1577 | | // where such conversions currently can't be handled. |
1578 | 518k | if (unsupportedTypeConversion(*this, LHSType, RHSType)) |
1579 | 28 | return QualType(); |
1580 | | |
1581 | | // Handle complex types first (C99 6.3.1.8p1). |
1582 | 518k | if (LHSType->isComplexType() || RHSType->isComplexType()517k ) |
1583 | 669 | return handleComplexFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
1584 | 669 | ACK == ACK_CompAssign); |
1585 | | |
1586 | | // Now handle "real" floating types (i.e. float, double, long double). |
1587 | 517k | if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()510k ) |
1588 | 11.8k | return handleFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
1589 | 11.8k | ACK == ACK_CompAssign); |
1590 | | |
1591 | | // Handle GCC complex int extension. |
1592 | 505k | if (LHSType->isComplexIntegerType() || RHSType->isComplexIntegerType()505k ) |
1593 | 164 | return handleComplexIntConversion(*this, LHS, RHS, LHSType, RHSType, |
1594 | 164 | ACK == ACK_CompAssign); |
1595 | | |
1596 | 505k | if (LHSType->isFixedPointType() || RHSType->isFixedPointType()504k ) |
1597 | 529 | return handleFixedPointConversion(*this, LHSType, RHSType); |
1598 | | |
1599 | | // Finally, we have two differing integer types. |
1600 | 504k | return handleIntegerConversion<doIntegralCast, doIntegralCast> |
1601 | 504k | (*this, LHS, RHS, LHSType, RHSType, ACK == ACK_CompAssign); |
1602 | 505k | } |
1603 | | |
1604 | | //===----------------------------------------------------------------------===// |
1605 | | // Semantic Analysis for various Expression Types |
1606 | | //===----------------------------------------------------------------------===// |
1607 | | |
1608 | | |
1609 | | ExprResult |
1610 | | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
1611 | | SourceLocation DefaultLoc, |
1612 | | SourceLocation RParenLoc, |
1613 | | Expr *ControllingExpr, |
1614 | | ArrayRef<ParsedType> ArgTypes, |
1615 | 308 | ArrayRef<Expr *> ArgExprs) { |
1616 | 308 | unsigned NumAssocs = ArgTypes.size(); |
1617 | 308 | assert(NumAssocs == ArgExprs.size()); |
1618 | | |
1619 | 0 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
1620 | 902 | for (unsigned i = 0; i < NumAssocs; ++i594 ) { |
1621 | 594 | if (ArgTypes[i]) |
1622 | 486 | (void) GetTypeFromParser(ArgTypes[i], &Types[i]); |
1623 | 108 | else |
1624 | 108 | Types[i] = nullptr; |
1625 | 594 | } |
1626 | | |
1627 | 308 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
1628 | 308 | ControllingExpr, |
1629 | 308 | llvm::makeArrayRef(Types, NumAssocs), |
1630 | 308 | ArgExprs); |
1631 | 308 | delete [] Types; |
1632 | 308 | return ER; |
1633 | 308 | } |
1634 | | |
1635 | | ExprResult |
1636 | | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
1637 | | SourceLocation DefaultLoc, |
1638 | | SourceLocation RParenLoc, |
1639 | | Expr *ControllingExpr, |
1640 | | ArrayRef<TypeSourceInfo *> Types, |
1641 | 325 | ArrayRef<Expr *> Exprs) { |
1642 | 325 | unsigned NumAssocs = Types.size(); |
1643 | 325 | assert(NumAssocs == Exprs.size()); |
1644 | | |
1645 | | // Decay and strip qualifiers for the controlling expression type, and handle |
1646 | | // placeholder type replacement. See committee discussion from WG14 DR423. |
1647 | 0 | { |
1648 | 325 | EnterExpressionEvaluationContext Unevaluated( |
1649 | 325 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
1650 | 325 | ExprResult R = DefaultFunctionArrayLvalueConversion(ControllingExpr); |
1651 | 325 | if (R.isInvalid()) |
1652 | 0 | return ExprError(); |
1653 | 325 | ControllingExpr = R.get(); |
1654 | 325 | } |
1655 | | |
1656 | 0 | bool TypeErrorFound = false, |
1657 | 325 | IsResultDependent = ControllingExpr->isTypeDependent(), |
1658 | 325 | ContainsUnexpandedParameterPack |
1659 | 325 | = ControllingExpr->containsUnexpandedParameterPack(); |
1660 | | |
1661 | | // The controlling expression is an unevaluated operand, so side effects are |
1662 | | // likely unintended. |
1663 | 325 | if (!inTemplateInstantiation() && !IsResultDependent308 && |
1664 | 325 | ControllingExpr->HasSideEffects(Context, false)294 ) |
1665 | 11 | Diag(ControllingExpr->getExprLoc(), |
1666 | 11 | diag::warn_side_effects_unevaluated_context); |
1667 | | |
1668 | 974 | for (unsigned i = 0; i < NumAssocs; ++i649 ) { |
1669 | 649 | if (Exprs[i]->containsUnexpandedParameterPack()) |
1670 | 0 | ContainsUnexpandedParameterPack = true; |
1671 | | |
1672 | 649 | if (Types[i]) { |
1673 | 541 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
1674 | 0 | ContainsUnexpandedParameterPack = true; |
1675 | | |
1676 | 541 | if (Types[i]->getType()->isDependentType()) { |
1677 | 2 | IsResultDependent = true; |
1678 | 539 | } else { |
1679 | | // C11 6.5.1.1p2 "The type name in a generic association shall specify a |
1680 | | // complete object type other than a variably modified type." |
1681 | 539 | unsigned D = 0; |
1682 | 539 | if (Types[i]->getType()->isIncompleteType()) |
1683 | 3 | D = diag::err_assoc_type_incomplete; |
1684 | 536 | else if (!Types[i]->getType()->isObjectType()) |
1685 | 2 | D = diag::err_assoc_type_nonobject; |
1686 | 534 | else if (Types[i]->getType()->isVariablyModifiedType()) |
1687 | 2 | D = diag::err_assoc_type_variably_modified; |
1688 | 532 | else { |
1689 | | // Because the controlling expression undergoes lvalue conversion, |
1690 | | // array conversion, and function conversion, an association which is |
1691 | | // of array type, function type, or is qualified can never be |
1692 | | // reached. We will warn about this so users are less surprised by |
1693 | | // the unreachable association. However, we don't have to handle |
1694 | | // function types; that's not an object type, so it's handled above. |
1695 | | // |
1696 | | // The logic is somewhat different for C++ because C++ has different |
1697 | | // lvalue to rvalue conversion rules than C. [conv.lvalue]p1 says, |
1698 | | // If T is a non-class type, the type of the prvalue is the cv- |
1699 | | // unqualified version of T. Otherwise, the type of the prvalue is T. |
1700 | | // The result of these rules is that all qualified types in an |
1701 | | // association in C are unreachable, and in C++, only qualified non- |
1702 | | // class types are unreachable. |
1703 | 532 | unsigned Reason = 0; |
1704 | 532 | QualType QT = Types[i]->getType(); |
1705 | 532 | if (QT->isArrayType()) |
1706 | 3 | Reason = 1; |
1707 | 529 | else if (QT.hasQualifiers() && |
1708 | 529 | (9 !LangOpts.CPlusPlus9 || !QT->isRecordType()3 )) |
1709 | 8 | Reason = 2; |
1710 | | |
1711 | 532 | if (Reason) |
1712 | 11 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
1713 | 11 | diag::warn_unreachable_association) |
1714 | 11 | << QT << (Reason - 1); |
1715 | 532 | } |
1716 | | |
1717 | 539 | if (D != 0) { |
1718 | 7 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
1719 | 7 | << Types[i]->getTypeLoc().getSourceRange() |
1720 | 7 | << Types[i]->getType(); |
1721 | 7 | TypeErrorFound = true; |
1722 | 7 | } |
1723 | | |
1724 | | // C11 6.5.1.1p2 "No two generic associations in the same generic |
1725 | | // selection shall specify compatible types." |
1726 | 983 | for (unsigned j = i+1; j < NumAssocs; ++j444 ) |
1727 | 444 | if (Types[j] && !Types[j]->getType()->isDependentType()338 && |
1728 | 444 | Context.typesAreCompatible(Types[i]->getType(), |
1729 | 334 | Types[j]->getType())) { |
1730 | 8 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
1731 | 8 | diag::err_assoc_compatible_types) |
1732 | 8 | << Types[j]->getTypeLoc().getSourceRange() |
1733 | 8 | << Types[j]->getType() |
1734 | 8 | << Types[i]->getType(); |
1735 | 8 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
1736 | 8 | diag::note_compat_assoc) |
1737 | 8 | << Types[i]->getTypeLoc().getSourceRange() |
1738 | 8 | << Types[i]->getType(); |
1739 | 8 | TypeErrorFound = true; |
1740 | 8 | } |
1741 | 539 | } |
1742 | 541 | } |
1743 | 649 | } |
1744 | 325 | if (TypeErrorFound) |
1745 | 11 | return ExprError(); |
1746 | | |
1747 | | // If we determined that the generic selection is result-dependent, don't |
1748 | | // try to compute the result expression. |
1749 | 314 | if (IsResultDependent) |
1750 | 13 | return GenericSelectionExpr::Create(Context, KeyLoc, ControllingExpr, Types, |
1751 | 13 | Exprs, DefaultLoc, RParenLoc, |
1752 | 13 | ContainsUnexpandedParameterPack); |
1753 | | |
1754 | 301 | SmallVector<unsigned, 1> CompatIndices; |
1755 | 301 | unsigned DefaultIndex = -1U; |
1756 | | // Look at the canonical type of the controlling expression in case it was a |
1757 | | // deduced type like __auto_type. However, when issuing diagnostics, use the |
1758 | | // type the user wrote in source rather than the canonical one. |
1759 | 898 | for (unsigned i = 0; i < NumAssocs; ++i597 ) { |
1760 | 597 | if (!Types[i]) |
1761 | 103 | DefaultIndex = i; |
1762 | 494 | else if (Context.typesAreCompatible( |
1763 | 494 | ControllingExpr->getType().getCanonicalType(), |
1764 | 494 | Types[i]->getType())) |
1765 | 264 | CompatIndices.push_back(i); |
1766 | 597 | } |
1767 | | |
1768 | | // C11 6.5.1.1p2 "The controlling expression of a generic selection shall have |
1769 | | // type compatible with at most one of the types named in its generic |
1770 | | // association list." |
1771 | 301 | if (CompatIndices.size() > 1) { |
1772 | | // We strip parens here because the controlling expression is typically |
1773 | | // parenthesized in macro definitions. |
1774 | 2 | ControllingExpr = ControllingExpr->IgnoreParens(); |
1775 | 2 | Diag(ControllingExpr->getBeginLoc(), diag::err_generic_sel_multi_match) |
1776 | 2 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
1777 | 2 | << (unsigned)CompatIndices.size(); |
1778 | 4 | for (unsigned I : CompatIndices) { |
1779 | 4 | Diag(Types[I]->getTypeLoc().getBeginLoc(), |
1780 | 4 | diag::note_compat_assoc) |
1781 | 4 | << Types[I]->getTypeLoc().getSourceRange() |
1782 | 4 | << Types[I]->getType(); |
1783 | 4 | } |
1784 | 2 | return ExprError(); |
1785 | 2 | } |
1786 | | |
1787 | | // C11 6.5.1.1p2 "If a generic selection has no default generic association, |
1788 | | // its controlling expression shall have type compatible with exactly one of |
1789 | | // the types named in its generic association list." |
1790 | 299 | if (DefaultIndex == -1U && CompatIndices.size() == 0196 ) { |
1791 | | // We strip parens here because the controlling expression is typically |
1792 | | // parenthesized in macro definitions. |
1793 | 13 | ControllingExpr = ControllingExpr->IgnoreParens(); |
1794 | 13 | Diag(ControllingExpr->getBeginLoc(), diag::err_generic_sel_no_match) |
1795 | 13 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
1796 | 13 | return ExprError(); |
1797 | 13 | } |
1798 | | |
1799 | | // C11 6.5.1.1p3 "If a generic selection has a generic association with a |
1800 | | // type name that is compatible with the type of the controlling expression, |
1801 | | // then the result expression of the generic selection is the expression |
1802 | | // in that generic association. Otherwise, the result expression of the |
1803 | | // generic selection is the expression in the default generic association." |
1804 | 286 | unsigned ResultIndex = |
1805 | 286 | CompatIndices.size() ? CompatIndices[0]260 : DefaultIndex26 ; |
1806 | | |
1807 | 286 | return GenericSelectionExpr::Create( |
1808 | 286 | Context, KeyLoc, ControllingExpr, Types, Exprs, DefaultLoc, RParenLoc, |
1809 | 286 | ContainsUnexpandedParameterPack, ResultIndex); |
1810 | 299 | } |
1811 | | |
1812 | | /// getUDSuffixLoc - Create a SourceLocation for a ud-suffix, given the |
1813 | | /// location of the token and the offset of the ud-suffix within it. |
1814 | | static SourceLocation getUDSuffixLoc(Sema &S, SourceLocation TokLoc, |
1815 | 380 | unsigned Offset) { |
1816 | 380 | return Lexer::AdvanceToTokenCharacter(TokLoc, Offset, S.getSourceManager(), |
1817 | 380 | S.getLangOpts()); |
1818 | 380 | } |
1819 | | |
1820 | | /// BuildCookedLiteralOperatorCall - A user-defined literal was found. Look up |
1821 | | /// the corresponding cooked (non-raw) literal operator, and build a call to it. |
1822 | | static ExprResult BuildCookedLiteralOperatorCall(Sema &S, Scope *Scope, |
1823 | | IdentifierInfo *UDSuffix, |
1824 | | SourceLocation UDSuffixLoc, |
1825 | | ArrayRef<Expr*> Args, |
1826 | 57 | SourceLocation LitEndLoc) { |
1827 | 57 | assert(Args.size() <= 2 && "too many arguments for literal operator"); |
1828 | | |
1829 | 0 | QualType ArgTy[2]; |
1830 | 114 | for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx57 ) { |
1831 | 57 | ArgTy[ArgIdx] = Args[ArgIdx]->getType(); |
1832 | 57 | if (ArgTy[ArgIdx]->isArrayType()) |
1833 | 0 | ArgTy[ArgIdx] = S.Context.getArrayDecayedType(ArgTy[ArgIdx]); |
1834 | 57 | } |
1835 | | |
1836 | 57 | DeclarationName OpName = |
1837 | 57 | S.Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); |
1838 | 57 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); |
1839 | 57 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); |
1840 | | |
1841 | 57 | LookupResult R(S, OpName, UDSuffixLoc, Sema::LookupOrdinaryName); |
1842 | 57 | if (S.LookupLiteralOperator(Scope, R, llvm::makeArrayRef(ArgTy, Args.size()), |
1843 | 57 | /*AllowRaw*/ false, /*AllowTemplate*/ false, |
1844 | 57 | /*AllowStringTemplatePack*/ false, |
1845 | 57 | /*DiagnoseMissing*/ true) == Sema::LOLR_Error) |
1846 | 10 | return ExprError(); |
1847 | | |
1848 | 47 | return S.BuildLiteralOperatorCall(R, OpNameInfo, Args, LitEndLoc); |
1849 | 57 | } |
1850 | | |
1851 | | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
1852 | | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
1853 | | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
1854 | | /// multiple tokens. However, the common case is that StringToks points to one |
1855 | | /// string. |
1856 | | /// |
1857 | | ExprResult |
1858 | 5.40M | Sema::ActOnStringLiteral(ArrayRef<Token> StringToks, Scope *UDLScope) { |
1859 | 5.40M | assert(!StringToks.empty() && "Must have at least one string!"); |
1860 | | |
1861 | 0 | StringLiteralParser Literal(StringToks, PP); |
1862 | 5.40M | if (Literal.hadError) |
1863 | 117 | return ExprError(); |
1864 | | |
1865 | 5.40M | SmallVector<SourceLocation, 4> StringTokLocs; |
1866 | 5.40M | for (const Token &Tok : StringToks) |
1867 | 5.57M | StringTokLocs.push_back(Tok.getLocation()); |
1868 | | |
1869 | 5.40M | QualType CharTy = Context.CharTy; |
1870 | 5.40M | StringLiteral::StringKind Kind = StringLiteral::Ordinary; |
1871 | 5.40M | if (Literal.isWide()) { |
1872 | 1.13k | CharTy = Context.getWideCharType(); |
1873 | 1.13k | Kind = StringLiteral::Wide; |
1874 | 5.40M | } else if (Literal.isUTF8()) { |
1875 | 297 | if (getLangOpts().Char8) |
1876 | 213 | CharTy = Context.Char8Ty; |
1877 | 297 | Kind = StringLiteral::UTF8; |
1878 | 5.40M | } else if (Literal.isUTF16()) { |
1879 | 144 | CharTy = Context.Char16Ty; |
1880 | 144 | Kind = StringLiteral::UTF16; |
1881 | 5.40M | } else if (Literal.isUTF32()) { |
1882 | 140 | CharTy = Context.Char32Ty; |
1883 | 140 | Kind = StringLiteral::UTF32; |
1884 | 5.40M | } else if (Literal.isPascal()) { |
1885 | 11 | CharTy = Context.UnsignedCharTy; |
1886 | 11 | } |
1887 | | |
1888 | | // Warn on initializing an array of char from a u8 string literal; this |
1889 | | // becomes ill-formed in C++2a. |
1890 | 5.40M | if (getLangOpts().CPlusPlus && !getLangOpts().CPlusPlus203.27M && |
1891 | 5.40M | !getLangOpts().Char83.24M && Kind == StringLiteral::UTF83.24M ) { |
1892 | 63 | Diag(StringTokLocs.front(), diag::warn_cxx20_compat_utf8_string); |
1893 | | |
1894 | | // Create removals for all 'u8' prefixes in the string literal(s). This |
1895 | | // ensures C++2a compatibility (but may change the program behavior when |
1896 | | // built by non-Clang compilers for which the execution character set is |
1897 | | // not always UTF-8). |
1898 | 63 | auto RemovalDiag = PDiag(diag::note_cxx20_compat_utf8_string_remove_u8); |
1899 | 63 | SourceLocation RemovalDiagLoc; |
1900 | 66 | for (const Token &Tok : StringToks) { |
1901 | 66 | if (Tok.getKind() == tok::utf8_string_literal) { |
1902 | 65 | if (RemovalDiagLoc.isInvalid()) |
1903 | 63 | RemovalDiagLoc = Tok.getLocation(); |
1904 | 65 | RemovalDiag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( |
1905 | 65 | Tok.getLocation(), |
1906 | 65 | Lexer::AdvanceToTokenCharacter(Tok.getLocation(), 2, |
1907 | 65 | getSourceManager(), getLangOpts()))); |
1908 | 65 | } |
1909 | 66 | } |
1910 | 63 | Diag(RemovalDiagLoc, RemovalDiag); |
1911 | 63 | } |
1912 | | |
1913 | 5.40M | QualType StrTy = |
1914 | 5.40M | Context.getStringLiteralArrayType(CharTy, Literal.GetNumStringChars()); |
1915 | | |
1916 | | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
1917 | 5.40M | StringLiteral *Lit = StringLiteral::Create(Context, Literal.GetString(), |
1918 | 5.40M | Kind, Literal.Pascal, StrTy, |
1919 | 5.40M | &StringTokLocs[0], |
1920 | 5.40M | StringTokLocs.size()); |
1921 | 5.40M | if (Literal.getUDSuffix().empty()) |
1922 | 5.40M | return Lit; |
1923 | | |
1924 | | // We're building a user-defined literal. |
1925 | 106 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
1926 | 106 | SourceLocation UDSuffixLoc = |
1927 | 106 | getUDSuffixLoc(*this, StringTokLocs[Literal.getUDSuffixToken()], |
1928 | 106 | Literal.getUDSuffixOffset()); |
1929 | | |
1930 | | // Make sure we're allowed user-defined literals here. |
1931 | 106 | if (!UDLScope) |
1932 | 6 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_string_udl)); |
1933 | | |
1934 | | // C++11 [lex.ext]p5: The literal L is treated as a call of the form |
1935 | | // operator "" X (str, len) |
1936 | 100 | QualType SizeType = Context.getSizeType(); |
1937 | | |
1938 | 100 | DeclarationName OpName = |
1939 | 100 | Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); |
1940 | 100 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); |
1941 | 100 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); |
1942 | | |
1943 | 100 | QualType ArgTy[] = { |
1944 | 100 | Context.getArrayDecayedType(StrTy), SizeType |
1945 | 100 | }; |
1946 | | |
1947 | 100 | LookupResult R(*this, OpName, UDSuffixLoc, LookupOrdinaryName); |
1948 | 100 | switch (LookupLiteralOperator(UDLScope, R, ArgTy, |
1949 | 100 | /*AllowRaw*/ false, /*AllowTemplate*/ true, |
1950 | 100 | /*AllowStringTemplatePack*/ true, |
1951 | 100 | /*DiagnoseMissing*/ true, Lit)) { |
1952 | | |
1953 | 60 | case LOLR_Cooked: { |
1954 | 60 | llvm::APInt Len(Context.getIntWidth(SizeType), Literal.GetNumStringChars()); |
1955 | 60 | IntegerLiteral *LenArg = IntegerLiteral::Create(Context, Len, SizeType, |
1956 | 60 | StringTokLocs[0]); |
1957 | 60 | Expr *Args[] = { Lit, LenArg }; |
1958 | | |
1959 | 60 | return BuildLiteralOperatorCall(R, OpNameInfo, Args, StringTokLocs.back()); |
1960 | 0 | } |
1961 | | |
1962 | 9 | case LOLR_Template: { |
1963 | 9 | TemplateArgumentListInfo ExplicitArgs; |
1964 | 9 | TemplateArgument Arg(Lit); |
1965 | 9 | TemplateArgumentLocInfo ArgInfo(Lit); |
1966 | 9 | ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); |
1967 | 9 | return BuildLiteralOperatorCall(R, OpNameInfo, None, StringTokLocs.back(), |
1968 | 9 | &ExplicitArgs); |
1969 | 0 | } |
1970 | | |
1971 | 13 | case LOLR_StringTemplatePack: { |
1972 | 13 | TemplateArgumentListInfo ExplicitArgs; |
1973 | | |
1974 | 13 | unsigned CharBits = Context.getIntWidth(CharTy); |
1975 | 13 | bool CharIsUnsigned = CharTy->isUnsignedIntegerType(); |
1976 | 13 | llvm::APSInt Value(CharBits, CharIsUnsigned); |
1977 | | |
1978 | 13 | TemplateArgument TypeArg(CharTy); |
1979 | 13 | TemplateArgumentLocInfo TypeArgInfo(Context.getTrivialTypeSourceInfo(CharTy)); |
1980 | 13 | ExplicitArgs.addArgument(TemplateArgumentLoc(TypeArg, TypeArgInfo)); |
1981 | | |
1982 | 129 | for (unsigned I = 0, N = Lit->getLength(); I != N; ++I116 ) { |
1983 | 116 | Value = Lit->getCodeUnit(I); |
1984 | 116 | TemplateArgument Arg(Context, Value, CharTy); |
1985 | 116 | TemplateArgumentLocInfo ArgInfo; |
1986 | 116 | ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); |
1987 | 116 | } |
1988 | 13 | return BuildLiteralOperatorCall(R, OpNameInfo, None, StringTokLocs.back(), |
1989 | 13 | &ExplicitArgs); |
1990 | 0 | } |
1991 | 0 | case LOLR_Raw: |
1992 | 0 | case LOLR_ErrorNoDiagnostic: |
1993 | 0 | llvm_unreachable("unexpected literal operator lookup result"); |
1994 | 18 | case LOLR_Error: |
1995 | 18 | return ExprError(); |
1996 | 100 | } |
1997 | 0 | llvm_unreachable("unexpected literal operator lookup result"); |
1998 | 0 | } |
1999 | | |
2000 | | DeclRefExpr * |
2001 | | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
2002 | | SourceLocation Loc, |
2003 | 40.6k | const CXXScopeSpec *SS) { |
2004 | 40.6k | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
2005 | 40.6k | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
2006 | 40.6k | } |
2007 | | |
2008 | | DeclRefExpr * |
2009 | | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
2010 | | const DeclarationNameInfo &NameInfo, |
2011 | | const CXXScopeSpec *SS, NamedDecl *FoundD, |
2012 | | SourceLocation TemplateKWLoc, |
2013 | 19.0M | const TemplateArgumentListInfo *TemplateArgs) { |
2014 | 19.0M | NestedNameSpecifierLoc NNS = |
2015 | 19.0M | SS ? SS->getWithLocInContext(Context)18.9M : NestedNameSpecifierLoc()41.0k ; |
2016 | 19.0M | return BuildDeclRefExpr(D, Ty, VK, NameInfo, NNS, FoundD, TemplateKWLoc, |
2017 | 19.0M | TemplateArgs); |
2018 | 19.0M | } |
2019 | | |
2020 | | // CUDA/HIP: Check whether a captured reference variable is referencing a |
2021 | | // host variable in a device or host device lambda. |
2022 | | static bool isCapturingReferenceToHostVarInCUDADeviceLambda(const Sema &S, |
2023 | 1.22M | VarDecl *VD) { |
2024 | 1.22M | if (!S.getLangOpts().CUDA || !VD->hasInit()151 ) |
2025 | 1.22M | return false; |
2026 | 112 | assert(VD->getType()->isReferenceType()); |
2027 | | |
2028 | | // Check whether the reference variable is referencing a host variable. |
2029 | 0 | auto *DRE = dyn_cast<DeclRefExpr>(VD->getInit()); |
2030 | 112 | if (!DRE) |
2031 | 0 | return false; |
2032 | 112 | auto *Referee = dyn_cast<VarDecl>(DRE->getDecl()); |
2033 | 112 | if (!Referee || !Referee->hasGlobalStorage() || |
2034 | 112 | Referee->hasAttr<CUDADeviceAttr>()) |
2035 | 26 | return false; |
2036 | | |
2037 | | // Check whether the current function is a device or host device lambda. |
2038 | | // Check whether the reference variable is a capture by getDeclContext() |
2039 | | // since refersToEnclosingVariableOrCapture() is not ready at this point. |
2040 | 86 | auto *MD = dyn_cast_or_null<CXXMethodDecl>(S.CurContext); |
2041 | 86 | if (MD && MD->getParent()->isLambda()32 && |
2042 | 86 | MD->getOverloadedOperator() == OO_Call32 && MD->hasAttr<CUDADeviceAttr>()32 && |
2043 | 86 | VD->getDeclContext() != MD32 ) |
2044 | 12 | return true; |
2045 | | |
2046 | 74 | return false; |
2047 | 86 | } |
2048 | | |
2049 | 21.4M | NonOdrUseReason Sema::getNonOdrUseReasonInCurrentContext(ValueDecl *D) { |
2050 | | // A declaration named in an unevaluated operand never constitutes an odr-use. |
2051 | 21.4M | if (isUnevaluatedContext()) |
2052 | 201k | return NOUR_Unevaluated; |
2053 | | |
2054 | | // C++2a [basic.def.odr]p4: |
2055 | | // A variable x whose name appears as a potentially-evaluated expression e |
2056 | | // is odr-used by e unless [...] x is a reference that is usable in |
2057 | | // constant expressions. |
2058 | | // CUDA/HIP: |
2059 | | // If a reference variable referencing a host variable is captured in a |
2060 | | // device or host device lambda, the value of the referee must be copied |
2061 | | // to the capture and the reference variable must be treated as odr-use |
2062 | | // since the value of the referee is not known at compile time and must |
2063 | | // be loaded from the captured. |
2064 | 21.2M | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
2065 | 15.3M | if (VD->getType()->isReferenceType() && |
2066 | 15.3M | !(1.25M getLangOpts().OpenMP1.25M && isOpenMPCapturedDecl(D)55.1k ) && |
2067 | 15.3M | !isCapturingReferenceToHostVarInCUDADeviceLambda(*this, VD)1.22M && |
2068 | 15.3M | VD->isUsableInConstantExpressions(Context)1.22M ) |
2069 | 3.47k | return NOUR_Constant; |
2070 | 15.3M | } |
2071 | | |
2072 | | // All remaining non-variable cases constitute an odr-use. For variables, we |
2073 | | // need to wait and see how the expression is used. |
2074 | 21.2M | return NOUR_None; |
2075 | 21.2M | } |
2076 | | |
2077 | | /// BuildDeclRefExpr - Build an expression that references a |
2078 | | /// declaration that does not require a closure capture. |
2079 | | DeclRefExpr * |
2080 | | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
2081 | | const DeclarationNameInfo &NameInfo, |
2082 | | NestedNameSpecifierLoc NNS, NamedDecl *FoundD, |
2083 | | SourceLocation TemplateKWLoc, |
2084 | 19.8M | const TemplateArgumentListInfo *TemplateArgs) { |
2085 | 19.8M | bool RefersToCapturedVariable = |
2086 | 19.8M | isa<VarDecl>(D) && |
2087 | 19.8M | NeedToCaptureVariable(cast<VarDecl>(D), NameInfo.getLoc())15.3M ; |
2088 | | |
2089 | 19.8M | DeclRefExpr *E = DeclRefExpr::Create( |
2090 | 19.8M | Context, NNS, TemplateKWLoc, D, RefersToCapturedVariable, NameInfo, Ty, |
2091 | 19.8M | VK, FoundD, TemplateArgs, getNonOdrUseReasonInCurrentContext(D)); |
2092 | 19.8M | MarkDeclRefReferenced(E); |
2093 | | |
2094 | | // C++ [except.spec]p17: |
2095 | | // An exception-specification is considered to be needed when: |
2096 | | // - in an expression, the function is the unique lookup result or |
2097 | | // the selected member of a set of overloaded functions. |
2098 | | // |
2099 | | // We delay doing this until after we've built the function reference and |
2100 | | // marked it as used so that: |
2101 | | // a) if the function is defaulted, we get errors from defining it before / |
2102 | | // instead of errors from computing its exception specification, and |
2103 | | // b) if the function is a defaulted comparison, we can use the body we |
2104 | | // build when defining it as input to the exception specification |
2105 | | // computation rather than computing a new body. |
2106 | 19.8M | if (auto *FPT = Ty->getAs<FunctionProtoType>()) { |
2107 | 1.76M | if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) { |
2108 | 1.75k | if (auto *NewFPT = ResolveExceptionSpec(NameInfo.getLoc(), FPT)) |
2109 | 1.75k | E->setType(Context.getQualifiedType(NewFPT, Ty.getQualifiers())); |
2110 | 1.75k | } |
2111 | 1.76M | } |
2112 | | |
2113 | 19.8M | if (getLangOpts().ObjCWeak && isa<VarDecl>(D)34.3k && |
2114 | 19.8M | Ty.getObjCLifetime() == Qualifiers::OCL_Weak19.8k && !isUnevaluatedContext()290 && |
2115 | 19.8M | !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, E->getBeginLoc())283 ) |
2116 | 26 | getCurFunction()->recordUseOfWeak(E); |
2117 | | |
2118 | 19.8M | FieldDecl *FD = dyn_cast<FieldDecl>(D); |
2119 | 19.8M | if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(D)) |
2120 | 40 | FD = IFD->getAnonField(); |
2121 | 19.8M | if (FD) { |
2122 | 1.00k | UnusedPrivateFields.remove(FD); |
2123 | | // Just in case we're building an illegal pointer-to-member. |
2124 | 1.00k | if (FD->isBitField()) |
2125 | 2 | E->setObjectKind(OK_BitField); |
2126 | 1.00k | } |
2127 | | |
2128 | | // C++ [expr.prim]/8: The expression [...] is a bit-field if the identifier |
2129 | | // designates a bit-field. |
2130 | 19.8M | if (auto *BD = dyn_cast<BindingDecl>(D)) |
2131 | 683 | if (auto *BE = BD->getBinding()) |
2132 | 627 | E->setObjectKind(BE->getObjectKind()); |
2133 | | |
2134 | 19.8M | return E; |
2135 | 19.8M | } |
2136 | | |
2137 | | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
2138 | | /// possibly a list of template arguments. |
2139 | | /// |
2140 | | /// If this produces template arguments, it is permitted to call |
2141 | | /// DecomposeTemplateName. |
2142 | | /// |
2143 | | /// This actually loses a lot of source location information for |
2144 | | /// non-standard name kinds; we should consider preserving that in |
2145 | | /// some way. |
2146 | | void |
2147 | | Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, |
2148 | | TemplateArgumentListInfo &Buffer, |
2149 | | DeclarationNameInfo &NameInfo, |
2150 | 18.5M | const TemplateArgumentListInfo *&TemplateArgs) { |
2151 | 18.5M | if (Id.getKind() == UnqualifiedIdKind::IK_TemplateId) { |
2152 | 292k | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
2153 | 292k | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
2154 | | |
2155 | 292k | ASTTemplateArgsPtr TemplateArgsPtr(Id.TemplateId->getTemplateArgs(), |
2156 | 292k | Id.TemplateId->NumArgs); |
2157 | 292k | translateTemplateArguments(TemplateArgsPtr, Buffer); |
2158 | | |
2159 | 292k | TemplateName TName = Id.TemplateId->Template.get(); |
2160 | 292k | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
2161 | 292k | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
2162 | 292k | TemplateArgs = &Buffer; |
2163 | 18.2M | } else { |
2164 | 18.2M | NameInfo = GetNameFromUnqualifiedId(Id); |
2165 | 18.2M | TemplateArgs = nullptr; |
2166 | 18.2M | } |
2167 | 18.5M | } |
2168 | | |
2169 | | static void emitEmptyLookupTypoDiagnostic( |
2170 | | const TypoCorrection &TC, Sema &SemaRef, const CXXScopeSpec &SS, |
2171 | | DeclarationName Typo, SourceLocation TypoLoc, ArrayRef<Expr *> Args, |
2172 | 3.36k | unsigned DiagnosticID, unsigned DiagnosticSuggestID) { |
2173 | 3.36k | DeclContext *Ctx = |
2174 | 3.36k | SS.isEmpty() ? nullptr3.23k : SemaRef.computeDeclContext(SS, false)130 ; |
2175 | 3.36k | if (!TC) { |
2176 | | // Emit a special diagnostic for failed member lookups. |
2177 | | // FIXME: computing the declaration context might fail here (?) |
2178 | 3.05k | if (Ctx) |
2179 | 44 | SemaRef.Diag(TypoLoc, diag::err_no_member) << Typo << Ctx |
2180 | 44 | << SS.getRange(); |
2181 | 3.01k | else |
2182 | 3.01k | SemaRef.Diag(TypoLoc, DiagnosticID) << Typo; |
2183 | 3.05k | return; |
2184 | 3.05k | } |
2185 | | |
2186 | 307 | std::string CorrectedStr = TC.getAsString(SemaRef.getLangOpts()); |
2187 | 307 | bool DroppedSpecifier = |
2188 | 307 | TC.WillReplaceSpecifier() && Typo.getAsString() == CorrectedStr101 ; |
2189 | 307 | unsigned NoteID = TC.getCorrectionDeclAs<ImplicitParamDecl>() |
2190 | 307 | ? diag::note_implicit_param_decl1 |
2191 | 307 | : diag::note_previous_decl306 ; |
2192 | 307 | if (!Ctx) |
2193 | 221 | SemaRef.diagnoseTypo(TC, SemaRef.PDiag(DiagnosticSuggestID) << Typo, |
2194 | 221 | SemaRef.PDiag(NoteID)); |
2195 | 86 | else |
2196 | 86 | SemaRef.diagnoseTypo(TC, SemaRef.PDiag(diag::err_no_member_suggest) |
2197 | 86 | << Typo << Ctx << DroppedSpecifier |
2198 | 86 | << SS.getRange(), |
2199 | 86 | SemaRef.PDiag(NoteID)); |
2200 | 307 | } |
2201 | | |
2202 | | /// Diagnose a lookup that found results in an enclosing class during error |
2203 | | /// recovery. This usually indicates that the results were found in a dependent |
2204 | | /// base class that could not be searched as part of a template definition. |
2205 | | /// Always issues a diagnostic (though this may be only a warning in MS |
2206 | | /// compatibility mode). |
2207 | | /// |
2208 | | /// Return \c true if the error is unrecoverable, or \c false if the caller |
2209 | | /// should attempt to recover using these lookup results. |
2210 | 37 | bool Sema::DiagnoseDependentMemberLookup(LookupResult &R) { |
2211 | | // During a default argument instantiation the CurContext points |
2212 | | // to a CXXMethodDecl; but we can't apply a this-> fixit inside a |
2213 | | // function parameter list, hence add an explicit check. |
2214 | 37 | bool isDefaultArgument = |
2215 | 37 | !CodeSynthesisContexts.empty() && |
2216 | 37 | CodeSynthesisContexts.back().Kind == |
2217 | 37 | CodeSynthesisContext::DefaultFunctionArgumentInstantiation; |
2218 | 37 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
2219 | 37 | bool isInstance = CurMethod && CurMethod->isInstance()35 && |
2220 | 37 | R.getNamingClass() == CurMethod->getParent()31 && |
2221 | 37 | !isDefaultArgument4 ; |
2222 | | |
2223 | | // There are two ways we can find a class-scope declaration during template |
2224 | | // instantiation that we did not find in the template definition: if it is a |
2225 | | // member of a dependent base class, or if it is declared after the point of |
2226 | | // use in the same class. Distinguish these by comparing the class in which |
2227 | | // the member was found to the naming class of the lookup. |
2228 | 37 | unsigned DiagID = diag::err_found_in_dependent_base; |
2229 | 37 | unsigned NoteID = diag::note_member_declared_at; |
2230 | 37 | if (R.getRepresentativeDecl()->getDeclContext()->Equals(R.getNamingClass())) { |
2231 | 4 | DiagID = getLangOpts().MSVCCompat ? diag::ext_found_later_in_class0 |
2232 | 4 | : diag::err_found_later_in_class; |
2233 | 33 | } else if (getLangOpts().MSVCCompat) { |
2234 | 23 | DiagID = diag::ext_found_in_dependent_base; |
2235 | 23 | NoteID = diag::note_dependent_member_use; |
2236 | 23 | } |
2237 | | |
2238 | 37 | if (isInstance) { |
2239 | | // Give a code modification hint to insert 'this->'. |
2240 | 4 | Diag(R.getNameLoc(), DiagID) |
2241 | 4 | << R.getLookupName() |
2242 | 4 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
2243 | 4 | CheckCXXThisCapture(R.getNameLoc()); |
2244 | 33 | } else { |
2245 | | // FIXME: Add a FixItHint to insert 'Base::' or 'Derived::' (assuming |
2246 | | // they're not shadowed). |
2247 | 33 | Diag(R.getNameLoc(), DiagID) << R.getLookupName(); |
2248 | 33 | } |
2249 | | |
2250 | 37 | for (NamedDecl *D : R) |
2251 | 37 | Diag(D->getLocation(), NoteID); |
2252 | | |
2253 | | // Return true if we are inside a default argument instantiation |
2254 | | // and the found name refers to an instance member function, otherwise |
2255 | | // the caller will try to create an implicit member call and this is wrong |
2256 | | // for default arguments. |
2257 | | // |
2258 | | // FIXME: Is this special case necessary? We could allow the caller to |
2259 | | // diagnose this. |
2260 | 37 | if (isDefaultArgument && ((*R.begin())->isCXXInstanceMember())4 ) { |
2261 | 2 | Diag(R.getNameLoc(), diag::err_member_call_without_object); |
2262 | 2 | return true; |
2263 | 2 | } |
2264 | | |
2265 | | // Tell the callee to try to recover. |
2266 | 35 | return false; |
2267 | 37 | } |
2268 | | |
2269 | | /// Diagnose an empty lookup. |
2270 | | /// |
2271 | | /// \return false if new lookup candidates were found |
2272 | | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
2273 | | CorrectionCandidateCallback &CCC, |
2274 | | TemplateArgumentListInfo *ExplicitTemplateArgs, |
2275 | 8.30k | ArrayRef<Expr *> Args, TypoExpr **Out) { |
2276 | 8.30k | DeclarationName Name = R.getLookupName(); |
2277 | | |
2278 | 8.30k | unsigned diagnostic = diag::err_undeclared_var_use; |
2279 | 8.30k | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
2280 | 8.30k | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
2281 | 8.30k | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName8.30k || |
2282 | 8.30k | Name.getNameKind() == DeclarationName::CXXConversionFunctionName8.30k ) { |
2283 | 10 | diagnostic = diag::err_undeclared_use; |
2284 | 10 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
2285 | 10 | } |
2286 | | |
2287 | | // If the original lookup was an unqualified lookup, fake an |
2288 | | // unqualified lookup. This is useful when (for example) the |
2289 | | // original lookup would not have found something because it was a |
2290 | | // dependent name. |
2291 | 8.30k | DeclContext *DC = SS.isEmpty() ? CurContext8.03k : nullptr267 ; |
2292 | 26.7k | while (DC) { |
2293 | 18.5k | if (isa<CXXRecordDecl>(DC)) { |
2294 | 392 | LookupQualifiedName(R, DC); |
2295 | | |
2296 | 392 | if (!R.empty()) { |
2297 | | // Don't give errors about ambiguities in this lookup. |
2298 | 35 | R.suppressDiagnostics(); |
2299 | | |
2300 | | // If there's a best viable function among the results, only mention |
2301 | | // that one in the notes. |
2302 | 35 | OverloadCandidateSet Candidates(R.getNameLoc(), |
2303 | 35 | OverloadCandidateSet::CSK_Normal); |
2304 | 35 | AddOverloadedCallCandidates(R, ExplicitTemplateArgs, Args, Candidates); |
2305 | 35 | OverloadCandidateSet::iterator Best; |
2306 | 35 | if (Candidates.BestViableFunction(*this, R.getNameLoc(), Best) == |
2307 | 35 | OR_Success) { |
2308 | 31 | R.clear(); |
2309 | 31 | R.addDecl(Best->FoundDecl.getDecl(), Best->FoundDecl.getAccess()); |
2310 | 31 | R.resolveKind(); |
2311 | 31 | } |
2312 | | |
2313 | 35 | return DiagnoseDependentMemberLookup(R); |
2314 | 35 | } |
2315 | | |
2316 | 357 | R.clear(); |
2317 | 357 | } |
2318 | | |
2319 | 18.4k | DC = DC->getLookupParent(); |
2320 | 18.4k | } |
2321 | | |
2322 | | // We didn't find anything, so try to correct for a typo. |
2323 | 8.27k | TypoCorrection Corrected; |
2324 | 8.27k | if (S && Out8.24k ) { |
2325 | 4.43k | SourceLocation TypoLoc = R.getNameLoc(); |
2326 | 4.43k | assert(!ExplicitTemplateArgs && |
2327 | 4.43k | "Diagnosing an empty lookup with explicit template args!"); |
2328 | 0 | *Out = CorrectTypoDelayed( |
2329 | 4.43k | R.getLookupNameInfo(), R.getLookupKind(), S, &SS, CCC, |
2330 | 4.43k | [=](const TypoCorrection &TC) { |
2331 | 3.36k | emitEmptyLookupTypoDiagnostic(TC, *this, SS, Name, TypoLoc, Args, |
2332 | 3.36k | diagnostic, diagnostic_suggest); |
2333 | 3.36k | }, |
2334 | 4.43k | nullptr, CTK_ErrorRecovery); |
2335 | 4.43k | if (*Out) |
2336 | 3.36k | return true; |
2337 | 4.43k | } else if (3.83k S3.83k && |
2338 | 3.83k | (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), |
2339 | 3.81k | S, &SS, CCC, CTK_ErrorRecovery))) { |
2340 | 243 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
2341 | 243 | bool DroppedSpecifier = |
2342 | 243 | Corrected.WillReplaceSpecifier() && Name.getAsString() == CorrectedStr43 ; |
2343 | 243 | R.setLookupName(Corrected.getCorrection()); |
2344 | | |
2345 | 243 | bool AcceptableWithRecovery = false; |
2346 | 243 | bool AcceptableWithoutRecovery = false; |
2347 | 243 | NamedDecl *ND = Corrected.getFoundDecl(); |
2348 | 243 | if (ND) { |
2349 | 231 | if (Corrected.isOverloaded()) { |
2350 | 64 | OverloadCandidateSet OCS(R.getNameLoc(), |
2351 | 64 | OverloadCandidateSet::CSK_Normal); |
2352 | 64 | OverloadCandidateSet::iterator Best; |
2353 | 816 | for (NamedDecl *CD : Corrected) { |
2354 | 816 | if (FunctionTemplateDecl *FTD = |
2355 | 816 | dyn_cast<FunctionTemplateDecl>(CD)) |
2356 | 20 | AddTemplateOverloadCandidate( |
2357 | 20 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, |
2358 | 20 | Args, OCS); |
2359 | 796 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) |
2360 | 788 | if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 00 ) |
2361 | 788 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), |
2362 | 788 | Args, OCS); |
2363 | 816 | } |
2364 | 64 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { |
2365 | 42 | case OR_Success: |
2366 | 42 | ND = Best->FoundDecl; |
2367 | 42 | Corrected.setCorrectionDecl(ND); |
2368 | 42 | break; |
2369 | 22 | default: |
2370 | | // FIXME: Arbitrarily pick the first declaration for the note. |
2371 | 22 | Corrected.setCorrectionDecl(ND); |
2372 | 22 | break; |
2373 | 64 | } |
2374 | 64 | } |
2375 | 231 | R.addDecl(ND); |
2376 | 231 | if (getLangOpts().CPlusPlus && ND->isCXXClassMember()) { |
2377 | 8 | CXXRecordDecl *Record = nullptr; |
2378 | 8 | if (Corrected.getCorrectionSpecifier()) { |
2379 | 3 | const Type *Ty = Corrected.getCorrectionSpecifier()->getAsType(); |
2380 | 3 | Record = Ty->getAsCXXRecordDecl(); |
2381 | 3 | } |
2382 | 8 | if (!Record) |
2383 | 5 | Record = cast<CXXRecordDecl>( |
2384 | 5 | ND->getDeclContext()->getRedeclContext()); |
2385 | 8 | R.setNamingClass(Record); |
2386 | 8 | } |
2387 | | |
2388 | 231 | auto *UnderlyingND = ND->getUnderlyingDecl(); |
2389 | 231 | AcceptableWithRecovery = isa<ValueDecl>(UnderlyingND) || |
2390 | 231 | isa<FunctionTemplateDecl>(UnderlyingND)36 ; |
2391 | | // FIXME: If we ended up with a typo for a type name or |
2392 | | // Objective-C class name, we're in trouble because the parser |
2393 | | // is in the wrong place to recover. Suggest the typo |
2394 | | // correction, but don't make it a fix-it since we're not going |
2395 | | // to recover well anyway. |
2396 | 231 | AcceptableWithoutRecovery = isa<TypeDecl>(UnderlyingND) || |
2397 | 231 | getAsTypeTemplateDecl(UnderlyingND)229 || |
2398 | 231 | isa<ObjCInterfaceDecl>(UnderlyingND)216 ; |
2399 | 231 | } else { |
2400 | | // FIXME: We found a keyword. Suggest it, but don't provide a fix-it |
2401 | | // because we aren't able to recover. |
2402 | 12 | AcceptableWithoutRecovery = true; |
2403 | 12 | } |
2404 | | |
2405 | 243 | if (AcceptableWithRecovery || AcceptableWithoutRecovery27 ) { |
2406 | 243 | unsigned NoteID = Corrected.getCorrectionDeclAs<ImplicitParamDecl>() |
2407 | 243 | ? diag::note_implicit_param_decl0 |
2408 | 243 | : diag::note_previous_decl; |
2409 | 243 | if (SS.isEmpty()) |
2410 | 243 | diagnoseTypo(Corrected, PDiag(diagnostic_suggest) << Name, |
2411 | 243 | PDiag(NoteID), AcceptableWithRecovery); |
2412 | 0 | else |
2413 | 0 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest) |
2414 | 0 | << Name << computeDeclContext(SS, false) |
2415 | 0 | << DroppedSpecifier << SS.getRange(), |
2416 | 0 | PDiag(NoteID), AcceptableWithRecovery); |
2417 | | |
2418 | | // Tell the callee whether to try to recover. |
2419 | 243 | return !AcceptableWithRecovery; |
2420 | 243 | } |
2421 | 243 | } |
2422 | 4.66k | R.clear(); |
2423 | | |
2424 | | // Emit a special diagnostic for failed member lookups. |
2425 | | // FIXME: computing the declaration context might fail here (?) |
2426 | 4.66k | if (!SS.isEmpty()) { |
2427 | 137 | Diag(R.getNameLoc(), diag::err_no_member) |
2428 | 137 | << Name << computeDeclContext(SS, false) |
2429 | 137 | << SS.getRange(); |
2430 | 137 | return true; |
2431 | 137 | } |
2432 | | |
2433 | | // Give up, we can't recover. |
2434 | 4.52k | Diag(R.getNameLoc(), diagnostic) << Name; |
2435 | 4.52k | return true; |
2436 | 4.66k | } |
2437 | | |
2438 | | /// In Microsoft mode, if we are inside a template class whose parent class has |
2439 | | /// dependent base classes, and we can't resolve an unqualified identifier, then |
2440 | | /// assume the identifier is a member of a dependent base class. We can only |
2441 | | /// recover successfully in static methods, instance methods, and other contexts |
2442 | | /// where 'this' is available. This doesn't precisely match MSVC's |
2443 | | /// instantiation model, but it's close enough. |
2444 | | static Expr * |
2445 | | recoverFromMSUnqualifiedLookup(Sema &S, ASTContext &Context, |
2446 | | DeclarationNameInfo &NameInfo, |
2447 | | SourceLocation TemplateKWLoc, |
2448 | 67 | const TemplateArgumentListInfo *TemplateArgs) { |
2449 | | // Only try to recover from lookup into dependent bases in static methods or |
2450 | | // contexts where 'this' is available. |
2451 | 67 | QualType ThisType = S.getCurrentThisType(); |
2452 | 67 | const CXXRecordDecl *RD = nullptr; |
2453 | 67 | if (!ThisType.isNull()) |
2454 | 30 | RD = ThisType->getPointeeType()->getAsCXXRecordDecl(); |
2455 | 37 | else if (auto *MD = dyn_cast<CXXMethodDecl>(S.CurContext)) |
2456 | 5 | RD = MD->getParent(); |
2457 | 67 | if (!RD || !RD->hasAnyDependentBases()35 ) |
2458 | 41 | return nullptr; |
2459 | | |
2460 | | // Diagnose this as unqualified lookup into a dependent base class. If 'this' |
2461 | | // is available, suggest inserting 'this->' as a fixit. |
2462 | 26 | SourceLocation Loc = NameInfo.getLoc(); |
2463 | 26 | auto DB = S.Diag(Loc, diag::ext_undeclared_unqual_id_with_dependent_base); |
2464 | 26 | DB << NameInfo.getName() << RD; |
2465 | | |
2466 | 26 | if (!ThisType.isNull()) { |
2467 | 22 | DB << FixItHint::CreateInsertion(Loc, "this->"); |
2468 | 22 | return CXXDependentScopeMemberExpr::Create( |
2469 | 22 | Context, /*This=*/nullptr, ThisType, /*IsArrow=*/true, |
2470 | 22 | /*Op=*/SourceLocation(), NestedNameSpecifierLoc(), TemplateKWLoc, |
2471 | 22 | /*FirstQualifierFoundInScope=*/nullptr, NameInfo, TemplateArgs); |
2472 | 22 | } |
2473 | | |
2474 | | // Synthesize a fake NNS that points to the derived class. This will |
2475 | | // perform name lookup during template instantiation. |
2476 | 4 | CXXScopeSpec SS; |
2477 | 4 | auto *NNS = |
2478 | 4 | NestedNameSpecifier::Create(Context, nullptr, true, RD->getTypeForDecl()); |
2479 | 4 | SS.MakeTrivial(Context, NNS, SourceRange(Loc, Loc)); |
2480 | 4 | return DependentScopeDeclRefExpr::Create( |
2481 | 4 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
2482 | 4 | TemplateArgs); |
2483 | 26 | } |
2484 | | |
2485 | | ExprResult |
2486 | | Sema::ActOnIdExpression(Scope *S, CXXScopeSpec &SS, |
2487 | | SourceLocation TemplateKWLoc, UnqualifiedId &Id, |
2488 | | bool HasTrailingLParen, bool IsAddressOfOperand, |
2489 | | CorrectionCandidateCallback *CCC, |
2490 | 16.8M | bool IsInlineAsmIdentifier, Token *KeywordReplacement) { |
2491 | 16.8M | assert(!(IsAddressOfOperand && HasTrailingLParen) && |
2492 | 16.8M | "cannot be direct & operand and have a trailing lparen"); |
2493 | 16.8M | if (SS.isInvalid()) |
2494 | 261 | return ExprError(); |
2495 | | |
2496 | 16.8M | TemplateArgumentListInfo TemplateArgsBuffer; |
2497 | | |
2498 | | // Decompose the UnqualifiedId into the following data. |
2499 | 16.8M | DeclarationNameInfo NameInfo; |
2500 | 16.8M | const TemplateArgumentListInfo *TemplateArgs; |
2501 | 16.8M | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
2502 | | |
2503 | 16.8M | DeclarationName Name = NameInfo.getName(); |
2504 | 16.8M | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
2505 | 16.8M | SourceLocation NameLoc = NameInfo.getLoc(); |
2506 | | |
2507 | 16.8M | if (II && II->isEditorPlaceholder()16.8M ) { |
2508 | | // FIXME: When typed placeholders are supported we can create a typed |
2509 | | // placeholder expression node. |
2510 | 16 | return ExprError(); |
2511 | 16 | } |
2512 | | |
2513 | | // C++ [temp.dep.expr]p3: |
2514 | | // An id-expression is type-dependent if it contains: |
2515 | | // -- an identifier that was declared with a dependent type, |
2516 | | // (note: handled after lookup) |
2517 | | // -- a template-id that is dependent, |
2518 | | // (note: handled in BuildTemplateIdExpr) |
2519 | | // -- a conversion-function-id that specifies a dependent type, |
2520 | | // -- a nested-name-specifier that contains a class-name that |
2521 | | // names a dependent type. |
2522 | | // Determine whether this is a member of an unknown specialization; |
2523 | | // we need to handle these differently. |
2524 | 16.8M | bool DependentID = false; |
2525 | 16.8M | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
2526 | 16.8M | Name.getCXXNameType()->isDependentType()30 ) { |
2527 | 6 | DependentID = true; |
2528 | 16.8M | } else if (SS.isSet()) { |
2529 | 1.53M | if (DeclContext *DC = computeDeclContext(SS, false)) { |
2530 | 687k | if (RequireCompleteDeclContext(SS, DC)) |
2531 | 2 | return ExprError(); |
2532 | 846k | } else { |
2533 | 846k | DependentID = true; |
2534 | 846k | } |
2535 | 1.53M | } |
2536 | | |
2537 | 16.8M | if (DependentID) |
2538 | 846k | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
2539 | 846k | IsAddressOfOperand, TemplateArgs); |
2540 | | |
2541 | | // Perform the required lookup. |
2542 | 15.9M | LookupResult R(*this, NameInfo, |
2543 | 15.9M | (Id.getKind() == UnqualifiedIdKind::IK_ImplicitSelfParam) |
2544 | 15.9M | ? LookupObjCImplicitSelfParam2.17k |
2545 | 15.9M | : LookupOrdinaryName15.9M ); |
2546 | 15.9M | if (TemplateKWLoc.isValid() || TemplateArgs15.9M ) { |
2547 | | // Lookup the template name again to correctly establish the context in |
2548 | | // which it was found. This is really unfortunate as we already did the |
2549 | | // lookup to determine that it was a template name in the first place. If |
2550 | | // this becomes a performance hit, we can work harder to preserve those |
2551 | | // results until we get here but it's likely not worth it. |
2552 | 274k | bool MemberOfUnknownSpecialization; |
2553 | 274k | AssumedTemplateKind AssumedTemplate; |
2554 | 274k | if (LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
2555 | 274k | MemberOfUnknownSpecialization, TemplateKWLoc, |
2556 | 274k | &AssumedTemplate)) |
2557 | 0 | return ExprError(); |
2558 | | |
2559 | 274k | if (MemberOfUnknownSpecialization || |
2560 | 274k | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
2561 | 0 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
2562 | 0 | IsAddressOfOperand, TemplateArgs); |
2563 | 15.7M | } else { |
2564 | 15.7M | bool IvarLookupFollowUp = II && !SS.isSet()15.7M && getCurMethodDecl()15.1M ; |
2565 | 15.7M | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
2566 | | |
2567 | | // If the result might be in a dependent base class, this is a dependent |
2568 | | // id-expression. |
2569 | 15.7M | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
2570 | 12 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
2571 | 12 | IsAddressOfOperand, TemplateArgs); |
2572 | | |
2573 | | // If this reference is in an Objective-C method, then we need to do |
2574 | | // some special Objective-C lookup, too. |
2575 | 15.7M | if (IvarLookupFollowUp) { |
2576 | 10.4k | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
2577 | 10.4k | if (E.isInvalid()) |
2578 | 5 | return ExprError(); |
2579 | | |
2580 | 10.4k | if (Expr *Ex = E.getAs<Expr>()) |
2581 | 1.24k | return Ex; |
2582 | 10.4k | } |
2583 | 15.7M | } |
2584 | | |
2585 | 15.9M | if (R.isAmbiguous()) |
2586 | 58 | return ExprError(); |
2587 | | |
2588 | | // This could be an implicitly declared function reference if the language |
2589 | | // mode allows it as a feature. |
2590 | 15.9M | if (R.empty() && HasTrailingLParen9.15k && II4.98k && |
2591 | 15.9M | getLangOpts().implicitFunctionsAllowed()4.95k ) { |
2592 | 314 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
2593 | 314 | if (D) R.addDecl(D); |
2594 | 314 | } |
2595 | | |
2596 | | // Determine whether this name might be a candidate for |
2597 | | // argument-dependent lookup. |
2598 | 15.9M | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
2599 | | |
2600 | 15.9M | if (R.empty() && !ADL8.84k ) { |
2601 | 4.47k | if (SS.isEmpty() && getLangOpts().MSVCCompat4.21k ) { |
2602 | 67 | if (Expr *E = recoverFromMSUnqualifiedLookup(*this, Context, NameInfo, |
2603 | 67 | TemplateKWLoc, TemplateArgs)) |
2604 | 26 | return E; |
2605 | 67 | } |
2606 | | |
2607 | | // Don't diagnose an empty lookup for inline assembly. |
2608 | 4.45k | if (IsInlineAsmIdentifier) |
2609 | 20 | return ExprError(); |
2610 | | |
2611 | | // If this name wasn't predeclared and if this is not a function |
2612 | | // call, diagnose the problem. |
2613 | 4.43k | TypoExpr *TE = nullptr; |
2614 | 4.43k | DefaultFilterCCC DefaultValidator(II, SS.isValid() ? SS.getScopeRep()267 |
2615 | 4.43k | : nullptr4.16k ); |
2616 | 4.43k | DefaultValidator.IsAddressOfOperand = IsAddressOfOperand; |
2617 | 4.43k | assert((!CCC || CCC->IsAddressOfOperand == IsAddressOfOperand) && |
2618 | 4.43k | "Typo correction callback misconfigured"); |
2619 | 4.43k | if (CCC) { |
2620 | | // Make sure the callback knows what the typo being diagnosed is. |
2621 | 4.15k | CCC->setTypoName(II); |
2622 | 4.15k | if (SS.isValid()) |
2623 | 0 | CCC->setTypoNNS(SS.getScopeRep()); |
2624 | 4.15k | } |
2625 | | // FIXME: DiagnoseEmptyLookup produces bad diagnostics if we're looking for |
2626 | | // a template name, but we happen to have always already looked up the name |
2627 | | // before we get here if it must be a template name. |
2628 | 4.43k | if (DiagnoseEmptyLookup(S, SS, R, CCC ? *CCC4.15k : DefaultValidator272 , nullptr, |
2629 | 4.43k | None, &TE)) { |
2630 | 4.43k | if (TE && KeywordReplacement3.36k ) { |
2631 | 2.39k | auto &State = getTypoExprState(TE); |
2632 | 2.39k | auto BestTC = State.Consumer->getNextCorrection(); |
2633 | 2.39k | if (BestTC.isKeyword()) { |
2634 | 6 | auto *II = BestTC.getCorrectionAsIdentifierInfo(); |
2635 | 6 | if (State.DiagHandler) |
2636 | 6 | State.DiagHandler(BestTC); |
2637 | 6 | KeywordReplacement->startToken(); |
2638 | 6 | KeywordReplacement->setKind(II->getTokenID()); |
2639 | 6 | KeywordReplacement->setIdentifierInfo(II); |
2640 | 6 | KeywordReplacement->setLocation(BestTC.getCorrectionRange().getBegin()); |
2641 | | // Clean up the state associated with the TypoExpr, since it has |
2642 | | // now been diagnosed (without a call to CorrectDelayedTyposInExpr). |
2643 | 6 | clearDelayedTypo(TE); |
2644 | | // Signal that a correction to a keyword was performed by returning a |
2645 | | // valid-but-null ExprResult. |
2646 | 6 | return (Expr*)nullptr; |
2647 | 6 | } |
2648 | 2.38k | State.Consumer->resetCorrectionStream(); |
2649 | 2.38k | } |
2650 | 4.42k | return TE ? TE3.35k : ExprError()1.06k ; |
2651 | 4.43k | } |
2652 | | |
2653 | 0 | assert(!R.empty() && |
2654 | 0 | "DiagnoseEmptyLookup returned false but added no results"); |
2655 | | |
2656 | | // If we found an Objective-C instance variable, let |
2657 | | // LookupInObjCMethod build the appropriate expression to |
2658 | | // reference the ivar. |
2659 | 0 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
2660 | 0 | R.clear(); |
2661 | 0 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
2662 | | // In a hopelessly buggy code, Objective-C instance variable |
2663 | | // lookup fails and no expression will be built to reference it. |
2664 | 0 | if (!E.isInvalid() && !E.get()) |
2665 | 0 | return ExprError(); |
2666 | 0 | return E; |
2667 | 0 | } |
2668 | 0 | } |
2669 | | |
2670 | | // This is guaranteed from this point on. |
2671 | 15.9M | assert(!R.empty() || ADL); |
2672 | | |
2673 | | // Check whether this might be a C++ implicit instance member access. |
2674 | | // C++ [class.mfct.non-static]p3: |
2675 | | // When an id-expression that is not part of a class member access |
2676 | | // syntax and not used to form a pointer to member is used in the |
2677 | | // body of a non-static member function of class X, if name lookup |
2678 | | // resolves the name in the id-expression to a non-static non-type |
2679 | | // member of some class C, the id-expression is transformed into a |
2680 | | // class member access expression using (*this) as the |
2681 | | // postfix-expression to the left of the . operator. |
2682 | | // |
2683 | | // But we don't actually need to do this for '&' operands if R |
2684 | | // resolved to a function or overloaded function set, because the |
2685 | | // expression is ill-formed if it actually works out to be a |
2686 | | // non-static member function: |
2687 | | // |
2688 | | // C++ [expr.ref]p4: |
2689 | | // Otherwise, if E1.E2 refers to a non-static member function. . . |
2690 | | // [t]he expression can be used only as the left-hand operand of a |
2691 | | // member function call. |
2692 | | // |
2693 | | // There are other safeguards against such uses, but it's important |
2694 | | // to get this right here so that we don't end up making a |
2695 | | // spuriously dependent expression if we're inside a dependent |
2696 | | // instance method. |
2697 | 15.9M | if (!R.empty() && (*R.begin())->isCXXClassMember()15.9M ) { |
2698 | 1.00M | bool MightBeImplicitMember; |
2699 | 1.00M | if (!IsAddressOfOperand) |
2700 | 973k | MightBeImplicitMember = true; |
2701 | 28.5k | else if (!SS.isEmpty()) |
2702 | 2.86k | MightBeImplicitMember = false; |
2703 | 25.6k | else if (R.isOverloadedResult()) |
2704 | 1.13k | MightBeImplicitMember = false; |
2705 | 24.5k | else if (R.isUnresolvableResult()) |
2706 | 7 | MightBeImplicitMember = true; |
2707 | 24.5k | else |
2708 | 24.5k | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
2709 | 24.5k | isa<IndirectFieldDecl>(R.getFoundDecl())449 || |
2710 | 24.5k | isa<MSPropertyDecl>(R.getFoundDecl())445 ; |
2711 | | |
2712 | 1.00M | if (MightBeImplicitMember) |
2713 | 997k | return BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
2714 | 997k | R, TemplateArgs, S); |
2715 | 1.00M | } |
2716 | | |
2717 | 14.9M | if (TemplateArgs || TemplateKWLoc.isValid()14.7M ) { |
2718 | | |
2719 | | // In C++1y, if this is a variable template id, then check it |
2720 | | // in BuildTemplateIdExpr(). |
2721 | | // The single lookup result must be a variable template declaration. |
2722 | 259k | if (Id.getKind() == UnqualifiedIdKind::IK_TemplateId && Id.TemplateId259k && |
2723 | 259k | Id.TemplateId->Kind == TNK_Var_template259k ) { |
2724 | 4.70k | assert(R.getAsSingle<VarTemplateDecl>() && |
2725 | 4.70k | "There should only be one declaration found."); |
2726 | 4.70k | } |
2727 | | |
2728 | 0 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, ADL, TemplateArgs); |
2729 | 259k | } |
2730 | | |
2731 | 14.7M | return BuildDeclarationNameExpr(SS, R, ADL); |
2732 | 14.9M | } |
2733 | | |
2734 | | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
2735 | | /// declaration name, generally during template instantiation. |
2736 | | /// There's a large number of things which don't need to be done along |
2737 | | /// this path. |
2738 | | ExprResult Sema::BuildQualifiedDeclarationNameExpr( |
2739 | | CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo, |
2740 | 1.38M | bool IsAddressOfOperand, const Scope *S, TypeSourceInfo **RecoveryTSI) { |
2741 | 1.38M | DeclContext *DC = computeDeclContext(SS, false); |
2742 | 1.38M | if (!DC) |
2743 | 458k | return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), |
2744 | 458k | NameInfo, /*TemplateArgs=*/nullptr); |
2745 | | |
2746 | 927k | if (RequireCompleteDeclContext(SS, DC)) |
2747 | 14 | return ExprError(); |
2748 | | |
2749 | 927k | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
2750 | 927k | LookupQualifiedName(R, DC); |
2751 | | |
2752 | 927k | if (R.isAmbiguous()) |
2753 | 0 | return ExprError(); |
2754 | | |
2755 | 927k | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
2756 | 0 | return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), |
2757 | 0 | NameInfo, /*TemplateArgs=*/nullptr); |
2758 | | |
2759 | 927k | if (R.empty()) { |
2760 | | // Don't diagnose problems with invalid record decl, the secondary no_member |
2761 | | // diagnostic during template instantiation is likely bogus, e.g. if a class |
2762 | | // is invalid because it's derived from an invalid base class, then missing |
2763 | | // members were likely supposed to be inherited. |
2764 | 202 | if (const auto *CD = dyn_cast<CXXRecordDecl>(DC)) |
2765 | 201 | if (CD->isInvalidDecl()) |
2766 | 2 | return ExprError(); |
2767 | 200 | Diag(NameInfo.getLoc(), diag::err_no_member) |
2768 | 200 | << NameInfo.getName() << DC << SS.getRange(); |
2769 | 200 | return ExprError(); |
2770 | 202 | } |
2771 | | |
2772 | 927k | if (const TypeDecl *TD = R.getAsSingle<TypeDecl>()) { |
2773 | | // Diagnose a missing typename if this resolved unambiguously to a type in |
2774 | | // a dependent context. If we can recover with a type, downgrade this to |
2775 | | // a warning in Microsoft compatibility mode. |
2776 | 38 | unsigned DiagID = diag::err_typename_missing; |
2777 | 38 | if (RecoveryTSI && getLangOpts().MSVCCompat3 ) |
2778 | 3 | DiagID = diag::ext_typename_missing; |
2779 | 38 | SourceLocation Loc = SS.getBeginLoc(); |
2780 | 38 | auto D = Diag(Loc, DiagID); |
2781 | 38 | D << SS.getScopeRep() << NameInfo.getName().getAsString() |
2782 | 38 | << SourceRange(Loc, NameInfo.getEndLoc()); |
2783 | | |
2784 | | // Don't recover if the caller isn't expecting us to or if we're in a SFINAE |
2785 | | // context. |
2786 | 38 | if (!RecoveryTSI) |
2787 | 35 | return ExprError(); |
2788 | | |
2789 | | // Only issue the fixit if we're prepared to recover. |
2790 | 3 | D << FixItHint::CreateInsertion(Loc, "typename "); |
2791 | | |
2792 | | // Recover by pretending this was an elaborated type. |
2793 | 3 | QualType Ty = Context.getTypeDeclType(TD); |
2794 | 3 | TypeLocBuilder TLB; |
2795 | 3 | TLB.pushTypeSpec(Ty).setNameLoc(NameInfo.getLoc()); |
2796 | | |
2797 | 3 | QualType ET = getElaboratedType(ETK_None, SS, Ty); |
2798 | 3 | ElaboratedTypeLoc QTL = TLB.push<ElaboratedTypeLoc>(ET); |
2799 | 3 | QTL.setElaboratedKeywordLoc(SourceLocation()); |
2800 | 3 | QTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
2801 | | |
2802 | 3 | *RecoveryTSI = TLB.getTypeSourceInfo(Context, ET); |
2803 | | |
2804 | 3 | return ExprEmpty(); |
2805 | 38 | } |
2806 | | |
2807 | | // Defend against this resolving to an implicit member access. We usually |
2808 | | // won't get here if this might be a legitimate a class member (we end up in |
2809 | | // BuildMemberReferenceExpr instead), but this can be valid if we're forming |
2810 | | // a pointer-to-member or in an unevaluated context in C++11. |
2811 | 927k | if (!R.empty() && (*R.begin())->isCXXClassMember() && !IsAddressOfOperand927k ) |
2812 | 927k | return BuildPossibleImplicitMemberExpr(SS, |
2813 | 927k | /*TemplateKWLoc=*/SourceLocation(), |
2814 | 927k | R, /*TemplateArgs=*/nullptr, S); |
2815 | | |
2816 | 88 | return BuildDeclarationNameExpr(SS, R, /* ADL */ false); |
2817 | 927k | } |
2818 | | |
2819 | | /// The parser has read a name in, and Sema has detected that we're currently |
2820 | | /// inside an ObjC method. Perform some additional checks and determine if we |
2821 | | /// should form a reference to an ivar. |
2822 | | /// |
2823 | | /// Ideally, most of this would be done by lookup, but there's |
2824 | | /// actually quite a lot of extra work involved. |
2825 | | DeclResult Sema::LookupIvarInObjCMethod(LookupResult &Lookup, Scope *S, |
2826 | 14.4k | IdentifierInfo *II) { |
2827 | 14.4k | SourceLocation Loc = Lookup.getNameLoc(); |
2828 | 14.4k | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
2829 | | |
2830 | | // Check for error condition which is already reported. |
2831 | 14.4k | if (!CurMethod) |
2832 | 0 | return DeclResult(true); |
2833 | | |
2834 | | // There are two cases to handle here. 1) scoped lookup could have failed, |
2835 | | // in which case we should look for an ivar. 2) scoped lookup could have |
2836 | | // found a decl, but that decl is outside the current instance method (i.e. |
2837 | | // a global variable). In these two cases, we do a lookup for an ivar with |
2838 | | // this name, if the lookup sucedes, we replace it our current decl. |
2839 | | |
2840 | | // If we're in a class method, we don't normally want to look for |
2841 | | // ivars. But if we don't find anything else, and there's an |
2842 | | // ivar, that's an error. |
2843 | 14.4k | bool IsClassMethod = CurMethod->isClassMethod(); |
2844 | | |
2845 | 14.4k | bool LookForIvars; |
2846 | 14.4k | if (Lookup.empty()) |
2847 | 2.16k | LookForIvars = true; |
2848 | 12.2k | else if (IsClassMethod) |
2849 | 929 | LookForIvars = false; |
2850 | 11.3k | else |
2851 | 11.3k | LookForIvars = (Lookup.isSingleResult() && |
2852 | 11.3k | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()11.3k ); |
2853 | 14.4k | ObjCInterfaceDecl *IFace = nullptr; |
2854 | 14.4k | if (LookForIvars) { |
2855 | 3.90k | IFace = CurMethod->getClassInterface(); |
2856 | 3.90k | ObjCInterfaceDecl *ClassDeclared; |
2857 | 3.90k | ObjCIvarDecl *IV = nullptr; |
2858 | 3.90k | if (IFace && (IV = IFace->lookupInstanceVariable(II, ClassDeclared))3.89k ) { |
2859 | | // Diagnose using an ivar in a class method. |
2860 | 2.18k | if (IsClassMethod) { |
2861 | 8 | Diag(Loc, diag::err_ivar_use_in_class_method) << IV->getDeclName(); |
2862 | 8 | return DeclResult(true); |
2863 | 8 | } |
2864 | | |
2865 | | // Diagnose the use of an ivar outside of the declaring class. |
2866 | 2.17k | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
2867 | 2.17k | !declaresSameEntity(ClassDeclared, IFace)675 && |
2868 | 2.17k | !getLangOpts().DebuggerSupport9 ) |
2869 | 3 | Diag(Loc, diag::err_private_ivar_access) << IV->getDeclName(); |
2870 | | |
2871 | | // Success. |
2872 | 2.17k | return IV; |
2873 | 2.18k | } |
2874 | 10.5k | } else if (CurMethod->isInstanceMethod()) { |
2875 | | // We should warn if a local variable hides an ivar. |
2876 | 9.57k | if (ObjCInterfaceDecl *IFace = CurMethod->getClassInterface()) { |
2877 | 9.56k | ObjCInterfaceDecl *ClassDeclared; |
2878 | 9.56k | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
2879 | 2 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
2880 | 2 | declaresSameEntity(IFace, ClassDeclared)0 ) |
2881 | 2 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
2882 | 2 | } |
2883 | 9.56k | } |
2884 | 9.57k | } else if (929 Lookup.isSingleResult()929 && |
2885 | 929 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()) { |
2886 | | // If accessing a stand-alone ivar in a class method, this is an error. |
2887 | 191 | if (const ObjCIvarDecl *IV = |
2888 | 191 | dyn_cast<ObjCIvarDecl>(Lookup.getFoundDecl())) { |
2889 | 1 | Diag(Loc, diag::err_ivar_use_in_class_method) << IV->getDeclName(); |
2890 | 1 | return DeclResult(true); |
2891 | 1 | } |
2892 | 191 | } |
2893 | | |
2894 | | // Didn't encounter an error, didn't find an ivar. |
2895 | 12.2k | return DeclResult(false); |
2896 | 14.4k | } |
2897 | | |
2898 | | ExprResult Sema::BuildIvarRefExpr(Scope *S, SourceLocation Loc, |
2899 | 2.17k | ObjCIvarDecl *IV) { |
2900 | 2.17k | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
2901 | 2.17k | assert(CurMethod && CurMethod->isInstanceMethod() && |
2902 | 2.17k | "should not reference ivar from this context"); |
2903 | | |
2904 | 0 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
2905 | 2.17k | assert(IFace && "should not reference ivar from this context"); |
2906 | | |
2907 | | // If we're referencing an invalid decl, just return this as a silent |
2908 | | // error node. The error diagnostic was already emitted on the decl. |
2909 | 2.17k | if (IV->isInvalidDecl()) |
2910 | 2 | return ExprError(); |
2911 | | |
2912 | | // Check if referencing a field with __attribute__((deprecated)). |
2913 | 2.17k | if (DiagnoseUseOfDecl(IV, Loc)) |
2914 | 0 | return ExprError(); |
2915 | | |
2916 | | // FIXME: This should use a new expr for a direct reference, don't |
2917 | | // turn this into Self->ivar, just return a BareIVarExpr or something. |
2918 | 2.17k | IdentifierInfo &II = Context.Idents.get("self"); |
2919 | 2.17k | UnqualifiedId SelfName; |
2920 | 2.17k | SelfName.setImplicitSelfParam(&II); |
2921 | 2.17k | CXXScopeSpec SelfScopeSpec; |
2922 | 2.17k | SourceLocation TemplateKWLoc; |
2923 | 2.17k | ExprResult SelfExpr = |
2924 | 2.17k | ActOnIdExpression(S, SelfScopeSpec, TemplateKWLoc, SelfName, |
2925 | 2.17k | /*HasTrailingLParen=*/false, |
2926 | 2.17k | /*IsAddressOfOperand=*/false); |
2927 | 2.17k | if (SelfExpr.isInvalid()) |
2928 | 0 | return ExprError(); |
2929 | | |
2930 | 2.17k | SelfExpr = DefaultLvalueConversion(SelfExpr.get()); |
2931 | 2.17k | if (SelfExpr.isInvalid()) |
2932 | 0 | return ExprError(); |
2933 | | |
2934 | 2.17k | MarkAnyDeclReferenced(Loc, IV, true); |
2935 | | |
2936 | 2.17k | ObjCMethodFamily MF = CurMethod->getMethodFamily(); |
2937 | 2.17k | if (MF != OMF_init && MF != OMF_dealloc1.79k && MF != OMF_finalize1.59k && |
2938 | 2.17k | !IvarBacksCurrentMethodAccessor(IFace, CurMethod, IV)1.59k ) |
2939 | 1.56k | Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName(); |
2940 | | |
2941 | 2.17k | ObjCIvarRefExpr *Result = new (Context) |
2942 | 2.17k | ObjCIvarRefExpr(IV, IV->getUsageType(SelfExpr.get()->getType()), Loc, |
2943 | 2.17k | IV->getLocation(), SelfExpr.get(), true, true); |
2944 | | |
2945 | 2.17k | if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
2946 | 15 | if (!isUnevaluatedContext() && |
2947 | 15 | !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) |
2948 | 4 | getCurFunction()->recordUseOfWeak(Result); |
2949 | 15 | } |
2950 | 2.17k | if (getLangOpts().ObjCAutoRefCount) |
2951 | 260 | if (const BlockDecl *BD = CurContext->getInnermostBlockDecl()) |
2952 | 24 | ImplicitlyRetainedSelfLocs.push_back({Loc, BD}); |
2953 | | |
2954 | 2.17k | return Result; |
2955 | 2.17k | } |
2956 | | |
2957 | | /// The parser has read a name in, and Sema has detected that we're currently |
2958 | | /// inside an ObjC method. Perform some additional checks and determine if we |
2959 | | /// should form a reference to an ivar. If so, build an expression referencing |
2960 | | /// that ivar. |
2961 | | ExprResult |
2962 | | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
2963 | 10.4k | IdentifierInfo *II, bool AllowBuiltinCreation) { |
2964 | | // FIXME: Integrate this lookup step into LookupParsedName. |
2965 | 10.4k | DeclResult Ivar = LookupIvarInObjCMethod(Lookup, S, II); |
2966 | 10.4k | if (Ivar.isInvalid()) |
2967 | 3 | return ExprError(); |
2968 | 10.4k | if (Ivar.isUsable()) |
2969 | 1.26k | return BuildIvarRefExpr(S, Lookup.getNameLoc(), |
2970 | 1.26k | cast<ObjCIvarDecl>(Ivar.get())); |
2971 | | |
2972 | 9.21k | if (Lookup.empty() && II173 && AllowBuiltinCreation173 ) |
2973 | 173 | LookupBuiltin(Lookup); |
2974 | | |
2975 | | // Sentinel value saying that we didn't do anything special. |
2976 | 9.21k | return ExprResult(false); |
2977 | 10.4k | } |
2978 | | |
2979 | | /// Cast a base object to a member's actual type. |
2980 | | /// |
2981 | | /// There are two relevant checks: |
2982 | | /// |
2983 | | /// C++ [class.access.base]p7: |
2984 | | /// |
2985 | | /// If a class member access operator [...] is used to access a non-static |
2986 | | /// data member or non-static member function, the reference is ill-formed if |
2987 | | /// the left operand [...] cannot be implicitly converted to a pointer to the |
2988 | | /// naming class of the right operand. |
2989 | | /// |
2990 | | /// C++ [expr.ref]p7: |
2991 | | /// |
2992 | | /// If E2 is a non-static data member or a non-static member function, the |
2993 | | /// program is ill-formed if the class of which E2 is directly a member is an |
2994 | | /// ambiguous base (11.8) of the naming class (11.9.3) of E2. |
2995 | | /// |
2996 | | /// Note that the latter check does not consider access; the access of the |
2997 | | /// "real" base class is checked as appropriate when checking the access of the |
2998 | | /// member name. |
2999 | | ExprResult |
3000 | | Sema::PerformObjectMemberConversion(Expr *From, |
3001 | | NestedNameSpecifier *Qualifier, |
3002 | | NamedDecl *FoundDecl, |
3003 | 1.11M | NamedDecl *Member) { |
3004 | 1.11M | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
3005 | 1.11M | if (!RD) |
3006 | 121k | return From; |
3007 | | |
3008 | 993k | QualType DestRecordType; |
3009 | 993k | QualType DestType; |
3010 | 993k | QualType FromRecordType; |
3011 | 993k | QualType FromType = From->getType(); |
3012 | 993k | bool PointerConversions = false; |
3013 | 993k | if (isa<FieldDecl>(Member)) { |
3014 | 985k | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
3015 | 985k | auto FromPtrType = FromType->getAs<PointerType>(); |
3016 | 985k | DestRecordType = Context.getAddrSpaceQualType( |
3017 | 985k | DestRecordType, FromPtrType |
3018 | 985k | ? FromType->getPointeeType().getAddressSpace()770k |
3019 | 985k | : FromType.getAddressSpace()214k ); |
3020 | | |
3021 | 985k | if (FromPtrType) { |
3022 | 770k | DestType = Context.getPointerType(DestRecordType); |
3023 | 770k | FromRecordType = FromPtrType->getPointeeType(); |
3024 | 770k | PointerConversions = true; |
3025 | 770k | } else { |
3026 | 214k | DestType = DestRecordType; |
3027 | 214k | FromRecordType = FromType; |
3028 | 214k | } |
3029 | 985k | } else if (CXXMethodDecl *8.01k Method8.01k = dyn_cast<CXXMethodDecl>(Member)) { |
3030 | 8.01k | if (Method->isStatic()) |
3031 | 0 | return From; |
3032 | | |
3033 | 8.01k | DestType = Method->getThisType(); |
3034 | 8.01k | DestRecordType = DestType->getPointeeType(); |
3035 | | |
3036 | 8.01k | if (FromType->getAs<PointerType>()) { |
3037 | 6.12k | FromRecordType = FromType->getPointeeType(); |
3038 | 6.12k | PointerConversions = true; |
3039 | 6.12k | } else { |
3040 | 1.89k | FromRecordType = FromType; |
3041 | 1.89k | DestType = DestRecordType; |
3042 | 1.89k | } |
3043 | | |
3044 | 8.01k | LangAS FromAS = FromRecordType.getAddressSpace(); |
3045 | 8.01k | LangAS DestAS = DestRecordType.getAddressSpace(); |
3046 | 8.01k | if (FromAS != DestAS) { |
3047 | 4 | QualType FromRecordTypeWithoutAS = |
3048 | 4 | Context.removeAddrSpaceQualType(FromRecordType); |
3049 | 4 | QualType FromTypeWithDestAS = |
3050 | 4 | Context.getAddrSpaceQualType(FromRecordTypeWithoutAS, DestAS); |
3051 | 4 | if (PointerConversions) |
3052 | 1 | FromTypeWithDestAS = Context.getPointerType(FromTypeWithDestAS); |
3053 | 4 | From = ImpCastExprToType(From, FromTypeWithDestAS, |
3054 | 4 | CK_AddressSpaceConversion, From->getValueKind()) |
3055 | 4 | .get(); |
3056 | 4 | } |
3057 | 8.01k | } else { |
3058 | | // No conversion necessary. |
3059 | 0 | return From; |
3060 | 0 | } |
3061 | | |
3062 | 993k | if (DestType->isDependentType() || FromType->isDependentType()448k ) |
3063 | 545k | return From; |
3064 | | |
3065 | | // If the unqualified types are the same, no conversion is necessary. |
3066 | 447k | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
3067 | 431k | return From; |
3068 | | |
3069 | 16.7k | SourceRange FromRange = From->getSourceRange(); |
3070 | 16.7k | SourceLocation FromLoc = FromRange.getBegin(); |
3071 | | |
3072 | 16.7k | ExprValueKind VK = From->getValueKind(); |
3073 | | |
3074 | | // C++ [class.member.lookup]p8: |
3075 | | // [...] Ambiguities can often be resolved by qualifying a name with its |
3076 | | // class name. |
3077 | | // |
3078 | | // If the member was a qualified name and the qualified referred to a |
3079 | | // specific base subobject type, we'll cast to that intermediate type |
3080 | | // first and then to the object in which the member is declared. That allows |
3081 | | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
3082 | | // |
3083 | | // class Base { public: int x; }; |
3084 | | // class Derived1 : public Base { }; |
3085 | | // class Derived2 : public Base { }; |
3086 | | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
3087 | | // |
3088 | | // void VeryDerived::f() { |
3089 | | // x = 17; // error: ambiguous base subobjects |
3090 | | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
3091 | | // } |
3092 | 16.7k | if (Qualifier && Qualifier->getAsType()6.34k ) { |
3093 | 6.33k | QualType QType = QualType(Qualifier->getAsType(), 0); |
3094 | 6.33k | assert(QType->isRecordType() && "lookup done with non-record type"); |
3095 | | |
3096 | 0 | QualType QRecordType = QualType(QType->castAs<RecordType>(), 0); |
3097 | | |
3098 | | // In C++98, the qualifier type doesn't actually have to be a base |
3099 | | // type of the object type, in which case we just ignore it. |
3100 | | // Otherwise build the appropriate casts. |
3101 | 6.33k | if (IsDerivedFrom(FromLoc, FromRecordType, QRecordType)) { |
3102 | 6.30k | CXXCastPath BasePath; |
3103 | 6.30k | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
3104 | 6.30k | FromLoc, FromRange, &BasePath)) |
3105 | 18 | return ExprError(); |
3106 | | |
3107 | 6.28k | if (PointerConversions) |
3108 | 6.10k | QType = Context.getPointerType(QType); |
3109 | 6.28k | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
3110 | 6.28k | VK, &BasePath).get(); |
3111 | | |
3112 | 6.28k | FromType = QType; |
3113 | 6.28k | FromRecordType = QRecordType; |
3114 | | |
3115 | | // If the qualifier type was the same as the destination type, |
3116 | | // we're done. |
3117 | 6.28k | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
3118 | 6.13k | return From; |
3119 | 6.28k | } |
3120 | 6.33k | } |
3121 | | |
3122 | 10.5k | CXXCastPath BasePath; |
3123 | 10.5k | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
3124 | 10.5k | FromLoc, FromRange, &BasePath, |
3125 | 10.5k | /*IgnoreAccess=*/true)) |
3126 | 7 | return ExprError(); |
3127 | | |
3128 | 10.5k | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
3129 | 10.5k | VK, &BasePath); |
3130 | 10.5k | } |
3131 | | |
3132 | | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
3133 | | const LookupResult &R, |
3134 | 21.4M | bool HasTrailingLParen) { |
3135 | | // Only when used directly as the postfix-expression of a call. |
3136 | 21.4M | if (!HasTrailingLParen) |
3137 | 16.3M | return false; |
3138 | | |
3139 | | // Never if a scope specifier was provided. |
3140 | 5.02M | if (SS.isSet()) |
3141 | 787k | return false; |
3142 | | |
3143 | | // Only in C++ or ObjC++. |
3144 | 4.23M | if (!getLangOpts().CPlusPlus) |
3145 | 2.40M | return false; |
3146 | | |
3147 | | // Turn off ADL when we find certain kinds of declarations during |
3148 | | // normal lookup: |
3149 | 2.36M | for (NamedDecl *D : R)1.83M { |
3150 | | // C++0x [basic.lookup.argdep]p3: |
3151 | | // -- a declaration of a class member |
3152 | | // Since using decls preserve this property, we check this on the |
3153 | | // original decl. |
3154 | 2.36M | if (D->isCXXClassMember()) |
3155 | 639k | return false; |
3156 | | |
3157 | | // C++0x [basic.lookup.argdep]p3: |
3158 | | // -- a block-scope function declaration that is not a |
3159 | | // using-declaration |
3160 | | // NOTE: we also trigger this for function templates (in fact, we |
3161 | | // don't check the decl type at all, since all other decl types |
3162 | | // turn off ADL anyway). |
3163 | 1.72M | if (isa<UsingShadowDecl>(D)) |
3164 | 74.1k | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
3165 | 1.64M | else if (D->getLexicalDeclContext()->isFunctionOrMethod()) |
3166 | 115k | return false; |
3167 | | |
3168 | | // C++0x [basic.lookup.argdep]p3: |
3169 | | // -- a declaration that is neither a function or a function |
3170 | | // template |
3171 | | // And also for builtin functions. |
3172 | 1.60M | if (isa<FunctionDecl>(D)) { |
3173 | 1.10M | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
3174 | | |
3175 | | // But also builtin functions. |
3176 | 1.10M | if (FDecl->getBuiltinID() && FDecl->isImplicit()548k ) |
3177 | 442k | return false; |
3178 | 1.10M | } else if (505k !isa<FunctionTemplateDecl>(D)505k ) |
3179 | 5.50k | return false; |
3180 | 1.60M | } |
3181 | | |
3182 | 628k | return true; |
3183 | 1.83M | } |
3184 | | |
3185 | | |
3186 | | /// Diagnoses obvious problems with the use of the given declaration |
3187 | | /// as an expression. This is only actually called for lookups that |
3188 | | /// were not overloaded, and it doesn't promise that the declaration |
3189 | | /// will in fact be used. |
3190 | 19.1M | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
3191 | 19.1M | if (D->isInvalidDecl()) |
3192 | 952 | return true; |
3193 | | |
3194 | 19.1M | if (isa<TypedefNameDecl>(D)) { |
3195 | 3 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
3196 | 3 | return true; |
3197 | 3 | } |
3198 | | |
3199 | 19.1M | if (isa<ObjCInterfaceDecl>(D)) { |
3200 | 2 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
3201 | 2 | return true; |
3202 | 2 | } |
3203 | | |
3204 | 19.1M | if (isa<NamespaceDecl>(D)) { |
3205 | 5 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
3206 | 5 | return true; |
3207 | 5 | } |
3208 | | |
3209 | 19.1M | return false; |
3210 | 19.1M | } |
3211 | | |
3212 | | // Certain multiversion types should be treated as overloaded even when there is |
3213 | | // only one result. |
3214 | 17.7M | static bool ShouldLookupResultBeMultiVersionOverload(const LookupResult &R) { |
3215 | 17.7M | assert(R.isSingleResult() && "Expected only a single result"); |
3216 | 0 | const auto *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); |
3217 | 17.7M | return FD && |
3218 | 17.7M | (2.98M FD->isCPUDispatchMultiVersion()2.98M || FD->isCPUSpecificMultiVersion()2.98M ); |
3219 | 17.7M | } |
3220 | | |
3221 | | ExprResult Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
3222 | | LookupResult &R, bool NeedsADL, |
3223 | 18.4M | bool AcceptInvalidDecl) { |
3224 | | // If this is a single, fully-resolved result and we don't need ADL, |
3225 | | // just build an ordinary singleton decl ref. |
3226 | 18.4M | if (!NeedsADL && R.isSingleResult()18.0M && |
3227 | 18.4M | !R.getAsSingle<FunctionTemplateDecl>()17.5M && |
3228 | 18.4M | !ShouldLookupResultBeMultiVersionOverload(R)17.5M ) |
3229 | 17.5M | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), R.getFoundDecl(), |
3230 | 17.5M | R.getRepresentativeDecl(), nullptr, |
3231 | 17.5M | AcceptInvalidDecl); |
3232 | | |
3233 | | // We only need to check the declaration if there's exactly one |
3234 | | // result, because in the overloaded case the results can only be |
3235 | | // functions and function templates. |
3236 | 844k | if (R.isSingleResult() && !ShouldLookupResultBeMultiVersionOverload(R)180k && |
3237 | 844k | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())180k ) |
3238 | 18 | return ExprError(); |
3239 | | |
3240 | | // Otherwise, just build an unresolved lookup expression. Suppress |
3241 | | // any lookup-related diagnostics; we'll hash these out later, when |
3242 | | // we've picked a target. |
3243 | 844k | R.suppressDiagnostics(); |
3244 | | |
3245 | 844k | UnresolvedLookupExpr *ULE |
3246 | 844k | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
3247 | 844k | SS.getWithLocInContext(Context), |
3248 | 844k | R.getLookupNameInfo(), |
3249 | 844k | NeedsADL, R.isOverloadedResult(), |
3250 | 844k | R.begin(), R.end()); |
3251 | | |
3252 | 844k | return ULE; |
3253 | 844k | } |
3254 | | |
3255 | | static void diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
3256 | | ValueDecl *var); |
3257 | | |
3258 | | /// Complete semantic analysis for a reference to the given declaration. |
3259 | | ExprResult Sema::BuildDeclarationNameExpr( |
3260 | | const CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo, NamedDecl *D, |
3261 | | NamedDecl *FoundD, const TemplateArgumentListInfo *TemplateArgs, |
3262 | 19.0M | bool AcceptInvalidDecl) { |
3263 | 19.0M | assert(D && "Cannot refer to a NULL declaration"); |
3264 | 0 | assert(!isa<FunctionTemplateDecl>(D) && |
3265 | 19.0M | "Cannot refer unambiguously to a function template"); |
3266 | | |
3267 | 0 | SourceLocation Loc = NameInfo.getLoc(); |
3268 | 19.0M | if (CheckDeclInExpr(*this, Loc, D)) { |
3269 | | // Recovery from invalid cases (e.g. D is an invalid Decl). |
3270 | | // We use the dependent type for the RecoveryExpr to prevent bogus follow-up |
3271 | | // diagnostics, as invalid decls use int as a fallback type. |
3272 | 944 | return CreateRecoveryExpr(NameInfo.getBeginLoc(), NameInfo.getEndLoc(), {}); |
3273 | 944 | } |
3274 | | |
3275 | 18.9M | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
3276 | | // Specifically diagnose references to class templates that are missing |
3277 | | // a template argument list. |
3278 | 12 | diagnoseMissingTemplateArguments(TemplateName(Template), Loc); |
3279 | 12 | return ExprError(); |
3280 | 12 | } |
3281 | | |
3282 | | // Make sure that we're referring to a value. |
3283 | 18.9M | if (!isa<ValueDecl, UnresolvedUsingIfExistsDecl>(D)) { |
3284 | 3.61k | Diag(Loc, diag::err_ref_non_value) << D << SS.getRange(); |
3285 | 3.61k | Diag(D->getLocation(), diag::note_declared_at); |
3286 | 3.61k | return ExprError(); |
3287 | 3.61k | } |
3288 | | |
3289 | | // Check whether this declaration can be used. Note that we suppress |
3290 | | // this check when we're going to perform argument-dependent lookup |
3291 | | // on this function name, because this might not be the function |
3292 | | // that overload resolution actually selects. |
3293 | 18.9M | if (DiagnoseUseOfDecl(D, Loc)) |
3294 | 138 | return ExprError(); |
3295 | | |
3296 | 18.9M | auto *VD = cast<ValueDecl>(D); |
3297 | | |
3298 | | // Only create DeclRefExpr's for valid Decl's. |
3299 | 18.9M | if (VD->isInvalidDecl() && !AcceptInvalidDecl18 ) |
3300 | 18 | return ExprError(); |
3301 | | |
3302 | | // Handle members of anonymous structs and unions. If we got here, |
3303 | | // and the reference is to a class member indirect field, then this |
3304 | | // must be the subject of a pointer-to-member expression. |
3305 | 18.9M | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
3306 | 114 | if (!indirectField->isCXXClassMember()) |
3307 | 74 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
3308 | 74 | indirectField); |
3309 | | |
3310 | 18.9M | QualType type = VD->getType(); |
3311 | 18.9M | if (type.isNull()) |
3312 | 1 | return ExprError(); |
3313 | 18.9M | ExprValueKind valueKind = VK_PRValue; |
3314 | | |
3315 | | // In 'T ...V;', the type of the declaration 'V' is 'T...', but the type of |
3316 | | // a reference to 'V' is simply (unexpanded) 'T'. The type, like the value, |
3317 | | // is expanded by some outer '...' in the context of the use. |
3318 | 18.9M | type = type.getNonPackExpansionType(); |
3319 | | |
3320 | 18.9M | switch (D->getKind()) { |
3321 | | // Ignore all the non-ValueDecl kinds. |
3322 | 0 | #define ABSTRACT_DECL(kind) |
3323 | 0 | #define VALUE(type, base) |
3324 | 0 | #define DECL(type, base) case Decl::type: |
3325 | 0 | #include "clang/AST/DeclNodes.inc" |
3326 | 0 | llvm_unreachable("invalid value decl kind"); |
3327 | | |
3328 | | // These shouldn't make it here. |
3329 | 0 | case Decl::ObjCAtDefsField: |
3330 | 0 | llvm_unreachable("forming non-member reference to ivar?"); |
3331 | | |
3332 | | // Enum constants are always r-values and never references. |
3333 | | // Unresolved using declarations are dependent. |
3334 | 418k | case Decl::EnumConstant: |
3335 | 418k | case Decl::UnresolvedUsingValue: |
3336 | 418k | case Decl::OMPDeclareReduction: |
3337 | 418k | case Decl::OMPDeclareMapper: |
3338 | 418k | valueKind = VK_PRValue; |
3339 | 418k | break; |
3340 | | |
3341 | | // Fields and indirect fields that got here must be for |
3342 | | // pointer-to-member expressions; we just call them l-values for |
3343 | | // internal consistency, because this subexpression doesn't really |
3344 | | // exist in the high-level semantics. |
3345 | 958 | case Decl::Field: |
3346 | 998 | case Decl::IndirectField: |
3347 | 1.00k | case Decl::ObjCIvar: |
3348 | 1.00k | assert(getLangOpts().CPlusPlus && "building reference to field in C?"); |
3349 | | |
3350 | | // These can't have reference type in well-formed programs, but |
3351 | | // for internal consistency we do this anyway. |
3352 | 0 | type = type.getNonReferenceType(); |
3353 | 1.00k | valueKind = VK_LValue; |
3354 | 1.00k | break; |
3355 | | |
3356 | | // Non-type template parameters are either l-values or r-values |
3357 | | // depending on the type. |
3358 | 409k | case Decl::NonTypeTemplateParm: { |
3359 | 409k | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
3360 | 48 | type = reftype->getPointeeType(); |
3361 | 48 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
3362 | 48 | break; |
3363 | 48 | } |
3364 | | |
3365 | | // [expr.prim.id.unqual]p2: |
3366 | | // If the entity is a template parameter object for a template |
3367 | | // parameter of type T, the type of the expression is const T. |
3368 | | // [...] The expression is an lvalue if the entity is a [...] template |
3369 | | // parameter object. |
3370 | 409k | if (type->isRecordType()) { |
3371 | 32 | type = type.getUnqualifiedType().withConst(); |
3372 | 32 | valueKind = VK_LValue; |
3373 | 32 | break; |
3374 | 32 | } |
3375 | | |
3376 | | // For non-references, we need to strip qualifiers just in case |
3377 | | // the template parameter was declared as 'const int' or whatever. |
3378 | 409k | valueKind = VK_PRValue; |
3379 | 409k | type = type.getUnqualifiedType(); |
3380 | 409k | break; |
3381 | 409k | } |
3382 | | |
3383 | 5.83M | case Decl::Var: |
3384 | 5.84M | case Decl::VarTemplateSpecialization: |
3385 | 5.84M | case Decl::VarTemplatePartialSpecialization: |
3386 | 5.84M | case Decl::Decomposition: |
3387 | 5.85M | case Decl::OMPCapturedExpr: |
3388 | | // In C, "extern void blah;" is valid and is an r-value. |
3389 | 5.85M | if (!getLangOpts().CPlusPlus && !type.hasQualifiers()1.05M && |
3390 | 5.85M | type->isVoidType()1.04M ) { |
3391 | 9 | valueKind = VK_PRValue; |
3392 | 9 | break; |
3393 | 9 | } |
3394 | 5.85M | LLVM_FALLTHROUGH5.85M ;5.85M |
3395 | | |
3396 | 5.86M | case Decl::ImplicitParam: |
3397 | 15.3M | case Decl::ParmVar: { |
3398 | | // These are always l-values. |
3399 | 15.3M | valueKind = VK_LValue; |
3400 | 15.3M | type = type.getNonReferenceType(); |
3401 | | |
3402 | | // FIXME: Does the addition of const really only apply in |
3403 | | // potentially-evaluated contexts? Since the variable isn't actually |
3404 | | // captured in an unevaluated context, it seems that the answer is no. |
3405 | 15.3M | if (!isUnevaluatedContext()) { |
3406 | 15.2M | QualType CapturedType = getCapturedDeclRefType(cast<VarDecl>(VD), Loc); |
3407 | 15.2M | if (!CapturedType.isNull()) |
3408 | 606k | type = CapturedType; |
3409 | 15.2M | } |
3410 | | |
3411 | 15.3M | break; |
3412 | 5.86M | } |
3413 | | |
3414 | 683 | case Decl::Binding: { |
3415 | | // These are always lvalues. |
3416 | 683 | valueKind = VK_LValue; |
3417 | 683 | type = type.getNonReferenceType(); |
3418 | | // FIXME: Support lambda-capture of BindingDecls, once CWG actually |
3419 | | // decides how that's supposed to work. |
3420 | 683 | auto *BD = cast<BindingDecl>(VD); |
3421 | 683 | if (BD->getDeclContext() != CurContext) { |
3422 | 17 | auto *DD = dyn_cast_or_null<VarDecl>(BD->getDecomposedDecl()); |
3423 | 17 | if (DD && DD->hasLocalStorage()) |
3424 | 2 | diagnoseUncapturableValueReference(*this, Loc, BD); |
3425 | 17 | } |
3426 | 683 | break; |
3427 | 5.86M | } |
3428 | | |
3429 | 2.72M | case Decl::Function: { |
3430 | 2.72M | if (unsigned BID = cast<FunctionDecl>(VD)->getBuiltinID()) { |
3431 | 1.87M | if (!Context.BuiltinInfo.isDirectlyAddressable(BID)) { |
3432 | 1.80M | type = Context.BuiltinFnTy; |
3433 | 1.80M | valueKind = VK_PRValue; |
3434 | 1.80M | break; |
3435 | 1.80M | } |
3436 | 1.87M | } |
3437 | | |
3438 | 919k | const FunctionType *fty = type->castAs<FunctionType>(); |
3439 | | |
3440 | | // If we're referring to a function with an __unknown_anytype |
3441 | | // result type, make the entire expression __unknown_anytype. |
3442 | 919k | if (fty->getReturnType() == Context.UnknownAnyTy) { |
3443 | 6 | type = Context.UnknownAnyTy; |
3444 | 6 | valueKind = VK_PRValue; |
3445 | 6 | break; |
3446 | 6 | } |
3447 | | |
3448 | | // Functions are l-values in C++. |
3449 | 919k | if (getLangOpts().CPlusPlus) { |
3450 | 73.5k | valueKind = VK_LValue; |
3451 | 73.5k | break; |
3452 | 73.5k | } |
3453 | | |
3454 | | // C99 DR 316 says that, if a function type comes from a |
3455 | | // function definition (without a prototype), that type is only |
3456 | | // used for checking compatibility. Therefore, when referencing |
3457 | | // the function, we pretend that we don't have the full function |
3458 | | // type. |
3459 | 846k | if (!cast<FunctionDecl>(VD)->hasPrototype() && isa<FunctionProtoType>(fty)1.33k ) |
3460 | 62 | type = Context.getFunctionNoProtoType(fty->getReturnType(), |
3461 | 62 | fty->getExtInfo()); |
3462 | | |
3463 | | // Functions are r-values in C. |
3464 | 846k | valueKind = VK_PRValue; |
3465 | 846k | break; |
3466 | 919k | } |
3467 | | |
3468 | 0 | case Decl::CXXDeductionGuide: |
3469 | 0 | llvm_unreachable("building reference to deduction guide"); |
3470 | |
|
3471 | 3 | case Decl::MSProperty: |
3472 | 15 | case Decl::MSGuid: |
3473 | 127 | case Decl::TemplateParamObject: |
3474 | | // FIXME: Should MSGuidDecl and template parameter objects be subject to |
3475 | | // capture in OpenMP, or duplicated between host and device? |
3476 | 127 | valueKind = VK_LValue; |
3477 | 127 | break; |
3478 | | |
3479 | 0 | case Decl::UnnamedGlobalConstant: |
3480 | 0 | valueKind = VK_LValue; |
3481 | 0 | break; |
3482 | | |
3483 | 105k | case Decl::CXXMethod: |
3484 | | // If we're referring to a method with an __unknown_anytype |
3485 | | // result type, make the entire expression __unknown_anytype. |
3486 | | // This should only be possible with a type written directly. |
3487 | 105k | if (const FunctionProtoType *proto = |
3488 | 105k | dyn_cast<FunctionProtoType>(VD->getType())) |
3489 | 105k | if (proto->getReturnType() == Context.UnknownAnyTy) { |
3490 | 0 | type = Context.UnknownAnyTy; |
3491 | 0 | valueKind = VK_PRValue; |
3492 | 0 | break; |
3493 | 0 | } |
3494 | | |
3495 | | // C++ methods are l-values if static, r-values if non-static. |
3496 | 105k | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
3497 | 103k | valueKind = VK_LValue; |
3498 | 103k | break; |
3499 | 103k | } |
3500 | 105k | LLVM_FALLTHROUGH1.65k ;1.65k |
3501 | | |
3502 | 1.67k | case Decl::CXXConversion: |
3503 | 1.67k | case Decl::CXXDestructor: |
3504 | 1.67k | case Decl::CXXConstructor: |
3505 | 1.67k | valueKind = VK_PRValue; |
3506 | 1.67k | break; |
3507 | 18.9M | } |
3508 | | |
3509 | 18.9M | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS, FoundD, |
3510 | 18.9M | /*FIXME: TemplateKWLoc*/ SourceLocation(), |
3511 | 18.9M | TemplateArgs); |
3512 | 18.9M | } |
3513 | | |
3514 | | static void ConvertUTF8ToWideString(unsigned CharByteWidth, StringRef Source, |
3515 | 14 | SmallString<32> &Target) { |
3516 | 14 | Target.resize(CharByteWidth * (Source.size() + 1)); |
3517 | 14 | char *ResultPtr = &Target[0]; |
3518 | 14 | const llvm::UTF8 *ErrorPtr; |
3519 | 14 | bool success = |
3520 | 14 | llvm::ConvertUTF8toWide(CharByteWidth, Source, ResultPtr, ErrorPtr); |
3521 | 14 | (void)success; |
3522 | 14 | assert(success); |
3523 | 0 | Target.resize(ResultPtr - &Target[0]); |
3524 | 14 | } |
3525 | | |
3526 | | ExprResult Sema::BuildPredefinedExpr(SourceLocation Loc, |
3527 | 896 | PredefinedExpr::IdentKind IK) { |
3528 | | // Pick the current block, lambda, captured statement or function. |
3529 | 896 | Decl *currentDecl = nullptr; |
3530 | 896 | if (const BlockScopeInfo *BSI = getCurBlock()) |
3531 | 41 | currentDecl = BSI->TheDecl; |
3532 | 855 | else if (const LambdaScopeInfo *LSI = getCurLambda()) |
3533 | 24 | currentDecl = LSI->CallOperator; |
3534 | 831 | else if (const CapturedRegionScopeInfo *CSI = getCurCapturedRegion()) |
3535 | 23 | currentDecl = CSI->TheCapturedDecl; |
3536 | 808 | else |
3537 | 808 | currentDecl = getCurFunctionOrMethodDecl(); |
3538 | | |
3539 | 896 | if (!currentDecl) { |
3540 | 5 | Diag(Loc, diag::ext_predef_outside_function); |
3541 | 5 | currentDecl = Context.getTranslationUnitDecl(); |
3542 | 5 | } |
3543 | | |
3544 | 896 | QualType ResTy; |
3545 | 896 | StringLiteral *SL = nullptr; |
3546 | 896 | if (cast<DeclContext>(currentDecl)->isDependentContext()) |
3547 | 83 | ResTy = Context.DependentTy; |
3548 | 813 | else { |
3549 | | // Pre-defined identifiers are of type char[x], where x is the length of |
3550 | | // the string. |
3551 | 813 | auto Str = PredefinedExpr::ComputeName(IK, currentDecl); |
3552 | 813 | unsigned Length = Str.length(); |
3553 | | |
3554 | 813 | llvm::APInt LengthI(32, Length + 1); |
3555 | 813 | if (IK == PredefinedExpr::LFunction || IK == PredefinedExpr::LFuncSig805 ) { |
3556 | 14 | ResTy = |
3557 | 14 | Context.adjustStringLiteralBaseType(Context.WideCharTy.withConst()); |
3558 | 14 | SmallString<32> RawChars; |
3559 | 14 | ConvertUTF8ToWideString(Context.getTypeSizeInChars(ResTy).getQuantity(), |
3560 | 14 | Str, RawChars); |
3561 | 14 | ResTy = Context.getConstantArrayType(ResTy, LengthI, nullptr, |
3562 | 14 | ArrayType::Normal, |
3563 | 14 | /*IndexTypeQuals*/ 0); |
3564 | 14 | SL = StringLiteral::Create(Context, RawChars, StringLiteral::Wide, |
3565 | 14 | /*Pascal*/ false, ResTy, Loc); |
3566 | 799 | } else { |
3567 | 799 | ResTy = Context.adjustStringLiteralBaseType(Context.CharTy.withConst()); |
3568 | 799 | ResTy = Context.getConstantArrayType(ResTy, LengthI, nullptr, |
3569 | 799 | ArrayType::Normal, |
3570 | 799 | /*IndexTypeQuals*/ 0); |
3571 | 799 | SL = StringLiteral::Create(Context, Str, StringLiteral::Ordinary, |
3572 | 799 | /*Pascal*/ false, ResTy, Loc); |
3573 | 799 | } |
3574 | 813 | } |
3575 | | |
3576 | 896 | return PredefinedExpr::Create(Context, Loc, ResTy, IK, SL); |
3577 | 896 | } |
3578 | | |
3579 | | ExprResult Sema::BuildSYCLUniqueStableNameExpr(SourceLocation OpLoc, |
3580 | | SourceLocation LParen, |
3581 | | SourceLocation RParen, |
3582 | 84 | TypeSourceInfo *TSI) { |
3583 | 84 | return SYCLUniqueStableNameExpr::Create(Context, OpLoc, LParen, RParen, TSI); |
3584 | 84 | } |
3585 | | |
3586 | | ExprResult Sema::ActOnSYCLUniqueStableNameExpr(SourceLocation OpLoc, |
3587 | | SourceLocation LParen, |
3588 | | SourceLocation RParen, |
3589 | 62 | ParsedType ParsedTy) { |
3590 | 62 | TypeSourceInfo *TSI = nullptr; |
3591 | 62 | QualType Ty = GetTypeFromParser(ParsedTy, &TSI); |
3592 | | |
3593 | 62 | if (Ty.isNull()) |
3594 | 0 | return ExprError(); |
3595 | 62 | if (!TSI) |
3596 | 0 | TSI = Context.getTrivialTypeSourceInfo(Ty, LParen); |
3597 | | |
3598 | 62 | return BuildSYCLUniqueStableNameExpr(OpLoc, LParen, RParen, TSI); |
3599 | 62 | } |
3600 | | |
3601 | 821 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
3602 | 821 | PredefinedExpr::IdentKind IK; |
3603 | | |
3604 | 821 | switch (Kind) { |
3605 | 0 | default: llvm_unreachable("Unknown simple primary expr!"); |
3606 | 365 | case tok::kw___func__: IK = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
3607 | 201 | case tok::kw___FUNCTION__: IK = PredefinedExpr::Function; break; |
3608 | 9 | case tok::kw___FUNCDNAME__: IK = PredefinedExpr::FuncDName; break; // [MS] |
3609 | 13 | case tok::kw___FUNCSIG__: IK = PredefinedExpr::FuncSig; break; // [MS] |
3610 | 8 | case tok::kw_L__FUNCTION__: IK = PredefinedExpr::LFunction; break; // [MS] |
3611 | 6 | case tok::kw_L__FUNCSIG__: IK = PredefinedExpr::LFuncSig; break; // [MS] |
3612 | 219 | case tok::kw___PRETTY_FUNCTION__: IK = PredefinedExpr::PrettyFunction; break; |
3613 | 821 | } |
3614 | | |
3615 | 821 | return BuildPredefinedExpr(Loc, IK); |
3616 | 821 | } |
3617 | | |
3618 | 567k | ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) { |
3619 | 567k | SmallString<16> CharBuffer; |
3620 | 567k | bool Invalid = false; |
3621 | 567k | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
3622 | 567k | if (Invalid) |
3623 | 0 | return ExprError(); |
3624 | | |
3625 | 567k | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
3626 | 567k | PP, Tok.getKind()); |
3627 | 567k | if (Literal.hadError()) |
3628 | 191 | return ExprError(); |
3629 | | |
3630 | 567k | QualType Ty; |
3631 | 567k | if (Literal.isWide()) |
3632 | 1.10k | Ty = Context.WideCharTy; // L'x' -> wchar_t in C and C++. |
3633 | 566k | else if (Literal.isUTF8() && getLangOpts().C2x151 ) |
3634 | 2 | Ty = Context.UnsignedCharTy; // u8'x' -> unsigned char in C2x |
3635 | 566k | else if (Literal.isUTF8() && getLangOpts().Char8149 ) |
3636 | 139 | Ty = Context.Char8Ty; // u8'x' -> char8_t when it exists. |
3637 | 565k | else if (Literal.isUTF16()) |
3638 | 110 | Ty = Context.Char16Ty; // u'x' -> char16_t in C11 and C++11. |
3639 | 565k | else if (Literal.isUTF32()) |
3640 | 108 | Ty = Context.Char32Ty; // U'x' -> char32_t in C11 and C++11. |
3641 | 565k | else if (!getLangOpts().CPlusPlus || Literal.isMultiChar()102k ) |
3642 | 526k | Ty = Context.IntTy; // 'x' -> int in C, 'wxyz' -> int in C++. |
3643 | 38.9k | else |
3644 | 38.9k | Ty = Context.CharTy; // 'x' -> char in C++; |
3645 | | // u8'x' -> char in C11-C17 and in C++ without char8_t. |
3646 | | |
3647 | 567k | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
3648 | 567k | if (Literal.isWide()) |
3649 | 1.10k | Kind = CharacterLiteral::Wide; |
3650 | 566k | else if (Literal.isUTF16()) |
3651 | 110 | Kind = CharacterLiteral::UTF16; |
3652 | 565k | else if (Literal.isUTF32()) |
3653 | 108 | Kind = CharacterLiteral::UTF32; |
3654 | 565k | else if (Literal.isUTF8()) |
3655 | 151 | Kind = CharacterLiteral::UTF8; |
3656 | | |
3657 | 567k | Expr *Lit = new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
3658 | 567k | Tok.getLocation()); |
3659 | | |
3660 | 567k | if (Literal.getUDSuffix().empty()) |
3661 | 567k | return Lit; |
3662 | | |
3663 | | // We're building a user-defined literal. |
3664 | 57 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
3665 | 57 | SourceLocation UDSuffixLoc = |
3666 | 57 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); |
3667 | | |
3668 | | // Make sure we're allowed user-defined literals here. |
3669 | 57 | if (!UDLScope) |
3670 | 0 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_character_udl)); |
3671 | | |
3672 | | // C++11 [lex.ext]p6: The literal L is treated as a call of the form |
3673 | | // operator "" X (ch) |
3674 | 57 | return BuildCookedLiteralOperatorCall(*this, UDLScope, UDSuffix, UDSuffixLoc, |
3675 | 57 | Lit, Tok.getLocation()); |
3676 | 57 | } |
3677 | | |
3678 | 5.04M | ExprResult Sema::ActOnIntegerConstant(SourceLocation Loc, uint64_t Val) { |
3679 | 5.04M | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
3680 | 5.04M | return IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val), |
3681 | 5.04M | Context.IntTy, Loc); |
3682 | 5.04M | } |
3683 | | |
3684 | | static Expr *BuildFloatingLiteral(Sema &S, NumericLiteralParser &Literal, |
3685 | 42.5k | QualType Ty, SourceLocation Loc) { |
3686 | 42.5k | const llvm::fltSemantics &Format = S.Context.getFloatTypeSemantics(Ty); |
3687 | | |
3688 | 42.5k | using llvm::APFloat; |
3689 | 42.5k | APFloat Val(Format); |
3690 | | |
3691 | 42.5k | APFloat::opStatus result = Literal.GetFloatValue(Val); |
3692 | | |
3693 | | // Overflow is always an error, but underflow is only an error if |
3694 | | // we underflowed to zero (APFloat reports denormals as underflow). |
3695 | 42.5k | if ((result & APFloat::opOverflow) || |
3696 | 42.5k | (42.5k (result & APFloat::opUnderflow)42.5k && Val.isZero()1.49k )) { |
3697 | 26 | unsigned diagnostic; |
3698 | 26 | SmallString<20> buffer; |
3699 | 26 | if (result & APFloat::opOverflow) { |
3700 | 14 | diagnostic = diag::warn_float_overflow; |
3701 | 14 | APFloat::getLargest(Format).toString(buffer); |
3702 | 14 | } else { |
3703 | 12 | diagnostic = diag::warn_float_underflow; |
3704 | 12 | APFloat::getSmallest(Format).toString(buffer); |
3705 | 12 | } |
3706 | | |
3707 | 26 | S.Diag(Loc, diagnostic) |
3708 | 26 | << Ty |
3709 | 26 | << StringRef(buffer.data(), buffer.size()); |
3710 | 26 | } |
3711 | | |
3712 | 42.5k | bool isExact = (result == APFloat::opOK); |
3713 | 42.5k | return FloatingLiteral::Create(S.Context, Val, isExact, Ty, Loc); |
3714 | 42.5k | } |
3715 | | |
3716 | 305 | bool Sema::CheckLoopHintExpr(Expr *E, SourceLocation Loc) { |
3717 | 305 | assert(E && "Invalid expression"); |
3718 | | |
3719 | 305 | if (E->isValueDependent()) |
3720 | 66 | return false; |
3721 | | |
3722 | 239 | QualType QT = E->getType(); |
3723 | 239 | if (!QT->isIntegerType() || QT->isBooleanType()238 || QT->isCharType()237 ) { |
3724 | 3 | Diag(E->getExprLoc(), diag::err_pragma_loop_invalid_argument_type) << QT; |
3725 | 3 | return true; |
3726 | 3 | } |
3727 | | |
3728 | 236 | llvm::APSInt ValueAPS; |
3729 | 236 | ExprResult R = VerifyIntegerConstantExpression(E, &ValueAPS); |
3730 | | |
3731 | 236 | if (R.isInvalid()) |
3732 | 1 | return true; |
3733 | | |
3734 | 235 | bool ValueIsPositive = ValueAPS.isStrictlyPositive(); |
3735 | 235 | if (!ValueIsPositive || ValueAPS.getActiveBits() > 31221 ) { |
3736 | 19 | Diag(E->getExprLoc(), diag::err_pragma_loop_invalid_argument_value) |
3737 | 19 | << toString(ValueAPS, 10) << ValueIsPositive; |
3738 | 19 | return true; |
3739 | 19 | } |
3740 | | |
3741 | 216 | return false; |
3742 | 235 | } |
3743 | | |
3744 | 8.32M | ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { |
3745 | | // Fast path for a single digit (which is quite common). A single digit |
3746 | | // cannot have a trigraph, escaped newline, radix prefix, or suffix. |
3747 | 8.32M | if (Tok.getLength() == 1) { |
3748 | 3.91M | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
3749 | 3.91M | return ActOnIntegerConstant(Tok.getLocation(), Val-'0'); |
3750 | 3.91M | } |
3751 | | |
3752 | 4.41M | SmallString<128> SpellingBuffer; |
3753 | | // NumericLiteralParser wants to overread by one character. Add padding to |
3754 | | // the buffer in case the token is copied to the buffer. If getSpelling() |
3755 | | // returns a StringRef to the memory buffer, it should have a null char at |
3756 | | // the EOF, so it is also safe. |
3757 | 4.41M | SpellingBuffer.resize(Tok.getLength() + 1); |
3758 | | |
3759 | | // Get the spelling of the token, which eliminates trigraphs, etc. |
3760 | 4.41M | bool Invalid = false; |
3761 | 4.41M | StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid); |
3762 | 4.41M | if (Invalid) |
3763 | 0 | return ExprError(); |
3764 | | |
3765 | 4.41M | NumericLiteralParser Literal(TokSpelling, Tok.getLocation(), |
3766 | 4.41M | PP.getSourceManager(), PP.getLangOpts(), |
3767 | 4.41M | PP.getTargetInfo(), PP.getDiagnostics()); |
3768 | 4.41M | if (Literal.hadError) |
3769 | 252 | return ExprError(); |
3770 | | |
3771 | 4.41M | if (Literal.hasUDSuffix()) { |
3772 | | // We're building a user-defined literal. |
3773 | 217 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
3774 | 217 | SourceLocation UDSuffixLoc = |
3775 | 217 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); |
3776 | | |
3777 | | // Make sure we're allowed user-defined literals here. |
3778 | 217 | if (!UDLScope) |
3779 | 0 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_numeric_udl)); |
3780 | | |
3781 | 217 | QualType CookedTy; |
3782 | 217 | if (Literal.isFloatingLiteral()) { |
3783 | | // C++11 [lex.ext]p4: If S contains a literal operator with parameter type |
3784 | | // long double, the literal is treated as a call of the form |
3785 | | // operator "" X (f L) |
3786 | 78 | CookedTy = Context.LongDoubleTy; |
3787 | 139 | } else { |
3788 | | // C++11 [lex.ext]p3: If S contains a literal operator with parameter type |
3789 | | // unsigned long long, the literal is treated as a call of the form |
3790 | | // operator "" X (n ULL) |
3791 | 139 | CookedTy = Context.UnsignedLongLongTy; |
3792 | 139 | } |
3793 | | |
3794 | 217 | DeclarationName OpName = |
3795 | 217 | Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); |
3796 | 217 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); |
3797 | 217 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); |
3798 | | |
3799 | 217 | SourceLocation TokLoc = Tok.getLocation(); |
3800 | | |
3801 | | // Perform literal operator lookup to determine if we're building a raw |
3802 | | // literal or a cooked one. |
3803 | 217 | LookupResult R(*this, OpName, UDSuffixLoc, LookupOrdinaryName); |
3804 | 217 | switch (LookupLiteralOperator(UDLScope, R, CookedTy, |
3805 | 217 | /*AllowRaw*/ true, /*AllowTemplate*/ true, |
3806 | 217 | /*AllowStringTemplatePack*/ false, |
3807 | 217 | /*DiagnoseMissing*/ !Literal.isImaginary)) { |
3808 | 30 | case LOLR_ErrorNoDiagnostic: |
3809 | | // Lookup failure for imaginary constants isn't fatal, there's still the |
3810 | | // GNU extension producing _Complex types. |
3811 | 30 | break; |
3812 | 18 | case LOLR_Error: |
3813 | 18 | return ExprError(); |
3814 | 107 | case LOLR_Cooked: { |
3815 | 107 | Expr *Lit; |
3816 | 107 | if (Literal.isFloatingLiteral()) { |
3817 | 37 | Lit = BuildFloatingLiteral(*this, Literal, CookedTy, Tok.getLocation()); |
3818 | 70 | } else { |
3819 | 70 | llvm::APInt ResultVal(Context.getTargetInfo().getLongLongWidth(), 0); |
3820 | 70 | if (Literal.GetIntegerValue(ResultVal)) |
3821 | 1 | Diag(Tok.getLocation(), diag::err_integer_literal_too_large) |
3822 | 1 | << /* Unsigned */ 1; |
3823 | 70 | Lit = IntegerLiteral::Create(Context, ResultVal, CookedTy, |
3824 | 70 | Tok.getLocation()); |
3825 | 70 | } |
3826 | 107 | return BuildLiteralOperatorCall(R, OpNameInfo, Lit, TokLoc); |
3827 | 0 | } |
3828 | | |
3829 | 33 | case LOLR_Raw: { |
3830 | | // C++11 [lit.ext]p3, p4: If S contains a raw literal operator, the |
3831 | | // literal is treated as a call of the form |
3832 | | // operator "" X ("n") |
3833 | 33 | unsigned Length = Literal.getUDSuffixOffset(); |
3834 | 33 | QualType StrTy = Context.getConstantArrayType( |
3835 | 33 | Context.adjustStringLiteralBaseType(Context.CharTy.withConst()), |
3836 | 33 | llvm::APInt(32, Length + 1), nullptr, ArrayType::Normal, 0); |
3837 | 33 | Expr *Lit = |
3838 | 33 | StringLiteral::Create(Context, StringRef(TokSpelling.data(), Length), |
3839 | 33 | StringLiteral::Ordinary, |
3840 | 33 | /*Pascal*/ false, StrTy, &TokLoc, 1); |
3841 | 33 | return BuildLiteralOperatorCall(R, OpNameInfo, Lit, TokLoc); |
3842 | 0 | } |
3843 | | |
3844 | 29 | case LOLR_Template: { |
3845 | | // C++11 [lit.ext]p3, p4: Otherwise (S contains a literal operator |
3846 | | // template), L is treated as a call fo the form |
3847 | | // operator "" X <'c1', 'c2', ... 'ck'>() |
3848 | | // where n is the source character sequence c1 c2 ... ck. |
3849 | 29 | TemplateArgumentListInfo ExplicitArgs; |
3850 | 29 | unsigned CharBits = Context.getIntWidth(Context.CharTy); |
3851 | 29 | bool CharIsUnsigned = Context.CharTy->isUnsignedIntegerType(); |
3852 | 29 | llvm::APSInt Value(CharBits, CharIsUnsigned); |
3853 | 184 | for (unsigned I = 0, N = Literal.getUDSuffixOffset(); I != N; ++I155 ) { |
3854 | 155 | Value = TokSpelling[I]; |
3855 | 155 | TemplateArgument Arg(Context, Value, Context.CharTy); |
3856 | 155 | TemplateArgumentLocInfo ArgInfo; |
3857 | 155 | ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); |
3858 | 155 | } |
3859 | 29 | return BuildLiteralOperatorCall(R, OpNameInfo, None, TokLoc, |
3860 | 29 | &ExplicitArgs); |
3861 | 0 | } |
3862 | 0 | case LOLR_StringTemplatePack: |
3863 | 0 | llvm_unreachable("unexpected literal operator lookup result"); |
3864 | 217 | } |
3865 | 217 | } |
3866 | | |
3867 | 4.41M | Expr *Res; |
3868 | | |
3869 | 4.41M | if (Literal.isFixedPointLiteral()) { |
3870 | 808 | QualType Ty; |
3871 | | |
3872 | 808 | if (Literal.isAccum) { |
3873 | 607 | if (Literal.isHalf) { |
3874 | 416 | Ty = Context.ShortAccumTy; |
3875 | 416 | } else if (191 Literal.isLong191 ) { |
3876 | 51 | Ty = Context.LongAccumTy; |
3877 | 140 | } else { |
3878 | 140 | Ty = Context.AccumTy; |
3879 | 140 | } |
3880 | 607 | } else if (201 Literal.isFract201 ) { |
3881 | 201 | if (Literal.isHalf) { |
3882 | 63 | Ty = Context.ShortFractTy; |
3883 | 138 | } else if (Literal.isLong) { |
3884 | 59 | Ty = Context.LongFractTy; |
3885 | 79 | } else { |
3886 | 79 | Ty = Context.FractTy; |
3887 | 79 | } |
3888 | 201 | } |
3889 | | |
3890 | 808 | if (Literal.isUnsigned) Ty = Context.getCorrespondingUnsignedType(Ty)229 ; |
3891 | | |
3892 | 808 | bool isSigned = !Literal.isUnsigned; |
3893 | 808 | unsigned scale = Context.getFixedPointScale(Ty); |
3894 | 808 | unsigned bit_width = Context.getTypeInfo(Ty).Width; |
3895 | | |
3896 | 808 | llvm::APInt Val(bit_width, 0, isSigned); |
3897 | 808 | bool Overflowed = Literal.GetFixedPointValue(Val, scale); |
3898 | 808 | bool ValIsZero = Val.isZero() && !Overflowed55 ; |
3899 | | |
3900 | 808 | auto MaxVal = Context.getFixedPointMax(Ty).getValue(); |
3901 | 808 | if (Literal.isFract && Val == MaxVal + 1201 && !ValIsZero36 ) |
3902 | | // Clause 6.4.4 - The value of a constant shall be in the range of |
3903 | | // representable values for its type, with exception for constants of a |
3904 | | // fract type with a value of exactly 1; such a constant shall denote |
3905 | | // the maximal value for the type. |
3906 | 32 | --Val; |
3907 | 776 | else if (Val.ugt(MaxVal) || Overflowed761 ) |
3908 | 31 | Diag(Tok.getLocation(), diag::err_too_large_for_fixed_point); |
3909 | | |
3910 | 808 | Res = FixedPointLiteral::CreateFromRawInt(Context, Val, Ty, |
3911 | 808 | Tok.getLocation(), scale); |
3912 | 4.41M | } else if (Literal.isFloatingLiteral()) { |
3913 | 42.5k | QualType Ty; |
3914 | 42.5k | if (Literal.isHalf){ |
3915 | 16 | if (getOpenCLOptions().isAvailableOption("cl_khr_fp16", getLangOpts())) |
3916 | 10 | Ty = Context.HalfTy; |
3917 | 6 | else { |
3918 | 6 | Diag(Tok.getLocation(), diag::err_half_const_requires_fp16); |
3919 | 6 | return ExprError(); |
3920 | 6 | } |
3921 | 42.5k | } else if (Literal.isFloat) |
3922 | 9.73k | Ty = Context.FloatTy; |
3923 | 32.7k | else if (Literal.isLong) |
3924 | 4.06k | Ty = Context.LongDoubleTy; |
3925 | 28.7k | else if (Literal.isFloat16) |
3926 | 111 | Ty = Context.Float16Ty; |
3927 | 28.6k | else if (Literal.isFloat128) |
3928 | 170 | Ty = Context.Float128Ty; |
3929 | 28.4k | else |
3930 | 28.4k | Ty = Context.DoubleTy; |
3931 | | |
3932 | 42.5k | Res = BuildFloatingLiteral(*this, Literal, Ty, Tok.getLocation()); |
3933 | | |
3934 | 42.5k | if (Ty == Context.DoubleTy) { |
3935 | 28.4k | if (getLangOpts().SinglePrecisionConstants) { |
3936 | 2 | if (Ty->castAs<BuiltinType>()->getKind() != BuiltinType::Float) { |
3937 | 2 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).get(); |
3938 | 2 | } |
3939 | 28.4k | } else if (getLangOpts().OpenCL && !getOpenCLOptions().isAvailableOption( |
3940 | 132 | "cl_khr_fp64", getLangOpts())) { |
3941 | | // Impose single-precision float type when cl_khr_fp64 is not enabled. |
3942 | 26 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64) |
3943 | 26 | << (getLangOpts().getOpenCLCompatibleVersion() >= 300); |
3944 | 26 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).get(); |
3945 | 26 | } |
3946 | 28.4k | } |
3947 | 4.36M | } else if (!Literal.isIntegerLiteral()) { |
3948 | 0 | return ExprError(); |
3949 | 4.36M | } else { |
3950 | 4.36M | QualType Ty; |
3951 | | |
3952 | | // 'long long' is a C99 or C++11 feature. |
3953 | 4.36M | if (!getLangOpts().C99 && Literal.isLongLong875k ) { |
3954 | 27.4k | if (getLangOpts().CPlusPlus) |
3955 | 27.4k | Diag(Tok.getLocation(), |
3956 | 27.4k | getLangOpts().CPlusPlus11 ? |
3957 | 27.3k | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong37 ); |
3958 | 9 | else |
3959 | 9 | Diag(Tok.getLocation(), diag::ext_c99_longlong); |
3960 | 27.4k | } |
3961 | | |
3962 | | // 'z/uz' literals are a C++2b feature. |
3963 | 4.36M | if (Literal.isSizeT) |
3964 | 94 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus |
3965 | 94 | ? getLangOpts().CPlusPlus2b84 |
3966 | 84 | ? diag::warn_cxx20_compat_size_t_suffix74 |
3967 | 84 | : diag::ext_cxx2b_size_t_suffix10 |
3968 | 94 | : diag::err_cxx2b_size_t_suffix10 ); |
3969 | | |
3970 | | // 'wb/uwb' literals are a C2x feature. We support _BitInt as a type in C++, |
3971 | | // but we do not currently support the suffix in C++ mode because it's not |
3972 | | // entirely clear whether WG21 will prefer this suffix to return a library |
3973 | | // type such as std::bit_int instead of returning a _BitInt. |
3974 | 4.36M | if (Literal.isBitInt && !getLangOpts().CPlusPlus64 ) |
3975 | 64 | PP.Diag(Tok.getLocation(), getLangOpts().C2x |
3976 | 64 | ? diag::warn_c2x_compat_bitint_suffix63 |
3977 | 64 | : diag::ext_c2x_bitint_suffix1 ); |
3978 | | |
3979 | | // Get the value in the widest-possible width. What is "widest" depends on |
3980 | | // whether the literal is a bit-precise integer or not. For a bit-precise |
3981 | | // integer type, try to scan the source to determine how many bits are |
3982 | | // needed to represent the value. This may seem a bit expensive, but trying |
3983 | | // to get the integer value from an overly-wide APInt is *extremely* |
3984 | | // expensive, so the naive approach of assuming |
3985 | | // llvm::IntegerType::MAX_INT_BITS is a big performance hit. |
3986 | 4.36M | unsigned BitsNeeded = |
3987 | 4.36M | Literal.isBitInt ? llvm::APInt::getSufficientBitsNeeded( |
3988 | 64 | Literal.getLiteralDigits(), Literal.getRadix()) |
3989 | 4.36M | : Context.getTargetInfo().getIntMaxTWidth()4.36M ; |
3990 | 4.36M | llvm::APInt ResultVal(BitsNeeded, 0); |
3991 | | |
3992 | 4.36M | if (Literal.GetIntegerValue(ResultVal)) { |
3993 | | // If this value didn't fit into uintmax_t, error and force to ull. |
3994 | 5 | Diag(Tok.getLocation(), diag::err_integer_literal_too_large) |
3995 | 5 | << /* Unsigned */ 1; |
3996 | 5 | Ty = Context.UnsignedLongLongTy; |
3997 | 5 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
3998 | 5 | "long long is not intmax_t?"); |
3999 | 4.36M | } else { |
4000 | | // If this value fits into a ULL, try to figure out what else it fits into |
4001 | | // according to the rules of C99 6.4.4.1p5. |
4002 | | |
4003 | | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
4004 | | // be an unsigned int. |
4005 | 4.36M | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 104.26M ; |
4006 | | |
4007 | | // Check from smallest to largest, picking the smallest type we can. |
4008 | 4.36M | unsigned Width = 0; |
4009 | | |
4010 | | // Microsoft specific integer suffixes are explicitly sized. |
4011 | 4.36M | if (Literal.MicrosoftInteger) { |
4012 | 50 | if (Literal.MicrosoftInteger == 8 && !Literal.isUnsigned9 ) { |
4013 | 5 | Width = 8; |
4014 | 5 | Ty = Context.CharTy; |
4015 | 45 | } else { |
4016 | 45 | Width = Literal.MicrosoftInteger; |
4017 | 45 | Ty = Context.getIntTypeForBitwidth(Width, |
4018 | 45 | /*Signed=*/!Literal.isUnsigned); |
4019 | 45 | } |
4020 | 50 | } |
4021 | | |
4022 | | // Bit-precise integer literals are automagically-sized based on the |
4023 | | // width required by the literal. |
4024 | 4.36M | if (Literal.isBitInt) { |
4025 | | // The signed version has one more bit for the sign value. There are no |
4026 | | // zero-width bit-precise integers, even if the literal value is 0. |
4027 | 64 | Width = std::max(ResultVal.getActiveBits(), 1u) + |
4028 | 64 | (Literal.isUnsigned ? 0u30 : 1u34 ); |
4029 | | |
4030 | | // Diagnose if the width of the constant is larger than BITINT_MAXWIDTH, |
4031 | | // and reset the type to the largest supported width. |
4032 | 64 | unsigned int MaxBitIntWidth = |
4033 | 64 | Context.getTargetInfo().getMaxBitIntWidth(); |
4034 | 64 | if (Width > MaxBitIntWidth) { |
4035 | 2 | Diag(Tok.getLocation(), diag::err_integer_literal_too_large) |
4036 | 2 | << Literal.isUnsigned; |
4037 | 2 | Width = MaxBitIntWidth; |
4038 | 2 | } |
4039 | | |
4040 | | // Reset the result value to the smaller APInt and select the correct |
4041 | | // type to be used. Note, we zext even for signed values because the |
4042 | | // literal itself is always an unsigned value (a preceeding - is a |
4043 | | // unary operator, not part of the literal). |
4044 | 64 | ResultVal = ResultVal.zextOrTrunc(Width); |
4045 | 64 | Ty = Context.getBitIntType(Literal.isUnsigned, Width); |
4046 | 64 | } |
4047 | | |
4048 | | // Check C++2b size_t literals. |
4049 | 4.36M | if (Literal.isSizeT) { |
4050 | 94 | assert(!Literal.MicrosoftInteger && |
4051 | 94 | "size_t literals can't be Microsoft literals"); |
4052 | 0 | unsigned SizeTSize = Context.getTargetInfo().getTypeWidth( |
4053 | 94 | Context.getTargetInfo().getSizeType()); |
4054 | | |
4055 | | // Does it fit in size_t? |
4056 | 94 | if (ResultVal.isIntN(SizeTSize)) { |
4057 | | // Does it fit in ssize_t? |
4058 | 92 | if (!Literal.isUnsigned && ResultVal[SizeTSize - 1] == 026 ) |
4059 | 24 | Ty = Context.getSignedSizeType(); |
4060 | 68 | else if (AllowUnsigned) |
4061 | 67 | Ty = Context.getSizeType(); |
4062 | 92 | Width = SizeTSize; |
4063 | 92 | } |
4064 | 94 | } |
4065 | | |
4066 | 4.36M | if (Ty.isNull() && !Literal.isLong4.36M && !Literal.isLongLong4.23M && |
4067 | 4.36M | !Literal.isSizeT4.18M ) { |
4068 | | // Are int/unsigned possibilities? |
4069 | 4.18M | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
4070 | | |
4071 | | // Does it fit in a unsigned int? |
4072 | 4.18M | if (ResultVal.isIntN(IntSize)) { |
4073 | | // Does it fit in a signed int? |
4074 | 4.18M | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 04.16M ) |
4075 | 4.13M | Ty = Context.IntTy; |
4076 | 50.4k | else if (AllowUnsigned) |
4077 | 50.2k | Ty = Context.UnsignedIntTy; |
4078 | 4.18M | Width = IntSize; |
4079 | 4.18M | } |
4080 | 4.18M | } |
4081 | | |
4082 | | // Are long/unsigned long possibilities? |
4083 | 4.36M | if (Ty.isNull() && !Literal.isLongLong183k && !Literal.isSizeT137k ) { |
4084 | 137k | unsigned LongSize = Context.getTargetInfo().getLongWidth(); |
4085 | | |
4086 | | // Does it fit in a unsigned long? |
4087 | 137k | if (ResultVal.isIntN(LongSize)) { |
4088 | | // Does it fit in a signed long? |
4089 | 137k | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 080.3k ) |
4090 | 80.1k | Ty = Context.LongTy; |
4091 | 57.2k | else if (AllowUnsigned) |
4092 | 57.0k | Ty = Context.UnsignedLongTy; |
4093 | | // Check according to the rules of C90 6.1.3.2p5. C++03 [lex.icon]p2 |
4094 | | // is compatible. |
4095 | 132 | else if (!getLangOpts().C99 && !getLangOpts().CPlusPlus1146 ) { |
4096 | 18 | const unsigned LongLongSize = |
4097 | 18 | Context.getTargetInfo().getLongLongWidth(); |
4098 | 18 | Diag(Tok.getLocation(), |
4099 | 18 | getLangOpts().CPlusPlus |
4100 | 18 | ? Literal.isLong6 |
4101 | 6 | ? diag::warn_old_implicitly_unsigned_long_cxx4 |
4102 | 6 | : /*C++98 UB*/ diag:: |
4103 | 2 | ext_old_implicitly_unsigned_long_cxx |
4104 | 18 | : diag::warn_old_implicitly_unsigned_long12 ) |
4105 | 18 | << (LongLongSize > LongSize ? /*will have type 'long long'*/ 09 |
4106 | 18 | : /*will be ill-formed*/ 19 ); |
4107 | 18 | Ty = Context.UnsignedLongTy; |
4108 | 18 | } |
4109 | 137k | Width = LongSize; |
4110 | 137k | } |
4111 | 137k | } |
4112 | | |
4113 | | // Check long long if needed. |
4114 | 4.36M | if (Ty.isNull() && !Literal.isSizeT46.4k ) { |
4115 | 46.4k | unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); |
4116 | | |
4117 | | // Does it fit in a unsigned long long? |
4118 | 46.4k | if (ResultVal.isIntN(LongLongSize)) { |
4119 | | // Does it fit in a signed long long? |
4120 | | // To be compatible with MSVC, hex integer literals ending with the |
4121 | | // LL or i64 suffix are always signed in Microsoft mode. |
4122 | 46.4k | if (!Literal.isUnsigned && (23.4k ResultVal[LongLongSize-1] == 023.4k || |
4123 | 23.4k | (45 getLangOpts().MSVCCompat45 && Literal.isLongLong6 ))) |
4124 | 23.4k | Ty = Context.LongLongTy; |
4125 | 23.0k | else if (AllowUnsigned) |
4126 | 23.0k | Ty = Context.UnsignedLongLongTy; |
4127 | 46.4k | Width = LongLongSize; |
4128 | 46.4k | } |
4129 | 46.4k | } |
4130 | | |
4131 | | // If we still couldn't decide a type, we either have 'size_t' literal |
4132 | | // that is out of range, or a decimal literal that does not fit in a |
4133 | | // signed long long and has no U suffix. |
4134 | 4.36M | if (Ty.isNull()) { |
4135 | 21 | if (Literal.isSizeT) |
4136 | 3 | Diag(Tok.getLocation(), diag::err_size_t_literal_too_large) |
4137 | 3 | << Literal.isUnsigned; |
4138 | 18 | else |
4139 | 18 | Diag(Tok.getLocation(), |
4140 | 18 | diag::ext_integer_literal_too_large_for_signed); |
4141 | 21 | Ty = Context.UnsignedLongLongTy; |
4142 | 21 | Width = Context.getTargetInfo().getLongLongWidth(); |
4143 | 21 | } |
4144 | | |
4145 | 4.36M | if (ResultVal.getBitWidth() != Width) |
4146 | 4.18M | ResultVal = ResultVal.trunc(Width); |
4147 | 4.36M | } |
4148 | 0 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
4149 | 4.36M | } |
4150 | | |
4151 | | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
4152 | 4.41M | if (Literal.isImaginary) { |
4153 | 364 | Res = new (Context) ImaginaryLiteral(Res, |
4154 | 364 | Context.getComplexType(Res->getType())); |
4155 | | |
4156 | 364 | Diag(Tok.getLocation(), diag::ext_imaginary_constant); |
4157 | 364 | } |
4158 | 4.41M | return Res; |
4159 | 4.41M | } |
4160 | | |
4161 | 2.56M | ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { |
4162 | 2.56M | assert(E && "ActOnParenExpr() missing expr"); |
4163 | 0 | QualType ExprTy = E->getType(); |
4164 | 2.56M | if (getLangOpts().ProtectParens && CurFPFeatures.getAllowFPReassociate()15 && |
4165 | 2.56M | !E->isLValue()8 && ExprTy->hasFloatingRepresentation()6 ) |
4166 | 5 | return BuildBuiltinCallExpr(R, Builtin::BI__arithmetic_fence, E); |
4167 | 2.56M | return new (Context) ParenExpr(L, R, E); |
4168 | 2.56M | } |
4169 | | |
4170 | | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
4171 | | SourceLocation Loc, |
4172 | 45 | SourceRange ArgRange) { |
4173 | | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
4174 | | // scalar or vector data type argument..." |
4175 | | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
4176 | | // type (C99 6.2.5p18) or void. |
4177 | 45 | if (!(T->isArithmeticType() || T->isVoidType()43 || T->isVectorType()42 )) { |
4178 | 3 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
4179 | 3 | << T << ArgRange; |
4180 | 3 | return true; |
4181 | 3 | } |
4182 | | |
4183 | 42 | assert((T->isVoidType() || !T->isIncompleteType()) && |
4184 | 42 | "Scalar types should always be complete"); |
4185 | 0 | return false; |
4186 | 45 | } |
4187 | | |
4188 | | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
4189 | | SourceLocation Loc, |
4190 | | SourceRange ArgRange, |
4191 | 88.8k | UnaryExprOrTypeTrait TraitKind) { |
4192 | | // Invalid types must be hard errors for SFINAE in C++. |
4193 | 88.8k | if (S.LangOpts.CPlusPlus) |
4194 | 75.5k | return true; |
4195 | | |
4196 | | // C99 6.5.3.4p1: |
4197 | 13.3k | if (T->isFunctionType() && |
4198 | 13.3k | (11 TraitKind == UETT_SizeOf11 || TraitKind == UETT_AlignOf1 || |
4199 | 11 | TraitKind == UETT_PreferredAlignOf1 )) { |
4200 | | // sizeof(function)/alignof(function) is allowed as an extension. |
4201 | 11 | S.Diag(Loc, diag::ext_sizeof_alignof_function_type) |
4202 | 11 | << getTraitSpelling(TraitKind) << ArgRange; |
4203 | 11 | return false; |
4204 | 11 | } |
4205 | | |
4206 | | // Allow sizeof(void)/alignof(void) as an extension, unless in OpenCL where |
4207 | | // this is an error (OpenCL v1.1 s6.3.k) |
4208 | 13.3k | if (T->isVoidType()) { |
4209 | 11 | unsigned DiagID = S.LangOpts.OpenCL ? diag::err_opencl_sizeof_alignof_type1 |
4210 | 11 | : diag::ext_sizeof_alignof_void_type10 ; |
4211 | 11 | S.Diag(Loc, DiagID) << getTraitSpelling(TraitKind) << ArgRange; |
4212 | 11 | return false; |
4213 | 11 | } |
4214 | | |
4215 | 13.2k | return true; |
4216 | 13.3k | } |
4217 | | |
4218 | | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
4219 | | SourceLocation Loc, |
4220 | | SourceRange ArgRange, |
4221 | 88.6k | UnaryExprOrTypeTrait TraitKind) { |
4222 | | // Reject sizeof(interface) and sizeof(interface<proto>) if the |
4223 | | // runtime doesn't allow it. |
4224 | 88.6k | if (!S.LangOpts.ObjCRuntime.allowsSizeofAlignof() && T->isObjCObjectType()88.6k ) { |
4225 | 3 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
4226 | 3 | << T << (TraitKind == UETT_SizeOf) |
4227 | 3 | << ArgRange; |
4228 | 3 | return true; |
4229 | 3 | } |
4230 | | |
4231 | 88.6k | return false; |
4232 | 88.6k | } |
4233 | | |
4234 | | /// Check whether E is a pointer from a decayed array type (the decayed |
4235 | | /// pointer type is equal to T) and emit a warning if it is. |
4236 | | static void warnOnSizeofOnArrayDecay(Sema &S, SourceLocation Loc, QualType T, |
4237 | 180 | Expr *E) { |
4238 | | // Don't warn if the operation changed the type. |
4239 | 180 | if (T != E->getType()) |
4240 | 63 | return; |
4241 | | |
4242 | | // Now look for array decays. |
4243 | 117 | ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E); |
4244 | 117 | if (!ICE || ICE->getCastKind() != CK_ArrayToPointerDecay45 ) |
4245 | 108 | return; |
4246 | | |
4247 | 9 | S.Diag(Loc, diag::warn_sizeof_array_decay) << ICE->getSourceRange() |
4248 | 9 | << ICE->getType() |
4249 | 9 | << ICE->getSubExpr()->getType(); |
4250 | 9 | } |
4251 | | |
4252 | | /// Check the constraints on expression operands to unary type expression |
4253 | | /// and type traits. |
4254 | | /// |
4255 | | /// Completes any types necessary and validates the constraints on the operand |
4256 | | /// expression. The logic mostly mirrors the type-based overload, but may modify |
4257 | | /// the expression as it completes the type for that expression through template |
4258 | | /// instantiation, etc. |
4259 | | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, |
4260 | 9.99k | UnaryExprOrTypeTrait ExprKind) { |
4261 | 9.99k | QualType ExprTy = E->getType(); |
4262 | 9.99k | assert(!ExprTy->isReferenceType()); |
4263 | | |
4264 | 0 | bool IsUnevaluatedOperand = |
4265 | 9.99k | (ExprKind == UETT_SizeOf || ExprKind == UETT_AlignOf189 || |
4266 | 9.99k | ExprKind == UETT_PreferredAlignOf151 || ExprKind == UETT_VecStep36 ); |
4267 | 9.99k | if (IsUnevaluatedOperand) { |
4268 | 9.99k | ExprResult Result = CheckUnevaluatedOperand(E); |
4269 | 9.99k | if (Result.isInvalid()) |
4270 | 0 | return true; |
4271 | 9.99k | E = Result.get(); |
4272 | 9.99k | } |
4273 | | |
4274 | | // The operand for sizeof and alignof is in an unevaluated expression context, |
4275 | | // so side effects could result in unintended consequences. |
4276 | | // Exclude instantiation-dependent expressions, because 'sizeof' is sometimes |
4277 | | // used to build SFINAE gadgets. |
4278 | | // FIXME: Should we consider instantiation-dependent operands to 'alignof'? |
4279 | 9.99k | if (IsUnevaluatedOperand && !inTemplateInstantiation() && |
4280 | 9.99k | !E->isInstantiationDependent()7.15k && |
4281 | 9.99k | !E->getType()->isVariableArrayType()5.26k && |
4282 | 9.99k | E->HasSideEffects(Context, false)5.22k ) |
4283 | 52 | Diag(E->getExprLoc(), diag::warn_side_effects_unevaluated_context); |
4284 | | |
4285 | 9.99k | if (ExprKind == UETT_VecStep) |
4286 | 36 | return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(), |
4287 | 36 | E->getSourceRange()); |
4288 | | |
4289 | | // Explicitly list some types as extensions. |
4290 | 9.96k | if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(), |
4291 | 9.96k | E->getSourceRange(), ExprKind)) |
4292 | 6 | return false; |
4293 | | |
4294 | | // 'alignof' applied to an expression only requires the base element type of |
4295 | | // the expression to be complete. 'sizeof' requires the expression's type to |
4296 | | // be complete (and will attempt to complete it if it's an array of unknown |
4297 | | // bound). |
4298 | 9.95k | if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf9.91k ) { |
4299 | 151 | if (RequireCompleteSizedType( |
4300 | 151 | E->getExprLoc(), Context.getBaseElementType(E->getType()), |
4301 | 151 | diag::err_sizeof_alignof_incomplete_or_sizeless_type, |
4302 | 151 | getTraitSpelling(ExprKind), E->getSourceRange())) |
4303 | 12 | return true; |
4304 | 9.80k | } else { |
4305 | 9.80k | if (RequireCompleteSizedExprType( |
4306 | 9.80k | E, diag::err_sizeof_alignof_incomplete_or_sizeless_type, |
4307 | 9.80k | getTraitSpelling(ExprKind), E->getSourceRange())) |
4308 | 61 | return true; |
4309 | 9.80k | } |
4310 | | |
4311 | | // Completing the expression's type may have changed it. |
4312 | 9.88k | ExprTy = E->getType(); |
4313 | 9.88k | assert(!ExprTy->isReferenceType()); |
4314 | | |
4315 | 9.88k | if (ExprTy->isFunctionType()) { |
4316 | 2 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_function_type) |
4317 | 2 | << getTraitSpelling(ExprKind) << E->getSourceRange(); |
4318 | 2 | return true; |
4319 | 2 | } |
4320 | | |
4321 | 9.88k | if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), |
4322 | 9.88k | E->getSourceRange(), ExprKind)) |
4323 | 0 | return true; |
4324 | | |
4325 | 9.88k | if (ExprKind == UETT_SizeOf) { |
4326 | 9.74k | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
4327 | 5.97k | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
4328 | 799 | QualType OType = PVD->getOriginalType(); |
4329 | 799 | QualType Type = PVD->getType(); |
4330 | 799 | if (Type->isPointerType() && OType->isArrayType()129 ) { |
4331 | 5 | Diag(E->getExprLoc(), diag::warn_sizeof_array_param) |
4332 | 5 | << Type << OType; |
4333 | 5 | Diag(PVD->getLocation(), diag::note_declared_at); |
4334 | 5 | } |
4335 | 799 | } |
4336 | 5.97k | } |
4337 | | |
4338 | | // Warn on "sizeof(array op x)" and "sizeof(x op array)", where the array |
4339 | | // decays into a pointer and returns an unintended result. This is most |
4340 | | // likely a typo for "sizeof(array) op x". |
4341 | 9.74k | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E->IgnoreParens())) { |
4342 | 90 | warnOnSizeofOnArrayDecay(*this, BO->getOperatorLoc(), BO->getType(), |
4343 | 90 | BO->getLHS()); |
4344 | 90 | warnOnSizeofOnArrayDecay(*this, BO->getOperatorLoc(), BO->getType(), |
4345 | 90 | BO->getRHS()); |
4346 | 90 | } |
4347 | 9.74k | } |
4348 | | |
4349 | 9.88k | return false; |
4350 | 9.88k | } |
4351 | | |
4352 | | /// Check the constraints on operands to unary expression and type |
4353 | | /// traits. |
4354 | | /// |
4355 | | /// This will complete any types necessary, and validate the various constraints |
4356 | | /// on those operands. |
4357 | | /// |
4358 | | /// The UsualUnaryConversions() function is *not* called by this routine. |
4359 | | /// C99 6.3.2.1p[2-4] all state: |
4360 | | /// Except when it is the operand of the sizeof operator ... |
4361 | | /// |
4362 | | /// C++ [expr.sizeof]p4 |
4363 | | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
4364 | | /// standard conversions are not applied to the operand of sizeof. |
4365 | | /// |
4366 | | /// This policy is followed for all of the unary trait expressions. |
4367 | | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, |
4368 | | SourceLocation OpLoc, |
4369 | | SourceRange ExprRange, |
4370 | 78.8k | UnaryExprOrTypeTrait ExprKind) { |
4371 | 78.8k | if (ExprType->isDependentType()) |
4372 | 0 | return false; |
4373 | | |
4374 | | // C++ [expr.sizeof]p2: |
4375 | | // When applied to a reference or a reference type, the result |
4376 | | // is the size of the referenced type. |
4377 | | // C++11 [expr.alignof]p3: |
4378 | | // When alignof is applied to a reference type, the result |
4379 | | // shall be the alignment of the referenced type. |
4380 | 78.8k | if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) |
4381 | 40 | ExprType = Ref->getPointeeType(); |
4382 | | |
4383 | | // C11 6.5.3.4/3, C++11 [expr.alignof]p3: |
4384 | | // When alignof or _Alignof is applied to an array type, the result |
4385 | | // is the alignment of the element type. |
4386 | 78.8k | if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf75.7k || |
4387 | 78.8k | ExprKind == UETT_OpenMPRequiredSimdAlign70.5k ) |
4388 | 8.35k | ExprType = Context.getBaseElementType(ExprType); |
4389 | | |
4390 | 78.8k | if (ExprKind == UETT_VecStep) |
4391 | 9 | return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); |
4392 | | |
4393 | | // Explicitly list some types as extensions. |
4394 | 78.8k | if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange, |
4395 | 78.8k | ExprKind)) |
4396 | 16 | return false; |
4397 | | |
4398 | 78.8k | if (RequireCompleteSizedType( |
4399 | 78.8k | OpLoc, ExprType, diag::err_sizeof_alignof_incomplete_or_sizeless_type, |
4400 | 78.8k | getTraitSpelling(ExprKind), ExprRange)) |
4401 | 122 | return true; |
4402 | | |
4403 | 78.7k | if (ExprType->isFunctionType()) { |
4404 | 8 | Diag(OpLoc, diag::err_sizeof_alignof_function_type) |
4405 | 8 | << getTraitSpelling(ExprKind) << ExprRange; |
4406 | 8 | return true; |
4407 | 8 | } |
4408 | | |
4409 | 78.7k | if (CheckObjCTraitOperandConstraints(*this, ExprType, OpLoc, ExprRange, |
4410 | 78.7k | ExprKind)) |
4411 | 3 | return true; |
4412 | | |
4413 | 78.7k | return false; |
4414 | 78.7k | } |
4415 | | |
4416 | 186 | static bool CheckAlignOfExpr(Sema &S, Expr *E, UnaryExprOrTypeTrait ExprKind) { |
4417 | | // Cannot know anything else if the expression is dependent. |
4418 | 186 | if (E->isTypeDependent()) |
4419 | 0 | return false; |
4420 | | |
4421 | 186 | if (E->getObjectKind() == OK_BitField) { |
4422 | 1 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_typeof_bitfield) |
4423 | 1 | << 1 << E->getSourceRange(); |
4424 | 1 | return true; |
4425 | 1 | } |
4426 | | |
4427 | 185 | ValueDecl *D = nullptr; |
4428 | 185 | Expr *Inner = E->IgnoreParens(); |
4429 | 185 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Inner)) { |
4430 | 111 | D = DRE->getDecl(); |
4431 | 111 | } else if (MemberExpr *74 ME74 = dyn_cast<MemberExpr>(Inner)) { |
4432 | 30 | D = ME->getMemberDecl(); |
4433 | 30 | } |
4434 | | |
4435 | | // If it's a field, require the containing struct to have a |
4436 | | // complete definition so that we can compute the layout. |
4437 | | // |
4438 | | // This can happen in C++11 onwards, either by naming the member |
4439 | | // in a way that is not transformed into a member access expression |
4440 | | // (in an unevaluated operand, for instance), or by naming the member |
4441 | | // in a trailing-return-type. |
4442 | | // |
4443 | | // For the record, since __alignof__ on expressions is a GCC |
4444 | | // extension, GCC seems to permit this but always gives the |
4445 | | // nonsensical answer 0. |
4446 | | // |
4447 | | // We don't really need the layout here --- we could instead just |
4448 | | // directly check for all the appropriate alignment-lowing |
4449 | | // attributes --- but that would require duplicating a lot of |
4450 | | // logic that just isn't worth duplicating for such a marginal |
4451 | | // use-case. |
4452 | 185 | if (FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) { |
4453 | | // Fast path this check, since we at least know the record has a |
4454 | | // definition if we can find a member of it. |
4455 | 36 | if (!FD->getParent()->isCompleteDefinition()) { |
4456 | 3 | S.Diag(E->getExprLoc(), diag::err_alignof_member_of_incomplete_type) |
4457 | 3 | << E->getSourceRange(); |
4458 | 3 | return true; |
4459 | 3 | } |
4460 | | |
4461 | | // Otherwise, if it's a field, and the field doesn't have |
4462 | | // reference type, then it must have a complete type (or be a |
4463 | | // flexible array member, which we explicitly want to |
4464 | | // white-list anyway), which makes the following checks trivial. |
4465 | 33 | if (!FD->getType()->isReferenceType()) |
4466 | 29 | return false; |
4467 | 33 | } |
4468 | | |
4469 | 153 | return S.CheckUnaryExprOrTypeTraitOperand(E, ExprKind); |
4470 | 185 | } |
4471 | | |
4472 | 36 | bool Sema::CheckVecStepExpr(Expr *E) { |
4473 | 36 | E = E->IgnoreParens(); |
4474 | | |
4475 | | // Cannot know anything else if the expression is dependent. |
4476 | 36 | if (E->isTypeDependent()) |
4477 | 0 | return false; |
4478 | | |
4479 | 36 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
4480 | 36 | } |
4481 | | |
4482 | | static void captureVariablyModifiedType(ASTContext &Context, QualType T, |
4483 | 18.4k | CapturingScopeInfo *CSI) { |
4484 | 18.4k | assert(T->isVariablyModifiedType()); |
4485 | 0 | assert(CSI != nullptr); |
4486 | | |
4487 | | // We're going to walk down into the type and look for VLA expressions. |
4488 | 26.2k | do { |
4489 | 26.2k | const Type *Ty = T.getTypePtr(); |
4490 | 26.2k | switch (Ty->getTypeClass()) { |
4491 | 0 | #define TYPE(Class, Base) |
4492 | 0 | #define ABSTRACT_TYPE(Class, Base) |
4493 | 0 | #define NON_CANONICAL_TYPE(Class, Base) |
4494 | 0 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
4495 | 0 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) |
4496 | 0 | #include "clang/AST/TypeNodes.inc" |
4497 | 0 | T = QualType(); |
4498 | 0 | break; |
4499 | | // These types are never variably-modified. |
4500 | 0 | case Type::Builtin: |
4501 | 0 | case Type::Complex: |
4502 | 0 | case Type::Vector: |
4503 | 0 | case Type::ExtVector: |
4504 | 0 | case Type::ConstantMatrix: |
4505 | 0 | case Type::Record: |
4506 | 0 | case Type::Enum: |
4507 | 0 | case Type::Elaborated: |
4508 | 0 | case Type::TemplateSpecialization: |
4509 | 0 | case Type::ObjCObject: |
4510 | 0 | case Type::ObjCInterface: |
4511 | 0 | case Type::ObjCObjectPointer: |
4512 | 0 | case Type::ObjCTypeParam: |
4513 | 0 | case Type::Pipe: |
4514 | 0 | case Type::BitInt: |
4515 | 0 | llvm_unreachable("type class is never variably-modified!"); |
4516 | 0 | case Type::Adjusted: |
4517 | 0 | T = cast<AdjustedType>(Ty)->getOriginalType(); |
4518 | 0 | break; |
4519 | 0 | case Type::Decayed: |
4520 | 0 | T = cast<DecayedType>(Ty)->getPointeeType(); |
4521 | 0 | break; |
4522 | 35 | case Type::Pointer: |
4523 | 35 | T = cast<PointerType>(Ty)->getPointeeType(); |
4524 | 35 | break; |
4525 | 0 | case Type::BlockPointer: |
4526 | 0 | T = cast<BlockPointerType>(Ty)->getPointeeType(); |
4527 | 0 | break; |
4528 | 0 | case Type::LValueReference: |
4529 | 0 | case Type::RValueReference: |
4530 | 0 | T = cast<ReferenceType>(Ty)->getPointeeType(); |
4531 | 0 | break; |
4532 | 0 | case Type::MemberPointer: |
4533 | 0 | T = cast<MemberPointerType>(Ty)->getPointeeType(); |
4534 | 0 | break; |
4535 | 0 | case Type::ConstantArray: |
4536 | 0 | case Type::IncompleteArray: |
4537 | | // Losing element qualification here is fine. |
4538 | 0 | T = cast<ArrayType>(Ty)->getElementType(); |
4539 | 0 | break; |
4540 | 26.1k | case Type::VariableArray: { |
4541 | | // Losing element qualification here is fine. |
4542 | 26.1k | const VariableArrayType *VAT = cast<VariableArrayType>(Ty); |
4543 | | |
4544 | | // Unknown size indication requires no size computation. |
4545 | | // Otherwise, evaluate and record it. |
4546 | 26.1k | auto Size = VAT->getSizeExpr(); |
4547 | 26.1k | if (Size && !CSI->isVLATypeCaptured(VAT) && |
4548 | 26.1k | (8.97k isa<CapturedRegionScopeInfo>(CSI)8.97k || isa<LambdaScopeInfo>(CSI)56 )) |
4549 | 8.96k | CSI->addVLATypeCapture(Size->getExprLoc(), VAT, Context.getSizeType()); |
4550 | | |
4551 | 26.1k | T = VAT->getElementType(); |
4552 | 26.1k | break; |
4553 | 0 | } |
4554 | 0 | case Type::FunctionProto: |
4555 | 0 | case Type::FunctionNoProto: |
4556 | 0 | T = cast<FunctionType>(Ty)->getReturnType(); |
4557 | 0 | break; |
4558 | 32 | case Type::Paren: |
4559 | 32 | case Type::TypeOf: |
4560 | 32 | case Type::UnaryTransform: |
4561 | 32 | case Type::Attributed: |
4562 | 32 | case Type::BTFTagAttributed: |
4563 | 32 | case Type::SubstTemplateTypeParm: |
4564 | 32 | case Type::MacroQualified: |
4565 | | // Keep walking after single level desugaring. |
4566 | 32 | T = T.getSingleStepDesugaredType(Context); |
4567 | 32 | break; |
4568 | 46 | case Type::Typedef: |
4569 | 46 | T = cast<TypedefType>(Ty)->desugar(); |
4570 | 46 | break; |
4571 | 0 | case Type::Decltype: |
4572 | 0 | T = cast<DecltypeType>(Ty)->desugar(); |
4573 | 0 | break; |
4574 | 0 | case Type::Using: |
4575 | 0 | T = cast<UsingType>(Ty)->desugar(); |
4576 | 0 | break; |
4577 | 0 | case Type::Auto: |
4578 | 0 | case Type::DeducedTemplateSpecialization: |
4579 | 0 | T = cast<DeducedType>(Ty)->getDeducedType(); |
4580 | 0 | break; |
4581 | 0 | case Type::TypeOfExpr: |
4582 | 0 | T = cast<TypeOfExprType>(Ty)->getUnderlyingExpr()->getType(); |
4583 | 0 | break; |
4584 | 0 | case Type::Atomic: |
4585 | 0 | T = cast<AtomicType>(Ty)->getValueType(); |
4586 | 0 | break; |
4587 | 26.2k | } |
4588 | 26.2k | } while (!T.isNull() && T->isVariablyModifiedType()); |
4589 | 18.4k | } |
4590 | | |
4591 | | /// Build a sizeof or alignof expression given a type operand. |
4592 | | ExprResult |
4593 | | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
4594 | | SourceLocation OpLoc, |
4595 | | UnaryExprOrTypeTrait ExprKind, |
4596 | 124k | SourceRange R) { |
4597 | 124k | if (!TInfo) |
4598 | 0 | return ExprError(); |
4599 | | |
4600 | 124k | QualType T = TInfo->getType(); |
4601 | | |
4602 | 124k | if (!T->isDependentType() && |
4603 | 124k | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)78.8k ) |
4604 | 135 | return ExprError(); |
4605 | | |
4606 | 124k | if (T->isVariablyModifiedType() && FunctionScopes.size() > 148 ) { |
4607 | 12 | if (auto *TT = T->getAs<TypedefType>()) { |
4608 | 6 | for (auto I = FunctionScopes.rbegin(), |
4609 | 6 | E = std::prev(FunctionScopes.rend()); |
4610 | 12 | I != E; ++I6 ) { |
4611 | 10 | auto *CSI = dyn_cast<CapturingScopeInfo>(*I); |
4612 | 10 | if (CSI == nullptr) |
4613 | 0 | break; |
4614 | 10 | DeclContext *DC = nullptr; |
4615 | 10 | if (auto *LSI = dyn_cast<LambdaScopeInfo>(CSI)) |
4616 | 10 | DC = LSI->CallOperator; |
4617 | 0 | else if (auto *CRSI = dyn_cast<CapturedRegionScopeInfo>(CSI)) |
4618 | 0 | DC = CRSI->TheCapturedDecl; |
4619 | 0 | else if (auto *BSI = dyn_cast<BlockScopeInfo>(CSI)) |
4620 | 0 | DC = BSI->TheDecl; |
4621 | 10 | if (DC) { |
4622 | 10 | if (DC->containsDecl(TT->getDecl())) |
4623 | 4 | break; |
4624 | 6 | captureVariablyModifiedType(Context, T, CSI); |
4625 | 6 | } |
4626 | 10 | } |
4627 | 6 | } |
4628 | 12 | } |
4629 | | |
4630 | | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
4631 | 124k | if (isUnevaluatedContext() && ExprKind == UETT_SizeOf85.1k && |
4632 | 124k | TInfo->getType()->isVariablyModifiedType()75.3k ) |
4633 | 28 | TInfo = TransformToPotentiallyEvaluated(TInfo); |
4634 | | |
4635 | 124k | return new (Context) UnaryExprOrTypeTraitExpr( |
4636 | 124k | ExprKind, TInfo, Context.getSizeType(), OpLoc, R.getEnd()); |
4637 | 124k | } |
4638 | | |
4639 | | /// Build a sizeof or alignof expression given an expression |
4640 | | /// operand. |
4641 | | ExprResult |
4642 | | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
4643 | 16.7k | UnaryExprOrTypeTrait ExprKind) { |
4644 | 16.7k | ExprResult PE = CheckPlaceholderExpr(E); |
4645 | 16.7k | if (PE.isInvalid()) |
4646 | 2 | return ExprError(); |
4647 | | |
4648 | 16.7k | E = PE.get(); |
4649 | | |
4650 | | // Verify that the operand is valid. |
4651 | 16.7k | bool isInvalid = false; |
4652 | 16.7k | if (E->isTypeDependent()) { |
4653 | | // Delay type-checking for type-dependent expressions. |
4654 | 10.0k | } else if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf10.0k ) { |
4655 | 186 | isInvalid = CheckAlignOfExpr(*this, E, ExprKind); |
4656 | 9.85k | } else if (ExprKind == UETT_VecStep) { |
4657 | 36 | isInvalid = CheckVecStepExpr(E); |
4658 | 9.82k | } else if (ExprKind == UETT_OpenMPRequiredSimdAlign) { |
4659 | 1 | Diag(E->getExprLoc(), diag::err_openmp_default_simd_align_expr); |
4660 | 1 | isInvalid = true; |
4661 | 9.81k | } else if (E->refersToBitField()) { // C99 6.5.3.4p1. |
4662 | 9 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_typeof_bitfield) << 0; |
4663 | 9 | isInvalid = true; |
4664 | 9.81k | } else { |
4665 | 9.81k | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
4666 | 9.81k | } |
4667 | | |
4668 | 16.7k | if (isInvalid) |
4669 | 90 | return ExprError(); |
4670 | | |
4671 | 16.6k | if (ExprKind == UETT_SizeOf && E->getType()->isVariableArrayType()16.4k ) { |
4672 | 46 | PE = TransformToPotentiallyEvaluated(E); |
4673 | 46 | if (PE.isInvalid()) return ExprError()0 ; |
4674 | 46 | E = PE.get(); |
4675 | 46 | } |
4676 | | |
4677 | | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
4678 | 16.6k | return new (Context) UnaryExprOrTypeTraitExpr( |
4679 | 16.6k | ExprKind, E, Context.getSizeType(), OpLoc, E->getSourceRange().getEnd()); |
4680 | 16.6k | } |
4681 | | |
4682 | | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
4683 | | /// expr and the same for @c alignof and @c __alignof |
4684 | | /// Note that the ArgRange is invalid if isType is false. |
4685 | | ExprResult |
4686 | | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
4687 | | UnaryExprOrTypeTrait ExprKind, bool IsType, |
4688 | 99.6k | void *TyOrEx, SourceRange ArgRange) { |
4689 | | // If error parsing type, ignore. |
4690 | 99.6k | if (!TyOrEx) return ExprError()41 ; |
4691 | | |
4692 | 99.5k | if (IsType) { |
4693 | 86.0k | TypeSourceInfo *TInfo; |
4694 | 86.0k | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
4695 | 86.0k | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
4696 | 86.0k | } |
4697 | | |
4698 | 13.5k | Expr *ArgEx = (Expr *)TyOrEx; |
4699 | 13.5k | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
4700 | 13.5k | return Result; |
4701 | 99.5k | } |
4702 | | |
4703 | | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
4704 | 365 | bool IsReal) { |
4705 | 365 | if (V.get()->isTypeDependent()) |
4706 | 0 | return S.Context.DependentTy; |
4707 | | |
4708 | | // _Real and _Imag are only l-values for normal l-values. |
4709 | 365 | if (V.get()->getObjectKind() != OK_Ordinary) { |
4710 | 0 | V = S.DefaultLvalueConversion(V.get()); |
4711 | 0 | if (V.isInvalid()) |
4712 | 0 | return QualType(); |
4713 | 0 | } |
4714 | | |
4715 | | // These operators return the element type of a complex type. |
4716 | 365 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
4717 | 258 | return CT->getElementType(); |
4718 | | |
4719 | | // Otherwise they pass through real integer and floating point types here. |
4720 | 107 | if (V.get()->getType()->isArithmeticType()) |
4721 | 90 | return V.get()->getType(); |
4722 | | |
4723 | | // Test for placeholders. |
4724 | 17 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
4725 | 17 | if (PR.isInvalid()) return QualType()0 ; |
4726 | 17 | if (PR.get() != V.get()) { |
4727 | 0 | V = PR; |
4728 | 0 | return CheckRealImagOperand(S, V, Loc, IsReal); |
4729 | 0 | } |
4730 | | |
4731 | | // Reject anything else. |
4732 | 17 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
4733 | 17 | << (IsReal ? "__real"9 : "__imag"8 ); |
4734 | 17 | return QualType(); |
4735 | 17 | } |
4736 | | |
4737 | | |
4738 | | |
4739 | | ExprResult |
4740 | | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
4741 | 48.8k | tok::TokenKind Kind, Expr *Input) { |
4742 | 48.8k | UnaryOperatorKind Opc; |
4743 | 48.8k | switch (Kind) { |
4744 | 0 | default: llvm_unreachable("Unknown unary op!"); |
4745 | 47.2k | case tok::plusplus: Opc = UO_PostInc; break; |
4746 | 1.59k | case tok::minusminus: Opc = UO_PostDec; break; |
4747 | 48.8k | } |
4748 | | |
4749 | | // Since this might is a postfix expression, get rid of ParenListExprs. |
4750 | 48.8k | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Input); |
4751 | 48.8k | if (Result.isInvalid()) return ExprError()0 ; |
4752 | 48.8k | Input = Result.get(); |
4753 | | |
4754 | 48.8k | return BuildUnaryOp(S, OpLoc, Opc, Input); |
4755 | 48.8k | } |
4756 | | |
4757 | | /// Diagnose if arithmetic on the given ObjC pointer is illegal. |
4758 | | /// |
4759 | | /// \return true on error |
4760 | | static bool checkArithmeticOnObjCPointer(Sema &S, |
4761 | | SourceLocation opLoc, |
4762 | 16 | Expr *op) { |
4763 | 16 | assert(op->getType()->isObjCObjectPointerType()); |
4764 | 16 | if (S.LangOpts.ObjCRuntime.allowsPointerArithmetic() && |
4765 | 16 | !S.LangOpts.ObjCSubscriptingLegacyRuntime11 ) |
4766 | 11 | return false; |
4767 | | |
4768 | 5 | S.Diag(opLoc, diag::err_arithmetic_nonfragile_interface) |
4769 | 5 | << op->getType()->castAs<ObjCObjectPointerType>()->getPointeeType() |
4770 | 5 | << op->getSourceRange(); |
4771 | 5 | return true; |
4772 | 16 | } |
4773 | | |
4774 | 159 | static bool isMSPropertySubscriptExpr(Sema &S, Expr *Base) { |
4775 | 159 | auto *BaseNoParens = Base->IgnoreParens(); |
4776 | 159 | if (auto *MSProp = dyn_cast<MSPropertyRefExpr>(BaseNoParens)) |
4777 | 76 | return MSProp->getPropertyDecl()->getType()->isArrayType(); |
4778 | 83 | return isa<MSPropertySubscriptExpr>(BaseNoParens); |
4779 | 159 | } |
4780 | | |
4781 | | // Returns the type used for LHS[RHS], given one of LHS, RHS is type-dependent. |
4782 | | // Typically this is DependentTy, but can sometimes be more precise. |
4783 | | // |
4784 | | // There are cases when we could determine a non-dependent type: |
4785 | | // - LHS and RHS may have non-dependent types despite being type-dependent |
4786 | | // (e.g. unbounded array static members of the current instantiation) |
4787 | | // - one may be a dependent-sized array with known element type |
4788 | | // - one may be a dependent-typed valid index (enum in current instantiation) |
4789 | | // |
4790 | | // We *always* return a dependent type, in such cases it is DependentTy. |
4791 | | // This avoids creating type-dependent expressions with non-dependent types. |
4792 | | // FIXME: is this important to avoid? See https://reviews.llvm.org/D107275 |
4793 | | static QualType getDependentArraySubscriptType(Expr *LHS, Expr *RHS, |
4794 | 87.8k | const ASTContext &Ctx) { |
4795 | 87.8k | assert(LHS->isTypeDependent() || RHS->isTypeDependent()); |
4796 | 0 | QualType LTy = LHS->getType(), RTy = RHS->getType(); |
4797 | 87.8k | QualType Result = Ctx.DependentTy; |
4798 | 87.8k | if (RTy->isIntegralOrUnscopedEnumerationType()) { |
4799 | 66.2k | if (const PointerType *PT = LTy->getAs<PointerType>()) |
4800 | 36.4k | Result = PT->getPointeeType(); |
4801 | 29.7k | else if (const ArrayType *AT = LTy->getAsArrayTypeUnsafe()) |
4802 | 10.9k | Result = AT->getElementType(); |
4803 | 66.2k | } else if (21.5k LTy->isIntegralOrUnscopedEnumerationType()21.5k ) { |
4804 | 5 | if (const PointerType *PT = RTy->getAs<PointerType>()) |
4805 | 3 | Result = PT->getPointeeType(); |
4806 | 2 | else if (const ArrayType *AT = RTy->getAsArrayTypeUnsafe()) |
4807 | 2 | Result = AT->getElementType(); |
4808 | 5 | } |
4809 | | // Ensure we return a dependent type. |
4810 | 87.8k | return Result->isDependentType() ? Result86.7k : Ctx.DependentTy1.08k ; |
4811 | 87.8k | } |
4812 | | |
4813 | | static bool checkArgsForPlaceholders(Sema &S, MultiExprArg args); |
4814 | | |
4815 | | ExprResult Sema::ActOnArraySubscriptExpr(Scope *S, Expr *base, |
4816 | | SourceLocation lbLoc, |
4817 | | MultiExprArg ArgExprs, |
4818 | 371k | SourceLocation rbLoc) { |
4819 | | |
4820 | 371k | if (base && !base->getType().isNull() && |
4821 | 371k | base->hasPlaceholderType(BuiltinType::OMPArraySection)371k ) |
4822 | 255 | return ActOnOMPArraySectionExpr(base, lbLoc, ArgExprs.front(), SourceLocation(), |
4823 | 255 | SourceLocation(), /*Length*/ nullptr, |
4824 | 255 | /*Stride=*/nullptr, rbLoc); |
4825 | | |
4826 | | // Since this might be a postfix expression, get rid of ParenListExprs. |
4827 | 370k | if (isa<ParenListExpr>(base)) { |
4828 | 12 | ExprResult result = MaybeConvertParenListExprToParenExpr(S, base); |
4829 | 12 | if (result.isInvalid()) |
4830 | 0 | return ExprError(); |
4831 | 12 | base = result.get(); |
4832 | 12 | } |
4833 | | |
4834 | | // Check if base and idx form a MatrixSubscriptExpr. |
4835 | | // |
4836 | | // Helper to check for comma expressions, which are not allowed as indices for |
4837 | | // matrix subscript expressions. |
4838 | 370k | auto CheckAndReportCommaError = [this, base, rbLoc](Expr *E) { |
4839 | 243 | if (isa<BinaryOperator>(E) && cast<BinaryOperator>(E)->isCommaOp()16 ) { |
4840 | 2 | Diag(E->getExprLoc(), diag::err_matrix_subscript_comma) |
4841 | 2 | << SourceRange(base->getBeginLoc(), rbLoc); |
4842 | 2 | return true; |
4843 | 2 | } |
4844 | 241 | return false; |
4845 | 243 | }; |
4846 | | // The matrix subscript operator ([][])is considered a single operator. |
4847 | | // Separating the index expressions by parenthesis is not allowed. |
4848 | 370k | if (base->hasPlaceholderType(BuiltinType::I
|