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 "CheckExprLifetime.h" 14 #include "TreeTransform.h" 15 #include "UsedDeclVisitor.h" 16 #include "clang/AST/ASTConsumer.h" 17 #include "clang/AST/ASTContext.h" 18 #include "clang/AST/ASTDiagnostic.h" 19 #include "clang/AST/ASTLambda.h" 20 #include "clang/AST/ASTMutationListener.h" 21 #include "clang/AST/CXXInheritance.h" 22 #include "clang/AST/Decl.h" 23 #include "clang/AST/DeclObjC.h" 24 #include "clang/AST/DeclTemplate.h" 25 #include "clang/AST/DynamicRecursiveASTVisitor.h" 26 #include "clang/AST/EvaluatedExprVisitor.h" 27 #include "clang/AST/Expr.h" 28 #include "clang/AST/ExprCXX.h" 29 #include "clang/AST/ExprObjC.h" 30 #include "clang/AST/MangleNumberingContext.h" 31 #include "clang/AST/OperationKinds.h" 32 #include "clang/AST/Type.h" 33 #include "clang/AST/TypeLoc.h" 34 #include "clang/Basic/Builtins.h" 35 #include "clang/Basic/DiagnosticSema.h" 36 #include "clang/Basic/PartialDiagnostic.h" 37 #include "clang/Basic/SourceManager.h" 38 #include "clang/Basic/Specifiers.h" 39 #include "clang/Basic/TargetInfo.h" 40 #include "clang/Basic/TypeTraits.h" 41 #include "clang/Lex/LiteralSupport.h" 42 #include "clang/Lex/Preprocessor.h" 43 #include "clang/Sema/AnalysisBasedWarnings.h" 44 #include "clang/Sema/DeclSpec.h" 45 #include "clang/Sema/DelayedDiagnostic.h" 46 #include "clang/Sema/Designator.h" 47 #include "clang/Sema/EnterExpressionEvaluationContext.h" 48 #include "clang/Sema/Initialization.h" 49 #include "clang/Sema/Lookup.h" 50 #include "clang/Sema/Overload.h" 51 #include "clang/Sema/ParsedTemplate.h" 52 #include "clang/Sema/Scope.h" 53 #include "clang/Sema/ScopeInfo.h" 54 #include "clang/Sema/SemaARM.h" 55 #include "clang/Sema/SemaCUDA.h" 56 #include "clang/Sema/SemaFixItUtils.h" 57 #include "clang/Sema/SemaHLSL.h" 58 #include "clang/Sema/SemaObjC.h" 59 #include "clang/Sema/SemaOpenMP.h" 60 #include "clang/Sema/SemaPseudoObject.h" 61 #include "clang/Sema/Template.h" 62 #include "llvm/ADT/STLExtras.h" 63 #include "llvm/ADT/StringExtras.h" 64 #include "llvm/Support/ConvertUTF.h" 65 #include "llvm/Support/SaveAndRestore.h" 66 #include "llvm/Support/TimeProfiler.h" 67 #include "llvm/Support/TypeSize.h" 68 #include <limits> 69 #include <optional> 70 71 using namespace clang; 72 using namespace sema; 73 74 bool Sema::CanUseDecl(NamedDecl *D, bool TreatUnavailableAsInvalid) { 75 // See if this is an auto-typed variable whose initializer we are parsing. 76 if (ParsingInitForAutoVars.count(D)) 77 return false; 78 79 // See if this is a deleted function. 80 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 81 if (FD->isDeleted()) 82 return false; 83 84 // If the function has a deduced return type, and we can't deduce it, 85 // then we can't use it either. 86 if (getLangOpts().CPlusPlus14 && FD->getReturnType()->isUndeducedType() && 87 DeduceReturnType(FD, SourceLocation(), /*Diagnose*/ false)) 88 return false; 89 90 // See if this is an aligned allocation/deallocation function that is 91 // unavailable. 92 if (TreatUnavailableAsInvalid && 93 isUnavailableAlignedAllocationFunction(*FD)) 94 return false; 95 } 96 97 // See if this function is unavailable. 98 if (TreatUnavailableAsInvalid && D->getAvailability() == AR_Unavailable && 99 cast<Decl>(CurContext)->getAvailability() != AR_Unavailable) 100 return false; 101 102 if (isa<UnresolvedUsingIfExistsDecl>(D)) 103 return false; 104 105 return true; 106 } 107 108 static void DiagnoseUnusedOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc) { 109 // Warn if this is used but marked unused. 110 if (const auto *A = D->getAttr<UnusedAttr>()) { 111 // [[maybe_unused]] should not diagnose uses, but __attribute__((unused)) 112 // should diagnose them. 113 if (A->getSemanticSpelling() != UnusedAttr::CXX11_maybe_unused && 114 A->getSemanticSpelling() != UnusedAttr::C23_maybe_unused) { 115 const Decl *DC = cast_or_null<Decl>(S.ObjC().getCurObjCLexicalContext()); 116 if (DC && !DC->hasAttr<UnusedAttr>()) 117 S.Diag(Loc, diag::warn_used_but_marked_unused) << D; 118 } 119 } 120 } 121 122 void Sema::NoteDeletedFunction(FunctionDecl *Decl) { 123 assert(Decl && Decl->isDeleted()); 124 125 if (Decl->isDefaulted()) { 126 // If the method was explicitly defaulted, point at that declaration. 127 if (!Decl->isImplicit()) 128 Diag(Decl->getLocation(), diag::note_implicitly_deleted); 129 130 // Try to diagnose why this special member function was implicitly 131 // deleted. This might fail, if that reason no longer applies. 132 DiagnoseDeletedDefaultedFunction(Decl); 133 return; 134 } 135 136 auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl); 137 if (Ctor && Ctor->isInheritingConstructor()) 138 return NoteDeletedInheritingConstructor(Ctor); 139 140 Diag(Decl->getLocation(), diag::note_availability_specified_here) 141 << Decl << 1; 142 } 143 144 /// Determine whether a FunctionDecl was ever declared with an 145 /// explicit storage class. 146 static bool hasAnyExplicitStorageClass(const FunctionDecl *D) { 147 for (auto *I : D->redecls()) { 148 if (I->getStorageClass() != SC_None) 149 return true; 150 } 151 return false; 152 } 153 154 /// Check whether we're in an extern inline function and referring to a 155 /// variable or function with internal linkage (C11 6.7.4p3). 156 /// 157 /// This is only a warning because we used to silently accept this code, but 158 /// in many cases it will not behave correctly. This is not enabled in C++ mode 159 /// because the restriction language is a bit weaker (C++11 [basic.def.odr]p6) 160 /// and so while there may still be user mistakes, most of the time we can't 161 /// prove that there are errors. 162 static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S, 163 const NamedDecl *D, 164 SourceLocation Loc) { 165 // This is disabled under C++; there are too many ways for this to fire in 166 // contexts where the warning is a false positive, or where it is technically 167 // correct but benign. 168 if (S.getLangOpts().CPlusPlus) 169 return; 170 171 // Check if this is an inlined function or method. 172 FunctionDecl *Current = S.getCurFunctionDecl(); 173 if (!Current) 174 return; 175 if (!Current->isInlined()) 176 return; 177 if (!Current->isExternallyVisible()) 178 return; 179 180 // Check if the decl has internal linkage. 181 if (D->getFormalLinkage() != Linkage::Internal) 182 return; 183 184 // Downgrade from ExtWarn to Extension if 185 // (1) the supposedly external inline function is in the main file, 186 // and probably won't be included anywhere else. 187 // (2) the thing we're referencing is a pure function. 188 // (3) the thing we're referencing is another inline function. 189 // This last can give us false negatives, but it's better than warning on 190 // wrappers for simple C library functions. 191 const FunctionDecl *UsedFn = dyn_cast<FunctionDecl>(D); 192 bool DowngradeWarning = S.getSourceManager().isInMainFile(Loc); 193 if (!DowngradeWarning && UsedFn) 194 DowngradeWarning = UsedFn->isInlined() || UsedFn->hasAttr<ConstAttr>(); 195 196 S.Diag(Loc, DowngradeWarning ? diag::ext_internal_in_extern_inline_quiet 197 : diag::ext_internal_in_extern_inline) 198 << /*IsVar=*/!UsedFn << D; 199 200 S.MaybeSuggestAddingStaticToDecl(Current); 201 202 S.Diag(D->getCanonicalDecl()->getLocation(), diag::note_entity_declared_at) 203 << D; 204 } 205 206 void Sema::MaybeSuggestAddingStaticToDecl(const FunctionDecl *Cur) { 207 const FunctionDecl *First = Cur->getFirstDecl(); 208 209 // Suggest "static" on the function, if possible. 210 if (!hasAnyExplicitStorageClass(First)) { 211 SourceLocation DeclBegin = First->getSourceRange().getBegin(); 212 Diag(DeclBegin, diag::note_convert_inline_to_static) 213 << Cur << FixItHint::CreateInsertion(DeclBegin, "static "); 214 } 215 } 216 217 bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs, 218 const ObjCInterfaceDecl *UnknownObjCClass, 219 bool ObjCPropertyAccess, 220 bool AvoidPartialAvailabilityChecks, 221 ObjCInterfaceDecl *ClassReceiver, 222 bool SkipTrailingRequiresClause) { 223 SourceLocation Loc = Locs.front(); 224 if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)) { 225 // If there were any diagnostics suppressed by template argument deduction, 226 // emit them now. 227 auto Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); 228 if (Pos != SuppressedDiagnostics.end()) { 229 for (const auto &[DiagLoc, PD] : Pos->second) { 230 DiagnosticBuilder Builder(Diags.Report(DiagLoc, PD.getDiagID())); 231 PD.Emit(Builder); 232 } 233 // Clear out the list of suppressed diagnostics, so that we don't emit 234 // them again for this specialization. However, we don't obsolete this 235 // entry from the table, because we want to avoid ever emitting these 236 // diagnostics again. 237 Pos->second.clear(); 238 } 239 240 // C++ [basic.start.main]p3: 241 // The function 'main' shall not be used within a program. 242 if (cast<FunctionDecl>(D)->isMain()) 243 Diag(Loc, diag::ext_main_used); 244 245 diagnoseUnavailableAlignedAllocation(*cast<FunctionDecl>(D), Loc); 246 } 247 248 // See if this is an auto-typed variable whose initializer we are parsing. 249 if (ParsingInitForAutoVars.count(D)) { 250 if (isa<BindingDecl>(D)) { 251 Diag(Loc, diag::err_binding_cannot_appear_in_own_initializer) 252 << D->getDeclName(); 253 } else { 254 Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) 255 << diag::ParsingInitFor::Var << D->getDeclName() 256 << cast<VarDecl>(D)->getType(); 257 } 258 return true; 259 } 260 261 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 262 // See if this is a deleted function. 263 if (FD->isDeleted()) { 264 auto *Ctor = dyn_cast<CXXConstructorDecl>(FD); 265 if (Ctor && Ctor->isInheritingConstructor()) 266 Diag(Loc, diag::err_deleted_inherited_ctor_use) 267 << Ctor->getParent() 268 << Ctor->getInheritedConstructor().getConstructor()->getParent(); 269 else { 270 StringLiteral *Msg = FD->getDeletedMessage(); 271 Diag(Loc, diag::err_deleted_function_use) 272 << (Msg != nullptr) << (Msg ? Msg->getString() : StringRef()); 273 } 274 NoteDeletedFunction(FD); 275 return true; 276 } 277 278 // [expr.prim.id]p4 279 // A program that refers explicitly or implicitly to a function with a 280 // trailing requires-clause whose constraint-expression is not satisfied, 281 // other than to declare it, is ill-formed. [...] 282 // 283 // See if this is a function with constraints that need to be satisfied. 284 // Check this before deducing the return type, as it might instantiate the 285 // definition. 286 if (!SkipTrailingRequiresClause && FD->getTrailingRequiresClause()) { 287 ConstraintSatisfaction Satisfaction; 288 if (CheckFunctionConstraints(FD, Satisfaction, Loc, 289 /*ForOverloadResolution*/ true)) 290 // A diagnostic will have already been generated (non-constant 291 // constraint expression, for example) 292 return true; 293 if (!Satisfaction.IsSatisfied) { 294 Diag(Loc, 295 diag::err_reference_to_function_with_unsatisfied_constraints) 296 << D; 297 DiagnoseUnsatisfiedConstraint(Satisfaction); 298 return true; 299 } 300 } 301 302 // If the function has a deduced return type, and we can't deduce it, 303 // then we can't use it either. 304 if (getLangOpts().CPlusPlus14 && FD->getReturnType()->isUndeducedType() && 305 DeduceReturnType(FD, Loc)) 306 return true; 307 308 if (getLangOpts().CUDA && !CUDA().CheckCall(Loc, FD)) 309 return true; 310 311 } 312 313 if (auto *Concept = dyn_cast<ConceptDecl>(D); 314 Concept && CheckConceptUseInDefinition(Concept, Loc)) 315 return true; 316 317 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) { 318 // Lambdas are only default-constructible or assignable in C++2a onwards. 319 if (MD->getParent()->isLambda() && 320 ((isa<CXXConstructorDecl>(MD) && 321 cast<CXXConstructorDecl>(MD)->isDefaultConstructor()) || 322 MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())) { 323 Diag(Loc, diag::warn_cxx17_compat_lambda_def_ctor_assign) 324 << !isa<CXXConstructorDecl>(MD); 325 } 326 } 327 328 auto getReferencedObjCProp = [](const NamedDecl *D) -> 329 const ObjCPropertyDecl * { 330 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) 331 return MD->findPropertyDecl(); 332 return nullptr; 333 }; 334 if (const ObjCPropertyDecl *ObjCPDecl = getReferencedObjCProp(D)) { 335 if (diagnoseArgIndependentDiagnoseIfAttrs(ObjCPDecl, Loc)) 336 return true; 337 } else if (diagnoseArgIndependentDiagnoseIfAttrs(D, Loc)) { 338 return true; 339 } 340 341 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 342 // Only the variables omp_in and omp_out are allowed in the combiner. 343 // Only the variables omp_priv and omp_orig are allowed in the 344 // initializer-clause. 345 auto *DRD = dyn_cast<OMPDeclareReductionDecl>(CurContext); 346 if (LangOpts.OpenMP && DRD && !CurContext->containsDecl(D) && 347 isa<VarDecl>(D)) { 348 Diag(Loc, diag::err_omp_wrong_var_in_declare_reduction) 349 << getCurFunction()->HasOMPDeclareReductionCombiner; 350 Diag(D->getLocation(), diag::note_entity_declared_at) << D; 351 return true; 352 } 353 354 // [OpenMP 5.0], 2.19.7.3. declare mapper Directive, Restrictions 355 // List-items in map clauses on this construct may only refer to the declared 356 // variable var and entities that could be referenced by a procedure defined 357 // at the same location. 358 // [OpenMP 5.2] Also allow iterator declared variables. 359 if (LangOpts.OpenMP && isa<VarDecl>(D) && 360 !OpenMP().isOpenMPDeclareMapperVarDeclAllowed(cast<VarDecl>(D))) { 361 Diag(Loc, diag::err_omp_declare_mapper_wrong_var) 362 << OpenMP().getOpenMPDeclareMapperVarName(); 363 Diag(D->getLocation(), diag::note_entity_declared_at) << D; 364 return true; 365 } 366 367 if (const auto *EmptyD = dyn_cast<UnresolvedUsingIfExistsDecl>(D)) { 368 Diag(Loc, diag::err_use_of_empty_using_if_exists); 369 Diag(EmptyD->getLocation(), diag::note_empty_using_if_exists_here); 370 return true; 371 } 372 373 DiagnoseAvailabilityOfDecl(D, Locs, UnknownObjCClass, ObjCPropertyAccess, 374 AvoidPartialAvailabilityChecks, ClassReceiver); 375 376 DiagnoseUnusedOfDecl(*this, D, Loc); 377 378 diagnoseUseOfInternalDeclInInlineFunction(*this, D, Loc); 379 380 if (D->hasAttr<AvailableOnlyInDefaultEvalMethodAttr>()) { 381 if (getLangOpts().getFPEvalMethod() != 382 LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine && 383 PP.getLastFPEvalPragmaLocation().isValid() && 384 PP.getCurrentFPEvalMethod() != getLangOpts().getFPEvalMethod()) 385 Diag(D->getLocation(), 386 diag::err_type_available_only_in_default_eval_method) 387 << D->getName(); 388 } 389 390 if (auto *VD = dyn_cast<ValueDecl>(D)) 391 checkTypeSupport(VD->getType(), Loc, VD); 392 393 if (LangOpts.SYCLIsDevice || 394 (LangOpts.OpenMP && LangOpts.OpenMPIsTargetDevice)) { 395 if (!Context.getTargetInfo().isTLSSupported()) 396 if (const auto *VD = dyn_cast<VarDecl>(D)) 397 if (VD->getTLSKind() != VarDecl::TLS_None) 398 targetDiag(*Locs.begin(), diag::err_thread_unsupported); 399 } 400 401 return false; 402 } 403 404 void Sema::DiagnoseSentinelCalls(const NamedDecl *D, SourceLocation Loc, 405 ArrayRef<Expr *> Args) { 406 const SentinelAttr *Attr = D->getAttr<SentinelAttr>(); 407 if (!Attr) 408 return; 409 410 // The number of formal parameters of the declaration. 411 unsigned NumFormalParams; 412 413 // The kind of declaration. This is also an index into a %select in 414 // the diagnostic. 415 enum { CK_Function, CK_Method, CK_Block } CalleeKind; 416 417 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { 418 NumFormalParams = MD->param_size(); 419 CalleeKind = CK_Method; 420 } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 421 NumFormalParams = FD->param_size(); 422 CalleeKind = CK_Function; 423 } else if (const auto *VD = dyn_cast<VarDecl>(D)) { 424 QualType Ty = VD->getType(); 425 const FunctionType *Fn = nullptr; 426 if (const auto *PtrTy = Ty->getAs<PointerType>()) { 427 Fn = PtrTy->getPointeeType()->getAs<FunctionType>(); 428 if (!Fn) 429 return; 430 CalleeKind = CK_Function; 431 } else if (const auto *PtrTy = Ty->getAs<BlockPointerType>()) { 432 Fn = PtrTy->getPointeeType()->castAs<FunctionType>(); 433 CalleeKind = CK_Block; 434 } else { 435 return; 436 } 437 438 if (const auto *proto = dyn_cast<FunctionProtoType>(Fn)) 439 NumFormalParams = proto->getNumParams(); 440 else 441 NumFormalParams = 0; 442 } else { 443 return; 444 } 445 446 // "NullPos" is the number of formal parameters at the end which 447 // effectively count as part of the variadic arguments. This is 448 // useful if you would prefer to not have *any* formal parameters, 449 // but the language forces you to have at least one. 450 unsigned NullPos = Attr->getNullPos(); 451 assert((NullPos == 0 || NullPos == 1) && "invalid null position on sentinel"); 452 NumFormalParams = (NullPos > NumFormalParams ? 0 : NumFormalParams - NullPos); 453 454 // The number of arguments which should follow the sentinel. 455 unsigned NumArgsAfterSentinel = Attr->getSentinel(); 456 457 // If there aren't enough arguments for all the formal parameters, 458 // the sentinel, and the args after the sentinel, complain. 459 if (Args.size() < NumFormalParams + NumArgsAfterSentinel + 1) { 460 Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); 461 Diag(D->getLocation(), diag::note_sentinel_here) << int(CalleeKind); 462 return; 463 } 464 465 // Otherwise, find the sentinel expression. 466 const Expr *SentinelExpr = Args[Args.size() - NumArgsAfterSentinel - 1]; 467 if (!SentinelExpr) 468 return; 469 if (SentinelExpr->isValueDependent()) 470 return; 471 if (Context.isSentinelNullExpr(SentinelExpr)) 472 return; 473 474 // Pick a reasonable string to insert. Optimistically use 'nil', 'nullptr', 475 // or 'NULL' if those are actually defined in the context. Only use 476 // 'nil' for ObjC methods, where it's much more likely that the 477 // variadic arguments form a list of object pointers. 478 SourceLocation MissingNilLoc = getLocForEndOfToken(SentinelExpr->getEndLoc()); 479 std::string NullValue; 480 if (CalleeKind == CK_Method && PP.isMacroDefined("nil")) 481 NullValue = "nil"; 482 else if (getLangOpts().CPlusPlus11) 483 NullValue = "nullptr"; 484 else if (PP.isMacroDefined("NULL")) 485 NullValue = "NULL"; 486 else 487 NullValue = "(void*) 0"; 488 489 if (MissingNilLoc.isInvalid()) 490 Diag(Loc, diag::warn_missing_sentinel) << int(CalleeKind); 491 else 492 Diag(MissingNilLoc, diag::warn_missing_sentinel) 493 << int(CalleeKind) 494 << FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue); 495 Diag(D->getLocation(), diag::note_sentinel_here) 496 << int(CalleeKind) << Attr->getRange(); 497 } 498 499 SourceRange Sema::getExprRange(Expr *E) const { 500 return E ? E->getSourceRange() : SourceRange(); 501 } 502 503 //===----------------------------------------------------------------------===// 504 // Standard Promotions and Conversions 505 //===----------------------------------------------------------------------===// 506 507 /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). 508 ExprResult Sema::DefaultFunctionArrayConversion(Expr *E, bool Diagnose) { 509 // Handle any placeholder expressions which made it here. 510 if (E->hasPlaceholderType()) { 511 ExprResult result = CheckPlaceholderExpr(E); 512 if (result.isInvalid()) return ExprError(); 513 E = result.get(); 514 } 515 516 QualType Ty = E->getType(); 517 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); 518 519 if (Ty->isFunctionType()) { 520 if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts())) 521 if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) 522 if (!checkAddressOfFunctionIsAvailable(FD, Diagnose, E->getExprLoc())) 523 return ExprError(); 524 525 E = ImpCastExprToType(E, Context.getPointerType(Ty), 526 CK_FunctionToPointerDecay).get(); 527 } else if (Ty->isArrayType()) { 528 // In C90 mode, arrays only promote to pointers if the array expression is 529 // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has 530 // type 'array of type' is converted to an expression that has type 'pointer 531 // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression 532 // that has type 'array of type' ...". The relevant change is "an lvalue" 533 // (C90) to "an expression" (C99). 534 // 535 // C++ 4.2p1: 536 // An lvalue or rvalue of type "array of N T" or "array of unknown bound of 537 // T" can be converted to an rvalue of type "pointer to T". 538 // 539 if (getLangOpts().C99 || getLangOpts().CPlusPlus || E->isLValue()) { 540 ExprResult Res = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), 541 CK_ArrayToPointerDecay); 542 if (Res.isInvalid()) 543 return ExprError(); 544 E = Res.get(); 545 } 546 } 547 return E; 548 } 549 550 static void CheckForNullPointerDereference(Sema &S, Expr *E) { 551 // Check to see if we are dereferencing a null pointer. If so, 552 // and if not volatile-qualified, this is undefined behavior that the 553 // optimizer will delete, so warn about it. People sometimes try to use this 554 // to get a deterministic trap and are surprised by clang's behavior. This 555 // only handles the pattern "*null", which is a very syntactic check. 556 const auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts()); 557 if (UO && UO->getOpcode() == UO_Deref && 558 UO->getSubExpr()->getType()->isPointerType()) { 559 const LangAS AS = 560 UO->getSubExpr()->getType()->getPointeeType().getAddressSpace(); 561 if ((!isTargetAddressSpace(AS) || 562 (isTargetAddressSpace(AS) && toTargetAddressSpace(AS) == 0)) && 563 UO->getSubExpr()->IgnoreParenCasts()->isNullPointerConstant( 564 S.Context, Expr::NPC_ValueDependentIsNotNull) && 565 !UO->getType().isVolatileQualified()) { 566 S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, 567 S.PDiag(diag::warn_indirection_through_null) 568 << UO->getSubExpr()->getSourceRange()); 569 S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, 570 S.PDiag(diag::note_indirection_through_null)); 571 } 572 } 573 } 574 575 static void DiagnoseDirectIsaAccess(Sema &S, const ObjCIvarRefExpr *OIRE, 576 SourceLocation AssignLoc, 577 const Expr* RHS) { 578 const ObjCIvarDecl *IV = OIRE->getDecl(); 579 if (!IV) 580 return; 581 582 DeclarationName MemberName = IV->getDeclName(); 583 IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); 584 if (!Member || !Member->isStr("isa")) 585 return; 586 587 const Expr *Base = OIRE->getBase(); 588 QualType BaseType = Base->getType(); 589 if (OIRE->isArrow()) 590 BaseType = BaseType->getPointeeType(); 591 if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) 592 if (ObjCInterfaceDecl *IDecl = OTy->getInterface()) { 593 ObjCInterfaceDecl *ClassDeclared = nullptr; 594 ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); 595 if (!ClassDeclared->getSuperClass() 596 && (*ClassDeclared->ivar_begin()) == IV) { 597 if (RHS) { 598 NamedDecl *ObjectSetClass = 599 S.LookupSingleName(S.TUScope, 600 &S.Context.Idents.get("object_setClass"), 601 SourceLocation(), S.LookupOrdinaryName); 602 if (ObjectSetClass) { 603 SourceLocation RHSLocEnd = S.getLocForEndOfToken(RHS->getEndLoc()); 604 S.Diag(OIRE->getExprLoc(), diag::warn_objc_isa_assign) 605 << FixItHint::CreateInsertion(OIRE->getBeginLoc(), 606 "object_setClass(") 607 << FixItHint::CreateReplacement( 608 SourceRange(OIRE->getOpLoc(), AssignLoc), ",") 609 << FixItHint::CreateInsertion(RHSLocEnd, ")"); 610 } 611 else 612 S.Diag(OIRE->getLocation(), diag::warn_objc_isa_assign); 613 } else { 614 NamedDecl *ObjectGetClass = 615 S.LookupSingleName(S.TUScope, 616 &S.Context.Idents.get("object_getClass"), 617 SourceLocation(), S.LookupOrdinaryName); 618 if (ObjectGetClass) 619 S.Diag(OIRE->getExprLoc(), diag::warn_objc_isa_use) 620 << FixItHint::CreateInsertion(OIRE->getBeginLoc(), 621 "object_getClass(") 622 << FixItHint::CreateReplacement( 623 SourceRange(OIRE->getOpLoc(), OIRE->getEndLoc()), ")"); 624 else 625 S.Diag(OIRE->getLocation(), diag::warn_objc_isa_use); 626 } 627 S.Diag(IV->getLocation(), diag::note_ivar_decl); 628 } 629 } 630 } 631 632 ExprResult Sema::DefaultLvalueConversion(Expr *E) { 633 // Handle any placeholder expressions which made it here. 634 if (E->hasPlaceholderType()) { 635 ExprResult result = CheckPlaceholderExpr(E); 636 if (result.isInvalid()) return ExprError(); 637 E = result.get(); 638 } 639 640 // C++ [conv.lval]p1: 641 // A glvalue of a non-function, non-array type T can be 642 // converted to a prvalue. 643 if (!E->isGLValue()) return E; 644 645 QualType T = E->getType(); 646 assert(!T.isNull() && "r-value conversion on typeless expression?"); 647 648 // lvalue-to-rvalue conversion cannot be applied to types that decay to 649 // pointers (i.e. function or array types). 650 if (T->canDecayToPointerType()) 651 return E; 652 653 // We don't want to throw lvalue-to-rvalue casts on top of 654 // expressions of certain types in C++. 655 if (getLangOpts().CPlusPlus) { 656 if (T == Context.OverloadTy || T->isRecordType() || 657 (T->isDependentType() && !T->isAnyPointerType() && 658 !T->isMemberPointerType())) 659 return E; 660 } 661 662 // The C standard is actually really unclear on this point, and 663 // DR106 tells us what the result should be but not why. It's 664 // generally best to say that void types just doesn't undergo 665 // lvalue-to-rvalue at all. Note that expressions of unqualified 666 // 'void' type are never l-values, but qualified void can be. 667 if (T->isVoidType()) 668 return E; 669 670 // OpenCL usually rejects direct accesses to values of 'half' type. 671 if (getLangOpts().OpenCL && 672 !getOpenCLOptions().isAvailableOption("cl_khr_fp16", getLangOpts()) && 673 T->isHalfType()) { 674 Diag(E->getExprLoc(), diag::err_opencl_half_load_store) 675 << 0 << T; 676 return ExprError(); 677 } 678 679 CheckForNullPointerDereference(*this, E); 680 if (const ObjCIsaExpr *OISA = dyn_cast<ObjCIsaExpr>(E->IgnoreParenCasts())) { 681 NamedDecl *ObjectGetClass = LookupSingleName(TUScope, 682 &Context.Idents.get("object_getClass"), 683 SourceLocation(), LookupOrdinaryName); 684 if (ObjectGetClass) 685 Diag(E->getExprLoc(), diag::warn_objc_isa_use) 686 << FixItHint::CreateInsertion(OISA->getBeginLoc(), "object_getClass(") 687 << FixItHint::CreateReplacement( 688 SourceRange(OISA->getOpLoc(), OISA->getIsaMemberLoc()), ")"); 689 else 690 Diag(E->getExprLoc(), diag::warn_objc_isa_use); 691 } 692 else if (const ObjCIvarRefExpr *OIRE = 693 dyn_cast<ObjCIvarRefExpr>(E->IgnoreParenCasts())) 694 DiagnoseDirectIsaAccess(*this, OIRE, SourceLocation(), /* Expr*/nullptr); 695 696 // C++ [conv.lval]p1: 697 // [...] If T is a non-class type, the type of the prvalue is the 698 // cv-unqualified version of T. Otherwise, the type of the 699 // rvalue is T. 700 // 701 // C99 6.3.2.1p2: 702 // If the lvalue has qualified type, the value has the unqualified 703 // version of the type of the lvalue; otherwise, the value has the 704 // type of the lvalue. 705 if (T.hasQualifiers()) 706 T = T.getUnqualifiedType(); 707 708 // Under the MS ABI, lock down the inheritance model now. 709 if (T->isMemberPointerType() && 710 Context.getTargetInfo().getCXXABI().isMicrosoft()) 711 (void)isCompleteType(E->getExprLoc(), T); 712 713 ExprResult Res = CheckLValueToRValueConversionOperand(E); 714 if (Res.isInvalid()) 715 return Res; 716 E = Res.get(); 717 718 // Loading a __weak object implicitly retains the value, so we need a cleanup to 719 // balance that. 720 if (E->getType().getObjCLifetime() == Qualifiers::OCL_Weak) 721 Cleanup.setExprNeedsCleanups(true); 722 723 if (E->getType().isDestructedType() == QualType::DK_nontrivial_c_struct) 724 Cleanup.setExprNeedsCleanups(true); 725 726 if (!BoundsSafetyCheckUseOfCountAttrPtr(Res.get())) 727 return ExprError(); 728 729 // C++ [conv.lval]p3: 730 // If T is cv std::nullptr_t, the result is a null pointer constant. 731 CastKind CK = T->isNullPtrType() ? CK_NullToPointer : CK_LValueToRValue; 732 Res = ImplicitCastExpr::Create(Context, T, CK, E, nullptr, VK_PRValue, 733 CurFPFeatureOverrides()); 734 735 // C11 6.3.2.1p2: 736 // ... if the lvalue has atomic type, the value has the non-atomic version 737 // of the type of the lvalue ... 738 if (const AtomicType *Atomic = T->getAs<AtomicType>()) { 739 T = Atomic->getValueType().getUnqualifiedType(); 740 Res = ImplicitCastExpr::Create(Context, T, CK_AtomicToNonAtomic, Res.get(), 741 nullptr, VK_PRValue, FPOptionsOverride()); 742 } 743 744 return Res; 745 } 746 747 ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E, bool Diagnose) { 748 ExprResult Res = DefaultFunctionArrayConversion(E, Diagnose); 749 if (Res.isInvalid()) 750 return ExprError(); 751 Res = DefaultLvalueConversion(Res.get()); 752 if (Res.isInvalid()) 753 return ExprError(); 754 return Res; 755 } 756 757 ExprResult Sema::CallExprUnaryConversions(Expr *E) { 758 QualType Ty = E->getType(); 759 ExprResult Res = E; 760 // Only do implicit cast for a function type, but not for a pointer 761 // to function type. 762 if (Ty->isFunctionType()) { 763 Res = ImpCastExprToType(E, Context.getPointerType(Ty), 764 CK_FunctionToPointerDecay); 765 if (Res.isInvalid()) 766 return ExprError(); 767 } 768 Res = DefaultLvalueConversion(Res.get()); 769 if (Res.isInvalid()) 770 return ExprError(); 771 return Res.get(); 772 } 773 774 /// UsualUnaryFPConversions - Promotes floating-point types according to the 775 /// current language semantics. 776 ExprResult Sema::UsualUnaryFPConversions(Expr *E) { 777 QualType Ty = E->getType(); 778 assert(!Ty.isNull() && "UsualUnaryFPConversions - missing type"); 779 780 LangOptions::FPEvalMethodKind EvalMethod = CurFPFeatures.getFPEvalMethod(); 781 if (EvalMethod != LangOptions::FEM_Source && Ty->isFloatingType() && 782 (getLangOpts().getFPEvalMethod() != 783 LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine || 784 PP.getLastFPEvalPragmaLocation().isValid())) { 785 switch (EvalMethod) { 786 default: 787 llvm_unreachable("Unrecognized float evaluation method"); 788 break; 789 case LangOptions::FEM_UnsetOnCommandLine: 790 llvm_unreachable("Float evaluation method should be set by now"); 791 break; 792 case LangOptions::FEM_Double: 793 if (Context.getFloatingTypeOrder(Context.DoubleTy, Ty) > 0) 794 // Widen the expression to double. 795 return Ty->isComplexType() 796 ? ImpCastExprToType(E, 797 Context.getComplexType(Context.DoubleTy), 798 CK_FloatingComplexCast) 799 : ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast); 800 break; 801 case LangOptions::FEM_Extended: 802 if (Context.getFloatingTypeOrder(Context.LongDoubleTy, Ty) > 0) 803 // Widen the expression to long double. 804 return Ty->isComplexType() 805 ? ImpCastExprToType( 806 E, Context.getComplexType(Context.LongDoubleTy), 807 CK_FloatingComplexCast) 808 : ImpCastExprToType(E, Context.LongDoubleTy, 809 CK_FloatingCast); 810 break; 811 } 812 } 813 814 // Half FP have to be promoted to float unless it is natively supported 815 if (Ty->isHalfType() && !getLangOpts().NativeHalfType) 816 return ImpCastExprToType(E, Context.FloatTy, CK_FloatingCast); 817 818 return E; 819 } 820 821 /// UsualUnaryConversions - Performs various conversions that are common to most 822 /// operators (C99 6.3). The conversions of array and function types are 823 /// sometimes suppressed. For example, the array->pointer conversion doesn't 824 /// apply if the array is an argument to the sizeof or address (&) operators. 825 /// In these instances, this routine should *not* be called. 826 ExprResult Sema::UsualUnaryConversions(Expr *E) { 827 // First, convert to an r-value. 828 ExprResult Res = DefaultFunctionArrayLvalueConversion(E); 829 if (Res.isInvalid()) 830 return ExprError(); 831 832 // Promote floating-point types. 833 Res = UsualUnaryFPConversions(Res.get()); 834 if (Res.isInvalid()) 835 return ExprError(); 836 E = Res.get(); 837 838 QualType Ty = E->getType(); 839 assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); 840 841 // Try to perform integral promotions if the object has a theoretically 842 // promotable type. 843 if (Ty->isIntegralOrUnscopedEnumerationType()) { 844 // C99 6.3.1.1p2: 845 // 846 // The following may be used in an expression wherever an int or 847 // unsigned int may be used: 848 // - an object or expression with an integer type whose integer 849 // conversion rank is less than or equal to the rank of int 850 // and unsigned int. 851 // - A bit-field of type _Bool, int, signed int, or unsigned int. 852 // 853 // If an int can represent all values of the original type, the 854 // value is converted to an int; otherwise, it is converted to an 855 // unsigned int. These are called the integer promotions. All 856 // other types are unchanged by the integer promotions. 857 858 QualType PTy = Context.isPromotableBitField(E); 859 if (!PTy.isNull()) { 860 E = ImpCastExprToType(E, PTy, CK_IntegralCast).get(); 861 return E; 862 } 863 if (Context.isPromotableIntegerType(Ty)) { 864 QualType PT = Context.getPromotedIntegerType(Ty); 865 E = ImpCastExprToType(E, PT, CK_IntegralCast).get(); 866 return E; 867 } 868 } 869 return E; 870 } 871 872 /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that 873 /// do not have a prototype. Arguments that have type float or __fp16 874 /// are promoted to double. All other argument types are converted by 875 /// UsualUnaryConversions(). 876 ExprResult Sema::DefaultArgumentPromotion(Expr *E) { 877 QualType Ty = E->getType(); 878 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); 879 880 ExprResult Res = UsualUnaryConversions(E); 881 if (Res.isInvalid()) 882 return ExprError(); 883 E = Res.get(); 884 885 // If this is a 'float' or '__fp16' (CVR qualified or typedef) 886 // promote to double. 887 // Note that default argument promotion applies only to float (and 888 // half/fp16); it does not apply to _Float16. 889 const BuiltinType *BTy = Ty->getAs<BuiltinType>(); 890 if (BTy && (BTy->getKind() == BuiltinType::Half || 891 BTy->getKind() == BuiltinType::Float)) { 892 if (getLangOpts().OpenCL && 893 !getOpenCLOptions().isAvailableOption("cl_khr_fp64", getLangOpts())) { 894 if (BTy->getKind() == BuiltinType::Half) { 895 E = ImpCastExprToType(E, Context.FloatTy, CK_FloatingCast).get(); 896 } 897 } else { 898 E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).get(); 899 } 900 } 901 if (BTy && 902 getLangOpts().getExtendIntArgs() == 903 LangOptions::ExtendArgsKind::ExtendTo64 && 904 Context.getTargetInfo().supportsExtendIntArgs() && Ty->isIntegerType() && 905 Context.getTypeSizeInChars(BTy) < 906 Context.getTypeSizeInChars(Context.LongLongTy)) { 907 E = (Ty->isUnsignedIntegerType()) 908 ? ImpCastExprToType(E, Context.UnsignedLongLongTy, CK_IntegralCast) 909 .get() 910 : ImpCastExprToType(E, Context.LongLongTy, CK_IntegralCast).get(); 911 assert(8 == Context.getTypeSizeInChars(Context.LongLongTy).getQuantity() && 912 "Unexpected typesize for LongLongTy"); 913 } 914 915 // C++ performs lvalue-to-rvalue conversion as a default argument 916 // promotion, even on class types, but note: 917 // C++11 [conv.lval]p2: 918 // When an lvalue-to-rvalue conversion occurs in an unevaluated 919 // operand or a subexpression thereof the value contained in the 920 // referenced object is not accessed. Otherwise, if the glvalue 921 // has a class type, the conversion copy-initializes a temporary 922 // of type T from the glvalue and the result of the conversion 923 // is a prvalue for the temporary. 924 // FIXME: add some way to gate this entire thing for correctness in 925 // potentially potentially evaluated contexts. 926 if (getLangOpts().CPlusPlus && E->isGLValue() && !isUnevaluatedContext()) { 927 ExprResult Temp = PerformCopyInitialization( 928 InitializedEntity::InitializeTemporary(E->getType()), 929 E->getExprLoc(), E); 930 if (Temp.isInvalid()) 931 return ExprError(); 932 E = Temp.get(); 933 } 934 935 // C++ [expr.call]p7, per CWG722: 936 // An argument that has (possibly cv-qualified) type std::nullptr_t is 937 // converted to void* ([conv.ptr]). 938 // (This does not apply to C23 nullptr) 939 if (getLangOpts().CPlusPlus && E->getType()->isNullPtrType()) 940 E = ImpCastExprToType(E, Context.VoidPtrTy, CK_NullToPointer).get(); 941 942 return E; 943 } 944 945 VarArgKind Sema::isValidVarArgType(const QualType &Ty) { 946 if (Ty->isIncompleteType()) { 947 // C++11 [expr.call]p7: 948 // After these conversions, if the argument does not have arithmetic, 949 // enumeration, pointer, pointer to member, or class type, the program 950 // is ill-formed. 951 // 952 // Since we've already performed null pointer conversion, array-to-pointer 953 // decay and function-to-pointer decay, the only such type in C++ is cv 954 // void. This also handles initializer lists as variadic arguments. 955 if (Ty->isVoidType()) 956 return VarArgKind::Invalid; 957 958 if (Ty->isObjCObjectType()) 959 return VarArgKind::Invalid; 960 return VarArgKind::Valid; 961 } 962 963 if (Ty.isDestructedType() == QualType::DK_nontrivial_c_struct) 964 return VarArgKind::Invalid; 965 966 if (Context.getTargetInfo().getTriple().isWasm() && 967 Ty.isWebAssemblyReferenceType()) { 968 return VarArgKind::Invalid; 969 } 970 971 if (Ty.isCXX98PODType(Context)) 972 return VarArgKind::Valid; 973 974 // C++11 [expr.call]p7: 975 // Passing a potentially-evaluated argument of class type (Clause 9) 976 // having a non-trivial copy constructor, a non-trivial move constructor, 977 // or a non-trivial destructor, with no corresponding parameter, 978 // is conditionally-supported with implementation-defined semantics. 979 if (getLangOpts().CPlusPlus11 && !Ty->isDependentType()) 980 if (CXXRecordDecl *Record = Ty->getAsCXXRecordDecl()) 981 if (!Record->hasNonTrivialCopyConstructor() && 982 !Record->hasNonTrivialMoveConstructor() && 983 !Record->hasNonTrivialDestructor()) 984 return VarArgKind::ValidInCXX11; 985 986 if (getLangOpts().ObjCAutoRefCount && Ty->isObjCLifetimeType()) 987 return VarArgKind::Valid; 988 989 if (Ty->isObjCObjectType()) 990 return VarArgKind::Invalid; 991 992 if (getLangOpts().HLSL && Ty->getAs<HLSLAttributedResourceType>()) 993 return VarArgKind::Valid; 994 995 if (getLangOpts().MSVCCompat) 996 return VarArgKind::MSVCUndefined; 997 998 if (getLangOpts().HLSL && Ty->getAs<HLSLAttributedResourceType>()) 999 return VarArgKind::Valid; 1000 1001 // FIXME: In C++11, these cases are conditionally-supported, meaning we're 1002 // permitted to reject them. We should consider doing so. 1003 return VarArgKind::Undefined; 1004 } 1005 1006 void Sema::checkVariadicArgument(const Expr *E, VariadicCallType CT) { 1007 // Don't allow one to pass an Objective-C interface to a vararg. 1008 const QualType &Ty = E->getType(); 1009 VarArgKind VAK = isValidVarArgType(Ty); 1010 1011 // Complain about passing non-POD types through varargs. 1012 switch (VAK) { 1013 case VarArgKind::ValidInCXX11: 1014 DiagRuntimeBehavior( 1015 E->getBeginLoc(), nullptr, 1016 PDiag(diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg) << Ty << CT); 1017 [[fallthrough]]; 1018 case VarArgKind::Valid: 1019 if (Ty->isRecordType()) { 1020 // This is unlikely to be what the user intended. If the class has a 1021 // 'c_str' member function, the user probably meant to call that. 1022 DiagRuntimeBehavior(E->getBeginLoc(), nullptr, 1023 PDiag(diag::warn_pass_class_arg_to_vararg) 1024 << Ty << CT << hasCStrMethod(E) << ".c_str()"); 1025 } 1026 break; 1027 1028 case VarArgKind::Undefined: 1029 case VarArgKind::MSVCUndefined: 1030 DiagRuntimeBehavior(E->getBeginLoc(), nullptr, 1031 PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) 1032 << getLangOpts().CPlusPlus11 << Ty << CT); 1033 break; 1034 1035 case VarArgKind::Invalid: 1036 if (Ty.isDestructedType() == QualType::DK_nontrivial_c_struct) 1037 Diag(E->getBeginLoc(), 1038 diag::err_cannot_pass_non_trivial_c_struct_to_vararg) 1039 << Ty << CT; 1040 else if (Ty->isObjCObjectType()) 1041 DiagRuntimeBehavior(E->getBeginLoc(), nullptr, 1042 PDiag(diag::err_cannot_pass_objc_interface_to_vararg) 1043 << Ty << CT); 1044 else 1045 Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg) 1046 << isa<InitListExpr>(E) << Ty << CT; 1047 break; 1048 } 1049 } 1050 1051 ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, 1052 FunctionDecl *FDecl) { 1053 if (const BuiltinType *PlaceholderTy = E->getType()->getAsPlaceholderType()) { 1054 // Strip the unbridged-cast placeholder expression off, if applicable. 1055 if (PlaceholderTy->getKind() == BuiltinType::ARCUnbridgedCast && 1056 (CT == VariadicCallType::Method || 1057 (FDecl && FDecl->hasAttr<CFAuditedTransferAttr>()))) { 1058 E = ObjC().stripARCUnbridgedCast(E); 1059 1060 // Otherwise, do normal placeholder checking. 1061 } else { 1062 ExprResult ExprRes = CheckPlaceholderExpr(E); 1063 if (ExprRes.isInvalid()) 1064 return ExprError(); 1065 E = ExprRes.get(); 1066 } 1067 } 1068 1069 ExprResult ExprRes = DefaultArgumentPromotion(E); 1070 if (ExprRes.isInvalid()) 1071 return ExprError(); 1072 1073 // Copy blocks to the heap. 1074 if (ExprRes.get()->getType()->isBlockPointerType()) 1075 maybeExtendBlockObject(ExprRes); 1076 1077 E = ExprRes.get(); 1078 1079 // Diagnostics regarding non-POD argument types are 1080 // emitted along with format string checking in Sema::CheckFunctionCall(). 1081 if (isValidVarArgType(E->getType()) == VarArgKind::Undefined) { 1082 // Turn this into a trap. 1083 CXXScopeSpec SS; 1084 SourceLocation TemplateKWLoc; 1085 UnqualifiedId Name; 1086 Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), 1087 E->getBeginLoc()); 1088 ExprResult TrapFn = ActOnIdExpression(TUScope, SS, TemplateKWLoc, Name, 1089 /*HasTrailingLParen=*/true, 1090 /*IsAddressOfOperand=*/false); 1091 if (TrapFn.isInvalid()) 1092 return ExprError(); 1093 1094 ExprResult Call = BuildCallExpr(TUScope, TrapFn.get(), E->getBeginLoc(), {}, 1095 E->getEndLoc()); 1096 if (Call.isInvalid()) 1097 return ExprError(); 1098 1099 ExprResult Comma = 1100 ActOnBinOp(TUScope, E->getBeginLoc(), tok::comma, Call.get(), E); 1101 if (Comma.isInvalid()) 1102 return ExprError(); 1103 return Comma.get(); 1104 } 1105 1106 if (!getLangOpts().CPlusPlus && 1107 RequireCompleteType(E->getExprLoc(), E->getType(), 1108 diag::err_call_incomplete_argument)) 1109 return ExprError(); 1110 1111 return E; 1112 } 1113 1114 /// Convert complex integers to complex floats and real integers to 1115 /// real floats as required for complex arithmetic. Helper function of 1116 /// UsualArithmeticConversions() 1117 /// 1118 /// \return false if the integer expression is an integer type and is 1119 /// successfully converted to the (complex) float type. 1120 static bool handleComplexIntegerToFloatConversion(Sema &S, ExprResult &IntExpr, 1121 ExprResult &ComplexExpr, 1122 QualType IntTy, 1123 QualType ComplexTy, 1124 bool SkipCast) { 1125 if (IntTy->isComplexType() || IntTy->isRealFloatingType()) return true; 1126 if (SkipCast) return false; 1127 if (IntTy->isIntegerType()) { 1128 QualType fpTy = ComplexTy->castAs<ComplexType>()->getElementType(); 1129 IntExpr = S.ImpCastExprToType(IntExpr.get(), fpTy, CK_IntegralToFloating); 1130 } else { 1131 assert(IntTy->isComplexIntegerType()); 1132 IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy, 1133 CK_IntegralComplexToFloatingComplex); 1134 } 1135 return false; 1136 } 1137 1138 // This handles complex/complex, complex/float, or float/complex. 1139 // When both operands are complex, the shorter operand is converted to the 1140 // type of the longer, and that is the type of the result. This corresponds 1141 // to what is done when combining two real floating-point operands. 1142 // The fun begins when size promotion occur across type domains. 1143 // From H&S 6.3.4: When one operand is complex and the other is a real 1144 // floating-point type, the less precise type is converted, within it's 1145 // real or complex domain, to the precision of the other type. For example, 1146 // when combining a "long double" with a "double _Complex", the 1147 // "double _Complex" is promoted to "long double _Complex". 1148 static QualType handleComplexFloatConversion(Sema &S, ExprResult &Shorter, 1149 QualType ShorterType, 1150 QualType LongerType, 1151 bool PromotePrecision) { 1152 bool LongerIsComplex = isa<ComplexType>(LongerType.getCanonicalType()); 1153 QualType Result = 1154 LongerIsComplex ? LongerType : S.Context.getComplexType(LongerType); 1155 1156 if (PromotePrecision) { 1157 if (isa<ComplexType>(ShorterType.getCanonicalType())) { 1158 Shorter = 1159 S.ImpCastExprToType(Shorter.get(), Result, CK_FloatingComplexCast); 1160 } else { 1161 if (LongerIsComplex) 1162 LongerType = LongerType->castAs<ComplexType>()->getElementType(); 1163 Shorter = S.ImpCastExprToType(Shorter.get(), LongerType, CK_FloatingCast); 1164 } 1165 } 1166 return Result; 1167 } 1168 1169 /// Handle arithmetic conversion with complex types. Helper function of 1170 /// UsualArithmeticConversions() 1171 static QualType handleComplexConversion(Sema &S, ExprResult &LHS, 1172 ExprResult &RHS, QualType LHSType, 1173 QualType RHSType, bool IsCompAssign) { 1174 // Handle (complex) integer types. 1175 if (!handleComplexIntegerToFloatConversion(S, RHS, LHS, RHSType, LHSType, 1176 /*SkipCast=*/false)) 1177 return LHSType; 1178 if (!handleComplexIntegerToFloatConversion(S, LHS, RHS, LHSType, RHSType, 1179 /*SkipCast=*/IsCompAssign)) 1180 return RHSType; 1181 1182 // Compute the rank of the two types, regardless of whether they are complex. 1183 int Order = S.Context.getFloatingTypeOrder(LHSType, RHSType); 1184 if (Order < 0) 1185 // Promote the precision of the LHS if not an assignment. 1186 return handleComplexFloatConversion(S, LHS, LHSType, RHSType, 1187 /*PromotePrecision=*/!IsCompAssign); 1188 // Promote the precision of the RHS unless it is already the same as the LHS. 1189 return handleComplexFloatConversion(S, RHS, RHSType, LHSType, 1190 /*PromotePrecision=*/Order > 0); 1191 } 1192 1193 /// Handle arithmetic conversion from integer to float. Helper function 1194 /// of UsualArithmeticConversions() 1195 static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, 1196 ExprResult &IntExpr, 1197 QualType FloatTy, QualType IntTy, 1198 bool ConvertFloat, bool ConvertInt) { 1199 if (IntTy->isIntegerType()) { 1200 if (ConvertInt) 1201 // Convert intExpr to the lhs floating point type. 1202 IntExpr = S.ImpCastExprToType(IntExpr.get(), FloatTy, 1203 CK_IntegralToFloating); 1204 return FloatTy; 1205 } 1206 1207 // Convert both sides to the appropriate complex float. 1208 assert(IntTy->isComplexIntegerType()); 1209 QualType result = S.Context.getComplexType(FloatTy); 1210 1211 // _Complex int -> _Complex float 1212 if (ConvertInt) 1213 IntExpr = S.ImpCastExprToType(IntExpr.get(), result, 1214 CK_IntegralComplexToFloatingComplex); 1215 1216 // float -> _Complex float 1217 if (ConvertFloat) 1218 FloatExpr = S.ImpCastExprToType(FloatExpr.get(), result, 1219 CK_FloatingRealToComplex); 1220 1221 return result; 1222 } 1223 1224 /// Handle arithmethic conversion with floating point types. Helper 1225 /// function of UsualArithmeticConversions() 1226 static QualType handleFloatConversion(Sema &S, ExprResult &LHS, 1227 ExprResult &RHS, QualType LHSType, 1228 QualType RHSType, bool IsCompAssign) { 1229 bool LHSFloat = LHSType->isRealFloatingType(); 1230 bool RHSFloat = RHSType->isRealFloatingType(); 1231 1232 // N1169 4.1.4: If one of the operands has a floating type and the other 1233 // operand has a fixed-point type, the fixed-point operand 1234 // is converted to the floating type [...] 1235 if (LHSType->isFixedPointType() || RHSType->isFixedPointType()) { 1236 if (LHSFloat) 1237 RHS = S.ImpCastExprToType(RHS.get(), LHSType, CK_FixedPointToFloating); 1238 else if (!IsCompAssign) 1239 LHS = S.ImpCastExprToType(LHS.get(), RHSType, CK_FixedPointToFloating); 1240 return LHSFloat ? LHSType : RHSType; 1241 } 1242 1243 // If we have two real floating types, convert the smaller operand 1244 // to the bigger result. 1245 if (LHSFloat && RHSFloat) { 1246 int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); 1247 if (order > 0) { 1248 RHS = S.ImpCastExprToType(RHS.get(), LHSType, CK_FloatingCast); 1249 return LHSType; 1250 } 1251 1252 assert(order < 0 && "illegal float comparison"); 1253 if (!IsCompAssign) 1254 LHS = S.ImpCastExprToType(LHS.get(), RHSType, CK_FloatingCast); 1255 return RHSType; 1256 } 1257 1258 if (LHSFloat) { 1259 // Half FP has to be promoted to float unless it is natively supported 1260 if (LHSType->isHalfType() && !S.getLangOpts().NativeHalfType) 1261 LHSType = S.Context.FloatTy; 1262 1263 return handleIntToFloatConversion(S, LHS, RHS, LHSType, RHSType, 1264 /*ConvertFloat=*/!IsCompAssign, 1265 /*ConvertInt=*/ true); 1266 } 1267 assert(RHSFloat); 1268 return handleIntToFloatConversion(S, RHS, LHS, RHSType, LHSType, 1269 /*ConvertFloat=*/ true, 1270 /*ConvertInt=*/!IsCompAssign); 1271 } 1272 1273 /// Diagnose attempts to convert between __float128, __ibm128 and 1274 /// long double if there is no support for such conversion. 1275 /// Helper function of UsualArithmeticConversions(). 1276 static bool unsupportedTypeConversion(const Sema &S, QualType LHSType, 1277 QualType RHSType) { 1278 // No issue if either is not a floating point type. 1279 if (!LHSType->isFloatingType() || !RHSType->isFloatingType()) 1280 return false; 1281 1282 // No issue if both have the same 128-bit float semantics. 1283 auto *LHSComplex = LHSType->getAs<ComplexType>(); 1284 auto *RHSComplex = RHSType->getAs<ComplexType>(); 1285 1286 QualType LHSElem = LHSComplex ? LHSComplex->getElementType() : LHSType; 1287 QualType RHSElem = RHSComplex ? RHSComplex->getElementType() : RHSType; 1288 1289 const llvm::fltSemantics &LHSSem = S.Context.getFloatTypeSemantics(LHSElem); 1290 const llvm::fltSemantics &RHSSem = S.Context.getFloatTypeSemantics(RHSElem); 1291 1292 if ((&LHSSem != &llvm::APFloat::PPCDoubleDouble() || 1293 &RHSSem != &llvm::APFloat::IEEEquad()) && 1294 (&LHSSem != &llvm::APFloat::IEEEquad() || 1295 &RHSSem != &llvm::APFloat::PPCDoubleDouble())) 1296 return false; 1297 1298 return true; 1299 } 1300 1301 typedef ExprResult PerformCastFn(Sema &S, Expr *operand, QualType toType); 1302 1303 namespace { 1304 /// These helper callbacks are placed in an anonymous namespace to 1305 /// permit their use as function template parameters. 1306 ExprResult doIntegralCast(Sema &S, Expr *op, QualType toType) { 1307 return S.ImpCastExprToType(op, toType, CK_IntegralCast); 1308 } 1309 1310 ExprResult doComplexIntegralCast(Sema &S, Expr *op, QualType toType) { 1311 return S.ImpCastExprToType(op, S.Context.getComplexType(toType), 1312 CK_IntegralComplexCast); 1313 } 1314 } 1315 1316 /// Handle integer arithmetic conversions. Helper function of 1317 /// UsualArithmeticConversions() 1318 template <PerformCastFn doLHSCast, PerformCastFn doRHSCast> 1319 static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, 1320 ExprResult &RHS, QualType LHSType, 1321 QualType RHSType, bool IsCompAssign) { 1322 // The rules for this case are in C99 6.3.1.8 1323 int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); 1324 bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); 1325 bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); 1326 if (LHSSigned == RHSSigned) { 1327 // Same signedness; use the higher-ranked type 1328 if (order >= 0) { 1329 RHS = (*doRHSCast)(S, RHS.get(), LHSType); 1330 return LHSType; 1331 } else if (!IsCompAssign) 1332 LHS = (*doLHSCast)(S, LHS.get(), RHSType); 1333 return RHSType; 1334 } else if (order != (LHSSigned ? 1 : -1)) { 1335 // The unsigned type has greater than or equal rank to the 1336 // signed type, so use the unsigned type 1337 if (RHSSigned) { 1338 RHS = (*doRHSCast)(S, RHS.get(), LHSType); 1339 return LHSType; 1340 } else if (!IsCompAssign) 1341 LHS = (*doLHSCast)(S, LHS.get(), RHSType); 1342 return RHSType; 1343 } else if (S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)) { 1344 // The two types are different widths; if we are here, that 1345 // means the signed type is larger than the unsigned type, so 1346 // use the signed type. 1347 if (LHSSigned) { 1348 RHS = (*doRHSCast)(S, RHS.get(), LHSType); 1349 return LHSType; 1350 } else if (!IsCompAssign) 1351 LHS = (*doLHSCast)(S, LHS.get(), RHSType); 1352 return RHSType; 1353 } else { 1354 // The signed type is higher-ranked than the unsigned type, 1355 // but isn't actually any bigger (like unsigned int and long 1356 // on most 32-bit systems). Use the unsigned type corresponding 1357 // to the signed type. 1358 QualType result = 1359 S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); 1360 RHS = (*doRHSCast)(S, RHS.get(), result); 1361 if (!IsCompAssign) 1362 LHS = (*doLHSCast)(S, LHS.get(), result); 1363 return result; 1364 } 1365 } 1366 1367 /// Handle conversions with GCC complex int extension. Helper function 1368 /// of UsualArithmeticConversions() 1369 static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, 1370 ExprResult &RHS, QualType LHSType, 1371 QualType RHSType, 1372 bool IsCompAssign) { 1373 const ComplexType *LHSComplexInt = LHSType->getAsComplexIntegerType(); 1374 const ComplexType *RHSComplexInt = RHSType->getAsComplexIntegerType(); 1375 1376 if (LHSComplexInt && RHSComplexInt) { 1377 QualType LHSEltType = LHSComplexInt->getElementType(); 1378 QualType RHSEltType = RHSComplexInt->getElementType(); 1379 QualType ScalarType = 1380 handleIntegerConversion<doComplexIntegralCast, doComplexIntegralCast> 1381 (S, LHS, RHS, LHSEltType, RHSEltType, IsCompAssign); 1382 1383 return S.Context.getComplexType(ScalarType); 1384 } 1385 1386 if (LHSComplexInt) { 1387 QualType LHSEltType = LHSComplexInt->getElementType(); 1388 QualType ScalarType = 1389 handleIntegerConversion<doComplexIntegralCast, doIntegralCast> 1390 (S, LHS, RHS, LHSEltType, RHSType, IsCompAssign); 1391 QualType ComplexType = S.Context.getComplexType(ScalarType); 1392 RHS = S.ImpCastExprToType(RHS.get(), ComplexType, 1393 CK_IntegralRealToComplex); 1394 1395 return ComplexType; 1396 } 1397 1398 assert(RHSComplexInt); 1399 1400 QualType RHSEltType = RHSComplexInt->getElementType(); 1401 QualType ScalarType = 1402 handleIntegerConversion<doIntegralCast, doComplexIntegralCast> 1403 (S, LHS, RHS, LHSType, RHSEltType, IsCompAssign); 1404 QualType ComplexType = S.Context.getComplexType(ScalarType); 1405 1406 if (!IsCompAssign) 1407 LHS = S.ImpCastExprToType(LHS.get(), ComplexType, 1408 CK_IntegralRealToComplex); 1409 return ComplexType; 1410 } 1411 1412 /// Return the rank of a given fixed point or integer type. The value itself 1413 /// doesn't matter, but the values must be increasing with proper increasing 1414 /// rank as described in N1169 4.1.1. 1415 static unsigned GetFixedPointRank(QualType Ty) { 1416 const auto *BTy = Ty->getAs<BuiltinType>(); 1417 assert(BTy && "Expected a builtin type."); 1418 1419 switch (BTy->getKind()) { 1420 case BuiltinType::ShortFract: 1421 case BuiltinType::UShortFract: 1422 case BuiltinType::SatShortFract: 1423 case BuiltinType::SatUShortFract: 1424 return 1; 1425 case BuiltinType::Fract: 1426 case BuiltinType::UFract: 1427 case BuiltinType::SatFract: 1428 case BuiltinType::SatUFract: 1429 return 2; 1430 case BuiltinType::LongFract: 1431 case BuiltinType::ULongFract: 1432 case BuiltinType::SatLongFract: 1433 case BuiltinType::SatULongFract: 1434 return 3; 1435 case BuiltinType::ShortAccum: 1436 case BuiltinType::UShortAccum: 1437 case BuiltinType::SatShortAccum: 1438 case BuiltinType::SatUShortAccum: 1439 return 4; 1440 case BuiltinType::Accum: 1441 case BuiltinType::UAccum: 1442 case BuiltinType::SatAccum: 1443 case BuiltinType::SatUAccum: 1444 return 5; 1445 case BuiltinType::LongAccum: 1446 case BuiltinType::ULongAccum: 1447 case BuiltinType::SatLongAccum: 1448 case BuiltinType::SatULongAccum: 1449 return 6; 1450 default: 1451 if (BTy->isInteger()) 1452 return 0; 1453 llvm_unreachable("Unexpected fixed point or integer type"); 1454 } 1455 } 1456 1457 /// handleFixedPointConversion - Fixed point operations between fixed 1458 /// point types and integers or other fixed point types do not fall under 1459 /// usual arithmetic conversion since these conversions could result in loss 1460 /// of precsision (N1169 4.1.4). These operations should be calculated with 1461 /// the full precision of their result type (N1169 4.1.6.2.1). 1462 static QualType handleFixedPointConversion(Sema &S, QualType LHSTy, 1463 QualType RHSTy) { 1464 assert((LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) && 1465 "Expected at least one of the operands to be a fixed point type"); 1466 assert((LHSTy->isFixedPointOrIntegerType() || 1467 RHSTy->isFixedPointOrIntegerType()) && 1468 "Special fixed point arithmetic operation conversions are only " 1469 "applied to ints or other fixed point types"); 1470 1471 // If one operand has signed fixed-point type and the other operand has 1472 // unsigned fixed-point type, then the unsigned fixed-point operand is 1473 // converted to its corresponding signed fixed-point type and the resulting 1474 // type is the type of the converted operand. 1475 if (RHSTy->isSignedFixedPointType() && LHSTy->isUnsignedFixedPointType()) 1476 LHSTy = S.Context.getCorrespondingSignedFixedPointType(LHSTy); 1477 else if (RHSTy->isUnsignedFixedPointType() && LHSTy->isSignedFixedPointType()) 1478 RHSTy = S.Context.getCorrespondingSignedFixedPointType(RHSTy); 1479 1480 // The result type is the type with the highest rank, whereby a fixed-point 1481 // conversion rank is always greater than an integer conversion rank; if the 1482 // type of either of the operands is a saturating fixedpoint type, the result 1483 // type shall be the saturating fixed-point type corresponding to the type 1484 // with the highest rank; the resulting value is converted (taking into 1485 // account rounding and overflow) to the precision of the resulting type. 1486 // Same ranks between signed and unsigned types are resolved earlier, so both 1487 // types are either signed or both unsigned at this point. 1488 unsigned LHSTyRank = GetFixedPointRank(LHSTy); 1489 unsigned RHSTyRank = GetFixedPointRank(RHSTy); 1490 1491 QualType ResultTy = LHSTyRank > RHSTyRank ? LHSTy : RHSTy; 1492 1493 if (LHSTy->isSaturatedFixedPointType() || RHSTy->isSaturatedFixedPointType()) 1494 ResultTy = S.Context.getCorrespondingSaturatedType(ResultTy); 1495 1496 return ResultTy; 1497 } 1498 1499 /// Check that the usual arithmetic conversions can be performed on this pair of 1500 /// expressions that might be of enumeration type. 1501 void Sema::checkEnumArithmeticConversions(Expr *LHS, Expr *RHS, 1502 SourceLocation Loc, 1503 ArithConvKind ACK) { 1504 // C++2a [expr.arith.conv]p1: 1505 // If one operand is of enumeration type and the other operand is of a 1506 // different enumeration type or a floating-point type, this behavior is 1507 // deprecated ([depr.arith.conv.enum]). 1508 // 1509 // Warn on this in all language modes. Produce a deprecation warning in C++20. 1510 // Eventually we will presumably reject these cases (in C++23 onwards?). 1511 QualType L = LHS->getEnumCoercedType(Context), 1512 R = RHS->getEnumCoercedType(Context); 1513 bool LEnum = L->isUnscopedEnumerationType(), 1514 REnum = R->isUnscopedEnumerationType(); 1515 bool IsCompAssign = ACK == ArithConvKind::CompAssign; 1516 if ((!IsCompAssign && LEnum && R->isFloatingType()) || 1517 (REnum && L->isFloatingType())) { 1518 Diag(Loc, getLangOpts().CPlusPlus26 ? diag::err_arith_conv_enum_float_cxx26 1519 : getLangOpts().CPlusPlus20 1520 ? diag::warn_arith_conv_enum_float_cxx20 1521 : diag::warn_arith_conv_enum_float) 1522 << LHS->getSourceRange() << RHS->getSourceRange() << (int)ACK << LEnum 1523 << L << R; 1524 } else if (!IsCompAssign && LEnum && REnum && 1525 !Context.hasSameUnqualifiedType(L, R)) { 1526 unsigned DiagID; 1527 // In C++ 26, usual arithmetic conversions between 2 different enum types 1528 // are ill-formed. 1529 if (getLangOpts().CPlusPlus26) 1530 DiagID = diag::warn_conv_mixed_enum_types_cxx26; 1531 else if (!L->castAs<EnumType>()->getDecl()->hasNameForLinkage() || 1532 !R->castAs<EnumType>()->getDecl()->hasNameForLinkage()) { 1533 // If either enumeration type is unnamed, it's less likely that the 1534 // user cares about this, but this situation is still deprecated in 1535 // C++2a. Use a different warning group. 1536 DiagID = getLangOpts().CPlusPlus20 1537 ? diag::warn_arith_conv_mixed_anon_enum_types_cxx20 1538 : diag::warn_arith_conv_mixed_anon_enum_types; 1539 } else if (ACK == ArithConvKind::Conditional) { 1540 // Conditional expressions are separated out because they have 1541 // historically had a different warning flag. 1542 DiagID = getLangOpts().CPlusPlus20 1543 ? diag::warn_conditional_mixed_enum_types_cxx20 1544 : diag::warn_conditional_mixed_enum_types; 1545 } else if (ACK == ArithConvKind::Comparison) { 1546 // Comparison expressions are separated out because they have 1547 // historically had a different warning flag. 1548 DiagID = getLangOpts().CPlusPlus20 1549 ? diag::warn_comparison_mixed_enum_types_cxx20 1550 : diag::warn_comparison_mixed_enum_types; 1551 } else { 1552 DiagID = getLangOpts().CPlusPlus20 1553 ? diag::warn_arith_conv_mixed_enum_types_cxx20 1554 : diag::warn_arith_conv_mixed_enum_types; 1555 } 1556 Diag(Loc, DiagID) << LHS->getSourceRange() << RHS->getSourceRange() 1557 << (int)ACK << L << R; 1558 } 1559 } 1560 1561 static void CheckUnicodeArithmeticConversions(Sema &SemaRef, Expr *LHS, 1562 Expr *RHS, SourceLocation Loc, 1563 ArithConvKind ACK) { 1564 QualType LHSType = LHS->getType().getUnqualifiedType(); 1565 QualType RHSType = RHS->getType().getUnqualifiedType(); 1566 1567 if (!SemaRef.getLangOpts().CPlusPlus || !LHSType->isUnicodeCharacterType() || 1568 !RHSType->isUnicodeCharacterType()) 1569 return; 1570 1571 if (ACK == ArithConvKind::Comparison) { 1572 if (SemaRef.getASTContext().hasSameType(LHSType, RHSType)) 1573 return; 1574 1575 auto IsSingleCodeUnitCP = [](const QualType &T, const llvm::APSInt &Value) { 1576 if (T->isChar8Type()) 1577 return llvm::IsSingleCodeUnitUTF8Codepoint(Value.getExtValue()); 1578 if (T->isChar16Type()) 1579 return llvm::IsSingleCodeUnitUTF16Codepoint(Value.getExtValue()); 1580 assert(T->isChar32Type()); 1581 return llvm::IsSingleCodeUnitUTF32Codepoint(Value.getExtValue()); 1582 }; 1583 1584 Expr::EvalResult LHSRes, RHSRes; 1585 bool LHSSuccess = LHS->EvaluateAsInt(LHSRes, SemaRef.getASTContext(), 1586 Expr::SE_AllowSideEffects, 1587 SemaRef.isConstantEvaluatedContext()); 1588 bool RHSuccess = RHS->EvaluateAsInt(RHSRes, SemaRef.getASTContext(), 1589 Expr::SE_AllowSideEffects, 1590 SemaRef.isConstantEvaluatedContext()); 1591 1592 // Don't warn if the one known value is a representable 1593 // in the type of both expressions. 1594 if (LHSSuccess != RHSuccess) { 1595 Expr::EvalResult &Res = LHSSuccess ? LHSRes : RHSRes; 1596 if (IsSingleCodeUnitCP(LHSType, Res.Val.getInt()) && 1597 IsSingleCodeUnitCP(RHSType, Res.Val.getInt())) 1598 return; 1599 } 1600 1601 if (!LHSSuccess || !RHSuccess) { 1602 SemaRef.Diag(Loc, diag::warn_comparison_unicode_mixed_types) 1603 << LHS->getSourceRange() << RHS->getSourceRange() << LHSType 1604 << RHSType; 1605 return; 1606 } 1607 1608 llvm::APSInt LHSValue(32); 1609 LHSValue = LHSRes.Val.getInt(); 1610 llvm::APSInt RHSValue(32); 1611 RHSValue = RHSRes.Val.getInt(); 1612 1613 bool LHSSafe = IsSingleCodeUnitCP(LHSType, LHSValue); 1614 bool RHSSafe = IsSingleCodeUnitCP(RHSType, RHSValue); 1615 if (LHSSafe && RHSSafe) 1616 return; 1617 1618 SemaRef.Diag(Loc, diag::warn_comparison_unicode_mixed_types_constant) 1619 << LHS->getSourceRange() << RHS->getSourceRange() << LHSType << RHSType 1620 << FormatUTFCodeUnitAsCodepoint(LHSValue.getExtValue(), LHSType) 1621 << FormatUTFCodeUnitAsCodepoint(RHSValue.getExtValue(), RHSType); 1622 return; 1623 } 1624 1625 if (SemaRef.getASTContext().hasSameType(LHSType, RHSType)) 1626 return; 1627 1628 SemaRef.Diag(Loc, diag::warn_arith_conv_mixed_unicode_types) 1629 << LHS->getSourceRange() << RHS->getSourceRange() << ACK << LHSType 1630 << RHSType; 1631 } 1632 1633 /// UsualArithmeticConversions - Performs various conversions that are common to 1634 /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this 1635 /// routine returns the first non-arithmetic type found. The client is 1636 /// responsible for emitting appropriate error diagnostics. 1637 QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, 1638 SourceLocation Loc, 1639 ArithConvKind ACK) { 1640 1641 checkEnumArithmeticConversions(LHS.get(), RHS.get(), Loc, ACK); 1642 1643 CheckUnicodeArithmeticConversions(*this, LHS.get(), RHS.get(), Loc, ACK); 1644 1645 if (ACK != ArithConvKind::CompAssign) { 1646 LHS = UsualUnaryConversions(LHS.get()); 1647 if (LHS.isInvalid()) 1648 return QualType(); 1649 } 1650 1651 RHS = UsualUnaryConversions(RHS.get()); 1652 if (RHS.isInvalid()) 1653 return QualType(); 1654 1655 // For conversion purposes, we ignore any qualifiers. 1656 // For example, "const float" and "float" are equivalent. 1657 QualType LHSType = LHS.get()->getType().getUnqualifiedType(); 1658 QualType RHSType = RHS.get()->getType().getUnqualifiedType(); 1659 1660 // For conversion purposes, we ignore any atomic qualifier on the LHS. 1661 if (const AtomicType *AtomicLHS = LHSType->getAs<AtomicType>()) 1662 LHSType = AtomicLHS->getValueType(); 1663 1664 // If both types are identical, no conversion is needed. 1665 if (Context.hasSameType(LHSType, RHSType)) 1666 return Context.getCommonSugaredType(LHSType, RHSType); 1667 1668 // If either side is a non-arithmetic type (e.g. a pointer), we are done. 1669 // The caller can deal with this (e.g. pointer + int). 1670 if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType()) 1671 return QualType(); 1672 1673 // Apply unary and bitfield promotions to the LHS's type. 1674 QualType LHSUnpromotedType = LHSType; 1675 if (Context.isPromotableIntegerType(LHSType)) 1676 LHSType = Context.getPromotedIntegerType(LHSType); 1677 QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(LHS.get()); 1678 if (!LHSBitfieldPromoteTy.isNull()) 1679 LHSType = LHSBitfieldPromoteTy; 1680 if (LHSType != LHSUnpromotedType && ACK != ArithConvKind::CompAssign) 1681 LHS = ImpCastExprToType(LHS.get(), LHSType, CK_IntegralCast); 1682 1683 // If both types are identical, no conversion is needed. 1684 if (Context.hasSameType(LHSType, RHSType)) 1685 return Context.getCommonSugaredType(LHSType, RHSType); 1686 1687 // At this point, we have two different arithmetic types. 1688 1689 // Diagnose attempts to convert between __ibm128, __float128 and long double 1690 // where such conversions currently can't be handled. 1691 if (unsupportedTypeConversion(*this, LHSType, RHSType)) 1692 return QualType(); 1693 1694 // Handle complex types first (C99 6.3.1.8p1). 1695 if (LHSType->isComplexType() || RHSType->isComplexType()) 1696 return handleComplexConversion(*this, LHS, RHS, LHSType, RHSType, 1697 ACK == ArithConvKind::CompAssign); 1698 1699 // Now handle "real" floating types (i.e. float, double, long double). 1700 if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) 1701 return handleFloatConversion(*this, LHS, RHS, LHSType, RHSType, 1702 ACK == ArithConvKind::CompAssign); 1703 1704 // Handle GCC complex int extension. 1705 if (LHSType->isComplexIntegerType() || RHSType->isComplexIntegerType()) 1706 return handleComplexIntConversion(*this, LHS, RHS, LHSType, RHSType, 1707 ACK == ArithConvKind::CompAssign); 1708 1709 if (LHSType->isFixedPointType() || RHSType->isFixedPointType()) 1710 return handleFixedPointConversion(*this, LHSType, RHSType); 1711 1712 // Finally, we have two differing integer types. 1713 return handleIntegerConversion<doIntegralCast, doIntegralCast>( 1714 *this, LHS, RHS, LHSType, RHSType, ACK == ArithConvKind::CompAssign); 1715 } 1716 1717 //===----------------------------------------------------------------------===// 1718 // Semantic Analysis for various Expression Types 1719 //===----------------------------------------------------------------------===// 1720 1721 1722 ExprResult Sema::ActOnGenericSelectionExpr( 1723 SourceLocation KeyLoc, SourceLocation DefaultLoc, SourceLocation RParenLoc, 1724 bool PredicateIsExpr, void *ControllingExprOrType, 1725 ArrayRef<ParsedType> ArgTypes, ArrayRef<Expr *> ArgExprs) { 1726 unsigned NumAssocs = ArgTypes.size(); 1727 assert(NumAssocs == ArgExprs.size()); 1728 1729 TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; 1730 for (unsigned i = 0; i < NumAssocs; ++i) { 1731 if (ArgTypes[i]) 1732 (void) GetTypeFromParser(ArgTypes[i], &Types[i]); 1733 else 1734 Types[i] = nullptr; 1735 } 1736 1737 // If we have a controlling type, we need to convert it from a parsed type 1738 // into a semantic type and then pass that along. 1739 if (!PredicateIsExpr) { 1740 TypeSourceInfo *ControllingType; 1741 (void)GetTypeFromParser(ParsedType::getFromOpaquePtr(ControllingExprOrType), 1742 &ControllingType); 1743 assert(ControllingType && "couldn't get the type out of the parser"); 1744 ControllingExprOrType = ControllingType; 1745 } 1746 1747 ExprResult ER = CreateGenericSelectionExpr( 1748 KeyLoc, DefaultLoc, RParenLoc, PredicateIsExpr, ControllingExprOrType, 1749 llvm::ArrayRef(Types, NumAssocs), ArgExprs); 1750 delete [] Types; 1751 return ER; 1752 } 1753 1754 ExprResult Sema::CreateGenericSelectionExpr( 1755 SourceLocation KeyLoc, SourceLocation DefaultLoc, SourceLocation RParenLoc, 1756 bool PredicateIsExpr, void *ControllingExprOrType, 1757 ArrayRef<TypeSourceInfo *> Types, ArrayRef<Expr *> Exprs) { 1758 unsigned NumAssocs = Types.size(); 1759 assert(NumAssocs == Exprs.size()); 1760 assert(ControllingExprOrType && 1761 "Must have either a controlling expression or a controlling type"); 1762 1763 Expr *ControllingExpr = nullptr; 1764 TypeSourceInfo *ControllingType = nullptr; 1765 if (PredicateIsExpr) { 1766 // Decay and strip qualifiers for the controlling expression type, and 1767 // handle placeholder type replacement. See committee discussion from WG14 1768 // DR423. 1769 EnterExpressionEvaluationContext Unevaluated( 1770 *this, Sema::ExpressionEvaluationContext::Unevaluated); 1771 ExprResult R = DefaultFunctionArrayLvalueConversion( 1772 reinterpret_cast<Expr *>(ControllingExprOrType)); 1773 if (R.isInvalid()) 1774 return ExprError(); 1775 ControllingExpr = R.get(); 1776 } else { 1777 // The extension form uses the type directly rather than converting it. 1778 ControllingType = reinterpret_cast<TypeSourceInfo *>(ControllingExprOrType); 1779 if (!ControllingType) 1780 return ExprError(); 1781 } 1782 1783 bool TypeErrorFound = false, 1784 IsResultDependent = ControllingExpr 1785 ? ControllingExpr->isTypeDependent() 1786 : ControllingType->getType()->isDependentType(), 1787 ContainsUnexpandedParameterPack = 1788 ControllingExpr 1789 ? ControllingExpr->containsUnexpandedParameterPack() 1790 : ControllingType->getType()->containsUnexpandedParameterPack(); 1791 1792 // The controlling expression is an unevaluated operand, so side effects are 1793 // likely unintended. 1794 if (!inTemplateInstantiation() && !IsResultDependent && ControllingExpr && 1795 ControllingExpr->HasSideEffects(Context, false)) 1796 Diag(ControllingExpr->getExprLoc(), 1797 diag::warn_side_effects_unevaluated_context); 1798 1799 for (unsigned i = 0; i < NumAssocs; ++i) { 1800 if (Exprs[i]->containsUnexpandedParameterPack()) 1801 ContainsUnexpandedParameterPack = true; 1802 1803 if (Types[i]) { 1804 if (Types[i]->getType()->containsUnexpandedParameterPack()) 1805 ContainsUnexpandedParameterPack = true; 1806 1807 if (Types[i]->getType()->isDependentType()) { 1808 IsResultDependent = true; 1809 } else { 1810 // We relax the restriction on use of incomplete types and non-object 1811 // types with the type-based extension of _Generic. Allowing incomplete 1812 // objects means those can be used as "tags" for a type-safe way to map 1813 // to a value. Similarly, matching on function types rather than 1814 // function pointer types can be useful. However, the restriction on VM 1815 // types makes sense to retain as there are open questions about how 1816 // the selection can be made at compile time. 1817 // 1818 // C11 6.5.1.1p2 "The type name in a generic association shall specify a 1819 // complete object type other than a variably modified type." 1820 // C2y removed the requirement that an expression form must 1821 // use a complete type, though it's still as-if the type has undergone 1822 // lvalue conversion. We support this as an extension in C23 and 1823 // earlier because GCC does so. 1824 unsigned D = 0; 1825 if (ControllingExpr && Types[i]->getType()->isIncompleteType()) 1826 D = LangOpts.C2y ? diag::warn_c2y_compat_assoc_type_incomplete 1827 : diag::ext_assoc_type_incomplete; 1828 else if (ControllingExpr && !Types[i]->getType()->isObjectType()) 1829 D = diag::err_assoc_type_nonobject; 1830 else if (Types[i]->getType()->isVariablyModifiedType()) 1831 D = diag::err_assoc_type_variably_modified; 1832 else if (ControllingExpr) { 1833 // Because the controlling expression undergoes lvalue conversion, 1834 // array conversion, and function conversion, an association which is 1835 // of array type, function type, or is qualified can never be 1836 // reached. We will warn about this so users are less surprised by 1837 // the unreachable association. However, we don't have to handle 1838 // function types; that's not an object type, so it's handled above. 1839 // 1840 // The logic is somewhat different for C++ because C++ has different 1841 // lvalue to rvalue conversion rules than C. [conv.lvalue]p1 says, 1842 // If T is a non-class type, the type of the prvalue is the cv- 1843 // unqualified version of T. Otherwise, the type of the prvalue is T. 1844 // The result of these rules is that all qualified types in an 1845 // association in C are unreachable, and in C++, only qualified non- 1846 // class types are unreachable. 1847 // 1848 // NB: this does not apply when the first operand is a type rather 1849 // than an expression, because the type form does not undergo 1850 // conversion. 1851 unsigned Reason = 0; 1852 QualType QT = Types[i]->getType(); 1853 if (QT->isArrayType()) 1854 Reason = 1; 1855 else if (QT.hasQualifiers() && 1856 (!LangOpts.CPlusPlus || !QT->isRecordType())) 1857 Reason = 2; 1858 1859 if (Reason) 1860 Diag(Types[i]->getTypeLoc().getBeginLoc(), 1861 diag::warn_unreachable_association) 1862 << QT << (Reason - 1); 1863 } 1864 1865 if (D != 0) { 1866 Diag(Types[i]->getTypeLoc().getBeginLoc(), D) 1867 << Types[i]->getTypeLoc().getSourceRange() << Types[i]->getType(); 1868 if (getDiagnostics().getDiagnosticLevel( 1869 D, Types[i]->getTypeLoc().getBeginLoc()) >= 1870 DiagnosticsEngine::Error) 1871 TypeErrorFound = true; 1872 } 1873 1874 // C11 6.5.1.1p2 "No two generic associations in the same generic 1875 // selection shall specify compatible types." 1876 for (unsigned j = i+1; j < NumAssocs; ++j) 1877 if (Types[j] && !Types[j]->getType()->isDependentType() && 1878 Context.typesAreCompatible(Types[i]->getType(), 1879 Types[j]->getType())) { 1880 Diag(Types[j]->getTypeLoc().getBeginLoc(), 1881 diag::err_assoc_compatible_types) 1882 << Types[j]->getTypeLoc().getSourceRange() 1883 << Types[j]->getType() 1884 << Types[i]->getType(); 1885 Diag(Types[i]->getTypeLoc().getBeginLoc(), 1886 diag::note_compat_assoc) 1887 << Types[i]->getTypeLoc().getSourceRange() 1888 << Types[i]->getType(); 1889 TypeErrorFound = true; 1890 } 1891 } 1892 } 1893 } 1894 if (TypeErrorFound) 1895 return ExprError(); 1896 1897 // If we determined that the generic selection is result-dependent, don't 1898 // try to compute the result expression. 1899 if (IsResultDependent) { 1900 if (ControllingExpr) 1901 return GenericSelectionExpr::Create(Context, KeyLoc, ControllingExpr, 1902 Types, Exprs, DefaultLoc, RParenLoc, 1903 ContainsUnexpandedParameterPack); 1904 return GenericSelectionExpr::Create(Context, KeyLoc, ControllingType, Types, 1905 Exprs, DefaultLoc, RParenLoc, 1906 ContainsUnexpandedParameterPack); 1907 } 1908 1909 SmallVector<unsigned, 1> CompatIndices; 1910 unsigned DefaultIndex = std::numeric_limits<unsigned>::max(); 1911 // Look at the canonical type of the controlling expression in case it was a 1912 // deduced type like __auto_type. However, when issuing diagnostics, use the 1913 // type the user wrote in source rather than the canonical one. 1914 for (unsigned i = 0; i < NumAssocs; ++i) { 1915 if (!Types[i]) 1916 DefaultIndex = i; 1917 else if (ControllingExpr && 1918 Context.typesAreCompatible( 1919 ControllingExpr->getType().getCanonicalType(), 1920 Types[i]->getType())) 1921 CompatIndices.push_back(i); 1922 else if (ControllingType && 1923 Context.typesAreCompatible( 1924 ControllingType->getType().getCanonicalType(), 1925 Types[i]->getType())) 1926 CompatIndices.push_back(i); 1927 } 1928 1929 auto GetControllingRangeAndType = [](Expr *ControllingExpr, 1930 TypeSourceInfo *ControllingType) { 1931 // We strip parens here because the controlling expression is typically 1932 // parenthesized in macro definitions. 1933 if (ControllingExpr) 1934 ControllingExpr = ControllingExpr->IgnoreParens(); 1935 1936 SourceRange SR = ControllingExpr 1937 ? ControllingExpr->getSourceRange() 1938 : ControllingType->getTypeLoc().getSourceRange(); 1939 QualType QT = ControllingExpr ? ControllingExpr->getType() 1940 : ControllingType->getType(); 1941 1942 return std::make_pair(SR, QT); 1943 }; 1944 1945 // C11 6.5.1.1p2 "The controlling expression of a generic selection shall have 1946 // type compatible with at most one of the types named in its generic 1947 // association list." 1948 if (CompatIndices.size() > 1) { 1949 auto P = GetControllingRangeAndType(ControllingExpr, ControllingType); 1950 SourceRange SR = P.first; 1951 Diag(SR.getBegin(), diag::err_generic_sel_multi_match) 1952 << SR << P.second << (unsigned)CompatIndices.size(); 1953 for (unsigned I : CompatIndices) { 1954 Diag(Types[I]->getTypeLoc().getBeginLoc(), 1955 diag::note_compat_assoc) 1956 << Types[I]->getTypeLoc().getSourceRange() 1957 << Types[I]->getType(); 1958 } 1959 return ExprError(); 1960 } 1961 1962 // C11 6.5.1.1p2 "If a generic selection has no default generic association, 1963 // its controlling expression shall have type compatible with exactly one of 1964 // the types named in its generic association list." 1965 if (DefaultIndex == std::numeric_limits<unsigned>::max() && 1966 CompatIndices.size() == 0) { 1967 auto P = GetControllingRangeAndType(ControllingExpr, ControllingType); 1968 SourceRange SR = P.first; 1969 Diag(SR.getBegin(), diag::err_generic_sel_no_match) << SR << P.second; 1970 return ExprError(); 1971 } 1972 1973 // C11 6.5.1.1p3 "If a generic selection has a generic association with a 1974 // type name that is compatible with the type of the controlling expression, 1975 // then the result expression of the generic selection is the expression 1976 // in that generic association. Otherwise, the result expression of the 1977 // generic selection is the expression in the default generic association." 1978 unsigned ResultIndex = 1979 CompatIndices.size() ? CompatIndices[0] : DefaultIndex; 1980 1981 if (ControllingExpr) { 1982 return GenericSelectionExpr::Create( 1983 Context, KeyLoc, ControllingExpr, Types, Exprs, DefaultLoc, RParenLoc, 1984 ContainsUnexpandedParameterPack, ResultIndex); 1985 } 1986 return GenericSelectionExpr::Create( 1987 Context, KeyLoc, ControllingType, Types, Exprs, DefaultLoc, RParenLoc, 1988 ContainsUnexpandedParameterPack, ResultIndex); 1989 } 1990 1991 static PredefinedIdentKind getPredefinedExprKind(tok::TokenKind Kind) { 1992 switch (Kind) { 1993 default: 1994 llvm_unreachable("unexpected TokenKind"); 1995 case tok::kw___func__: 1996 return PredefinedIdentKind::Func; // [C99 6.4.2.2] 1997 case tok::kw___FUNCTION__: 1998 return PredefinedIdentKind::Function; 1999 case tok::kw___FUNCDNAME__: 2000 return PredefinedIdentKind::FuncDName; // [MS] 2001 case tok::kw___FUNCSIG__: 2002 return PredefinedIdentKind::FuncSig; // [MS] 2003 case tok::kw_L__FUNCTION__: 2004 return PredefinedIdentKind::LFunction; // [MS] 2005 case tok::kw_L__FUNCSIG__: 2006 return PredefinedIdentKind::LFuncSig; // [MS] 2007 case tok::kw___PRETTY_FUNCTION__: 2008 return PredefinedIdentKind::PrettyFunction; // [GNU] 2009 } 2010 } 2011 2012 /// getPredefinedExprDecl - Returns Decl of a given DeclContext that can be used 2013 /// to determine the value of a PredefinedExpr. This can be either a 2014 /// block, lambda, captured statement, function, otherwise a nullptr. 2015 static Decl *getPredefinedExprDecl(DeclContext *DC) { 2016 while (DC && !isa<BlockDecl, CapturedDecl, FunctionDecl, ObjCMethodDecl>(DC)) 2017 DC = DC->getParent(); 2018 return cast_or_null<Decl>(DC); 2019 } 2020 2021 /// getUDSuffixLoc - Create a SourceLocation for a ud-suffix, given the 2022 /// location of the token and the offset of the ud-suffix within it. 2023 static SourceLocation getUDSuffixLoc(Sema &S, SourceLocation TokLoc, 2024 unsigned Offset) { 2025 return Lexer::AdvanceToTokenCharacter(TokLoc, Offset, S.getSourceManager(), 2026 S.getLangOpts()); 2027 } 2028 2029 /// BuildCookedLiteralOperatorCall - A user-defined literal was found. Look up 2030 /// the corresponding cooked (non-raw) literal operator, and build a call to it. 2031 static ExprResult BuildCookedLiteralOperatorCall(Sema &S, Scope *Scope, 2032 IdentifierInfo *UDSuffix, 2033 SourceLocation UDSuffixLoc, 2034 ArrayRef<Expr*> Args, 2035 SourceLocation LitEndLoc) { 2036 assert(Args.size() <= 2 && "too many arguments for literal operator"); 2037 2038 QualType ArgTy[2]; 2039 for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { 2040 ArgTy[ArgIdx] = Args[ArgIdx]->getType(); 2041 if (ArgTy[ArgIdx]->isArrayType()) 2042 ArgTy[ArgIdx] = S.Context.getArrayDecayedType(ArgTy[ArgIdx]); 2043 } 2044 2045 DeclarationName OpName = 2046 S.Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); 2047 DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); 2048 OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); 2049 2050 LookupResult R(S, OpName, UDSuffixLoc, Sema::LookupOrdinaryName); 2051 if (S.LookupLiteralOperator(Scope, R, llvm::ArrayRef(ArgTy, Args.size()), 2052 /*AllowRaw*/ false, /*AllowTemplate*/ false, 2053 /*AllowStringTemplatePack*/ false, 2054 /*DiagnoseMissing*/ true) == Sema::LOLR_Error) 2055 return ExprError(); 2056 2057 return S.BuildLiteralOperatorCall(R, OpNameInfo, Args, LitEndLoc); 2058 } 2059 2060 ExprResult Sema::ActOnUnevaluatedStringLiteral(ArrayRef<Token> StringToks) { 2061 // StringToks needs backing storage as it doesn't hold array elements itself 2062 std::vector<Token> ExpandedToks; 2063 if (getLangOpts().MicrosoftExt) 2064 StringToks = ExpandedToks = ExpandFunctionLocalPredefinedMacros(StringToks); 2065 2066 StringLiteralParser Literal(StringToks, PP, 2067 StringLiteralEvalMethod::Unevaluated); 2068 if (Literal.hadError) 2069 return ExprError(); 2070 2071 SmallVector<SourceLocation, 4> StringTokLocs; 2072 for (const Token &Tok : StringToks) 2073 StringTokLocs.push_back(Tok.getLocation()); 2074 2075 StringLiteral *Lit = StringLiteral::Create(Context, Literal.GetString(), 2076 StringLiteralKind::Unevaluated, 2077 false, {}, StringTokLocs); 2078 2079 if (!Literal.getUDSuffix().empty()) { 2080 SourceLocation UDSuffixLoc = 2081 getUDSuffixLoc(*this, StringTokLocs[Literal.getUDSuffixToken()], 2082 Literal.getUDSuffixOffset()); 2083 return ExprError(Diag(UDSuffixLoc, diag::err_invalid_string_udl)); 2084 } 2085 2086 return Lit; 2087 } 2088 2089 std::vector<Token> 2090 Sema::ExpandFunctionLocalPredefinedMacros(ArrayRef<Token> Toks) { 2091 // MSVC treats some predefined identifiers (e.g. __FUNCTION__) as function 2092 // local macros that expand to string literals that may be concatenated. 2093 // These macros are expanded here (in Sema), because StringLiteralParser 2094 // (in Lex) doesn't know the enclosing function (because it hasn't been 2095 // parsed yet). 2096 assert(getLangOpts().MicrosoftExt); 2097 2098 // Note: Although function local macros are defined only inside functions, 2099 // we ensure a valid `CurrentDecl` even outside of a function. This allows 2100 // expansion of macros into empty string literals without additional checks. 2101 Decl *CurrentDecl = getPredefinedExprDecl(CurContext); 2102 if (!CurrentDecl) 2103 CurrentDecl = Context.getTranslationUnitDecl(); 2104 2105 std::vector<Token> ExpandedToks; 2106 ExpandedToks.reserve(Toks.size()); 2107 for (const Token &Tok : Toks) { 2108 if (!isFunctionLocalStringLiteralMacro(Tok.getKind(), getLangOpts())) { 2109 assert(tok::isStringLiteral(Tok.getKind())); 2110 ExpandedToks.emplace_back(Tok); 2111 continue; 2112 } 2113 if (isa<TranslationUnitDecl>(CurrentDecl)) 2114 Diag(Tok.getLocation(), diag::ext_predef_outside_function); 2115 // Stringify predefined expression 2116 Diag(Tok.getLocation(), diag::ext_string_literal_from_predefined) 2117 << Tok.getKind(); 2118 SmallString<64> Str; 2119 llvm::raw_svector_ostream OS(Str); 2120 Token &Exp = ExpandedToks.emplace_back(); 2121 Exp.startToken(); 2122 if (Tok.getKind() == tok::kw_L__FUNCTION__ || 2123 Tok.getKind() == tok::kw_L__FUNCSIG__) { 2124 OS << 'L'; 2125 Exp.setKind(tok::wide_string_literal); 2126 } else { 2127 Exp.setKind(tok::string_literal); 2128 } 2129 OS << '"' 2130 << Lexer::Stringify(PredefinedExpr::ComputeName( 2131 getPredefinedExprKind(Tok.getKind()), CurrentDecl)) 2132 << '"'; 2133 PP.CreateString(OS.str(), Exp, Tok.getLocation(), Tok.getEndLoc()); 2134 } 2135 return ExpandedToks; 2136 } 2137 2138 ExprResult 2139 Sema::ActOnStringLiteral(ArrayRef<Token> StringToks, Scope *UDLScope) { 2140 assert(!StringToks.empty() && "Must have at least one string!"); 2141 2142 // StringToks needs backing storage as it doesn't hold array elements itself 2143 std::vector<Token> ExpandedToks; 2144 if (getLangOpts().MicrosoftExt) 2145 StringToks = ExpandedToks = ExpandFunctionLocalPredefinedMacros(StringToks); 2146 2147 StringLiteralParser Literal(StringToks, PP); 2148 if (Literal.hadError) 2149 return ExprError(); 2150 2151 SmallVector<SourceLocation, 4> StringTokLocs; 2152 for (const Token &Tok : StringToks) 2153 StringTokLocs.push_back(Tok.getLocation()); 2154 2155 QualType CharTy = Context.CharTy; 2156 StringLiteralKind Kind = StringLiteralKind::Ordinary; 2157 if (Literal.isWide()) { 2158 CharTy = Context.getWideCharType(); 2159 Kind = StringLiteralKind::Wide; 2160 } else if (Literal.isUTF8()) { 2161 if (getLangOpts().Char8) 2162 CharTy = Context.Char8Ty; 2163 else if (getLangOpts().C23) 2164 CharTy = Context.UnsignedCharTy; 2165 Kind = StringLiteralKind::UTF8; 2166 } else if (Literal.isUTF16()) { 2167 CharTy = Context.Char16Ty; 2168 Kind = StringLiteralKind::UTF16; 2169 } else if (Literal.isUTF32()) { 2170 CharTy = Context.Char32Ty; 2171 Kind = StringLiteralKind::UTF32; 2172 } else if (Literal.isPascal()) { 2173 CharTy = Context.UnsignedCharTy; 2174 } 2175 2176 // Warn on u8 string literals before C++20 and C23, whose type 2177 // was an array of char before but becomes an array of char8_t. 2178 // In C++20, it cannot be used where a pointer to char is expected. 2179 // In C23, it might have an unexpected value if char was signed. 2180 if (Kind == StringLiteralKind::UTF8 && 2181 (getLangOpts().CPlusPlus 2182 ? !getLangOpts().CPlusPlus20 && !getLangOpts().Char8 2183 : !getLangOpts().C23)) { 2184 Diag(StringTokLocs.front(), getLangOpts().CPlusPlus 2185 ? diag::warn_cxx20_compat_utf8_string 2186 : diag::warn_c23_compat_utf8_string); 2187 2188 // Create removals for all 'u8' prefixes in the string literal(s). This 2189 // ensures C++20/C23 compatibility (but may change the program behavior when 2190 // built by non-Clang compilers for which the execution character set is 2191 // not always UTF-8). 2192 auto RemovalDiag = PDiag(diag::note_cxx20_c23_compat_utf8_string_remove_u8); 2193 SourceLocation RemovalDiagLoc; 2194 for (const Token &Tok : StringToks) { 2195 if (Tok.getKind() == tok::utf8_string_literal) { 2196 if (RemovalDiagLoc.isInvalid()) 2197 RemovalDiagLoc = Tok.getLocation(); 2198 RemovalDiag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( 2199 Tok.getLocation(), 2200 Lexer::AdvanceToTokenCharacter(Tok.getLocation(), 2, 2201 getSourceManager(), getLangOpts()))); 2202 } 2203 } 2204 Diag(RemovalDiagLoc, RemovalDiag); 2205 } 2206 2207 QualType StrTy = 2208 Context.getStringLiteralArrayType(CharTy, Literal.GetNumStringChars()); 2209 2210 // Pass &StringTokLocs[0], StringTokLocs.size() to factory! 2211 StringLiteral *Lit = StringLiteral::Create( 2212 Context, Literal.GetString(), Kind, Literal.Pascal, StrTy, StringTokLocs); 2213 if (Literal.getUDSuffix().empty()) 2214 return Lit; 2215 2216 // We're building a user-defined literal. 2217 IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); 2218 SourceLocation UDSuffixLoc = 2219 getUDSuffixLoc(*this, StringTokLocs[Literal.getUDSuffixToken()], 2220 Literal.getUDSuffixOffset()); 2221 2222 // Make sure we're allowed user-defined literals here. 2223 if (!UDLScope) 2224 return ExprError(Diag(UDSuffixLoc, diag::err_invalid_string_udl)); 2225 2226 // C++11 [lex.ext]p5: The literal L is treated as a call of the form 2227 // operator "" X (str, len) 2228 QualType SizeType = Context.getSizeType(); 2229 2230 DeclarationName OpName = 2231 Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); 2232 DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); 2233 OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); 2234 2235 QualType ArgTy[] = { 2236 Context.getArrayDecayedType(StrTy), SizeType 2237 }; 2238 2239 LookupResult R(*this, OpName, UDSuffixLoc, LookupOrdinaryName); 2240 switch (LookupLiteralOperator(UDLScope, R, ArgTy, 2241 /*AllowRaw*/ false, /*AllowTemplate*/ true, 2242 /*AllowStringTemplatePack*/ true, 2243 /*DiagnoseMissing*/ true, Lit)) { 2244 2245 case LOLR_Cooked: { 2246 llvm::APInt Len(Context.getIntWidth(SizeType), Literal.GetNumStringChars()); 2247 IntegerLiteral *LenArg = IntegerLiteral::Create(Context, Len, SizeType, 2248 StringTokLocs[0]); 2249 Expr *Args[] = { Lit, LenArg }; 2250 2251 return BuildLiteralOperatorCall(R, OpNameInfo, Args, StringTokLocs.back()); 2252 } 2253 2254 case LOLR_Template: { 2255 TemplateArgumentListInfo ExplicitArgs; 2256 TemplateArgument Arg(Lit, /*IsCanonical=*/false); 2257 TemplateArgumentLocInfo ArgInfo(Lit); 2258 ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); 2259 return BuildLiteralOperatorCall(R, OpNameInfo, {}, StringTokLocs.back(), 2260 &ExplicitArgs); 2261 } 2262 2263 case LOLR_StringTemplatePack: { 2264 TemplateArgumentListInfo ExplicitArgs; 2265 2266 unsigned CharBits = Context.getIntWidth(CharTy); 2267 bool CharIsUnsigned = CharTy->isUnsignedIntegerType(); 2268 llvm::APSInt Value(CharBits, CharIsUnsigned); 2269 2270 TemplateArgument TypeArg(CharTy); 2271 TemplateArgumentLocInfo TypeArgInfo(Context.getTrivialTypeSourceInfo(CharTy)); 2272 ExplicitArgs.addArgument(TemplateArgumentLoc(TypeArg, TypeArgInfo)); 2273 2274 for (unsigned I = 0, N = Lit->getLength(); I != N; ++I) { 2275 Value = Lit->getCodeUnit(I); 2276 TemplateArgument Arg(Context, Value, CharTy); 2277 TemplateArgumentLocInfo ArgInfo; 2278 ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); 2279 } 2280 return BuildLiteralOperatorCall(R, OpNameInfo, {}, StringTokLocs.back(), 2281 &ExplicitArgs); 2282 } 2283 case LOLR_Raw: 2284 case LOLR_ErrorNoDiagnostic: 2285 llvm_unreachable("unexpected literal operator lookup result"); 2286 case LOLR_Error: 2287 return ExprError(); 2288 } 2289 llvm_unreachable("unexpected literal operator lookup result"); 2290 } 2291 2292 DeclRefExpr * 2293 Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, 2294 SourceLocation Loc, 2295 const CXXScopeSpec *SS) { 2296 DeclarationNameInfo NameInfo(D->getDeclName(), Loc); 2297 return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); 2298 } 2299 2300 DeclRefExpr * 2301 Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, 2302 const DeclarationNameInfo &NameInfo, 2303 const CXXScopeSpec *SS, NamedDecl *FoundD, 2304 SourceLocation TemplateKWLoc, 2305 const TemplateArgumentListInfo *TemplateArgs) { 2306 NestedNameSpecifierLoc NNS = 2307 SS ? SS->getWithLocInContext(Context) : NestedNameSpecifierLoc(); 2308 return BuildDeclRefExpr(D, Ty, VK, NameInfo, NNS, FoundD, TemplateKWLoc, 2309 TemplateArgs); 2310 } 2311 2312 // CUDA/HIP: Check whether a captured reference variable is referencing a 2313 // host variable in a device or host device lambda. 2314 static bool isCapturingReferenceToHostVarInCUDADeviceLambda(const Sema &S, 2315 VarDecl *VD) { 2316 if (!S.getLangOpts().CUDA || !VD->hasInit()) 2317 return false; 2318 assert(VD->getType()->isReferenceType()); 2319 2320 // Check whether the reference variable is referencing a host variable. 2321 auto *DRE = dyn_cast<DeclRefExpr>(VD->getInit()); 2322 if (!DRE) 2323 return false; 2324 auto *Referee = dyn_cast<VarDecl>(DRE->getDecl()); 2325 if (!Referee || !Referee->hasGlobalStorage() || 2326 Referee->hasAttr<CUDADeviceAttr>()) 2327 return false; 2328 2329 // Check whether the current function is a device or host device lambda. 2330 // Check whether the reference variable is a capture by getDeclContext() 2331 // since refersToEnclosingVariableOrCapture() is not ready at this point. 2332 auto *MD = dyn_cast_or_null<CXXMethodDecl>(S.CurContext); 2333 if (MD && MD->getParent()->isLambda() && 2334 MD->getOverloadedOperator() == OO_Call && MD->hasAttr<CUDADeviceAttr>() && 2335 VD->getDeclContext() != MD) 2336 return true; 2337 2338 return false; 2339 } 2340 2341 NonOdrUseReason Sema::getNonOdrUseReasonInCurrentContext(ValueDecl *D) { 2342 // A declaration named in an unevaluated operand never constitutes an odr-use. 2343 if (isUnevaluatedContext()) 2344 return NOUR_Unevaluated; 2345 2346 // C++2a [basic.def.odr]p4: 2347 // A variable x whose name appears as a potentially-evaluated expression e 2348 // is odr-used by e unless [...] x is a reference that is usable in 2349 // constant expressions. 2350 // CUDA/HIP: 2351 // If a reference variable referencing a host variable is captured in a 2352 // device or host device lambda, the value of the referee must be copied 2353 // to the capture and the reference variable must be treated as odr-use 2354 // since the value of the referee is not known at compile time and must 2355 // be loaded from the captured. 2356 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 2357 if (VD->getType()->isReferenceType() && 2358 !(getLangOpts().OpenMP && OpenMP().isOpenMPCapturedDecl(D)) && 2359 !isCapturingReferenceToHostVarInCUDADeviceLambda(*this, VD) && 2360 VD->isUsableInConstantExpressions(Context)) 2361 return NOUR_Constant; 2362 } 2363 2364 // All remaining non-variable cases constitute an odr-use. For variables, we 2365 // need to wait and see how the expression is used. 2366 return NOUR_None; 2367 } 2368 2369 DeclRefExpr * 2370 Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, 2371 const DeclarationNameInfo &NameInfo, 2372 NestedNameSpecifierLoc NNS, NamedDecl *FoundD, 2373 SourceLocation TemplateKWLoc, 2374 const TemplateArgumentListInfo *TemplateArgs) { 2375 bool RefersToCapturedVariable = isa<VarDecl, BindingDecl>(D) && 2376 NeedToCaptureVariable(D, NameInfo.getLoc()); 2377 2378 DeclRefExpr *E = DeclRefExpr::Create( 2379 Context, NNS, TemplateKWLoc, D, RefersToCapturedVariable, NameInfo, Ty, 2380 VK, FoundD, TemplateArgs, getNonOdrUseReasonInCurrentContext(D)); 2381 MarkDeclRefReferenced(E); 2382 2383 // C++ [except.spec]p17: 2384 // An exception-specification is considered to be needed when: 2385 // - in an expression, the function is the unique lookup result or 2386 // the selected member of a set of overloaded functions. 2387 // 2388 // We delay doing this until after we've built the function reference and 2389 // marked it as used so that: 2390 // a) if the function is defaulted, we get errors from defining it before / 2391 // instead of errors from computing its exception specification, and 2392 // b) if the function is a defaulted comparison, we can use the body we 2393 // build when defining it as input to the exception specification 2394 // computation rather than computing a new body. 2395 if (const auto *FPT = Ty->getAs<FunctionProtoType>()) { 2396 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) { 2397 if (const auto *NewFPT = ResolveExceptionSpec(NameInfo.getLoc(), FPT)) 2398 E->setType(Context.getQualifiedType(NewFPT, Ty.getQualifiers())); 2399 } 2400 } 2401 2402 if (getLangOpts().ObjCWeak && isa<VarDecl>(D) && 2403 Ty.getObjCLifetime() == Qualifiers::OCL_Weak && !isUnevaluatedContext() && 2404 !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, E->getBeginLoc())) 2405 getCurFunction()->recordUseOfWeak(E); 2406 2407 const auto *FD = dyn_cast<FieldDecl>(D); 2408 if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) 2409 FD = IFD->getAnonField(); 2410 if (FD) { 2411 UnusedPrivateFields.remove(FD); 2412 // Just in case we're building an illegal pointer-to-member. 2413 if (FD->isBitField()) 2414 E->setObjectKind(OK_BitField); 2415 } 2416 2417 // C++ [expr.prim]/8: The expression [...] is a bit-field if the identifier 2418 // designates a bit-field. 2419 if (const auto *BD = dyn_cast<BindingDecl>(D)) 2420 if (const auto *BE = BD->getBinding()) 2421 E->setObjectKind(BE->getObjectKind()); 2422 2423 return E; 2424 } 2425 2426 void 2427 Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, 2428 TemplateArgumentListInfo &Buffer, 2429 DeclarationNameInfo &NameInfo, 2430 const TemplateArgumentListInfo *&TemplateArgs) { 2431 if (Id.getKind() == UnqualifiedIdKind::IK_TemplateId) { 2432 Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); 2433 Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); 2434 2435 ASTTemplateArgsPtr TemplateArgsPtr(Id.TemplateId->getTemplateArgs(), 2436 Id.TemplateId->NumArgs); 2437 translateTemplateArguments(TemplateArgsPtr, Buffer); 2438 2439 TemplateName TName = Id.TemplateId->Template.get(); 2440 SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; 2441 NameInfo = Context.getNameForTemplate(TName, TNameLoc); 2442 TemplateArgs = &Buffer; 2443 } else { 2444 NameInfo = GetNameFromUnqualifiedId(Id); 2445 TemplateArgs = nullptr; 2446 } 2447 } 2448 2449 bool Sema::DiagnoseDependentMemberLookup(const LookupResult &R) { 2450 // During a default argument instantiation the CurContext points 2451 // to a CXXMethodDecl; but we can't apply a this-> fixit inside a 2452 // function parameter list, hence add an explicit check. 2453 bool isDefaultArgument = 2454 !CodeSynthesisContexts.empty() && 2455 CodeSynthesisContexts.back().Kind == 2456 CodeSynthesisContext::DefaultFunctionArgumentInstantiation; 2457 const auto *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); 2458 bool isInstance = CurMethod && CurMethod->isInstance() && 2459 R.getNamingClass() == CurMethod->getParent() && 2460 !isDefaultArgument; 2461 2462 // There are two ways we can find a class-scope declaration during template 2463 // instantiation that we did not find in the template definition: if it is a 2464 // member of a dependent base class, or if it is declared after the point of 2465 // use in the same class. Distinguish these by comparing the class in which 2466 // the member was found to the naming class of the lookup. 2467 unsigned DiagID = diag::err_found_in_dependent_base; 2468 unsigned NoteID = diag::note_member_declared_at; 2469 if (R.getRepresentativeDecl()->getDeclContext()->Equals(R.getNamingClass())) { 2470 DiagID = getLangOpts().MSVCCompat ? diag::ext_found_later_in_class 2471 : diag::err_found_later_in_class; 2472 } else if (getLangOpts().MSVCCompat) { 2473 DiagID = diag::ext_found_in_dependent_base; 2474 NoteID = diag::note_dependent_member_use; 2475 } 2476 2477 if (isInstance) { 2478 // Give a code modification hint to insert 'this->'. 2479 Diag(R.getNameLoc(), DiagID) 2480 << R.getLookupName() 2481 << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); 2482 CheckCXXThisCapture(R.getNameLoc()); 2483 } else { 2484 // FIXME: Add a FixItHint to insert 'Base::' or 'Derived::' (assuming 2485 // they're not shadowed). 2486 Diag(R.getNameLoc(), DiagID) << R.getLookupName(); 2487 } 2488 2489 for (const NamedDecl *D : R) 2490 Diag(D->getLocation(), NoteID); 2491 2492 // Return true if we are inside a default argument instantiation 2493 // and the found name refers to an instance member function, otherwise 2494 // the caller will try to create an implicit member call and this is wrong 2495 // for default arguments. 2496 // 2497 // FIXME: Is this special case necessary? We could allow the caller to 2498 // diagnose this. 2499 if (isDefaultArgument && ((*R.begin())->isCXXInstanceMember())) { 2500 Diag(R.getNameLoc(), diag::err_member_call_without_object) << 0; 2501 return true; 2502 } 2503 2504 // Tell the callee to try to recover. 2505 return false; 2506 } 2507 2508 bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, 2509 CorrectionCandidateCallback &CCC, 2510 TemplateArgumentListInfo *ExplicitTemplateArgs, 2511 ArrayRef<Expr *> Args, DeclContext *LookupCtx) { 2512 DeclarationName Name = R.getLookupName(); 2513 SourceRange NameRange = R.getLookupNameInfo().getSourceRange(); 2514 2515 unsigned diagnostic = diag::err_undeclared_var_use; 2516 unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; 2517 if (Name.getNameKind() == DeclarationName::CXXOperatorName || 2518 Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || 2519 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { 2520 diagnostic = diag::err_undeclared_use; 2521 diagnostic_suggest = diag::err_undeclared_use_suggest; 2522 } 2523 2524 // If the original lookup was an unqualified lookup, fake an 2525 // unqualified lookup. This is useful when (for example) the 2526 // original lookup would not have found something because it was a 2527 // dependent name. 2528 DeclContext *DC = 2529 LookupCtx ? LookupCtx : (SS.isEmpty() ? CurContext : nullptr); 2530 while (DC) { 2531 if (isa<CXXRecordDecl>(DC)) { 2532 if (ExplicitTemplateArgs) { 2533 if (LookupTemplateName( 2534 R, S, SS, Context.getRecordType(cast<CXXRecordDecl>(DC)), 2535 /*EnteringContext*/ false, TemplateNameIsRequired, 2536 /*RequiredTemplateKind*/ nullptr, /*AllowTypoCorrection*/ true)) 2537 return true; 2538 } else { 2539 LookupQualifiedName(R, DC); 2540 } 2541 2542 if (!R.empty()) { 2543 // Don't give errors about ambiguities in this lookup. 2544 R.suppressDiagnostics(); 2545 2546 // If there's a best viable function among the results, only mention 2547 // that one in the notes. 2548 OverloadCandidateSet Candidates(R.getNameLoc(), 2549 OverloadCandidateSet::CSK_Normal); 2550 AddOverloadedCallCandidates(R, ExplicitTemplateArgs, Args, Candidates); 2551 OverloadCandidateSet::iterator Best; 2552 if (Candidates.BestViableFunction(*this, R.getNameLoc(), Best) == 2553 OR_Success) { 2554 R.clear(); 2555 R.addDecl(Best->FoundDecl.getDecl(), Best->FoundDecl.getAccess()); 2556 R.resolveKind(); 2557 } 2558 2559 return DiagnoseDependentMemberLookup(R); 2560 } 2561 2562 R.clear(); 2563 } 2564 2565 DC = DC->getLookupParent(); 2566 } 2567 2568 // We didn't find anything, so try to correct for a typo. 2569 TypoCorrection Corrected; 2570 if (S && (Corrected = 2571 CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, 2572 CCC, CorrectTypoKind::ErrorRecovery, LookupCtx))) { 2573 std::string CorrectedStr(Corrected.getAsString(getLangOpts())); 2574 bool DroppedSpecifier = 2575 Corrected.WillReplaceSpecifier() && Name.getAsString() == CorrectedStr; 2576 R.setLookupName(Corrected.getCorrection()); 2577 2578 bool AcceptableWithRecovery = false; 2579 bool AcceptableWithoutRecovery = false; 2580 NamedDecl *ND = Corrected.getFoundDecl(); 2581 if (ND) { 2582 if (Corrected.isOverloaded()) { 2583 OverloadCandidateSet OCS(R.getNameLoc(), 2584 OverloadCandidateSet::CSK_Normal); 2585 OverloadCandidateSet::iterator Best; 2586 for (NamedDecl *CD : Corrected) { 2587 if (FunctionTemplateDecl *FTD = 2588 dyn_cast<FunctionTemplateDecl>(CD)) 2589 AddTemplateOverloadCandidate( 2590 FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, 2591 Args, OCS); 2592 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) 2593 if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 0) 2594 AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), 2595 Args, OCS); 2596 } 2597 switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { 2598 case OR_Success: 2599 ND = Best->FoundDecl; 2600 Corrected.setCorrectionDecl(ND); 2601 break; 2602 default: 2603 // FIXME: Arbitrarily pick the first declaration for the note. 2604 Corrected.setCorrectionDecl(ND); 2605 break; 2606 } 2607 } 2608 R.addDecl(ND); 2609 if (getLangOpts().CPlusPlus && ND->isCXXClassMember()) { 2610 CXXRecordDecl *Record = nullptr; 2611 if (Corrected.getCorrectionSpecifier()) { 2612 const Type *Ty = Corrected.getCorrectionSpecifier()->getAsType(); 2613 Record = Ty->getAsCXXRecordDecl(); 2614 } 2615 if (!Record) 2616 Record = cast<CXXRecordDecl>( 2617 ND->getDeclContext()->getRedeclContext()); 2618 R.setNamingClass(Record); 2619 } 2620 2621 auto *UnderlyingND = ND->getUnderlyingDecl(); 2622 AcceptableWithRecovery = isa<ValueDecl>(UnderlyingND) || 2623 isa<FunctionTemplateDecl>(UnderlyingND); 2624 // FIXME: If we ended up with a typo for a type name or 2625 // Objective-C class name, we're in trouble because the parser 2626 // is in the wrong place to recover. Suggest the typo 2627 // correction, but don't make it a fix-it since we're not going 2628 // to recover well anyway. 2629 AcceptableWithoutRecovery = isa<TypeDecl>(UnderlyingND) || 2630 getAsTypeTemplateDecl(UnderlyingND) || 2631 isa<ObjCInterfaceDecl>(UnderlyingND); 2632 } else { 2633 // FIXME: We found a keyword. Suggest it, but don't provide a fix-it 2634 // because we aren't able to recover. 2635 AcceptableWithoutRecovery = true; 2636 } 2637 2638 if (AcceptableWithRecovery || AcceptableWithoutRecovery) { 2639 unsigned NoteID = Corrected.getCorrectionDeclAs<ImplicitParamDecl>() 2640 ? diag::note_implicit_param_decl 2641 : diag::note_previous_decl; 2642 if (SS.isEmpty()) 2643 diagnoseTypo(Corrected, PDiag(diagnostic_suggest) << Name << NameRange, 2644 PDiag(NoteID), AcceptableWithRecovery); 2645 else 2646 diagnoseTypo(Corrected, 2647 PDiag(diag::err_no_member_suggest) 2648 << Name << computeDeclContext(SS, false) 2649 << DroppedSpecifier << NameRange, 2650 PDiag(NoteID), AcceptableWithRecovery); 2651 2652 // Tell the callee whether to try to recover. 2653 return !AcceptableWithRecovery; 2654 } 2655 } 2656 R.clear(); 2657 2658 // Emit a special diagnostic for failed member lookups. 2659 // FIXME: computing the declaration context might fail here (?) 2660 if (!SS.isEmpty()) { 2661 Diag(R.getNameLoc(), diag::err_no_member) 2662 << Name << computeDeclContext(SS, false) << NameRange; 2663 return true; 2664 } 2665 2666 // Give up, we can't recover. 2667 Diag(R.getNameLoc(), diagnostic) << Name << NameRange; 2668 return true; 2669 } 2670 2671 /// In Microsoft mode, if we are inside a template class whose parent class has 2672 /// dependent base classes, and we can't resolve an unqualified identifier, then 2673 /// assume the identifier is a member of a dependent base class. We can only 2674 /// recover successfully in static methods, instance methods, and other contexts 2675 /// where 'this' is available. This doesn't precisely match MSVC's 2676 /// instantiation model, but it's close enough. 2677 static Expr * 2678 recoverFromMSUnqualifiedLookup(Sema &S, ASTContext &Context, 2679 DeclarationNameInfo &NameInfo, 2680 SourceLocation TemplateKWLoc, 2681 const TemplateArgumentListInfo *TemplateArgs) { 2682 // Only try to recover from lookup into dependent bases in static methods or 2683 // contexts where 'this' is available. 2684 QualType ThisType = S.getCurrentThisType(); 2685 const CXXRecordDecl *RD = nullptr; 2686 if (!ThisType.isNull()) 2687 RD = ThisType->getPointeeType()->getAsCXXRecordDecl(); 2688 else if (auto *MD = dyn_cast<CXXMethodDecl>(S.CurContext)) 2689 RD = MD->getParent(); 2690 if (!RD || !RD->hasDefinition() || !RD->hasAnyDependentBases()) 2691 return nullptr; 2692 2693 // Diagnose this as unqualified lookup into a dependent base class. If 'this' 2694 // is available, suggest inserting 'this->' as a fixit. 2695 SourceLocation Loc = NameInfo.getLoc(); 2696 auto DB = S.Diag(Loc, diag::ext_undeclared_unqual_id_with_dependent_base); 2697 DB << NameInfo.getName() << RD; 2698 2699 if (!ThisType.isNull()) { 2700 DB << FixItHint::CreateInsertion(Loc, "this->"); 2701 return CXXDependentScopeMemberExpr::Create( 2702 Context, /*This=*/nullptr, ThisType, /*IsArrow=*/true, 2703 /*Op=*/SourceLocation(), NestedNameSpecifierLoc(), TemplateKWLoc, 2704 /*FirstQualifierFoundInScope=*/nullptr, NameInfo, TemplateArgs); 2705 } 2706 2707 // Synthesize a fake NNS that points to the derived class. This will 2708 // perform name lookup during template instantiation. 2709 CXXScopeSpec SS; 2710 auto *NNS = 2711 NestedNameSpecifier::Create(Context, nullptr, RD->getTypeForDecl()); 2712 SS.MakeTrivial(Context, NNS, SourceRange(Loc, Loc)); 2713 return DependentScopeDeclRefExpr::Create( 2714 Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, 2715 TemplateArgs); 2716 } 2717 2718 ExprResult 2719 Sema::ActOnIdExpression(Scope *S, CXXScopeSpec &SS, 2720 SourceLocation TemplateKWLoc, UnqualifiedId &Id, 2721 bool HasTrailingLParen, bool IsAddressOfOperand, 2722 CorrectionCandidateCallback *CCC, 2723 bool IsInlineAsmIdentifier, Token *KeywordReplacement) { 2724 assert(!(IsAddressOfOperand && HasTrailingLParen) && 2725 "cannot be direct & operand and have a trailing lparen"); 2726 if (SS.isInvalid()) 2727 return ExprError(); 2728 2729 TemplateArgumentListInfo TemplateArgsBuffer; 2730 2731 // Decompose the UnqualifiedId into the following data. 2732 DeclarationNameInfo NameInfo; 2733 const TemplateArgumentListInfo *TemplateArgs; 2734 DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); 2735 2736 DeclarationName Name = NameInfo.getName(); 2737 IdentifierInfo *II = Name.getAsIdentifierInfo(); 2738 SourceLocation NameLoc = NameInfo.getLoc(); 2739 2740 if (II && II->isEditorPlaceholder()) { 2741 // FIXME: When typed placeholders are supported we can create a typed 2742 // placeholder expression node. 2743 return ExprError(); 2744 } 2745 2746 // This specially handles arguments of attributes appertains to a type of C 2747 // struct field such that the name lookup within a struct finds the member 2748 // name, which is not the case for other contexts in C. 2749 if (isAttrContext() && !getLangOpts().CPlusPlus && S->isClassScope()) { 2750 // See if this is reference to a field of struct. 2751 LookupResult R(*this, NameInfo, LookupMemberName); 2752 // LookupName handles a name lookup from within anonymous struct. 2753 if (LookupName(R, S)) { 2754 if (auto *VD = dyn_cast<ValueDecl>(R.getFoundDecl())) { 2755 QualType type = VD->getType().getNonReferenceType(); 2756 // This will eventually be translated into MemberExpr upon 2757 // the use of instantiated struct fields. 2758 return BuildDeclRefExpr(VD, type, VK_LValue, NameLoc); 2759 } 2760 } 2761 } 2762 2763 // Perform the required lookup. 2764 LookupResult R(*this, NameInfo, 2765 (Id.getKind() == UnqualifiedIdKind::IK_ImplicitSelfParam) 2766 ? LookupObjCImplicitSelfParam 2767 : LookupOrdinaryName); 2768 if (TemplateKWLoc.isValid() || TemplateArgs) { 2769 // Lookup the template name again to correctly establish the context in 2770 // which it was found. This is really unfortunate as we already did the 2771 // lookup to determine that it was a template name in the first place. If 2772 // this becomes a performance hit, we can work harder to preserve those 2773 // results until we get here but it's likely not worth it. 2774 AssumedTemplateKind AssumedTemplate; 2775 if (LookupTemplateName(R, S, SS, /*ObjectType=*/QualType(), 2776 /*EnteringContext=*/false, TemplateKWLoc, 2777 &AssumedTemplate)) 2778 return ExprError(); 2779 2780 if (R.wasNotFoundInCurrentInstantiation() || SS.isInvalid()) 2781 return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, 2782 IsAddressOfOperand, TemplateArgs); 2783 } else { 2784 bool IvarLookupFollowUp = II && !SS.isSet() && getCurMethodDecl(); 2785 LookupParsedName(R, S, &SS, /*ObjectType=*/QualType(), 2786 /*AllowBuiltinCreation=*/!IvarLookupFollowUp); 2787 2788 // If the result might be in a dependent base class, this is a dependent 2789 // id-expression. 2790 if (R.wasNotFoundInCurrentInstantiation() || SS.isInvalid()) 2791 return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, 2792 IsAddressOfOperand, TemplateArgs); 2793 2794 // If this reference is in an Objective-C method, then we need to do 2795 // some special Objective-C lookup, too. 2796 if (IvarLookupFollowUp) { 2797 ExprResult E(ObjC().LookupInObjCMethod(R, S, II, true)); 2798 if (E.isInvalid()) 2799 return ExprError(); 2800 2801 if (Expr *Ex = E.getAs<Expr>()) 2802 return Ex; 2803 } 2804 } 2805 2806 if (R.isAmbiguous()) 2807 return ExprError(); 2808 2809 // This could be an implicitly declared function reference if the language 2810 // mode allows it as a feature. 2811 if (R.empty() && HasTrailingLParen && II && 2812 getLangOpts().implicitFunctionsAllowed()) { 2813 NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); 2814 if (D) R.addDecl(D); 2815 } 2816 2817 // Determine whether this name might be a candidate for 2818 // argument-dependent lookup. 2819 bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); 2820 2821 if (R.empty() && !ADL) { 2822 if (SS.isEmpty() && getLangOpts().MSVCCompat) { 2823 if (Expr *E = recoverFromMSUnqualifiedLookup(*this, Context, NameInfo, 2824 TemplateKWLoc, TemplateArgs)) 2825 return E; 2826 } 2827 2828 // Don't diagnose an empty lookup for inline assembly. 2829 if (IsInlineAsmIdentifier) 2830 return ExprError(); 2831 2832 // If this name wasn't predeclared and if this is not a function 2833 // call, diagnose the problem. 2834 DefaultFilterCCC DefaultValidator(II, SS.isValid() ? SS.getScopeRep() 2835 : nullptr); 2836 DefaultValidator.IsAddressOfOperand = IsAddressOfOperand; 2837 assert((!CCC || CCC->IsAddressOfOperand == IsAddressOfOperand) && 2838 "Typo correction callback misconfigured"); 2839 if (CCC) { 2840 // Make sure the callback knows what the typo being diagnosed is. 2841 CCC->setTypoName(II); 2842 if (SS.isValid()) 2843 CCC->setTypoNNS(SS.getScopeRep()); 2844 } 2845 // FIXME: DiagnoseEmptyLookup produces bad diagnostics if we're looking for 2846 // a template name, but we happen to have always already looked up the name 2847 // before we get here if it must be a template name. 2848 if (DiagnoseEmptyLookup(S, SS, R, CCC ? *CCC : DefaultValidator, nullptr, 2849 {}, nullptr)) 2850 return ExprError(); 2851 2852 assert(!R.empty() && 2853 "DiagnoseEmptyLookup returned false but added no results"); 2854 2855 // If we found an Objective-C instance variable, let 2856 // LookupInObjCMethod build the appropriate expression to 2857 // reference the ivar. 2858 if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { 2859 R.clear(); 2860 ExprResult E(ObjC().LookupInObjCMethod(R, S, Ivar->getIdentifier())); 2861 // In a hopelessly buggy code, Objective-C instance variable 2862 // lookup fails and no expression will be built to reference it. 2863 if (!E.isInvalid() && !E.get()) 2864 return ExprError(); 2865 return E; 2866 } 2867 } 2868 2869 // This is guaranteed from this point on. 2870 assert(!R.empty() || ADL); 2871 2872 // Check whether this might be a C++ implicit instance member access. 2873 // C++ [class.mfct.non-static]p3: 2874 // When an id-expression that is not part of a class member access 2875 // syntax and not used to form a pointer to member is used in the 2876 // body of a non-static member function of class X, if name lookup 2877 // resolves the name in the id-expression to a non-static non-type 2878 // member of some class C, the id-expression is transformed into a 2879 // class member access expression using (*this) as the 2880 // postfix-expression to the left of the . operator. 2881 // 2882 // But we don't actually need to do this for '&' operands if R 2883 // resolved to a function or overloaded function set, because the 2884 // expression is ill-formed if it actually works out to be a 2885 // non-static member function: 2886 // 2887 // C++ [expr.ref]p4: 2888 // Otherwise, if E1.E2 refers to a non-static member function. . . 2889 // [t]he expression can be used only as the left-hand operand of a 2890 // member function call. 2891 // 2892 // There are other safeguards against such uses, but it's important 2893 // to get this right here so that we don't end up making a 2894 // spuriously dependent expression if we're inside a dependent 2895 // instance method. 2896 if (isPotentialImplicitMemberAccess(SS, R, IsAddressOfOperand)) 2897 return BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, 2898 S); 2899 2900 if (TemplateArgs || TemplateKWLoc.isValid()) { 2901 2902 // In C++1y, if this is a variable template id, then check it 2903 // in BuildTemplateIdExpr(). 2904 // The single lookup result must be a variable template declaration. 2905 if (Id.getKind() == UnqualifiedIdKind::IK_TemplateId && Id.TemplateId && 2906 Id.TemplateId->Kind == TNK_Var_template) { 2907 assert(R.getAsSingle<VarTemplateDecl>() && 2908 "There should only be one declaration found."); 2909 } 2910 2911 return BuildTemplateIdExpr(SS, TemplateKWLoc, R, ADL, TemplateArgs); 2912 } 2913 2914 return BuildDeclarationNameExpr(SS, R, ADL); 2915 } 2916 2917 ExprResult Sema::BuildQualifiedDeclarationNameExpr( 2918 CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo, 2919 bool IsAddressOfOperand, TypeSourceInfo **RecoveryTSI) { 2920 LookupResult R(*this, NameInfo, LookupOrdinaryName); 2921 LookupParsedName(R, /*S=*/nullptr, &SS, /*ObjectType=*/QualType()); 2922 2923 if (R.isAmbiguous()) 2924 return ExprError(); 2925 2926 if (R.wasNotFoundInCurrentInstantiation() || SS.isInvalid()) 2927 return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), 2928 NameInfo, /*TemplateArgs=*/nullptr); 2929 2930 if (R.empty()) { 2931 // Don't diagnose problems with invalid record decl, the secondary no_member 2932 // diagnostic during template instantiation is likely bogus, e.g. if a class 2933 // is invalid because it's derived from an invalid base class, then missing 2934 // members were likely supposed to be inherited. 2935 DeclContext *DC = computeDeclContext(SS); 2936 if (const auto *CD = dyn_cast<CXXRecordDecl>(DC)) 2937 if (CD->isInvalidDecl() || CD->isBeingDefined()) 2938 return ExprError(); 2939 Diag(NameInfo.getLoc(), diag::err_no_member) 2940 << NameInfo.getName() << DC << SS.getRange(); 2941 return ExprError(); 2942 } 2943 2944 if (const TypeDecl *TD = R.getAsSingle<TypeDecl>()) { 2945 QualType Ty = Context.getTypeDeclType(TD); 2946 QualType ET = getElaboratedType(ElaboratedTypeKeyword::None, SS, Ty); 2947 2948 // Diagnose a missing typename if this resolved unambiguously to a type in 2949 // a dependent context. If we can recover with a type, downgrade this to 2950 // a warning in Microsoft compatibility mode. 2951 unsigned DiagID = diag::err_typename_missing; 2952 if (RecoveryTSI && getLangOpts().MSVCCompat) 2953 DiagID = diag::ext_typename_missing; 2954 SourceLocation Loc = SS.getBeginLoc(); 2955 auto D = Diag(Loc, DiagID); 2956 D << ET << SourceRange(Loc, NameInfo.getEndLoc()); 2957 2958 // Don't recover if the caller isn't expecting us to or if we're in a SFINAE 2959 // context. 2960 if (!RecoveryTSI) 2961 return ExprError(); 2962 2963 // Only issue the fixit if we're prepared to recover. 2964 D << FixItHint::CreateInsertion(Loc, "typename "); 2965 2966 // Recover by pretending this was an elaborated type. 2967 TypeLocBuilder TLB; 2968 TLB.pushTypeSpec(Ty).setNameLoc(NameInfo.getLoc()); 2969 2970 ElaboratedTypeLoc QTL = TLB.push<ElaboratedTypeLoc>(ET); 2971 QTL.setElaboratedKeywordLoc(SourceLocation()); 2972 QTL.setQualifierLoc(SS.getWithLocInContext(Context)); 2973 2974 *RecoveryTSI = TLB.getTypeSourceInfo(Context, ET); 2975 2976 return ExprEmpty(); 2977 } 2978 2979 // If necessary, build an implicit class member access. 2980 if (isPotentialImplicitMemberAccess(SS, R, IsAddressOfOperand)) 2981 return BuildPossibleImplicitMemberExpr(SS, 2982 /*TemplateKWLoc=*/SourceLocation(), 2983 R, /*TemplateArgs=*/nullptr, 2984 /*S=*/nullptr); 2985 2986 return BuildDeclarationNameExpr(SS, R, /*ADL=*/false); 2987 } 2988 2989 ExprResult 2990 Sema::PerformObjectMemberConversion(Expr *From, 2991 NestedNameSpecifier *Qualifier, 2992 NamedDecl *FoundDecl, 2993 NamedDecl *Member) { 2994 const auto *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); 2995 if (!RD) 2996 return From; 2997 2998 QualType DestRecordType; 2999 QualType DestType; 3000 QualType FromRecordType; 3001 QualType FromType = From->getType(); 3002 bool PointerConversions = false; 3003 if (isa<FieldDecl>(Member)) { 3004 DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); 3005 auto FromPtrType = FromType->getAs<PointerType>(); 3006 DestRecordType = Context.getAddrSpaceQualType( 3007 DestRecordType, FromPtrType 3008 ? FromType->getPointeeType().getAddressSpace() 3009 : FromType.getAddressSpace()); 3010 3011 if (FromPtrType) { 3012 DestType = Context.getPointerType(DestRecordType); 3013 FromRecordType = FromPtrType->getPointeeType(); 3014 PointerConversions = true; 3015 } else { 3016 DestType = DestRecordType; 3017 FromRecordType = FromType; 3018 } 3019 } else if (const auto *Method = dyn_cast<CXXMethodDecl>(Member)) { 3020 if (!Method->isImplicitObjectMemberFunction()) 3021 return From; 3022 3023 DestType = Method->getThisType().getNonReferenceType(); 3024 DestRecordType = Method->getFunctionObjectParameterType(); 3025 3026 if (FromType->getAs<PointerType>()) { 3027 FromRecordType = FromType->getPointeeType(); 3028 PointerConversions = true; 3029 } else { 3030 FromRecordType = FromType; 3031 DestType = DestRecordType; 3032 } 3033 3034 LangAS FromAS = FromRecordType.getAddressSpace(); 3035 LangAS DestAS = DestRecordType.getAddressSpace(); 3036 if (FromAS != DestAS) { 3037 QualType FromRecordTypeWithoutAS = 3038 Context.removeAddrSpaceQualType(FromRecordType); 3039 QualType FromTypeWithDestAS = 3040 Context.getAddrSpaceQualType(FromRecordTypeWithoutAS, DestAS); 3041 if (PointerConversions) 3042 FromTypeWithDestAS = Context.getPointerType(FromTypeWithDestAS); 3043 From = ImpCastExprToType(From, FromTypeWithDestAS, 3044 CK_AddressSpaceConversion, From->getValueKind()) 3045 .get(); 3046 } 3047 } else { 3048 // No conversion necessary. 3049 return From; 3050 } 3051 3052 if (DestType->isDependentType() || FromType->isDependentType()) 3053 return From; 3054 3055 // If the unqualified types are the same, no conversion is necessary. 3056 if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) 3057 return From; 3058 3059 SourceRange FromRange = From->getSourceRange(); 3060 SourceLocation FromLoc = FromRange.getBegin(); 3061 3062 ExprValueKind VK = From->getValueKind(); 3063 3064 // C++ [class.member.lookup]p8: 3065 // [...] Ambiguities can often be resolved by qualifying a name with its 3066 // class name. 3067 // 3068 // If the member was a qualified name and the qualified referred to a 3069 // specific base subobject type, we'll cast to that intermediate type 3070 // first and then to the object in which the member is declared. That allows 3071 // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: 3072 // 3073 // class Base { public: int x; }; 3074 // class Derived1 : public Base { }; 3075 // class Derived2 : public Base { }; 3076 // class VeryDerived : public Derived1, public Derived2 { void f(); }; 3077 // 3078 // void VeryDerived::f() { 3079 // x = 17; // error: ambiguous base subobjects 3080 // Derived1::x = 17; // okay, pick the Base subobject of Derived1 3081 // } 3082 if (Qualifier && Qualifier->getAsType()) { 3083 QualType QType = QualType(Qualifier->getAsType(), 0); 3084 assert(QType->isRecordType() && "lookup done with non-record type"); 3085 3086 QualType QRecordType = QualType(QType->castAs<RecordType>(), 0); 3087 3088 // In C++98, the qualifier type doesn't actually have to be a base 3089 // type of the object type, in which case we just ignore it. 3090 // Otherwise build the appropriate casts. 3091 if (IsDerivedFrom(FromLoc, FromRecordType, QRecordType)) { 3092 CXXCastPath BasePath; 3093 if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, 3094 FromLoc, FromRange, &BasePath)) 3095 return ExprError(); 3096 3097 if (PointerConversions) 3098 QType = Context.getPointerType(QType); 3099 From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, 3100 VK, &BasePath).get(); 3101 3102 FromType = QType; 3103 FromRecordType = QRecordType; 3104 3105 // If the qualifier type was the same as the destination type, 3106 // we're done. 3107 if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) 3108 return From; 3109 } 3110 } 3111 3112 CXXCastPath BasePath; 3113 if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, 3114 FromLoc, FromRange, &BasePath, 3115 /*IgnoreAccess=*/true)) 3116 return ExprError(); 3117 3118 // Propagate qualifiers to base subobjects as per: 3119 // C++ [basic.type.qualifier]p1.2: 3120 // A volatile object is [...] a subobject of a volatile object. 3121 Qualifiers FromTypeQuals = FromType.getQualifiers(); 3122 FromTypeQuals.setAddressSpace(DestType.getAddressSpace()); 3123 DestType = Context.getQualifiedType(DestType, FromTypeQuals); 3124 3125 return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, VK, 3126 &BasePath); 3127 } 3128 3129 bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, 3130 const LookupResult &R, 3131 bool HasTrailingLParen) { 3132 // Only when used directly as the postfix-expression of a call. 3133 if (!HasTrailingLParen) 3134 return false; 3135 3136 // Never if a scope specifier was provided. 3137 if (SS.isNotEmpty()) 3138 return false; 3139 3140 // Only in C++ or ObjC++. 3141 if (!getLangOpts().CPlusPlus) 3142 return false; 3143 3144 // Turn off ADL when we find certain kinds of declarations during 3145 // normal lookup: 3146 for (const NamedDecl *D : R) { 3147 // C++0x [basic.lookup.argdep]p3: 3148 // -- a declaration of a class member 3149 // Since using decls preserve this property, we check this on the 3150 // original decl. 3151 if (D->isCXXClassMember()) 3152 return false; 3153 3154 // C++0x [basic.lookup.argdep]p3: 3155 // -- a block-scope function declaration that is not a 3156 // using-declaration 3157 // NOTE: we also trigger this for function templates (in fact, we 3158 // don't check the decl type at all, since all other decl types 3159 // turn off ADL anyway). 3160 if (isa<UsingShadowDecl>(D)) 3161 D = cast<UsingShadowDecl>(D)->getTargetDecl(); 3162 else if (D->getLexicalDeclContext()->isFunctionOrMethod()) 3163 return false; 3164 3165 // C++0x [basic.lookup.argdep]p3: 3166 // -- a declaration that is neither a function or a function 3167 // template 3168 // And also for builtin functions. 3169 if (const auto *FDecl = dyn_cast<FunctionDecl>(D)) { 3170 // But also builtin functions. 3171 if (FDecl->getBuiltinID() && FDecl->isImplicit()) 3172 return false; 3173 } else if (!isa<FunctionTemplateDecl>(D)) 3174 return false; 3175 } 3176 3177 return true; 3178 } 3179 3180 3181 /// Diagnoses obvious problems with the use of the given declaration 3182 /// as an expression. This is only actually called for lookups that 3183 /// were not overloaded, and it doesn't promise that the declaration 3184 /// will in fact be used. 3185 static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D, 3186 bool AcceptInvalid) { 3187 if (D->isInvalidDecl() && !AcceptInvalid) 3188 return true; 3189 3190 if (isa<TypedefNameDecl>(D)) { 3191 S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); 3192 return true; 3193 } 3194 3195 if (isa<ObjCInterfaceDecl>(D)) { 3196 S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); 3197 return true; 3198 } 3199 3200 if (isa<NamespaceDecl>(D)) { 3201 S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); 3202 return true; 3203 } 3204 3205 return false; 3206 } 3207 3208 // Certain multiversion types should be treated as overloaded even when there is 3209 // only one result. 3210 static bool ShouldLookupResultBeMultiVersionOverload(const LookupResult &R) { 3211 assert(R.isSingleResult() && "Expected only a single result"); 3212 const auto *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); 3213 return FD && 3214 (FD->isCPUDispatchMultiVersion() || FD->isCPUSpecificMultiVersion()); 3215 } 3216 3217 ExprResult Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, 3218 LookupResult &R, bool NeedsADL, 3219 bool AcceptInvalidDecl) { 3220 // If this is a single, fully-resolved result and we don't need ADL, 3221 // just build an ordinary singleton decl ref. 3222 if (!NeedsADL && R.isSingleResult() && 3223 !R.getAsSingle<FunctionTemplateDecl>() && 3224 !ShouldLookupResultBeMultiVersionOverload(R)) 3225 return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), R.getFoundDecl(), 3226 R.getRepresentativeDecl(), nullptr, 3227 AcceptInvalidDecl); 3228 3229 // We only need to check the declaration if there's exactly one 3230 // result, because in the overloaded case the results can only be 3231 // functions and function templates. 3232 if (R.isSingleResult() && !ShouldLookupResultBeMultiVersionOverload(R) && 3233 CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl(), 3234 AcceptInvalidDecl)) 3235 return ExprError(); 3236 3237 // Otherwise, just build an unresolved lookup expression. Suppress 3238 // any lookup-related diagnostics; we'll hash these out later, when 3239 // we've picked a target. 3240 R.suppressDiagnostics(); 3241 3242 UnresolvedLookupExpr *ULE = UnresolvedLookupExpr::Create( 3243 Context, R.getNamingClass(), SS.getWithLocInContext(Context), 3244 R.getLookupNameInfo(), NeedsADL, R.begin(), R.end(), 3245 /*KnownDependent=*/false, /*KnownInstantiationDependent=*/false); 3246 3247 return ULE; 3248 } 3249 3250 static void diagnoseUncapturableValueReferenceOrBinding(Sema &S, 3251 SourceLocation loc, 3252 ValueDecl *var); 3253 3254 ExprResult Sema::BuildDeclarationNameExpr( 3255 const CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo, NamedDecl *D, 3256 NamedDecl *FoundD, const TemplateArgumentListInfo *TemplateArgs, 3257 bool AcceptInvalidDecl) { 3258 assert(D && "Cannot refer to a NULL declaration"); 3259 assert(!isa<FunctionTemplateDecl>(D) && 3260 "Cannot refer unambiguously to a function template"); 3261 3262 SourceLocation Loc = NameInfo.getLoc(); 3263 if (CheckDeclInExpr(*this, Loc, D, AcceptInvalidDecl)) { 3264 // Recovery from invalid cases (e.g. D is an invalid Decl). 3265 // We use the dependent type for the RecoveryExpr to prevent bogus follow-up 3266 // diagnostics, as invalid decls use int as a fallback type. 3267 return CreateRecoveryExpr(NameInfo.getBeginLoc(), NameInfo.getEndLoc(), {}); 3268 } 3269 3270 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) { 3271 // Specifically diagnose references to class templates that are missing 3272 // a template argument list. 3273 diagnoseMissingTemplateArguments(SS, /*TemplateKeyword=*/false, TD, Loc); 3274 return ExprError(); 3275 } 3276 3277 // Make sure that we're referring to a value. 3278 if (!isa<ValueDecl, UnresolvedUsingIfExistsDecl>(D)) { 3279 Diag(Loc, diag::err_ref_non_value) << D << SS.getRange(); 3280 Diag(D->getLocation(), diag::note_declared_at); 3281 return ExprError(); 3282 } 3283 3284 // Check whether this declaration can be used. Note that we suppress 3285 // this check when we're going to perform argument-dependent lookup 3286 // on this function name, because this might not be the function 3287 // that overload resolution actually selects. 3288 if (DiagnoseUseOfDecl(D, Loc)) 3289 return ExprError(); 3290 3291 auto *VD = cast<ValueDecl>(D); 3292 3293 // Only create DeclRefExpr's for valid Decl's. 3294 if (VD->isInvalidDecl() && !AcceptInvalidDecl) 3295 return ExprError(); 3296 3297 // Handle members of anonymous structs and unions. If we got here, 3298 // and the reference is to a class member indirect field, then this 3299 // must be the subject of a pointer-to-member expression. 3300 if (auto *IndirectField = dyn_cast<IndirectFieldDecl>(VD); 3301 IndirectField && !IndirectField->isCXXClassMember()) 3302 return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), 3303 IndirectField); 3304 3305 QualType type = VD->getType(); 3306 if (type.isNull()) 3307 return ExprError(); 3308 ExprValueKind valueKind = VK_PRValue; 3309 3310 // In 'T ...V;', the type of the declaration 'V' is 'T...', but the type of 3311 // a reference to 'V' is simply (unexpanded) 'T'. The type, like the value, 3312 // is expanded by some outer '...' in the context of the use. 3313 type = type.getNonPackExpansionType(); 3314 3315 switch (D->getKind()) { 3316 // Ignore all the non-ValueDecl kinds. 3317 #define ABSTRACT_DECL(kind) 3318 #define VALUE(type, base) 3319 #define DECL(type, base) case Decl::type: 3320 #include "clang/AST/DeclNodes.inc" 3321 llvm_unreachable("invalid value decl kind"); 3322 3323 // These shouldn't make it here. 3324 case Decl::ObjCAtDefsField: 3325 llvm_unreachable("forming non-member reference to ivar?"); 3326 3327 // Enum constants are always r-values and never references. 3328 // Unresolved using declarations are dependent. 3329 case Decl::EnumConstant: 3330 case Decl::UnresolvedUsingValue: 3331 case Decl::OMPDeclareReduction: 3332 case Decl::OMPDeclareMapper: 3333 valueKind = VK_PRValue; 3334 break; 3335 3336 // Fields and indirect fields that got here must be for 3337 // pointer-to-member expressions; we just call them l-values for 3338 // internal consistency, because this subexpression doesn't really 3339 // exist in the high-level semantics. 3340 case Decl::Field: 3341 case Decl::IndirectField: 3342 case Decl::ObjCIvar: 3343 assert((getLangOpts().CPlusPlus || isAttrContext()) && 3344 "building reference to field in C?"); 3345 3346 // These can't have reference type in well-formed programs, but 3347 // for internal consistency we do this anyway. 3348 type = type.getNonReferenceType(); 3349 valueKind = VK_LValue; 3350 break; 3351 3352 // Non-type template parameters are either l-values or r-values 3353 // depending on the type. 3354 case Decl::NonTypeTemplateParm: { 3355 if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { 3356 type = reftype->getPointeeType(); 3357 valueKind = VK_LValue; // even if the parameter is an r-value reference 3358 break; 3359 } 3360 3361 // [expr.prim.id.unqual]p2: 3362 // If the entity is a template parameter object for a template 3363 // parameter of type T, the type of the expression is const T. 3364 // [...] The expression is an lvalue if the entity is a [...] template 3365 // parameter object. 3366 if (type->isRecordType()) { 3367 type = type.getUnqualifiedType().withConst(); 3368 valueKind = VK_LValue; 3369 break; 3370 } 3371 3372 // For non-references, we need to strip qualifiers just in case 3373 // the template parameter was declared as 'const int' or whatever. 3374 valueKind = VK_PRValue; 3375 type = type.getUnqualifiedType(); 3376 break; 3377 } 3378 3379 case Decl::Var: 3380 case Decl::VarTemplateSpecialization: 3381 case Decl::VarTemplatePartialSpecialization: 3382 case Decl::Decomposition: 3383 case Decl::Binding: 3384 case Decl::OMPCapturedExpr: 3385 // In C, "extern void blah;" is valid and is an r-value. 3386 if (!getLangOpts().CPlusPlus && !type.hasQualifiers() && 3387 type->isVoidType()) { 3388 valueKind = VK_PRValue; 3389 break; 3390 } 3391 [[fallthrough]]; 3392 3393 case Decl::ImplicitParam: 3394 case Decl::ParmVar: { 3395 // These are always l-values. 3396 valueKind = VK_LValue; 3397 type = type.getNonReferenceType(); 3398 3399 // FIXME: Does the addition of const really only apply in 3400 // potentially-evaluated contexts? Since the variable isn't actually 3401 // captured in an unevaluated context, it seems that the answer is no. 3402 if (!isUnevaluatedContext()) { 3403 QualType CapturedType = getCapturedDeclRefType(cast<ValueDecl>(VD), Loc); 3404 if (!CapturedType.isNull()) 3405 type = CapturedType; 3406 } 3407 break; 3408 } 3409 3410 case Decl::Function: { 3411 if (unsigned BID = cast<FunctionDecl>(VD)->getBuiltinID()) { 3412 if (!Context.BuiltinInfo.isDirectlyAddressable(BID)) { 3413 type = Context.BuiltinFnTy; 3414 valueKind = VK_PRValue; 3415 break; 3416 } 3417 } 3418 3419 const FunctionType *fty = type->castAs<FunctionType>(); 3420 3421 // If we're referring to a function with an __unknown_anytype 3422 // result type, make the entire expression __unknown_anytype. 3423 if (fty->getReturnType() == Context.UnknownAnyTy) { 3424 type = Context.UnknownAnyTy; 3425 valueKind = VK_PRValue; 3426 break; 3427 } 3428 3429 // Functions are l-values in C++. 3430 if (getLangOpts().CPlusPlus) { 3431 valueKind = VK_LValue; 3432 break; 3433 } 3434 3435 // C99 DR 316 says that, if a function type comes from a 3436 // function definition (without a prototype), that type is only 3437 // used for checking compatibility. Therefore, when referencing 3438 // the function, we pretend that we don't have the full function 3439 // type. 3440 if (!cast<FunctionDecl>(VD)->hasPrototype() && isa<FunctionProtoType>(fty)) 3441 type = Context.getFunctionNoProtoType(fty->getReturnType(), 3442 fty->getExtInfo()); 3443 3444 // Functions are r-values in C. 3445 valueKind = VK_PRValue; 3446 break; 3447 } 3448 3449 case Decl::CXXDeductionGuide: 3450 llvm_unreachable("building reference to deduction guide"); 3451 3452 case Decl::MSProperty: 3453 case Decl::MSGuid: 3454 case Decl::TemplateParamObject: 3455 // FIXME: Should MSGuidDecl and template parameter objects be subject to 3456 // capture in OpenMP, or duplicated between host and device? 3457 valueKind = VK_LValue; 3458 break; 3459 3460 case Decl::UnnamedGlobalConstant: 3461 valueKind = VK_LValue; 3462 break; 3463 3464 case Decl::CXXMethod: 3465 // If we're referring to a method with an __unknown_anytype 3466 // result type, make the entire expression __unknown_anytype. 3467 // This should only be possible with a type written directly. 3468 if (const FunctionProtoType *proto = 3469 dyn_cast<FunctionProtoType>(VD->getType())) 3470 if (proto->getReturnType() == Context.UnknownAnyTy) { 3471 type = Context.UnknownAnyTy; 3472 valueKind = VK_PRValue; 3473 break; 3474 } 3475 3476 // C++ methods are l-values if static, r-values if non-static. 3477 if (cast<CXXMethodDecl>(VD)->isStatic()) { 3478 valueKind = VK_LValue; 3479 break; 3480 } 3481 [[fallthrough]]; 3482 3483 case Decl::CXXConversion: 3484 case Decl::CXXDestructor: 3485 case Decl::CXXConstructor: 3486 valueKind = VK_PRValue; 3487 break; 3488 } 3489 3490 auto *E = 3491 BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS, FoundD, 3492 /*FIXME: TemplateKWLoc*/ SourceLocation(), TemplateArgs); 3493 // Clang AST consumers assume a DeclRefExpr refers to a valid decl. We 3494 // wrap a DeclRefExpr referring to an invalid decl with a dependent-type 3495 // RecoveryExpr to avoid follow-up semantic analysis (thus prevent bogus 3496 // diagnostics). 3497 if (VD->isInvalidDecl() && E) 3498 return CreateRecoveryExpr(E->getBeginLoc(), E->getEndLoc(), {E}); 3499 return E; 3500 } 3501 3502 static void ConvertUTF8ToWideString(unsigned CharByteWidth, StringRef Source, 3503 SmallString<32> &Target) { 3504 Target.resize(CharByteWidth * (Source.size() + 1)); 3505 char *ResultPtr = &Target[0]; 3506 const llvm::UTF8 *ErrorPtr; 3507 bool success = 3508 llvm::ConvertUTF8toWide(CharByteWidth, Source, ResultPtr, ErrorPtr); 3509 (void)success; 3510 assert(success); 3511 Target.resize(ResultPtr - &Target[0]); 3512 } 3513 3514 ExprResult Sema::BuildPredefinedExpr(SourceLocation Loc, 3515 PredefinedIdentKind IK) { 3516 Decl *currentDecl = getPredefinedExprDecl(CurContext); 3517 if (!currentDecl) { 3518 Diag(Loc, diag::ext_predef_outside_function); 3519 currentDecl = Context.getTranslationUnitDecl(); 3520 } 3521 3522 QualType ResTy; 3523 StringLiteral *SL = nullptr; 3524 if (cast<DeclContext>(currentDecl)->isDependentContext()) 3525 ResTy = Context.DependentTy; 3526 else { 3527 // Pre-defined identifiers are of type char[x], where x is the length of 3528 // the string. 3529 bool ForceElaboratedPrinting = 3530 IK == PredefinedIdentKind::Function && getLangOpts().MSVCCompat; 3531 auto Str = 3532 PredefinedExpr::ComputeName(IK, currentDecl, ForceElaboratedPrinting); 3533 unsigned Length = Str.length(); 3534 3535 llvm::APInt LengthI(32, Length + 1); 3536 if (IK == PredefinedIdentKind::LFunction || 3537 IK == PredefinedIdentKind::LFuncSig) { 3538 ResTy = 3539 Context.adjustStringLiteralBaseType(Context.WideCharTy.withConst()); 3540 SmallString<32> RawChars; 3541 ConvertUTF8ToWideString(Context.getTypeSizeInChars(ResTy).getQuantity(), 3542 Str, RawChars); 3543 ResTy = Context.getConstantArrayType(ResTy, LengthI, nullptr, 3544 ArraySizeModifier::Normal, 3545 /*IndexTypeQuals*/ 0); 3546 SL = StringLiteral::Create(Context, RawChars, StringLiteralKind::Wide, 3547 /*Pascal*/ false, ResTy, Loc); 3548 } else { 3549 ResTy = Context.adjustStringLiteralBaseType(Context.CharTy.withConst()); 3550 ResTy = Context.getConstantArrayType(ResTy, LengthI, nullptr, 3551 ArraySizeModifier::Normal, 3552 /*IndexTypeQuals*/ 0); 3553 SL = StringLiteral::Create(Context, Str, StringLiteralKind::Ordinary, 3554 /*Pascal*/ false, ResTy, Loc); 3555 } 3556 } 3557 3558 return PredefinedExpr::Create(Context, Loc, ResTy, IK, LangOpts.MicrosoftExt, 3559 SL); 3560 } 3561 3562 ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { 3563 return BuildPredefinedExpr(Loc, getPredefinedExprKind(Kind)); 3564 } 3565 3566 ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) { 3567 SmallString<16> CharBuffer; 3568 bool Invalid = false; 3569 StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); 3570 if (Invalid) 3571 return ExprError(); 3572 3573 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), 3574 PP, Tok.getKind()); 3575 if (Literal.hadError()) 3576 return ExprError(); 3577 3578 QualType Ty; 3579 if (Literal.isWide()) 3580 Ty = Context.WideCharTy; // L'x' -> wchar_t in C and C++. 3581 else if (Literal.isUTF8() && getLangOpts().C23) 3582 Ty = Context.UnsignedCharTy; // u8'x' -> unsigned char in C23 3583 else if (Literal.isUTF8() && getLangOpts().Char8) 3584 Ty = Context.Char8Ty; // u8'x' -> char8_t when it exists. 3585 else if (Literal.isUTF16()) 3586 Ty = Context.Char16Ty; // u'x' -> char16_t in C11 and C++11. 3587 else if (Literal.isUTF32()) 3588 Ty = Context.Char32Ty; // U'x' -> char32_t in C11 and C++11. 3589 else if (!getLangOpts().CPlusPlus || Literal.isMultiChar()) 3590 Ty = Context.IntTy; // 'x' -> int in C, 'wxyz' -> int in C++. 3591 else 3592 Ty = Context.CharTy; // 'x' -> char in C++; 3593 // u8'x' -> char in C11-C17 and in C++ without char8_t. 3594 3595 CharacterLiteralKind Kind = CharacterLiteralKind::Ascii; 3596 if (Literal.isWide()) 3597 Kind = CharacterLiteralKind::Wide; 3598 else if (Literal.isUTF16()) 3599 Kind = CharacterLiteralKind::UTF16; 3600 else if (Literal.isUTF32()) 3601 Kind = CharacterLiteralKind::UTF32; 3602 else if (Literal.isUTF8()) 3603 Kind = CharacterLiteralKind::UTF8; 3604 3605 Expr *Lit = new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, 3606 Tok.getLocation()); 3607 3608 if (Literal.getUDSuffix().empty()) 3609 return Lit; 3610 3611 // We're building a user-defined literal. 3612 IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); 3613 SourceLocation UDSuffixLoc = 3614 getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); 3615 3616 // Make sure we're allowed user-defined literals here. 3617 if (!UDLScope) 3618 return ExprError(Diag(UDSuffixLoc, diag::err_invalid_character_udl)); 3619 3620 // C++11 [lex.ext]p6: The literal L is treated as a call of the form 3621 // operator "" X (ch) 3622 return BuildCookedLiteralOperatorCall(*this, UDLScope, UDSuffix, UDSuffixLoc, 3623 Lit, Tok.getLocation()); 3624 } 3625 3626 ExprResult Sema::ActOnIntegerConstant(SourceLocation Loc, int64_t Val) { 3627 unsigned IntSize = Context.getTargetInfo().getIntWidth(); 3628 return IntegerLiteral::Create(Context, 3629 llvm::APInt(IntSize, Val, /*isSigned=*/true), 3630 Context.IntTy, Loc); 3631 } 3632 3633 static Expr *BuildFloatingLiteral(Sema &S, NumericLiteralParser &Literal, 3634 QualType Ty, SourceLocation Loc) { 3635 const llvm::fltSemantics &Format = S.Context.getFloatTypeSemantics(Ty); 3636 3637 using llvm::APFloat; 3638 APFloat Val(Format); 3639 3640 llvm::RoundingMode RM = S.CurFPFeatures.getRoundingMode(); 3641 if (RM == llvm::RoundingMode::Dynamic) 3642 RM = llvm::RoundingMode::NearestTiesToEven; 3643 APFloat::opStatus result = Literal.GetFloatValue(Val, RM); 3644 3645 // Overflow is always an error, but underflow is only an error if 3646 // we underflowed to zero (APFloat reports denormals as underflow). 3647 if ((result & APFloat::opOverflow) || 3648 ((result & APFloat::opUnderflow) && Val.isZero())) { 3649 unsigned diagnostic; 3650 SmallString<20> buffer; 3651 if (result & APFloat::opOverflow) { 3652 diagnostic = diag::warn_float_overflow; 3653 APFloat::getLargest(Format).toString(buffer); 3654 } else { 3655 diagnostic = diag::warn_float_underflow; 3656 APFloat::getSmallest(Format).toString(buffer); 3657 } 3658 3659 S.Diag(Loc, diagnostic) << Ty << buffer.str(); 3660 } 3661 3662 bool isExact = (result == APFloat::opOK); 3663 return FloatingLiteral::Create(S.Context, Val, isExact, Ty, Loc); 3664 } 3665 3666 bool Sema::CheckLoopHintExpr(Expr *E, SourceLocation Loc, bool AllowZero) { 3667 assert(E && "Invalid expression"); 3668 3669 if (E->isValueDependent()) 3670 return false; 3671 3672 QualType QT = E->getType(); 3673 if (!QT->isIntegerType() || QT->isBooleanType() || QT->isCharType()) { 3674 Diag(E->getExprLoc(), diag::err_pragma_loop_invalid_argument_type) << QT; 3675 return true; 3676 } 3677 3678 llvm::APSInt ValueAPS; 3679 ExprResult R = VerifyIntegerConstantExpression(E, &ValueAPS); 3680 3681 if (R.isInvalid()) 3682 return true; 3683 3684 // GCC allows the value of unroll count to be 0. 3685 // https://gcc.gnu.org/onlinedocs/gcc/Loop-Specific-Pragmas.html says 3686 // "The values of 0 and 1 block any unrolling of the loop." 3687 // The values doesn't have to be strictly positive in '#pragma GCC unroll' and 3688 // '#pragma unroll' cases. 3689 bool ValueIsPositive = 3690 AllowZero ? ValueAPS.isNonNegative() : ValueAPS.isStrictlyPositive(); 3691 if (!ValueIsPositive || ValueAPS.getActiveBits() > 31) { 3692 Diag(E->getExprLoc(), diag::err_requires_positive_value) 3693 << toString(ValueAPS, 10) << ValueIsPositive; 3694 return true; 3695 } 3696 3697 return false; 3698 } 3699 3700 ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { 3701 // Fast path for a single digit (which is quite common). A single digit 3702 // cannot have a trigraph, escaped newline, radix prefix, or suffix. 3703 if (Tok.getLength() == 1 || Tok.getKind() == tok::binary_data) { 3704 const uint8_t Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); 3705 return ActOnIntegerConstant(Tok.getLocation(), Val); 3706 } 3707 3708 SmallString<128> SpellingBuffer; 3709 // NumericLiteralParser wants to overread by one character. Add padding to 3710 // the buffer in case the token is copied to the buffer. If getSpelling() 3711 // returns a StringRef to the memory buffer, it should have a null char at 3712 // the EOF, so it is also safe. 3713 SpellingBuffer.resize(Tok.getLength() + 1); 3714 3715 // Get the spelling of the token, which eliminates trigraphs, etc. 3716 bool Invalid = false; 3717 StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid); 3718 if (Invalid) 3719 return ExprError(); 3720 3721 NumericLiteralParser Literal(TokSpelling, Tok.getLocation(), 3722 PP.getSourceManager(), PP.getLangOpts(), 3723 PP.getTargetInfo(), PP.getDiagnostics()); 3724 if (Literal.hadError) 3725 return ExprError(); 3726 3727 if (Literal.hasUDSuffix()) { 3728 // We're building a user-defined literal. 3729 const IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); 3730 SourceLocation UDSuffixLoc = 3731 getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); 3732 3733 // Make sure we're allowed user-defined literals here. 3734 if (!UDLScope) 3735 return ExprError(Diag(UDSuffixLoc, diag::err_invalid_numeric_udl)); 3736 3737 QualType CookedTy; 3738 if (Literal.isFloatingLiteral()) { 3739 // C++11 [lex.ext]p4: If S contains a literal operator with parameter type 3740 // long double, the literal is treated as a call of the form 3741 // operator "" X (f L) 3742 CookedTy = Context.LongDoubleTy; 3743 } else { 3744 // C++11 [lex.ext]p3: If S contains a literal operator with parameter type 3745 // unsigned long long, the literal is treated as a call of the form 3746 // operator "" X (n ULL) 3747 CookedTy = Context.UnsignedLongLongTy; 3748 } 3749 3750 DeclarationName OpName = 3751 Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); 3752 DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); 3753 OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); 3754 3755 SourceLocation TokLoc = Tok.getLocation(); 3756 3757 // Perform literal operator lookup to determine if we're building a raw 3758 // literal or a cooked one. 3759 LookupResult R(*this, OpName, UDSuffixLoc, LookupOrdinaryName); 3760 switch (LookupLiteralOperator(UDLScope, R, CookedTy, 3761 /*AllowRaw*/ true, /*AllowTemplate*/ true, 3762 /*AllowStringTemplatePack*/ false, 3763 /*DiagnoseMissing*/ !Literal.isImaginary)) { 3764 case LOLR_ErrorNoDiagnostic: 3765 // Lookup failure for imaginary constants isn't fatal, there's still the 3766 // GNU extension producing _Complex types. 3767 break; 3768 case LOLR_Error: 3769 return ExprError(); 3770 case LOLR_Cooked: { 3771 Expr *Lit; 3772 if (Literal.isFloatingLiteral()) { 3773 Lit = BuildFloatingLiteral(*this, Literal, CookedTy, Tok.getLocation()); 3774 } else { 3775 llvm::APInt ResultVal(Context.getTargetInfo().getLongLongWidth(), 0); 3776 if (Literal.GetIntegerValue(ResultVal)) 3777 Diag(Tok.getLocation(), diag::err_integer_literal_too_large) 3778 << /* Unsigned */ 1; 3779 Lit = IntegerLiteral::Create(Context, ResultVal, CookedTy, 3780 Tok.getLocation()); 3781 } 3782 return BuildLiteralOperatorCall(R, OpNameInfo, Lit, TokLoc); 3783 } 3784 3785 case LOLR_Raw: { 3786 // C++11 [lit.ext]p3, p4: If S contains a raw literal operator, the 3787 // literal is treated as a call of the form 3788 // operator "" X ("n") 3789 unsigned Length = Literal.getUDSuffixOffset(); 3790 QualType StrTy = Context.getConstantArrayType( 3791 Context.adjustStringLiteralBaseType(Context.CharTy.withConst()), 3792 llvm::APInt(32, Length + 1), nullptr, ArraySizeModifier::Normal, 0); 3793 Expr *Lit = 3794 StringLiteral::Create(Context, StringRef(TokSpelling.data(), Length), 3795 StringLiteralKind::Ordinary, 3796 /*Pascal*/ false, StrTy, TokLoc); 3797 return BuildLiteralOperatorCall(R, OpNameInfo, Lit, TokLoc); 3798 } 3799 3800 case LOLR_Template: { 3801 // C++11 [lit.ext]p3, p4: Otherwise (S contains a literal operator 3802 // template), L is treated as a call fo the form 3803 // operator "" X <'c1', 'c2', ... 'ck'>() 3804 // where n is the source character sequence c1 c2 ... ck. 3805 TemplateArgumentListInfo ExplicitArgs; 3806 unsigned CharBits = Context.getIntWidth(Context.CharTy); 3807 bool CharIsUnsigned = Context.CharTy->isUnsignedIntegerType(); 3808 llvm::APSInt Value(CharBits, CharIsUnsigned); 3809 for (unsigned I = 0, N = Literal.getUDSuffixOffset(); I != N; ++I) { 3810 Value = TokSpelling[I]; 3811 TemplateArgument Arg(Context, Value, Context.CharTy); 3812 TemplateArgumentLocInfo ArgInfo; 3813 ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); 3814 } 3815 return BuildLiteralOperatorCall(R, OpNameInfo, {}, TokLoc, &ExplicitArgs); 3816 } 3817 case LOLR_StringTemplatePack: 3818 llvm_unreachable("unexpected literal operator lookup result"); 3819 } 3820 } 3821 3822 Expr *Res; 3823 3824 if (Literal.isFixedPointLiteral()) { 3825 QualType Ty; 3826 3827 if (Literal.isAccum) { 3828 if (Literal.isHalf) { 3829 Ty = Context.ShortAccumTy; 3830 } else if (Literal.isLong) { 3831 Ty = Context.LongAccumTy; 3832 } else { 3833 Ty = Context.AccumTy; 3834 } 3835 } else if (Literal.isFract) { 3836 if (Literal.isHalf) { 3837 Ty = Context.ShortFractTy; 3838 } else if (Literal.isLong) { 3839 Ty = Context.LongFractTy; 3840 } else { 3841 Ty = Context.FractTy; 3842 } 3843 } 3844 3845 if (Literal.isUnsigned) Ty = Context.getCorrespondingUnsignedType(Ty); 3846 3847 bool isSigned = !Literal.isUnsigned; 3848 unsigned scale = Context.getFixedPointScale(Ty); 3849 unsigned bit_width = Context.getTypeInfo(Ty).Width; 3850 3851 llvm::APInt Val(bit_width, 0, isSigned); 3852 bool Overflowed = Literal.GetFixedPointValue(Val, scale); 3853 bool ValIsZero = Val.isZero() && !Overflowed; 3854 3855 auto MaxVal = Context.getFixedPointMax(Ty).getValue(); 3856 if (Literal.isFract && Val == MaxVal + 1 && !ValIsZero) 3857 // Clause 6.4.4 - The value of a constant shall be in the range of 3858 // representable values for its type, with exception for constants of a 3859 // fract type with a value of exactly 1; such a constant shall denote 3860 // the maximal value for the type. 3861 --Val; 3862 else if (Val.ugt(MaxVal) || Overflowed) 3863 Diag(Tok.getLocation(), diag::err_too_large_for_fixed_point); 3864 3865 Res = FixedPointLiteral::CreateFromRawInt(Context, Val, Ty, 3866 Tok.getLocation(), scale); 3867 } else if (Literal.isFloatingLiteral()) { 3868 QualType Ty; 3869 if (Literal.isHalf){ 3870 if (getLangOpts().HLSL || 3871 getOpenCLOptions().isAvailableOption("cl_khr_fp16", getLangOpts())) 3872 Ty = Context.HalfTy; 3873 else { 3874 Diag(Tok.getLocation(), diag::err_half_const_requires_fp16); 3875 return ExprError(); 3876 } 3877 } else if (Literal.isFloat) 3878 Ty = Context.FloatTy; 3879 else if (Literal.isLong) 3880 Ty = !getLangOpts().HLSL ? Context.LongDoubleTy : Context.DoubleTy; 3881 else if (Literal.isFloat16) 3882 Ty = Context.Float16Ty; 3883 else if (Literal.isFloat128) 3884 Ty = Context.Float128Ty; 3885 else if (getLangOpts().HLSL) 3886 Ty = Context.FloatTy; 3887 else 3888 Ty = Context.DoubleTy; 3889 3890 Res = BuildFloatingLiteral(*this, Literal, Ty, Tok.getLocation()); 3891 3892 if (Ty == Context.DoubleTy) { 3893 if (getLangOpts().SinglePrecisionConstants) { 3894 if (Ty->castAs<BuiltinType>()->getKind() != BuiltinType::Float) { 3895 Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).get(); 3896 } 3897 } else if (getLangOpts().OpenCL && !getOpenCLOptions().isAvailableOption( 3898 "cl_khr_fp64", getLangOpts())) { 3899 // Impose single-precision float type when cl_khr_fp64 is not enabled. 3900 Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64) 3901 << (getLangOpts().getOpenCLCompatibleVersion() >= 300); 3902 Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).get(); 3903 } 3904 } 3905 } else if (!Literal.isIntegerLiteral()) { 3906 return ExprError(); 3907 } else { 3908 QualType Ty; 3909 3910 // 'z/uz' literals are a C++23 feature. 3911 if (Literal.isSizeT) 3912 Diag(Tok.getLocation(), getLangOpts().CPlusPlus 3913 ? getLangOpts().CPlusPlus23 3914 ? diag::warn_cxx20_compat_size_t_suffix 3915 : diag::ext_cxx23_size_t_suffix 3916 : diag::err_cxx23_size_t_suffix); 3917 3918 // 'wb/uwb' literals are a C23 feature. We support _BitInt as a type in C++, 3919 // but we do not currently support the suffix in C++ mode because it's not 3920 // entirely clear whether WG21 will prefer this suffix to return a library 3921 // type such as std::bit_int instead of returning a _BitInt. '__wb/__uwb' 3922 // literals are a C++ extension. 3923 if (Literal.isBitInt) 3924 PP.Diag(Tok.getLocation(), 3925 getLangOpts().CPlusPlus ? diag::ext_cxx_bitint_suffix 3926 : getLangOpts().C23 ? diag::warn_c23_compat_bitint_suffix 3927 : diag::ext_c23_bitint_suffix); 3928 3929 // Get the value in the widest-possible width. What is "widest" depends on 3930 // whether the literal is a bit-precise integer or not. For a bit-precise 3931 // integer type, try to scan the source to determine how many bits are 3932 // needed to represent the value. This may seem a bit expensive, but trying 3933 // to get the integer value from an overly-wide APInt is *extremely* 3934 // expensive, so the naive approach of assuming 3935 // llvm::IntegerType::MAX_INT_BITS is a big performance hit. 3936 unsigned BitsNeeded = Context.getTargetInfo().getIntMaxTWidth(); 3937 if (Literal.isBitInt) 3938 BitsNeeded = llvm::APInt::getSufficientBitsNeeded( 3939 Literal.getLiteralDigits(), Literal.getRadix()); 3940 if (Literal.MicrosoftInteger) { 3941 if (Literal.MicrosoftInteger == 128 && 3942 !Context.getTargetInfo().hasInt128Type()) 3943 PP.Diag(Tok.getLocation(), diag::err_integer_literal_too_large) 3944 << Literal.isUnsigned; 3945 BitsNeeded = Literal.MicrosoftInteger; 3946 } 3947 3948 llvm::APInt ResultVal(BitsNeeded, 0); 3949 3950 if (Literal.GetIntegerValue(ResultVal)) { 3951 // If this value didn't fit into uintmax_t, error and force to ull. 3952 Diag(Tok.getLocation(), diag::err_integer_literal_too_large) 3953 << /* Unsigned */ 1; 3954 Ty = Context.UnsignedLongLongTy; 3955 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && 3956 "long long is not intmax_t?"); 3957 } else { 3958 // If this value fits into a ULL, try to figure out what else it fits into 3959 // according to the rules of C99 6.4.4.1p5. 3960 3961 // Octal, Hexadecimal, and integers with a U suffix are allowed to 3962 // be an unsigned int. 3963 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; 3964 3965 // HLSL doesn't really have `long` or `long long`. We support the `ll` 3966 // suffix for portability of code with C++, but both `l` and `ll` are 3967 // 64-bit integer types, and we want the type of `1l` and `1ll` to be the 3968 // same. 3969 if (getLangOpts().HLSL && !Literal.isLong && Literal.isLongLong) { 3970 Literal.isLong = true; 3971 Literal.isLongLong = false; 3972 } 3973 3974 // Check from smallest to largest, picking the smallest type we can. 3975 unsigned Width = 0; 3976 3977 // Microsoft specific integer suffixes are explicitly sized. 3978 if (Literal.MicrosoftInteger) { 3979 if (Literal.MicrosoftInteger == 8 && !Literal.isUnsigned) { 3980 Width = 8; 3981 Ty = Context.CharTy; 3982 } else { 3983 Width = Literal.MicrosoftInteger; 3984 Ty = Context.getIntTypeForBitwidth(Width, 3985 /*Signed=*/!Literal.isUnsigned); 3986 } 3987 } 3988 3989 // Bit-precise integer literals are automagically-sized based on the 3990 // width required by the literal. 3991 if (Literal.isBitInt) { 3992 // The signed version has one more bit for the sign value. There are no 3993 // zero-width bit-precise integers, even if the literal value is 0. 3994 Width = std::max(ResultVal.getActiveBits(), 1u) + 3995 (Literal.isUnsigned ? 0u : 1u); 3996 3997 // Diagnose if the width of the constant is larger than BITINT_MAXWIDTH, 3998 // and reset the type to the largest supported width. 3999 unsigned int MaxBitIntWidth = 4000 Context.getTargetInfo().getMaxBitIntWidth(); 4001 if (Width > MaxBitIntWidth) { 4002 Diag(Tok.getLocation(), diag::err_integer_literal_too_large) 4003 << Literal.isUnsigned; 4004 Width = MaxBitIntWidth; 4005 } 4006 4007 // Reset the result value to the smaller APInt and select the correct 4008 // type to be used. Note, we zext even for signed values because the 4009 // literal itself is always an unsigned value (a preceeding - is a 4010 // unary operator, not part of the literal). 4011 ResultVal = ResultVal.zextOrTrunc(Width); 4012 Ty = Context.getBitIntType(Literal.isUnsigned, Width); 4013 } 4014 4015 // Check C++23 size_t literals. 4016 if (Literal.isSizeT) { 4017 assert(!Literal.MicrosoftInteger && 4018 "size_t literals can't be Microsoft literals"); 4019 unsigned SizeTSize = Context.getTargetInfo().getTypeWidth( 4020 Context.getTargetInfo().getSizeType()); 4021 4022 // Does it fit in size_t? 4023 if (ResultVal.isIntN(SizeTSize)) { 4024 // Does it fit in ssize_t? 4025 if (!Literal.isUnsigned && ResultVal[SizeTSize - 1] == 0) 4026 Ty = Context.getSignedSizeType(); 4027 else if (AllowUnsigned) 4028 Ty = Context.getSizeType(); 4029 Width = SizeTSize; 4030 } 4031 } 4032 4033 if (Ty.isNull() && !Literal.isLong && !Literal.isLongLong && 4034 !Literal.isSizeT) { 4035 // Are int/unsigned possibilities? 4036 unsigned IntSize = Context.getTargetInfo().getIntWidth(); 4037 4038 // Does it fit in a unsigned int? 4039 if (ResultVal.isIntN(IntSize)) { 4040 // Does it fit in a signed int? 4041 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) 4042 Ty = Context.IntTy; 4043 else if (AllowUnsigned) 4044 Ty = Context.UnsignedIntTy; 4045 Width = IntSize; 4046 } 4047 } 4048 4049 // Are long/unsigned long possibilities? 4050 if (Ty.isNull() && !Literal.isLongLong && !Literal.isSizeT) { 4051 unsigned LongSize = Context.getTargetInfo().getLongWidth(); 4052 4053 // Does it fit in a unsigned long? 4054 if (ResultVal.isIntN(LongSize)) { 4055 // Does it fit in a signed long? 4056 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) 4057 Ty = Context.LongTy; 4058 else if (AllowUnsigned) 4059 Ty = Context.UnsignedLongTy; 4060 // Check according to the rules of C90 6.1.3.2p5. C++03 [lex.icon]p2 4061 // is compatible. 4062 else if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) { 4063 const unsigned LongLongSize = 4064 Context.getTargetInfo().getLongLongWidth(); 4065 Diag(Tok.getLocation(), 4066 getLangOpts().CPlusPlus 4067 ? Literal.isLong 4068 ? diag::warn_old_implicitly_unsigned_long_cxx 4069 : /*C++98 UB*/ diag:: 4070 ext_old_implicitly_unsigned_long_cxx 4071 : diag::warn_old_implicitly_unsigned_long) 4072 << (LongLongSize > LongSize ? /*will have type 'long long'*/ 0 4073 : /*will be ill-formed*/ 1); 4074 Ty = Context.UnsignedLongTy; 4075 } 4076 Width = LongSize; 4077 } 4078 } 4079 4080 // Check long long if needed. 4081 if (Ty.isNull() && !Literal.isSizeT) { 4082 unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); 4083 4084 // Does it fit in a unsigned long long? 4085 if (ResultVal.isIntN(LongLongSize)) { 4086 // Does it fit in a signed long long? 4087 // To be compatible with MSVC, hex integer literals ending with the 4088 // LL or i64 suffix are always signed in Microsoft mode. 4089 if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || 4090 (getLangOpts().MSVCCompat && Literal.isLongLong))) 4091 Ty = Context.LongLongTy; 4092 else if (AllowUnsigned) 4093 Ty = Context.UnsignedLongLongTy; 4094 Width = LongLongSize; 4095 4096 // 'long long' is a C99 or C++11 feature, whether the literal 4097 // explicitly specified 'long long' or we needed the extra width. 4098 if (getLangOpts().CPlusPlus) 4099 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 4100 ? diag::warn_cxx98_compat_longlong 4101 : diag::ext_cxx11_longlong); 4102 else if (!getLangOpts().C99) 4103 Diag(Tok.getLocation(), diag::ext_c99_longlong); 4104 } 4105 } 4106 4107 // If we still couldn't decide a type, we either have 'size_t' literal 4108 // that is out of range, or a decimal literal that does not fit in a 4109 // signed long long and has no U suffix. 4110 if (Ty.isNull()) { 4111 if (Literal.isSizeT) 4112 Diag(Tok.getLocation(), diag::err_size_t_literal_too_large) 4113 << Literal.isUnsigned; 4114 else 4115 Diag(Tok.getLocation(), 4116 diag::ext_integer_literal_too_large_for_signed); 4117 Ty = Context.UnsignedLongLongTy; 4118 Width = Context.getTargetInfo().getLongLongWidth(); 4119 } 4120 4121 if (ResultVal.getBitWidth() != Width) 4122 ResultVal = ResultVal.trunc(Width); 4123 } 4124 Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); 4125 } 4126 4127 // If this is an imaginary literal, create the ImaginaryLiteral wrapper. 4128 if (Literal.isImaginary) { 4129 Res = new (Context) ImaginaryLiteral(Res, 4130 Context.getComplexType(Res->getType())); 4131 4132 // In C++, this is a GNU extension. In C, it's a C2y extension. 4133 unsigned DiagId; 4134 if (getLangOpts().CPlusPlus) 4135 DiagId = diag::ext_gnu_imaginary_constant; 4136 else if (getLangOpts().C2y) 4137 DiagId = diag::warn_c23_compat_imaginary_constant; 4138 else 4139 DiagId = diag::ext_c2y_imaginary_constant; 4140 Diag(Tok.getLocation(), DiagId); 4141 } 4142 return Res; 4143 } 4144 4145 ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { 4146 assert(E && "ActOnParenExpr() missing expr"); 4147 QualType ExprTy = E->getType(); 4148 if (getLangOpts().ProtectParens && CurFPFeatures.getAllowFPReassociate() && 4149 !E->isLValue() && ExprTy->hasFloatingRepresentation()) 4150 return BuildBuiltinCallExpr(R, Builtin::BI__arithmetic_fence, E); 4151 return new (Context) ParenExpr(L, R, E); 4152 } 4153 4154 static bool CheckVecStepTraitOperandType(Sema &S, QualType T, 4155 SourceLocation Loc, 4156 SourceRange ArgRange) { 4157 // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in 4158 // scalar or vector data type argument..." 4159 // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic 4160 // type (C99 6.2.5p18) or void. 4161 if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { 4162 S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) 4163 << T << ArgRange; 4164 return true; 4165 } 4166 4167 assert((T->isVoidType() || !T->isIncompleteType()) && 4168 "Scalar types should always be complete"); 4169 return false; 4170 } 4171 4172 static bool CheckVectorElementsTraitOperandType(Sema &S, QualType T, 4173 SourceLocation Loc, 4174 SourceRange ArgRange) { 4175 // builtin_vectorelements supports both fixed-sized and scalable vectors. 4176 if (!T->isVectorType() && !T->isSizelessVectorType()) 4177 return S.Diag(Loc, diag::err_builtin_non_vector_type) 4178 << "" 4179 << "__builtin_vectorelements" << T << ArgRange; 4180 4181 return false; 4182 } 4183 4184 static bool checkPtrAuthTypeDiscriminatorOperandType(Sema &S, QualType T, 4185 SourceLocation Loc, 4186 SourceRange ArgRange) { 4187 if (S.checkPointerAuthEnabled(Loc, ArgRange)) 4188 return true; 4189 4190 if (!T->isFunctionType() && !T->isFunctionPointerType() && 4191 !T->isFunctionReferenceType() && !T->isMemberFunctionPointerType()) { 4192 S.Diag(Loc, diag::err_ptrauth_type_disc_undiscriminated) << T << ArgRange; 4193 return true; 4194 } 4195 4196 return false; 4197 } 4198 4199 static bool CheckExtensionTraitOperandType(Sema &S, QualType T, 4200 SourceLocation Loc, 4201 SourceRange ArgRange, 4202 UnaryExprOrTypeTrait TraitKind) { 4203 // Invalid types must be hard errors for SFINAE in C++. 4204 if (S.LangOpts.CPlusPlus) 4205 return true; 4206 4207 // C99 6.5.3.4p1: 4208 if (T->isFunctionType() && 4209 (TraitKind == UETT_SizeOf || TraitKind == UETT_AlignOf || 4210 TraitKind == UETT_PreferredAlignOf)) { 4211 // sizeof(function)/alignof(function) is allowed as an extension. 4212 S.Diag(Loc, diag::ext_sizeof_alignof_function_type) 4213 << getTraitSpelling(TraitKind) << ArgRange; 4214 return false; 4215 } 4216 4217 // Allow sizeof(void)/alignof(void) as an extension, unless in OpenCL where 4218 // this is an error (OpenCL v1.1 s6.3.k) 4219 if (T->isVoidType()) { 4220 unsigned DiagID = S.LangOpts.OpenCL ? diag::err_opencl_sizeof_alignof_type 4221 : diag::ext_sizeof_alignof_void_type; 4222 S.Diag(Loc, DiagID) << getTraitSpelling(TraitKind) << ArgRange; 4223 return false; 4224 } 4225 4226 return true; 4227 } 4228 4229 static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, 4230 SourceLocation Loc, 4231 SourceRange ArgRange, 4232 UnaryExprOrTypeTrait TraitKind) { 4233 // Reject sizeof(interface) and sizeof(interface<proto>) if the 4234 // runtime doesn't allow it. 4235 if (!S.LangOpts.ObjCRuntime.allowsSizeofAlignof() && T->isObjCObjectType()) { 4236 S.Diag(Loc, diag::err_sizeof_nonfragile_interface) 4237 << T << (TraitKind == UETT_SizeOf) 4238 << ArgRange; 4239 return true; 4240 } 4241 4242 return false; 4243 } 4244 4245 /// Check whether E is a pointer from a decayed array type (the decayed 4246 /// pointer type is equal to T) and emit a warning if it is. 4247 static void warnOnSizeofOnArrayDecay(Sema &S, SourceLocation Loc, QualType T, 4248 const Expr *E) { 4249 // Don't warn if the operation changed the type. 4250 if (T != E->getType()) 4251 return; 4252 4253 // Now look for array decays. 4254 const auto *ICE = dyn_cast<ImplicitCastExpr>(E); 4255 if (!ICE || ICE->getCastKind() != CK_ArrayToPointerDecay) 4256 return; 4257 4258 S.Diag(Loc, diag::warn_sizeof_array_decay) << ICE->getSourceRange() 4259 << ICE->getType() 4260 << ICE->getSubExpr()->getType(); 4261 } 4262 4263 bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, 4264 UnaryExprOrTypeTrait ExprKind) { 4265 QualType ExprTy = E->getType(); 4266 assert(!ExprTy->isReferenceType()); 4267 4268 bool IsUnevaluatedOperand = 4269 (ExprKind == UETT_SizeOf || ExprKind == UETT_DataSizeOf || 4270 ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf || 4271 ExprKind == UETT_VecStep || ExprKind == UETT_CountOf); 4272 if (IsUnevaluatedOperand) { 4273 ExprResult Result = CheckUnevaluatedOperand(E); 4274 if (Result.isInvalid()) 4275 return true; 4276 E = Result.get(); 4277 } 4278 4279 // The operand for sizeof and alignof is in an unevaluated expression context, 4280 // so side effects could result in unintended consequences. 4281 // Exclude instantiation-dependent expressions, because 'sizeof' is sometimes 4282 // used to build SFINAE gadgets. 4283 // FIXME: Should we consider instantiation-dependent operands to 'alignof'? 4284 if (IsUnevaluatedOperand && !inTemplateInstantiation() && 4285 !E->isInstantiationDependent() && 4286 !E->getType()->isVariableArrayType() && 4287 E->HasSideEffects(Context, false)) 4288 Diag(E->getExprLoc(), diag::warn_side_effects_unevaluated_context); 4289 4290 if (ExprKind == UETT_VecStep) 4291 return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(), 4292 E->getSourceRange()); 4293 4294 if (ExprKind == UETT_VectorElements) 4295 return CheckVectorElementsTraitOperandType(*this, ExprTy, E->getExprLoc(), 4296 E->getSourceRange()); 4297 4298 // Explicitly list some types as extensions. 4299 if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(), 4300 E->getSourceRange(), ExprKind)) 4301 return false; 4302 4303 // WebAssembly tables are always illegal operands to unary expressions and 4304 // type traits. 4305 if (Context.getTargetInfo().getTriple().isWasm() && 4306 E->getType()->isWebAssemblyTableType()) { 4307 Diag(E->getExprLoc(), diag::err_wasm_table_invalid_uett_operand) 4308 << getTraitSpelling(ExprKind); 4309 return true; 4310 } 4311 4312 // 'alignof' applied to an expression only requires the base element type of 4313 // the expression to be complete. 'sizeof' requires the expression's type to 4314 // be complete (and will attempt to complete it if it's an array of unknown 4315 // bound). 4316 if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf) { 4317 if (RequireCompleteSizedType( 4318 E->getExprLoc(), Context.getBaseElementType(E->getType()), 4319 diag::err_sizeof_alignof_incomplete_or_sizeless_type, 4320 getTraitSpelling(ExprKind), E->getSourceRange())) 4321 return true; 4322 } else { 4323 if (RequireCompleteSizedExprType( 4324 E, diag::err_sizeof_alignof_incomplete_or_sizeless_type, 4325 getTraitSpelling(ExprKind), E->getSourceRange())) 4326 return true; 4327 } 4328 4329 // Completing the expression's type may have changed it. 4330 ExprTy = E->getType(); 4331 assert(!ExprTy->isReferenceType()); 4332 4333 if (ExprTy->isFunctionType()) { 4334 Diag(E->getExprLoc(), diag::err_sizeof_alignof_function_type) 4335 << getTraitSpelling(ExprKind) << E->getSourceRange(); 4336 return true; 4337 } 4338 4339 if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), 4340 E->getSourceRange(), ExprKind)) 4341 return true; 4342 4343 if (ExprKind == UETT_CountOf) { 4344 // The type has to be an array type. We already checked for incomplete 4345 // types above. 4346 QualType ExprType = E->IgnoreParens()->getType(); 4347 if (!ExprType->isArrayType()) { 4348 Diag(E->getExprLoc(), diag::err_countof_arg_not_array_type) << ExprType; 4349 return true; 4350 } 4351 // FIXME: warn on _Countof on an array parameter. Not warning on it 4352 // currently because there are papers in WG14 about array types which do 4353 // not decay that could impact this behavior, so we want to see if anything 4354 // changes here before coming up with a warning group for _Countof-related 4355 // diagnostics. 4356 } 4357 4358 if (ExprKind == UETT_SizeOf) { 4359 if (const auto *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { 4360 if (const auto *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { 4361 QualType OType = PVD->getOriginalType(); 4362 QualType Type = PVD->getType(); 4363 if (Type->isPointerType() && OType->isArrayType()) { 4364 Diag(E->getExprLoc(), diag::warn_sizeof_array_param) 4365 << Type << OType; 4366 Diag(PVD->getLocation(), diag::note_declared_at); 4367 } 4368 } 4369 } 4370 4371 // Warn on "sizeof(array op x)" and "sizeof(x op array)", where the array 4372 // decays into a pointer and returns an unintended result. This is most 4373 // likely a typo for "sizeof(array) op x". 4374 if (const auto *BO = dyn_cast<BinaryOperator>(E->IgnoreParens())) { 4375 warnOnSizeofOnArrayDecay(*this, BO->getOperatorLoc(), BO->getType(), 4376 BO->getLHS()); 4377 warnOnSizeofOnArrayDecay(*this, BO->getOperatorLoc(), BO->getType(), 4378 BO->getRHS()); 4379 } 4380 } 4381 4382 return false; 4383 } 4384 4385 static bool CheckAlignOfExpr(Sema &S, Expr *E, UnaryExprOrTypeTrait ExprKind) { 4386 // Cannot know anything else if the expression is dependent. 4387 if (E->isTypeDependent()) 4388 return false; 4389 4390 if (E->getObjectKind() == OK_BitField) { 4391 S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_typeof_bitfield) 4392 << 1 << E->getSourceRange(); 4393 return true; 4394 } 4395 4396 ValueDecl *D = nullptr; 4397 Expr *Inner = E->IgnoreParens(); 4398 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Inner)) { 4399 D = DRE->getDecl(); 4400 } else if (MemberExpr *ME = dyn_cast<MemberExpr>(Inner)) { 4401 D = ME->getMemberDecl(); 4402 } 4403 4404 // If it's a field, require the containing struct to have a 4405 // complete definition so that we can compute the layout. 4406 // 4407 // This can happen in C++11 onwards, either by naming the member 4408 // in a way that is not transformed into a member access expression 4409 // (in an unevaluated operand, for instance), or by naming the member 4410 // in a trailing-return-type. 4411 // 4412 // For the record, since __alignof__ on expressions is a GCC 4413 // extension, GCC seems to permit this but always gives the 4414 // nonsensical answer 0. 4415 // 4416 // We don't really need the layout here --- we could instead just 4417 // directly check for all the appropriate alignment-lowing 4418 // attributes --- but that would require duplicating a lot of 4419 // logic that just isn't worth duplicating for such a marginal 4420 // use-case. 4421 if (FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) { 4422 // Fast path this check, since we at least know the record has a 4423 // definition if we can find a member of it. 4424 if (!FD->getParent()->isCompleteDefinition()) { 4425 S.Diag(E->getExprLoc(), diag::err_alignof_member_of_incomplete_type) 4426 << E->getSourceRange(); 4427 return true; 4428 } 4429 4430 // Otherwise, if it's a field, and the field doesn't have 4431 // reference type, then it must have a complete type (or be a 4432 // flexible array member, which we explicitly want to 4433 // white-list anyway), which makes the following checks trivial. 4434 if (!FD->getType()->isReferenceType()) 4435 return false; 4436 } 4437 4438 return S.CheckUnaryExprOrTypeTraitOperand(E, ExprKind); 4439 } 4440 4441 bool Sema::CheckVecStepExpr(Expr *E) { 4442 E = E->IgnoreParens(); 4443 4444 // Cannot know anything else if the expression is dependent. 4445 if (E->isTypeDependent()) 4446 return false; 4447 4448 return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); 4449 } 4450 4451 static void captureVariablyModifiedType(ASTContext &Context, QualType T, 4452 CapturingScopeInfo *CSI) { 4453 assert(T->isVariablyModifiedType()); 4454 assert(CSI != nullptr); 4455 4456 // We're going to walk down into the type and look for VLA expressions. 4457 do { 4458 const Type *Ty = T.getTypePtr(); 4459 switch (Ty->getTypeClass()) { 4460 #define TYPE(Class, Base) 4461 #define ABSTRACT_TYPE(Class, Base) 4462 #define NON_CANONICAL_TYPE(Class, Base) 4463 #define DEPENDENT_TYPE(Class, Base) case Type::Class: 4464 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) 4465 #include "clang/AST/TypeNodes.inc" 4466 T = QualType(); 4467 break; 4468 // These types are never variably-modified. 4469 case Type::Builtin: 4470 case Type::Complex: 4471 case Type::Vector: 4472 case Type::ExtVector: 4473 case Type::ConstantMatrix: 4474 case Type::Record: 4475 case Type::Enum: 4476 case Type::TemplateSpecialization: 4477 case Type::ObjCObject: 4478 case Type::ObjCInterface: 4479 case Type::ObjCObjectPointer: 4480 case Type::ObjCTypeParam: 4481 case Type::Pipe: 4482 case Type::BitInt: 4483 case Type::HLSLInlineSpirv: 4484 llvm_unreachable("type class is never variably-modified!"); 4485 case Type::Elaborated: 4486 T = cast<ElaboratedType>(Ty)->getNamedType(); 4487 break; 4488 case Type::Adjusted: 4489 T = cast<AdjustedType>(Ty)->getOriginalType(); 4490 break; 4491 case Type::Decayed: 4492 T = cast<DecayedType>(Ty)->getPointeeType(); 4493 break; 4494 case Type::ArrayParameter: 4495 T = cast<ArrayParameterType>(Ty)->getElementType(); 4496 break; 4497 case Type::Pointer: 4498 T = cast<PointerType>(Ty)->getPointeeType(); 4499 break; 4500 case Type::BlockPointer: 4501 T = cast<BlockPointerType>(Ty)->getPointeeType(); 4502 break; 4503 case Type::LValueReference: 4504 case Type::RValueReference: 4505 T = cast<ReferenceType>(Ty)->getPointeeType(); 4506 break; 4507 case Type::MemberPointer: 4508 T = cast<MemberPointerType>(Ty)->getPointeeType(); 4509 break; 4510 case Type::ConstantArray: 4511 case Type::IncompleteArray: 4512 // Losing element qualification here is fine. 4513 T = cast<ArrayType>(Ty)->getElementType(); 4514 break; 4515 case Type::VariableArray: { 4516 // Losing element qualification here is fine. 4517 const VariableArrayType *VAT = cast<VariableArrayType>(Ty); 4518 4519 // Unknown size indication requires no size computation. 4520 // Otherwise, evaluate and record it. 4521 auto Size = VAT->getSizeExpr(); 4522 if (Size && !CSI->isVLATypeCaptured(VAT) && 4523 (isa<CapturedRegionScopeInfo>(CSI) || isa<LambdaScopeInfo>(CSI))) 4524 CSI->addVLATypeCapture(Size->getExprLoc(), VAT, Context.getSizeType()); 4525 4526 T = VAT->getElementType(); 4527 break; 4528 } 4529 case Type::FunctionProto: 4530 case Type::FunctionNoProto: 4531 T = cast<FunctionType>(Ty)->getReturnType(); 4532 break; 4533 case Type::Paren: 4534 case Type::TypeOf: 4535 case Type::UnaryTransform: 4536 case Type::Attributed: 4537 case Type::BTFTagAttributed: 4538 case Type::HLSLAttributedResource: 4539 case Type::SubstTemplateTypeParm: 4540 case Type::MacroQualified: 4541 case Type::CountAttributed: 4542 // Keep walking after single level desugaring. 4543 T = T.getSingleStepDesugaredType(Context); 4544 break; 4545 case Type::Typedef: 4546 T = cast<TypedefType>(Ty)->desugar(); 4547 break; 4548 case Type::Decltype: 4549 T = cast<DecltypeType>(Ty)->desugar(); 4550 break; 4551 case Type::PackIndexing: 4552 T = cast<PackIndexingType>(Ty)->desugar(); 4553 break; 4554 case Type::Using: 4555 T = cast<UsingType>(Ty)->desugar(); 4556 break; 4557 case Type::Auto: 4558 case Type::DeducedTemplateSpecialization: 4559 T = cast<DeducedType>(Ty)->getDeducedType(); 4560 break; 4561 case Type::TypeOfExpr: 4562 T = cast<TypeOfExprType>(Ty)->getUnderlyingExpr()->getType(); 4563 break; 4564 case Type::Atomic: 4565 T = cast<AtomicType>(Ty)->getValueType(); 4566 break; 4567 } 4568 } while (!T.isNull() && T->isVariablyModifiedType()); 4569 } 4570 4571 bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, 4572 SourceLocation OpLoc, 4573 SourceRange ExprRange, 4574 UnaryExprOrTypeTrait ExprKind, 4575 StringRef KWName) { 4576 if (ExprType->isDependentType()) 4577 return false; 4578 4579 // C++ [expr.sizeof]p2: 4580 // When applied to a reference or a reference type, the result 4581 // is the size of the referenced type. 4582 // C++11 [expr.alignof]p3: 4583 // When alignof is applied to a reference type, the result 4584 // shall be the alignment of the referenced type. 4585 if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) 4586 ExprType = Ref->getPointeeType(); 4587 4588 // C11 6.5.3.4/3, C++11 [expr.alignof]p3: 4589 // When alignof or _Alignof is applied to an array type, the result 4590 // is the alignment of the element type. 4591 if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf || 4592 ExprKind == UETT_OpenMPRequiredSimdAlign) { 4593 // If the trait is 'alignof' in C before C2y, the ability to apply the 4594 // trait to an incomplete array is an extension. 4595 if (ExprKind == UETT_AlignOf && !getLangOpts().CPlusPlus && 4596 ExprType->isIncompleteArrayType()) 4597 Diag(OpLoc, getLangOpts().C2y 4598 ? diag::warn_c2y_compat_alignof_incomplete_array 4599 : diag::ext_c2y_alignof_incomplete_array); 4600 ExprType = Context.getBaseElementType(ExprType); 4601 } 4602 4603 if (ExprKind == UETT_VecStep) 4604 return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); 4605 4606 if (ExprKind == UETT_VectorElements) 4607 return CheckVectorElementsTraitOperandType(*this, ExprType, OpLoc, 4608 ExprRange); 4609 4610 if (ExprKind == UETT_PtrAuthTypeDiscriminator) 4611 return checkPtrAuthTypeDiscriminatorOperandType(*this, ExprType, OpLoc, 4612 ExprRange); 4613 4614 // Explicitly list some types as extensions. 4615 if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange, 4616 ExprKind)) 4617 return false; 4618 4619 if (RequireCompleteSizedType( 4620 OpLoc, ExprType, diag::err_sizeof_alignof_incomplete_or_sizeless_type, 4621 KWName, ExprRange)) 4622 return true; 4623 4624 if (ExprType->isFunctionType()) { 4625 Diag(OpLoc, diag::err_sizeof_alignof_function_type) << KWName << ExprRange; 4626 return true; 4627 } 4628 4629 if (ExprKind == UETT_CountOf) { 4630 // The type has to be an array type. We already checked for incomplete 4631 // types above. 4632 if (!ExprType->isArrayType()) { 4633 Diag(OpLoc, diag::err_countof_arg_not_array_type) << ExprType; 4634 return true; 4635 } 4636 } 4637 4638 // WebAssembly tables are always illegal operands to unary expressions and 4639 // type traits. 4640 if (Context.getTargetInfo().getTriple().isWasm() && 4641 ExprType->isWebAssemblyTableType()) { 4642 Diag(OpLoc, diag::err_wasm_table_invalid_uett_operand) 4643 << getTraitSpelling(ExprKind); 4644 return true; 4645 } 4646 4647 if (CheckObjCTraitOperandConstraints(*this, ExprType, OpLoc, ExprRange, 4648 ExprKind)) 4649 return true; 4650 4651 if (ExprType->isVariablyModifiedType() && FunctionScopes.size() > 1) { 4652 if (auto *TT = ExprType->getAs<TypedefType>()) { 4653 for (auto I = FunctionScopes.rbegin(), 4654 E = std::prev(FunctionScopes.rend()); 4655 I != E; ++I) { 4656 auto *CSI = dyn_cast<CapturingScopeInfo>(*I); 4657 if (CSI == nullptr) 4658 break; 4659 DeclContext *DC = nullptr; 4660 if (auto *LSI = dyn_cast<LambdaScopeInfo>(CSI)) 4661 DC = LSI->CallOperator; 4662 else if (auto *CRSI = dyn_cast<CapturedRegionScopeInfo>(CSI)) 4663 DC = CRSI->TheCapturedDecl; 4664 else if (auto *BSI = dyn_cast<BlockScopeInfo>(CSI)) 4665 DC = BSI->TheDecl; 4666 if (DC) { 4667 if (DC->containsDecl(TT->getDecl())) 4668 break; 4669 captureVariablyModifiedType(Context, ExprType, CSI); 4670 } 4671 } 4672 } 4673 } 4674 4675 return false; 4676 } 4677 4678 ExprResult Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, 4679 SourceLocation OpLoc, 4680 UnaryExprOrTypeTrait ExprKind, 4681 SourceRange R) { 4682 if (!TInfo) 4683 return ExprError(); 4684 4685 QualType T = TInfo->getType(); 4686 4687 if (!T->isDependentType() && 4688 CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind, 4689 getTraitSpelling(ExprKind))) 4690 return ExprError(); 4691 4692 // Adds overload of TransformToPotentiallyEvaluated for TypeSourceInfo to 4693 // properly deal with VLAs in nested calls of sizeof and typeof. 4694 if (currentEvaluationContext().isUnevaluated() && 4695 currentEvaluationContext().InConditionallyConstantEvaluateContext && 4696 (ExprKind == UETT_SizeOf || ExprKind == UETT_CountOf) && 4697 TInfo->getType()->isVariablyModifiedType()) 4698 TInfo = TransformToPotentiallyEvaluated(TInfo); 4699 4700 // It's possible that the transformation above failed. 4701 if (!TInfo) 4702 return ExprError(); 4703 4704 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. 4705 return new (Context) UnaryExprOrTypeTraitExpr( 4706 ExprKind, TInfo, Context.getSizeType(), OpLoc, R.getEnd()); 4707 } 4708 4709 ExprResult 4710 Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, 4711 UnaryExprOrTypeTrait ExprKind) { 4712 ExprResult PE = CheckPlaceholderExpr(E); 4713 if (PE.isInvalid()) 4714 return ExprError(); 4715 4716 E = PE.get(); 4717 4718 // Verify that the operand is valid. 4719 bool isInvalid = false; 4720 if (E->isTypeDependent()) { 4721 // Delay type-checking for type-dependent expressions. 4722 } else if (ExprKind == UETT_AlignOf || ExprKind == UETT_PreferredAlignOf) { 4723 isInvalid = CheckAlignOfExpr(*this, E, ExprKind); 4724 } else if (ExprKind == UETT_VecStep) { 4725 isInvalid = CheckVecStepExpr(E); 4726 } else if (ExprKind == UETT_OpenMPRequiredSimdAlign) { 4727 Diag(E->getExprLoc(), diag::err_openmp_default_simd_align_expr); 4728 isInvalid = true; 4729 } else if (E->refersToBitField()) { // C99 6.5.3.4p1. 4730 Diag(E->getExprLoc(), diag::err_sizeof_alignof_typeof_bitfield) << 0; 4731 isInvalid = true; 4732 } else if (ExprKind == UETT_VectorElements || ExprKind == UETT_SizeOf || 4733 ExprKind == UETT_CountOf) { // FIXME: __datasizeof? 4734 isInvalid = CheckUnaryExprOrTypeTraitOperand(E, ExprKind); 4735 } 4736 4737 if (isInvalid) 4738 return ExprError(); 4739 4740 if ((ExprKind == UETT_SizeOf || ExprKind == UETT_CountOf) && 4741 E->getType()->isVariableArrayType()) { 4742 PE = TransformToPotentiallyEvaluated(E); 4743 if (PE.isInvalid()) return ExprError(); 4744 E = PE.get(); 4745 } 4746 4747 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. 4748 return new (Context) UnaryExprOrTypeTraitExpr( 4749 ExprKind, E, Context.getSizeType(), OpLoc, E->getSourceRange().getEnd()); 4750 } 4751 4752 ExprResult 4753 Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, 4754 UnaryExprOrTypeTrait ExprKind, bool IsType, 4755 void *TyOrEx, SourceRange ArgRange) { 4756 // If error parsing type, ignore. 4757 if (!TyOrEx) return ExprError(); 4758 4759 if (IsType) { 4760 TypeSourceInfo *TInfo; 4761 (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); 4762 return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); 4763 } 4764 4765 Expr *ArgEx = (Expr *)TyOrEx; 4766 ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); 4767 return Result; 4768 } 4769 4770 bool Sema::CheckAlignasTypeArgument(StringRef KWName, TypeSourceInfo *TInfo, 4771 SourceLocation OpLoc, SourceRange R) { 4772 if (!TInfo) 4773 return true; 4774 return CheckUnaryExprOrTypeTraitOperand(TInfo->getType(), OpLoc, R, 4775 UETT_AlignOf, KWName); 4776 } 4777 4778 bool Sema::ActOnAlignasTypeArgument(StringRef KWName, ParsedType Ty, 4779 SourceLocation OpLoc, SourceRange R) { 4780 TypeSourceInfo *TInfo; 4781 (void)GetTypeFromParser(ParsedType::getFromOpaquePtr(Ty.getAsOpaquePtr()), 4782 &TInfo); 4783 return CheckAlignasTypeArgument(KWName, TInfo, OpLoc, R); 4784 } 4785 4786 static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, 4787 bool IsReal) { 4788 if (V.get()->isTypeDependent()) 4789 return S.Context.DependentTy; 4790 4791 // _Real and _Imag are only l-values for normal l-values. 4792 if (V.get()->getObjectKind() != OK_Ordinary) { 4793 V = S.DefaultLvalueConversion(V.get()); 4794 if (V.isInvalid()) 4795 return QualType(); 4796 } 4797 4798 // These operators return the element type of a complex type. 4799 if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) 4800 return CT->getElementType(); 4801 4802 // Otherwise they pass through real integer and floating point types here. 4803 if (V.get()->getType()->isArithmeticType()) 4804 return V.get()->getType(); 4805 4806 // Test for placeholders. 4807 ExprResult PR = S.CheckPlaceholderExpr(V.get()); 4808 if (PR.isInvalid()) return QualType(); 4809 if (PR.get() != V.get()) { 4810 V = PR; 4811 return CheckRealImagOperand(S, V, Loc, IsReal); 4812 } 4813 4814 // Reject anything else. 4815 S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() 4816 << (IsReal ? "__real" : "__imag"); 4817 return QualType(); 4818 } 4819 4820 4821 4822 ExprResult 4823 Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, 4824 tok::TokenKind Kind, Expr *Input) { 4825 UnaryOperatorKind Opc; 4826 switch (Kind) { 4827 default: llvm_unreachable("Unknown unary op!"); 4828 case tok::plusplus: Opc = UO_PostInc; break; 4829 case tok::minusminus: Opc = UO_PostDec; break; 4830 } 4831 4832 // Since this might is a postfix expression, get rid of ParenListExprs. 4833 ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Input); 4834 if (Result.isInvalid()) return ExprError(); 4835 Input = Result.get(); 4836 4837 return BuildUnaryOp(S, OpLoc, Opc, Input); 4838 } 4839 4840 /// Diagnose if arithmetic on the given ObjC pointer is illegal. 4841 /// 4842 /// \return true on error 4843 static bool checkArithmeticOnObjCPointer(Sema &S, 4844 SourceLocation opLoc, 4845 Expr *op) { 4846 assert(op->getType()->isObjCObjectPointerType()); 4847 if (S.LangOpts.ObjCRuntime.allowsPointerArithmetic() && 4848 !S.LangOpts.ObjCSubscriptingLegacyRuntime) 4849 return false; 4850 4851 S.Diag(opLoc, diag::err_arithmetic_nonfragile_interface) 4852 << op->getType()->castAs<ObjCObjectPointerType>()->getPointeeType() 4853 << op->getSourceRange(); 4854 return true; 4855 } 4856 4857 static bool isMSPropertySubscriptExpr(Sema &S, Expr *Base) { 4858 auto *BaseNoParens = Base->IgnoreParens(); 4859 if (auto *MSProp = dyn_cast<MSPropertyRefExpr>(BaseNoParens)) 4860 return MSProp->getPropertyDecl()->getType()->isArrayType(); 4861 return isa<MSPropertySubscriptExpr>(BaseNoParens); 4862 } 4863 4864 // Returns the type used for LHS[RHS], given one of LHS, RHS is type-dependent. 4865 // Typically this is DependentTy, but can sometimes be more precise. 4866 // 4867 // There are cases when we could determine a non-dependent type: 4868 // - LHS and RHS may have non-dependent types despite being type-dependent 4869 // (e.g. unbounded array static members of the current instantiation) 4870 // - one may be a dependent-sized array with known element type 4871 // - one may be a dependent-typed valid index (enum in current instantiation) 4872 // 4873 // We *always* return a dependent type, in such cases it is DependentTy. 4874 // This avoids creating type-dependent expressions with non-dependent types. 4875 // FIXME: is this important to avoid? See https://reviews.llvm.org/D107275 4876 static QualType getDependentArraySubscriptType(Expr *LHS, Expr *RHS, 4877 const ASTContext &Ctx) { 4878 assert(LHS->isTypeDependent() || RHS->isTypeDependent()); 4879 QualType LTy = LHS->getType(), RTy = RHS->getType(); 4880 QualType Result = Ctx.DependentTy; 4881 if (RTy->isIntegralOrUnscopedEnumerationType()) { 4882 if (const PointerType *PT = LTy->getAs<PointerType>()) 4883 Result = PT->getPointeeType(); 4884 else if (const ArrayType *AT = LTy->getAsArrayTypeUnsafe()) 4885 Result = AT->getElementType(); 4886 } else if (LTy->isIntegralOrUnscopedEnumerationType()) { 4887 if (const PointerType *PT = RTy->getAs<PointerType>()) 4888 Result = PT->getPointeeType(); 4889 else if (const ArrayType *AT = RTy->getAsArrayTypeUnsafe()) 4890 Result = AT->getElementType(); 4891 } 4892 // Ensure we return a dependent type. 4893 return Result->isDependentType() ? Result : Ctx.DependentTy; 4894 } 4895 4896 ExprResult Sema::ActOnArraySubscriptExpr(Scope *S, Expr *base, 4897 SourceLocation lbLoc, 4898 MultiExprArg ArgExprs, 4899 SourceLocation rbLoc) { 4900 4901 if (base && !base->getType().isNull() && 4902 base->hasPlaceholderType(BuiltinType::ArraySection)) { 4903 auto *AS = cast<ArraySectionExpr>(base); 4904 if (AS->isOMPArraySection()) 4905 return OpenMP().ActOnOMPArraySectionExpr( 4906 base, lbLoc, ArgExprs.front(), SourceLocation(), SourceLocation(), 4907 /*Length*/ nullptr, 4908 /*Stride=*/nullptr, rbLoc); 4909 4910 return OpenACC().ActOnArraySectionExpr(base, lbLoc, ArgExprs.front(), 4911 SourceLocation(), /*Length*/ nullptr, 4912 rbLoc); 4913 } 4914 4915 // Since this might be a postfix expression, get rid of ParenListExprs. 4916 if (isa<ParenListExpr>(base)) { 4917 ExprResult result = MaybeConvertParenListExprToParenExpr(S, base); 4918 if (result.isInvalid()) 4919 return ExprError(); 4920 base = result.get(); 4921 } 4922 4923 // Check if base and idx form a MatrixSubscriptExpr. 4924 // 4925 // Helper to check for comma expressions, which are not allowed as indices for 4926 // matrix subscript expressions. 4927 auto CheckAndReportCommaError = [this, base, rbLoc](Expr *E) { 4928 if (isa<BinaryOperator>(E) && cast<BinaryOperator>(E)->isCommaOp()) { 4929 Diag(E->getExprLoc(), diag::err_matrix_subscript_comma) 4930 << SourceRange(base->getBeginLoc(), rbLoc); 4931 return true; 4932 } 4933 return false; 4934 }; 4935 // The matrix subscript operator ([][])is considered a single operator. 4936 // Separating the index expressions by parenthesis is not allowed. 4937 if (base && !base->getType().isNull() && 4938 base->hasPlaceholderType(BuiltinType::IncompleteMatrixIdx) && 4939 !isa<MatrixSubscriptExpr>(base)) { 4940 Diag(base->getExprLoc(), diag::err_matrix_separate_incomplete_index) 4941 << SourceRange(base->getBeginLoc(), rbLoc); 4942 return ExprError(); 4943 } 4944 // If the base is a MatrixSubscriptExpr, try to create a new 4945 // MatrixSubscriptExpr. 4946 auto *matSubscriptE = dyn_cast<MatrixSubscriptExpr>(base); 4947 if (matSubscriptE) { 4948 assert(ArgExprs.size() == 1); 4949 if (CheckAndReportCommaError(ArgExprs.front())) 4950 return ExprError(); 4951 4952 assert(matSubscriptE->isIncomplete() && 4953 "base has to be an incomplete matrix subscript"); 4954 return CreateBuiltinMatrixSubscriptExpr(matSubscriptE->getBase(), 4955 matSubscriptE->getRowIdx(), 4956 ArgExprs.front(), rbLoc); 4957 } 4958 if (base->getType()->isWebAssemblyTableType()) { 4959 Diag(base->getExprLoc(), diag::err_wasm_table_art) 4960 << SourceRange(base->getBeginLoc(), rbLoc) << 3; 4961 return ExprError(); 4962 } 4963 4964 CheckInvalidBuiltinCountedByRef(base, 4965 BuiltinCountedByRefKind::ArraySubscript); 4966 4967 // Handle any non-overload placeholder types in the base and index 4968 // expressions. We can't handle overloads here because the other 4969 // operand might be an overloadable type, in which case the overload 4970 // resolution for the operator overload should get the first crack 4971 // at the overload. 4972 bool IsMSPropertySubscript = false; 4973 if (base->getType()->isNonOverloadPlaceholderType()) { 4974 IsMSPropertySubscript = isMSPropertySubscriptExpr(*this, base); 4975 if (!IsMSPropertySubscript) { 4976 ExprResult result = CheckPlaceholderExpr(base); 4977 if (result.isInvalid()) 4978 return ExprError(); 4979 base = result.get(); 4980 } 4981 } 4982 4983 // If the base is a matrix type, try to create a new MatrixSubscriptExpr. 4984 if (base->getType()->isMatrixType()) { 4985 assert(ArgExprs.size() == 1); 4986 if (CheckAndReportCommaError(ArgExprs.front())) 4987 return ExprError(); 4988 4989 return CreateBuiltinMatrixSubscriptExpr(base, ArgExprs.front(), nullptr, 4990 rbLoc); 4991 } 4992 4993 if (ArgExprs.size() == 1 && getLangOpts().CPlusPlus20) { 4994 Expr *idx = ArgExprs[0]; 4995 if ((isa<BinaryOperator>(idx) && cast<BinaryOperator>(idx)->isCommaOp()) || 4996 (isa<CXXOperatorCallExpr>(idx) && 4997 cast<CXXOperatorCallExpr>(idx)->getOperator() == OO_Comma)) { 4998 Diag(idx->getExprLoc(), diag::warn_deprecated_comma_subscript) 4999 << SourceRange(base->getBeginLoc(), rbLoc); 5000 } 5001 } 5002 5003 if (ArgExprs.size() == 1 && 5004 ArgExprs[0]->getType()->isNonOverloadPlaceholderType()) { 5005 ExprResult result = CheckPlaceholderExpr(ArgExprs[0]); 5006 if (result.isInvalid()) 5007 return ExprError(); 5008 ArgExprs[0] = result.get(); 5009 } else { 5010 if (CheckArgsForPlaceholders(ArgExprs)) 5011 return ExprError(); 5012 } 5013 5014 // Build an unanalyzed expression if either operand is type-dependent. 5015 if (getLangOpts().CPlusPlus && ArgExprs.size() == 1 && 5016 (base->isTypeDependent() || 5017 Expr::hasAnyTypeDependentArguments(ArgExprs)) && 5018 !isa<PackExpansionExpr>(ArgExprs[0])) { 5019 return new (Context) ArraySubscriptExpr( 5020 base, ArgExprs.front(), 5021 getDependentArraySubscriptType(base, ArgExprs.front(), getASTContext()), 5022 VK_LValue, OK_Ordinary, rbLoc); 5023 } 5024 5025 // MSDN, property (C++) 5026 // https://msdn.microsoft.com/en-us/library/yhfk0thd(v=vs.120).aspx 5027 // This attribute can also be used in the declaration of an empty array in a 5028 // class or structure definition. For example: 5029 // __declspec(property(get=GetX, put=PutX)) int x[]; 5030 // The above statement indicates that x[] can be used with one or more array 5031 // indices. In this case, i=p->x[a][b] will be turned into i=p->GetX(a, b), 5032 // and p->x[a][b] = i will be turned into p->PutX(a, b, i); 5033 if (IsMSPropertySubscript) { 5034 assert(ArgExprs.size() == 1); 5035 // Build MS property subscript expression if base is MS property reference 5036 // or MS property subscript. 5037 return new (Context) 5038 MSPropertySubscriptExpr(base, ArgExprs.front(), Context.PseudoObjectTy, 5039 VK_LValue, OK_Ordinary, rbLoc); 5040 } 5041 5042 // Use C++ overloaded-operator rules if either operand has record 5043 // type. The spec says to do this if either type is *overloadable*, 5044 // but enum types can't declare subscript operators or conversion 5045 // operators, so there's nothing interesting for overload resolution 5046 // to do if there aren't any record types involved. 5047 // 5048 // ObjC pointers have their own subscripting logic that is not tied 5049 // to overload resolution and so should not take this path. 5050 if (getLangOpts().CPlusPlus && !base->getType()->isObjCObjectPointerType() && 5051 ((base->getType()->isRecordType() || 5052 (ArgExprs.size() != 1 || isa<PackExpansionExpr>(ArgExprs[0]) || 5053 ArgExprs[0]->getType()->isRecordType())))) { 5054 return CreateOverloadedArraySubscriptExpr(lbLoc, rbLoc, base, ArgExprs); 5055 } 5056 5057 ExprResult Res = 5058 CreateBuiltinArraySubscriptExpr(base, lbLoc, ArgExprs.front(), rbLoc); 5059 5060 if (!Res.isInvalid() && isa<ArraySubscriptExpr>(Res.get())) 5061 CheckSubscriptAccessOfNoDeref(cast<ArraySubscriptExpr>(Res.get())); 5062 5063 return Res; 5064 } 5065 5066 ExprResult Sema::tryConvertExprToType(Expr *E, QualType Ty) { 5067 InitializedEntity Entity = InitializedEntity::InitializeTemporary(Ty); 5068 InitializationKind Kind = 5069 InitializationKind::CreateCopy(E->getBeginLoc(), SourceLocation()); 5070 InitializationSequence InitSeq(*this, Entity, Kind, E); 5071 return InitSeq.Perform(*this, Entity, Kind, E); 5072 } 5073 5074 ExprResult Sema::CreateBuiltinMatrixSubscriptExpr(Expr *Base, Expr *RowIdx, 5075 Expr *ColumnIdx, 5076 SourceLocation RBLoc) { 5077 ExprResult BaseR = CheckPlaceholderExpr(Base); 5078 if (BaseR.isInvalid()) 5079 return BaseR; 5080 Base = BaseR.get(); 5081 5082 ExprResult RowR = CheckPlaceholderExpr(RowIdx); 5083 if (RowR.isInvalid()) 5084 return RowR; 5085 RowIdx = RowR.get(); 5086 5087 if (!ColumnIdx) 5088 return new (Context) MatrixSubscriptExpr( 5089 Base, RowIdx, ColumnIdx, Context.IncompleteMatrixIdxTy, RBLoc); 5090 5091 // Build an unanalyzed expression if any of the operands is type-dependent. 5092 if (Base->isTypeDependent() || RowIdx->isTypeDependent() || 5093 ColumnIdx->isTypeDependent()) 5094 return new (Context) MatrixSubscriptExpr(Base, RowIdx, ColumnIdx, 5095 Context.DependentTy, RBLoc); 5096 5097 ExprResult ColumnR = CheckPlaceholderExpr(ColumnIdx); 5098 if (ColumnR.isInvalid()) 5099 return ColumnR; 5100 ColumnIdx = ColumnR.get(); 5101 5102 // Check that IndexExpr is an integer expression. If it is a constant 5103 // expression, check that it is less than Dim (= the number of elements in the 5104 // corresponding dimension). 5105 auto IsIndexValid = [&](Expr *IndexExpr, unsigned Dim, 5106 bool IsColumnIdx) -> Expr * { 5107 if (!IndexExpr->getType()->isIntegerType() && 5108 !IndexExpr->isTypeDependent()) { 5109 Diag(IndexExpr->getBeginLoc(), diag::err_matrix_index_not_integer) 5110 << IsColumnIdx; 5111 return nullptr; 5112 } 5113 5114 if (std::optional<llvm::APSInt> Idx = 5115 IndexExpr->getIntegerConstantExpr(Context)) { 5116 if ((*Idx < 0 || *Idx >= Dim)) { 5117 Diag(IndexExpr->getBeginLoc(), diag::err_matrix_index_outside_range) 5118 << IsColumnIdx << Dim; 5119 return nullptr; 5120 } 5121 } 5122 5123 ExprResult ConvExpr = IndexExpr; 5124 assert(!ConvExpr.isInvalid() && 5125 "should be able to convert any integer type to size type"); 5126 return ConvExpr.get(); 5127 }; 5128 5129 auto *MTy = Base->getType()->getAs<ConstantMatrixType>(); 5130 RowIdx = IsIndexValid(RowIdx, MTy->getNumRows(), false); 5131 ColumnIdx = IsIndexValid(ColumnIdx, MTy->getNumColumns(), true); 5132 if (!RowIdx || !ColumnIdx) 5133 return ExprError(); 5134 5135 return new (Context) MatrixSubscriptExpr(Base, RowIdx, ColumnIdx, 5136 MTy->getElementType(), RBLoc); 5137 } 5138 5139 void Sema::CheckAddressOfNoDeref(const Expr *E) { 5140 ExpressionEvaluationContextRecord &LastRecord = ExprEvalContexts.back(); 5141 const Expr *StrippedExpr = E->IgnoreParenImpCasts(); 5142 5143 // For expressions like `&(*s).b`, the base is recorded and what should be 5144 // checked. 5145 const MemberExpr *Member = nullptr; 5146 while ((Member = dyn_cast<MemberExpr>(StrippedExpr)) && !Member->isArrow()) 5147 StrippedExpr = Member->getBase()->IgnoreParenImpCasts(); 5148 5149 LastRecord.PossibleDerefs.erase(StrippedExpr); 5150 } 5151 5152 void Sema::CheckSubscriptAccessOfNoDeref(const ArraySubscriptExpr *E) { 5153 if (isUnevaluatedContext()) 5154 return; 5155 5156 QualType ResultTy = E->getType(); 5157 ExpressionEvaluationContextRecord &LastRecord = ExprEvalContexts.back(); 5158 5159 // Bail if the element is an array since it is not memory access. 5160 if (isa<ArrayType>(ResultTy)) 5161 return; 5162 5163 if (ResultTy->hasAttr(attr::NoDeref)) { 5164 LastRecord.PossibleDerefs.insert(E); 5165 return; 5166 } 5167 5168 // Check if the base type is a pointer to a member access of a struct 5169 // marked with noderef. 5170 const Expr *Base = E->getBase(); 5171 QualType BaseTy = Base->getType(); 5172 if (!(isa<ArrayType>(BaseTy) || isa<PointerType>(BaseTy))) 5173 // Not a pointer access 5174 return; 5175 5176 const MemberExpr *Member = nullptr; 5177 while ((Member = dyn_cast<MemberExpr>(Base->IgnoreParenCasts())) && 5178 Member->isArrow()) 5179 Base = Member->getBase(); 5180 5181 if (const auto *Ptr = dyn_cast<PointerType>(Base->getType())) { 5182 if (Ptr->getPointeeType()->hasAttr(attr::NoDeref)) 5183 LastRecord.PossibleDerefs.insert(E); 5184 } 5185 } 5186 5187 ExprResult 5188 Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, 5189 Expr *Idx, SourceLocation RLoc) { 5190 Expr *LHSExp = Base; 5191 Expr *RHSExp = Idx; 5192 5193 ExprValueKind VK = VK_LValue; 5194 ExprObjectKind OK = OK_Ordinary; 5195 5196 // Per C++ core issue 1213, the result is an xvalue if either operand is 5197 // a non-lvalue array, and an lvalue otherwise. 5198 if (getLangOpts().CPlusPlus11) { 5199 for (auto *Op : {LHSExp, RHSExp}) { 5200 Op = Op->IgnoreImplicit(); 5201 if (Op->getType()->isArrayType() && !Op->isLValue()) 5202 VK = VK_XValue; 5203 } 5204 } 5205 5206 // Perform default conversions. 5207 if (!LHSExp->getType()->isSubscriptableVectorType()) { 5208 ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); 5209 if (Result.isInvalid()) 5210 return ExprError(); 5211 LHSExp = Result.get(); 5212 } 5213 ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); 5214 if (Result.isInvalid()) 5215 return ExprError(); 5216 RHSExp = Result.get(); 5217 5218 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); 5219 5220 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent 5221 // to the expression *((e1)+(e2)). This means the array "Base" may actually be 5222 // in the subscript position. As a result, we need to derive the array base 5223 // and index from the expression types. 5224 Expr *BaseExpr, *IndexExpr; 5225 QualType ResultType; 5226 if (LHSTy->isDependentType() || RHSTy->isDependentType()) { 5227 BaseExpr = LHSExp; 5228 IndexExpr = RHSExp; 5229 ResultType = 5230 getDependentArraySubscriptType(LHSExp, RHSExp, getASTContext()); 5231 } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { 5232 BaseExpr = LHSExp; 5233 IndexExpr = RHSExp; 5234 ResultType = PTy->getPointeeType(); 5235 } else if (const ObjCObjectPointerType *PTy = 5236 LHSTy->getAs<ObjCObjectPointerType>()) { 5237 BaseExpr = LHSExp; 5238 IndexExpr = RHSExp; 5239 5240 // Use custom logic if this should be the pseudo-object subscript 5241 // expression. 5242 if (!LangOpts.isSubscriptPointerArithmetic()) 5243 return ObjC().BuildObjCSubscriptExpression(RLoc, BaseExpr, IndexExpr, 5244 nullptr, nullptr); 5245 5246 ResultType = PTy->getPointeeType(); 5247 } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { 5248 // Handle the uncommon case of "123[Ptr]". 5249 BaseExpr = RHSExp; 5250 IndexExpr = LHSExp; 5251 ResultType = PTy->getPointeeType(); 5252 } else if (const ObjCObjectPointerType *PTy = 5253 RHSTy->getAs<ObjCObjectPointerType>()) { 5254 // Handle the uncommon case of "123[Ptr]". 5255 BaseExpr = RHSExp; 5256 IndexExpr = LHSExp; 5257 ResultType = PTy->getPointeeType(); 5258 if (!LangOpts.isSubscriptPointerArithmetic()) { 5259 Diag(LLoc, diag::err_subscript_nonfragile_interface) 5260 << ResultType << BaseExpr->getSourceRange(); 5261 return ExprError(); 5262 } 5263 } else if (LHSTy->isSubscriptableVectorType()) { 5264 if (LHSTy->isBuiltinType() && 5265 LHSTy->getAs<BuiltinType>()->isSveVLSBuiltinType()) { 5266 const BuiltinType *BTy = LHSTy->getAs<BuiltinType>(); 5267 if (BTy->isSVEBool()) 5268 return ExprError(Diag(LLoc, diag::err_subscript_svbool_t) 5269 << LHSExp->getSourceRange() 5270 << RHSExp->getSourceRange()); 5271 ResultType = BTy->getSveEltType(Context); 5272 } else { 5273 const VectorType *VTy = LHSTy->getAs<VectorType>(); 5274 ResultType = VTy->getElementType(); 5275 } 5276 BaseExpr = LHSExp; // vectors: V[123] 5277 IndexExpr = RHSExp; 5278 // We apply C++ DR1213 to vector subscripting too. 5279 if (getLangOpts().CPlusPlus11 && LHSExp->isPRValue()) { 5280 ExprResult Materialized = TemporaryMaterializationConversion(LHSExp); 5281 if (Materialized.isInvalid()) 5282 return ExprError(); 5283 LHSExp = Materialized.get(); 5284 } 5285 VK = LHSExp->getValueKind(); 5286 if (VK != VK_PRValue) 5287 OK = OK_VectorComponent; 5288 5289 QualType BaseType = BaseExpr->getType(); 5290 Qualifiers BaseQuals = BaseType.getQualifiers(); 5291 Qualifiers MemberQuals = ResultType.getQualifiers(); 5292 Qualifiers Combined = BaseQuals + MemberQuals; 5293 if (Combined != MemberQuals) 5294 ResultType = Context.getQualifiedType(ResultType, Combined); 5295 } else if (LHSTy->isArrayType()) { 5296 // If we see an array that wasn't promoted by 5297 // DefaultFunctionArrayLvalueConversion, it must be an array that 5298 // wasn't promoted because of the C90 rule that doesn't 5299 // allow promoting non-lvalue arrays. Warn, then 5300 // force the promotion here. 5301 Diag(LHSExp->getBeginLoc(), diag::ext_subscript_non_lvalue) 5302 << LHSExp->getSourceRange(); 5303 LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), 5304 CK_ArrayToPointerDecay).get(); 5305 LHSTy = LHSExp->getType(); 5306 5307 BaseExpr = LHSExp; 5308 IndexExpr = RHSExp; 5309 ResultType = LHSTy->castAs<PointerType>()->getPointeeType(); 5310 } else if (RHSTy->isArrayType()) { 5311 // Same as previous, except for 123[f().a] case 5312 Diag(RHSExp->getBeginLoc(), diag::ext_subscript_non_lvalue) 5313 << RHSExp->getSourceRange(); 5314 RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), 5315 CK_ArrayToPointerDecay).get(); 5316 RHSTy = RHSExp->getType(); 5317 5318 BaseExpr = RHSExp; 5319 IndexExpr = LHSExp; 5320 ResultType = RHSTy->castAs<PointerType>()->getPointeeType(); 5321 } else { 5322 return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) 5323 << LHSExp->getSourceRange() << RHSExp->getSourceRange()); 5324 } 5325 // C99 6.5.2.1p1 5326 if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) 5327 return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) 5328 << IndexExpr->getSourceRange()); 5329 5330 if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || 5331 IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) && 5332 !IndexExpr->isTypeDependent()) { 5333 std::optional<llvm::APSInt> IntegerContantExpr = 5334 IndexExpr->getIntegerConstantExpr(getASTContext()); 5335 if (!IntegerContantExpr.has_value() || 5336 IntegerContantExpr.value().isNegative()) 5337 Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); 5338 } 5339 5340 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, 5341 // C++ [expr.sub]p1: The type "T" shall be a completely-defined object 5342 // type. Note that Functions are not objects, and that (in C99 parlance) 5343 // incomplete types are not object types. 5344 if (ResultType->isFunctionType()) { 5345 Diag(BaseExpr->getBeginLoc(), diag::err_subscript_function_type) 5346 << ResultType << BaseExpr->getSourceRange(); 5347 return ExprError(); 5348 } 5349 5350 if (ResultType->isVoidType() && !getLangOpts().CPlusPlus) { 5351 // GNU extension: subscripting on pointer to void 5352 Diag(LLoc, diag::ext_gnu_subscript_void_type) 5353 << BaseExpr->getSourceRange(); 5354 5355 // C forbids expressions of unqualified void type from being l-values. 5356 // See IsCForbiddenLValueType. 5357 if (!ResultType.hasQualifiers()) 5358 VK = VK_PRValue; 5359 } else if (!ResultType->isDependentType() && 5360 !ResultType.isWebAssemblyReferenceType() && 5361 RequireCompleteSizedType( 5362 LLoc, ResultType, 5363 diag::err_subscript_incomplete_or_sizeless_type, BaseExpr)) 5364 return ExprError(); 5365 5366 assert(VK == VK_PRValue || LangOpts.CPlusPlus || 5367 !ResultType.isCForbiddenLValueType()); 5368 5369 if (LHSExp->IgnoreParenImpCasts()->getType()->isVariablyModifiedType() && 5370 FunctionScopes.size() > 1) { 5371 if (auto *TT = 5372 LHSExp->IgnoreParenImpCasts()->getType()->getAs<TypedefType>()) { 5373 for (auto I = FunctionScopes.rbegin(), 5374 E = std::prev(FunctionScopes.rend()); 5375 I != E; ++I) { 5376 auto *CSI = dyn_cast<CapturingScopeInfo>(*I); 5377 if (CSI == nullptr) 5378 break; 5379 DeclContext *DC = nullptr; 5380 if (auto *LSI = dyn_cast<LambdaScopeInfo>(CSI)) 5381 DC = LSI->CallOperator; 5382 else if (auto *CRSI = dyn_cast<CapturedRegionScopeInfo>(CSI)) 5383 DC = CRSI->TheCapturedDecl; 5384 else if (auto *BSI = dyn_cast<BlockScopeInfo>(CSI)) 5385 DC = BSI->TheDecl; 5386 if (DC) { 5387 if (DC->containsDecl(TT->getDecl())) 5388 break; 5389 captureVariablyModifiedType( 5390 Context, LHSExp->IgnoreParenImpCasts()->getType(), CSI); 5391 } 5392 } 5393 } 5394 } 5395 5396 return new (Context) 5397 ArraySubscriptExpr(LHSExp, RHSExp, ResultType, VK, OK, RLoc); 5398 } 5399 5400 bool Sema::CheckCXXDefaultArgExpr(SourceLocation CallLoc, FunctionDecl *FD, 5401 ParmVarDecl *Param, Expr *RewrittenInit, 5402 bool SkipImmediateInvocations) { 5403 if (Param->hasUnparsedDefaultArg()) { 5404 assert(!RewrittenInit && "Should not have a rewritten init expression yet"); 5405 // If we've already cleared out the location for the default argument, 5406 // that means we're parsing it right now. 5407 if (!UnparsedDefaultArgLocs.count(Param)) { 5408 Diag(Param->getBeginLoc(), diag::err_recursive_default_argument) << FD; 5409 Diag(CallLoc, diag::note_recursive_default_argument_used_here); 5410 Param->setInvalidDecl(); 5411 return true; 5412 } 5413 5414 Diag(CallLoc, diag::err_use_of_default_argument_to_function_declared_later) 5415 << FD << cast<CXXRecordDecl>(FD->getDeclContext()); 5416 Diag(UnparsedDefaultArgLocs[Param], 5417 diag::note_default_argument_declared_here); 5418 return true; 5419 } 5420 5421 if (Param->hasUninstantiatedDefaultArg()) { 5422 assert(!RewrittenInit && "Should not have a rewitten init expression yet"); 5423 if (InstantiateDefaultArgument(CallLoc, FD, Param)) 5424 return true; 5425 } 5426 5427 Expr *Init = RewrittenInit ? RewrittenInit : Param->getInit(); 5428 assert(Init && "default argument but no initializer?"); 5429 5430 // If the default expression creates temporaries, we need to 5431 // push them to the current stack of expression temporaries so they'll 5432 // be properly destroyed. 5433 // FIXME: We should really be rebuilding the default argument with new 5434 // bound temporaries; see the comment in PR5810. 5435 // We don't need to do that with block decls, though, because 5436 // blocks in default argument expression can never capture anything. 5437 if (auto *InitWithCleanup = dyn_cast<ExprWithCleanups>(Init)) { 5438 // Set the "needs cleanups" bit regardless of whether there are 5439 // any explicit objects. 5440 Cleanup.setExprNeedsCleanups(InitWithCleanup->cleanupsHaveSideEffects()); 5441 // Append all the objects to the cleanup list. Right now, this 5442 // should always be a no-op, because blocks in default argument 5443 // expressions should never be able to capture anything. 5444 assert(!InitWithCleanup->getNumObjects() && 5445 "default argument expression has capturing blocks?"); 5446 } 5447 // C++ [expr.const]p15.1: 5448 // An expression or conversion is in an immediate function context if it is 5449 // potentially evaluated and [...] its innermost enclosing non-block scope 5450 // is a function parameter scope of an immediate function. 5451 EnterExpressionEvaluationContext EvalContext( 5452 *this, 5453 FD->isImmediateFunction() 5454 ? ExpressionEvaluationContext::ImmediateFunctionContext 5455 : ExpressionEvaluationContext::PotentiallyEvaluated, 5456 Param); 5457 ExprEvalContexts.back().IsCurrentlyCheckingDefaultArgumentOrInitializer = 5458 SkipImmediateInvocations; 5459 runWithSufficientStackSpace(CallLoc, [&] { 5460 MarkDeclarationsReferencedInExpr(Init, /*SkipLocalVariables=*/true); 5461 }); 5462 return false; 5463 } 5464 5465 struct ImmediateCallVisitor : DynamicRecursiveASTVisitor { 5466 const ASTContext &Context; 5467 ImmediateCallVisitor(const ASTContext &Ctx) : Context(Ctx) { 5468 ShouldVisitImplicitCode = true; 5469 } 5470 5471 bool HasImmediateCalls = false; 5472 5473 bool VisitCallExpr(CallExpr *E) override { 5474 if (const FunctionDecl *FD = E->getDirectCallee()) 5475 HasImmediateCalls |= FD->isImmediateFunction(); 5476 return DynamicRecursiveASTVisitor::VisitStmt(E); 5477 } 5478 5479 bool VisitCXXConstructExpr(CXXConstructExpr *E) override { 5480 if (const FunctionDecl *FD = E->getConstructor()) 5481 HasImmediateCalls |= FD->isImmediateFunction(); 5482 return DynamicRecursiveASTVisitor::VisitStmt(E); 5483 } 5484 5485 // SourceLocExpr are not immediate invocations 5486 // but CXXDefaultInitExpr/CXXDefaultArgExpr containing a SourceLocExpr 5487 // need to be rebuilt so that they refer to the correct SourceLocation and 5488 // DeclContext. 5489 bool VisitSourceLocExpr(SourceLocExpr *E) override { 5490 HasImmediateCalls = true; 5491 return DynamicRecursiveASTVisitor::VisitStmt(E); 5492 } 5493 5494 // A nested lambda might have parameters with immediate invocations 5495 // in their default arguments. 5496 // The compound statement is not visited (as it does not constitute a 5497 // subexpression). 5498 // FIXME: We should consider visiting and transforming captures 5499 // with init expressions. 5500 bool VisitLambdaExpr(LambdaExpr *E) override { 5501 return VisitCXXMethodDecl(E->getCallOperator()); 5502 } 5503 5504 bool VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) override { 5505 return TraverseStmt(E->getExpr()); 5506 } 5507 5508 bool VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) override { 5509 return TraverseStmt(E->getExpr()); 5510 } 5511 }; 5512 5513 struct EnsureImmediateInvocationInDefaultArgs 5514 : TreeTransform<EnsureImmediateInvocationInDefaultArgs> { 5515 EnsureImmediateInvocationInDefaultArgs(Sema &SemaRef) 5516 : TreeTransform(SemaRef) {} 5517 5518 bool AlwaysRebuild() { return true; } 5519 5520 // Lambda can only have immediate invocations in the default 5521 // args of their parameters, which is transformed upon calling the closure. 5522 // The body is not a subexpression, so we have nothing to do. 5523 // FIXME: Immediate calls in capture initializers should be transformed. 5524 ExprResult TransformLambdaExpr(LambdaExpr *E) { return E; } 5525 ExprResult TransformBlockExpr(BlockExpr *E) { return E; } 5526 5527 // Make sure we don't rebuild the this pointer as it would 5528 // cause it to incorrectly point it to the outermost class 5529 // in the case of nested struct initialization. 5530 ExprResult TransformCXXThisExpr(CXXThisExpr *E) { return E; } 5531 5532 // Rewrite to source location to refer to the context in which they are used. 5533 ExprResult TransformSourceLocExpr(SourceLocExpr *E) { 5534 DeclContext *DC = E->getParentContext(); 5535 if (DC == SemaRef.CurContext) 5536 return E; 5537 5538 // FIXME: During instantiation, because the rebuild of defaults arguments 5539 // is not always done in the context of the template instantiator, 5540 // we run the risk of producing a dependent source location 5541 // that would never be rebuilt. 5542 // This usually happens during overload resolution, or in contexts 5543 // where the value of the source location does not matter. 5544 // However, we should find a better way to deal with source location 5545 // of function templates. 5546 if (!SemaRef.CurrentInstantiationScope || 5547 !SemaRef.CurContext->isDependentContext() || DC->isDependentContext()) 5548 DC = SemaRef.CurContext; 5549 5550 return getDerived().RebuildSourceLocExpr( 5551 E->getIdentKind(), E->getType(), E->getBeginLoc(), E->getEndLoc(), DC); 5552 } 5553 }; 5554 5555 ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, 5556 FunctionDecl *FD, ParmVarDecl *Param, 5557 Expr *Init) { 5558 assert(Param->hasDefaultArg() && "can't build nonexistent default arg"); 5559 5560 bool NestedDefaultChecking = isCheckingDefaultArgumentOrInitializer(); 5561 bool NeedRebuild = needsRebuildOfDefaultArgOrInit(); 5562 std::optional<ExpressionEvaluationContextRecord::InitializationContext> 5563 InitializationContext = 5564 OutermostDeclarationWithDelayedImmediateInvocations(); 5565 if (!InitializationContext.has_value()) 5566 InitializationContext.emplace(CallLoc, Param, CurContext); 5567 5568 if (!Init && !Param->hasUnparsedDefaultArg()) { 5569 // Mark that we are replacing a default argument first. 5570 // If we are instantiating a template we won't have to 5571 // retransform immediate calls. 5572 // C++ [expr.const]p15.1: 5573 // An expression or conversion is in an immediate function context if it 5574 // is potentially evaluated and [...] its innermost enclosing non-block 5575 // scope is a function parameter scope of an immediate function. 5576 EnterExpressionEvaluationContext EvalContext( 5577 *this, 5578 FD->isImmediateFunction() 5579 ? ExpressionEvaluationContext::ImmediateFunctionContext 5580 : ExpressionEvaluationContext::PotentiallyEvaluated, 5581 Param); 5582 5583 if (Param->hasUninstantiatedDefaultArg()) { 5584 if (InstantiateDefaultArgument(CallLoc, FD, Param)) 5585 return ExprError(); 5586 } 5587 // CWG2631 5588 // An immediate invocation that is not evaluated where it appears is 5589 // evaluated and checked for whether it is a constant expression at the 5590 // point where the enclosing initializer is used in a function call. 5591 ImmediateCallVisitor V(getASTContext()); 5592 if (!NestedDefaultChecking) 5593 V.TraverseDecl(Param); 5594 5595 // Rewrite the call argument that was created from the corresponding 5596 // parameter's default argument. 5597 if (V.HasImmediateCalls || 5598 (NeedRebuild && isa_and_present<ExprWithCleanups>(Param->getInit()))) { 5599 if (V.HasImmediateCalls) 5600 ExprEvalContexts.back().DelayedDefaultInitializationContext = { 5601 CallLoc, Param, CurContext}; 5602 // Pass down lifetime extending flag, and collect temporaries in 5603 // CreateMaterializeTemporaryExpr when we rewrite the call argument. 5604 currentEvaluationContext().InLifetimeExtendingContext = 5605 parentEvaluationContext().InLifetimeExtendingContext; 5606 EnsureImmediateInvocationInDefaultArgs Immediate(*this); 5607 ExprResult Res; 5608 runWithSufficientStackSpace(CallLoc, [&] { 5609 Res = Immediate.TransformInitializer(Param->getInit(), 5610 /*NotCopy=*/false); 5611 }); 5612 if (Res.isInvalid()) 5613 return ExprError(); 5614 Res = ConvertParamDefaultArgument(Param, Res.get(), 5615 Res.get()->getBeginLoc()); 5616 if (Res.isInvalid()) 5617 return ExprError(); 5618 Init = Res.get(); 5619 } 5620 } 5621 5622 if (CheckCXXDefaultArgExpr( 5623 CallLoc, FD, Param, Init, 5624 /*SkipImmediateInvocations=*/NestedDefaultChecking)) 5625 return ExprError(); 5626 5627 return CXXDefaultArgExpr::Create(Context, InitializationContext->Loc, Param, 5628 Init, InitializationContext->Context); 5629 } 5630 5631 static FieldDecl *FindFieldDeclInstantiationPattern(const ASTContext &Ctx, 5632 FieldDecl *Field) { 5633 if (FieldDecl *Pattern = Ctx.getInstantiatedFromUnnamedFieldDecl(Field)) 5634 return Pattern; 5635 auto *ParentRD = cast<CXXRecordDecl>(Field->getParent()); 5636 CXXRecordDecl *ClassPattern = ParentRD->getTemplateInstantiationPattern(); 5637 DeclContext::lookup_result Lookup = 5638 ClassPattern->lookup(Field->getDeclName()); 5639 auto Rng = llvm::make_filter_range( 5640 Lookup, [](auto &&L) { return isa<FieldDecl>(*L); }); 5641 if (Rng.empty()) 5642 return nullptr; 5643 // FIXME: this breaks clang/test/Modules/pr28812.cpp 5644 // assert(std::distance(Rng.begin(), Rng.end()) <= 1 5645 // && "Duplicated instantiation pattern for field decl"); 5646 return cast<FieldDecl>(*Rng.begin()); 5647 } 5648 5649 ExprResult Sema::BuildCXXDefaultInitExpr(SourceLocation Loc, FieldDecl *Field) { 5650 assert(Field->hasInClassInitializer()); 5651 5652 CXXThisScopeRAII This(*this, Field->getParent(), Qualifiers()); 5653 5654 auto *ParentRD = cast<CXXRecordDecl>(Field->getParent()); 5655 5656 std::optional<ExpressionEvaluationContextRecord::InitializationContext> 5657 InitializationContext = 5658 OutermostDeclarationWithDelayedImmediateInvocations(); 5659 if (!InitializationContext.has_value()) 5660 InitializationContext.emplace(Loc, Field, CurContext); 5661 5662 Expr *Init = nullptr; 5663 5664 bool NestedDefaultChecking = isCheckingDefaultArgumentOrInitializer(); 5665 bool NeedRebuild = needsRebuildOfDefaultArgOrInit(); 5666 EnterExpressionEvaluationContext EvalContext( 5667 *this, ExpressionEvaluationContext::PotentiallyEvaluated, Field); 5668 5669 if (!Field->getInClassInitializer()) { 5670 // Maybe we haven't instantiated the in-class initializer. Go check the 5671 // pattern FieldDecl to see if it has one. 5672 if (isTemplateInstantiation(ParentRD->getTemplateSpecializationKind())) { 5673 FieldDecl *Pattern = 5674 FindFieldDeclInstantiationPattern(getASTContext(), Field); 5675 assert(Pattern && "We must have set the Pattern!"); 5676 if (!Pattern->hasInClassInitializer() || 5677 InstantiateInClassInitializer(Loc, Field, Pattern, 5678 getTemplateInstantiationArgs(Field))) { 5679 Field->setInvalidDecl(); 5680 return ExprError(); 5681 } 5682 } 5683 } 5684 5685 // CWG2631 5686 // An immediate invocation that is not evaluated where it appears is 5687 // evaluated and checked for whether it is a constant expression at the 5688 // point where the enclosing initializer is used in a [...] a constructor 5689 // definition, or an aggregate initialization. 5690 ImmediateCallVisitor V(getASTContext()); 5691 if (!NestedDefaultChecking) 5692 V.TraverseDecl(Field); 5693 5694 // CWG1815 5695 // Support lifetime extension of temporary created by aggregate 5696 // initialization using a default member initializer. We should rebuild 5697 // the initializer in a lifetime extension context if the initializer 5698 // expression is an ExprWithCleanups. Then make sure the normal lifetime 5699 // extension code recurses into the default initializer and does lifetime 5700 // extension when warranted. 5701 bool ContainsAnyTemporaries = 5702 isa_and_present<ExprWithCleanups>(Field->getInClassInitializer()); 5703 if (Field->getInClassInitializer() && 5704 !Field->getInClassInitializer()->containsErrors() && 5705 (V.HasImmediateCalls || (NeedRebuild && ContainsAnyTemporaries))) { 5706 ExprEvalContexts.back().DelayedDefaultInitializationContext = {Loc, Field, 5707 CurContext}; 5708 ExprEvalContexts.back().IsCurrentlyCheckingDefaultArgumentOrInitializer = 5709 NestedDefaultChecking; 5710 // Pass down lifetime extending flag, and collect temporaries in 5711 // CreateMaterializeTemporaryExpr when we rewrite the call argument. 5712 currentEvaluationContext().InLifetimeExtendingContext = 5713 parentEvaluationContext().InLifetimeExtendingContext; 5714 EnsureImmediateInvocationInDefaultArgs Immediate(*this); 5715 ExprResult Res; 5716 runWithSufficientStackSpace(Loc, [&] { 5717 Res = Immediate.TransformInitializer(Field->getInClassInitializer(), 5718 /*CXXDirectInit=*/false); 5719 }); 5720 if (!Res.isInvalid()) 5721 Res = ConvertMemberDefaultInitExpression(Field, Res.get(), Loc); 5722 if (Res.isInvalid()) { 5723 Field->setInvalidDecl(); 5724 return ExprError(); 5725 } 5726 Init = Res.get(); 5727 } 5728 5729 if (Field->getInClassInitializer()) { 5730 Expr *E = Init ? Init : Field->getInClassInitializer(); 5731 if (!NestedDefaultChecking) 5732 runWithSufficientStackSpace(Loc, [&] { 5733 MarkDeclarationsReferencedInExpr(E, /*SkipLocalVariables=*/false); 5734 }); 5735 if (isInLifetimeExtendingContext()) 5736 DiscardCleanupsInEvaluationContext(); 5737 // C++11 [class.base.init]p7: 5738 // The initialization of each base and member constitutes a 5739 // full-expression. 5740 ExprResult Res = ActOnFinishFullExpr(E, /*DiscardedValue=*/false); 5741 if (Res.isInvalid()) { 5742 Field->setInvalidDecl(); 5743 return ExprError(); 5744 } 5745 Init = Res.get(); 5746 5747 return CXXDefaultInitExpr::Create(Context, InitializationContext->Loc, 5748 Field, InitializationContext->Context, 5749 Init); 5750 } 5751 5752 // DR1351: 5753 // If the brace-or-equal-initializer of a non-static data member 5754 // invokes a defaulted default constructor of its class or of an 5755 // enclosing class in a potentially evaluated subexpression, the 5756 // program is ill-formed. 5757 // 5758 // This resolution is unworkable: the exception specification of the 5759 // default constructor can be needed in an unevaluated context, in 5760 // particular, in the operand of a noexcept-expression, and we can be 5761 // unable to compute an exception specification for an enclosed class. 5762 // 5763 // Any attempt to resolve the exception specification of a defaulted default 5764 // constructor before the initializer is lexically complete will ultimately 5765 // come here at which point we can diagnose it. 5766 RecordDecl *OutermostClass = ParentRD->getOuterLexicalRecordContext(); 5767 Diag(Loc, diag::err_default_member_initializer_not_yet_parsed) 5768 << OutermostClass << Field; 5769 Diag(Field->getEndLoc(), 5770 diag::note_default_member_initializer_not_yet_parsed); 5771 // Recover by marking the field invalid, unless we're in a SFINAE context. 5772 if (!isSFINAEContext()) 5773 Field->setInvalidDecl(); 5774 return ExprError(); 5775 } 5776 5777 VariadicCallType Sema::getVariadicCallType(FunctionDecl *FDecl, 5778 const FunctionProtoType *Proto, 5779 Expr *Fn) { 5780 if (Proto && Proto->isVariadic()) { 5781 if (isa_and_nonnull<CXXConstructorDecl>(FDecl)) 5782 return VariadicCallType::Constructor; 5783 else if (Fn && Fn->getType()->isBlockPointerType()) 5784 return VariadicCallType::Block; 5785 else if (FDecl) { 5786 if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) 5787 if (Method->isInstance()) 5788 return VariadicCallType::Method; 5789 } else if (Fn && Fn->getType() == Context.BoundMemberTy) 5790 return VariadicCallType::Method; 5791 return VariadicCallType::Function; 5792 } 5793 return VariadicCallType::DoesNotApply; 5794 } 5795 5796 namespace { 5797 class FunctionCallCCC final : public FunctionCallFilterCCC { 5798 public: 5799 FunctionCallCCC(Sema &SemaRef, const IdentifierInfo *FuncName, 5800 unsigned NumArgs, MemberExpr *ME) 5801 : FunctionCallFilterCCC(SemaRef, NumArgs, false, ME), 5802 FunctionName(FuncName) {} 5803 5804 bool ValidateCandidate(const TypoCorrection &candidate) override { 5805 if (!candidate.getCorrectionSpecifier() || 5806 candidate.getCorrectionAsIdentifierInfo() != FunctionName) { 5807 return false; 5808 } 5809 5810 return FunctionCallFilterCCC::ValidateCandidate(candidate); 5811 } 5812 5813 std::unique_ptr<CorrectionCandidateCallback> clone() override { 5814 return std::make_unique<FunctionCallCCC>(*this); 5815 } 5816 5817 private: 5818 const IdentifierInfo *const FunctionName; 5819 }; 5820 } 5821 5822 static TypoCorrection TryTypoCorrectionForCall(Sema &S, Expr *Fn, 5823 FunctionDecl *FDecl, 5824 ArrayRef<Expr *> Args) { 5825 MemberExpr *ME = dyn_cast<MemberExpr>(Fn); 5826 DeclarationName FuncName = FDecl->getDeclName(); 5827 SourceLocation NameLoc = ME ? ME->getMemberLoc() : Fn->getBeginLoc(); 5828 5829 FunctionCallCCC CCC(S, FuncName.getAsIdentifierInfo(), Args.size(), ME); 5830 if (TypoCorrection Corrected = S.CorrectTypo( 5831 DeclarationNameInfo(FuncName, NameLoc), Sema::LookupOrdinaryName, 5832 S.getScopeForContext(S.CurContext), nullptr, CCC, 5833 CorrectTypoKind::ErrorRecovery)) { 5834 if (NamedDecl *ND = Corrected.getFoundDecl()) { 5835 if (Corrected.isOverloaded()) { 5836 OverloadCandidateSet OCS(NameLoc, OverloadCandidateSet::CSK_Normal); 5837 OverloadCandidateSet::iterator Best; 5838 for (NamedDecl *CD : Corrected) { 5839 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) 5840 S.AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), Args, 5841 OCS); 5842 } 5843 switch (OCS.BestViableFunction(S, NameLoc, Best)) { 5844 case OR_Success: 5845 ND = Best->FoundDecl; 5846 Corrected.setCorrectionDecl(ND); 5847 break; 5848 default: 5849 break; 5850 } 5851 } 5852 ND = ND->getUnderlyingDecl(); 5853 if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) 5854 return Corrected; 5855 } 5856 } 5857 return TypoCorrection(); 5858 } 5859 5860 // [C++26][[expr.unary.op]/p4 5861 // A pointer to member is only formed when an explicit & 5862 // is used and its operand is a qualified-id not enclosed in parentheses. 5863 static bool isParenthetizedAndQualifiedAddressOfExpr(Expr *Fn) { 5864 if (!isa<ParenExpr>(Fn)) 5865 return false; 5866 5867 Fn = Fn->IgnoreParens(); 5868 5869 auto *UO = dyn_cast<UnaryOperator>(Fn); 5870 if (!UO || UO->getOpcode() != clang::UO_AddrOf) 5871 return false; 5872 if (auto *DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr()->IgnoreParens())) { 5873 return DRE->hasQualifier(); 5874 } 5875 if (auto *OVL = dyn_cast<OverloadExpr>(UO->getSubExpr()->IgnoreParens())) 5876 return OVL->getQualifier(); 5877 return false; 5878 } 5879 5880 bool 5881 Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, 5882 FunctionDecl *FDecl, 5883 const FunctionProtoType *Proto, 5884 ArrayRef<Expr *> Args, 5885 SourceLocation RParenLoc, 5886 bool IsExecConfig) { 5887 // Bail out early if calling a builtin with custom typechecking. 5888 if (FDecl) 5889 if (unsigned ID = FDecl->getBuiltinID()) 5890 if (Context.BuiltinInfo.hasCustomTypechecking(ID)) 5891 return false; 5892 5893 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by 5894 // assignment, to the types of the corresponding parameter, ... 5895 5896 bool AddressOf = isParenthetizedAndQualifiedAddressOfExpr(Fn); 5897 bool HasExplicitObjectParameter = 5898 !AddressOf && FDecl && FDecl->hasCXXExplicitFunctionObjectParameter(); 5899 unsigned ExplicitObjectParameterOffset = HasExplicitObjectParameter ? 1 : 0; 5900 unsigned NumParams = Proto->getNumParams(); 5901 bool Invalid = false; 5902 unsigned MinArgs = FDecl ? FDecl->getMinRequiredArguments() : NumParams; 5903 unsigned FnKind = Fn->getType()->isBlockPointerType() 5904 ? 1 /* block */ 5905 : (IsExecConfig ? 3 /* kernel function (exec config) */ 5906 : 0 /* function */); 5907 5908 // If too few arguments are available (and we don't have default 5909 // arguments for the remaining parameters), don't make the call. 5910 if (Args.size() < NumParams) { 5911 if (Args.size() < MinArgs) { 5912 TypoCorrection TC; 5913 if (FDecl && (TC = TryTypoCorrectionForCall(*this, Fn, FDecl, Args))) { 5914 unsigned diag_id = 5915 MinArgs == NumParams && !Proto->isVariadic() 5916 ? diag::err_typecheck_call_too_few_args_suggest 5917 : diag::err_typecheck_call_too_few_args_at_least_suggest; 5918 diagnoseTypo( 5919 TC, PDiag(diag_id) 5920 << FnKind << MinArgs - ExplicitObjectParameterOffset 5921 << static_cast<unsigned>(Args.size()) - 5922 ExplicitObjectParameterOffset 5923 << HasExplicitObjectParameter << TC.getCorrectionRange()); 5924 } else if (MinArgs - ExplicitObjectParameterOffset == 1 && FDecl && 5925 FDecl->getParamDecl(ExplicitObjectParameterOffset) 5926 ->getDeclName()) 5927 Diag(RParenLoc, 5928 MinArgs == NumParams && !Proto->isVariadic() 5929 ? diag::err_typecheck_call_too_few_args_one 5930 : diag::err_typecheck_call_too_few_args_at_least_one) 5931 << FnKind << FDecl->getParamDecl(ExplicitObjectParameterOffset) 5932 << HasExplicitObjectParameter << Fn->getSourceRange(); 5933 else 5934 Diag(RParenLoc, MinArgs == NumParams && !Proto->isVariadic() 5935 ? diag::err_typecheck_call_too_few_args 5936 : diag::err_typecheck_call_too_few_args_at_least) 5937 << FnKind << MinArgs - ExplicitObjectParameterOffset 5938 << static_cast<unsigned>(Args.size()) - 5939 ExplicitObjectParameterOffset 5940 << HasExplicitObjectParameter << Fn->getSourceRange(); 5941 5942 // Emit the location of the prototype. 5943 if (!TC && FDecl && !FDecl->getBuiltinID() && !IsExecConfig) 5944 Diag(FDecl->getLocation(), diag::note_callee_decl) 5945 << FDecl << FDecl->getParametersSourceRange(); 5946 5947 return true; 5948 } 5949 // We reserve space for the default arguments when we create 5950 // the call expression, before calling ConvertArgumentsForCall. 5951 assert((Call->getNumArgs() == NumParams) && 5952 "We should have reserved space for the default arguments before!"); 5953 } 5954 5955 // If too many are passed and not variadic, error on the extras and drop 5956 // them. 5957 if (Args.size() > NumParams) { 5958 if (!Proto->isVariadic()) { 5959 TypoCorrection TC; 5960 if (FDecl && (TC = TryTypoCorrectionForCall(*this, Fn, FDecl, Args))) { 5961 unsigned diag_id = 5962 MinArgs == NumParams && !Proto->isVariadic() 5963 ? diag::err_typecheck_call_too_many_args_suggest 5964 : diag::err_typecheck_call_too_many_args_at_most_suggest; 5965 diagnoseTypo( 5966 TC, PDiag(diag_id) 5967 << FnKind << NumParams - ExplicitObjectParameterOffset 5968 << static_cast<unsigned>(Args.size()) - 5969 ExplicitObjectParameterOffset 5970 << HasExplicitObjectParameter << TC.getCorrectionRange()); 5971 } else if (NumParams - ExplicitObjectParameterOffset == 1 && FDecl && 5972 FDecl->getParamDecl(ExplicitObjectParameterOffset) 5973 ->getDeclName()) 5974 Diag(Args[NumParams]->getBeginLoc(), 5975 MinArgs == NumParams 5976 ? diag::err_typecheck_call_too_many_args_one 5977 : diag::err_typecheck_call_too_many_args_at_most_one) 5978 << FnKind << FDecl->getParamDecl(ExplicitObjectParameterOffset) 5979 << static_cast<unsigned>(Args.size()) - 5980 ExplicitObjectParameterOffset 5981 << HasExplicitObjectParameter << Fn->getSourceRange() 5982 << SourceRange(Args[NumParams]->getBeginLoc(), 5983 Args.back()->getEndLoc()); 5984 else 5985 Diag(Args[NumParams]->getBeginLoc(), 5986 MinArgs == NumParams 5987 ? diag::err_typecheck_call_too_many_args 5988 : diag::err_typecheck_call_too_many_args_at_most) 5989 << FnKind << NumParams - ExplicitObjectParameterOffset 5990 << static_cast<unsigned>(Args.size()) - 5991 ExplicitObjectParameterOffset 5992 << HasExplicitObjectParameter << Fn->getSourceRange() 5993 << SourceRange(Args[NumParams]->getBeginLoc(), 5994 Args.back()->getEndLoc()); 5995 5996 // Emit the location of the prototype. 5997 if (!TC && FDecl && !FDecl->getBuiltinID() && !IsExecConfig) 5998 Diag(FDecl->getLocation(), diag::note_callee_decl) 5999 << FDecl << FDecl->getParametersSourceRange(); 6000 6001 // This deletes the extra arguments. 6002 Call->shrinkNumArgs(NumParams); 6003 return true; 6004 } 6005 } 6006 SmallVector<Expr *, 8> AllArgs; 6007 VariadicCallType CallType = getVariadicCallType(FDecl, Proto, Fn); 6008 6009 Invalid = GatherArgumentsForCall(Call->getExprLoc(), FDecl, Proto, 0, Args, 6010 AllArgs, CallType); 6011 if (Invalid) 6012 return true; 6013 unsigned TotalNumArgs = AllArgs.size(); 6014 for (unsigned i = 0; i < TotalNumArgs; ++i) 6015 Call->setArg(i, AllArgs[i]); 6016 6017 Call->computeDependence(); 6018 return false; 6019 } 6020 6021 bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, FunctionDecl *FDecl, 6022 const FunctionProtoType *Proto, 6023 unsigned FirstParam, ArrayRef<Expr *> Args, 6024 SmallVectorImpl<Expr *> &AllArgs, 6025 VariadicCallType CallType, bool AllowExplicit, 6026 bool IsListInitialization) { 6027 unsigned NumParams = Proto->getNumParams(); 6028 bool Invalid = false; 6029 size_t ArgIx = 0; 6030 // Continue to check argument types (even if we have too few/many args). 6031 for (unsigned i = FirstParam; i < NumParams; i++) { 6032 QualType ProtoArgType = Proto->getParamType(i); 6033 6034 Expr *Arg; 6035 ParmVarDecl *Param = FDecl ? FDecl->getParamDecl(i) : nullptr; 6036 if (ArgIx < Args.size()) { 6037 Arg = Args[ArgIx++]; 6038 6039 if (RequireCompleteType(Arg->getBeginLoc(), ProtoArgType, 6040 diag::err_call_incomplete_argument, Arg)) 6041 return true; 6042 6043 // Strip the unbridged-cast placeholder expression off, if applicable. 6044 bool CFAudited = false; 6045 if (Arg->getType() == Context.ARCUnbridgedCastTy && 6046 FDecl && FDecl->hasAttr<CFAuditedTransferAttr>() && 6047 (!Param || !Param->hasAttr<CFConsumedAttr>())) 6048 Arg = ObjC().stripARCUnbridgedCast(Arg); 6049 else if (getLangOpts().ObjCAutoRefCount && 6050 FDecl && FDecl->hasAttr<CFAuditedTransferAttr>() && 6051 (!Param || !Param->hasAttr<CFConsumedAttr>())) 6052 CFAudited = true; 6053 6054 if (Proto->getExtParameterInfo(i).isNoEscape() && 6055 ProtoArgType->isBlockPointerType()) 6056 if (auto *BE = dyn_cast<BlockExpr>(Arg->IgnoreParenNoopCasts(Context))) 6057 BE->getBlockDecl()->setDoesNotEscape(); 6058 if ((Proto->getExtParameterInfo(i).getABI() == ParameterABI::HLSLOut || 6059 Proto->getExtParameterInfo(i).getABI() == ParameterABI::HLSLInOut)) { 6060 ExprResult ArgExpr = HLSL().ActOnOutParamExpr(Param, Arg); 6061 if (ArgExpr.isInvalid()) 6062 return true; 6063 Arg = ArgExpr.getAs<Expr>(); 6064 } 6065 6066 InitializedEntity Entity = 6067 Param ? InitializedEntity::InitializeParameter(Context, Param, 6068 ProtoArgType) 6069 : InitializedEntity::InitializeParameter( 6070 Context, ProtoArgType, Proto->isParamConsumed(i)); 6071 6072 // Remember that parameter belongs to a CF audited API. 6073 if (CFAudited) 6074 Entity.setParameterCFAudited(); 6075 6076 ExprResult ArgE = PerformCopyInitialization( 6077 Entity, SourceLocation(), Arg, IsListInitialization, AllowExplicit); 6078 if (ArgE.isInvalid()) 6079 return true; 6080 6081 Arg = ArgE.getAs<Expr>(); 6082 } else { 6083 assert(Param && "can't use default arguments without a known callee"); 6084 6085 ExprResult ArgExpr = BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); 6086 if (ArgExpr.isInvalid()) 6087 return true; 6088 6089 Arg = ArgExpr.getAs<Expr>(); 6090 } 6091 6092 // Check for array bounds violations for each argument to the call. This 6093 // check only triggers warnings when the argument isn't a more complex Expr 6094 // with its own checking, such as a BinaryOperator. 6095 CheckArrayAccess(Arg); 6096 6097 // Check for violations of C99 static array rules (C99 6.7.5.3p7). 6098 CheckStaticArrayArgument(CallLoc, Param, Arg); 6099 6100 AllArgs.push_back(Arg); 6101 } 6102 6103 // If this is a variadic call, handle args passed through "...". 6104 if (CallType != VariadicCallType::DoesNotApply) { 6105 // Assume that extern "C" functions with variadic arguments that 6106 // return __unknown_anytype aren't *really* variadic. 6107 if (Proto->getReturnType() == Context.UnknownAnyTy && FDecl && 6108 FDecl->isExternC()) { 6109 for (Expr *A : Args.slice(ArgIx)) { 6110 QualType paramType; // ignored 6111 ExprResult arg = checkUnknownAnyArg(CallLoc, A, paramType); 6112 Invalid |= arg.isInvalid(); 6113 AllArgs.push_back(arg.get()); 6114 } 6115 6116 // Otherwise do argument promotion, (C99 6.5.2.2p7). 6117 } else { 6118 for (Expr *A : Args.slice(ArgIx)) { 6119 ExprResult Arg = DefaultVariadicArgumentPromotion(A, CallType, FDecl); 6120 Invalid |= Arg.isInvalid(); 6121 AllArgs.push_back(Arg.get()); 6122 } 6123 } 6124 6125 // Check for array bounds violations. 6126 for (Expr *A : Args.slice(ArgIx)) 6127 CheckArrayAccess(A); 6128 } 6129 return Invalid; 6130 } 6131 6132 static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) { 6133 TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc(); 6134 if (DecayedTypeLoc DTL = TL.getAs<DecayedTypeLoc>()) 6135 TL = DTL.getOriginalLoc(); 6136 if (ArrayTypeLoc ATL = TL.getAs<ArrayTypeLoc>()) 6137 S.Diag(PVD->getLocation(), diag::note_callee_static_array) 6138 << ATL.getLocalSourceRange(); 6139 } 6140 6141 void 6142 Sema::CheckStaticArrayArgument(SourceLocation CallLoc, 6143 ParmVarDecl *Param, 6144 const Expr *ArgExpr) { 6145 // Static array parameters are not supported in C++. 6146 if (!Param || getLangOpts().CPlusPlus) 6147 return; 6148 6149 QualType OrigTy = Param->getOriginalType(); 6150 6151 const ArrayType *AT = Context.getAsArrayType(OrigTy); 6152 if (!AT || AT->getSizeModifier() != ArraySizeModifier::Static) 6153 return; 6154 6155 if (ArgExpr->isNullPointerConstant(Context, 6156 Expr::NPC_NeverValueDependent)) { 6157 Diag(CallLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); 6158 DiagnoseCalleeStaticArrayParam(*this, Param); 6159 return; 6160 } 6161 6162 const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT); 6163 if (!CAT) 6164 return; 6165 6166 const ConstantArrayType *ArgCAT = 6167 Context.getAsConstantArrayType(ArgExpr->IgnoreParenCasts()->getType()); 6168 if (!ArgCAT) 6169 return; 6170 6171 if (getASTContext().hasSameUnqualifiedType(CAT->getElementType(), 6172 ArgCAT->getElementType())) { 6173 if (ArgCAT->getSize().ult(CAT->getSize())) { 6174 Diag(CallLoc, diag::warn_static_array_too_small) 6175 << ArgExpr->getSourceRange() << (unsigned)ArgCAT->getZExtSize() 6176 << (unsigned)CAT->getZExtSize() << 0; 6177 DiagnoseCalleeStaticArrayParam(*this, Param); 6178 } 6179 return; 6180 } 6181 6182 std::optional<CharUnits> ArgSize = 6183 getASTContext().getTypeSizeInCharsIfKnown(ArgCAT); 6184 std::optional<CharUnits> ParmSize = 6185 getASTContext().getTypeSizeInCharsIfKnown(CAT); 6186 if (ArgSize && ParmSize && *ArgSize < *ParmSize) { 6187 Diag(CallLoc, diag::warn_static_array_too_small) 6188 << ArgExpr->getSourceRange() << (unsigned)ArgSize->getQuantity() 6189 << (unsigned)ParmSize->getQuantity() << 1; 6190 DiagnoseCalleeStaticArrayParam(*this, Param); 6191 } 6192 } 6193 6194 /// Given a function expression of unknown-any type, try to rebuild it 6195 /// to have a function type. 6196 static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); 6197 6198 /// Is the given type a placeholder that we need to lower out 6199 /// immediately during argument processing? 6200 static bool isPlaceholderToRemoveAsArg(QualType type) { 6201 // Placeholders are never sugared. 6202 const BuiltinType *placeholder = dyn_cast<BuiltinType>(type); 6203 if (!placeholder) return false; 6204 6205 switch (placeholder->getKind()) { 6206 // Ignore all the non-placeholder types. 6207 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6208 case BuiltinType::Id: 6209 #include "clang/Basic/OpenCLImageTypes.def" 6210 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 6211 case BuiltinType::Id: 6212 #include "clang/Basic/OpenCLExtensionTypes.def" 6213 // In practice we'll never use this, since all SVE types are sugared 6214 // via TypedefTypes rather than exposed directly as BuiltinTypes. 6215 #define SVE_TYPE(Name, Id, SingletonId) \ 6216 case BuiltinType::Id: 6217 #include "clang/Basic/AArch64ACLETypes.def" 6218 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 6219 case BuiltinType::Id: 6220 #include "clang/Basic/PPCTypes.def" 6221 #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: 6222 #include "clang/Basic/RISCVVTypes.def" 6223 #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: 6224 #include "clang/Basic/WebAssemblyReferenceTypes.def" 6225 #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id: 6226 #include "clang/Basic/AMDGPUTypes.def" 6227 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: 6228 #include "clang/Basic/HLSLIntangibleTypes.def" 6229 #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) 6230 #define BUILTIN_TYPE(ID, SINGLETON_ID) case BuiltinType::ID: 6231 #include "clang/AST/BuiltinTypes.def" 6232 return false; 6233 6234 case BuiltinType::UnresolvedTemplate: 6235 // We cannot lower out overload sets; they might validly be resolved 6236 // by the call machinery. 6237 case BuiltinType::Overload: 6238 return false; 6239 6240 // Unbridged casts in ARC can be handled in some call positions and 6241 // should be left in place. 6242 case BuiltinType::ARCUnbridgedCast: 6243 return false; 6244 6245 // Pseudo-objects should be converted as soon as possible. 6246 case BuiltinType::PseudoObject: 6247 return true; 6248 6249 // The debugger mode could theoretically but currently does not try 6250 // to resolve unknown-typed arguments based on known parameter types. 6251 case BuiltinType::UnknownAny: 6252 return true; 6253 6254 // These are always invalid as call arguments and should be reported. 6255 case BuiltinType::BoundMember: 6256 case BuiltinType::BuiltinFn: 6257 case BuiltinType::IncompleteMatrixIdx: 6258 case BuiltinType::ArraySection: 6259 case BuiltinType::OMPArrayShaping: 6260 case BuiltinType::OMPIterator: 6261 return true; 6262 6263 } 6264 llvm_unreachable("bad builtin type kind"); 6265 } 6266 6267 bool Sema::CheckArgsForPlaceholders(MultiExprArg args) { 6268 // Apply this processing to all the arguments at once instead of 6269 // dying at the first failure. 6270 bool hasInvalid = false; 6271 for (size_t i = 0, e = args.size(); i != e; i++) { 6272 if (isPlaceholderToRemoveAsArg(args[i]->getType())) { 6273 ExprResult result = CheckPlaceholderExpr(args[i]); 6274 if (result.isInvalid()) hasInvalid = true; 6275 else args[i] = result.get(); 6276 } 6277 } 6278 return hasInvalid; 6279 } 6280 6281 /// If a builtin function has a pointer argument with no explicit address 6282 /// space, then it should be able to accept a pointer to any address 6283 /// space as input. In order to do this, we need to replace the 6284 /// standard builtin declaration with one that uses the same address space 6285 /// as the call. 6286 /// 6287 /// \returns nullptr If this builtin is not a candidate for a rewrite i.e. 6288 /// it does not contain any pointer arguments without 6289 /// an address space qualifer. Otherwise the rewritten 6290 /// FunctionDecl is returned. 6291 /// TODO: Handle pointer return types. 6292 static FunctionDecl *rewriteBuiltinFunctionDecl(Sema *Sema, ASTContext &Context, 6293 FunctionDecl *FDecl, 6294 MultiExprArg ArgExprs) { 6295 6296 QualType DeclType = FDecl->getType(); 6297 const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(DeclType); 6298 6299 if (!Context.BuiltinInfo.hasPtrArgsOrResult(FDecl->getBuiltinID()) || !FT || 6300 ArgExprs.size() < FT->getNumParams()) 6301 return nullptr; 6302 6303 bool NeedsNewDecl = false; 6304 unsigned i = 0; 6305 SmallVector<QualType, 8> OverloadParams; 6306 6307 for (QualType ParamType : FT->param_types()) { 6308 6309 // Convert array arguments to pointer to simplify type lookup. 6310 ExprResult ArgRes = 6311 Sema->DefaultFunctionArrayLvalueConversion(ArgExprs[i++]); 6312 if (ArgRes.isInvalid()) 6313 return nullptr; 6314 Expr *Arg = ArgRes.get(); 6315 QualType ArgType = Arg->getType(); 6316 if (!ParamType->isPointerType() || 6317 ParamType->getPointeeType().hasAddressSpace() || 6318 !ArgType->isPointerType() || 6319 !ArgType->getPointeeType().hasAddressSpace() || 6320 isPtrSizeAddressSpace(ArgType->getPointeeType().getAddressSpace())) { 6321 OverloadParams.push_back(ParamType); 6322 continue; 6323 } 6324 6325 QualType PointeeType = ParamType->getPointeeType(); 6326 NeedsNewDecl = true; 6327 LangAS AS = ArgType->getPointeeType().getAddressSpace(); 6328 6329 PointeeType = Context.getAddrSpaceQualType(PointeeType, AS); 6330 OverloadParams.push_back(Context.getPointerType(PointeeType)); 6331 } 6332 6333 if (!NeedsNewDecl) 6334 return nullptr; 6335 6336 FunctionProtoType::ExtProtoInfo EPI; 6337 EPI.Variadic = FT->isVariadic(); 6338 QualType OverloadTy = Context.getFunctionType(FT->getReturnType(), 6339 OverloadParams, EPI); 6340 DeclContext *Parent = FDecl->getParent(); 6341 FunctionDecl *OverloadDecl = FunctionDecl::Create( 6342 Context, Parent, FDecl->getLocation(), FDecl->getLocation(), 6343 FDecl->getIdentifier(), OverloadTy, 6344 /*TInfo=*/nullptr, SC_Extern, Sema->getCurFPFeatures().isFPConstrained(), 6345 false, 6346 /*hasPrototype=*/true); 6347 SmallVector<ParmVarDecl*, 16> Params; 6348 FT = cast<FunctionProtoType>(OverloadTy); 6349 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { 6350 QualType ParamType = FT->getParamType(i); 6351 ParmVarDecl *Parm = 6352 ParmVarDecl::Create(Context, OverloadDecl, SourceLocation(), 6353 SourceLocation(), nullptr, ParamType, 6354 /*TInfo=*/nullptr, SC_None, nullptr); 6355 Parm->setScopeInfo(0, i); 6356 Params.push_back(Parm); 6357 } 6358 OverloadDecl->setParams(Params); 6359 // We cannot merge host/device attributes of redeclarations. They have to 6360 // be consistent when created. 6361 if (Sema->LangOpts.CUDA) { 6362 if (FDecl->hasAttr<CUDAHostAttr>()) 6363 OverloadDecl->addAttr(CUDAHostAttr::CreateImplicit(Context)); 6364 if (FDecl->hasAttr<CUDADeviceAttr>()) 6365 OverloadDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context)); 6366 } 6367 Sema->mergeDeclAttributes(OverloadDecl, FDecl); 6368 return OverloadDecl; 6369 } 6370 6371 static void checkDirectCallValidity(Sema &S, const Expr *Fn, 6372 FunctionDecl *Callee, 6373 MultiExprArg ArgExprs) { 6374 // `Callee` (when called with ArgExprs) may be ill-formed. enable_if (and 6375 // similar attributes) really don't like it when functions are called with an 6376 // invalid number of args. 6377 if (S.TooManyArguments(Callee->getNumParams(), ArgExprs.size(), 6378 /*PartialOverloading=*/false) && 6379 !Callee->isVariadic()) 6380 return; 6381 if (Callee->getMinRequiredArguments() > ArgExprs.size()) 6382 return; 6383 6384 if (const EnableIfAttr *Attr = 6385 S.CheckEnableIf(Callee, Fn->getBeginLoc(), ArgExprs, true)) { 6386 S.Diag(Fn->getBeginLoc(), 6387 isa<CXXMethodDecl>(Callee) 6388 ? diag::err_ovl_no_viable_member_function_in_call 6389 : diag::err_ovl_no_viable_function_in_call) 6390 << Callee << Callee->getSourceRange(); 6391 S.Diag(Callee->getLocation(), 6392 diag::note_ovl_candidate_disabled_by_function_cond_attr) 6393 << Attr->getCond()->getSourceRange() << Attr->getMessage(); 6394 return; 6395 } 6396 } 6397 6398 static bool enclosingClassIsRelatedToClassInWhichMembersWereFound( 6399 const UnresolvedMemberExpr *const UME, Sema &S) { 6400 6401 const auto GetFunctionLevelDCIfCXXClass = 6402 [](Sema &S) -> const CXXRecordDecl * { 6403 const DeclContext *const DC = S.getFunctionLevelDeclContext(); 6404 if (!DC || !DC->getParent()) 6405 return nullptr; 6406 6407 // If the call to some member function was made from within a member 6408 // function body 'M' return return 'M's parent. 6409 if (const auto *MD = dyn_cast<CXXMethodDecl>(DC)) 6410 return MD->getParent()->getCanonicalDecl(); 6411 // else the call was made from within a default member initializer of a 6412 // class, so return the class. 6413 if (const auto *RD = dyn_cast<CXXRecordDecl>(DC)) 6414 return RD->getCanonicalDecl(); 6415 return nullptr; 6416 }; 6417 // If our DeclContext is neither a member function nor a class (in the 6418 // case of a lambda in a default member initializer), we can't have an 6419 // enclosing 'this'. 6420 6421 const CXXRecordDecl *const CurParentClass = GetFunctionLevelDCIfCXXClass(S); 6422 if (!CurParentClass) 6423 return false; 6424 6425 // The naming class for implicit member functions call is the class in which 6426 // name lookup starts. 6427 const CXXRecordDecl *const NamingClass = 6428 UME->getNamingClass()->getCanonicalDecl(); 6429 assert(NamingClass && "Must have naming class even for implicit access"); 6430 6431 // If the unresolved member functions were found in a 'naming class' that is 6432 // related (either the same or derived from) to the class that contains the 6433 // member function that itself contained the implicit member access. 6434 6435 return CurParentClass == NamingClass || 6436 CurParentClass->isDerivedFrom(NamingClass); 6437 } 6438 6439 static void 6440 tryImplicitlyCaptureThisIfImplicitMemberFunctionAccessWithDependentArgs( 6441 Sema &S, const UnresolvedMemberExpr *const UME, SourceLocation CallLoc) { 6442 6443 if (!UME) 6444 return; 6445 6446 LambdaScopeInfo *const CurLSI = S.getCurLambda(); 6447 // Only try and implicitly capture 'this' within a C++ Lambda if it hasn't 6448 // already been captured, or if this is an implicit member function call (if 6449 // it isn't, an attempt to capture 'this' should already have been made). 6450 if (!CurLSI || CurLSI->ImpCaptureStyle == CurLSI->ImpCap_None || 6451 !UME->isImplicitAccess() || CurLSI->isCXXThisCaptured()) 6452 return; 6453 6454 // Check if the naming class in which the unresolved members were found is 6455 // related (same as or is a base of) to the enclosing class. 6456 6457 if (!enclosingClassIsRelatedToClassInWhichMembersWereFound(UME, S)) 6458 return; 6459 6460 6461 DeclContext *EnclosingFunctionCtx = S.CurContext->getParent()->getParent(); 6462 // If the enclosing function is not dependent, then this lambda is 6463 // capture ready, so if we can capture this, do so. 6464 if (!EnclosingFunctionCtx->isDependentContext()) { 6465 // If the current lambda and all enclosing lambdas can capture 'this' - 6466 // then go ahead and capture 'this' (since our unresolved overload set 6467 // contains at least one non-static member function). 6468 if (!S.CheckCXXThisCapture(CallLoc, /*Explcit*/ false, /*Diagnose*/ false)) 6469 S.CheckCXXThisCapture(CallLoc); 6470 } else if (S.CurContext->isDependentContext()) { 6471 // ... since this is an implicit member reference, that might potentially 6472 // involve a 'this' capture, mark 'this' for potential capture in 6473 // enclosing lambdas. 6474 if (CurLSI->ImpCaptureStyle != CurLSI->ImpCap_None) 6475 CurLSI->addPotentialThisCapture(CallLoc); 6476 } 6477 } 6478 6479 // Once a call is fully resolved, warn for unqualified calls to specific 6480 // C++ standard functions, like move and forward. 6481 static void DiagnosedUnqualifiedCallsToStdFunctions(Sema &S, 6482 const CallExpr *Call) { 6483 // We are only checking unary move and forward so exit early here. 6484 if (Call->getNumArgs() != 1) 6485 return; 6486 6487 const Expr *E = Call->getCallee()->IgnoreParenImpCasts(); 6488 if (!E || isa<UnresolvedLookupExpr>(E)) 6489 return; 6490 const DeclRefExpr *DRE = dyn_cast_if_present<DeclRefExpr>(E); 6491 if (!DRE || !DRE->getLocation().isValid()) 6492 return; 6493 6494 if (DRE->getQualifier()) 6495 return; 6496 6497 const FunctionDecl *FD = Call->getDirectCallee(); 6498 if (!FD) 6499 return; 6500 6501 // Only warn for some functions deemed more frequent or problematic. 6502 unsigned BuiltinID = FD->getBuiltinID(); 6503 if (BuiltinID != Builtin::BImove && BuiltinID != Builtin::BIforward) 6504 return; 6505 6506 S.Diag(DRE->getLocation(), diag::warn_unqualified_call_to_std_cast_function) 6507 << FD->getQualifiedNameAsString() 6508 << FixItHint::CreateInsertion(DRE->getLocation(), "std::"); 6509 } 6510 6511 ExprResult Sema::ActOnCallExpr(Scope *Scope, Expr *Fn, SourceLocation LParenLoc, 6512 MultiExprArg ArgExprs, SourceLocation RParenLoc, 6513 Expr *ExecConfig) { 6514 ExprResult Call = 6515 BuildCallExpr(Scope, Fn, LParenLoc, ArgExprs, RParenLoc, ExecConfig, 6516 /*IsExecConfig=*/false, /*AllowRecovery=*/true); 6517 if (Call.isInvalid()) 6518 return Call; 6519 6520 // Diagnose uses of the C++20 "ADL-only template-id call" feature in earlier 6521 // language modes. 6522 if (const auto *ULE = dyn_cast<UnresolvedLookupExpr>(Fn); 6523 ULE && ULE->hasExplicitTemplateArgs() && 6524 ULE->decls_begin() == ULE->decls_end()) { 6525 DiagCompat(Fn->getExprLoc(), diag_compat::adl_only_template_id) 6526 << ULE->getName(); 6527 } 6528 6529 if (LangOpts.OpenMP) 6530 Call = OpenMP().ActOnOpenMPCall(Call, Scope, LParenLoc, ArgExprs, RParenLoc, 6531 ExecConfig); 6532 if (LangOpts.CPlusPlus) { 6533 if (const auto *CE = dyn_cast<CallExpr>(Call.get())) 6534 DiagnosedUnqualifiedCallsToStdFunctions(*this, CE); 6535 6536 // If we previously found that the id-expression of this call refers to a 6537 // consteval function but the call is dependent, we should not treat is an 6538 // an invalid immediate call. 6539 if (auto *DRE = dyn_cast<DeclRefExpr>(Fn->IgnoreParens()); 6540 DRE && Call.get()->isValueDependent()) { 6541 currentEvaluationContext().ReferenceToConsteval.erase(DRE); 6542 } 6543 } 6544 return Call; 6545 } 6546 6547 // Any type that could be used to form a callable expression 6548 static bool MayBeFunctionType(const ASTContext &Context, const Expr *E) { 6549 QualType T = E->getType(); 6550 if (T->isDependentType()) 6551 return true; 6552 6553 if (T == Context.BoundMemberTy || T == Context.UnknownAnyTy || 6554 T == Context.BuiltinFnTy || T == Context.OverloadTy || 6555 T->isFunctionType() || T->isFunctionReferenceType() || 6556 T->isMemberFunctionPointerType() || T->isFunctionPointerType() || 6557 T->isBlockPointerType() || T->isRecordType()) 6558 return true; 6559 6560 return isa<CallExpr, DeclRefExpr, MemberExpr, CXXPseudoDestructorExpr, 6561 OverloadExpr, UnresolvedMemberExpr, UnaryOperator>(E); 6562 } 6563 6564 ExprResult Sema::BuildCallExpr(Scope *Scope, Expr *Fn, SourceLocation LParenLoc, 6565 MultiExprArg ArgExprs, SourceLocation RParenLoc, 6566 Expr *ExecConfig, bool IsExecConfig, 6567 bool AllowRecovery) { 6568 // Since this might be a postfix expression, get rid of ParenListExprs. 6569 ExprResult Result = MaybeConvertParenListExprToParenExpr(Scope, Fn); 6570 if (Result.isInvalid()) return ExprError(); 6571 Fn = Result.get(); 6572 6573 if (CheckArgsForPlaceholders(ArgExprs)) 6574 return ExprError(); 6575 6576 // The result of __builtin_counted_by_ref cannot be used as a function 6577 // argument. It allows leaking and modification of bounds safety information. 6578 for (const Expr *Arg : ArgExprs) 6579 if (CheckInvalidBuiltinCountedByRef(Arg, 6580 BuiltinCountedByRefKind::FunctionArg)) 6581 return ExprError(); 6582 6583 if (getLangOpts().CPlusPlus) { 6584 // If this is a pseudo-destructor expression, build the call immediately. 6585 if (isa<CXXPseudoDestructorExpr>(Fn)) { 6586 if (!ArgExprs.empty()) { 6587 // Pseudo-destructor calls should not have any arguments. 6588 Diag(Fn->getBeginLoc(), diag::err_pseudo_dtor_call_with_args) 6589 << FixItHint::CreateRemoval( 6590 SourceRange(ArgExprs.front()->getBeginLoc(), 6591 ArgExprs.back()->getEndLoc())); 6592 } 6593 6594 return CallExpr::Create(Context, Fn, /*Args=*/{}, Context.VoidTy, 6595 VK_PRValue, RParenLoc, CurFPFeatureOverrides()); 6596 } 6597 if (Fn->getType() == Context.PseudoObjectTy) { 6598 ExprResult result = CheckPlaceholderExpr(Fn); 6599 if (result.isInvalid()) return ExprError(); 6600 Fn = result.get(); 6601 } 6602 6603 // Determine whether this is a dependent call inside a C++ template, 6604 // in which case we won't do any semantic analysis now. 6605 if (Fn->isTypeDependent() || Expr::hasAnyTypeDependentArguments(ArgExprs)) { 6606 if (ExecConfig) { 6607 return CUDAKernelCallExpr::Create(Context, Fn, 6608 cast<CallExpr>(ExecConfig), ArgExprs, 6609 Context.DependentTy, VK_PRValue, 6610 RParenLoc, CurFPFeatureOverrides()); 6611 } else { 6612 6613 tryImplicitlyCaptureThisIfImplicitMemberFunctionAccessWithDependentArgs( 6614 *this, dyn_cast<UnresolvedMemberExpr>(Fn->IgnoreParens()), 6615 Fn->getBeginLoc()); 6616 6617 // If the type of the function itself is not dependent 6618 // check that it is a reasonable as a function, as type deduction 6619 // later assume the CallExpr has a sensible TYPE. 6620 if (!MayBeFunctionType(Context, Fn)) 6621 return ExprError( 6622 Diag(LParenLoc, diag::err_typecheck_call_not_function) 6623 << Fn->getType() << Fn->getSourceRange()); 6624 6625 return CallExpr::Create(Context, Fn, ArgExprs, Context.DependentTy, 6626 VK_PRValue, RParenLoc, CurFPFeatureOverrides()); 6627 } 6628 } 6629 6630 // Determine whether this is a call to an object (C++ [over.call.object]). 6631 if (Fn->getType()->isRecordType()) 6632 return BuildCallToObjectOfClassType(Scope, Fn, LParenLoc, ArgExprs, 6633 RParenLoc); 6634 6635 if (Fn->getType() == Context.UnknownAnyTy) { 6636 ExprResult result = rebuildUnknownAnyFunction(*this, Fn); 6637 if (result.isInvalid()) return ExprError(); 6638 Fn = result.get(); 6639 } 6640 6641 if (Fn->getType() == Context.BoundMemberTy) { 6642 return BuildCallToMemberFunction(Scope, Fn, LParenLoc, ArgExprs, 6643 RParenLoc, ExecConfig, IsExecConfig, 6644 AllowRecovery); 6645 } 6646 } 6647 6648 // Check for overloaded calls. This can happen even in C due to extensions. 6649 if (Fn->getType() == Context.OverloadTy) { 6650 OverloadExpr::FindResult find = OverloadExpr::find(Fn); 6651 6652 // We aren't supposed to apply this logic if there's an '&' involved. 6653 if (!find.HasFormOfMemberPointer || find.IsAddressOfOperandWithParen) { 6654 if (Expr::hasAnyTypeDependentArguments(ArgExprs)) 6655 return CallExpr::Create(Context, Fn, ArgExprs, Context.DependentTy, 6656 VK_PRValue, RParenLoc, CurFPFeatureOverrides()); 6657 OverloadExpr *ovl = find.Expression; 6658 if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(ovl)) 6659 return BuildOverloadedCallExpr( 6660 Scope, Fn, ULE, LParenLoc, ArgExprs, RParenLoc, ExecConfig, 6661 /*AllowTypoCorrection=*/true, find.IsAddressOfOperand); 6662 return BuildCallToMemberFunction(Scope, Fn, LParenLoc, ArgExprs, 6663 RParenLoc, ExecConfig, IsExecConfig, 6664 AllowRecovery); 6665 } 6666 } 6667 6668 // If we're directly calling a function, get the appropriate declaration. 6669 if (Fn->getType() == Context.UnknownAnyTy) { 6670 ExprResult result = rebuildUnknownAnyFunction(*this, Fn); 6671 if (result.isInvalid()) return ExprError(); 6672 Fn = result.get(); 6673 } 6674 6675 Expr *NakedFn = Fn->IgnoreParens(); 6676 6677 bool CallingNDeclIndirectly = false; 6678 NamedDecl *NDecl = nullptr; 6679 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) { 6680 if (UnOp->getOpcode() == UO_AddrOf) { 6681 CallingNDeclIndirectly = true; 6682 NakedFn = UnOp->getSubExpr()->IgnoreParens(); 6683 } 6684 } 6685 6686 if (auto *DRE = dyn_cast<DeclRefExpr>(NakedFn)) { 6687 NDecl = DRE->getDecl(); 6688 6689 FunctionDecl *FDecl = dyn_cast<FunctionDecl>(NDecl); 6690 if (FDecl && FDecl->getBuiltinID()) { 6691 // Rewrite the function decl for this builtin by replacing parameters 6692 // with no explicit address space with the address space of the arguments 6693 // in ArgExprs. 6694 if ((FDecl = 6695 rewriteBuiltinFunctionDecl(this, Context, FDecl, ArgExprs))) { 6696 NDecl = FDecl; 6697 Fn = DeclRefExpr::Create( 6698 Context, FDecl->getQualifierLoc(), SourceLocation(), FDecl, false, 6699 SourceLocation(), FDecl->getType(), Fn->getValueKind(), FDecl, 6700 nullptr, DRE->isNonOdrUse()); 6701 } 6702 } 6703 } else if (auto *ME = dyn_cast<MemberExpr>(NakedFn)) 6704 NDecl = ME->getMemberDecl(); 6705 6706 if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(NDecl)) { 6707 if (CallingNDeclIndirectly && !checkAddressOfFunctionIsAvailable( 6708 FD, /*Complain=*/true, Fn->getBeginLoc())) 6709 return ExprError(); 6710 6711 checkDirectCallValidity(*this, Fn, FD, ArgExprs); 6712 6713 // If this expression is a call to a builtin function in HIP device 6714 // compilation, allow a pointer-type argument to default address space to be 6715 // passed as a pointer-type parameter to a non-default address space. 6716 // If Arg is declared in the default address space and Param is declared 6717 // in a non-default address space, perform an implicit address space cast to 6718 // the parameter type. 6719 if (getLangOpts().HIP && getLangOpts().CUDAIsDevice && FD && 6720 FD->getBuiltinID()) { 6721 for (unsigned Idx = 0; Idx < ArgExprs.size() && Idx < FD->param_size(); 6722 ++Idx) { 6723 ParmVarDecl *Param = FD->getParamDecl(Idx); 6724 if (!ArgExprs[Idx] || !Param || !Param->getType()->isPointerType() || 6725 !ArgExprs[Idx]->getType()->isPointerType()) 6726 continue; 6727 6728 auto ParamAS = Param->getType()->getPointeeType().getAddressSpace(); 6729 auto ArgTy = ArgExprs[Idx]->getType(); 6730 auto ArgPtTy = ArgTy->getPointeeType(); 6731 auto ArgAS = ArgPtTy.getAddressSpace(); 6732 6733 // Add address space cast if target address spaces are different 6734 bool NeedImplicitASC = 6735 ParamAS != LangAS::Default && // Pointer params in generic AS don't need special handling. 6736 ( ArgAS == LangAS::Default || // We do allow implicit conversion from generic AS 6737 // or from specific AS which has target AS matching that of Param. 6738 getASTContext().getTargetAddressSpace(ArgAS) == getASTContext().getTargetAddressSpace(ParamAS)); 6739 if (!NeedImplicitASC) 6740 continue; 6741 6742 // First, ensure that the Arg is an RValue. 6743 if (ArgExprs[Idx]->isGLValue()) { 6744 ArgExprs[Idx] = ImplicitCastExpr::Create( 6745 Context, ArgExprs[Idx]->getType(), CK_NoOp, ArgExprs[Idx], 6746 nullptr, VK_PRValue, FPOptionsOverride()); 6747 } 6748 6749 // Construct a new arg type with address space of Param 6750 Qualifiers ArgPtQuals = ArgPtTy.getQualifiers(); 6751 ArgPtQuals.setAddressSpace(ParamAS); 6752 auto NewArgPtTy = 6753 Context.getQualifiedType(ArgPtTy.getUnqualifiedType(), ArgPtQuals); 6754 auto NewArgTy = 6755 Context.getQualifiedType(Context.getPointerType(NewArgPtTy), 6756 ArgTy.getQualifiers()); 6757 6758 // Finally perform an implicit address space cast 6759 ArgExprs[Idx] = ImpCastExprToType(ArgExprs[Idx], NewArgTy, 6760 CK_AddressSpaceConversion) 6761 .get(); 6762 } 6763 } 6764 } 6765 6766 if (Context.isDependenceAllowed() && 6767 (Fn->isTypeDependent() || Expr::hasAnyTypeDependentArguments(ArgExprs))) { 6768 assert(!getLangOpts().CPlusPlus); 6769 assert((Fn->containsErrors() || 6770 llvm::any_of(ArgExprs, 6771 [](clang::Expr *E) { return E->containsErrors(); })) && 6772 "should only occur in error-recovery path."); 6773 return CallExpr::Create(Context, Fn, ArgExprs, Context.DependentTy, 6774 VK_PRValue, RParenLoc, CurFPFeatureOverrides()); 6775 } 6776 return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, ArgExprs, RParenLoc, 6777 ExecConfig, IsExecConfig); 6778 } 6779 6780 Expr *Sema::BuildBuiltinCallExpr(SourceLocation Loc, Builtin::ID Id, 6781 MultiExprArg CallArgs) { 6782 std::string Name = Context.BuiltinInfo.getName(Id); 6783 LookupResult R(*this, &Context.Idents.get(Name), Loc, 6784 Sema::LookupOrdinaryName); 6785 LookupName(R, TUScope, /*AllowBuiltinCreation=*/true); 6786 6787 auto *BuiltInDecl = R.getAsSingle<FunctionDecl>(); 6788 assert(BuiltInDecl && "failed to find builtin declaration"); 6789 6790 ExprResult DeclRef = 6791 BuildDeclRefExpr(BuiltInDecl, BuiltInDecl->getType(), VK_LValue, Loc); 6792 assert(DeclRef.isUsable() && "Builtin reference cannot fail"); 6793 6794 ExprResult Call = 6795 BuildCallExpr(/*Scope=*/nullptr, DeclRef.get(), Loc, CallArgs, Loc); 6796 6797 assert(!Call.isInvalid() && "Call to builtin cannot fail!"); 6798 return Call.get(); 6799 } 6800 6801 ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy, 6802 SourceLocation BuiltinLoc, 6803 SourceLocation RParenLoc) { 6804 QualType DstTy = GetTypeFromParser(ParsedDestTy); 6805 return BuildAsTypeExpr(E, DstTy, BuiltinLoc, RParenLoc); 6806 } 6807 6808 ExprResult Sema::BuildAsTypeExpr(Expr *E, QualType DestTy, 6809 SourceLocation BuiltinLoc, 6810 SourceLocation RParenLoc) { 6811 ExprValueKind VK = VK_PRValue; 6812 ExprObjectKind OK = OK_Ordinary; 6813 QualType SrcTy = E->getType(); 6814 if (!SrcTy->isDependentType() && 6815 Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) 6816 return ExprError( 6817 Diag(BuiltinLoc, diag::err_invalid_astype_of_different_size) 6818 << DestTy << SrcTy << E->getSourceRange()); 6819 return new (Context) AsTypeExpr(E, DestTy, VK, OK, BuiltinLoc, RParenLoc); 6820 } 6821 6822 ExprResult Sema::ActOnConvertVectorExpr(Expr *E, ParsedType ParsedDestTy, 6823 SourceLocation BuiltinLoc, 6824 SourceLocation RParenLoc) { 6825 TypeSourceInfo *TInfo; 6826 GetTypeFromParser(ParsedDestTy, &TInfo); 6827 return ConvertVectorExpr(E, TInfo, BuiltinLoc, RParenLoc); 6828 } 6829 6830 ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, 6831 SourceLocation LParenLoc, 6832 ArrayRef<Expr *> Args, 6833 SourceLocation RParenLoc, Expr *Config, 6834 bool IsExecConfig, ADLCallKind UsesADL) { 6835 FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); 6836 unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); 6837 6838 // Functions with 'interrupt' attribute cannot be called directly. 6839 if (FDecl) { 6840 if (FDecl->hasAttr<AnyX86InterruptAttr>()) { 6841 Diag(Fn->getExprLoc(), diag::err_anyx86_interrupt_called); 6842 return ExprError(); 6843 } 6844 if (FDecl->hasAttr<ARMInterruptAttr>()) { 6845 Diag(Fn->getExprLoc(), diag::err_arm_interrupt_called); 6846 return ExprError(); 6847 } 6848 } 6849 6850 // X86 interrupt handlers may only call routines with attribute 6851 // no_caller_saved_registers since there is no efficient way to 6852 // save and restore the non-GPR state. 6853 if (auto *Caller = getCurFunctionDecl()) { 6854 if (Caller->hasAttr<AnyX86InterruptAttr>() || 6855 Caller->hasAttr<AnyX86NoCallerSavedRegistersAttr>()) { 6856 const TargetInfo &TI = Context.getTargetInfo(); 6857 bool HasNonGPRRegisters = 6858 TI.hasFeature("sse") || TI.hasFeature("x87") || TI.hasFeature("mmx"); 6859 if (HasNonGPRRegisters && 6860 (!FDecl || !FDecl->hasAttr<AnyX86NoCallerSavedRegistersAttr>())) { 6861 Diag(Fn->getExprLoc(), diag::warn_anyx86_excessive_regsave) 6862 << (Caller->hasAttr<AnyX86InterruptAttr>() ? 0 : 1); 6863 if (FDecl) 6864 Diag(FDecl->getLocation(), diag::note_callee_decl) << FDecl; 6865 } 6866 } 6867 } 6868 6869 // Promote the function operand. 6870 // We special-case function promotion here because we only allow promoting 6871 // builtin functions to function pointers in the callee of a call. 6872 ExprResult Result; 6873 QualType ResultTy; 6874 if (BuiltinID && 6875 Fn->getType()->isSpecificBuiltinType(BuiltinType::BuiltinFn)) { 6876 // Extract the return type from the (builtin) function pointer type. 6877 // FIXME Several builtins still have setType in 6878 // Sema::CheckBuiltinFunctionCall. One should review their definitions in 6879 // Builtins.td to ensure they are correct before removing setType calls. 6880 QualType FnPtrTy = Context.getPointerType(FDecl->getType()); 6881 Result = ImpCastExprToType(Fn, FnPtrTy, CK_BuiltinFnToFnPtr).get(); 6882 ResultTy = FDecl->getCallResultType(); 6883 } else { 6884 Result = CallExprUnaryConversions(Fn); 6885 ResultTy = Context.BoolTy; 6886 } 6887 if (Result.isInvalid()) 6888 return ExprError(); 6889 Fn = Result.get(); 6890 6891 // Check for a valid function type, but only if it is not a builtin which 6892 // requires custom type checking. These will be handled by 6893 // CheckBuiltinFunctionCall below just after creation of the call expression. 6894 const FunctionType *FuncT = nullptr; 6895 if (!BuiltinID || !Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) { 6896 retry: 6897 if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { 6898 // C99 6.5.2.2p1 - "The expression that denotes the called function shall 6899 // have type pointer to function". 6900 FuncT = PT->getPointeeType()->getAs<FunctionType>(); 6901 if (!FuncT) 6902 return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) 6903 << Fn->getType() << Fn->getSourceRange()); 6904 } else if (const BlockPointerType *BPT = 6905 Fn->getType()->getAs<BlockPointerType>()) { 6906 FuncT = BPT->getPointeeType()->castAs<FunctionType>(); 6907 } else { 6908 // Handle calls to expressions of unknown-any type. 6909 if (Fn->getType() == Context.UnknownAnyTy) { 6910 ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); 6911 if (rewrite.isInvalid()) 6912 return ExprError(); 6913 Fn = rewrite.get(); 6914 goto retry; 6915 } 6916 6917 return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) 6918 << Fn->getType() << Fn->getSourceRange()); 6919 } 6920 } 6921 6922 // Get the number of parameters in the function prototype, if any. 6923 // We will allocate space for max(Args.size(), NumParams) arguments 6924 // in the call expression. 6925 const auto *Proto = dyn_cast_or_null<FunctionProtoType>(FuncT); 6926 unsigned NumParams = Proto ? Proto->getNumParams() : 0; 6927 6928 CallExpr *TheCall; 6929 if (Config) { 6930 assert(UsesADL == ADLCallKind::NotADL && 6931 "CUDAKernelCallExpr should not use ADL"); 6932 TheCall = CUDAKernelCallExpr::Create(Context, Fn, cast<CallExpr>(Config), 6933 Args, ResultTy, VK_PRValue, RParenLoc, 6934 CurFPFeatureOverrides(), NumParams); 6935 } else { 6936 TheCall = 6937 CallExpr::Create(Context, Fn, Args, ResultTy, VK_PRValue, RParenLoc, 6938 CurFPFeatureOverrides(), NumParams, UsesADL); 6939 } 6940 6941 // Bail out early if calling a builtin with custom type checking. 6942 if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) { 6943 ExprResult E = CheckBuiltinFunctionCall(FDecl, BuiltinID, TheCall); 6944 if (!E.isInvalid() && Context.BuiltinInfo.isImmediate(BuiltinID)) 6945 E = CheckForImmediateInvocation(E, FDecl); 6946 return E; 6947 } 6948 6949 if (getLangOpts().CUDA) { 6950 if (Config) { 6951 // CUDA: Kernel calls must be to global functions 6952 if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) 6953 return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) 6954 << FDecl << Fn->getSourceRange()); 6955 6956 // CUDA: Kernel function must have 'void' return type 6957 if (!FuncT->getReturnType()->isVoidType() && 6958 !FuncT->getReturnType()->getAs<AutoType>() && 6959 !FuncT->getReturnType()->isInstantiationDependentType()) 6960 return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) 6961 << Fn->getType() << Fn->getSourceRange()); 6962 } else { 6963 // CUDA: Calls to global functions must be configured 6964 if (FDecl && FDecl->hasAttr<CUDAGlobalAttr>()) 6965 return ExprError(Diag(LParenLoc, diag::err_global_call_not_config) 6966 << FDecl << Fn->getSourceRange()); 6967 } 6968 } 6969 6970 // Check for a valid return type 6971 if (CheckCallReturnType(FuncT->getReturnType(), Fn->getBeginLoc(), TheCall, 6972 FDecl)) 6973 return ExprError(); 6974 6975 // We know the result type of the call, set it. 6976 TheCall->setType(FuncT->getCallResultType(Context)); 6977 TheCall->setValueKind(Expr::getValueKindForType(FuncT->getReturnType())); 6978 6979 // WebAssembly tables can't be used as arguments. 6980 if (Context.getTargetInfo().getTriple().isWasm()) { 6981 for (const Expr *Arg : Args) { 6982 if (Arg && Arg->getType()->isWebAssemblyTableType()) { 6983 return ExprError(Diag(Arg->getExprLoc(), 6984 diag::err_wasm_table_as_function_parameter)); 6985 } 6986 } 6987 } 6988 6989 if (Proto) { 6990 if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, RParenLoc, 6991 IsExecConfig)) 6992 return ExprError(); 6993 } else { 6994 assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); 6995 6996 if (FDecl) { 6997 // Check if we have too few/too many template arguments, based 6998 // on our knowledge of the function definition. 6999 const FunctionDecl *Def = nullptr; 7000 if (FDecl->hasBody(Def) && Args.size() != Def->param_size()) { 7001 Proto = Def->getType()->getAs<FunctionProtoType>(); 7002 if (!Proto || !(Proto->isVariadic() && Args.size() >= Def->param_size())) 7003 Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) 7004 << (Args.size() > Def->param_size()) << FDecl << Fn->getSourceRange(); 7005 } 7006 7007 // If the function we're calling isn't a function prototype, but we have 7008 // a function prototype from a prior declaratiom, use that prototype. 7009 if (!FDecl->hasPrototype()) 7010 Proto = FDecl->getType()->getAs<FunctionProtoType>(); 7011 } 7012 7013 // If we still haven't found a prototype to use but there are arguments to 7014 // the call, diagnose this as calling a function without a prototype. 7015 // However, if we found a function declaration, check to see if 7016 // -Wdeprecated-non-prototype was disabled where the function was declared. 7017 // If so, we will silence the diagnostic here on the assumption that this 7018 // interface is intentional and the user knows what they're doing. We will 7019 // also silence the diagnostic if there is a function declaration but it 7020 // was implicitly defined (the user already gets diagnostics about the 7021 // creation of the implicit function declaration, so the additional warning 7022 // is not helpful). 7023 if (!Proto && !Args.empty() && 7024 (!FDecl || (!FDecl->isImplicit() && 7025 !Diags.isIgnored(diag::warn_strict_uses_without_prototype, 7026 FDecl->getLocation())))) 7027 Diag(LParenLoc, diag::warn_strict_uses_without_prototype) 7028 << (FDecl != nullptr) << FDecl; 7029 7030 // Promote the arguments (C99 6.5.2.2p6). 7031 for (unsigned i = 0, e = Args.size(); i != e; i++) { 7032 Expr *Arg = Args[i]; 7033 7034 if (Proto && i < Proto->getNumParams()) { 7035 InitializedEntity Entity = InitializedEntity::InitializeParameter( 7036 Context, Proto->getParamType(i), Proto->isParamConsumed(i)); 7037 ExprResult ArgE = 7038 PerformCopyInitialization(Entity, SourceLocation(), Arg); 7039 if (ArgE.isInvalid()) 7040 return true; 7041 7042 Arg = ArgE.getAs<Expr>(); 7043 7044 } else { 7045 ExprResult ArgE = DefaultArgumentPromotion(Arg); 7046 7047 if (ArgE.isInvalid()) 7048 return true; 7049 7050 Arg = ArgE.getAs<Expr>(); 7051 } 7052 7053 if (RequireCompleteType(Arg->getBeginLoc(), Arg->getType(), 7054 diag::err_call_incomplete_argument, Arg)) 7055 return ExprError(); 7056 7057 TheCall->setArg(i, Arg); 7058 } 7059 TheCall->computeDependence(); 7060 } 7061 7062 if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) 7063 if (Method->isImplicitObjectMemberFunction()) 7064 return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) 7065 << Fn->getSourceRange() << 0); 7066 7067 // Check for sentinels 7068 if (NDecl) 7069 DiagnoseSentinelCalls(NDecl, LParenLoc, Args); 7070 7071 // Warn for unions passing across security boundary (CMSE). 7072 if (FuncT != nullptr && FuncT->getCmseNSCallAttr()) { 7073 for (unsigned i = 0, e = Args.size(); i != e; i++) { 7074 if (const auto *RT = 7075 dyn_cast<RecordType>(Args[i]->getType().getCanonicalType())) { 7076 if (RT->getDecl()->isOrContainsUnion()) 7077 Diag(Args[i]->getBeginLoc(), diag::warn_cmse_nonsecure_union) 7078 << 0 << i; 7079 } 7080 } 7081 } 7082 7083 // Do special checking on direct calls to functions. 7084 if (FDecl) { 7085 if (CheckFunctionCall(FDecl, TheCall, Proto)) 7086 return ExprError(); 7087 7088 checkFortifiedBuiltinMemoryFunction(FDecl, TheCall); 7089 7090 if (BuiltinID) 7091 return CheckBuiltinFunctionCall(FDecl, BuiltinID, TheCall); 7092 } else if (NDecl) { 7093 if (CheckPointerCall(NDecl, TheCall, Proto)) 7094 return ExprError(); 7095 } else { 7096 if (CheckOtherCall(TheCall, Proto)) 7097 return ExprError(); 7098 } 7099 7100 return CheckForImmediateInvocation(MaybeBindToTemporary(TheCall), FDecl); 7101 } 7102 7103 ExprResult 7104 Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, 7105 SourceLocation RParenLoc, Expr *InitExpr) { 7106 assert(Ty && "ActOnCompoundLiteral(): missing type"); 7107 assert(InitExpr && "ActOnCompoundLiteral(): missing expression"); 7108 7109 TypeSourceInfo *TInfo; 7110 QualType literalType = GetTypeFromParser(Ty, &TInfo); 7111 if (!TInfo) 7112 TInfo = Context.getTrivialTypeSourceInfo(literalType); 7113 7114 return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); 7115 } 7116 7117 ExprResult 7118 Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, 7119 SourceLocation RParenLoc, Expr *LiteralExpr) { 7120 QualType literalType = TInfo->getType(); 7121 7122 if (literalType->isArrayType()) { 7123 if (RequireCompleteSizedType( 7124 LParenLoc, Context.getBaseElementType(literalType), 7125 diag::err_array_incomplete_or_sizeless_type, 7126 SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd()))) 7127 return ExprError(); 7128 if (literalType->isVariableArrayType()) { 7129 // C23 6.7.10p4: An entity of variable length array type shall not be 7130 // initialized except by an empty initializer. 7131 // 7132 // The C extension warnings are issued from ParseBraceInitializer() and 7133 // do not need to be issued here. However, we continue to issue an error 7134 // in the case there are initializers or we are compiling C++. We allow 7135 // use of VLAs in C++, but it's not clear we want to allow {} to zero 7136 // init a VLA in C++ in all cases (such as with non-trivial constructors). 7137 // FIXME: should we allow this construct in C++ when it makes sense to do 7138 // so? 7139 // 7140 // But: C99-C23 6.5.2.5 Compound literals constraint 1: The type name 7141 // shall specify an object type or an array of unknown size, but not a 7142 // variable length array type. This seems odd, as it allows 'int a[size] = 7143 // {}', but forbids 'int *a = (int[size]){}'. As this is what the standard 7144 // says, this is what's implemented here for C (except for the extension 7145 // that permits constant foldable size arrays) 7146 7147 auto diagID = LangOpts.CPlusPlus 7148 ? diag::err_variable_object_no_init 7149 : diag::err_compound_literal_with_vla_type; 7150 if (!tryToFixVariablyModifiedVarType(TInfo, literalType, LParenLoc, 7151 diagID)) 7152 return ExprError(); 7153 } 7154 } else if (!literalType->isDependentType() && 7155 RequireCompleteType(LParenLoc, literalType, 7156 diag::err_typecheck_decl_incomplete_type, 7157 SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd()))) 7158 return ExprError(); 7159 7160 InitializedEntity Entity 7161 = InitializedEntity::InitializeCompoundLiteralInit(TInfo); 7162 InitializationKind Kind 7163 = InitializationKind::CreateCStyleCast(LParenLoc, 7164 SourceRange(LParenLoc, RParenLoc), 7165 /*InitList=*/true); 7166 InitializationSequence InitSeq(*this, Entity, Kind, LiteralExpr); 7167 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, LiteralExpr, 7168 &literalType); 7169 if (Result.isInvalid()) 7170 return ExprError(); 7171 LiteralExpr = Result.get(); 7172 7173 // We treat the compound literal as being at file scope if it's not in a 7174 // function or method body, or within the function's prototype scope. This 7175 // means the following compound literal is not at file scope: 7176 // void func(char *para[(int [1]){ 0 }[0]); 7177 const Scope *S = getCurScope(); 7178 bool IsFileScope = !CurContext->isFunctionOrMethod() && 7179 !S->isInCFunctionScope() && 7180 (!S || !S->isFunctionPrototypeScope()); 7181 7182 // In C, compound literals are l-values for some reason. 7183 // For GCC compatibility, in C++, file-scope array compound literals with 7184 // constant initializers are also l-values, and compound literals are 7185 // otherwise prvalues. 7186 // 7187 // (GCC also treats C++ list-initialized file-scope array prvalues with 7188 // constant initializers as l-values, but that's non-conforming, so we don't 7189 // follow it there.) 7190 // 7191 // FIXME: It would be better to handle the lvalue cases as materializing and 7192 // lifetime-extending a temporary object, but our materialized temporaries 7193 // representation only supports lifetime extension from a variable, not "out 7194 // of thin air". 7195 // FIXME: For C++, we might want to instead lifetime-extend only if a pointer 7196 // is bound to the result of applying array-to-pointer decay to the compound 7197 // literal. 7198 // FIXME: GCC supports compound literals of reference type, which should 7199 // obviously have a value kind derived from the kind of reference involved. 7200 ExprValueKind VK = 7201 (getLangOpts().CPlusPlus && !(IsFileScope && literalType->isArrayType())) 7202 ? VK_PRValue 7203 : VK_LValue; 7204 7205 // C99 6.5.2.5 7206 // "If the compound literal occurs outside the body of a function, the 7207 // initializer list shall consist of constant expressions." 7208 if (IsFileScope) 7209 if (auto ILE = dyn_cast<InitListExpr>(LiteralExpr)) 7210 for (unsigned i = 0, j = ILE->getNumInits(); i != j; i++) { 7211 Expr *Init = ILE->getInit(i); 7212 if (!Init->isTypeDependent() && !Init->isValueDependent() && 7213 !Init->isConstantInitializer(Context, /*IsForRef=*/false)) { 7214 Diag(Init->getExprLoc(), diag::err_init_element_not_constant) 7215 << Init->getSourceBitField(); 7216 return ExprError(); 7217 } 7218 7219 ILE->setInit(i, ConstantExpr::Create(Context, Init)); 7220 } 7221 7222 auto *E = new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, VK, 7223 LiteralExpr, IsFileScope); 7224 if (IsFileScope) { 7225 if (!LiteralExpr->isTypeDependent() && 7226 !LiteralExpr->isValueDependent() && 7227 !literalType->isDependentType()) // C99 6.5.2.5p3 7228 if (CheckForConstantInitializer(LiteralExpr)) 7229 return ExprError(); 7230 } else if (literalType.getAddressSpace() != LangAS::opencl_private && 7231 literalType.getAddressSpace() != LangAS::Default) { 7232 // Embedded-C extensions to C99 6.5.2.5: 7233 // "If the compound literal occurs inside the body of a function, the 7234 // type name shall not be qualified by an address-space qualifier." 7235 Diag(LParenLoc, diag::err_compound_literal_with_address_space) 7236 << SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd()); 7237 return ExprError(); 7238 } 7239 7240 if (!IsFileScope && !getLangOpts().CPlusPlus) { 7241 // Compound literals that have automatic storage duration are destroyed at 7242 // the end of the scope in C; in C++, they're just temporaries. 7243 7244 // Emit diagnostics if it is or contains a C union type that is non-trivial 7245 // to destruct. 7246 if (E->getType().hasNonTrivialToPrimitiveDestructCUnion()) 7247 checkNonTrivialCUnion(E->getType(), E->getExprLoc(), 7248 NonTrivialCUnionContext::CompoundLiteral, 7249 NTCUK_Destruct); 7250 7251 // Diagnose jumps that enter or exit the lifetime of the compound literal. 7252 if (literalType.isDestructedType()) { 7253 Cleanup.setExprNeedsCleanups(true); 7254 ExprCleanupObjects.push_back(E); 7255 getCurFunction()->setHasBranchProtectedScope(); 7256 } 7257 } 7258 7259 if (E->getType().hasNonTrivialToPrimitiveDefaultInitializeCUnion() || 7260 E->getType().hasNonTrivialToPrimitiveCopyCUnion()) 7261 checkNonTrivialCUnionInInitializer(E->getInitializer(), 7262 E->getInitializer()->getExprLoc()); 7263 7264 return MaybeBindToTemporary(E); 7265 } 7266 7267 ExprResult 7268 Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, 7269 SourceLocation RBraceLoc) { 7270 // Only produce each kind of designated initialization diagnostic once. 7271 SourceLocation FirstDesignator; 7272 bool DiagnosedArrayDesignator = false; 7273 bool DiagnosedNestedDesignator = false; 7274 bool DiagnosedMixedDesignator = false; 7275 7276 // Check that any designated initializers are syntactically valid in the 7277 // current language mode. 7278 for (unsigned I = 0, E = InitArgList.size(); I != E; ++I) { 7279 if (auto *DIE = dyn_cast<DesignatedInitExpr>(InitArgList[I])) { 7280 if (FirstDesignator.isInvalid()) 7281 FirstDesignator = DIE->getBeginLoc(); 7282 7283 if (!getLangOpts().CPlusPlus) 7284 break; 7285 7286 if (!DiagnosedNestedDesignator && DIE->size() > 1) { 7287 DiagnosedNestedDesignator = true; 7288 Diag(DIE->getBeginLoc(), diag::ext_designated_init_nested) 7289 << DIE->getDesignatorsSourceRange(); 7290 } 7291 7292 for (auto &Desig : DIE->designators()) { 7293 if (!Desig.isFieldDesignator() && !DiagnosedArrayDesignator) { 7294 DiagnosedArrayDesignator = true; 7295 Diag(Desig.getBeginLoc(), diag::ext_designated_init_array) 7296 << Desig.getSourceRange(); 7297 } 7298 } 7299 7300 if (!DiagnosedMixedDesignator && 7301 !isa<DesignatedInitExpr>(InitArgList[0])) { 7302 DiagnosedMixedDesignator = true; 7303 Diag(DIE->getBeginLoc(), diag::ext_designated_init_mixed) 7304 << DIE->getSourceRange(); 7305 Diag(InitArgList[0]->getBeginLoc(), diag::note_designated_init_mixed) 7306 << InitArgList[0]->getSourceRange(); 7307 } 7308 } else if (getLangOpts().CPlusPlus && !DiagnosedMixedDesignator && 7309 isa<DesignatedInitExpr>(InitArgList[0])) { 7310 DiagnosedMixedDesignator = true; 7311 auto *DIE = cast<DesignatedInitExpr>(InitArgList[0]); 7312 Diag(DIE->getBeginLoc(), diag::ext_designated_init_mixed) 7313 << DIE->getSourceRange(); 7314 Diag(InitArgList[I]->getBeginLoc(), diag::note_designated_init_mixed) 7315 << InitArgList[I]->getSourceRange(); 7316 } 7317 } 7318 7319 if (FirstDesignator.isValid()) { 7320 // Only diagnose designated initiaization as a C++20 extension if we didn't 7321 // already diagnose use of (non-C++20) C99 designator syntax. 7322 if (getLangOpts().CPlusPlus && !DiagnosedArrayDesignator && 7323 !DiagnosedNestedDesignator && !DiagnosedMixedDesignator) { 7324 Diag(FirstDesignator, getLangOpts().CPlusPlus20 7325 ? diag::warn_cxx17_compat_designated_init 7326 : diag::ext_cxx_designated_init); 7327 } else if (!getLangOpts().CPlusPlus && !getLangOpts().C99) { 7328 Diag(FirstDesignator, diag::ext_designated_init); 7329 } 7330 } 7331 7332 return BuildInitList(LBraceLoc, InitArgList, RBraceLoc); 7333 } 7334 7335 ExprResult 7336 Sema::BuildInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, 7337 SourceLocation RBraceLoc) { 7338 // Semantic analysis for initializers is done by ActOnDeclarator() and 7339 // CheckInitializer() - it requires knowledge of the object being initialized. 7340 7341 // Immediately handle non-overload placeholders. Overloads can be 7342 // resolved contextually, but everything else here can't. 7343 for (unsigned I = 0, E = InitArgList.size(); I != E; ++I) { 7344 if (InitArgList[I]->getType()->isNonOverloadPlaceholderType()) { 7345 ExprResult result = CheckPlaceholderExpr(InitArgList[I]); 7346 7347 // Ignore failures; dropping the entire initializer list because 7348 // of one failure would be terrible for indexing/etc. 7349 if (result.isInvalid()) continue; 7350 7351 InitArgList[I] = result.get(); 7352 } 7353 } 7354 7355 InitListExpr *E = 7356 new (Context) InitListExpr(Context, LBraceLoc, InitArgList, RBraceLoc); 7357 E->setType(Context.VoidTy); // FIXME: just a place holder for now. 7358 return E; 7359 } 7360 7361 void Sema::maybeExtendBlockObject(ExprResult &E) { 7362 assert(E.get()->getType()->isBlockPointerType()); 7363 assert(E.get()->isPRValue()); 7364 7365 // Only do this in an r-value context. 7366 if (!getLangOpts().ObjCAutoRefCount) return; 7367 7368 E = ImplicitCastExpr::Create( 7369 Context, E.get()->getType(), CK_ARCExtendBlockObject, E.get(), 7370 /*base path*/ nullptr, VK_PRValue, FPOptionsOverride()); 7371 Cleanup.setExprNeedsCleanups(true); 7372 } 7373 7374 CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { 7375 // Both Src and Dest are scalar types, i.e. arithmetic or pointer. 7376 // Also, callers should have filtered out the invalid cases with 7377 // pointers. Everything else should be possible. 7378 7379 QualType SrcTy = Src.get()->getType(); 7380 if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) 7381 return CK_NoOp; 7382 7383 switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) { 7384 case Type::STK_MemberPointer: 7385 llvm_unreachable("member pointer type in C"); 7386 7387 case Type::STK_CPointer: 7388 case Type::STK_BlockPointer: 7389 case Type::STK_ObjCObjectPointer: 7390 switch (DestTy->getScalarTypeKind()) { 7391 case Type::STK_CPointer: { 7392 LangAS SrcAS = SrcTy->getPointeeType().getAddressSpace(); 7393 LangAS DestAS = DestTy->getPointeeType().getAddressSpace(); 7394 if (SrcAS != DestAS) 7395 return CK_AddressSpaceConversion; 7396 if (Context.hasCvrSimilarType(SrcTy, DestTy)) 7397 return CK_NoOp; 7398 return CK_BitCast; 7399 } 7400 case Type::STK_BlockPointer: 7401 return (SrcKind == Type::STK_BlockPointer 7402 ? CK_BitCast : CK_AnyPointerToBlockPointerCast); 7403 case Type::STK_ObjCObjectPointer: 7404 if (SrcKind == Type::STK_ObjCObjectPointer) 7405 return CK_BitCast; 7406 if (SrcKind == Type::STK_CPointer) 7407 return CK_CPointerToObjCPointerCast; 7408 maybeExtendBlockObject(Src); 7409 return CK_BlockPointerToObjCPointerCast; 7410 case Type::STK_Bool: 7411 return CK_PointerToBoolean; 7412 case Type::STK_Integral: 7413 return CK_PointerToIntegral; 7414 case Type::STK_Floating: 7415 case Type::STK_FloatingComplex: 7416 case Type::STK_IntegralComplex: 7417 case Type::STK_MemberPointer: 7418 case Type::STK_FixedPoint: 7419 llvm_unreachable("illegal cast from pointer"); 7420 } 7421 llvm_unreachable("Should have returned before this"); 7422 7423 case Type::STK_FixedPoint: 7424 switch (DestTy->getScalarTypeKind()) { 7425 case Type::STK_FixedPoint: 7426 return CK_FixedPointCast; 7427 case Type::STK_Bool: 7428 return CK_FixedPointToBoolean; 7429 case Type::STK_Integral: 7430 return CK_FixedPointToIntegral; 7431 case Type::STK_Floating: 7432 return CK_FixedPointToFloating; 7433 case Type::STK_IntegralComplex: 7434 case Type::STK_FloatingComplex: 7435 Diag(Src.get()->getExprLoc(), 7436 diag::err_unimplemented_conversion_with_fixed_point_type) 7437 << DestTy; 7438 return CK_IntegralCast; 7439 case Type::STK_CPointer: 7440 case Type::STK_ObjCObjectPointer: 7441 case Type::STK_BlockPointer: 7442 case Type::STK_MemberPointer: 7443 llvm_unreachable("illegal cast to pointer type"); 7444 } 7445 llvm_unreachable("Should have returned before this"); 7446 7447 case Type::STK_Bool: // casting from bool is like casting from an integer 7448 case Type::STK_Integral: 7449 switch (DestTy->getScalarTypeKind()) { 7450 case Type::STK_CPointer: 7451 case Type::STK_ObjCObjectPointer: 7452 case Type::STK_BlockPointer: 7453 if (Src.get()->isNullPointerConstant(Context, 7454 Expr::NPC_ValueDependentIsNull)) 7455 return CK_NullToPointer; 7456 return CK_IntegralToPointer; 7457 case Type::STK_Bool: 7458 return CK_IntegralToBoolean; 7459 case Type::STK_Integral: 7460 return CK_IntegralCast; 7461 case Type::STK_Floating: 7462 return CK_IntegralToFloating; 7463 case Type::STK_IntegralComplex: 7464 Src = ImpCastExprToType(Src.get(), 7465 DestTy->castAs<ComplexType>()->getElementType(), 7466 CK_IntegralCast); 7467 return CK_IntegralRealToComplex; 7468 case Type::STK_FloatingComplex: 7469 Src = ImpCastExprToType(Src.get(), 7470 DestTy->castAs<ComplexType>()->getElementType(), 7471 CK_IntegralToFloating); 7472 return CK_FloatingRealToComplex; 7473 case Type::STK_MemberPointer: 7474 llvm_unreachable("member pointer type in C"); 7475 case Type::STK_FixedPoint: 7476 return CK_IntegralToFixedPoint; 7477 } 7478 llvm_unreachable("Should have returned before this"); 7479 7480 case Type::STK_Floating: 7481 switch (DestTy->getScalarTypeKind()) { 7482 case Type::STK_Floating: 7483 return CK_FloatingCast; 7484 case Type::STK_Bool: 7485 return CK_FloatingToBoolean; 7486 case Type::STK_Integral: 7487 return CK_FloatingToIntegral; 7488 case Type::STK_FloatingComplex: 7489 Src = ImpCastExprToType(Src.get(), 7490 DestTy->castAs<ComplexType>()->getElementType(), 7491 CK_FloatingCast); 7492 return CK_FloatingRealToComplex; 7493 case Type::STK_IntegralComplex: 7494 Src = ImpCastExprToType(Src.get(), 7495 DestTy->castAs<ComplexType>()->getElementType(), 7496 CK_FloatingToIntegral); 7497 return CK_IntegralRealToComplex; 7498 case Type::STK_CPointer: 7499 case Type::STK_ObjCObjectPointer: 7500 case Type::STK_BlockPointer: 7501 llvm_unreachable("valid float->pointer cast?"); 7502 case Type::STK_MemberPointer: 7503 llvm_unreachable("member pointer type in C"); 7504 case Type::STK_FixedPoint: 7505 return CK_FloatingToFixedPoint; 7506 } 7507 llvm_unreachable("Should have returned before this"); 7508 7509 case Type::STK_FloatingComplex: 7510 switch (DestTy->getScalarTypeKind()) { 7511 case Type::STK_FloatingComplex: 7512 return CK_FloatingComplexCast; 7513 case Type::STK_IntegralComplex: 7514 return CK_FloatingComplexToIntegralComplex; 7515 case Type::STK_Floating: { 7516 QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); 7517 if (Context.hasSameType(ET, DestTy)) 7518 return CK_FloatingComplexToReal; 7519 Src = ImpCastExprToType(Src.get(), ET, CK_FloatingComplexToReal); 7520 return CK_FloatingCast; 7521 } 7522 case Type::STK_Bool: 7523 return CK_FloatingComplexToBoolean; 7524 case Type::STK_Integral: 7525 Src = ImpCastExprToType(Src.get(), 7526 SrcTy->castAs<ComplexType>()->getElementType(), 7527 CK_FloatingComplexToReal); 7528 return CK_FloatingToIntegral; 7529 case Type::STK_CPointer: 7530 case Type::STK_ObjCObjectPointer: 7531 case Type::STK_BlockPointer: 7532 llvm_unreachable("valid complex float->pointer cast?"); 7533 case Type::STK_MemberPointer: 7534 llvm_unreachable("member pointer type in C"); 7535 case Type::STK_FixedPoint: 7536 Diag(Src.get()->getExprLoc(), 7537 diag::err_unimplemented_conversion_with_fixed_point_type) 7538 << SrcTy; 7539 return CK_IntegralCast; 7540 } 7541 llvm_unreachable("Should have returned before this"); 7542 7543 case Type::STK_IntegralComplex: 7544 switch (DestTy->getScalarTypeKind()) { 7545 case Type::STK_FloatingComplex: 7546 return CK_IntegralComplexToFloatingComplex; 7547 case Type::STK_IntegralComplex: 7548 return CK_IntegralComplexCast; 7549 case Type::STK_Integral: { 7550 QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); 7551 if (Context.hasSameType(ET, DestTy)) 7552 return CK_IntegralComplexToReal; 7553 Src = ImpCastExprToType(Src.get(), ET, CK_IntegralComplexToReal); 7554 return CK_IntegralCast; 7555 } 7556 case Type::STK_Bool: 7557 return CK_IntegralComplexToBoolean; 7558 case Type::STK_Floating: 7559 Src = ImpCastExprToType(Src.get(), 7560 SrcTy->castAs<ComplexType>()->getElementType(), 7561 CK_IntegralComplexToReal); 7562 return CK_IntegralToFloating; 7563 case Type::STK_CPointer: 7564 case Type::STK_ObjCObjectPointer: 7565 case Type::STK_BlockPointer: 7566 llvm_unreachable("valid complex int->pointer cast?"); 7567 case Type::STK_MemberPointer: 7568 llvm_unreachable("member pointer type in C"); 7569 case Type::STK_FixedPoint: 7570 Diag(Src.get()->getExprLoc(), 7571 diag::err_unimplemented_conversion_with_fixed_point_type) 7572 << SrcTy; 7573 return CK_IntegralCast; 7574 } 7575 llvm_unreachable("Should have returned before this"); 7576 } 7577 7578 llvm_unreachable("Unhandled scalar cast"); 7579 } 7580 7581 static bool breakDownVectorType(QualType type, uint64_t &len, 7582 QualType &eltType) { 7583 // Vectors are simple. 7584 if (const VectorType *vecType = type->getAs<VectorType>()) { 7585 len = vecType->getNumElements(); 7586 eltType = vecType->getElementType(); 7587 assert(eltType->isScalarType() || eltType->isMFloat8Type()); 7588 return true; 7589 } 7590 7591 // We allow lax conversion to and from non-vector types, but only if 7592 // they're real types (i.e. non-complex, non-pointer scalar types). 7593 if (!type->isRealType()) return false; 7594 7595 len = 1; 7596 eltType = type; 7597 return true; 7598 } 7599 7600 bool Sema::isValidSveBitcast(QualType srcTy, QualType destTy) { 7601 assert(srcTy->isVectorType() || destTy->isVectorType()); 7602 7603 auto ValidScalableConversion = [](QualType FirstType, QualType SecondType) { 7604 if (!FirstType->isSVESizelessBuiltinType()) 7605 return false; 7606 7607 const auto *VecTy = SecondType->getAs<VectorType>(); 7608 return VecTy && VecTy->getVectorKind() == VectorKind::SveFixedLengthData; 7609 }; 7610 7611 return ValidScalableConversion(srcTy, destTy) || 7612 ValidScalableConversion(destTy, srcTy); 7613 } 7614 7615 bool Sema::areMatrixTypesOfTheSameDimension(QualType srcTy, QualType destTy) { 7616 if (!destTy->isMatrixType() || !srcTy->isMatrixType()) 7617 return false; 7618 7619 const ConstantMatrixType *matSrcType = srcTy->getAs<ConstantMatrixType>(); 7620 const ConstantMatrixType *matDestType = destTy->getAs<ConstantMatrixType>(); 7621 7622 return matSrcType->getNumRows() == matDestType->getNumRows() && 7623 matSrcType->getNumColumns() == matDestType->getNumColumns(); 7624 } 7625 7626 bool Sema::areVectorTypesSameSize(QualType SrcTy, QualType DestTy) { 7627 assert(DestTy->isVectorType() || SrcTy->isVectorType()); 7628 7629 uint64_t SrcLen, DestLen; 7630 QualType SrcEltTy, DestEltTy; 7631 if (!breakDownVectorType(SrcTy, SrcLen, SrcEltTy)) 7632 return false; 7633 if (!breakDownVectorType(DestTy, DestLen, DestEltTy)) 7634 return false; 7635 7636 // ASTContext::getTypeSize will return the size rounded up to a 7637 // power of 2, so instead of using that, we need to use the raw 7638 // element size multiplied by the element count. 7639 uint64_t SrcEltSize = Context.getTypeSize(SrcEltTy); 7640 uint64_t DestEltSize = Context.getTypeSize(DestEltTy); 7641 7642 return (SrcLen * SrcEltSize == DestLen * DestEltSize); 7643 } 7644 7645 bool Sema::anyAltivecTypes(QualType SrcTy, QualType DestTy) { 7646 assert((DestTy->isVectorType() || SrcTy->isVectorType()) && 7647 "expected at least one type to be a vector here"); 7648 7649 bool IsSrcTyAltivec = 7650 SrcTy->isVectorType() && ((SrcTy->castAs<VectorType>()->getVectorKind() == 7651 VectorKind::AltiVecVector) || 7652 (SrcTy->castAs<VectorType>()->getVectorKind() == 7653 VectorKind::AltiVecBool) || 7654 (SrcTy->castAs<VectorType>()->getVectorKind() == 7655 VectorKind::AltiVecPixel)); 7656 7657 bool IsDestTyAltivec = DestTy->isVectorType() && 7658 ((DestTy->castAs<VectorType>()->getVectorKind() == 7659 VectorKind::AltiVecVector) || 7660 (DestTy->castAs<VectorType>()->getVectorKind() == 7661 VectorKind::AltiVecBool) || 7662 (DestTy->castAs<VectorType>()->getVectorKind() == 7663 VectorKind::AltiVecPixel)); 7664 7665 return (IsSrcTyAltivec || IsDestTyAltivec); 7666 } 7667 7668 bool Sema::areLaxCompatibleVectorTypes(QualType srcTy, QualType destTy) { 7669 assert(destTy->isVectorType() || srcTy->isVectorType()); 7670 7671 // Disallow lax conversions between scalars and ExtVectors (these 7672 // conversions are allowed for other vector types because common headers 7673 // depend on them). Most scalar OP ExtVector cases are handled by the 7674 // splat path anyway, which does what we want (convert, not bitcast). 7675 // What this rules out for ExtVectors is crazy things like char4*float. 7676 if (srcTy->isScalarType() && destTy->isExtVectorType()) return false; 7677 if (destTy->isScalarType() && srcTy->isExtVectorType()) return false; 7678 7679 return areVectorTypesSameSize(srcTy, destTy); 7680 } 7681 7682 bool Sema::isLaxVectorConversion(QualType srcTy, QualType destTy) { 7683 assert(destTy->isVectorType() || srcTy->isVectorType()); 7684 7685 switch (Context.getLangOpts().getLaxVectorConversions()) { 7686 case LangOptions::LaxVectorConversionKind::None: 7687 return false; 7688 7689 case LangOptions::LaxVectorConversionKind::Integer: 7690 if (!srcTy->isIntegralOrEnumerationType()) { 7691 auto *Vec = srcTy->getAs<VectorType>(); 7692 if (!Vec || !Vec->getElementType()->isIntegralOrEnumerationType()) 7693 return false; 7694 } 7695 if (!destTy->isIntegralOrEnumerationType()) { 7696 auto *Vec = destTy->getAs<VectorType>(); 7697 if (!Vec || !Vec->getElementType()->isIntegralOrEnumerationType()) 7698 return false; 7699 } 7700 // OK, integer (vector) -> integer (vector) bitcast. 7701 break; 7702 7703 case LangOptions::LaxVectorConversionKind::All: 7704 break; 7705 } 7706 7707 return areLaxCompatibleVectorTypes(srcTy, destTy); 7708 } 7709 7710 bool Sema::CheckMatrixCast(SourceRange R, QualType DestTy, QualType SrcTy, 7711 CastKind &Kind) { 7712 if (SrcTy->isMatrixType() && DestTy->isMatrixType()) { 7713 if (!areMatrixTypesOfTheSameDimension(SrcTy, DestTy)) { 7714 return Diag(R.getBegin(), diag::err_invalid_conversion_between_matrixes) 7715 << DestTy << SrcTy << R; 7716 } 7717 } else if (SrcTy->isMatrixType()) { 7718 return Diag(R.getBegin(), 7719 diag::err_invalid_conversion_between_matrix_and_type) 7720 << SrcTy << DestTy << R; 7721 } else if (DestTy->isMatrixType()) { 7722 return Diag(R.getBegin(), 7723 diag::err_invalid_conversion_between_matrix_and_type) 7724 << DestTy << SrcTy << R; 7725 } 7726 7727 Kind = CK_MatrixCast; 7728 return false; 7729 } 7730 7731 bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, 7732 CastKind &Kind) { 7733 assert(VectorTy->isVectorType() && "Not a vector type!"); 7734 7735 if (Ty->isVectorType() || Ty->isIntegralType(Context)) { 7736 if (!areLaxCompatibleVectorTypes(Ty, VectorTy)) 7737 return Diag(R.getBegin(), 7738 Ty->isVectorType() ? 7739 diag::err_invalid_conversion_between_vectors : 7740 diag::err_invalid_conversion_between_vector_and_integer) 7741 << VectorTy << Ty << R; 7742 } else 7743 return Diag(R.getBegin(), 7744 diag::err_invalid_conversion_between_vector_and_scalar) 7745 << VectorTy << Ty << R; 7746 7747 Kind = CK_BitCast; 7748 return false; 7749 } 7750 7751 ExprResult Sema::prepareVectorSplat(QualType VectorTy, Expr *SplattedExpr) { 7752 QualType DestElemTy = VectorTy->castAs<VectorType>()->getElementType(); 7753 7754 if (DestElemTy == SplattedExpr->getType()) 7755 return SplattedExpr; 7756 7757 assert(DestElemTy->isFloatingType() || 7758 DestElemTy->isIntegralOrEnumerationType()); 7759 7760 CastKind CK; 7761 if (VectorTy->isExtVectorType() && SplattedExpr->getType()->isBooleanType()) { 7762 // OpenCL requires that we convert `true` boolean expressions to -1, but 7763 // only when splatting vectors. 7764 if (DestElemTy->isFloatingType()) { 7765 // To avoid having to have a CK_BooleanToSignedFloating cast kind, we cast 7766 // in two steps: boolean to signed integral, then to floating. 7767 ExprResult CastExprRes = ImpCastExprToType(SplattedExpr, Context.IntTy, 7768 CK_BooleanToSignedIntegral); 7769 SplattedExpr = CastExprRes.get(); 7770 CK = CK_IntegralToFloating; 7771 } else { 7772 CK = CK_BooleanToSignedIntegral; 7773 } 7774 } else { 7775 ExprResult CastExprRes = SplattedExpr; 7776 CK = PrepareScalarCast(CastExprRes, DestElemTy); 7777 if (CastExprRes.isInvalid()) 7778 return ExprError(); 7779 SplattedExpr = CastExprRes.get(); 7780 } 7781 return ImpCastExprToType(SplattedExpr, DestElemTy, CK); 7782 } 7783 7784 ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, 7785 Expr *CastExpr, CastKind &Kind) { 7786 assert(DestTy->isExtVectorType() && "Not an extended vector type!"); 7787 7788 QualType SrcTy = CastExpr->getType(); 7789 7790 // If SrcTy is a VectorType, the total size must match to explicitly cast to 7791 // an ExtVectorType. 7792 // In OpenCL, casts between vectors of different types are not allowed. 7793 // (See OpenCL 6.2). 7794 if (SrcTy->isVectorType()) { 7795 if (!areLaxCompatibleVectorTypes(SrcTy, DestTy) || 7796 (getLangOpts().OpenCL && 7797 !Context.hasSameUnqualifiedType(DestTy, SrcTy))) { 7798 Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) 7799 << DestTy << SrcTy << R; 7800 return ExprError(); 7801 } 7802 Kind = CK_BitCast; 7803 return CastExpr; 7804 } 7805 7806 // All non-pointer scalars can be cast to ExtVector type. The appropriate 7807 // conversion will take place first from scalar to elt type, and then 7808 // splat from elt type to vector. 7809 if (SrcTy->isPointerType()) 7810 return Diag(R.getBegin(), 7811 diag::err_invalid_conversion_between_vector_and_scalar) 7812 << DestTy << SrcTy << R; 7813 7814 Kind = CK_VectorSplat; 7815 return prepareVectorSplat(DestTy, CastExpr); 7816 } 7817 7818 ExprResult 7819 Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, 7820 Declarator &D, ParsedType &Ty, 7821 SourceLocation RParenLoc, Expr *CastExpr) { 7822 assert(!D.isInvalidType() && (CastExpr != nullptr) && 7823 "ActOnCastExpr(): missing type or expr"); 7824 7825 TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, CastExpr->getType()); 7826 if (D.isInvalidType()) 7827 return ExprError(); 7828 7829 if (getLangOpts().CPlusPlus) { 7830 // Check that there are no default arguments (C++ only). 7831 CheckExtraCXXDefaultArguments(D); 7832 } 7833 7834 checkUnusedDeclAttributes(D); 7835 7836 QualType castType = castTInfo->getType(); 7837 Ty = CreateParsedType(castType, castTInfo); 7838 7839 bool isVectorLiteral = false; 7840 7841 // Check for an altivec or OpenCL literal, 7842 // i.e. all the elements are integer constants. 7843 ParenExpr *PE = dyn_cast<ParenExpr>(CastExpr); 7844 ParenListExpr *PLE = dyn_cast<ParenListExpr>(CastExpr); 7845 if ((getLangOpts().AltiVec || getLangOpts().ZVector || getLangOpts().OpenCL) 7846 && castType->isVectorType() && (PE || PLE)) { 7847 if (PLE && PLE->getNumExprs() == 0) { 7848 Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); 7849 return ExprError(); 7850 } 7851 if (PE || PLE->getNumExprs() == 1) { 7852 Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); 7853 if (!E->isTypeDependent() && !E->getType()->isVectorType()) 7854 isVectorLiteral = true; 7855 } 7856 else 7857 isVectorLiteral = true; 7858 } 7859 7860 // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' 7861 // then handle it as such. 7862 if (isVectorLiteral) 7863 return BuildVectorLiteral(LParenLoc, RParenLoc, CastExpr, castTInfo); 7864 7865 // If the Expr being casted is a ParenListExpr, handle it specially. 7866 // This is not an AltiVec-style cast, so turn the ParenListExpr into a 7867 // sequence of BinOp comma operators. 7868 if (isa<ParenListExpr>(CastExpr)) { 7869 ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); 7870 if (Result.isInvalid()) return ExprError(); 7871 CastExpr = Result.get(); 7872 } 7873 7874 if (getLangOpts().CPlusPlus && !castType->isVoidType()) 7875 Diag(LParenLoc, diag::warn_old_style_cast) << CastExpr->getSourceRange(); 7876 7877 ObjC().CheckTollFreeBridgeCast(castType, CastExpr); 7878 7879 ObjC().CheckObjCBridgeRelatedCast(castType, CastExpr); 7880 7881 DiscardMisalignedMemberAddress(castType.getTypePtr(), CastExpr); 7882 7883 return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); 7884 } 7885 7886 ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, 7887 SourceLocation RParenLoc, Expr *E, 7888 TypeSourceInfo *TInfo) { 7889 assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && 7890 "Expected paren or paren list expression"); 7891 7892 Expr **exprs; 7893 unsigned numExprs; 7894 Expr *subExpr; 7895 SourceLocation LiteralLParenLoc, LiteralRParenLoc; 7896 if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { 7897 LiteralLParenLoc = PE->getLParenLoc(); 7898 LiteralRParenLoc = PE->getRParenLoc(); 7899 exprs = PE->getExprs(); 7900 numExprs = PE->getNumExprs(); 7901 } else { // isa<ParenExpr> by assertion at function entrance 7902 LiteralLParenLoc = cast<ParenExpr>(E)->getLParen(); 7903 LiteralRParenLoc = cast<ParenExpr>(E)->getRParen(); 7904 subExpr = cast<ParenExpr>(E)->getSubExpr(); 7905 exprs = &subExpr; 7906 numExprs = 1; 7907 } 7908 7909 QualType Ty = TInfo->getType(); 7910 assert(Ty->isVectorType() && "Expected vector type"); 7911 7912 SmallVector<Expr *, 8> initExprs; 7913 const VectorType *VTy = Ty->castAs<VectorType>(); 7914 unsigned numElems = VTy->getNumElements(); 7915 7916 // '(...)' form of vector initialization in AltiVec: the number of 7917 // initializers must be one or must match the size of the vector. 7918 // If a single value is specified in the initializer then it will be 7919 // replicated to all the components of the vector 7920 if (CheckAltivecInitFromScalar(E->getSourceRange(), Ty, 7921 VTy->getElementType())) 7922 return ExprError(); 7923 if (ShouldSplatAltivecScalarInCast(VTy)) { 7924 // The number of initializers must be one or must match the size of the 7925 // vector. If a single value is specified in the initializer then it will 7926 // be replicated to all the components of the vector 7927 if (numExprs == 1) { 7928 QualType ElemTy = VTy->getElementType(); 7929 ExprResult Literal = DefaultLvalueConversion(exprs[0]); 7930 if (Literal.isInvalid()) 7931 return ExprError(); 7932 Literal = ImpCastExprToType(Literal.get(), ElemTy, 7933 PrepareScalarCast(Literal, ElemTy)); 7934 return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.get()); 7935 } 7936 else if (numExprs < numElems) { 7937 Diag(E->getExprLoc(), 7938 diag::err_incorrect_number_of_vector_initializers); 7939 return ExprError(); 7940 } 7941 else 7942 initExprs.append(exprs, exprs + numExprs); 7943 } 7944 else { 7945 // For OpenCL, when the number of initializers is a single value, 7946 // it will be replicated to all components of the vector. 7947 if (getLangOpts().OpenCL && VTy->getVectorKind() == VectorKind::Generic && 7948 numExprs == 1) { 7949 QualType ElemTy = VTy->getElementType(); 7950 ExprResult Literal = DefaultLvalueConversion(exprs[0]); 7951 if (Literal.isInvalid()) 7952 return ExprError(); 7953 Literal = ImpCastExprToType(Literal.get(), ElemTy, 7954 PrepareScalarCast(Literal, ElemTy)); 7955 return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.get()); 7956 } 7957 7958 initExprs.append(exprs, exprs + numExprs); 7959 } 7960 // FIXME: This means that pretty-printing the final AST will produce curly 7961 // braces instead of the original commas. 7962 InitListExpr *initE = new (Context) InitListExpr(Context, LiteralLParenLoc, 7963 initExprs, LiteralRParenLoc); 7964 initE->setType(Ty); 7965 return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); 7966 } 7967 7968 ExprResult 7969 Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { 7970 ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); 7971 if (!E) 7972 return OrigExpr; 7973 7974 ExprResult Result(E->getExpr(0)); 7975 7976 for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) 7977 Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), 7978 E->getExpr(i)); 7979 7980 if (Result.isInvalid()) return ExprError(); 7981 7982 return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); 7983 } 7984 7985 ExprResult Sema::ActOnParenListExpr(SourceLocation L, 7986 SourceLocation R, 7987 MultiExprArg Val) { 7988 return ParenListExpr::Create(Context, L, Val, R); 7989 } 7990 7991 ExprResult Sema::ActOnCXXParenListInitExpr(ArrayRef<Expr *> Args, QualType T, 7992 unsigned NumUserSpecifiedExprs, 7993 SourceLocation InitLoc, 7994 SourceLocation LParenLoc, 7995 SourceLocation RParenLoc) { 7996 return CXXParenListInitExpr::Create(Context, Args, T, NumUserSpecifiedExprs, 7997 InitLoc, LParenLoc, RParenLoc); 7998 } 7999 8000 bool Sema::DiagnoseConditionalForNull(const Expr *LHSExpr, const Expr *RHSExpr, 8001 SourceLocation QuestionLoc) { 8002 const Expr *NullExpr = LHSExpr; 8003 const Expr *NonPointerExpr = RHSExpr; 8004 Expr::NullPointerConstantKind NullKind = 8005 NullExpr->isNullPointerConstant(Context, 8006 Expr::NPC_ValueDependentIsNotNull); 8007 8008 if (NullKind == Expr::NPCK_NotNull) { 8009 NullExpr = RHSExpr; 8010 NonPointerExpr = LHSExpr; 8011 NullKind = 8012 NullExpr->isNullPointerConstant(Context, 8013 Expr::NPC_ValueDependentIsNotNull); 8014 } 8015 8016 if (NullKind == Expr::NPCK_NotNull) 8017 return false; 8018 8019 if (NullKind == Expr::NPCK_ZeroExpression) 8020 return false; 8021 8022 if (NullKind == Expr::NPCK_ZeroLiteral) { 8023 // In this case, check to make sure that we got here from a "NULL" 8024 // string in the source code. 8025 NullExpr = NullExpr->IgnoreParenImpCasts(); 8026 SourceLocation loc = NullExpr->getExprLoc(); 8027 if (!findMacroSpelling(loc, "NULL")) 8028 return false; 8029 } 8030 8031 int DiagType = (NullKind == Expr::NPCK_CXX11_nullptr); 8032 Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) 8033 << NonPointerExpr->getType() << DiagType 8034 << NonPointerExpr->getSourceRange(); 8035 return true; 8036 } 8037 8038 /// Return false if the condition expression is valid, true otherwise. 8039 static bool checkCondition(Sema &S, const Expr *Cond, 8040 SourceLocation QuestionLoc) { 8041 QualType CondTy = Cond->getType(); 8042 8043 // OpenCL v1.1 s6.3.i says the condition cannot be a floating point type. 8044 if (S.getLangOpts().OpenCL && CondTy->isFloatingType()) { 8045 S.Diag(QuestionLoc, diag::err_typecheck_cond_expect_nonfloat) 8046 << CondTy << Cond->getSourceRange(); 8047 return true; 8048 } 8049 8050 // C99 6.5.15p2 8051 if (CondTy->isScalarType()) return false; 8052 8053 S.Diag(QuestionLoc, diag::err_typecheck_cond_expect_scalar) 8054 << CondTy << Cond->getSourceRange(); 8055 return true; 8056 } 8057 8058 /// Return false if the NullExpr can be promoted to PointerTy, 8059 /// true otherwise. 8060 static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, 8061 QualType PointerTy) { 8062 if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || 8063 !NullExpr.get()->isNullPointerConstant(S.Context, 8064 Expr::NPC_ValueDependentIsNull)) 8065 return true; 8066 8067 NullExpr = S.ImpCastExprToType(NullExpr.get(), PointerTy, CK_NullToPointer); 8068 return false; 8069 } 8070 8071 /// Checks compatibility between two pointers and return the resulting 8072 /// type. 8073 static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, 8074 ExprResult &RHS, 8075 SourceLocation Loc) { 8076 QualType LHSTy = LHS.get()->getType(); 8077 QualType RHSTy = RHS.get()->getType(); 8078 8079 if (S.Context.hasSameType(LHSTy, RHSTy)) { 8080 // Two identical pointers types are always compatible. 8081 return S.Context.getCommonSugaredType(LHSTy, RHSTy); 8082 } 8083 8084 QualType lhptee, rhptee; 8085 8086 // Get the pointee types. 8087 bool IsBlockPointer = false; 8088 if (const BlockPointerType *LHSBTy = LHSTy->getAs<BlockPointerType>()) { 8089 lhptee = LHSBTy->getPointeeType(); 8090 rhptee = RHSTy->castAs<BlockPointerType>()->getPointeeType(); 8091 IsBlockPointer = true; 8092 } else { 8093 lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); 8094 rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); 8095 } 8096 8097 // C99 6.5.15p6: If both operands are pointers to compatible types or to 8098 // differently qualified versions of compatible types, the result type is 8099 // a pointer to an appropriately qualified version of the composite 8100 // type. 8101 8102 // Only CVR-qualifiers exist in the standard, and the differently-qualified 8103 // clause doesn't make sense for our extensions. E.g. address space 2 should 8104 // be incompatible with address space 3: they may live on different devices or 8105 // anything. 8106 Qualifiers lhQual = lhptee.getQualifiers(); 8107 Qualifiers rhQual = rhptee.getQualifiers(); 8108 8109 LangAS ResultAddrSpace = LangAS::Default; 8110 LangAS LAddrSpace = lhQual.getAddressSpace(); 8111 LangAS RAddrSpace = rhQual.getAddressSpace(); 8112 8113 // OpenCL v1.1 s6.5 - Conversion between pointers to distinct address 8114 // spaces is disallowed. 8115 if (lhQual.isAddressSpaceSupersetOf(rhQual, S.getASTContext())) 8116 ResultAddrSpace = LAddrSpace; 8117 else if (rhQual.isAddressSpaceSupersetOf(lhQual, S.getASTContext())) 8118 ResultAddrSpace = RAddrSpace; 8119 else { 8120 S.Diag(Loc, diag::err_typecheck_op_on_nonoverlapping_address_space_pointers) 8121 << LHSTy << RHSTy << 2 << LHS.get()->getSourceRange() 8122 << RHS.get()->getSourceRange(); 8123 return QualType(); 8124 } 8125 8126 unsigned MergedCVRQual = lhQual.getCVRQualifiers() | rhQual.getCVRQualifiers(); 8127 auto LHSCastKind = CK_BitCast, RHSCastKind = CK_BitCast; 8128 lhQual.removeCVRQualifiers(); 8129 rhQual.removeCVRQualifiers(); 8130 8131 if (!lhQual.getPointerAuth().isEquivalent(rhQual.getPointerAuth())) { 8132 S.Diag(Loc, diag::err_typecheck_cond_incompatible_ptrauth) 8133 << LHSTy << RHSTy << LHS.get()->getSourceRange() 8134 << RHS.get()->getSourceRange(); 8135 return QualType(); 8136 } 8137 8138 // OpenCL v2.0 specification doesn't extend compatibility of type qualifiers 8139 // (C99 6.7.3) for address spaces. We assume that the check should behave in 8140 // the same manner as it's defined for CVR qualifiers, so for OpenCL two 8141 // qual types are compatible iff 8142 // * corresponded types are compatible 8143 // * CVR qualifiers are equal 8144 // * address spaces are equal 8145 // Thus for conditional operator we merge CVR and address space unqualified 8146 // pointees and if there is a composite type we return a pointer to it with 8147 // merged qualifiers. 8148 LHSCastKind = 8149 LAddrSpace == ResultAddrSpace ? CK_BitCast : CK_AddressSpaceConversion; 8150 RHSCastKind = 8151 RAddrSpace == ResultAddrSpace ? CK_BitCast : CK_AddressSpaceConversion; 8152 lhQual.removeAddressSpace(); 8153 rhQual.removeAddressSpace(); 8154 8155 lhptee = S.Context.getQualifiedType(lhptee.getUnqualifiedType(), lhQual); 8156 rhptee = S.Context.getQualifiedType(rhptee.getUnqualifiedType(), rhQual); 8157 8158 QualType CompositeTy = S.Context.mergeTypes( 8159 lhptee, rhptee, /*OfBlockPointer=*/false, /*Unqualified=*/false, 8160 /*BlockReturnType=*/false, /*IsConditionalOperator=*/true); 8161 8162 if (CompositeTy.isNull()) { 8163 // In this situation, we assume void* type. No especially good 8164 // reason, but this is what gcc does, and we do have to pick 8165 // to get a consistent AST. 8166 QualType incompatTy; 8167 incompatTy = S.Context.getPointerType( 8168 S.Context.getAddrSpaceQualType(S.Context.VoidTy, ResultAddrSpace)); 8169 LHS = S.ImpCastExprToType(LHS.get(), incompatTy, LHSCastKind); 8170 RHS = S.ImpCastExprToType(RHS.get(), incompatTy, RHSCastKind); 8171 8172 // FIXME: For OpenCL the warning emission and cast to void* leaves a room 8173 // for casts between types with incompatible address space qualifiers. 8174 // For the following code the compiler produces casts between global and 8175 // local address spaces of the corresponded innermost pointees: 8176 // local int *global *a; 8177 // global int *global *b; 8178 // a = (0 ? a : b); // see C99 6.5.16.1.p1. 8179 S.Diag(Loc, diag::ext_typecheck_cond_incompatible_pointers) 8180 << LHSTy << RHSTy << LHS.get()->getSourceRange() 8181 << RHS.get()->getSourceRange(); 8182 8183 return incompatTy; 8184 } 8185 8186 // The pointer types are compatible. 8187 // In case of OpenCL ResultTy should have the address space qualifier 8188 // which is a superset of address spaces of both the 2nd and the 3rd 8189 // operands of the conditional operator. 8190 QualType ResultTy = [&, ResultAddrSpace]() { 8191 if (S.getLangOpts().OpenCL) { 8192 Qualifiers CompositeQuals = CompositeTy.getQualifiers(); 8193 CompositeQuals.setAddressSpace(ResultAddrSpace); 8194 return S.Context 8195 .getQualifiedType(CompositeTy.getUnqualifiedType(), CompositeQuals) 8196 .withCVRQualifiers(MergedCVRQual); 8197 } 8198 return CompositeTy.withCVRQualifiers(MergedCVRQual); 8199 }(); 8200 if (IsBlockPointer) 8201 ResultTy = S.Context.getBlockPointerType(ResultTy); 8202 else 8203 ResultTy = S.Context.getPointerType(ResultTy); 8204 8205 LHS = S.ImpCastExprToType(LHS.get(), ResultTy, LHSCastKind); 8206 RHS = S.ImpCastExprToType(RHS.get(), ResultTy, RHSCastKind); 8207 return ResultTy; 8208 } 8209 8210 /// Return the resulting type when the operands are both block pointers. 8211 static QualType checkConditionalBlockPointerCompatibility(Sema &S, 8212 ExprResult &LHS, 8213 ExprResult &RHS, 8214 SourceLocation Loc) { 8215 QualType LHSTy = LHS.get()->getType(); 8216 QualType RHSTy = RHS.get()->getType(); 8217 8218 if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { 8219 if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { 8220 QualType destType = S.Context.getPointerType(S.Context.VoidTy); 8221 LHS = S.ImpCastExprToType(LHS.get(), destType, CK_BitCast); 8222 RHS = S.ImpCastExprToType(RHS.get(), destType, CK_BitCast); 8223 return destType; 8224 } 8225 S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) 8226 << LHSTy << RHSTy << LHS.get()->getSourceRange() 8227 << RHS.get()->getSourceRange(); 8228 return QualType(); 8229 } 8230 8231 // We have 2 block pointer types. 8232 return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); 8233 } 8234 8235 /// Return the resulting type when the operands are both pointers. 8236 static QualType 8237 checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, 8238 ExprResult &RHS, 8239 SourceLocation Loc) { 8240 // get the pointer types 8241 QualType LHSTy = LHS.get()->getType(); 8242 QualType RHSTy = RHS.get()->getType(); 8243 8244 // get the "pointed to" types 8245 QualType lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); 8246 QualType rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); 8247 8248 // ignore qualifiers on void (C99 6.5.15p3, clause 6) 8249 if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { 8250 // Figure out necessary qualifiers (C99 6.5.15p6) 8251 QualType destPointee 8252 = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); 8253 QualType destType = S.Context.getPointerType(destPointee); 8254 // Add qualifiers if necessary. 8255 LHS = S.ImpCastExprToType(LHS.get(), destType, CK_NoOp); 8256 // Promote to void*. 8257 RHS = S.ImpCastExprToType(RHS.get(), destType, CK_BitCast); 8258 return destType; 8259 } 8260 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { 8261 QualType destPointee 8262 = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); 8263 QualType destType = S.Context.getPointerType(destPointee); 8264 // Add qualifiers if necessary. 8265 RHS = S.ImpCastExprToType(RHS.get(), destType, CK_NoOp); 8266 // Promote to void*. 8267 LHS = S.ImpCastExprToType(LHS.get(), destType, CK_BitCast); 8268 return destType; 8269 } 8270 8271 return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); 8272 } 8273 8274 /// Return false if the first expression is not an integer and the second 8275 /// expression is not a pointer, true otherwise. 8276 static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, 8277 Expr* PointerExpr, SourceLocation Loc, 8278 bool IsIntFirstExpr) { 8279 if (!PointerExpr->getType()->isPointerType() || 8280 !Int.get()->getType()->isIntegerType()) 8281 return false; 8282 8283 Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; 8284 Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); 8285 8286 S.Diag(Loc, diag::ext_typecheck_cond_pointer_integer_mismatch) 8287 << Expr1->getType() << Expr2->getType() 8288 << Expr1->getSourceRange() << Expr2->getSourceRange(); 8289 Int = S.ImpCastExprToType(Int.get(), PointerExpr->getType(), 8290 CK_IntegralToPointer); 8291 return true; 8292 } 8293 8294 /// Simple conversion between integer and floating point types. 8295 /// 8296 /// Used when handling the OpenCL conditional operator where the 8297 /// condition is a vector while the other operands are scalar. 8298 /// 8299 /// OpenCL v1.1 s6.3.i and s6.11.6 together require that the scalar 8300 /// types are either integer or floating type. Between the two 8301 /// operands, the type with the higher rank is defined as the "result 8302 /// type". The other operand needs to be promoted to the same type. No 8303 /// other type promotion is allowed. We cannot use 8304 /// UsualArithmeticConversions() for this purpose, since it always 8305 /// promotes promotable types. 8306 static QualType OpenCLArithmeticConversions(Sema &S, ExprResult &LHS, 8307 ExprResult &RHS, 8308 SourceLocation QuestionLoc) { 8309 LHS = S.DefaultFunctionArrayLvalueConversion(LHS.get()); 8310 if (LHS.isInvalid()) 8311 return QualType(); 8312 RHS = S.DefaultFunctionArrayLvalueConversion(RHS.get()); 8313 if (RHS.isInvalid()) 8314 return QualType(); 8315 8316 // For conversion purposes, we ignore any qualifiers. 8317 // For example, "const float" and "float" are equivalent. 8318 QualType LHSType = 8319 S.Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); 8320 QualType RHSType = 8321 S.Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); 8322 8323 if (!LHSType->isIntegerType() && !LHSType->isRealFloatingType()) { 8324 S.Diag(QuestionLoc, diag::err_typecheck_cond_expect_int_float) 8325 << LHSType << LHS.get()->getSourceRange(); 8326 return QualType(); 8327 } 8328 8329 if (!RHSType->isIntegerType() && !RHSType->isRealFloatingType()) { 8330 S.Diag(QuestionLoc, diag::err_typecheck_cond_expect_int_float) 8331 << RHSType << RHS.get()->getSourceRange(); 8332 return QualType(); 8333 } 8334 8335 // If both types are identical, no conversion is needed. 8336 if (LHSType == RHSType) 8337 return LHSType; 8338 8339 // Now handle "real" floating types (i.e. float, double, long double). 8340 if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) 8341 return handleFloatConversion(S, LHS, RHS, LHSType, RHSType, 8342 /*IsCompAssign = */ false); 8343 8344 // Finally, we have two differing integer types. 8345 return handleIntegerConversion<doIntegralCast, doIntegralCast> 8346 (S, LHS, RHS, LHSType, RHSType, /*IsCompAssign = */ false); 8347 } 8348 8349 /// Convert scalar operands to a vector that matches the 8350 /// condition in length. 8351 /// 8352 /// Used when handling the OpenCL conditional operator where the 8353 /// condition is a vector while the other operands are scalar. 8354 /// 8355 /// We first compute the "result type" for the scalar operands 8356 /// according to OpenCL v1.1 s6.3.i. Both operands are then converted 8357 /// into a vector of that type where the length matches the condition 8358 /// vector type. s6.11.6 requires that the element types of the result 8359 /// and the condition must have the same number of bits. 8360 static QualType 8361 OpenCLConvertScalarsToVectors(Sema &S, ExprResult &LHS, ExprResult &RHS, 8362 QualType CondTy, SourceLocation QuestionLoc) { 8363 QualType ResTy = OpenCLArithmeticConversions(S, LHS, RHS, QuestionLoc); 8364 if (ResTy.isNull()) return QualType(); 8365 8366 const VectorType *CV = CondTy->getAs<VectorType>(); 8367 assert(CV); 8368 8369 // Determine the vector result type 8370 unsigned NumElements = CV->getNumElements(); 8371 QualType VectorTy = S.Context.getExtVectorType(ResTy, NumElements); 8372 8373 // Ensure that all types have the same number of bits 8374 if (S.Context.getTypeSize(CV->getElementType()) 8375 != S.Context.getTypeSize(ResTy)) { 8376 // Since VectorTy is created internally, it does not pretty print 8377 // with an OpenCL name. Instead, we just print a description. 8378 std::string EleTyName = ResTy.getUnqualifiedType().getAsString(); 8379 SmallString<64> Str; 8380 llvm::raw_svector_ostream OS(Str); 8381 OS << "(vector of " << NumElements << " '" << EleTyName << "' values)"; 8382 S.Diag(QuestionLoc, diag::err_conditional_vector_element_size) 8383 << CondTy << OS.str(); 8384 return QualType(); 8385 } 8386 8387 // Convert operands to the vector result type 8388 LHS = S.ImpCastExprToType(LHS.get(), VectorTy, CK_VectorSplat); 8389 RHS = S.ImpCastExprToType(RHS.get(), VectorTy, CK_VectorSplat); 8390 8391 return VectorTy; 8392 } 8393 8394 /// Return false if this is a valid OpenCL condition vector 8395 static bool checkOpenCLConditionVector(Sema &S, Expr *Cond, 8396 SourceLocation QuestionLoc) { 8397 // OpenCL v1.1 s6.11.6 says the elements of the vector must be of 8398 // integral type. 8399 const VectorType *CondTy = Cond->getType()->getAs<VectorType>(); 8400 assert(CondTy); 8401 QualType EleTy = CondTy->getElementType(); 8402 if (EleTy->isIntegerType()) return false; 8403 8404 S.Diag(QuestionLoc, diag::err_typecheck_cond_expect_nonfloat) 8405 << Cond->getType() << Cond->getSourceRange(); 8406 return true; 8407 } 8408 8409 /// Return false if the vector condition type and the vector 8410 /// result type are compatible. 8411 /// 8412 /// OpenCL v1.1 s6.11.6 requires that both vector types have the same 8413 /// number of elements, and their element types have the same number 8414 /// of bits. 8415 static bool checkVectorResult(Sema &S, QualType CondTy, QualType VecResTy, 8416 SourceLocation QuestionLoc) { 8417 const VectorType *CV = CondTy->getAs<VectorType>(); 8418 const VectorType *RV = VecResTy->getAs<VectorType>(); 8419 assert(CV && RV); 8420 8421 if (CV->getNumElements() != RV->getNumElements()) { 8422 S.Diag(QuestionLoc, diag::err_conditional_vector_size) 8423 << CondTy << VecResTy; 8424 return true; 8425 } 8426 8427 QualType CVE = CV->getElementType(); 8428 QualType RVE = RV->getElementType(); 8429 8430 if (S.Context.getTypeSize(CVE) != S.Context.getTypeSize(RVE)) { 8431 S.Diag(QuestionLoc, diag::err_conditional_vector_element_size) 8432 << CondTy << VecResTy; 8433 return true; 8434 } 8435 8436 return false; 8437 } 8438 8439 /// Return the resulting type for the conditional operator in 8440 /// OpenCL (aka "ternary selection operator", OpenCL v1.1 8441 /// s6.3.i) when the condition is a vector type. 8442 static QualType 8443 OpenCLCheckVectorConditional(Sema &S, ExprResult &Cond, 8444 ExprResult &LHS, ExprResult &RHS, 8445 SourceLocation QuestionLoc) { 8446 Cond = S.DefaultFunctionArrayLvalueConversion(Cond.get()); 8447 if (Cond.isInvalid()) 8448 return QualType(); 8449 QualType CondTy = Cond.get()->getType(); 8450 8451 if (checkOpenCLConditionVector(S, Cond.get(), QuestionLoc)) 8452 return QualType(); 8453 8454 // If either operand is a vector then find the vector type of the 8455 // result as specified in OpenCL v1.1 s6.3.i. 8456 if (LHS.get()->getType()->isVectorType() || 8457 RHS.get()->getType()->isVectorType()) { 8458 bool IsBoolVecLang = 8459 !S.getLangOpts().OpenCL && !S.getLangOpts().OpenCLCPlusPlus; 8460 QualType VecResTy = 8461 S.CheckVectorOperands(LHS, RHS, QuestionLoc, 8462 /*isCompAssign*/ false, 8463 /*AllowBothBool*/ true, 8464 /*AllowBoolConversions*/ false, 8465 /*AllowBooleanOperation*/ IsBoolVecLang, 8466 /*ReportInvalid*/ true); 8467 if (VecResTy.isNull()) 8468 return QualType(); 8469 // The result type must match the condition type as specified in 8470 // OpenCL v1.1 s6.11.6. 8471 if (checkVectorResult(S, CondTy, VecResTy, QuestionLoc)) 8472 return QualType(); 8473 return VecResTy; 8474 } 8475 8476 // Both operands are scalar. 8477 return OpenCLConvertScalarsToVectors(S, LHS, RHS, CondTy, QuestionLoc); 8478 } 8479 8480 /// Return true if the Expr is block type 8481 static bool checkBlockType(Sema &S, const Expr *E) { 8482 if (E->getType()->isBlockPointerType()) { 8483 S.Diag(E->getExprLoc(), diag::err_opencl_ternary_with_block); 8484 return true; 8485 } 8486 8487 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 8488 QualType Ty = CE->getCallee()->getType(); 8489 if (Ty->isBlockPointerType()) { 8490 S.Diag(E->getExprLoc(), diag::err_opencl_ternary_with_block); 8491 return true; 8492 } 8493 } 8494 return false; 8495 } 8496 8497 /// Note that LHS is not null here, even if this is the gnu "x ?: y" extension. 8498 /// In that case, LHS = cond. 8499 /// C99 6.5.15 8500 QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, 8501 ExprResult &RHS, ExprValueKind &VK, 8502 ExprObjectKind &OK, 8503 SourceLocation QuestionLoc) { 8504 8505 ExprResult LHSResult = CheckPlaceholderExpr(LHS.get()); 8506 if (!LHSResult.isUsable()) return QualType(); 8507 LHS = LHSResult; 8508 8509 ExprResult RHSResult = CheckPlaceholderExpr(RHS.get()); 8510 if (!RHSResult.isUsable()) return QualType(); 8511 RHS = RHSResult; 8512 8513 // C++ is sufficiently different to merit its own checker. 8514 if (getLangOpts().CPlusPlus) 8515 return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); 8516 8517 VK = VK_PRValue; 8518 OK = OK_Ordinary; 8519 8520 if (Context.isDependenceAllowed() && 8521 (Cond.get()->isTypeDependent() || LHS.get()->isTypeDependent() || 8522 RHS.get()->isTypeDependent())) { 8523 assert(!getLangOpts().CPlusPlus); 8524 assert((Cond.get()->containsErrors() || LHS.get()->containsErrors() || 8525 RHS.get()->containsErrors()) && 8526 "should only occur in error-recovery path."); 8527 return Context.DependentTy; 8528 } 8529 8530 // The OpenCL operator with a vector condition is sufficiently 8531 // different to merit its own checker. 8532 if ((getLangOpts().OpenCL && Cond.get()->getType()->isVectorType()) || 8533 Cond.get()->getType()->isExtVectorType()) 8534 return OpenCLCheckVectorConditional(*this, Cond, LHS, RHS, QuestionLoc); 8535 8536 // First, check the condition. 8537 Cond = UsualUnaryConversions(Cond.get()); 8538 if (Cond.isInvalid()) 8539 return QualType(); 8540 if (checkCondition(*this, Cond.get(), QuestionLoc)) 8541 return QualType(); 8542 8543 // Handle vectors. 8544 if (LHS.get()->getType()->isVectorType() || 8545 RHS.get()->getType()->isVectorType()) 8546 return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/ false, 8547 /*AllowBothBool*/ true, 8548 /*AllowBoolConversions*/ false, 8549 /*AllowBooleanOperation*/ false, 8550 /*ReportInvalid*/ true); 8551 8552 QualType ResTy = UsualArithmeticConversions(LHS, RHS, QuestionLoc, 8553 ArithConvKind::Conditional); 8554 if (LHS.isInvalid() || RHS.isInvalid()) 8555 return QualType(); 8556 8557 // WebAssembly tables are not allowed as conditional LHS or RHS. 8558 QualType LHSTy = LHS.get()->getType(); 8559 QualType RHSTy = RHS.get()->getType(); 8560 if (LHSTy->isWebAssemblyTableType() || RHSTy->isWebAssemblyTableType()) { 8561 Diag(QuestionLoc, diag::err_wasm_table_conditional_expression) 8562 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 8563 return QualType(); 8564 } 8565 8566 // Diagnose attempts to convert between __ibm128, __float128 and long double 8567 // where such conversions currently can't be handled. 8568 if (unsupportedTypeConversion(*this, LHSTy, RHSTy)) { 8569 Diag(QuestionLoc, 8570 diag::err_typecheck_cond_incompatible_operands) << LHSTy << RHSTy 8571 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 8572 return QualType(); 8573 } 8574 8575 // OpenCL v2.0 s6.12.5 - Blocks cannot be used as expressions of the ternary 8576 // selection operator (?:). 8577 if (getLangOpts().OpenCL && 8578 ((int)checkBlockType(*this, LHS.get()) | (int)checkBlockType(*this, RHS.get()))) { 8579 return QualType(); 8580 } 8581 8582 // If both operands have arithmetic type, do the usual arithmetic conversions 8583 // to find a common type: C99 6.5.15p3,5. 8584 if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { 8585 // Disallow invalid arithmetic conversions, such as those between bit- 8586 // precise integers types of different sizes, or between a bit-precise 8587 // integer and another type. 8588 if (ResTy.isNull() && (LHSTy->isBitIntType() || RHSTy->isBitIntType())) { 8589 Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) 8590 << LHSTy << RHSTy << LHS.get()->getSourceRange() 8591 << RHS.get()->getSourceRange(); 8592 return QualType(); 8593 } 8594 8595 LHS = ImpCastExprToType(LHS.get(), ResTy, PrepareScalarCast(LHS, ResTy)); 8596 RHS = ImpCastExprToType(RHS.get(), ResTy, PrepareScalarCast(RHS, ResTy)); 8597 8598 return ResTy; 8599 } 8600 8601 // If both operands are the same structure or union type, the result is that 8602 // type. 8603 if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 8604 if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) 8605 if (LHSRT->getDecl() == RHSRT->getDecl()) 8606 // "If both the operands have structure or union type, the result has 8607 // that type." This implies that CV qualifiers are dropped. 8608 return Context.getCommonSugaredType(LHSTy.getUnqualifiedType(), 8609 RHSTy.getUnqualifiedType()); 8610 // FIXME: Type of conditional expression must be complete in C mode. 8611 } 8612 8613 // C99 6.5.15p5: "If both operands have void type, the result has void type." 8614 // The following || allows only one side to be void (a GCC-ism). 8615 if (LHSTy->isVoidType() || RHSTy->isVoidType()) { 8616 QualType ResTy; 8617 if (LHSTy->isVoidType() && RHSTy->isVoidType()) { 8618 ResTy = Context.getCommonSugaredType(LHSTy, RHSTy); 8619 } else if (RHSTy->isVoidType()) { 8620 ResTy = RHSTy; 8621 Diag(RHS.get()->getBeginLoc(), diag::ext_typecheck_cond_one_void) 8622 << RHS.get()->getSourceRange(); 8623 } else { 8624 ResTy = LHSTy; 8625 Diag(LHS.get()->getBeginLoc(), diag::ext_typecheck_cond_one_void) 8626 << LHS.get()->getSourceRange(); 8627 } 8628 LHS = ImpCastExprToType(LHS.get(), ResTy, CK_ToVoid); 8629 RHS = ImpCastExprToType(RHS.get(), ResTy, CK_ToVoid); 8630 return ResTy; 8631 } 8632 8633 // C23 6.5.15p7: 8634 // ... if both the second and third operands have nullptr_t type, the 8635 // result also has that type. 8636 if (LHSTy->isNullPtrType() && Context.hasSameType(LHSTy, RHSTy)) 8637 return ResTy; 8638 8639 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has 8640 // the type of the other operand." 8641 if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; 8642 if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; 8643 8644 // All objective-c pointer type analysis is done here. 8645 QualType compositeType = 8646 ObjC().FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); 8647 if (LHS.isInvalid() || RHS.isInvalid()) 8648 return QualType(); 8649 if (!compositeType.isNull()) 8650 return compositeType; 8651 8652 8653 // Handle block pointer types. 8654 if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) 8655 return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, 8656 QuestionLoc); 8657 8658 // Check constraints for C object pointers types (C99 6.5.15p3,6). 8659 if (LHSTy->isPointerType() && RHSTy->isPointerType()) 8660 return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, 8661 QuestionLoc); 8662 8663 // GCC compatibility: soften pointer/integer mismatch. Note that 8664 // null pointers have been filtered out by this point. 8665 if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, 8666 /*IsIntFirstExpr=*/true)) 8667 return RHSTy; 8668 if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, 8669 /*IsIntFirstExpr=*/false)) 8670 return LHSTy; 8671 8672 // Emit a better diagnostic if one of the expressions is a null pointer 8673 // constant and the other is not a pointer type. In this case, the user most 8674 // likely forgot to take the address of the other expression. 8675 if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) 8676 return QualType(); 8677 8678 // Finally, if the LHS and RHS types are canonically the same type, we can 8679 // use the common sugared type. 8680 if (Context.hasSameType(LHSTy, RHSTy)) 8681 return Context.getCommonSugaredType(LHSTy, RHSTy); 8682 8683 // Otherwise, the operands are not compatible. 8684 Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) 8685 << LHSTy << RHSTy << LHS.get()->getSourceRange() 8686 << RHS.get()->getSourceRange(); 8687 return QualType(); 8688 } 8689 8690 /// SuggestParentheses - Emit a note with a fixit hint that wraps 8691 /// ParenRange in parentheses. 8692 static void SuggestParentheses(Sema &Self, SourceLocation Loc, 8693 const PartialDiagnostic &Note, 8694 SourceRange ParenRange) { 8695 SourceLocation EndLoc = Self.getLocForEndOfToken(ParenRange.getEnd()); 8696 if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && 8697 EndLoc.isValid()) { 8698 Self.Diag(Loc, Note) 8699 << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") 8700 << FixItHint::CreateInsertion(EndLoc, ")"); 8701 } else { 8702 // We can't display the parentheses, so just show the bare note. 8703 Self.Diag(Loc, Note) << ParenRange; 8704 } 8705 } 8706 8707 static bool IsArithmeticOp(BinaryOperatorKind Opc) { 8708 return BinaryOperator::isAdditiveOp(Opc) || 8709 BinaryOperator::isMultiplicativeOp(Opc) || 8710 BinaryOperator::isShiftOp(Opc) || Opc == BO_And || Opc == BO_Or; 8711 // This only checks for bitwise-or and bitwise-and, but not bitwise-xor and 8712 // not any of the logical operators. Bitwise-xor is commonly used as a 8713 // logical-xor because there is no logical-xor operator. The logical 8714 // operators, including uses of xor, have a high false positive rate for 8715 // precedence warnings. 8716 } 8717 8718 /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary 8719 /// expression, either using a built-in or overloaded operator, 8720 /// and sets *OpCode to the opcode and *RHSExprs to the right-hand side 8721 /// expression. 8722 static bool IsArithmeticBinaryExpr(const Expr *E, BinaryOperatorKind *Opcode, 8723 const Expr **RHSExprs) { 8724 // Don't strip parenthesis: we should not warn if E is in parenthesis. 8725 E = E->IgnoreImpCasts(); 8726 E = E->IgnoreConversionOperatorSingleStep(); 8727 E = E->IgnoreImpCasts(); 8728 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) { 8729 E = MTE->getSubExpr(); 8730 E = E->IgnoreImpCasts(); 8731 } 8732 8733 // Built-in binary operator. 8734 if (const auto *OP = dyn_cast<BinaryOperator>(E); 8735 OP && IsArithmeticOp(OP->getOpcode())) { 8736 *Opcode = OP->getOpcode(); 8737 *RHSExprs = OP->getRHS(); 8738 return true; 8739 } 8740 8741 // Overloaded operator. 8742 if (const auto *Call = dyn_cast<CXXOperatorCallExpr>(E)) { 8743 if (Call->getNumArgs() != 2) 8744 return false; 8745 8746 // Make sure this is really a binary operator that is safe to pass into 8747 // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. 8748 OverloadedOperatorKind OO = Call->getOperator(); 8749 if (OO < OO_Plus || OO > OO_Arrow || 8750 OO == OO_PlusPlus || OO == OO_MinusMinus) 8751 return false; 8752 8753 BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); 8754 if (IsArithmeticOp(OpKind)) { 8755 *Opcode = OpKind; 8756 *RHSExprs = Call->getArg(1); 8757 return true; 8758 } 8759 } 8760 8761 return false; 8762 } 8763 8764 /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type 8765 /// or is a logical expression such as (x==y) which has int type, but is 8766 /// commonly interpreted as boolean. 8767 static bool ExprLooksBoolean(const Expr *E) { 8768 E = E->IgnoreParenImpCasts(); 8769 8770 if (E->getType()->isBooleanType()) 8771 return true; 8772 if (const auto *OP = dyn_cast<BinaryOperator>(E)) 8773 return OP->isComparisonOp() || OP->isLogicalOp(); 8774 if (const auto *OP = dyn_cast<UnaryOperator>(E)) 8775 return OP->getOpcode() == UO_LNot; 8776 if (E->getType()->isPointerType()) 8777 return true; 8778 // FIXME: What about overloaded operator calls returning "unspecified boolean 8779 // type"s (commonly pointer-to-members)? 8780 8781 return false; 8782 } 8783 8784 /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator 8785 /// and binary operator are mixed in a way that suggests the programmer assumed 8786 /// the conditional operator has higher precedence, for example: 8787 /// "int x = a + someBinaryCondition ? 1 : 2". 8788 static void DiagnoseConditionalPrecedence(Sema &Self, SourceLocation OpLoc, 8789 Expr *Condition, const Expr *LHSExpr, 8790 const Expr *RHSExpr) { 8791 BinaryOperatorKind CondOpcode; 8792 const Expr *CondRHS; 8793 8794 if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) 8795 return; 8796 if (!ExprLooksBoolean(CondRHS)) 8797 return; 8798 8799 // The condition is an arithmetic binary expression, with a right- 8800 // hand side that looks boolean, so warn. 8801 8802 unsigned DiagID = BinaryOperator::isBitwiseOp(CondOpcode) 8803 ? diag::warn_precedence_bitwise_conditional 8804 : diag::warn_precedence_conditional; 8805 8806 Self.Diag(OpLoc, DiagID) 8807 << Condition->getSourceRange() 8808 << BinaryOperator::getOpcodeStr(CondOpcode); 8809 8810 SuggestParentheses( 8811 Self, OpLoc, 8812 Self.PDiag(diag::note_precedence_silence) 8813 << BinaryOperator::getOpcodeStr(CondOpcode), 8814 SourceRange(Condition->getBeginLoc(), Condition->getEndLoc())); 8815 8816 SuggestParentheses(Self, OpLoc, 8817 Self.PDiag(diag::note_precedence_conditional_first), 8818 SourceRange(CondRHS->getBeginLoc(), RHSExpr->getEndLoc())); 8819 } 8820 8821 /// Compute the nullability of a conditional expression. 8822 static QualType computeConditionalNullability(QualType ResTy, bool IsBin, 8823 QualType LHSTy, QualType RHSTy, 8824 ASTContext &Ctx) { 8825 if (!ResTy->isAnyPointerType()) 8826 return ResTy; 8827 8828 auto GetNullability = [](QualType Ty) { 8829 std::optional<NullabilityKind> Kind = Ty->getNullability(); 8830 if (Kind) { 8831 // For our purposes, treat _Nullable_result as _Nullable. 8832 if (*Kind == NullabilityKind::NullableResult) 8833 return NullabilityKind::Nullable; 8834 return *Kind; 8835 } 8836 return NullabilityKind::Unspecified; 8837 }; 8838 8839 auto LHSKind = GetNullability(LHSTy), RHSKind = GetNullability(RHSTy); 8840 NullabilityKind MergedKind; 8841 8842 // Compute nullability of a binary conditional expression. 8843 if (IsBin) { 8844 if (LHSKind == NullabilityKind::NonNull) 8845 MergedKind = NullabilityKind::NonNull; 8846 else 8847 MergedKind = RHSKind; 8848 // Compute nullability of a normal conditional expression. 8849 } else { 8850 if (LHSKind == NullabilityKind::Nullable || 8851 RHSKind == NullabilityKind::Nullable) 8852 MergedKind = NullabilityKind::Nullable; 8853 else if (LHSKind == NullabilityKind::NonNull) 8854 MergedKind = RHSKind; 8855 else if (RHSKind == NullabilityKind::NonNull) 8856 MergedKind = LHSKind; 8857 else 8858 MergedKind = NullabilityKind::Unspecified; 8859 } 8860 8861 // Return if ResTy already has the correct nullability. 8862 if (GetNullability(ResTy) == MergedKind) 8863 return ResTy; 8864 8865 // Strip all nullability from ResTy. 8866 while (ResTy->getNullability()) 8867 ResTy = ResTy.getSingleStepDesugaredType(Ctx); 8868 8869 // Create a new AttributedType with the new nullability kind. 8870 return Ctx.getAttributedType(MergedKind, ResTy, ResTy); 8871 } 8872 8873 ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, 8874 SourceLocation ColonLoc, 8875 Expr *CondExpr, Expr *LHSExpr, 8876 Expr *RHSExpr) { 8877 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS 8878 // was the condition. 8879 OpaqueValueExpr *opaqueValue = nullptr; 8880 Expr *commonExpr = nullptr; 8881 if (!LHSExpr) { 8882 commonExpr = CondExpr; 8883 // Lower out placeholder types first. This is important so that we don't 8884 // try to capture a placeholder. This happens in few cases in C++; such 8885 // as Objective-C++'s dictionary subscripting syntax. 8886 if (commonExpr->hasPlaceholderType()) { 8887 ExprResult result = CheckPlaceholderExpr(commonExpr); 8888 if (!result.isUsable()) return ExprError(); 8889 commonExpr = result.get(); 8890 } 8891 // We usually want to apply unary conversions *before* saving, except 8892 // in the special case of a C++ l-value conditional. 8893 if (!(getLangOpts().CPlusPlus 8894 && !commonExpr->isTypeDependent() 8895 && commonExpr->getValueKind() == RHSExpr->getValueKind() 8896 && commonExpr->isGLValue() 8897 && commonExpr->isOrdinaryOrBitFieldObject() 8898 && RHSExpr->isOrdinaryOrBitFieldObject() 8899 && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { 8900 ExprResult commonRes = UsualUnaryConversions(commonExpr); 8901 if (commonRes.isInvalid()) 8902 return ExprError(); 8903 commonExpr = commonRes.get(); 8904 } 8905 8906 // If the common expression is a class or array prvalue, materialize it 8907 // so that we can safely refer to it multiple times. 8908 if (commonExpr->isPRValue() && (commonExpr->getType()->isRecordType() || 8909 commonExpr->getType()->isArrayType())) { 8910 ExprResult MatExpr = TemporaryMaterializationConversion(commonExpr); 8911 if (MatExpr.isInvalid()) 8912 return ExprError(); 8913 commonExpr = MatExpr.get(); 8914 } 8915 8916 opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), 8917 commonExpr->getType(), 8918 commonExpr->getValueKind(), 8919 commonExpr->getObjectKind(), 8920 commonExpr); 8921 LHSExpr = CondExpr = opaqueValue; 8922 } 8923 8924 QualType LHSTy = LHSExpr->getType(), RHSTy = RHSExpr->getType(); 8925 ExprValueKind VK = VK_PRValue; 8926 ExprObjectKind OK = OK_Ordinary; 8927 ExprResult Cond = CondExpr, LHS = LHSExpr, RHS = RHSExpr; 8928 QualType result = CheckConditionalOperands(Cond, LHS, RHS, 8929 VK, OK, QuestionLoc); 8930 if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || 8931 RHS.isInvalid()) 8932 return ExprError(); 8933 8934 DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), 8935 RHS.get()); 8936 8937 CheckBoolLikeConversion(Cond.get(), QuestionLoc); 8938 8939 result = computeConditionalNullability(result, commonExpr, LHSTy, RHSTy, 8940 Context); 8941 8942 if (!commonExpr) 8943 return new (Context) 8944 ConditionalOperator(Cond.get(), QuestionLoc, LHS.get(), ColonLoc, 8945 RHS.get(), result, VK, OK); 8946 8947 return new (Context) BinaryConditionalOperator( 8948 commonExpr, opaqueValue, Cond.get(), LHS.get(), RHS.get(), QuestionLoc, 8949 ColonLoc, result, VK, OK); 8950 } 8951 8952 bool Sema::IsInvalidSMECallConversion(QualType FromType, QualType ToType) { 8953 unsigned FromAttributes = 0, ToAttributes = 0; 8954 if (const auto *FromFn = 8955 dyn_cast<FunctionProtoType>(Context.getCanonicalType(FromType))) 8956 FromAttributes = 8957 FromFn->getAArch64SMEAttributes() & FunctionType::SME_AttributeMask; 8958 if (const auto *ToFn = 8959 dyn_cast<FunctionProtoType>(Context.getCanonicalType(ToType))) 8960 ToAttributes = 8961 ToFn->getAArch64SMEAttributes() & FunctionType::SME_AttributeMask; 8962 8963 return FromAttributes != ToAttributes; 8964 } 8965 8966 // Check if we have a conversion between incompatible cmse function pointer 8967 // types, that is, a conversion between a function pointer with the 8968 // cmse_nonsecure_call attribute and one without. 8969 static bool IsInvalidCmseNSCallConversion(Sema &S, QualType FromType, 8970 QualType ToType) { 8971 if (const auto *ToFn = 8972 dyn_cast<FunctionType>(S.Context.getCanonicalType(ToType))) { 8973 if (const auto *FromFn = 8974 dyn_cast<FunctionType>(S.Context.getCanonicalType(FromType))) { 8975 FunctionType::ExtInfo ToEInfo = ToFn->getExtInfo(); 8976 FunctionType::ExtInfo FromEInfo = FromFn->getExtInfo(); 8977 8978 return ToEInfo.getCmseNSCall() != FromEInfo.getCmseNSCall(); 8979 } 8980 } 8981 return false; 8982 } 8983 8984 // checkPointerTypesForAssignment - This is a very tricky routine (despite 8985 // being closely modeled after the C99 spec:-). The odd characteristic of this 8986 // routine is it effectively iqnores the qualifiers on the top level pointee. 8987 // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. 8988 // FIXME: add a couple examples in this comment. 8989 static AssignConvertType checkPointerTypesForAssignment(Sema &S, 8990 QualType LHSType, 8991 QualType RHSType, 8992 SourceLocation Loc) { 8993 assert(LHSType.isCanonical() && "LHS not canonicalized!"); 8994 assert(RHSType.isCanonical() && "RHS not canonicalized!"); 8995 8996 // get the "pointed to" type (ignoring qualifiers at the top level) 8997 const Type *lhptee, *rhptee; 8998 Qualifiers lhq, rhq; 8999 std::tie(lhptee, lhq) = 9000 cast<PointerType>(LHSType)->getPointeeType().split().asPair(); 9001 std::tie(rhptee, rhq) = 9002 cast<PointerType>(RHSType)->getPointeeType().split().asPair(); 9003 9004 AssignConvertType ConvTy = AssignConvertType::Compatible; 9005 9006 // C99 6.5.16.1p1: This following citation is common to constraints 9007 // 3 & 4 (below). ...and the type *pointed to* by the left has all the 9008 // qualifiers of the type *pointed to* by the right; 9009 9010 // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. 9011 if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && 9012 lhq.compatiblyIncludesObjCLifetime(rhq)) { 9013 // Ignore lifetime for further calculation. 9014 lhq.removeObjCLifetime(); 9015 rhq.removeObjCLifetime(); 9016 } 9017 9018 if (!lhq.compatiblyIncludes(rhq, S.getASTContext())) { 9019 // Treat address-space mismatches as fatal. 9020 if (!lhq.isAddressSpaceSupersetOf(rhq, S.getASTContext())) 9021 return AssignConvertType::IncompatiblePointerDiscardsQualifiers; 9022 9023 // It's okay to add or remove GC or lifetime qualifiers when converting to 9024 // and from void*. 9025 else if (lhq.withoutObjCGCAttr().withoutObjCLifetime().compatiblyIncludes( 9026 rhq.withoutObjCGCAttr().withoutObjCLifetime(), 9027 S.getASTContext()) && 9028 (lhptee->isVoidType() || rhptee->isVoidType())) 9029 ; // keep old 9030 9031 // Treat lifetime mismatches as fatal. 9032 else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) 9033 ConvTy = AssignConvertType::IncompatiblePointerDiscardsQualifiers; 9034 9035 // Treat pointer-auth mismatches as fatal. 9036 else if (!lhq.getPointerAuth().isEquivalent(rhq.getPointerAuth())) 9037 ConvTy = AssignConvertType::IncompatiblePointerDiscardsQualifiers; 9038 9039 // For GCC/MS compatibility, other qualifier mismatches are treated 9040 // as still compatible in C. 9041 else 9042 ConvTy = AssignConvertType::CompatiblePointerDiscardsQualifiers; 9043 } 9044 9045 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or 9046 // incomplete type and the other is a pointer to a qualified or unqualified 9047 // version of void... 9048 if (lhptee->isVoidType()) { 9049 if (rhptee->isIncompleteOrObjectType()) 9050 return ConvTy; 9051 9052 // As an extension, we allow cast to/from void* to function pointer. 9053 assert(rhptee->isFunctionType()); 9054 return AssignConvertType::FunctionVoidPointer; 9055 } 9056 9057 if (rhptee->isVoidType()) { 9058 // In C, void * to another pointer type is compatible, but we want to note 9059 // that there will be an implicit conversion happening here. 9060 if (lhptee->isIncompleteOrObjectType()) 9061 return ConvTy == AssignConvertType::Compatible && 9062 !S.getLangOpts().CPlusPlus 9063 ? AssignConvertType::CompatibleVoidPtrToNonVoidPtr 9064 : ConvTy; 9065 9066 // As an extension, we allow cast to/from void* to function pointer. 9067 assert(lhptee->isFunctionType()); 9068 return AssignConvertType::FunctionVoidPointer; 9069 } 9070 9071 if (!S.Diags.isIgnored( 9072 diag::warn_typecheck_convert_incompatible_function_pointer_strict, 9073 Loc) && 9074 RHSType->isFunctionPointerType() && LHSType->isFunctionPointerType() && 9075 !S.TryFunctionConversion(RHSType, LHSType, RHSType)) 9076 return AssignConvertType::IncompatibleFunctionPointerStrict; 9077 9078 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or 9079 // unqualified versions of compatible types, ... 9080 QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); 9081 if (!S.Context.typesAreCompatible(ltrans, rtrans)) { 9082 // Check if the pointee types are compatible ignoring the sign. 9083 // We explicitly check for char so that we catch "char" vs 9084 // "unsigned char" on systems where "char" is unsigned. 9085 if (lhptee->isCharType()) 9086 ltrans = S.Context.UnsignedCharTy; 9087 else if (lhptee->hasSignedIntegerRepresentation()) 9088 ltrans = S.Context.getCorrespondingUnsignedType(ltrans); 9089 9090 if (rhptee->isCharType()) 9091 rtrans = S.Context.UnsignedCharTy; 9092 else if (rhptee->hasSignedIntegerRepresentation()) 9093 rtrans = S.Context.getCorrespondingUnsignedType(rtrans); 9094 9095 if (ltrans == rtrans) { 9096 // Types are compatible ignoring the sign. Qualifier incompatibility 9097 // takes priority over sign incompatibility because the sign 9098 // warning can be disabled. 9099 if (!S.IsAssignConvertCompatible(ConvTy)) 9100 return ConvTy; 9101 9102 return AssignConvertType::IncompatiblePointerSign; 9103 } 9104 9105 // If we are a multi-level pointer, it's possible that our issue is simply 9106 // one of qualification - e.g. char ** -> const char ** is not allowed. If 9107 // the eventual target type is the same and the pointers have the same 9108 // level of indirection, this must be the issue. 9109 if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { 9110 do { 9111 std::tie(lhptee, lhq) = 9112 cast<PointerType>(lhptee)->getPointeeType().split().asPair(); 9113 std::tie(rhptee, rhq) = 9114 cast<PointerType>(rhptee)->getPointeeType().split().asPair(); 9115 9116 // Inconsistent address spaces at this point is invalid, even if the 9117 // address spaces would be compatible. 9118 // FIXME: This doesn't catch address space mismatches for pointers of 9119 // different nesting levels, like: 9120 // __local int *** a; 9121 // int ** b = a; 9122 // It's not clear how to actually determine when such pointers are 9123 // invalidly incompatible. 9124 if (lhq.getAddressSpace() != rhq.getAddressSpace()) 9125 return AssignConvertType:: 9126 IncompatibleNestedPointerAddressSpaceMismatch; 9127 9128 } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); 9129 9130 if (lhptee == rhptee) 9131 return AssignConvertType::IncompatibleNestedPointerQualifiers; 9132 } 9133 9134 // General pointer incompatibility takes priority over qualifiers. 9135 if (RHSType->isFunctionPointerType() && LHSType->isFunctionPointerType()) 9136 return AssignConvertType::IncompatibleFunctionPointer; 9137 return AssignConvertType::IncompatiblePointer; 9138 } 9139 bool DiscardingCFIUncheckedCallee, AddingCFIUncheckedCallee; 9140 if (!S.getLangOpts().CPlusPlus && 9141 S.IsFunctionConversion(ltrans, rtrans, &DiscardingCFIUncheckedCallee, 9142 &AddingCFIUncheckedCallee)) { 9143 // Allow conversions between CFIUncheckedCallee-ness. 9144 if (!DiscardingCFIUncheckedCallee && !AddingCFIUncheckedCallee) 9145 return AssignConvertType::IncompatibleFunctionPointer; 9146 } 9147 if (IsInvalidCmseNSCallConversion(S, ltrans, rtrans)) 9148 return AssignConvertType::IncompatibleFunctionPointer; 9149 if (S.IsInvalidSMECallConversion(rtrans, ltrans)) 9150 return AssignConvertType::IncompatibleFunctionPointer; 9151 return ConvTy; 9152 } 9153 9154 /// checkBlockPointerTypesForAssignment - This routine determines whether two 9155 /// block pointer types are compatible or whether a block and normal pointer 9156 /// are compatible. It is more restrict than comparing two function pointer 9157 // types. 9158 static AssignConvertType checkBlockPointerTypesForAssignment(Sema &S, 9159 QualType LHSType, 9160 QualType RHSType) { 9161 assert(LHSType.isCanonical() && "LHS not canonicalized!"); 9162 assert(RHSType.isCanonical() && "RHS not canonicalized!"); 9163 9164 QualType lhptee, rhptee; 9165 9166 // get the "pointed to" type (ignoring qualifiers at the top level) 9167 lhptee = cast<BlockPointerType>(LHSType)->getPointeeType(); 9168 rhptee = cast<BlockPointerType>(RHSType)->getPointeeType(); 9169 9170 // In C++, the types have to match exactly. 9171 if (S.getLangOpts().CPlusPlus) 9172 return AssignConvertType::IncompatibleBlockPointer; 9173 9174 AssignConvertType ConvTy = AssignConvertType::Compatible; 9175 9176 // For blocks we enforce that qualifiers are identical. 9177 Qualifiers LQuals = lhptee.getLocalQualifiers(); 9178 Qualifiers RQuals = rhptee.getLocalQualifiers(); 9179 if (S.getLangOpts().OpenCL) { 9180 LQuals.removeAddressSpace(); 9181 RQuals.removeAddressSpace(); 9182 } 9183 if (LQuals != RQuals) 9184 ConvTy = AssignConvertType::CompatiblePointerDiscardsQualifiers; 9185 9186 // FIXME: OpenCL doesn't define the exact compile time semantics for a block 9187 // assignment. 9188 // The current behavior is similar to C++ lambdas. A block might be 9189 // assigned to a variable iff its return type and parameters are compatible 9190 // (C99 6.2.7) with the corresponding return type and parameters of the LHS of 9191 // an assignment. Presumably it should behave in way that a function pointer 9192 // assignment does in C, so for each parameter and return type: 9193 // * CVR and address space of LHS should be a superset of CVR and address 9194 // space of RHS. 9195 // * unqualified types should be compatible. 9196 if (S.getLangOpts().OpenCL) { 9197 if (!S.Context.typesAreBlockPointerCompatible( 9198 S.Context.getQualifiedType(LHSType.getUnqualifiedType(), LQuals), 9199 S.Context.getQualifiedType(RHSType.getUnqualifiedType(), RQuals))) 9200 return AssignConvertType::IncompatibleBlockPointer; 9201 } else if (!S.Context.typesAreBlockPointerCompatible(LHSType, RHSType)) 9202 return AssignConvertType::IncompatibleBlockPointer; 9203 9204 return ConvTy; 9205 } 9206 9207 /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types 9208 /// for assignment compatibility. 9209 static AssignConvertType checkObjCPointerTypesForAssignment(Sema &S, 9210 QualType LHSType, 9211 QualType RHSType) { 9212 assert(LHSType.isCanonical() && "LHS was not canonicalized!"); 9213 assert(RHSType.isCanonical() && "RHS was not canonicalized!"); 9214 9215 if (LHSType->isObjCBuiltinType()) { 9216 // Class is not compatible with ObjC object pointers. 9217 if (LHSType->isObjCClassType() && !RHSType->isObjCBuiltinType() && 9218 !RHSType->isObjCQualifiedClassType()) 9219 return AssignConvertType::IncompatiblePointer; 9220 return AssignConvertType::Compatible; 9221 } 9222 if (RHSType->isObjCBuiltinType()) { 9223 if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() && 9224 !LHSType->isObjCQualifiedClassType()) 9225 return AssignConvertType::IncompatiblePointer; 9226 return AssignConvertType::Compatible; 9227 } 9228 QualType lhptee = LHSType->castAs<ObjCObjectPointerType>()->getPointeeType(); 9229 QualType rhptee = RHSType->castAs<ObjCObjectPointerType>()->getPointeeType(); 9230 9231 if (!lhptee.isAtLeastAsQualifiedAs(rhptee, S.getASTContext()) && 9232 // make an exception for id<P> 9233 !LHSType->isObjCQualifiedIdType()) 9234 return AssignConvertType::CompatiblePointerDiscardsQualifiers; 9235 9236 if (S.Context.typesAreCompatible(LHSType, RHSType)) 9237 return AssignConvertType::Compatible; 9238 if (LHSType->isObjCQualifiedIdType() || RHSType->isObjCQualifiedIdType()) 9239 return AssignConvertType::IncompatibleObjCQualifiedId; 9240 return AssignConvertType::IncompatiblePointer; 9241 } 9242 9243 AssignConvertType Sema::CheckAssignmentConstraints(SourceLocation Loc, 9244 QualType LHSType, 9245 QualType RHSType) { 9246 // Fake up an opaque expression. We don't actually care about what 9247 // cast operations are required, so if CheckAssignmentConstraints 9248 // adds casts to this they'll be wasted, but fortunately that doesn't 9249 // usually happen on valid code. 9250 OpaqueValueExpr RHSExpr(Loc, RHSType, VK_PRValue); 9251 ExprResult RHSPtr = &RHSExpr; 9252 CastKind K; 9253 9254 return CheckAssignmentConstraints(LHSType, RHSPtr, K, /*ConvertRHS=*/false); 9255 } 9256 9257 /// This helper function returns true if QT is a vector type that has element 9258 /// type ElementType. 9259 static bool isVector(QualType QT, QualType ElementType) { 9260 if (const VectorType *VT = QT->getAs<VectorType>()) 9261 return VT->getElementType().getCanonicalType() == ElementType; 9262 return false; 9263 } 9264 9265 /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently 9266 /// has code to accommodate several GCC extensions when type checking 9267 /// pointers. Here are some objectionable examples that GCC considers warnings: 9268 /// 9269 /// int a, *pint; 9270 /// short *pshort; 9271 /// struct foo *pfoo; 9272 /// 9273 /// pint = pshort; // warning: assignment from incompatible pointer type 9274 /// a = pint; // warning: assignment makes integer from pointer without a cast 9275 /// pint = a; // warning: assignment makes pointer from integer without a cast 9276 /// pint = pfoo; // warning: assignment from incompatible pointer type 9277 /// 9278 /// As a result, the code for dealing with pointers is more complex than the 9279 /// C99 spec dictates. 9280 /// 9281 /// Sets 'Kind' for any result kind except Incompatible. 9282 AssignConvertType Sema::CheckAssignmentConstraints(QualType LHSType, 9283 ExprResult &RHS, 9284 CastKind &Kind, 9285 bool ConvertRHS) { 9286 QualType RHSType = RHS.get()->getType(); 9287 QualType OrigLHSType = LHSType; 9288 9289 // Get canonical types. We're not formatting these types, just comparing 9290 // them. 9291 LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType(); 9292 RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType(); 9293 9294 // Common case: no conversion required. 9295 if (LHSType == RHSType) { 9296 Kind = CK_NoOp; 9297 return AssignConvertType::Compatible; 9298 } 9299 9300 // If the LHS has an __auto_type, there are no additional type constraints 9301 // to be worried about. 9302 if (const auto *AT = dyn_cast<AutoType>(LHSType)) { 9303 if (AT->isGNUAutoType()) { 9304 Kind = CK_NoOp; 9305 return AssignConvertType::Compatible; 9306 } 9307 } 9308 9309 // If we have an atomic type, try a non-atomic assignment, then just add an 9310 // atomic qualification step. 9311 if (const AtomicType *AtomicTy = dyn_cast<AtomicType>(LHSType)) { 9312 AssignConvertType Result = 9313 CheckAssignmentConstraints(AtomicTy->getValueType(), RHS, Kind); 9314 if (!IsAssignConvertCompatible(Result)) 9315 return Result; 9316 if (Kind != CK_NoOp && ConvertRHS) 9317 RHS = ImpCastExprToType(RHS.get(), AtomicTy->getValueType(), Kind); 9318 Kind = CK_NonAtomicToAtomic; 9319 return Result; 9320 } 9321 9322 // If the left-hand side is a reference type, then we are in a 9323 // (rare!) case where we've allowed the use of references in C, 9324 // e.g., as a parameter type in a built-in function. In this case, 9325 // just make sure that the type referenced is compatible with the 9326 // right-hand side type. The caller is responsible for adjusting 9327 // LHSType so that the resulting expression does not have reference 9328 // type. 9329 if (const ReferenceType *LHSTypeRef = LHSType->getAs<ReferenceType>()) { 9330 if (Context.typesAreCompatible(LHSTypeRef->getPointeeType(), RHSType)) { 9331 Kind = CK_LValueBitCast; 9332 return AssignConvertType::Compatible; 9333 } 9334 return AssignConvertType::Incompatible; 9335 } 9336 9337 // Allow scalar to ExtVector assignments, and assignments of an ExtVector type 9338 // to the same ExtVector type. 9339 if (LHSType->isExtVectorType()) { 9340 if (RHSType->isExtVectorType()) 9341 return AssignConvertType::Incompatible; 9342 if (RHSType->isArithmeticType()) { 9343 // CK_VectorSplat does T -> vector T, so first cast to the element type. 9344 if (ConvertRHS) 9345 RHS = prepareVectorSplat(LHSType, RHS.get()); 9346 Kind = CK_VectorSplat; 9347 return AssignConvertType::Compatible; 9348 } 9349 } 9350 9351 // Conversions to or from vector type. 9352 if (LHSType->isVectorType() || RHSType->isVectorType()) { 9353 if (LHSType->isVectorType() && RHSType->isVectorType()) { 9354 // Allow assignments of an AltiVec vector type to an equivalent GCC 9355 // vector type and vice versa 9356 if (Context.areCompatibleVectorTypes(LHSType, RHSType)) { 9357 Kind = CK_BitCast; 9358 return AssignConvertType::Compatible; 9359 } 9360 9361 // If we are allowing lax vector conversions, and LHS and RHS are both 9362 // vectors, the total size only needs to be the same. This is a bitcast; 9363 // no bits are changed but the result type is different. 9364 if (isLaxVectorConversion(RHSType, LHSType)) { 9365 // The default for lax vector conversions with Altivec vectors will 9366 // change, so if we are converting between vector types where 9367 // at least one is an Altivec vector, emit a warning. 9368 if (Context.getTargetInfo().getTriple().isPPC() && 9369 anyAltivecTypes(RHSType, LHSType) && 9370 !Context.areCompatibleVectorTypes(RHSType, LHSType)) 9371 Diag(RHS.get()->getExprLoc(), diag::warn_deprecated_lax_vec_conv_all) 9372 << RHSType << LHSType; 9373 Kind = CK_BitCast; 9374 return AssignConvertType::IncompatibleVectors; 9375 } 9376 } 9377 9378 // When the RHS comes from another lax conversion (e.g. binops between 9379 // scalars and vectors) the result is canonicalized as a vector. When the 9380 // LHS is also a vector, the lax is allowed by the condition above. Handle 9381 // the case where LHS is a scalar. 9382 if (LHSType->isScalarType()) { 9383 const VectorType *VecType = RHSType->getAs<VectorType>(); 9384 if (VecType && VecType->getNumElements() == 1 && 9385 isLaxVectorConversion(RHSType, LHSType)) { 9386 if (Context.getTargetInfo().getTriple().isPPC() && 9387 (VecType->getVectorKind() == VectorKind::AltiVecVector || 9388 VecType->getVectorKind() == VectorKind::AltiVecBool || 9389 VecType->getVectorKind() == VectorKind::AltiVecPixel)) 9390 Diag(RHS.get()->getExprLoc(), diag::warn_deprecated_lax_vec_conv_all) 9391 << RHSType << LHSType; 9392 ExprResult *VecExpr = &RHS; 9393 *VecExpr = ImpCastExprToType(VecExpr->get(), LHSType, CK_BitCast); 9394 Kind = CK_BitCast; 9395 return AssignConvertType::Compatible; 9396 } 9397 } 9398 9399 // Allow assignments between fixed-length and sizeless SVE vectors. 9400 if ((LHSType->isSVESizelessBuiltinType() && RHSType->isVectorType()) || 9401 (LHSType->isVectorType() && RHSType->isSVESizelessBuiltinType())) 9402 if (ARM().areCompatibleSveTypes(LHSType, RHSType) || 9403 ARM().areLaxCompatibleSveTypes(LHSType, RHSType)) { 9404 Kind = CK_BitCast; 9405 return AssignConvertType::Compatible; 9406 } 9407 9408 // Allow assignments between fixed-length and sizeless RVV vectors. 9409 if ((LHSType->isRVVSizelessBuiltinType() && RHSType->isVectorType()) || 9410 (LHSType->isVectorType() && RHSType->isRVVSizelessBuiltinType())) { 9411 if (Context.areCompatibleRVVTypes(LHSType, RHSType) || 9412 Context.areLaxCompatibleRVVTypes(LHSType, RHSType)) { 9413 Kind = CK_BitCast; 9414 return AssignConvertType::Compatible; 9415 } 9416 } 9417 9418 return AssignConvertType::Incompatible; 9419 } 9420 9421 // Diagnose attempts to convert between __ibm128, __float128 and long double 9422 // where such conversions currently can't be handled. 9423 if (unsupportedTypeConversion(*this, LHSType, RHSType)) 9424 return AssignConvertType::Incompatible; 9425 9426 // Disallow assigning a _Complex to a real type in C++ mode since it simply 9427 // discards the imaginary part. 9428 if (getLangOpts().CPlusPlus && RHSType->getAs<ComplexType>() && 9429 !LHSType->getAs<ComplexType>()) 9430 return AssignConvertType::Incompatible; 9431 9432 // Arithmetic conversions. 9433 if (LHSType->isArithmeticType() && RHSType->isArithmeticType() && 9434 !(getLangOpts().CPlusPlus && LHSType->isEnumeralType())) { 9435 if (ConvertRHS) 9436 Kind = PrepareScalarCast(RHS, LHSType); 9437 return AssignConvertType::Compatible; 9438 } 9439 9440 // Conversions to normal pointers. 9441 if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) { 9442 // U* -> T* 9443 if (isa<PointerType>(RHSType)) { 9444 LangAS AddrSpaceL = LHSPointer->getPointeeType().getAddressSpace(); 9445 LangAS AddrSpaceR = RHSType->getPointeeType().getAddressSpace(); 9446 if (AddrSpaceL != AddrSpaceR) 9447 Kind = CK_AddressSpaceConversion; 9448 else if (Context.hasCvrSimilarType(RHSType, LHSType)) 9449 Kind = CK_NoOp; 9450 else 9451 Kind = CK_BitCast; 9452 return checkPointerTypesForAssignment(*this, LHSType, RHSType, 9453 RHS.get()->getBeginLoc()); 9454 } 9455 9456 // int -> T* 9457 if (RHSType->isIntegerType()) { 9458 Kind = CK_IntegralToPointer; // FIXME: null? 9459 return AssignConvertType::IntToPointer; 9460 } 9461 9462 // C pointers are not compatible with ObjC object pointers, 9463 // with two exceptions: 9464 if (isa<ObjCObjectPointerType>(RHSType)) { 9465 // - conversions to void* 9466 if (LHSPointer->getPointeeType()->isVoidType()) { 9467 Kind = CK_BitCast; 9468 return AssignConvertType::Compatible; 9469 } 9470 9471 // - conversions from 'Class' to the redefinition type 9472 if (RHSType->isObjCClassType() && 9473 Context.hasSameType(LHSType, 9474 Context.getObjCClassRedefinitionType())) { 9475 Kind = CK_BitCast; 9476 return AssignConvertType::Compatible; 9477 } 9478 9479 Kind = CK_BitCast; 9480 return AssignConvertType::IncompatiblePointer; 9481 } 9482 9483 // U^ -> void* 9484 if (RHSType->getAs<BlockPointerType>()) { 9485 if (LHSPointer->getPointeeType()->isVoidType()) { 9486 LangAS AddrSpaceL = LHSPointer->getPointeeType().getAddressSpace(); 9487 LangAS AddrSpaceR = RHSType->getAs<BlockPointerType>() 9488 ->getPointeeType() 9489 .getAddressSpace(); 9490 Kind = 9491 AddrSpaceL != AddrSpaceR ? CK_AddressSpaceConversion : CK_BitCast; 9492 return AssignConvertType::Compatible; 9493 } 9494 } 9495 9496 return AssignConvertType::Incompatible; 9497 } 9498 9499 // Conversions to block pointers. 9500 if (isa<BlockPointerType>(LHSType)) { 9501 // U^ -> T^ 9502 if (RHSType->isBlockPointerType()) { 9503 LangAS AddrSpaceL = LHSType->getAs<BlockPointerType>() 9504 ->getPointeeType() 9505 .getAddressSpace(); 9506 LangAS AddrSpaceR = RHSType->getAs<BlockPointerType>() 9507 ->getPointeeType() 9508 .getAddressSpace(); 9509 Kind = AddrSpaceL != AddrSpaceR ? CK_AddressSpaceConversion : CK_BitCast; 9510 return checkBlockPointerTypesForAssignment(*this, LHSType, RHSType); 9511 } 9512 9513 // int or null -> T^ 9514 if (RHSType->isIntegerType()) { 9515 Kind = CK_IntegralToPointer; // FIXME: null 9516 return AssignConvertType::IntToBlockPointer; 9517 } 9518 9519 // id -> T^ 9520 if (getLangOpts().ObjC && RHSType->isObjCIdType()) { 9521 Kind = CK_AnyPointerToBlockPointerCast; 9522 return AssignConvertType::Compatible; 9523 } 9524 9525 // void* -> T^ 9526 if (const PointerType *RHSPT = RHSType->getAs<PointerType>()) 9527 if (RHSPT->getPointeeType()->isVoidType()) { 9528 Kind = CK_AnyPointerToBlockPointerCast; 9529 return AssignConvertType::Compatible; 9530 } 9531 9532 return AssignConvertType::Incompatible; 9533 } 9534 9535 // Conversions to Objective-C pointers. 9536 if (isa<ObjCObjectPointerType>(LHSType)) { 9537 // A* -> B* 9538 if (RHSType->isObjCObjectPointerType()) { 9539 Kind = CK_BitCast; 9540 AssignConvertType result = 9541 checkObjCPointerTypesForAssignment(*this, LHSType, RHSType); 9542 if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && 9543 result == AssignConvertType::Compatible && 9544 !ObjC().CheckObjCARCUnavailableWeakConversion(OrigLHSType, RHSType)) 9545 result = AssignConvertType::IncompatibleObjCWeakRef; 9546 return result; 9547 } 9548 9549 // int or null -> A* 9550 if (RHSType->isIntegerType()) { 9551 Kind = CK_IntegralToPointer; // FIXME: null 9552 return AssignConvertType::IntToPointer; 9553 } 9554 9555 // In general, C pointers are not compatible with ObjC object pointers, 9556 // with two exceptions: 9557 if (isa<PointerType>(RHSType)) { 9558 Kind = CK_CPointerToObjCPointerCast; 9559 9560 // - conversions from 'void*' 9561 if (RHSType->isVoidPointerType()) { 9562 return AssignConvertType::Compatible; 9563 } 9564 9565 // - conversions to 'Class' from its redefinition type 9566 if (LHSType->isObjCClassType() && 9567 Context.hasSameType(RHSType, 9568 Context.getObjCClassRedefinitionType())) { 9569 return AssignConvertType::Compatible; 9570 } 9571 9572 return AssignConvertType::IncompatiblePointer; 9573 } 9574 9575 // Only under strict condition T^ is compatible with an Objective-C pointer. 9576 if (RHSType->isBlockPointerType() && 9577 LHSType->isBlockCompatibleObjCPointerType(Context)) { 9578 if (ConvertRHS) 9579 maybeExtendBlockObject(RHS); 9580 Kind = CK_BlockPointerToObjCPointerCast; 9581 return AssignConvertType::Compatible; 9582 } 9583 9584 return AssignConvertType::Incompatible; 9585 } 9586 9587 // Conversion to nullptr_t (C23 only) 9588 if (getLangOpts().C23 && LHSType->isNullPtrType() && 9589 RHS.get()->isNullPointerConstant(Context, 9590 Expr::NPC_ValueDependentIsNull)) { 9591 // null -> nullptr_t 9592 Kind = CK_NullToPointer; 9593 return AssignConvertType::Compatible; 9594 } 9595 9596 // Conversions from pointers that are not covered by the above. 9597 if (isa<PointerType>(RHSType)) { 9598 // T* -> _Bool 9599 if (LHSType == Context.BoolTy) { 9600 Kind = CK_PointerToBoolean; 9601 return AssignConvertType::Compatible; 9602 } 9603 9604 // T* -> int 9605 if (LHSType->isIntegerType()) { 9606 Kind = CK_PointerToIntegral; 9607 return AssignConvertType::PointerToInt; 9608 } 9609 9610 return AssignConvertType::Incompatible; 9611 } 9612 9613 // Conversions from Objective-C pointers that are not covered by the above. 9614 if (isa<ObjCObjectPointerType>(RHSType)) { 9615 // T* -> _Bool 9616 if (LHSType == Context.BoolTy) { 9617 Kind = CK_PointerToBoolean; 9618 return AssignConvertType::Compatible; 9619 } 9620 9621 // T* -> int 9622 if (LHSType->isIntegerType()) { 9623 Kind = CK_PointerToIntegral; 9624 return AssignConvertType::PointerToInt; 9625 } 9626 9627 return AssignConvertType::Incompatible; 9628 } 9629 9630 // struct A -> struct B 9631 if (isa<TagType>(LHSType) && isa<TagType>(RHSType)) { 9632 if (Context.typesAreCompatible(LHSType, RHSType)) { 9633 Kind = CK_NoOp; 9634 return AssignConvertType::Compatible; 9635 } 9636 } 9637 9638 if (LHSType->isSamplerT() && RHSType->isIntegerType()) { 9639 Kind = CK_IntToOCLSampler; 9640 return AssignConvertType::Compatible; 9641 } 9642 9643 return AssignConvertType::Incompatible; 9644 } 9645 9646 /// Constructs a transparent union from an expression that is 9647 /// used to initialize the transparent union. 9648 static void ConstructTransparentUnion(Sema &S, ASTContext &C, 9649 ExprResult &EResult, QualType UnionType, 9650 FieldDecl *Field) { 9651 // Build an initializer list that designates the appropriate member 9652 // of the transparent union. 9653 Expr *E = EResult.get(); 9654 InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), 9655 E, SourceLocation()); 9656 Initializer->setType(UnionType); 9657 Initializer->setInitializedFieldInUnion(Field); 9658 9659 // Build a compound literal constructing a value of the transparent 9660 // union type from this initializer list. 9661 TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); 9662 EResult = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, 9663 VK_PRValue, Initializer, false); 9664 } 9665 9666 AssignConvertType 9667 Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, 9668 ExprResult &RHS) { 9669 QualType RHSType = RHS.get()->getType(); 9670 9671 // If the ArgType is a Union type, we want to handle a potential 9672 // transparent_union GCC extension. 9673 const RecordType *UT = ArgType->getAsUnionType(); 9674 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) 9675 return AssignConvertType::Incompatible; 9676 9677 // The field to initialize within the transparent union. 9678 RecordDecl *UD = UT->getDecl(); 9679 FieldDecl *InitField = nullptr; 9680 // It's compatible if the expression matches any of the fields. 9681 for (auto *it : UD->fields()) { 9682 if (it->getType()->isPointerType()) { 9683 // If the transparent union contains a pointer type, we allow: 9684 // 1) void pointer 9685 // 2) null pointer constant 9686 if (RHSType->isPointerType()) 9687 if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { 9688 RHS = ImpCastExprToType(RHS.get(), it->getType(), CK_BitCast); 9689 InitField = it; 9690 break; 9691 } 9692 9693 if (RHS.get()->isNullPointerConstant(Context, 9694 Expr::NPC_ValueDependentIsNull)) { 9695 RHS = ImpCastExprToType(RHS.get(), it->getType(), 9696 CK_NullToPointer); 9697 InitField = it; 9698 break; 9699 } 9700 } 9701 9702 CastKind Kind; 9703 if (CheckAssignmentConstraints(it->getType(), RHS, Kind) == 9704 AssignConvertType::Compatible) { 9705 RHS = ImpCastExprToType(RHS.get(), it->getType(), Kind); 9706 InitField = it; 9707 break; 9708 } 9709 } 9710 9711 if (!InitField) 9712 return AssignConvertType::Incompatible; 9713 9714 ConstructTransparentUnion(*this, Context, RHS, ArgType, InitField); 9715 return AssignConvertType::Compatible; 9716 } 9717 9718 AssignConvertType Sema::CheckSingleAssignmentConstraints(QualType LHSType, 9719 ExprResult &CallerRHS, 9720 bool Diagnose, 9721 bool DiagnoseCFAudited, 9722 bool ConvertRHS) { 9723 // We need to be able to tell the caller whether we diagnosed a problem, if 9724 // they ask us to issue diagnostics. 9725 assert((ConvertRHS || !Diagnose) && "can't indicate whether we diagnosed"); 9726 9727 // If ConvertRHS is false, we want to leave the caller's RHS untouched. Sadly, 9728 // we can't avoid *all* modifications at the moment, so we need some somewhere 9729 // to put the updated value. 9730 ExprResult LocalRHS = CallerRHS; 9731 ExprResult &RHS = ConvertRHS ? CallerRHS : LocalRHS; 9732 9733 if (const auto *LHSPtrType = LHSType->getAs<PointerType>()) { 9734 if (const auto *RHSPtrType = RHS.get()->getType()->getAs<PointerType>()) { 9735 if (RHSPtrType->getPointeeType()->hasAttr(attr::NoDeref) && 9736 !LHSPtrType->getPointeeType()->hasAttr(attr::NoDeref)) { 9737 Diag(RHS.get()->getExprLoc(), 9738 diag::warn_noderef_to_dereferenceable_pointer) 9739 << RHS.get()->getSourceRange(); 9740 } 9741 } 9742 } 9743 9744 if (getLangOpts().CPlusPlus) { 9745 if (!LHSType->isRecordType() && !LHSType->isAtomicType()) { 9746 // C++ 5.17p3: If the left operand is not of class type, the 9747 // expression is implicitly converted (C++ 4) to the 9748 // cv-unqualified type of the left operand. 9749 QualType RHSType = RHS.get()->getType(); 9750 if (Diagnose) { 9751 RHS = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), 9752 AssignmentAction::Assigning); 9753 } else { 9754 ImplicitConversionSequence ICS = 9755 TryImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), 9756 /*SuppressUserConversions=*/false, 9757 AllowedExplicit::None, 9758 /*InOverloadResolution=*/false, 9759 /*CStyle=*/false, 9760 /*AllowObjCWritebackConversion=*/false); 9761 if (ICS.isFailure()) 9762 return AssignConvertType::Incompatible; 9763 RHS = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), 9764 ICS, AssignmentAction::Assigning); 9765 } 9766 if (RHS.isInvalid()) 9767 return AssignConvertType::Incompatible; 9768 AssignConvertType result = AssignConvertType::Compatible; 9769 if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && 9770 !ObjC().CheckObjCARCUnavailableWeakConversion(LHSType, RHSType)) 9771 result = AssignConvertType::IncompatibleObjCWeakRef; 9772 return result; 9773 } 9774 9775 // FIXME: Currently, we fall through and treat C++ classes like C 9776 // structures. 9777 // FIXME: We also fall through for atomics; not sure what should 9778 // happen there, though. 9779 } else if (RHS.get()->getType() == Context.OverloadTy) { 9780 // As a set of extensions to C, we support overloading on functions. These 9781 // functions need to be resolved here. 9782 DeclAccessPair DAP; 9783 if (FunctionDecl *FD = ResolveAddressOfOverloadedFunction( 9784 RHS.get(), LHSType, /*Complain=*/false, DAP)) 9785 RHS = FixOverloadedFunctionReference(RHS.get(), DAP, FD); 9786 else 9787 return AssignConvertType::Incompatible; 9788 } 9789 9790 // This check seems unnatural, however it is necessary to ensure the proper 9791 // conversion of functions/arrays. If the conversion were done for all 9792 // DeclExpr's (created by ActOnIdExpression), it would mess up the unary 9793 // expressions that suppress this implicit conversion (&, sizeof). This needs 9794 // to happen before we check for null pointer conversions because C does not 9795 // undergo the same implicit conversions as C++ does above (by the calls to 9796 // TryImplicitConversion() and PerformImplicitConversion()) which insert the 9797 // lvalue to rvalue cast before checking for null pointer constraints. This 9798 // addresses code like: nullptr_t val; int *ptr; ptr = val; 9799 // 9800 // Suppress this for references: C++ 8.5.3p5. 9801 if (!LHSType->isReferenceType()) { 9802 // FIXME: We potentially allocate here even if ConvertRHS is false. 9803 RHS = DefaultFunctionArrayLvalueConversion(RHS.get(), Diagnose); 9804 if (RHS.isInvalid()) 9805 return AssignConvertType::Incompatible; 9806 } 9807 9808 // The constraints are expressed in terms of the atomic, qualified, or 9809 // unqualified type of the LHS. 9810 QualType LHSTypeAfterConversion = LHSType.getAtomicUnqualifiedType(); 9811 9812 // C99 6.5.16.1p1: the left operand is a pointer and the right is 9813 // a null pointer constant <C23>or its type is nullptr_t;</C23>. 9814 if ((LHSTypeAfterConversion->isPointerType() || 9815 LHSTypeAfterConversion->isObjCObjectPointerType() || 9816 LHSTypeAfterConversion->isBlockPointerType()) && 9817 ((getLangOpts().C23 && RHS.get()->getType()->isNullPtrType()) || 9818 RHS.get()->isNullPointerConstant(Context, 9819 Expr::NPC_ValueDependentIsNull))) { 9820 AssignConvertType Ret = AssignConvertType::Compatible; 9821 if (Diagnose || ConvertRHS) { 9822 CastKind Kind; 9823 CXXCastPath Path; 9824 CheckPointerConversion(RHS.get(), LHSType, Kind, Path, 9825 /*IgnoreBaseAccess=*/false, Diagnose); 9826 9827 // If there is a conversion of some kind, check to see what kind of 9828 // pointer conversion happened so we can diagnose a C++ compatibility 9829 // diagnostic if the conversion is invalid. This only matters if the RHS 9830 // is some kind of void pointer. We have a carve-out when the RHS is from 9831 // a macro expansion because the use of a macro may indicate different 9832 // code between C and C++. Consider: char *s = NULL; where NULL is 9833 // defined as (void *)0 in C (which would be invalid in C++), but 0 in 9834 // C++, which is valid in C++. 9835 if (Kind != CK_NoOp && !getLangOpts().CPlusPlus && 9836 !RHS.get()->getBeginLoc().isMacroID()) { 9837 QualType CanRHS = 9838 RHS.get()->getType().getCanonicalType().getUnqualifiedType(); 9839 QualType CanLHS = LHSType.getCanonicalType().getUnqualifiedType(); 9840 if (CanRHS->isVoidPointerType() && CanLHS->isPointerType()) { 9841 Ret = checkPointerTypesForAssignment(*this, CanLHS, CanRHS, 9842 RHS.get()->getExprLoc()); 9843 // Anything that's not considered perfectly compatible would be 9844 // incompatible in C++. 9845 if (Ret != AssignConvertType::Compatible) 9846 Ret = AssignConvertType::CompatibleVoidPtrToNonVoidPtr; 9847 } 9848 } 9849 9850 if (ConvertRHS) 9851 RHS = ImpCastExprToType(RHS.get(), LHSType, Kind, VK_PRValue, &Path); 9852 } 9853 return Ret; 9854 } 9855 // C23 6.5.16.1p1: the left operand has type atomic, qualified, or 9856 // unqualified bool, and the right operand is a pointer or its type is 9857 // nullptr_t. 9858 if (getLangOpts().C23 && LHSType->isBooleanType() && 9859 RHS.get()->getType()->isNullPtrType()) { 9860 // NB: T* -> _Bool is handled in CheckAssignmentConstraints, this only 9861 // only handles nullptr -> _Bool due to needing an extra conversion 9862 // step. 9863 // We model this by converting from nullptr -> void * and then let the 9864 // conversion from void * -> _Bool happen naturally. 9865 if (Diagnose || ConvertRHS) { 9866 CastKind Kind; 9867 CXXCastPath Path; 9868 CheckPointerConversion(RHS.get(), Context.VoidPtrTy, Kind, Path, 9869 /*IgnoreBaseAccess=*/false, Diagnose); 9870 if (ConvertRHS) 9871 RHS = ImpCastExprToType(RHS.get(), Context.VoidPtrTy, Kind, VK_PRValue, 9872 &Path); 9873 } 9874 } 9875 9876 // OpenCL queue_t type assignment. 9877 if (LHSType->isQueueT() && RHS.get()->isNullPointerConstant( 9878 Context, Expr::NPC_ValueDependentIsNull)) { 9879 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); 9880 return AssignConvertType::Compatible; 9881 } 9882 9883 CastKind Kind; 9884 AssignConvertType result = 9885 CheckAssignmentConstraints(LHSType, RHS, Kind, ConvertRHS); 9886 9887 // C99 6.5.16.1p2: The value of the right operand is converted to the 9888 // type of the assignment expression. 9889 // CheckAssignmentConstraints allows the left-hand side to be a reference, 9890 // so that we can use references in built-in functions even in C. 9891 // The getNonReferenceType() call makes sure that the resulting expression 9892 // does not have reference type. 9893 if (result != AssignConvertType::Incompatible && 9894 RHS.get()->getType() != LHSType) { 9895 QualType Ty = LHSType.getNonLValueExprType(Context); 9896 Expr *E = RHS.get(); 9897 9898 // Check for various Objective-C errors. If we are not reporting 9899 // diagnostics and just checking for errors, e.g., during overload 9900 // resolution, return Incompatible to indicate the failure. 9901 if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() && 9902 ObjC().CheckObjCConversion(SourceRange(), Ty, E, 9903 CheckedConversionKind::Implicit, Diagnose, 9904 DiagnoseCFAudited) != SemaObjC::ACR_okay) { 9905 if (!Diagnose) 9906 return AssignConvertType::Incompatible; 9907 } 9908 if (getLangOpts().ObjC && 9909 (ObjC().CheckObjCBridgeRelatedConversions(E->getBeginLoc(), LHSType, 9910 E->getType(), E, Diagnose) || 9911 ObjC().CheckConversionToObjCLiteral(LHSType, E, Diagnose))) { 9912 if (!Diagnose) 9913 return AssignConvertType::Incompatible; 9914 // Replace the expression with a corrected version and continue so we 9915 // can find further errors. 9916 RHS = E; 9917 return AssignConvertType::Compatible; 9918 } 9919 9920 if (ConvertRHS) 9921 RHS = ImpCastExprToType(E, Ty, Kind); 9922 } 9923 9924 return result; 9925 } 9926 9927 namespace { 9928 /// The original operand to an operator, prior to the application of the usual 9929 /// arithmetic conversions and converting the arguments of a builtin operator 9930 /// candidate. 9931 struct OriginalOperand { 9932 explicit OriginalOperand(Expr *Op) : Orig(Op), Conversion(nullptr) { 9933 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Op)) 9934 Op = MTE->getSubExpr(); 9935 if (auto *BTE = dyn_cast<CXXBindTemporaryExpr>(Op)) 9936 Op = BTE->getSubExpr(); 9937 if (auto *ICE = dyn_cast<ImplicitCastExpr>(Op)) { 9938 Orig = ICE->getSubExprAsWritten(); 9939 Conversion = ICE->getConversionFunction(); 9940 } 9941 } 9942 9943 QualType getType() const { return Orig->getType(); } 9944 9945 Expr *Orig; 9946 NamedDecl *Conversion; 9947 }; 9948 } 9949 9950 QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, 9951 ExprResult &RHS) { 9952 OriginalOperand OrigLHS(LHS.get()), OrigRHS(RHS.get()); 9953 9954 Diag(Loc, diag::err_typecheck_invalid_operands) 9955 << OrigLHS.getType() << OrigRHS.getType() 9956 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 9957 9958 // If a user-defined conversion was applied to either of the operands prior 9959 // to applying the built-in operator rules, tell the user about it. 9960 if (OrigLHS.Conversion) { 9961 Diag(OrigLHS.Conversion->getLocation(), 9962 diag::note_typecheck_invalid_operands_converted) 9963 << 0 << LHS.get()->getType(); 9964 } 9965 if (OrigRHS.Conversion) { 9966 Diag(OrigRHS.Conversion->getLocation(), 9967 diag::note_typecheck_invalid_operands_converted) 9968 << 1 << RHS.get()->getType(); 9969 } 9970 9971 return QualType(); 9972 } 9973 9974 QualType Sema::InvalidLogicalVectorOperands(SourceLocation Loc, ExprResult &LHS, 9975 ExprResult &RHS) { 9976 QualType LHSType = LHS.get()->IgnoreImpCasts()->getType(); 9977 QualType RHSType = RHS.get()->IgnoreImpCasts()->getType(); 9978 9979 bool LHSNatVec = LHSType->isVectorType(); 9980 bool RHSNatVec = RHSType->isVectorType(); 9981 9982 if (!(LHSNatVec && RHSNatVec)) { 9983 Expr *Vector = LHSNatVec ? LHS.get() : RHS.get(); 9984 Expr *NonVector = !LHSNatVec ? LHS.get() : RHS.get(); 9985 Diag(Loc, diag::err_typecheck_logical_vector_expr_gnu_cpp_restrict) 9986 << 0 << Vector->getType() << NonVector->IgnoreImpCasts()->getType() 9987 << Vector->getSourceRange(); 9988 return QualType(); 9989 } 9990 9991 Diag(Loc, diag::err_typecheck_logical_vector_expr_gnu_cpp_restrict) 9992 << 1 << LHSType << RHSType << LHS.get()->getSourceRange() 9993 << RHS.get()->getSourceRange(); 9994 9995 return QualType(); 9996 } 9997 9998 /// Try to convert a value of non-vector type to a vector type by converting 9999 /// the type to the element type of the vector and then performing a splat. 10000 /// If the language is OpenCL, we only use conversions that promote scalar 10001 /// rank; for C, Obj-C, and C++ we allow any real scalar conversion except 10002 /// for float->int. 10003 /// 10004 /// OpenCL V2.0 6.2.6.p2: 10005 /// An error shall occur if any scalar operand type has greater rank 10006 /// than the type of the vector element. 10007 /// 10008 /// \param scalar - if non-null, actually perform the conversions 10009 /// \return true if the operation fails (but without diagnosing the failure) 10010 static bool tryVectorConvertAndSplat(Sema &S, ExprResult *scalar, 10011 QualType scalarTy, 10012 QualType vectorEltTy, 10013 QualType vectorTy, 10014 unsigned &DiagID) { 10015 // The conversion to apply to the scalar before splatting it, 10016 // if necessary. 10017 CastKind scalarCast = CK_NoOp; 10018 10019 if (vectorEltTy->isBooleanType() && scalarTy->isIntegralType(S.Context)) { 10020 scalarCast = CK_IntegralToBoolean; 10021 } else if (vectorEltTy->isIntegralType(S.Context)) { 10022 if (S.getLangOpts().OpenCL && (scalarTy->isRealFloatingType() || 10023 (scalarTy->isIntegerType() && 10024 S.Context.getIntegerTypeOrder(vectorEltTy, scalarTy) < 0))) { 10025 DiagID = diag::err_opencl_scalar_type_rank_greater_than_vector_type; 10026 return true; 10027 } 10028 if (!scalarTy->isIntegralType(S.Context)) 10029 return true; 10030 scalarCast = CK_IntegralCast; 10031 } else if (vectorEltTy->isRealFloatingType()) { 10032 if (scalarTy->isRealFloatingType()) { 10033 if (S.getLangOpts().OpenCL && 10034 S.Context.getFloatingTypeOrder(vectorEltTy, scalarTy) < 0) { 10035 DiagID = diag::err_opencl_scalar_type_rank_greater_than_vector_type; 10036 return true; 10037 } 10038 scalarCast = CK_FloatingCast; 10039 } 10040 else if (scalarTy->isIntegralType(S.Context)) 10041 scalarCast = CK_IntegralToFloating; 10042 else 10043 return true; 10044 } else { 10045 return true; 10046 } 10047 10048 // Adjust scalar if desired. 10049 if (scalar) { 10050 if (scalarCast != CK_NoOp) 10051 *scalar = S.ImpCastExprToType(scalar->get(), vectorEltTy, scalarCast); 10052 *scalar = S.ImpCastExprToType(scalar->get(), vectorTy, CK_VectorSplat); 10053 } 10054 return false; 10055 } 10056 10057 /// Convert vector E to a vector with the same number of elements but different 10058 /// element type. 10059 static ExprResult convertVector(Expr *E, QualType ElementType, Sema &S) { 10060 const auto *VecTy = E->getType()->getAs<VectorType>(); 10061 assert(VecTy && "Expression E must be a vector"); 10062 QualType NewVecTy = 10063 VecTy->isExtVectorType() 10064 ? S.Context.getExtVectorType(ElementType, VecTy->getNumElements()) 10065 : S.Context.getVectorType(ElementType, VecTy->getNumElements(), 10066 VecTy->getVectorKind()); 10067 10068 // Look through the implicit cast. Return the subexpression if its type is 10069 // NewVecTy. 10070 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 10071 if (ICE->getSubExpr()->getType() == NewVecTy) 10072 return ICE->getSubExpr(); 10073 10074 auto Cast = ElementType->isIntegerType() ? CK_IntegralCast : CK_FloatingCast; 10075 return S.ImpCastExprToType(E, NewVecTy, Cast); 10076 } 10077 10078 /// Test if a (constant) integer Int can be casted to another integer type 10079 /// IntTy without losing precision. 10080 static bool canConvertIntToOtherIntTy(Sema &S, ExprResult *Int, 10081 QualType OtherIntTy) { 10082 if (Int->get()->containsErrors()) 10083 return false; 10084 10085 QualType IntTy = Int->get()->getType().getUnqualifiedType(); 10086 10087 // Reject cases where the value of the Int is unknown as that would 10088 // possibly cause truncation, but accept cases where the scalar can be 10089 // demoted without loss of precision. 10090 Expr::EvalResult EVResult; 10091 bool CstInt = Int->get()->EvaluateAsInt(EVResult, S.Context); 10092 int Order = S.Context.getIntegerTypeOrder(OtherIntTy, IntTy); 10093 bool IntSigned = IntTy->hasSignedIntegerRepresentation(); 10094 bool OtherIntSigned = OtherIntTy->hasSignedIntegerRepresentation(); 10095 10096 if (CstInt) { 10097 // If the scalar is constant and is of a higher order and has more active 10098 // bits that the vector element type, reject it. 10099 llvm::APSInt Result = EVResult.Val.getInt(); 10100 unsigned NumBits = IntSigned 10101 ? (Result.isNegative() ? Result.getSignificantBits() 10102 : Result.getActiveBits()) 10103 : Result.getActiveBits(); 10104 if (Order < 0 && S.Context.getIntWidth(OtherIntTy) < NumBits) 10105 return true; 10106 10107 // If the signedness of the scalar type and the vector element type 10108 // differs and the number of bits is greater than that of the vector 10109 // element reject it. 10110 return (IntSigned != OtherIntSigned && 10111 NumBits > S.Context.getIntWidth(OtherIntTy)); 10112 } 10113 10114 // Reject cases where the value of the scalar is not constant and it's 10115 // order is greater than that of the vector element type. 10116 return (Order < 0); 10117 } 10118 10119 /// Test if a (constant) integer Int can be casted to floating point type 10120 /// FloatTy without losing precision. 10121 static bool canConvertIntTyToFloatTy(Sema &S, ExprResult *Int, 10122 QualType FloatTy) { 10123 if (Int->get()->containsErrors()) 10124 return false; 10125 10126 QualType IntTy = Int->get()->getType().getUnqualifiedType(); 10127 10128 // Determine if the integer constant can be expressed as a floating point 10129 // number of the appropriate type. 10130 Expr::EvalResult EVResult; 10131 bool CstInt = Int->get()->EvaluateAsInt(EVResult, S.Context); 10132 10133 uint64_t Bits = 0; 10134 if (CstInt) { 10135 // Reject constants that would be truncated if they were converted to 10136 // the floating point type. Test by simple to/from conversion. 10137 // FIXME: Ideally the conversion to an APFloat and from an APFloat 10138 // could be avoided if there was a convertFromAPInt method 10139 // which could signal back if implicit truncation occurred. 10140 llvm::APSInt Result = EVResult.Val.getInt(); 10141 llvm::APFloat Float(S.Context.getFloatTypeSemantics(FloatTy)); 10142 Float.convertFromAPInt(Result, IntTy->hasSignedIntegerRepresentation(), 10143 llvm::APFloat::rmTowardZero); 10144 llvm::APSInt ConvertBack(S.Context.getIntWidth(IntTy), 10145 !IntTy->hasSignedIntegerRepresentation()); 10146 bool Ignored = false; 10147 Float.convertToInteger(ConvertBack, llvm::APFloat::rmNearestTiesToEven, 10148 &Ignored); 10149 if (Result != ConvertBack) 10150 return true; 10151 } else { 10152 // Reject types that cannot be fully encoded into the mantissa of 10153 // the float. 10154 Bits = S.Context.getTypeSize(IntTy); 10155 unsigned FloatPrec = llvm::APFloat::semanticsPrecision( 10156 S.Context.getFloatTypeSemantics(FloatTy)); 10157 if (Bits > FloatPrec) 10158 return true; 10159 } 10160 10161 return false; 10162 } 10163 10164 /// Attempt to convert and splat Scalar into a vector whose types matches 10165 /// Vector following GCC conversion rules. The rule is that implicit 10166 /// conversion can occur when Scalar can be casted to match Vector's element 10167 /// type without causing truncation of Scalar. 10168 static bool tryGCCVectorConvertAndSplat(Sema &S, ExprResult *Scalar, 10169 ExprResult *Vector) { 10170 QualType ScalarTy = Scalar->get()->getType().getUnqualifiedType(); 10171 QualType VectorTy = Vector->get()->getType().getUnqualifiedType(); 10172 QualType VectorEltTy; 10173 10174 if (const auto *VT = VectorTy->getAs<VectorType>()) { 10175 assert(!isa<ExtVectorType>(VT) && 10176 "ExtVectorTypes should not be handled here!"); 10177 VectorEltTy = VT->getElementType(); 10178 } else if (VectorTy->isSveVLSBuiltinType()) { 10179 VectorEltTy = 10180 VectorTy->castAs<BuiltinType>()->getSveEltType(S.getASTContext()); 10181 } else { 10182 llvm_unreachable("Only Fixed-Length and SVE Vector types are handled here"); 10183 } 10184 10185 // Reject cases where the vector element type or the scalar element type are 10186 // not integral or floating point types. 10187 if (!VectorEltTy->isArithmeticType() || !ScalarTy->isArithmeticType()) 10188 return true; 10189 10190 // The conversion to apply to the scalar before splatting it, 10191 // if necessary. 10192 CastKind ScalarCast = CK_NoOp; 10193 10194 // Accept cases where the vector elements are integers and the scalar is 10195 // an integer. 10196 // FIXME: Notionally if the scalar was a floating point value with a precise 10197 // integral representation, we could cast it to an appropriate integer 10198 // type and then perform the rest of the checks here. GCC will perform 10199 // this conversion in some cases as determined by the input language. 10200 // We should accept it on a language independent basis. 10201 if (VectorEltTy->isIntegralType(S.Context) && 10202 ScalarTy->isIntegralType(S.Context) && 10203 S.Context.getIntegerTypeOrder(VectorEltTy, ScalarTy)) { 10204 10205 if (canConvertIntToOtherIntTy(S, Scalar, VectorEltTy)) 10206 return true; 10207 10208 ScalarCast = CK_IntegralCast; 10209 } else if (VectorEltTy->isIntegralType(S.Context) && 10210 ScalarTy->isRealFloatingType()) { 10211 if (S.Context.getTypeSize(VectorEltTy) == S.Context.getTypeSize(ScalarTy)) 10212 ScalarCast = CK_FloatingToIntegral; 10213 else 10214 return true; 10215 } else if (VectorEltTy->isRealFloatingType()) { 10216 if (ScalarTy->isRealFloatingType()) { 10217 10218 // Reject cases where the scalar type is not a constant and has a higher 10219 // Order than the vector element type. 10220 llvm::APFloat Result(0.0); 10221 10222 // Determine whether this is a constant scalar. In the event that the 10223 // value is dependent (and thus cannot be evaluated by the constant 10224 // evaluator), skip the evaluation. This will then diagnose once the 10225 // expression is instantiated. 10226 bool CstScalar = Scalar->get()->isValueDependent() || 10227 Scalar->get()->EvaluateAsFloat(Result, S.Context); 10228 int Order = S.Context.getFloatingTypeOrder(VectorEltTy, ScalarTy); 10229 if (!CstScalar && Order < 0) 10230 return true; 10231 10232 // If the scalar cannot be safely casted to the vector element type, 10233 // reject it. 10234 if (CstScalar) { 10235 bool Truncated = false; 10236 Result.convert(S.Context.getFloatTypeSemantics(VectorEltTy), 10237 llvm::APFloat::rmNearestTiesToEven, &Truncated); 10238 if (Truncated) 10239 return true; 10240 } 10241 10242 ScalarCast = CK_FloatingCast; 10243 } else if (ScalarTy->isIntegralType(S.Context)) { 10244 if (canConvertIntTyToFloatTy(S, Scalar, VectorEltTy)) 10245 return true; 10246 10247 ScalarCast = CK_IntegralToFloating; 10248 } else 10249 return true; 10250 } else if (ScalarTy->isEnumeralType()) 10251 return true; 10252 10253 // Adjust scalar if desired. 10254 if (ScalarCast != CK_NoOp) 10255 *Scalar = S.ImpCastExprToType(Scalar->get(), VectorEltTy, ScalarCast); 10256 *Scalar = S.ImpCastExprToType(Scalar->get(), VectorTy, CK_VectorSplat); 10257 return false; 10258 } 10259 10260 QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, 10261 SourceLocation Loc, bool IsCompAssign, 10262 bool AllowBothBool, 10263 bool AllowBoolConversions, 10264 bool AllowBoolOperation, 10265 bool ReportInvalid) { 10266 if (!IsCompAssign) { 10267 LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); 10268 if (LHS.isInvalid()) 10269 return QualType(); 10270 } 10271 RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); 10272 if (RHS.isInvalid()) 10273 return QualType(); 10274 10275 // For conversion purposes, we ignore any qualifiers. 10276 // For example, "const float" and "float" are equivalent. 10277 QualType LHSType = LHS.get()->getType().getUnqualifiedType(); 10278 QualType RHSType = RHS.get()->getType().getUnqualifiedType(); 10279 10280 const VectorType *LHSVecType = LHSType->getAs<VectorType>(); 10281 const VectorType *RHSVecType = RHSType->getAs<VectorType>(); 10282 assert(LHSVecType || RHSVecType); 10283 10284 if (getLangOpts().HLSL) 10285 return HLSL().handleVectorBinOpConversion(LHS, RHS, LHSType, RHSType, 10286 IsCompAssign); 10287 10288 // Any operation with MFloat8 type is only possible with C intrinsics 10289 if ((LHSVecType && LHSVecType->getElementType()->isMFloat8Type()) || 10290 (RHSVecType && RHSVecType->getElementType()->isMFloat8Type())) 10291 return InvalidOperands(Loc, LHS, RHS); 10292 10293 // AltiVec-style "vector bool op vector bool" combinations are allowed 10294 // for some operators but not others. 10295 if (!AllowBothBool && LHSVecType && 10296 LHSVecType->getVectorKind() == VectorKind::AltiVecBool && RHSVecType && 10297 RHSVecType->getVectorKind() == VectorKind::AltiVecBool) 10298 return ReportInvalid ? InvalidOperands(Loc, LHS, RHS) : QualType(); 10299 10300 // This operation may not be performed on boolean vectors. 10301 if (!AllowBoolOperation && 10302 (LHSType->isExtVectorBoolType() || RHSType->isExtVectorBoolType())) 10303 return ReportInvalid ? InvalidOperands(Loc, LHS, RHS) : QualType(); 10304 10305 // If the vector types are identical, return. 10306 if (Context.hasSameType(LHSType, RHSType)) 10307 return Context.getCommonSugaredType(LHSType, RHSType); 10308 10309 // If we have compatible AltiVec and GCC vector types, use the AltiVec type. 10310 if (LHSVecType && RHSVecType && 10311 Context.areCompatibleVectorTypes(LHSType, RHSType)) { 10312 if (isa<ExtVectorType>(LHSVecType)) { 10313 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); 10314 return LHSType; 10315 } 10316 10317 if (!IsCompAssign) 10318 LHS = ImpCastExprToType(LHS.get(), RHSType, CK_BitCast); 10319 return RHSType; 10320 } 10321 10322 // AllowBoolConversions says that bool and non-bool AltiVec vectors 10323 // can be mixed, with the result being the non-bool type. The non-bool 10324 // operand must have integer element type. 10325 if (AllowBoolConversions && LHSVecType && RHSVecType && 10326 LHSVecType->getNumElements() == RHSVecType->getNumElements() && 10327 (Context.getTypeSize(LHSVecType->getElementType()) == 10328 Context.getTypeSize(RHSVecType->getElementType()))) { 10329 if (LHSVecType->getVectorKind() == VectorKind::AltiVecVector && 10330 LHSVecType->getElementType()->isIntegerType() && 10331 RHSVecType->getVectorKind() == VectorKind::AltiVecBool) { 10332 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); 10333 return LHSType; 10334 } 10335 if (!IsCompAssign && 10336 LHSVecType->getVectorKind() == VectorKind::AltiVecBool && 10337 RHSVecType->getVectorKind() == VectorKind::AltiVecVector && 10338 RHSVecType->getElementType()->isIntegerType()) { 10339 LHS = ImpCastExprToType(LHS.get(), RHSType, CK_BitCast); 10340 return RHSType; 10341 } 10342 } 10343 10344 // Expressions containing fixed-length and sizeless SVE/RVV vectors are 10345 // invalid since the ambiguity can affect the ABI. 10346 auto IsSveRVVConversion = [](QualType FirstType, QualType SecondType, 10347 unsigned &SVEorRVV) { 10348 const VectorType *VecType = SecondType->getAs<VectorType>(); 10349 SVEorRVV = 0; 10350 if (FirstType->isSizelessBuiltinType() && VecType) { 10351 if (VecType->getVectorKind() == VectorKind::SveFixedLengthData || 10352 VecType->getVectorKind() == VectorKind::SveFixedLengthPredicate) 10353 return true; 10354 if (VecType->getVectorKind() == VectorKind::RVVFixedLengthData || 10355 VecType->getVectorKind() == VectorKind::RVVFixedLengthMask || 10356 VecType->getVectorKind() == VectorKind::RVVFixedLengthMask_1 || 10357 VecType->getVectorKind() == VectorKind::RVVFixedLengthMask_2 || 10358 VecType->getVectorKind() == VectorKind::RVVFixedLengthMask_4) { 10359 SVEorRVV = 1; 10360 return true; 10361 } 10362 } 10363 10364 return false; 10365 }; 10366 10367 unsigned SVEorRVV; 10368 if (IsSveRVVConversion(LHSType, RHSType, SVEorRVV) || 10369 IsSveRVVConversion(RHSType, LHSType, SVEorRVV)) { 10370 Diag(Loc, diag::err_typecheck_sve_rvv_ambiguous) 10371 << SVEorRVV << LHSType << RHSType; 10372 return QualType(); 10373 } 10374 10375 // Expressions containing GNU and SVE or RVV (fixed or sizeless) vectors are 10376 // invalid since the ambiguity can affect the ABI. 10377 auto IsSveRVVGnuConversion = [](QualType FirstType, QualType SecondType, 10378 unsigned &SVEorRVV) { 10379 const VectorType *FirstVecType = FirstType->getAs<VectorType>(); 10380 const VectorType *SecondVecType = SecondType->getAs<VectorType>(); 10381 10382 SVEorRVV = 0; 10383 if (FirstVecType && SecondVecType) { 10384 if (FirstVecType->getVectorKind() == VectorKind::Generic) { 10385 if (SecondVecType->getVectorKind() == VectorKind::SveFixedLengthData || 10386 SecondVecType->getVectorKind() == 10387 VectorKind::SveFixedLengthPredicate) 10388 return true; 10389 if (SecondVecType->getVectorKind() == VectorKind::RVVFixedLengthData || 10390 SecondVecType->getVectorKind() == VectorKind::RVVFixedLengthMask || 10391 SecondVecType->getVectorKind() == 10392 VectorKind::RVVFixedLengthMask_1 || 10393 SecondVecType->getVectorKind() == 10394 VectorKind::RVVFixedLengthMask_2 || 10395 SecondVecType->getVectorKind() == 10396 VectorKind::RVVFixedLengthMask_4) { 10397 SVEorRVV = 1; 10398 return true; 10399 } 10400 } 10401 return false; 10402 } 10403 10404 if (SecondVecType && 10405 SecondVecType->getVectorKind() == VectorKind::Generic) { 10406 if (FirstType->isSVESizelessBuiltinType()) 10407 return true; 10408 if (FirstType->isRVVSizelessBuiltinType()) { 10409 SVEorRVV = 1; 10410 return true; 10411 } 10412 } 10413 10414 return false; 10415 }; 10416 10417 if (IsSveRVVGnuConversion(LHSType, RHSType, SVEorRVV) || 10418 IsSveRVVGnuConversion(RHSType, LHSType, SVEorRVV)) { 10419 Diag(Loc, diag::err_typecheck_sve_rvv_gnu_ambiguous) 10420 << SVEorRVV << LHSType << RHSType; 10421 return QualType(); 10422 } 10423 10424 // If there's a vector type and a scalar, try to convert the scalar to 10425 // the vector element type and splat. 10426 unsigned DiagID = diag::err_typecheck_vector_not_convertable; 10427 if (!RHSVecType) { 10428 if (isa<ExtVectorType>(LHSVecType)) { 10429 if (!tryVectorConvertAndSplat(*this, &RHS, RHSType, 10430 LHSVecType->getElementType(), LHSType, 10431 DiagID)) 10432 return LHSType; 10433 } else { 10434 if (!tryGCCVectorConvertAndSplat(*this, &RHS, &LHS)) 10435 return LHSType; 10436 } 10437 } 10438 if (!LHSVecType) { 10439 if (isa<ExtVectorType>(RHSVecType)) { 10440 if (!tryVectorConvertAndSplat(*this, (IsCompAssign ? nullptr : &LHS), 10441 LHSType, RHSVecType->getElementType(), 10442 RHSType, DiagID)) 10443 return RHSType; 10444 } else { 10445 if (LHS.get()->isLValue() || 10446 !tryGCCVectorConvertAndSplat(*this, &LHS, &RHS)) 10447 return RHSType; 10448 } 10449 } 10450 10451 // FIXME: The code below also handles conversion between vectors and 10452 // non-scalars, we should break this down into fine grained specific checks 10453 // and emit proper diagnostics. 10454 QualType VecType = LHSVecType ? LHSType : RHSType; 10455 const VectorType *VT = LHSVecType ? LHSVecType : RHSVecType; 10456 QualType OtherType = LHSVecType ? RHSType : LHSType; 10457 ExprResult *OtherExpr = LHSVecType ? &RHS : &LHS; 10458 if (isLaxVectorConversion(OtherType, VecType)) { 10459 if (Context.getTargetInfo().getTriple().isPPC() && 10460 anyAltivecTypes(RHSType, LHSType) && 10461 !Context.areCompatibleVectorTypes(RHSType, LHSType)) 10462 Diag(Loc, diag::warn_deprecated_lax_vec_conv_all) << RHSType << LHSType; 10463 // If we're allowing lax vector conversions, only the total (data) size 10464 // needs to be the same. For non compound assignment, if one of the types is 10465 // scalar, the result is always the vector type. 10466 if (!IsCompAssign) { 10467 *OtherExpr = ImpCastExprToType(OtherExpr->get(), VecType, CK_BitCast); 10468 return VecType; 10469 // In a compound assignment, lhs += rhs, 'lhs' is a lvalue src, forbidding 10470 // any implicit cast. Here, the 'rhs' should be implicit casted to 'lhs' 10471 // type. Note that this is already done by non-compound assignments in 10472 // CheckAssignmentConstraints. If it's a scalar type, only bitcast for 10473 // <1 x T> -> T. The result is also a vector type. 10474 } else if (OtherType->isExtVectorType() || OtherType->isVectorType() || 10475 (OtherType->isScalarType() && VT->getNumElements() == 1)) { 10476 ExprResult *RHSExpr = &RHS; 10477 *RHSExpr = ImpCastExprToType(RHSExpr->get(), LHSType, CK_BitCast); 10478 return VecType; 10479 } 10480 } 10481 10482 // Okay, the expression is invalid. 10483 10484 // If there's a non-vector, non-real operand, diagnose that. 10485 if ((!RHSVecType && !RHSType->isRealType()) || 10486 (!LHSVecType && !LHSType->isRealType())) { 10487 Diag(Loc, diag::err_typecheck_vector_not_convertable_non_scalar) 10488 << LHSType << RHSType 10489 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 10490 return QualType(); 10491 } 10492 10493 // OpenCL V1.1 6.2.6.p1: 10494 // If the operands are of more than one vector type, then an error shall 10495 // occur. Implicit conversions between vector types are not permitted, per 10496 // section 6.2.1. 10497 if (getLangOpts().OpenCL && 10498 RHSVecType && isa<ExtVectorType>(RHSVecType) && 10499 LHSVecType && isa<ExtVectorType>(LHSVecType)) { 10500 Diag(Loc, diag::err_opencl_implicit_vector_conversion) << LHSType 10501 << RHSType; 10502 return QualType(); 10503 } 10504 10505 10506 // If there is a vector type that is not a ExtVector and a scalar, we reach 10507 // this point if scalar could not be converted to the vector's element type 10508 // without truncation. 10509 if ((RHSVecType && !isa<ExtVectorType>(RHSVecType)) || 10510 (LHSVecType && !isa<ExtVectorType>(LHSVecType))) { 10511 QualType Scalar = LHSVecType ? RHSType : LHSType; 10512 QualType Vector = LHSVecType ? LHSType : RHSType; 10513 unsigned ScalarOrVector = LHSVecType && RHSVecType ? 1 : 0; 10514 Diag(Loc, 10515 diag::err_typecheck_vector_not_convertable_implict_truncation) 10516 << ScalarOrVector << Scalar << Vector; 10517 10518 return QualType(); 10519 } 10520 10521 // Otherwise, use the generic diagnostic. 10522 Diag(Loc, DiagID) 10523 << LHSType << RHSType 10524 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 10525 return QualType(); 10526 } 10527 10528 QualType Sema::CheckSizelessVectorOperands(ExprResult &LHS, ExprResult &RHS, 10529 SourceLocation Loc, 10530 bool IsCompAssign, 10531 ArithConvKind OperationKind) { 10532 if (!IsCompAssign) { 10533 LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); 10534 if (LHS.isInvalid()) 10535 return QualType(); 10536 } 10537 RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); 10538 if (RHS.isInvalid()) 10539 return QualType(); 10540 10541 QualType LHSType = LHS.get()->getType().getUnqualifiedType(); 10542 QualType RHSType = RHS.get()->getType().getUnqualifiedType(); 10543 10544 const BuiltinType *LHSBuiltinTy = LHSType->getAs<BuiltinType>(); 10545 const BuiltinType *RHSBuiltinTy = RHSType->getAs<BuiltinType>(); 10546 10547 unsigned DiagID = diag::err_typecheck_invalid_operands; 10548 if ((OperationKind == ArithConvKind::Arithmetic) && 10549 ((LHSBuiltinTy && LHSBuiltinTy->isSVEBool()) || 10550 (RHSBuiltinTy && RHSBuiltinTy->isSVEBool()))) { 10551 Diag(Loc, DiagID) << LHSType << RHSType << LHS.get()->getSourceRange() 10552 << RHS.get()->getSourceRange(); 10553 return QualType(); 10554 } 10555 10556 if (Context.hasSameType(LHSType, RHSType)) 10557 return LHSType; 10558 10559 if (LHSType->isSveVLSBuiltinType() && !RHSType->isSveVLSBuiltinType()) { 10560 if (!tryGCCVectorConvertAndSplat(*this, &RHS, &LHS)) 10561 return LHSType; 10562 } 10563 if (RHSType->isSveVLSBuiltinType() && !LHSType->isSveVLSBuiltinType()) { 10564 if (LHS.get()->isLValue() || 10565 !tryGCCVectorConvertAndSplat(*this, &LHS, &RHS)) 10566 return RHSType; 10567 } 10568 10569 if ((!LHSType->isSveVLSBuiltinType() && !LHSType->isRealType()) || 10570 (!RHSType->isSveVLSBuiltinType() && !RHSType->isRealType())) { 10571 Diag(Loc, diag::err_typecheck_vector_not_convertable_non_scalar) 10572 << LHSType << RHSType << LHS.get()->getSourceRange() 10573 << RHS.get()->getSourceRange(); 10574 return QualType(); 10575 } 10576 10577 if (LHSType->isSveVLSBuiltinType() && RHSType->isSveVLSBuiltinType() && 10578 Context.getBuiltinVectorTypeInfo(LHSBuiltinTy).EC != 10579 Context.getBuiltinVectorTypeInfo(RHSBuiltinTy).EC) { 10580 Diag(Loc, diag::err_typecheck_vector_lengths_not_equal) 10581 << LHSType << RHSType << LHS.get()->getSourceRange() 10582 << RHS.get()->getSourceRange(); 10583 return QualType(); 10584 } 10585 10586 if (LHSType->isSveVLSBuiltinType() || RHSType->isSveVLSBuiltinType()) { 10587 QualType Scalar = LHSType->isSveVLSBuiltinType() ? RHSType : LHSType; 10588 QualType Vector = LHSType->isSveVLSBuiltinType() ? LHSType : RHSType; 10589 bool ScalarOrVector = 10590 LHSType->isSveVLSBuiltinType() && RHSType->isSveVLSBuiltinType(); 10591 10592 Diag(Loc, diag::err_typecheck_vector_not_convertable_implict_truncation) 10593 << ScalarOrVector << Scalar << Vector; 10594 10595 return QualType(); 10596 } 10597 10598 Diag(Loc, DiagID) << LHSType << RHSType << LHS.get()->getSourceRange() 10599 << RHS.get()->getSourceRange(); 10600 return QualType(); 10601 } 10602 10603 // checkArithmeticNull - Detect when a NULL constant is used improperly in an 10604 // expression. These are mainly cases where the null pointer is used as an 10605 // integer instead of a pointer. 10606 static void checkArithmeticNull(Sema &S, ExprResult &LHS, ExprResult &RHS, 10607 SourceLocation Loc, bool IsCompare) { 10608 // The canonical way to check for a GNU null is with isNullPointerConstant, 10609 // but we use a bit of a hack here for speed; this is a relatively 10610 // hot path, and isNullPointerConstant is slow. 10611 bool LHSNull = isa<GNUNullExpr>(LHS.get()->IgnoreParenImpCasts()); 10612 bool RHSNull = isa<GNUNullExpr>(RHS.get()->IgnoreParenImpCasts()); 10613 10614 QualType NonNullType = LHSNull ? RHS.get()->getType() : LHS.get()->getType(); 10615 10616 // Avoid analyzing cases where the result will either be invalid (and 10617 // diagnosed as such) or entirely valid and not something to warn about. 10618 if ((!LHSNull && !RHSNull) || NonNullType->isBlockPointerType() || 10619 NonNullType->isMemberPointerType() || NonNullType->isFunctionType()) 10620 return; 10621 10622 // Comparison operations would not make sense with a null pointer no matter 10623 // what the other expression is. 10624 if (!IsCompare) { 10625 S.Diag(Loc, diag::warn_null_in_arithmetic_operation) 10626 << (LHSNull ? LHS.get()->getSourceRange() : SourceRange()) 10627 << (RHSNull ? RHS.get()->getSourceRange() : SourceRange()); 10628 return; 10629 } 10630 10631 // The rest of the operations only make sense with a null pointer 10632 // if the other expression is a pointer. 10633 if (LHSNull == RHSNull || NonNullType->isAnyPointerType() || 10634 NonNullType->canDecayToPointerType()) 10635 return; 10636 10637 S.Diag(Loc, diag::warn_null_in_comparison_operation) 10638 << LHSNull /* LHS is NULL */ << NonNullType 10639 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 10640 } 10641 10642 static void DetectPrecisionLossInComplexDivision(Sema &S, QualType DivisorTy, 10643 SourceLocation OpLoc) { 10644 // If the divisor is real, then this is real/real or complex/real division. 10645 // Either way there can be no precision loss. 10646 auto *CT = DivisorTy->getAs<ComplexType>(); 10647 if (!CT) 10648 return; 10649 10650 QualType ElementType = CT->getElementType(); 10651 bool IsComplexRangePromoted = S.getLangOpts().getComplexRange() == 10652 LangOptions::ComplexRangeKind::CX_Promoted; 10653 if (!ElementType->isFloatingType() || !IsComplexRangePromoted) 10654 return; 10655 10656 ASTContext &Ctx = S.getASTContext(); 10657 QualType HigherElementType = Ctx.GetHigherPrecisionFPType(ElementType); 10658 const llvm::fltSemantics &ElementTypeSemantics = 10659 Ctx.getFloatTypeSemantics(ElementType); 10660 const llvm::fltSemantics &HigherElementTypeSemantics = 10661 Ctx.getFloatTypeSemantics(HigherElementType); 10662 10663 if ((llvm::APFloat::semanticsMaxExponent(ElementTypeSemantics) * 2 + 1 > 10664 llvm::APFloat::semanticsMaxExponent(HigherElementTypeSemantics)) || 10665 (HigherElementType == Ctx.LongDoubleTy && 10666 !Ctx.getTargetInfo().hasLongDoubleType())) { 10667 // Retain the location of the first use of higher precision type. 10668 if (!S.LocationOfExcessPrecisionNotSatisfied.isValid()) 10669 S.LocationOfExcessPrecisionNotSatisfied = OpLoc; 10670 for (auto &[Type, Num] : S.ExcessPrecisionNotSatisfied) { 10671 if (Type == HigherElementType) { 10672 Num++; 10673 return; 10674 } 10675 } 10676 S.ExcessPrecisionNotSatisfied.push_back(std::make_pair( 10677 HigherElementType, S.ExcessPrecisionNotSatisfied.size())); 10678 } 10679 } 10680 10681 static void DiagnoseDivisionSizeofPointerOrArray(Sema &S, Expr *LHS, Expr *RHS, 10682 SourceLocation Loc) { 10683 const auto *LUE = dyn_cast<UnaryExprOrTypeTraitExpr>(LHS); 10684 const auto *RUE = dyn_cast<UnaryExprOrTypeTraitExpr>(RHS); 10685 if (!LUE || !RUE) 10686 return; 10687 if (LUE->getKind() != UETT_SizeOf || LUE->isArgumentType() || 10688 RUE->getKind() != UETT_SizeOf) 10689 return; 10690 10691 const Expr *LHSArg = LUE->getArgumentExpr()->IgnoreParens(); 10692 QualType LHSTy = LHSArg->getType(); 10693 QualType RHSTy; 10694 10695 if (RUE->isArgumentType()) 10696 RHSTy = RUE->getArgumentType().getNonReferenceType(); 10697 else 10698 RHSTy = RUE->getArgumentExpr()->IgnoreParens()->getType(); 10699 10700 if (LHSTy->isPointerType() && !RHSTy->isPointerType()) { 10701 if (!S.Context.hasSameUnqualifiedType(LHSTy->getPointeeType(), RHSTy)) 10702 return; 10703 10704 S.Diag(Loc, diag::warn_division_sizeof_ptr) << LHS << LHS->getSourceRange(); 10705 if (const auto *DRE = dyn_cast<DeclRefExpr>(LHSArg)) { 10706 if (const ValueDecl *LHSArgDecl = DRE->getDecl()) 10707 S.Diag(LHSArgDecl->getLocation(), diag::note_pointer_declared_here) 10708 << LHSArgDecl; 10709 } 10710 } else if (const auto *ArrayTy = S.Context.getAsArrayType(LHSTy)) { 10711 QualType ArrayElemTy = ArrayTy->getElementType(); 10712 if (ArrayElemTy != S.Context.getBaseElementType(ArrayTy) || 10713 ArrayElemTy->isDependentType() || RHSTy->isDependentType() || 10714 RHSTy->isReferenceType() || ArrayElemTy->isCharType() || 10715 S.Context.getTypeSize(ArrayElemTy) == S.Context.getTypeSize(RHSTy)) 10716 return; 10717 S.Diag(Loc, diag::warn_division_sizeof_array) 10718 << LHSArg->getSourceRange() << ArrayElemTy << RHSTy; 10719 if (const auto *DRE = dyn_cast<DeclRefExpr>(LHSArg)) { 10720 if (const ValueDecl *LHSArgDecl = DRE->getDecl()) 10721 S.Diag(LHSArgDecl->getLocation(), diag::note_array_declared_here) 10722 << LHSArgDecl; 10723 } 10724 10725 S.Diag(Loc, diag::note_precedence_silence) << RHS; 10726 } 10727 } 10728 10729 static void DiagnoseBadDivideOrRemainderValues(Sema& S, ExprResult &LHS, 10730 ExprResult &RHS, 10731 SourceLocation Loc, bool IsDiv) { 10732 // Check for division/remainder by zero. 10733 Expr::EvalResult RHSValue; 10734 if (!RHS.get()->isValueDependent() && 10735 RHS.get()->EvaluateAsInt(RHSValue, S.Context) && 10736 RHSValue.Val.getInt() == 0) 10737 S.DiagRuntimeBehavior(Loc, RHS.get(), 10738 S.PDiag(diag::warn_remainder_division_by_zero) 10739 << IsDiv << RHS.get()->getSourceRange()); 10740 } 10741 10742 QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS, 10743 SourceLocation Loc, 10744 bool IsCompAssign, bool IsDiv) { 10745 checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); 10746 10747 QualType LHSTy = LHS.get()->getType(); 10748 QualType RHSTy = RHS.get()->getType(); 10749 if (LHSTy->isVectorType() || RHSTy->isVectorType()) 10750 return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign, 10751 /*AllowBothBool*/ getLangOpts().AltiVec, 10752 /*AllowBoolConversions*/ false, 10753 /*AllowBooleanOperation*/ false, 10754 /*ReportInvalid*/ true); 10755 if (LHSTy->isSveVLSBuiltinType() || RHSTy->isSveVLSBuiltinType()) 10756 return CheckSizelessVectorOperands(LHS, RHS, Loc, IsCompAssign, 10757 ArithConvKind::Arithmetic); 10758 if (!IsDiv && 10759 (LHSTy->isConstantMatrixType() || RHSTy->isConstantMatrixType())) 10760 return CheckMatrixMultiplyOperands(LHS, RHS, Loc, IsCompAssign); 10761 // For division, only matrix-by-scalar is supported. Other combinations with 10762 // matrix types are invalid. 10763 if (IsDiv && LHSTy->isConstantMatrixType() && RHSTy->isArithmeticType()) 10764 return CheckMatrixElementwiseOperands(LHS, RHS, Loc, IsCompAssign); 10765 10766 QualType compType = UsualArithmeticConversions( 10767 LHS, RHS, Loc, 10768 IsCompAssign ? ArithConvKind::CompAssign : ArithConvKind::Arithmetic); 10769 if (LHS.isInvalid() || RHS.isInvalid()) 10770 return QualType(); 10771 10772 10773 if (compType.isNull() || !compType->isArithmeticType()) 10774 return InvalidOperands(Loc, LHS, RHS); 10775 if (IsDiv) { 10776 DetectPrecisionLossInComplexDivision(*this, RHS.get()->getType(), Loc); 10777 DiagnoseBadDivideOrRemainderValues(*this, LHS, RHS, Loc, IsDiv); 10778 DiagnoseDivisionSizeofPointerOrArray(*this, LHS.get(), RHS.get(), Loc); 10779 } 10780 return compType; 10781 } 10782 10783 QualType Sema::CheckRemainderOperands( 10784 ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { 10785 checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); 10786 10787 // Note: This check is here to simplify the double exclusions of 10788 // scalar and vector HLSL checks. No getLangOpts().HLSL 10789 // is needed since all languages exlcude doubles. 10790 if (LHS.get()->getType()->isDoubleType() || 10791 RHS.get()->getType()->isDoubleType() || 10792 (LHS.get()->getType()->isVectorType() && LHS.get() 10793 ->getType() 10794 ->getAs<VectorType>() 10795 ->getElementType() 10796 ->isDoubleType()) || 10797 (RHS.get()->getType()->isVectorType() && RHS.get() 10798 ->getType() 10799 ->getAs<VectorType>() 10800 ->getElementType() 10801 ->isDoubleType())) 10802 return InvalidOperands(Loc, LHS, RHS); 10803 10804 if (LHS.get()->getType()->isVectorType() || 10805 RHS.get()->getType()->isVectorType()) { 10806 if ((LHS.get()->getType()->hasIntegerRepresentation() && 10807 RHS.get()->getType()->hasIntegerRepresentation()) || 10808 (getLangOpts().HLSL && 10809 (LHS.get()->getType()->hasFloatingRepresentation() || 10810 RHS.get()->getType()->hasFloatingRepresentation()))) 10811 return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign, 10812 /*AllowBothBool*/ getLangOpts().AltiVec, 10813 /*AllowBoolConversions*/ false, 10814 /*AllowBooleanOperation*/ false, 10815 /*ReportInvalid*/ true); 10816 return InvalidOperands(Loc, LHS, RHS); 10817 } 10818 10819 if (LHS.get()->getType()->isSveVLSBuiltinType() || 10820 RHS.get()->getType()->isSveVLSBuiltinType()) { 10821 if (LHS.get()->getType()->hasIntegerRepresentation() && 10822 RHS.get()->getType()->hasIntegerRepresentation()) 10823 return CheckSizelessVectorOperands(LHS, RHS, Loc, IsCompAssign, 10824 ArithConvKind::Arithmetic); 10825 10826 return InvalidOperands(Loc, LHS, RHS); 10827 } 10828 10829 QualType compType = UsualArithmeticConversions( 10830 LHS, RHS, Loc, 10831 IsCompAssign ? ArithConvKind::CompAssign : ArithConvKind::Arithmetic); 10832 if (LHS.isInvalid() || RHS.isInvalid()) 10833 return QualType(); 10834 10835 if (compType.isNull() || 10836 (!compType->isIntegerType() && 10837 !(getLangOpts().HLSL && compType->isFloatingType()))) 10838 return InvalidOperands(Loc, LHS, RHS); 10839 DiagnoseBadDivideOrRemainderValues(*this, LHS, RHS, Loc, false /* IsDiv */); 10840 return compType; 10841 } 10842 10843 /// Diagnose invalid arithmetic on two void pointers. 10844 static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, 10845 Expr *LHSExpr, Expr *RHSExpr) { 10846 S.Diag(Loc, S.getLangOpts().CPlusPlus 10847 ? diag::err_typecheck_pointer_arith_void_type 10848 : diag::ext_gnu_void_ptr) 10849 << 1 /* two pointers */ << LHSExpr->getSourceRange() 10850 << RHSExpr->getSourceRange(); 10851 } 10852 10853 /// Diagnose invalid arithmetic on a void pointer. 10854 static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, 10855 Expr *Pointer) { 10856 S.Diag(Loc, S.getLangOpts().CPlusPlus 10857 ? diag::err_typecheck_pointer_arith_void_type 10858 : diag::ext_gnu_void_ptr) 10859 << 0 /* one pointer */ << Pointer->getSourceRange(); 10860 } 10861 10862 /// Diagnose invalid arithmetic on a null pointer. 10863 /// 10864 /// If \p IsGNUIdiom is true, the operation is using the 'p = (i8*)nullptr + n' 10865 /// idiom, which we recognize as a GNU extension. 10866 /// 10867 static void diagnoseArithmeticOnNullPointer(Sema &S, SourceLocation Loc, 10868 Expr *Pointer, bool IsGNUIdiom) { 10869 if (IsGNUIdiom) 10870 S.Diag(Loc, diag::warn_gnu_null_ptr_arith) 10871 << Pointer->getSourceRange(); 10872 else 10873 S.Diag(Loc, diag::warn_pointer_arith_null_ptr) 10874 << S.getLangOpts().CPlusPlus << Pointer->getSourceRange(); 10875 } 10876 10877 /// Diagnose invalid subraction on a null pointer. 10878 /// 10879 static void diagnoseSubtractionOnNullPointer(Sema &S, SourceLocation Loc, 10880 Expr *Pointer, bool BothNull) { 10881 // Null - null is valid in C++ [expr.add]p7 10882 if (BothNull && S.getLangOpts().CPlusPlus) 10883 return; 10884 10885 // Is this s a macro from a system header? 10886 if (S.Diags.getSuppressSystemWarnings() && S.SourceMgr.isInSystemMacro(Loc)) 10887 return; 10888 10889 S.DiagRuntimeBehavior(Loc, Pointer, 10890 S.PDiag(diag::warn_pointer_sub_null_ptr) 10891 << S.getLangOpts().CPlusPlus 10892 << Pointer->getSourceRange()); 10893 } 10894 10895 /// Diagnose invalid arithmetic on two function pointers. 10896 static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, 10897 Expr *LHS, Expr *RHS) { 10898 assert(LHS->getType()->isAnyPointerType()); 10899 assert(RHS->getType()->isAnyPointerType()); 10900 S.Diag(Loc, S.getLangOpts().CPlusPlus 10901 ? diag::err_typecheck_pointer_arith_function_type 10902 : diag::ext_gnu_ptr_func_arith) 10903 << 1 /* two pointers */ << LHS->getType()->getPointeeType() 10904 // We only show the second type if it differs from the first. 10905 << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), 10906 RHS->getType()) 10907 << RHS->getType()->getPointeeType() 10908 << LHS->getSourceRange() << RHS->getSourceRange(); 10909 } 10910 10911 /// Diagnose invalid arithmetic on a function pointer. 10912 static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, 10913 Expr *Pointer) { 10914 assert(Pointer->getType()->isAnyPointerType()); 10915 S.Diag(Loc, S.getLangOpts().CPlusPlus 10916 ? diag::err_typecheck_pointer_arith_function_type 10917 : diag::ext_gnu_ptr_func_arith) 10918 << 0 /* one pointer */ << Pointer->getType()->getPointeeType() 10919 << 0 /* one pointer, so only one type */ 10920 << Pointer->getSourceRange(); 10921 } 10922 10923 /// Emit error if Operand is incomplete pointer type 10924 /// 10925 /// \returns True if pointer has incomplete type 10926 static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, 10927 Expr *Operand) { 10928 QualType ResType = Operand->getType(); 10929 if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) 10930 ResType = ResAtomicType->getValueType(); 10931 10932 assert(ResType->isAnyPointerType()); 10933 QualType PointeeTy = ResType->getPointeeType(); 10934 return S.RequireCompleteSizedType( 10935 Loc, PointeeTy, 10936 diag::err_typecheck_arithmetic_incomplete_or_sizeless_type, 10937 Operand->getSourceRange()); 10938 } 10939 10940 /// Check the validity of an arithmetic pointer operand. 10941 /// 10942 /// If the operand has pointer type, this code will check for pointer types 10943 /// which are invalid in arithmetic operations. These will be diagnosed 10944 /// appropriately, including whether or not the use is supported as an 10945 /// extension. 10946 /// 10947 /// \returns True when the operand is valid to use (even if as an extension). 10948 static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, 10949 Expr *Operand) { 10950 QualType ResType = Operand->getType(); 10951 if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) 10952 ResType = ResAtomicType->getValueType(); 10953 10954 if (!ResType->isAnyPointerType()) return true; 10955 10956 QualType PointeeTy = ResType->getPointeeType(); 10957 if (PointeeTy->isVoidType()) { 10958 diagnoseArithmeticOnVoidPointer(S, Loc, Operand); 10959 return !S.getLangOpts().CPlusPlus; 10960 } 10961 if (PointeeTy->isFunctionType()) { 10962 diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); 10963 return !S.getLangOpts().CPlusPlus; 10964 } 10965 10966 if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; 10967 10968 return true; 10969 } 10970 10971 /// Check the validity of a binary arithmetic operation w.r.t. pointer 10972 /// operands. 10973 /// 10974 /// This routine will diagnose any invalid arithmetic on pointer operands much 10975 /// like \see checkArithmeticOpPointerOperand. However, it has special logic 10976 /// for emitting a single diagnostic even for operations where both LHS and RHS 10977 /// are (potentially problematic) pointers. 10978 /// 10979 /// \returns True when the operand is valid to use (even if as an extension). 10980 static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, 10981 Expr *LHSExpr, Expr *RHSExpr) { 10982 bool isLHSPointer = LHSExpr->getType()->isAnyPointerType(); 10983 bool isRHSPointer = RHSExpr->getType()->isAnyPointerType(); 10984 if (!isLHSPointer && !isRHSPointer) return true; 10985 10986 QualType LHSPointeeTy, RHSPointeeTy; 10987 if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType(); 10988 if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType(); 10989 10990 // if both are pointers check if operation is valid wrt address spaces 10991 if (isLHSPointer && isRHSPointer) { 10992 if (!LHSPointeeTy.isAddressSpaceOverlapping(RHSPointeeTy, 10993 S.getASTContext())) { 10994 S.Diag(Loc, 10995 diag::err_typecheck_op_on_nonoverlapping_address_space_pointers) 10996 << LHSExpr->getType() << RHSExpr->getType() << 1 /*arithmetic op*/ 10997 << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); 10998 return false; 10999 } 11000 } 11001 11002 // Check for arithmetic on pointers to incomplete types. 11003 bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); 11004 bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); 11005 if (isLHSVoidPtr || isRHSVoidPtr) { 11006 if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHSExpr); 11007 else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHSExpr); 11008 else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHSExpr, RHSExpr); 11009 11010 return !S.getLangOpts().CPlusPlus; 11011 } 11012 11013 bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); 11014 bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); 11015 if (isLHSFuncPtr || isRHSFuncPtr) { 11016 if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHSExpr); 11017 else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, 11018 RHSExpr); 11019 else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHSExpr, RHSExpr); 11020 11021 return !S.getLangOpts().CPlusPlus; 11022 } 11023 11024 if (isLHSPointer && checkArithmeticIncompletePointerType(S, Loc, LHSExpr)) 11025 return false; 11026 if (isRHSPointer && checkArithmeticIncompletePointerType(S, Loc, RHSExpr)) 11027 return false; 11028 11029 return true; 11030 } 11031 11032 /// diagnoseStringPlusInt - Emit a warning when adding an integer to a string 11033 /// literal. 11034 static void diagnoseStringPlusInt(Sema &Self, SourceLocation OpLoc, 11035 Expr *LHSExpr, Expr *RHSExpr) { 11036 StringLiteral* StrExpr = dyn_cast<StringLiteral>(LHSExpr->IgnoreImpCasts()); 11037 Expr* IndexExpr = RHSExpr; 11038 if (!StrExpr) { 11039 StrExpr = dyn_cast<StringLiteral>(RHSExpr->IgnoreImpCasts()); 11040 IndexExpr = LHSExpr; 11041 } 11042 11043 bool IsStringPlusInt = StrExpr && 11044 IndexExpr->getType()->isIntegralOrUnscopedEnumerationType(); 11045 if (!IsStringPlusInt || IndexExpr->isValueDependent()) 11046 return; 11047 11048 SourceRange DiagRange(LHSExpr->getBeginLoc(), RHSExpr->getEndLoc()); 11049 Self.Diag(OpLoc, diag::warn_string_plus_int) 11050 << DiagRange << IndexExpr->IgnoreImpCasts()->getType(); 11051 11052 // Only print a fixit for "str" + int, not for int + "str". 11053 if (IndexExpr == RHSExpr) { 11054 SourceLocation EndLoc = Self.getLocForEndOfToken(RHSExpr->getEndLoc()); 11055 Self.Diag(OpLoc, diag::note_string_plus_scalar_silence) 11056 << FixItHint::CreateInsertion(LHSExpr->getBeginLoc(), "&") 11057 << FixItHint::CreateReplacement(SourceRange(OpLoc), "[") 11058 << FixItHint::CreateInsertion(EndLoc, "]"); 11059 } else 11060 Self.Diag(OpLoc, diag::note_string_plus_scalar_silence); 11061 } 11062 11063 /// Emit a warning when adding a char literal to a string. 11064 static void diagnoseStringPlusChar(Sema &Self, SourceLocation OpLoc, 11065 Expr *LHSExpr, Expr *RHSExpr) { 11066 const Expr *StringRefExpr = LHSExpr; 11067 const CharacterLiteral *CharExpr = 11068 dyn_cast<CharacterLiteral>(RHSExpr->IgnoreImpCasts()); 11069 11070 if (!CharExpr) { 11071 CharExpr = dyn_cast<CharacterLiteral>(LHSExpr->IgnoreImpCasts()); 11072 StringRefExpr = RHSExpr; 11073 } 11074 11075 if (!CharExpr || !StringRefExpr) 11076 return; 11077 11078 const QualType StringType = StringRefExpr->getType(); 11079 11080 // Return if not a PointerType. 11081 if (!StringType->isAnyPointerType()) 11082 return; 11083 11084 // Return if not a CharacterType. 11085 if (!StringType->getPointeeType()->isAnyCharacterType()) 11086 return; 11087 11088 ASTContext &Ctx = Self.getASTContext(); 11089 SourceRange DiagRange(LHSExpr->getBeginLoc(), RHSExpr->getEndLoc()); 11090 11091 const QualType CharType = CharExpr->getType(); 11092 if (!CharType->isAnyCharacterType() && 11093 CharType->isIntegerType() && 11094 llvm::isUIntN(Ctx.getCharWidth(), CharExpr->getValue())) { 11095 Self.Diag(OpLoc, diag::warn_string_plus_char) 11096 << DiagRange << Ctx.CharTy; 11097 } else { 11098 Self.Diag(OpLoc, diag::warn_string_plus_char) 11099 << DiagRange << CharExpr->getType(); 11100 } 11101 11102 // Only print a fixit for str + char, not for char + str. 11103 if (isa<CharacterLiteral>(RHSExpr->IgnoreImpCasts())) { 11104 SourceLocation EndLoc = Self.getLocForEndOfToken(RHSExpr->getEndLoc()); 11105 Self.Diag(OpLoc, diag::note_string_plus_scalar_silence) 11106 << FixItHint::CreateInsertion(LHSExpr->getBeginLoc(), "&") 11107 << FixItHint::CreateReplacement(SourceRange(OpLoc), "[") 11108 << FixItHint::CreateInsertion(EndLoc, "]"); 11109 } else { 11110 Self.Diag(OpLoc, diag::note_string_plus_scalar_silence); 11111 } 11112 } 11113 11114 /// Emit error when two pointers are incompatible. 11115 static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, 11116 Expr *LHSExpr, Expr *RHSExpr) { 11117 assert(LHSExpr->getType()->isAnyPointerType()); 11118 assert(RHSExpr->getType()->isAnyPointerType()); 11119 S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) 11120 << LHSExpr->getType() << RHSExpr->getType() << LHSExpr->getSourceRange() 11121 << RHSExpr->getSourceRange(); 11122 } 11123 11124 // C99 6.5.6 11125 QualType Sema::CheckAdditionOperands(ExprResult &LHS, ExprResult &RHS, 11126 SourceLocation Loc, BinaryOperatorKind Opc, 11127 QualType* CompLHSTy) { 11128 checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); 11129 11130 if (LHS.get()->getType()->isVectorType() || 11131 RHS.get()->getType()->isVectorType()) { 11132 QualType compType = 11133 CheckVectorOperands(LHS, RHS, Loc, CompLHSTy, 11134 /*AllowBothBool*/ getLangOpts().AltiVec, 11135 /*AllowBoolConversions*/ getLangOpts().ZVector, 11136 /*AllowBooleanOperation*/ false, 11137 /*ReportInvalid*/ true); 11138 if (CompLHSTy) *CompLHSTy = compType; 11139 return compType; 11140 } 11141 11142 if (LHS.get()->getType()->isSveVLSBuiltinType() || 11143 RHS.get()->getType()->isSveVLSBuiltinType()) { 11144 QualType compType = CheckSizelessVectorOperands(LHS, RHS, Loc, CompLHSTy, 11145 ArithConvKind::Arithmetic); 11146 if (CompLHSTy) 11147 *CompLHSTy = compType; 11148 return compType; 11149 } 11150 11151 if (LHS.get()->getType()->isConstantMatrixType() || 11152 RHS.get()->getType()->isConstantMatrixType()) { 11153 QualType compType = 11154 CheckMatrixElementwiseOperands(LHS, RHS, Loc, CompLHSTy); 11155 if (CompLHSTy) 11156 *CompLHSTy = compType; 11157 return compType; 11158 } 11159 11160 QualType compType = UsualArithmeticConversions( 11161 LHS, RHS, Loc, 11162 CompLHSTy ? ArithConvKind::CompAssign : ArithConvKind::Arithmetic); 11163 if (LHS.isInvalid() || RHS.isInvalid()) 11164 return QualType(); 11165 11166 // Diagnose "string literal" '+' int and string '+' "char literal". 11167 if (Opc == BO_Add) { 11168 diagnoseStringPlusInt(*this, Loc, LHS.get(), RHS.get()); 11169 diagnoseStringPlusChar(*this, Loc, LHS.get(), RHS.get()); 11170 } 11171 11172 // handle the common case first (both operands are arithmetic). 11173 if (!compType.isNull() && compType->isArithmeticType()) { 11174 if (CompLHSTy) *CompLHSTy = compType; 11175 return compType; 11176 } 11177 11178 // Type-checking. Ultimately the pointer's going to be in PExp; 11179 // note that we bias towards the LHS being the pointer. 11180 Expr *PExp = LHS.get(), *IExp = RHS.get(); 11181 11182 bool isObjCPointer; 11183 if (PExp->getType()->isPointerType()) { 11184 isObjCPointer = false; 11185 } else if (PExp->getType()->isObjCObjectPointerType()) { 11186 isObjCPointer = true; 11187 } else { 11188 std::swap(PExp, IExp); 11189 if (PExp->getType()->isPointerType()) { 11190 isObjCPointer = false; 11191 } else if (PExp->getType()->isObjCObjectPointerType()) { 11192 isObjCPointer = true; 11193 } else { 11194 return InvalidOperands(Loc, LHS, RHS); 11195 } 11196 } 11197 assert(PExp->getType()->isAnyPointerType()); 11198 11199 if (!IExp->getType()->isIntegerType()) 11200 return InvalidOperands(Loc, LHS, RHS); 11201 11202 // Adding to a null pointer results in undefined behavior. 11203 if (PExp->IgnoreParenCasts()->isNullPointerConstant( 11204 Context, Expr::NPC_ValueDependentIsNotNull)) { 11205 // In C++ adding zero to a null pointer is defined. 11206 Expr::EvalResult KnownVal; 11207 if (!getLangOpts().CPlusPlus || 11208 (!IExp->isValueDependent() && 11209 (!IExp->EvaluateAsInt(KnownVal, Context) || 11210 KnownVal.Val.getInt() != 0))) { 11211 // Check the conditions to see if this is the 'p = nullptr + n' idiom. 11212 bool IsGNUIdiom = BinaryOperator::isNullPointerArithmeticExtension( 11213 Context, BO_Add, PExp, IExp); 11214 diagnoseArithmeticOnNullPointer(*this, Loc, PExp, IsGNUIdiom); 11215 } 11216 } 11217 11218 if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) 11219 return QualType(); 11220 11221 if (isObjCPointer && checkArithmeticOnObjCPointer(*this, Loc, PExp)) 11222 return QualType(); 11223 11224 // Arithmetic on label addresses is normally allowed, except when we add 11225 // a ptrauth signature to the addresses. 11226 if (isa<AddrLabelExpr>(PExp) && getLangOpts().PointerAuthIndirectGotos) { 11227 Diag(Loc, diag::err_ptrauth_indirect_goto_addrlabel_arithmetic) 11228 << /*addition*/ 1; 11229 return QualType(); 11230 } 11231 11232 // Check array bounds for pointer arithemtic 11233 CheckArrayAccess(PExp, IExp); 11234 11235 if (CompLHSTy) { 11236 QualType LHSTy = Context.isPromotableBitField(LHS.get()); 11237 if (LHSTy.isNull()) { 11238 LHSTy = LHS.get()->getType(); 11239 if (Context.isPromotableIntegerType(LHSTy)) 11240 LHSTy = Context.getPromotedIntegerType(LHSTy); 11241 } 11242 *CompLHSTy = LHSTy; 11243 } 11244 11245 return PExp->getType(); 11246 } 11247 11248 // C99 6.5.6 11249 QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, 11250 SourceLocation Loc, 11251 QualType* CompLHSTy) { 11252 checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); 11253 11254 if (LHS.get()->getType()->isVectorType() || 11255 RHS.get()->getType()->isVectorType()) { 11256 QualType compType = 11257 CheckVectorOperands(LHS, RHS, Loc, CompLHSTy, 11258 /*AllowBothBool*/ getLangOpts().AltiVec, 11259 /*AllowBoolConversions*/ getLangOpts().ZVector, 11260 /*AllowBooleanOperation*/ false, 11261 /*ReportInvalid*/ true); 11262 if (CompLHSTy) *CompLHSTy = compType; 11263 return compType; 11264 } 11265 11266 if (LHS.get()->getType()->isSveVLSBuiltinType() || 11267 RHS.get()->getType()->isSveVLSBuiltinType()) { 11268 QualType compType = CheckSizelessVectorOperands(LHS, RHS, Loc, CompLHSTy, 11269 ArithConvKind::Arithmetic); 11270 if (CompLHSTy) 11271 *CompLHSTy = compType; 11272 return compType; 11273 } 11274 11275 if (LHS.get()->getType()->isConstantMatrixType() || 11276 RHS.get()->getType()->isConstantMatrixType()) { 11277 QualType compType = 11278 CheckMatrixElementwiseOperands(LHS, RHS, Loc, CompLHSTy); 11279 if (CompLHSTy) 11280 *CompLHSTy = compType; 11281 return compType; 11282 } 11283 11284 QualType compType = UsualArithmeticConversions( 11285 LHS, RHS, Loc, 11286 CompLHSTy ? ArithConvKind::CompAssign : ArithConvKind::Arithmetic); 11287 if (LHS.isInvalid() || RHS.isInvalid()) 11288 return QualType(); 11289 11290 // Enforce type constraints: C99 6.5.6p3. 11291 11292 // Handle the common case first (both operands are arithmetic). 11293 if (!compType.isNull() && compType->isArithmeticType()) { 11294 if (CompLHSTy) *CompLHSTy = compType; 11295 return compType; 11296 } 11297 11298 // Either ptr - int or ptr - ptr. 11299 if (LHS.get()->getType()->isAnyPointerType()) { 11300 QualType lpointee = LHS.get()->getType()->getPointeeType(); 11301 11302 // Diagnose bad cases where we step over interface counts. 11303 if (LHS.get()->getType()->isObjCObjectPointerType() && 11304 checkArithmeticOnObjCPointer(*this, Loc, LHS.get())) 11305 return QualType(); 11306 11307 // Arithmetic on label addresses is normally allowed, except when we add 11308 // a ptrauth signature to the addresses. 11309 if (isa<AddrLabelExpr>(LHS.get()) && 11310 getLangOpts().PointerAuthIndirectGotos) { 11311 Diag(Loc, diag::err_ptrauth_indirect_goto_addrlabel_arithmetic) 11312 << /*subtraction*/ 0; 11313 return QualType(); 11314 } 11315 11316 // The result type of a pointer-int computation is the pointer type. 11317 if (RHS.get()->getType()->isIntegerType()) { 11318 // Subtracting from a null pointer should produce a warning. 11319 // The last argument to the diagnose call says this doesn't match the 11320 // GNU int-to-pointer idiom. 11321 if (LHS.get()->IgnoreParenCasts()->isNullPointerConstant(Context, 11322 Expr::NPC_ValueDependentIsNotNull)) { 11323 // In C++ adding zero to a null pointer is defined. 11324 Expr::EvalResult KnownVal; 11325 if (!getLangOpts().CPlusPlus || 11326 (!RHS.get()->isValueDependent() && 11327 (!RHS.get()->EvaluateAsInt(KnownVal, Context) || 11328 KnownVal.Val.getInt() != 0))) { 11329 diagnoseArithmeticOnNullPointer(*this, Loc, LHS.get(), false); 11330 } 11331 } 11332 11333 if (!checkArithmeticOpPointerOperand(*this, Loc, LHS.get())) 11334 return QualType(); 11335 11336 // Check array bounds for pointer arithemtic 11337 CheckArrayAccess(LHS.get(), RHS.get(), /*ArraySubscriptExpr*/nullptr, 11338 /*AllowOnePastEnd*/true, /*IndexNegated*/true); 11339 11340 if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); 11341 return LHS.get()->getType(); 11342 } 11343 11344 // Handle pointer-pointer subtractions. 11345 if (const PointerType *RHSPTy 11346 = RHS.get()->getType()->getAs<PointerType>()) { 11347 QualType rpointee = RHSPTy->getPointeeType(); 11348 11349 if (getLangOpts().CPlusPlus) { 11350 // Pointee types must be the same: C++ [expr.add] 11351 if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { 11352 diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); 11353 } 11354 } else { 11355 // Pointee types must be compatible C99 6.5.6p3 11356 if (!Context.typesAreCompatible( 11357 Context.getCanonicalType(lpointee).getUnqualifiedType(), 11358 Context.getCanonicalType(rpointee).getUnqualifiedType())) { 11359 diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); 11360 return QualType(); 11361 } 11362 } 11363 11364 if (!checkArithmeticBinOpPointerOperands(*this, Loc, 11365 LHS.get(), RHS.get())) 11366 return QualType(); 11367 11368 bool LHSIsNullPtr = LHS.get()->IgnoreParenCasts()->isNullPointerConstant( 11369 Context, Expr::NPC_ValueDependentIsNotNull); 11370 bool RHSIsNullPtr = RHS.get()->IgnoreParenCasts()->isNullPointerConstant( 11371 Context, Expr::NPC_ValueDependentIsNotNull); 11372 11373 // Subtracting nullptr or from nullptr is suspect 11374 if (LHSIsNullPtr) 11375 diagnoseSubtractionOnNullPointer(*this, Loc, LHS.get(), RHSIsNullPtr); 11376 if (RHSIsNullPtr) 11377 diagnoseSubtractionOnNullPointer(*this, Loc, RHS.get(), LHSIsNullPtr); 11378 11379 // The pointee type may have zero size. As an extension, a structure or 11380 // union may have zero size or an array may have zero length. In this 11381 // case subtraction does not make sense. 11382 if (!rpointee->isVoidType() && !rpointee->isFunctionType()) { 11383 CharUnits ElementSize = Context.getTypeSizeInChars(rpointee); 11384 if (ElementSize.isZero()) { 11385 Diag(Loc,diag::warn_sub_ptr_zero_size_types) 11386 << rpointee.getUnqualifiedType() 11387 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 11388 } 11389 } 11390 11391 if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); 11392 return Context.getPointerDiffType(); 11393 } 11394 } 11395 11396 return InvalidOperands(Loc, LHS, RHS); 11397 } 11398 11399 static bool isScopedEnumerationType(QualType T) { 11400 if (const EnumType *ET = T->getAs<EnumType>()) 11401 return ET->getDecl()->isScoped(); 11402 return false; 11403 } 11404 11405 static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, 11406 SourceLocation Loc, BinaryOperatorKind Opc, 11407 QualType LHSType) { 11408 // OpenCL 6.3j: shift values are effectively % word size of LHS (more defined), 11409 // so skip remaining warnings as we don't want to modify values within Sema. 11410 if (S.getLangOpts().OpenCL) 11411 return; 11412 11413 if (Opc == BO_Shr && 11414 LHS.get()->IgnoreParenImpCasts()->getType()->isBooleanType()) 11415 S.Diag(Loc, diag::warn_shift_bool) << LHS.get()->getSourceRange(); 11416 11417 // Check right/shifter operand 11418 Expr::EvalResult RHSResult; 11419 if (RHS.get()->isValueDependent() || 11420 !RHS.get()->EvaluateAsInt(RHSResult, S.Context)) 11421 return; 11422 llvm::APSInt Right = RHSResult.Val.getInt(); 11423 11424 if (Right.isNegative()) { 11425 S.DiagRuntimeBehavior(Loc, RHS.get(), 11426 S.PDiag(diag::warn_shift_negative) 11427 << RHS.get()->getSourceRange()); 11428 return; 11429 } 11430 11431 QualType LHSExprType = LHS.get()->getType(); 11432 uint64_t LeftSize = S.Context.getTypeSize(LHSExprType); 11433 if (LHSExprType->isBitIntType()) 11434 LeftSize = S.Context.getIntWidth(LHSExprType); 11435 else if (LHSExprType->isFixedPointType()) { 11436 auto FXSema = S.Context.getFixedPointSemantics(LHSExprType); 11437 LeftSize = FXSema.getWidth() - (unsigned)FXSema.hasUnsignedPadding(); 11438 } 11439 if (Right.uge(LeftSize)) { 11440 S.DiagRuntimeBehavior(Loc, RHS.get(), 11441 S.PDiag(diag::warn_shift_gt_typewidth) 11442 << RHS.get()->getSourceRange()); 11443 return; 11444 } 11445 11446 // FIXME: We probably need to handle fixed point types specially here. 11447 if (Opc != BO_Shl || LHSExprType->isFixedPointType()) 11448 return; 11449 11450 // When left shifting an ICE which is signed, we can check for overflow which 11451 // according to C++ standards prior to C++2a has undefined behavior 11452 // ([expr.shift] 5.8/2). Unsigned integers have defined behavior modulo one 11453 // more than the maximum value representable in the result type, so never 11454 // warn for those. (FIXME: Unsigned left-shift overflow in a constant 11455 // expression is still probably a bug.) 11456 Expr::EvalResult LHSResult; 11457 if (LHS.get()->isValueDependent() || 11458 LHSType->hasUnsignedIntegerRepresentation() || 11459 !LHS.get()->EvaluateAsInt(LHSResult, S.Context)) 11460 return; 11461 llvm::APSInt Left = LHSResult.Val.getInt(); 11462 11463 // Don't warn if signed overflow is defined, then all the rest of the 11464 // diagnostics will not be triggered because the behavior is defined. 11465 // Also don't warn in C++20 mode (and newer), as signed left shifts 11466 // always wrap and never overflow. 11467 if (S.getLangOpts().isSignedOverflowDefined() || S.getLangOpts().CPlusPlus20) 11468 return; 11469 11470 // If LHS does not have a non-negative value then, the 11471 // behavior is undefined before C++2a. Warn about it. 11472 if (Left.isNegative()) { 11473 S.DiagRuntimeBehavior(Loc, LHS.get(), 11474 S.PDiag(diag::warn_shift_lhs_negative) 11475 << LHS.get()->getSourceRange()); 11476 return; 11477 } 11478 11479 llvm::APInt ResultBits = 11480 static_cast<llvm::APInt &>(Right) + Left.getSignificantBits(); 11481 if (ResultBits.ule(LeftSize)) 11482 return; 11483 llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); 11484 Result = Result.shl(Right); 11485 11486 // Print the bit representation of the signed integer as an unsigned 11487 // hexadecimal number. 11488 SmallString<40> HexResult; 11489 Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); 11490 11491 // If we are only missing a sign bit, this is less likely to result in actual 11492 // bugs -- if the result is cast back to an unsigned type, it will have the 11493 // expected value. Thus we place this behind a different warning that can be 11494 // turned off separately if needed. 11495 if (ResultBits - 1 == LeftSize) { 11496 S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) 11497 << HexResult << LHSType 11498 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 11499 return; 11500 } 11501 11502 S.Diag(Loc, diag::warn_shift_result_gt_typewidth) 11503 << HexResult.str() << Result.getSignificantBits() << LHSType 11504 << Left.getBitWidth() << LHS.get()->getSourceRange() 11505 << RHS.get()->getSourceRange(); 11506 } 11507 11508 /// Return the resulting type when a vector is shifted 11509 /// by a scalar or vector shift amount. 11510 static QualType checkVectorShift(Sema &S, ExprResult &LHS, ExprResult &RHS, 11511 SourceLocation Loc, bool IsCompAssign) { 11512 // OpenCL v1.1 s6.3.j says RHS can be a vector only if LHS is a vector. 11513 if ((S.LangOpts.OpenCL || S.LangOpts.ZVector) && 11514 !LHS.get()->getType()->isVectorType()) { 11515 S.Diag(Loc, diag::err_shift_rhs_only_vector) 11516 << RHS.get()->getType() << LHS.get()->getType() 11517 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 11518 return QualType(); 11519 } 11520 11521 if (!IsCompAssign) { 11522 LHS = S.UsualUnaryConversions(LHS.get()); 11523 if (LHS.isInvalid()) return QualType(); 11524 } 11525 11526 RHS = S.UsualUnaryConversions(RHS.get()); 11527 if (RHS.isInvalid()) return QualType(); 11528 11529 QualType LHSType = LHS.get()->getType(); 11530 // Note that LHS might be a scalar because the routine calls not only in 11531 // OpenCL case. 11532 const VectorType *LHSVecTy = LHSType->getAs<VectorType>(); 11533 QualType LHSEleType = LHSVecTy ? LHSVecTy->getElementType() : LHSType; 11534 11535 // Note that RHS might not be a vector. 11536 QualType RHSType = RHS.get()->getType(); 11537 const VectorType *RHSVecTy = RHSType->getAs<VectorType>(); 11538 QualType RHSEleType = RHSVecTy ? RHSVecTy->getElementType() : RHSType; 11539 11540 // Do not allow shifts for boolean vectors. 11541 if ((LHSVecTy && LHSVecTy->isExtVectorBoolType()) || 11542 (RHSVecTy && RHSVecTy->isExtVectorBoolType())) { 11543 S.Diag(Loc, diag::err_typecheck_invalid_operands) 11544 << LHS.get()->getType() << RHS.get()->getType() 11545 << LHS.get()->getSourceRange(); 11546 return QualType(); 11547 } 11548 11549 // The operands need to be integers. 11550 if (!LHSEleType->isIntegerType()) { 11551 S.Diag(Loc, diag::err_typecheck_expect_int) 11552 << LHS.get()->getType() << LHS.get()->getSourceRange(); 11553 return QualType(); 11554 } 11555 11556 if (!RHSEleType->isIntegerType()) { 11557 S.Diag(Loc, diag::err_typecheck_expect_int) 11558 << RHS.get()->getType() << RHS.get()->getSourceRange(); 11559 return QualType(); 11560 } 11561 11562 if (!LHSVecTy) { 11563 assert(RHSVecTy); 11564 if (IsCompAssign) 11565 return RHSType; 11566 if (LHSEleType != RHSEleType) { 11567 LHS = S.ImpCastExprToType(LHS.get(),RHSEleType, CK_IntegralCast); 11568 LHSEleType = RHSEleType; 11569 } 11570 QualType VecTy = 11571 S.Context.getExtVectorType(LHSEleType, RHSVecTy->getNumElements()); 11572 LHS = S.ImpCastExprToType(LHS.get(), VecTy, CK_VectorSplat); 11573 LHSType = VecTy; 11574 } else if (RHSVecTy) { 11575 // OpenCL v1.1 s6.3.j says that for vector types, the operators 11576 // are applied component-wise. So if RHS is a vector, then ensure 11577 // that the number of elements is the same as LHS... 11578 if (RHSVecTy->getNumElements() != LHSVecTy->getNumElements()) { 11579 S.Diag(Loc, diag::err_typecheck_vector_lengths_not_equal) 11580 << LHS.get()->getType() << RHS.get()->getType() 11581 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 11582 return QualType(); 11583 } 11584 if (!S.LangOpts.OpenCL && !S.LangOpts.ZVector) { 11585 const BuiltinType *LHSBT = LHSEleType->getAs<clang::BuiltinType>(); 11586 const BuiltinType *RHSBT = RHSEleType->getAs<clang::BuiltinType>(); 11587 if (LHSBT != RHSBT && 11588 S.Context.getTypeSize(LHSBT) != S.Context.getTypeSize(RHSBT)) { 11589 S.Diag(Loc, diag::warn_typecheck_vector_element_sizes_not_equal) 11590 << LHS.get()->getType() << RHS.get()->getType() 11591 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 11592 } 11593 } 11594 } else { 11595 // ...else expand RHS to match the number of elements in LHS. 11596 QualType VecTy = 11597 S.Context.getExtVectorType(RHSEleType, LHSVecTy->getNumElements()); 11598 RHS = S.ImpCastExprToType(RHS.get(), VecTy, CK_VectorSplat); 11599 } 11600 11601 return LHSType; 11602 } 11603 11604 static QualType checkSizelessVectorShift(Sema &S, ExprResult &LHS, 11605 ExprResult &RHS, SourceLocation Loc, 11606 bool IsCompAssign) { 11607 if (!IsCompAssign) { 11608 LHS = S.UsualUnaryConversions(LHS.get()); 11609 if (LHS.isInvalid()) 11610 return QualType(); 11611 } 11612 11613 RHS = S.UsualUnaryConversions(RHS.get()); 11614 if (RHS.isInvalid()) 11615 return QualType(); 11616 11617 QualType LHSType = LHS.get()->getType(); 11618 const BuiltinType *LHSBuiltinTy = LHSType->castAs<BuiltinType>(); 11619 QualType LHSEleType = LHSType->isSveVLSBuiltinType() 11620 ? LHSBuiltinTy->getSveEltType(S.getASTContext()) 11621 : LHSType; 11622 11623 // Note that RHS might not be a vector 11624 QualType RHSType = RHS.get()->getType(); 11625 const BuiltinType *RHSBuiltinTy = RHSType->castAs<BuiltinType>(); 11626 QualType RHSEleType = RHSType->isSveVLSBuiltinType() 11627 ? RHSBuiltinTy->getSveEltType(S.getASTContext()) 11628 : RHSType; 11629 11630 if ((LHSBuiltinTy && LHSBuiltinTy->isSVEBool()) || 11631 (RHSBuiltinTy && RHSBuiltinTy->isSVEBool())) { 11632 S.Diag(Loc, diag::err_typecheck_invalid_operands) 11633 << LHSType << RHSType << LHS.get()->getSourceRange(); 11634 return QualType(); 11635 } 11636 11637 if (!LHSEleType->isIntegerType()) { 11638 S.Diag(Loc, diag::err_typecheck_expect_int) 11639 << LHS.get()->getType() << LHS.get()->getSourceRange(); 11640 return QualType(); 11641 } 11642 11643 if (!RHSEleType->isIntegerType()) { 11644 S.Diag(Loc, diag::err_typecheck_expect_int) 11645 << RHS.get()->getType() << RHS.get()->getSourceRange(); 11646 return QualType(); 11647 } 11648 11649 if (LHSType->isSveVLSBuiltinType() && RHSType->isSveVLSBuiltinType() && 11650 (S.Context.getBuiltinVectorTypeInfo(LHSBuiltinTy).EC != 11651 S.Context.getBuiltinVectorTypeInfo(RHSBuiltinTy).EC)) { 11652 S.Diag(Loc, diag::err_typecheck_invalid_operands) 11653 << LHSType << RHSType << LHS.get()->getSourceRange() 11654 << RHS.get()->getSourceRange(); 11655 return QualType(); 11656 } 11657 11658 if (!LHSType->isSveVLSBuiltinType()) { 11659 assert(RHSType->isSveVLSBuiltinType()); 11660 if (IsCompAssign) 11661 return RHSType; 11662 if (LHSEleType != RHSEleType) { 11663 LHS = S.ImpCastExprToType(LHS.get(), RHSEleType, clang::CK_IntegralCast); 11664 LHSEleType = RHSEleType; 11665 } 11666 const llvm::ElementCount VecSize = 11667 S.Context.getBuiltinVectorTypeInfo(RHSBuiltinTy).EC; 11668 QualType VecTy = 11669 S.Context.getScalableVectorType(LHSEleType, VecSize.getKnownMinValue()); 11670 LHS = S.ImpCastExprToType(LHS.get(), VecTy, clang::CK_VectorSplat); 11671 LHSType = VecTy; 11672 } else if (RHSBuiltinTy && RHSBuiltinTy->isSveVLSBuiltinType()) { 11673 if (S.Context.getTypeSize(RHSBuiltinTy) != 11674 S.Context.getTypeSize(LHSBuiltinTy)) { 11675 S.Diag(Loc, diag::err_typecheck_vector_lengths_not_equal) 11676 << LHSType << RHSType << LHS.get()->getSourceRange() 11677 << RHS.get()->getSourceRange(); 11678 return QualType(); 11679 } 11680 } else { 11681 const llvm::ElementCount VecSize = 11682 S.Context.getBuiltinVectorTypeInfo(LHSBuiltinTy).EC; 11683 if (LHSEleType != RHSEleType) { 11684 RHS = S.ImpCastExprToType(RHS.get(), LHSEleType, clang::CK_IntegralCast); 11685 RHSEleType = LHSEleType; 11686 } 11687 QualType VecTy = 11688 S.Context.getScalableVectorType(RHSEleType, VecSize.getKnownMinValue()); 11689 RHS = S.ImpCastExprToType(RHS.get(), VecTy, CK_VectorSplat); 11690 } 11691 11692 return LHSType; 11693 } 11694 11695 // C99 6.5.7 11696 QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, 11697 SourceLocation Loc, BinaryOperatorKind Opc, 11698 bool IsCompAssign) { 11699 checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); 11700 11701 // Vector shifts promote their scalar inputs to vector type. 11702 if (LHS.get()->getType()->isVectorType() || 11703 RHS.get()->getType()->isVectorType()) { 11704 if (LangOpts.ZVector) { 11705 // The shift operators for the z vector extensions work basically 11706 // like general shifts, except that neither the LHS nor the RHS is 11707 // allowed to be a "vector bool". 11708 if (auto LHSVecType = LHS.get()->getType()->getAs<VectorType>()) 11709 if (LHSVecType->getVectorKind() == VectorKind::AltiVecBool) 11710 return InvalidOperands(Loc, LHS, RHS); 11711 if (auto RHSVecType = RHS.get()->getType()->getAs<VectorType>()) 11712 if (RHSVecType->getVectorKind() == VectorKind::AltiVecBool) 11713 return InvalidOperands(Loc, LHS, RHS); 11714 } 11715 return checkVectorShift(*this, LHS, RHS, Loc, IsCompAssign); 11716 } 11717 11718 if (LHS.get()->getType()->isSveVLSBuiltinType() || 11719 RHS.get()->getType()->isSveVLSBuiltinType()) 11720 return checkSizelessVectorShift(*this, LHS, RHS, Loc, IsCompAssign); 11721 11722 // Shifts don't perform usual arithmetic conversions, they just do integer 11723 // promotions on each operand. C99 6.5.7p3 11724 11725 // For the LHS, do usual unary conversions, but then reset them away 11726 // if this is a compound assignment. 11727 ExprResult OldLHS = LHS; 11728 LHS = UsualUnaryConversions(LHS.get()); 11729 if (LHS.isInvalid()) 11730 return QualType(); 11731 QualType LHSType = LHS.get()->getType(); 11732 if (IsCompAssign) LHS = OldLHS; 11733 11734 // The RHS is simpler. 11735 RHS = UsualUnaryConversions(RHS.get()); 11736 if (RHS.isInvalid()) 11737 return QualType(); 11738 QualType RHSType = RHS.get()->getType(); 11739 11740 // C99 6.5.7p2: Each of the operands shall have integer type. 11741 // Embedded-C 4.1.6.2.2: The LHS may also be fixed-point. 11742 if ((!LHSType->isFixedPointOrIntegerType() && 11743 !LHSType->hasIntegerRepresentation()) || 11744 !RHSType->hasIntegerRepresentation()) 11745 return InvalidOperands(Loc, LHS, RHS); 11746 11747 // C++0x: Don't allow scoped enums. FIXME: Use something better than 11748 // hasIntegerRepresentation() above instead of this. 11749 if (isScopedEnumerationType(LHSType) || 11750 isScopedEnumerationType(RHSType)) { 11751 return InvalidOperands(Loc, LHS, RHS); 11752 } 11753 DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType); 11754 11755 // "The type of the result is that of the promoted left operand." 11756 return LHSType; 11757 } 11758 11759 /// Diagnose bad pointer comparisons. 11760 static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, 11761 ExprResult &LHS, ExprResult &RHS, 11762 bool IsError) { 11763 S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_distinct_pointers 11764 : diag::ext_typecheck_comparison_of_distinct_pointers) 11765 << LHS.get()->getType() << RHS.get()->getType() 11766 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 11767 } 11768 11769 /// Returns false if the pointers are converted to a composite type, 11770 /// true otherwise. 11771 static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, 11772 ExprResult &LHS, ExprResult &RHS) { 11773 // C++ [expr.rel]p2: 11774 // [...] Pointer conversions (4.10) and qualification 11775 // conversions (4.4) are performed on pointer operands (or on 11776 // a pointer operand and a null pointer constant) to bring 11777 // them to their composite pointer type. [...] 11778 // 11779 // C++ [expr.eq]p1 uses the same notion for (in)equality 11780 // comparisons of pointers. 11781 11782 QualType LHSType = LHS.get()->getType(); 11783 QualType RHSType = RHS.get()->getType(); 11784 assert(LHSType->isPointerType() || RHSType->isPointerType() || 11785 LHSType->isMemberPointerType() || RHSType->isMemberPointerType()); 11786 11787 QualType T = S.FindCompositePointerType(Loc, LHS, RHS); 11788 if (T.isNull()) { 11789 if ((LHSType->isAnyPointerType() || LHSType->isMemberPointerType()) && 11790 (RHSType->isAnyPointerType() || RHSType->isMemberPointerType())) 11791 diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); 11792 else 11793 S.InvalidOperands(Loc, LHS, RHS); 11794 return true; 11795 } 11796 11797 return false; 11798 } 11799 11800 static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, 11801 ExprResult &LHS, 11802 ExprResult &RHS, 11803 bool IsError) { 11804 S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_fptr_to_void 11805 : diag::ext_typecheck_comparison_of_fptr_to_void) 11806 << LHS.get()->getType() << RHS.get()->getType() 11807 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 11808 } 11809 11810 static bool isObjCObjectLiteral(ExprResult &E) { 11811 switch (E.get()->IgnoreParenImpCasts()->getStmtClass()) { 11812 case Stmt::ObjCArrayLiteralClass: 11813 case Stmt::ObjCDictionaryLiteralClass: 11814 case Stmt::ObjCStringLiteralClass: 11815 case Stmt::ObjCBoxedExprClass: 11816 return true; 11817 default: 11818 // Note that ObjCBoolLiteral is NOT an object literal! 11819 return false; 11820 } 11821 } 11822 11823 static bool hasIsEqualMethod(Sema &S, const Expr *LHS, const Expr *RHS) { 11824 const ObjCObjectPointerType *Type = 11825 LHS->getType()->getAs<ObjCObjectPointerType>(); 11826 11827 // If this is not actually an Objective-C object, bail out. 11828 if (!Type) 11829 return false; 11830 11831 // Get the LHS object's interface type. 11832 QualType InterfaceType = Type->getPointeeType(); 11833 11834 // If the RHS isn't an Objective-C object, bail out. 11835 if (!RHS->getType()->isObjCObjectPointerType()) 11836 return false; 11837 11838 // Try to find the -isEqual: method. 11839 Selector IsEqualSel = S.ObjC().NSAPIObj->getIsEqualSelector(); 11840 ObjCMethodDecl *Method = 11841 S.ObjC().LookupMethodInObjectType(IsEqualSel, InterfaceType, 11842 /*IsInstance=*/true); 11843 if (!Method) { 11844 if (Type->isObjCIdType()) { 11845 // For 'id', just check the global pool. 11846 Method = 11847 S.ObjC().LookupInstanceMethodInGlobalPool(IsEqualSel, SourceRange(), 11848 /*receiverId=*/true); 11849 } else { 11850 // Check protocols. 11851 Method = S.ObjC().LookupMethodInQualifiedType(IsEqualSel, Type, 11852 /*IsInstance=*/true); 11853 } 11854 } 11855 11856 if (!Method) 11857 return false; 11858 11859 QualType T = Method->parameters()[0]->getType(); 11860 if (!T->isObjCObjectPointerType()) 11861 return false; 11862 11863 QualType R = Method->getReturnType(); 11864 if (!R->isScalarType()) 11865 return false; 11866 11867 return true; 11868 } 11869 11870 static void diagnoseObjCLiteralComparison(Sema &S, SourceLocation Loc, 11871 ExprResult &LHS, ExprResult &RHS, 11872 BinaryOperator::Opcode Opc){ 11873 Expr *Literal; 11874 Expr *Other; 11875 if (isObjCObjectLiteral(LHS)) { 11876 Literal = LHS.get(); 11877 Other = RHS.get(); 11878 } else { 11879 Literal = RHS.get(); 11880 Other = LHS.get(); 11881 } 11882 11883 // Don't warn on comparisons against nil. 11884 Other = Other->IgnoreParenCasts(); 11885 if (Other->isNullPointerConstant(S.getASTContext(), 11886 Expr::NPC_ValueDependentIsNotNull)) 11887 return; 11888 11889 // This should be kept in sync with warn_objc_literal_comparison. 11890 // LK_String should always be after the other literals, since it has its own 11891 // warning flag. 11892 SemaObjC::ObjCLiteralKind LiteralKind = S.ObjC().CheckLiteralKind(Literal); 11893 assert(LiteralKind != SemaObjC::LK_Block); 11894 if (LiteralKind == SemaObjC::LK_None) { 11895 llvm_unreachable("Unknown Objective-C object literal kind"); 11896 } 11897 11898 if (LiteralKind == SemaObjC::LK_String) 11899 S.Diag(Loc, diag::warn_objc_string_literal_comparison) 11900 << Literal->getSourceRange(); 11901 else 11902 S.Diag(Loc, diag::warn_objc_literal_comparison) 11903 << LiteralKind << Literal->getSourceRange(); 11904 11905 if (BinaryOperator::isEqualityOp(Opc) && 11906 hasIsEqualMethod(S, LHS.get(), RHS.get())) { 11907 SourceLocation Start = LHS.get()->getBeginLoc(); 11908 SourceLocation End = S.getLocForEndOfToken(RHS.get()->getEndLoc()); 11909 CharSourceRange OpRange = 11910 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); 11911 11912 S.Diag(Loc, diag::note_objc_literal_comparison_isequal) 11913 << FixItHint::CreateInsertion(Start, Opc == BO_EQ ? "[" : "![") 11914 << FixItHint::CreateReplacement(OpRange, " isEqual:") 11915 << FixItHint::CreateInsertion(End, "]"); 11916 } 11917 } 11918 11919 /// Warns on !x < y, !x & y where !(x < y), !(x & y) was probably intended. 11920 static void diagnoseLogicalNotOnLHSofCheck(Sema &S, ExprResult &LHS, 11921 ExprResult &RHS, SourceLocation Loc, 11922 BinaryOperatorKind Opc) { 11923 // Check that left hand side is !something. 11924 UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS.get()->IgnoreImpCasts()); 11925 if (!UO || UO->getOpcode() != UO_LNot) return; 11926 11927 // Only check if the right hand side is non-bool arithmetic type. 11928 if (RHS.get()->isKnownToHaveBooleanValue()) return; 11929 11930 // Make sure that the something in !something is not bool. 11931 Expr *SubExpr = UO->getSubExpr()->IgnoreImpCasts(); 11932 if (SubExpr->isKnownToHaveBooleanValue()) return; 11933 11934 // Emit warning. 11935 bool IsBitwiseOp = Opc == BO_And || Opc == BO_Or || Opc == BO_Xor; 11936 S.Diag(UO->getOperatorLoc(), diag::warn_logical_not_on_lhs_of_check) 11937 << Loc << IsBitwiseOp; 11938 11939 // First note suggest !(x < y) 11940 SourceLocation FirstOpen = SubExpr->getBeginLoc(); 11941 SourceLocation FirstClose = RHS.get()->getEndLoc(); 11942 FirstClose = S.getLocForEndOfToken(FirstClose); 11943 if (FirstClose.isInvalid()) 11944 FirstOpen = SourceLocation(); 11945 S.Diag(UO->getOperatorLoc(), diag::note_logical_not_fix) 11946 << IsBitwiseOp 11947 << FixItHint::CreateInsertion(FirstOpen, "(") 11948 << FixItHint::CreateInsertion(FirstClose, ")"); 11949 11950 // Second note suggests (!x) < y 11951 SourceLocation SecondOpen = LHS.get()->getBeginLoc(); 11952 SourceLocation SecondClose = LHS.get()->getEndLoc(); 11953 SecondClose = S.getLocForEndOfToken(SecondClose); 11954 if (SecondClose.isInvalid()) 11955 SecondOpen = SourceLocation(); 11956 S.Diag(UO->getOperatorLoc(), diag::note_logical_not_silence_with_parens) 11957 << FixItHint::CreateInsertion(SecondOpen, "(") 11958 << FixItHint::CreateInsertion(SecondClose, ")"); 11959 } 11960 11961 // Returns true if E refers to a non-weak array. 11962 static bool checkForArray(const Expr *E) { 11963 const ValueDecl *D = nullptr; 11964 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { 11965 D = DR->getDecl(); 11966 } else if (const MemberExpr *Mem = dyn_cast<MemberExpr>(E)) { 11967 if (Mem->isImplicitAccess()) 11968 D = Mem->getMemberDecl(); 11969 } 11970 if (!D) 11971 return false; 11972 return D->getType()->isArrayType() && !D->isWeak(); 11973 } 11974 11975 /// Detect patterns ptr + size >= ptr and ptr + size < ptr, where ptr is a 11976 /// pointer and size is an unsigned integer. Return whether the result is 11977 /// always true/false. 11978 static std::optional<bool> isTautologicalBoundsCheck(Sema &S, const Expr *LHS, 11979 const Expr *RHS, 11980 BinaryOperatorKind Opc) { 11981 if (!LHS->getType()->isPointerType() || 11982 S.getLangOpts().PointerOverflowDefined) 11983 return std::nullopt; 11984 11985 // Canonicalize to >= or < predicate. 11986 switch (Opc) { 11987 case BO_GE: 11988 case BO_LT: 11989 break; 11990 case BO_GT: 11991 std::swap(LHS, RHS); 11992 Opc = BO_LT; 11993 break; 11994 case BO_LE: 11995 std::swap(LHS, RHS); 11996 Opc = BO_GE; 11997 break; 11998 default: 11999 return std::nullopt; 12000 } 12001 12002 auto *BO = dyn_cast<BinaryOperator>(LHS); 12003 if (!BO || BO->getOpcode() != BO_Add) 12004 return std::nullopt; 12005 12006 Expr *Other; 12007 if (Expr::isSameComparisonOperand(BO->getLHS(), RHS)) 12008 Other = BO->getRHS(); 12009 else if (Expr::isSameComparisonOperand(BO->getRHS(), RHS)) 12010 Other = BO->getLHS(); 12011 else 12012 return std::nullopt; 12013 12014 if (!Other->getType()->isUnsignedIntegerType()) 12015 return std::nullopt; 12016 12017 return Opc == BO_GE; 12018 } 12019 12020 /// Diagnose some forms of syntactically-obvious tautological comparison. 12021 static void diagnoseTautologicalComparison(Sema &S, SourceLocation Loc, 12022 Expr *LHS, Expr *RHS, 12023 BinaryOperatorKind Opc) { 12024 Expr *LHSStripped = LHS->IgnoreParenImpCasts(); 12025 Expr *RHSStripped = RHS->IgnoreParenImpCasts(); 12026 12027 QualType LHSType = LHS->getType(); 12028 QualType RHSType = RHS->getType(); 12029 if (LHSType->hasFloatingRepresentation() || 12030 (LHSType->isBlockPointerType() && !BinaryOperator::isEqualityOp(Opc)) || 12031 S.inTemplateInstantiation()) 12032 return; 12033 12034 // WebAssembly Tables cannot be compared, therefore shouldn't emit 12035 // Tautological diagnostics. 12036 if (LHSType->isWebAssemblyTableType() || RHSType->isWebAssemblyTableType()) 12037 return; 12038 12039 // Comparisons between two array types are ill-formed for operator<=>, so 12040 // we shouldn't emit any additional warnings about it. 12041 if (Opc == BO_Cmp && LHSType->isArrayType() && RHSType->isArrayType()) 12042 return; 12043 12044 // For non-floating point types, check for self-comparisons of the form 12045 // x == x, x != x, x < x, etc. These always evaluate to a constant, and 12046 // often indicate logic errors in the program. 12047 // 12048 // NOTE: Don't warn about comparison expressions resulting from macro 12049 // expansion. Also don't warn about comparisons which are only self 12050 // comparisons within a template instantiation. The warnings should catch 12051 // obvious cases in the definition of the template anyways. The idea is to 12052 // warn when the typed comparison operator will always evaluate to the same 12053 // result. 12054 12055 // Used for indexing into %select in warn_comparison_always 12056 enum { 12057 AlwaysConstant, 12058 AlwaysTrue, 12059 AlwaysFalse, 12060 AlwaysEqual, // std::strong_ordering::equal from operator<=> 12061 }; 12062 12063 // C++1a [array.comp]: 12064 // Equality and relational comparisons ([expr.eq], [expr.rel]) between two 12065 // operands of array type. 12066 // C++2a [depr.array.comp]: 12067 // Equality and relational comparisons ([expr.eq], [expr.rel]) between two 12068 // operands of array type are deprecated. 12069 if (S.getLangOpts().CPlusPlus && LHSStripped->getType()->isArrayType() && 12070 RHSStripped->getType()->isArrayType()) { 12071 auto IsDeprArrayComparionIgnored = 12072 S.getDiagnostics().isIgnored(diag::warn_depr_array_comparison, Loc); 12073 auto DiagID = S.getLangOpts().CPlusPlus26 12074 ? diag::warn_array_comparison_cxx26 12075 : !S.getLangOpts().CPlusPlus20 || IsDeprArrayComparionIgnored 12076 ? diag::warn_array_comparison 12077 : diag::warn_depr_array_comparison; 12078 S.Diag(Loc, DiagID) << LHS->getSourceRange() << RHS->getSourceRange() 12079 << LHSStripped->getType() << RHSStripped->getType(); 12080 // Carry on to produce the tautological comparison warning, if this 12081 // expression is potentially-evaluated, we can resolve the array to a 12082 // non-weak declaration, and so on. 12083 } 12084 12085 if (!LHS->getBeginLoc().isMacroID() && !RHS->getBeginLoc().isMacroID()) { 12086 if (Expr::isSameComparisonOperand(LHS, RHS)) { 12087 unsigned Result; 12088 switch (Opc) { 12089 case BO_EQ: 12090 case BO_LE: 12091 case BO_GE: 12092 Result = AlwaysTrue; 12093 break; 12094 case BO_NE: 12095 case BO_LT: 12096 case BO_GT: 12097 Result = AlwaysFalse; 12098 break; 12099 case BO_Cmp: 12100 Result = AlwaysEqual; 12101 break; 12102 default: 12103 Result = AlwaysConstant; 12104 break; 12105 } 12106 S.DiagRuntimeBehavior(Loc, nullptr, 12107 S.PDiag(diag::warn_comparison_always) 12108 << 0 /*self-comparison*/ 12109 << Result); 12110 } else if (checkForArray(LHSStripped) && checkForArray(RHSStripped)) { 12111 // What is it always going to evaluate to? 12112 unsigned Result; 12113 switch (Opc) { 12114 case BO_EQ: // e.g. array1 == array2 12115 Result = AlwaysFalse; 12116 break; 12117 case BO_NE: // e.g. array1 != array2 12118 Result = AlwaysTrue; 12119 break; 12120 default: // e.g. array1 <= array2 12121 // The best we can say is 'a constant' 12122 Result = AlwaysConstant; 12123 break; 12124 } 12125 S.DiagRuntimeBehavior(Loc, nullptr, 12126 S.PDiag(diag::warn_comparison_always) 12127 << 1 /*array comparison*/ 12128 << Result); 12129 } else if (std::optional<bool> Res = 12130 isTautologicalBoundsCheck(S, LHS, RHS, Opc)) { 12131 S.DiagRuntimeBehavior(Loc, nullptr, 12132 S.PDiag(diag::warn_comparison_always) 12133 << 2 /*pointer comparison*/ 12134 << (*Res ? AlwaysTrue : AlwaysFalse)); 12135 } 12136 } 12137 12138 if (isa<CastExpr>(LHSStripped)) 12139 LHSStripped = LHSStripped->IgnoreParenCasts(); 12140 if (isa<CastExpr>(RHSStripped)) 12141 RHSStripped = RHSStripped->IgnoreParenCasts(); 12142 12143 // Warn about comparisons against a string constant (unless the other 12144 // operand is null); the user probably wants string comparison function. 12145 Expr *LiteralString = nullptr; 12146 Expr *LiteralStringStripped = nullptr; 12147 if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && 12148 !RHSStripped->isNullPointerConstant(S.Context, 12149 Expr::NPC_ValueDependentIsNull)) { 12150 LiteralString = LHS; 12151 LiteralStringStripped = LHSStripped; 12152 } else if ((isa<StringLiteral>(RHSStripped) || 12153 isa<ObjCEncodeExpr>(RHSStripped)) && 12154 !LHSStripped->isNullPointerConstant(S.Context, 12155 Expr::NPC_ValueDependentIsNull)) { 12156 LiteralString = RHS; 12157 LiteralStringStripped = RHSStripped; 12158 } 12159 12160 if (LiteralString) { 12161 S.DiagRuntimeBehavior(Loc, nullptr, 12162 S.PDiag(diag::warn_stringcompare) 12163 << isa<ObjCEncodeExpr>(LiteralStringStripped) 12164 << LiteralString->getSourceRange()); 12165 } 12166 } 12167 12168 static ImplicitConversionKind castKindToImplicitConversionKind(CastKind CK) { 12169 switch (CK) { 12170 default: { 12171 #ifndef NDEBUG 12172 llvm::errs() << "unhandled cast kind: " << CastExpr::getCastKindName(CK) 12173 << "\n"; 12174 #endif 12175 llvm_unreachable("unhandled cast kind"); 12176 } 12177 case CK_UserDefinedConversion: 12178 return ICK_Identity; 12179 case CK_LValueToRValue: 12180 return ICK_Lvalue_To_Rvalue; 12181 case CK_ArrayToPointerDecay: 12182 return ICK_Array_To_Pointer; 12183 case CK_FunctionToPointerDecay: 12184 return ICK_Function_To_Pointer; 12185 case CK_IntegralCast: 12186 return ICK_Integral_Conversion; 12187 case CK_FloatingCast: 12188 return ICK_Floating_Conversion; 12189 case CK_IntegralToFloating: 12190 case CK_FloatingToIntegral: 12191 return ICK_Floating_Integral; 12192 case CK_IntegralComplexCast: 12193 case CK_FloatingComplexCast: 12194 case CK_FloatingComplexToIntegralComplex: 12195 case CK_IntegralComplexToFloatingComplex: 12196 return ICK_Complex_Conversion; 12197 case CK_FloatingComplexToReal: 12198 case CK_FloatingRealToComplex: 12199 case CK_IntegralComplexToReal: 12200 case CK_IntegralRealToComplex: 12201 return ICK_Complex_Real; 12202 case CK_HLSLArrayRValue: 12203 return ICK_HLSL_Array_RValue; 12204 } 12205 } 12206 12207 static bool checkThreeWayNarrowingConversion(Sema &S, QualType ToType, Expr *E, 12208 QualType FromType, 12209 SourceLocation Loc) { 12210 // Check for a narrowing implicit conversion. 12211 StandardConversionSequence SCS; 12212 SCS.setAsIdentityConversion(); 12213 SCS.setToType(0, FromType); 12214 SCS.setToType(1, ToType); 12215 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 12216 SCS.Second = castKindToImplicitConversionKind(ICE->getCastKind()); 12217 12218 APValue PreNarrowingValue; 12219 QualType PreNarrowingType; 12220 switch (SCS.getNarrowingKind(S.Context, E, PreNarrowingValue, 12221 PreNarrowingType, 12222 /*IgnoreFloatToIntegralConversion*/ true)) { 12223 case NK_Dependent_Narrowing: 12224 // Implicit conversion to a narrower type, but the expression is 12225 // value-dependent so we can't tell whether it's actually narrowing. 12226 case NK_Not_Narrowing: 12227 return false; 12228 12229 case NK_Constant_Narrowing: 12230 // Implicit conversion to a narrower type, and the value is not a constant 12231 // expression. 12232 S.Diag(E->getBeginLoc(), diag::err_spaceship_argument_narrowing) 12233 << /*Constant*/ 1 12234 << PreNarrowingValue.getAsString(S.Context, PreNarrowingType) << ToType; 12235 return true; 12236 12237 case NK_Variable_Narrowing: 12238 // Implicit conversion to a narrower type, and the value is not a constant 12239 // expression. 12240 case NK_Type_Narrowing: 12241 S.Diag(E->getBeginLoc(), diag::err_spaceship_argument_narrowing) 12242 << /*Constant*/ 0 << FromType << ToType; 12243 // TODO: It's not a constant expression, but what if the user intended it 12244 // to be? Can we produce notes to help them figure out why it isn't? 12245 return true; 12246 } 12247 llvm_unreachable("unhandled case in switch"); 12248 } 12249 12250 static QualType checkArithmeticOrEnumeralThreeWayCompare(Sema &S, 12251 ExprResult &LHS, 12252 ExprResult &RHS, 12253 SourceLocation Loc) { 12254 QualType LHSType = LHS.get()->getType(); 12255 QualType RHSType = RHS.get()->getType(); 12256 // Dig out the original argument type and expression before implicit casts 12257 // were applied. These are the types/expressions we need to check the 12258 // [expr.spaceship] requirements against. 12259 ExprResult LHSStripped = LHS.get()->IgnoreParenImpCasts(); 12260 ExprResult RHSStripped = RHS.get()->IgnoreParenImpCasts(); 12261 QualType LHSStrippedType = LHSStripped.get()->getType(); 12262 QualType RHSStrippedType = RHSStripped.get()->getType(); 12263 12264 // C++2a [expr.spaceship]p3: If one of the operands is of type bool and the 12265 // other is not, the program is ill-formed. 12266 if (LHSStrippedType->isBooleanType() != RHSStrippedType->isBooleanType()) { 12267 S.InvalidOperands(Loc, LHSStripped, RHSStripped); 12268 return QualType(); 12269 } 12270 12271 // FIXME: Consider combining this with checkEnumArithmeticConversions. 12272 int NumEnumArgs = (int)LHSStrippedType->isEnumeralType() + 12273 RHSStrippedType->isEnumeralType(); 12274 if (NumEnumArgs == 1) { 12275 bool LHSIsEnum = LHSStrippedType->isEnumeralType(); 12276 QualType OtherTy = LHSIsEnum ? RHSStrippedType : LHSStrippedType; 12277 if (OtherTy->hasFloatingRepresentation()) { 12278 S.InvalidOperands(Loc, LHSStripped, RHSStripped); 12279 return QualType(); 12280 } 12281 } 12282 if (NumEnumArgs == 2) { 12283 // C++2a [expr.spaceship]p5: If both operands have the same enumeration 12284 // type E, the operator yields the result of converting the operands 12285 // to the underlying type of E and applying <=> to the converted operands. 12286 if (!S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { 12287 S.InvalidOperands(Loc, LHS, RHS); 12288 return QualType(); 12289 } 12290 QualType IntType = 12291 LHSStrippedType->castAs<EnumType>()->getDecl()->getIntegerType(); 12292 assert(IntType->isArithmeticType()); 12293 12294 // We can't use `CK_IntegralCast` when the underlying type is 'bool', so we 12295 // promote the boolean type, and all other promotable integer types, to 12296 // avoid this. 12297 if (S.Context.isPromotableIntegerType(IntType)) 12298 IntType = S.Context.getPromotedIntegerType(IntType); 12299 12300 LHS = S.ImpCastExprToType(LHS.get(), IntType, CK_IntegralCast); 12301 RHS = S.ImpCastExprToType(RHS.get(), IntType, CK_IntegralCast); 12302 LHSType = RHSType = IntType; 12303 } 12304 12305 // C++2a [expr.spaceship]p4: If both operands have arithmetic types, the 12306 // usual arithmetic conversions are applied to the operands. 12307 QualType Type = 12308 S.UsualArithmeticConversions(LHS, RHS, Loc, ArithConvKind::Comparison); 12309 if (LHS.isInvalid() || RHS.isInvalid()) 12310 return QualType(); 12311 if (Type.isNull()) 12312 return S.InvalidOperands(Loc, LHS, RHS); 12313 12314 std::optional<ComparisonCategoryType> CCT = 12315 getComparisonCategoryForBuiltinCmp(Type); 12316 if (!CCT) 12317 return S.InvalidOperands(Loc, LHS, RHS); 12318 12319 bool HasNarrowing = checkThreeWayNarrowingConversion( 12320 S, Type, LHS.get(), LHSType, LHS.get()->getBeginLoc()); 12321 HasNarrowing |= checkThreeWayNarrowingConversion(S, Type, RHS.get(), RHSType, 12322 RHS.get()->getBeginLoc()); 12323 if (HasNarrowing) 12324 return QualType(); 12325 12326 assert(!Type.isNull() && "composite type for <=> has not been set"); 12327 12328 return S.CheckComparisonCategoryType( 12329 *CCT, Loc, Sema::ComparisonCategoryUsage::OperatorInExpression); 12330 } 12331 12332 static QualType checkArithmeticOrEnumeralCompare(Sema &S, ExprResult &LHS, 12333 ExprResult &RHS, 12334 SourceLocation Loc, 12335 BinaryOperatorKind Opc) { 12336 if (Opc == BO_Cmp) 12337 return checkArithmeticOrEnumeralThreeWayCompare(S, LHS, RHS, Loc); 12338 12339 // C99 6.5.8p3 / C99 6.5.9p4 12340 QualType Type = 12341 S.UsualArithmeticConversions(LHS, RHS, Loc, ArithConvKind::Comparison); 12342 if (LHS.isInvalid() || RHS.isInvalid()) 12343 return QualType(); 12344 if (Type.isNull()) 12345 return S.InvalidOperands(Loc, LHS, RHS); 12346 assert(Type->isArithmeticType() || Type->isEnumeralType()); 12347 12348 if (Type->isAnyComplexType() && BinaryOperator::isRelationalOp(Opc)) 12349 return S.InvalidOperands(Loc, LHS, RHS); 12350 12351 // Check for comparisons of floating point operands using != and ==. 12352 if (Type->hasFloatingRepresentation()) 12353 S.CheckFloatComparison(Loc, LHS.get(), RHS.get(), Opc); 12354 12355 // The result of comparisons is 'bool' in C++, 'int' in C. 12356 return S.Context.getLogicalOperationType(); 12357 } 12358 12359 void Sema::CheckPtrComparisonWithNullChar(ExprResult &E, ExprResult &NullE) { 12360 if (!NullE.get()->getType()->isAnyPointerType()) 12361 return; 12362 int NullValue = PP.isMacroDefined("NULL") ? 0 : 1; 12363 if (!E.get()->getType()->isAnyPointerType() && 12364 E.get()->isNullPointerConstant(Context, 12365 Expr::NPC_ValueDependentIsNotNull) == 12366 Expr::NPCK_ZeroExpression) { 12367 if (const auto *CL = dyn_cast<CharacterLiteral>(E.get())) { 12368 if (CL->getValue() == 0) 12369 Diag(E.get()->getExprLoc(), diag::warn_pointer_compare) 12370 << NullValue 12371 << FixItHint::CreateReplacement(E.get()->getExprLoc(), 12372 NullValue ? "NULL" : "(void *)0"); 12373 } else if (const auto *CE = dyn_cast<CStyleCastExpr>(E.get())) { 12374 TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); 12375 QualType T = Context.getCanonicalType(TI->getType()).getUnqualifiedType(); 12376 if (T == Context.CharTy) 12377 Diag(E.get()->getExprLoc(), diag::warn_pointer_compare) 12378 << NullValue 12379 << FixItHint::CreateReplacement(E.get()->getExprLoc(), 12380 NullValue ? "NULL" : "(void *)0"); 12381 } 12382 } 12383 } 12384 12385 // C99 6.5.8, C++ [expr.rel] 12386 QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, 12387 SourceLocation Loc, 12388 BinaryOperatorKind Opc) { 12389 bool IsRelational = BinaryOperator::isRelationalOp(Opc); 12390 bool IsThreeWay = Opc == BO_Cmp; 12391 bool IsOrdered = IsRelational || IsThreeWay; 12392 auto IsAnyPointerType = [](ExprResult E) { 12393 QualType Ty = E.get()->getType(); 12394 return Ty->isPointerType() || Ty->isMemberPointerType(); 12395 }; 12396 12397 // C++2a [expr.spaceship]p6: If at least one of the operands is of pointer 12398 // type, array-to-pointer, ..., conversions are performed on both operands to 12399 // bring them to their composite type. 12400 // Otherwise, all comparisons expect an rvalue, so convert to rvalue before 12401 // any type-related checks. 12402 if (!IsThreeWay || IsAnyPointerType(LHS) || IsAnyPointerType(RHS)) { 12403 LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); 12404 if (LHS.isInvalid()) 12405 return QualType(); 12406 RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); 12407 if (RHS.isInvalid()) 12408 return QualType(); 12409 } else { 12410 LHS = DefaultLvalueConversion(LHS.get()); 12411 if (LHS.isInvalid()) 12412 return QualType(); 12413 RHS = DefaultLvalueConversion(RHS.get()); 12414 if (RHS.isInvalid()) 12415 return QualType(); 12416 } 12417 12418 checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/true); 12419 if (!getLangOpts().CPlusPlus && BinaryOperator::isEqualityOp(Opc)) { 12420 CheckPtrComparisonWithNullChar(LHS, RHS); 12421 CheckPtrComparisonWithNullChar(RHS, LHS); 12422 } 12423 12424 // Handle vector comparisons separately. 12425 if (LHS.get()->getType()->isVectorType() || 12426 RHS.get()->getType()->isVectorType()) 12427 return CheckVectorCompareOperands(LHS, RHS, Loc, Opc); 12428 12429 if (LHS.get()->getType()->isSveVLSBuiltinType() || 12430 RHS.get()->getType()->isSveVLSBuiltinType()) 12431 return CheckSizelessVectorCompareOperands(LHS, RHS, Loc, Opc); 12432 12433 diagnoseLogicalNotOnLHSofCheck(*this, LHS, RHS, Loc, Opc); 12434 diagnoseTautologicalComparison(*this, Loc, LHS.get(), RHS.get(), Opc); 12435 12436 QualType LHSType = LHS.get()->getType(); 12437 QualType RHSType = RHS.get()->getType(); 12438 if ((LHSType->isArithmeticType() || LHSType->isEnumeralType()) && 12439 (RHSType->isArithmeticType() || RHSType->isEnumeralType())) 12440 return checkArithmeticOrEnumeralCompare(*this, LHS, RHS, Loc, Opc); 12441 12442 if ((LHSType->isPointerType() && 12443 LHSType->getPointeeType().isWebAssemblyReferenceType()) || 12444 (RHSType->isPointerType() && 12445 RHSType->getPointeeType().isWebAssemblyReferenceType())) 12446 return InvalidOperands(Loc, LHS, RHS); 12447 12448 const Expr::NullPointerConstantKind LHSNullKind = 12449 LHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull); 12450 const Expr::NullPointerConstantKind RHSNullKind = 12451 RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull); 12452 bool LHSIsNull = LHSNullKind != Expr::NPCK_NotNull; 12453 bool RHSIsNull = RHSNullKind != Expr::NPCK_NotNull; 12454 12455 auto computeResultTy = [&]() { 12456 if (Opc != BO_Cmp) 12457 return Context.getLogicalOperationType(); 12458 assert(getLangOpts().CPlusPlus); 12459 assert(Context.hasSameType(LHS.get()->getType(), RHS.get()->getType())); 12460 12461 QualType CompositeTy = LHS.get()->getType(); 12462 assert(!CompositeTy->isReferenceType()); 12463 12464 std::optional<ComparisonCategoryType> CCT = 12465 getComparisonCategoryForBuiltinCmp(CompositeTy); 12466 if (!CCT) 12467 return InvalidOperands(Loc, LHS, RHS); 12468 12469 if (CompositeTy->isPointerType() && LHSIsNull != RHSIsNull) { 12470 // P0946R0: Comparisons between a null pointer constant and an object 12471 // pointer result in std::strong_equality, which is ill-formed under 12472 // P1959R0. 12473 Diag(Loc, diag::err_typecheck_three_way_comparison_of_pointer_and_zero) 12474 << (LHSIsNull ? LHS.get()->getSourceRange() 12475 : RHS.get()->getSourceRange()); 12476 return QualType(); 12477 } 12478 12479 return CheckComparisonCategoryType( 12480 *CCT, Loc, ComparisonCategoryUsage::OperatorInExpression); 12481 }; 12482 12483 if (!IsOrdered && LHSIsNull != RHSIsNull) { 12484 bool IsEquality = Opc == BO_EQ; 12485 if (RHSIsNull) 12486 DiagnoseAlwaysNonNullPointer(LHS.get(), RHSNullKind, IsEquality, 12487 RHS.get()->getSourceRange()); 12488 else 12489 DiagnoseAlwaysNonNullPointer(RHS.get(), LHSNullKind, IsEquality, 12490 LHS.get()->getSourceRange()); 12491 } 12492 12493 if (IsOrdered && LHSType->isFunctionPointerType() && 12494 RHSType->isFunctionPointerType()) { 12495 // Valid unless a relational comparison of function pointers 12496 bool IsError = Opc == BO_Cmp; 12497 auto DiagID = 12498 IsError ? diag::err_typecheck_ordered_comparison_of_function_pointers 12499 : getLangOpts().CPlusPlus 12500 ? diag::warn_typecheck_ordered_comparison_of_function_pointers 12501 : diag::ext_typecheck_ordered_comparison_of_function_pointers; 12502 Diag(Loc, DiagID) << LHSType << RHSType << LHS.get()->getSourceRange() 12503 << RHS.get()->getSourceRange(); 12504 if (IsError) 12505 return QualType(); 12506 } 12507 12508 if ((LHSType->isIntegerType() && !LHSIsNull) || 12509 (RHSType->isIntegerType() && !RHSIsNull)) { 12510 // Skip normal pointer conversion checks in this case; we have better 12511 // diagnostics for this below. 12512 } else if (getLangOpts().CPlusPlus) { 12513 // Equality comparison of a function pointer to a void pointer is invalid, 12514 // but we allow it as an extension. 12515 // FIXME: If we really want to allow this, should it be part of composite 12516 // pointer type computation so it works in conditionals too? 12517 if (!IsOrdered && 12518 ((LHSType->isFunctionPointerType() && RHSType->isVoidPointerType()) || 12519 (RHSType->isFunctionPointerType() && LHSType->isVoidPointerType()))) { 12520 // This is a gcc extension compatibility comparison. 12521 // In a SFINAE context, we treat this as a hard error to maintain 12522 // conformance with the C++ standard. 12523 diagnoseFunctionPointerToVoidComparison( 12524 *this, Loc, LHS, RHS, /*isError*/ (bool)isSFINAEContext()); 12525 12526 if (isSFINAEContext()) 12527 return QualType(); 12528 12529 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); 12530 return computeResultTy(); 12531 } 12532 12533 // C++ [expr.eq]p2: 12534 // If at least one operand is a pointer [...] bring them to their 12535 // composite pointer type. 12536 // C++ [expr.spaceship]p6 12537 // If at least one of the operands is of pointer type, [...] bring them 12538 // to their composite pointer type. 12539 // C++ [expr.rel]p2: 12540 // If both operands are pointers, [...] bring them to their composite 12541 // pointer type. 12542 // For <=>, the only valid non-pointer types are arrays and functions, and 12543 // we already decayed those, so this is really the same as the relational 12544 // comparison rule. 12545 if ((int)LHSType->isPointerType() + (int)RHSType->isPointerType() >= 12546 (IsOrdered ? 2 : 1) && 12547 (!LangOpts.ObjCAutoRefCount || !(LHSType->isObjCObjectPointerType() || 12548 RHSType->isObjCObjectPointerType()))) { 12549 if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) 12550 return QualType(); 12551 return computeResultTy(); 12552 } 12553 } else if (LHSType->isPointerType() && 12554 RHSType->isPointerType()) { // C99 6.5.8p2 12555 // All of the following pointer-related warnings are GCC extensions, except 12556 // when handling null pointer constants. 12557 QualType LCanPointeeTy = 12558 LHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); 12559 QualType RCanPointeeTy = 12560 RHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); 12561 12562 // C99 6.5.9p2 and C99 6.5.8p2 12563 if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), 12564 RCanPointeeTy.getUnqualifiedType())) { 12565 if (IsRelational) { 12566 // Pointers both need to point to complete or incomplete types 12567 if ((LCanPointeeTy->isIncompleteType() != 12568 RCanPointeeTy->isIncompleteType()) && 12569 !getLangOpts().C11) { 12570 Diag(Loc, diag::ext_typecheck_compare_complete_incomplete_pointers) 12571 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange() 12572 << LHSType << RHSType << LCanPointeeTy->isIncompleteType() 12573 << RCanPointeeTy->isIncompleteType(); 12574 } 12575 } 12576 } else if (!IsRelational && 12577 (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { 12578 // Valid unless comparison between non-null pointer and function pointer 12579 if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) 12580 && !LHSIsNull && !RHSIsNull) 12581 diagnoseFunctionPointerToVoidComparison(*this, Loc, LHS, RHS, 12582 /*isError*/false); 12583 } else { 12584 // Invalid 12585 diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); 12586 } 12587 if (LCanPointeeTy != RCanPointeeTy) { 12588 // Treat NULL constant as a special case in OpenCL. 12589 if (getLangOpts().OpenCL && !LHSIsNull && !RHSIsNull) { 12590 if (!LCanPointeeTy.isAddressSpaceOverlapping(RCanPointeeTy, 12591 getASTContext())) { 12592 Diag(Loc, 12593 diag::err_typecheck_op_on_nonoverlapping_address_space_pointers) 12594 << LHSType << RHSType << 0 /* comparison */ 12595 << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); 12596 } 12597 } 12598 LangAS AddrSpaceL = LCanPointeeTy.getAddressSpace(); 12599 LangAS AddrSpaceR = RCanPointeeTy.getAddressSpace(); 12600 CastKind Kind = AddrSpaceL != AddrSpaceR ? CK_AddressSpaceConversion 12601 : CK_BitCast; 12602 12603 const FunctionType *LFn = LCanPointeeTy->getAs<FunctionType>(); 12604 const FunctionType *RFn = RCanPointeeTy->getAs<FunctionType>(); 12605 bool LHSHasCFIUncheckedCallee = LFn && LFn->getCFIUncheckedCalleeAttr(); 12606 bool RHSHasCFIUncheckedCallee = RFn && RFn->getCFIUncheckedCalleeAttr(); 12607 bool ChangingCFIUncheckedCallee = 12608 LHSHasCFIUncheckedCallee != RHSHasCFIUncheckedCallee; 12609 12610 if (LHSIsNull && !RHSIsNull) 12611 LHS = ImpCastExprToType(LHS.get(), RHSType, Kind); 12612 else if (!ChangingCFIUncheckedCallee) 12613 RHS = ImpCastExprToType(RHS.get(), LHSType, Kind); 12614 } 12615 return computeResultTy(); 12616 } 12617 12618 12619 // C++ [expr.eq]p4: 12620 // Two operands of type std::nullptr_t or one operand of type 12621 // std::nullptr_t and the other a null pointer constant compare 12622 // equal. 12623 // C23 6.5.9p5: 12624 // If both operands have type nullptr_t or one operand has type nullptr_t 12625 // and the other is a null pointer constant, they compare equal if the 12626 // former is a null pointer. 12627 if (!IsOrdered && LHSIsNull && RHSIsNull) { 12628 if (LHSType->isNullPtrType()) { 12629 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); 12630 return computeResultTy(); 12631 } 12632 if (RHSType->isNullPtrType()) { 12633 LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); 12634 return computeResultTy(); 12635 } 12636 } 12637 12638 if (!getLangOpts().CPlusPlus && !IsOrdered && (LHSIsNull || RHSIsNull)) { 12639 // C23 6.5.9p6: 12640 // Otherwise, at least one operand is a pointer. If one is a pointer and 12641 // the other is a null pointer constant or has type nullptr_t, they 12642 // compare equal 12643 if (LHSIsNull && RHSType->isPointerType()) { 12644 LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); 12645 return computeResultTy(); 12646 } 12647 if (RHSIsNull && LHSType->isPointerType()) { 12648 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); 12649 return computeResultTy(); 12650 } 12651 } 12652 12653 // Comparison of Objective-C pointers and block pointers against nullptr_t. 12654 // These aren't covered by the composite pointer type rules. 12655 if (!IsOrdered && RHSType->isNullPtrType() && 12656 (LHSType->isObjCObjectPointerType() || LHSType->isBlockPointerType())) { 12657 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); 12658 return computeResultTy(); 12659 } 12660 if (!IsOrdered && LHSType->isNullPtrType() && 12661 (RHSType->isObjCObjectPointerType() || RHSType->isBlockPointerType())) { 12662 LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); 12663 return computeResultTy(); 12664 } 12665 12666 if (getLangOpts().CPlusPlus) { 12667 if (IsRelational && 12668 ((LHSType->isNullPtrType() && RHSType->isPointerType()) || 12669 (RHSType->isNullPtrType() && LHSType->isPointerType()))) { 12670 // HACK: Relational comparison of nullptr_t against a pointer type is 12671 // invalid per DR583, but we allow it within std::less<> and friends, 12672 // since otherwise common uses of it break. 12673 // FIXME: Consider removing this hack once LWG fixes std::less<> and 12674 // friends to have std::nullptr_t overload candidates. 12675 DeclContext *DC = CurContext; 12676 if (isa<FunctionDecl>(DC)) 12677 DC = DC->getParent(); 12678 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 12679 if (CTSD->isInStdNamespace() && 12680 llvm::StringSwitch<bool>(CTSD->getName()) 12681 .Cases("less", "less_equal", "greater", "greater_equal", true) 12682 .Default(false)) { 12683 if (RHSType->isNullPtrType()) 12684 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); 12685 else 12686 LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); 12687 return computeResultTy(); 12688 } 12689 } 12690 } 12691 12692 // C++ [expr.eq]p2: 12693 // If at least one operand is a pointer to member, [...] bring them to 12694 // their composite pointer type. 12695 if (!IsOrdered && 12696 (LHSType->isMemberPointerType() || RHSType->isMemberPointerType())) { 12697 if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) 12698 return QualType(); 12699 else 12700 return computeResultTy(); 12701 } 12702 } 12703 12704 // Handle block pointer types. 12705 if (!IsOrdered && LHSType->isBlockPointerType() && 12706 RHSType->isBlockPointerType()) { 12707 QualType lpointee = LHSType->castAs<BlockPointerType>()->getPointeeType(); 12708 QualType rpointee = RHSType->castAs<BlockPointerType>()->getPointeeType(); 12709 12710 if (!LHSIsNull && !RHSIsNull && 12711 !Context.typesAreCompatible(lpointee, rpointee)) { 12712 Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) 12713 << LHSType << RHSType << LHS.get()->getSourceRange() 12714 << RHS.get()->getSourceRange(); 12715 } 12716 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); 12717 return computeResultTy(); 12718 } 12719 12720 // Allow block pointers to be compared with null pointer constants. 12721 if (!IsOrdered 12722 && ((LHSType->isBlockPointerType() && RHSType->isPointerType()) 12723 || (LHSType->isPointerType() && RHSType->isBlockPointerType()))) { 12724 if (!LHSIsNull && !RHSIsNull) { 12725 if (!((RHSType->isPointerType() && RHSType->castAs<PointerType>() 12726 ->getPointeeType()->isVoidType()) 12727 || (LHSType->isPointerType() && LHSType->castAs<PointerType>() 12728 ->getPointeeType()->isVoidType()))) 12729 Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) 12730 << LHSType << RHSType << LHS.get()->getSourceRange() 12731 << RHS.get()->getSourceRange(); 12732 } 12733 if (LHSIsNull && !RHSIsNull) 12734 LHS = ImpCastExprToType(LHS.get(), RHSType, 12735 RHSType->isPointerType() ? CK_BitCast 12736 : CK_AnyPointerToBlockPointerCast); 12737 else 12738 RHS = ImpCastExprToType(RHS.get(), LHSType, 12739 LHSType->isPointerType() ? CK_BitCast 12740 : CK_AnyPointerToBlockPointerCast); 12741 return computeResultTy(); 12742 } 12743 12744 if (LHSType->isObjCObjectPointerType() || 12745 RHSType->isObjCObjectPointerType()) { 12746 const PointerType *LPT = LHSType->getAs<PointerType>(); 12747 const PointerType *RPT = RHSType->getAs<PointerType>(); 12748 if (LPT || RPT) { 12749 bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; 12750 bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; 12751 12752 if (!LPtrToVoid && !RPtrToVoid && 12753 !Context.typesAreCompatible(LHSType, RHSType)) { 12754 diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, 12755 /*isError*/false); 12756 } 12757 // FIXME: If LPtrToVoid, we should presumably convert the LHS rather than 12758 // the RHS, but we have test coverage for this behavior. 12759 // FIXME: Consider using convertPointersToCompositeType in C++. 12760 if (LHSIsNull && !RHSIsNull) { 12761 Expr *E = LHS.get(); 12762 if (getLangOpts().ObjCAutoRefCount) 12763 ObjC().CheckObjCConversion(SourceRange(), RHSType, E, 12764 CheckedConversionKind::Implicit); 12765 LHS = ImpCastExprToType(E, RHSType, 12766 RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); 12767 } 12768 else { 12769 Expr *E = RHS.get(); 12770 if (getLangOpts().ObjCAutoRefCount) 12771 ObjC().CheckObjCConversion(SourceRange(), LHSType, E, 12772 CheckedConversionKind::Implicit, 12773 /*Diagnose=*/true, 12774 /*DiagnoseCFAudited=*/false, Opc); 12775 RHS = ImpCastExprToType(E, LHSType, 12776 LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); 12777 } 12778 return computeResultTy(); 12779 } 12780 if (LHSType->isObjCObjectPointerType() && 12781 RHSType->isObjCObjectPointerType()) { 12782 if (!Context.areComparableObjCPointerTypes(LHSType, RHSType)) 12783 diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, 12784 /*isError*/false); 12785 if (isObjCObjectLiteral(LHS) || isObjCObjectLiteral(RHS)) 12786 diagnoseObjCLiteralComparison(*this, Loc, LHS, RHS, Opc); 12787 12788 if (LHSIsNull && !RHSIsNull) 12789 LHS = ImpCastExprToType(LHS.get(), RHSType, CK_BitCast); 12790 else 12791 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_BitCast); 12792 return computeResultTy(); 12793 } 12794 12795 if (!IsOrdered && LHSType->isBlockPointerType() && 12796 RHSType->isBlockCompatibleObjCPointerType(Context)) { 12797 LHS = ImpCastExprToType(LHS.get(), RHSType, 12798 CK_BlockPointerToObjCPointerCast); 12799 return computeResultTy(); 12800 } else if (!IsOrdered && 12801 LHSType->isBlockCompatibleObjCPointerType(Context) && 12802 RHSType->isBlockPointerType()) { 12803 RHS = ImpCastExprToType(RHS.get(), LHSType, 12804 CK_BlockPointerToObjCPointerCast); 12805 return computeResultTy(); 12806 } 12807 } 12808 if ((LHSType->isAnyPointerType() && RHSType->isIntegerType()) || 12809 (LHSType->isIntegerType() && RHSType->isAnyPointerType())) { 12810 unsigned DiagID = 0; 12811 bool isError = false; 12812 if (LangOpts.DebuggerSupport) { 12813 // Under a debugger, allow the comparison of pointers to integers, 12814 // since users tend to want to compare addresses. 12815 } else if ((LHSIsNull && LHSType->isIntegerType()) || 12816 (RHSIsNull && RHSType->isIntegerType())) { 12817 if (IsOrdered) { 12818 isError = getLangOpts().CPlusPlus; 12819 DiagID = 12820 isError ? diag::err_typecheck_ordered_comparison_of_pointer_and_zero 12821 : diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; 12822 } 12823 } else if (getLangOpts().CPlusPlus) { 12824 DiagID = diag::err_typecheck_comparison_of_pointer_integer; 12825 isError = true; 12826 } else if (IsOrdered) 12827 DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; 12828 else 12829 DiagID = diag::ext_typecheck_comparison_of_pointer_integer; 12830 12831 if (DiagID) { 12832 Diag(Loc, DiagID) 12833 << LHSType << RHSType << LHS.get()->getSourceRange() 12834 << RHS.get()->getSourceRange(); 12835 if (isError) 12836 return QualType(); 12837 } 12838 12839 if (LHSType->isIntegerType()) 12840 LHS = ImpCastExprToType(LHS.get(), RHSType, 12841 LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); 12842 else 12843 RHS = ImpCastExprToType(RHS.get(), LHSType, 12844 RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); 12845 return computeResultTy(); 12846 } 12847 12848 // Handle block pointers. 12849 if (!IsOrdered && RHSIsNull 12850 && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { 12851 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); 12852 return computeResultTy(); 12853 } 12854 if (!IsOrdered && LHSIsNull 12855 && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { 12856 LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); 12857 return computeResultTy(); 12858 } 12859 12860 if (getLangOpts().getOpenCLCompatibleVersion() >= 200) { 12861 if (LHSType->isClkEventT() && RHSType->isClkEventT()) { 12862 return computeResultTy(); 12863 } 12864 12865 if (LHSType->isQueueT() && RHSType->isQueueT()) { 12866 return computeResultTy(); 12867 } 12868 12869 if (LHSIsNull && RHSType->isQueueT()) { 12870 LHS = ImpCastExprToType(LHS.get(), RHSType, CK_NullToPointer); 12871 return computeResultTy(); 12872 } 12873 12874 if (LHSType->isQueueT() && RHSIsNull) { 12875 RHS = ImpCastExprToType(RHS.get(), LHSType, CK_NullToPointer); 12876 return computeResultTy(); 12877 } 12878 } 12879 12880 return InvalidOperands(Loc, LHS, RHS); 12881 } 12882 12883 QualType Sema::GetSignedVectorType(QualType V) { 12884 const VectorType *VTy = V->castAs<VectorType>(); 12885 unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); 12886 12887 if (isa<ExtVectorType>(VTy)) { 12888 if (VTy->isExtVectorBoolType()) 12889 return Context.getExtVectorType(Context.BoolTy, VTy->getNumElements()); 12890 if (TypeSize == Context.getTypeSize(Context.CharTy)) 12891 return Context.getExtVectorType(Context.CharTy, VTy->getNumElements()); 12892 if (TypeSize == Context.getTypeSize(Context.ShortTy)) 12893 return Context.getExtVectorType(Context.ShortTy, VTy->getNumElements()); 12894 if (TypeSize == Context.getTypeSize(Context.IntTy)) 12895 return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); 12896 if (TypeSize == Context.getTypeSize(Context.Int128Ty)) 12897 return Context.getExtVectorType(Context.Int128Ty, VTy->getNumElements()); 12898 if (TypeSize == Context.getTypeSize(Context.LongTy)) 12899 return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); 12900 assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && 12901 "Unhandled vector element size in vector compare"); 12902 return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); 12903 } 12904 12905 if (TypeSize == Context.getTypeSize(Context.Int128Ty)) 12906 return Context.getVectorType(Context.Int128Ty, VTy->getNumElements(), 12907 VectorKind::Generic); 12908 if (TypeSize == Context.getTypeSize(Context.LongLongTy)) 12909 return Context.getVectorType(Context.LongLongTy, VTy->getNumElements(), 12910 VectorKind::Generic); 12911 if (TypeSize == Context.getTypeSize(Context.LongTy)) 12912 return Context.getVectorType(Context.LongTy, VTy->getNumElements(), 12913 VectorKind::Generic); 12914 if (TypeSize == Context.getTypeSize(Context.IntTy)) 12915 return Context.getVectorType(Context.IntTy, VTy->getNumElements(), 12916 VectorKind::Generic); 12917 if (TypeSize == Context.getTypeSize(Context.ShortTy)) 12918 return Context.getVectorType(Context.ShortTy, VTy->getNumElements(), 12919 VectorKind::Generic); 12920 assert(TypeSize == Context.getTypeSize(Context.CharTy) && 12921 "Unhandled vector element size in vector compare"); 12922 return Context.getVectorType(Context.CharTy, VTy->getNumElements(), 12923 VectorKind::Generic); 12924 } 12925 12926 QualType Sema::GetSignedSizelessVectorType(QualType V) { 12927 const BuiltinType *VTy = V->castAs<BuiltinType>(); 12928 assert(VTy->isSizelessBuiltinType() && "expected sizeless type"); 12929 12930 const QualType ETy = V->getSveEltType(Context); 12931 const auto TypeSize = Context.getTypeSize(ETy); 12932 12933 const QualType IntTy = Context.getIntTypeForBitwidth(TypeSize, true); 12934 const llvm::ElementCount VecSize = Context.getBuiltinVectorTypeInfo(VTy).EC; 12935 return Context.getScalableVectorType(IntTy, VecSize.getKnownMinValue()); 12936 } 12937 12938 QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, 12939 SourceLocation Loc, 12940 BinaryOperatorKind Opc) { 12941 if (Opc == BO_Cmp) { 12942 Diag(Loc, diag::err_three_way_vector_comparison); 12943 return QualType(); 12944 } 12945 12946 // Check to make sure we're operating on vectors of the same type and width, 12947 // Allowing one side to be a scalar of element type. 12948 QualType vType = 12949 CheckVectorOperands(LHS, RHS, Loc, /*isCompAssign*/ false, 12950 /*AllowBothBool*/ true, 12951 /*AllowBoolConversions*/ getLangOpts().ZVector, 12952 /*AllowBooleanOperation*/ true, 12953 /*ReportInvalid*/ true); 12954 if (vType.isNull()) 12955 return vType; 12956 12957 QualType LHSType = LHS.get()->getType(); 12958 12959 // Determine the return type of a vector compare. By default clang will return 12960 // a scalar for all vector compares except vector bool and vector pixel. 12961 // With the gcc compiler we will always return a vector type and with the xl 12962 // compiler we will always return a scalar type. This switch allows choosing 12963 // which behavior is prefered. 12964 if (getLangOpts().AltiVec) { 12965 switch (getLangOpts().getAltivecSrcCompat()) { 12966 case LangOptions::AltivecSrcCompatKind::Mixed: 12967 // If AltiVec, the comparison results in a numeric type, i.e. 12968 // bool for C++, int for C 12969 if (vType->castAs<VectorType>()->getVectorKind() == 12970 VectorKind::AltiVecVector) 12971 return Context.getLogicalOperationType(); 12972 else 12973 Diag(Loc, diag::warn_deprecated_altivec_src_compat); 12974 break; 12975 case LangOptions::AltivecSrcCompatKind::GCC: 12976 // For GCC we always return the vector type. 12977 break; 12978 case LangOptions::AltivecSrcCompatKind::XL: 12979 return Context.getLogicalOperationType(); 12980 break; 12981 } 12982 } 12983 12984 // For non-floating point types, check for self-comparisons of the form 12985 // x == x, x != x, x < x, etc. These always evaluate to a constant, and 12986 // often indicate logic errors in the program. 12987 diagnoseTautologicalComparison(*this, Loc, LHS.get(), RHS.get(), Opc); 12988 12989 // Check for comparisons of floating point operands using != and ==. 12990 if (LHSType->hasFloatingRepresentation()) { 12991 assert(RHS.get()->getType()->hasFloatingRepresentation()); 12992 CheckFloatComparison(Loc, LHS.get(), RHS.get(), Opc); 12993 } 12994 12995 // Return a signed type for the vector. 12996 return GetSignedVectorType(vType); 12997 } 12998 12999 QualType Sema::CheckSizelessVectorCompareOperands(ExprResult &LHS, 13000 ExprResult &RHS, 13001 SourceLocation Loc, 13002 BinaryOperatorKind Opc) { 13003 if (Opc == BO_Cmp) { 13004 Diag(Loc, diag::err_three_way_vector_comparison); 13005 return QualType(); 13006 } 13007 13008 // Check to make sure we're operating on vectors of the same type and width, 13009 // Allowing one side to be a scalar of element type. 13010 QualType vType = CheckSizelessVectorOperands( 13011 LHS, RHS, Loc, /*isCompAssign*/ false, ArithConvKind::Comparison); 13012 13013 if (vType.isNull()) 13014 return vType; 13015 13016 QualType LHSType = LHS.get()->getType(); 13017 13018 // For non-floating point types, check for self-comparisons of the form 13019 // x == x, x != x, x < x, etc. These always evaluate to a constant, and 13020 // often indicate logic errors in the program. 13021 diagnoseTautologicalComparison(*this, Loc, LHS.get(), RHS.get(), Opc); 13022 13023 // Check for comparisons of floating point operands using != and ==. 13024 if (LHSType->hasFloatingRepresentation()) { 13025 assert(RHS.get()->getType()->hasFloatingRepresentation()); 13026 CheckFloatComparison(Loc, LHS.get(), RHS.get(), Opc); 13027 } 13028 13029 const BuiltinType *LHSBuiltinTy = LHSType->getAs<BuiltinType>(); 13030 const BuiltinType *RHSBuiltinTy = RHS.get()->getType()->getAs<BuiltinType>(); 13031 13032 if (LHSBuiltinTy && RHSBuiltinTy && LHSBuiltinTy->isSVEBool() && 13033 RHSBuiltinTy->isSVEBool()) 13034 return LHSType; 13035 13036 // Return a signed type for the vector. 13037 return GetSignedSizelessVectorType(vType); 13038 } 13039 13040 static void diagnoseXorMisusedAsPow(Sema &S, const ExprResult &XorLHS, 13041 const ExprResult &XorRHS, 13042 const SourceLocation Loc) { 13043 // Do not diagnose macros. 13044 if (Loc.isMacroID()) 13045 return; 13046 13047 // Do not diagnose if both LHS and RHS are macros. 13048 if (XorLHS.get()->getExprLoc().isMacroID() && 13049 XorRHS.get()->getExprLoc().isMacroID()) 13050 return; 13051 13052 bool Negative = false; 13053 bool ExplicitPlus = false; 13054 const auto *LHSInt = dyn_cast<IntegerLiteral>(XorLHS.get()); 13055 const auto *RHSInt = dyn_cast<IntegerLiteral>(XorRHS.get()); 13056 13057 if (!LHSInt) 13058 return; 13059 if (!RHSInt) { 13060 // Check negative literals. 13061 if (const auto *UO = dyn_cast<UnaryOperator>(XorRHS.get())) { 13062 UnaryOperatorKind Opc = UO->getOpcode(); 13063 if (Opc != UO_Minus && Opc != UO_Plus) 13064 return; 13065 RHSInt = dyn_cast<IntegerLiteral>(UO->getSubExpr()); 13066 if (!RHSInt) 13067 return; 13068 Negative = (Opc == UO_Minus); 13069 ExplicitPlus = !Negative; 13070 } else { 13071 return; 13072 } 13073 } 13074 13075 const llvm::APInt &LeftSideValue = LHSInt->getValue(); 13076 llvm::APInt RightSideValue = RHSInt->getValue(); 13077 if (LeftSideValue != 2 && LeftSideValue != 10) 13078 return; 13079 13080 if (LeftSideValue.getBitWidth() != RightSideValue.getBitWidth()) 13081 return; 13082 13083 CharSourceRange ExprRange = CharSourceRange::getCharRange( 13084 LHSInt->getBeginLoc(), S.getLocForEndOfToken(RHSInt->getLocation())); 13085 llvm::StringRef ExprStr = 13086 Lexer::getSourceText(ExprRange, S.getSourceManager(), S.getLangOpts()); 13087 13088 CharSourceRange XorRange = 13089 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); 13090 llvm::StringRef XorStr = 13091 Lexer::getSourceText(XorRange, S.getSourceManager(), S.getLangOpts()); 13092 // Do not diagnose if xor keyword/macro is used. 13093 if (XorStr == "xor") 13094 return; 13095 13096 std::string LHSStr = std::string(Lexer::getSourceText( 13097 CharSourceRange::getTokenRange(LHSInt->getSourceRange()), 13098 S.getSourceManager(), S.getLangOpts())); 13099 std::string RHSStr = std::string(Lexer::getSourceText( 13100 CharSourceRange::getTokenRange(RHSInt->getSourceRange()), 13101 S.getSourceManager(), S.getLangOpts())); 13102 13103 if (Negative) { 13104 RightSideValue = -RightSideValue; 13105 RHSStr = "-" + RHSStr; 13106 } else if (ExplicitPlus) { 13107 RHSStr = "+" + RHSStr; 13108 } 13109 13110 StringRef LHSStrRef = LHSStr; 13111 StringRef RHSStrRef = RHSStr; 13112 // Do not diagnose literals with digit separators, binary, hexadecimal, octal 13113 // literals. 13114 if (LHSStrRef.starts_with("0b") || LHSStrRef.starts_with("0B") || 13115 RHSStrRef.starts_with("0b") || RHSStrRef.starts_with("0B") || 13116 LHSStrRef.starts_with("0x") || LHSStrRef.starts_with("0X") || 13117 RHSStrRef.starts_with("0x") || RHSStrRef.starts_with("0X") || 13118 (LHSStrRef.size() > 1 && LHSStrRef.starts_with("0")) || 13119 (RHSStrRef.size() > 1 && RHSStrRef.starts_with("0")) || 13120 LHSStrRef.contains('\'') || RHSStrRef.contains('\'')) 13121 return; 13122 13123 bool SuggestXor = 13124 S.getLangOpts().CPlusPlus || S.getPreprocessor().isMacroDefined("xor"); 13125 const llvm::APInt XorValue = LeftSideValue ^ RightSideValue; 13126 int64_t RightSideIntValue = RightSideValue.getSExtValue(); 13127 if (LeftSideValue == 2 && RightSideIntValue >= 0) { 13128 std::string SuggestedExpr = "1 << " + RHSStr; 13129 bool Overflow = false; 13130 llvm::APInt One = (LeftSideValue - 1); 13131 llvm::APInt PowValue = One.sshl_ov(RightSideValue, Overflow); 13132 if (Overflow) { 13133 if (RightSideIntValue < 64) 13134 S.Diag(Loc, diag::warn_xor_used_as_pow_base) 13135 << ExprStr << toString(XorValue, 10, true) << ("1LL << " + RHSStr) 13136 << FixItHint::CreateReplacement(ExprRange, "1LL << " + RHSStr); 13137 else if (RightSideIntValue == 64) 13138 S.Diag(Loc, diag::warn_xor_used_as_pow) 13139 << ExprStr << toString(XorValue, 10, true); 13140 else 13141 return; 13142 } else { 13143 S.Diag(Loc, diag::warn_xor_used_as_pow_base_extra) 13144 << ExprStr << toString(XorValue, 10, true) << SuggestedExpr 13145 << toString(PowValue, 10, true) 13146 << FixItHint::CreateReplacement( 13147 ExprRange, (RightSideIntValue == 0) ? "1" : SuggestedExpr); 13148 } 13149 13150 S.Diag(Loc, diag::note_xor_used_as_pow_silence) 13151 << ("0x2 ^ " + RHSStr) << SuggestXor; 13152 } else if (LeftSideValue == 10) { 13153 std::string SuggestedValue = "1e" + std::to_string(RightSideIntValue); 13154 S.Diag(Loc, diag::warn_xor_used_as_pow_base) 13155 << ExprStr << toString(XorValue, 10, true) << SuggestedValue 13156 << FixItHint::CreateReplacement(ExprRange, SuggestedValue); 13157 S.Diag(Loc, diag::note_xor_used_as_pow_silence) 13158 << ("0xA ^ " + RHSStr) << SuggestXor; 13159 } 13160 } 13161 13162 QualType Sema::CheckVectorLogicalOperands(ExprResult &LHS, ExprResult &RHS, 13163 SourceLocation Loc, 13164 BinaryOperatorKind Opc) { 13165 // Ensure that either both operands are of the same vector type, or 13166 // one operand is of a vector type and the other is of its element type. 13167 QualType vType = CheckVectorOperands(LHS, RHS, Loc, false, 13168 /*AllowBothBool*/ true, 13169 /*AllowBoolConversions*/ false, 13170 /*AllowBooleanOperation*/ false, 13171 /*ReportInvalid*/ false); 13172 if (vType.isNull()) 13173 return InvalidOperands(Loc, LHS, RHS); 13174 if (getLangOpts().OpenCL && 13175 getLangOpts().getOpenCLCompatibleVersion() < 120 && 13176 vType->hasFloatingRepresentation()) 13177 return InvalidOperands(Loc, LHS, RHS); 13178 // FIXME: The check for C++ here is for GCC compatibility. GCC rejects the 13179 // usage of the logical operators && and || with vectors in C. This 13180 // check could be notionally dropped. 13181 if (!getLangOpts().CPlusPlus && 13182 !(isa<ExtVectorType>(vType->getAs<VectorType>()))) 13183 return InvalidLogicalVectorOperands(Loc, LHS, RHS); 13184 // Beginning with HLSL 2021, HLSL disallows logical operators on vector 13185 // operands and instead requires the use of the `and`, `or`, `any`, `all`, and 13186 // `select` functions. 13187 if (getLangOpts().HLSL && 13188 getLangOpts().getHLSLVersion() >= LangOptionsBase::HLSL_2021) { 13189 (void)InvalidOperands(Loc, LHS, RHS); 13190 HLSL().emitLogicalOperatorFixIt(LHS.get(), RHS.get(), Opc); 13191 return QualType(); 13192 } 13193 13194 return GetSignedVectorType(LHS.get()->getType()); 13195 } 13196 13197 QualType Sema::CheckMatrixElementwiseOperands(ExprResult &LHS, ExprResult &RHS, 13198 SourceLocation Loc, 13199 bool IsCompAssign) { 13200 if (!IsCompAssign) { 13201 LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); 13202 if (LHS.isInvalid()) 13203 return QualType(); 13204 } 13205 RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); 13206 if (RHS.isInvalid()) 13207 return QualType(); 13208 13209 // For conversion purposes, we ignore any qualifiers. 13210 // For example, "const float" and "float" are equivalent. 13211 QualType LHSType = LHS.get()->getType().getUnqualifiedType(); 13212 QualType RHSType = RHS.get()->getType().getUnqualifiedType(); 13213 13214 const MatrixType *LHSMatType = LHSType->getAs<MatrixType>(); 13215 const MatrixType *RHSMatType = RHSType->getAs<MatrixType>(); 13216 assert((LHSMatType || RHSMatType) && "At least one operand must be a matrix"); 13217 13218 if (Context.hasSameType(LHSType, RHSType)) 13219 return Context.getCommonSugaredType(LHSType, RHSType); 13220 13221 // Type conversion may change LHS/RHS. Keep copies to the original results, in 13222 // case we have to return InvalidOperands. 13223 ExprResult OriginalLHS = LHS; 13224 ExprResult OriginalRHS = RHS; 13225 if (LHSMatType && !RHSMatType) { 13226 RHS = tryConvertExprToType(RHS.get(), LHSMatType->getElementType()); 13227 if (!RHS.isInvalid()) 13228 return LHSType; 13229 13230 return InvalidOperands(Loc, OriginalLHS, OriginalRHS); 13231 } 13232 13233 if (!LHSMatType && RHSMatType) { 13234 LHS = tryConvertExprToType(LHS.get(), RHSMatType->getElementType()); 13235 if (!LHS.isInvalid()) 13236 return RHSType; 13237 return InvalidOperands(Loc, OriginalLHS, OriginalRHS); 13238 } 13239 13240 return InvalidOperands(Loc, LHS, RHS); 13241 } 13242 13243 QualType Sema::CheckMatrixMultiplyOperands(ExprResult &LHS, ExprResult &RHS, 13244 SourceLocation Loc, 13245 bool IsCompAssign) { 13246 if (!IsCompAssign) { 13247 LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); 13248 if (LHS.isInvalid()) 13249 return QualType(); 13250 } 13251 RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); 13252 if (RHS.isInvalid()) 13253 return QualType(); 13254 13255 auto *LHSMatType = LHS.get()->getType()->getAs<ConstantMatrixType>(); 13256 auto *RHSMatType = RHS.get()->getType()->getAs<ConstantMatrixType>(); 13257 assert((LHSMatType || RHSMatType) && "At least one operand must be a matrix"); 13258 13259 if (LHSMatType && RHSMatType) { 13260 if (LHSMatType->getNumColumns() != RHSMatType->getNumRows()) 13261 return InvalidOperands(Loc, LHS, RHS); 13262 13263 if (Context.hasSameType(LHSMatType, RHSMatType)) 13264 return Context.getCommonSugaredType( 13265 LHS.get()->getType().getUnqualifiedType(), 13266 RHS.get()->getType().getUnqualifiedType()); 13267 13268 QualType LHSELTy = LHSMatType->getElementType(), 13269 RHSELTy = RHSMatType->getElementType(); 13270 if (!Context.hasSameType(LHSELTy, RHSELTy)) 13271 return InvalidOperands(Loc, LHS, RHS); 13272 13273 return Context.getConstantMatrixType( 13274 Context.getCommonSugaredType(LHSELTy, RHSELTy), 13275 LHSMatType->getNumRows(), RHSMatType->getNumColumns()); 13276 } 13277 return CheckMatrixElementwiseOperands(LHS, RHS, Loc, IsCompAssign); 13278 } 13279 13280 static bool isLegalBoolVectorBinaryOp(BinaryOperatorKind Opc) { 13281 switch (Opc) { 13282 default: 13283 return false; 13284 case BO_And: 13285 case BO_AndAssign: 13286 case BO_Or: 13287 case BO_OrAssign: 13288 case BO_Xor: 13289 case BO_XorAssign: 13290 return true; 13291 } 13292 } 13293 13294 inline QualType Sema::CheckBitwiseOperands(ExprResult &LHS, ExprResult &RHS, 13295 SourceLocation Loc, 13296 BinaryOperatorKind Opc) { 13297 checkArithmeticNull(*this, LHS, RHS, Loc, /*IsCompare=*/false); 13298 13299 bool IsCompAssign = 13300 Opc == BO_AndAssign || Opc == BO_OrAssign || Opc == BO_XorAssign; 13301 13302 bool LegalBoolVecOperator = isLegalBoolVectorBinaryOp(Opc); 13303 13304 if (LHS.get()->getType()->isVectorType() || 13305 RHS.get()->getType()->isVectorType()) { 13306 if (LHS.get()->getType()->hasIntegerRepresentation() && 13307 RHS.get()->getType()->hasIntegerRepresentation()) 13308 return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign, 13309 /*AllowBothBool*/ true, 13310 /*AllowBoolConversions*/ getLangOpts().ZVector, 13311 /*AllowBooleanOperation*/ LegalBoolVecOperator, 13312 /*ReportInvalid*/ true); 13313 return InvalidOperands(Loc, LHS, RHS); 13314 } 13315 13316 if (LHS.get()->getType()->isSveVLSBuiltinType() || 13317 RHS.get()->getType()->isSveVLSBuiltinType()) { 13318 if (LHS.get()->getType()->hasIntegerRepresentation() && 13319 RHS.get()->getType()->hasIntegerRepresentation()) 13320 return CheckSizelessVectorOperands(LHS, RHS, Loc, IsCompAssign, 13321 ArithConvKind::BitwiseOp); 13322 return InvalidOperands(Loc, LHS, RHS); 13323 } 13324 13325 if (LHS.get()->getType()->isSveVLSBuiltinType() || 13326 RHS.get()->getType()->isSveVLSBuiltinType()) { 13327 if (LHS.get()->getType()->hasIntegerRepresentation() && 13328 RHS.get()->getType()->hasIntegerRepresentation()) 13329 return CheckSizelessVectorOperands(LHS, RHS, Loc, IsCompAssign, 13330 ArithConvKind::BitwiseOp); 13331 return InvalidOperands(Loc, LHS, RHS); 13332 } 13333 13334 if (Opc == BO_And) 13335 diagnoseLogicalNotOnLHSofCheck(*this, LHS, RHS, Loc, Opc); 13336 13337 if (LHS.get()->getType()->hasFloatingRepresentation() || 13338 RHS.get()->getType()->hasFloatingRepresentation()) 13339 return InvalidOperands(Loc, LHS, RHS); 13340 13341 ExprResult LHSResult = LHS, RHSResult = RHS; 13342 QualType compType = UsualArithmeticConversions( 13343 LHSResult, RHSResult, Loc, 13344 IsCompAssign ? ArithConvKind::CompAssign : ArithConvKind::BitwiseOp); 13345 if (LHSResult.isInvalid() || RHSResult.isInvalid()) 13346 return QualType(); 13347 LHS = LHSResult.get(); 13348 RHS = RHSResult.get(); 13349 13350 if (Opc == BO_Xor) 13351 diagnoseXorMisusedAsPow(*this, LHS, RHS, Loc); 13352 13353 if (!compType.isNull() && compType->isIntegralOrUnscopedEnumerationType()) 13354 return compType; 13355 return InvalidOperands(Loc, LHS, RHS); 13356 } 13357 13358 // C99 6.5.[13,14] 13359 inline QualType Sema::CheckLogicalOperands(ExprResult &LHS, ExprResult &RHS, 13360 SourceLocation Loc, 13361 BinaryOperatorKind Opc) { 13362 // Check vector operands differently. 13363 if (LHS.get()->getType()->isVectorType() || 13364 RHS.get()->getType()->isVectorType()) 13365 return CheckVectorLogicalOperands(LHS, RHS, Loc, Opc); 13366 13367 bool EnumConstantInBoolContext = false; 13368 for (const ExprResult &HS : {LHS, RHS}) { 13369 if (const auto *DREHS = dyn_cast<DeclRefExpr>(HS.get())) { 13370 const auto *ECDHS = dyn_cast<EnumConstantDecl>(DREHS->getDecl()); 13371 if (ECDHS && ECDHS->getInitVal() != 0 && ECDHS->getInitVal() != 1) 13372 EnumConstantInBoolContext = true; 13373 } 13374 } 13375 13376 if (EnumConstantInBoolContext) 13377 Diag(Loc, diag::warn_enum_constant_in_bool_context); 13378 13379 // WebAssembly tables can't be used with logical operators. 13380 QualType LHSTy = LHS.get()->getType(); 13381 QualType RHSTy = RHS.get()->getType(); 13382 const auto *LHSATy = dyn_cast<ArrayType>(LHSTy); 13383 const auto *RHSATy = dyn_cast<ArrayType>(RHSTy); 13384 if ((LHSATy && LHSATy->getElementType().isWebAssemblyReferenceType()) || 13385 (RHSATy && RHSATy->getElementType().isWebAssemblyReferenceType())) { 13386 return InvalidOperands(Loc, LHS, RHS); 13387 } 13388 13389 // Diagnose cases where the user write a logical and/or but probably meant a 13390 // bitwise one. We do this when the LHS is a non-bool integer and the RHS 13391 // is a constant. 13392 if (!EnumConstantInBoolContext && LHS.get()->getType()->isIntegerType() && 13393 !LHS.get()->getType()->isBooleanType() && 13394 RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() && 13395 // Don't warn in macros or template instantiations. 13396 !Loc.isMacroID() && !inTemplateInstantiation()) { 13397 // If the RHS can be constant folded, and if it constant folds to something 13398 // that isn't 0 or 1 (which indicate a potential logical operation that 13399 // happened to fold to true/false) then warn. 13400 // Parens on the RHS are ignored. 13401 Expr::EvalResult EVResult; 13402 if (RHS.get()->EvaluateAsInt(EVResult, Context)) { 13403 llvm::APSInt Result = EVResult.Val.getInt(); 13404 if ((getLangOpts().CPlusPlus && !RHS.get()->getType()->isBooleanType() && 13405 !RHS.get()->getExprLoc().isMacroID()) || 13406 (Result != 0 && Result != 1)) { 13407 Diag(Loc, diag::warn_logical_instead_of_bitwise) 13408 << RHS.get()->getSourceRange() << (Opc == BO_LAnd ? "&&" : "||"); 13409 // Suggest replacing the logical operator with the bitwise version 13410 Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) 13411 << (Opc == BO_LAnd ? "&" : "|") 13412 << FixItHint::CreateReplacement( 13413 SourceRange(Loc, getLocForEndOfToken(Loc)), 13414 Opc == BO_LAnd ? "&" : "|"); 13415 if (Opc == BO_LAnd) 13416 // Suggest replacing "Foo() && kNonZero" with "Foo()" 13417 Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) 13418 << FixItHint::CreateRemoval( 13419 SourceRange(getLocForEndOfToken(LHS.get()->getEndLoc()), 13420 RHS.get()->getEndLoc())); 13421 } 13422 } 13423 } 13424 13425 if (!Context.getLangOpts().CPlusPlus) { 13426 // OpenCL v1.1 s6.3.g: The logical operators and (&&), or (||) do 13427 // not operate on the built-in scalar and vector float types. 13428 if (Context.getLangOpts().OpenCL && 13429 Context.getLangOpts().OpenCLVersion < 120) { 13430 if (LHS.get()->getType()->isFloatingType() || 13431 RHS.get()->getType()->isFloatingType()) 13432 return InvalidOperands(Loc, LHS, RHS); 13433 } 13434 13435 LHS = UsualUnaryConversions(LHS.get()); 13436 if (LHS.isInvalid()) 13437 return QualType(); 13438 13439 RHS = UsualUnaryConversions(RHS.get()); 13440 if (RHS.isInvalid()) 13441 return QualType(); 13442 13443 if (!LHS.get()->getType()->isScalarType() || 13444 !RHS.get()->getType()->isScalarType()) 13445 return InvalidOperands(Loc, LHS, RHS); 13446 13447 return Context.IntTy; 13448 } 13449 13450 // The following is safe because we only use this method for 13451 // non-overloadable operands. 13452 13453 // C++ [expr.log.and]p1 13454 // C++ [expr.log.or]p1 13455 // The operands are both contextually converted to type bool. 13456 ExprResult LHSRes = PerformContextuallyConvertToBool(LHS.get()); 13457 if (LHSRes.isInvalid()) 13458 return InvalidOperands(Loc, LHS, RHS); 13459 LHS = LHSRes; 13460 13461 ExprResult RHSRes = PerformContextuallyConvertToBool(RHS.get()); 13462 if (RHSRes.isInvalid()) 13463 return InvalidOperands(Loc, LHS, RHS); 13464 RHS = RHSRes; 13465 13466 // C++ [expr.log.and]p2 13467 // C++ [expr.log.or]p2 13468 // The result is a bool. 13469 return Context.BoolTy; 13470 } 13471 13472 static bool IsReadonlyMessage(Expr *E, Sema &S) { 13473 const MemberExpr *ME = dyn_cast<MemberExpr>(E); 13474 if (!ME) return false; 13475 if (!isa<FieldDecl>(ME->getMemberDecl())) return false; 13476 ObjCMessageExpr *Base = dyn_cast<ObjCMessageExpr>( 13477 ME->getBase()->IgnoreImplicit()->IgnoreParenImpCasts()); 13478 if (!Base) return false; 13479 return Base->getMethodDecl() != nullptr; 13480 } 13481 13482 /// Is the given expression (which must be 'const') a reference to a 13483 /// variable which was originally non-const, but which has become 13484 /// 'const' due to being captured within a block? 13485 enum NonConstCaptureKind { NCCK_None, NCCK_Block, NCCK_Lambda }; 13486 static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) { 13487 assert(E->isLValue() && E->getType().isConstQualified()); 13488 E = E->IgnoreParens(); 13489 13490 // Must be a reference to a declaration from an enclosing scope. 13491 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); 13492 if (!DRE) return NCCK_None; 13493 if (!DRE->refersToEnclosingVariableOrCapture()) return NCCK_None; 13494 13495 ValueDecl *Value = dyn_cast<ValueDecl>(DRE->getDecl()); 13496 13497 // The declaration must be a value which is not declared 'const'. 13498 if (!Value || Value->getType().isConstQualified()) 13499 return NCCK_None; 13500 13501 BindingDecl *Binding = dyn_cast<BindingDecl>(Value); 13502 if (Binding) { 13503 assert(S.getLangOpts().CPlusPlus && "BindingDecl outside of C++?"); 13504 assert(!isa<BlockDecl>(Binding->getDeclContext())); 13505 return NCCK_Lambda; 13506 } 13507 13508 VarDecl *Var = dyn_cast<VarDecl>(Value); 13509 if (!Var) 13510 return NCCK_None; 13511 13512 assert(Var->hasLocalStorage() && "capture added 'const' to non-local?"); 13513 13514 // Decide whether the first capture was for a block or a lambda. 13515 DeclContext *DC = S.CurContext, *Prev = nullptr; 13516 // Decide whether the first capture was for a block or a lambda. 13517 while (DC) { 13518 // For init-capture, it is possible that the variable belongs to the 13519 // template pattern of the current context. 13520 if (auto *FD = dyn_cast<FunctionDecl>(DC)) 13521 if (Var->isInitCapture() && 13522 FD->getTemplateInstantiationPattern() == Var->getDeclContext()) 13523 break; 13524 if (DC == Var->getDeclContext()) 13525 break; 13526 Prev = DC; 13527 DC = DC->getParent(); 13528 } 13529 // Unless we have an init-capture, we've gone one step too far. 13530 if (!Var->isInitCapture()) 13531 DC = Prev; 13532 return (isa<BlockDecl>(DC) ? NCCK_Block : NCCK_Lambda); 13533 } 13534 13535 static bool IsTypeModifiable(QualType Ty, bool IsDereference) { 13536 Ty = Ty.getNonReferenceType(); 13537 if (IsDereference && Ty->isPointerType()) 13538 Ty = Ty->getPointeeType(); 13539 return !Ty.isConstQualified(); 13540 } 13541 13542 // Update err_typecheck_assign_const and note_typecheck_assign_const 13543 // when this enum is changed. 13544 enum { 13545 ConstFunction, 13546 ConstVariable, 13547 ConstMember, 13548 ConstMethod, 13549 NestedConstMember, 13550 ConstUnknown, // Keep as last element 13551 }; 13552 13553 /// Emit the "read-only variable not assignable" error and print notes to give 13554 /// more information about why the variable is not assignable, such as pointing 13555 /// to the declaration of a const variable, showing that a method is const, or 13556 /// that the function is returning a const reference. 13557 static void DiagnoseConstAssignment(Sema &S, const Expr *E, 13558 SourceLocation Loc) { 13559 SourceRange ExprRange = E->getSourceRange(); 13560 13561 // Only emit one error on the first const found. All other consts will emit 13562 // a note to the error. 13563 bool DiagnosticEmitted = false; 13564 13565 // Track if the current expression is the result of a dereference, and if the 13566 // next checked expression is the result of a dereference. 13567 bool IsDereference = false; 13568 bool NextIsDereference = false; 13569 13570 // Loop to process MemberExpr chains. 13571 while (true) { 13572 IsDereference = NextIsDereference; 13573 13574 E = E->IgnoreImplicit()->IgnoreParenImpCasts(); 13575 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 13576 NextIsDereference = ME->isArrow(); 13577 const ValueDecl *VD = ME->getMemberDecl(); 13578 if (const FieldDecl *Field = dyn_cast<FieldDecl>(VD)) { 13579 // Mutable fields can be modified even if the class is const. 13580 if (Field->isMutable()) { 13581 assert(DiagnosticEmitted && "Expected diagnostic not emitted."); 13582 break; 13583 } 13584 13585 if (!IsTypeModifiable(Field->getType(), IsDereference)) { 13586 if (!DiagnosticEmitted) { 13587 S.Diag(Loc, diag::err_typecheck_assign_const) 13588 << ExprRange << ConstMember << false /*static*/ << Field 13589 << Field->getType(); 13590 DiagnosticEmitted = true; 13591 } 13592 S.Diag(VD->getLocation(), diag::note_typecheck_assign_const) 13593 << ConstMember << false /*static*/ << Field << Field->getType() 13594 << Field->getSourceRange(); 13595 } 13596 E = ME->getBase(); 13597 continue; 13598 } else if (const VarDecl *VDecl = dyn_cast<VarDecl>(VD)) { 13599 if (VDecl->getType().isConstQualified()) { 13600 if (!DiagnosticEmitted) { 13601 S.Diag(Loc, diag::err_typecheck_assign_const) 13602 << ExprRange << ConstMember << true /*static*/ << VDecl 13603 << VDecl->getType(); 13604 DiagnosticEmitted = true; 13605 } 13606 S.Diag(VD->getLocation(), diag::note_typecheck_assign_const) 13607 << ConstMember << true /*static*/ << VDecl << VDecl->getType() 13608 << VDecl->getSourceRange(); 13609 } 13610 // Static fields do not inherit constness from parents. 13611 break; 13612 } 13613 break; // End MemberExpr 13614 } else if (const ArraySubscriptExpr *ASE = 13615 dyn_cast<ArraySubscriptExpr>(E)) { 13616 E = ASE->getBase()->IgnoreParenImpCasts(); 13617 continue; 13618 } else if (const ExtVectorElementExpr *EVE = 13619 dyn_cast<ExtVectorElementExpr>(E)) { 13620 E = EVE->getBase()->IgnoreParenImpCasts(); 13621 continue; 13622 } 13623 break; 13624 } 13625 13626 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 13627 // Function calls 13628 const FunctionDecl *FD = CE->getDirectCallee(); 13629 if (FD && !IsTypeModifiable(FD->getReturnType(), IsDereference)) { 13630 if (!DiagnosticEmitted) { 13631 S.Diag(Loc, diag::err_typecheck_assign_const) << ExprRange 13632 << ConstFunction << FD; 13633 DiagnosticEmitted = true; 13634 } 13635 S.Diag(FD->getReturnTypeSourceRange().getBegin(), 13636 diag::note_typecheck_assign_const) 13637 << ConstFunction << FD << FD->getReturnType() 13638 << FD->getReturnTypeSourceRange(); 13639 } 13640 } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 13641 // Point to variable declaration. 13642 if (const ValueDecl *VD = DRE->getDecl()) { 13643 if (!IsTypeModifiable(VD->getType(), IsDereference)) { 13644 if (!DiagnosticEmitted) { 13645 S.Diag(Loc, diag::err_typecheck_assign_const) 13646 << ExprRange << ConstVariable << VD << VD->getType(); 13647 DiagnosticEmitted = true; 13648 } 13649 S.Diag(VD->getLocation(), diag::note_typecheck_assign_const) 13650 << ConstVariable << VD << VD->getType() << VD->getSourceRange(); 13651 } 13652 } 13653 } else if (isa<CXXThisExpr>(E)) { 13654 if (const DeclContext *DC = S.getFunctionLevelDeclContext()) { 13655 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) { 13656 if (MD->isConst()) { 13657 if (!DiagnosticEmitted) { 13658 S.Diag(Loc, diag::err_typecheck_assign_const) << ExprRange 13659 << ConstMethod << MD; 13660 DiagnosticEmitted = true; 13661 } 13662 S.Diag(MD->getLocation(), diag::note_typecheck_assign_const) 13663 << ConstMethod << MD << MD->getSourceRange(); 13664 } 13665 } 13666 } 13667 } 13668 13669 if (DiagnosticEmitted) 13670 return; 13671 13672 // Can't determine a more specific message, so display the generic error. 13673 S.Diag(Loc, diag::err_typecheck_assign_const) << ExprRange << ConstUnknown; 13674 } 13675 13676 enum OriginalExprKind { 13677 OEK_Variable, 13678 OEK_Member, 13679 OEK_LValue 13680 }; 13681 13682 static void DiagnoseRecursiveConstFields(Sema &S, const ValueDecl *VD, 13683 const RecordType *Ty, 13684 SourceLocation Loc, SourceRange Range, 13685 OriginalExprKind OEK, 13686 bool &DiagnosticEmitted) { 13687 std::vector<const RecordType *> RecordTypeList; 13688 RecordTypeList.push_back(Ty); 13689 unsigned NextToCheckIndex = 0; 13690 // We walk the record hierarchy breadth-first to ensure that we print 13691 // diagnostics in field nesting order. 13692 while (RecordTypeList.size() > NextToCheckIndex) { 13693 bool IsNested = NextToCheckIndex > 0; 13694 for (const FieldDecl *Field : 13695 RecordTypeList[NextToCheckIndex]->getDecl()->fields()) { 13696 // First, check every field for constness. 13697 QualType FieldTy = Field->getType(); 13698 if (FieldTy.isConstQualified()) { 13699 if (!DiagnosticEmitted) { 13700 S.Diag(Loc, diag::err_typecheck_assign_const) 13701 << Range << NestedConstMember << OEK << VD 13702 << IsNested << Field; 13703 DiagnosticEmitted = true; 13704 } 13705 S.Diag(Field->getLocation(), diag::note_typecheck_assign_const) 13706 << NestedConstMember << IsNested << Field 13707 << FieldTy << Field->getSourceRange(); 13708 } 13709 13710 // Then we append it to the list to check next in order. 13711 FieldTy = FieldTy.getCanonicalType(); 13712 if (const auto *FieldRecTy = FieldTy->getAs<RecordType>()) { 13713 if (!llvm::is_contained(RecordTypeList, FieldRecTy)) 13714 RecordTypeList.push_back(FieldRecTy); 13715 } 13716 } 13717 ++NextToCheckIndex; 13718 } 13719 } 13720 13721 /// Emit an error for the case where a record we are trying to assign to has a 13722 /// const-qualified field somewhere in its hierarchy. 13723 static void DiagnoseRecursiveConstFields(Sema &S, const Expr *E, 13724 SourceLocation Loc) { 13725 QualType Ty = E->getType(); 13726 assert(Ty->isRecordType() && "lvalue was not record?"); 13727 SourceRange Range = E->getSourceRange(); 13728 const RecordType *RTy = Ty.getCanonicalType()->getAs<RecordType>(); 13729 bool DiagEmitted = false; 13730 13731 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) 13732 DiagnoseRecursiveConstFields(S, ME->getMemberDecl(), RTy, Loc, 13733 Range, OEK_Member, DiagEmitted); 13734 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 13735 DiagnoseRecursiveConstFields(S, DRE->getDecl(), RTy, Loc, 13736 Range, OEK_Variable, DiagEmitted); 13737 else 13738 DiagnoseRecursiveConstFields(S, nullptr, RTy, Loc, 13739 Range, OEK_LValue, DiagEmitted); 13740 if (!DiagEmitted) 13741 DiagnoseConstAssignment(S, E, Loc); 13742 } 13743 13744 /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, 13745 /// emit an error and return true. If so, return false. 13746 static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { 13747 assert(!E->hasPlaceholderType(BuiltinType::PseudoObject)); 13748 13749 S.CheckShadowingDeclModification(E, Loc); 13750 13751 SourceLocation OrigLoc = Loc; 13752 Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, 13753 &Loc); 13754 if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) 13755 IsLV = Expr::MLV_InvalidMessageExpression; 13756 if (IsLV == Expr::MLV_Valid) 13757 return false; 13758 13759 unsigned DiagID = 0; 13760 bool NeedType = false; 13761 switch (IsLV) { // C99 6.5.16p2 13762 case Expr::MLV_ConstQualified: 13763 // Use a specialized diagnostic when we're assigning to an object 13764 // from an enclosing function or block. 13765 if (NonConstCaptureKind NCCK = isReferenceToNonConstCapture(S, E)) { 13766 if (NCCK == NCCK_Block) 13767 DiagID = diag::err_block_decl_ref_not_modifiable_lvalue; 13768 else 13769 DiagID = diag::err_lambda_decl_ref_not_modifiable_lvalue; 13770 break; 13771 } 13772 13773 // In ARC, use some specialized diagnostics for occasions where we 13774 // infer 'const'. These are always pseudo-strong variables. 13775 if (S.getLangOpts().ObjCAutoRefCount) { 13776 DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); 13777 if (declRef && isa<VarDecl>(declRef->getDecl())) { 13778 VarDecl *var = cast<VarDecl>(declRef->getDecl()); 13779 13780 // Use the normal diagnostic if it's pseudo-__strong but the 13781 // user actually wrote 'const'. 13782 if (var->isARCPseudoStrong() && 13783 (!var->getTypeSourceInfo() || 13784 !var->getTypeSourceInfo()->getType().isConstQualified())) { 13785 // There are three pseudo-strong cases: 13786 // - self 13787 ObjCMethodDecl *method = S.getCurMethodDecl(); 13788 if (method && var == method->getSelfDecl()) { 13789 DiagID = method->isClassMethod() 13790 ? diag::err_typecheck_arc_assign_self_class_method 13791 : diag::err_typecheck_arc_assign_self; 13792 13793 // - Objective-C externally_retained attribute. 13794 } else if (var->hasAttr<ObjCExternallyRetainedAttr>() || 13795 isa<ParmVarDecl>(var)) { 13796 DiagID = diag::err_typecheck_arc_assign_externally_retained; 13797 13798 // - fast enumeration variables 13799 } else { 13800 DiagID = diag::err_typecheck_arr_assign_enumeration; 13801 } 13802 13803 SourceRange Assign; 13804 if (Loc != OrigLoc) 13805 Assign = SourceRange(OrigLoc, OrigLoc); 13806 S.Diag(Loc, DiagID) << E->getSourceRange() << Assign; 13807 // We need to preserve the AST regardless, so migration tool 13808 // can do its job. 13809 return false; 13810 } 13811 } 13812 } 13813 13814 // If none of the special cases above are triggered, then this is a 13815 // simple const assignment. 13816 if (DiagID == 0) { 13817 DiagnoseConstAssignment(S, E, Loc); 13818 return true; 13819 } 13820 13821 break; 13822 case Expr::MLV_ConstAddrSpace: 13823 DiagnoseConstAssignment(S, E, Loc); 13824 return true; 13825 case Expr::MLV_ConstQualifiedField: 13826 DiagnoseRecursiveConstFields(S, E, Loc); 13827 return true; 13828 case Expr::MLV_ArrayType: 13829 case Expr::MLV_ArrayTemporary: 13830 DiagID = diag::err_typecheck_array_not_modifiable_lvalue; 13831 NeedType = true; 13832 break; 13833 case Expr::MLV_NotObjectType: 13834 DiagID = diag::err_typecheck_non_object_not_modifiable_lvalue; 13835 NeedType = true; 13836 break; 13837 case Expr::MLV_LValueCast: 13838 DiagID = diag::err_typecheck_lvalue_casts_not_supported; 13839 break; 13840 case Expr::MLV_Valid: 13841 llvm_unreachable("did not take early return for MLV_Valid"); 13842 case Expr::MLV_InvalidExpression: 13843 case Expr::MLV_MemberFunction: 13844 case Expr::MLV_ClassTemporary: 13845 DiagID = diag::err_typecheck_expression_not_modifiable_lvalue; 13846 break; 13847 case Expr::MLV_IncompleteType: 13848 case Expr::MLV_IncompleteVoidType: 13849 return S.RequireCompleteType(Loc, E->getType(), 13850 diag::err_typecheck_incomplete_type_not_modifiable_lvalue, E); 13851 case Expr::MLV_DuplicateVectorComponents: 13852 DiagID = diag::err_typecheck_duplicate_vector_components_not_mlvalue; 13853 break; 13854 case Expr::MLV_NoSetterProperty: 13855 llvm_unreachable("readonly properties should be processed differently"); 13856 case Expr::MLV_InvalidMessageExpression: 13857 DiagID = diag::err_readonly_message_assignment; 13858 break; 13859 case Expr::MLV_SubObjCPropertySetting: 13860 DiagID = diag::err_no_subobject_property_setting; 13861 break; 13862 } 13863 13864 SourceRange Assign; 13865 if (Loc != OrigLoc) 13866 Assign = SourceRange(OrigLoc, OrigLoc); 13867 if (NeedType) 13868 S.Diag(Loc, DiagID) << E->getType() << E->getSourceRange() << Assign; 13869 else 13870 S.Diag(Loc, DiagID) << E->getSourceRange() << Assign; 13871 return true; 13872 } 13873 13874 static void CheckIdentityFieldAssignment(Expr *LHSExpr, Expr *RHSExpr, 13875 SourceLocation Loc, 13876 Sema &Sema) { 13877 if (Sema.inTemplateInstantiation()) 13878 return; 13879 if (Sema.isUnevaluatedContext()) 13880 return; 13881 if (Loc.isInvalid() || Loc.isMacroID()) 13882 return; 13883 if (LHSExpr->getExprLoc().isMacroID() || RHSExpr->getExprLoc().isMacroID()) 13884 return; 13885 13886 // C / C++ fields 13887 MemberExpr *ML = dyn_cast<MemberExpr>(LHSExpr); 13888 MemberExpr *MR = dyn_cast<MemberExpr>(RHSExpr); 13889 if (ML && MR) { 13890 if (!(isa<CXXThisExpr>(ML->getBase()) && isa<CXXThisExpr>(MR->getBase()))) 13891 return; 13892 const ValueDecl *LHSDecl = 13893 cast<ValueDecl>(ML->getMemberDecl()->getCanonicalDecl()); 13894 const ValueDecl *RHSDecl = 13895 cast<ValueDecl>(MR->getMemberDecl()->getCanonicalDecl()); 13896 if (LHSDecl != RHSDecl) 13897 return; 13898 if (LHSDecl->getType().isVolatileQualified()) 13899 return; 13900 if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) 13901 if (RefTy->getPointeeType().isVolatileQualified()) 13902 return; 13903 13904 Sema.Diag(Loc, diag::warn_identity_field_assign) << 0; 13905 } 13906 13907 // Objective-C instance variables 13908 ObjCIvarRefExpr *OL = dyn_cast<ObjCIvarRefExpr>(LHSExpr); 13909 ObjCIvarRefExpr *OR = dyn_cast<ObjCIvarRefExpr>(RHSExpr); 13910 if (OL && OR && OL->getDecl() == OR->getDecl()) { 13911 DeclRefExpr *RL = dyn_cast<DeclRefExpr>(OL->getBase()->IgnoreImpCasts()); 13912 DeclRefExpr *RR = dyn_cast<DeclRefExpr>(OR->getBase()->IgnoreImpCasts()); 13913 if (RL && RR && RL->getDecl() == RR->getDecl()) 13914 Sema.Diag(Loc, diag::warn_identity_field_assign) << 1; 13915 } 13916 } 13917 13918 // C99 6.5.16.1 13919 QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, 13920 SourceLocation Loc, 13921 QualType CompoundType, 13922 BinaryOperatorKind Opc) { 13923 assert(!LHSExpr->hasPlaceholderType(BuiltinType::PseudoObject)); 13924 13925 // Verify that LHS is a modifiable lvalue, and emit error if not. 13926 if (CheckForModifiableLvalue(LHSExpr, Loc, *this)) 13927 return QualType(); 13928 13929 QualType LHSType = LHSExpr->getType(); 13930 QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : 13931 CompoundType; 13932 13933 if (RHS.isUsable()) { 13934 // Even if this check fails don't return early to allow the best 13935 // possible error recovery and to allow any subsequent diagnostics to 13936 // work. 13937 const ValueDecl *Assignee = nullptr; 13938 bool ShowFullyQualifiedAssigneeName = false; 13939 // In simple cases describe what is being assigned to 13940 if (auto *DR = dyn_cast<DeclRefExpr>(LHSExpr->IgnoreParenCasts())) { 13941 Assignee = DR->getDecl(); 13942 } else if (auto *ME = dyn_cast<MemberExpr>(LHSExpr->IgnoreParenCasts())) { 13943 Assignee = ME->getMemberDecl(); 13944 ShowFullyQualifiedAssigneeName = true; 13945 } 13946 13947 BoundsSafetyCheckAssignmentToCountAttrPtr( 13948 LHSType, RHS.get(), AssignmentAction::Assigning, Loc, Assignee, 13949 ShowFullyQualifiedAssigneeName); 13950 } 13951 13952 // OpenCL v1.2 s6.1.1.1 p2: 13953 // The half data type can only be used to declare a pointer to a buffer that 13954 // contains half values 13955 if (getLangOpts().OpenCL && 13956 !getOpenCLOptions().isAvailableOption("cl_khr_fp16", getLangOpts()) && 13957 LHSType->isHalfType()) { 13958 Diag(Loc, diag::err_opencl_half_load_store) << 1 13959 << LHSType.getUnqualifiedType(); 13960 return QualType(); 13961 } 13962 13963 // WebAssembly tables can't be used on RHS of an assignment expression. 13964 if (RHSType->isWebAssemblyTableType()) { 13965 Diag(Loc, diag::err_wasm_table_art) << 0; 13966 return QualType(); 13967 } 13968 13969 AssignConvertType ConvTy; 13970 if (CompoundType.isNull()) { 13971 Expr *RHSCheck = RHS.get(); 13972 13973 CheckIdentityFieldAssignment(LHSExpr, RHSCheck, Loc, *this); 13974 13975 QualType LHSTy(LHSType); 13976 ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); 13977 if (RHS.isInvalid()) 13978 return QualType(); 13979 // Special case of NSObject attributes on c-style pointer types. 13980 if (ConvTy == AssignConvertType::IncompatiblePointer && 13981 ((Context.isObjCNSObjectType(LHSType) && 13982 RHSType->isObjCObjectPointerType()) || 13983 (Context.isObjCNSObjectType(RHSType) && 13984 LHSType->isObjCObjectPointerType()))) 13985 ConvTy = AssignConvertType::Compatible; 13986 13987 if (IsAssignConvertCompatible(ConvTy) && LHSType->isObjCObjectType()) 13988 Diag(Loc, diag::err_objc_object_assignment) << LHSType; 13989 13990 // If the RHS is a unary plus or minus, check to see if they = and + are 13991 // right next to each other. If so, the user may have typo'd "x =+ 4" 13992 // instead of "x += 4". 13993 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) 13994 RHSCheck = ICE->getSubExpr(); 13995 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { 13996 if ((UO->getOpcode() == UO_Plus || UO->getOpcode() == UO_Minus) && 13997 Loc.isFileID() && UO->getOperatorLoc().isFileID() && 13998 // Only if the two operators are exactly adjacent. 13999 Loc.getLocWithOffset(1) == UO->getOperatorLoc() && 14000 // And there is a space or other character before the subexpr of the 14001 // unary +/-. We don't want to warn on "x=-1". 14002 Loc.getLocWithOffset(2) != UO->getSubExpr()->getBeginLoc() && 14003 UO->getSubExpr()->getBeginLoc().isFileID()) { 14004 Diag(Loc, diag::warn_not_compound_assign) 14005 << (UO->getOpcode() == UO_Plus ? "+" : "-") 14006 << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); 14007 } 14008 } 14009 14010 if (IsAssignConvertCompatible(ConvTy)) { 14011 if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) { 14012 // Warn about retain cycles where a block captures the LHS, but 14013 // not if the LHS is a simple variable into which the block is 14014 // being stored...unless that variable can be captured by reference! 14015 const Expr *InnerLHS = LHSExpr->IgnoreParenCasts(); 14016 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InnerLHS); 14017 if (!DRE || DRE->getDecl()->hasAttr<BlocksAttr>()) 14018 ObjC().checkRetainCycles(LHSExpr, RHS.get()); 14019 } 14020 14021 if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong || 14022 LHSType.isNonWeakInMRRWithObjCWeak(Context)) { 14023 // It is safe to assign a weak reference into a strong variable. 14024 // Although this code can still have problems: 14025 // id x = self.weakProp; 14026 // id y = self.weakProp; 14027 // we do not warn to warn spuriously when 'x' and 'y' are on separate 14028 // paths through the function. This should be revisited if 14029 // -Wrepeated-use-of-weak is made flow-sensitive. 14030 // For ObjCWeak only, we do not warn if the assign is to a non-weak 14031 // variable, which will be valid for the current autorelease scope. 14032 if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, 14033 RHS.get()->getBeginLoc())) 14034 getCurFunction()->markSafeWeakUse(RHS.get()); 14035 14036 } else if (getLangOpts().ObjCAutoRefCount || getLangOpts().ObjCWeak) { 14037 checkUnsafeExprAssigns(Loc, LHSExpr, RHS.get()); 14038 } 14039 } 14040 } else { 14041 // Compound assignment "x += y" 14042 ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); 14043 } 14044 14045 if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, RHS.get(), 14046 AssignmentAction::Assigning)) 14047 return QualType(); 14048 14049 CheckForNullPointerDereference(*this, LHSExpr); 14050 14051 AssignedEntity AE{LHSExpr}; 14052 checkAssignmentLifetime(*this, AE, RHS.get()); 14053 14054 if (getLangOpts().CPlusPlus20 && LHSType.isVolatileQualified()) { 14055 if (CompoundType.isNull()) { 14056 // C++2a [expr.ass]p5: 14057 // A simple-assignment whose left operand is of a volatile-qualified 14058 // type is deprecated unless the assignment is either a discarded-value 14059 // expression or an unevaluated operand 14060 ExprEvalContexts.back().VolatileAssignmentLHSs.push_back(LHSExpr); 14061 } 14062 } 14063 14064 // C11 6.5.16p3: The type of an assignment expression is the type of the 14065 // left operand would have after lvalue conversion. 14066 // C11 6.3.2.1p2: ...this is called lvalue conversion. If the lvalue has 14067 // qualified type, the value has the unqualified version of the type of the 14068 // lvalue; additionally, if the lvalue has atomic type, the value has the 14069 // non-atomic version of the type of the lvalue. 14070 // C++ 5.17p1: the type of the assignment expression is that of its left 14071 // operand. 14072 return getLangOpts().CPlusPlus ? LHSType : LHSType.getAtomicUnqualifiedType(); 14073 } 14074 14075 // Scenarios to ignore if expression E is: 14076 // 1. an explicit cast expression into void 14077 // 2. a function call expression that returns void 14078 static bool IgnoreCommaOperand(const Expr *E, const ASTContext &Context) { 14079 E = E->IgnoreParens(); 14080 14081 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { 14082 if (CE->getCastKind() == CK_ToVoid) { 14083 return true; 14084 } 14085 14086 // static_cast<void> on a dependent type will not show up as CK_ToVoid. 14087 if (CE->getCastKind() == CK_Dependent && E->getType()->isVoidType() && 14088 CE->getSubExpr()->getType()->isDependentType()) { 14089 return true; 14090 } 14091 } 14092 14093 if (const auto *CE = dyn_cast<CallExpr>(E)) 14094 return CE->getCallReturnType(Context)->isVoidType(); 14095 return false; 14096 } 14097 14098 void Sema::DiagnoseCommaOperator(const Expr *LHS, SourceLocation Loc) { 14099 // No warnings in macros 14100 if (Loc.isMacroID()) 14101 return; 14102 14103 // Don't warn in template instantiations. 14104 if (inTemplateInstantiation()) 14105 return; 14106 14107 // Scope isn't fine-grained enough to explicitly list the specific cases, so 14108 // instead, skip more than needed, then call back into here with the 14109 // CommaVisitor in SemaStmt.cpp. 14110 // The listed locations are the initialization and increment portions 14111 // of a for loop. The additional checks are on the condition of 14112 // if statements, do/while loops, and for loops. 14113 // Differences in scope flags for C89 mode requires the extra logic. 14114 const unsigned ForIncrementFlags = 14115 getLangOpts().C99 || getLangOpts().CPlusPlus 14116 ? Scope::ControlScope | Scope::ContinueScope | Scope::BreakScope 14117 : Scope::ContinueScope | Scope::BreakScope; 14118 const unsigned ForInitFlags = Scope::ControlScope | Scope::DeclScope; 14119 const unsigned ScopeFlags = getCurScope()->getFlags(); 14120 if ((ScopeFlags & ForIncrementFlags) == ForIncrementFlags || 14121 (ScopeFlags & ForInitFlags) == ForInitFlags) 14122 return; 14123 14124 // If there are multiple comma operators used together, get the RHS of the 14125 // of the comma operator as the LHS. 14126 while (const BinaryOperator *BO = dyn_cast<BinaryOperator>(LHS)) { 14127 if (BO->getOpcode() != BO_Comma) 14128 break; 14129 LHS = BO->getRHS(); 14130 } 14131 14132 // Only allow some expressions on LHS to not warn. 14133 if (IgnoreCommaOperand(LHS, Context)) 14134 return; 14135 14136 Diag(Loc, diag::warn_comma_operator); 14137 Diag(LHS->getBeginLoc(), diag::note_cast_to_void) 14138 << LHS->getSourceRange() 14139 << FixItHint::CreateInsertion(LHS->getBeginLoc(), 14140 LangOpts.CPlusPlus ? "static_cast<void>(" 14141 : "(void)(") 14142 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(LHS->getEndLoc()), 14143 ")"); 14144 } 14145 14146 // C99 6.5.17 14147 static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, 14148 SourceLocation Loc) { 14149 LHS = S.CheckPlaceholderExpr(LHS.get()); 14150 RHS = S.CheckPlaceholderExpr(RHS.get()); 14151 if (LHS.isInvalid() || RHS.isInvalid()) 14152 return QualType(); 14153 14154 // C's comma performs lvalue conversion (C99 6.3.2.1) on both its 14155 // operands, but not unary promotions. 14156 // C++'s comma does not do any conversions at all (C++ [expr.comma]p1). 14157 14158 // So we treat the LHS as a ignored value, and in C++ we allow the 14159 // containing site to determine what should be done with the RHS. 14160 LHS = S.IgnoredValueConversions(LHS.get()); 14161 if (LHS.isInvalid()) 14162 return QualType(); 14163 14164 S.DiagnoseUnusedExprResult(LHS.get(), diag::warn_unused_comma_left_operand); 14165 14166 if (!S.getLangOpts().CPlusPlus) { 14167 RHS = S.DefaultFunctionArrayLvalueConversion(RHS.get()); 14168 if (RHS.isInvalid()) 14169 return QualType(); 14170 if (!RHS.get()->getType()->isVoidType()) 14171 S.RequireCompleteType(Loc, RHS.get()->getType(), 14172 diag::err_incomplete_type); 14173 } 14174 14175 if (!S.getDiagnostics().isIgnored(diag::warn_comma_operator, Loc)) 14176 S.DiagnoseCommaOperator(LHS.get(), Loc); 14177 14178 return RHS.get()->getType(); 14179 } 14180 14181 /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine 14182 /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. 14183 static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, 14184 ExprValueKind &VK, 14185 ExprObjectKind &OK, 14186 SourceLocation OpLoc, bool IsInc, 14187 bool IsPrefix) { 14188 QualType ResType = Op->getType(); 14189 // Atomic types can be used for increment / decrement where the non-atomic 14190 // versions can, so ignore the _Atomic() specifier for the purpose of 14191 // checking. 14192 if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) 14193 ResType = ResAtomicType->getValueType(); 14194 14195 assert(!ResType.isNull() && "no type for increment/decrement expression"); 14196 14197 if (S.getLangOpts().CPlusPlus && ResType->isBooleanType()) { 14198 // Decrement of bool is not allowed. 14199 if (!IsInc) { 14200 S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); 14201 return QualType(); 14202 } 14203 // Increment of bool sets it to true, but is deprecated. 14204 S.Diag(OpLoc, S.getLangOpts().CPlusPlus17 ? diag::ext_increment_bool 14205 : diag::warn_increment_bool) 14206 << Op->getSourceRange(); 14207 } else if (S.getLangOpts().CPlusPlus && ResType->isEnumeralType()) { 14208 // Error on enum increments and decrements in C++ mode 14209 S.Diag(OpLoc, diag::err_increment_decrement_enum) << IsInc << ResType; 14210 return QualType(); 14211 } else if (ResType->isRealType()) { 14212 // OK! 14213 } else if (ResType->isPointerType()) { 14214 // C99 6.5.2.4p2, 6.5.6p2 14215 if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) 14216 return QualType(); 14217 } else if (ResType->isObjCObjectPointerType()) { 14218 // On modern runtimes, ObjC pointer arithmetic is forbidden. 14219 // Otherwise, we just need a complete type. 14220 if (checkArithmeticIncompletePointerType(S, OpLoc, Op) || 14221 checkArithmeticOnObjCPointer(S, OpLoc, Op)) 14222 return QualType(); 14223 } else if (ResType->isAnyComplexType()) { 14224 // C99 does not support ++/-- on complex types, we allow as an extension. 14225 S.Diag(OpLoc, S.getLangOpts().C2y ? diag::warn_c2y_compat_increment_complex 14226 : diag::ext_c2y_increment_complex) 14227 << IsInc << Op->getSourceRange(); 14228 } else if (ResType->isPlaceholderType()) { 14229 ExprResult PR = S.CheckPlaceholderExpr(Op); 14230 if (PR.isInvalid()) return QualType(); 14231 return CheckIncrementDecrementOperand(S, PR.get(), VK, OK, OpLoc, 14232 IsInc, IsPrefix); 14233 } else if (S.getLangOpts().AltiVec && ResType->isVectorType()) { 14234 // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) 14235 } else if (S.getLangOpts().ZVector && ResType->isVectorType() && 14236 (ResType->castAs<VectorType>()->getVectorKind() != 14237 VectorKind::AltiVecBool)) { 14238 // The z vector extensions allow ++ and -- for non-bool vectors. 14239 } else if (S.getLangOpts().OpenCL && ResType->isVectorType() && 14240 ResType->castAs<VectorType>()->getElementType()->isIntegerType()) { 14241 // OpenCL V1.2 6.3 says dec/inc ops operate on integer vector types. 14242 } else { 14243 S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) 14244 << ResType << int(IsInc) << Op->getSourceRange(); 14245 return QualType(); 14246 } 14247 // At this point, we know we have a real, complex or pointer type. 14248 // Now make sure the operand is a modifiable lvalue. 14249 if (CheckForModifiableLvalue(Op, OpLoc, S)) 14250 return QualType(); 14251 if (S.getLangOpts().CPlusPlus20 && ResType.isVolatileQualified()) { 14252 // C++2a [expr.pre.inc]p1, [expr.post.inc]p1: 14253 // An operand with volatile-qualified type is deprecated 14254 S.Diag(OpLoc, diag::warn_deprecated_increment_decrement_volatile) 14255 << IsInc << ResType; 14256 } 14257 // In C++, a prefix increment is the same type as the operand. Otherwise 14258 // (in C or with postfix), the increment is the unqualified type of the 14259 // operand. 14260 if (IsPrefix && S.getLangOpts().CPlusPlus) { 14261 VK = VK_LValue; 14262 OK = Op->getObjectKind(); 14263 return ResType; 14264 } else { 14265 VK = VK_PRValue; 14266 return ResType.getUnqualifiedType(); 14267 } 14268 } 14269 14270 /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). 14271 /// This routine allows us to typecheck complex/recursive expressions 14272 /// where the declaration is needed for type checking. We only need to 14273 /// handle cases when the expression references a function designator 14274 /// or is an lvalue. Here are some examples: 14275 /// - &(x) => x 14276 /// - &*****f => f for f a function designator. 14277 /// - &s.xx => s 14278 /// - &s.zz[1].yy -> s, if zz is an array 14279 /// - *(x + 1) -> x, if x is an array 14280 /// - &"123"[2] -> 0 14281 /// - & __real__ x -> x 14282 /// 14283 /// FIXME: We don't recurse to the RHS of a comma, nor handle pointers to 14284 /// members. 14285 static ValueDecl *getPrimaryDecl(Expr *E) { 14286 switch (E->getStmtClass()) { 14287 case Stmt::DeclRefExprClass: 14288 return cast<DeclRefExpr>(E)->getDecl(); 14289 case Stmt::MemberExprClass: 14290 // If this is an arrow operator, the address is an offset from 14291 // the base's value, so the object the base refers to is 14292 // irrelevant. 14293 if (cast<MemberExpr>(E)->isArrow()) 14294 return nullptr; 14295 // Otherwise, the expression refers to a part of the base 14296 return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); 14297 case Stmt::ArraySubscriptExprClass: { 14298 // FIXME: This code shouldn't be necessary! We should catch the implicit 14299 // promotion of register arrays earlier. 14300 Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); 14301 if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { 14302 if (ICE->getSubExpr()->getType()->isArrayType()) 14303 return getPrimaryDecl(ICE->getSubExpr()); 14304 } 14305 return nullptr; 14306 } 14307 case Stmt::UnaryOperatorClass: { 14308 UnaryOperator *UO = cast<UnaryOperator>(E); 14309 14310 switch(UO->getOpcode()) { 14311 case UO_Real: 14312 case UO_Imag: 14313 case UO_Extension: 14314 return getPrimaryDecl(UO->getSubExpr()); 14315 default: 14316 return nullptr; 14317 } 14318 } 14319 case Stmt::ParenExprClass: 14320 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); 14321 case Stmt::ImplicitCastExprClass: 14322 // If the result of an implicit cast is an l-value, we care about 14323 // the sub-expression; otherwise, the result here doesn't matter. 14324 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); 14325 case Stmt::CXXUuidofExprClass: 14326 return cast<CXXUuidofExpr>(E)->getGuidDecl(); 14327 default: 14328 return nullptr; 14329 } 14330 } 14331 14332 namespace { 14333 enum { 14334 AO_Bit_Field = 0, 14335 AO_Vector_Element = 1, 14336 AO_Property_Expansion = 2, 14337 AO_Register_Variable = 3, 14338 AO_Matrix_Element = 4, 14339 AO_No_Error = 5 14340 }; 14341 } 14342 /// Diagnose invalid operand for address of operations. 14343 /// 14344 /// \param Type The type of operand which cannot have its address taken. 14345 static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, 14346 Expr *E, unsigned Type) { 14347 S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); 14348 } 14349 14350 bool Sema::CheckUseOfCXXMethodAsAddressOfOperand(SourceLocation OpLoc, 14351 const Expr *Op, 14352 const CXXMethodDecl *MD) { 14353 const auto *DRE = cast<DeclRefExpr>(Op->IgnoreParens()); 14354 14355 if (Op != DRE) 14356 return Diag(OpLoc, diag::err_parens_pointer_member_function) 14357 << Op->getSourceRange(); 14358 14359 // Taking the address of a dtor is illegal per C++ [class.dtor]p2. 14360 if (isa<CXXDestructorDecl>(MD)) 14361 return Diag(OpLoc, diag::err_typecheck_addrof_dtor) 14362 << DRE->getSourceRange(); 14363 14364 if (DRE->getQualifier()) 14365 return false; 14366 14367 if (MD->getParent()->getName().empty()) 14368 return Diag(OpLoc, diag::err_unqualified_pointer_member_function) 14369 << DRE->getSourceRange(); 14370 14371 SmallString<32> Str; 14372 StringRef Qual = (MD->getParent()->getName() + "::").toStringRef(Str); 14373 return Diag(OpLoc, diag::err_unqualified_pointer_member_function) 14374 << DRE->getSourceRange() 14375 << FixItHint::CreateInsertion(DRE->getSourceRange().getBegin(), Qual); 14376 } 14377 14378 QualType Sema::CheckAddressOfOperand(ExprResult &OrigOp, SourceLocation OpLoc) { 14379 if (const BuiltinType *PTy = OrigOp.get()->getType()->getAsPlaceholderType()){ 14380 if (PTy->getKind() == BuiltinType::Overload) { 14381 Expr *E = OrigOp.get()->IgnoreParens(); 14382 if (!isa<OverloadExpr>(E)) { 14383 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf); 14384 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof_addrof_function) 14385 << OrigOp.get()->getSourceRange(); 14386 return QualType(); 14387 } 14388 14389 OverloadExpr *Ovl = cast<OverloadExpr>(E); 14390 if (isa<UnresolvedMemberExpr>(Ovl)) 14391 if (!ResolveSingleFunctionTemplateSpecialization(Ovl)) { 14392 Diag(OpLoc, diag::err_invalid_form_pointer_member_function) 14393 << OrigOp.get()->getSourceRange(); 14394 return QualType(); 14395 } 14396 14397 return Context.OverloadTy; 14398 } 14399 14400 if (PTy->getKind() == BuiltinType::UnknownAny) 14401 return Context.UnknownAnyTy; 14402 14403 if (PTy->getKind() == BuiltinType::BoundMember) { 14404 Diag(OpLoc, diag::err_invalid_form_pointer_member_function) 14405 << OrigOp.get()->getSourceRange(); 14406 return QualType(); 14407 } 14408 14409 OrigOp = CheckPlaceholderExpr(OrigOp.get()); 14410 if (OrigOp.isInvalid()) return QualType(); 14411 } 14412 14413 if (OrigOp.get()->isTypeDependent()) 14414 return Context.DependentTy; 14415 14416 assert(!OrigOp.get()->hasPlaceholderType()); 14417 14418 // Make sure to ignore parentheses in subsequent checks 14419 Expr *op = OrigOp.get()->IgnoreParens(); 14420 14421 // In OpenCL captures for blocks called as lambda functions 14422 // are located in the private address space. Blocks used in 14423 // enqueue_kernel can be located in a different address space 14424 // depending on a vendor implementation. Thus preventing 14425 // taking an address of the capture to avoid invalid AS casts. 14426 if (LangOpts.OpenCL) { 14427 auto* VarRef = dyn_cast<DeclRefExpr>(op); 14428 if (VarRef && VarRef->refersToEnclosingVariableOrCapture()) { 14429 Diag(op->getExprLoc(), diag::err_opencl_taking_address_capture); 14430 return QualType(); 14431 } 14432 } 14433 14434 if (getLangOpts().C99) { 14435 // Implement C99-only parts of addressof rules. 14436 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { 14437 if (uOp->getOpcode() == UO_Deref) 14438 // Per C99 6.5.3.2, the address of a deref always returns a valid result 14439 // (assuming the deref expression is valid). 14440 return uOp->getSubExpr()->getType(); 14441 } 14442 // Technically, there should be a check for array subscript 14443 // expressions here, but the result of one is always an lvalue anyway. 14444 } 14445 ValueDecl *dcl = getPrimaryDecl(op); 14446 14447 if (auto *FD = dyn_cast_or_null<FunctionDecl>(dcl)) 14448 if (!checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, 14449 op->getBeginLoc())) 14450 return QualType(); 14451 14452 Expr::LValueClassification lval = op->ClassifyLValue(Context); 14453 unsigned AddressOfError = AO_No_Error; 14454 14455 if (lval == Expr::LV_ClassTemporary || lval == Expr::LV_ArrayTemporary) { 14456 bool sfinae = (bool)isSFINAEContext(); 14457 Diag(OpLoc, isSFINAEContext() ? diag::err_typecheck_addrof_temporary 14458 : diag::ext_typecheck_addrof_temporary) 14459 << op->getType() << op->getSourceRange(); 14460 if (sfinae) 14461 return QualType(); 14462 // Materialize the temporary as an lvalue so that we can take its address. 14463 OrigOp = op = 14464 CreateMaterializeTemporaryExpr(op->getType(), OrigOp.get(), true); 14465 } else if (isa<ObjCSelectorExpr>(op)) { 14466 return Context.getPointerType(op->getType()); 14467 } else if (lval == Expr::LV_MemberFunction) { 14468 // If it's an instance method, make a member pointer. 14469 // The expression must have exactly the form &A::foo. 14470 14471 // If the underlying expression isn't a decl ref, give up. 14472 if (!isa<DeclRefExpr>(op)) { 14473 Diag(OpLoc, diag::err_invalid_form_pointer_member_function) 14474 << OrigOp.get()->getSourceRange(); 14475 return QualType(); 14476 } 14477 DeclRefExpr *DRE = cast<DeclRefExpr>(op); 14478 CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); 14479 14480 CheckUseOfCXXMethodAsAddressOfOperand(OpLoc, OrigOp.get(), MD); 14481 QualType MPTy = Context.getMemberPointerType( 14482 op->getType(), DRE->getQualifier(), MD->getParent()); 14483 14484 if (getLangOpts().PointerAuthCalls && MD->isVirtual() && 14485 !isUnevaluatedContext() && !MPTy->isDependentType()) { 14486 // When pointer authentication is enabled, argument and return types of 14487 // vitual member functions must be complete. This is because vitrual 14488 // member function pointers are implemented using virtual dispatch 14489 // thunks and the thunks cannot be emitted if the argument or return 14490 // types are incomplete. 14491 auto ReturnOrParamTypeIsIncomplete = [&](QualType T, 14492 SourceLocation DeclRefLoc, 14493 SourceLocation RetArgTypeLoc) { 14494 if (RequireCompleteType(DeclRefLoc, T, diag::err_incomplete_type)) { 14495 Diag(DeclRefLoc, 14496 diag::note_ptrauth_virtual_function_pointer_incomplete_arg_ret); 14497 Diag(RetArgTypeLoc, 14498 diag::note_ptrauth_virtual_function_incomplete_arg_ret_type) 14499 << T; 14500 return true; 14501 } 14502 return false; 14503 }; 14504 QualType RetTy = MD->getReturnType(); 14505 bool IsIncomplete = 14506 !RetTy->isVoidType() && 14507 ReturnOrParamTypeIsIncomplete( 14508 RetTy, OpLoc, MD->getReturnTypeSourceRange().getBegin()); 14509 for (auto *PVD : MD->parameters()) 14510 IsIncomplete |= ReturnOrParamTypeIsIncomplete(PVD->getType(), OpLoc, 14511 PVD->getBeginLoc()); 14512 if (IsIncomplete) 14513 return QualType(); 14514 } 14515 14516 // Under the MS ABI, lock down the inheritance model now. 14517 if (Context.getTargetInfo().getCXXABI().isMicrosoft()) 14518 (void)isCompleteType(OpLoc, MPTy); 14519 return MPTy; 14520 } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { 14521 // C99 6.5.3.2p1 14522 // The operand must be either an l-value or a function designator 14523 if (!op->getType()->isFunctionType()) { 14524 // Use a special diagnostic for loads from property references. 14525 if (isa<PseudoObjectExpr>(op)) { 14526 AddressOfError = AO_Property_Expansion; 14527 } else { 14528 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) 14529 << op->getType() << op->getSourceRange(); 14530 return QualType(); 14531 } 14532 } else if (const auto *DRE = dyn_cast<DeclRefExpr>(op)) { 14533 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(DRE->getDecl())) 14534 CheckUseOfCXXMethodAsAddressOfOperand(OpLoc, OrigOp.get(), MD); 14535 } 14536 14537 } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 14538 // The operand cannot be a bit-field 14539 AddressOfError = AO_Bit_Field; 14540 } else if (op->getObjectKind() == OK_VectorComponent) { 14541 // The operand cannot be an element of a vector 14542 AddressOfError = AO_Vector_Element; 14543 } else if (op->getObjectKind() == OK_MatrixComponent) { 14544 // The operand cannot be an element of a matrix. 14545 AddressOfError = AO_Matrix_Element; 14546 } else if (dcl) { // C99 6.5.3.2p1 14547 // We have an lvalue with a decl. Make sure the decl is not declared 14548 // with the register storage-class specifier. 14549 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { 14550 // in C++ it is not error to take address of a register 14551 // variable (c++03 7.1.1P3) 14552 if (vd->getStorageClass() == SC_Register && 14553 !getLangOpts().CPlusPlus) { 14554 AddressOfError = AO_Register_Variable; 14555 } 14556 } else if (isa<MSPropertyDecl>(dcl)) { 14557 AddressOfError = AO_Property_Expansion; 14558 } else if (isa<FunctionTemplateDecl>(dcl)) { 14559 return Context.OverloadTy; 14560 } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { 14561 // Okay: we can take the address of a field. 14562 // Could be a pointer to member, though, if there is an explicit 14563 // scope qualifier for the class. 14564 14565 // [C++26] [expr.prim.id.general] 14566 // If an id-expression E denotes a non-static non-type member 14567 // of some class C [...] and if E is a qualified-id, E is 14568 // not the un-parenthesized operand of the unary & operator [...] 14569 // the id-expression is transformed into a class member access expression. 14570 if (auto *DRE = dyn_cast<DeclRefExpr>(op); 14571 DRE && DRE->getQualifier() && !isa<ParenExpr>(OrigOp.get())) { 14572 DeclContext *Ctx = dcl->getDeclContext(); 14573 if (Ctx && Ctx->isRecord()) { 14574 if (dcl->getType()->isReferenceType()) { 14575 Diag(OpLoc, 14576 diag::err_cannot_form_pointer_to_member_of_reference_type) 14577 << dcl->getDeclName() << dcl->getType(); 14578 return QualType(); 14579 } 14580 14581 while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) 14582 Ctx = Ctx->getParent(); 14583 14584 QualType MPTy = Context.getMemberPointerType( 14585 op->getType(), DRE->getQualifier(), cast<CXXRecordDecl>(Ctx)); 14586 // Under the MS ABI, lock down the inheritance model now. 14587 if (Context.getTargetInfo().getCXXABI().isMicrosoft()) 14588 (void)isCompleteType(OpLoc, MPTy); 14589 return MPTy; 14590 } 14591 } 14592 } else if (!isa<FunctionDecl, NonTypeTemplateParmDecl, BindingDecl, 14593 MSGuidDecl, UnnamedGlobalConstantDecl>(dcl)) 14594 llvm_unreachable("Unknown/unexpected decl type"); 14595 } 14596 14597 if (AddressOfError != AO_No_Error) { 14598 diagnoseAddressOfInvalidType(*this, OpLoc, op, AddressOfError); 14599 return QualType(); 14600 } 14601 14602 if (lval == Expr::LV_IncompleteVoidType) { 14603 // Taking the address of a void variable is technically illegal, but we 14604 // allow it in cases which are otherwise valid. 14605 // Example: "extern void x; void* y = &x;". 14606 Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); 14607 } 14608 14609 // If the operand has type "type", the result has type "pointer to type". 14610 if (op->getType()->isObjCObjectType()) 14611 return Context.getObjCObjectPointerType(op->getType()); 14612 14613 // Cannot take the address of WebAssembly references or tables. 14614 if (Context.getTargetInfo().getTriple().isWasm()) { 14615 QualType OpTy = op->getType(); 14616 if (OpTy.isWebAssemblyReferenceType()) { 14617 Diag(OpLoc, diag::err_wasm_ca_reference) 14618 << 1 << OrigOp.get()->getSourceRange(); 14619 return QualType(); 14620 } 14621 if (OpTy->isWebAssemblyTableType()) { 14622 Diag(OpLoc, diag::err_wasm_table_pr) 14623 << 1 << OrigOp.get()->getSourceRange(); 14624 return QualType(); 14625 } 14626 } 14627 14628 CheckAddressOfPackedMember(op); 14629 14630 return Context.getPointerType(op->getType()); 14631 } 14632 14633 static void RecordModifiableNonNullParam(Sema &S, const Expr *Exp) { 14634 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp); 14635 if (!DRE) 14636 return; 14637 const Decl *D = DRE->getDecl(); 14638 if (!D) 14639 return; 14640 const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D); 14641 if (!Param) 14642 return; 14643 if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(Param->getDeclContext())) 14644 if (!FD->hasAttr<NonNullAttr>() && !Param->hasAttr<NonNullAttr>()) 14645 return; 14646 if (FunctionScopeInfo *FD = S.getCurFunction()) 14647 FD->ModifiedNonNullParams.insert(Param); 14648 } 14649 14650 /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). 14651 static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, 14652 SourceLocation OpLoc, 14653 bool IsAfterAmp = false) { 14654 ExprResult ConvResult = S.UsualUnaryConversions(Op); 14655 if (ConvResult.isInvalid()) 14656 return QualType(); 14657 Op = ConvResult.get(); 14658 QualType OpTy = Op->getType(); 14659 QualType Result; 14660 14661 if (isa<CXXReinterpretCastExpr>(Op)) { 14662 QualType OpOrigType = Op->IgnoreParenCasts()->getType(); 14663 S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, 14664 Op->getSourceRange()); 14665 } 14666 14667 if (const PointerType *PT = OpTy->getAs<PointerType>()) 14668 { 14669 Result = PT->getPointeeType(); 14670 } 14671 else if (const ObjCObjectPointerType *OPT = 14672 OpTy->getAs<ObjCObjectPointerType>()) 14673 Result = OPT->getPointeeType(); 14674 else { 14675 ExprResult PR = S.CheckPlaceholderExpr(Op); 14676 if (PR.isInvalid()) return QualType(); 14677 if (PR.get() != Op) 14678 return CheckIndirectionOperand(S, PR.get(), VK, OpLoc); 14679 } 14680 14681 if (Result.isNull()) { 14682 S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) 14683 << OpTy << Op->getSourceRange(); 14684 return QualType(); 14685 } 14686 14687 if (Result->isVoidType()) { 14688 // C++ [expr.unary.op]p1: 14689 // [...] the expression to which [the unary * operator] is applied shall 14690 // be a pointer to an object type, or a pointer to a function type 14691 LangOptions LO = S.getLangOpts(); 14692 if (LO.CPlusPlus) 14693 S.Diag(OpLoc, diag::err_typecheck_indirection_through_void_pointer_cpp) 14694 << OpTy << Op->getSourceRange(); 14695 else if (!(LO.C99 && IsAfterAmp) && !S.isUnevaluatedContext()) 14696 S.Diag(OpLoc, diag::ext_typecheck_indirection_through_void_pointer) 14697 << OpTy << Op->getSourceRange(); 14698 } 14699 14700 // Dereferences are usually l-values... 14701 VK = VK_LValue; 14702 14703 // ...except that certain expressions are never l-values in C. 14704 if (!S.getLangOpts().CPlusPlus && Result.isCForbiddenLValueType()) 14705 VK = VK_PRValue; 14706 14707 return Result; 14708 } 14709 14710 BinaryOperatorKind Sema::ConvertTokenKindToBinaryOpcode(tok::TokenKind Kind) { 14711 BinaryOperatorKind Opc; 14712 switch (Kind) { 14713 default: llvm_unreachable("Unknown binop!"); 14714 case tok::periodstar: Opc = BO_PtrMemD; break; 14715 case tok::arrowstar: Opc = BO_PtrMemI; break; 14716 case tok::star: Opc = BO_Mul; break; 14717 case tok::slash: Opc = BO_Div; break; 14718 case tok::percent: Opc = BO_Rem; break; 14719 case tok::plus: Opc = BO_Add; break; 14720 case tok::minus: Opc = BO_Sub; break; 14721 case tok::lessless: Opc = BO_Shl; break; 14722 case tok::greatergreater: Opc = BO_Shr; break; 14723 case tok::lessequal: Opc = BO_LE; break; 14724 case tok::less: Opc = BO_LT; break; 14725 case tok::greaterequal: Opc = BO_GE; break; 14726 case tok::greater: Opc = BO_GT; break; 14727 case tok::exclaimequal: Opc = BO_NE; break; 14728 case tok::equalequal: Opc = BO_EQ; break; 14729 case tok::spaceship: Opc = BO_Cmp; break; 14730 case tok::amp: Opc = BO_And; break; 14731 case tok::caret: Opc = BO_Xor; break; 14732 case tok::pipe: Opc = BO_Or; break; 14733 case tok::ampamp: Opc = BO_LAnd; break; 14734 case tok::pipepipe: Opc = BO_LOr; break; 14735 case tok::equal: Opc = BO_Assign; break; 14736 case tok::starequal: Opc = BO_MulAssign; break; 14737 case tok::slashequal: Opc = BO_DivAssign; break; 14738 case tok::percentequal: Opc = BO_RemAssign; break; 14739 case tok::plusequal: Opc = BO_AddAssign; break; 14740 case tok::minusequal: Opc = BO_SubAssign; break; 14741 case tok::lesslessequal: Opc = BO_ShlAssign; break; 14742 case tok::greatergreaterequal: Opc = BO_ShrAssign; break; 14743 case tok::ampequal: Opc = BO_AndAssign; break; 14744 case tok::caretequal: Opc = BO_XorAssign; break; 14745 case tok::pipeequal: Opc = BO_OrAssign; break; 14746 case tok::comma: Opc = BO_Comma; break; 14747 } 14748 return Opc; 14749 } 14750 14751 static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( 14752 tok::TokenKind Kind) { 14753 UnaryOperatorKind Opc; 14754 switch (Kind) { 14755 default: llvm_unreachable("Unknown unary op!"); 14756 case tok::plusplus: Opc = UO_PreInc; break; 14757 case tok::minusminus: Opc = UO_PreDec; break; 14758 case tok::amp: Opc = UO_AddrOf; break; 14759 case tok::star: Opc = UO_Deref; break; 14760 case tok::plus: Opc = UO_Plus; break; 14761 case tok::minus: Opc = UO_Minus; break; 14762 case tok::tilde: Opc = UO_Not; break; 14763 case tok::exclaim: Opc = UO_LNot; break; 14764 case tok::kw___real: Opc = UO_Real; break; 14765 case tok::kw___imag: Opc = UO_Imag; break; 14766 case tok::kw___extension__: Opc = UO_Extension; break; 14767 } 14768 return Opc; 14769 } 14770 14771 const FieldDecl * 14772 Sema::getSelfAssignmentClassMemberCandidate(const ValueDecl *SelfAssigned) { 14773 // Explore the case for adding 'this->' to the LHS of a self assignment, very 14774 // common for setters. 14775 // struct A { 14776 // int X; 14777 // -void setX(int X) { X = X; } 14778 // +void setX(int X) { this->X = X; } 14779 // }; 14780 14781 // Only consider parameters for self assignment fixes. 14782 if (!isa<ParmVarDecl>(SelfAssigned)) 14783 return nullptr; 14784 const auto *Method = 14785 dyn_cast_or_null<CXXMethodDecl>(getCurFunctionDecl(true)); 14786 if (!Method) 14787 return nullptr; 14788 14789 const CXXRecordDecl *Parent = Method->getParent(); 14790 // In theory this is fixable if the lambda explicitly captures this, but 14791 // that's added complexity that's rarely going to be used. 14792 if (Parent->isLambda()) 14793 return nullptr; 14794 14795 // FIXME: Use an actual Lookup operation instead of just traversing fields 14796 // in order to get base class fields. 14797 auto Field = 14798 llvm::find_if(Parent->fields(), 14799 [Name(SelfAssigned->getDeclName())](const FieldDecl *F) { 14800 return F->getDeclName() == Name; 14801 }); 14802 return (Field != Parent->field_end()) ? *Field : nullptr; 14803 } 14804 14805 /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. 14806 /// This warning suppressed in the event of macro expansions. 14807 static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, 14808 SourceLocation OpLoc, bool IsBuiltin) { 14809 if (S.inTemplateInstantiation()) 14810 return; 14811 if (S.isUnevaluatedContext()) 14812 return; 14813 if (OpLoc.isInvalid() || OpLoc.isMacroID()) 14814 return; 14815 LHSExpr = LHSExpr->IgnoreParenImpCasts(); 14816 RHSExpr = RHSExpr->IgnoreParenImpCasts(); 14817 const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); 14818 const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); 14819 if (!LHSDeclRef || !RHSDeclRef || 14820 LHSDeclRef->getLocation().isMacroID() || 14821 RHSDeclRef->getLocation().isMacroID()) 14822 return; 14823 const ValueDecl *LHSDecl = 14824 cast<ValueDecl>(LHSDeclRef->getDecl()->getCanonicalDecl()); 14825 const ValueDecl *RHSDecl = 14826 cast<ValueDecl>(RHSDeclRef->getDecl()->getCanonicalDecl()); 14827 if (LHSDecl != RHSDecl) 14828 return; 14829 if (LHSDecl->getType().isVolatileQualified()) 14830 return; 14831 if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) 14832 if (RefTy->getPointeeType().isVolatileQualified()) 14833 return; 14834 14835 auto Diag = S.Diag(OpLoc, IsBuiltin ? diag::warn_self_assignment_builtin 14836 : diag::warn_self_assignment_overloaded) 14837 << LHSDeclRef->getType() << LHSExpr->getSourceRange() 14838 << RHSExpr->getSourceRange(); 14839 if (const FieldDecl *SelfAssignField = 14840 S.getSelfAssignmentClassMemberCandidate(RHSDecl)) 14841 Diag << 1 << SelfAssignField 14842 << FixItHint::CreateInsertion(LHSDeclRef->getBeginLoc(), "this->"); 14843 else 14844 Diag << 0; 14845 } 14846 14847 /// Check if a bitwise-& is performed on an Objective-C pointer. This 14848 /// is usually indicative of introspection within the Objective-C pointer. 14849 static void checkObjCPointerIntrospection(Sema &S, ExprResult &L, ExprResult &R, 14850 SourceLocation OpLoc) { 14851 if (!S.getLangOpts().ObjC) 14852 return; 14853 14854 const Expr *ObjCPointerExpr = nullptr, *OtherExpr = nullptr; 14855 const Expr *LHS = L.get(); 14856 const Expr *RHS = R.get(); 14857 14858 if (LHS->IgnoreParenCasts()->getType()->isObjCObjectPointerType()) { 14859 ObjCPointerExpr = LHS; 14860 OtherExpr = RHS; 14861 } 14862 else if (RHS->IgnoreParenCasts()->getType()->isObjCObjectPointerType()) { 14863 ObjCPointerExpr = RHS; 14864 OtherExpr = LHS; 14865 } 14866 14867 // This warning is deliberately made very specific to reduce false 14868 // positives with logic that uses '&' for hashing. This logic mainly 14869 // looks for code trying to introspect into tagged pointers, which 14870 // code should generally never do. 14871 if (ObjCPointerExpr && isa<IntegerLiteral>(OtherExpr->IgnoreParenCasts())) { 14872 unsigned Diag = diag::warn_objc_pointer_masking; 14873 // Determine if we are introspecting the result of performSelectorXXX. 14874 const Expr *Ex = ObjCPointerExpr->IgnoreParenCasts(); 14875 // Special case messages to -performSelector and friends, which 14876 // can return non-pointer values boxed in a pointer value. 14877 // Some clients may wish to silence warnings in this subcase. 14878 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(Ex)) { 14879 Selector S = ME->getSelector(); 14880 StringRef SelArg0 = S.getNameForSlot(0); 14881 if (SelArg0.starts_with("performSelector")) 14882 Diag = diag::warn_objc_pointer_masking_performSelector; 14883 } 14884 14885 S.Diag(OpLoc, Diag) 14886 << ObjCPointerExpr->getSourceRange(); 14887 } 14888 } 14889 14890 // This helper function promotes a binary operator's operands (which are of a 14891 // half vector type) to a vector of floats and then truncates the result to 14892 // a vector of either half or short. 14893 static ExprResult convertHalfVecBinOp(Sema &S, ExprResult LHS, ExprResult RHS, 14894 BinaryOperatorKind Opc, QualType ResultTy, 14895 ExprValueKind VK, ExprObjectKind OK, 14896 bool IsCompAssign, SourceLocation OpLoc, 14897 FPOptionsOverride FPFeatures) { 14898 auto &Context = S.getASTContext(); 14899 assert((isVector(ResultTy, Context.HalfTy) || 14900 isVector(ResultTy, Context.ShortTy)) && 14901 "Result must be a vector of half or short"); 14902 assert(isVector(LHS.get()->getType(), Context.HalfTy) && 14903 isVector(RHS.get()->getType(), Context.HalfTy) && 14904 "both operands expected to be a half vector"); 14905 14906 RHS = convertVector(RHS.get(), Context.FloatTy, S); 14907 QualType BinOpResTy = RHS.get()->getType(); 14908 14909 // If Opc is a comparison, ResultType is a vector of shorts. In that case, 14910 // change BinOpResTy to a vector of ints. 14911 if (isVector(ResultTy, Context.ShortTy)) 14912 BinOpResTy = S.GetSignedVectorType(BinOpResTy); 14913 14914 if (IsCompAssign) 14915 return CompoundAssignOperator::Create(Context, LHS.get(), RHS.get(), Opc, 14916 ResultTy, VK, OK, OpLoc, FPFeatures, 14917 BinOpResTy, BinOpResTy); 14918 14919 LHS = convertVector(LHS.get(), Context.FloatTy, S); 14920 auto *BO = BinaryOperator::Create(Context, LHS.get(), RHS.get(), Opc, 14921 BinOpResTy, VK, OK, OpLoc, FPFeatures); 14922 return convertVector(BO, ResultTy->castAs<VectorType>()->getElementType(), S); 14923 } 14924 14925 /// Returns true if conversion between vectors of halfs and vectors of floats 14926 /// is needed. 14927 static bool needsConversionOfHalfVec(bool OpRequiresConversion, ASTContext &Ctx, 14928 Expr *E0, Expr *E1 = nullptr) { 14929 if (!OpRequiresConversion || Ctx.getLangOpts().NativeHalfType || 14930 Ctx.getTargetInfo().useFP16ConversionIntrinsics()) 14931 return false; 14932 14933 auto HasVectorOfHalfType = [&Ctx](Expr *E) { 14934 QualType Ty = E->IgnoreImplicit()->getType(); 14935 14936 // Don't promote half precision neon vectors like float16x4_t in arm_neon.h 14937 // to vectors of floats. Although the element type of the vectors is __fp16, 14938 // the vectors shouldn't be treated as storage-only types. See the 14939 // discussion here: https://reviews.llvm.org/rG825235c140e7 14940 if (const VectorType *VT = Ty->getAs<VectorType>()) { 14941 if (VT->getVectorKind() == VectorKind::Neon) 14942 return false; 14943 return VT->getElementType().getCanonicalType() == Ctx.HalfTy; 14944 } 14945 return false; 14946 }; 14947 14948 return HasVectorOfHalfType(E0) && (!E1 || HasVectorOfHalfType(E1)); 14949 } 14950 14951 ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, 14952 BinaryOperatorKind Opc, Expr *LHSExpr, 14953 Expr *RHSExpr, bool ForFoldExpression) { 14954 if (getLangOpts().CPlusPlus11 && isa<InitListExpr>(RHSExpr)) { 14955 // The syntax only allows initializer lists on the RHS of assignment, 14956 // so we don't need to worry about accepting invalid code for 14957 // non-assignment operators. 14958 // C++11 5.17p9: 14959 // The meaning of x = {v} [...] is that of x = T(v) [...]. The meaning 14960 // of x = {} is x = T(). 14961 InitializationKind Kind = InitializationKind::CreateDirectList( 14962 RHSExpr->getBeginLoc(), RHSExpr->getBeginLoc(), RHSExpr->getEndLoc()); 14963 InitializedEntity Entity = 14964 InitializedEntity::InitializeTemporary(LHSExpr->getType()); 14965 InitializationSequence InitSeq(*this, Entity, Kind, RHSExpr); 14966 ExprResult Init = InitSeq.Perform(*this, Entity, Kind, RHSExpr); 14967 if (Init.isInvalid()) 14968 return Init; 14969 RHSExpr = Init.get(); 14970 } 14971 14972 ExprResult LHS = LHSExpr, RHS = RHSExpr; 14973 QualType ResultTy; // Result type of the binary operator. 14974 // The following two variables are used for compound assignment operators 14975 QualType CompLHSTy; // Type of LHS after promotions for computation 14976 QualType CompResultTy; // Type of computation result 14977 ExprValueKind VK = VK_PRValue; 14978 ExprObjectKind OK = OK_Ordinary; 14979 bool ConvertHalfVec = false; 14980 14981 if (!LHS.isUsable() || !RHS.isUsable()) 14982 return ExprError(); 14983 14984 if (getLangOpts().OpenCL) { 14985 QualType LHSTy = LHSExpr->getType(); 14986 QualType RHSTy = RHSExpr->getType(); 14987 // OpenCLC v2.0 s6.13.11.1 allows atomic variables to be initialized by 14988 // the ATOMIC_VAR_INIT macro. 14989 if (LHSTy->isAtomicType() || RHSTy->isAtomicType()) { 14990 SourceRange SR(LHSExpr->getBeginLoc(), RHSExpr->getEndLoc()); 14991 if (BO_Assign == Opc) 14992 Diag(OpLoc, diag::err_opencl_atomic_init) << 0 << SR; 14993 else 14994 ResultTy = InvalidOperands(OpLoc, LHS, RHS); 14995 return ExprError(); 14996 } 14997 14998 // OpenCL special types - image, sampler, pipe, and blocks are to be used 14999 // only with a builtin functions and therefore should be disallowed here. 15000 if (LHSTy->isImageType() || RHSTy->isImageType() || 15001 LHSTy->isSamplerT() || RHSTy->isSamplerT() || 15002 LHSTy->isPipeType() || RHSTy->isPipeType() || 15003 LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { 15004 ResultTy = InvalidOperands(OpLoc, LHS, RHS); 15005 return ExprError(); 15006 } 15007 } 15008 15009 checkTypeSupport(LHSExpr->getType(), OpLoc, /*ValueDecl*/ nullptr); 15010 checkTypeSupport(RHSExpr->getType(), OpLoc, /*ValueDecl*/ nullptr); 15011 15012 switch (Opc) { 15013 case BO_Assign: 15014 ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType(), Opc); 15015 if (getLangOpts().CPlusPlus && 15016 LHS.get()->getObjectKind() != OK_ObjCProperty) { 15017 VK = LHS.get()->getValueKind(); 15018 OK = LHS.get()->getObjectKind(); 15019 } 15020 if (!ResultTy.isNull()) { 15021 DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc, true); 15022 DiagnoseSelfMove(LHS.get(), RHS.get(), OpLoc); 15023 15024 // Avoid copying a block to the heap if the block is assigned to a local 15025 // auto variable that is declared in the same scope as the block. This 15026 // optimization is unsafe if the local variable is declared in an outer 15027 // scope. For example: 15028 // 15029 // BlockTy b; 15030 // { 15031 // b = ^{...}; 15032 // } 15033 // // It is unsafe to invoke the block here if it wasn't copied to the 15034 // // heap. 15035 // b(); 15036 15037 if (auto *BE = dyn_cast<BlockExpr>(RHS.get()->IgnoreParens())) 15038 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParens())) 15039 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 15040 if (VD->hasLocalStorage() && getCurScope()->isDeclScope(VD)) 15041 BE->getBlockDecl()->setCanAvoidCopyToHeap(); 15042 15043 if (LHS.get()->getType().hasNonTrivialToPrimitiveCopyCUnion()) 15044 checkNonTrivialCUnion(LHS.get()->getType(), LHS.get()->getExprLoc(), 15045 NonTrivialCUnionContext::Assignment, NTCUK_Copy); 15046 } 15047 RecordModifiableNonNullParam(*this, LHS.get()); 15048 break; 15049 case BO_PtrMemD: 15050 case BO_PtrMemI: 15051 ResultTy = CheckPointerToMemberOperands(LHS, RHS, VK, OpLoc, 15052 Opc == BO_PtrMemI); 15053 break; 15054 case BO_Mul: 15055 case BO_Div: 15056 ConvertHalfVec = true; 15057 ResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, false, 15058 Opc == BO_Div); 15059 break; 15060 case BO_Rem: 15061 ResultTy = CheckRemainderOperands(LHS, RHS, OpLoc); 15062 break; 15063 case BO_Add: 15064 ConvertHalfVec = true; 15065 ResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc); 15066 break; 15067 case BO_Sub: 15068 ConvertHalfVec = true; 15069 ResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc); 15070 break; 15071 case BO_Shl: 15072 case BO_Shr: 15073 ResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc); 15074 break; 15075 case BO_LE: 15076 case BO_LT: 15077 case BO_GE: 15078 case BO_GT: 15079 ConvertHalfVec = true; 15080 ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc); 15081 15082 if (const auto *BI = dyn_cast<BinaryOperator>(LHSExpr); 15083 !ForFoldExpression && BI && BI->isComparisonOp()) 15084 Diag(OpLoc, diag::warn_consecutive_comparison) 15085 << BI->getOpcodeStr() << BinaryOperator::getOpcodeStr(Opc); 15086 15087 break; 15088 case BO_EQ: 15089 case BO_NE: 15090 ConvertHalfVec = true; 15091 ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc); 15092 break; 15093 case BO_Cmp: 15094 ConvertHalfVec = true; 15095 ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc); 15096 assert(ResultTy.isNull() || ResultTy->getAsCXXRecordDecl()); 15097 break; 15098 case BO_And: 15099 checkObjCPointerIntrospection(*this, LHS, RHS, OpLoc); 15100 [[fallthrough]]; 15101 case BO_Xor: 15102 case BO_Or: 15103 ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, Opc); 15104 break; 15105 case BO_LAnd: 15106 case BO_LOr: 15107 ConvertHalfVec = true; 15108 ResultTy = CheckLogicalOperands(LHS, RHS, OpLoc, Opc); 15109 break; 15110 case BO_MulAssign: 15111 case BO_DivAssign: 15112 ConvertHalfVec = true; 15113 CompResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, true, 15114 Opc == BO_DivAssign); 15115 CompLHSTy = CompResultTy; 15116 if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) 15117 ResultTy = 15118 CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy, Opc); 15119 break; 15120 case BO_RemAssign: 15121 CompResultTy = CheckRemainderOperands(LHS, RHS, OpLoc, true); 15122 CompLHSTy = CompResultTy; 15123 if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) 15124 ResultTy = 15125 CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy, Opc); 15126 break; 15127 case BO_AddAssign: 15128 ConvertHalfVec = true; 15129 CompResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc, &CompLHSTy); 15130 if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) 15131 ResultTy = 15132 CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy, Opc); 15133 break; 15134 case BO_SubAssign: 15135 ConvertHalfVec = true; 15136 CompResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc, &CompLHSTy); 15137 if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) 15138 ResultTy = 15139 CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy, Opc); 15140 break; 15141 case BO_ShlAssign: 15142 case BO_ShrAssign: 15143 CompResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc, true); 15144 CompLHSTy = CompResultTy; 15145 if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) 15146 ResultTy = 15147 CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy, Opc); 15148 break; 15149 case BO_AndAssign: 15150 case BO_OrAssign: // fallthrough 15151 DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc, true); 15152 [[fallthrough]]; 15153 case BO_XorAssign: 15154 CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, Opc); 15155 CompLHSTy = CompResultTy; 15156 if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) 15157 ResultTy = 15158 CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy, Opc); 15159 break; 15160 case BO_Comma: 15161 ResultTy = CheckCommaOperands(*this, LHS, RHS, OpLoc); 15162 if (getLangOpts().CPlusPlus && !RHS.isInvalid()) { 15163 VK = RHS.get()->getValueKind(); 15164 OK = RHS.get()->getObjectKind(); 15165 } 15166 break; 15167 } 15168 if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid()) 15169 return ExprError(); 15170 15171 // Some of the binary operations require promoting operands of half vector to 15172 // float vectors and truncating the result back to half vector. For now, we do 15173 // this only when HalfArgsAndReturn is set (that is, when the target is arm or 15174 // arm64). 15175 assert( 15176 (Opc == BO_Comma || isVector(RHS.get()->getType(), Context.HalfTy) == 15177 isVector(LHS.get()->getType(), Context.HalfTy)) && 15178 "both sides are half vectors or neither sides are"); 15179 ConvertHalfVec = 15180 needsConversionOfHalfVec(ConvertHalfVec, Context, LHS.get(), RHS.get()); 15181 15182 // Check for array bounds violations for both sides of the BinaryOperator 15183 CheckArrayAccess(LHS.get()); 15184 CheckArrayAccess(RHS.get()); 15185 15186 if (const ObjCIsaExpr *OISA = dyn_cast<ObjCIsaExpr>(LHS.get()->IgnoreParenCasts())) { 15187 NamedDecl *ObjectSetClass = LookupSingleName(TUScope, 15188 &Context.Idents.get("object_setClass"), 15189 SourceLocation(), LookupOrdinaryName); 15190 if (ObjectSetClass && isa<ObjCIsaExpr>(LHS.get())) { 15191 SourceLocation RHSLocEnd = getLocForEndOfToken(RHS.get()->getEndLoc()); 15192 Diag(LHS.get()->getExprLoc(), diag::warn_objc_isa_assign) 15193 << FixItHint::CreateInsertion(LHS.get()->getBeginLoc(), 15194 "object_setClass(") 15195 << FixItHint::CreateReplacement(SourceRange(OISA->getOpLoc(), OpLoc), 15196 ",") 15197 << FixItHint::CreateInsertion(RHSLocEnd, ")"); 15198 } 15199 else 15200 Diag(LHS.get()->getExprLoc(), diag::warn_objc_isa_assign); 15201 } 15202 else if (const ObjCIvarRefExpr *OIRE = 15203 dyn_cast<ObjCIvarRefExpr>(LHS.get()->IgnoreParenCasts())) 15204 DiagnoseDirectIsaAccess(*this, OIRE, OpLoc, RHS.get()); 15205 15206 // Opc is not a compound assignment if CompResultTy is null. 15207 if (CompResultTy.isNull()) { 15208 if (ConvertHalfVec) 15209 return convertHalfVecBinOp(*this, LHS, RHS, Opc, ResultTy, VK, OK, false, 15210 OpLoc, CurFPFeatureOverrides()); 15211 return BinaryOperator::Create(Context, LHS.get(), RHS.get(), Opc, ResultTy, 15212 VK, OK, OpLoc, CurFPFeatureOverrides()); 15213 } 15214 15215 // Handle compound assignments. 15216 if (getLangOpts().CPlusPlus && LHS.get()->getObjectKind() != 15217 OK_ObjCProperty) { 15218 VK = VK_LValue; 15219 OK = LHS.get()->getObjectKind(); 15220 } 15221 15222 // The LHS is not converted to the result type for fixed-point compound 15223 // assignment as the common type is computed on demand. Reset the CompLHSTy 15224 // to the LHS type we would have gotten after unary conversions. 15225 if (CompResultTy->isFixedPointType()) 15226 CompLHSTy = UsualUnaryConversions(LHS.get()).get()->getType(); 15227 15228 if (ConvertHalfVec) 15229 return convertHalfVecBinOp(*this, LHS, RHS, Opc, ResultTy, VK, OK, true, 15230 OpLoc, CurFPFeatureOverrides()); 15231 15232 return CompoundAssignOperator::Create( 15233 Context, LHS.get(), RHS.get(), Opc, ResultTy, VK, OK, OpLoc, 15234 CurFPFeatureOverrides(), CompLHSTy, CompResultTy); 15235 } 15236 15237 /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison 15238 /// operators are mixed in a way that suggests that the programmer forgot that 15239 /// comparison operators have higher precedence. The most typical example of 15240 /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". 15241 static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, 15242 SourceLocation OpLoc, Expr *LHSExpr, 15243 Expr *RHSExpr) { 15244 BinaryOperator *LHSBO = dyn_cast<BinaryOperator>(LHSExpr); 15245 BinaryOperator *RHSBO = dyn_cast<BinaryOperator>(RHSExpr); 15246 15247 // Check that one of the sides is a comparison operator and the other isn't. 15248 bool isLeftComp = LHSBO && LHSBO->isComparisonOp(); 15249 bool isRightComp = RHSBO && RHSBO->isComparisonOp(); 15250 if (isLeftComp == isRightComp) 15251 return; 15252 15253 // Bitwise operations are sometimes used as eager logical ops. 15254 // Don't diagnose this. 15255 bool isLeftBitwise = LHSBO && LHSBO->isBitwiseOp(); 15256 bool isRightBitwise = RHSBO && RHSBO->isBitwiseOp(); 15257 if (isLeftBitwise || isRightBitwise) 15258 return; 15259 15260 SourceRange DiagRange = isLeftComp 15261 ? SourceRange(LHSExpr->getBeginLoc(), OpLoc) 15262 : SourceRange(OpLoc, RHSExpr->getEndLoc()); 15263 StringRef OpStr = isLeftComp ? LHSBO->getOpcodeStr() : RHSBO->getOpcodeStr(); 15264 SourceRange ParensRange = 15265 isLeftComp 15266 ? SourceRange(LHSBO->getRHS()->getBeginLoc(), RHSExpr->getEndLoc()) 15267 : SourceRange(LHSExpr->getBeginLoc(), RHSBO->getLHS()->getEndLoc()); 15268 15269 Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) 15270 << DiagRange << BinaryOperator::getOpcodeStr(Opc) << OpStr; 15271 SuggestParentheses(Self, OpLoc, 15272 Self.PDiag(diag::note_precedence_silence) << OpStr, 15273 (isLeftComp ? LHSExpr : RHSExpr)->getSourceRange()); 15274 SuggestParentheses(Self, OpLoc, 15275 Self.PDiag(diag::note_precedence_bitwise_first) 15276 << BinaryOperator::getOpcodeStr(Opc), 15277 ParensRange); 15278 } 15279 15280 /// It accepts a '&&' expr that is inside a '||' one. 15281 /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression 15282 /// in parentheses. 15283 static void 15284 EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, 15285 BinaryOperator *Bop) { 15286 assert(Bop->getOpcode() == BO_LAnd); 15287 Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) 15288 << Bop->getSourceRange() << OpLoc; 15289 SuggestParentheses(Self, Bop->getOperatorLoc(), 15290 Self.PDiag(diag::note_precedence_silence) 15291 << Bop->getOpcodeStr(), 15292 Bop->getSourceRange()); 15293 } 15294 15295 /// Look for '&&' in the left hand of a '||' expr. 15296 static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, 15297 Expr *LHSExpr, Expr *RHSExpr) { 15298 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(LHSExpr)) { 15299 if (Bop->getOpcode() == BO_LAnd) { 15300 // If it's "string_literal && a || b" don't warn since the precedence 15301 // doesn't matter. 15302 if (!isa<StringLiteral>(Bop->getLHS()->IgnoreParenImpCasts())) 15303 return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); 15304 } else if (Bop->getOpcode() == BO_LOr) { 15305 if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { 15306 // If it's "a || b && string_literal || c" we didn't warn earlier for 15307 // "a || b && string_literal", but warn now. 15308 if (RBop->getOpcode() == BO_LAnd && 15309 isa<StringLiteral>(RBop->getRHS()->IgnoreParenImpCasts())) 15310 return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); 15311 } 15312 } 15313 } 15314 } 15315 15316 /// Look for '&&' in the right hand of a '||' expr. 15317 static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, 15318 Expr *LHSExpr, Expr *RHSExpr) { 15319 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(RHSExpr)) { 15320 if (Bop->getOpcode() == BO_LAnd) { 15321 // If it's "a || b && string_literal" don't warn since the precedence 15322 // doesn't matter. 15323 if (!isa<StringLiteral>(Bop->getRHS()->IgnoreParenImpCasts())) 15324 return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); 15325 } 15326 } 15327 } 15328 15329 /// Look for bitwise op in the left or right hand of a bitwise op with 15330 /// lower precedence and emit a diagnostic together with a fixit hint that wraps 15331 /// the '&' expression in parentheses. 15332 static void DiagnoseBitwiseOpInBitwiseOp(Sema &S, BinaryOperatorKind Opc, 15333 SourceLocation OpLoc, Expr *SubExpr) { 15334 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(SubExpr)) { 15335 if (Bop->isBitwiseOp() && Bop->getOpcode() < Opc) { 15336 S.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_op_in_bitwise_op) 15337 << Bop->getOpcodeStr() << BinaryOperator::getOpcodeStr(Opc) 15338 << Bop->getSourceRange() << OpLoc; 15339 SuggestParentheses(S, Bop->getOperatorLoc(), 15340 S.PDiag(diag::note_precedence_silence) 15341 << Bop->getOpcodeStr(), 15342 Bop->getSourceRange()); 15343 } 15344 } 15345 } 15346 15347 static void DiagnoseAdditionInShift(Sema &S, SourceLocation OpLoc, 15348 Expr *SubExpr, StringRef Shift) { 15349 if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(SubExpr)) { 15350 if (Bop->getOpcode() == BO_Add || Bop->getOpcode() == BO_Sub) { 15351 StringRef Op = Bop->getOpcodeStr(); 15352 S.Diag(Bop->getOperatorLoc(), diag::warn_addition_in_bitshift) 15353 << Bop->getSourceRange() << OpLoc << Shift << Op; 15354 SuggestParentheses(S, Bop->getOperatorLoc(), 15355 S.PDiag(diag::note_precedence_silence) << Op, 15356 Bop->getSourceRange()); 15357 } 15358 } 15359 } 15360 15361 static void DiagnoseShiftCompare(Sema &S, SourceLocation OpLoc, 15362 Expr *LHSExpr, Expr *RHSExpr) { 15363 CXXOperatorCallExpr *OCE = dyn_cast<CXXOperatorCallExpr>(LHSExpr); 15364 if (!OCE) 15365 return; 15366 15367 FunctionDecl *FD = OCE->getDirectCallee(); 15368 if (!FD || !FD->isOverloadedOperator()) 15369 return; 15370 15371 OverloadedOperatorKind Kind = FD->getOverloadedOperator(); 15372 if (Kind != OO_LessLess && Kind != OO_GreaterGreater) 15373 return; 15374 15375 S.Diag(OpLoc, diag::warn_overloaded_shift_in_comparison) 15376 << LHSExpr->getSourceRange() << RHSExpr->getSourceRange() 15377 << (Kind == OO_LessLess); 15378 SuggestParentheses(S, OCE->getOperatorLoc(), 15379 S.PDiag(diag::note_precedence_silence) 15380 << (Kind == OO_LessLess ? "<<" : ">>"), 15381 OCE->getSourceRange()); 15382 SuggestParentheses( 15383 S, OpLoc, S.PDiag(diag::note_evaluate_comparison_first), 15384 SourceRange(OCE->getArg(1)->getBeginLoc(), RHSExpr->getEndLoc())); 15385 } 15386 15387 /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky 15388 /// precedence. 15389 static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, 15390 SourceLocation OpLoc, Expr *LHSExpr, 15391 Expr *RHSExpr){ 15392 // Diagnose "arg1 'bitwise' arg2 'eq' arg3". 15393 if (BinaryOperator::isBitwiseOp(Opc)) 15394 DiagnoseBitwisePrecedence(Self, Opc, OpLoc, LHSExpr, RHSExpr); 15395 15396 // Diagnose "arg1 & arg2 | arg3" 15397 if ((Opc == BO_Or || Opc == BO_Xor) && 15398 !OpLoc.isMacroID()/* Don't warn in macros. */) { 15399 DiagnoseBitwiseOpInBitwiseOp(Self, Opc, OpLoc, LHSExpr); 15400 DiagnoseBitwiseOpInBitwiseOp(Self, Opc, OpLoc, RHSExpr); 15401 } 15402 15403 // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. 15404 // We don't warn for 'assert(a || b && "bad")' since this is safe. 15405 if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { 15406 DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); 15407 DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); 15408 } 15409 15410 if ((Opc == BO_Shl && LHSExpr->getType()->isIntegralType(Self.getASTContext())) 15411 || Opc == BO_Shr) { 15412 StringRef Shift = BinaryOperator::getOpcodeStr(Opc); 15413 DiagnoseAdditionInShift(Self, OpLoc, LHSExpr, Shift); 15414 DiagnoseAdditionInShift(Self, OpLoc, RHSExpr, Shift); 15415 } 15416 15417 // Warn on overloaded shift operators and comparisons, such as: 15418 // cout << 5 == 4; 15419 if (BinaryOperator::isComparisonOp(Opc)) 15420 DiagnoseShiftCompare(Self, OpLoc, LHSExpr, RHSExpr); 15421 } 15422 15423 ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, 15424 tok::TokenKind Kind, 15425 Expr *LHSExpr, Expr *RHSExpr) { 15426 BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); 15427 assert(LHSExpr && "ActOnBinOp(): missing left expression"); 15428 assert(RHSExpr && "ActOnBinOp(): missing right expression"); 15429 15430 // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" 15431 DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); 15432 15433 BuiltinCountedByRefKind K = BinaryOperator::isAssignmentOp(Opc) 15434 ? BuiltinCountedByRefKind::Assignment 15435 : BuiltinCountedByRefKind::BinaryExpr; 15436 15437 CheckInvalidBuiltinCountedByRef(LHSExpr, K); 15438 CheckInvalidBuiltinCountedByRef(RHSExpr, K); 15439 15440 return BuildBinOp(S, TokLoc, Opc, LHSExpr, RHSExpr); 15441 } 15442 15443 void Sema::LookupBinOp(Scope *S, SourceLocation OpLoc, BinaryOperatorKind Opc, 15444 UnresolvedSetImpl &Functions) { 15445 OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); 15446 if (OverOp != OO_None && OverOp != OO_Equal) 15447 LookupOverloadedOperatorName(OverOp, S, Functions); 15448 15449 // In C++20 onwards, we may have a second operator to look up. 15450 if (getLangOpts().CPlusPlus20) { 15451 if (OverloadedOperatorKind ExtraOp = getRewrittenOverloadedOperator(OverOp)) 15452 LookupOverloadedOperatorName(ExtraOp, S, Functions); 15453 } 15454 } 15455 15456 /// Build an overloaded binary operator expression in the given scope. 15457 static ExprResult BuildOverloadedBinOp(Sema &S, Scope *Sc, SourceLocation OpLoc, 15458 BinaryOperatorKind Opc, 15459 Expr *LHS, Expr *RHS) { 15460 switch (Opc) { 15461 case BO_Assign: 15462 // In the non-overloaded case, we warn about self-assignment (x = x) for 15463 // both simple assignment and certain compound assignments where algebra 15464 // tells us the operation yields a constant result. When the operator is 15465 // overloaded, we can't do the latter because we don't want to assume that 15466 // those algebraic identities still apply; for example, a path-building 15467 // library might use operator/= to append paths. But it's still reasonable 15468 // to assume that simple assignment is just moving/copying values around 15469 // and so self-assignment is likely a bug. 15470 DiagnoseSelfAssignment(S, LHS, RHS, OpLoc, false); 15471 [[fallthrough]]; 15472 case BO_DivAssign: 15473 case BO_RemAssign: 15474 case BO_SubAssign: 15475 case BO_AndAssign: 15476 case BO_OrAssign: 15477 case BO_XorAssign: 15478 CheckIdentityFieldAssignment(LHS, RHS, OpLoc, S); 15479 break; 15480 default: 15481 break; 15482 } 15483 15484 // Find all of the overloaded operators visible from this point. 15485 UnresolvedSet<16> Functions; 15486 S.LookupBinOp(Sc, OpLoc, Opc, Functions); 15487 15488 // Build the (potentially-overloaded, potentially-dependent) 15489 // binary operation. 15490 return S.CreateOverloadedBinOp(OpLoc, Opc, Functions, LHS, RHS); 15491 } 15492 15493 ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, 15494 BinaryOperatorKind Opc, Expr *LHSExpr, 15495 Expr *RHSExpr, bool ForFoldExpression) { 15496 if (!LHSExpr || !RHSExpr) 15497 return ExprError(); 15498 15499 // We want to end up calling one of SemaPseudoObject::checkAssignment 15500 // (if the LHS is a pseudo-object), BuildOverloadedBinOp (if 15501 // both expressions are overloadable or either is type-dependent), 15502 // or CreateBuiltinBinOp (in any other case). We also want to get 15503 // any placeholder types out of the way. 15504 15505 // Handle pseudo-objects in the LHS. 15506 if (const BuiltinType *pty = LHSExpr->getType()->getAsPlaceholderType()) { 15507 // Assignments with a pseudo-object l-value need special analysis. 15508 if (pty->getKind() == BuiltinType::PseudoObject && 15509 BinaryOperator::isAssignmentOp(Opc)) 15510 return PseudoObject().checkAssignment(S, OpLoc, Opc, LHSExpr, RHSExpr); 15511 15512 // Don't resolve overloads if the other type is overloadable. 15513 if (getLangOpts().CPlusPlus && pty->getKind() == BuiltinType::Overload) { 15514 // We can't actually test that if we still have a placeholder, 15515 // though. Fortunately, none of the exceptions we see in that 15516 // code below are valid when the LHS is an overload set. Note 15517 // that an overload set can be dependently-typed, but it never 15518 // instantiates to having an overloadable type. 15519 ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); 15520 if (resolvedRHS.isInvalid()) return ExprError(); 15521 RHSExpr = resolvedRHS.get(); 15522 15523 if (RHSExpr->isTypeDependent() || 15524 RHSExpr->getType()->isOverloadableType()) 15525 return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); 15526 } 15527 15528 // If we're instantiating "a.x < b" or "A::x < b" and 'x' names a function 15529 // template, diagnose the missing 'template' keyword instead of diagnosing 15530 // an invalid use of a bound member function. 15531 // 15532 // Note that "A::x < b" might be valid if 'b' has an overloadable type due 15533 // to C++1z [over.over]/1.4, but we already checked for that case above. 15534 if (Opc == BO_LT && inTemplateInstantiation() && 15535 (pty->getKind() == BuiltinType::BoundMember || 15536 pty->getKind() == BuiltinType::Overload)) { 15537 auto *OE = dyn_cast<OverloadExpr>(LHSExpr); 15538 if (OE && !OE->hasTemplateKeyword() && !OE->hasExplicitTemplateArgs() && 15539 llvm::any_of(OE->decls(), [](NamedDecl *ND) { 15540 return isa<FunctionTemplateDecl>(ND); 15541 })) { 15542 Diag(OE->getQualifier() ? OE->getQualifierLoc().getBeginLoc() 15543 : OE->getNameLoc(), 15544 diag::err_template_kw_missing) 15545 << OE->getName().getAsIdentifierInfo(); 15546 return ExprError(); 15547 } 15548 } 15549 15550 ExprResult LHS = CheckPlaceholderExpr(LHSExpr); 15551 if (LHS.isInvalid()) return ExprError(); 15552 LHSExpr = LHS.get(); 15553 } 15554 15555 // Handle pseudo-objects in the RHS. 15556 if (const BuiltinType *pty = RHSExpr->getType()->getAsPlaceholderType()) { 15557 // An overload in the RHS can potentially be resolved by the type 15558 // being assigned to. 15559 if (Opc == BO_Assign && pty->getKind() == BuiltinType::Overload) { 15560 if (getLangOpts().CPlusPlus && 15561 (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent() || 15562 LHSExpr->getType()->isOverloadableType())) 15563 return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); 15564 15565 return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr, 15566 ForFoldExpression); 15567 } 15568 15569 // Don't resolve overloads if the other type is overloadable. 15570 if (getLangOpts().CPlusPlus && pty->getKind() == BuiltinType::Overload && 15571 LHSExpr->getType()->isOverloadableType()) 15572 return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); 15573 15574 ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); 15575 if (!resolvedRHS.isUsable()) return ExprError(); 15576 RHSExpr = resolvedRHS.get(); 15577 } 15578 15579 if (getLangOpts().CPlusPlus) { 15580 // Otherwise, build an overloaded op if either expression is type-dependent 15581 // or has an overloadable type. 15582 if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent() || 15583 LHSExpr->getType()->isOverloadableType() || 15584 RHSExpr->getType()->isOverloadableType()) 15585 return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); 15586 } 15587 15588 if (getLangOpts().RecoveryAST && 15589 (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent())) { 15590 assert(!getLangOpts().CPlusPlus); 15591 assert((LHSExpr->containsErrors() || RHSExpr->containsErrors()) && 15592 "Should only occur in error-recovery path."); 15593 if (BinaryOperator::isCompoundAssignmentOp(Opc)) 15594 // C [6.15.16] p3: 15595 // An assignment expression has the value of the left operand after the 15596 // assignment, but is not an lvalue. 15597 return CompoundAssignOperator::Create( 15598 Context, LHSExpr, RHSExpr, Opc, 15599 LHSExpr->getType().getUnqualifiedType(), VK_PRValue, OK_Ordinary, 15600 OpLoc, CurFPFeatureOverrides()); 15601 QualType ResultType; 15602 switch (Opc) { 15603 case BO_Assign: 15604 ResultType = LHSExpr->getType().getUnqualifiedType(); 15605 break; 15606 case BO_LT: 15607 case BO_GT: 15608 case BO_LE: 15609 case BO_GE: 15610 case BO_EQ: 15611 case BO_NE: 15612 case BO_LAnd: 15613 case BO_LOr: 15614 // These operators have a fixed result type regardless of operands. 15615 ResultType = Context.IntTy; 15616 break; 15617 case BO_Comma: 15618 ResultType = RHSExpr->getType(); 15619 break; 15620 default: 15621 ResultType = Context.DependentTy; 15622 break; 15623 } 15624 return BinaryOperator::Create(Context, LHSExpr, RHSExpr, Opc, ResultType, 15625 VK_PRValue, OK_Ordinary, OpLoc, 15626 CurFPFeatureOverrides()); 15627 } 15628 15629 // Build a built-in binary operation. 15630 return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr, ForFoldExpression); 15631 } 15632 15633 static bool isOverflowingIntegerType(ASTContext &Ctx, QualType T) { 15634 if (T.isNull() || T->isDependentType()) 15635 return false; 15636 15637 if (!Ctx.isPromotableIntegerType(T)) 15638 return true; 15639 15640 return Ctx.getIntWidth(T) >= Ctx.getIntWidth(Ctx.IntTy); 15641 } 15642 15643 ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, 15644 UnaryOperatorKind Opc, Expr *InputExpr, 15645 bool IsAfterAmp) { 15646 ExprResult Input = InputExpr; 15647 ExprValueKind VK = VK_PRValue; 15648 ExprObjectKind OK = OK_Ordinary; 15649 QualType resultType; 15650 bool CanOverflow = false; 15651 15652 bool ConvertHalfVec = false; 15653 if (getLangOpts().OpenCL) { 15654 QualType Ty = InputExpr->getType(); 15655 // The only legal unary operation for atomics is '&'. 15656 if ((Opc != UO_AddrOf && Ty->isAtomicType()) || 15657 // OpenCL special types - image, sampler, pipe, and blocks are to be used 15658 // only with a builtin functions and therefore should be disallowed here. 15659 (Ty->isImageType() || Ty->isSamplerT() || Ty->isPipeType() 15660 || Ty->isBlockPointerType())) { 15661 return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) 15662 << InputExpr->getType() 15663 << Input.get()->getSourceRange()); 15664 } 15665 } 15666 15667 if (getLangOpts().HLSL && OpLoc.isValid()) { 15668 if (Opc == UO_AddrOf) 15669 return ExprError(Diag(OpLoc, diag::err_hlsl_operator_unsupported) << 0); 15670 if (Opc == UO_Deref) 15671 return ExprError(Diag(OpLoc, diag::err_hlsl_operator_unsupported) << 1); 15672 } 15673 15674 if (InputExpr->isTypeDependent() && 15675 InputExpr->getType()->isSpecificBuiltinType(BuiltinType::Dependent)) { 15676 resultType = Context.DependentTy; 15677 } else { 15678 switch (Opc) { 15679 case UO_PreInc: 15680 case UO_PreDec: 15681 case UO_PostInc: 15682 case UO_PostDec: 15683 resultType = 15684 CheckIncrementDecrementOperand(*this, Input.get(), VK, OK, OpLoc, 15685 Opc == UO_PreInc || Opc == UO_PostInc, 15686 Opc == UO_PreInc || Opc == UO_PreDec); 15687 CanOverflow = isOverflowingIntegerType(Context, resultType); 15688 break; 15689 case UO_AddrOf: 15690 resultType = CheckAddressOfOperand(Input, OpLoc); 15691 CheckAddressOfNoDeref(InputExpr); 15692 RecordModifiableNonNullParam(*this, InputExpr); 15693 break; 15694 case UO_Deref: { 15695 Input = DefaultFunctionArrayLvalueConversion(Input.get()); 15696 if (Input.isInvalid()) 15697 return ExprError(); 15698 resultType = 15699 CheckIndirectionOperand(*this, Input.get(), VK, OpLoc, IsAfterAmp); 15700 break; 15701 } 15702 case UO_Plus: 15703 case UO_Minus: 15704 CanOverflow = Opc == UO_Minus && 15705 isOverflowingIntegerType(Context, Input.get()->getType()); 15706 Input = UsualUnaryConversions(Input.get()); 15707 if (Input.isInvalid()) 15708 return ExprError(); 15709 // Unary plus and minus require promoting an operand of half vector to a 15710 // float vector and truncating the result back to a half vector. For now, 15711 // we do this only when HalfArgsAndReturns is set (that is, when the 15712 // target is arm or arm64). 15713 ConvertHalfVec = needsConversionOfHalfVec(true, Context, Input.get()); 15714 15715 // If the operand is a half vector, promote it to a float vector. 15716 if (ConvertHalfVec) 15717 Input = convertVector(Input.get(), Context.FloatTy, *this); 15718 resultType = Input.get()->getType(); 15719 if (resultType->isArithmeticType()) // C99 6.5.3.3p1 15720 break; 15721 else if (resultType->isVectorType() && 15722 // The z vector extensions don't allow + or - with bool vectors. 15723 (!Context.getLangOpts().ZVector || 15724 resultType->castAs<VectorType>()->getVectorKind() != 15725 VectorKind::AltiVecBool)) 15726 break; 15727 else if (resultType->isSveVLSBuiltinType()) // SVE vectors allow + and - 15728 break; 15729 else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6 15730 Opc == UO_Plus && resultType->isPointerType()) 15731 break; 15732 15733 return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) 15734 << resultType << Input.get()->getSourceRange()); 15735 15736 case UO_Not: // bitwise complement 15737 Input = UsualUnaryConversions(Input.get()); 15738 if (Input.isInvalid()) 15739 return ExprError(); 15740 resultType = Input.get()->getType(); 15741 // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. 15742 if (resultType->isComplexType() || resultType->isComplexIntegerType()) 15743 // C99 does not support '~' for complex conjugation. 15744 Diag(OpLoc, diag::ext_integer_complement_complex) 15745 << resultType << Input.get()->getSourceRange(); 15746 else if (resultType->hasIntegerRepresentation()) 15747 break; 15748 else if (resultType->isExtVectorType() && Context.getLangOpts().OpenCL) { 15749 // OpenCL v1.1 s6.3.f: The bitwise operator not (~) does not operate 15750 // on vector float types. 15751 QualType T = resultType->castAs<ExtVectorType>()->getElementType(); 15752 if (!T->isIntegerType()) 15753 return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) 15754 << resultType << Input.get()->getSourceRange()); 15755 } else { 15756 return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) 15757 << resultType << Input.get()->getSourceRange()); 15758 } 15759 break; 15760 15761 case UO_LNot: // logical negation 15762 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). 15763 Input = DefaultFunctionArrayLvalueConversion(Input.get()); 15764 if (Input.isInvalid()) 15765 return ExprError(); 15766 resultType = Input.get()->getType(); 15767 15768 // Though we still have to promote half FP to float... 15769 if (resultType->isHalfType() && !Context.getLangOpts().NativeHalfType) { 15770 Input = ImpCastExprToType(Input.get(), Context.FloatTy, CK_FloatingCast) 15771 .get(); 15772 resultType = Context.FloatTy; 15773 } 15774 15775 // WebAsembly tables can't be used in unary expressions. 15776 if (resultType->isPointerType() && 15777 resultType->getPointeeType().isWebAssemblyReferenceType()) { 15778 return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) 15779 << resultType << Input.get()->getSourceRange()); 15780 } 15781 15782 if (resultType->isScalarType() && !isScopedEnumerationType(resultType)) { 15783 // C99 6.5.3.3p1: ok, fallthrough; 15784 if (Context.getLangOpts().CPlusPlus) { 15785 // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: 15786 // operand contextually converted to bool. 15787 Input = ImpCastExprToType(Input.get(), Context.BoolTy, 15788 ScalarTypeToBooleanCastKind(resultType)); 15789 } else if (Context.getLangOpts().OpenCL && 15790 Context.getLangOpts().OpenCLVersion < 120) { 15791 // OpenCL v1.1 6.3.h: The logical operator not (!) does not 15792 // operate on scalar float types. 15793 if (!resultType->isIntegerType() && !resultType->isPointerType()) 15794 return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) 15795 << resultType << Input.get()->getSourceRange()); 15796 } 15797 } else if (resultType->isExtVectorType()) { 15798 if (Context.getLangOpts().OpenCL && 15799 Context.getLangOpts().getOpenCLCompatibleVersion() < 120) { 15800 // OpenCL v1.1 6.3.h: The logical operator not (!) does not 15801 // operate on vector float types. 15802 QualType T = resultType->castAs<ExtVectorType>()->getElementType(); 15803 if (!T->isIntegerType()) 15804 return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) 15805 << resultType << Input.get()->getSourceRange()); 15806 } 15807 // Vector logical not returns the signed variant of the operand type. 15808 resultType = GetSignedVectorType(resultType); 15809 break; 15810 } else if (Context.getLangOpts().CPlusPlus && 15811 resultType->isVectorType()) { 15812 const VectorType *VTy = resultType->castAs<VectorType>(); 15813 if (VTy->getVectorKind() != VectorKind::Generic) 15814 return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) 15815 << resultType << Input.get()->getSourceRange()); 15816 15817 // Vector logical not returns the signed variant of the operand type. 15818 resultType = GetSignedVectorType(resultType); 15819 break; 15820 } else { 15821 return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) 15822 << resultType << Input.get()->getSourceRange()); 15823 } 15824 15825 // LNot always has type int. C99 6.5.3.3p5. 15826 // In C++, it's bool. C++ 5.3.1p8 15827 resultType = Context.getLogicalOperationType(); 15828 break; 15829 case UO_Real: 15830 case UO_Imag: 15831 resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); 15832 // _Real maps ordinary l-values into ordinary l-values. _Imag maps 15833 // ordinary complex l-values to ordinary l-values and all other values to 15834 // r-values. 15835 if (Input.isInvalid()) 15836 return ExprError(); 15837 if (Opc == UO_Real || Input.get()->getType()->isAnyComplexType()) { 15838 if (Input.get()->isGLValue() && 15839 Input.get()->getObjectKind() == OK_Ordinary) 15840 VK = Input.get()->getValueKind(); 15841 } else if (!getLangOpts().CPlusPlus) { 15842 // In C, a volatile scalar is read by __imag. In C++, it is not. 15843 Input = DefaultLvalueConversion(Input.get()); 15844 } 15845 break; 15846 case UO_Extension: 15847 resultType = Input.get()->getType(); 15848 VK = Input.get()->getValueKind(); 15849 OK = Input.get()->getObjectKind(); 15850 break; 15851 case UO_Coawait: 15852 // It's unnecessary to represent the pass-through operator co_await in the 15853 // AST; just return the input expression instead. 15854 assert(!Input.get()->getType()->isDependentType() && 15855 "the co_await expression must be non-dependant before " 15856 "building operator co_await"); 15857 return Input; 15858 } 15859 } 15860 if (resultType.isNull() || Input.isInvalid()) 15861 return ExprError(); 15862 15863 // Check for array bounds violations in the operand of the UnaryOperator, 15864 // except for the '*' and '&' operators that have to be handled specially 15865 // by CheckArrayAccess (as there are special cases like &array[arraysize] 15866 // that are explicitly defined as valid by the standard). 15867 if (Opc != UO_AddrOf && Opc != UO_Deref) 15868 CheckArrayAccess(Input.get()); 15869 15870 auto *UO = 15871 UnaryOperator::Create(Context, Input.get(), Opc, resultType, VK, OK, 15872 OpLoc, CanOverflow, CurFPFeatureOverrides()); 15873 15874 if (Opc == UO_Deref && UO->getType()->hasAttr(attr::NoDeref) && 15875 !isa<ArrayType>(UO->getType().getDesugaredType(Context)) && 15876 !isUnevaluatedContext()) 15877 ExprEvalContexts.back().PossibleDerefs.insert(UO); 15878 15879 // Convert the result back to a half vector. 15880 if (ConvertHalfVec) 15881 return convertVector(UO, Context.HalfTy, *this); 15882 return UO; 15883 } 15884 15885 bool Sema::isQualifiedMemberAccess(Expr *E) { 15886 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 15887 if (!DRE->getQualifier()) 15888 return false; 15889 15890 ValueDecl *VD = DRE->getDecl(); 15891 if (!VD->isCXXClassMember()) 15892 return false; 15893 15894 if (isa<FieldDecl>(VD) || isa<IndirectFieldDecl>(VD)) 15895 return true; 15896 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(VD)) 15897 return Method->isImplicitObjectMemberFunction(); 15898 15899 return false; 15900 } 15901 15902 if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { 15903 if (!ULE->getQualifier()) 15904 return false; 15905 15906 for (NamedDecl *D : ULE->decls()) { 15907 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 15908 if (Method->isImplicitObjectMemberFunction()) 15909 return true; 15910 } else { 15911 // Overload set does not contain methods. 15912 break; 15913 } 15914 } 15915 15916 return false; 15917 } 15918 15919 return false; 15920 } 15921 15922 ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, 15923 UnaryOperatorKind Opc, Expr *Input, 15924 bool IsAfterAmp) { 15925 // First things first: handle placeholders so that the 15926 // overloaded-operator check considers the right type. 15927 if (const BuiltinType *pty = Input->getType()->getAsPlaceholderType()) { 15928 // Increment and decrement of pseudo-object references. 15929 if (pty->getKind() == BuiltinType::PseudoObject && 15930 UnaryOperator::isIncrementDecrementOp(Opc)) 15931 return PseudoObject().checkIncDec(S, OpLoc, Opc, Input); 15932 15933 // extension is always a builtin operator. 15934 if (Opc == UO_Extension) 15935 return CreateBuiltinUnaryOp(OpLoc, Opc, Input); 15936 15937 // & gets special logic for several kinds of placeholder. 15938 // The builtin code knows what to do. 15939 if (Opc == UO_AddrOf && 15940 (pty->getKind() == BuiltinType::Overload || 15941 pty->getKind() == BuiltinType::UnknownAny || 15942 pty->getKind() == BuiltinType::BoundMember)) 15943 return CreateBuiltinUnaryOp(OpLoc, Opc, Input); 15944 15945 // Anything else needs to be handled now. 15946 ExprResult Result = CheckPlaceholderExpr(Input); 15947 if (Result.isInvalid()) return ExprError(); 15948 Input = Result.get(); 15949 } 15950 15951 if (getLangOpts().CPlusPlus && Input->getType()->isOverloadableType() && 15952 UnaryOperator::getOverloadedOperator(Opc) != OO_None && 15953 !(Opc == UO_AddrOf && isQualifiedMemberAccess(Input))) { 15954 // Find all of the overloaded operators visible from this point. 15955 UnresolvedSet<16> Functions; 15956 OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); 15957 if (S && OverOp != OO_None) 15958 LookupOverloadedOperatorName(OverOp, S, Functions); 15959 15960 return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); 15961 } 15962 15963 return CreateBuiltinUnaryOp(OpLoc, Opc, Input, IsAfterAmp); 15964 } 15965 15966 ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, tok::TokenKind Op, 15967 Expr *Input, bool IsAfterAmp) { 15968 return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input, 15969 IsAfterAmp); 15970 } 15971 15972 ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, 15973 LabelDecl *TheDecl) { 15974 TheDecl->markUsed(Context); 15975 // Create the AST node. The address of a label always has type 'void*'. 15976 auto *Res = new (Context) AddrLabelExpr( 15977 OpLoc, LabLoc, TheDecl, Context.getPointerType(Context.VoidTy)); 15978 15979 if (getCurFunction()) 15980 getCurFunction()->AddrLabels.push_back(Res); 15981 15982 return Res; 15983 } 15984 15985 void Sema::ActOnStartStmtExpr() { 15986 PushExpressionEvaluationContext(ExprEvalContexts.back().Context); 15987 // Make sure we diagnose jumping into a statement expression. 15988 setFunctionHasBranchProtectedScope(); 15989 } 15990 15991 void Sema::ActOnStmtExprError() { 15992 // Note that function is also called by TreeTransform when leaving a 15993 // StmtExpr scope without rebuilding anything. 15994 15995 DiscardCleanupsInEvaluationContext(); 15996 PopExpressionEvaluationContext(); 15997 } 15998 15999 ExprResult Sema::ActOnStmtExpr(Scope *S, SourceLocation LPLoc, Stmt *SubStmt, 16000 SourceLocation RPLoc) { 16001 return BuildStmtExpr(LPLoc, SubStmt, RPLoc, getTemplateDepth(S)); 16002 } 16003 16004 ExprResult Sema::BuildStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, 16005 SourceLocation RPLoc, unsigned TemplateDepth) { 16006 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); 16007 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); 16008 16009 if (hasAnyUnrecoverableErrorsInThisFunction()) 16010 DiscardCleanupsInEvaluationContext(); 16011 assert(!Cleanup.exprNeedsCleanups() && 16012 "cleanups within StmtExpr not correctly bound!"); 16013 PopExpressionEvaluationContext(); 16014 16015 // FIXME: there are a variety of strange constraints to enforce here, for 16016 // example, it is not possible to goto into a stmt expression apparently. 16017 // More semantic analysis is needed. 16018 16019 // If there are sub-stmts in the compound stmt, take the type of the last one 16020 // as the type of the stmtexpr. 16021 QualType Ty = Context.VoidTy; 16022 bool StmtExprMayBindToTemp = false; 16023 if (!Compound->body_empty()) { 16024 // For GCC compatibility we get the last Stmt excluding trailing NullStmts. 16025 if (const auto *LastStmt = 16026 dyn_cast<ValueStmt>(Compound->getStmtExprResult())) { 16027 if (const Expr *Value = LastStmt->getExprStmt()) { 16028 StmtExprMayBindToTemp = true; 16029 Ty = Value->getType(); 16030 } 16031 } 16032 } 16033 16034 // FIXME: Check that expression type is complete/non-abstract; statement 16035 // expressions are not lvalues. 16036 Expr *ResStmtExpr = 16037 new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc, TemplateDepth); 16038 if (StmtExprMayBindToTemp) 16039 return MaybeBindToTemporary(ResStmtExpr); 16040 return ResStmtExpr; 16041 } 16042 16043 ExprResult Sema::ActOnStmtExprResult(ExprResult ER) { 16044 if (ER.isInvalid()) 16045 return ExprError(); 16046 16047 // Do function/array conversion on the last expression, but not 16048 // lvalue-to-rvalue. However, initialize an unqualified type. 16049 ER = DefaultFunctionArrayConversion(ER.get()); 16050 if (ER.isInvalid()) 16051 return ExprError(); 16052 Expr *E = ER.get(); 16053 16054 if (E->isTypeDependent()) 16055 return E; 16056 16057 // In ARC, if the final expression ends in a consume, splice 16058 // the consume out and bind it later. In the alternate case 16059 // (when dealing with a retainable type), the result 16060 // initialization will create a produce. In both cases the 16061 // result will be +1, and we'll need to balance that out with 16062 // a bind. 16063 auto *Cast = dyn_cast<ImplicitCastExpr>(E); 16064 if (Cast && Cast->getCastKind() == CK_ARCConsumeObject) 16065 return Cast->getSubExpr(); 16066 16067 // FIXME: Provide a better location for the initialization. 16068 return PerformCopyInitialization( 16069 InitializedEntity::InitializeStmtExprResult( 16070 E->getBeginLoc(), E->getType().getAtomicUnqualifiedType()), 16071 SourceLocation(), E); 16072 } 16073 16074 ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, 16075 TypeSourceInfo *TInfo, 16076 ArrayRef<OffsetOfComponent> Components, 16077 SourceLocation RParenLoc) { 16078 QualType ArgTy = TInfo->getType(); 16079 bool Dependent = ArgTy->isDependentType(); 16080 SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); 16081 16082 // We must have at least one component that refers to the type, and the first 16083 // one is known to be a field designator. Verify that the ArgTy represents 16084 // a struct/union/class. 16085 if (!Dependent && !ArgTy->isRecordType()) 16086 return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) 16087 << ArgTy << TypeRange); 16088 16089 // Type must be complete per C99 7.17p3 because a declaring a variable 16090 // with an incomplete type would be ill-formed. 16091 if (!Dependent 16092 && RequireCompleteType(BuiltinLoc, ArgTy, 16093 diag::err_offsetof_incomplete_type, TypeRange)) 16094 return ExprError(); 16095 16096 bool DidWarnAboutNonPOD = false; 16097 QualType CurrentType = ArgTy; 16098 SmallVector<OffsetOfNode, 4> Comps; 16099 SmallVector<Expr*, 4> Exprs; 16100 for (const OffsetOfComponent &OC : Components) { 16101 if (OC.isBrackets) { 16102 // Offset of an array sub-field. TODO: Should we allow vector elements? 16103 if (!CurrentType->isDependentType()) { 16104 const ArrayType *AT = Context.getAsArrayType(CurrentType); 16105 if(!AT) 16106 return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) 16107 << CurrentType); 16108 CurrentType = AT->getElementType(); 16109 } else 16110 CurrentType = Context.DependentTy; 16111 16112 ExprResult IdxRval = DefaultLvalueConversion(static_cast<Expr*>(OC.U.E)); 16113 if (IdxRval.isInvalid()) 16114 return ExprError(); 16115 Expr *Idx = IdxRval.get(); 16116 16117 // The expression must be an integral expression. 16118 // FIXME: An integral constant expression? 16119 if (!Idx->isTypeDependent() && !Idx->isValueDependent() && 16120 !Idx->getType()->isIntegerType()) 16121 return ExprError( 16122 Diag(Idx->getBeginLoc(), diag::err_typecheck_subscript_not_integer) 16123 << Idx->getSourceRange()); 16124 16125 // Record this array index. 16126 Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); 16127 Exprs.push_back(Idx); 16128 continue; 16129 } 16130 16131 // Offset of a field. 16132 if (CurrentType->isDependentType()) { 16133 // We have the offset of a field, but we can't look into the dependent 16134 // type. Just record the identifier of the field. 16135 Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); 16136 CurrentType = Context.DependentTy; 16137 continue; 16138 } 16139 16140 // We need to have a complete type to look into. 16141 if (RequireCompleteType(OC.LocStart, CurrentType, 16142 diag::err_offsetof_incomplete_type)) 16143 return ExprError(); 16144 16145 // Look for the designated field. 16146 const RecordType *RC = CurrentType->getAs<RecordType>(); 16147 if (!RC) 16148 return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) 16149 << CurrentType); 16150 RecordDecl *RD = RC->getDecl(); 16151 16152 // C++ [lib.support.types]p5: 16153 // The macro offsetof accepts a restricted set of type arguments in this 16154 // International Standard. type shall be a POD structure or a POD union 16155 // (clause 9). 16156 // C++11 [support.types]p4: 16157 // If type is not a standard-layout class (Clause 9), the results are 16158 // undefined. 16159 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { 16160 bool IsSafe = LangOpts.CPlusPlus11? CRD->isStandardLayout() : CRD->isPOD(); 16161 unsigned DiagID = 16162 LangOpts.CPlusPlus11? diag::ext_offsetof_non_standardlayout_type 16163 : diag::ext_offsetof_non_pod_type; 16164 16165 if (!IsSafe && !DidWarnAboutNonPOD && !isUnevaluatedContext()) { 16166 Diag(BuiltinLoc, DiagID) 16167 << SourceRange(Components[0].LocStart, OC.LocEnd) << CurrentType; 16168 DidWarnAboutNonPOD = true; 16169 } 16170 } 16171 16172 // Look for the field. 16173 LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); 16174 LookupQualifiedName(R, RD); 16175 FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); 16176 IndirectFieldDecl *IndirectMemberDecl = nullptr; 16177 if (!MemberDecl) { 16178 if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) 16179 MemberDecl = IndirectMemberDecl->getAnonField(); 16180 } 16181 16182 if (!MemberDecl) { 16183 // Lookup could be ambiguous when looking up a placeholder variable 16184 // __builtin_offsetof(S, _). 16185 // In that case we would already have emitted a diagnostic 16186 if (!R.isAmbiguous()) 16187 Diag(BuiltinLoc, diag::err_no_member) 16188 << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, OC.LocEnd); 16189 return ExprError(); 16190 } 16191 16192 // C99 7.17p3: 16193 // (If the specified member is a bit-field, the behavior is undefined.) 16194 // 16195 // We diagnose this as an error. 16196 if (MemberDecl->isBitField()) { 16197 Diag(OC.LocEnd, diag::err_offsetof_bitfield) 16198 << MemberDecl->getDeclName() 16199 << SourceRange(BuiltinLoc, RParenLoc); 16200 Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); 16201 return ExprError(); 16202 } 16203 16204 RecordDecl *Parent = MemberDecl->getParent(); 16205 if (IndirectMemberDecl) 16206 Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); 16207 16208 // If the member was found in a base class, introduce OffsetOfNodes for 16209 // the base class indirections. 16210 CXXBasePaths Paths; 16211 if (IsDerivedFrom(OC.LocStart, CurrentType, Context.getTypeDeclType(Parent), 16212 Paths)) { 16213 if (Paths.getDetectedVirtual()) { 16214 Diag(OC.LocEnd, diag::err_offsetof_field_of_virtual_base) 16215 << MemberDecl->getDeclName() 16216 << SourceRange(BuiltinLoc, RParenLoc); 16217 return ExprError(); 16218 } 16219 16220 CXXBasePath &Path = Paths.front(); 16221 for (const CXXBasePathElement &B : Path) 16222 Comps.push_back(OffsetOfNode(B.Base)); 16223 } 16224 16225 if (IndirectMemberDecl) { 16226 for (auto *FI : IndirectMemberDecl->chain()) { 16227 assert(isa<FieldDecl>(FI)); 16228 Comps.push_back(OffsetOfNode(OC.LocStart, 16229 cast<FieldDecl>(FI), OC.LocEnd)); 16230 } 16231 } else 16232 Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); 16233 16234 CurrentType = MemberDecl->getType().getNonReferenceType(); 16235 } 16236 16237 return OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, TInfo, 16238 Comps, Exprs, RParenLoc); 16239 } 16240 16241 ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, 16242 SourceLocation BuiltinLoc, 16243 SourceLocation TypeLoc, 16244 ParsedType ParsedArgTy, 16245 ArrayRef<OffsetOfComponent> Components, 16246 SourceLocation RParenLoc) { 16247 16248 TypeSourceInfo *ArgTInfo; 16249 QualType ArgTy = GetTypeFromParser(ParsedArgTy, &ArgTInfo); 16250 if (ArgTy.isNull()) 16251 return ExprError(); 16252 16253 if (!ArgTInfo) 16254 ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); 16255 16256 return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, Components, RParenLoc); 16257 } 16258 16259 16260 ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, 16261 Expr *CondExpr, 16262 Expr *LHSExpr, Expr *RHSExpr, 16263 SourceLocation RPLoc) { 16264 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); 16265 16266 ExprValueKind VK = VK_PRValue; 16267 ExprObjectKind OK = OK_Ordinary; 16268 QualType resType; 16269 bool CondIsTrue = false; 16270 if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { 16271 resType = Context.DependentTy; 16272 } else { 16273 // The conditional expression is required to be a constant expression. 16274 llvm::APSInt condEval(32); 16275 ExprResult CondICE = VerifyIntegerConstantExpression( 16276 CondExpr, &condEval, diag::err_typecheck_choose_expr_requires_constant); 16277 if (CondICE.isInvalid()) 16278 return ExprError(); 16279 CondExpr = CondICE.get(); 16280 CondIsTrue = condEval.getZExtValue(); 16281 16282 // If the condition is > zero, then the AST type is the same as the LHSExpr. 16283 Expr *ActiveExpr = CondIsTrue ? LHSExpr : RHSExpr; 16284 16285 resType = ActiveExpr->getType(); 16286 VK = ActiveExpr->getValueKind(); 16287 OK = ActiveExpr->getObjectKind(); 16288 } 16289 16290 return new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, 16291 resType, VK, OK, RPLoc, CondIsTrue); 16292 } 16293 16294 //===----------------------------------------------------------------------===// 16295 // Clang Extensions. 16296 //===----------------------------------------------------------------------===// 16297 16298 void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { 16299 BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); 16300 16301 if (LangOpts.CPlusPlus) { 16302 MangleNumberingContext *MCtx; 16303 Decl *ManglingContextDecl; 16304 std::tie(MCtx, ManglingContextDecl) = 16305 getCurrentMangleNumberContext(Block->getDeclContext()); 16306 if (MCtx) { 16307 unsigned ManglingNumber = MCtx->getManglingNumber(Block); 16308 Block->setBlockMangling(ManglingNumber, ManglingContextDecl); 16309 } 16310 } 16311 16312 PushBlockScope(CurScope, Block); 16313 CurContext->addDecl(Block); 16314 if (CurScope) 16315 PushDeclContext(CurScope, Block); 16316 else 16317 CurContext = Block; 16318 16319 getCurBlock()->HasImplicitReturnType = true; 16320 16321 // Enter a new evaluation context to insulate the block from any 16322 // cleanups from the enclosing full-expression. 16323 PushExpressionEvaluationContext( 16324 ExpressionEvaluationContext::PotentiallyEvaluated); 16325 } 16326 16327 void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, 16328 Scope *CurScope) { 16329 assert(ParamInfo.getIdentifier() == nullptr && 16330 "block-id should have no identifier!"); 16331 assert(ParamInfo.getContext() == DeclaratorContext::BlockLiteral); 16332 BlockScopeInfo *CurBlock = getCurBlock(); 16333 16334 TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo); 16335 QualType T = Sig->getType(); 16336 DiagnoseUnexpandedParameterPack(CaretLoc, Sig, UPPC_Block); 16337 16338 // GetTypeForDeclarator always produces a function type for a block 16339 // literal signature. Furthermore, it is always a FunctionProtoType 16340 // unless the function was written with a typedef. 16341 assert(T->isFunctionType() && 16342 "GetTypeForDeclarator made a non-function block signature"); 16343 16344 // Look for an explicit signature in that function type. 16345 FunctionProtoTypeLoc ExplicitSignature; 16346 16347 if ((ExplicitSignature = Sig->getTypeLoc() 16348 .getAsAdjusted<FunctionProtoTypeLoc>())) { 16349 16350 // Check whether that explicit signature was synthesized by 16351 // GetTypeForDeclarator. If so, don't save that as part of the 16352 // written signature. 16353 if (ExplicitSignature.getLocalRangeBegin() == 16354 ExplicitSignature.getLocalRangeEnd()) { 16355 // This would be much cheaper if we stored TypeLocs instead of 16356 // TypeSourceInfos. 16357 TypeLoc Result = ExplicitSignature.getReturnLoc(); 16358 unsigned Size = Result.getFullDataSize(); 16359 Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); 16360 Sig->getTypeLoc().initializeFullCopy(Result, Size); 16361 16362 ExplicitSignature = FunctionProtoTypeLoc(); 16363 } 16364 } 16365 16366 CurBlock->TheDecl->setSignatureAsWritten(Sig); 16367 CurBlock->FunctionType = T; 16368 16369 const auto *Fn = T->castAs<FunctionType>(); 16370 QualType RetTy = Fn->getReturnType(); 16371 bool isVariadic = 16372 (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); 16373 16374 CurBlock->TheDecl->setIsVariadic(isVariadic); 16375 16376 // Context.DependentTy is used as a placeholder for a missing block 16377 // return type. TODO: what should we do with declarators like: 16378 // ^ * { ... } 16379 // If the answer is "apply template argument deduction".... 16380 if (RetTy != Context.DependentTy) { 16381 CurBlock->ReturnType = RetTy; 16382 CurBlock->TheDecl->setBlockMissingReturnType(false); 16383 CurBlock->HasImplicitReturnType = false; 16384 } 16385 16386 // Push block parameters from the declarator if we had them. 16387 SmallVector<ParmVarDecl*, 8> Params; 16388 if (ExplicitSignature) { 16389 for (unsigned I = 0, E = ExplicitSignature.getNumParams(); I != E; ++I) { 16390 ParmVarDecl *Param = ExplicitSignature.getParam(I); 16391 if (Param->getIdentifier() == nullptr && !Param->isImplicit() && 16392 !Param->isInvalidDecl() && !getLangOpts().CPlusPlus) { 16393 // Diagnose this as an extension in C17 and earlier. 16394 if (!getLangOpts().C23) 16395 Diag(Param->getLocation(), diag::ext_parameter_name_omitted_c23); 16396 } 16397 Params.push_back(Param); 16398 } 16399 16400 // Fake up parameter variables if we have a typedef, like 16401 // ^ fntype { ... } 16402 } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { 16403 for (const auto &I : Fn->param_types()) { 16404 ParmVarDecl *Param = BuildParmVarDeclForTypedef( 16405 CurBlock->TheDecl, ParamInfo.getBeginLoc(), I); 16406 Params.push_back(Param); 16407 } 16408 } 16409 16410 // Set the parameters on the block decl. 16411 if (!Params.empty()) { 16412 CurBlock->TheDecl->setParams(Params); 16413 CheckParmsForFunctionDef(CurBlock->TheDecl->parameters(), 16414 /*CheckParameterNames=*/false); 16415 } 16416 16417 // Finally we can process decl attributes. 16418 ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); 16419 16420 // Put the parameter variables in scope. 16421 for (auto *AI : CurBlock->TheDecl->parameters()) { 16422 AI->setOwningFunction(CurBlock->TheDecl); 16423 16424 // If this has an identifier, add it to the scope stack. 16425 if (AI->getIdentifier()) { 16426 CheckShadow(CurBlock->TheScope, AI); 16427 16428 PushOnScopeChains(AI, CurBlock->TheScope); 16429 } 16430 16431 if (AI->isInvalidDecl()) 16432 CurBlock->TheDecl->setInvalidDecl(); 16433 } 16434 } 16435 16436 void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { 16437 // Leave the expression-evaluation context. 16438 DiscardCleanupsInEvaluationContext(); 16439 PopExpressionEvaluationContext(); 16440 16441 // Pop off CurBlock, handle nested blocks. 16442 PopDeclContext(); 16443 PopFunctionScopeInfo(); 16444 } 16445 16446 ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, 16447 Stmt *Body, Scope *CurScope) { 16448 // If blocks are disabled, emit an error. 16449 if (!LangOpts.Blocks) 16450 Diag(CaretLoc, diag::err_blocks_disable) << LangOpts.OpenCL; 16451 16452 // Leave the expression-evaluation context. 16453 if (hasAnyUnrecoverableErrorsInThisFunction()) 16454 DiscardCleanupsInEvaluationContext(); 16455 assert(!Cleanup.exprNeedsCleanups() && 16456 "cleanups within block not correctly bound!"); 16457 PopExpressionEvaluationContext(); 16458 16459 BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); 16460 BlockDecl *BD = BSI->TheDecl; 16461 16462 maybeAddDeclWithEffects(BD); 16463 16464 if (BSI->HasImplicitReturnType) 16465 deduceClosureReturnType(*BSI); 16466 16467 QualType RetTy = Context.VoidTy; 16468 if (!BSI->ReturnType.isNull()) 16469 RetTy = BSI->ReturnType; 16470 16471 bool NoReturn = BD->hasAttr<NoReturnAttr>(); 16472 QualType BlockTy; 16473 16474 // If the user wrote a function type in some form, try to use that. 16475 if (!BSI->FunctionType.isNull()) { 16476 const FunctionType *FTy = BSI->FunctionType->castAs<FunctionType>(); 16477 16478 FunctionType::ExtInfo Ext = FTy->getExtInfo(); 16479 if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); 16480 16481 // Turn protoless block types into nullary block types. 16482 if (isa<FunctionNoProtoType>(FTy)) { 16483 FunctionProtoType::ExtProtoInfo EPI; 16484 EPI.ExtInfo = Ext; 16485 BlockTy = Context.getFunctionType(RetTy, {}, EPI); 16486 16487 // Otherwise, if we don't need to change anything about the function type, 16488 // preserve its sugar structure. 16489 } else if (FTy->getReturnType() == RetTy && 16490 (!NoReturn || FTy->getNoReturnAttr())) { 16491 BlockTy = BSI->FunctionType; 16492 16493 // Otherwise, make the minimal modifications to the function type. 16494 } else { 16495 const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); 16496 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); 16497 EPI.TypeQuals = Qualifiers(); 16498 EPI.ExtInfo = Ext; 16499 BlockTy = Context.getFunctionType(RetTy, FPT->getParamTypes(), EPI); 16500 } 16501 16502 // If we don't have a function type, just build one from nothing. 16503 } else { 16504 FunctionProtoType::ExtProtoInfo EPI; 16505 EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); 16506 BlockTy = Context.getFunctionType(RetTy, {}, EPI); 16507 } 16508 16509 DiagnoseUnusedParameters(BD->parameters()); 16510 BlockTy = Context.getBlockPointerType(BlockTy); 16511 16512 // If needed, diagnose invalid gotos and switches in the block. 16513 if (getCurFunction()->NeedsScopeChecking() && 16514 !PP.isCodeCompletionEnabled()) 16515 DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); 16516 16517 BD->setBody(cast<CompoundStmt>(Body)); 16518 16519 if (Body && getCurFunction()->HasPotentialAvailabilityViolations) 16520 DiagnoseUnguardedAvailabilityViolations(BD); 16521 16522 // Try to apply the named return value optimization. We have to check again 16523 // if we can do this, though, because blocks keep return statements around 16524 // to deduce an implicit return type. 16525 if (getLangOpts().CPlusPlus && RetTy->isRecordType() && 16526 !BD->isDependentContext()) 16527 computeNRVO(Body, BSI); 16528 16529 if (RetTy.hasNonTrivialToPrimitiveDestructCUnion() || 16530 RetTy.hasNonTrivialToPrimitiveCopyCUnion()) 16531 checkNonTrivialCUnion(RetTy, BD->getCaretLocation(), 16532 NonTrivialCUnionContext::FunctionReturn, 16533 NTCUK_Destruct | NTCUK_Copy); 16534 16535 PopDeclContext(); 16536 16537 // Set the captured variables on the block. 16538 SmallVector<BlockDecl::Capture, 4> Captures; 16539 for (Capture &Cap : BSI->Captures) { 16540 if (Cap.isInvalid() || Cap.isThisCapture()) 16541 continue; 16542 // Cap.getVariable() is always a VarDecl because 16543 // blocks cannot capture structured bindings or other ValueDecl kinds. 16544 auto *Var = cast<VarDecl>(Cap.getVariable()); 16545 Expr *CopyExpr = nullptr; 16546 if (getLangOpts().CPlusPlus && Cap.isCopyCapture()) { 16547 if (const RecordType *Record = 16548 Cap.getCaptureType()->getAs<RecordType>()) { 16549 // The capture logic needs the destructor, so make sure we mark it. 16550 // Usually this is unnecessary because most local variables have 16551 // their destructors marked at declaration time, but parameters are 16552 // an exception because it's technically only the call site that 16553 // actually requires the destructor. 16554 if (isa<ParmVarDecl>(Var)) 16555 FinalizeVarWithDestructor(Var, Record); 16556 16557 // Enter a separate potentially-evaluated context while building block 16558 // initializers to isolate their cleanups from those of the block 16559 // itself. 16560 // FIXME: Is this appropriate even when the block itself occurs in an 16561 // unevaluated operand? 16562 EnterExpressionEvaluationContext EvalContext( 16563 *this, ExpressionEvaluationContext::PotentiallyEvaluated); 16564 16565 SourceLocation Loc = Cap.getLocation(); 16566 16567 ExprResult Result = BuildDeclarationNameExpr( 16568 CXXScopeSpec(), DeclarationNameInfo(Var->getDeclName(), Loc), Var); 16569 16570 // According to the blocks spec, the capture of a variable from 16571 // the stack requires a const copy constructor. This is not true 16572 // of the copy/move done to move a __block variable to the heap. 16573 if (!Result.isInvalid() && 16574 !Result.get()->getType().isConstQualified()) { 16575 Result = ImpCastExprToType(Result.get(), 16576 Result.get()->getType().withConst(), 16577 CK_NoOp, VK_LValue); 16578 } 16579 16580 if (!Result.isInvalid()) { 16581 Result = PerformCopyInitialization( 16582 InitializedEntity::InitializeBlock(Var->getLocation(), 16583 Cap.getCaptureType()), 16584 Loc, Result.get()); 16585 } 16586 16587 // Build a full-expression copy expression if initialization 16588 // succeeded and used a non-trivial constructor. Recover from 16589 // errors by pretending that the copy isn't necessary. 16590 if (!Result.isInvalid() && 16591 !cast<CXXConstructExpr>(Result.get())->getConstructor() 16592 ->isTrivial()) { 16593 Result = MaybeCreateExprWithCleanups(Result); 16594 CopyExpr = Result.get(); 16595 } 16596 } 16597 } 16598 16599 BlockDecl::Capture NewCap(Var, Cap.isBlockCapture(), Cap.isNested(), 16600 CopyExpr); 16601 Captures.push_back(NewCap); 16602 } 16603 BD->setCaptures(Context, Captures, BSI->CXXThisCaptureIndex != 0); 16604 16605 // Pop the block scope now but keep it alive to the end of this function. 16606 AnalysisBasedWarnings::Policy WP = 16607 AnalysisWarnings.getPolicyInEffectAt(Body->getEndLoc()); 16608 PoppedFunctionScopePtr ScopeRAII = PopFunctionScopeInfo(&WP, BD, BlockTy); 16609 16610 BlockExpr *Result = new (Context) 16611 BlockExpr(BD, BlockTy, BSI->ContainsUnexpandedParameterPack); 16612 16613 // If the block isn't obviously global, i.e. it captures anything at 16614 // all, then we need to do a few things in the surrounding context: 16615 if (Result->getBlockDecl()->hasCaptures()) { 16616 // First, this expression has a new cleanup object. 16617 ExprCleanupObjects.push_back(Result->getBlockDecl()); 16618 Cleanup.setExprNeedsCleanups(true); 16619 16620 // It also gets a branch-protected scope if any of the captured 16621 // variables needs destruction. 16622 for (const auto &CI : Result->getBlockDecl()->captures()) { 16623 const VarDecl *var = CI.getVariable(); 16624 if (var->getType().isDestructedType() != QualType::DK_none) { 16625 setFunctionHasBranchProtectedScope(); 16626 break; 16627 } 16628 } 16629 } 16630 16631 if (getCurFunction()) 16632 getCurFunction()->addBlock(BD); 16633 16634 // This can happen if the block's return type is deduced, but 16635 // the return expression is invalid. 16636 if (BD->isInvalidDecl()) 16637 return CreateRecoveryExpr(Result->getBeginLoc(), Result->getEndLoc(), 16638 {Result}, Result->getType()); 16639 return Result; 16640 } 16641 16642 ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, Expr *E, ParsedType Ty, 16643 SourceLocation RPLoc) { 16644 TypeSourceInfo *TInfo; 16645 GetTypeFromParser(Ty, &TInfo); 16646 return BuildVAArgExpr(BuiltinLoc, E, TInfo, RPLoc); 16647 } 16648 16649 ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, 16650 Expr *E, TypeSourceInfo *TInfo, 16651 SourceLocation RPLoc) { 16652 Expr *OrigExpr = E; 16653 bool IsMS = false; 16654 16655 // CUDA device code does not support varargs. 16656 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) { 16657 if (const FunctionDecl *F = dyn_cast<FunctionDecl>(CurContext)) { 16658 CUDAFunctionTarget T = CUDA().IdentifyTarget(F); 16659 if (T == CUDAFunctionTarget::Global || T == CUDAFunctionTarget::Device || 16660 T == CUDAFunctionTarget::HostDevice) 16661 return ExprError(Diag(E->getBeginLoc(), diag::err_va_arg_in_device)); 16662 } 16663 } 16664 16665 // NVPTX does not support va_arg expression. 16666 if (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice && 16667 Context.getTargetInfo().getTriple().isNVPTX()) 16668 targetDiag(E->getBeginLoc(), diag::err_va_arg_in_device); 16669 16670 // It might be a __builtin_ms_va_list. (But don't ever mark a va_arg() 16671 // as Microsoft ABI on an actual Microsoft platform, where 16672 // __builtin_ms_va_list and __builtin_va_list are the same.) 16673 if (!E->isTypeDependent() && Context.getTargetInfo().hasBuiltinMSVaList() && 16674 Context.getTargetInfo().getBuiltinVaListKind() != TargetInfo::CharPtrBuiltinVaList) { 16675 QualType MSVaListType = Context.getBuiltinMSVaListType(); 16676 if (Context.hasSameType(MSVaListType, E->getType())) { 16677 if (CheckForModifiableLvalue(E, BuiltinLoc, *this)) 16678 return ExprError(); 16679 IsMS = true; 16680 } 16681 } 16682 16683 // Get the va_list type 16684 QualType VaListType = Context.getBuiltinVaListType(); 16685 if (!IsMS) { 16686 if (VaListType->isArrayType()) { 16687 // Deal with implicit array decay; for example, on x86-64, 16688 // va_list is an array, but it's supposed to decay to 16689 // a pointer for va_arg. 16690 VaListType = Context.getArrayDecayedType(VaListType); 16691 // Make sure the input expression also decays appropriately. 16692 ExprResult Result = UsualUnaryConversions(E); 16693 if (Result.isInvalid()) 16694 return ExprError(); 16695 E = Result.get(); 16696 } else if (VaListType->isRecordType() && getLangOpts().CPlusPlus) { 16697 // If va_list is a record type and we are compiling in C++ mode, 16698 // check the argument using reference binding. 16699 InitializedEntity Entity = InitializedEntity::InitializeParameter( 16700 Context, Context.getLValueReferenceType(VaListType), false); 16701 ExprResult Init = PerformCopyInitialization(Entity, SourceLocation(), E); 16702 if (Init.isInvalid()) 16703 return ExprError(); 16704 E = Init.getAs<Expr>(); 16705 } else { 16706 // Otherwise, the va_list argument must be an l-value because 16707 // it is modified by va_arg. 16708 if (!E->isTypeDependent() && 16709 CheckForModifiableLvalue(E, BuiltinLoc, *this)) 16710 return ExprError(); 16711 } 16712 } 16713 16714 if (!IsMS && !E->isTypeDependent() && 16715 !Context.hasSameType(VaListType, E->getType())) 16716 return ExprError( 16717 Diag(E->getBeginLoc(), 16718 diag::err_first_argument_to_va_arg_not_of_type_va_list) 16719 << OrigExpr->getType() << E->getSourceRange()); 16720 16721 if (!TInfo->getType()->isDependentType()) { 16722 if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), 16723 diag::err_second_parameter_to_va_arg_incomplete, 16724 TInfo->getTypeLoc())) 16725 return ExprError(); 16726 16727 if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), 16728 TInfo->getType(), 16729 diag::err_second_parameter_to_va_arg_abstract, 16730 TInfo->getTypeLoc())) 16731 return ExprError(); 16732 16733 if (!TInfo->getType().isPODType(Context)) { 16734 Diag(TInfo->getTypeLoc().getBeginLoc(), 16735 TInfo->getType()->isObjCLifetimeType() 16736 ? diag::warn_second_parameter_to_va_arg_ownership_qualified 16737 : diag::warn_second_parameter_to_va_arg_not_pod) 16738 << TInfo->getType() 16739 << TInfo->getTypeLoc().getSourceRange(); 16740 } 16741 16742 if (TInfo->getType()->isArrayType()) { 16743 DiagRuntimeBehavior(TInfo->getTypeLoc().getBeginLoc(), E, 16744 PDiag(diag::warn_second_parameter_to_va_arg_array) 16745 << TInfo->getType() 16746 << TInfo->getTypeLoc().getSourceRange()); 16747 } 16748 16749 // Check for va_arg where arguments of the given type will be promoted 16750 // (i.e. this va_arg is guaranteed to have undefined behavior). 16751 QualType PromoteType; 16752 if (Context.isPromotableIntegerType(TInfo->getType())) { 16753 PromoteType = Context.getPromotedIntegerType(TInfo->getType()); 16754 // [cstdarg.syn]p1 defers the C++ behavior to what the C standard says, 16755 // and C23 7.16.1.1p2 says, in part: 16756 // If type is not compatible with the type of the actual next argument 16757 // (as promoted according to the default argument promotions), the 16758 // behavior is undefined, except for the following cases: 16759 // - both types are pointers to qualified or unqualified versions of 16760 // compatible types; 16761 // - one type is compatible with a signed integer type, the other 16762 // type is compatible with the corresponding unsigned integer type, 16763 // and the value is representable in both types; 16764 // - one type is pointer to qualified or unqualified void and the 16765 // other is a pointer to a qualified or unqualified character type; 16766 // - or, the type of the next argument is nullptr_t and type is a 16767 // pointer type that has the same representation and alignment 16768 // requirements as a pointer to a character type. 16769 // Given that type compatibility is the primary requirement (ignoring 16770 // qualifications), you would think we could call typesAreCompatible() 16771 // directly to test this. However, in C++, that checks for *same type*, 16772 // which causes false positives when passing an enumeration type to 16773 // va_arg. Instead, get the underlying type of the enumeration and pass 16774 // that. 16775 QualType UnderlyingType = TInfo->getType(); 16776 if (const auto *ET = UnderlyingType->getAs<EnumType>()) 16777 UnderlyingType = ET->getDecl()->getIntegerType(); 16778 if (Context.typesAreCompatible(PromoteType, UnderlyingType, 16779 /*CompareUnqualified*/ true)) 16780 PromoteType = QualType(); 16781 16782 // If the types are still not compatible, we need to test whether the 16783 // promoted type and the underlying type are the same except for 16784 // signedness. Ask the AST for the correctly corresponding type and see 16785 // if that's compatible. 16786 if (!PromoteType.isNull() && !UnderlyingType->isBooleanType() && 16787 PromoteType->isUnsignedIntegerType() != 16788 UnderlyingType->isUnsignedIntegerType()) { 16789 UnderlyingType = 16790 UnderlyingType->isUnsignedIntegerType() 16791 ? Context.getCorrespondingSignedType(UnderlyingType) 16792 : Context.getCorrespondingUnsignedType(UnderlyingType); 16793 if (Context.typesAreCompatible(PromoteType, UnderlyingType, 16794 /*CompareUnqualified*/ true)) 16795 PromoteType = QualType(); 16796 } 16797 } 16798 if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) 16799 PromoteType = Context.DoubleTy; 16800 if (!PromoteType.isNull()) 16801 DiagRuntimeBehavior(TInfo->getTypeLoc().getBeginLoc(), E, 16802 PDiag(diag::warn_second_parameter_to_va_arg_never_compatible) 16803 << TInfo->getType() 16804 << PromoteType 16805 << TInfo->getTypeLoc().getSourceRange()); 16806 } 16807 16808 QualType T = TInfo->getType().getNonLValueExprType(Context); 16809 return new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T, IsMS); 16810 } 16811 16812 ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { 16813 // The type of __null will be int or long, depending on the size of 16814 // pointers on the target. 16815 QualType Ty; 16816 unsigned pw = Context.getTargetInfo().getPointerWidth(LangAS::Default); 16817 if (pw == Context.getTargetInfo().getIntWidth()) 16818 Ty = Context.IntTy; 16819 else if (pw == Context.getTargetInfo().getLongWidth()) 16820 Ty = Context.LongTy; 16821 else if (pw == Context.getTargetInfo().getLongLongWidth()) 16822 Ty = Context.LongLongTy; 16823 else { 16824 llvm_unreachable("I don't know size of pointer!"); 16825 } 16826 16827 return new (Context) GNUNullExpr(Ty, TokenLoc); 16828 } 16829 16830 static CXXRecordDecl *LookupStdSourceLocationImpl(Sema &S, SourceLocation Loc) { 16831 CXXRecordDecl *ImplDecl = nullptr; 16832 16833 // Fetch the std::source_location::__impl decl. 16834 if (NamespaceDecl *Std = S.getStdNamespace()) { 16835 LookupResult ResultSL(S, &S.PP.getIdentifierTable().get("source_location"), 16836 Loc, Sema::LookupOrdinaryName); 16837 if (S.LookupQualifiedName(ResultSL, Std)) { 16838 if (auto *SLDecl = ResultSL.getAsSingle<RecordDecl>()) { 16839 LookupResult ResultImpl(S, &S.PP.getIdentifierTable().get("__impl"), 16840 Loc, Sema::LookupOrdinaryName); 16841 if ((SLDecl->isCompleteDefinition() || SLDecl->isBeingDefined()) && 16842 S.LookupQualifiedName(ResultImpl, SLDecl)) { 16843 ImplDecl = ResultImpl.getAsSingle<CXXRecordDecl>(); 16844 } 16845 } 16846 } 16847 } 16848 16849 if (!ImplDecl || !ImplDecl->isCompleteDefinition()) { 16850 S.Diag(Loc, diag::err_std_source_location_impl_not_found); 16851 return nullptr; 16852 } 16853 16854 // Verify that __impl is a trivial struct type, with no base classes, and with 16855 // only the four expected fields. 16856 if (ImplDecl->isUnion() || !ImplDecl->isStandardLayout() || 16857 ImplDecl->getNumBases() != 0) { 16858 S.Diag(Loc, diag::err_std_source_location_impl_malformed); 16859 return nullptr; 16860 } 16861 16862 unsigned Count = 0; 16863 for (FieldDecl *F : ImplDecl->fields()) { 16864 StringRef Name = F->getName(); 16865 16866 if (Name == "_M_file_name") { 16867 if (F->getType() != 16868 S.Context.getPointerType(S.Context.CharTy.withConst())) 16869 break; 16870 Count++; 16871 } else if (Name == "_M_function_name") { 16872 if (F->getType() != 16873 S.Context.getPointerType(S.Context.CharTy.withConst())) 16874 break; 16875 Count++; 16876 } else if (Name == "_M_line") { 16877 if (!F->getType()->isIntegerType()) 16878 break; 16879 Count++; 16880 } else if (Name == "_M_column") { 16881 if (!F->getType()->isIntegerType()) 16882 break; 16883 Count++; 16884 } else { 16885 Count = 100; // invalid 16886 break; 16887 } 16888 } 16889 if (Count != 4) { 16890 S.Diag(Loc, diag::err_std_source_location_impl_malformed); 16891 return nullptr; 16892 } 16893 16894 return ImplDecl; 16895 } 16896 16897 ExprResult Sema::ActOnSourceLocExpr(SourceLocIdentKind Kind, 16898 SourceLocation BuiltinLoc, 16899 SourceLocation RPLoc) { 16900 QualType ResultTy; 16901 switch (Kind) { 16902 case SourceLocIdentKind::File: 16903 case SourceLocIdentKind::FileName: 16904 case SourceLocIdentKind::Function: 16905 case SourceLocIdentKind::FuncSig: { 16906 QualType ArrTy = Context.getStringLiteralArrayType(Context.CharTy, 0); 16907 ResultTy = 16908 Context.getPointerType(ArrTy->getAsArrayTypeUnsafe()->getElementType()); 16909 break; 16910 } 16911 case SourceLocIdentKind::Line: 16912 case SourceLocIdentKind::Column: 16913 ResultTy = Context.UnsignedIntTy; 16914 break; 16915 case SourceLocIdentKind::SourceLocStruct: 16916 if (!StdSourceLocationImplDecl) { 16917 StdSourceLocationImplDecl = 16918 LookupStdSourceLocationImpl(*this, BuiltinLoc); 16919 if (!StdSourceLocationImplDecl) 16920 return ExprError(); 16921 } 16922 ResultTy = Context.getPointerType( 16923 Context.getRecordType(StdSourceLocationImplDecl).withConst()); 16924 break; 16925 } 16926 16927 return BuildSourceLocExpr(Kind, ResultTy, BuiltinLoc, RPLoc, CurContext); 16928 } 16929 16930 ExprResult Sema::BuildSourceLocExpr(SourceLocIdentKind Kind, QualType ResultTy, 16931 SourceLocation BuiltinLoc, 16932 SourceLocation RPLoc, 16933 DeclContext *ParentContext) { 16934 return new (Context) 16935 SourceLocExpr(Context, Kind, ResultTy, BuiltinLoc, RPLoc, ParentContext); 16936 } 16937 16938 ExprResult Sema::ActOnEmbedExpr(SourceLocation EmbedKeywordLoc, 16939 StringLiteral *BinaryData, StringRef FileName) { 16940 EmbedDataStorage *Data = new (Context) EmbedDataStorage; 16941 Data->BinaryData = BinaryData; 16942 Data->FileName = FileName; 16943 return new (Context) 16944 EmbedExpr(Context, EmbedKeywordLoc, Data, /*NumOfElements=*/0, 16945 Data->getDataElementCount()); 16946 } 16947 16948 static bool maybeDiagnoseAssignmentToFunction(Sema &S, QualType DstType, 16949 const Expr *SrcExpr) { 16950 if (!DstType->isFunctionPointerType() || 16951 !SrcExpr->getType()->isFunctionType()) 16952 return false; 16953 16954 auto *DRE = dyn_cast<DeclRefExpr>(SrcExpr->IgnoreParenImpCasts()); 16955 if (!DRE) 16956 return false; 16957 16958 auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl()); 16959 if (!FD) 16960 return false; 16961 16962 return !S.checkAddressOfFunctionIsAvailable(FD, 16963 /*Complain=*/true, 16964 SrcExpr->getBeginLoc()); 16965 } 16966 16967 bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, 16968 SourceLocation Loc, 16969 QualType DstType, QualType SrcType, 16970 Expr *SrcExpr, AssignmentAction Action, 16971 bool *Complained) { 16972 if (Complained) 16973 *Complained = false; 16974 16975 // Decode the result (notice that AST's are still created for extensions). 16976 bool CheckInferredResultType = false; 16977 bool isInvalid = false; 16978 unsigned DiagKind = 0; 16979 ConversionFixItGenerator ConvHints; 16980 bool MayHaveConvFixit = false; 16981 bool MayHaveFunctionDiff = false; 16982 const ObjCInterfaceDecl *IFace = nullptr; 16983 const ObjCProtocolDecl *PDecl = nullptr; 16984 16985 switch (ConvTy) { 16986 case AssignConvertType::Compatible: 16987 DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr); 16988 return false; 16989 case AssignConvertType::CompatibleVoidPtrToNonVoidPtr: 16990 // Still a valid conversion, but we may want to diagnose for C++ 16991 // compatibility reasons. 16992 DiagKind = diag::warn_compatible_implicit_pointer_conv; 16993 break; 16994 case AssignConvertType::PointerToInt: 16995 if (getLangOpts().CPlusPlus) { 16996 DiagKind = diag::err_typecheck_convert_pointer_int; 16997 isInvalid = true; 16998 } else { 16999 DiagKind = diag::ext_typecheck_convert_pointer_int; 17000 } 17001 ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); 17002 MayHaveConvFixit = true; 17003 break; 17004 case AssignConvertType::IntToPointer: 17005 if (getLangOpts().CPlusPlus) { 17006 DiagKind = diag::err_typecheck_convert_int_pointer; 17007 isInvalid = true; 17008 } else { 17009 DiagKind = diag::ext_typecheck_convert_int_pointer; 17010 } 17011 ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); 17012 MayHaveConvFixit = true; 17013 break; 17014 case AssignConvertType::IncompatibleFunctionPointerStrict: 17015 DiagKind = 17016 diag::warn_typecheck_convert_incompatible_function_pointer_strict; 17017 ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); 17018 MayHaveConvFixit = true; 17019 break; 17020 case AssignConvertType::IncompatibleFunctionPointer: 17021 if (getLangOpts().CPlusPlus) { 17022 DiagKind = diag::err_typecheck_convert_incompatible_function_pointer; 17023 isInvalid = true; 17024 } else { 17025 DiagKind = diag::ext_typecheck_convert_incompatible_function_pointer; 17026 } 17027 ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); 17028 MayHaveConvFixit = true; 17029 break; 17030 case AssignConvertType::IncompatiblePointer: 17031 if (Action == AssignmentAction::Passing_CFAudited) { 17032 DiagKind = diag::err_arc_typecheck_convert_incompatible_pointer; 17033 } else if (getLangOpts().CPlusPlus) { 17034 DiagKind = diag::err_typecheck_convert_incompatible_pointer; 17035 isInvalid = true; 17036 } else { 17037 DiagKind = diag::ext_typecheck_convert_incompatible_pointer; 17038 } 17039 CheckInferredResultType = DstType->isObjCObjectPointerType() && 17040 SrcType->isObjCObjectPointerType(); 17041 if (CheckInferredResultType) { 17042 SrcType = SrcType.getUnqualifiedType(); 17043 DstType = DstType.getUnqualifiedType(); 17044 } else { 17045 ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); 17046 } 17047 MayHaveConvFixit = true; 17048 break; 17049 case AssignConvertType::IncompatiblePointerSign: 17050 if (getLangOpts().CPlusPlus) { 17051 DiagKind = diag::err_typecheck_convert_incompatible_pointer_sign; 17052 isInvalid = true; 17053 } else { 17054 DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; 17055 } 17056 break; 17057 case AssignConvertType::FunctionVoidPointer: 17058 if (getLangOpts().CPlusPlus) { 17059 DiagKind = diag::err_typecheck_convert_pointer_void_func; 17060 isInvalid = true; 17061 } else { 17062 DiagKind = diag::ext_typecheck_convert_pointer_void_func; 17063 } 17064 break; 17065 case AssignConvertType::IncompatiblePointerDiscardsQualifiers: { 17066 // Perform array-to-pointer decay if necessary. 17067 if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); 17068 17069 isInvalid = true; 17070 17071 Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); 17072 Qualifiers rhq = DstType->getPointeeType().getQualifiers(); 17073 if (lhq.getAddressSpace() != rhq.getAddressSpace()) { 17074 DiagKind = diag::err_typecheck_incompatible_address_space; 17075 break; 17076 } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { 17077 DiagKind = diag::err_typecheck_incompatible_ownership; 17078 break; 17079 } else if (!lhq.getPointerAuth().isEquivalent(rhq.getPointerAuth())) { 17080 DiagKind = diag::err_typecheck_incompatible_ptrauth; 17081 break; 17082 } 17083 17084 llvm_unreachable("unknown error case for discarding qualifiers!"); 17085 // fallthrough 17086 } 17087 case AssignConvertType::CompatiblePointerDiscardsQualifiers: 17088 // If the qualifiers lost were because we were applying the 17089 // (deprecated) C++ conversion from a string literal to a char* 17090 // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: 17091 // Ideally, this check would be performed in 17092 // checkPointerTypesForAssignment. However, that would require a 17093 // bit of refactoring (so that the second argument is an 17094 // expression, rather than a type), which should be done as part 17095 // of a larger effort to fix checkPointerTypesForAssignment for 17096 // C++ semantics. 17097 if (getLangOpts().CPlusPlus && 17098 IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) 17099 return false; 17100 if (getLangOpts().CPlusPlus) { 17101 DiagKind = diag::err_typecheck_convert_discards_qualifiers; 17102 isInvalid = true; 17103 } else { 17104 DiagKind = diag::ext_typecheck_convert_discards_qualifiers; 17105 } 17106 17107 break; 17108 case AssignConvertType::IncompatibleNestedPointerQualifiers: 17109 if (getLangOpts().CPlusPlus) { 17110 isInvalid = true; 17111 DiagKind = diag::err_nested_pointer_qualifier_mismatch; 17112 } else { 17113 DiagKind = diag::ext_nested_pointer_qualifier_mismatch; 17114 } 17115 break; 17116 case AssignConvertType::IncompatibleNestedPointerAddressSpaceMismatch: 17117 DiagKind = diag::err_typecheck_incompatible_nested_address_space; 17118 isInvalid = true; 17119 break; 17120 case AssignConvertType::IntToBlockPointer: 17121 DiagKind = diag::err_int_to_block_pointer; 17122 isInvalid = true; 17123 break; 17124 case AssignConvertType::IncompatibleBlockPointer: 17125 DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; 17126 isInvalid = true; 17127 break; 17128 case AssignConvertType::IncompatibleObjCQualifiedId: { 17129 if (SrcType->isObjCQualifiedIdType()) { 17130 const ObjCObjectPointerType *srcOPT = 17131 SrcType->castAs<ObjCObjectPointerType>(); 17132 for (auto *srcProto : srcOPT->quals()) { 17133 PDecl = srcProto; 17134 break; 17135 } 17136 if (const ObjCInterfaceType *IFaceT = 17137 DstType->castAs<ObjCObjectPointerType>()->getInterfaceType()) 17138 IFace = IFaceT->getDecl(); 17139 } 17140 else if (DstType->isObjCQualifiedIdType()) { 17141 const ObjCObjectPointerType *dstOPT = 17142 DstType->castAs<ObjCObjectPointerType>(); 17143 for (auto *dstProto : dstOPT->quals()) { 17144 PDecl = dstProto; 17145 break; 17146 } 17147 if (const ObjCInterfaceType *IFaceT = 17148 SrcType->castAs<ObjCObjectPointerType>()->getInterfaceType()) 17149 IFace = IFaceT->getDecl(); 17150 } 17151 if (getLangOpts().CPlusPlus) { 17152 DiagKind = diag::err_incompatible_qualified_id; 17153 isInvalid = true; 17154 } else { 17155 DiagKind = diag::warn_incompatible_qualified_id; 17156 } 17157 break; 17158 } 17159 case AssignConvertType::IncompatibleVectors: 17160 if (getLangOpts().CPlusPlus) { 17161 DiagKind = diag::err_incompatible_vectors; 17162 isInvalid = true; 17163 } else { 17164 DiagKind = diag::warn_incompatible_vectors; 17165 } 17166 break; 17167 case AssignConvertType::IncompatibleObjCWeakRef: 17168 DiagKind = diag::err_arc_weak_unavailable_assign; 17169 isInvalid = true; 17170 break; 17171 case AssignConvertType::Incompatible: 17172 if (maybeDiagnoseAssignmentToFunction(*this, DstType, SrcExpr)) { 17173 if (Complained) 17174 *Complained = true; 17175 return true; 17176 } 17177 17178 DiagKind = diag::err_typecheck_convert_incompatible; 17179 ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); 17180 MayHaveConvFixit = true; 17181 isInvalid = true; 17182 MayHaveFunctionDiff = true; 17183 break; 17184 } 17185 17186 QualType FirstType, SecondType; 17187 switch (Action) { 17188 case AssignmentAction::Assigning: 17189 case AssignmentAction::Initializing: 17190 // The destination type comes first. 17191 FirstType = DstType; 17192 SecondType = SrcType; 17193 break; 17194 17195 case AssignmentAction::Returning: 17196 case AssignmentAction::Passing: 17197 case AssignmentAction::Passing_CFAudited: 17198 case AssignmentAction::Converting: 17199 case AssignmentAction::Sending: 17200 case AssignmentAction::Casting: 17201 // The source type comes first. 17202 FirstType = SrcType; 17203 SecondType = DstType; 17204 break; 17205 } 17206 17207 PartialDiagnostic FDiag = PDiag(DiagKind); 17208 AssignmentAction ActionForDiag = Action; 17209 if (Action == AssignmentAction::Passing_CFAudited) 17210 ActionForDiag = AssignmentAction::Passing; 17211 17212 FDiag << FirstType << SecondType << ActionForDiag 17213 << SrcExpr->getSourceRange(); 17214 17215 if (DiagKind == diag::ext_typecheck_convert_incompatible_pointer_sign || 17216 DiagKind == diag::err_typecheck_convert_incompatible_pointer_sign) { 17217 auto isPlainChar = [](const clang::Type *Type) { 17218 return Type->isSpecificBuiltinType(BuiltinType::Char_S) || 17219 Type->isSpecificBuiltinType(BuiltinType::Char_U); 17220 }; 17221 FDiag << (isPlainChar(FirstType->getPointeeOrArrayElementType()) || 17222 isPlainChar(SecondType->getPointeeOrArrayElementType())); 17223 } 17224 17225 // If we can fix the conversion, suggest the FixIts. 17226 if (!ConvHints.isNull()) { 17227 for (FixItHint &H : ConvHints.Hints) 17228 FDiag << H; 17229 } 17230 17231 if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } 17232 17233 if (MayHaveFunctionDiff) 17234 HandleFunctionTypeMismatch(FDiag, SecondType, FirstType); 17235 17236 Diag(Loc, FDiag); 17237 if ((DiagKind == diag::warn_incompatible_qualified_id || 17238 DiagKind == diag::err_incompatible_qualified_id) && 17239 PDecl && IFace && !IFace->hasDefinition()) 17240 Diag(IFace->getLocation(), diag::note_incomplete_class_and_qualified_id) 17241 << IFace << PDecl; 17242 17243 if (SecondType == Context.OverloadTy) 17244 NoteAllOverloadCandidates(OverloadExpr::find(SrcExpr).Expression, 17245 FirstType, /*TakingAddress=*/true); 17246 17247 if (CheckInferredResultType) 17248 ObjC().EmitRelatedResultTypeNote(SrcExpr); 17249 17250 if (Action == AssignmentAction::Returning && 17251 ConvTy == AssignConvertType::IncompatiblePointer) 17252 ObjC().EmitRelatedResultTypeNoteForReturn(DstType); 17253 17254 if (Complained) 17255 *Complained = true; 17256 return isInvalid; 17257 } 17258 17259 ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, 17260 llvm::APSInt *Result, 17261 AllowFoldKind CanFold) { 17262 class SimpleICEDiagnoser : public VerifyICEDiagnoser { 17263 public: 17264 SemaDiagnosticBuilder diagnoseNotICEType(Sema &S, SourceLocation Loc, 17265 QualType T) override { 17266 return S.Diag(Loc, diag::err_ice_not_integral) 17267 << T << S.LangOpts.CPlusPlus; 17268 } 17269 SemaDiagnosticBuilder diagnoseNotICE(Sema &S, SourceLocation Loc) override { 17270 return S.Diag(Loc, diag::err_expr_not_ice) << S.LangOpts.CPlusPlus; 17271 } 17272 } Diagnoser; 17273 17274 return VerifyIntegerConstantExpression(E, Result, Diagnoser, CanFold); 17275 } 17276 17277 ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, 17278 llvm::APSInt *Result, 17279 unsigned DiagID, 17280 AllowFoldKind CanFold) { 17281 class IDDiagnoser : public VerifyICEDiagnoser { 17282 unsigned DiagID; 17283 17284 public: 17285 IDDiagnoser(unsigned DiagID) 17286 : VerifyICEDiagnoser(DiagID == 0), DiagID(DiagID) { } 17287 17288 SemaDiagnosticBuilder diagnoseNotICE(Sema &S, SourceLocation Loc) override { 17289 return S.Diag(Loc, DiagID); 17290 } 17291 } Diagnoser(DiagID); 17292 17293 return VerifyIntegerConstantExpression(E, Result, Diagnoser, CanFold); 17294 } 17295 17296 Sema::SemaDiagnosticBuilder 17297 Sema::VerifyICEDiagnoser::diagnoseNotICEType(Sema &S, SourceLocation Loc, 17298 QualType T) { 17299 return diagnoseNotICE(S, Loc); 17300 } 17301 17302 Sema::SemaDiagnosticBuilder 17303 Sema::VerifyICEDiagnoser::diagnoseFold(Sema &S, SourceLocation Loc) { 17304 return S.Diag(Loc, diag::ext_expr_not_ice) << S.LangOpts.CPlusPlus; 17305 } 17306 17307 ExprResult 17308 Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, 17309 VerifyICEDiagnoser &Diagnoser, 17310 AllowFoldKind CanFold) { 17311 SourceLocation DiagLoc = E->getBeginLoc(); 17312 17313 if (getLangOpts().CPlusPlus11) { 17314 // C++11 [expr.const]p5: 17315 // If an expression of literal class type is used in a context where an 17316 // integral constant expression is required, then that class type shall 17317 // have a single non-explicit conversion function to an integral or 17318 // unscoped enumeration type 17319 ExprResult Converted; 17320 class CXX11ConvertDiagnoser : public ICEConvertDiagnoser { 17321 VerifyICEDiagnoser &BaseDiagnoser; 17322 public: 17323 CXX11ConvertDiagnoser(VerifyICEDiagnoser &BaseDiagnoser) 17324 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, 17325 BaseDiagnoser.Suppress, true), 17326 BaseDiagnoser(BaseDiagnoser) {} 17327 17328 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 17329 QualType T) override { 17330 return BaseDiagnoser.diagnoseNotICEType(S, Loc, T); 17331 } 17332 17333 SemaDiagnosticBuilder diagnoseIncomplete( 17334 Sema &S, SourceLocation Loc, QualType T) override { 17335 return S.Diag(Loc, diag::err_ice_incomplete_type) << T; 17336 } 17337 17338 SemaDiagnosticBuilder diagnoseExplicitConv( 17339 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { 17340 return S.Diag(Loc, diag::err_ice_explicit_conversion) << T << ConvTy; 17341 } 17342 17343 SemaDiagnosticBuilder noteExplicitConv( 17344 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { 17345 return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) 17346 << ConvTy->isEnumeralType() << ConvTy; 17347 } 17348 17349 SemaDiagnosticBuilder diagnoseAmbiguous( 17350 Sema &S, SourceLocation Loc, QualType T) override { 17351 return S.Diag(Loc, diag::err_ice_ambiguous_conversion) << T; 17352 } 17353 17354 SemaDiagnosticBuilder noteAmbiguous( 17355 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { 17356 return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) 17357 << ConvTy->isEnumeralType() << ConvTy; 17358 } 17359 17360 SemaDiagnosticBuilder diagnoseConversion( 17361 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { 17362 llvm_unreachable("conversion functions are permitted"); 17363 } 17364 } ConvertDiagnoser(Diagnoser); 17365 17366 Converted = PerformContextualImplicitConversion(DiagLoc, E, 17367 ConvertDiagnoser); 17368 if (Converted.isInvalid()) 17369 return Converted; 17370 E = Converted.get(); 17371 // The 'explicit' case causes us to get a RecoveryExpr. Give up here so we 17372 // don't try to evaluate it later. We also don't want to return the 17373 // RecoveryExpr here, as it results in this call succeeding, thus callers of 17374 // this function will attempt to use 'Value'. 17375 if (isa<RecoveryExpr>(E)) 17376 return ExprError(); 17377 if (!E->getType()->isIntegralOrUnscopedEnumerationType()) 17378 return ExprError(); 17379 } else if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { 17380 // An ICE must be of integral or unscoped enumeration type. 17381 if (!Diagnoser.Suppress) 17382 Diagnoser.diagnoseNotICEType(*this, DiagLoc, E->getType()) 17383 << E->getSourceRange(); 17384 return ExprError(); 17385 } 17386 17387 ExprResult RValueExpr = DefaultLvalueConversion(E); 17388 if (RValueExpr.isInvalid()) 17389 return ExprError(); 17390 17391 E = RValueExpr.get(); 17392 17393 // Circumvent ICE checking in C++11 to avoid evaluating the expression twice 17394 // in the non-ICE case. 17395 if (!getLangOpts().CPlusPlus11 && E->isIntegerConstantExpr(Context)) { 17396 SmallVector<PartialDiagnosticAt, 8> Notes; 17397 if (Result) 17398 *Result = E->EvaluateKnownConstIntCheckOverflow(Context, &Notes); 17399 if (!isa<ConstantExpr>(E)) 17400 E = Result ? ConstantExpr::Create(Context, E, APValue(*Result)) 17401 : ConstantExpr::Create(Context, E); 17402 17403 if (Notes.empty()) 17404 return E; 17405 17406 // If our only note is the usual "invalid subexpression" note, just point 17407 // the caret at its location rather than producing an essentially 17408 // redundant note. 17409 if (Notes.size() == 1 && Notes[0].second.getDiagID() == 17410 diag::note_invalid_subexpr_in_const_expr) { 17411 DiagLoc = Notes[0].first; 17412 Notes.clear(); 17413 } 17414 17415 if (getLangOpts().CPlusPlus) { 17416 if (!Diagnoser.Suppress) { 17417 Diagnoser.diagnoseNotICE(*this, DiagLoc) << E->getSourceRange(); 17418 for (const PartialDiagnosticAt &Note : Notes) 17419 Diag(Note.first, Note.second); 17420 } 17421 return ExprError(); 17422 } 17423 17424 Diagnoser.diagnoseFold(*this, DiagLoc) << E->getSourceRange(); 17425 for (const PartialDiagnosticAt &Note : Notes) 17426 Diag(Note.first, Note.second); 17427 17428 return E; 17429 } 17430 17431 Expr::EvalResult EvalResult; 17432 SmallVector<PartialDiagnosticAt, 8> Notes; 17433 EvalResult.Diag = &Notes; 17434 17435 // Try to evaluate the expression, and produce diagnostics explaining why it's 17436 // not a constant expression as a side-effect. 17437 bool Folded = 17438 E->EvaluateAsRValue(EvalResult, Context, /*isConstantContext*/ true) && 17439 EvalResult.Val.isInt() && !EvalResult.HasSideEffects && 17440 (!getLangOpts().CPlusPlus || !EvalResult.HasUndefinedBehavior); 17441 17442 if (!isa<ConstantExpr>(E)) 17443 E = ConstantExpr::Create(Context, E, EvalResult.Val); 17444 17445 // In C++11, we can rely on diagnostics being produced for any expression 17446 // which is not a constant expression. If no diagnostics were produced, then 17447 // this is a constant expression. 17448 if (Folded && getLangOpts().CPlusPlus11 && Notes.empty()) { 17449 if (Result) 17450 *Result = EvalResult.Val.getInt(); 17451 return E; 17452 } 17453 17454 // If our only note is the usual "invalid subexpression" note, just point 17455 // the caret at its location rather than producing an essentially 17456 // redundant note. 17457 if (Notes.size() == 1 && Notes[0].second.getDiagID() == 17458 diag::note_invalid_subexpr_in_const_expr) { 17459 DiagLoc = Notes[0].first; 17460 Notes.clear(); 17461 } 17462 17463 if (!Folded || CanFold == AllowFoldKind::No) { 17464 if (!Diagnoser.Suppress) { 17465 Diagnoser.diagnoseNotICE(*this, DiagLoc) << E->getSourceRange(); 17466 for (const PartialDiagnosticAt &Note : Notes) 17467 Diag(Note.first, Note.second); 17468 } 17469 17470 return ExprError(); 17471 } 17472 17473 Diagnoser.diagnoseFold(*this, DiagLoc) << E->getSourceRange(); 17474 for (const PartialDiagnosticAt &Note : Notes) 17475 Diag(Note.first, Note.second); 17476 17477 if (Result) 17478 *Result = EvalResult.Val.getInt(); 17479 return E; 17480 } 17481 17482 namespace { 17483 // Handle the case where we conclude a expression which we speculatively 17484 // considered to be unevaluated is actually evaluated. 17485 class TransformToPE : public TreeTransform<TransformToPE> { 17486 typedef TreeTransform<TransformToPE> BaseTransform; 17487 17488 public: 17489 TransformToPE(Sema &SemaRef) : BaseTransform(SemaRef) { } 17490 17491 // Make sure we redo semantic analysis 17492 bool AlwaysRebuild() { return true; } 17493 bool ReplacingOriginal() { return true; } 17494 17495 // We need to special-case DeclRefExprs referring to FieldDecls which 17496 // are not part of a member pointer formation; normal TreeTransforming 17497 // doesn't catch this case because of the way we represent them in the AST. 17498 // FIXME: This is a bit ugly; is it really the best way to handle this 17499 // case? 17500 // 17501 // Error on DeclRefExprs referring to FieldDecls. 17502 ExprResult TransformDeclRefExpr(DeclRefExpr *E) { 17503 if (isa<FieldDecl>(E->getDecl()) && 17504 !SemaRef.isUnevaluatedContext()) 17505 return SemaRef.Diag(E->getLocation(), 17506 diag::err_invalid_non_static_member_use) 17507 << E->getDecl() << E->getSourceRange(); 17508 17509 return BaseTransform::TransformDeclRefExpr(E); 17510 } 17511 17512 // Exception: filter out member pointer formation 17513 ExprResult TransformUnaryOperator(UnaryOperator *E) { 17514 if (E->getOpcode() == UO_AddrOf && E->getType()->isMemberPointerType()) 17515 return E; 17516 17517 return BaseTransform::TransformUnaryOperator(E); 17518 } 17519 17520 // The body of a lambda-expression is in a separate expression evaluation 17521 // context so never needs to be transformed. 17522 // FIXME: Ideally we wouldn't transform the closure type either, and would 17523 // just recreate the capture expressions and lambda expression. 17524 StmtResult TransformLambdaBody(LambdaExpr *E, Stmt *Body) { 17525 return SkipLambdaBody(E, Body); 17526 } 17527 }; 17528 } 17529 17530 ExprResult Sema::TransformToPotentiallyEvaluated(Expr *E) { 17531 assert(isUnevaluatedContext() && 17532 "Should only transform unevaluated expressions"); 17533 ExprEvalContexts.back().Context = 17534 ExprEvalContexts[ExprEvalContexts.size()-2].Context; 17535 if (isUnevaluatedContext()) 17536 return E; 17537 return TransformToPE(*this).TransformExpr(E); 17538 } 17539 17540 TypeSourceInfo *Sema::TransformToPotentiallyEvaluated(TypeSourceInfo *TInfo) { 17541 assert(isUnevaluatedContext() && 17542 "Should only transform unevaluated expressions"); 17543 ExprEvalContexts.back().Context = parentEvaluationContext().Context; 17544 if (isUnevaluatedContext()) 17545 return TInfo; 17546 return TransformToPE(*this).TransformType(TInfo); 17547 } 17548 17549 void 17550 Sema::PushExpressionEvaluationContext( 17551 ExpressionEvaluationContext NewContext, Decl *LambdaContextDecl, 17552 ExpressionEvaluationContextRecord::ExpressionKind ExprContext) { 17553 ExprEvalContexts.emplace_back(NewContext, ExprCleanupObjects.size(), Cleanup, 17554 LambdaContextDecl, ExprContext); 17555 17556 // Discarded statements and immediate contexts nested in other 17557 // discarded statements or immediate context are themselves 17558 // a discarded statement or an immediate context, respectively. 17559 ExprEvalContexts.back().InDiscardedStatement = 17560 parentEvaluationContext().isDiscardedStatementContext(); 17561 17562 // C++23 [expr.const]/p15 17563 // An expression or conversion is in an immediate function context if [...] 17564 // it is a subexpression of a manifestly constant-evaluated expression or 17565 // conversion. 17566 const auto &Prev = parentEvaluationContext(); 17567 ExprEvalContexts.back().InImmediateFunctionContext = 17568 Prev.isImmediateFunctionContext() || Prev.isConstantEvaluated(); 17569 17570 ExprEvalContexts.back().InImmediateEscalatingFunctionContext = 17571 Prev.InImmediateEscalatingFunctionContext; 17572 17573 Cleanup.reset(); 17574 if (!MaybeODRUseExprs.empty()) 17575 std::swap(MaybeODRUseExprs, ExprEvalContexts.back().SavedMaybeODRUseExprs); 17576 } 17577 17578 void 17579 Sema::PushExpressionEvaluationContext( 17580 ExpressionEvaluationContext NewContext, ReuseLambdaContextDecl_t, 17581 ExpressionEvaluationContextRecord::ExpressionKind ExprContext) { 17582 Decl *ClosureContextDecl = ExprEvalContexts.back().ManglingContextDecl; 17583 PushExpressionEvaluationContext(NewContext, ClosureContextDecl, ExprContext); 17584 } 17585 17586 void Sema::PushExpressionEvaluationContextForFunction( 17587 ExpressionEvaluationContext NewContext, FunctionDecl *FD) { 17588 // [expr.const]/p14.1 17589 // An expression or conversion is in an immediate function context if it is 17590 // potentially evaluated and either: its innermost enclosing non-block scope 17591 // is a function parameter scope of an immediate function. 17592 PushExpressionEvaluationContext( 17593 FD && FD->isConsteval() 17594 ? ExpressionEvaluationContext::ImmediateFunctionContext 17595 : NewContext); 17596 const Sema::ExpressionEvaluationContextRecord &Parent = 17597 parentEvaluationContext(); 17598 Sema::ExpressionEvaluationContextRecord &Current = currentEvaluationContext(); 17599 17600 Current.InDiscardedStatement = false; 17601 17602 if (FD) { 17603 17604 // Each ExpressionEvaluationContextRecord also keeps track of whether the 17605 // context is nested in an immediate function context, so smaller contexts 17606 // that appear inside immediate functions (like variable initializers) are 17607 // considered to be inside an immediate function context even though by 17608 // themselves they are not immediate function contexts. But when a new 17609 // function is entered, we need to reset this tracking, since the entered 17610 // function might be not an immediate function. 17611 17612 Current.InImmediateEscalatingFunctionContext = 17613 getLangOpts().CPlusPlus20 && FD->isImmediateEscalating(); 17614 17615 if (isLambdaMethod(FD)) 17616 Current.InImmediateFunctionContext = 17617 FD->isConsteval() || 17618 (isLambdaMethod(FD) && (Parent.isConstantEvaluated() || 17619 Parent.isImmediateFunctionContext())); 17620 else 17621 Current.InImmediateFunctionContext = FD->isConsteval(); 17622 } 17623 } 17624 17625 namespace { 17626 17627 const DeclRefExpr *CheckPossibleDeref(Sema &S, const Expr *PossibleDeref) { 17628 PossibleDeref = PossibleDeref->IgnoreParenImpCasts(); 17629 if (const auto *E = dyn_cast<UnaryOperator>(PossibleDeref)) { 17630 if (E->getOpcode() == UO_Deref) 17631 return CheckPossibleDeref(S, E->getSubExpr()); 17632 } else if (const auto *E = dyn_cast<ArraySubscriptExpr>(PossibleDeref)) { 17633 return CheckPossibleDeref(S, E->getBase()); 17634 } else if (const auto *E = dyn_cast<MemberExpr>(PossibleDeref)) { 17635 return CheckPossibleDeref(S, E->getBase()); 17636 } else if (const auto E = dyn_cast<DeclRefExpr>(PossibleDeref)) { 17637 QualType Inner; 17638 QualType Ty = E->getType(); 17639 if (const auto *Ptr = Ty->getAs<PointerType>()) 17640 Inner = Ptr->getPointeeType(); 17641 else if (const auto *Arr = S.Context.getAsArrayType(Ty)) 17642 Inner = Arr->getElementType(); 17643 else 17644 return nullptr; 17645 17646 if (Inner->hasAttr(attr::NoDeref)) 17647 return E; 17648 } 17649 return nullptr; 17650 } 17651 17652 } // namespace 17653 17654 void Sema::WarnOnPendingNoDerefs(ExpressionEvaluationContextRecord &Rec) { 17655 for (const Expr *E : Rec.PossibleDerefs) { 17656 const DeclRefExpr *DeclRef = CheckPossibleDeref(*this, E); 17657 if (DeclRef) { 17658 const ValueDecl *Decl = DeclRef->getDecl(); 17659 Diag(E->getExprLoc(), diag::warn_dereference_of_noderef_type) 17660 << Decl->getName() << E->getSourceRange(); 17661 Diag(Decl->getLocation(), diag::note_previous_decl) << Decl->getName(); 17662 } else { 17663 Diag(E->getExprLoc(), diag::warn_dereference_of_noderef_type_no_decl) 17664 << E->getSourceRange(); 17665 } 17666 } 17667 Rec.PossibleDerefs.clear(); 17668 } 17669 17670 void Sema::CheckUnusedVolatileAssignment(Expr *E) { 17671 if (!E->getType().isVolatileQualified() || !getLangOpts().CPlusPlus20) 17672 return; 17673 17674 // Note: ignoring parens here is not justified by the standard rules, but 17675 // ignoring parentheses seems like a more reasonable approach, and this only 17676 // drives a deprecation warning so doesn't affect conformance. 17677 if (auto *BO = dyn_cast<BinaryOperator>(E->IgnoreParenImpCasts())) { 17678 if (BO->getOpcode() == BO_Assign) { 17679 auto &LHSs = ExprEvalContexts.back().VolatileAssignmentLHSs; 17680 llvm::erase(LHSs, BO->getLHS()); 17681 } 17682 } 17683 } 17684 17685 void Sema::MarkExpressionAsImmediateEscalating(Expr *E) { 17686 assert(getLangOpts().CPlusPlus20 && 17687 ExprEvalContexts.back().InImmediateEscalatingFunctionContext && 17688 "Cannot mark an immediate escalating expression outside of an " 17689 "immediate escalating context"); 17690 if (auto *Call = dyn_cast<CallExpr>(E->IgnoreImplicit()); 17691 Call && Call->getCallee()) { 17692 if (auto *DeclRef = 17693 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) 17694 DeclRef->setIsImmediateEscalating(true); 17695 } else if (auto *Ctr = dyn_cast<CXXConstructExpr>(E->IgnoreImplicit())) { 17696 Ctr->setIsImmediateEscalating(true); 17697 } else if (auto *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreImplicit())) { 17698 DeclRef->setIsImmediateEscalating(true); 17699 } else { 17700 assert(false && "expected an immediately escalating expression"); 17701 } 17702 if (FunctionScopeInfo *FI = getCurFunction()) 17703 FI->FoundImmediateEscalatingExpression = true; 17704 } 17705 17706 ExprResult Sema::CheckForImmediateInvocation(ExprResult E, FunctionDecl *Decl) { 17707 if (isUnevaluatedContext() || !E.isUsable() || !Decl || 17708 !Decl->isImmediateFunction() || isAlwaysConstantEvaluatedContext() || 17709 isCheckingDefaultArgumentOrInitializer() || 17710 RebuildingImmediateInvocation || isImmediateFunctionContext()) 17711 return E; 17712 17713 /// Opportunistically remove the callee from ReferencesToConsteval if we can. 17714 /// It's OK if this fails; we'll also remove this in 17715 /// HandleImmediateInvocations, but catching it here allows us to avoid 17716 /// walking the AST looking for it in simple cases. 17717 if (auto *Call = dyn_cast<CallExpr>(E.get()->IgnoreImplicit())) 17718 if (auto *DeclRef = 17719 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) 17720 ExprEvalContexts.back().ReferenceToConsteval.erase(DeclRef); 17721 17722 // C++23 [expr.const]/p16 17723 // An expression or conversion is immediate-escalating if it is not initially 17724 // in an immediate function context and it is [...] an immediate invocation 17725 // that is not a constant expression and is not a subexpression of an 17726 // immediate invocation. 17727 APValue Cached; 17728 auto CheckConstantExpressionAndKeepResult = [&]() { 17729 llvm::SmallVector<PartialDiagnosticAt, 8> Notes; 17730 Expr::EvalResult Eval; 17731 Eval.Diag = &Notes; 17732 bool Res = E.get()->EvaluateAsConstantExpr( 17733 Eval, getASTContext(), ConstantExprKind::ImmediateInvocation); 17734 if (Res && Notes.empty()) { 17735 Cached = std::move(Eval.Val); 17736 return true; 17737 } 17738 return false; 17739 }; 17740 17741 if (!E.get()->isValueDependent() && 17742 ExprEvalContexts.back().InImmediateEscalatingFunctionContext && 17743 !CheckConstantExpressionAndKeepResult()) { 17744 MarkExpressionAsImmediateEscalating(E.get()); 17745 return E; 17746 } 17747 17748 if (Cleanup.exprNeedsCleanups()) { 17749 // Since an immediate invocation is a full expression itself - it requires 17750 // an additional ExprWithCleanups node, but it can participate to a bigger 17751 // full expression which actually requires cleanups to be run after so 17752 // create ExprWithCleanups without using MaybeCreateExprWithCleanups as it 17753 // may discard cleanups for outer expression too early. 17754 17755 // Note that ExprWithCleanups created here must always have empty cleanup 17756 // objects: 17757 // - compound literals do not create cleanup objects in C++ and immediate 17758 // invocations are C++-only. 17759 // - blocks are not allowed inside constant expressions and compiler will 17760 // issue an error if they appear there. 17761 // 17762 // Hence, in correct code any cleanup objects created inside current 17763 // evaluation context must be outside the immediate invocation. 17764 E = ExprWithCleanups::Create(getASTContext(), E.get(), 17765 Cleanup.cleanupsHaveSideEffects(), {}); 17766 } 17767 17768 ConstantExpr *Res = ConstantExpr::Create( 17769 getASTContext(), E.get(), 17770 ConstantExpr::getStorageKind(Decl->getReturnType().getTypePtr(), 17771 getASTContext()), 17772 /*IsImmediateInvocation*/ true); 17773 if (Cached.hasValue()) 17774 Res->MoveIntoResult(Cached, getASTContext()); 17775 /// Value-dependent constant expressions should not be immediately 17776 /// evaluated until they are instantiated. 17777 if (!Res->isValueDependent()) 17778 ExprEvalContexts.back().ImmediateInvocationCandidates.emplace_back(Res, 0); 17779 return Res; 17780 } 17781 17782 static void EvaluateAndDiagnoseImmediateInvocation( 17783 Sema &SemaRef, Sema::ImmediateInvocationCandidate Candidate) { 17784 llvm::SmallVector<PartialDiagnosticAt, 8> Notes; 17785 Expr::EvalResult Eval; 17786 Eval.Diag = &Notes; 17787 ConstantExpr *CE = Candidate.getPointer(); 17788 bool Result = CE->EvaluateAsConstantExpr( 17789 Eval, SemaRef.getASTContext(), ConstantExprKind::ImmediateInvocation); 17790 if (!Result || !Notes.empty()) { 17791 SemaRef.FailedImmediateInvocations.insert(CE); 17792 Expr *InnerExpr = CE->getSubExpr()->IgnoreImplicit(); 17793 if (auto *FunctionalCast = dyn_cast<CXXFunctionalCastExpr>(InnerExpr)) 17794 InnerExpr = FunctionalCast->getSubExpr()->IgnoreImplicit(); 17795 FunctionDecl *FD = nullptr; 17796 if (auto *Call = dyn_cast<CallExpr>(InnerExpr)) 17797 FD = cast<FunctionDecl>(Call->getCalleeDecl()); 17798 else if (auto *Call = dyn_cast<CXXConstructExpr>(InnerExpr)) 17799 FD = Call->getConstructor(); 17800 else if (auto *Cast = dyn_cast<CastExpr>(InnerExpr)) 17801 FD = dyn_cast_or_null<FunctionDecl>(Cast->getConversionFunction()); 17802 17803 assert(FD && FD->isImmediateFunction() && 17804 "could not find an immediate function in this expression"); 17805 if (FD->isInvalidDecl()) 17806 return; 17807 SemaRef.Diag(CE->getBeginLoc(), diag::err_invalid_consteval_call) 17808 << FD << FD->isConsteval(); 17809 if (auto Context = 17810 SemaRef.InnermostDeclarationWithDelayedImmediateInvocations()) { 17811 SemaRef.Diag(Context->Loc, diag::note_invalid_consteval_initializer) 17812 << Context->Decl; 17813 SemaRef.Diag(Context->Decl->getBeginLoc(), diag::note_declared_at); 17814 } 17815 if (!FD->isConsteval()) 17816 SemaRef.DiagnoseImmediateEscalatingReason(FD); 17817 for (auto &Note : Notes) 17818 SemaRef.Diag(Note.first, Note.second); 17819 return; 17820 } 17821 CE->MoveIntoResult(Eval.Val, SemaRef.getASTContext()); 17822 } 17823 17824 static void RemoveNestedImmediateInvocation( 17825 Sema &SemaRef, Sema::ExpressionEvaluationContextRecord &Rec, 17826 SmallVector<Sema::ImmediateInvocationCandidate, 4>::reverse_iterator It) { 17827 struct ComplexRemove : TreeTransform<ComplexRemove> { 17828 using Base = TreeTransform<ComplexRemove>; 17829 llvm::SmallPtrSetImpl<DeclRefExpr *> &DRSet; 17830 SmallVector<Sema::ImmediateInvocationCandidate, 4> &IISet; 17831 SmallVector<Sema::ImmediateInvocationCandidate, 4>::reverse_iterator 17832 CurrentII; 17833 ComplexRemove(Sema &SemaRef, llvm::SmallPtrSetImpl<DeclRefExpr *> &DR, 17834 SmallVector<Sema::ImmediateInvocationCandidate, 4> &II, 17835 SmallVector<Sema::ImmediateInvocationCandidate, 17836 4>::reverse_iterator Current) 17837 : Base(SemaRef), DRSet(DR), IISet(II), CurrentII(Current) {} 17838 void RemoveImmediateInvocation(ConstantExpr* E) { 17839 auto It = std::find_if(CurrentII, IISet.rend(), 17840 [E](Sema::ImmediateInvocationCandidate Elem) { 17841 return Elem.getPointer() == E; 17842 }); 17843 // It is possible that some subexpression of the current immediate 17844 // invocation was handled from another expression evaluation context. Do 17845 // not handle the current immediate invocation if some of its 17846 // subexpressions failed before. 17847 if (It == IISet.rend()) { 17848 if (SemaRef.FailedImmediateInvocations.contains(E)) 17849 CurrentII->setInt(1); 17850 } else { 17851 It->setInt(1); // Mark as deleted 17852 } 17853 } 17854 ExprResult TransformConstantExpr(ConstantExpr *E) { 17855 if (!E->isImmediateInvocation()) 17856 return Base::TransformConstantExpr(E); 17857 RemoveImmediateInvocation(E); 17858 return Base::TransformExpr(E->getSubExpr()); 17859 } 17860 /// Base::TransfromCXXOperatorCallExpr doesn't traverse the callee so 17861 /// we need to remove its DeclRefExpr from the DRSet. 17862 ExprResult TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) { 17863 DRSet.erase(cast<DeclRefExpr>(E->getCallee()->IgnoreImplicit())); 17864 return Base::TransformCXXOperatorCallExpr(E); 17865 } 17866 /// Base::TransformUserDefinedLiteral doesn't preserve the 17867 /// UserDefinedLiteral node. 17868 ExprResult TransformUserDefinedLiteral(UserDefinedLiteral *E) { return E; } 17869 /// Base::TransformInitializer skips ConstantExpr so we need to visit them 17870 /// here. 17871 ExprResult TransformInitializer(Expr *Init, bool NotCopyInit) { 17872 if (!Init) 17873 return Init; 17874 17875 // We cannot use IgnoreImpCasts because we need to preserve 17876 // full expressions. 17877 while (true) { 17878 if (auto *ICE = dyn_cast<ImplicitCastExpr>(Init)) 17879 Init = ICE->getSubExpr(); 17880 else if (auto *ICE = dyn_cast<MaterializeTemporaryExpr>(Init)) 17881 Init = ICE->getSubExpr(); 17882 else 17883 break; 17884 } 17885 /// ConstantExprs are the first layer of implicit node to be removed so if 17886 /// Init isn't a ConstantExpr, no ConstantExpr will be skipped. 17887 if (auto *CE = dyn_cast<ConstantExpr>(Init); 17888 CE && CE->isImmediateInvocation()) 17889 RemoveImmediateInvocation(CE); 17890 return Base::TransformInitializer(Init, NotCopyInit); 17891 } 17892 ExprResult TransformDeclRefExpr(DeclRefExpr *E) { 17893 DRSet.erase(E); 17894 return E; 17895 } 17896 ExprResult TransformLambdaExpr(LambdaExpr *E) { 17897 // Do not rebuild lambdas to avoid creating a new type. 17898 // Lambdas have already been processed inside their eval contexts. 17899 return E; 17900 } 17901 bool AlwaysRebuild() { return false; } 17902 bool ReplacingOriginal() { return true; } 17903 bool AllowSkippingCXXConstructExpr() { 17904 bool Res = AllowSkippingFirstCXXConstructExpr; 17905 AllowSkippingFirstCXXConstructExpr = true; 17906 return Res; 17907 } 17908 bool AllowSkippingFirstCXXConstructExpr = true; 17909 } Transformer(SemaRef, Rec.ReferenceToConsteval, 17910 Rec.ImmediateInvocationCandidates, It); 17911 17912 /// CXXConstructExpr with a single argument are getting skipped by 17913 /// TreeTransform in some situtation because they could be implicit. This 17914 /// can only occur for the top-level CXXConstructExpr because it is used 17915 /// nowhere in the expression being transformed therefore will not be rebuilt. 17916 /// Setting AllowSkippingFirstCXXConstructExpr to false will prevent from 17917 /// skipping the first CXXConstructExpr. 17918 if (isa<CXXConstructExpr>(It->getPointer()->IgnoreImplicit())) 17919 Transformer.AllowSkippingFirstCXXConstructExpr = false; 17920 17921 ExprResult Res = Transformer.TransformExpr(It->getPointer()->getSubExpr()); 17922 // The result may not be usable in case of previous compilation errors. 17923 // In this case evaluation of the expression may result in crash so just 17924 // don't do anything further with the result. 17925 if (Res.isUsable()) { 17926 Res = SemaRef.MaybeCreateExprWithCleanups(Res); 17927 It->getPointer()->setSubExpr(Res.get()); 17928 } 17929 } 17930 17931 static void 17932 HandleImmediateInvocations(Sema &SemaRef, 17933 Sema::ExpressionEvaluationContextRecord &Rec) { 17934 if ((Rec.ImmediateInvocationCandidates.size() == 0 && 17935 Rec.ReferenceToConsteval.size() == 0) || 17936 Rec.isImmediateFunctionContext() || SemaRef.RebuildingImmediateInvocation) 17937 return; 17938 17939 // An expression or conversion is 'manifestly constant-evaluated' if it is: 17940 // [...] 17941 // - the initializer of a variable that is usable in constant expressions or 17942 // has constant initialization. 17943 if (SemaRef.getLangOpts().CPlusPlus23 && 17944 Rec.ExprContext == 17945 Sema::ExpressionEvaluationContextRecord::EK_VariableInit) { 17946 auto *VD = cast<VarDecl>(Rec.ManglingContextDecl); 17947 if (VD->isUsableInConstantExpressions(SemaRef.Context) || 17948 VD->hasConstantInitialization()) { 17949 // An expression or conversion is in an 'immediate function context' if it 17950 // is potentially evaluated and either: 17951 // [...] 17952 // - it is a subexpression of a manifestly constant-evaluated expression 17953 // or conversion. 17954 return; 17955 } 17956 } 17957 17958 /// When we have more than 1 ImmediateInvocationCandidates or previously 17959 /// failed immediate invocations, we need to check for nested 17960 /// ImmediateInvocationCandidates in order to avoid duplicate diagnostics. 17961 /// Otherwise we only need to remove ReferenceToConsteval in the immediate 17962 /// invocation. 17963 if (Rec.ImmediateInvocationCandidates.size() > 1 || 17964 !SemaRef.FailedImmediateInvocations.empty()) { 17965 17966 /// Prevent sema calls during the tree transform from adding pointers that 17967 /// are already in the sets. 17968 llvm::SaveAndRestore DisableIITracking( 17969 SemaRef.RebuildingImmediateInvocation, true); 17970 17971 /// Prevent diagnostic during tree transfrom as they are duplicates 17972 Sema::TentativeAnalysisScope DisableDiag(SemaRef); 17973 17974 for (auto It = Rec.ImmediateInvocationCandidates.rbegin(); 17975 It != Rec.ImmediateInvocationCandidates.rend(); It++) 17976 if (!It->getInt()) 17977 RemoveNestedImmediateInvocation(SemaRef, Rec, It); 17978 } else if (Rec.ImmediateInvocationCandidates.size() == 1 && 17979 Rec.ReferenceToConsteval.size()) { 17980 struct SimpleRemove : DynamicRecursiveASTVisitor { 17981 llvm::SmallPtrSetImpl<DeclRefExpr *> &DRSet; 17982 SimpleRemove(llvm::SmallPtrSetImpl<DeclRefExpr *> &S) : DRSet(S) {} 17983 bool VisitDeclRefExpr(DeclRefExpr *E) override { 17984 DRSet.erase(E); 17985 return DRSet.size(); 17986 } 17987 } Visitor(Rec.ReferenceToConsteval); 17988 Visitor.TraverseStmt( 17989 Rec.ImmediateInvocationCandidates.front().getPointer()->getSubExpr()); 17990 } 17991 for (auto CE : Rec.ImmediateInvocationCandidates) 17992 if (!CE.getInt()) 17993 EvaluateAndDiagnoseImmediateInvocation(SemaRef, CE); 17994 for (auto *DR : Rec.ReferenceToConsteval) { 17995 // If the expression is immediate escalating, it is not an error; 17996 // The outer context itself becomes immediate and further errors, 17997 // if any, will be handled by DiagnoseImmediateEscalatingReason. 17998 if (DR->isImmediateEscalating()) 17999 continue; 18000 auto *FD = cast<FunctionDecl>(DR->getDecl()); 18001 const NamedDecl *ND = FD; 18002 if (const auto *MD = dyn_cast<CXXMethodDecl>(ND); 18003 MD && (MD->isLambdaStaticInvoker() || isLambdaCallOperator(MD))) 18004 ND = MD->getParent(); 18005 18006 // C++23 [expr.const]/p16 18007 // An expression or conversion is immediate-escalating if it is not 18008 // initially in an immediate function context and it is [...] a 18009 // potentially-evaluated id-expression that denotes an immediate function 18010 // that is not a subexpression of an immediate invocation. 18011 bool ImmediateEscalating = false; 18012 bool IsPotentiallyEvaluated = 18013 Rec.Context == 18014 Sema::ExpressionEvaluationContext::PotentiallyEvaluated || 18015 Rec.Context == 18016 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed; 18017 if (SemaRef.inTemplateInstantiation() && IsPotentiallyEvaluated) 18018 ImmediateEscalating = Rec.InImmediateEscalatingFunctionContext; 18019 18020 if (!Rec.InImmediateEscalatingFunctionContext || 18021 (SemaRef.inTemplateInstantiation() && !ImmediateEscalating)) { 18022 SemaRef.Diag(DR->getBeginLoc(), diag::err_invalid_consteval_take_address) 18023 << ND << isa<CXXRecordDecl>(ND) << FD->isConsteval(); 18024 if (!FD->getBuiltinID()) 18025 SemaRef.Diag(ND->getLocation(), diag::note_declared_at); 18026 if (auto Context = 18027 SemaRef.InnermostDeclarationWithDelayedImmediateInvocations()) { 18028 SemaRef.Diag(Context->Loc, diag::note_invalid_consteval_initializer) 18029 << Context->Decl; 18030 SemaRef.Diag(Context->Decl->getBeginLoc(), diag::note_declared_at); 18031 } 18032 if (FD->isImmediateEscalating() && !FD->isConsteval()) 18033 SemaRef.DiagnoseImmediateEscalatingReason(FD); 18034 18035 } else { 18036 SemaRef.MarkExpressionAsImmediateEscalating(DR); 18037 } 18038 } 18039 } 18040 18041 void Sema::PopExpressionEvaluationContext() { 18042 ExpressionEvaluationContextRecord& Rec = ExprEvalContexts.back(); 18043 if (!Rec.Lambdas.empty()) { 18044 using ExpressionKind = ExpressionEvaluationContextRecord::ExpressionKind; 18045 if (!getLangOpts().CPlusPlus20 && 18046 (Rec.ExprContext == ExpressionKind::EK_TemplateArgument || 18047 Rec.isUnevaluated() || 18048 (Rec.isConstantEvaluated() && !getLangOpts().CPlusPlus17))) { 18049 unsigned D; 18050 if (Rec.isUnevaluated()) { 18051 // C++11 [expr.prim.lambda]p2: 18052 // A lambda-expression shall not appear in an unevaluated operand 18053 // (Clause 5). 18054 D = diag::err_lambda_unevaluated_operand; 18055 } else if (Rec.isConstantEvaluated() && !getLangOpts().CPlusPlus17) { 18056 // C++1y [expr.const]p2: 18057 // A conditional-expression e is a core constant expression unless the 18058 // evaluation of e, following the rules of the abstract machine, would 18059 // evaluate [...] a lambda-expression. 18060 D = diag::err_lambda_in_constant_expression; 18061 } else if (Rec.ExprContext == ExpressionKind::EK_TemplateArgument) { 18062 // C++17 [expr.prim.lamda]p2: 18063 // A lambda-expression shall not appear [...] in a template-argument. 18064 D = diag::err_lambda_in_invalid_context; 18065 } else 18066 llvm_unreachable("Couldn't infer lambda error message."); 18067 18068 for (const auto *L : Rec.Lambdas) 18069 Diag(L->getBeginLoc(), D); 18070 } 18071 } 18072 18073 // Append the collected materialized temporaries into previous context before 18074 // exit if the previous also is a lifetime extending context. 18075 if (getLangOpts().CPlusPlus23 && Rec.InLifetimeExtendingContext && 18076 parentEvaluationContext().InLifetimeExtendingContext && 18077 !Rec.ForRangeLifetimeExtendTemps.empty()) { 18078 parentEvaluationContext().ForRangeLifetimeExtendTemps.append( 18079 Rec.ForRangeLifetimeExtendTemps); 18080 } 18081 18082 WarnOnPendingNoDerefs(Rec); 18083 HandleImmediateInvocations(*this, Rec); 18084 18085 // Warn on any volatile-qualified simple-assignments that are not discarded- 18086 // value expressions nor unevaluated operands (those cases get removed from 18087 // this list by CheckUnusedVolatileAssignment). 18088 for (auto *BO : Rec.VolatileAssignmentLHSs) 18089 Diag(BO->getBeginLoc(), diag::warn_deprecated_simple_assign_volatile) 18090 << BO->getType(); 18091 18092 // When are coming out of an unevaluated context, clear out any 18093 // temporaries that we may have created as part of the evaluation of 18094 // the expression in that context: they aren't relevant because they 18095 // will never be constructed. 18096 if (Rec.isUnevaluated() || Rec.isConstantEvaluated()) { 18097 ExprCleanupObjects.erase(ExprCleanupObjects.begin() + Rec.NumCleanupObjects, 18098 ExprCleanupObjects.end()); 18099 Cleanup = Rec.ParentCleanup; 18100 CleanupVarDeclMarking(); 18101 std::swap(MaybeODRUseExprs, Rec.SavedMaybeODRUseExprs); 18102 // Otherwise, merge the contexts together. 18103 } else { 18104 Cleanup.mergeFrom(Rec.ParentCleanup); 18105 MaybeODRUseExprs.insert_range(Rec.SavedMaybeODRUseExprs); 18106 } 18107 18108 // Pop the current expression evaluation context off the stack. 18109 ExprEvalContexts.pop_back(); 18110 } 18111 18112 void Sema::DiscardCleanupsInEvaluationContext() { 18113 ExprCleanupObjects.erase( 18114 ExprCleanupObjects.begin() + ExprEvalContexts.back().NumCleanupObjects, 18115 ExprCleanupObjects.end()); 18116 Cleanup.reset(); 18117 MaybeODRUseExprs.clear(); 18118 } 18119 18120 ExprResult Sema::HandleExprEvaluationContextForTypeof(Expr *E) { 18121 ExprResult Result = CheckPlaceholderExpr(E); 18122 if (Result.isInvalid()) 18123 return ExprError(); 18124 E = Result.get(); 18125 if (!E->getType()->isVariablyModifiedType()) 18126 return E; 18127 return TransformToPotentiallyEvaluated(E); 18128 } 18129 18130 /// Are we in a context that is potentially constant evaluated per C++20 18131 /// [expr.const]p12? 18132 static bool isPotentiallyConstantEvaluatedContext(Sema &SemaRef) { 18133 /// C++2a [expr.const]p12: 18134 // An expression or conversion is potentially constant evaluated if it is 18135 switch (SemaRef.ExprEvalContexts.back().Context) { 18136 case Sema::ExpressionEvaluationContext::ConstantEvaluated: 18137 case Sema::ExpressionEvaluationContext::ImmediateFunctionContext: 18138 18139 // -- a manifestly constant-evaluated expression, 18140 case Sema::ExpressionEvaluationContext::PotentiallyEvaluated: 18141 case Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed: 18142 case Sema::ExpressionEvaluationContext::DiscardedStatement: 18143 // -- a potentially-evaluated expression, 18144 case Sema::ExpressionEvaluationContext::UnevaluatedList: 18145 // -- an immediate subexpression of a braced-init-list, 18146 18147 // -- [FIXME] an expression of the form & cast-expression that occurs 18148 // within a templated entity 18149 // -- a subexpression of one of the above that is not a subexpression of 18150 // a nested unevaluated operand. 18151 return true; 18152 18153 case Sema::ExpressionEvaluationContext::Unevaluated: 18154 case Sema::ExpressionEvaluationContext::UnevaluatedAbstract: 18155 // Expressions in this context are never evaluated. 18156 return false; 18157 } 18158 llvm_unreachable("Invalid context"); 18159 } 18160 18161 /// Return true if this function has a calling convention that requires mangling 18162 /// in the size of the parameter pack. 18163 static bool funcHasParameterSizeMangling(Sema &S, FunctionDecl *FD) { 18164 // These manglings are only applicable for targets whcih use Microsoft 18165 // mangling scheme for C. 18166 if (!S.Context.getTargetInfo().shouldUseMicrosoftCCforMangling()) 18167 return false; 18168 18169 // If this is C++ and this isn't an extern "C" function, parameters do not 18170 // need to be complete. In this case, C++ mangling will apply, which doesn't 18171 // use the size of the parameters. 18172 if (S.getLangOpts().CPlusPlus && !FD->isExternC()) 18173 return false; 18174 18175 // Stdcall, fastcall, and vectorcall need this special treatment. 18176 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv(); 18177 switch (CC) { 18178 case CC_X86StdCall: 18179 case CC_X86FastCall: 18180 case CC_X86VectorCall: 18181 return true; 18182 default: 18183 break; 18184 } 18185 return false; 18186 } 18187 18188 /// Require that all of the parameter types of function be complete. Normally, 18189 /// parameter types are only required to be complete when a function is called 18190 /// or defined, but to mangle functions with certain calling conventions, the 18191 /// mangler needs to know the size of the parameter list. In this situation, 18192 /// MSVC doesn't emit an error or instantiate templates. Instead, MSVC mangles 18193 /// the function as _foo@0, i.e. zero bytes of parameters, which will usually 18194 /// result in a linker error. Clang doesn't implement this behavior, and instead 18195 /// attempts to error at compile time. 18196 static void CheckCompleteParameterTypesForMangler(Sema &S, FunctionDecl *FD, 18197 SourceLocation Loc) { 18198 class ParamIncompleteTypeDiagnoser : public Sema::TypeDiagnoser { 18199 FunctionDecl *FD; 18200 ParmVarDecl *Param; 18201 18202 public: 18203 ParamIncompleteTypeDiagnoser(FunctionDecl *FD, ParmVarDecl *Param) 18204 : FD(FD), Param(Param) {} 18205 18206 void diagnose(Sema &S, SourceLocation Loc, QualType T) override { 18207 CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv(); 18208 StringRef CCName; 18209 switch (CC) { 18210 case CC_X86StdCall: 18211 CCName = "stdcall"; 18212 break; 18213 case CC_X86FastCall: 18214 CCName = "fastcall"; 18215 break; 18216 case CC_X86VectorCall: 18217 CCName = "vectorcall"; 18218 break; 18219 default: 18220 llvm_unreachable("CC does not need mangling"); 18221 } 18222 18223 S.Diag(Loc, diag::err_cconv_incomplete_param_type) 18224 << Param->getDeclName() << FD->getDeclName() << CCName; 18225 } 18226 }; 18227 18228 for (ParmVarDecl *Param : FD->parameters()) { 18229 ParamIncompleteTypeDiagnoser Diagnoser(FD, Param); 18230 S.RequireCompleteType(Loc, Param->getType(), Diagnoser); 18231 } 18232 } 18233 18234 namespace { 18235 enum class OdrUseContext { 18236 /// Declarations in this context are not odr-used. 18237 None, 18238 /// Declarations in this context are formally odr-used, but this is a 18239 /// dependent context. 18240 Dependent, 18241 /// Declarations in this context are odr-used but not actually used (yet). 18242 FormallyOdrUsed, 18243 /// Declarations in this context are used. 18244 Used 18245 }; 18246 } 18247 18248 /// Are we within a context in which references to resolved functions or to 18249 /// variables result in odr-use? 18250 static OdrUseContext isOdrUseContext(Sema &SemaRef) { 18251 const Sema::ExpressionEvaluationContextRecord &Context = 18252 SemaRef.currentEvaluationContext(); 18253 18254 if (Context.isUnevaluated()) 18255 return OdrUseContext::None; 18256 18257 if (SemaRef.CurContext->isDependentContext()) 18258 return OdrUseContext::Dependent; 18259 18260 if (Context.isDiscardedStatementContext()) 18261 return OdrUseContext::FormallyOdrUsed; 18262 18263 else if (Context.Context == 18264 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed) 18265 return OdrUseContext::FormallyOdrUsed; 18266 18267 return OdrUseContext::Used; 18268 } 18269 18270 static bool isImplicitlyDefinableConstexprFunction(FunctionDecl *Func) { 18271 if (!Func->isConstexpr()) 18272 return false; 18273 18274 if (Func->isImplicitlyInstantiable() || !Func->isUserProvided()) 18275 return true; 18276 18277 // Lambda conversion operators are never user provided. 18278 if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(Func)) 18279 return isLambdaConversionOperator(Conv); 18280 18281 auto *CCD = dyn_cast<CXXConstructorDecl>(Func); 18282 return CCD && CCD->getInheritedConstructor(); 18283 } 18284 18285 void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func, 18286 bool MightBeOdrUse) { 18287 assert(Func && "No function?"); 18288 18289 Func->setReferenced(); 18290 18291 // Recursive functions aren't really used until they're used from some other 18292 // context. 18293 bool IsRecursiveCall = CurContext == Func; 18294 18295 // C++11 [basic.def.odr]p3: 18296 // A function whose name appears as a potentially-evaluated expression is 18297 // odr-used if it is the unique lookup result or the selected member of a 18298 // set of overloaded functions [...]. 18299 // 18300 // We (incorrectly) mark overload resolution as an unevaluated context, so we 18301 // can just check that here. 18302 OdrUseContext OdrUse = 18303 MightBeOdrUse ? isOdrUseContext(*this) : OdrUseContext::None; 18304 if (IsRecursiveCall && OdrUse == OdrUseContext::Used) 18305 OdrUse = OdrUseContext::FormallyOdrUsed; 18306 18307 // Trivial default constructors and destructors are never actually used. 18308 // FIXME: What about other special members? 18309 if (Func->isTrivial() && !Func->hasAttr<DLLExportAttr>() && 18310 OdrUse == OdrUseContext::Used) { 18311 if (auto *Constructor = dyn_cast<CXXConstructorDecl>(Func)) 18312 if (Constructor->isDefaultConstructor()) 18313 OdrUse = OdrUseContext::FormallyOdrUsed; 18314 if (isa<CXXDestructorDecl>(Func)) 18315 OdrUse = OdrUseContext::FormallyOdrUsed; 18316 } 18317 18318 // C++20 [expr.const]p12: 18319 // A function [...] is needed for constant evaluation if it is [...] a 18320 // constexpr function that is named by an expression that is potentially 18321 // constant evaluated 18322 bool NeededForConstantEvaluation = 18323 isPotentiallyConstantEvaluatedContext(*this) && 18324 isImplicitlyDefinableConstexprFunction(Func); 18325 18326 // Determine whether we require a function definition to exist, per 18327 // C++11 [temp.inst]p3: 18328 // Unless a function template specialization has been explicitly 18329 // instantiated or explicitly specialized, the function template 18330 // specialization is implicitly instantiated when the specialization is 18331 // referenced in a context that requires a function definition to exist. 18332 // C++20 [temp.inst]p7: 18333 // The existence of a definition of a [...] function is considered to 18334 // affect the semantics of the program if the [...] function is needed for 18335 // constant evaluation by an expression 18336 // C++20 [basic.def.odr]p10: 18337 // Every program shall contain exactly one definition of every non-inline 18338 // function or variable that is odr-used in that program outside of a 18339 // discarded statement 18340 // C++20 [special]p1: 18341 // The implementation will implicitly define [defaulted special members] 18342 // if they are odr-used or needed for constant evaluation. 18343 // 18344 // Note that we skip the implicit instantiation of templates that are only 18345 // used in unused default arguments or by recursive calls to themselves. 18346 // This is formally non-conforming, but seems reasonable in practice. 18347 bool NeedDefinition = 18348 !IsRecursiveCall && 18349 (OdrUse == OdrUseContext::Used || 18350 (NeededForConstantEvaluation && !Func->isPureVirtual())); 18351 18352 // C++14 [temp.expl.spec]p6: 18353 // If a template [...] is explicitly specialized then that specialization 18354 // shall be declared before the first use of that specialization that would 18355 // cause an implicit instantiation to take place, in every translation unit 18356 // in which such a use occurs 18357 if (NeedDefinition && 18358 (Func->getTemplateSpecializationKind() != TSK_Undeclared || 18359 Func->getMemberSpecializationInfo())) 18360 checkSpecializationReachability(Loc, Func); 18361 18362 if (getLangOpts().CUDA) 18363 CUDA().CheckCall(Loc, Func); 18364 18365 // If we need a definition, try to create one. 18366 if (NeedDefinition && !Func->getBody()) { 18367 runWithSufficientStackSpace(Loc, [&] { 18368 if (CXXConstructorDecl *Constructor = 18369 dyn_cast<CXXConstructorDecl>(Func)) { 18370 Constructor = cast<CXXConstructorDecl>(Constructor->getFirstDecl()); 18371 if (Constructor->isDefaulted() && !Constructor->isDeleted()) { 18372 if (Constructor->isDefaultConstructor()) { 18373 if (Constructor->isTrivial() && 18374 !Constructor->hasAttr<DLLExportAttr>()) 18375 return; 18376 DefineImplicitDefaultConstructor(Loc, Constructor); 18377 } else if (Constructor->isCopyConstructor()) { 18378 DefineImplicitCopyConstructor(Loc, Constructor); 18379 } else if (Constructor->isMoveConstructor()) { 18380 DefineImplicitMoveConstructor(Loc, Constructor); 18381 } 18382 } else if (Constructor->getInheritedConstructor()) { 18383 DefineInheritingConstructor(Loc, Constructor); 18384 } 18385 } else if (CXXDestructorDecl *Destructor = 18386 dyn_cast<CXXDestructorDecl>(Func)) { 18387 Destructor = cast<CXXDestructorDecl>(Destructor->getFirstDecl()); 18388 if (Destructor->isDefaulted() && !Destructor->isDeleted()) { 18389 if (Destructor->isTrivial() && !Destructor->hasAttr<DLLExportAttr>()) 18390 return; 18391 DefineImplicitDestructor(Loc, Destructor); 18392 } 18393 if (Destructor->isVirtual() && getLangOpts().AppleKext) 18394 MarkVTableUsed(Loc, Destructor->getParent()); 18395 } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(Func)) { 18396 if (MethodDecl->isOverloadedOperator() && 18397 MethodDecl->getOverloadedOperator() == OO_Equal) { 18398 MethodDecl = cast<CXXMethodDecl>(MethodDecl->getFirstDecl()); 18399 if (MethodDecl->isDefaulted() && !MethodDecl->isDeleted()) { 18400 if (MethodDecl->isCopyAssignmentOperator()) 18401 DefineImplicitCopyAssignment(Loc, MethodDecl); 18402 else if (MethodDecl->isMoveAssignmentOperator()) 18403 DefineImplicitMoveAssignment(Loc, MethodDecl); 18404 } 18405 } else if (isa<CXXConversionDecl>(MethodDecl) && 18406 MethodDecl->getParent()->isLambda()) { 18407 CXXConversionDecl *Conversion = 18408 cast<CXXConversionDecl>(MethodDecl->getFirstDecl()); 18409 if (Conversion->isLambdaToBlockPointerConversion()) 18410 DefineImplicitLambdaToBlockPointerConversion(Loc, Conversion); 18411 else 18412 DefineImplicitLambdaToFunctionPointerConversion(Loc, Conversion); 18413 } else if (MethodDecl->isVirtual() && getLangOpts().AppleKext) 18414 MarkVTableUsed(Loc, MethodDecl->getParent()); 18415 } 18416 18417 if (Func->isDefaulted() && !Func->isDeleted()) { 18418 DefaultedComparisonKind DCK = getDefaultedComparisonKind(Func); 18419 if (DCK != DefaultedComparisonKind::None) 18420 DefineDefaultedComparison(Loc, Func, DCK); 18421 } 18422 18423 // Implicit instantiation of function templates and member functions of 18424 // class templates. 18425 if (Func->isImplicitlyInstantiable()) { 18426 TemplateSpecializationKind TSK = 18427 Func->getTemplateSpecializationKindForInstantiation(); 18428 SourceLocation PointOfInstantiation = Func->getPointOfInstantiation(); 18429 bool FirstInstantiation = PointOfInstantiation.isInvalid(); 18430 if (FirstInstantiation) { 18431 PointOfInstantiation = Loc; 18432 if (auto *MSI = Func->getMemberSpecializationInfo()) 18433 MSI->setPointOfInstantiation(Loc); 18434 // FIXME: Notify listener. 18435 else 18436 Func->setTemplateSpecializationKind(TSK, PointOfInstantiation); 18437 } else if (TSK != TSK_ImplicitInstantiation) { 18438 // Use the point of use as the point of instantiation, instead of the 18439 // point of explicit instantiation (which we track as the actual point 18440 // of instantiation). This gives better backtraces in diagnostics. 18441 PointOfInstantiation = Loc; 18442 } 18443 18444 if (FirstInstantiation || TSK != TSK_ImplicitInstantiation || 18445 Func->isConstexpr()) { 18446 if (isa<CXXRecordDecl>(Func->getDeclContext()) && 18447 cast<CXXRecordDecl>(Func->getDeclContext())->isLocalClass() && 18448 CodeSynthesisContexts.size()) 18449 PendingLocalImplicitInstantiations.push_back( 18450 std::make_pair(Func, PointOfInstantiation)); 18451 else if (Func->isConstexpr()) 18452 // Do not defer instantiations of constexpr functions, to avoid the 18453 // expression evaluator needing to call back into Sema if it sees a 18454 // call to such a function. 18455 InstantiateFunctionDefinition(PointOfInstantiation, Func); 18456 else { 18457 Func->setInstantiationIsPending(true); 18458 PendingInstantiations.push_back( 18459 std::make_pair(Func, PointOfInstantiation)); 18460 if (llvm::isTimeTraceVerbose()) { 18461 llvm::timeTraceAddInstantEvent("DeferInstantiation", [&] { 18462 std::string Name; 18463 llvm::raw_string_ostream OS(Name); 18464 Func->getNameForDiagnostic(OS, getPrintingPolicy(), 18465 /*Qualified=*/true); 18466 return Name; 18467 }); 18468 } 18469 // Notify the consumer that a function was implicitly instantiated. 18470 Consumer.HandleCXXImplicitFunctionInstantiation(Func); 18471 } 18472 } 18473 } else { 18474 // Walk redefinitions, as some of them may be instantiable. 18475 for (auto *i : Func->redecls()) { 18476 if (!i->isUsed(false) && i->isImplicitlyInstantiable()) 18477 MarkFunctionReferenced(Loc, i, MightBeOdrUse); 18478 } 18479 } 18480 }); 18481 } 18482 18483 // If a constructor was defined in the context of a default parameter 18484 // or of another default member initializer (ie a PotentiallyEvaluatedIfUsed 18485 // context), its initializers may not be referenced yet. 18486 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Func)) { 18487 EnterExpressionEvaluationContext EvalContext( 18488 *this, 18489 Constructor->isImmediateFunction() 18490 ? ExpressionEvaluationContext::ImmediateFunctionContext 18491 : ExpressionEvaluationContext::PotentiallyEvaluated, 18492 Constructor); 18493 for (CXXCtorInitializer *Init : Constructor->inits()) { 18494 if (Init->isInClassMemberInitializer()) 18495 runWithSufficientStackSpace(Init->getSourceLocation(), [&]() { 18496 MarkDeclarationsReferencedInExpr(Init->getInit()); 18497 }); 18498 } 18499 } 18500 18501 // C++14 [except.spec]p17: 18502 // An exception-specification is considered to be needed when: 18503 // - the function is odr-used or, if it appears in an unevaluated operand, 18504 // would be odr-used if the expression were potentially-evaluated; 18505 // 18506 // Note, we do this even if MightBeOdrUse is false. That indicates that the 18507 // function is a pure virtual function we're calling, and in that case the 18508 // function was selected by overload resolution and we need to resolve its 18509 // exception specification for a different reason. 18510 const FunctionProtoType *FPT = Func->getType()->getAs<FunctionProtoType>(); 18511 if (FPT && isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) 18512 ResolveExceptionSpec(Loc, FPT); 18513 18514 // A callee could be called by a host function then by a device function. 18515 // If we only try recording once, we will miss recording the use on device 18516 // side. Therefore keep trying until it is recorded. 18517 if (LangOpts.OffloadImplicitHostDeviceTemplates && LangOpts.CUDAIsDevice && 18518 !getASTContext().CUDAImplicitHostDeviceFunUsedByDevice.count(Func)) 18519 CUDA().RecordImplicitHostDeviceFuncUsedByDevice(Func); 18520 18521 // If this is the first "real" use, act on that. 18522 if (OdrUse == OdrUseContext::Used && !Func->isUsed(/*CheckUsedAttr=*/false)) { 18523 // Keep track of used but undefined functions. 18524 if (!Func->isDefined() && !Func->isInAnotherModuleUnit()) { 18525 if (mightHaveNonExternalLinkage(Func)) 18526 UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc)); 18527 else if (Func->getMostRecentDecl()->isInlined() && 18528 !LangOpts.GNUInline && 18529 !Func->getMostRecentDecl()->hasAttr<GNUInlineAttr>()) 18530 UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc)); 18531 else if (isExternalWithNoLinkageType(Func)) 18532 UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc)); 18533 } 18534 18535 // Some x86 Windows calling conventions mangle the size of the parameter 18536 // pack into the name. Computing the size of the parameters requires the 18537 // parameter types to be complete. Check that now. 18538 if (funcHasParameterSizeMangling(*this, Func)) 18539 CheckCompleteParameterTypesForMangler(*this, Func, Loc); 18540 18541 // In the MS C++ ABI, the compiler emits destructor variants where they are 18542 // used. If the destructor is used here but defined elsewhere, mark the 18543 // virtual base destructors referenced. If those virtual base destructors 18544 // are inline, this will ensure they are defined when emitting the complete 18545 // destructor variant. This checking may be redundant if the destructor is 18546 // provided later in this TU. 18547 if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { 18548 if (auto *Dtor = dyn_cast<CXXDestructorDecl>(Func)) { 18549 CXXRecordDecl *Parent = Dtor->getParent(); 18550 if (Parent->getNumVBases() > 0 && !Dtor->getBody()) 18551 CheckCompleteDestructorVariant(Loc, Dtor); 18552 } 18553 } 18554 18555 Func->markUsed(Context); 18556 } 18557 } 18558 18559 /// Directly mark a variable odr-used. Given a choice, prefer to use 18560 /// MarkVariableReferenced since it does additional checks and then 18561 /// calls MarkVarDeclODRUsed. 18562 /// If the variable must be captured: 18563 /// - if FunctionScopeIndexToStopAt is null, capture it in the CurContext 18564 /// - else capture it in the DeclContext that maps to the 18565 /// *FunctionScopeIndexToStopAt on the FunctionScopeInfo stack. 18566 static void 18567 MarkVarDeclODRUsed(ValueDecl *V, SourceLocation Loc, Sema &SemaRef, 18568 const unsigned *const FunctionScopeIndexToStopAt = nullptr) { 18569 // Keep track of used but undefined variables. 18570 // FIXME: We shouldn't suppress this warning for static data members. 18571 VarDecl *Var = V->getPotentiallyDecomposedVarDecl(); 18572 assert(Var && "expected a capturable variable"); 18573 18574 if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly && 18575 (!Var->isExternallyVisible() || Var->isInline() || 18576 SemaRef.isExternalWithNoLinkageType(Var)) && 18577 !(Var->isStaticDataMember() && Var->hasInit())) { 18578 SourceLocation &old = SemaRef.UndefinedButUsed[Var->getCanonicalDecl()]; 18579 if (old.isInvalid()) 18580 old = Loc; 18581 } 18582 QualType CaptureType, DeclRefType; 18583 if (SemaRef.LangOpts.OpenMP) 18584 SemaRef.OpenMP().tryCaptureOpenMPLambdas(V); 18585 SemaRef.tryCaptureVariable(V, Loc, TryCaptureKind::Implicit, 18586 /*EllipsisLoc*/ SourceLocation(), 18587 /*BuildAndDiagnose*/ true, CaptureType, 18588 DeclRefType, FunctionScopeIndexToStopAt); 18589 18590 if (SemaRef.LangOpts.CUDA && Var->hasGlobalStorage()) { 18591 auto *FD = dyn_cast_or_null<FunctionDecl>(SemaRef.CurContext); 18592 auto VarTarget = SemaRef.CUDA().IdentifyTarget(Var); 18593 auto UserTarget = SemaRef.CUDA().IdentifyTarget(FD); 18594 if (VarTarget == SemaCUDA::CVT_Host && 18595 (UserTarget == CUDAFunctionTarget::Device || 18596 UserTarget == CUDAFunctionTarget::HostDevice || 18597 UserTarget == CUDAFunctionTarget::Global)) { 18598 // Diagnose ODR-use of host global variables in device functions. 18599 // Reference of device global variables in host functions is allowed 18600 // through shadow variables therefore it is not diagnosed. 18601 if (SemaRef.LangOpts.CUDAIsDevice && !SemaRef.LangOpts.HIPStdPar) { 18602 SemaRef.targetDiag(Loc, diag::err_ref_bad_target) 18603 << /*host*/ 2 << /*variable*/ 1 << Var << UserTarget; 18604 SemaRef.targetDiag(Var->getLocation(), 18605 Var->getType().isConstQualified() 18606 ? diag::note_cuda_const_var_unpromoted 18607 : diag::note_cuda_host_var); 18608 } 18609 } else if (VarTarget == SemaCUDA::CVT_Device && 18610 !Var->hasAttr<CUDASharedAttr>() && 18611 (UserTarget == CUDAFunctionTarget::Host || 18612 UserTarget == CUDAFunctionTarget::HostDevice)) { 18613 // Record a CUDA/HIP device side variable if it is ODR-used 18614 // by host code. This is done conservatively, when the variable is 18615 // referenced in any of the following contexts: 18616 // - a non-function context 18617 // - a host function 18618 // - a host device function 18619 // This makes the ODR-use of the device side variable by host code to 18620 // be visible in the device compilation for the compiler to be able to 18621 // emit template variables instantiated by host code only and to 18622 // externalize the static device side variable ODR-used by host code. 18623 if (!Var->hasExternalStorage()) 18624 SemaRef.getASTContext().CUDADeviceVarODRUsedByHost.insert(Var); 18625 else if (SemaRef.LangOpts.GPURelocatableDeviceCode && 18626 (!FD || (!FD->getDescribedFunctionTemplate() && 18627 SemaRef.getASTContext().GetGVALinkageForFunction(FD) == 18628 GVA_StrongExternal))) 18629 SemaRef.getASTContext().CUDAExternalDeviceDeclODRUsedByHost.insert(Var); 18630 } 18631 } 18632 18633 V->markUsed(SemaRef.Context); 18634 } 18635 18636 void Sema::MarkCaptureUsedInEnclosingContext(ValueDecl *Capture, 18637 SourceLocation Loc, 18638 unsigned CapturingScopeIndex) { 18639 MarkVarDeclODRUsed(Capture, Loc, *this, &CapturingScopeIndex); 18640 } 18641 18642 void diagnoseUncapturableValueReferenceOrBinding(Sema &S, SourceLocation loc, 18643 ValueDecl *var) { 18644 DeclContext *VarDC = var->getDeclContext(); 18645 18646 // If the parameter still belongs to the translation unit, then 18647 // we're actually just using one parameter in the declaration of 18648 // the next. 18649 if (isa<ParmVarDecl>(var) && 18650 isa<TranslationUnitDecl>(VarDC)) 18651 return; 18652 18653 // For C code, don't diagnose about capture if we're not actually in code 18654 // right now; it's impossible to write a non-constant expression outside of 18655 // function context, so we'll get other (more useful) diagnostics later. 18656 // 18657 // For C++, things get a bit more nasty... it would be nice to suppress this 18658 // diagnostic for certain cases like using a local variable in an array bound 18659 // for a member of a local class, but the correct predicate is not obvious. 18660 if (!S.getLangOpts().CPlusPlus && !S.CurContext->isFunctionOrMethod()) 18661 return; 18662 18663 unsigned ValueKind = isa<BindingDecl>(var) ? 1 : 0; 18664 unsigned ContextKind = 3; // unknown 18665 if (isa<CXXMethodDecl>(VarDC) && 18666 cast<CXXRecordDecl>(VarDC->getParent())->isLambda()) { 18667 ContextKind = 2; 18668 } else if (isa<FunctionDecl>(VarDC)) { 18669 ContextKind = 0; 18670 } else if (isa<BlockDecl>(VarDC)) { 18671 ContextKind = 1; 18672 } 18673 18674 S.Diag(loc, diag::err_reference_to_local_in_enclosing_context) 18675 << var << ValueKind << ContextKind << VarDC; 18676 S.Diag(var->getLocation(), diag::note_entity_declared_at) 18677 << var; 18678 18679 // FIXME: Add additional diagnostic info about class etc. which prevents 18680 // capture. 18681 } 18682 18683 static bool isVariableAlreadyCapturedInScopeInfo(CapturingScopeInfo *CSI, 18684 ValueDecl *Var, 18685 bool &SubCapturesAreNested, 18686 QualType &CaptureType, 18687 QualType &DeclRefType) { 18688 // Check whether we've already captured it. 18689 if (CSI->CaptureMap.count(Var)) { 18690 // If we found a capture, any subcaptures are nested. 18691 SubCapturesAreNested = true; 18692 18693 // Retrieve the capture type for this variable. 18694 CaptureType = CSI->getCapture(Var).getCaptureType(); 18695 18696 // Compute the type of an expression that refers to this variable. 18697 DeclRefType = CaptureType.getNonReferenceType(); 18698 18699 // Similarly to mutable captures in lambda, all the OpenMP captures by copy 18700 // are mutable in the sense that user can change their value - they are 18701 // private instances of the captured declarations. 18702 const Capture &Cap = CSI->getCapture(Var); 18703 // C++ [expr.prim.lambda]p10: 18704 // The type of such a data member is [...] an lvalue reference to the 18705 // referenced function type if the entity is a reference to a function. 18706 // [...] 18707 if (Cap.isCopyCapture() && !DeclRefType->isFunctionType() && 18708 !(isa<LambdaScopeInfo>(CSI) && 18709 !cast<LambdaScopeInfo>(CSI)->lambdaCaptureShouldBeConst()) && 18710 !(isa<CapturedRegionScopeInfo>(CSI) && 18711 cast<CapturedRegionScopeInfo>(CSI)->CapRegionKind == CR_OpenMP)) 18712 DeclRefType.addConst(); 18713 return true; 18714 } 18715 return false; 18716 } 18717 18718 // Only block literals, captured statements, and lambda expressions can 18719 // capture; other scopes don't work. 18720 static DeclContext *getParentOfCapturingContextOrNull(DeclContext *DC, 18721 ValueDecl *Var, 18722 SourceLocation Loc, 18723 const bool Diagnose, 18724 Sema &S) { 18725 if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC) || isLambdaCallOperator(DC)) 18726 return getLambdaAwareParentOfDeclContext(DC); 18727 18728 VarDecl *Underlying = Var->getPotentiallyDecomposedVarDecl(); 18729 if (Underlying) { 18730 if (Underlying->hasLocalStorage() && Diagnose) 18731 diagnoseUncapturableValueReferenceOrBinding(S, Loc, Var); 18732 } 18733 return nullptr; 18734 } 18735 18736 // Certain capturing entities (lambdas, blocks etc.) are not allowed to capture 18737 // certain types of variables (unnamed, variably modified types etc.) 18738 // so check for eligibility. 18739 static bool isVariableCapturable(CapturingScopeInfo *CSI, ValueDecl *Var, 18740 SourceLocation Loc, const bool Diagnose, 18741 Sema &S) { 18742 18743 assert((isa<VarDecl, BindingDecl>(Var)) && 18744 "Only variables and structured bindings can be captured"); 18745 18746 bool IsBlock = isa<BlockScopeInfo>(CSI); 18747 bool IsLambda = isa<LambdaScopeInfo>(CSI); 18748 18749 // Lambdas are not allowed to capture unnamed variables 18750 // (e.g. anonymous unions). 18751 // FIXME: The C++11 rule don't actually state this explicitly, but I'm 18752 // assuming that's the intent. 18753 if (IsLambda && !Var->getDeclName()) { 18754 if (Diagnose) { 18755 S.Diag(Loc, diag::err_lambda_capture_anonymous_var); 18756 S.Diag(Var->getLocation(), diag::note_declared_at); 18757 } 18758 return false; 18759 } 18760 18761 // Prohibit variably-modified types in blocks; they're difficult to deal with. 18762 if (Var->getType()->isVariablyModifiedType() && IsBlock) { 18763 if (Diagnose) { 18764 S.Diag(Loc, diag::err_ref_vm_type); 18765 S.Diag(Var->getLocation(), diag::note_previous_decl) << Var; 18766 } 18767 return false; 18768 } 18769 // Prohibit structs with flexible array members too. 18770 // We cannot capture what is in the tail end of the struct. 18771 if (const RecordType *VTTy = Var->getType()->getAs<RecordType>()) { 18772 if (VTTy->getDecl()->hasFlexibleArrayMember()) { 18773 if (Diagnose) { 18774 if (IsBlock) 18775 S.Diag(Loc, diag::err_ref_flexarray_type); 18776 else 18777 S.Diag(Loc, diag::err_lambda_capture_flexarray_type) << Var; 18778 S.Diag(Var->getLocation(), diag::note_previous_decl) << Var; 18779 } 18780 return false; 18781 } 18782 } 18783 const bool HasBlocksAttr = Var->hasAttr<BlocksAttr>(); 18784 // Lambdas and captured statements are not allowed to capture __block 18785 // variables; they don't support the expected semantics. 18786 if (HasBlocksAttr && (IsLambda || isa<CapturedRegionScopeInfo>(CSI))) { 18787 if (Diagnose) { 18788 S.Diag(Loc, diag::err_capture_block_variable) << Var << !IsLambda; 18789 S.Diag(Var->getLocation(), diag::note_previous_decl) << Var; 18790 } 18791 return false; 18792 } 18793 // OpenCL v2.0 s6.12.5: Blocks cannot reference/capture other blocks 18794 if (S.getLangOpts().OpenCL && IsBlock && 18795 Var->getType()->isBlockPointerType()) { 18796 if (Diagnose) 18797 S.Diag(Loc, diag::err_opencl_block_ref_block); 18798 return false; 18799 } 18800 18801 if (isa<BindingDecl>(Var)) { 18802 if (!IsLambda || !S.getLangOpts().CPlusPlus) { 18803 if (Diagnose) 18804 diagnoseUncapturableValueReferenceOrBinding(S, Loc, Var); 18805 return false; 18806 } else if (Diagnose && S.getLangOpts().CPlusPlus) { 18807 S.Diag(Loc, S.LangOpts.CPlusPlus20 18808 ? diag::warn_cxx17_compat_capture_binding 18809 : diag::ext_capture_binding) 18810 << Var; 18811 S.Diag(Var->getLocation(), diag::note_entity_declared_at) << Var; 18812 } 18813 } 18814 18815 return true; 18816 } 18817 18818 // Returns true if the capture by block was successful. 18819 static bool captureInBlock(BlockScopeInfo *BSI, ValueDecl *Var, 18820 SourceLocation Loc, const bool BuildAndDiagnose, 18821 QualType &CaptureType, QualType &DeclRefType, 18822 const bool Nested, Sema &S, bool Invalid) { 18823 bool ByRef = false; 18824 18825 // Blocks are not allowed to capture arrays, excepting OpenCL. 18826 // OpenCL v2.0 s1.12.5 (revision 40): arrays are captured by reference 18827 // (decayed to pointers). 18828 if (!Invalid && !S.getLangOpts().OpenCL && CaptureType->isArrayType()) { 18829 if (BuildAndDiagnose) { 18830 S.Diag(Loc, diag::err_ref_array_type); 18831 S.Diag(Var->getLocation(), diag::note_previous_decl) << Var; 18832 Invalid = true; 18833 } else { 18834 return false; 18835 } 18836 } 18837 18838 // Forbid the block-capture of autoreleasing variables. 18839 if (!Invalid && 18840 CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { 18841 if (BuildAndDiagnose) { 18842 S.Diag(Loc, diag::err_arc_autoreleasing_capture) 18843 << /*block*/ 0; 18844 S.Diag(Var->getLocation(), diag::note_previous_decl) << Var; 18845 Invalid = true; 18846 } else { 18847 return false; 18848 } 18849 } 18850 18851 // Warn about implicitly autoreleasing indirect parameters captured by blocks. 18852 if (const auto *PT = CaptureType->getAs<PointerType>()) { 18853 QualType PointeeTy = PT->getPointeeType(); 18854 18855 if (!Invalid && PointeeTy->getAs<ObjCObjectPointerType>() && 18856 PointeeTy.getObjCLifetime() == Qualifiers::OCL_Autoreleasing && 18857 !S.Context.hasDirectOwnershipQualifier(PointeeTy)) { 18858 if (BuildAndDiagnose) { 18859 SourceLocation VarLoc = Var->getLocation(); 18860 S.Diag(Loc, diag::warn_block_capture_autoreleasing); 18861 S.Diag(VarLoc, diag::note_declare_parameter_strong); 18862 } 18863 } 18864 } 18865 18866 const bool HasBlocksAttr = Var->hasAttr<BlocksAttr>(); 18867 if (HasBlocksAttr || CaptureType->isReferenceType() || 18868 (S.getLangOpts().OpenMP && S.OpenMP().isOpenMPCapturedDecl(Var))) { 18869 // Block capture by reference does not change the capture or 18870 // declaration reference types. 18871 ByRef = true; 18872 } else { 18873 // Block capture by copy introduces 'const'. 18874 CaptureType = CaptureType.getNonReferenceType().withConst(); 18875 DeclRefType = CaptureType; 18876 } 18877 18878 // Actually capture the variable. 18879 if (BuildAndDiagnose) 18880 BSI->addCapture(Var, HasBlocksAttr, ByRef, Nested, Loc, SourceLocation(), 18881 CaptureType, Invalid); 18882 18883 return !Invalid; 18884 } 18885 18886 /// Capture the given variable in the captured region. 18887 static bool captureInCapturedRegion( 18888 CapturedRegionScopeInfo *RSI, ValueDecl *Var, SourceLocation Loc, 18889 const bool BuildAndDiagnose, QualType &CaptureType, QualType &DeclRefType, 18890 const bool RefersToCapturedVariable, TryCaptureKind Kind, bool IsTopScope, 18891 Sema &S, bool Invalid) { 18892 // By default, capture variables by reference. 18893 bool ByRef = true; 18894 if (IsTopScope && Kind != TryCaptureKind::Implicit) { 18895 ByRef = (Kind == TryCaptureKind::ExplicitByRef); 18896 } else if (S.getLangOpts().OpenMP && RSI->CapRegionKind == CR_OpenMP) { 18897 // Using an LValue reference type is consistent with Lambdas (see below). 18898 if (S.OpenMP().isOpenMPCapturedDecl(Var)) { 18899 bool HasConst = DeclRefType.isConstQualified(); 18900 DeclRefType = DeclRefType.getUnqualifiedType(); 18901 // Don't lose diagnostics about assignments to const. 18902 if (HasConst) 18903 DeclRefType.addConst(); 18904 } 18905 // Do not capture firstprivates in tasks. 18906 if (S.OpenMP().isOpenMPPrivateDecl(Var, RSI->OpenMPLevel, 18907 RSI->OpenMPCaptureLevel) != OMPC_unknown) 18908 return true; 18909 ByRef = S.OpenMP().isOpenMPCapturedByRef(Var, RSI->OpenMPLevel, 18910 RSI->OpenMPCaptureLevel); 18911 } 18912 18913 if (ByRef) 18914 CaptureType = S.Context.getLValueReferenceType(DeclRefType); 18915 else 18916 CaptureType = DeclRefType; 18917 18918 // Actually capture the variable. 18919 if (BuildAndDiagnose) 18920 RSI->addCapture(Var, /*isBlock*/ false, ByRef, RefersToCapturedVariable, 18921 Loc, SourceLocation(), CaptureType, Invalid); 18922 18923 return !Invalid; 18924 } 18925 18926 /// Capture the given variable in the lambda. 18927 static bool captureInLambda(LambdaScopeInfo *LSI, ValueDecl *Var, 18928 SourceLocation Loc, const bool BuildAndDiagnose, 18929 QualType &CaptureType, QualType &DeclRefType, 18930 const bool RefersToCapturedVariable, 18931 const TryCaptureKind Kind, 18932 SourceLocation EllipsisLoc, const bool IsTopScope, 18933 Sema &S, bool Invalid) { 18934 // Determine whether we are capturing by reference or by value. 18935 bool ByRef = false; 18936 if (IsTopScope && Kind != TryCaptureKind::Implicit) { 18937 ByRef = (Kind == TryCaptureKind::ExplicitByRef); 18938 } else { 18939 ByRef = (LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByref); 18940 } 18941 18942 if (BuildAndDiagnose && S.Context.getTargetInfo().getTriple().isWasm() && 18943 CaptureType.getNonReferenceType().isWebAssemblyReferenceType()) { 18944 S.Diag(Loc, diag::err_wasm_ca_reference) << 0; 18945 Invalid = true; 18946 } 18947 18948 // Compute the type of the field that will capture this variable. 18949 if (ByRef) { 18950 // C++11 [expr.prim.lambda]p15: 18951 // An entity is captured by reference if it is implicitly or 18952 // explicitly captured but not captured by copy. It is 18953 // unspecified whether additional unnamed non-static data 18954 // members are declared in the closure type for entities 18955 // captured by reference. 18956 // 18957 // FIXME: It is not clear whether we want to build an lvalue reference 18958 // to the DeclRefType or to CaptureType.getNonReferenceType(). GCC appears 18959 // to do the former, while EDG does the latter. Core issue 1249 will 18960 // clarify, but for now we follow GCC because it's a more permissive and 18961 // easily defensible position. 18962 CaptureType = S.Context.getLValueReferenceType(DeclRefType); 18963 } else { 18964 // C++11 [expr.prim.lambda]p14: 18965 // For each entity captured by copy, an unnamed non-static 18966 // data member is declared in the closure type. The 18967 // declaration order of these members is unspecified. The type 18968 // of such a data member is the type of the corresponding 18969 // captured entity if the entity is not a reference to an 18970 // object, or the referenced type otherwise. [Note: If the 18971 // captured entity is a reference to a function, the 18972 // corresponding data member is also a reference to a 18973 // function. - end note ] 18974 if (const ReferenceType *RefType = CaptureType->getAs<ReferenceType>()){ 18975 if (!RefType->getPointeeType()->isFunctionType()) 18976 CaptureType = RefType->getPointeeType(); 18977 } 18978 18979 // Forbid the lambda copy-capture of autoreleasing variables. 18980 if (!Invalid && 18981 CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { 18982 if (BuildAndDiagnose) { 18983 S.Diag(Loc, diag::err_arc_autoreleasing_capture) << /*lambda*/ 1; 18984 S.Diag(Var->getLocation(), diag::note_previous_decl) 18985 << Var->getDeclName(); 18986 Invalid = true; 18987 } else { 18988 return false; 18989 } 18990 } 18991 18992 // Make sure that by-copy captures are of a complete and non-abstract type. 18993 if (!Invalid && BuildAndDiagnose) { 18994 if (!CaptureType->isDependentType() && 18995 S.RequireCompleteSizedType( 18996 Loc, CaptureType, 18997 diag::err_capture_of_incomplete_or_sizeless_type, 18998 Var->getDeclName())) 18999 Invalid = true; 19000 else if (S.RequireNonAbstractType(Loc, CaptureType, 19001 diag::err_capture_of_abstract_type)) 19002 Invalid = true; 19003 } 19004 } 19005 19006 // Compute the type of a reference to this captured variable. 19007 if (ByRef) 19008 DeclRefType = CaptureType.getNonReferenceType(); 19009 else { 19010 // C++ [expr.prim.lambda]p5: 19011 // The closure type for a lambda-expression has a public inline 19012 // function call operator [...]. This function call operator is 19013 // declared const (9.3.1) if and only if the lambda-expression's 19014 // parameter-declaration-clause is not followed by mutable. 19015 DeclRefType = CaptureType.getNonReferenceType(); 19016 bool Const = LSI->lambdaCaptureShouldBeConst(); 19017 // C++ [expr.prim.lambda]p10: 19018 // The type of such a data member is [...] an lvalue reference to the 19019 // referenced function type if the entity is a reference to a function. 19020 // [...] 19021 if (Const && !CaptureType->isReferenceType() && 19022 !DeclRefType->isFunctionType()) 19023 DeclRefType.addConst(); 19024 } 19025 19026 // Add the capture. 19027 if (BuildAndDiagnose) 19028 LSI->addCapture(Var, /*isBlock=*/false, ByRef, RefersToCapturedVariable, 19029 Loc, EllipsisLoc, CaptureType, Invalid); 19030 19031 return !Invalid; 19032 } 19033 19034 static bool canCaptureVariableByCopy(ValueDecl *Var, 19035 const ASTContext &Context) { 19036 // Offer a Copy fix even if the type is dependent. 19037 if (Var->getType()->isDependentType()) 19038 return true; 19039 QualType T = Var->getType().getNonReferenceType(); 19040 if (T.isTriviallyCopyableType(Context)) 19041 return true; 19042 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { 19043 19044 if (!(RD = RD->getDefinition())) 19045 return false; 19046 if (RD->hasSimpleCopyConstructor()) 19047 return true; 19048 if (RD->hasUserDeclaredCopyConstructor()) 19049 for (CXXConstructorDecl *Ctor : RD->ctors()) 19050 if (Ctor->isCopyConstructor()) 19051 return !Ctor->isDeleted(); 19052 } 19053 return false; 19054 } 19055 19056 /// Create up to 4 fix-its for explicit reference and value capture of \p Var or 19057 /// default capture. Fixes may be omitted if they aren't allowed by the 19058 /// standard, for example we can't emit a default copy capture fix-it if we 19059 /// already explicitly copy capture capture another variable. 19060 static void buildLambdaCaptureFixit(Sema &Sema, LambdaScopeInfo *LSI, 19061 ValueDecl *Var) { 19062 assert(LSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None); 19063 // Don't offer Capture by copy of default capture by copy fixes if Var is 19064 // known not to be copy constructible. 19065 bool ShouldOfferCopyFix = canCaptureVariableByCopy(Var, Sema.getASTContext()); 19066 19067 SmallString<32> FixBuffer; 19068 StringRef Separator = LSI->NumExplicitCaptures > 0 ? ", " : ""; 19069 if (Var->getDeclName().isIdentifier() && !Var->getName().empty()) { 19070 SourceLocation VarInsertLoc = LSI->IntroducerRange.getEnd(); 19071 if (ShouldOfferCopyFix) { 19072 // Offer fixes to insert an explicit capture for the variable. 19073 // [] -> [VarName] 19074 // [OtherCapture] -> [OtherCapture, VarName] 19075 FixBuffer.assign({Separator, Var->getName()}); 19076 Sema.Diag(VarInsertLoc, diag::note_lambda_variable_capture_fixit) 19077 << Var << /*value*/ 0 19078 << FixItHint::CreateInsertion(VarInsertLoc, FixBuffer); 19079 } 19080 // As above but capture by reference. 19081 FixBuffer.assign({Separator, "&", Var->getName()}); 19082 Sema.Diag(VarInsertLoc, diag::note_lambda_variable_capture_fixit) 19083 << Var << /*reference*/ 1 19084 << FixItHint::CreateInsertion(VarInsertLoc, FixBuffer); 19085 } 19086 19087 // Only try to offer default capture if there are no captures excluding this 19088 // and init captures. 19089 // [this]: OK. 19090 // [X = Y]: OK. 19091 // [&A, &B]: Don't offer. 19092 // [A, B]: Don't offer. 19093 if (llvm::any_of(LSI->Captures, [](Capture &C) { 19094 return !C.isThisCapture() && !C.isInitCapture(); 19095 })) 19096 return; 19097 19098 // The default capture specifiers, '=' or '&', must appear first in the 19099 // capture body. 19100 SourceLocation DefaultInsertLoc = 19101 LSI->IntroducerRange.getBegin().getLocWithOffset(1); 19102 19103 if (ShouldOfferCopyFix) { 19104 bool CanDefaultCopyCapture = true; 19105 // [=, *this] OK since c++17 19106 // [=, this] OK since c++20 19107 if (LSI->isCXXThisCaptured() && !Sema.getLangOpts().CPlusPlus20) 19108 CanDefaultCopyCapture = Sema.getLangOpts().CPlusPlus17 19109 ? LSI->getCXXThisCapture().isCopyCapture() 19110 : false; 19111 // We can't use default capture by copy if any captures already specified 19112 // capture by copy. 19113 if (CanDefaultCopyCapture && llvm::none_of(LSI->Captures, [](Capture &C) { 19114 return !C.isThisCapture() && !C.isInitCapture() && C.isCopyCapture(); 19115 })) { 19116 FixBuffer.assign({"=", Separator}); 19117 Sema.Diag(DefaultInsertLoc, diag::note_lambda_default_capture_fixit) 19118 << /*value*/ 0 19119 << FixItHint::CreateInsertion(DefaultInsertLoc, FixBuffer); 19120 } 19121 } 19122 19123 // We can't use default capture by reference if any captures already specified 19124 // capture by reference. 19125 if (llvm::none_of(LSI->Captures, [](Capture &C) { 19126 return !C.isInitCapture() && C.isReferenceCapture() && 19127 !C.isThisCapture(); 19128 })) { 19129 FixBuffer.assign({"&", Separator}); 19130 Sema.Diag(DefaultInsertLoc, diag::note_lambda_default_capture_fixit) 19131 << /*reference*/ 1 19132 << FixItHint::CreateInsertion(DefaultInsertLoc, FixBuffer); 19133 } 19134 } 19135 19136 bool Sema::tryCaptureVariable( 19137 ValueDecl *Var, SourceLocation ExprLoc, TryCaptureKind Kind, 19138 SourceLocation EllipsisLoc, bool BuildAndDiagnose, QualType &CaptureType, 19139 QualType &DeclRefType, const unsigned *const FunctionScopeIndexToStopAt) { 19140 // An init-capture is notionally from the context surrounding its 19141 // declaration, but its parent DC is the lambda class. 19142 DeclContext *VarDC = Var->getDeclContext(); 19143 DeclContext *DC = CurContext; 19144 19145 // Skip past RequiresExprBodys because they don't constitute function scopes. 19146 while (DC->isRequiresExprBody()) 19147 DC = DC->getParent(); 19148 19149 // tryCaptureVariable is called every time a DeclRef is formed, 19150 // it can therefore have non-negigible impact on performances. 19151 // For local variables and when there is no capturing scope, 19152 // we can bailout early. 19153 if (CapturingFunctionScopes == 0 && (!BuildAndDiagnose || VarDC == DC)) 19154 return true; 19155 19156 // Exception: Function parameters are not tied to the function's DeclContext 19157 // until we enter the function definition. Capturing them anyway would result 19158 // in an out-of-bounds error while traversing DC and its parents. 19159 if (isa<ParmVarDecl>(Var) && !VarDC->isFunctionOrMethod()) 19160 return true; 19161 19162 const auto *VD = dyn_cast<VarDecl>(Var); 19163 if (VD) { 19164 if (VD->isInitCapture()) 19165 VarDC = VarDC->getParent(); 19166 } else { 19167 VD = Var->getPotentiallyDecomposedVarDecl(); 19168 } 19169 assert(VD && "Cannot capture a null variable"); 19170 19171 const unsigned MaxFunctionScopesIndex = FunctionScopeIndexToStopAt 19172 ? *FunctionScopeIndexToStopAt : FunctionScopes.size() - 1; 19173 // We need to sync up the Declaration Context with the 19174 // FunctionScopeIndexToStopAt 19175 if (FunctionScopeIndexToStopAt) { 19176 assert(!FunctionScopes.empty() && "No function scopes to stop at?"); 19177 unsigned FSIndex = FunctionScopes.size() - 1; 19178 // When we're parsing the lambda parameter list, the current DeclContext is 19179 // NOT the lambda but its parent. So move away the current LSI before 19180 // aligning DC and FunctionScopeIndexToStopAt. 19181 if (auto *LSI = dyn_cast<LambdaScopeInfo>(FunctionScopes[FSIndex]); 19182 FSIndex && LSI && !LSI->AfterParameterList) 19183 --FSIndex; 19184 assert(MaxFunctionScopesIndex <= FSIndex && 19185 "FunctionScopeIndexToStopAt should be no greater than FSIndex into " 19186 "FunctionScopes."); 19187 while (FSIndex != MaxFunctionScopesIndex) { 19188 DC = getLambdaAwareParentOfDeclContext(DC); 19189 --FSIndex; 19190 } 19191 } 19192 19193 // Capture global variables if it is required to use private copy of this 19194 // variable. 19195 bool IsGlobal = !VD->hasLocalStorage(); 19196 if (IsGlobal && !(LangOpts.OpenMP && 19197 OpenMP().isOpenMPCapturedDecl(Var, /*CheckScopeInfo=*/true, 19198 MaxFunctionScopesIndex))) 19199 return true; 19200 19201 if (isa<VarDecl>(Var)) 19202 Var = cast<VarDecl>(Var->getCanonicalDecl()); 19203 19204 // Walk up the stack to determine whether we can capture the variable, 19205 // performing the "simple" checks that don't depend on type. We stop when 19206 // we've either hit the declared scope of the variable or find an existing 19207 // capture of that variable. We start from the innermost capturing-entity 19208 // (the DC) and ensure that all intervening capturing-entities 19209 // (blocks/lambdas etc.) between the innermost capturer and the variable`s 19210 // declcontext can either capture the variable or have already captured 19211 // the variable. 19212 CaptureType = Var->getType(); 19213 DeclRefType = CaptureType.getNonReferenceType(); 19214 bool Nested = false; 19215 bool Explicit = (Kind != TryCaptureKind::Implicit); 19216 unsigned FunctionScopesIndex = MaxFunctionScopesIndex; 19217 do { 19218 19219 LambdaScopeInfo *LSI = nullptr; 19220 if (!FunctionScopes.empty()) 19221 LSI = dyn_cast_or_null<LambdaScopeInfo>( 19222 FunctionScopes[FunctionScopesIndex]); 19223 19224 bool IsInScopeDeclarationContext = 19225 !LSI || LSI->AfterParameterList || CurContext == LSI->CallOperator; 19226 19227 if (LSI && !LSI->AfterParameterList) { 19228 // This allows capturing parameters from a default value which does not 19229 // seems correct 19230 if (isa<ParmVarDecl>(Var) && !Var->getDeclContext()->isFunctionOrMethod()) 19231 return true; 19232 } 19233 // If the variable is declared in the current context, there is no need to 19234 // capture it. 19235 if (IsInScopeDeclarationContext && 19236 FunctionScopesIndex == MaxFunctionScopesIndex && VarDC == DC) 19237 return true; 19238 19239 // Only block literals, captured statements, and lambda expressions can 19240 // capture; other scopes don't work. 19241 DeclContext *ParentDC = 19242 !IsInScopeDeclarationContext 19243 ? DC->getParent() 19244 : getParentOfCapturingContextOrNull(DC, Var, ExprLoc, 19245 BuildAndDiagnose, *this); 19246 // We need to check for the parent *first* because, if we *have* 19247 // private-captured a global variable, we need to recursively capture it in 19248 // intermediate blocks, lambdas, etc. 19249 if (!ParentDC) { 19250 if (IsGlobal) { 19251 FunctionScopesIndex = MaxFunctionScopesIndex - 1; 19252 break; 19253 } 19254 return true; 19255 } 19256 19257 FunctionScopeInfo *FSI = FunctionScopes[FunctionScopesIndex]; 19258 CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FSI); 19259 19260 // Check whether we've already captured it. 19261 if (isVariableAlreadyCapturedInScopeInfo(CSI, Var, Nested, CaptureType, 19262 DeclRefType)) { 19263 CSI->getCapture(Var).markUsed(BuildAndDiagnose); 19264 break; 19265 } 19266 19267 // When evaluating some attributes (like enable_if) we might refer to a 19268 // function parameter appertaining to the same declaration as that 19269 // attribute. 19270 if (const auto *Parm = dyn_cast<ParmVarDecl>(Var); 19271 Parm && Parm->getDeclContext() == DC) 19272 return true; 19273 19274 // If we are instantiating a generic lambda call operator body, 19275 // we do not want to capture new variables. What was captured 19276 // during either a lambdas transformation or initial parsing 19277 // should be used. 19278 if (isGenericLambdaCallOperatorSpecialization(DC)) { 19279 if (BuildAndDiagnose) { 19280 LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CSI); 19281 if (LSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None) { 19282 Diag(ExprLoc, diag::err_lambda_impcap) << Var; 19283 Diag(Var->getLocation(), diag::note_previous_decl) << Var; 19284 Diag(LSI->Lambda->getBeginLoc(), diag::note_lambda_decl); 19285 buildLambdaCaptureFixit(*this, LSI, Var); 19286 } else 19287 diagnoseUncapturableValueReferenceOrBinding(*this, ExprLoc, Var); 19288 } 19289 return true; 19290 } 19291 19292 // Try to capture variable-length arrays types. 19293 if (Var->getType()->isVariablyModifiedType()) { 19294 // We're going to walk down into the type and look for VLA 19295 // expressions. 19296 QualType QTy = Var->getType(); 19297 if (ParmVarDecl *PVD = dyn_cast_or_null<ParmVarDecl>(Var)) 19298 QTy = PVD->getOriginalType(); 19299 captureVariablyModifiedType(Context, QTy, CSI); 19300 } 19301 19302 if (getLangOpts().OpenMP) { 19303 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(CSI)) { 19304 // OpenMP private variables should not be captured in outer scope, so 19305 // just break here. Similarly, global variables that are captured in a 19306 // target region should not be captured outside the scope of the region. 19307 if (RSI->CapRegionKind == CR_OpenMP) { 19308 // FIXME: We should support capturing structured bindings in OpenMP. 19309 if (isa<BindingDecl>(Var)) { 19310 if (BuildAndDiagnose) { 19311 Diag(ExprLoc, diag::err_capture_binding_openmp) << Var; 19312 Diag(Var->getLocation(), diag::note_entity_declared_at) << Var; 19313 } 19314 return true; 19315 } 19316 OpenMPClauseKind IsOpenMPPrivateDecl = OpenMP().isOpenMPPrivateDecl( 19317 Var, RSI->OpenMPLevel, RSI->OpenMPCaptureLevel); 19318 // If the variable is private (i.e. not captured) and has variably 19319 // modified type, we still need to capture the type for correct 19320 // codegen in all regions, associated with the construct. Currently, 19321 // it is captured in the innermost captured region only. 19322 if (IsOpenMPPrivateDecl != OMPC_unknown && 19323 Var->getType()->isVariablyModifiedType()) { 19324 QualType QTy = Var->getType(); 19325 if (ParmVarDecl *PVD = dyn_cast_or_null<ParmVarDecl>(Var)) 19326 QTy = PVD->getOriginalType(); 19327 for (int I = 1, 19328 E = OpenMP().getNumberOfConstructScopes(RSI->OpenMPLevel); 19329 I < E; ++I) { 19330 auto *OuterRSI = cast<CapturedRegionScopeInfo>( 19331 FunctionScopes[FunctionScopesIndex - I]); 19332 assert(RSI->OpenMPLevel == OuterRSI->OpenMPLevel && 19333 "Wrong number of captured regions associated with the " 19334 "OpenMP construct."); 19335 captureVariablyModifiedType(Context, QTy, OuterRSI); 19336 } 19337 } 19338 bool IsTargetCap = 19339 IsOpenMPPrivateDecl != OMPC_private && 19340 OpenMP().isOpenMPTargetCapturedDecl(Var, RSI->OpenMPLevel, 19341 RSI->OpenMPCaptureLevel); 19342 // Do not capture global if it is not privatized in outer regions. 19343 bool IsGlobalCap = 19344 IsGlobal && OpenMP().isOpenMPGlobalCapturedDecl( 19345 Var, RSI->OpenMPLevel, RSI->OpenMPCaptureLevel); 19346 19347 // When we detect target captures we are looking from inside the 19348 // target region, therefore we need to propagate the capture from the 19349 // enclosing region. Therefore, the capture is not initially nested. 19350 if (IsTargetCap) 19351 OpenMP().adjustOpenMPTargetScopeIndex(FunctionScopesIndex, 19352 RSI->OpenMPLevel); 19353 19354 if (IsTargetCap || IsOpenMPPrivateDecl == OMPC_private || 19355 (IsGlobal && !IsGlobalCap)) { 19356 Nested = !IsTargetCap; 19357 bool HasConst = DeclRefType.isConstQualified(); 19358 DeclRefType = DeclRefType.getUnqualifiedType(); 19359 // Don't lose diagnostics about assignments to const. 19360 if (HasConst) 19361 DeclRefType.addConst(); 19362 CaptureType = Context.getLValueReferenceType(DeclRefType); 19363 break; 19364 } 19365 } 19366 } 19367 } 19368 if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None && !Explicit) { 19369 // No capture-default, and this is not an explicit capture 19370 // so cannot capture this variable. 19371 if (BuildAndDiagnose) { 19372 Diag(ExprLoc, diag::err_lambda_impcap) << Var; 19373 Diag(Var->getLocation(), diag::note_previous_decl) << Var; 19374 auto *LSI = cast<LambdaScopeInfo>(CSI); 19375 if (LSI->Lambda) { 19376 Diag(LSI->Lambda->getBeginLoc(), diag::note_lambda_decl); 19377 buildLambdaCaptureFixit(*this, LSI, Var); 19378 } 19379 // FIXME: If we error out because an outer lambda can not implicitly 19380 // capture a variable that an inner lambda explicitly captures, we 19381 // should have the inner lambda do the explicit capture - because 19382 // it makes for cleaner diagnostics later. This would purely be done 19383 // so that the diagnostic does not misleadingly claim that a variable 19384 // can not be captured by a lambda implicitly even though it is captured 19385 // explicitly. Suggestion: 19386 // - create const bool VariableCaptureWasInitiallyExplicit = Explicit 19387 // at the function head 19388 // - cache the StartingDeclContext - this must be a lambda 19389 // - captureInLambda in the innermost lambda the variable. 19390 } 19391 return true; 19392 } 19393 Explicit = false; 19394 FunctionScopesIndex--; 19395 if (IsInScopeDeclarationContext) 19396 DC = ParentDC; 19397 } while (!VarDC->Equals(DC)); 19398 19399 // Walk back down the scope stack, (e.g. from outer lambda to inner lambda) 19400 // computing the type of the capture at each step, checking type-specific 19401 // requirements, and adding captures if requested. 19402 // If the variable had already been captured previously, we start capturing 19403 // at the lambda nested within that one. 19404 bool Invalid = false; 19405 for (unsigned I = ++FunctionScopesIndex, N = MaxFunctionScopesIndex + 1; I != N; 19406 ++I) { 19407 CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[I]); 19408 19409 // Certain capturing entities (lambdas, blocks etc.) are not allowed to capture 19410 // certain types of variables (unnamed, variably modified types etc.) 19411 // so check for eligibility. 19412 if (!Invalid) 19413 Invalid = 19414 !isVariableCapturable(CSI, Var, ExprLoc, BuildAndDiagnose, *this); 19415 19416 // After encountering an error, if we're actually supposed to capture, keep 19417 // capturing in nested contexts to suppress any follow-on diagnostics. 19418 if (Invalid && !BuildAndDiagnose) 19419 return true; 19420 19421 if (BlockScopeInfo *BSI = dyn_cast<BlockScopeInfo>(CSI)) { 19422 Invalid = !captureInBlock(BSI, Var, ExprLoc, BuildAndDiagnose, CaptureType, 19423 DeclRefType, Nested, *this, Invalid); 19424 Nested = true; 19425 } else if (CapturedRegionScopeInfo *RSI = dyn_cast<CapturedRegionScopeInfo>(CSI)) { 19426 Invalid = !captureInCapturedRegion( 19427 RSI, Var, ExprLoc, BuildAndDiagnose, CaptureType, DeclRefType, Nested, 19428 Kind, /*IsTopScope*/ I == N - 1, *this, Invalid); 19429 Nested = true; 19430 } else { 19431 LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CSI); 19432 Invalid = 19433 !captureInLambda(LSI, Var, ExprLoc, BuildAndDiagnose, CaptureType, 19434 DeclRefType, Nested, Kind, EllipsisLoc, 19435 /*IsTopScope*/ I == N - 1, *this, Invalid); 19436 Nested = true; 19437 } 19438 19439 if (Invalid && !BuildAndDiagnose) 19440 return true; 19441 } 19442 return Invalid; 19443 } 19444 19445 bool Sema::tryCaptureVariable(ValueDecl *Var, SourceLocation Loc, 19446 TryCaptureKind Kind, SourceLocation EllipsisLoc) { 19447 QualType CaptureType; 19448 QualType DeclRefType; 19449 return tryCaptureVariable(Var, Loc, Kind, EllipsisLoc, 19450 /*BuildAndDiagnose=*/true, CaptureType, 19451 DeclRefType, nullptr); 19452 } 19453 19454 bool Sema::NeedToCaptureVariable(ValueDecl *Var, SourceLocation Loc) { 19455 QualType CaptureType; 19456 QualType DeclRefType; 19457 return !tryCaptureVariable( 19458 Var, Loc, TryCaptureKind::Implicit, SourceLocation(), 19459 /*BuildAndDiagnose=*/false, CaptureType, DeclRefType, nullptr); 19460 } 19461 19462 QualType Sema::getCapturedDeclRefType(ValueDecl *Var, SourceLocation Loc) { 19463 assert(Var && "Null value cannot be captured"); 19464 19465 QualType CaptureType; 19466 QualType DeclRefType; 19467 19468 // Determine whether we can capture this variable. 19469 if (tryCaptureVariable(Var, Loc, TryCaptureKind::Implicit, SourceLocation(), 19470 /*BuildAndDiagnose=*/false, CaptureType, DeclRefType, 19471 nullptr)) 19472 return QualType(); 19473 19474 return DeclRefType; 19475 } 19476 19477 namespace { 19478 // Helper to copy the template arguments from a DeclRefExpr or MemberExpr. 19479 // The produced TemplateArgumentListInfo* points to data stored within this 19480 // object, so should only be used in contexts where the pointer will not be 19481 // used after the CopiedTemplateArgs object is destroyed. 19482 class CopiedTemplateArgs { 19483 bool HasArgs; 19484 TemplateArgumentListInfo TemplateArgStorage; 19485 public: 19486 template<typename RefExpr> 19487 CopiedTemplateArgs(RefExpr *E) : HasArgs(E->hasExplicitTemplateArgs()) { 19488 if (HasArgs) 19489 E->copyTemplateArgumentsInto(TemplateArgStorage); 19490 } 19491 operator TemplateArgumentListInfo*() 19492 #ifdef __has_cpp_attribute 19493 #if __has_cpp_attribute(clang::lifetimebound) 19494 [[clang::lifetimebound]] 19495 #endif 19496 #endif 19497 { 19498 return HasArgs ? &TemplateArgStorage : nullptr; 19499 } 19500 }; 19501 } 19502 19503 /// Walk the set of potential results of an expression and mark them all as 19504 /// non-odr-uses if they satisfy the side-conditions of the NonOdrUseReason. 19505 /// 19506 /// \return A new expression if we found any potential results, ExprEmpty() if 19507 /// not, and ExprError() if we diagnosed an error. 19508 static ExprResult rebuildPotentialResultsAsNonOdrUsed(Sema &S, Expr *E, 19509 NonOdrUseReason NOUR) { 19510 // Per C++11 [basic.def.odr], a variable is odr-used "unless it is 19511 // an object that satisfies the requirements for appearing in a 19512 // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) 19513 // is immediately applied." This function handles the lvalue-to-rvalue 19514 // conversion part. 19515 // 19516 // If we encounter a node that claims to be an odr-use but shouldn't be, we 19517 // transform it into the relevant kind of non-odr-use node and rebuild the 19518 // tree of nodes leading to it. 19519 // 19520 // This is a mini-TreeTransform that only transforms a restricted subset of 19521 // nodes (and only certain operands of them). 19522 19523 // Rebuild a subexpression. 19524 auto Rebuild = [&](Expr *Sub) { 19525 return rebuildPotentialResultsAsNonOdrUsed(S, Sub, NOUR); 19526 }; 19527 19528 // Check whether a potential result satisfies the requirements of NOUR. 19529 auto IsPotentialResultOdrUsed = [&](NamedDecl *D) { 19530 // Any entity other than a VarDecl is always odr-used whenever it's named 19531 // in a potentially-evaluated expression. 19532 auto *VD = dyn_cast<VarDecl>(D); 19533 if (!VD) 19534 return true; 19535 19536 // C++2a [basic.def.odr]p4: 19537 // A variable x whose name appears as a potentially-evalauted expression 19538 // e is odr-used by e unless 19539 // -- x is a reference that is usable in constant expressions, or 19540 // -- x is a variable of non-reference type that is usable in constant 19541 // expressions and has no mutable subobjects, and e is an element of 19542 // the set of potential results of an expression of 19543 // non-volatile-qualified non-class type to which the lvalue-to-rvalue 19544 // conversion is applied, or 19545 // -- x is a variable of non-reference type, and e is an element of the 19546 // set of potential results of a discarded-value expression to which 19547 // the lvalue-to-rvalue conversion is not applied 19548 // 19549 // We check the first bullet and the "potentially-evaluated" condition in 19550 // BuildDeclRefExpr. We check the type requirements in the second bullet 19551 // in CheckLValueToRValueConversionOperand below. 19552 switch (NOUR) { 19553 case NOUR_None: 19554 case NOUR_Unevaluated: 19555 llvm_unreachable("unexpected non-odr-use-reason"); 19556 19557 case NOUR_Constant: 19558 // Constant references were handled when they were built. 19559 if (VD->getType()->isReferenceType()) 19560 return true; 19561 if (auto *RD = VD->getType()->getAsCXXRecordDecl()) 19562 if (RD->hasDefinition() && RD->hasMutableFields()) 19563 return true; 19564 if (!VD->isUsableInConstantExpressions(S.Context)) 19565 return true; 19566 break; 19567 19568 case NOUR_Discarded: 19569 if (VD->getType()->isReferenceType()) 19570 return true; 19571 break; 19572 } 19573 return false; 19574 }; 19575 19576 // Check whether this expression may be odr-used in CUDA/HIP. 19577 auto MaybeCUDAODRUsed = [&]() -> bool { 19578 if (!S.LangOpts.CUDA) 19579 return false; 19580 LambdaScopeInfo *LSI = S.getCurLambda(); 19581 if (!LSI) 19582 return false; 19583 auto *DRE = dyn_cast<DeclRefExpr>(E); 19584 if (!DRE) 19585 return false; 19586 auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); 19587 if (!VD) 19588 return false; 19589 return LSI->CUDAPotentialODRUsedVars.count(VD); 19590 }; 19591 19592 // Mark that this expression does not constitute an odr-use. 19593 auto MarkNotOdrUsed = [&] { 19594 if (!MaybeCUDAODRUsed()) { 19595 S.MaybeODRUseExprs.remove(E); 19596 if (LambdaScopeInfo *LSI = S.getCurLambda()) 19597 LSI->markVariableExprAsNonODRUsed(E); 19598 } 19599 }; 19600 19601 // C++2a [basic.def.odr]p2: 19602 // The set of potential results of an expression e is defined as follows: 19603 switch (E->getStmtClass()) { 19604 // -- If e is an id-expression, ... 19605 case Expr::DeclRefExprClass: { 19606 auto *DRE = cast<DeclRefExpr>(E); 19607 if (DRE->isNonOdrUse() || IsPotentialResultOdrUsed(DRE->getDecl())) 19608 break; 19609 19610 // Rebuild as a non-odr-use DeclRefExpr. 19611 MarkNotOdrUsed(); 19612 return DeclRefExpr::Create( 19613 S.Context, DRE->getQualifierLoc(), DRE->getTemplateKeywordLoc(), 19614 DRE->getDecl(), DRE->refersToEnclosingVariableOrCapture(), 19615 DRE->getNameInfo(), DRE->getType(), DRE->getValueKind(), 19616 DRE->getFoundDecl(), CopiedTemplateArgs(DRE), NOUR); 19617 } 19618 19619 case Expr::FunctionParmPackExprClass: { 19620 auto *FPPE = cast<FunctionParmPackExpr>(E); 19621 // If any of the declarations in the pack is odr-used, then the expression 19622 // as a whole constitutes an odr-use. 19623 for (ValueDecl *D : *FPPE) 19624 if (IsPotentialResultOdrUsed(D)) 19625 return ExprEmpty(); 19626 19627 // FIXME: Rebuild as a non-odr-use FunctionParmPackExpr? In practice, 19628 // nothing cares about whether we marked this as an odr-use, but it might 19629 // be useful for non-compiler tools. 19630 MarkNotOdrUsed(); 19631 break; 19632 } 19633 19634 // -- If e is a subscripting operation with an array operand... 19635 case Expr::ArraySubscriptExprClass: { 19636 auto *ASE = cast<ArraySubscriptExpr>(E); 19637 Expr *OldBase = ASE->getBase()->IgnoreImplicit(); 19638 if (!OldBase->getType()->isArrayType()) 19639 break; 19640 ExprResult Base = Rebuild(OldBase); 19641 if (!Base.isUsable()) 19642 return Base; 19643 Expr *LHS = ASE->getBase() == ASE->getLHS() ? Base.get() : ASE->getLHS(); 19644 Expr *RHS = ASE->getBase() == ASE->getRHS() ? Base.get() : ASE->getRHS(); 19645 SourceLocation LBracketLoc = ASE->getBeginLoc(); // FIXME: Not stored. 19646 return S.ActOnArraySubscriptExpr(nullptr, LHS, LBracketLoc, RHS, 19647 ASE->getRBracketLoc()); 19648 } 19649 19650 case Expr::MemberExprClass: { 19651 auto *ME = cast<MemberExpr>(E); 19652 // -- If e is a class member access expression [...] naming a non-static 19653 // data member... 19654 if (isa<FieldDecl>(ME->getMemberDecl())) { 19655 ExprResult Base = Rebuild(ME->getBase()); 19656 if (!Base.isUsable()) 19657 return Base; 19658 return MemberExpr::Create( 19659 S.Context, Base.get(), ME->isArrow(), ME->getOperatorLoc(), 19660 ME->getQualifierLoc(), ME->getTemplateKeywordLoc(), 19661 ME->getMemberDecl(), ME->getFoundDecl(), ME->getMemberNameInfo(), 19662 CopiedTemplateArgs(ME), ME->getType(), ME->getValueKind(), 19663 ME->getObjectKind(), ME->isNonOdrUse()); 19664 } 19665 19666 if (ME->getMemberDecl()->isCXXInstanceMember()) 19667 break; 19668 19669 // -- If e is a class member access expression naming a static data member, 19670 // ... 19671 if (ME->isNonOdrUse() || IsPotentialResultOdrUsed(ME->getMemberDecl())) 19672 break; 19673 19674 // Rebuild as a non-odr-use MemberExpr. 19675 MarkNotOdrUsed(); 19676 return MemberExpr::Create( 19677 S.Context, ME->getBase(), ME->isArrow(), ME->getOperatorLoc(), 19678 ME->getQualifierLoc(), ME->getTemplateKeywordLoc(), ME->getMemberDecl(), 19679 ME->getFoundDecl(), ME->getMemberNameInfo(), CopiedTemplateArgs(ME), 19680 ME->getType(), ME->getValueKind(), ME->getObjectKind(), NOUR); 19681 } 19682 19683 case Expr::BinaryOperatorClass: { 19684 auto *BO = cast<BinaryOperator>(E); 19685 Expr *LHS = BO->getLHS(); 19686 Expr *RHS = BO->getRHS(); 19687 // -- If e is a pointer-to-member expression of the form e1 .* e2 ... 19688 if (BO->getOpcode() == BO_PtrMemD) { 19689 ExprResult Sub = Rebuild(LHS); 19690 if (!Sub.isUsable()) 19691 return Sub; 19692 BO->setLHS(Sub.get()); 19693 // -- If e is a comma expression, ... 19694 } else if (BO->getOpcode() == BO_Comma) { 19695 ExprResult Sub = Rebuild(RHS); 19696 if (!Sub.isUsable()) 19697 return Sub; 19698 BO->setRHS(Sub.get()); 19699 } else { 19700 break; 19701 } 19702 return ExprResult(BO); 19703 } 19704 19705 // -- If e has the form (e1)... 19706 case Expr::ParenExprClass: { 19707 auto *PE = cast<ParenExpr>(E); 19708 ExprResult Sub = Rebuild(PE->getSubExpr()); 19709 if (!Sub.isUsable()) 19710 return Sub; 19711 return S.ActOnParenExpr(PE->getLParen(), PE->getRParen(), Sub.get()); 19712 } 19713 19714 // -- If e is a glvalue conditional expression, ... 19715 // We don't apply this to a binary conditional operator. FIXME: Should we? 19716 case Expr::ConditionalOperatorClass: { 19717 auto *CO = cast<ConditionalOperator>(E); 19718 ExprResult LHS = Rebuild(CO->getLHS()); 19719 if (LHS.isInvalid()) 19720 return ExprError(); 19721 ExprResult RHS = Rebuild(CO->getRHS()); 19722 if (RHS.isInvalid()) 19723 return ExprError(); 19724 if (!LHS.isUsable() && !RHS.isUsable()) 19725 return ExprEmpty(); 19726 if (!LHS.isUsable()) 19727 LHS = CO->getLHS(); 19728 if (!RHS.isUsable()) 19729 RHS = CO->getRHS(); 19730 return S.ActOnConditionalOp(CO->getQuestionLoc(), CO->getColonLoc(), 19731 CO->getCond(), LHS.get(), RHS.get()); 19732 } 19733 19734 // [Clang extension] 19735 // -- If e has the form __extension__ e1... 19736 case Expr::UnaryOperatorClass: { 19737 auto *UO = cast<UnaryOperator>(E); 19738 if (UO->getOpcode() != UO_Extension) 19739 break; 19740 ExprResult Sub = Rebuild(UO->getSubExpr()); 19741 if (!Sub.isUsable()) 19742 return Sub; 19743 return S.BuildUnaryOp(nullptr, UO->getOperatorLoc(), UO_Extension, 19744 Sub.get()); 19745 } 19746 19747 // [Clang extension] 19748 // -- If e has the form _Generic(...), the set of potential results is the 19749 // union of the sets of potential results of the associated expressions. 19750 case Expr::GenericSelectionExprClass: { 19751 auto *GSE = cast<GenericSelectionExpr>(E); 19752 19753 SmallVector<Expr *, 4> AssocExprs; 19754 bool AnyChanged = false; 19755 for (Expr *OrigAssocExpr : GSE->getAssocExprs()) { 19756 ExprResult AssocExpr = Rebuild(OrigAssocExpr); 19757 if (AssocExpr.isInvalid()) 19758 return ExprError(); 19759 if (AssocExpr.isUsable()) { 19760 AssocExprs.push_back(AssocExpr.get()); 19761 AnyChanged = true; 19762 } else { 19763 AssocExprs.push_back(OrigAssocExpr); 19764 } 19765 } 19766 19767 void *ExOrTy = nullptr; 19768 bool IsExpr = GSE->isExprPredicate(); 19769 if (IsExpr) 19770 ExOrTy = GSE->getControllingExpr(); 19771 else 19772 ExOrTy = GSE->getControllingType(); 19773 return AnyChanged ? S.CreateGenericSelectionExpr( 19774 GSE->getGenericLoc(), GSE->getDefaultLoc(), 19775 GSE->getRParenLoc(), IsExpr, ExOrTy, 19776 GSE->getAssocTypeSourceInfos(), AssocExprs) 19777 : ExprEmpty(); 19778 } 19779 19780 // [Clang extension] 19781 // -- If e has the form __builtin_choose_expr(...), the set of potential 19782 // results is the union of the sets of potential results of the 19783 // second and third subexpressions. 19784 case Expr::ChooseExprClass: { 19785 auto *CE = cast<ChooseExpr>(E); 19786 19787 ExprResult LHS = Rebuild(CE->getLHS()); 19788 if (LHS.isInvalid()) 19789 return ExprError(); 19790 19791 ExprResult RHS = Rebuild(CE->getLHS()); 19792 if (RHS.isInvalid()) 19793 return ExprError(); 19794 19795 if (!LHS.get() && !RHS.get()) 19796 return ExprEmpty(); 19797 if (!LHS.isUsable()) 19798 LHS = CE->getLHS(); 19799 if (!RHS.isUsable()) 19800 RHS = CE->getRHS(); 19801 19802 return S.ActOnChooseExpr(CE->getBuiltinLoc(), CE->getCond(), LHS.get(), 19803 RHS.get(), CE->getRParenLoc()); 19804 } 19805 19806 // Step through non-syntactic nodes. 19807 case Expr::ConstantExprClass: { 19808 auto *CE = cast<ConstantExpr>(E); 19809 ExprResult Sub = Rebuild(CE->getSubExpr()); 19810 if (!Sub.isUsable()) 19811 return Sub; 19812 return ConstantExpr::Create(S.Context, Sub.get()); 19813 } 19814 19815 // We could mostly rely on the recursive rebuilding to rebuild implicit 19816 // casts, but not at the top level, so rebuild them here. 19817 case Expr::ImplicitCastExprClass: { 19818 auto *ICE = cast<ImplicitCastExpr>(E); 19819 // Only step through the narrow set of cast kinds we expect to encounter. 19820 // Anything else suggests we've left the region in which potential results 19821 // can be found. 19822 switch (ICE->getCastKind()) { 19823 case CK_NoOp: 19824 case CK_DerivedToBase: 19825 case CK_UncheckedDerivedToBase: { 19826 ExprResult Sub = Rebuild(ICE->getSubExpr()); 19827 if (!Sub.isUsable()) 19828 return Sub; 19829 CXXCastPath Path(ICE->path()); 19830 return S.ImpCastExprToType(Sub.get(), ICE->getType(), ICE->getCastKind(), 19831 ICE->getValueKind(), &Path); 19832 } 19833 19834 default: 19835 break; 19836 } 19837 break; 19838 } 19839 19840 default: 19841 break; 19842 } 19843 19844 // Can't traverse through this node. Nothing to do. 19845 return ExprEmpty(); 19846 } 19847 19848 ExprResult Sema::CheckLValueToRValueConversionOperand(Expr *E) { 19849 // Check whether the operand is or contains an object of non-trivial C union 19850 // type. 19851 if (E->getType().isVolatileQualified() && 19852 (E->getType().hasNonTrivialToPrimitiveDestructCUnion() || 19853 E->getType().hasNonTrivialToPrimitiveCopyCUnion())) 19854 checkNonTrivialCUnion(E->getType(), E->getExprLoc(), 19855 NonTrivialCUnionContext::LValueToRValueVolatile, 19856 NTCUK_Destruct | NTCUK_Copy); 19857 19858 // C++2a [basic.def.odr]p4: 19859 // [...] an expression of non-volatile-qualified non-class type to which 19860 // the lvalue-to-rvalue conversion is applied [...] 19861 if (E->getType().isVolatileQualified() || E->getType()->getAs<RecordType>()) 19862 return E; 19863 19864 ExprResult Result = 19865 rebuildPotentialResultsAsNonOdrUsed(*this, E, NOUR_Constant); 19866 if (Result.isInvalid()) 19867 return ExprError(); 19868 return Result.get() ? Result : E; 19869 } 19870 19871 ExprResult Sema::ActOnConstantExpression(ExprResult Res) { 19872 if (!Res.isUsable()) 19873 return Res; 19874 19875 // If a constant-expression is a reference to a variable where we delay 19876 // deciding whether it is an odr-use, just assume we will apply the 19877 // lvalue-to-rvalue conversion. In the one case where this doesn't happen 19878 // (a non-type template argument), we have special handling anyway. 19879 return CheckLValueToRValueConversionOperand(Res.get()); 19880 } 19881 19882 void Sema::CleanupVarDeclMarking() { 19883 // Iterate through a local copy in case MarkVarDeclODRUsed makes a recursive 19884 // call. 19885 MaybeODRUseExprSet LocalMaybeODRUseExprs; 19886 std::swap(LocalMaybeODRUseExprs, MaybeODRUseExprs); 19887 19888 for (Expr *E : LocalMaybeODRUseExprs) { 19889 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) { 19890 MarkVarDeclODRUsed(cast<VarDecl>(DRE->getDecl()), 19891 DRE->getLocation(), *this); 19892 } else if (auto *ME = dyn_cast<MemberExpr>(E)) { 19893 MarkVarDeclODRUsed(cast<VarDecl>(ME->getMemberDecl()), ME->getMemberLoc(), 19894 *this); 19895 } else if (auto *FP = dyn_cast<FunctionParmPackExpr>(E)) { 19896 for (ValueDecl *VD : *FP) 19897 MarkVarDeclODRUsed(VD, FP->getParameterPackLocation(), *this); 19898 } else { 19899 llvm_unreachable("Unexpected expression"); 19900 } 19901 } 19902 19903 assert(MaybeODRUseExprs.empty() && 19904 "MarkVarDeclODRUsed failed to cleanup MaybeODRUseExprs?"); 19905 } 19906 19907 static void DoMarkPotentialCapture(Sema &SemaRef, SourceLocation Loc, 19908 ValueDecl *Var, Expr *E) { 19909 VarDecl *VD = Var->getPotentiallyDecomposedVarDecl(); 19910 if (!VD) 19911 return; 19912 19913 const bool RefersToEnclosingScope = 19914 (SemaRef.CurContext != VD->getDeclContext() && 19915 VD->getDeclContext()->isFunctionOrMethod() && VD->hasLocalStorage()); 19916 if (RefersToEnclosingScope) { 19917 LambdaScopeInfo *const LSI = 19918 SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true); 19919 if (LSI && (!LSI->CallOperator || 19920 !LSI->CallOperator->Encloses(Var->getDeclContext()))) { 19921 // If a variable could potentially be odr-used, defer marking it so 19922 // until we finish analyzing the full expression for any 19923 // lvalue-to-rvalue 19924 // or discarded value conversions that would obviate odr-use. 19925 // Add it to the list of potential captures that will be analyzed 19926 // later (ActOnFinishFullExpr) for eventual capture and odr-use marking 19927 // unless the variable is a reference that was initialized by a constant 19928 // expression (this will never need to be captured or odr-used). 19929 // 19930 // FIXME: We can simplify this a lot after implementing P0588R1. 19931 assert(E && "Capture variable should be used in an expression."); 19932 if (!Var->getType()->isReferenceType() || 19933 !VD->isUsableInConstantExpressions(SemaRef.Context)) 19934 LSI->addPotentialCapture(E->IgnoreParens()); 19935 } 19936 } 19937 } 19938 19939 static void DoMarkVarDeclReferenced( 19940 Sema &SemaRef, SourceLocation Loc, VarDecl *Var, Expr *E, 19941 llvm::DenseMap<const VarDecl *, int> &RefsMinusAssignments) { 19942 assert((!E || isa<DeclRefExpr>(E) || isa<MemberExpr>(E) || 19943 isa<FunctionParmPackExpr>(E)) && 19944 "Invalid Expr argument to DoMarkVarDeclReferenced"); 19945 Var->setReferenced(); 19946 19947 if (Var->isInvalidDecl()) 19948 return; 19949 19950 auto *MSI = Var->getMemberSpecializationInfo(); 19951 TemplateSpecializationKind TSK = MSI ? MSI->getTemplateSpecializationKind() 19952 : Var->getTemplateSpecializationKind(); 19953 19954 OdrUseContext OdrUse = isOdrUseContext(SemaRef); 19955 bool UsableInConstantExpr = 19956 Var->mightBeUsableInConstantExpressions(SemaRef.Context); 19957 19958 if (Var->isLocalVarDeclOrParm() && !Var->hasExternalStorage()) { 19959 RefsMinusAssignments.insert({Var, 0}).first->getSecond()++; 19960 } 19961 19962 // C++20 [expr.const]p12: 19963 // A variable [...] is needed for constant evaluation if it is [...] a 19964 // variable whose name appears as a potentially constant evaluated 19965 // expression that is either a contexpr variable or is of non-volatile 19966 // const-qualified integral type or of reference type 19967 bool NeededForConstantEvaluation = 19968 isPotentiallyConstantEvaluatedContext(SemaRef) && UsableInConstantExpr; 19969 19970 bool NeedDefinition = 19971 OdrUse == OdrUseContext::Used || NeededForConstantEvaluation; 19972 19973 assert(!isa<VarTemplatePartialSpecializationDecl>(Var) && 19974 "Can't instantiate a partial template specialization."); 19975 19976 // If this might be a member specialization of a static data member, check 19977 // the specialization is visible. We already did the checks for variable 19978 // template specializations when we created them. 19979 if (NeedDefinition && TSK != TSK_Undeclared && 19980 !isa<VarTemplateSpecializationDecl>(Var)) 19981 SemaRef.checkSpecializationVisibility(Loc, Var); 19982 19983 // Perform implicit instantiation of static data members, static data member 19984 // templates of class templates, and variable template specializations. Delay 19985 // instantiations of variable templates, except for those that could be used 19986 // in a constant expression. 19987 if (NeedDefinition && isTemplateInstantiation(TSK)) { 19988 // Per C++17 [temp.explicit]p10, we may instantiate despite an explicit 19989 // instantiation declaration if a variable is usable in a constant 19990 // expression (among other cases). 19991 bool TryInstantiating = 19992 TSK == TSK_ImplicitInstantiation || 19993 (TSK == TSK_ExplicitInstantiationDeclaration && UsableInConstantExpr); 19994 19995 if (TryInstantiating) { 19996 SourceLocation PointOfInstantiation = 19997 MSI ? MSI->getPointOfInstantiation() : Var->getPointOfInstantiation(); 19998 bool FirstInstantiation = PointOfInstantiation.isInvalid(); 19999 if (FirstInstantiation) { 20000 PointOfInstantiation = Loc; 20001 if (MSI) 20002 MSI->setPointOfInstantiation(PointOfInstantiation); 20003 // FIXME: Notify listener. 20004 else 20005 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); 20006 } 20007 20008 if (UsableInConstantExpr || Var->getType()->isUndeducedType()) { 20009 // Do not defer instantiations of variables that could be used in a 20010 // constant expression. 20011 // The type deduction also needs a complete initializer. 20012 SemaRef.runWithSufficientStackSpace(PointOfInstantiation, [&] { 20013 SemaRef.InstantiateVariableDefinition(PointOfInstantiation, Var); 20014 }); 20015 20016 // The size of an incomplete array type can be updated by 20017 // instantiating the initializer. The DeclRefExpr's type should be 20018 // updated accordingly too, or users of it would be confused! 20019 if (E) 20020 SemaRef.getCompletedType(E); 20021 20022 // Re-set the member to trigger a recomputation of the dependence bits 20023 // for the expression. 20024 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) 20025 DRE->setDecl(DRE->getDecl()); 20026 else if (auto *ME = dyn_cast_or_null<MemberExpr>(E)) 20027 ME->setMemberDecl(ME->getMemberDecl()); 20028 } else if (FirstInstantiation) { 20029 SemaRef.PendingInstantiations 20030 .push_back(std::make_pair(Var, PointOfInstantiation)); 20031 } else { 20032 bool Inserted = false; 20033 for (auto &I : SemaRef.SavedPendingInstantiations) { 20034 auto Iter = llvm::find_if( 20035 I, [Var](const Sema::PendingImplicitInstantiation &P) { 20036 return P.first == Var; 20037 }); 20038 if (Iter != I.end()) { 20039 SemaRef.PendingInstantiations.push_back(*Iter); 20040 I.erase(Iter); 20041 Inserted = true; 20042 break; 20043 } 20044 } 20045 20046 // FIXME: For a specialization of a variable template, we don't 20047 // distinguish between "declaration and type implicitly instantiated" 20048 // and "implicit instantiation of definition requested", so we have 20049 // no direct way to avoid enqueueing the pending instantiation 20050 // multiple times. 20051 if (isa<VarTemplateSpecializationDecl>(Var) && !Inserted) 20052 SemaRef.PendingInstantiations 20053 .push_back(std::make_pair(Var, PointOfInstantiation)); 20054 } 20055 } 20056 } 20057 20058 // C++2a [basic.def.odr]p4: 20059 // A variable x whose name appears as a potentially-evaluated expression e 20060 // is odr-used by e unless 20061 // -- x is a reference that is usable in constant expressions 20062 // -- x is a variable of non-reference type that is usable in constant 20063 // expressions and has no mutable subobjects [FIXME], and e is an 20064 // element of the set of potential results of an expression of 20065 // non-volatile-qualified non-class type to which the lvalue-to-rvalue 20066 // conversion is applied 20067 // -- x is a variable of non-reference type, and e is an element of the set 20068 // of potential results of a discarded-value expression to which the 20069 // lvalue-to-rvalue conversion is not applied [FIXME] 20070 // 20071 // We check the first part of the second bullet here, and 20072 // Sema::CheckLValueToRValueConversionOperand deals with the second part. 20073 // FIXME: To get the third bullet right, we need to delay this even for 20074 // variables that are not usable in constant expressions. 20075 20076 // If we already know this isn't an odr-use, there's nothing more to do. 20077 if (DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(E)) 20078 if (DRE->isNonOdrUse()) 20079 return; 20080 if (MemberExpr *ME = dyn_cast_or_null<MemberExpr>(E)) 20081 if (ME->isNonOdrUse()) 20082 return; 20083 20084 switch (OdrUse) { 20085 case OdrUseContext::None: 20086 // In some cases, a variable may not have been marked unevaluated, if it 20087 // appears in a defaukt initializer. 20088 assert((!E || isa<FunctionParmPackExpr>(E) || 20089 SemaRef.isUnevaluatedContext()) && 20090 "missing non-odr-use marking for unevaluated decl ref"); 20091 break; 20092 20093 case OdrUseContext::FormallyOdrUsed: 20094 // FIXME: Ignoring formal odr-uses results in incorrect lambda capture 20095 // behavior. 20096 break; 20097 20098 case OdrUseContext::Used: 20099 // If we might later find that this expression isn't actually an odr-use, 20100 // delay the marking. 20101 if (E && Var->isUsableInConstantExpressions(SemaRef.Context)) 20102 SemaRef.MaybeODRUseExprs.insert(E); 20103 else 20104 MarkVarDeclODRUsed(Var, Loc, SemaRef); 20105 break; 20106 20107 case OdrUseContext::Dependent: 20108 // If this is a dependent context, we don't need to mark variables as 20109 // odr-used, but we may still need to track them for lambda capture. 20110 // FIXME: Do we also need to do this inside dependent typeid expressions 20111 // (which are modeled as unevaluated at this point)? 20112 DoMarkPotentialCapture(SemaRef, Loc, Var, E); 20113 break; 20114 } 20115 } 20116 20117 static void DoMarkBindingDeclReferenced(Sema &SemaRef, SourceLocation Loc, 20118 BindingDecl *BD, Expr *E) { 20119 BD->setReferenced(); 20120 20121 if (BD->isInvalidDecl()) 20122 return; 20123 20124 OdrUseContext OdrUse = isOdrUseContext(SemaRef); 20125 if (OdrUse == OdrUseContext::Used) { 20126 QualType CaptureType, DeclRefType; 20127 SemaRef.tryCaptureVariable(BD, Loc, TryCaptureKind::Implicit, 20128 /*EllipsisLoc*/ SourceLocation(), 20129 /*BuildAndDiagnose*/ true, CaptureType, 20130 DeclRefType, 20131 /*FunctionScopeIndexToStopAt*/ nullptr); 20132 } else if (OdrUse == OdrUseContext::Dependent) { 20133 DoMarkPotentialCapture(SemaRef, Loc, BD, E); 20134 } 20135 } 20136 20137 void Sema::MarkVariableReferenced(SourceLocation Loc, VarDecl *Var) { 20138 DoMarkVarDeclReferenced(*this, Loc, Var, nullptr, RefsMinusAssignments); 20139 } 20140 20141 // C++ [temp.dep.expr]p3: 20142 // An id-expression is type-dependent if it contains: 20143 // - an identifier associated by name lookup with an entity captured by copy 20144 // in a lambda-expression that has an explicit object parameter whose type 20145 // is dependent ([dcl.fct]), 20146 static void FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter( 20147 Sema &SemaRef, ValueDecl *D, Expr *E) { 20148 auto *ID = dyn_cast<DeclRefExpr>(E); 20149 if (!ID || ID->isTypeDependent() || !ID->refersToEnclosingVariableOrCapture()) 20150 return; 20151 20152 // If any enclosing lambda with a dependent explicit object parameter either 20153 // explicitly captures the variable by value, or has a capture default of '=' 20154 // and does not capture the variable by reference, then the type of the DRE 20155 // is dependent on the type of that lambda's explicit object parameter. 20156 auto IsDependent = [&]() { 20157 for (auto *Scope : llvm::reverse(SemaRef.FunctionScopes)) { 20158 auto *LSI = dyn_cast<sema::LambdaScopeInfo>(Scope); 20159 if (!LSI) 20160 continue; 20161 20162 if (LSI->Lambda && !LSI->Lambda->Encloses(SemaRef.CurContext) && 20163 LSI->AfterParameterList) 20164 return false; 20165 20166 const auto *MD = LSI->CallOperator; 20167 if (MD->getType().isNull()) 20168 continue; 20169 20170 const auto *Ty = MD->getType()->getAs<FunctionProtoType>(); 20171 if (!Ty || !MD->isExplicitObjectMemberFunction() || 20172 !Ty->getParamType(0)->isDependentType()) 20173 continue; 20174 20175 if (auto *C = LSI->CaptureMap.count(D) ? &LSI->getCapture(D) : nullptr) { 20176 if (C->isCopyCapture()) 20177 return true; 20178 continue; 20179 } 20180 20181 if (LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByval) 20182 return true; 20183 } 20184 return false; 20185 }(); 20186 20187 ID->setCapturedByCopyInLambdaWithExplicitObjectParameter( 20188 IsDependent, SemaRef.getASTContext()); 20189 } 20190 20191 static void 20192 MarkExprReferenced(Sema &SemaRef, SourceLocation Loc, Decl *D, Expr *E, 20193 bool MightBeOdrUse, 20194 llvm::DenseMap<const VarDecl *, int> &RefsMinusAssignments) { 20195 if (SemaRef.OpenMP().isInOpenMPDeclareTargetContext()) 20196 SemaRef.OpenMP().checkDeclIsAllowedInOpenMPTarget(E, D); 20197 20198 if (SemaRef.getLangOpts().OpenACC) 20199 SemaRef.OpenACC().CheckDeclReference(Loc, E, D); 20200 20201 if (VarDecl *Var = dyn_cast<VarDecl>(D)) { 20202 DoMarkVarDeclReferenced(SemaRef, Loc, Var, E, RefsMinusAssignments); 20203 if (SemaRef.getLangOpts().CPlusPlus) 20204 FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter(SemaRef, 20205 Var, E); 20206 return; 20207 } 20208 20209 if (BindingDecl *Decl = dyn_cast<BindingDecl>(D)) { 20210 DoMarkBindingDeclReferenced(SemaRef, Loc, Decl, E); 20211 if (SemaRef.getLangOpts().CPlusPlus) 20212 FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter(SemaRef, 20213 Decl, E); 20214 return; 20215 } 20216 SemaRef.MarkAnyDeclReferenced(Loc, D, MightBeOdrUse); 20217 20218 // If this is a call to a method via a cast, also mark the method in the 20219 // derived class used in case codegen can devirtualize the call. 20220 const MemberExpr *ME = dyn_cast<MemberExpr>(E); 20221 if (!ME) 20222 return; 20223 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); 20224 if (!MD) 20225 return; 20226 // Only attempt to devirtualize if this is truly a virtual call. 20227 bool IsVirtualCall = MD->isVirtual() && 20228 ME->performsVirtualDispatch(SemaRef.getLangOpts()); 20229 if (!IsVirtualCall) 20230 return; 20231 20232 // If it's possible to devirtualize the call, mark the called function 20233 // referenced. 20234 CXXMethodDecl *DM = MD->getDevirtualizedMethod( 20235 ME->getBase(), SemaRef.getLangOpts().AppleKext); 20236 if (DM) 20237 SemaRef.MarkAnyDeclReferenced(Loc, DM, MightBeOdrUse); 20238 } 20239 20240 void Sema::MarkDeclRefReferenced(DeclRefExpr *E, const Expr *Base) { 20241 // [basic.def.odr] (CWG 1614) 20242 // A function is named by an expression or conversion [...] 20243 // unless it is a pure virtual function and either the expression is not an 20244 // id-expression naming the function with an explicitly qualified name or 20245 // the expression forms a pointer to member 20246 bool OdrUse = true; 20247 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(E->getDecl())) 20248 if (Method->isVirtual() && 20249 !Method->getDevirtualizedMethod(Base, getLangOpts().AppleKext)) 20250 OdrUse = false; 20251 20252 if (auto *FD = dyn_cast<FunctionDecl>(E->getDecl())) { 20253 if (!isUnevaluatedContext() && !isConstantEvaluatedContext() && 20254 !isImmediateFunctionContext() && 20255 !isCheckingDefaultArgumentOrInitializer() && 20256 FD->isImmediateFunction() && !RebuildingImmediateInvocation && 20257 !FD->isDependentContext()) 20258 ExprEvalContexts.back().ReferenceToConsteval.insert(E); 20259 } 20260 MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E, OdrUse, 20261 RefsMinusAssignments); 20262 } 20263 20264 void Sema::MarkMemberReferenced(MemberExpr *E) { 20265 // C++11 [basic.def.odr]p2: 20266 // A non-overloaded function whose name appears as a potentially-evaluated 20267 // expression or a member of a set of candidate functions, if selected by 20268 // overload resolution when referred to from a potentially-evaluated 20269 // expression, is odr-used, unless it is a pure virtual function and its 20270 // name is not explicitly qualified. 20271 bool MightBeOdrUse = true; 20272 if (E->performsVirtualDispatch(getLangOpts())) { 20273 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) 20274 if (Method->isPureVirtual()) 20275 MightBeOdrUse = false; 20276 } 20277 SourceLocation Loc = 20278 E->getMemberLoc().isValid() ? E->getMemberLoc() : E->getBeginLoc(); 20279 MarkExprReferenced(*this, Loc, E->getMemberDecl(), E, MightBeOdrUse, 20280 RefsMinusAssignments); 20281 } 20282 20283 void Sema::MarkFunctionParmPackReferenced(FunctionParmPackExpr *E) { 20284 for (ValueDecl *VD : *E) 20285 MarkExprReferenced(*this, E->getParameterPackLocation(), VD, E, true, 20286 RefsMinusAssignments); 20287 } 20288 20289 /// Perform marking for a reference to an arbitrary declaration. It 20290 /// marks the declaration referenced, and performs odr-use checking for 20291 /// functions and variables. This method should not be used when building a 20292 /// normal expression which refers to a variable. 20293 void Sema::MarkAnyDeclReferenced(SourceLocation Loc, Decl *D, 20294 bool MightBeOdrUse) { 20295 if (MightBeOdrUse) { 20296 if (auto *VD = dyn_cast<VarDecl>(D)) { 20297 MarkVariableReferenced(Loc, VD); 20298 return; 20299 } 20300 } 20301 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 20302 MarkFunctionReferenced(Loc, FD, MightBeOdrUse); 20303 return; 20304 } 20305 D->setReferenced(); 20306 } 20307 20308 namespace { 20309 // Mark all of the declarations used by a type as referenced. 20310 // FIXME: Not fully implemented yet! We need to have a better understanding 20311 // of when we're entering a context we should not recurse into. 20312 // FIXME: This is and EvaluatedExprMarker are more-or-less equivalent to 20313 // TreeTransforms rebuilding the type in a new context. Rather than 20314 // duplicating the TreeTransform logic, we should consider reusing it here. 20315 // Currently that causes problems when rebuilding LambdaExprs. 20316 class MarkReferencedDecls : public DynamicRecursiveASTVisitor { 20317 Sema &S; 20318 SourceLocation Loc; 20319 20320 public: 20321 MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) {} 20322 20323 bool TraverseTemplateArgument(const TemplateArgument &Arg) override; 20324 }; 20325 } 20326 20327 bool MarkReferencedDecls::TraverseTemplateArgument( 20328 const TemplateArgument &Arg) { 20329 { 20330 // A non-type template argument is a constant-evaluated context. 20331 EnterExpressionEvaluationContext Evaluated( 20332 S, Sema::ExpressionEvaluationContext::ConstantEvaluated); 20333 if (Arg.getKind() == TemplateArgument::Declaration) { 20334 if (Decl *D = Arg.getAsDecl()) 20335 S.MarkAnyDeclReferenced(Loc, D, true); 20336 } else if (Arg.getKind() == TemplateArgument::Expression) { 20337 S.MarkDeclarationsReferencedInExpr(Arg.getAsExpr(), false); 20338 } 20339 } 20340 20341 return DynamicRecursiveASTVisitor::TraverseTemplateArgument(Arg); 20342 } 20343 20344 void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { 20345 MarkReferencedDecls Marker(*this, Loc); 20346 Marker.TraverseType(T); 20347 } 20348 20349 namespace { 20350 /// Helper class that marks all of the declarations referenced by 20351 /// potentially-evaluated subexpressions as "referenced". 20352 class EvaluatedExprMarker : public UsedDeclVisitor<EvaluatedExprMarker> { 20353 public: 20354 typedef UsedDeclVisitor<EvaluatedExprMarker> Inherited; 20355 bool SkipLocalVariables; 20356 ArrayRef<const Expr *> StopAt; 20357 20358 EvaluatedExprMarker(Sema &S, bool SkipLocalVariables, 20359 ArrayRef<const Expr *> StopAt) 20360 : Inherited(S), SkipLocalVariables(SkipLocalVariables), StopAt(StopAt) {} 20361 20362 void visitUsedDecl(SourceLocation Loc, Decl *D) { 20363 S.MarkFunctionReferenced(Loc, cast<FunctionDecl>(D)); 20364 } 20365 20366 void Visit(Expr *E) { 20367 if (llvm::is_contained(StopAt, E)) 20368 return; 20369 Inherited::Visit(E); 20370 } 20371 20372 void VisitConstantExpr(ConstantExpr *E) { 20373 // Don't mark declarations within a ConstantExpression, as this expression 20374 // will be evaluated and folded to a value. 20375 } 20376 20377 void VisitDeclRefExpr(DeclRefExpr *E) { 20378 // If we were asked not to visit local variables, don't. 20379 if (SkipLocalVariables) { 20380 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) 20381 if (VD->hasLocalStorage()) 20382 return; 20383 } 20384 20385 // FIXME: This can trigger the instantiation of the initializer of a 20386 // variable, which can cause the expression to become value-dependent 20387 // or error-dependent. Do we need to propagate the new dependence bits? 20388 S.MarkDeclRefReferenced(E); 20389 } 20390 20391 void VisitMemberExpr(MemberExpr *E) { 20392 S.MarkMemberReferenced(E); 20393 Visit(E->getBase()); 20394 } 20395 }; 20396 } // namespace 20397 20398 void Sema::MarkDeclarationsReferencedInExpr(Expr *E, 20399 bool SkipLocalVariables, 20400 ArrayRef<const Expr*> StopAt) { 20401 EvaluatedExprMarker(*this, SkipLocalVariables, StopAt).Visit(E); 20402 } 20403 20404 /// Emit a diagnostic when statements are reachable. 20405 /// FIXME: check for reachability even in expressions for which we don't build a 20406 /// CFG (eg, in the initializer of a global or in a constant expression). 20407 /// For example, 20408 /// namespace { auto *p = new double[3][false ? (1, 2) : 3]; } 20409 bool Sema::DiagIfReachable(SourceLocation Loc, ArrayRef<const Stmt *> Stmts, 20410 const PartialDiagnostic &PD) { 20411 if (!Stmts.empty() && getCurFunctionOrMethodDecl()) { 20412 if (!FunctionScopes.empty()) 20413 FunctionScopes.back()->PossiblyUnreachableDiags.push_back( 20414 sema::PossiblyUnreachableDiag(PD, Loc, Stmts)); 20415 return true; 20416 } 20417 20418 // The initializer of a constexpr variable or of the first declaration of a 20419 // static data member is not syntactically a constant evaluated constant, 20420 // but nonetheless is always required to be a constant expression, so we 20421 // can skip diagnosing. 20422 // FIXME: Using the mangling context here is a hack. 20423 if (auto *VD = dyn_cast_or_null<VarDecl>( 20424 ExprEvalContexts.back().ManglingContextDecl)) { 20425 if (VD->isConstexpr() || 20426 (VD->isStaticDataMember() && VD->isFirstDecl() && !VD->isInline())) 20427 return false; 20428 // FIXME: For any other kind of variable, we should build a CFG for its 20429 // initializer and check whether the context in question is reachable. 20430 } 20431 20432 Diag(Loc, PD); 20433 return true; 20434 } 20435 20436 /// Emit a diagnostic that describes an effect on the run-time behavior 20437 /// of the program being compiled. 20438 /// 20439 /// This routine emits the given diagnostic when the code currently being 20440 /// type-checked is "potentially evaluated", meaning that there is a 20441 /// possibility that the code will actually be executable. Code in sizeof() 20442 /// expressions, code used only during overload resolution, etc., are not 20443 /// potentially evaluated. This routine will suppress such diagnostics or, 20444 /// in the absolutely nutty case of potentially potentially evaluated 20445 /// expressions (C++ typeid), queue the diagnostic to potentially emit it 20446 /// later. 20447 /// 20448 /// This routine should be used for all diagnostics that describe the run-time 20449 /// behavior of a program, such as passing a non-POD value through an ellipsis. 20450 /// Failure to do so will likely result in spurious diagnostics or failures 20451 /// during overload resolution or within sizeof/alignof/typeof/typeid. 20452 bool Sema::DiagRuntimeBehavior(SourceLocation Loc, ArrayRef<const Stmt*> Stmts, 20453 const PartialDiagnostic &PD) { 20454 20455 if (ExprEvalContexts.back().isDiscardedStatementContext()) 20456 return false; 20457 20458 switch (ExprEvalContexts.back().Context) { 20459 case ExpressionEvaluationContext::Unevaluated: 20460 case ExpressionEvaluationContext::UnevaluatedList: 20461 case ExpressionEvaluationContext::UnevaluatedAbstract: 20462 case ExpressionEvaluationContext::DiscardedStatement: 20463 // The argument will never be evaluated, so don't complain. 20464 break; 20465 20466 case ExpressionEvaluationContext::ConstantEvaluated: 20467 case ExpressionEvaluationContext::ImmediateFunctionContext: 20468 // Relevant diagnostics should be produced by constant evaluation. 20469 break; 20470 20471 case ExpressionEvaluationContext::PotentiallyEvaluated: 20472 case ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed: 20473 return DiagIfReachable(Loc, Stmts, PD); 20474 } 20475 20476 return false; 20477 } 20478 20479 bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *Statement, 20480 const PartialDiagnostic &PD) { 20481 return DiagRuntimeBehavior( 20482 Loc, Statement ? llvm::ArrayRef(Statement) : llvm::ArrayRef<Stmt *>(), 20483 PD); 20484 } 20485 20486 bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, 20487 CallExpr *CE, FunctionDecl *FD) { 20488 if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) 20489 return false; 20490 20491 // If we're inside a decltype's expression, don't check for a valid return 20492 // type or construct temporaries until we know whether this is the last call. 20493 if (ExprEvalContexts.back().ExprContext == 20494 ExpressionEvaluationContextRecord::EK_Decltype) { 20495 ExprEvalContexts.back().DelayedDecltypeCalls.push_back(CE); 20496 return false; 20497 } 20498 20499 class CallReturnIncompleteDiagnoser : public TypeDiagnoser { 20500 FunctionDecl *FD; 20501 CallExpr *CE; 20502 20503 public: 20504 CallReturnIncompleteDiagnoser(FunctionDecl *FD, CallExpr *CE) 20505 : FD(FD), CE(CE) { } 20506 20507 void diagnose(Sema &S, SourceLocation Loc, QualType T) override { 20508 if (!FD) { 20509 S.Diag(Loc, diag::err_call_incomplete_return) 20510 << T << CE->getSourceRange(); 20511 return; 20512 } 20513 20514 S.Diag(Loc, diag::err_call_function_incomplete_return) 20515 << CE->getSourceRange() << FD << T; 20516 S.Diag(FD->getLocation(), diag::note_entity_declared_at) 20517 << FD->getDeclName(); 20518 } 20519 } Diagnoser(FD, CE); 20520 20521 if (RequireCompleteType(Loc, ReturnType, Diagnoser)) 20522 return true; 20523 20524 return false; 20525 } 20526 20527 // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses 20528 // will prevent this condition from triggering, which is what we want. 20529 void Sema::DiagnoseAssignmentAsCondition(Expr *E) { 20530 SourceLocation Loc; 20531 20532 unsigned diagnostic = diag::warn_condition_is_assignment; 20533 bool IsOrAssign = false; 20534 20535 if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { 20536 if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) 20537 return; 20538 20539 IsOrAssign = Op->getOpcode() == BO_OrAssign; 20540 20541 // Greylist some idioms by putting them into a warning subcategory. 20542 if (ObjCMessageExpr *ME 20543 = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { 20544 Selector Sel = ME->getSelector(); 20545 20546 // self = [<foo> init...] 20547 if (ObjC().isSelfExpr(Op->getLHS()) && ME->getMethodFamily() == OMF_init) 20548 diagnostic = diag::warn_condition_is_idiomatic_assignment; 20549 20550 // <foo> = [<bar> nextObject] 20551 else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") 20552 diagnostic = diag::warn_condition_is_idiomatic_assignment; 20553 } 20554 20555 Loc = Op->getOperatorLoc(); 20556 } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { 20557 if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) 20558 return; 20559 20560 IsOrAssign = Op->getOperator() == OO_PipeEqual; 20561 Loc = Op->getOperatorLoc(); 20562 } else if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) 20563 return DiagnoseAssignmentAsCondition(POE->getSyntacticForm()); 20564 else { 20565 // Not an assignment. 20566 return; 20567 } 20568 20569 Diag(Loc, diagnostic) << E->getSourceRange(); 20570 20571 SourceLocation Open = E->getBeginLoc(); 20572 SourceLocation Close = getLocForEndOfToken(E->getSourceRange().getEnd()); 20573 Diag(Loc, diag::note_condition_assign_silence) 20574 << FixItHint::CreateInsertion(Open, "(") 20575 << FixItHint::CreateInsertion(Close, ")"); 20576 20577 if (IsOrAssign) 20578 Diag(Loc, diag::note_condition_or_assign_to_comparison) 20579 << FixItHint::CreateReplacement(Loc, "!="); 20580 else 20581 Diag(Loc, diag::note_condition_assign_to_comparison) 20582 << FixItHint::CreateReplacement(Loc, "=="); 20583 } 20584 20585 void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *ParenE) { 20586 // Don't warn if the parens came from a macro. 20587 SourceLocation parenLoc = ParenE->getBeginLoc(); 20588 if (parenLoc.isInvalid() || parenLoc.isMacroID()) 20589 return; 20590 // Don't warn for dependent expressions. 20591 if (ParenE->isTypeDependent()) 20592 return; 20593 20594 Expr *E = ParenE->IgnoreParens(); 20595 if (ParenE->isProducedByFoldExpansion() && ParenE->getSubExpr() == E) 20596 return; 20597 20598 if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) 20599 if (opE->getOpcode() == BO_EQ && 20600 opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) 20601 == Expr::MLV_Valid) { 20602 SourceLocation Loc = opE->getOperatorLoc(); 20603 20604 Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); 20605 SourceRange ParenERange = ParenE->getSourceRange(); 20606 Diag(Loc, diag::note_equality_comparison_silence) 20607 << FixItHint::CreateRemoval(ParenERange.getBegin()) 20608 << FixItHint::CreateRemoval(ParenERange.getEnd()); 20609 Diag(Loc, diag::note_equality_comparison_to_assign) 20610 << FixItHint::CreateReplacement(Loc, "="); 20611 } 20612 } 20613 20614 ExprResult Sema::CheckBooleanCondition(SourceLocation Loc, Expr *E, 20615 bool IsConstexpr) { 20616 DiagnoseAssignmentAsCondition(E); 20617 if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) 20618 DiagnoseEqualityWithExtraParens(parenE); 20619 20620 ExprResult result = CheckPlaceholderExpr(E); 20621 if (result.isInvalid()) return ExprError(); 20622 E = result.get(); 20623 20624 if (!E->isTypeDependent()) { 20625 if (getLangOpts().CPlusPlus) 20626 return CheckCXXBooleanCondition(E, IsConstexpr); // C++ 6.4p4 20627 20628 ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); 20629 if (ERes.isInvalid()) 20630 return ExprError(); 20631 E = ERes.get(); 20632 20633 QualType T = E->getType(); 20634 if (!T->isScalarType()) { // C99 6.8.4.1p1 20635 Diag(Loc, diag::err_typecheck_statement_requires_scalar) 20636 << T << E->getSourceRange(); 20637 return ExprError(); 20638 } 20639 CheckBoolLikeConversion(E, Loc); 20640 } 20641 20642 return E; 20643 } 20644 20645 Sema::ConditionResult Sema::ActOnCondition(Scope *S, SourceLocation Loc, 20646 Expr *SubExpr, ConditionKind CK, 20647 bool MissingOK) { 20648 // MissingOK indicates whether having no condition expression is valid 20649 // (for loop) or invalid (e.g. while loop). 20650 if (!SubExpr) 20651 return MissingOK ? ConditionResult() : ConditionError(); 20652 20653 ExprResult Cond; 20654 switch (CK) { 20655 case ConditionKind::Boolean: 20656 Cond = CheckBooleanCondition(Loc, SubExpr); 20657 break; 20658 20659 case ConditionKind::ConstexprIf: 20660 // Note: this might produce a FullExpr 20661 Cond = CheckBooleanCondition(Loc, SubExpr, true); 20662 break; 20663 20664 case ConditionKind::Switch: 20665 Cond = CheckSwitchCondition(Loc, SubExpr); 20666 break; 20667 } 20668 if (Cond.isInvalid()) { 20669 Cond = CreateRecoveryExpr(SubExpr->getBeginLoc(), SubExpr->getEndLoc(), 20670 {SubExpr}, PreferredConditionType(CK)); 20671 if (!Cond.get()) 20672 return ConditionError(); 20673 } else if (Cond.isUsable() && !isa<FullExpr>(Cond.get())) 20674 Cond = ActOnFinishFullExpr(Cond.get(), Loc, /*DiscardedValue*/ false); 20675 20676 if (!Cond.isUsable()) 20677 return ConditionError(); 20678 20679 return ConditionResult(*this, nullptr, Cond, 20680 CK == ConditionKind::ConstexprIf); 20681 } 20682 20683 namespace { 20684 /// A visitor for rebuilding a call to an __unknown_any expression 20685 /// to have an appropriate type. 20686 struct RebuildUnknownAnyFunction 20687 : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { 20688 20689 Sema &S; 20690 20691 RebuildUnknownAnyFunction(Sema &S) : S(S) {} 20692 20693 ExprResult VisitStmt(Stmt *S) { 20694 llvm_unreachable("unexpected statement!"); 20695 } 20696 20697 ExprResult VisitExpr(Expr *E) { 20698 S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_call) 20699 << E->getSourceRange(); 20700 return ExprError(); 20701 } 20702 20703 /// Rebuild an expression which simply semantically wraps another 20704 /// expression which it shares the type and value kind of. 20705 template <class T> ExprResult rebuildSugarExpr(T *E) { 20706 ExprResult SubResult = Visit(E->getSubExpr()); 20707 if (SubResult.isInvalid()) return ExprError(); 20708 20709 Expr *SubExpr = SubResult.get(); 20710 E->setSubExpr(SubExpr); 20711 E->setType(SubExpr->getType()); 20712 E->setValueKind(SubExpr->getValueKind()); 20713 assert(E->getObjectKind() == OK_Ordinary); 20714 return E; 20715 } 20716 20717 ExprResult VisitParenExpr(ParenExpr *E) { 20718 return rebuildSugarExpr(E); 20719 } 20720 20721 ExprResult VisitUnaryExtension(UnaryOperator *E) { 20722 return rebuildSugarExpr(E); 20723 } 20724 20725 ExprResult VisitUnaryAddrOf(UnaryOperator *E) { 20726 ExprResult SubResult = Visit(E->getSubExpr()); 20727 if (SubResult.isInvalid()) return ExprError(); 20728 20729 Expr *SubExpr = SubResult.get(); 20730 E->setSubExpr(SubExpr); 20731 E->setType(S.Context.getPointerType(SubExpr->getType())); 20732 assert(E->isPRValue()); 20733 assert(E->getObjectKind() == OK_Ordinary); 20734 return E; 20735 } 20736 20737 ExprResult resolveDecl(Expr *E, ValueDecl *VD) { 20738 if (!isa<FunctionDecl>(VD)) return VisitExpr(E); 20739 20740 E->setType(VD->getType()); 20741 20742 assert(E->isPRValue()); 20743 if (S.getLangOpts().CPlusPlus && 20744 !(isa<CXXMethodDecl>(VD) && 20745 cast<CXXMethodDecl>(VD)->isInstance())) 20746 E->setValueKind(VK_LValue); 20747 20748 return E; 20749 } 20750 20751 ExprResult VisitMemberExpr(MemberExpr *E) { 20752 return resolveDecl(E, E->getMemberDecl()); 20753 } 20754 20755 ExprResult VisitDeclRefExpr(DeclRefExpr *E) { 20756 return resolveDecl(E, E->getDecl()); 20757 } 20758 }; 20759 } 20760 20761 /// Given a function expression of unknown-any type, try to rebuild it 20762 /// to have a function type. 20763 static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { 20764 ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); 20765 if (Result.isInvalid()) return ExprError(); 20766 return S.DefaultFunctionArrayConversion(Result.get()); 20767 } 20768 20769 namespace { 20770 /// A visitor for rebuilding an expression of type __unknown_anytype 20771 /// into one which resolves the type directly on the referring 20772 /// expression. Strict preservation of the original source 20773 /// structure is not a goal. 20774 struct RebuildUnknownAnyExpr 20775 : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { 20776 20777 Sema &S; 20778 20779 /// The current destination type. 20780 QualType DestType; 20781 20782 RebuildUnknownAnyExpr(Sema &S, QualType CastType) 20783 : S(S), DestType(CastType) {} 20784 20785 ExprResult VisitStmt(Stmt *S) { 20786 llvm_unreachable("unexpected statement!"); 20787 } 20788 20789 ExprResult VisitExpr(Expr *E) { 20790 S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) 20791 << E->getSourceRange(); 20792 return ExprError(); 20793 } 20794 20795 ExprResult VisitCallExpr(CallExpr *E); 20796 ExprResult VisitObjCMessageExpr(ObjCMessageExpr *E); 20797 20798 /// Rebuild an expression which simply semantically wraps another 20799 /// expression which it shares the type and value kind of. 20800 template <class T> ExprResult rebuildSugarExpr(T *E) { 20801 ExprResult SubResult = Visit(E->getSubExpr()); 20802 if (SubResult.isInvalid()) return ExprError(); 20803 Expr *SubExpr = SubResult.get(); 20804 E->setSubExpr(SubExpr); 20805 E->setType(SubExpr->getType()); 20806 E->setValueKind(SubExpr->getValueKind()); 20807 assert(E->getObjectKind() == OK_Ordinary); 20808 return E; 20809 } 20810 20811 ExprResult VisitParenExpr(ParenExpr *E) { 20812 return rebuildSugarExpr(E); 20813 } 20814 20815 ExprResult VisitUnaryExtension(UnaryOperator *E) { 20816 return rebuildSugarExpr(E); 20817 } 20818 20819 ExprResult VisitUnaryAddrOf(UnaryOperator *E) { 20820 const PointerType *Ptr = DestType->getAs<PointerType>(); 20821 if (!Ptr) { 20822 S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof) 20823 << E->getSourceRange(); 20824 return ExprError(); 20825 } 20826 20827 if (isa<CallExpr>(E->getSubExpr())) { 20828 S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof_call) 20829 << E->getSourceRange(); 20830 return ExprError(); 20831 } 20832 20833 assert(E->isPRValue()); 20834 assert(E->getObjectKind() == OK_Ordinary); 20835 E->setType(DestType); 20836 20837 // Build the sub-expression as if it were an object of the pointee type. 20838 DestType = Ptr->getPointeeType(); 20839 ExprResult SubResult = Visit(E->getSubExpr()); 20840 if (SubResult.isInvalid()) return ExprError(); 20841 E->setSubExpr(SubResult.get()); 20842 return E; 20843 } 20844 20845 ExprResult VisitImplicitCastExpr(ImplicitCastExpr *E); 20846 20847 ExprResult resolveDecl(Expr *E, ValueDecl *VD); 20848 20849 ExprResult VisitMemberExpr(MemberExpr *E) { 20850 return resolveDecl(E, E->getMemberDecl()); 20851 } 20852 20853 ExprResult VisitDeclRefExpr(DeclRefExpr *E) { 20854 return resolveDecl(E, E->getDecl()); 20855 } 20856 }; 20857 } 20858 20859 /// Rebuilds a call expression which yielded __unknown_anytype. 20860 ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { 20861 Expr *CalleeExpr = E->getCallee(); 20862 20863 enum FnKind { 20864 FK_MemberFunction, 20865 FK_FunctionPointer, 20866 FK_BlockPointer 20867 }; 20868 20869 FnKind Kind; 20870 QualType CalleeType = CalleeExpr->getType(); 20871 if (CalleeType == S.Context.BoundMemberTy) { 20872 assert(isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E)); 20873 Kind = FK_MemberFunction; 20874 CalleeType = Expr::findBoundMemberType(CalleeExpr); 20875 } else if (const PointerType *Ptr = CalleeType->getAs<PointerType>()) { 20876 CalleeType = Ptr->getPointeeType(); 20877 Kind = FK_FunctionPointer; 20878 } else { 20879 CalleeType = CalleeType->castAs<BlockPointerType>()->getPointeeType(); 20880 Kind = FK_BlockPointer; 20881 } 20882 const FunctionType *FnType = CalleeType->castAs<FunctionType>(); 20883 20884 // Verify that this is a legal result type of a function. 20885 if ((DestType->isArrayType() && !S.getLangOpts().allowArrayReturnTypes()) || 20886 DestType->isFunctionType()) { 20887 unsigned diagID = diag::err_func_returning_array_function; 20888 if (Kind == FK_BlockPointer) 20889 diagID = diag::err_block_returning_array_function; 20890 20891 S.Diag(E->getExprLoc(), diagID) 20892 << DestType->isFunctionType() << DestType; 20893 return ExprError(); 20894 } 20895 20896 // Otherwise, go ahead and set DestType as the call's result. 20897 E->setType(DestType.getNonLValueExprType(S.Context)); 20898 E->setValueKind(Expr::getValueKindForType(DestType)); 20899 assert(E->getObjectKind() == OK_Ordinary); 20900 20901 // Rebuild the function type, replacing the result type with DestType. 20902 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType); 20903 if (Proto) { 20904 // __unknown_anytype(...) is a special case used by the debugger when 20905 // it has no idea what a function's signature is. 20906 // 20907 // We want to build this call essentially under the K&R 20908 // unprototyped rules, but making a FunctionNoProtoType in C++ 20909 // would foul up all sorts of assumptions. However, we cannot 20910 // simply pass all arguments as variadic arguments, nor can we 20911 // portably just call the function under a non-variadic type; see 20912 // the comment on IR-gen's TargetInfo::isNoProtoCallVariadic. 20913 // However, it turns out that in practice it is generally safe to 20914 // call a function declared as "A foo(B,C,D);" under the prototype 20915 // "A foo(B,C,D,...);". The only known exception is with the 20916 // Windows ABI, where any variadic function is implicitly cdecl 20917 // regardless of its normal CC. Therefore we change the parameter 20918 // types to match the types of the arguments. 20919 // 20920 // This is a hack, but it is far superior to moving the 20921 // corresponding target-specific code from IR-gen to Sema/AST. 20922 20923 ArrayRef<QualType> ParamTypes = Proto->getParamTypes(); 20924 SmallVector<QualType, 8> ArgTypes; 20925 if (ParamTypes.empty() && Proto->isVariadic()) { // the special case 20926 ArgTypes.reserve(E->getNumArgs()); 20927 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 20928 ArgTypes.push_back(S.Context.getReferenceQualifiedType(E->getArg(i))); 20929 } 20930 ParamTypes = ArgTypes; 20931 } 20932 DestType = S.Context.getFunctionType(DestType, ParamTypes, 20933 Proto->getExtProtoInfo()); 20934 } else { 20935 DestType = S.Context.getFunctionNoProtoType(DestType, 20936 FnType->getExtInfo()); 20937 } 20938 20939 // Rebuild the appropriate pointer-to-function type. 20940 switch (Kind) { 20941 case FK_MemberFunction: 20942 // Nothing to do. 20943 break; 20944 20945 case FK_FunctionPointer: 20946 DestType = S.Context.getPointerType(DestType); 20947 break; 20948 20949 case FK_BlockPointer: 20950 DestType = S.Context.getBlockPointerType(DestType); 20951 break; 20952 } 20953 20954 // Finally, we can recurse. 20955 ExprResult CalleeResult = Visit(CalleeExpr); 20956 if (!CalleeResult.isUsable()) return ExprError(); 20957 E->setCallee(CalleeResult.get()); 20958 20959 // Bind a temporary if necessary. 20960 return S.MaybeBindToTemporary(E); 20961 } 20962 20963 ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { 20964 // Verify that this is a legal result type of a call. 20965 if (DestType->isArrayType() || DestType->isFunctionType()) { 20966 S.Diag(E->getExprLoc(), diag::err_func_returning_array_function) 20967 << DestType->isFunctionType() << DestType; 20968 return ExprError(); 20969 } 20970 20971 // Rewrite the method result type if available. 20972 if (ObjCMethodDecl *Method = E->getMethodDecl()) { 20973 assert(Method->getReturnType() == S.Context.UnknownAnyTy); 20974 Method->setReturnType(DestType); 20975 } 20976 20977 // Change the type of the message. 20978 E->setType(DestType.getNonReferenceType()); 20979 E->setValueKind(Expr::getValueKindForType(DestType)); 20980 20981 return S.MaybeBindToTemporary(E); 20982 } 20983 20984 ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { 20985 // The only case we should ever see here is a function-to-pointer decay. 20986 if (E->getCastKind() == CK_FunctionToPointerDecay) { 20987 assert(E->isPRValue()); 20988 assert(E->getObjectKind() == OK_Ordinary); 20989 20990 E->setType(DestType); 20991 20992 // Rebuild the sub-expression as the pointee (function) type. 20993 DestType = DestType->castAs<PointerType>()->getPointeeType(); 20994 20995 ExprResult Result = Visit(E->getSubExpr()); 20996 if (!Result.isUsable()) return ExprError(); 20997 20998 E->setSubExpr(Result.get()); 20999 return E; 21000 } else if (E->getCastKind() == CK_LValueToRValue) { 21001 assert(E->isPRValue()); 21002 assert(E->getObjectKind() == OK_Ordinary); 21003 21004 assert(isa<BlockPointerType>(E->getType())); 21005 21006 E->setType(DestType); 21007 21008 // The sub-expression has to be a lvalue reference, so rebuild it as such. 21009 DestType = S.Context.getLValueReferenceType(DestType); 21010 21011 ExprResult Result = Visit(E->getSubExpr()); 21012 if (!Result.isUsable()) return ExprError(); 21013 21014 E->setSubExpr(Result.get()); 21015 return E; 21016 } else { 21017 llvm_unreachable("Unhandled cast type!"); 21018 } 21019 } 21020 21021 ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { 21022 ExprValueKind ValueKind = VK_LValue; 21023 QualType Type = DestType; 21024 21025 // We know how to make this work for certain kinds of decls: 21026 21027 // - functions 21028 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(VD)) { 21029 if (const PointerType *Ptr = Type->getAs<PointerType>()) { 21030 DestType = Ptr->getPointeeType(); 21031 ExprResult Result = resolveDecl(E, VD); 21032 if (Result.isInvalid()) return ExprError(); 21033 return S.ImpCastExprToType(Result.get(), Type, CK_FunctionToPointerDecay, 21034 VK_PRValue); 21035 } 21036 21037 if (!Type->isFunctionType()) { 21038 S.Diag(E->getExprLoc(), diag::err_unknown_any_function) 21039 << VD << E->getSourceRange(); 21040 return ExprError(); 21041 } 21042 if (const FunctionProtoType *FT = Type->getAs<FunctionProtoType>()) { 21043 // We must match the FunctionDecl's type to the hack introduced in 21044 // RebuildUnknownAnyExpr::VisitCallExpr to vararg functions of unknown 21045 // type. See the lengthy commentary in that routine. 21046 QualType FDT = FD->getType(); 21047 const FunctionType *FnType = FDT->castAs<FunctionType>(); 21048 const FunctionProtoType *Proto = dyn_cast_or_null<FunctionProtoType>(FnType); 21049 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); 21050 if (DRE && Proto && Proto->getParamTypes().empty() && Proto->isVariadic()) { 21051 SourceLocation Loc = FD->getLocation(); 21052 FunctionDecl *NewFD = FunctionDecl::Create( 21053 S.Context, FD->getDeclContext(), Loc, Loc, 21054 FD->getNameInfo().getName(), DestType, FD->getTypeSourceInfo(), 21055 SC_None, S.getCurFPFeatures().isFPConstrained(), 21056 false /*isInlineSpecified*/, FD->hasPrototype(), 21057 /*ConstexprKind*/ ConstexprSpecKind::Unspecified); 21058 21059 if (FD->getQualifier()) 21060 NewFD->setQualifierInfo(FD->getQualifierLoc()); 21061 21062 SmallVector<ParmVarDecl*, 16> Params; 21063 for (const auto &AI : FT->param_types()) { 21064 ParmVarDecl *Param = 21065 S.BuildParmVarDeclForTypedef(FD, Loc, AI); 21066 Param->setScopeInfo(0, Params.size()); 21067 Params.push_back(Param); 21068 } 21069 NewFD->setParams(Params); 21070 DRE->setDecl(NewFD); 21071 VD = DRE->getDecl(); 21072 } 21073 } 21074 21075 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) 21076 if (MD->isInstance()) { 21077 ValueKind = VK_PRValue; 21078 Type = S.Context.BoundMemberTy; 21079 } 21080 21081 // Function references aren't l-values in C. 21082 if (!S.getLangOpts().CPlusPlus) 21083 ValueKind = VK_PRValue; 21084 21085 // - variables 21086 } else if (isa<VarDecl>(VD)) { 21087 if (const ReferenceType *RefTy = Type->getAs<ReferenceType>()) { 21088 Type = RefTy->getPointeeType(); 21089 } else if (Type->isFunctionType()) { 21090 S.Diag(E->getExprLoc(), diag::err_unknown_any_var_function_type) 21091 << VD << E->getSourceRange(); 21092 return ExprError(); 21093 } 21094 21095 // - nothing else 21096 } else { 21097 S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_decl) 21098 << VD << E->getSourceRange(); 21099 return ExprError(); 21100 } 21101 21102 // Modifying the declaration like this is friendly to IR-gen but 21103 // also really dangerous. 21104 VD->setType(DestType); 21105 E->setType(Type); 21106 E->setValueKind(ValueKind); 21107 return E; 21108 } 21109 21110 ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, 21111 Expr *CastExpr, CastKind &CastKind, 21112 ExprValueKind &VK, CXXCastPath &Path) { 21113 // The type we're casting to must be either void or complete. 21114 if (!CastType->isVoidType() && 21115 RequireCompleteType(TypeRange.getBegin(), CastType, 21116 diag::err_typecheck_cast_to_incomplete)) 21117 return ExprError(); 21118 21119 // Rewrite the casted expression from scratch. 21120 ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); 21121 if (!result.isUsable()) return ExprError(); 21122 21123 CastExpr = result.get(); 21124 VK = CastExpr->getValueKind(); 21125 CastKind = CK_NoOp; 21126 21127 return CastExpr; 21128 } 21129 21130 ExprResult Sema::forceUnknownAnyToType(Expr *E, QualType ToType) { 21131 return RebuildUnknownAnyExpr(*this, ToType).Visit(E); 21132 } 21133 21134 ExprResult Sema::checkUnknownAnyArg(SourceLocation callLoc, 21135 Expr *arg, QualType ¶mType) { 21136 // If the syntactic form of the argument is not an explicit cast of 21137 // any sort, just do default argument promotion. 21138 ExplicitCastExpr *castArg = dyn_cast<ExplicitCastExpr>(arg->IgnoreParens()); 21139 if (!castArg) { 21140 ExprResult result = DefaultArgumentPromotion(arg); 21141 if (result.isInvalid()) return ExprError(); 21142 paramType = result.get()->getType(); 21143 return result; 21144 } 21145 21146 // Otherwise, use the type that was written in the explicit cast. 21147 assert(!arg->hasPlaceholderType()); 21148 paramType = castArg->getTypeAsWritten(); 21149 21150 // Copy-initialize a parameter of that type. 21151 InitializedEntity entity = 21152 InitializedEntity::InitializeParameter(Context, paramType, 21153 /*consumed*/ false); 21154 return PerformCopyInitialization(entity, callLoc, arg); 21155 } 21156 21157 static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { 21158 Expr *orig = E; 21159 unsigned diagID = diag::err_uncasted_use_of_unknown_any; 21160 while (true) { 21161 E = E->IgnoreParenImpCasts(); 21162 if (CallExpr *call = dyn_cast<CallExpr>(E)) { 21163 E = call->getCallee(); 21164 diagID = diag::err_uncasted_call_of_unknown_any; 21165 } else { 21166 break; 21167 } 21168 } 21169 21170 SourceLocation loc; 21171 NamedDecl *d; 21172 if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(E)) { 21173 loc = ref->getLocation(); 21174 d = ref->getDecl(); 21175 } else if (MemberExpr *mem = dyn_cast<MemberExpr>(E)) { 21176 loc = mem->getMemberLoc(); 21177 d = mem->getMemberDecl(); 21178 } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(E)) { 21179 diagID = diag::err_uncasted_call_of_unknown_any; 21180 loc = msg->getSelectorStartLoc(); 21181 d = msg->getMethodDecl(); 21182 if (!d) { 21183 S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) 21184 << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() 21185 << orig->getSourceRange(); 21186 return ExprError(); 21187 } 21188 } else { 21189 S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) 21190 << E->getSourceRange(); 21191 return ExprError(); 21192 } 21193 21194 S.Diag(loc, diagID) << d << orig->getSourceRange(); 21195 21196 // Never recoverable. 21197 return ExprError(); 21198 } 21199 21200 ExprResult Sema::CheckPlaceholderExpr(Expr *E) { 21201 const BuiltinType *placeholderType = E->getType()->getAsPlaceholderType(); 21202 if (!placeholderType) return E; 21203 21204 switch (placeholderType->getKind()) { 21205 case BuiltinType::UnresolvedTemplate: { 21206 auto *ULE = cast<UnresolvedLookupExpr>(E); 21207 const DeclarationNameInfo &NameInfo = ULE->getNameInfo(); 21208 // There's only one FoundDecl for UnresolvedTemplate type. See 21209 // BuildTemplateIdExpr. 21210 NamedDecl *Temp = *ULE->decls_begin(); 21211 const bool IsTypeAliasTemplateDecl = isa<TypeAliasTemplateDecl>(Temp); 21212 21213 NestedNameSpecifier *NNS = ULE->getQualifierLoc().getNestedNameSpecifier(); 21214 // FIXME: AssumedTemplate is not very appropriate for error recovery here, 21215 // as it models only the unqualified-id case, where this case can clearly be 21216 // qualified. Thus we can't just qualify an assumed template. 21217 TemplateName TN; 21218 if (auto *TD = dyn_cast<TemplateDecl>(Temp)) 21219 TN = Context.getQualifiedTemplateName(NNS, ULE->hasTemplateKeyword(), 21220 TemplateName(TD)); 21221 else 21222 TN = Context.getAssumedTemplateName(NameInfo.getName()); 21223 21224 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_type_template) 21225 << TN << ULE->getSourceRange() << IsTypeAliasTemplateDecl; 21226 Diag(Temp->getLocation(), diag::note_referenced_type_template) 21227 << IsTypeAliasTemplateDecl; 21228 21229 TemplateArgumentListInfo TAL(ULE->getLAngleLoc(), ULE->getRAngleLoc()); 21230 bool HasAnyDependentTA = false; 21231 for (const TemplateArgumentLoc &Arg : ULE->template_arguments()) { 21232 HasAnyDependentTA |= Arg.getArgument().isDependent(); 21233 TAL.addArgument(Arg); 21234 } 21235 21236 QualType TST; 21237 { 21238 SFINAETrap Trap(*this); 21239 TST = CheckTemplateIdType(TN, NameInfo.getBeginLoc(), TAL); 21240 } 21241 if (TST.isNull()) 21242 TST = Context.getTemplateSpecializationType( 21243 TN, ULE->template_arguments(), /*CanonicalArgs=*/{}, 21244 HasAnyDependentTA ? Context.DependentTy : Context.IntTy); 21245 QualType ET = 21246 Context.getElaboratedType(ElaboratedTypeKeyword::None, NNS, TST); 21247 return CreateRecoveryExpr(NameInfo.getBeginLoc(), NameInfo.getEndLoc(), {}, 21248 ET); 21249 } 21250 21251 // Overloaded expressions. 21252 case BuiltinType::Overload: { 21253 // Try to resolve a single function template specialization. 21254 // This is obligatory. 21255 ExprResult Result = E; 21256 if (ResolveAndFixSingleFunctionTemplateSpecialization(Result, false)) 21257 return Result; 21258 21259 // No guarantees that ResolveAndFixSingleFunctionTemplateSpecialization 21260 // leaves Result unchanged on failure. 21261 Result = E; 21262 if (resolveAndFixAddressOfSingleOverloadCandidate(Result)) 21263 return Result; 21264 21265 // If that failed, try to recover with a call. 21266 tryToRecoverWithCall(Result, PDiag(diag::err_ovl_unresolvable), 21267 /*complain*/ true); 21268 return Result; 21269 } 21270 21271 // Bound member functions. 21272 case BuiltinType::BoundMember: { 21273 ExprResult result = E; 21274 const Expr *BME = E->IgnoreParens(); 21275 PartialDiagnostic PD = PDiag(diag::err_bound_member_function); 21276 // Try to give a nicer diagnostic if it is a bound member that we recognize. 21277 if (isa<CXXPseudoDestructorExpr>(BME)) { 21278 PD = PDiag(diag::err_dtor_expr_without_call) << /*pseudo-destructor*/ 1; 21279 } else if (const auto *ME = dyn_cast<MemberExpr>(BME)) { 21280 if (ME->getMemberNameInfo().getName().getNameKind() == 21281 DeclarationName::CXXDestructorName) 21282 PD = PDiag(diag::err_dtor_expr_without_call) << /*destructor*/ 0; 21283 } 21284 tryToRecoverWithCall(result, PD, 21285 /*complain*/ true); 21286 return result; 21287 } 21288 21289 // ARC unbridged casts. 21290 case BuiltinType::ARCUnbridgedCast: { 21291 Expr *realCast = ObjC().stripARCUnbridgedCast(E); 21292 ObjC().diagnoseARCUnbridgedCast(realCast); 21293 return realCast; 21294 } 21295 21296 // Expressions of unknown type. 21297 case BuiltinType::UnknownAny: 21298 return diagnoseUnknownAnyExpr(*this, E); 21299 21300 // Pseudo-objects. 21301 case BuiltinType::PseudoObject: 21302 return PseudoObject().checkRValue(E); 21303 21304 case BuiltinType::BuiltinFn: { 21305 // Accept __noop without parens by implicitly converting it to a call expr. 21306 auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()); 21307 if (DRE) { 21308 auto *FD = cast<FunctionDecl>(DRE->getDecl()); 21309 unsigned BuiltinID = FD->getBuiltinID(); 21310 if (BuiltinID == Builtin::BI__noop) { 21311 E = ImpCastExprToType(E, Context.getPointerType(FD->getType()), 21312 CK_BuiltinFnToFnPtr) 21313 .get(); 21314 return CallExpr::Create(Context, E, /*Args=*/{}, Context.IntTy, 21315 VK_PRValue, SourceLocation(), 21316 FPOptionsOverride()); 21317 } 21318 21319 if (Context.BuiltinInfo.isInStdNamespace(BuiltinID)) { 21320 // Any use of these other than a direct call is ill-formed as of C++20, 21321 // because they are not addressable functions. In earlier language 21322 // modes, warn and force an instantiation of the real body. 21323 Diag(E->getBeginLoc(), 21324 getLangOpts().CPlusPlus20 21325 ? diag::err_use_of_unaddressable_function 21326 : diag::warn_cxx20_compat_use_of_unaddressable_function); 21327 if (FD->isImplicitlyInstantiable()) { 21328 // Require a definition here because a normal attempt at 21329 // instantiation for a builtin will be ignored, and we won't try 21330 // again later. We assume that the definition of the template 21331 // precedes this use. 21332 InstantiateFunctionDefinition(E->getBeginLoc(), FD, 21333 /*Recursive=*/false, 21334 /*DefinitionRequired=*/true, 21335 /*AtEndOfTU=*/false); 21336 } 21337 // Produce a properly-typed reference to the function. 21338 CXXScopeSpec SS; 21339 SS.Adopt(DRE->getQualifierLoc()); 21340 TemplateArgumentListInfo TemplateArgs; 21341 DRE->copyTemplateArgumentsInto(TemplateArgs); 21342 return BuildDeclRefExpr( 21343 FD, FD->getType(), VK_LValue, DRE->getNameInfo(), 21344 DRE->hasQualifier() ? &SS : nullptr, DRE->getFoundDecl(), 21345 DRE->getTemplateKeywordLoc(), 21346 DRE->hasExplicitTemplateArgs() ? &TemplateArgs : nullptr); 21347 } 21348 } 21349 21350 Diag(E->getBeginLoc(), diag::err_builtin_fn_use); 21351 return ExprError(); 21352 } 21353 21354 case BuiltinType::IncompleteMatrixIdx: 21355 Diag(cast<MatrixSubscriptExpr>(E->IgnoreParens()) 21356 ->getRowIdx() 21357 ->getBeginLoc(), 21358 diag::err_matrix_incomplete_index); 21359 return ExprError(); 21360 21361 // Expressions of unknown type. 21362 case BuiltinType::ArraySection: 21363 Diag(E->getBeginLoc(), diag::err_array_section_use) 21364 << cast<ArraySectionExpr>(E)->isOMPArraySection(); 21365 return ExprError(); 21366 21367 // Expressions of unknown type. 21368 case BuiltinType::OMPArrayShaping: 21369 return ExprError(Diag(E->getBeginLoc(), diag::err_omp_array_shaping_use)); 21370 21371 case BuiltinType::OMPIterator: 21372 return ExprError(Diag(E->getBeginLoc(), diag::err_omp_iterator_use)); 21373 21374 // Everything else should be impossible. 21375 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 21376 case BuiltinType::Id: 21377 #include "clang/Basic/OpenCLImageTypes.def" 21378 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 21379 case BuiltinType::Id: 21380 #include "clang/Basic/OpenCLExtensionTypes.def" 21381 #define SVE_TYPE(Name, Id, SingletonId) \ 21382 case BuiltinType::Id: 21383 #include "clang/Basic/AArch64ACLETypes.def" 21384 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 21385 case BuiltinType::Id: 21386 #include "clang/Basic/PPCTypes.def" 21387 #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: 21388 #include "clang/Basic/RISCVVTypes.def" 21389 #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: 21390 #include "clang/Basic/WebAssemblyReferenceTypes.def" 21391 #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id: 21392 #include "clang/Basic/AMDGPUTypes.def" 21393 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: 21394 #include "clang/Basic/HLSLIntangibleTypes.def" 21395 #define BUILTIN_TYPE(Id, SingletonId) case BuiltinType::Id: 21396 #define PLACEHOLDER_TYPE(Id, SingletonId) 21397 #include "clang/AST/BuiltinTypes.def" 21398 break; 21399 } 21400 21401 llvm_unreachable("invalid placeholder type!"); 21402 } 21403 21404 bool Sema::CheckCaseExpression(Expr *E) { 21405 if (E->isTypeDependent()) 21406 return true; 21407 if (E->isValueDependent() || E->isIntegerConstantExpr(Context)) 21408 return E->getType()->isIntegralOrEnumerationType(); 21409 return false; 21410 } 21411 21412 ExprResult Sema::CreateRecoveryExpr(SourceLocation Begin, SourceLocation End, 21413 ArrayRef<Expr *> SubExprs, QualType T) { 21414 if (!Context.getLangOpts().RecoveryAST) 21415 return ExprError(); 21416 21417 if (isSFINAEContext()) 21418 return ExprError(); 21419 21420 if (T.isNull() || T->isUndeducedType() || 21421 !Context.getLangOpts().RecoveryASTType) 21422 // We don't know the concrete type, fallback to dependent type. 21423 T = Context.DependentTy; 21424 21425 return RecoveryExpr::Create(Context, T, Begin, End, SubExprs); 21426 } 21427