clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SemaDeclCXX.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include/clang/Sema -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../include -I /usr/src/gnu/usr.bin/clang/libclangSema/obj -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/lib/Sema/SemaDeclCXX.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "clang/AST/ASTConsumer.h" |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/ASTLambda.h" |
| 16 | #include "clang/AST/ASTMutationListener.h" |
| 17 | #include "clang/AST/CXXInheritance.h" |
| 18 | #include "clang/AST/CharUnits.h" |
| 19 | #include "clang/AST/ComparisonCategories.h" |
| 20 | #include "clang/AST/EvaluatedExprVisitor.h" |
| 21 | #include "clang/AST/ExprCXX.h" |
| 22 | #include "clang/AST/RecordLayout.h" |
| 23 | #include "clang/AST/RecursiveASTVisitor.h" |
| 24 | #include "clang/AST/StmtVisitor.h" |
| 25 | #include "clang/AST/TypeLoc.h" |
| 26 | #include "clang/AST/TypeOrdering.h" |
| 27 | #include "clang/Basic/AttributeCommonInfo.h" |
| 28 | #include "clang/Basic/PartialDiagnostic.h" |
| 29 | #include "clang/Basic/TargetInfo.h" |
| 30 | #include "clang/Lex/LiteralSupport.h" |
| 31 | #include "clang/Lex/Preprocessor.h" |
| 32 | #include "clang/Sema/CXXFieldCollector.h" |
| 33 | #include "clang/Sema/DeclSpec.h" |
| 34 | #include "clang/Sema/Initialization.h" |
| 35 | #include "clang/Sema/Lookup.h" |
| 36 | #include "clang/Sema/ParsedTemplate.h" |
| 37 | #include "clang/Sema/Scope.h" |
| 38 | #include "clang/Sema/ScopeInfo.h" |
| 39 | #include "clang/Sema/SemaInternal.h" |
| 40 | #include "clang/Sema/Template.h" |
| 41 | #include "llvm/ADT/ScopeExit.h" |
| 42 | #include "llvm/ADT/SmallString.h" |
| 43 | #include "llvm/ADT/STLExtras.h" |
| 44 | #include "llvm/ADT/StringExtras.h" |
| 45 | #include <map> |
| 46 | #include <set> |
| 47 | |
| 48 | using namespace clang; |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | namespace { |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | class CheckDefaultArgumentVisitor |
| 61 | : public ConstStmtVisitor<CheckDefaultArgumentVisitor, bool> { |
| 62 | Sema &S; |
| 63 | const Expr *DefaultArg; |
| 64 | |
| 65 | public: |
| 66 | CheckDefaultArgumentVisitor(Sema &S, const Expr *DefaultArg) |
| 67 | : S(S), DefaultArg(DefaultArg) {} |
| 68 | |
| 69 | bool VisitExpr(const Expr *Node); |
| 70 | bool VisitDeclRefExpr(const DeclRefExpr *DRE); |
| 71 | bool VisitCXXThisExpr(const CXXThisExpr *ThisE); |
| 72 | bool VisitLambdaExpr(const LambdaExpr *Lambda); |
| 73 | bool VisitPseudoObjectExpr(const PseudoObjectExpr *POE); |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | bool CheckDefaultArgumentVisitor::VisitExpr(const Expr *Node) { |
| 78 | bool IsInvalid = false; |
| 79 | for (const Stmt *SubStmt : Node->children()) |
| 80 | IsInvalid |= Visit(SubStmt); |
| 81 | return IsInvalid; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(const DeclRefExpr *DRE) { |
| 88 | const NamedDecl *Decl = DRE->getDecl(); |
| 89 | if (const auto *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | if (DRE->isNonOdrUse() != NOUR_Unevaluated) |
| 99 | return S.Diag(DRE->getBeginLoc(), |
| 100 | diag::err_param_default_argument_references_param) |
| 101 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
| 102 | } else if (const auto *VDecl = dyn_cast<VarDecl>(Decl)) { |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | if (VDecl->isLocalVarDecl() && !DRE->isNonOdrUse()) |
| 115 | return S.Diag(DRE->getBeginLoc(), |
| 116 | diag::err_param_default_argument_references_local) |
| 117 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
| 118 | } |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(const CXXThisExpr *ThisE) { |
| 125 | |
| 126 | |
| 127 | |
| 128 | return S.Diag(ThisE->getBeginLoc(), |
| 129 | diag::err_param_default_argument_references_this) |
| 130 | << ThisE->getSourceRange(); |
| 131 | } |
| 132 | |
| 133 | bool CheckDefaultArgumentVisitor::VisitPseudoObjectExpr( |
| 134 | const PseudoObjectExpr *POE) { |
| 135 | bool Invalid = false; |
| 136 | for (const Expr *E : POE->semantics()) { |
| 137 | |
| 138 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) { |
| 139 | E = OVE->getSourceExpr(); |
| 140 | assert(E && "pseudo-object binding without source expression?"); |
| 141 | } |
| 142 | |
| 143 | Invalid |= Visit(E); |
| 144 | } |
| 145 | return Invalid; |
| 146 | } |
| 147 | |
| 148 | bool CheckDefaultArgumentVisitor::VisitLambdaExpr(const LambdaExpr *Lambda) { |
| 149 | |
| 150 | |
| 151 | |
| 152 | if (Lambda->capture_begin() == Lambda->capture_end()) |
| 153 | return false; |
| 154 | |
| 155 | return S.Diag(Lambda->getBeginLoc(), diag::err_lambda_capture_default_arg); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | Sema::ImplicitExceptionSpecification::CalledDecl(SourceLocation CallLoc, |
| 161 | const CXXMethodDecl *Method) { |
| 162 | |
| 163 | if (!Method || ComputedEST == EST_MSAny) |
| 164 | return; |
| 165 | |
| 166 | const FunctionProtoType *Proto |
| 167 | = Method->getType()->getAs<FunctionProtoType>(); |
| 168 | Proto = Self->ResolveExceptionSpec(CallLoc, Proto); |
| 169 | if (!Proto) |
| 170 | return; |
| 171 | |
| 172 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
| 173 | |
| 174 | |
| 175 | if (ComputedEST == EST_None) |
| 176 | return; |
| 177 | |
| 178 | if (EST == EST_None && Method->hasAttr<NoThrowAttr>()) |
| 179 | EST = EST_BasicNoexcept; |
| 180 | |
| 181 | switch (EST) { |
| 182 | case EST_Unparsed: |
| 183 | case EST_Uninstantiated: |
| 184 | case EST_Unevaluated: |
| 185 | llvm_unreachable("should not see unresolved exception specs here"); |
| 186 | |
| 187 | |
| 188 | case EST_MSAny: |
| 189 | case EST_None: |
| 190 | |
| 191 | |
| 192 | ClearExceptions(); |
| 193 | ComputedEST = EST; |
| 194 | return; |
| 195 | case EST_NoexceptFalse: |
| 196 | ClearExceptions(); |
| 197 | ComputedEST = EST_None; |
| 198 | return; |
| 199 | |
| 200 | |
| 201 | |
| 202 | case EST_BasicNoexcept: |
| 203 | case EST_NoexceptTrue: |
| 204 | case EST_NoThrow: |
| 205 | return; |
| 206 | |
| 207 | |
| 208 | case EST_DynamicNone: |
| 209 | if (ComputedEST == EST_BasicNoexcept) |
| 210 | ComputedEST = EST_DynamicNone; |
| 211 | return; |
| 212 | case EST_DependentNoexcept: |
| 213 | llvm_unreachable( |
| 214 | "should not generate implicit declarations for dependent cases"); |
| 215 | case EST_Dynamic: |
| 216 | break; |
| 217 | } |
| 218 | assert(EST == EST_Dynamic && "EST case not considered earlier."); |
| 219 | assert(ComputedEST != EST_None && |
| 220 | "Shouldn't collect exceptions when throw-all is guaranteed."); |
| 221 | ComputedEST = EST_Dynamic; |
| 222 | |
| 223 | for (const auto &E : Proto->exceptions()) |
| 224 | if (ExceptionsSeen.insert(Self->Context.getCanonicalType(E)).second) |
| 225 | Exceptions.push_back(E); |
| 226 | } |
| 227 | |
| 228 | void Sema::ImplicitExceptionSpecification::CalledStmt(Stmt *S) { |
| 229 | if (!S || ComputedEST == EST_MSAny) |
| 230 | return; |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | if (Self->canThrow(S)) |
| 254 | ComputedEST = EST_None; |
| 255 | } |
| 256 | |
| 257 | ExprResult Sema::ConvertParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
| 258 | SourceLocation EqualLoc) { |
| 259 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 260 | diag::err_typecheck_decl_incomplete_type)) |
| 261 | return true; |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 270 | Param); |
| 271 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 272 | EqualLoc); |
| 273 | InitializationSequence InitSeq(*this, Entity, Kind, Arg); |
| 274 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Arg); |
| 275 | if (Result.isInvalid()) |
| 276 | return true; |
| 277 | Arg = Result.getAs<Expr>(); |
| 278 | |
| 279 | CheckCompletedExpr(Arg, EqualLoc); |
| 280 | Arg = MaybeCreateExprWithCleanups(Arg); |
| 281 | |
| 282 | return Arg; |
| 283 | } |
| 284 | |
| 285 | void Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
| 286 | SourceLocation EqualLoc) { |
| 287 | |
| 288 | Param->setDefaultArg(Arg); |
| 289 | |
| 290 | |
| 291 | |
| 292 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 293 | = UnparsedDefaultArgInstantiations.find(Param); |
| 294 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 295 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 296 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 297 | |
| 298 | |
| 299 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | void |
| 307 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
| 308 | Expr *DefaultArg) { |
| 309 | if (!param || !DefaultArg) |
| 310 | return; |
| 311 | |
| 312 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
| 313 | UnparsedDefaultArgLocs.erase(Param); |
| 314 | |
| 315 | auto Fail = [&] { |
| 316 | Param->setInvalidDecl(); |
| 317 | Param->setDefaultArg(new (Context) OpaqueValueExpr( |
| 318 | EqualLoc, Param->getType().getNonReferenceType(), VK_PRValue)); |
| 319 | }; |
| 320 | |
| 321 | |
| 322 | if (!getLangOpts().CPlusPlus) { |
| 323 | Diag(EqualLoc, diag::err_param_default_argument) |
| 324 | << DefaultArg->getSourceRange(); |
| 325 | return Fail(); |
| 326 | } |
| 327 | |
| 328 | |
| 329 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 330 | return Fail(); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | if (Param->isParameterPack()) { |
| 337 | Diag(EqualLoc, diag::err_param_default_argument_on_parameter_pack) |
| 338 | << DefaultArg->getSourceRange(); |
| 339 | |
| 340 | Param->setDefaultArg(nullptr); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | ExprResult Result = ConvertParamDefaultArgument(Param, DefaultArg, EqualLoc); |
| 345 | if (Result.isInvalid()) |
| 346 | return Fail(); |
| 347 | |
| 348 | DefaultArg = Result.getAs<Expr>(); |
| 349 | |
| 350 | |
| 351 | CheckDefaultArgumentVisitor DefaultArgChecker(*this, DefaultArg); |
| 352 | if (DefaultArgChecker.Visit(DefaultArg)) |
| 353 | return Fail(); |
| 354 | |
| 355 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
| 356 | } |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
| 363 | SourceLocation EqualLoc, |
| 364 | SourceLocation ArgLoc) { |
| 365 | if (!param) |
| 366 | return; |
| 367 | |
| 368 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
| 369 | Param->setUnparsedDefaultArg(); |
| 370 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | |
| 375 | void Sema::ActOnParamDefaultArgumentError(Decl *param, |
| 376 | SourceLocation EqualLoc) { |
| 377 | if (!param) |
| 378 | return; |
| 379 | |
| 380 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
| 381 | Param->setInvalidDecl(); |
| 382 | UnparsedDefaultArgLocs.erase(Param); |
| 383 | Param->setDefaultArg(new (Context) OpaqueValueExpr( |
| 384 | EqualLoc, Param->getType().getNonReferenceType(), VK_PRValue)); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | bool MightBeFunction = D.isFunctionDeclarationContext(); |
| 401 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 402 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 403 | if (chunk.Kind == DeclaratorChunk::Function) { |
| 404 | if (MightBeFunction) { |
| 405 | |
| 406 | |
| 407 | |
| 408 | MightBeFunction = false; |
| 409 | continue; |
| 410 | } |
| 411 | for (unsigned argIdx = 0, e = chunk.Fun.NumParams; argIdx != e; |
| 412 | ++argIdx) { |
| 413 | ParmVarDecl *Param = cast<ParmVarDecl>(chunk.Fun.Params[argIdx].Param); |
| 414 | if (Param->hasUnparsedDefaultArg()) { |
| 415 | std::unique_ptr<CachedTokens> Toks = |
| 416 | std::move(chunk.Fun.Params[argIdx].DefaultArgTokens); |
| 417 | SourceRange SR; |
| 418 | if (Toks->size() > 1) |
| 419 | SR = SourceRange((*Toks)[1].getLocation(), |
| 420 | Toks->back().getLocation()); |
| 421 | else |
| 422 | SR = UnparsedDefaultArgLocs[Param]; |
| 423 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 424 | << SR; |
| 425 | } else if (Param->getDefaultArg()) { |
| 426 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 427 | << Param->getDefaultArg()->getSourceRange(); |
| 428 | Param->setDefaultArg(nullptr); |
| 429 | } |
| 430 | } |
| 431 | } else if (chunk.Kind != DeclaratorChunk::Paren) { |
| 432 | MightBeFunction = false; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | static bool functionDeclHasDefaultArgument(const FunctionDecl *FD) { |
| 438 | return std::any_of(FD->param_begin(), FD->param_end(), [](ParmVarDecl *P) { |
| 439 | return P->hasDefaultArg() && !P->hasInheritedDefaultArg(); |
| 440 | }); |
| 441 | } |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old, |
| 448 | Scope *S) { |
| 449 | bool Invalid = false; |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | DeclContext *ScopeDC = New->isLocalExternDecl() |
| 455 | ? New->getLexicalDeclContext() |
| 456 | : New->getDeclContext(); |
| 457 | |
| 458 | |
| 459 | FunctionDecl *PrevForDefaultArgs = Old; |
| 460 | for (; PrevForDefaultArgs; |
| 461 | |
| 462 | |
| 463 | PrevForDefaultArgs = New->isLocalExternDecl() |
| 464 | ? nullptr |
| 465 | : PrevForDefaultArgs->getPreviousDecl()) { |
| 466 | |
| 467 | if (!LookupResult::isVisible(*this, PrevForDefaultArgs)) |
| 468 | continue; |
| 469 | |
| 470 | if (S && !isDeclInScope(PrevForDefaultArgs, ScopeDC, S) && |
| 471 | !New->isCXXClassMember()) { |
| 472 | |
| 473 | |
| 474 | |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | if (PrevForDefaultArgs->isLocalExternDecl() != New->isLocalExternDecl()) { |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | for (unsigned p = 0, NumParams = PrevForDefaultArgs |
| 509 | ? PrevForDefaultArgs->getNumParams() |
| 510 | : 0; |
| 511 | p < NumParams; ++p) { |
| 512 | ParmVarDecl *OldParam = PrevForDefaultArgs->getParamDecl(p); |
| 513 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 514 | |
| 515 | bool OldParamHasDfl = OldParam ? OldParam->hasDefaultArg() : false; |
| 516 | bool NewParamHasDfl = NewParam->hasDefaultArg(); |
| 517 | |
| 518 | if (OldParamHasDfl && NewParamHasDfl) { |
| 519 | unsigned DiagDefaultParamID = |
| 520 | diag::err_param_default_argument_redefinition; |
| 521 | |
| 522 | |
| 523 | |
| 524 | Invalid = true; |
| 525 | if (getLangOpts().MicrosoftExt) { |
| 526 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(New); |
| 527 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
| 528 | |
| 529 | NewParam->setHasInheritedDefaultArg(); |
| 530 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 531 | NewParam->setUninstantiatedDefaultArg( |
| 532 | OldParam->getUninstantiatedDefaultArg()); |
| 533 | else |
| 534 | NewParam->setDefaultArg(OldParam->getInit()); |
| 535 | DiagDefaultParamID = diag::ext_param_default_argument_redefinition; |
| 536 | Invalid = false; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
| 549 | << NewParam->getDefaultArgRange(); |
| 550 | |
| 551 | |
| 552 | |
| 553 | for (auto Older = PrevForDefaultArgs; |
| 554 | OldParam->hasInheritedDefaultArg(); ) { |
| 555 | Older = Older->getPreviousDecl(); |
| 556 | OldParam = Older->getParamDecl(p); |
| 557 | } |
| 558 | |
| 559 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 560 | << OldParam->getDefaultArgRange(); |
| 561 | } else if (OldParamHasDfl) { |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | if (New->getFriendObjectKind() == Decl::FOK_None || |
| 567 | !New->getLexicalDeclContext()->isDependentContext()) { |
| 568 | |
| 569 | |
| 570 | NewParam->setHasInheritedDefaultArg(); |
| 571 | if (OldParam->hasUnparsedDefaultArg()) |
| 572 | NewParam->setUnparsedDefaultArg(); |
| 573 | else if (OldParam->hasUninstantiatedDefaultArg()) |
| 574 | NewParam->setUninstantiatedDefaultArg( |
| 575 | OldParam->getUninstantiatedDefaultArg()); |
| 576 | else |
| 577 | NewParam->setDefaultArg(OldParam->getInit()); |
| 578 | } |
| 579 | } else if (NewParamHasDfl) { |
| 580 | if (New->getDescribedFunctionTemplate()) { |
| 581 | |
| 582 | Diag(NewParam->getLocation(), |
| 583 | diag::err_param_default_argument_template_redecl) |
| 584 | << NewParam->getDefaultArgRange(); |
| 585 | Diag(PrevForDefaultArgs->getLocation(), |
| 586 | diag::note_template_prev_declaration) |
| 587 | << false; |
| 588 | } else if (New->getTemplateSpecializationKind() |
| 589 | != TSK_ImplicitInstantiation && |
| 590 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 601 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 602 | << New->getDeclName() |
| 603 | << NewParam->getDefaultArgRange(); |
| 604 | } else if (New->getDeclContext()->isDependentContext()) { |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | int WhichKind = 2; |
| 615 | if (CXXRecordDecl *Record |
| 616 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 617 | if (Record->getDescribedClassTemplate()) |
| 618 | WhichKind = 0; |
| 619 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 620 | WhichKind = 1; |
| 621 | else |
| 622 | WhichKind = 2; |
| 623 | } |
| 624 | |
| 625 | Diag(NewParam->getLocation(), |
| 626 | diag::err_param_default_argument_member_template_redecl) |
| 627 | << WhichKind |
| 628 | << NewParam->getDefaultArgRange(); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | if (isa<CXXConstructorDecl>(New) && |
| 637 | New->getMinRequiredArguments() < Old->getMinRequiredArguments()) { |
| 638 | CXXSpecialMember NewSM = getSpecialMember(cast<CXXMethodDecl>(New)), |
| 639 | OldSM = getSpecialMember(cast<CXXMethodDecl>(Old)); |
| 640 | if (NewSM != OldSM) { |
| 641 | ParmVarDecl *NewParam = New->getParamDecl(New->getMinRequiredArguments()); |
| 642 | assert(NewParam->hasDefaultArg()); |
| 643 | Diag(NewParam->getLocation(), diag::err_default_arg_makes_ctor_special) |
| 644 | << NewParam->getDefaultArgRange() << NewSM; |
| 645 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | const FunctionDecl *Def; |
| 650 | |
| 651 | |
| 652 | |
| 653 | if (New->getConstexprKind() != Old->getConstexprKind()) { |
| 654 | Diag(New->getLocation(), diag::err_constexpr_redecl_mismatch) |
| 655 | << New << static_cast<int>(New->getConstexprKind()) |
| 656 | << static_cast<int>(Old->getConstexprKind()); |
| 657 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 658 | Invalid = true; |
| 659 | } else if (!Old->getMostRecentDecl()->isInlined() && New->isInlined() && |
| 660 | Old->isDefined(Def) && |
| 661 | |
| 662 | |
| 663 | |
| 664 | (New->isInlineSpecified() || |
| 665 | New->getFriendObjectKind() == Decl::FOK_None)) { |
| 666 | |
| 667 | |
| 668 | |
| 669 | Diag(New->getLocation(), diag::err_inline_decl_follows_def) << New; |
| 670 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 671 | Invalid = true; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | if (isa<CXXDeductionGuideDecl>(New) && |
| 679 | !New->isFunctionTemplateSpecialization() && isVisible(Old)) { |
| 680 | Diag(New->getLocation(), diag::err_deduction_guide_redeclared); |
| 681 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 682 | } |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | if (Old->getFriendObjectKind() == Decl::FOK_Undeclared && |
| 689 | functionDeclHasDefaultArgument(Old)) { |
| 690 | Diag(New->getLocation(), diag::err_friend_decl_with_def_arg_redeclared); |
| 691 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 692 | Invalid = true; |
| 693 | } |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | const FunctionDecl *OldDefinition = nullptr; |
| 702 | if (New->isThisDeclarationInstantiatedFromAFriendDefinition() && |
| 703 | Old->isDefined(OldDefinition, true)) |
| 704 | CheckForFunctionRedefinition(New, OldDefinition); |
| 705 | |
| 706 | return Invalid; |
| 707 | } |
| 708 | |
| 709 | NamedDecl * |
| 710 | Sema::ActOnDecompositionDeclarator(Scope *S, Declarator &D, |
| 711 | MultiTemplateParamsArg TemplateParamLists) { |
| 712 | assert(D.isDecompositionDeclarator()); |
| 713 | const DecompositionDeclarator &Decomp = D.getDecompositionDeclarator(); |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | if (!D.mayHaveDecompositionDeclarator()) { |
| 719 | Diag(Decomp.getLSquareLoc(), diag::err_decomp_decl_context) |
| 720 | << Decomp.getSourceRange(); |
| 721 | return nullptr; |
| 722 | } |
| 723 | |
| 724 | if (!TemplateParamLists.empty()) { |
| 725 | |
| 726 | |
| 727 | Diag(TemplateParamLists.front()->getTemplateLoc(), |
| 728 | diag::err_decomp_decl_template); |
| 729 | return nullptr; |
| 730 | } |
| 731 | |
| 732 | Diag(Decomp.getLSquareLoc(), |
| 733 | !getLangOpts().CPlusPlus17 |
| 734 | ? diag::ext_decomp_decl |
| 735 | : D.getContext() == DeclaratorContext::Condition |
| 736 | ? diag::ext_decomp_decl_cond |
| 737 | : diag::warn_cxx14_compat_decomp_decl) |
| 738 | << Decomp.getSourceRange(); |
| 739 | |
| 740 | |
| 741 | DeclContext *const DC = CurContext; |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | auto &DS = D.getDeclSpec(); |
| 750 | { |
| 751 | SmallVector<StringRef, 8> BadSpecifiers; |
| 752 | SmallVector<SourceLocation, 8> BadSpecifierLocs; |
| 753 | SmallVector<StringRef, 8> CPlusPlus20Specifiers; |
| 754 | SmallVector<SourceLocation, 8> CPlusPlus20SpecifierLocs; |
| 755 | if (auto SCS = DS.getStorageClassSpec()) { |
| 756 | if (SCS == DeclSpec::SCS_static) { |
| 757 | CPlusPlus20Specifiers.push_back(DeclSpec::getSpecifierName(SCS)); |
| 758 | CPlusPlus20SpecifierLocs.push_back(DS.getStorageClassSpecLoc()); |
| 759 | } else { |
| 760 | BadSpecifiers.push_back(DeclSpec::getSpecifierName(SCS)); |
| 761 | BadSpecifierLocs.push_back(DS.getStorageClassSpecLoc()); |
| 762 | } |
| 763 | } |
| 764 | if (auto TSCS = DS.getThreadStorageClassSpec()) { |
| 765 | CPlusPlus20Specifiers.push_back(DeclSpec::getSpecifierName(TSCS)); |
| 766 | CPlusPlus20SpecifierLocs.push_back(DS.getThreadStorageClassSpecLoc()); |
| 767 | } |
| 768 | if (DS.hasConstexprSpecifier()) { |
| 769 | BadSpecifiers.push_back( |
| 770 | DeclSpec::getSpecifierName(DS.getConstexprSpecifier())); |
| 771 | BadSpecifierLocs.push_back(DS.getConstexprSpecLoc()); |
| 772 | } |
| 773 | if (DS.isInlineSpecified()) { |
| 774 | BadSpecifiers.push_back("inline"); |
| 775 | BadSpecifierLocs.push_back(DS.getInlineSpecLoc()); |
| 776 | } |
| 777 | if (!BadSpecifiers.empty()) { |
| 778 | auto &&Err = Diag(BadSpecifierLocs.front(), diag::err_decomp_decl_spec); |
| 779 | Err << (int)BadSpecifiers.size() |
| 780 | << llvm::join(BadSpecifiers.begin(), BadSpecifiers.end(), " "); |
| 781 | |
| 782 | |
| 783 | for (auto Loc : BadSpecifierLocs) |
| 784 | Err << SourceRange(Loc, Loc); |
| 785 | } else if (!CPlusPlus20Specifiers.empty()) { |
| 786 | auto &&Warn = Diag(CPlusPlus20SpecifierLocs.front(), |
| 787 | getLangOpts().CPlusPlus20 |
| 788 | ? diag::warn_cxx17_compat_decomp_decl_spec |
| 789 | : diag::ext_decomp_decl_spec); |
| 790 | Warn << (int)CPlusPlus20Specifiers.size() |
| 791 | << llvm::join(CPlusPlus20Specifiers.begin(), |
| 792 | CPlusPlus20Specifiers.end(), " "); |
| 793 | for (auto Loc : CPlusPlus20SpecifierLocs) |
| 794 | Warn << SourceRange(Loc, Loc); |
| 795 | } |
| 796 | |
| 797 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 798 | return nullptr; |
| 799 | } |
| 800 | |
| 801 | |
| 802 | |
| 803 | if ((DS.getTypeQualifiers() & DeclSpec::TQ_volatile) && |
| 804 | getLangOpts().CPlusPlus20) |
| 805 | Diag(DS.getVolatileSpecLoc(), |
| 806 | diag::warn_deprecated_volatile_structured_binding); |
| 807 | |
| 808 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 809 | QualType R = TInfo->getType(); |
| 810 | |
| 811 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 812 | UPPC_DeclarationType)) |
| 813 | D.setInvalidType(); |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | if (DS.getTypeSpecType() != DeclSpec::TST_auto || |
| 819 | D.hasGroupingParens() || D.getNumTypeObjects() > 1 || |
| 820 | (D.getNumTypeObjects() == 1 && |
| 821 | D.getTypeObject(0).Kind != DeclaratorChunk::Reference)) { |
| 822 | Diag(Decomp.getLSquareLoc(), |
| 823 | (D.hasGroupingParens() || |
| 824 | (D.getNumTypeObjects() && |
| 825 | D.getTypeObject(0).Kind == DeclaratorChunk::Paren)) |
| 826 | ? diag::err_decomp_decl_parens |
| 827 | : diag::err_decomp_decl_type) |
| 828 | << R; |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | if (R->isFunctionType()) |
| 834 | D.setInvalidType(); |
| 835 | } |
| 836 | |
| 837 | |
| 838 | SmallVector<BindingDecl*, 8> Bindings; |
| 839 | |
| 840 | |
| 841 | for (auto &B : D.getDecompositionDeclarator().bindings()) { |
| 842 | |
| 843 | DeclarationNameInfo NameInfo(B.Name, B.NameLoc); |
| 844 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
| 845 | ForVisibleRedeclaration); |
| 846 | LookupName(Previous, S, |
| 847 | DC->getRedeclContext()->isTranslationUnit()); |
| 848 | |
| 849 | |
| 850 | if (Previous.isSingleResult() && |
| 851 | Previous.getFoundDecl()->isTemplateParameter()) { |
| 852 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
| 853 | Previous.getFoundDecl()); |
| 854 | Previous.clear(); |
| 855 | } |
| 856 | |
| 857 | auto *BD = BindingDecl::Create(Context, DC, B.NameLoc, B.Name); |
| 858 | |
| 859 | |
| 860 | NamedDecl *ShadowedDecl = D.getCXXScopeSpec().isEmpty() |
| 861 | ? getShadowedDeclaration(BD, Previous) |
| 862 | : nullptr; |
| 863 | |
| 864 | bool ConsiderLinkage = DC->isFunctionOrMethod() && |
| 865 | DS.getStorageClassSpec() == DeclSpec::SCS_extern; |
| 866 | FilterLookupForScope(Previous, DC, S, ConsiderLinkage, |
| 867 | false); |
| 868 | |
| 869 | if (!Previous.empty()) { |
| 870 | auto *Old = Previous.getRepresentativeDecl(); |
| 871 | Diag(B.NameLoc, diag::err_redefinition) << B.Name; |
| 872 | Diag(Old->getLocation(), diag::note_previous_definition); |
| 873 | } else if (ShadowedDecl && !D.isRedeclaration()) { |
| 874 | CheckShadow(BD, ShadowedDecl, Previous); |
| 875 | } |
| 876 | PushOnScopeChains(BD, S, true); |
| 877 | Bindings.push_back(BD); |
| 878 | ParsingInitForAutoVars.insert(BD); |
| 879 | } |
| 880 | |
| 881 | |
| 882 | |
| 883 | DeclarationNameInfo NameInfo((IdentifierInfo *)nullptr, |
| 884 | Decomp.getLSquareLoc()); |
| 885 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
| 886 | ForVisibleRedeclaration); |
| 887 | |
| 888 | |
| 889 | bool AddToScope = true; |
| 890 | NamedDecl *New = |
| 891 | ActOnVariableDeclarator(S, D, DC, TInfo, Previous, |
| 892 | MultiTemplateParamsArg(), AddToScope, Bindings); |
| 893 | if (AddToScope) { |
| 894 | S->AddDecl(New); |
| 895 | CurContext->addHiddenDecl(New); |
| 896 | } |
| 897 | |
| 898 | if (isInOpenMPDeclareTargetContext()) |
| 899 | checkDeclIsAllowedInOpenMPTarget(nullptr, New); |
| 900 | |
| 901 | return New; |
| 902 | } |
| 903 | |
| 904 | static bool checkSimpleDecomposition( |
| 905 | Sema &S, ArrayRef<BindingDecl *> Bindings, ValueDecl *Src, |
| 906 | QualType DecompType, const llvm::APSInt &NumElems, QualType ElemType, |
| 907 | llvm::function_ref<ExprResult(SourceLocation, Expr *, unsigned)> GetInit) { |
| 908 | if ((int64_t)Bindings.size() != NumElems) { |
| 909 | S.Diag(Src->getLocation(), diag::err_decomp_decl_wrong_number_bindings) |
| 910 | << DecompType << (unsigned)Bindings.size() |
| 911 | << (unsigned)NumElems.getLimitedValue(UINT_MAX) |
| 912 | << toString(NumElems, 10) << (NumElems < Bindings.size()); |
| 913 | return true; |
| 914 | } |
| 915 | |
| 916 | unsigned I = 0; |
| 917 | for (auto *B : Bindings) { |
| 918 | SourceLocation Loc = B->getLocation(); |
| 919 | ExprResult E = S.BuildDeclRefExpr(Src, DecompType, VK_LValue, Loc); |
| 920 | if (E.isInvalid()) |
| 921 | return true; |
| 922 | E = GetInit(Loc, E.get(), I++); |
| 923 | if (E.isInvalid()) |
| 924 | return true; |
| 925 | B->setBinding(ElemType, E.get()); |
| 926 | } |
| 927 | |
| 928 | return false; |
| 929 | } |
| 930 | |
| 931 | static bool checkArrayLikeDecomposition(Sema &S, |
| 932 | ArrayRef<BindingDecl *> Bindings, |
| 933 | ValueDecl *Src, QualType DecompType, |
| 934 | const llvm::APSInt &NumElems, |
| 935 | QualType ElemType) { |
| 936 | return checkSimpleDecomposition( |
| 937 | S, Bindings, Src, DecompType, NumElems, ElemType, |
| 938 | [&](SourceLocation Loc, Expr *Base, unsigned I) -> ExprResult { |
| 939 | ExprResult E = S.ActOnIntegerConstant(Loc, I); |
| 940 | if (E.isInvalid()) |
| 941 | return ExprError(); |
| 942 | return S.CreateBuiltinArraySubscriptExpr(Base, Loc, E.get(), Loc); |
| 943 | }); |
| 944 | } |
| 945 | |
| 946 | static bool checkArrayDecomposition(Sema &S, ArrayRef<BindingDecl*> Bindings, |
| 947 | ValueDecl *Src, QualType DecompType, |
| 948 | const ConstantArrayType *CAT) { |
| 949 | return checkArrayLikeDecomposition(S, Bindings, Src, DecompType, |
| 950 | llvm::APSInt(CAT->getSize()), |
| 951 | CAT->getElementType()); |
| 952 | } |
| 953 | |
| 954 | static bool checkVectorDecomposition(Sema &S, ArrayRef<BindingDecl*> Bindings, |
| 955 | ValueDecl *Src, QualType DecompType, |
| 956 | const VectorType *VT) { |
| 957 | return checkArrayLikeDecomposition( |
| 958 | S, Bindings, Src, DecompType, llvm::APSInt::get(VT->getNumElements()), |
| 959 | S.Context.getQualifiedType(VT->getElementType(), |
| 960 | DecompType.getQualifiers())); |
| 961 | } |
| 962 | |
| 963 | static bool checkComplexDecomposition(Sema &S, |
| 964 | ArrayRef<BindingDecl *> Bindings, |
| 965 | ValueDecl *Src, QualType DecompType, |
| 966 | const ComplexType *CT) { |
| 967 | return checkSimpleDecomposition( |
| 968 | S, Bindings, Src, DecompType, llvm::APSInt::get(2), |
| 969 | S.Context.getQualifiedType(CT->getElementType(), |
| 970 | DecompType.getQualifiers()), |
| 971 | [&](SourceLocation Loc, Expr *Base, unsigned I) -> ExprResult { |
| 972 | return S.CreateBuiltinUnaryOp(Loc, I ? UO_Imag : UO_Real, Base); |
| 973 | }); |
| 974 | } |
| 975 | |
| 976 | static std::string printTemplateArgs(const PrintingPolicy &PrintingPolicy, |
| 977 | TemplateArgumentListInfo &Args, |
| 978 | const TemplateParameterList *Params) { |
| 979 | SmallString<128> SS; |
| 980 | llvm::raw_svector_ostream OS(SS); |
| 981 | bool First = true; |
| 982 | unsigned I = 0; |
| 983 | for (auto &Arg : Args.arguments()) { |
| 984 | if (!First) |
| 985 | OS << ", "; |
| 986 | Arg.getArgument().print( |
| 987 | PrintingPolicy, OS, |
| 988 | TemplateParameterList::shouldIncludeTypeForArgument(Params, I)); |
| 989 | First = false; |
| 990 | I++; |
| 991 | } |
| 992 | return std::string(OS.str()); |
| 993 | } |
| 994 | |
| 995 | static bool lookupStdTypeTraitMember(Sema &S, LookupResult &TraitMemberLookup, |
| 996 | SourceLocation Loc, StringRef Trait, |
| 997 | TemplateArgumentListInfo &Args, |
| 998 | unsigned DiagID) { |
| 999 | auto DiagnoseMissing = [&] { |
| 1000 | if (DiagID) |
| 1001 | S.Diag(Loc, DiagID) << printTemplateArgs(S.Context.getPrintingPolicy(), |
| 1002 | Args, nullptr); |
| 1003 | return true; |
| 1004 | }; |
| 1005 | |
| 1006 | |
| 1007 | NamespaceDecl *Std = S.getStdNamespace(); |
| 1008 | if (!Std) |
| 1009 | return DiagnoseMissing(); |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | LookupResult Result(S, &S.PP.getIdentifierTable().get(Trait), |
| 1017 | Loc, Sema::LookupOrdinaryName); |
| 1018 | if (!S.LookupQualifiedName(Result, Std)) |
| 1019 | return DiagnoseMissing(); |
| 1020 | if (Result.isAmbiguous()) |
| 1021 | return true; |
| 1022 | |
| 1023 | ClassTemplateDecl *TraitTD = Result.getAsSingle<ClassTemplateDecl>(); |
| 1024 | if (!TraitTD) { |
| 1025 | Result.suppressDiagnostics(); |
| 1026 | NamedDecl *Found = *Result.begin(); |
| 1027 | S.Diag(Loc, diag::err_std_type_trait_not_class_template) << Trait; |
| 1028 | S.Diag(Found->getLocation(), diag::note_declared_at); |
| 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | QualType TraitTy = S.CheckTemplateIdType(TemplateName(TraitTD), Loc, Args); |
| 1034 | if (TraitTy.isNull()) |
| 1035 | return true; |
| 1036 | if (!S.isCompleteType(Loc, TraitTy)) { |
| 1037 | if (DiagID) |
| 1038 | S.RequireCompleteType( |
| 1039 | Loc, TraitTy, DiagID, |
| 1040 | printTemplateArgs(S.Context.getPrintingPolicy(), Args, |
| 1041 | TraitTD->getTemplateParameters())); |
| 1042 | return true; |
| 1043 | } |
| 1044 | |
| 1045 | CXXRecordDecl *RD = TraitTy->getAsCXXRecordDecl(); |
| 1046 | assert(RD && "specialization of class template is not a class?"); |
| 1047 | |
| 1048 | |
| 1049 | S.LookupQualifiedName(TraitMemberLookup, RD); |
| 1050 | return TraitMemberLookup.isAmbiguous(); |
| 1051 | } |
| 1052 | |
| 1053 | static TemplateArgumentLoc |
| 1054 | getTrivialIntegralTemplateArgument(Sema &S, SourceLocation Loc, QualType T, |
| 1055 | uint64_t I) { |
| 1056 | TemplateArgument Arg(S.Context, S.Context.MakeIntValue(I, T), T); |
| 1057 | return S.getTrivialTemplateArgumentLoc(Arg, T, Loc); |
| 1058 | } |
| 1059 | |
| 1060 | static TemplateArgumentLoc |
| 1061 | getTrivialTypeTemplateArgument(Sema &S, SourceLocation Loc, QualType T) { |
| 1062 | return S.getTrivialTemplateArgumentLoc(TemplateArgument(T), QualType(), Loc); |
| 1063 | } |
| 1064 | |
| 1065 | namespace { enum class IsTupleLike { TupleLike, NotTupleLike, Error }; } |
| 1066 | |
| 1067 | static IsTupleLike isTupleLike(Sema &S, SourceLocation Loc, QualType T, |
| 1068 | llvm::APSInt &Size) { |
| 1069 | EnterExpressionEvaluationContext ContextRAII( |
| 1070 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 1071 | |
| 1072 | DeclarationName Value = S.PP.getIdentifierInfo("value"); |
| 1073 | LookupResult R(S, Value, Loc, Sema::LookupOrdinaryName); |
| 1074 | |
| 1075 | |
| 1076 | TemplateArgumentListInfo Args(Loc, Loc); |
| 1077 | Args.addArgument(getTrivialTypeTemplateArgument(S, Loc, T)); |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | if (lookupStdTypeTraitMember(S, R, Loc, "tuple_size", Args, 0) || |
| 1082 | R.empty()) |
| 1083 | return IsTupleLike::NotTupleLike; |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | struct ICEDiagnoser : Sema::VerifyICEDiagnoser { |
| 1089 | LookupResult &R; |
| 1090 | TemplateArgumentListInfo &Args; |
| 1091 | ICEDiagnoser(LookupResult &R, TemplateArgumentListInfo &Args) |
| 1092 | : R(R), Args(Args) {} |
| 1093 | Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S, |
| 1094 | SourceLocation Loc) override { |
| 1095 | return S.Diag(Loc, diag::err_decomp_decl_std_tuple_size_not_constant) |
| 1096 | << printTemplateArgs(S.Context.getPrintingPolicy(), Args, |
| 1097 | nullptr); |
| 1098 | } |
| 1099 | } Diagnoser(R, Args); |
| 1100 | |
| 1101 | ExprResult E = |
| 1102 | S.BuildDeclarationNameExpr(CXXScopeSpec(), R, false); |
| 1103 | if (E.isInvalid()) |
| 1104 | return IsTupleLike::Error; |
| 1105 | |
| 1106 | E = S.VerifyIntegerConstantExpression(E.get(), &Size, Diagnoser); |
| 1107 | if (E.isInvalid()) |
| 1108 | return IsTupleLike::Error; |
| 1109 | |
| 1110 | return IsTupleLike::TupleLike; |
| 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | static QualType getTupleLikeElementType(Sema &S, SourceLocation Loc, |
| 1115 | unsigned I, QualType T) { |
| 1116 | |
| 1117 | TemplateArgumentListInfo Args(Loc, Loc); |
| 1118 | Args.addArgument( |
| 1119 | getTrivialIntegralTemplateArgument(S, Loc, S.Context.getSizeType(), I)); |
| 1120 | Args.addArgument(getTrivialTypeTemplateArgument(S, Loc, T)); |
| 1121 | |
| 1122 | DeclarationName TypeDN = S.PP.getIdentifierInfo("type"); |
| 1123 | LookupResult R(S, TypeDN, Loc, Sema::LookupOrdinaryName); |
| 1124 | if (lookupStdTypeTraitMember( |
| 1125 | S, R, Loc, "tuple_element", Args, |
| 1126 | diag::err_decomp_decl_std_tuple_element_not_specialized)) |
| 1127 | return QualType(); |
| 1128 | |
| 1129 | auto *TD = R.getAsSingle<TypeDecl>(); |
| 1130 | if (!TD) { |
| 1131 | R.suppressDiagnostics(); |
| 1132 | S.Diag(Loc, diag::err_decomp_decl_std_tuple_element_not_specialized) |
| 1133 | << printTemplateArgs(S.Context.getPrintingPolicy(), Args, |
| 1134 | nullptr); |
| 1135 | if (!R.empty()) |
| 1136 | S.Diag(R.getRepresentativeDecl()->getLocation(), diag::note_declared_at); |
| 1137 | return QualType(); |
| 1138 | } |
| 1139 | |
| 1140 | return S.Context.getTypeDeclType(TD); |
| 1141 | } |
| 1142 | |
| 1143 | namespace { |
| 1144 | struct InitializingBinding { |
| 1145 | Sema &S; |
| 1146 | InitializingBinding(Sema &S, BindingDecl *BD) : S(S) { |
| 1147 | Sema::CodeSynthesisContext Ctx; |
| 1148 | Ctx.Kind = Sema::CodeSynthesisContext::InitializingStructuredBinding; |
| 1149 | Ctx.PointOfInstantiation = BD->getLocation(); |
| 1150 | Ctx.Entity = BD; |
| 1151 | S.pushCodeSynthesisContext(Ctx); |
| 1152 | } |
| 1153 | ~InitializingBinding() { |
| 1154 | S.popCodeSynthesisContext(); |
| 1155 | } |
| 1156 | }; |
| 1157 | } |
| 1158 | |
| 1159 | static bool checkTupleLikeDecomposition(Sema &S, |
| 1160 | ArrayRef<BindingDecl *> Bindings, |
| 1161 | VarDecl *Src, QualType DecompType, |
| 1162 | const llvm::APSInt &TupleSize) { |
| 1163 | if ((int64_t)Bindings.size() != TupleSize) { |
| 1164 | S.Diag(Src->getLocation(), diag::err_decomp_decl_wrong_number_bindings) |
| 1165 | << DecompType << (unsigned)Bindings.size() |
| 1166 | << (unsigned)TupleSize.getLimitedValue(UINT_MAX) |
| 1167 | << toString(TupleSize, 10) << (TupleSize < Bindings.size()); |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
| 1171 | if (Bindings.empty()) |
| 1172 | return false; |
| 1173 | |
| 1174 | DeclarationName GetDN = S.PP.getIdentifierInfo("get"); |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | LookupResult MemberGet(S, GetDN, Src->getLocation(), Sema::LookupMemberName); |
| 1180 | bool UseMemberGet = false; |
| 1181 | if (S.isCompleteType(Src->getLocation(), DecompType)) { |
| 1182 | if (auto *RD = DecompType->getAsCXXRecordDecl()) |
| 1183 | S.LookupQualifiedName(MemberGet, RD); |
| 1184 | if (MemberGet.isAmbiguous()) |
| 1185 | return true; |
| 1186 | |
| 1187 | |
| 1188 | for (NamedDecl *D : MemberGet) { |
| 1189 | if (FunctionTemplateDecl *FTD = |
| 1190 | dyn_cast<FunctionTemplateDecl>(D->getUnderlyingDecl())) { |
| 1191 | TemplateParameterList *TPL = FTD->getTemplateParameters(); |
| 1192 | if (TPL->size() != 0 && |
| 1193 | isa<NonTypeTemplateParmDecl>(TPL->getParam(0))) { |
| 1194 | |
| 1195 | UseMemberGet = true; |
| 1196 | break; |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | unsigned I = 0; |
| 1203 | for (auto *B : Bindings) { |
| 1204 | InitializingBinding InitContext(S, B); |
| 1205 | SourceLocation Loc = B->getLocation(); |
| 1206 | |
| 1207 | ExprResult E = S.BuildDeclRefExpr(Src, DecompType, VK_LValue, Loc); |
| 1208 | if (E.isInvalid()) |
| 1209 | return true; |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | if (!Src->getType()->isLValueReferenceType()) |
| 1214 | E = ImplicitCastExpr::Create(S.Context, E.get()->getType(), CK_NoOp, |
| 1215 | E.get(), nullptr, VK_XValue, |
| 1216 | FPOptionsOverride()); |
| 1217 | |
| 1218 | TemplateArgumentListInfo Args(Loc, Loc); |
| 1219 | Args.addArgument( |
| 1220 | getTrivialIntegralTemplateArgument(S, Loc, S.Context.getSizeType(), I)); |
| 1221 | |
| 1222 | if (UseMemberGet) { |
| 1223 | |
| 1224 | |
| 1225 | E = S.BuildMemberReferenceExpr(E.get(), DecompType, Loc, false, |
| 1226 | CXXScopeSpec(), SourceLocation(), nullptr, |
| 1227 | MemberGet, &Args, nullptr); |
| 1228 | if (E.isInvalid()) |
| 1229 | return true; |
| 1230 | |
| 1231 | E = S.BuildCallExpr(nullptr, E.get(), Loc, None, Loc); |
| 1232 | } else { |
| 1233 | |
| 1234 | |
| 1235 | Expr *Get = UnresolvedLookupExpr::Create( |
| 1236 | S.Context, nullptr, NestedNameSpecifierLoc(), SourceLocation(), |
| 1237 | DeclarationNameInfo(GetDN, Loc), true, &Args, |
| 1238 | UnresolvedSetIterator(), UnresolvedSetIterator()); |
| 1239 | |
| 1240 | Expr *Arg = E.get(); |
| 1241 | E = S.BuildCallExpr(nullptr, Get, Loc, Arg, Loc); |
| 1242 | } |
| 1243 | if (E.isInvalid()) |
| 1244 | return true; |
| 1245 | Expr *Init = E.get(); |
| 1246 | |
| 1247 | |
| 1248 | QualType T = getTupleLikeElementType(S, Loc, I, DecompType); |
| 1249 | if (T.isNull()) |
| 1250 | return true; |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | QualType RefType = |
| 1256 | S.BuildReferenceType(T, E.get()->isLValue(), Loc, B->getDeclName()); |
| 1257 | if (RefType.isNull()) |
| 1258 | return true; |
| 1259 | auto *RefVD = VarDecl::Create( |
| 1260 | S.Context, Src->getDeclContext(), Loc, Loc, |
| 1261 | B->getDeclName().getAsIdentifierInfo(), RefType, |
| 1262 | S.Context.getTrivialTypeSourceInfo(T, Loc), Src->getStorageClass()); |
| 1263 | RefVD->setLexicalDeclContext(Src->getLexicalDeclContext()); |
| 1264 | RefVD->setTSCSpec(Src->getTSCSpec()); |
| 1265 | RefVD->setImplicit(); |
| 1266 | if (Src->isInlineSpecified()) |
| 1267 | RefVD->setInlineSpecified(); |
| 1268 | RefVD->getLexicalDeclContext()->addHiddenDecl(RefVD); |
| 1269 | |
| 1270 | InitializedEntity Entity = InitializedEntity::InitializeBinding(RefVD); |
| 1271 | InitializationKind Kind = InitializationKind::CreateCopy(Loc, Loc); |
| 1272 | InitializationSequence Seq(S, Entity, Kind, Init); |
| 1273 | E = Seq.Perform(S, Entity, Kind, Init); |
| 1274 | if (E.isInvalid()) |
| 1275 | return true; |
| 1276 | E = S.ActOnFinishFullExpr(E.get(), Loc, false); |
| 1277 | if (E.isInvalid()) |
| 1278 | return true; |
| 1279 | RefVD->setInit(E.get()); |
| 1280 | S.CheckCompleteVariableDeclaration(RefVD); |
| 1281 | |
| 1282 | E = S.BuildDeclarationNameExpr(CXXScopeSpec(), |
| 1283 | DeclarationNameInfo(B->getDeclName(), Loc), |
| 1284 | RefVD); |
| 1285 | if (E.isInvalid()) |
| 1286 | return true; |
| 1287 | |
| 1288 | B->setBinding(T, E.get()); |
| 1289 | I++; |
| 1290 | } |
| 1291 | |
| 1292 | return false; |
| 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | static DeclAccessPair findDecomposableBaseClass(Sema &S, SourceLocation Loc, |
| 1299 | const CXXRecordDecl *RD, |
| 1300 | CXXCastPath &BasePath) { |
| 1301 | auto BaseHasFields = [](const CXXBaseSpecifier *Specifier, |
| 1302 | CXXBasePath &Path) { |
| 1303 | return Specifier->getType()->getAsCXXRecordDecl()->hasDirectFields(); |
| 1304 | }; |
| 1305 | |
| 1306 | const CXXRecordDecl *ClassWithFields = nullptr; |
| 1307 | AccessSpecifier AS = AS_public; |
| 1308 | if (RD->hasDirectFields()) |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | ClassWithFields = RD; |
| 1313 | else { |
| 1314 | |
| 1315 | CXXBasePaths Paths; |
| 1316 | Paths.setOrigin(const_cast<CXXRecordDecl*>(RD)); |
| 1317 | if (!RD->lookupInBases(BaseHasFields, Paths)) { |
| 1318 | |
| 1319 | |
| 1320 | return DeclAccessPair::make(const_cast<CXXRecordDecl*>(RD), AS_public); |
| 1321 | } |
| 1322 | |
| 1323 | CXXBasePath *BestPath = nullptr; |
| 1324 | for (auto &P : Paths) { |
| 1325 | if (!BestPath) |
| 1326 | BestPath = &P; |
| 1327 | else if (!S.Context.hasSameType(P.back().Base->getType(), |
| 1328 | BestPath->back().Base->getType())) { |
| 1329 | |
| 1330 | S.Diag(Loc, diag::err_decomp_decl_multiple_bases_with_members) |
| 1331 | << false << RD << BestPath->back().Base->getType() |
| 1332 | << P.back().Base->getType(); |
| 1333 | return DeclAccessPair(); |
| 1334 | } else if (P.Access < BestPath->Access) { |
| 1335 | BestPath = &P; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | |
| 1340 | QualType BaseType = BestPath->back().Base->getType(); |
| 1341 | if (Paths.isAmbiguous(S.Context.getCanonicalType(BaseType))) { |
| 1342 | S.Diag(Loc, diag::err_decomp_decl_ambiguous_base) |
| 1343 | << RD << BaseType << S.getAmbiguousPathsDisplayString(Paths); |
| 1344 | return DeclAccessPair(); |
| 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | S.CheckBaseClassAccess(Loc, BaseType, S.Context.getRecordType(RD), |
| 1349 | *BestPath, diag::err_decomp_decl_inaccessible_base); |
| 1350 | AS = BestPath->Access; |
| 1351 | |
| 1352 | ClassWithFields = BaseType->getAsCXXRecordDecl(); |
| 1353 | S.BuildBasePathArray(Paths, BasePath); |
| 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | CXXBasePaths Paths; |
| 1359 | if (ClassWithFields->lookupInBases(BaseHasFields, Paths)) { |
| 1360 | S.Diag(Loc, diag::err_decomp_decl_multiple_bases_with_members) |
| 1361 | << (ClassWithFields == RD) << RD << ClassWithFields |
| 1362 | << Paths.front().back().Base->getType(); |
| 1363 | return DeclAccessPair(); |
| 1364 | } |
| 1365 | |
| 1366 | return DeclAccessPair::make(const_cast<CXXRecordDecl*>(ClassWithFields), AS); |
| 1367 | } |
| 1368 | |
| 1369 | static bool checkMemberDecomposition(Sema &S, ArrayRef<BindingDecl*> Bindings, |
| 1370 | ValueDecl *Src, QualType DecompType, |
| 1371 | const CXXRecordDecl *OrigRD) { |
| 1372 | if (S.RequireCompleteType(Src->getLocation(), DecompType, |
| 1373 | diag::err_incomplete_type)) |
| 1374 | return true; |
| 1375 | |
| 1376 | CXXCastPath BasePath; |
| 1377 | DeclAccessPair BasePair = |
| 1378 | findDecomposableBaseClass(S, Src->getLocation(), OrigRD, BasePath); |
| 1379 | const CXXRecordDecl *RD = cast_or_null<CXXRecordDecl>(BasePair.getDecl()); |
| 1380 | if (!RD) |
| 1381 | return true; |
| 1382 | QualType BaseType = S.Context.getQualifiedType(S.Context.getRecordType(RD), |
| 1383 | DecompType.getQualifiers()); |
| 1384 | |
| 1385 | auto DiagnoseBadNumberOfBindings = [&]() -> bool { |
| 1386 | unsigned NumFields = |
| 1387 | std::count_if(RD->field_begin(), RD->field_end(), |
| 1388 | [](FieldDecl *FD) { return !FD->isUnnamedBitfield(); }); |
| 1389 | assert(Bindings.size() != NumFields); |
| 1390 | S.Diag(Src->getLocation(), diag::err_decomp_decl_wrong_number_bindings) |
| 1391 | << DecompType << (unsigned)Bindings.size() << NumFields << NumFields |
| 1392 | << (NumFields < Bindings.size()); |
| 1393 | return true; |
| 1394 | }; |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 | unsigned I = 0; |
| 1400 | for (auto *FD : RD->fields()) { |
| 1401 | if (FD->isUnnamedBitfield()) |
| 1402 | continue; |
| 1403 | |
| 1404 | |
| 1405 | |
| 1406 | if (!FD->getDeclName()) { |
| 1407 | if (RD->isLambda()) { |
| 1408 | S.Diag(Src->getLocation(), diag::err_decomp_decl_lambda); |
| 1409 | S.Diag(RD->getLocation(), diag::note_lambda_decl); |
| 1410 | return true; |
| 1411 | } |
| 1412 | |
| 1413 | if (FD->isAnonymousStructOrUnion()) { |
| 1414 | S.Diag(Src->getLocation(), diag::err_decomp_decl_anon_union_member) |
| 1415 | << DecompType << FD->getType()->isUnionType(); |
| 1416 | S.Diag(FD->getLocation(), diag::note_declared_at); |
| 1417 | return true; |
| 1418 | } |
| 1419 | |
| 1420 | |
| 1421 | } |
| 1422 | |
| 1423 | |
| 1424 | if (I >= Bindings.size()) |
| 1425 | return DiagnoseBadNumberOfBindings(); |
| 1426 | auto *B = Bindings[I++]; |
| 1427 | SourceLocation Loc = B->getLocation(); |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | S.CheckStructuredBindingMemberAccess( |
| 1434 | Loc, const_cast<CXXRecordDecl *>(OrigRD), |
| 1435 | DeclAccessPair::make(FD, CXXRecordDecl::MergeAccess( |
| 1436 | BasePair.getAccess(), FD->getAccess()))); |
| 1437 | |
| 1438 | |
| 1439 | ExprResult E = S.BuildDeclRefExpr(Src, DecompType, VK_LValue, Loc); |
| 1440 | if (E.isInvalid()) |
| 1441 | return true; |
| 1442 | E = S.ImpCastExprToType(E.get(), BaseType, CK_UncheckedDerivedToBase, |
| 1443 | VK_LValue, &BasePath); |
| 1444 | if (E.isInvalid()) |
| 1445 | return true; |
| 1446 | E = S.BuildFieldReferenceExpr(E.get(), false, Loc, |
| 1447 | CXXScopeSpec(), FD, |
| 1448 | DeclAccessPair::make(FD, FD->getAccess()), |
| 1449 | DeclarationNameInfo(FD->getDeclName(), Loc)); |
| 1450 | if (E.isInvalid()) |
| 1451 | return true; |
| 1452 | |
| 1453 | |
| 1454 | |
| 1455 | |
| 1456 | |
| 1457 | |
| 1458 | Qualifiers Q = DecompType.getQualifiers(); |
| 1459 | if (FD->isMutable()) |
| 1460 | Q.removeConst(); |
| 1461 | B->setBinding(S.BuildQualifiedType(FD->getType(), Loc, Q), E.get()); |
| 1462 | } |
| 1463 | |
| 1464 | if (I != Bindings.size()) |
| 1465 | return DiagnoseBadNumberOfBindings(); |
| 1466 | |
| 1467 | return false; |
| 1468 | } |
| 1469 | |
| 1470 | void Sema::CheckCompleteDecompositionDeclaration(DecompositionDecl *DD) { |
| 1471 | QualType DecompType = DD->getType(); |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | if (DecompType->isDependentType()) { |
| 1476 | for (auto *B : DD->bindings()) |
| 1477 | B->setType(Context.DependentTy); |
| 1478 | return; |
| 1479 | } |
| 1480 | |
| 1481 | DecompType = DecompType.getNonReferenceType(); |
| 1482 | ArrayRef<BindingDecl*> Bindings = DD->bindings(); |
| 1483 | |
| 1484 | |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 | if (auto *CAT = Context.getAsConstantArrayType(DecompType)) { |
| 1489 | if (checkArrayDecomposition(*this, Bindings, DD, DecompType, CAT)) |
| 1490 | DD->setInvalidDecl(); |
| 1491 | return; |
| 1492 | } |
| 1493 | if (auto *VT = DecompType->getAs<VectorType>()) { |
| 1494 | if (checkVectorDecomposition(*this, Bindings, DD, DecompType, VT)) |
| 1495 | DD->setInvalidDecl(); |
| 1496 | return; |
| 1497 | } |
| 1498 | if (auto *CT = DecompType->getAs<ComplexType>()) { |
| 1499 | if (checkComplexDecomposition(*this, Bindings, DD, DecompType, CT)) |
| 1500 | DD->setInvalidDecl(); |
| 1501 | return; |
| 1502 | } |
| 1503 | |
| 1504 | |
| 1505 | |
| 1506 | |
| 1507 | llvm::APSInt TupleSize(32); |
| 1508 | switch (isTupleLike(*this, DD->getLocation(), DecompType, TupleSize)) { |
| 1509 | case IsTupleLike::Error: |
| 1510 | DD->setInvalidDecl(); |
| 1511 | return; |
| 1512 | |
| 1513 | case IsTupleLike::TupleLike: |
| 1514 | if (checkTupleLikeDecomposition(*this, Bindings, DD, DecompType, TupleSize)) |
| 1515 | DD->setInvalidDecl(); |
| 1516 | return; |
| 1517 | |
| 1518 | case IsTupleLike::NotTupleLike: |
| 1519 | break; |
| 1520 | } |
| 1521 | |
| 1522 | |
| 1523 | |
| 1524 | CXXRecordDecl *RD = DecompType->getAsCXXRecordDecl(); |
| 1525 | if (!RD || RD->isUnion()) { |
| 1526 | Diag(DD->getLocation(), diag::err_decomp_decl_unbindable_type) |
| 1527 | << DD << !RD << DecompType; |
| 1528 | DD->setInvalidDecl(); |
| 1529 | return; |
| 1530 | } |
| 1531 | |
| 1532 | |
| 1533 | |
| 1534 | |
| 1535 | if (checkMemberDecomposition(*this, Bindings, DD, DecompType, RD)) |
| 1536 | DD->setInvalidDecl(); |
| 1537 | } |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | |
| 1542 | |
| 1543 | |
| 1544 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 1545 | |
| 1546 | if (!getLangOpts().CXXExceptions) |
| 1547 | return; |
| 1548 | |
| 1549 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 1550 | "Should only be called if types are otherwise the same."); |
| 1551 | |
| 1552 | QualType NewType = New->getType(); |
| 1553 | QualType OldType = Old->getType(); |
| 1554 | |
| 1555 | |
| 1556 | |
| 1557 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 1558 | NewType = R->getPointeeType(); |
| 1559 | OldType = OldType->castAs<ReferenceType>()->getPointeeType(); |
| 1560 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 1561 | NewType = P->getPointeeType(); |
| 1562 | OldType = OldType->castAs<PointerType>()->getPointeeType(); |
| 1563 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 1564 | NewType = M->getPointeeType(); |
| 1565 | OldType = OldType->castAs<MemberPointerType>()->getPointeeType(); |
| 1566 | } |
| 1567 | |
| 1568 | if (!NewType->isFunctionProtoType()) |
| 1569 | return; |
| 1570 | |
| 1571 | |
| 1572 | |
| 1573 | |
| 1574 | if (CheckEquivalentExceptionSpec( |
| 1575 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 1576 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 1577 | New->setInvalidDecl(); |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | |
| 1582 | |
| 1583 | |
| 1584 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 1585 | unsigned NumParams = FD->getNumParams(); |
| 1586 | unsigned ParamIdx = 0; |
| 1587 | |
| 1588 | |
| 1589 | |
| 1590 | |
| 1591 | if (FD->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 1592 | return; |
| 1593 | if (auto *FTD = FD->getDescribedFunctionTemplate()) |
| 1594 | if (FTD->isMemberSpecialization()) |
| 1595 | return; |
| 1596 | |
| 1597 | |
| 1598 | for (; ParamIdx < NumParams; ++ParamIdx) { |
| 1599 | ParmVarDecl *Param = FD->getParamDecl(ParamIdx); |
| 1600 | if (Param->hasDefaultArg()) |
| 1601 | break; |
| 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | |
| 1606 | |
| 1607 | |
| 1608 | |
| 1609 | for (; ParamIdx < NumParams; ++ParamIdx) { |
| 1610 | ParmVarDecl *Param = FD->getParamDecl(ParamIdx); |
| 1611 | if (!Param->hasDefaultArg() && !Param->isParameterPack() && |
| 1612 | !(CurrentInstantiationScope && |
| 1613 | CurrentInstantiationScope->isLocalPackExpansion(Param))) { |
| 1614 | if (Param->isInvalidDecl()) |
| 1615 | ; |
| 1616 | else if (Param->getIdentifier()) |
| 1617 | Diag(Param->getLocation(), |
| 1618 | diag::err_param_default_argument_missing_name) |
| 1619 | << Param->getIdentifier(); |
| 1620 | else |
| 1621 | Diag(Param->getLocation(), |
| 1622 | diag::err_param_default_argument_missing); |
| 1623 | } |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | |
| 1630 | template <typename... Ts> |
| 1631 | static bool CheckLiteralType(Sema &SemaRef, Sema::CheckConstexprKind Kind, |
| 1632 | SourceLocation Loc, QualType T, unsigned DiagID, |
| 1633 | Ts &&...DiagArgs) { |
| 1634 | if (T->isDependentType()) |
| 1635 | return false; |
| 1636 | |
| 1637 | switch (Kind) { |
| 1638 | case Sema::CheckConstexprKind::Diagnose: |
| 1639 | return SemaRef.RequireLiteralType(Loc, T, DiagID, |
| 1640 | std::forward<Ts>(DiagArgs)...); |
| 1641 | |
| 1642 | case Sema::CheckConstexprKind::CheckValid: |
| 1643 | return !T->isLiteralType(SemaRef.Context); |
| 1644 | } |
| 1645 | |
| 1646 | llvm_unreachable("unknown CheckConstexprKind"); |
| 1647 | } |
| 1648 | |
| 1649 | |
| 1650 | static bool CheckConstexprDestructorSubobjects(Sema &SemaRef, |
| 1651 | const CXXDestructorDecl *DD, |
| 1652 | Sema::CheckConstexprKind Kind) { |
| 1653 | auto Check = [&](SourceLocation Loc, QualType T, const FieldDecl *FD) { |
| 1654 | const CXXRecordDecl *RD = |
| 1655 | T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 1656 | if (!RD || RD->hasConstexprDestructor()) |
| 1657 | return true; |
| 1658 | |
| 1659 | if (Kind == Sema::CheckConstexprKind::Diagnose) { |
| 1660 | SemaRef.Diag(DD->getLocation(), diag::err_constexpr_dtor_subobject) |
| 1661 | << static_cast<int>(DD->getConstexprKind()) << !FD |
| 1662 | << (FD ? FD->getDeclName() : DeclarationName()) << T; |
| 1663 | SemaRef.Diag(Loc, diag::note_constexpr_dtor_subobject) |
| 1664 | << !FD << (FD ? FD->getDeclName() : DeclarationName()) << T; |
| 1665 | } |
| 1666 | return false; |
| 1667 | }; |
| 1668 | |
| 1669 | const CXXRecordDecl *RD = DD->getParent(); |
| 1670 | for (const CXXBaseSpecifier &B : RD->bases()) |
| 1671 | if (!Check(B.getBaseTypeLoc(), B.getType(), nullptr)) |
| 1672 | return false; |
| 1673 | for (const FieldDecl *FD : RD->fields()) |
| 1674 | if (!Check(FD->getLocation(), FD->getType(), FD)) |
| 1675 | return false; |
| 1676 | return true; |
| 1677 | } |
| 1678 | |
| 1679 | |
| 1680 | |
| 1681 | static bool CheckConstexprParameterTypes(Sema &SemaRef, |
| 1682 | const FunctionDecl *FD, |
| 1683 | Sema::CheckConstexprKind Kind) { |
| 1684 | unsigned ArgIndex = 0; |
| 1685 | const auto *FT = FD->getType()->castAs<FunctionProtoType>(); |
| 1686 | for (FunctionProtoType::param_type_iterator i = FT->param_type_begin(), |
| 1687 | e = FT->param_type_end(); |
| 1688 | i != e; ++i, ++ArgIndex) { |
| 1689 | const ParmVarDecl *PD = FD->getParamDecl(ArgIndex); |
| 1690 | SourceLocation ParamLoc = PD->getLocation(); |
| 1691 | if (CheckLiteralType(SemaRef, Kind, ParamLoc, *i, |
| 1692 | diag::err_constexpr_non_literal_param, ArgIndex + 1, |
| 1693 | PD->getSourceRange(), isa<CXXConstructorDecl>(FD), |
| 1694 | FD->isConsteval())) |
| 1695 | return false; |
| 1696 | } |
| 1697 | return true; |
| 1698 | } |
| 1699 | |
| 1700 | |
| 1701 | |
| 1702 | static bool CheckConstexprReturnType(Sema &SemaRef, const FunctionDecl *FD, |
| 1703 | Sema::CheckConstexprKind Kind) { |
| 1704 | if (CheckLiteralType(SemaRef, Kind, FD->getLocation(), FD->getReturnType(), |
| 1705 | diag::err_constexpr_non_literal_return, |
| 1706 | FD->isConsteval())) |
| 1707 | return false; |
| 1708 | return true; |
| 1709 | } |
| 1710 | |
| 1711 | |
| 1712 | |
| 1713 | |
| 1714 | |
| 1715 | |
| 1716 | static unsigned getRecordDiagFromTagKind(TagTypeKind Tag) { |
| 1717 | switch (Tag) { |
| 1718 | case TTK_Struct: return 0; |
| 1719 | case TTK_Interface: return 1; |
| 1720 | case TTK_Class: return 2; |
| 1721 | default: llvm_unreachable("Invalid tag kind for record diagnostic!"); |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | static bool CheckConstexprFunctionBody(Sema &SemaRef, const FunctionDecl *Dcl, |
| 1726 | Stmt *Body, |
| 1727 | Sema::CheckConstexprKind Kind); |
| 1728 | |
| 1729 | |
| 1730 | |
| 1731 | |
| 1732 | |
| 1733 | |
| 1734 | |
| 1735 | bool Sema::CheckConstexprFunctionDefinition(const FunctionDecl *NewFD, |
| 1736 | CheckConstexprKind Kind) { |
| 1737 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| 1738 | if (MD && MD->isInstance()) { |
| 1739 | |
| 1740 | |
| 1741 | |
| 1742 | |
| 1743 | |
| 1744 | |
| 1745 | |
| 1746 | const CXXRecordDecl *RD = MD->getParent(); |
| 1747 | if (RD->getNumVBases()) { |
| 1748 | if (Kind == CheckConstexprKind::CheckValid) |
| 1749 | return false; |
| 1750 | |
| 1751 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual_base) |
| 1752 | << isa<CXXConstructorDecl>(NewFD) |
| 1753 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases(); |
| 1754 | for (const auto &I : RD->vbases()) |
| 1755 | Diag(I.getBeginLoc(), diag::note_constexpr_virtual_base_here) |
| 1756 | << I.getSourceRange(); |
| 1757 | return false; |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | if (!isa<CXXConstructorDecl>(NewFD)) { |
| 1762 | |
| 1763 | |
| 1764 | |
| 1765 | |
| 1766 | const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD); |
| 1767 | if (Method && Method->isVirtual()) { |
| 1768 | if (getLangOpts().CPlusPlus20) { |
| 1769 | if (Kind == CheckConstexprKind::Diagnose) |
| 1770 | Diag(Method->getLocation(), diag::warn_cxx17_compat_constexpr_virtual); |
| 1771 | } else { |
| 1772 | if (Kind == CheckConstexprKind::CheckValid) |
| 1773 | return false; |
| 1774 | |
| 1775 | Method = Method->getCanonicalDecl(); |
| 1776 | Diag(Method->getLocation(), diag::err_constexpr_virtual); |
| 1777 | |
| 1778 | |
| 1779 | |
| 1780 | const CXXMethodDecl *WrittenVirtual = Method; |
| 1781 | while (!WrittenVirtual->isVirtualAsWritten()) |
| 1782 | WrittenVirtual = *WrittenVirtual->begin_overridden_methods(); |
| 1783 | if (WrittenVirtual != Method) |
| 1784 | Diag(WrittenVirtual->getLocation(), |
| 1785 | diag::note_overridden_virtual_function); |
| 1786 | return false; |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | |
| 1791 | if (!CheckConstexprReturnType(*this, NewFD, Kind)) |
| 1792 | return false; |
| 1793 | } |
| 1794 | |
| 1795 | if (auto *Dtor = dyn_cast<CXXDestructorDecl>(NewFD)) { |
| 1796 | |
| 1797 | |
| 1798 | |
| 1799 | if (!Dtor->getParent()->defaultedDestructorIsConstexpr()) { |
| 1800 | if (Kind == CheckConstexprKind::CheckValid) |
| 1801 | return false; |
| 1802 | if (!CheckConstexprDestructorSubobjects(*this, Dtor, Kind)) |
| 1803 | return false; |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | |
| 1808 | if (!CheckConstexprParameterTypes(*this, NewFD, Kind)) |
| 1809 | return false; |
| 1810 | |
| 1811 | Stmt *Body = NewFD->getBody(); |
| 1812 | assert(Body && |
| 1813 | "CheckConstexprFunctionDefinition called on function with no body"); |
| 1814 | return CheckConstexprFunctionBody(*this, NewFD, Body, Kind); |
| 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | |
| 1819 | |
| 1820 | |
| 1821 | |
| 1822 | static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl, |
| 1823 | DeclStmt *DS, SourceLocation &Cxx1yLoc, |
| 1824 | Sema::CheckConstexprKind Kind) { |
| 1825 | |
| 1826 | |
| 1827 | |
| 1828 | for (const auto *DclIt : DS->decls()) { |
| 1829 | switch (DclIt->getKind()) { |
| 1830 | case Decl::StaticAssert: |
| 1831 | case Decl::Using: |
| 1832 | case Decl::UsingShadow: |
| 1833 | case Decl::UsingDirective: |
| 1834 | case Decl::UnresolvedUsingTypename: |
| 1835 | case Decl::UnresolvedUsingValue: |
| 1836 | case Decl::UsingEnum: |
| 1837 | |
| 1838 | |
| 1839 | |
| 1840 | |
| 1841 | continue; |
| 1842 | |
| 1843 | case Decl::Typedef: |
| 1844 | case Decl::TypeAlias: { |
| 1845 | |
| 1846 | |
| 1847 | const auto *TN = cast<TypedefNameDecl>(DclIt); |
| 1848 | if (TN->getUnderlyingType()->isVariablyModifiedType()) { |
| 1849 | |
| 1850 | if (Kind == Sema::CheckConstexprKind::Diagnose) { |
| 1851 | TypeLoc TL = TN->getTypeSourceInfo()->getTypeLoc(); |
| 1852 | SemaRef.Diag(TL.getBeginLoc(), diag::err_constexpr_vla) |
| 1853 | << TL.getSourceRange() << TL.getType() |
| 1854 | << isa<CXXConstructorDecl>(Dcl); |
| 1855 | } |
| 1856 | return false; |
| 1857 | } |
| 1858 | continue; |
| 1859 | } |
| 1860 | |
| 1861 | case Decl::Enum: |
| 1862 | case Decl::CXXRecord: |
| 1863 | |
| 1864 | if (cast<TagDecl>(DclIt)->isThisDeclarationADefinition()) { |
| 1865 | if (Kind == Sema::CheckConstexprKind::Diagnose) { |
| 1866 | SemaRef.Diag(DS->getBeginLoc(), |
| 1867 | SemaRef.getLangOpts().CPlusPlus14 |
| 1868 | ? diag::warn_cxx11_compat_constexpr_type_definition |
| 1869 | : diag::ext_constexpr_type_definition) |
| 1870 | << isa<CXXConstructorDecl>(Dcl); |
| 1871 | } else if (!SemaRef.getLangOpts().CPlusPlus14) { |
| 1872 | return false; |
| 1873 | } |
| 1874 | } |
| 1875 | continue; |
| 1876 | |
| 1877 | case Decl::EnumConstant: |
| 1878 | case Decl::IndirectField: |
| 1879 | case Decl::ParmVar: |
| 1880 | |
| 1881 | |
| 1882 | continue; |
| 1883 | |
| 1884 | case Decl::Var: |
| 1885 | case Decl::Decomposition: { |
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | |
| 1890 | const auto *VD = cast<VarDecl>(DclIt); |
| 1891 | if (VD->isThisDeclarationADefinition()) { |
| 1892 | if (VD->isStaticLocal()) { |
| 1893 | if (Kind == Sema::CheckConstexprKind::Diagnose) { |
| 1894 | SemaRef.Diag(VD->getLocation(), |
| 1895 | diag::err_constexpr_local_var_static) |
| 1896 | << isa<CXXConstructorDecl>(Dcl) |
| 1897 | << (VD->getTLSKind() == VarDecl::TLS_Dynamic); |
| 1898 | } |
| 1899 | return false; |
| 1900 | } |
| 1901 | if (CheckLiteralType(SemaRef, Kind, VD->getLocation(), VD->getType(), |
| 1902 | diag::err_constexpr_local_var_non_literal_type, |
| 1903 | isa<CXXConstructorDecl>(Dcl))) |
| 1904 | return false; |
| 1905 | if (!VD->getType()->isDependentType() && |
| 1906 | !VD->hasInit() && !VD->isCXXForRangeDecl()) { |
| 1907 | if (Kind == Sema::CheckConstexprKind::Diagnose) { |
| 1908 | SemaRef.Diag( |
| 1909 | VD->getLocation(), |
| 1910 | SemaRef.getLangOpts().CPlusPlus20 |
| 1911 | ? diag::warn_cxx17_compat_constexpr_local_var_no_init |
| 1912 | : diag::ext_constexpr_local_var_no_init) |
| 1913 | << isa<CXXConstructorDecl>(Dcl); |
| 1914 | } else if (!SemaRef.getLangOpts().CPlusPlus20) { |
| 1915 | return false; |
| 1916 | } |
| 1917 | continue; |
| 1918 | } |
| 1919 | } |
| 1920 | if (Kind == Sema::CheckConstexprKind::Diagnose) { |
| 1921 | SemaRef.Diag(VD->getLocation(), |
| 1922 | SemaRef.getLangOpts().CPlusPlus14 |
| 1923 | ? diag::warn_cxx11_compat_constexpr_local_var |
| 1924 | : diag::ext_constexpr_local_var) |
| 1925 | << isa<CXXConstructorDecl>(Dcl); |
| 1926 | } else if (!SemaRef.getLangOpts().CPlusPlus14) { |
| 1927 | return false; |
| 1928 | } |
| 1929 | continue; |
| 1930 | } |
| 1931 | |
| 1932 | case Decl::NamespaceAlias: |
| 1933 | case Decl::Function: |
| 1934 | |
| 1935 | |
| 1936 | if (!Cxx1yLoc.isValid()) |
| 1937 | Cxx1yLoc = DS->getBeginLoc(); |
| 1938 | continue; |
| 1939 | |
| 1940 | default: |
| 1941 | if (Kind == Sema::CheckConstexprKind::Diagnose) { |
| 1942 | SemaRef.Diag(DS->getBeginLoc(), diag::err_constexpr_body_invalid_stmt) |
| 1943 | << isa<CXXConstructorDecl>(Dcl) << Dcl->isConsteval(); |
| 1944 | } |
| 1945 | return false; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | return true; |
| 1950 | } |
| 1951 | |
| 1952 | |
| 1953 | |
| 1954 | |
| 1955 | |
| 1956 | |
| 1957 | |
| 1958 | |
| 1959 | |
| 1960 | |
| 1961 | |
| 1962 | |
| 1963 | |
| 1964 | |
| 1965 | static bool CheckConstexprCtorInitializer(Sema &SemaRef, |
| 1966 | const FunctionDecl *Dcl, |
| 1967 | FieldDecl *Field, |
| 1968 | llvm::SmallSet<Decl*, 16> &Inits, |
| 1969 | bool &Diagnosed, |
| 1970 | Sema::CheckConstexprKind Kind) { |
| 1971 | |
| 1972 | if (Kind == Sema::CheckConstexprKind::CheckValid && |
| 1973 | SemaRef.getLangOpts().CPlusPlus20) |
| 1974 | return true; |
| 1975 | |
| 1976 | if (Field->isInvalidDecl()) |
| 1977 | return true; |
| 1978 | |
| 1979 | if (Field->isUnnamedBitfield()) |
| 1980 | return true; |
| 1981 | |
| 1982 | |
| 1983 | |
| 1984 | |
| 1985 | if (Field->isAnonymousStructOrUnion() && |
| 1986 | (Field->getType()->isUnionType() |
| 1987 | ? !Field->getType()->getAsCXXRecordDecl()->hasVariantMembers() |
| 1988 | : Field->getType()->getAsCXXRecordDecl()->isEmpty())) |
| 1989 | return true; |
| 1990 | |
| 1991 | if (!Inits.count(Field)) { |
| 1992 | if (Kind == Sema::CheckConstexprKind::Diagnose) { |
| 1993 | if (!Diagnosed) { |
| 1994 | SemaRef.Diag(Dcl->getLocation(), |
| 1995 | SemaRef.getLangOpts().CPlusPlus20 |
| 1996 | ? diag::warn_cxx17_compat_constexpr_ctor_missing_init |
| 1997 | : diag::ext_constexpr_ctor_missing_init); |
| 1998 | Diagnosed = true; |
| 1999 | } |
| 2000 | SemaRef.Diag(Field->getLocation(), |
| 2001 | diag::note_constexpr_ctor_missing_init); |
| 2002 | } else if (!SemaRef.getLangOpts().CPlusPlus20) { |
| 2003 | return false; |
| 2004 | } |
| 2005 | } else if (Field->isAnonymousStructOrUnion()) { |
| 2006 | const RecordDecl *RD = Field->getType()->castAs<RecordType>()->getDecl(); |
| 2007 | for (auto *I : RD->fields()) |
| 2008 | |
| 2009 | |
| 2010 | if (!RD->isUnion() || Inits.count(I)) |
| 2011 | if (!CheckConstexprCtorInitializer(SemaRef, Dcl, I, Inits, Diagnosed, |
| 2012 | Kind)) |
| 2013 | return false; |
| 2014 | } |
| 2015 | return true; |
| 2016 | } |
| 2017 | |
| 2018 | |
| 2019 | |
| 2020 | static bool |
| 2021 | CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S, |
| 2022 | SmallVectorImpl<SourceLocation> &ReturnStmts, |
| 2023 | SourceLocation &Cxx1yLoc, SourceLocation &Cxx2aLoc, |
| 2024 | Sema::CheckConstexprKind Kind) { |
| 2025 | |
| 2026 | switch (S->getStmtClass()) { |
| 2027 | case Stmt::NullStmtClass: |
| 2028 | |
| 2029 | return true; |
| 2030 | |
| 2031 | case Stmt::DeclStmtClass: |
| 2032 | |
| 2033 | |
| 2034 | |
| 2035 | |
| 2036 | |
| 2037 | if (!CheckConstexprDeclStmt(SemaRef, Dcl, cast<DeclStmt>(S), Cxx1yLoc, Kind)) |
| 2038 | return false; |
| 2039 | return true; |
| 2040 | |
| 2041 | case Stmt::ReturnStmtClass: |
| 2042 | |
| 2043 | if (isa<CXXConstructorDecl>(Dcl)) { |
| 2044 | |
| 2045 | if (!Cxx1yLoc.isValid()) |
| 2046 | Cxx1yLoc = S->getBeginLoc(); |
| 2047 | return true; |
| 2048 | } |
| 2049 | |
| 2050 | ReturnStmts.push_back(S->getBeginLoc()); |
| 2051 | return true; |
| 2052 | |
| 2053 | case Stmt::CompoundStmtClass: { |
| 2054 | |
| 2055 | if (!Cxx1yLoc.isValid()) |
| 2056 | Cxx1yLoc = S->getBeginLoc(); |
| 2057 | |
| 2058 | CompoundStmt *CompStmt = cast<CompoundStmt>(S); |
| 2059 | for (auto *BodyIt : CompStmt->body()) { |
| 2060 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, BodyIt, ReturnStmts, |
| 2061 | Cxx1yLoc, Cxx2aLoc, Kind)) |
| 2062 | return false; |
| 2063 | } |
| 2064 | return true; |
| 2065 | } |
| 2066 | |
| 2067 | case Stmt::AttributedStmtClass: |
| 2068 | if (!Cxx1yLoc.isValid()) |
| 2069 | Cxx1yLoc = S->getBeginLoc(); |
| 2070 | return true; |
| 2071 | |
| 2072 | case Stmt::IfStmtClass: { |
| 2073 | |
| 2074 | if (!Cxx1yLoc.isValid()) |
| 2075 | Cxx1yLoc = S->getBeginLoc(); |
| 2076 | |
| 2077 | IfStmt *If = cast<IfStmt>(S); |
| 2078 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, If->getThen(), ReturnStmts, |
| 2079 | Cxx1yLoc, Cxx2aLoc, Kind)) |
| 2080 | return false; |
| 2081 | if (If->getElse() && |
| 2082 | !CheckConstexprFunctionStmt(SemaRef, Dcl, If->getElse(), ReturnStmts, |
| 2083 | Cxx1yLoc, Cxx2aLoc, Kind)) |
| 2084 | return false; |
| 2085 | return true; |
| 2086 | } |
| 2087 | |
| 2088 | case Stmt::WhileStmtClass: |
| 2089 | case Stmt::DoStmtClass: |
| 2090 | case Stmt::ForStmtClass: |
| 2091 | case Stmt::CXXForRangeStmtClass: |
| 2092 | case Stmt::ContinueStmtClass: |
| 2093 | |
| 2094 | |
| 2095 | if (!SemaRef.getLangOpts().CPlusPlus14) |
| 2096 | break; |
| 2097 | if (!Cxx1yLoc.isValid()) |
| 2098 | Cxx1yLoc = S->getBeginLoc(); |
| 2099 | for (Stmt *SubStmt : S->children()) |
| 2100 | if (SubStmt && |
| 2101 | !CheckConstexprFunctionStmt(SemaRef, Dcl, SubStmt, ReturnStmts, |
| 2102 | Cxx1yLoc, Cxx2aLoc, Kind)) |
| 2103 | return false; |
| 2104 | return true; |
| 2105 | |
| 2106 | case Stmt::SwitchStmtClass: |
| 2107 | case Stmt::CaseStmtClass: |
| 2108 | case Stmt::DefaultStmtClass: |
| 2109 | case Stmt::BreakStmtClass: |
| 2110 | |
| 2111 | |
| 2112 | if (!Cxx1yLoc.isValid()) |
| 2113 | Cxx1yLoc = S->getBeginLoc(); |
| 2114 | for (Stmt *SubStmt : S->children()) |
| 2115 | if (SubStmt && |
| 2116 | !CheckConstexprFunctionStmt(SemaRef, Dcl, SubStmt, ReturnStmts, |
| 2117 | Cxx1yLoc, Cxx2aLoc, Kind)) |
| 2118 | return false; |
| 2119 | return true; |
| 2120 | |
| 2121 | case Stmt::GCCAsmStmtClass: |
| 2122 | case Stmt::MSAsmStmtClass: |
| 2123 | |
| 2124 | case Stmt::CXXTryStmtClass: |
| 2125 | if (Cxx2aLoc.isInvalid()) |
| 2126 | Cxx2aLoc = S->getBeginLoc(); |
| 2127 | for (Stmt *SubStmt : S->children()) { |
| 2128 | if (SubStmt && |
| 2129 | !CheckConstexprFunctionStmt(SemaRef, Dcl, SubStmt, ReturnStmts, |
| 2130 | Cxx1yLoc, Cxx2aLoc, Kind)) |
| 2131 | return false; |
| 2132 | } |
| 2133 | return true; |
| 2134 | |
| 2135 | case Stmt::CXXCatchStmtClass: |
| 2136 | |
| 2137 | |
| 2138 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, |
| 2139 | cast<CXXCatchStmt>(S)->getHandlerBlock(), |
| 2140 | ReturnStmts, Cxx1yLoc, Cxx2aLoc, Kind)) |
| 2141 | return false; |
| 2142 | return true; |
| 2143 | |
| 2144 | default: |
| 2145 | if (!isa<Expr>(S)) |
| 2146 | break; |
| 2147 | |
| 2148 | |
| 2149 | if (!Cxx1yLoc.isValid()) |
| 2150 | Cxx1yLoc = S->getBeginLoc(); |
| 2151 | return true; |
| 2152 | } |
| 2153 | |
| 2154 | if (Kind == Sema::CheckConstexprKind::Diagnose) { |
| 2155 | SemaRef.Diag(S->getBeginLoc(), diag::err_constexpr_body_invalid_stmt) |
| 2156 | << isa<CXXConstructorDecl>(Dcl) << Dcl->isConsteval(); |
| 2157 | } |
| 2158 | return false; |
| 2159 | } |
| 2160 | |
| 2161 | |
| 2162 | |
| 2163 | |
| 2164 | |
| 2165 | |
| 2166 | static bool CheckConstexprFunctionBody(Sema &SemaRef, const FunctionDecl *Dcl, |
| 2167 | Stmt *Body, |
| 2168 | Sema::CheckConstexprKind Kind) { |
| 2169 | SmallVector<SourceLocation, 4> ReturnStmts; |
| 2170 | |
| 2171 | if (isa<CXXTryStmt>(Body)) { |
| 2172 | |
| 2173 | |
| 2174 | |
| 2175 | |
| 2176 | |
| 2177 | |
| 2178 | |
| 2179 | |
| 2180 | |
| 2181 | |
| 2182 | |
| 2183 | |
| 2184 | switch (Kind) { |
| 2185 | case Sema::CheckConstexprKind::CheckValid: |
| 2186 | if (!SemaRef.getLangOpts().CPlusPlus20) |
| 2187 | return false; |
| 2188 | break; |
| 2189 | |
| 2190 | case Sema::CheckConstexprKind::Diagnose: |
| 2191 | SemaRef.Diag(Body->getBeginLoc(), |
| 2192 | !SemaRef.getLangOpts().CPlusPlus20 |
| 2193 | ? diag::ext_constexpr_function_try_block_cxx20 |
| 2194 | : diag::warn_cxx17_compat_constexpr_function_try_block) |
| 2195 | << isa<CXXConstructorDecl>(Dcl); |
| 2196 | break; |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | |
| 2201 | |
| 2202 | |
| 2203 | |
| 2204 | |
| 2205 | SourceLocation Cxx1yLoc, Cxx2aLoc; |
| 2206 | for (Stmt *SubStmt : Body->children()) { |
| 2207 | if (SubStmt && |
| 2208 | !CheckConstexprFunctionStmt(SemaRef, Dcl, SubStmt, ReturnStmts, |
| 2209 | Cxx1yLoc, Cxx2aLoc, Kind)) |
| 2210 | return false; |
| 2211 | } |
| 2212 | |
| 2213 | if (Kind == Sema::CheckConstexprKind::CheckValid) { |
| 2214 | |
| 2215 | |
| 2216 | if ((Cxx2aLoc.isValid() && !SemaRef.getLangOpts().CPlusPlus20) || |
| 2217 | (Cxx1yLoc.isValid() && !SemaRef.getLangOpts().CPlusPlus17)) |
| 2218 | return false; |
| 2219 | } else if (Cxx2aLoc.isValid()) { |
| 2220 | SemaRef.Diag(Cxx2aLoc, |
| 2221 | SemaRef.getLangOpts().CPlusPlus20 |
| 2222 | ? diag::warn_cxx17_compat_constexpr_body_invalid_stmt |
| 2223 | : diag::ext_constexpr_body_invalid_stmt_cxx20) |
| 2224 | << isa<CXXConstructorDecl>(Dcl); |
| 2225 | } else if (Cxx1yLoc.isValid()) { |
| 2226 | SemaRef.Diag(Cxx1yLoc, |
| 2227 | SemaRef.getLangOpts().CPlusPlus14 |
| 2228 | ? diag::warn_cxx11_compat_constexpr_body_invalid_stmt |
| 2229 | : diag::ext_constexpr_body_invalid_stmt) |
| 2230 | << isa<CXXConstructorDecl>(Dcl); |
| 2231 | } |
| 2232 | |
| 2233 | if (const CXXConstructorDecl *Constructor |
| 2234 | = dyn_cast<CXXConstructorDecl>(Dcl)) { |
| 2235 | const CXXRecordDecl *RD = Constructor->getParent(); |
| 2236 | |
| 2237 | |
| 2238 | |
| 2239 | |
| 2240 | |
| 2241 | |
| 2242 | if (RD->isUnion()) { |
| 2243 | if (Constructor->getNumCtorInitializers() == 0 && |
| 2244 | RD->hasVariantMembers()) { |
| 2245 | if (Kind == Sema::CheckConstexprKind::Diagnose) { |
| 2246 | SemaRef.Diag( |
| 2247 | Dcl->getLocation(), |
| 2248 | SemaRef.getLangOpts().CPlusPlus20 |
| 2249 | ? diag::warn_cxx17_compat_constexpr_union_ctor_no_init |
| 2250 | : diag::ext_constexpr_union_ctor_no_init); |
| 2251 | } else if (!SemaRef.getLangOpts().CPlusPlus20) { |
| 2252 | return false; |
| 2253 | } |
| 2254 | } |
| 2255 | } else if (!Constructor->isDependentContext() && |
| 2256 | !Constructor->isDelegatingConstructor()) { |
| 2257 | assert(RD->getNumVBases() == 0 && "constexpr ctor with virtual bases"); |
| 2258 | |
| 2259 | |
| 2260 | |
| 2261 | bool AnyAnonStructUnionMembers = false; |
| 2262 | unsigned Fields = 0; |
| 2263 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 2264 | E = RD->field_end(); I != E; ++I, ++Fields) { |
| 2265 | if (I->isAnonymousStructOrUnion()) { |
| 2266 | AnyAnonStructUnionMembers = true; |
| 2267 | break; |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | |
| 2272 | |
| 2273 | |
| 2274 | if (AnyAnonStructUnionMembers || |
| 2275 | Constructor->getNumCtorInitializers() != RD->getNumBases() + Fields) { |
| 2276 | |
| 2277 | |
| 2278 | |
| 2279 | llvm::SmallSet<Decl*, 16> Inits; |
| 2280 | for (const auto *I: Constructor->inits()) { |
| 2281 | if (FieldDecl *FD = I->getMember()) |
| 2282 | Inits.insert(FD); |
| 2283 | else if (IndirectFieldDecl *ID = I->getIndirectMember()) |
| 2284 | Inits.insert(ID->chain_begin(), ID->chain_end()); |
| 2285 | } |
| 2286 | |
| 2287 | bool Diagnosed = false; |
| 2288 | for (auto *I : RD->fields()) |
| 2289 | if (!CheckConstexprCtorInitializer(SemaRef, Dcl, I, Inits, Diagnosed, |
| 2290 | Kind)) |
| 2291 | return false; |
| 2292 | } |
| 2293 | } |
| 2294 | } else { |
| 2295 | if (ReturnStmts.empty()) { |
| 2296 | |
| 2297 | |
| 2298 | |
| 2299 | |
| 2300 | bool OK = SemaRef.getLangOpts().CPlusPlus14 && |
| 2301 | (Dcl->getReturnType()->isVoidType() || |
| 2302 | Dcl->getReturnType()->isDependentType()); |
| 2303 | switch (Kind) { |
| 2304 | case Sema::CheckConstexprKind::Diagnose: |
| 2305 | SemaRef.Diag(Dcl->getLocation(), |
| 2306 | OK ? diag::warn_cxx11_compat_constexpr_body_no_return |
| 2307 | : diag::err_constexpr_body_no_return) |
| 2308 | << Dcl->isConsteval(); |
| 2309 | if (!OK) |
| 2310 | return false; |
| 2311 | break; |
| 2312 | |
| 2313 | case Sema::CheckConstexprKind::CheckValid: |
| 2314 | |
| 2315 | |
| 2316 | |
| 2317 | if (!SemaRef.getLangOpts().CPlusPlus14) |
| 2318 | return false; |
| 2319 | break; |
| 2320 | } |
| 2321 | } else if (ReturnStmts.size() > 1) { |
| 2322 | switch (Kind) { |
| 2323 | case Sema::CheckConstexprKind::Diagnose: |
| 2324 | SemaRef.Diag( |
| 2325 | ReturnStmts.back(), |
| 2326 | SemaRef.getLangOpts().CPlusPlus14 |
| 2327 | ? diag::warn_cxx11_compat_constexpr_body_multiple_return |
| 2328 | : diag::ext_constexpr_body_multiple_return); |
| 2329 | for (unsigned I = 0; I < ReturnStmts.size() - 1; ++I) |
| 2330 | SemaRef.Diag(ReturnStmts[I], |
| 2331 | diag::note_constexpr_body_previous_return); |
| 2332 | break; |
| 2333 | |
| 2334 | case Sema::CheckConstexprKind::CheckValid: |
| 2335 | if (!SemaRef.getLangOpts().CPlusPlus14) |
| 2336 | return false; |
| 2337 | break; |
| 2338 | } |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | |
| 2343 | |
| 2344 | |
| 2345 | |
| 2346 | |
| 2347 | |
| 2348 | |
| 2349 | |
| 2350 | |
| 2351 | |
| 2352 | |
| 2353 | |
| 2354 | |
| 2355 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 2356 | if (Kind == Sema::CheckConstexprKind::Diagnose && |
| 2357 | !Expr::isPotentialConstantExpr(Dcl, Diags)) { |
| 2358 | SemaRef.Diag(Dcl->getLocation(), |
| 2359 | diag::ext_constexpr_function_never_constant_expr) |
| 2360 | << isa<CXXConstructorDecl>(Dcl) << Dcl->isConsteval(); |
| 2361 | for (size_t I = 0, N = Diags.size(); I != N; ++I) |
| 2362 | SemaRef.Diag(Diags[I].first, Diags[I].second); |
| 2363 | |
| 2364 | |
| 2365 | } |
| 2366 | |
| 2367 | return true; |
| 2368 | } |
| 2369 | |
| 2370 | |
| 2371 | |
| 2372 | |
| 2373 | |
| 2374 | |
| 2375 | |
| 2376 | |
| 2377 | |
| 2378 | CXXRecordDecl *Sema::getCurrentClass(Scope *, const CXXScopeSpec *SS) { |
| 2379 | assert(getLangOpts().CPlusPlus && "No class names in C!"); |
| 2380 | |
| 2381 | if (SS && SS->isInvalid()) |
| 2382 | return nullptr; |
| 2383 | |
| 2384 | if (SS && SS->isNotEmpty()) { |
| 2385 | DeclContext *DC = computeDeclContext(*SS, true); |
| 2386 | return dyn_cast_or_null<CXXRecordDecl>(DC); |
| 2387 | } |
| 2388 | |
| 2389 | return dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 2390 | } |
| 2391 | |
| 2392 | |
| 2393 | |
| 2394 | |
| 2395 | |
| 2396 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *S, |
| 2397 | const CXXScopeSpec *SS) { |
| 2398 | CXXRecordDecl *CurDecl = getCurrentClass(S, SS); |
| 2399 | return CurDecl && &II == CurDecl->getIdentifier(); |
| 2400 | } |
| 2401 | |
| 2402 | |
| 2403 | |
| 2404 | |
| 2405 | bool Sema::isCurrentClassNameTypo(IdentifierInfo *&II, const CXXScopeSpec *SS) { |
| 2406 | assert(getLangOpts().CPlusPlus && "No class names in C!"); |
| 2407 | |
| 2408 | if (!getLangOpts().SpellChecking) |
| 2409 | return false; |
| 2410 | |
| 2411 | CXXRecordDecl *CurDecl; |
| 2412 | if (SS && SS->isSet() && !SS->isInvalid()) { |
| 2413 | DeclContext *DC = computeDeclContext(*SS, true); |
| 2414 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 2415 | } else |
| 2416 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 2417 | |
| 2418 | if (CurDecl && CurDecl->getIdentifier() && II != CurDecl->getIdentifier() && |
| 2419 | 3 * II->getName().edit_distance(CurDecl->getIdentifier()->getName()) |
| 2420 | < II->getLength()) { |
| 2421 | II = CurDecl->getIdentifier(); |
| 2422 | return true; |
| 2423 | } |
| 2424 | |
| 2425 | return false; |
| 2426 | } |
| 2427 | |
| 2428 | |
| 2429 | |
| 2430 | static bool findCircularInheritance(const CXXRecordDecl *Class, |
| 2431 | const CXXRecordDecl *Current) { |
| 2432 | SmallVector<const CXXRecordDecl*, 8> Queue; |
| 2433 | |
| 2434 | Class = Class->getCanonicalDecl(); |
| 2435 | while (true) { |
| 2436 | for (const auto &I : Current->bases()) { |
| 2437 | CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl(); |
| 2438 | if (!Base) |
| 2439 | continue; |
| 2440 | |
| 2441 | Base = Base->getDefinition(); |
| 2442 | if (!Base) |
| 2443 | continue; |
| 2444 | |
| 2445 | if (Base->getCanonicalDecl() == Class) |
| 2446 | return true; |
| 2447 | |
| 2448 | Queue.push_back(Base); |
| 2449 | } |
| 2450 | |
| 2451 | if (Queue.empty()) |
| 2452 | return false; |
| 2453 | |
| 2454 | Current = Queue.pop_back_val(); |
| 2455 | } |
| 2456 | |
| 2457 | return false; |
| 2458 | } |
| 2459 | |
| 2460 | |
| 2461 | |
| 2462 | |
| 2463 | |
| 2464 | CXXBaseSpecifier * |
| 2465 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 2466 | SourceRange SpecifierRange, |
| 2467 | bool Virtual, AccessSpecifier Access, |
| 2468 | TypeSourceInfo *TInfo, |
| 2469 | SourceLocation EllipsisLoc) { |
| 2470 | QualType BaseType = TInfo->getType(); |
| 2471 | if (BaseType->containsErrors()) { |
| 2472 | |
| 2473 | return nullptr; |
| 2474 | } |
| 2475 | |
| 2476 | |
| 2477 | if (Class->isUnion()) { |
| 2478 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 2479 | << SpecifierRange; |
| 2480 | return nullptr; |
| 2481 | } |
| 2482 | |
| 2483 | if (EllipsisLoc.isValid() && |
| 2484 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 2485 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 2486 | << TInfo->getTypeLoc().getSourceRange(); |
| 2487 | EllipsisLoc = SourceLocation(); |
| 2488 | } |
| 2489 | |
| 2490 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
| 2491 | |
| 2492 | if (BaseType->isDependentType()) { |
| 2493 | |
| 2494 | |
| 2495 | |
| 2496 | if (CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl()) { |
| 2497 | if (BaseDecl->getCanonicalDecl() == Class->getCanonicalDecl() || |
| 2498 | ((BaseDecl = BaseDecl->getDefinition()) && |
| 2499 | findCircularInheritance(Class, BaseDecl))) { |
| 2500 | Diag(BaseLoc, diag::err_circular_inheritance) |
| 2501 | << BaseType << Context.getTypeDeclType(Class); |
| 2502 | |
| 2503 | if (BaseDecl->getCanonicalDecl() != Class->getCanonicalDecl()) |
| 2504 | Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 2505 | << BaseType; |
| 2506 | |
| 2507 | return nullptr; |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | |
| 2512 | |
| 2513 | |
| 2514 | |
| 2515 | |
| 2516 | |
| 2517 | if (!Class->getTypeForDecl()->isDependentType()) |
| 2518 | Class->setInvalidDecl(); |
| 2519 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
| 2520 | Class->getTagKind() == TTK_Class, |
| 2521 | Access, TInfo, EllipsisLoc); |
| 2522 | } |
| 2523 | |
| 2524 | |
| 2525 | if (!BaseType->isRecordType()) { |
| 2526 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 2527 | return nullptr; |
| 2528 | } |
| 2529 | |
| 2530 | |
| 2531 | |
| 2532 | if (BaseType->isUnionType()) { |
| 2533 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 2534 | return nullptr; |
| 2535 | } |
| 2536 | |
| 2537 | |
| 2538 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 2539 | if (Attr *ClassAttr = getDLLAttr(Class)) { |
| 2540 | if (auto *BaseTemplate = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 2541 | BaseType->getAsCXXRecordDecl())) { |
| 2542 | propagateDLLAttrToBaseClassTemplate(Class, ClassAttr, BaseTemplate, |
| 2543 | BaseLoc); |
| 2544 | } |
| 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | |
| 2549 | |
| 2550 | |
| 2551 | if (RequireCompleteType(BaseLoc, BaseType, |
| 2552 | diag::err_incomplete_base_class, SpecifierRange)) { |
| 2553 | Class->setInvalidDecl(); |
| 2554 | return nullptr; |
| 2555 | } |
| 2556 | |
| 2557 | |
| 2558 | RecordDecl *BaseDecl = BaseType->castAs<RecordType>()->getDecl(); |
| 2559 | assert(BaseDecl && "Record type has no declaration"); |
| 2560 | BaseDecl = BaseDecl->getDefinition(); |
| 2561 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
| 2562 | CXXRecordDecl *CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 2563 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
| 2564 | |
| 2565 | |
| 2566 | |
| 2567 | |
| 2568 | const auto *BaseCSA = CXXBaseDecl->getAttr<CodeSegAttr>(); |
| 2569 | const auto *DerivedCSA = Class->getAttr<CodeSegAttr>(); |
| 2570 | if ((DerivedCSA || BaseCSA) && |
| 2571 | (!BaseCSA || !DerivedCSA || BaseCSA->getName() != DerivedCSA->getName())) { |
| 2572 | Diag(Class->getLocation(), diag::err_mismatched_code_seg_base); |
| 2573 | Diag(CXXBaseDecl->getLocation(), diag::note_base_class_specified_here) |
| 2574 | << CXXBaseDecl; |
| 2575 | return nullptr; |
| 2576 | } |
| 2577 | |
| 2578 | |
| 2579 | |
| 2580 | |
| 2581 | |
| 2582 | |
| 2583 | |
| 2584 | if (CXXBaseDecl->hasFlexibleArrayMember()) { |
| 2585 | Diag(BaseLoc, diag::err_base_class_has_flexible_array_member) |
| 2586 | << CXXBaseDecl->getDeclName(); |
| 2587 | return nullptr; |
| 2588 | } |
| 2589 | |
| 2590 | |
| 2591 | |
| 2592 | |
| 2593 | if (FinalAttr *FA = CXXBaseDecl->getAttr<FinalAttr>()) { |
| 2594 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| 2595 | << CXXBaseDecl->getDeclName() |
| 2596 | << FA->isSpelledAsSealed(); |
| 2597 | Diag(CXXBaseDecl->getLocation(), diag::note_entity_declared_at) |
| 2598 | << CXXBaseDecl->getDeclName() << FA->getRange(); |
| 2599 | return nullptr; |
| 2600 | } |
| 2601 | |
| 2602 | if (BaseDecl->isInvalidDecl()) |
| 2603 | Class->setInvalidDecl(); |
| 2604 | |
| 2605 | |
| 2606 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
| 2607 | Class->getTagKind() == TTK_Class, |
| 2608 | Access, TInfo, EllipsisLoc); |
| 2609 | } |
| 2610 | |
| 2611 | |
| 2612 | |
| 2613 | |
| 2614 | |
| 2615 | |
| 2616 | BaseResult |
| 2617 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
| 2618 | ParsedAttributes &Attributes, |
| 2619 | bool Virtual, AccessSpecifier Access, |
| 2620 | ParsedType basetype, SourceLocation BaseLoc, |
| 2621 | SourceLocation EllipsisLoc) { |
| 2622 | if (!classdecl) |
| 2623 | return true; |
| 2624 | |
| 2625 | AdjustDeclIfTemplate(classdecl); |
| 2626 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
| 2627 | if (!Class) |
| 2628 | return true; |
| 2629 | |
| 2630 | |
| 2631 | Class->setIsParsingBaseSpecifiers(); |
| 2632 | |
| 2633 | |
| 2634 | |
| 2635 | for (const ParsedAttr &AL : Attributes) { |
| 2636 | if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute) |
| 2637 | continue; |
| 2638 | Diag(AL.getLoc(), AL.getKind() == ParsedAttr::UnknownAttribute |
| 2639 | ? (unsigned)diag::warn_unknown_attribute_ignored |
| 2640 | : (unsigned)diag::err_base_specifier_attribute) |
| 2641 | << AL << AL.getRange(); |
| 2642 | } |
| 2643 | |
| 2644 | TypeSourceInfo *TInfo = nullptr; |
| 2645 | GetTypeFromParser(basetype, &TInfo); |
| 2646 | |
| 2647 | if (EllipsisLoc.isInvalid() && |
| 2648 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
| 2649 | UPPC_BaseType)) |
| 2650 | return true; |
| 2651 | |
| 2652 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
| 2653 | Virtual, Access, TInfo, |
| 2654 | EllipsisLoc)) |
| 2655 | return BaseSpec; |
| 2656 | else |
| 2657 | Class->setInvalidDecl(); |
| 2658 | |
| 2659 | return true; |
| 2660 | } |
| 2661 | |
| 2662 | |
| 2663 | |
| 2664 | typedef llvm::SmallPtrSet<QualType, 4> IndirectBaseSet; |
| 2665 | |
| 2666 | |
| 2667 | static void |
| 2668 | NoteIndirectBases(ASTContext &Context, IndirectBaseSet &Set, |
| 2669 | const QualType &Type) |
| 2670 | { |
| 2671 | |
| 2672 | |
| 2673 | if (auto Rec = Type->getAs<RecordType>()) { |
| 2674 | auto Decl = Rec->getAsCXXRecordDecl(); |
| 2675 | |
| 2676 | |
| 2677 | for (const auto &BaseSpec : Decl->bases()) { |
| 2678 | QualType Base = Context.getCanonicalType(BaseSpec.getType()) |
| 2679 | .getUnqualifiedType(); |
| 2680 | if (Set.insert(Base).second) |
| 2681 | |
| 2682 | NoteIndirectBases(Context, Set, Base); |
| 2683 | } |
| 2684 | } |
| 2685 | } |
| 2686 | |
| 2687 | |
| 2688 | |
| 2689 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, |
| 2690 | MutableArrayRef<CXXBaseSpecifier *> Bases) { |
| 2691 | if (Bases.empty()) |
| 2692 | return false; |
| 2693 | |
| 2694 | |
| 2695 | |
| 2696 | |
| 2697 | |
| 2698 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 2699 | |
| 2700 | |
| 2701 | |
| 2702 | IndirectBaseSet IndirectBaseTypes; |
| 2703 | |
| 2704 | |
| 2705 | unsigned NumGoodBases = 0; |
| 2706 | bool Invalid = false; |
| 2707 | for (unsigned idx = 0; idx < Bases.size(); ++idx) { |
| 2708 | QualType NewBaseType |
| 2709 | = Context.getCanonicalType(Bases[idx]->getType()); |
| 2710 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
| 2711 | |
| 2712 | CXXBaseSpecifier *&KnownBase = KnownBaseTypes[NewBaseType]; |
| 2713 | if (KnownBase) { |
| 2714 | |
| 2715 | |
| 2716 | |
| 2717 | Diag(Bases[idx]->getBeginLoc(), diag::err_duplicate_base_class) |
| 2718 | << KnownBase->getType() << Bases[idx]->getSourceRange(); |
| 2719 | |
| 2720 | |
| 2721 | |
| 2722 | Context.Deallocate(Bases[idx]); |
| 2723 | |
| 2724 | Invalid = true; |
| 2725 | } else { |
| 2726 | |
| 2727 | KnownBase = Bases[idx]; |
| 2728 | Bases[NumGoodBases++] = Bases[idx]; |
| 2729 | |
| 2730 | |
| 2731 | if (Bases.size() > 1) |
| 2732 | NoteIndirectBases(Context, IndirectBaseTypes, NewBaseType); |
| 2733 | |
| 2734 | if (const RecordType *Record = NewBaseType->getAs<RecordType>()) { |
| 2735 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 2736 | if (Class->isInterface() && |
| 2737 | (!RD->isInterfaceLike() || |
| 2738 | KnownBase->getAccessSpecifier() != AS_public)) { |
| 2739 | |
| 2740 | |
| 2741 | Diag(KnownBase->getBeginLoc(), diag::err_invalid_base_in_interface) |
| 2742 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD |
| 2743 | << RD->getSourceRange(); |
| 2744 | Invalid = true; |
| 2745 | } |
| 2746 | if (RD->hasAttr<WeakAttr>()) |
| 2747 | Class->addAttr(WeakAttr::CreateImplicit(Context)); |
| 2748 | } |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | |
| 2753 | Class->setBases(Bases.data(), NumGoodBases); |
| 2754 | |
| 2755 | |
| 2756 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) { |
| 2757 | |
| 2758 | QualType BaseType = Bases[idx]->getType(); |
| 2759 | |
| 2760 | |
| 2761 | |
| 2762 | if (BaseType->isDependentType()) |
| 2763 | continue; |
| 2764 | |
| 2765 | CanQualType CanonicalBase = Context.getCanonicalType(BaseType) |
| 2766 | .getUnqualifiedType(); |
| 2767 | |
| 2768 | if (IndirectBaseTypes.count(CanonicalBase)) { |
| 2769 | CXXBasePaths Paths(true, true, |
| 2770 | true); |
| 2771 | bool found |
| 2772 | = Class->isDerivedFrom(CanonicalBase->getAsCXXRecordDecl(), Paths); |
| 2773 | assert(found); |
| 2774 | (void)found; |
| 2775 | |
| 2776 | if (Paths.isAmbiguous(CanonicalBase)) |
| 2777 | Diag(Bases[idx]->getBeginLoc(), diag::warn_inaccessible_base_class) |
| 2778 | << BaseType << getAmbiguousPathsDisplayString(Paths) |
| 2779 | << Bases[idx]->getSourceRange(); |
| 2780 | else |
| 2781 | assert(Bases[idx]->isVirtual()); |
| 2782 | } |
| 2783 | |
| 2784 | |
| 2785 | |
| 2786 | Context.Deallocate(Bases[idx]); |
| 2787 | } |
| 2788 | |
| 2789 | return Invalid; |
| 2790 | } |
| 2791 | |
| 2792 | |
| 2793 | |
| 2794 | |
| 2795 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, |
| 2796 | MutableArrayRef<CXXBaseSpecifier *> Bases) { |
| 2797 | if (!ClassDecl || Bases.empty()) |
| 2798 | return; |
| 2799 | |
| 2800 | AdjustDeclIfTemplate(ClassDecl); |
| 2801 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), Bases); |
| 2802 | } |
| 2803 | |
| 2804 | |
| 2805 | |
| 2806 | bool Sema::IsDerivedFrom(SourceLocation Loc, QualType Derived, QualType Base) { |
| 2807 | if (!getLangOpts().CPlusPlus) |
| 2808 | return false; |
| 2809 | |
| 2810 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
| 2811 | if (!DerivedRD) |
| 2812 | return false; |
| 2813 | |
| 2814 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
| 2815 | if (!BaseRD) |
| 2816 | return false; |
| 2817 | |
| 2818 | |
| 2819 | |
| 2820 | if (BaseRD->isInvalidDecl() || DerivedRD->isInvalidDecl()) |
| 2821 | return false; |
| 2822 | |
| 2823 | |
| 2824 | |
| 2825 | if (!isCompleteType(Loc, Derived) && !DerivedRD->isBeingDefined()) |
| 2826 | return false; |
| 2827 | |
| 2828 | return DerivedRD->isDerivedFrom(BaseRD); |
| 2829 | } |
| 2830 | |
| 2831 | |
| 2832 | |
| 2833 | bool Sema::IsDerivedFrom(SourceLocation Loc, QualType Derived, QualType Base, |
| 2834 | CXXBasePaths &Paths) { |
| 2835 | if (!getLangOpts().CPlusPlus) |
| 2836 | return false; |
| 2837 | |
| 2838 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
| 2839 | if (!DerivedRD) |
| 2840 | return false; |
| 2841 | |
| 2842 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
| 2843 | if (!BaseRD) |
| 2844 | return false; |
| 2845 | |
| 2846 | if (!isCompleteType(Loc, Derived) && !DerivedRD->isBeingDefined()) |
| 2847 | return false; |
| 2848 | |
| 2849 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 2850 | } |
| 2851 | |
| 2852 | static void BuildBasePathArray(const CXXBasePath &Path, |
| 2853 | CXXCastPath &BasePathArray) { |
| 2854 | |
| 2855 | |
| 2856 | |
| 2857 | unsigned Start = 0; |
| 2858 | for (unsigned I = Path.size(); I != 0; --I) { |
| 2859 | if (Path[I - 1].Base->isVirtual()) { |
| 2860 | Start = I - 1; |
| 2861 | break; |
| 2862 | } |
| 2863 | } |
| 2864 | |
| 2865 | |
| 2866 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
| 2867 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
| 2868 | } |
| 2869 | |
| 2870 | |
| 2871 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
| 2872 | CXXCastPath &BasePathArray) { |
| 2873 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 2874 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 2875 | return ::BuildBasePathArray(Paths.front(), BasePathArray); |
| 2876 | } |
| 2877 | |
| 2878 | |
| 2879 | |
| 2880 | |
| 2881 | |
| 2882 | |
| 2883 | |
| 2884 | |
| 2885 | |
| 2886 | |
| 2887 | |
| 2888 | |
| 2889 | bool |
| 2890 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| 2891 | unsigned InaccessibleBaseID, |
| 2892 | unsigned AmbiguousBaseConvID, |
| 2893 | SourceLocation Loc, SourceRange Range, |
| 2894 | DeclarationName Name, |
| 2895 | CXXCastPath *BasePath, |
| 2896 | bool IgnoreAccess) { |
| 2897 | |
| 2898 | |
| 2899 | |
| 2900 | |
| 2901 | CXXBasePaths Paths(true, true, |
| 2902 | false); |
| 2903 | bool DerivationOkay = IsDerivedFrom(Loc, Derived, Base, Paths); |
| 2904 | if (!DerivationOkay) |
| 2905 | return true; |
| 2906 | |
| 2907 | const CXXBasePath *Path = nullptr; |
| 2908 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) |
| 2909 | Path = &Paths.front(); |
| 2910 | |
| 2911 | |
| 2912 | |
| 2913 | |
| 2914 | if (!Path && getLangOpts().MSVCCompat) { |
| 2915 | for (const CXXBasePath &PossiblePath : Paths) { |
| 2916 | if (PossiblePath.size() == 1) { |
| 2917 | Path = &PossiblePath; |
| 2918 | if (AmbiguousBaseConvID) |
| 2919 | Diag(Loc, diag::ext_ms_ambiguous_direct_base) |
| 2920 | << Base << Derived << Range; |
| 2921 | break; |
| 2922 | } |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | if (Path) { |
| 2927 | if (!IgnoreAccess) { |
| 2928 | |
| 2929 | switch ( |
| 2930 | CheckBaseClassAccess(Loc, Base, Derived, *Path, InaccessibleBaseID)) { |
| 2931 | case AR_inaccessible: |
| 2932 | return true; |
| 2933 | case AR_accessible: |
| 2934 | case AR_dependent: |
| 2935 | case AR_delayed: |
| 2936 | break; |
| 2937 | } |
| 2938 | } |
| 2939 | |
| 2940 | |
| 2941 | if (BasePath) |
| 2942 | ::BuildBasePathArray(*Path, *BasePath); |
| 2943 | return false; |
| 2944 | } |
| 2945 | |
| 2946 | if (AmbiguousBaseConvID) { |
| 2947 | |
| 2948 | |
| 2949 | |
| 2950 | |
| 2951 | |
| 2952 | |
| 2953 | Paths.clear(); |
| 2954 | Paths.setRecordingPaths(true); |
| 2955 | bool StillOkay = IsDerivedFrom(Loc, Derived, Base, Paths); |
| 2956 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 2957 | (void)StillOkay; |
| 2958 | |
| 2959 | |
| 2960 | |
| 2961 | |
| 2962 | |
| 2963 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2964 | |
| 2965 | Diag(Loc, AmbiguousBaseConvID) |
| 2966 | << Derived << Base << PathDisplayStr << Range << Name; |
| 2967 | } |
| 2968 | return true; |
| 2969 | } |
| 2970 | |
| 2971 | bool |
| 2972 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| 2973 | SourceLocation Loc, SourceRange Range, |
| 2974 | CXXCastPath *BasePath, |
| 2975 | bool IgnoreAccess) { |
| 2976 | return CheckDerivedToBaseConversion( |
| 2977 | Derived, Base, diag::err_upcast_to_inaccessible_base, |
| 2978 | diag::err_ambiguous_derived_to_base_conv, Loc, Range, DeclarationName(), |
| 2979 | BasePath, IgnoreAccess); |
| 2980 | } |
| 2981 | |
| 2982 | |
| 2983 | |
| 2984 | |
| 2985 | |
| 2986 | |
| 2987 | |
| 2988 | |
| 2989 | |
| 2990 | |
| 2991 | |
| 2992 | |
| 2993 | |
| 2994 | |
| 2995 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 2996 | std::string PathDisplayStr; |
| 2997 | std::set<unsigned> DisplayedPaths; |
| 2998 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 2999 | Path != Paths.end(); ++Path) { |
| 3000 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 3001 | |
| 3002 | |
| 3003 | PathDisplayStr += "\n "; |
| 3004 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 3005 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 3006 | Element != Path->end(); ++Element) |
| 3007 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | return PathDisplayStr; |
| 3012 | } |
| 3013 | |
| 3014 | |
| 3015 | |
| 3016 | |
| 3017 | |
| 3018 | |
| 3019 | bool Sema::ActOnAccessSpecifier(AccessSpecifier Access, SourceLocation ASLoc, |
| 3020 | SourceLocation ColonLoc, |
| 3021 | const ParsedAttributesView &Attrs) { |
| 3022 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
| 3023 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
| 3024 | ASLoc, ColonLoc); |
| 3025 | CurContext->addHiddenDecl(ASDecl); |
| 3026 | return ProcessAccessDeclAttributeList(ASDecl, Attrs); |
| 3027 | } |
| 3028 | |
| 3029 | |
| 3030 | void Sema::CheckOverrideControl(NamedDecl *D) { |
| 3031 | if (D->isInvalidDecl()) |
| 3032 | return; |
| 3033 | |
| 3034 | |
| 3035 | if (!D->hasAttr<OverrideAttr>() && !D->hasAttr<FinalAttr>()) |
| 3036 | return; |
| 3037 | |
| 3038 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
| 3039 | |
| 3040 | |
| 3041 | if (MD && MD->isInstance() && |
| 3042 | (MD->getParent()->hasAnyDependentBases() || |
| 3043 | MD->getType()->isDependentType())) |
| 3044 | return; |
| 3045 | |
| 3046 | if (MD && !MD->isVirtual()) { |
| 3047 | |
| 3048 | |
| 3049 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 3050 | FindHiddenVirtualMethods(MD, OverloadedMethods); |
| 3051 | |
| 3052 | if (!OverloadedMethods.empty()) { |
| 3053 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 3054 | Diag(OA->getLocation(), |
| 3055 | diag::override_keyword_hides_virtual_member_function) |
| 3056 | << "override" << (OverloadedMethods.size() > 1); |
| 3057 | } else if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
| 3058 | Diag(FA->getLocation(), |
| 3059 | diag::override_keyword_hides_virtual_member_function) |
| 3060 | << (FA->isSpelledAsSealed() ? "sealed" : "final") |
| 3061 | << (OverloadedMethods.size() > 1); |
| 3062 | } |
| 3063 | NoteHiddenVirtualMethods(MD, OverloadedMethods); |
| 3064 | MD->setInvalidDecl(); |
| 3065 | return; |
| 3066 | } |
| 3067 | |
| 3068 | |
| 3069 | } |
| 3070 | |
| 3071 | if (!MD || !MD->isVirtual()) { |
| 3072 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 3073 | Diag(OA->getLocation(), |
| 3074 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 3075 | << "override" << FixItHint::CreateRemoval(OA->getLocation()); |
| 3076 | D->dropAttr<OverrideAttr>(); |
| 3077 | } |
| 3078 | if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
| 3079 | Diag(FA->getLocation(), |
| 3080 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 3081 | << (FA->isSpelledAsSealed() ? "sealed" : "final") |
| 3082 | << FixItHint::CreateRemoval(FA->getLocation()); |
| 3083 | D->dropAttr<FinalAttr>(); |
| 3084 | } |
| 3085 | return; |
| 3086 | } |
| 3087 | |
| 3088 | |
| 3089 | |
| 3090 | |
| 3091 | |
| 3092 | bool HasOverriddenMethods = MD->size_overridden_methods() != 0; |
| 3093 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) |
| 3094 | Diag(MD->getLocation(), diag::err_function_marked_override_not_overriding) |
| 3095 | << MD->getDeclName(); |
| 3096 | } |
| 3097 | |
| 3098 | void Sema::DiagnoseAbsenceOfOverrideControl(NamedDecl *D, bool Inconsistent) { |
| 3099 | if (D->isInvalidDecl() || D->hasAttr<OverrideAttr>()) |
| 3100 | return; |
| 3101 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
| 3102 | if (!MD || MD->isImplicit() || MD->hasAttr<FinalAttr>()) |
| 3103 | return; |
| 3104 | |
| 3105 | SourceLocation Loc = MD->getLocation(); |
| 3106 | SourceLocation SpellingLoc = Loc; |
| 3107 | if (getSourceManager().isMacroArgExpansion(Loc)) |
| 3108 | SpellingLoc = getSourceManager().getImmediateExpansionRange(Loc).getBegin(); |
| 3109 | SpellingLoc = getSourceManager().getSpellingLoc(SpellingLoc); |
| 3110 | if (SpellingLoc.isValid() && getSourceManager().isInSystemHeader(SpellingLoc)) |
| 3111 | return; |
| 3112 | |
| 3113 | if (MD->size_overridden_methods() > 0) { |
| 3114 | auto EmitDiag = [&](unsigned DiagInconsistent, unsigned DiagSuggest) { |
| 3115 | unsigned DiagID = |
| 3116 | Inconsistent && !Diags.isIgnored(DiagInconsistent, MD->getLocation()) |
| 3117 | ? DiagInconsistent |
| 3118 | : DiagSuggest; |
| 3119 | Diag(MD->getLocation(), DiagID) << MD->getDeclName(); |
| 3120 | const CXXMethodDecl *OMD = *MD->begin_overridden_methods(); |
| 3121 | Diag(OMD->getLocation(), diag::note_overridden_virtual_function); |
| 3122 | }; |
| 3123 | if (isa<CXXDestructorDecl>(MD)) |
| 3124 | EmitDiag( |
| 3125 | diag::warn_inconsistent_destructor_marked_not_override_overriding, |
| 3126 | diag::warn_suggest_destructor_marked_not_override_overriding); |
| 3127 | else |
| 3128 | EmitDiag(diag::warn_inconsistent_function_marked_not_override_overriding, |
| 3129 | diag::warn_suggest_function_marked_not_override_overriding); |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | |
| 3134 | |
| 3135 | |
| 3136 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 3137 | const CXXMethodDecl *Old) { |
| 3138 | FinalAttr *FA = Old->getAttr<FinalAttr>(); |
| 3139 | if (!FA) |
| 3140 | return false; |
| 3141 | |
| 3142 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 3143 | << New->getDeclName() |
| 3144 | << FA->isSpelledAsSealed(); |
| 3145 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 3146 | return true; |
| 3147 | } |
| 3148 | |
| 3149 | static bool InitializationHasSideEffects(const FieldDecl &FD) { |
| 3150 | const Type *T = FD.getType()->getBaseElementTypeUnsafe(); |
| 3151 | |
| 3152 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 3153 | return !RD->isCompleteDefinition() || |
| 3154 | !RD->hasTrivialDefaultConstructor() || |
| 3155 | !RD->hasTrivialDestructor(); |
| 3156 | return false; |
| 3157 | } |
| 3158 | |
| 3159 | static const ParsedAttr *getMSPropertyAttr(const ParsedAttributesView &list) { |
| 3160 | ParsedAttributesView::const_iterator Itr = |
| 3161 | llvm::find_if(list, [](const ParsedAttr &AL) { |
| 3162 | return AL.isDeclspecPropertyAttribute(); |
| 3163 | }); |
| 3164 | if (Itr != list.end()) |
| 3165 | return &*Itr; |
| 3166 | return nullptr; |
| 3167 | } |
| 3168 | |
| 3169 | |
| 3170 | void Sema::CheckShadowInheritedFields(const SourceLocation &Loc, |
| 3171 | DeclarationName FieldName, |
| 3172 | const CXXRecordDecl *RD, |
| 3173 | bool DeclIsField) { |
| 3174 | if (Diags.isIgnored(diag::warn_shadow_field, Loc)) |
| 3175 | return; |
| 3176 | |
| 3177 | |
| 3178 | std::map<CXXRecordDecl*, NamedDecl*> Bases; |
| 3179 | auto FieldShadowed = [&](const CXXBaseSpecifier *Specifier, |
| 3180 | CXXBasePath &Path) { |
| 3181 | const auto Base = Specifier->getType()->getAsCXXRecordDecl(); |
| 3182 | |
| 3183 | if (Bases.find(Base) != Bases.end()) |
| 3184 | return true; |
| 3185 | for (const auto Field : Base->lookup(FieldName)) { |
| 3186 | if ((isa<FieldDecl>(Field) || isa<IndirectFieldDecl>(Field)) && |
| 3187 | Field->getAccess() != AS_private) { |
| 3188 | assert(Field->getAccess() != AS_none); |
| 3189 | assert(Bases.find(Base) == Bases.end()); |
| 3190 | Bases[Base] = Field; |
| 3191 | return true; |
| 3192 | } |
| 3193 | } |
| 3194 | return false; |
| 3195 | }; |
| 3196 | |
| 3197 | CXXBasePaths Paths(true, true, |
| 3198 | true); |
| 3199 | if (!RD->lookupInBases(FieldShadowed, Paths)) |
| 3200 | return; |
| 3201 | |
| 3202 | for (const auto &P : Paths) { |
| 3203 | auto Base = P.back().Base->getType()->getAsCXXRecordDecl(); |
| 3204 | auto It = Bases.find(Base); |
| 3205 | |
| 3206 | if (It == Bases.end()) |
| 3207 | continue; |
| 3208 | auto BaseField = It->second; |
| 3209 | assert(BaseField->getAccess() != AS_private); |
| 3210 | if (AS_none != |
| 3211 | CXXRecordDecl::MergeAccess(P.Access, BaseField->getAccess())) { |
| 3212 | Diag(Loc, diag::warn_shadow_field) |
| 3213 | << FieldName << RD << Base << DeclIsField; |
| 3214 | Diag(BaseField->getLocation(), diag::note_shadow_field); |
| 3215 | Bases.erase(It); |
| 3216 | } |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | |
| 3221 | |
| 3222 | |
| 3223 | |
| 3224 | |
| 3225 | NamedDecl * |
| 3226 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
| 3227 | MultiTemplateParamsArg TemplateParameterLists, |
| 3228 | Expr *BW, const VirtSpecifiers &VS, |
| 3229 | InClassInitStyle InitStyle) { |
| 3230 | const DeclSpec &DS = D.getDeclSpec(); |
| 3231 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 3232 | DeclarationName Name = NameInfo.getName(); |
| 3233 | SourceLocation Loc = NameInfo.getLoc(); |
| 3234 | |
| 3235 | |
| 3236 | if (Loc.isInvalid()) |
| 3237 | Loc = D.getBeginLoc(); |
| 3238 | |
| 3239 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 3240 | |
| 3241 | assert(isa<CXXRecordDecl>(CurContext)); |
| 3242 | assert(!DS.isFriendSpecified()); |
| 3243 | |
| 3244 | bool isFunc = D.isDeclarationOfFunction(); |
| 3245 | const ParsedAttr *MSPropertyAttr = |
| 3246 | getMSPropertyAttr(D.getDeclSpec().getAttributes()); |
| 3247 | |
| 3248 | if (cast<CXXRecordDecl>(CurContext)->isInterface()) { |
| 3249 | |
| 3250 | |
| 3251 | |
| 3252 | unsigned InvalidDecl; |
| 3253 | bool ShowDeclName = true; |
| 3254 | if (!isFunc && |
| 3255 | (DS.getStorageClassSpec() == DeclSpec::SCS_typedef || MSPropertyAttr)) |
| 3256 | InvalidDecl = 0; |
| 3257 | else if (!isFunc) |
| 3258 | InvalidDecl = 1; |
| 3259 | else if (AS != AS_public) |
| 3260 | InvalidDecl = 2; |
| 3261 | else if (DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 3262 | InvalidDecl = 3; |
| 3263 | else switch (Name.getNameKind()) { |
| 3264 | case DeclarationName::CXXConstructorName: |
| 3265 | InvalidDecl = 4; |
| 3266 | ShowDeclName = false; |
| 3267 | break; |
| 3268 | |
| 3269 | case DeclarationName::CXXDestructorName: |
| 3270 | InvalidDecl = 5; |
| 3271 | ShowDeclName = false; |
| 3272 | break; |
| 3273 | |
| 3274 | case DeclarationName::CXXOperatorName: |
| 3275 | case DeclarationName::CXXConversionFunctionName: |
| 3276 | InvalidDecl = 6; |
| 3277 | break; |
| 3278 | |
| 3279 | default: |
| 3280 | InvalidDecl = 0; |
| 3281 | break; |
| 3282 | } |
| 3283 | |
| 3284 | if (InvalidDecl) { |
| 3285 | if (ShowDeclName) |
| 3286 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 3287 | << (InvalidDecl-1) << Name; |
| 3288 | else |
| 3289 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 3290 | << (InvalidDecl-1) << ""; |
| 3291 | return nullptr; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | |
| 3296 | |
| 3297 | |
| 3298 | |
| 3299 | |
| 3300 | switch (DS.getStorageClassSpec()) { |
| 3301 | case DeclSpec::SCS_unspecified: |
| 3302 | case DeclSpec::SCS_typedef: |
| 3303 | case DeclSpec::SCS_static: |
| 3304 | break; |
| 3305 | case DeclSpec::SCS_mutable: |
| 3306 | if (isFunc) { |
| 3307 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
| 3308 | |
| 3309 | |
| 3310 | |
| 3311 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 3312 | } |
| 3313 | break; |
| 3314 | default: |
| 3315 | Diag(DS.getStorageClassSpecLoc(), |
| 3316 | diag::err_storageclass_invalid_for_member); |
| 3317 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 3318 | break; |
| 3319 | } |
| 3320 | |
| 3321 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 3322 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
| 3323 | !isFunc); |
| 3324 | |
| 3325 | if (DS.hasConstexprSpecifier() && isInstField) { |
| 3326 | SemaDiagnosticBuilder B = |
| 3327 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr_member); |
| 3328 | SourceLocation ConstexprLoc = DS.getConstexprSpecLoc(); |
| 3329 | if (InitStyle == ICIS_NoInit) { |
| 3330 | B << 0 << 0; |
| 3331 | if (D.getDeclSpec().getTypeQualifiers() & DeclSpec::TQ_const) |
| 3332 | B << FixItHint::CreateRemoval(ConstexprLoc); |
| 3333 | else { |
| 3334 | B << FixItHint::CreateReplacement(ConstexprLoc, "const"); |
| 3335 | D.getMutableDeclSpec().ClearConstexprSpec(); |
| 3336 | const char *PrevSpec; |
| 3337 | unsigned DiagID; |
| 3338 | bool Failed = D.getMutableDeclSpec().SetTypeQual( |
| 3339 | DeclSpec::TQ_const, ConstexprLoc, PrevSpec, DiagID, getLangOpts()); |
| 3340 | (void)Failed; |
| 3341 | assert(!Failed && "Making a constexpr member const shouldn't fail"); |
| 3342 | } |
| 3343 | } else { |
| 3344 | B << 1; |
| 3345 | const char *PrevSpec; |
| 3346 | unsigned DiagID; |
| 3347 | if (D.getMutableDeclSpec().SetStorageClassSpec( |
| 3348 | *this, DeclSpec::SCS_static, ConstexprLoc, PrevSpec, DiagID, |
| 3349 | Context.getPrintingPolicy())) { |
| 3350 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_mutable && |
| 3351 | "This is the only DeclSpec that should fail to be applied"); |
| 3352 | B << 1; |
| 3353 | } else { |
| 3354 | B << 0 << FixItHint::CreateInsertion(ConstexprLoc, "static "); |
| 3355 | isInstField = false; |
| 3356 | } |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | NamedDecl *Member; |
| 3361 | if (isInstField) { |
| 3362 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 3363 | |
| 3364 | |
| 3365 | if (!Name.isIdentifier()) { |
| 3366 | Diag(Loc, diag::err_bad_variable_name) |
| 3367 | << Name; |
| 3368 | return nullptr; |
| 3369 | } |
| 3370 | |
| 3371 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 3372 | |
| 3373 | |
| 3374 | |
| 3375 | if (TemplateParameterLists.size()) { |
| 3376 | TemplateParameterList* TemplateParams = TemplateParameterLists[0]; |
| 3377 | if (TemplateParams->size()) { |
| 3378 | |
| 3379 | Diag(D.getIdentifierLoc(), diag::err_template_member) |
| 3380 | << II |
| 3381 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 3382 | TemplateParams->getRAngleLoc()); |
| 3383 | } else { |
| 3384 | |
| 3385 | Diag(TemplateParams->getTemplateLoc(), |
| 3386 | diag::err_template_member_noparams) |
| 3387 | << II |
| 3388 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 3389 | TemplateParams->getRAngleLoc()); |
| 3390 | } |
| 3391 | return nullptr; |
| 3392 | } |
| 3393 | |
| 3394 | if (SS.isSet() && !SS.isInvalid()) { |
| 3395 | |
| 3396 | |
| 3397 | |
| 3398 | |
| 3399 | |
| 3400 | |
| 3401 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 3402 | diagnoseQualifiedDeclaration(SS, DC, Name, D.getIdentifierLoc(), |
| 3403 | D.getName().getKind() == |
| 3404 | UnqualifiedIdKind::IK_TemplateId); |
| 3405 | else |
| 3406 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 3407 | << Name << SS.getRange(); |
| 3408 | |
| 3409 | SS.clear(); |
| 3410 | } |
| 3411 | |
| 3412 | if (MSPropertyAttr) { |
| 3413 | Member = HandleMSProperty(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 3414 | BitWidth, InitStyle, AS, *MSPropertyAttr); |
| 3415 | if (!Member) |
| 3416 | return nullptr; |
| 3417 | isInstField = false; |
| 3418 | } else { |
| 3419 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 3420 | BitWidth, InitStyle, AS); |
| 3421 | if (!Member) |
| 3422 | return nullptr; |
| 3423 | } |
| 3424 | |
| 3425 | CheckShadowInheritedFields(Loc, Name, cast<CXXRecordDecl>(CurContext)); |
| 3426 | } else { |
| 3427 | Member = HandleDeclarator(S, D, TemplateParameterLists); |
| 3428 | if (!Member) |
| 3429 | return nullptr; |
| 3430 | |
| 3431 | |
| 3432 | if (BitWidth) { |
| 3433 | if (Member->isInvalidDecl()) { |
| 3434 | |
| 3435 | } else if (isa<VarDecl>(Member) || isa<VarTemplateDecl>(Member)) { |
| 3436 | |
| 3437 | |
| 3438 | Diag(Loc, diag::err_static_not_bitfield) |
| 3439 | << Name << BitWidth->getSourceRange(); |
| 3440 | } else if (isa<TypedefDecl>(Member)) { |
| 3441 | |
| 3442 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 3443 | << Name << BitWidth->getSourceRange(); |
| 3444 | } else { |
| 3445 | |
| 3446 | |
| 3447 | Diag(Loc, diag::err_not_integral_type_bitfield) |
| 3448 | << Name << cast<ValueDecl>(Member)->getType() |
| 3449 | << BitWidth->getSourceRange(); |
| 3450 | } |
| 3451 | |
| 3452 | BitWidth = nullptr; |
| 3453 | Member->setInvalidDecl(); |
| 3454 | } |
| 3455 | |
| 3456 | NamedDecl *NonTemplateMember = Member; |
| 3457 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 3458 | NonTemplateMember = FunTmpl->getTemplatedDecl(); |
| 3459 | else if (VarTemplateDecl *VarTmpl = dyn_cast<VarTemplateDecl>(Member)) |
| 3460 | NonTemplateMember = VarTmpl->getTemplatedDecl(); |
| 3461 | |
| 3462 | Member->setAccess(AS); |
| 3463 | |
| 3464 | |
| 3465 | |
| 3466 | if (NonTemplateMember != Member) |
| 3467 | NonTemplateMember->setAccess(AS); |
| 3468 | |
| 3469 | |
| 3470 | |
| 3471 | |
| 3472 | if (auto *DG = dyn_cast<CXXDeductionGuideDecl>(NonTemplateMember)) { |
| 3473 | auto *TD = DG->getDeducedTemplate(); |
| 3474 | |
| 3475 | |
| 3476 | if (AS != TD->getAccess() && |
| 3477 | TD->getDeclContext()->getRedeclContext()->Equals( |
| 3478 | DG->getDeclContext()->getRedeclContext())) { |
| 3479 | Diag(DG->getBeginLoc(), diag::err_deduction_guide_wrong_access); |
| 3480 | Diag(TD->getBeginLoc(), diag::note_deduction_guide_template_access) |
| 3481 | << TD->getAccess(); |
| 3482 | const AccessSpecDecl *LastAccessSpec = nullptr; |
| 3483 | for (const auto *D : cast<CXXRecordDecl>(CurContext)->decls()) { |
| 3484 | if (const auto *AccessSpec = dyn_cast<AccessSpecDecl>(D)) |
| 3485 | LastAccessSpec = AccessSpec; |
| 3486 | } |
| 3487 | assert(LastAccessSpec && "differing access with no access specifier"); |
| 3488 | Diag(LastAccessSpec->getBeginLoc(), diag::note_deduction_guide_access) |
| 3489 | << AS; |
| 3490 | } |
| 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | if (VS.isOverrideSpecified()) |
| 3495 | Member->addAttr(OverrideAttr::Create(Context, VS.getOverrideLoc(), |
| 3496 | AttributeCommonInfo::AS_Keyword)); |
| 3497 | if (VS.isFinalSpecified()) |
| 3498 | Member->addAttr(FinalAttr::Create( |
| 3499 | Context, VS.getFinalLoc(), AttributeCommonInfo::AS_Keyword, |
| 3500 | static_cast<FinalAttr::Spelling>(VS.isFinalSpelledSealed()))); |
| 3501 | |
| 3502 | if (VS.getLastLocation().isValid()) { |
| 3503 | |
| 3504 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 3505 | MD->setRangeEnd(VS.getLastLocation()); |
| 3506 | } |
| 3507 | |
| 3508 | CheckOverrideControl(Member); |
| 3509 | |
| 3510 | assert((Name || isInstField) && "No identifier for non-field ?"); |
| 3511 | |
| 3512 | if (isInstField) { |
| 3513 | FieldDecl *FD = cast<FieldDecl>(Member); |
| 3514 | FieldCollector->Add(FD); |
| 3515 | |
| 3516 | if (!Diags.isIgnored(diag::warn_unused_private_field, FD->getLocation())) { |
| 3517 | |
| 3518 | |
| 3519 | if (!FD->isImplicit() && FD->getDeclName() && |
| 3520 | FD->getAccess() == AS_private && |
| 3521 | !FD->hasAttr<UnusedAttr>() && |
| 3522 | !FD->getParent()->isDependentContext() && |
| 3523 | !InitializationHasSideEffects(*FD)) |
| 3524 | UnusedPrivateFields.insert(FD); |
| 3525 | } |
| 3526 | } |
| 3527 | |
| 3528 | return Member; |
| 3529 | } |
| 3530 | |
| 3531 | namespace { |
| 3532 | class UninitializedFieldVisitor |
| 3533 | : public EvaluatedExprVisitor<UninitializedFieldVisitor> { |
| 3534 | Sema &S; |
| 3535 | |
| 3536 | |
| 3537 | llvm::SmallPtrSetImpl<ValueDecl*> &Decls; |
| 3538 | |
| 3539 | |
| 3540 | llvm::SmallPtrSetImpl<QualType> &BaseClasses; |
| 3541 | |
| 3542 | |
| 3543 | llvm::SmallVector<ValueDecl*, 4> DeclsToRemove; |
| 3544 | |
| 3545 | const CXXConstructorDecl *Constructor; |
| 3546 | |
| 3547 | |
| 3548 | |
| 3549 | bool InitList; |
| 3550 | FieldDecl *InitListFieldDecl; |
| 3551 | llvm::SmallVector<unsigned, 4> InitFieldIndex; |
| 3552 | |
| 3553 | public: |
| 3554 | typedef EvaluatedExprVisitor<UninitializedFieldVisitor> Inherited; |
| 3555 | UninitializedFieldVisitor(Sema &S, |
| 3556 | llvm::SmallPtrSetImpl<ValueDecl*> &Decls, |
| 3557 | llvm::SmallPtrSetImpl<QualType> &BaseClasses) |
| 3558 | : Inherited(S.Context), S(S), Decls(Decls), BaseClasses(BaseClasses), |
| 3559 | Constructor(nullptr), InitList(false), InitListFieldDecl(nullptr) {} |
| 3560 | |
| 3561 | |
| 3562 | bool IsInitListMemberExprInitialized(MemberExpr *ME, |
| 3563 | bool CheckReferenceOnly) { |
| 3564 | llvm::SmallVector<FieldDecl*, 4> Fields; |
| 3565 | bool ReferenceField = false; |
| 3566 | while (ME) { |
| 3567 | FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| 3568 | if (!FD) |
| 3569 | return false; |
| 3570 | Fields.push_back(FD); |
| 3571 | if (FD->getType()->isReferenceType()) |
| 3572 | ReferenceField = true; |
| 3573 | ME = dyn_cast<MemberExpr>(ME->getBase()->IgnoreParenImpCasts()); |
| 3574 | } |
| 3575 | |
| 3576 | |
| 3577 | |
| 3578 | if (CheckReferenceOnly && !ReferenceField) |
| 3579 | return true; |
| 3580 | |
| 3581 | llvm::SmallVector<unsigned, 4> UsedFieldIndex; |
| 3582 | |
| 3583 | |
| 3584 | for (auto I = Fields.rbegin() + 1, E = Fields.rend(); I != E; ++I) { |
| 3585 | UsedFieldIndex.push_back((*I)->getFieldIndex()); |
| 3586 | } |
| 3587 | |
| 3588 | for (auto UsedIter = UsedFieldIndex.begin(), |
| 3589 | UsedEnd = UsedFieldIndex.end(), |
| 3590 | OrigIter = InitFieldIndex.begin(), |
| 3591 | OrigEnd = InitFieldIndex.end(); |
| 3592 | UsedIter != UsedEnd && OrigIter != OrigEnd; ++UsedIter, ++OrigIter) { |
| 3593 | if (*UsedIter < *OrigIter) |
| 3594 | return true; |
| 3595 | if (*UsedIter > *OrigIter) |
| 3596 | break; |
| 3597 | } |
| 3598 | |
| 3599 | return false; |
| 3600 | } |
| 3601 | |
| 3602 | void HandleMemberExpr(MemberExpr *ME, bool CheckReferenceOnly, |
| 3603 | bool AddressOf) { |
| 3604 | if (isa<EnumConstantDecl>(ME->getMemberDecl())) |
| 3605 | return; |
| 3606 | |
| 3607 | |
| 3608 | |
| 3609 | MemberExpr *FieldME = ME; |
| 3610 | |
| 3611 | bool AllPODFields = FieldME->getType().isPODType(S.Context); |
| 3612 | |
| 3613 | Expr *Base = ME; |
| 3614 | while (MemberExpr *SubME = |
| 3615 | dyn_cast<MemberExpr>(Base->IgnoreParenImpCasts())) { |
| 3616 | |
| 3617 | if (isa<VarDecl>(SubME->getMemberDecl())) |
| 3618 | return; |
| 3619 | |
| 3620 | if (FieldDecl *FD = dyn_cast<FieldDecl>(SubME->getMemberDecl())) |
| 3621 | if (!FD->isAnonymousStructOrUnion()) |
| 3622 | FieldME = SubME; |
| 3623 | |
| 3624 | if (!FieldME->getType().isPODType(S.Context)) |
| 3625 | AllPODFields = false; |
| 3626 | |
| 3627 | Base = SubME->getBase(); |
| 3628 | } |
| 3629 | |
| 3630 | if (!isa<CXXThisExpr>(Base->IgnoreParenImpCasts())) { |
| 3631 | Visit(Base); |
| 3632 | return; |
| 3633 | } |
| 3634 | |
| 3635 | if (AddressOf && AllPODFields) |
| 3636 | return; |
| 3637 | |
| 3638 | ValueDecl* FoundVD = FieldME->getMemberDecl(); |
| 3639 | |
| 3640 | if (ImplicitCastExpr *BaseCast = dyn_cast<ImplicitCastExpr>(Base)) { |
| 3641 | while (isa<ImplicitCastExpr>(BaseCast->getSubExpr())) { |
| 3642 | BaseCast = cast<ImplicitCastExpr>(BaseCast->getSubExpr()); |
| 3643 | } |
| 3644 | |
| 3645 | if (BaseCast->getCastKind() == CK_UncheckedDerivedToBase) { |
| 3646 | QualType T = BaseCast->getType(); |
| 3647 | if (T->isPointerType() && |
| 3648 | BaseClasses.count(T->getPointeeType())) { |
| 3649 | S.Diag(FieldME->getExprLoc(), diag::warn_base_class_is_uninit) |
| 3650 | << T->getPointeeType() << FoundVD; |
| 3651 | } |
| 3652 | } |
| 3653 | } |
| 3654 | |
| 3655 | if (!Decls.count(FoundVD)) |
| 3656 | return; |
| 3657 | |
| 3658 | const bool IsReference = FoundVD->getType()->isReferenceType(); |
| 3659 | |
| 3660 | if (InitList && !AddressOf && FoundVD == InitListFieldDecl) { |
| 3661 | |
| 3662 | if (IsInitListMemberExprInitialized(ME, CheckReferenceOnly)) { |
| 3663 | return; |
| 3664 | } |
| 3665 | } else { |
| 3666 | |
| 3667 | if (CheckReferenceOnly && !IsReference) |
| 3668 | return; |
| 3669 | } |
| 3670 | |
| 3671 | unsigned diag = IsReference |
| 3672 | ? diag::warn_reference_field_is_uninit |
| 3673 | : diag::warn_field_is_uninit; |
| 3674 | S.Diag(FieldME->getExprLoc(), diag) << FoundVD; |
| 3675 | if (Constructor) |
| 3676 | S.Diag(Constructor->getLocation(), |
| 3677 | diag::note_uninit_in_this_constructor) |
| 3678 | << (Constructor->isDefaultConstructor() && Constructor->isImplicit()); |
| 3679 | |
| 3680 | } |
| 3681 | |
| 3682 | void HandleValue(Expr *E, bool AddressOf) { |
| 3683 | E = E->IgnoreParens(); |
| 3684 | |
| 3685 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 3686 | HandleMemberExpr(ME, false , |
| 3687 | AddressOf ); |
| 3688 | return; |
| 3689 | } |
| 3690 | |
| 3691 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 3692 | Visit(CO->getCond()); |
| 3693 | HandleValue(CO->getTrueExpr(), AddressOf); |
| 3694 | HandleValue(CO->getFalseExpr(), AddressOf); |
| 3695 | return; |
| 3696 | } |
| 3697 | |
| 3698 | if (BinaryConditionalOperator *BCO = |
| 3699 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 3700 | Visit(BCO->getCond()); |
| 3701 | HandleValue(BCO->getFalseExpr(), AddressOf); |
| 3702 | return; |
| 3703 | } |
| 3704 | |
| 3705 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) { |
| 3706 | HandleValue(OVE->getSourceExpr(), AddressOf); |
| 3707 | return; |
| 3708 | } |
| 3709 | |
| 3710 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 3711 | switch (BO->getOpcode()) { |
| 3712 | default: |
| 3713 | break; |
| 3714 | case(BO_PtrMemD): |
| 3715 | case(BO_PtrMemI): |
| 3716 | HandleValue(BO->getLHS(), AddressOf); |
| 3717 | Visit(BO->getRHS()); |
| 3718 | return; |
| 3719 | case(BO_Comma): |
| 3720 | Visit(BO->getLHS()); |
| 3721 | HandleValue(BO->getRHS(), AddressOf); |
| 3722 | return; |
| 3723 | } |
| 3724 | } |
| 3725 | |
| 3726 | Visit(E); |
| 3727 | } |
| 3728 | |
| 3729 | void CheckInitListExpr(InitListExpr *ILE) { |
| 3730 | InitFieldIndex.push_back(0); |
| 3731 | for (auto Child : ILE->children()) { |
| 3732 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(Child)) { |
| 3733 | CheckInitListExpr(SubList); |
| 3734 | } else { |
| 3735 | Visit(Child); |
| 3736 | } |
| 3737 | ++InitFieldIndex.back(); |
| 3738 | } |
| 3739 | InitFieldIndex.pop_back(); |
| 3740 | } |
| 3741 | |
| 3742 | void CheckInitializer(Expr *E, const CXXConstructorDecl *FieldConstructor, |
| 3743 | FieldDecl *Field, const Type *BaseClass) { |
| 3744 | |
| 3745 | |
| 3746 | for (ValueDecl* VD : DeclsToRemove) |
| 3747 | Decls.erase(VD); |
| 3748 | DeclsToRemove.clear(); |
| 3749 | |
| 3750 | Constructor = FieldConstructor; |
| 3751 | InitListExpr *ILE = dyn_cast<InitListExpr>(E); |
| 3752 | |
| 3753 | if (ILE && Field) { |
| 3754 | InitList = true; |
| 3755 | InitListFieldDecl = Field; |
| 3756 | InitFieldIndex.clear(); |
| 3757 | CheckInitListExpr(ILE); |
| 3758 | } else { |
| 3759 | InitList = false; |
| 3760 | Visit(E); |
| 3761 | } |
| 3762 | |
| 3763 | if (Field) |
| 3764 | Decls.erase(Field); |
| 3765 | if (BaseClass) |
| 3766 | BaseClasses.erase(BaseClass->getCanonicalTypeInternal()); |
| 3767 | } |
| 3768 | |
| 3769 | void VisitMemberExpr(MemberExpr *ME) { |
| 3770 | |
| 3771 | HandleMemberExpr(ME, true , false ); |
| 3772 | } |
| 3773 | |
| 3774 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 3775 | if (E->getCastKind() == CK_LValueToRValue) { |
| 3776 | HandleValue(E->getSubExpr(), false ); |
| 3777 | return; |
| 3778 | } |
| 3779 | |
| 3780 | Inherited::VisitImplicitCastExpr(E); |
| 3781 | } |
| 3782 | |
| 3783 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 3784 | if (E->getConstructor()->isCopyConstructor()) { |
| 3785 | Expr *ArgExpr = E->getArg(0); |
| 3786 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(ArgExpr)) |
| 3787 | if (ILE->getNumInits() == 1) |
| 3788 | ArgExpr = ILE->getInit(0); |
| 3789 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 3790 | if (ICE->getCastKind() == CK_NoOp) |
| 3791 | ArgExpr = ICE->getSubExpr(); |
| 3792 | HandleValue(ArgExpr, false ); |
| 3793 | return; |
| 3794 | } |
| 3795 | Inherited::VisitCXXConstructExpr(E); |
| 3796 | } |
| 3797 | |
| 3798 | void VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 3799 | Expr *Callee = E->getCallee(); |
| 3800 | if (isa<MemberExpr>(Callee)) { |
| 3801 | HandleValue(Callee, false ); |
| 3802 | for (auto Arg : E->arguments()) |
| 3803 | Visit(Arg); |
| 3804 | return; |
| 3805 | } |
| 3806 | |
| 3807 | Inherited::VisitCXXMemberCallExpr(E); |
| 3808 | } |
| 3809 | |
| 3810 | void VisitCallExpr(CallExpr *E) { |
| 3811 | |
| 3812 | if (E->isCallToStdMove()) { |
| 3813 | HandleValue(E->getArg(0), false); |
| 3814 | return; |
| 3815 | } |
| 3816 | |
| 3817 | Inherited::VisitCallExpr(E); |
| 3818 | } |
| 3819 | |
| 3820 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { |
| 3821 | Expr *Callee = E->getCallee(); |
| 3822 | |
| 3823 | if (isa<UnresolvedLookupExpr>(Callee)) |
| 3824 | return Inherited::VisitCXXOperatorCallExpr(E); |
| 3825 | |
| 3826 | Visit(Callee); |
| 3827 | for (auto Arg : E->arguments()) |
| 3828 | HandleValue(Arg->IgnoreParenImpCasts(), false ); |
| 3829 | } |
| 3830 | |
| 3831 | void VisitBinaryOperator(BinaryOperator *E) { |
| 3832 | |
| 3833 | |
| 3834 | if (E->getOpcode() == BO_Assign) |
| 3835 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E->getLHS())) |
| 3836 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 3837 | if (!FD->getType()->isReferenceType()) |
| 3838 | DeclsToRemove.push_back(FD); |
| 3839 | |
| 3840 | if (E->isCompoundAssignmentOp()) { |
| 3841 | HandleValue(E->getLHS(), false ); |
| 3842 | Visit(E->getRHS()); |
| 3843 | return; |
| 3844 | } |
| 3845 | |
| 3846 | Inherited::VisitBinaryOperator(E); |
| 3847 | } |
| 3848 | |
| 3849 | void VisitUnaryOperator(UnaryOperator *E) { |
| 3850 | if (E->isIncrementDecrementOp()) { |
| 3851 | HandleValue(E->getSubExpr(), false ); |
| 3852 | return; |
| 3853 | } |
| 3854 | if (E->getOpcode() == UO_AddrOf) { |
| 3855 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E->getSubExpr())) { |
| 3856 | HandleValue(ME->getBase(), true ); |
| 3857 | return; |
| 3858 | } |
| 3859 | } |
| 3860 | |
| 3861 | Inherited::VisitUnaryOperator(E); |
| 3862 | } |
| 3863 | }; |
| 3864 | |
| 3865 | |
| 3866 | |
| 3867 | |
| 3868 | |
| 3869 | |
| 3870 | |
| 3871 | static void DiagnoseUninitializedFields( |
| 3872 | Sema &SemaRef, const CXXConstructorDecl *Constructor) { |
| 3873 | |
| 3874 | if (SemaRef.getDiagnostics().isIgnored(diag::warn_field_is_uninit, |
| 3875 | Constructor->getLocation())) { |
| 3876 | return; |
| 3877 | } |
| 3878 | |
| 3879 | if (Constructor->isInvalidDecl()) |
| 3880 | return; |
| 3881 | |
| 3882 | const CXXRecordDecl *RD = Constructor->getParent(); |
| 3883 | |
| 3884 | if (RD->isDependentContext()) |
| 3885 | return; |
| 3886 | |
| 3887 | |
| 3888 | llvm::SmallPtrSet<ValueDecl*, 4> UninitializedFields; |
| 3889 | |
| 3890 | |
| 3891 | for (auto *I : RD->decls()) { |
| 3892 | if (auto *FD = dyn_cast<FieldDecl>(I)) { |
| 3893 | UninitializedFields.insert(FD); |
| 3894 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(I)) { |
| 3895 | UninitializedFields.insert(IFD->getAnonField()); |
| 3896 | } |
| 3897 | } |
| 3898 | |
| 3899 | llvm::SmallPtrSet<QualType, 4> UninitializedBaseClasses; |
| 3900 | for (auto I : RD->bases()) |
| 3901 | UninitializedBaseClasses.insert(I.getType().getCanonicalType()); |
| 3902 | |
| 3903 | if (UninitializedFields.empty() && UninitializedBaseClasses.empty()) |
| 3904 | return; |
| 3905 | |
| 3906 | UninitializedFieldVisitor UninitializedChecker(SemaRef, |
| 3907 | UninitializedFields, |
| 3908 | UninitializedBaseClasses); |
| 3909 | |
| 3910 | for (const auto *FieldInit : Constructor->inits()) { |
| 3911 | if (UninitializedFields.empty() && UninitializedBaseClasses.empty()) |
| 3912 | break; |
| 3913 | |
| 3914 | Expr *InitExpr = FieldInit->getInit(); |
| 3915 | if (!InitExpr) |
| 3916 | continue; |
| 3917 | |
| 3918 | if (CXXDefaultInitExpr *Default = |
| 3919 | dyn_cast<CXXDefaultInitExpr>(InitExpr)) { |
| 3920 | InitExpr = Default->getExpr(); |
| 3921 | if (!InitExpr) |
| 3922 | continue; |
| 3923 | |
| 3924 | UninitializedChecker.CheckInitializer(InitExpr, Constructor, |
| 3925 | FieldInit->getAnyMember(), |
| 3926 | FieldInit->getBaseClass()); |
| 3927 | } else { |
| 3928 | UninitializedChecker.CheckInitializer(InitExpr, nullptr, |
| 3929 | FieldInit->getAnyMember(), |
| 3930 | FieldInit->getBaseClass()); |
| 3931 | } |
| 3932 | } |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | |
| 3937 | |
| 3938 | |
| 3939 | void Sema::ActOnStartCXXInClassMemberInitializer() { |
| 3940 | |
| 3941 | |
| 3942 | PushFunctionScope(); |
| 3943 | } |
| 3944 | |
| 3945 | void Sema::ActOnStartTrailingRequiresClause(Scope *S, Declarator &D) { |
| 3946 | if (!D.isFunctionDeclarator()) |
| 3947 | return; |
| 3948 | auto &FTI = D.getFunctionTypeInfo(); |
| 3949 | if (!FTI.Params) |
| 3950 | return; |
| 3951 | for (auto &Param : ArrayRef<DeclaratorChunk::ParamInfo>(FTI.Params, |
| 3952 | FTI.NumParams)) { |
| 3953 | auto *ParamDecl = cast<NamedDecl>(Param.Param); |
| 3954 | if (ParamDecl->getDeclName()) |
| 3955 | PushOnScopeChains(ParamDecl, S, false); |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | ExprResult Sema::ActOnFinishTrailingRequiresClause(ExprResult ConstraintExpr) { |
| 3960 | return ActOnRequiresClause(ConstraintExpr); |
| 3961 | } |
| 3962 | |
| 3963 | ExprResult Sema::ActOnRequiresClause(ExprResult ConstraintExpr) { |
| 3964 | if (ConstraintExpr.isInvalid()) |
| 3965 | return ExprError(); |
| 3966 | |
| 3967 | ConstraintExpr = CorrectDelayedTyposInExpr(ConstraintExpr); |
| 3968 | if (ConstraintExpr.isInvalid()) |
| 3969 | return ExprError(); |
| 3970 | |
| 3971 | if (DiagnoseUnexpandedParameterPack(ConstraintExpr.get(), |
| 3972 | UPPC_RequiresClause)) |
| 3973 | return ExprError(); |
| 3974 | |
| 3975 | return ConstraintExpr; |
| 3976 | } |
| 3977 | |
| 3978 | |
| 3979 | |
| 3980 | |
| 3981 | void Sema::ActOnFinishCXXInClassMemberInitializer(Decl *D, |
| 3982 | SourceLocation InitLoc, |
| 3983 | Expr *InitExpr) { |
| 3984 | |
| 3985 | PopFunctionScopeInfo(nullptr, D); |
| 3986 | |
| 3987 | FieldDecl *FD = dyn_cast<FieldDecl>(D); |
| 3988 | assert((isa<MSPropertyDecl>(D) || FD->getInClassInitStyle() != ICIS_NoInit) && |
| 3989 | "must set init style when field is created"); |
| 3990 | |
| 3991 | if (!InitExpr) { |
| 3992 | D->setInvalidDecl(); |
| 3993 | if (FD) |
| 3994 | FD->removeInClassInitializer(); |
| 3995 | return; |
| 3996 | } |
| 3997 | |
| 3998 | if (DiagnoseUnexpandedParameterPack(InitExpr, UPPC_Initializer)) { |
| 3999 | FD->setInvalidDecl(); |
| 4000 | FD->removeInClassInitializer(); |
| 4001 | return; |
| 4002 | } |
| 4003 | |
| 4004 | ExprResult Init = InitExpr; |
| 4005 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
| 4006 | InitializedEntity Entity = |
| 4007 | InitializedEntity::InitializeMemberFromDefaultMemberInitializer(FD); |
| 4008 | InitializationKind Kind = |
| 4009 | FD->getInClassInitStyle() == ICIS_ListInit |
| 4010 | ? InitializationKind::CreateDirectList(InitExpr->getBeginLoc(), |
| 4011 | InitExpr->getBeginLoc(), |
| 4012 | InitExpr->getEndLoc()) |
| 4013 | : InitializationKind::CreateCopy(InitExpr->getBeginLoc(), InitLoc); |
| 4014 | InitializationSequence Seq(*this, Entity, Kind, InitExpr); |
| 4015 | Init = Seq.Perform(*this, Entity, Kind, InitExpr); |
| 4016 | if (Init.isInvalid()) { |
| 4017 | FD->setInvalidDecl(); |
| 4018 | return; |
| 4019 | } |
| 4020 | } |
| 4021 | |
| 4022 | |
| 4023 | |
| 4024 | |
| 4025 | Init = ActOnFinishFullExpr(Init.get(), InitLoc, false); |
| 4026 | if (Init.isInvalid()) { |
| 4027 | FD->setInvalidDecl(); |
| 4028 | return; |
| 4029 | } |
| 4030 | |
| 4031 | InitExpr = Init.get(); |
| 4032 | |
| 4033 | FD->setInClassInitializer(InitExpr); |
| 4034 | } |
| 4035 | |
| 4036 | |
| 4037 | |
| 4038 | |
| 4039 | static bool FindBaseInitializer(Sema &SemaRef, |
| 4040 | CXXRecordDecl *ClassDecl, |
| 4041 | QualType BaseType, |
| 4042 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 4043 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 4044 | |
| 4045 | DirectBaseSpec = nullptr; |
| 4046 | for (const auto &Base : ClassDecl->bases()) { |
| 4047 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base.getType())) { |
| 4048 | |
| 4049 | |
| 4050 | DirectBaseSpec = &Base; |
| 4051 | break; |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | |
| 4056 | |
| 4057 | |
| 4058 | VirtualBaseSpec = nullptr; |
| 4059 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 4060 | |
| 4061 | |
| 4062 | CXXBasePaths Paths(true, true, |
| 4063 | false); |
| 4064 | if (SemaRef.IsDerivedFrom(ClassDecl->getLocation(), |
| 4065 | SemaRef.Context.getTypeDeclType(ClassDecl), |
| 4066 | BaseType, Paths)) { |
| 4067 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 4068 | Path != Paths.end(); ++Path) { |
| 4069 | if (Path->back().Base->isVirtual()) { |
| 4070 | VirtualBaseSpec = Path->back().Base; |
| 4071 | break; |
| 4072 | } |
| 4073 | } |
| 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | return DirectBaseSpec || VirtualBaseSpec; |
| 4078 | } |
| 4079 | |
| 4080 | |
| 4081 | MemInitResult |
| 4082 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| 4083 | Scope *S, |
| 4084 | CXXScopeSpec &SS, |
| 4085 | IdentifierInfo *MemberOrBase, |
| 4086 | ParsedType TemplateTypeTy, |
| 4087 | const DeclSpec &DS, |
| 4088 | SourceLocation IdLoc, |
| 4089 | Expr *InitList, |
| 4090 | SourceLocation EllipsisLoc) { |
| 4091 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
| 4092 | DS, IdLoc, InitList, |
| 4093 | EllipsisLoc); |
| 4094 | } |
| 4095 | |
| 4096 | |
| 4097 | MemInitResult |
| 4098 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| 4099 | Scope *S, |
| 4100 | CXXScopeSpec &SS, |
| 4101 | IdentifierInfo *MemberOrBase, |
| 4102 | ParsedType TemplateTypeTy, |
| 4103 | const DeclSpec &DS, |
| 4104 | SourceLocation IdLoc, |
| 4105 | SourceLocation LParenLoc, |
| 4106 | ArrayRef<Expr *> Args, |
| 4107 | SourceLocation RParenLoc, |
| 4108 | SourceLocation EllipsisLoc) { |
| 4109 | Expr *List = ParenListExpr::Create(Context, LParenLoc, Args, RParenLoc); |
| 4110 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
| 1 | Calling 'Sema::BuildMemInitializer' | |
|
| 4111 | DS, IdLoc, List, EllipsisLoc); |
| 4112 | } |
| 4113 | |
| 4114 | namespace { |
| 4115 | |
| 4116 | |
| 4117 | |
| 4118 | class MemInitializerValidatorCCC final : public CorrectionCandidateCallback { |
| 4119 | public: |
| 4120 | explicit MemInitializerValidatorCCC(CXXRecordDecl *ClassDecl) |
| 4121 | : ClassDecl(ClassDecl) {} |
| 4122 | |
| 4123 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 4124 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 4125 | if (FieldDecl *Member = dyn_cast<FieldDecl>(ND)) |
| 4126 | return Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl); |
| 4127 | return isa<TypeDecl>(ND); |
| 4128 | } |
| 4129 | return false; |
| 4130 | } |
| 4131 | |
| 4132 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 4133 | return std::make_unique<MemInitializerValidatorCCC>(*this); |
| 4134 | } |
| 4135 | |
| 4136 | private: |
| 4137 | CXXRecordDecl *ClassDecl; |
| 4138 | }; |
| 4139 | |
| 4140 | } |
| 4141 | |
| 4142 | ValueDecl *Sema::tryLookupCtorInitMemberDecl(CXXRecordDecl *ClassDecl, |
| 4143 | CXXScopeSpec &SS, |
| 4144 | ParsedType TemplateTypeTy, |
| 4145 | IdentifierInfo *MemberOrBase) { |
| 4146 | if (SS.getScopeRep() || TemplateTypeTy) |
| 4147 | return nullptr; |
| 4148 | for (auto *D : ClassDecl->lookup(MemberOrBase)) |
| 4149 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) |
| 4150 | return cast<ValueDecl>(D); |
| 4151 | return nullptr; |
| 4152 | } |
| 4153 | |
| 4154 | |
| 4155 | MemInitResult |
| 4156 | Sema::BuildMemInitializer(Decl *ConstructorD, |
| 4157 | Scope *S, |
| 4158 | CXXScopeSpec &SS, |
| 4159 | IdentifierInfo *MemberOrBase, |
| 4160 | ParsedType TemplateTypeTy, |
| 4161 | const DeclSpec &DS, |
| 4162 | SourceLocation IdLoc, |
| 4163 | Expr *Init, |
| 4164 | SourceLocation EllipsisLoc) { |
| 4165 | ExprResult Res = CorrectDelayedTyposInExpr(Init); |
| 4166 | if (!Res.isUsable()) |
| |
| 4167 | return true; |
| 4168 | Init = Res.get(); |
| 4169 | |
| 4170 | if (!ConstructorD) |
| 3 | | Assuming 'ConstructorD' is non-null | |
|
| |
| 4171 | return true; |
| 4172 | |
| 4173 | AdjustDeclIfTemplate(ConstructorD); |
| 4174 | |
| 4175 | CXXConstructorDecl *Constructor |
| 4176 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
| 5 | | Assuming 'ConstructorD' is a 'CXXConstructorDecl' | |
|
| 4177 | if (!Constructor) { |
| |
| 4178 | |
| 4179 | |
| 4180 | |
| 4181 | |
| 4182 | return true; |
| 4183 | } |
| 4184 | |
| 4185 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4186 | |
| 4187 | |
| 4188 | |
| 4189 | |
| 4190 | |
| 4191 | |
| 4192 | |
| 4193 | |
| 4194 | |
| 4195 | |
| 4196 | |
| 4197 | |
| 4198 | |
| 4199 | if (ValueDecl *Member = tryLookupCtorInitMemberDecl( |
| |
| 4200 | ClassDecl, SS, TemplateTypeTy, MemberOrBase)) { |
| 4201 | if (EllipsisLoc.isValid()) |
| |
| 4202 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 4203 | << MemberOrBase |
| 4204 | << SourceRange(IdLoc, Init->getSourceRange().getEnd()); |
| 4205 | |
| 4206 | return BuildMemberInitializer(Member, Init, IdLoc); |
| 9 | | Calling 'Sema::BuildMemberInitializer' | |
|
| 4207 | } |
| 4208 | |
| 4209 | QualType BaseType; |
| 4210 | TypeSourceInfo *TInfo = nullptr; |
| 4211 | |
| 4212 | if (TemplateTypeTy) { |
| 4213 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
| 4214 | if (BaseType.isNull()) |
| 4215 | return true; |
| 4216 | } else if (DS.getTypeSpecType() == TST_decltype) { |
| 4217 | BaseType = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 4218 | } else if (DS.getTypeSpecType() == TST_decltype_auto) { |
| 4219 | Diag(DS.getTypeSpecTypeLoc(), diag::err_decltype_auto_invalid); |
| 4220 | return true; |
| 4221 | } else { |
| 4222 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 4223 | LookupParsedName(R, S, &SS); |
| 4224 | |
| 4225 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 4226 | if (!TyD) { |
| 4227 | if (R.isAmbiguous()) return true; |
| 4228 | |
| 4229 | |
| 4230 | R.suppressDiagnostics(); |
| 4231 | |
| 4232 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 4233 | bool NotUnknownSpecialization = false; |
| 4234 | DeclContext *DC = computeDeclContext(SS, false); |
| 4235 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 4236 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 4237 | |
| 4238 | if (!NotUnknownSpecialization) { |
| 4239 | |
| 4240 | |
| 4241 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 4242 | SS.getWithLocInContext(Context), |
| 4243 | *MemberOrBase, IdLoc); |
| 4244 | if (BaseType.isNull()) |
| 4245 | return true; |
| 4246 | |
| 4247 | TInfo = Context.CreateTypeSourceInfo(BaseType); |
| 4248 | DependentNameTypeLoc TL = |
| 4249 | TInfo->getTypeLoc().castAs<DependentNameTypeLoc>(); |
| 4250 | if (!TL.isNull()) { |
| 4251 | TL.setNameLoc(IdLoc); |
| 4252 | TL.setElaboratedKeywordLoc(SourceLocation()); |
| 4253 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 4254 | } |
| 4255 | |
| 4256 | R.clear(); |
| 4257 | R.setLookupName(MemberOrBase); |
| 4258 | } |
| 4259 | } |
| 4260 | |
| 4261 | |
| 4262 | TypoCorrection Corr; |
| 4263 | MemInitializerValidatorCCC CCC(ClassDecl); |
| 4264 | if (R.empty() && BaseType.isNull() && |
| 4265 | (Corr = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
| 4266 | CCC, CTK_ErrorRecovery, ClassDecl))) { |
| 4267 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
| 4268 | |
| 4269 | |
| 4270 | |
| 4271 | diagnoseTypo(Corr, |
| 4272 | PDiag(diag::err_mem_init_not_member_or_class_suggest) |
| 4273 | << MemberOrBase << true); |
| 4274 | return BuildMemberInitializer(Member, Init, IdLoc); |
| 4275 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
| 4276 | const CXXBaseSpecifier *DirectBaseSpec; |
| 4277 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 4278 | if (FindBaseInitializer(*this, ClassDecl, |
| 4279 | Context.getTypeDeclType(Type), |
| 4280 | DirectBaseSpec, VirtualBaseSpec)) { |
| 4281 | |
| 4282 | |
| 4283 | |
| 4284 | diagnoseTypo(Corr, |
| 4285 | PDiag(diag::err_mem_init_not_member_or_class_suggest) |
| 4286 | << MemberOrBase << false, |
| 4287 | PDiag() ); |
| 4288 | |
| 4289 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec ? DirectBaseSpec |
| 4290 | : VirtualBaseSpec; |
| 4291 | Diag(BaseSpec->getBeginLoc(), diag::note_base_class_specified_here) |
| 4292 | << BaseSpec->getType() << BaseSpec->getSourceRange(); |
| 4293 | |
| 4294 | TyD = Type; |
| 4295 | } |
| 4296 | } |
| 4297 | } |
| 4298 | |
| 4299 | if (!TyD && BaseType.isNull()) { |
| 4300 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 4301 | << MemberOrBase << SourceRange(IdLoc,Init->getSourceRange().getEnd()); |
| 4302 | return true; |
| 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | if (BaseType.isNull()) { |
| 4307 | BaseType = Context.getTypeDeclType(TyD); |
| 4308 | MarkAnyDeclReferenced(TyD->getLocation(), TyD, false); |
| 4309 | if (SS.isSet()) { |
| 4310 | BaseType = Context.getElaboratedType(ETK_None, SS.getScopeRep(), |
| 4311 | BaseType); |
| 4312 | TInfo = Context.CreateTypeSourceInfo(BaseType); |
| 4313 | ElaboratedTypeLoc TL = TInfo->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
| 4314 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
| 4315 | TL.setElaboratedKeywordLoc(SourceLocation()); |
| 4316 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 4317 | } |
| 4318 | } |
| 4319 | } |
| 4320 | |
| 4321 | if (!TInfo) |
| 4322 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
| 4323 | |
| 4324 | return BuildBaseInitializer(BaseType, TInfo, Init, ClassDecl, EllipsisLoc); |
| 4325 | } |
| 4326 | |
| 4327 | MemInitResult |
| 4328 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr *Init, |
| 4329 | SourceLocation IdLoc) { |
| 4330 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 10 | | Assuming 'Member' is not a 'FieldDecl' | |
|
| 4331 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 11 | | Assuming 'Member' is not a 'IndirectFieldDecl' | |
|
| 12 | | 'IndirectMember' initialized to a null pointer value | |
|
| 4332 | assert((DirectMember || IndirectMember) && |
| 4333 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 4334 | |
| 4335 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
| 13 | | Assuming the condition is false | |
|
| |
| 4336 | return true; |
| 4337 | |
| 4338 | if (Member->isInvalidDecl()) |
| 15 | | Assuming the condition is false | |
|
| |
| 4339 | return true; |
| 4340 | |
| 4341 | MultiExprArg Args; |
| 4342 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 17 | | Assuming 'Init' is not a 'ParenListExpr' | |
|
| |
| 4343 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
| 4344 | } else if (InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
| 19 | | Assuming 'Init' is not a 'InitListExpr' | |
|
| |
| 4345 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
| 4346 | } else { |
| 4347 | |
| 4348 | Args = Init; |
| 4349 | } |
| 4350 | |
| 4351 | SourceRange InitRange = Init->getSourceRange(); |
| 4352 | |
| 4353 | if (Member->getType()->isDependentType() || Init->isTypeDependent()) { |
| 21 | | Assuming the condition is false | |
|
| 22 | | Assuming the condition is false | |
|
| |
| 4354 | |
| 4355 | |
| 4356 | DiscardCleanupsInEvaluationContext(); |
| 4357 | } else { |
| 4358 | bool InitList = false; |
| 4359 | if (isa<InitListExpr>(Init)) { |
| 24 | | Assuming 'Init' is not a 'InitListExpr' | |
|
| |
| 4360 | InitList = true; |
| 4361 | Args = Init; |
| 4362 | } |
| 4363 | |
| 4364 | |
| 4365 | InitializedEntity MemberEntity = |
| 4366 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, nullptr) |
| |
| 4367 | : InitializedEntity::InitializeMember(IndirectMember, |
| 27 | | Passing null pointer value via 1st parameter 'Member' | |
|
| 28 | | Calling 'InitializedEntity::InitializeMember' | |
|
| 4368 | nullptr); |
| 4369 | InitializationKind Kind = |
| 4370 | InitList ? InitializationKind::CreateDirectList( |
| 4371 | IdLoc, Init->getBeginLoc(), Init->getEndLoc()) |
| 4372 | : InitializationKind::CreateDirect(IdLoc, InitRange.getBegin(), |
| 4373 | InitRange.getEnd()); |
| 4374 | |
| 4375 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args); |
| 4376 | ExprResult MemberInit = InitSeq.Perform(*this, MemberEntity, Kind, Args, |
| 4377 | nullptr); |
| 4378 | if (MemberInit.isInvalid()) |
| 4379 | return true; |
| 4380 | |
| 4381 | |
| 4382 | |
| 4383 | |
| 4384 | MemberInit = ActOnFinishFullExpr(MemberInit.get(), InitRange.getBegin(), |
| 4385 | false); |
| 4386 | if (MemberInit.isInvalid()) |
| 4387 | return true; |
| 4388 | |
| 4389 | Init = MemberInit.get(); |
| 4390 | } |
| 4391 | |
| 4392 | if (DirectMember) { |
| 4393 | return new (Context) CXXCtorInitializer(Context, DirectMember, IdLoc, |
| 4394 | InitRange.getBegin(), Init, |
| 4395 | InitRange.getEnd()); |
| 4396 | } else { |
| 4397 | return new (Context) CXXCtorInitializer(Context, IndirectMember, IdLoc, |
| 4398 | InitRange.getBegin(), Init, |
| 4399 | InitRange.getEnd()); |
| 4400 | } |
| 4401 | } |
| 4402 | |
| 4403 | MemInitResult |
| 4404 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, |
| 4405 | CXXRecordDecl *ClassDecl) { |
| 4406 | SourceLocation NameLoc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 4407 | if (!LangOpts.CPlusPlus11) |
| 4408 | return Diag(NameLoc, diag::err_delegating_ctor) |
| 4409 | << TInfo->getTypeLoc().getLocalSourceRange(); |
| 4410 | Diag(NameLoc, diag::warn_cxx98_compat_delegating_ctor); |
| 4411 | |
| 4412 | bool InitList = true; |
| 4413 | MultiExprArg Args = Init; |
| 4414 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 4415 | InitList = false; |
| 4416 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
| 4417 | } |
| 4418 | |
| 4419 | SourceRange InitRange = Init->getSourceRange(); |
| 4420 | |
| 4421 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 4422 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 4423 | InitializationKind Kind = |
| 4424 | InitList ? InitializationKind::CreateDirectList( |
| 4425 | NameLoc, Init->getBeginLoc(), Init->getEndLoc()) |
| 4426 | : InitializationKind::CreateDirect(NameLoc, InitRange.getBegin(), |
| 4427 | InitRange.getEnd()); |
| 4428 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args); |
| 4429 | ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, |
| 4430 | Args, nullptr); |
| 4431 | if (DelegationInit.isInvalid()) |
| 4432 | return true; |
| 4433 | |
| 4434 | assert(cast<CXXConstructExpr>(DelegationInit.get())->getConstructor() && |
| 4435 | "Delegating constructor with no target?"); |
| 4436 | |
| 4437 | |
| 4438 | |
| 4439 | |
| 4440 | DelegationInit = ActOnFinishFullExpr( |
| 4441 | DelegationInit.get(), InitRange.getBegin(), false); |
| 4442 | if (DelegationInit.isInvalid()) |
| 4443 | return true; |
| 4444 | |
| 4445 | |
| 4446 | |
| 4447 | |
| 4448 | |
| 4449 | |
| 4450 | |
| 4451 | |
| 4452 | if (CurContext->isDependentContext()) |
| 4453 | DelegationInit = Init; |
| 4454 | |
| 4455 | return new (Context) CXXCtorInitializer(Context, TInfo, InitRange.getBegin(), |
| 4456 | DelegationInit.getAs<Expr>(), |
| 4457 | InitRange.getEnd()); |
| 4458 | } |
| 4459 | |
| 4460 | MemInitResult |
| 4461 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
| 4462 | Expr *Init, CXXRecordDecl *ClassDecl, |
| 4463 | SourceLocation EllipsisLoc) { |
| 4464 | SourceLocation BaseLoc |
| 4465 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 4466 | |
| 4467 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 4468 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 4469 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 4470 | |
| 4471 | |
| 4472 | |
| 4473 | |
| 4474 | |
| 4475 | |
| 4476 | |
| 4477 | bool Dependent = BaseType->isDependentType() || Init->isTypeDependent(); |
| 4478 | |
| 4479 | SourceRange InitRange = Init->getSourceRange(); |
| 4480 | if (EllipsisLoc.isValid()) { |
| 4481 | |
| 4482 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 4483 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 4484 | << SourceRange(BaseLoc, InitRange.getEnd()); |
| 4485 | |
| 4486 | EllipsisLoc = SourceLocation(); |
| 4487 | } |
| 4488 | } else { |
| 4489 | |
| 4490 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 4491 | return true; |
| 4492 | |
| 4493 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
| 4494 | return true; |
| 4495 | } |
| 4496 | |
| 4497 | |
| 4498 | const CXXBaseSpecifier *DirectBaseSpec = nullptr; |
| 4499 | const CXXBaseSpecifier *VirtualBaseSpec = nullptr; |
| 4500 | if (!Dependent) { |
| 4501 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 4502 | BaseType)) |
| 4503 | return BuildDelegatingInitializer(BaseTInfo, Init, ClassDecl); |
| 4504 | |
| 4505 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 4506 | VirtualBaseSpec); |
| 4507 | |
| 4508 | |
| 4509 | |
| 4510 | |
| 4511 | |
| 4512 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 4513 | |
| 4514 | |
| 4515 | |
| 4516 | |
| 4517 | |
| 4518 | if (ClassDecl->hasAnyDependentBases()) |
| 4519 | Dependent = true; |
| 4520 | else |
| 4521 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 4522 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 4523 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 4524 | } |
| 4525 | } |
| 4526 | |
| 4527 | if (Dependent) { |
| 4528 | DiscardCleanupsInEvaluationContext(); |
| 4529 | |
| 4530 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| 4531 | false, |
| 4532 | InitRange.getBegin(), Init, |
| 4533 | InitRange.getEnd(), EllipsisLoc); |
| 4534 | } |
| 4535 | |
| 4536 | |
| 4537 | |
| 4538 | |
| 4539 | |
| 4540 | if (DirectBaseSpec && VirtualBaseSpec) |
| 4541 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
| 4542 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 4543 | |
| 4544 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec; |
| 4545 | if (!BaseSpec) |
| 4546 | BaseSpec = VirtualBaseSpec; |
| 4547 | |
| 4548 | |
| 4549 | bool InitList = true; |
| 4550 | MultiExprArg Args = Init; |
| 4551 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 4552 | InitList = false; |
| 4553 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
| 4554 | } |
| 4555 | |
| 4556 | InitializedEntity BaseEntity = |
| 4557 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
| 4558 | InitializationKind Kind = |
| 4559 | InitList ? InitializationKind::CreateDirectList(BaseLoc) |
| 4560 | : InitializationKind::CreateDirect(BaseLoc, InitRange.getBegin(), |
| 4561 | InitRange.getEnd()); |
| 4562 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args); |
| 4563 | ExprResult BaseInit = InitSeq.Perform(*this, BaseEntity, Kind, Args, nullptr); |
| 4564 | if (BaseInit.isInvalid()) |
| 4565 | return true; |
| 4566 | |
| 4567 | |
| 4568 | |
| 4569 | |
| 4570 | BaseInit = ActOnFinishFullExpr(BaseInit.get(), InitRange.getBegin(), |
| 4571 | false); |
| 4572 | if (BaseInit.isInvalid()) |
| 4573 | return true; |
| 4574 | |
| 4575 | |
| 4576 | |
| 4577 | |
| 4578 | |
| 4579 | |
| 4580 | |
| 4581 | |
| 4582 | if (CurContext->isDependentContext()) |
| 4583 | BaseInit = Init; |
| 4584 | |
| 4585 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| 4586 | BaseSpec->isVirtual(), |
| 4587 | InitRange.getBegin(), |
| 4588 | BaseInit.getAs<Expr>(), |
| 4589 | InitRange.getEnd(), EllipsisLoc); |
| 4590 | } |
| 4591 | |
| 4592 | |
| 4593 | static Expr *CastForMoving(Sema &SemaRef, Expr *E, QualType T = QualType()) { |
| 4594 | if (T.isNull()) T = E->getType(); |
| 4595 | QualType TargetType = SemaRef.BuildReferenceType( |
| 4596 | T, false, SourceLocation(), DeclarationName()); |
| 4597 | SourceLocation ExprLoc = E->getBeginLoc(); |
| 4598 | TypeSourceInfo *TargetLoc = SemaRef.Context.getTrivialTypeSourceInfo( |
| 4599 | TargetType, ExprLoc); |
| 4600 | |
| 4601 | return SemaRef.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 4602 | SourceRange(ExprLoc, ExprLoc), |
| 4603 | E->getSourceRange()).get(); |
| 4604 | } |
| 4605 | |
| 4606 | |
| 4607 | |
| 4608 | enum ImplicitInitializerKind { |
| 4609 | IIK_Default, |
| 4610 | IIK_Copy, |
| 4611 | IIK_Move, |
| 4612 | IIK_Inherit |
| 4613 | }; |
| 4614 | |
| 4615 | static bool |
| 4616 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
| 4617 | ImplicitInitializerKind ImplicitInitKind, |
| 4618 | CXXBaseSpecifier *BaseSpec, |
| 4619 | bool IsInheritedVirtualBase, |
| 4620 | CXXCtorInitializer *&CXXBaseInit) { |
| 4621 | InitializedEntity InitEntity |
| 4622 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 4623 | IsInheritedVirtualBase); |
| 4624 | |
| 4625 | ExprResult BaseInit; |
| 4626 | |
| 4627 | switch (ImplicitInitKind) { |
| 4628 | case IIK_Inherit: |
| 4629 | case IIK_Default: { |
| 4630 | InitializationKind InitKind |
| 4631 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 4632 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 4633 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
| 4634 | break; |
| 4635 | } |
| 4636 | |
| 4637 | case IIK_Move: |
| 4638 | case IIK_Copy: { |
| 4639 | bool Moving = ImplicitInitKind == IIK_Move; |
| 4640 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 4641 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 4642 | |
| 4643 | Expr *CopyCtorArg = |
| 4644 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
| 4645 | SourceLocation(), Param, false, |
| 4646 | Constructor->getLocation(), ParamType, |
| 4647 | VK_LValue, nullptr); |
| 4648 | |
| 4649 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(CopyCtorArg)); |
| 4650 | |
| 4651 | |
| 4652 | QualType ArgTy = |
| 4653 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 4654 | ParamType.getQualifiers()); |
| 4655 | |
| 4656 | if (Moving) { |
| 4657 | CopyCtorArg = CastForMoving(SemaRef, CopyCtorArg); |
| 4658 | } |
| 4659 | |
| 4660 | CXXCastPath BasePath; |
| 4661 | BasePath.push_back(BaseSpec); |
| 4662 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 4663 | CK_UncheckedDerivedToBase, |
| 4664 | Moving ? VK_XValue : VK_LValue, |
| 4665 | &BasePath).get(); |
| 4666 | |
| 4667 | InitializationKind InitKind |
| 4668 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 4669 | SourceLocation(), SourceLocation()); |
| 4670 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, CopyCtorArg); |
| 4671 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, CopyCtorArg); |
| 4672 | break; |
| 4673 | } |
| 4674 | } |
| 4675 | |
| 4676 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
| 4677 | if (BaseInit.isInvalid()) |
| 4678 | return true; |
| 4679 | |
| 4680 | CXXBaseInit = |
| 4681 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 4682 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 4683 | SourceLocation()), |
| 4684 | BaseSpec->isVirtual(), |
| 4685 | SourceLocation(), |
| 4686 | BaseInit.getAs<Expr>(), |
| 4687 | SourceLocation(), |
| 4688 | SourceLocation()); |
| 4689 | |
| 4690 | return false; |
| 4691 | } |
| 4692 | |
| 4693 | static bool RefersToRValueRef(Expr *MemRef) { |
| 4694 | ValueDecl *Referenced = cast<MemberExpr>(MemRef)->getMemberDecl(); |
| 4695 | return Referenced->getType()->isRValueReferenceType(); |
| 4696 | } |
| 4697 | |
| 4698 | static bool |
| 4699 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
| 4700 | ImplicitInitializerKind ImplicitInitKind, |
| 4701 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
| 4702 | CXXCtorInitializer *&CXXMemberInit) { |
| 4703 | if (Field->isInvalidDecl()) |
| 4704 | return true; |
| 4705 | |
| 4706 | SourceLocation Loc = Constructor->getLocation(); |
| 4707 | |
| 4708 | if (ImplicitInitKind == IIK_Copy || ImplicitInitKind == IIK_Move) { |
| 4709 | bool Moving = ImplicitInitKind == IIK_Move; |
| 4710 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 4711 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 4712 | |
| 4713 | |
| 4714 | if (Field->isZeroLengthBitField(SemaRef.Context)) |
| 4715 | return false; |
| 4716 | |
| 4717 | Expr *MemberExprBase = |
| 4718 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
| 4719 | SourceLocation(), Param, false, |
| 4720 | Loc, ParamType, VK_LValue, nullptr); |
| 4721 | |
| 4722 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(MemberExprBase)); |
| 4723 | |
| 4724 | if (Moving) { |
| 4725 | MemberExprBase = CastForMoving(SemaRef, MemberExprBase); |
| 4726 | } |
| 4727 | |
| 4728 | |
| 4729 | CXXScopeSpec SS; |
| 4730 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 4731 | Sema::LookupMemberName); |
| 4732 | MemberLookup.addDecl(Indirect ? cast<ValueDecl>(Indirect) |
| 4733 | : cast<ValueDecl>(Field), AS_public); |
| 4734 | MemberLookup.resolveKind(); |
| 4735 | ExprResult CtorArg |
| 4736 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
| 4737 | ParamType, Loc, |
| 4738 | false, |
| 4739 | SS, |
| 4740 | SourceLocation(), |
| 4741 | nullptr, |
| 4742 | MemberLookup, |
| 4743 | nullptr, |
| 4744 | nullptr); |
| 4745 | if (CtorArg.isInvalid()) |
| 4746 | return true; |
| 4747 | |
| 4748 | |
| 4749 | |
| 4750 | |
| 4751 | if (RefersToRValueRef(CtorArg.get())) { |
| 4752 | CtorArg = CastForMoving(SemaRef, CtorArg.get()); |
| 4753 | } |
| 4754 | |
| 4755 | InitializedEntity Entity = |
| 4756 | Indirect ? InitializedEntity::InitializeMember(Indirect, nullptr, |
| 4757 | true) |
| 4758 | : InitializedEntity::InitializeMember(Field, nullptr, |
| 4759 | true); |
| 4760 | |
| 4761 | |
| 4762 | InitializationKind InitKind = |
| 4763 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 4764 | |
| 4765 | Expr *CtorArgE = CtorArg.getAs<Expr>(); |
| 4766 | InitializationSequence InitSeq(SemaRef, Entity, InitKind, CtorArgE); |
| 4767 | ExprResult MemberInit = |
| 4768 | InitSeq.Perform(SemaRef, Entity, InitKind, MultiExprArg(&CtorArgE, 1)); |
| 4769 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
| 4770 | if (MemberInit.isInvalid()) |
| 4771 | return true; |
| 4772 | |
| 4773 | if (Indirect) |
| 4774 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer( |
| 4775 | SemaRef.Context, Indirect, Loc, Loc, MemberInit.getAs<Expr>(), Loc); |
| 4776 | else |
| 4777 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer( |
| 4778 | SemaRef.Context, Field, Loc, Loc, MemberInit.getAs<Expr>(), Loc); |
| 4779 | return false; |
| 4780 | } |
| 4781 | |
| 4782 | assert((ImplicitInitKind == IIK_Default || ImplicitInitKind == IIK_Inherit) && |
| 4783 | "Unhandled implicit init kind!"); |
| 4784 | |
| 4785 | QualType FieldBaseElementType = |
| 4786 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 4787 | |
| 4788 | if (FieldBaseElementType->isRecordType()) { |
| 4789 | InitializedEntity InitEntity = |
| 4790 | Indirect ? InitializedEntity::InitializeMember(Indirect, nullptr, |
| 4791 | true) |
| 4792 | : InitializedEntity::InitializeMember(Field, nullptr, |
| 4793 | true); |
| 4794 | InitializationKind InitKind = |
| 4795 | InitializationKind::CreateDefault(Loc); |
| 4796 | |
| 4797 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 4798 | ExprResult MemberInit = |
| 4799 | InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
| 4800 | |
| 4801 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
| 4802 | if (MemberInit.isInvalid()) |
| 4803 | return true; |
| 4804 | |
| 4805 | if (Indirect) |
| 4806 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 4807 | Indirect, Loc, |
| 4808 | Loc, |
| 4809 | MemberInit.get(), |
| 4810 | Loc); |
| 4811 | else |
| 4812 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 4813 | Field, Loc, Loc, |
| 4814 | MemberInit.get(), |
| 4815 | Loc); |
| 4816 | return false; |
| 4817 | } |
| 4818 | |
| 4819 | if (!Field->getParent()->isUnion()) { |
| 4820 | if (FieldBaseElementType->isReferenceType()) { |
| 4821 | SemaRef.Diag(Constructor->getLocation(), |
| 4822 | diag::err_uninitialized_member_in_ctor) |
| 4823 | << (int)Constructor->isImplicit() |
| 4824 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 4825 | << 0 << Field->getDeclName(); |
| 4826 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 4827 | return true; |
| 4828 | } |
| 4829 | |
| 4830 | if (FieldBaseElementType.isConstQualified()) { |
| 4831 | SemaRef.Diag(Constructor->getLocation(), |
| 4832 | diag::err_uninitialized_member_in_ctor) |
| 4833 | << (int)Constructor->isImplicit() |
| 4834 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 4835 | << 1 << Field->getDeclName(); |
| 4836 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 4837 | return true; |
| 4838 | } |
| 4839 | } |
| 4840 | |
| 4841 | if (FieldBaseElementType.hasNonTrivialObjCLifetime()) { |
| 4842 | |
| 4843 | |
| 4844 | CXXMemberInit |
| 4845 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 4846 | Loc, Loc, |
| 4847 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 4848 | Loc); |
| 4849 | return false; |
| 4850 | } |
| 4851 | |
| 4852 | |
| 4853 | CXXMemberInit = nullptr; |
| 4854 | return false; |
| 4855 | } |
| 4856 | |
| 4857 | namespace { |
| 4858 | struct BaseAndFieldInfo { |
| 4859 | Sema &S; |
| 4860 | CXXConstructorDecl *Ctor; |
| 4861 | bool AnyErrorsInInits; |
| 4862 | ImplicitInitializerKind IIK; |
| 4863 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
| 4864 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
| 4865 | llvm::DenseMap<TagDecl*, FieldDecl*> ActiveUnionMember; |
| 4866 | |
| 4867 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 4868 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
| 4869 | bool Generated = Ctor->isImplicit() || Ctor->isDefaulted(); |
| 4870 | if (Ctor->getInheritedConstructor()) |
| 4871 | IIK = IIK_Inherit; |
| 4872 | else if (Generated && Ctor->isCopyConstructor()) |
| 4873 | IIK = IIK_Copy; |
| 4874 | else if (Generated && Ctor->isMoveConstructor()) |
| 4875 | IIK = IIK_Move; |
| 4876 | else |
| 4877 | IIK = IIK_Default; |
| 4878 | } |
| 4879 | |
| 4880 | bool isImplicitCopyOrMove() const { |
| 4881 | switch (IIK) { |
| 4882 | case IIK_Copy: |
| 4883 | case IIK_Move: |
| 4884 | return true; |
| 4885 | |
| 4886 | case IIK_Default: |
| 4887 | case IIK_Inherit: |
| 4888 | return false; |
| 4889 | } |
| 4890 | |
| 4891 | llvm_unreachable("Invalid ImplicitInitializerKind!"); |
| 4892 | } |
| 4893 | |
| 4894 | bool addFieldInitializer(CXXCtorInitializer *Init) { |
| 4895 | AllToInit.push_back(Init); |
| 4896 | |
| 4897 | |
| 4898 | if (Init->getInit()->HasSideEffects(S.Context)) |
| 4899 | S.UnusedPrivateFields.remove(Init->getAnyMember()); |
| 4900 | |
| 4901 | return false; |
| 4902 | } |
| 4903 | |
| 4904 | bool isInactiveUnionMember(FieldDecl *Field) { |
| 4905 | RecordDecl *Record = Field->getParent(); |
| 4906 | if (!Record->isUnion()) |
| 4907 | return false; |
| 4908 | |
| 4909 | if (FieldDecl *Active = |
| 4910 | ActiveUnionMember.lookup(Record->getCanonicalDecl())) |
| 4911 | return Active != Field->getCanonicalDecl(); |
| 4912 | |
| 4913 | |
| 4914 | if (isImplicitCopyOrMove()) |
| 4915 | return true; |
| 4916 | |
| 4917 | |
| 4918 | |
| 4919 | if (Field->hasInClassInitializer()) |
| 4920 | return false; |
| 4921 | |
| 4922 | |
| 4923 | if (!Field->isAnonymousStructOrUnion()) |
| 4924 | return true; |
| 4925 | CXXRecordDecl *FieldRD = Field->getType()->getAsCXXRecordDecl(); |
| 4926 | return !FieldRD->hasInClassInitializer(); |
| 4927 | } |
| 4928 | |
| 4929 | |
| 4930 | |
| 4931 | |
| 4932 | bool isWithinInactiveUnionMember(FieldDecl *Field, |
| 4933 | IndirectFieldDecl *Indirect) { |
| 4934 | if (!Indirect) |
| 4935 | return isInactiveUnionMember(Field); |
| 4936 | |
| 4937 | for (auto *C : Indirect->chain()) { |
| 4938 | FieldDecl *Field = dyn_cast<FieldDecl>(C); |
| 4939 | if (Field && isInactiveUnionMember(Field)) |
| 4940 | return true; |
| 4941 | } |
| 4942 | return false; |
| 4943 | } |
| 4944 | }; |
| 4945 | } |
| 4946 | |
| 4947 | |
| 4948 | |
| 4949 | static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) { |
| 4950 | if (T->isIncompleteArrayType()) |
| 4951 | return true; |
| 4952 | |
| 4953 | while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) { |
| 4954 | if (!ArrayT->getSize()) |
| 4955 | return true; |
| 4956 | |
| 4957 | T = ArrayT->getElementType(); |
| 4958 | } |
| 4959 | |
| 4960 | return false; |
| 4961 | } |
| 4962 | |
| 4963 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
| 4964 | FieldDecl *Field, |
| 4965 | IndirectFieldDecl *Indirect = nullptr) { |
| 4966 | if (Field->isInvalidDecl()) |
| 4967 | return false; |
| 4968 | |
| 4969 | |
| 4970 | if (CXXCtorInitializer *Init = |
| 4971 | Info.AllBaseFields.lookup(Field->getCanonicalDecl())) |
| 4972 | return Info.addFieldInitializer(Init); |
| 4973 | |
| 4974 | |
| 4975 | |
| 4976 | |
| 4977 | |
| 4978 | |
| 4979 | |
| 4980 | |
| 4981 | |
| 4982 | |
| 4983 | |
| 4984 | |
| 4985 | |
| 4986 | if (Info.isWithinInactiveUnionMember(Field, Indirect)) |
| 4987 | return false; |
| 4988 | |
| 4989 | if (Field->hasInClassInitializer() && !Info.isImplicitCopyOrMove()) { |
| 4990 | ExprResult DIE = |
| 4991 | SemaRef.BuildCXXDefaultInitExpr(Info.Ctor->getLocation(), Field); |
| 4992 | if (DIE.isInvalid()) |
| 4993 | return true; |
| 4994 | |
| 4995 | auto Entity = InitializedEntity::InitializeMember(Field, nullptr, true); |
| 4996 | SemaRef.checkInitializerLifetime(Entity, DIE.get()); |
| 4997 | |
| 4998 | CXXCtorInitializer *Init; |
| 4999 | if (Indirect) |
| 5000 | Init = new (SemaRef.Context) |
| 5001 | CXXCtorInitializer(SemaRef.Context, Indirect, SourceLocation(), |
| 5002 | SourceLocation(), DIE.get(), SourceLocation()); |
| 5003 | else |
| 5004 | Init = new (SemaRef.Context) |
| 5005 | CXXCtorInitializer(SemaRef.Context, Field, SourceLocation(), |
| 5006 | SourceLocation(), DIE.get(), SourceLocation()); |
| 5007 | return Info.addFieldInitializer(Init); |
| 5008 | } |
| 5009 | |
| 5010 | |
| 5011 | if (isIncompleteOrZeroLengthArrayType(SemaRef.Context, Field->getType())) |
| 5012 | return false; |
| 5013 | |
| 5014 | |
| 5015 | |
| 5016 | |
| 5017 | if (Info.AnyErrorsInInits) |
| 5018 | return false; |
| 5019 | |
| 5020 | CXXCtorInitializer *Init = nullptr; |
| 5021 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 5022 | Indirect, Init)) |
| 5023 | return true; |
| 5024 | |
| 5025 | if (!Init) |
| 5026 | return false; |
| 5027 | |
| 5028 | return Info.addFieldInitializer(Init); |
| 5029 | } |
| 5030 | |
| 5031 | bool |
| 5032 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 5033 | CXXCtorInitializer *Initializer) { |
| 5034 | assert(Initializer->isDelegatingInitializer()); |
| 5035 | Constructor->setNumCtorInitializers(1); |
| 5036 | CXXCtorInitializer **initializer = |
| 5037 | new (Context) CXXCtorInitializer*[1]; |
| 5038 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 5039 | Constructor->setCtorInitializers(initializer); |
| 5040 | |
| 5041 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
| 5042 | MarkFunctionReferenced(Initializer->getSourceLocation(), Dtor); |
| 5043 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 5044 | } |
| 5045 | |
| 5046 | DelegatingCtorDecls.push_back(Constructor); |
| 5047 | |
| 5048 | DiagnoseUninitializedFields(*this, Constructor); |
| 5049 | |
| 5050 | return false; |
| 5051 | } |
| 5052 | |
| 5053 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors, |
| 5054 | ArrayRef<CXXCtorInitializer *> Initializers) { |
| 5055 | if (Constructor->isDependentContext()) { |
| 5056 | |
| 5057 | |
| 5058 | if (!Initializers.empty()) { |
| 5059 | Constructor->setNumCtorInitializers(Initializers.size()); |
| 5060 | CXXCtorInitializer **baseOrMemberInitializers = |
| 5061 | new (Context) CXXCtorInitializer*[Initializers.size()]; |
| 5062 | memcpy(baseOrMemberInitializers, Initializers.data(), |
| 5063 | Initializers.size() * sizeof(CXXCtorInitializer*)); |
| 5064 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
| 5065 | } |
| 5066 | |
| 5067 | |
| 5068 | if (AnyErrors) |
| 5069 | Constructor->setInvalidDecl(); |
| 5070 | |
| 5071 | return false; |
| 5072 | } |
| 5073 | |
| 5074 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
| 5075 | |
| 5076 | |
| 5077 | |
| 5078 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
| 5079 | if (!ClassDecl) |
| 5080 | return true; |
| 5081 | |
| 5082 | bool HadError = false; |
| 5083 | |
| 5084 | for (unsigned i = 0; i < Initializers.size(); i++) { |
| 5085 | CXXCtorInitializer *Member = Initializers[i]; |
| 5086 | |
| 5087 | if (Member->isBaseInitializer()) |
| 5088 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
| 5089 | else { |
| 5090 | Info.AllBaseFields[Member->getAnyMember()->getCanonicalDecl()] = Member; |
| 5091 | |
| 5092 | if (IndirectFieldDecl *F = Member->getIndirectMember()) { |
| 5093 | for (auto *C : F->chain()) { |
| 5094 | FieldDecl *FD = dyn_cast<FieldDecl>(C); |
| 5095 | if (FD && FD->getParent()->isUnion()) |
| 5096 | Info.ActiveUnionMember.insert(std::make_pair( |
| 5097 | FD->getParent()->getCanonicalDecl(), FD->getCanonicalDecl())); |
| 5098 | } |
| 5099 | } else if (FieldDecl *FD = Member->getMember()) { |
| 5100 | if (FD->getParent()->isUnion()) |
| 5101 | Info.ActiveUnionMember.insert(std::make_pair( |
| 5102 | FD->getParent()->getCanonicalDecl(), FD->getCanonicalDecl())); |
| 5103 | } |
| 5104 | } |
| 5105 | } |
| 5106 | |
| 5107 | |
| 5108 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 5109 | for (auto &I : ClassDecl->bases()) { |
| 5110 | if (I.isVirtual()) |
| 5111 | DirectVBases.insert(&I); |
| 5112 | } |
| 5113 | |
| 5114 | |
| 5115 | for (auto &VBase : ClassDecl->vbases()) { |
| 5116 | if (CXXCtorInitializer *Value |
| 5117 | = Info.AllBaseFields.lookup(VBase.getType()->getAs<RecordType>())) { |
| 5118 | |
| 5119 | |
| 5120 | |
| 5121 | |
| 5122 | if (ClassDecl->isAbstract()) { |
| 5123 | |
| 5124 | |
| 5125 | Diag(Value->getSourceLocation(), diag::warn_abstract_vbase_init_ignored) |
| 5126 | << VBase.getType() << ClassDecl; |
| 5127 | DiagnoseAbstractType(ClassDecl); |
| 5128 | } |
| 5129 | |
| 5130 | Info.AllToInit.push_back(Value); |
| 5131 | } else if (!AnyErrors && !ClassDecl->isAbstract()) { |
| 5132 | |
| 5133 | |
| 5134 | |
| 5135 | |
| 5136 | bool IsInheritedVirtualBase = !DirectVBases.count(&VBase); |
| 5137 | CXXCtorInitializer *CXXBaseInit; |
| 5138 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
| 5139 | &VBase, IsInheritedVirtualBase, |
| 5140 | CXXBaseInit)) { |
| 5141 | HadError = true; |
| 5142 | continue; |
| 5143 | } |
| 5144 | |
| 5145 | Info.AllToInit.push_back(CXXBaseInit); |
| 5146 | } |
| 5147 | } |
| 5148 | |
| 5149 | |
| 5150 | for (auto &Base : ClassDecl->bases()) { |
| 5151 | |
| 5152 | if (Base.isVirtual()) |
| 5153 | continue; |
| 5154 | |
| 5155 | if (CXXCtorInitializer *Value |
| 5156 | = Info.AllBaseFields.lookup(Base.getType()->getAs<RecordType>())) { |
| 5157 | Info.AllToInit.push_back(Value); |
| 5158 | } else if (!AnyErrors) { |
| 5159 | CXXCtorInitializer *CXXBaseInit; |
| 5160 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
| 5161 | &Base, false, |
| 5162 | CXXBaseInit)) { |
| 5163 | HadError = true; |
| 5164 | continue; |
| 5165 | } |
| 5166 | |
| 5167 | Info.AllToInit.push_back(CXXBaseInit); |
| 5168 | } |
| 5169 | } |
| 5170 | |
| 5171 | |
| 5172 | for (auto *Mem : ClassDecl->decls()) { |
| 5173 | if (auto *F = dyn_cast<FieldDecl>(Mem)) { |
| 5174 | |
| 5175 | |
| 5176 | |
| 5177 | |
| 5178 | if (F->isUnnamedBitfield()) |
| 5179 | continue; |
| 5180 | |
| 5181 | |
| 5182 | |
| 5183 | |
| 5184 | if (F->isAnonymousStructOrUnion() && !Info.isImplicitCopyOrMove()) |
| 5185 | continue; |
| 5186 | |
| 5187 | if (CollectFieldInitializer(*this, Info, F)) |
| 5188 | HadError = true; |
| 5189 | continue; |
| 5190 | } |
| 5191 | |
| 5192 | |
| 5193 | if (Info.isImplicitCopyOrMove()) |
| 5194 | continue; |
| 5195 | |
| 5196 | if (auto *F = dyn_cast<IndirectFieldDecl>(Mem)) { |
| 5197 | if (F->getType()->isIncompleteArrayType()) { |
| 5198 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 5199 | "Incomplete array type is not valid"); |
| 5200 | continue; |
| 5201 | } |
| 5202 | |
| 5203 | |
| 5204 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 5205 | HadError = true; |
| 5206 | |
| 5207 | continue; |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | unsigned NumInitializers = Info.AllToInit.size(); |
| 5212 | if (NumInitializers > 0) { |
| 5213 | Constructor->setNumCtorInitializers(NumInitializers); |
| 5214 | CXXCtorInitializer **baseOrMemberInitializers = |
| 5215 | new (Context) CXXCtorInitializer*[NumInitializers]; |
| 5216 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
| 5217 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 5218 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
| 5219 | |
| 5220 | |
| 5221 | |
| 5222 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 5223 | Constructor->getParent()); |
| 5224 | } |
| 5225 | |
| 5226 | return HadError; |
| 5227 | } |
| 5228 | |
| 5229 | static void PopulateKeysForFields(FieldDecl *Field, SmallVectorImpl<const void*> &IdealInits) { |
| 5230 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
| 5231 | const RecordDecl *RD = RT->getDecl(); |
| 5232 | if (RD->isAnonymousStructOrUnion()) { |
| 5233 | for (auto *Field : RD->fields()) |
| 5234 | PopulateKeysForFields(Field, IdealInits); |
| 5235 | return; |
| 5236 | } |
| 5237 | } |
| 5238 | IdealInits.push_back(Field->getCanonicalDecl()); |
| 5239 | } |
| 5240 | |
| 5241 | static const void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 5242 | return Context.getCanonicalType(BaseType).getTypePtr(); |
| 5243 | } |
| 5244 | |
| 5245 | static const void *GetKeyForMember(ASTContext &Context, |
| 5246 | CXXCtorInitializer *Member) { |
| 5247 | if (!Member->isAnyMemberInitializer()) |
| 5248 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
| 5249 | |
| 5250 | return Member->getAnyMember()->getCanonicalDecl(); |
| 5251 | } |
| 5252 | |
| 5253 | static void AddInitializerToDiag(const Sema::SemaDiagnosticBuilder &Diag, |
| 5254 | const CXXCtorInitializer *Previous, |
| 5255 | const CXXCtorInitializer *Current) { |
| 5256 | if (Previous->isAnyMemberInitializer()) |
| 5257 | Diag << 0 << Previous->getAnyMember(); |
| 5258 | else |
| 5259 | Diag << 1 << Previous->getTypeSourceInfo()->getType(); |
| 5260 | |
| 5261 | if (Current->isAnyMemberInitializer()) |
| 5262 | Diag << 0 << Current->getAnyMember(); |
| 5263 | else |
| 5264 | Diag << 1 << Current->getTypeSourceInfo()->getType(); |
| 5265 | } |
| 5266 | |
| 5267 | static void DiagnoseBaseOrMemInitializerOrder( |
| 5268 | Sema &SemaRef, const CXXConstructorDecl *Constructor, |
| 5269 | ArrayRef<CXXCtorInitializer *> Inits) { |
| 5270 | if (Constructor->getDeclContext()->isDependentContext()) |
| 5271 | return; |
| 5272 | |
| 5273 | |
| 5274 | |
| 5275 | bool ShouldCheckOrder = false; |
| 5276 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
| 5277 | CXXCtorInitializer *Init = Inits[InitIndex]; |
| 5278 | if (!SemaRef.Diags.isIgnored(diag::warn_initializer_out_of_order, |
| 5279 | Init->getSourceLocation())) { |
| 5280 | ShouldCheckOrder = true; |
| 5281 | break; |
| 5282 | } |
| 5283 | } |
| 5284 | if (!ShouldCheckOrder) |
| 5285 | return; |
| 5286 | |
| 5287 | |
| 5288 | |
| 5289 | |
| 5290 | SmallVector<const void*, 32> IdealInitKeys; |
| 5291 | |
| 5292 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 5293 | |
| 5294 | |
| 5295 | for (const auto &VBase : ClassDecl->vbases()) |
| 5296 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase.getType())); |
| 5297 | |
| 5298 | |
| 5299 | for (const auto &Base : ClassDecl->bases()) { |
| 5300 | if (Base.isVirtual()) |
| 5301 | continue; |
| 5302 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base.getType())); |
| 5303 | } |
| 5304 | |
| 5305 | |
| 5306 | for (auto *Field : ClassDecl->fields()) { |
| 5307 | if (Field->isUnnamedBitfield()) |
| 5308 | continue; |
| 5309 | |
| 5310 | PopulateKeysForFields(Field, IdealInitKeys); |
| 5311 | } |
| 5312 | |
| 5313 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 5314 | unsigned IdealIndex = 0; |
| 5315 | |
| 5316 | |
| 5317 | |
| 5318 | SmallVector<unsigned> WarnIndexes; |
| 5319 | |
| 5320 | |
| 5321 | SmallVector<std::pair<unsigned, unsigned>, 32> CorrelatedInitOrder; |
| 5322 | |
| 5323 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
| 5324 | const void *InitKey = GetKeyForMember(SemaRef.Context, Inits[InitIndex]); |
| 5325 | |
| 5326 | |
| 5327 | |
| 5328 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 5329 | if (InitKey == IdealInitKeys[IdealIndex]) |
| 5330 | break; |
| 5331 | |
| 5332 | |
| 5333 | |
| 5334 | |
| 5335 | if (IdealIndex == NumIdealInits && InitIndex) { |
| 5336 | WarnIndexes.push_back(InitIndex); |
| 5337 | |
| 5338 | |
| 5339 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 5340 | if (InitKey == IdealInitKeys[IdealIndex]) |
| 5341 | break; |
| 5342 | |
| 5343 | assert(IdealIndex < NumIdealInits && |
| 5344 | "initializer not found in initializer list"); |
| 5345 | } |
| 5346 | CorrelatedInitOrder.emplace_back(IdealIndex, InitIndex); |
| 5347 | } |
| 5348 | |
| 5349 | if (WarnIndexes.empty()) |
| 5350 | return; |
| 5351 | |
| 5352 | |
| 5353 | llvm::sort(CorrelatedInitOrder, |
| 5354 | [](auto &LHS, auto &RHS) { return LHS.first < RHS.first; }); |
| 5355 | |
| 5356 | |
| 5357 | |
| 5358 | { |
| 5359 | Sema::SemaDiagnosticBuilder D = SemaRef.Diag( |
| 5360 | Inits[WarnIndexes.front() - 1]->getSourceLocation(), |
| 5361 | WarnIndexes.size() == 1 ? diag::warn_initializer_out_of_order |
| 5362 | : diag::warn_some_initializers_out_of_order); |
| 5363 | |
| 5364 | for (unsigned I = 0; I < CorrelatedInitOrder.size(); ++I) { |
| 5365 | if (CorrelatedInitOrder[I].second == I) |
| 5366 | continue; |
| 5367 | |
| 5368 | |
| 5369 | |
| 5370 | D << FixItHint::CreateReplacement( |
| 5371 | Inits[I]->getSourceRange(), |
| 5372 | Lexer::getSourceText( |
| 5373 | CharSourceRange::getTokenRange( |
| 5374 | Inits[CorrelatedInitOrder[I].second]->getSourceRange()), |
| 5375 | SemaRef.getSourceManager(), SemaRef.getLangOpts())); |
| 5376 | } |
| 5377 | |
| 5378 | |
| 5379 | |
| 5380 | if (WarnIndexes.size() == 1) { |
| 5381 | AddInitializerToDiag(D, Inits[WarnIndexes.front() - 1], |
| 5382 | Inits[WarnIndexes.front()]); |
| 5383 | return; |
| 5384 | } |
| 5385 | } |
| 5386 | |
| 5387 | |
| 5388 | for (unsigned WarnIndex : WarnIndexes) { |
| 5389 | const clang::CXXCtorInitializer *PrevInit = Inits[WarnIndex - 1]; |
| 5390 | auto D = SemaRef.Diag(PrevInit->getSourceLocation(), |
| 5391 | diag::note_initializer_out_of_order); |
| 5392 | AddInitializerToDiag(D, PrevInit, Inits[WarnIndex]); |
| 5393 | D << PrevInit->getSourceRange(); |
| 5394 | } |
| 5395 | } |
| 5396 | |
| 5397 | namespace { |
| 5398 | bool CheckRedundantInit(Sema &S, |
| 5399 | CXXCtorInitializer *Init, |
| 5400 | CXXCtorInitializer *&PrevInit) { |
| 5401 | if (!PrevInit) { |
| 5402 | PrevInit = Init; |
| 5403 | return false; |
| 5404 | } |
| 5405 | |
| 5406 | if (FieldDecl *Field = Init->getAnyMember()) |
| 5407 | S.Diag(Init->getSourceLocation(), |
| 5408 | diag::err_multiple_mem_initialization) |
| 5409 | << Field->getDeclName() |
| 5410 | << Init->getSourceRange(); |
| 5411 | else { |
| 5412 | const Type *BaseClass = Init->getBaseClass(); |
| 5413 | assert(BaseClass && "neither field nor base"); |
| 5414 | S.Diag(Init->getSourceLocation(), |
| 5415 | diag::err_multiple_base_initialization) |
| 5416 | << QualType(BaseClass, 0) |
| 5417 | << Init->getSourceRange(); |
| 5418 | } |
| 5419 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 5420 | << 0 << PrevInit->getSourceRange(); |
| 5421 | |
| 5422 | return true; |
| 5423 | } |
| 5424 | |
| 5425 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
| 5426 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 5427 | |
| 5428 | bool CheckRedundantUnionInit(Sema &S, |
| 5429 | CXXCtorInitializer *Init, |
| 5430 | RedundantUnionMap &Unions) { |
| 5431 | FieldDecl *Field = Init->getAnyMember(); |
| 5432 | RecordDecl *Parent = Field->getParent(); |
| 5433 | NamedDecl *Child = Field; |
| 5434 | |
| 5435 | while (Parent->isAnonymousStructOrUnion() || Parent->isUnion()) { |
| 5436 | if (Parent->isUnion()) { |
| 5437 | UnionEntry &En = Unions[Parent]; |
| 5438 | if (En.first && En.first != Child) { |
| 5439 | S.Diag(Init->getSourceLocation(), |
| 5440 | diag::err_multiple_mem_union_initialization) |
| 5441 | << Field->getDeclName() |
| 5442 | << Init->getSourceRange(); |
| 5443 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 5444 | << 0 << En.second->getSourceRange(); |
| 5445 | return true; |
| 5446 | } |
| 5447 | if (!En.first) { |
| 5448 | En.first = Child; |
| 5449 | En.second = Init; |
| 5450 | } |
| 5451 | if (!Parent->isAnonymousStructOrUnion()) |
| 5452 | return false; |
| 5453 | } |
| 5454 | |
| 5455 | Child = Parent; |
| 5456 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 5457 | } |
| 5458 | |
| 5459 | return false; |
| 5460 | } |
| 5461 | } |
| 5462 | |
| 5463 | |
| 5464 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
| 5465 | SourceLocation ColonLoc, |
| 5466 | ArrayRef<CXXCtorInitializer*> MemInits, |
| 5467 | bool AnyErrors) { |
| 5468 | if (!ConstructorDecl) |
| 5469 | return; |
| 5470 | |
| 5471 | AdjustDeclIfTemplate(ConstructorDecl); |
| 5472 | |
| 5473 | CXXConstructorDecl *Constructor |
| 5474 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
| 5475 | |
| 5476 | if (!Constructor) { |
| 5477 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 5478 | return; |
| 5479 | } |
| 5480 | |
| 5481 | |
| 5482 | |
| 5483 | |
| 5484 | llvm::DenseMap<const void *, CXXCtorInitializer *> Members; |
| 5485 | |
| 5486 | |
| 5487 | RedundantUnionMap MemberUnions; |
| 5488 | |
| 5489 | bool HadError = false; |
| 5490 | for (unsigned i = 0; i < MemInits.size(); i++) { |
| 5491 | CXXCtorInitializer *Init = MemInits[i]; |
| 5492 | |
| 5493 | |
| 5494 | Init->setSourceOrder(i); |
| 5495 | |
| 5496 | if (Init->isAnyMemberInitializer()) { |
| 5497 | const void *Key = GetKeyForMember(Context, Init); |
| 5498 | if (CheckRedundantInit(*this, Init, Members[Key]) || |
| 5499 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 5500 | HadError = true; |
| 5501 | } else if (Init->isBaseInitializer()) { |
| 5502 | const void *Key = GetKeyForMember(Context, Init); |
| 5503 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 5504 | HadError = true; |
| 5505 | } else { |
| 5506 | assert(Init->isDelegatingInitializer()); |
| 5507 | |
| 5508 | if (MemInits.size() != 1) { |
| 5509 | Diag(Init->getSourceLocation(), |
| 5510 | diag::err_delegating_initializer_alone) |
| 5511 | << Init->getSourceRange() << MemInits[i ? 0 : 1]->getSourceRange(); |
| 5512 | |
| 5513 | } |
| 5514 | SetDelegatingInitializer(Constructor, MemInits[i]); |
| 5515 | |
| 5516 | return; |
| 5517 | } |
| 5518 | } |
| 5519 | |
| 5520 | if (HadError) |
| 5521 | return; |
| 5522 | |
| 5523 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits); |
| 5524 | |
| 5525 | SetCtorInitializers(Constructor, AnyErrors, MemInits); |
| 5526 | |
| 5527 | DiagnoseUninitializedFields(*this, Constructor); |
| 5528 | } |
| 5529 | |
| 5530 | void |
| 5531 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 5532 | CXXRecordDecl *ClassDecl) { |
| 5533 | |
| 5534 | |
| 5535 | if (ClassDecl->isDependentContext() || ClassDecl->isUnion()) |
| 5536 | return; |
| 5537 | |
| 5538 | |
| 5539 | |
| 5540 | |
| 5541 | |
| 5542 | |
| 5543 | |
| 5544 | for (auto *Field : ClassDecl->fields()) { |
| 5545 | if (Field->isInvalidDecl()) |
| 5546 | continue; |
| 5547 | |
| 5548 | |
| 5549 | if (isIncompleteOrZeroLengthArrayType(Context, Field->getType())) |
| 5550 | continue; |
| 5551 | |
| 5552 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 5553 | |
| 5554 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 5555 | if (!RT) |
| 5556 | continue; |
| 5557 | |
| 5558 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 5559 | if (FieldClassDecl->isInvalidDecl()) |
| 5560 | continue; |
| 5561 | if (FieldClassDecl->hasIrrelevantDestructor()) |
| 5562 | continue; |
| 5563 | |
| 5564 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
| 5565 | continue; |
| 5566 | |
| 5567 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
| 5568 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
| 5569 | CheckDestructorAccess(Field->getLocation(), Dtor, |
| 5570 | PDiag(diag::err_access_dtor_field) |
| 5571 | << Field->getDeclName() |
| 5572 | << FieldType); |
| 5573 | |
| 5574 | MarkFunctionReferenced(Location, Dtor); |
| 5575 | DiagnoseUseOfDecl(Dtor, Location); |
| 5576 | } |
| 5577 | |
| 5578 | |
| 5579 | |
| 5580 | bool VisitVirtualBases = !ClassDecl->isAbstract(); |
| 5581 | |
| 5582 | |
| 5583 | |
| 5584 | |
| 5585 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5586 | CXXDestructorDecl *Dtor = ClassDecl->getDestructor(); |
| 5587 | if (Dtor && Dtor->isUsed()) |
| 5588 | VisitVirtualBases = false; |
| 5589 | } |
| 5590 | |
| 5591 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 5592 | |
| 5593 | |
| 5594 | for (const auto &Base : ClassDecl->bases()) { |
| 5595 | const RecordType *RT = Base.getType()->getAs<RecordType>(); |
| 5596 | if (!RT) |
| 5597 | continue; |
| 5598 | |
| 5599 | |
| 5600 | if (Base.isVirtual()) { |
| 5601 | if (!VisitVirtualBases) |
| 5602 | continue; |
| 5603 | DirectVirtualBases.insert(RT); |
| 5604 | } |
| 5605 | |
| 5606 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 5607 | |
| 5608 | if (BaseClassDecl->isInvalidDecl()) |
| 5609 | continue; |
| 5610 | if (BaseClassDecl->hasIrrelevantDestructor()) |
| 5611 | continue; |
| 5612 | |
| 5613 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
| 5614 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
| 5615 | |
| 5616 | |
| 5617 | CheckDestructorAccess(Base.getBeginLoc(), Dtor, |
| 5618 | PDiag(diag::err_access_dtor_base) |
| 5619 | << Base.getType() << Base.getSourceRange(), |
| 5620 | Context.getTypeDeclType(ClassDecl)); |
| 5621 | |
| 5622 | MarkFunctionReferenced(Location, Dtor); |
| 5623 | DiagnoseUseOfDecl(Dtor, Location); |
| 5624 | } |
| 5625 | |
| 5626 | if (VisitVirtualBases) |
| 5627 | MarkVirtualBaseDestructorsReferenced(Location, ClassDecl, |
| 5628 | &DirectVirtualBases); |
| 5629 | } |
| 5630 | |
| 5631 | void Sema::MarkVirtualBaseDestructorsReferenced( |
| 5632 | SourceLocation Location, CXXRecordDecl *ClassDecl, |
| 5633 | llvm::SmallPtrSetImpl<const RecordType *> *DirectVirtualBases) { |
| 5634 | |
| 5635 | for (const auto &VBase : ClassDecl->vbases()) { |
| 5636 | |
| 5637 | const RecordType *RT = VBase.getType()->castAs<RecordType>(); |
| 5638 | |
| 5639 | |
| 5640 | if (DirectVirtualBases && DirectVirtualBases->count(RT)) |
| 5641 | continue; |
| 5642 | |
| 5643 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 5644 | |
| 5645 | if (BaseClassDecl->isInvalidDecl()) |
| 5646 | continue; |
| 5647 | if (BaseClassDecl->hasIrrelevantDestructor()) |
| 5648 | continue; |
| 5649 | |
| 5650 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
| 5651 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
| 5652 | if (CheckDestructorAccess( |
| 5653 | ClassDecl->getLocation(), Dtor, |
| 5654 | PDiag(diag::err_access_dtor_vbase) |
| 5655 | << Context.getTypeDeclType(ClassDecl) << VBase.getType(), |
| 5656 | Context.getTypeDeclType(ClassDecl)) == |
| 5657 | AR_accessible) { |
| 5658 | CheckDerivedToBaseConversion( |
| 5659 | Context.getTypeDeclType(ClassDecl), VBase.getType(), |
| 5660 | diag::err_access_dtor_vbase, 0, ClassDecl->getLocation(), |
| 5661 | SourceRange(), DeclarationName(), nullptr); |
| 5662 | } |
| 5663 | |
| 5664 | MarkFunctionReferenced(Location, Dtor); |
| 5665 | DiagnoseUseOfDecl(Dtor, Location); |
| 5666 | } |
| 5667 | } |
| 5668 | |
| 5669 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
| 5670 | if (!CDtorDecl) |
| 5671 | return; |
| 5672 | |
| 5673 | if (CXXConstructorDecl *Constructor |
| 5674 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) { |
| 5675 | SetCtorInitializers(Constructor, false); |
| 5676 | DiagnoseUninitializedFields(*this, Constructor); |
| 5677 | } |
| 5678 | } |
| 5679 | |
| 5680 | bool Sema::isAbstractType(SourceLocation Loc, QualType T) { |
| 5681 | if (!getLangOpts().CPlusPlus) |
| 5682 | return false; |
| 5683 | |
| 5684 | const auto *RD = Context.getBaseElementType(T)->getAsCXXRecordDecl(); |
| 5685 | if (!RD) |
| 5686 | return false; |
| 5687 | |
| 5688 | |
| 5689 | |
| 5690 | |
| 5691 | |
| 5692 | |
| 5693 | |
| 5694 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 5695 | if (!Def || Def->isBeingDefined()) |
| 5696 | return false; |
| 5697 | |
| 5698 | return RD->isAbstract(); |
| 5699 | } |
| 5700 | |
| 5701 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| 5702 | TypeDiagnoser &Diagnoser) { |
| 5703 | if (!isAbstractType(Loc, T)) |
| 5704 | return false; |
| 5705 | |
| 5706 | T = Context.getBaseElementType(T); |
| 5707 | Diagnoser.diagnose(*this, Loc, T); |
| 5708 | DiagnoseAbstractType(T->getAsCXXRecordDecl()); |
| 5709 | return true; |
| 5710 | } |
| 5711 | |
| 5712 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 5713 | |
| 5714 | |
| 5715 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
| 5716 | return; |
| 5717 | |
| 5718 | |
| 5719 | |
| 5720 | |
| 5721 | if (Diags.isLastDiagnosticIgnored()) |
| 5722 | return; |
| 5723 | |
| 5724 | CXXFinalOverriderMap FinalOverriders; |
| 5725 | RD->getFinalOverriders(FinalOverriders); |
| 5726 | |
| 5727 | |
| 5728 | |
| 5729 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 5730 | |
| 5731 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 5732 | MEnd = FinalOverriders.end(); |
| 5733 | M != MEnd; |
| 5734 | ++M) { |
| 5735 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 5736 | SOEnd = M->second.end(); |
| 5737 | SO != SOEnd; ++SO) { |
| 5738 | |
| 5739 | |
| 5740 | |
| 5741 | |
| 5742 | |
| 5743 | |
| 5744 | if (SO->second.size() != 1) |
| 5745 | continue; |
| 5746 | |
| 5747 | if (!SO->second.front().Method->isPure()) |
| 5748 | continue; |
| 5749 | |
| 5750 | if (!SeenPureMethods.insert(SO->second.front().Method).second) |
| 5751 | continue; |
| 5752 | |
| 5753 | Diag(SO->second.front().Method->getLocation(), |
| 5754 | diag::note_pure_virtual_function) |
| 5755 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
| 5756 | } |
| 5757 | } |
| 5758 | |
| 5759 | if (!PureVirtualClassDiagSet) |
| 5760 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 5761 | PureVirtualClassDiagSet->insert(RD); |
| 5762 | } |
| 5763 | |
| 5764 | namespace { |
| 5765 | struct AbstractUsageInfo { |
| 5766 | Sema &S; |
| 5767 | CXXRecordDecl *Record; |
| 5768 | CanQualType AbstractType; |
| 5769 | bool Invalid; |
| 5770 | |
| 5771 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 5772 | : S(S), Record(Record), |
| 5773 | AbstractType(S.Context.getCanonicalType( |
| 5774 | S.Context.getTypeDeclType(Record))), |
| 5775 | Invalid(false) {} |
| 5776 | |
| 5777 | void DiagnoseAbstractType() { |
| 5778 | if (Invalid) return; |
| 5779 | S.DiagnoseAbstractType(Record); |
| 5780 | Invalid = true; |
| 5781 | } |
| 5782 | |
| 5783 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 5784 | }; |
| 5785 | |
| 5786 | struct CheckAbstractUsage { |
| 5787 | AbstractUsageInfo &Info; |
| 5788 | const NamedDecl *Ctx; |
| 5789 | |
| 5790 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 5791 | : Info(Info), Ctx(Ctx) {} |
| 5792 | |
| 5793 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 5794 | switch (TL.getTypeLocClass()) { |
| 5795 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 5796 | #define TYPELOC(CLASS, PARENT) \ |
| 5797 | case TypeLoc::CLASS: Check(TL.castAs<CLASS##TypeLoc>(), Sel); break; |
| 5798 | #include "clang/AST/TypeLocNodes.def" |
| 5799 | } |
| 5800 | } |
| 5801 | |
| 5802 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 5803 | Visit(TL.getReturnLoc(), Sema::AbstractReturnType); |
| 5804 | for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) { |
| 5805 | if (!TL.getParam(I)) |
| 5806 | continue; |
| 5807 | |
| 5808 | TypeSourceInfo *TSI = TL.getParam(I)->getTypeSourceInfo(); |
| 5809 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
| 5810 | } |
| 5811 | } |
| 5812 | |
| 5813 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 5814 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 5815 | } |
| 5816 | |
| 5817 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 5818 | |
| 5819 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 5820 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 5821 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 5822 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 5823 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 5824 | |
| 5825 | } |
| 5826 | } |
| 5827 | |
| 5828 | |
| 5829 | #define CheckPolymorphic(Type) \ |
| 5830 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 5831 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 5832 | } |
| 5833 | CheckPolymorphic(PointerTypeLoc) |
| 5834 | CheckPolymorphic(ReferenceTypeLoc) |
| 5835 | CheckPolymorphic(MemberPointerTypeLoc) |
| 5836 | CheckPolymorphic(BlockPointerTypeLoc) |
| 5837 | CheckPolymorphic(AtomicTypeLoc) |
| 5838 | |
| 5839 | |
| 5840 | |
| 5841 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 5842 | |
| 5843 | |
| 5844 | |
| 5845 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 5846 | return Visit(Next, Sel); |
| 5847 | |
| 5848 | |
| 5849 | |
| 5850 | if (Sel == Sema::AbstractNone) return; |
| 5851 | |
| 5852 | |
| 5853 | QualType T = TL.getType(); |
| 5854 | if (T->isArrayType()) { |
| 5855 | Sel = Sema::AbstractArrayType; |
| 5856 | T = Info.S.Context.getBaseElementType(T); |
| 5857 | } |
| 5858 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 5859 | if (CT != Info.AbstractType) return; |
| 5860 | |
| 5861 | |
| 5862 | if (Sel == Sema::AbstractArrayType) { |
| 5863 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 5864 | << T << TL.getSourceRange(); |
| 5865 | } else { |
| 5866 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 5867 | << Sel << T << TL.getSourceRange(); |
| 5868 | } |
| 5869 | Info.DiagnoseAbstractType(); |
| 5870 | } |
| 5871 | }; |
| 5872 | |
| 5873 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 5874 | Sema::AbstractDiagSelID Sel) { |
| 5875 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 5876 | } |
| 5877 | |
| 5878 | } |
| 5879 | |
| 5880 | |
| 5881 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 5882 | CXXMethodDecl *MD) { |
| 5883 | |
| 5884 | |
| 5885 | if (MD->doesThisDeclarationHaveABody()) |
| 5886 | return; |
| 5887 | |
| 5888 | |
| 5889 | |
| 5890 | |
| 5891 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 5892 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 5893 | } |
| 5894 | |
| 5895 | |
| 5896 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 5897 | CXXRecordDecl *RD) { |
| 5898 | for (auto *D : RD->decls()) { |
| 5899 | if (D->isImplicit()) continue; |
| 5900 | |
| 5901 | |
| 5902 | if (isa<CXXMethodDecl>(D)) { |
| 5903 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 5904 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 5905 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 5906 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 5907 | |
| 5908 | |
| 5909 | } else if (isa<FieldDecl>(D)) { |
| 5910 | FieldDecl *FD = cast<FieldDecl>(D); |
| 5911 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 5912 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 5913 | } else if (isa<VarDecl>(D)) { |
| 5914 | VarDecl *VD = cast<VarDecl>(D); |
| 5915 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 5916 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 5917 | |
| 5918 | |
| 5919 | } else if (isa<CXXRecordDecl>(D)) { |
| 5920 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 5921 | } else if (isa<ClassTemplateDecl>(D)) { |
| 5922 | CheckAbstractClassUsage(Info, |
| 5923 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 5924 | } |
| 5925 | } |
| 5926 | } |
| 5927 | |
| 5928 | static void ReferenceDllExportedMembers(Sema &S, CXXRecordDecl *Class) { |
| 5929 | Attr *ClassAttr = getDLLAttr(Class); |
| 5930 | if (!ClassAttr) |
| 5931 | return; |
| 5932 | |
| 5933 | assert(ClassAttr->getKind() == attr::DLLExport); |
| 5934 | |
| 5935 | TemplateSpecializationKind TSK = Class->getTemplateSpecializationKind(); |
| 5936 | |
| 5937 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 5938 | |
| 5939 | |
| 5940 | return; |
| 5941 | |
| 5942 | |
| 5943 | struct MarkingClassDllexported { |
| 5944 | Sema &S; |
| 5945 | MarkingClassDllexported(Sema &S, CXXRecordDecl *Class, |
| 5946 | SourceLocation AttrLoc) |
| 5947 | : S(S) { |
| 5948 | Sema::CodeSynthesisContext Ctx; |
| 5949 | Ctx.Kind = Sema::CodeSynthesisContext::MarkingClassDllexported; |
| 5950 | Ctx.PointOfInstantiation = AttrLoc; |
| 5951 | Ctx.Entity = Class; |
| 5952 | S.pushCodeSynthesisContext(Ctx); |
| 5953 | } |
| 5954 | ~MarkingClassDllexported() { |
| 5955 | S.popCodeSynthesisContext(); |
| 5956 | } |
| 5957 | } MarkingDllexportedContext(S, Class, ClassAttr->getLocation()); |
| 5958 | |
| 5959 | if (S.Context.getTargetInfo().getTriple().isWindowsGNUEnvironment()) |
| 5960 | S.MarkVTableUsed(Class->getLocation(), Class, true); |
| 5961 | |
| 5962 | for (Decl *Member : Class->decls()) { |
| 5963 | |
| 5964 | |
| 5965 | auto *VD = dyn_cast<VarDecl>(Member); |
| 5966 | if (VD && Member->getAttr<DLLExportAttr>() && |
| 5967 | VD->getStorageClass() == SC_Static && |
| 5968 | TSK == TSK_ImplicitInstantiation) |
| 5969 | S.MarkVariableReferenced(VD->getLocation(), VD); |
| 5970 | |
| 5971 | auto *MD = dyn_cast<CXXMethodDecl>(Member); |
| 5972 | if (!MD) |
| 5973 | continue; |
| 5974 | |
| 5975 | if (Member->getAttr<DLLExportAttr>()) { |
| 5976 | if (MD->isUserProvided()) { |
| 5977 | |
| 5978 | |
| 5979 | |
| 5980 | if (TSK == TSK_ImplicitInstantiation && !ClassAttr->isInherited()) |
| 5981 | continue; |
| 5982 | |
| 5983 | S.MarkFunctionReferenced(Class->getLocation(), MD); |
| 5984 | |
| 5985 | |
| 5986 | |
| 5987 | } else if (MD->isExplicitlyDefaulted()) { |
| 5988 | |
| 5989 | S.MarkFunctionReferenced(Class->getLocation(), MD); |
| 5990 | |
| 5991 | if (TSK != TSK_ExplicitInstantiationDefinition) { |
| 5992 | |
| 5993 | |
| 5994 | S.Consumer.HandleTopLevelDecl(DeclGroupRef(MD)); |
| 5995 | } |
| 5996 | } else if (!MD->isTrivial() || |
| 5997 | MD->isCopyAssignmentOperator() || |
| 5998 | MD->isMoveAssignmentOperator()) { |
| 5999 | |
| 6000 | |
| 6001 | |
| 6002 | |
| 6003 | S.MarkFunctionReferenced(Class->getLocation(), MD); |
| 6004 | |
| 6005 | |
| 6006 | |
| 6007 | S.Consumer.HandleTopLevelDecl(DeclGroupRef(MD)); |
| 6008 | } |
| 6009 | } |
| 6010 | } |
| 6011 | } |
| 6012 | |
| 6013 | static void checkForMultipleExportedDefaultConstructors(Sema &S, |
| 6014 | CXXRecordDecl *Class) { |
| 6015 | |
| 6016 | |
| 6017 | if (!S.Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 6018 | return; |
| 6019 | |
| 6020 | CXXConstructorDecl *LastExportedDefaultCtor = nullptr; |
| 6021 | for (Decl *Member : Class->decls()) { |
| 6022 | |
| 6023 | auto *CD = dyn_cast<CXXConstructorDecl>(Member); |
| 6024 | if (!CD || !CD->isDefaultConstructor()) |
| 6025 | continue; |
| 6026 | auto *Attr = CD->getAttr<DLLExportAttr>(); |
| 6027 | if (!Attr) |
| 6028 | continue; |
| 6029 | |
| 6030 | |
| 6031 | |
| 6032 | if (!Class->isDependentContext()) { |
| 6033 | for (ParmVarDecl *PD : CD->parameters()) { |
| 6034 | (void)S.CheckCXXDefaultArgExpr(Attr->getLocation(), CD, PD); |
| 6035 | S.DiscardCleanupsInEvaluationContext(); |
| 6036 | } |
| 6037 | } |
| 6038 | |
| 6039 | if (LastExportedDefaultCtor) { |
| 6040 | S.Diag(LastExportedDefaultCtor->getLocation(), |
| 6041 | diag::err_attribute_dll_ambiguous_default_ctor) |
| 6042 | << Class; |
| 6043 | S.Diag(CD->getLocation(), diag::note_entity_declared_at) |
| 6044 | << CD->getDeclName(); |
| 6045 | return; |
| 6046 | } |
| 6047 | LastExportedDefaultCtor = CD; |
| 6048 | } |
| 6049 | } |
| 6050 | |
| 6051 | static void checkCUDADeviceBuiltinSurfaceClassTemplate(Sema &S, |
| 6052 | CXXRecordDecl *Class) { |
| 6053 | bool ErrorReported = false; |
| 6054 | auto reportIllegalClassTemplate = [&ErrorReported](Sema &S, |
| 6055 | ClassTemplateDecl *TD) { |
| 6056 | if (ErrorReported) |
| 6057 | return; |
| 6058 | S.Diag(TD->getLocation(), |
| 6059 | diag::err_cuda_device_builtin_surftex_cls_template) |
| 6060 | << 0 << TD; |
| 6061 | ErrorReported = true; |
| 6062 | }; |
| 6063 | |
| 6064 | ClassTemplateDecl *TD = Class->getDescribedClassTemplate(); |
| 6065 | if (!TD) { |
| 6066 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(Class); |
| 6067 | if (!SD) { |
| 6068 | S.Diag(Class->getLocation(), |
| 6069 | diag::err_cuda_device_builtin_surftex_ref_decl) |
| 6070 | << 0 << Class; |
| 6071 | S.Diag(Class->getLocation(), |
| 6072 | diag::note_cuda_device_builtin_surftex_should_be_template_class) |
| 6073 | << Class; |
| 6074 | return; |
| 6075 | } |
| 6076 | TD = SD->getSpecializedTemplate(); |
| 6077 | } |
| 6078 | |
| 6079 | TemplateParameterList *Params = TD->getTemplateParameters(); |
| 6080 | unsigned N = Params->size(); |
| 6081 | |
| 6082 | if (N != 2) { |
| 6083 | reportIllegalClassTemplate(S, TD); |
| 6084 | S.Diag(TD->getLocation(), |
| 6085 | diag::note_cuda_device_builtin_surftex_cls_should_have_n_args) |
| 6086 | << TD << 2; |
| 6087 | } |
| 6088 | if (N > 0 && !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
| 6089 | reportIllegalClassTemplate(S, TD); |
| 6090 | S.Diag(TD->getLocation(), |
| 6091 | diag::note_cuda_device_builtin_surftex_cls_should_have_match_arg) |
| 6092 | << TD << 0 << 0; |
| 6093 | } |
| 6094 | if (N > 1) { |
| 6095 | auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(1)); |
| 6096 | if (!NTTP || !NTTP->getType()->isIntegralOrEnumerationType()) { |
| 6097 | reportIllegalClassTemplate(S, TD); |
| 6098 | S.Diag(TD->getLocation(), |
| 6099 | diag::note_cuda_device_builtin_surftex_cls_should_have_match_arg) |
| 6100 | << TD << 1 << 1; |
| 6101 | } |
| 6102 | } |
| 6103 | } |
| 6104 | |
| 6105 | static void checkCUDADeviceBuiltinTextureClassTemplate(Sema &S, |
| 6106 | CXXRecordDecl *Class) { |
| 6107 | bool ErrorReported = false; |
| 6108 | auto reportIllegalClassTemplate = [&ErrorReported](Sema &S, |
| 6109 | ClassTemplateDecl *TD) { |
| 6110 | if (ErrorReported) |
| 6111 | return; |
| 6112 | S.Diag(TD->getLocation(), |
| 6113 | diag::err_cuda_device_builtin_surftex_cls_template) |
| 6114 | << 1 << TD; |
| 6115 | ErrorReported = true; |
| 6116 | }; |
| 6117 | |
| 6118 | ClassTemplateDecl *TD = Class->getDescribedClassTemplate(); |
| 6119 | if (!TD) { |
| 6120 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(Class); |
| 6121 | if (!SD) { |
| 6122 | S.Diag(Class->getLocation(), |
| 6123 | diag::err_cuda_device_builtin_surftex_ref_decl) |
| 6124 | << 1 << Class; |
| 6125 | S.Diag(Class->getLocation(), |
| 6126 | diag::note_cuda_device_builtin_surftex_should_be_template_class) |
| 6127 | << Class; |
| 6128 | return; |
| 6129 | } |
| 6130 | TD = SD->getSpecializedTemplate(); |
| 6131 | } |
| 6132 | |
| 6133 | TemplateParameterList *Params = TD->getTemplateParameters(); |
| 6134 | unsigned N = Params->size(); |
| 6135 | |
| 6136 | if (N != 3) { |
| 6137 | reportIllegalClassTemplate(S, TD); |
| 6138 | S.Diag(TD->getLocation(), |
| 6139 | diag::note_cuda_device_builtin_surftex_cls_should_have_n_args) |
| 6140 | << TD << 3; |
| 6141 | } |
| 6142 | if (N > 0 && !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
| 6143 | reportIllegalClassTemplate(S, TD); |
| 6144 | S.Diag(TD->getLocation(), |
| 6145 | diag::note_cuda_device_builtin_surftex_cls_should_have_match_arg) |
| 6146 | << TD << 0 << 0; |
| 6147 | } |
| 6148 | if (N > 1) { |
| 6149 | auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(1)); |
| 6150 | if (!NTTP || !NTTP->getType()->isIntegralOrEnumerationType()) { |
| 6151 | reportIllegalClassTemplate(S, TD); |
| 6152 | S.Diag(TD->getLocation(), |
| 6153 | diag::note_cuda_device_builtin_surftex_cls_should_have_match_arg) |
| 6154 | << TD << 1 << 1; |
| 6155 | } |
| 6156 | } |
| 6157 | if (N > 2) { |
| 6158 | auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(2)); |
| 6159 | if (!NTTP || !NTTP->getType()->isIntegralOrEnumerationType()) { |
| 6160 | reportIllegalClassTemplate(S, TD); |
| 6161 | S.Diag(TD->getLocation(), |
| 6162 | diag::note_cuda_device_builtin_surftex_cls_should_have_match_arg) |
| 6163 | << TD << 2 << 1; |
| 6164 | } |
| 6165 | } |
| 6166 | } |
| 6167 | |
| 6168 | void Sema::checkClassLevelCodeSegAttribute(CXXRecordDecl *Class) { |
| 6169 | |
| 6170 | for (auto *Method : Class->methods()) { |
| 6171 | if (Method->isUserProvided()) |
| 6172 | continue; |
| 6173 | if (Attr *A = getImplicitCodeSegOrSectionAttrForFunction(Method, true)) |
| 6174 | Method->addAttr(A); |
| 6175 | } |
| 6176 | } |
| 6177 | |
| 6178 | |
| 6179 | void Sema::checkClassLevelDLLAttribute(CXXRecordDecl *Class) { |
| 6180 | Attr *ClassAttr = getDLLAttr(Class); |
| 6181 | |
| 6182 | |
| 6183 | if (Context.getTargetInfo().shouldDLLImportComdatSymbols() && !ClassAttr) { |
| 6184 | if (auto *Spec = dyn_cast<ClassTemplatePartialSpecializationDecl>(Class)) { |
| 6185 | if (Attr *TemplateAttr = |
| 6186 | getDLLAttr(Spec->getSpecializedTemplate()->getTemplatedDecl())) { |
| 6187 | auto *A = cast<InheritableAttr>(TemplateAttr->clone(getASTContext())); |
| 6188 | A->setInherited(true); |
| 6189 | ClassAttr = A; |
| 6190 | } |
| 6191 | } |
| 6192 | } |
| 6193 | |
| 6194 | if (!ClassAttr) |
| 6195 | return; |
| 6196 | |
| 6197 | if (!Class->isExternallyVisible()) { |
| 6198 | Diag(Class->getLocation(), diag::err_attribute_dll_not_extern) |
| 6199 | << Class << ClassAttr; |
| 6200 | return; |
| 6201 | } |
| 6202 | |
| 6203 | if (Context.getTargetInfo().shouldDLLImportComdatSymbols() && |
| 6204 | !ClassAttr->isInherited()) { |
| 6205 | |
| 6206 | for (Decl *Member : Class->decls()) { |
| 6207 | if (!isa<VarDecl>(Member) && !isa<CXXMethodDecl>(Member)) |
| 6208 | continue; |
| 6209 | InheritableAttr *MemberAttr = getDLLAttr(Member); |
| 6210 | if (!MemberAttr || MemberAttr->isInherited() || Member->isInvalidDecl()) |
| 6211 | continue; |
| 6212 | |
| 6213 | Diag(MemberAttr->getLocation(), |
| 6214 | diag::err_attribute_dll_member_of_dll_class) |
| 6215 | << MemberAttr << ClassAttr; |
| 6216 | Diag(ClassAttr->getLocation(), diag::note_previous_attribute); |
| 6217 | Member->setInvalidDecl(); |
| 6218 | } |
| 6219 | } |
| 6220 | |
| 6221 | if (Class->getDescribedClassTemplate()) |
| 6222 | |
| 6223 | return; |
| 6224 | |
| 6225 | |
| 6226 | const bool ClassExported = ClassAttr->getKind() == attr::DLLExport; |
| 6227 | |
| 6228 | |
| 6229 | |
| 6230 | |
| 6231 | const bool PropagatedImport = |
| 6232 | !ClassExported && |
| 6233 | cast<DLLImportAttr>(ClassAttr)->wasPropagatedToBaseTemplate(); |
| 6234 | |
| 6235 | TemplateSpecializationKind TSK = Class->getTemplateSpecializationKind(); |
| 6236 | |
| 6237 | |
| 6238 | |
| 6239 | if (ClassExported && !ClassAttr->isInherited() && |
| 6240 | TSK == TSK_ExplicitInstantiationDeclaration && |
| 6241 | !Context.getTargetInfo().getTriple().isWindowsGNUEnvironment()) { |
| 6242 | Class->dropAttr<DLLExportAttr>(); |
| 6243 | return; |
| 6244 | } |
| 6245 | |
| 6246 | |
| 6247 | ForceDeclarationOfImplicitMembers(Class); |
| 6248 | |
| 6249 | |
| 6250 | |
| 6251 | |
| 6252 | for (Decl *Member : Class->decls()) { |
| 6253 | VarDecl *VD = dyn_cast<VarDecl>(Member); |
| 6254 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 6255 | |
| 6256 | |
| 6257 | if (!VD && !MD) |
| 6258 | continue; |
| 6259 | |
| 6260 | if (MD) { |
| 6261 | |
| 6262 | if (MD->isDeleted()) |
| 6263 | continue; |
| 6264 | |
| 6265 | if (MD->isInlined()) { |
| 6266 | |
| 6267 | |
| 6268 | if (!Context.getTargetInfo().shouldDLLImportComdatSymbols() && |
| 6269 | TSK != TSK_ExplicitInstantiationDeclaration && |
| 6270 | TSK != TSK_ExplicitInstantiationDefinition) |
| 6271 | continue; |
| 6272 | |
| 6273 | |
| 6274 | |
| 6275 | |
| 6276 | auto *Ctor = dyn_cast<CXXConstructorDecl>(MD); |
| 6277 | if ((MD->isMoveAssignmentOperator() || |
| 6278 | (Ctor && Ctor->isMoveConstructor())) && |
| 6279 | !getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015)) |
| 6280 | continue; |
| 6281 | |
| 6282 | |
| 6283 | |
| 6284 | if (getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015) && |
| 6285 | (Ctor || isa<CXXDestructorDecl>(MD)) && MD->isTrivial()) |
| 6286 | continue; |
| 6287 | } |
| 6288 | } |
| 6289 | |
| 6290 | |
| 6291 | |
| 6292 | if (VD && PropagatedImport) |
| 6293 | continue; |
| 6294 | |
| 6295 | if (!cast<NamedDecl>(Member)->isExternallyVisible()) |
| 6296 | continue; |
| 6297 | |
| 6298 | if (!getDLLAttr(Member)) { |
| 6299 | InheritableAttr *NewAttr = nullptr; |
| 6300 | |
| 6301 | |
| 6302 | |
| 6303 | if (!getLangOpts().DllExportInlines && MD && MD->isInlined() && |
| 6304 | TSK != TSK_ExplicitInstantiationDeclaration && |
| 6305 | TSK != TSK_ExplicitInstantiationDefinition) { |
| 6306 | if (ClassExported) { |
| 6307 | NewAttr = ::new (getASTContext()) |
| 6308 | DLLExportStaticLocalAttr(getASTContext(), *ClassAttr); |
| 6309 | } else { |
| 6310 | NewAttr = ::new (getASTContext()) |
| 6311 | DLLImportStaticLocalAttr(getASTContext(), *ClassAttr); |
| 6312 | } |
| 6313 | } else { |
| 6314 | NewAttr = cast<InheritableAttr>(ClassAttr->clone(getASTContext())); |
| 6315 | } |
| 6316 | |
| 6317 | NewAttr->setInherited(true); |
| 6318 | Member->addAttr(NewAttr); |
| 6319 | |
| 6320 | if (MD) { |
| 6321 | |
| 6322 | |
| 6323 | for (FunctionDecl *FD = MD->getMostRecentDecl(); FD; |
| 6324 | FD = FD->getPreviousDecl()) { |
| 6325 | if (FD->getFriendObjectKind() == Decl::FOK_None) |
| 6326 | continue; |
| 6327 | assert(!getDLLAttr(FD) && |
| 6328 | "friend re-decl should not already have a DLLAttr"); |
| 6329 | NewAttr = cast<InheritableAttr>(ClassAttr->clone(getASTContext())); |
| 6330 | NewAttr->setInherited(true); |
| 6331 | FD->addAttr(NewAttr); |
| 6332 | } |
| 6333 | } |
| 6334 | } |
| 6335 | } |
| 6336 | |
| 6337 | if (ClassExported) |
| 6338 | DelayedDllExportClasses.push_back(Class); |
| 6339 | } |
| 6340 | |
| 6341 | |
| 6342 | |
| 6343 | void Sema::propagateDLLAttrToBaseClassTemplate( |
| 6344 | CXXRecordDecl *Class, Attr *ClassAttr, |
| 6345 | ClassTemplateSpecializationDecl *BaseTemplateSpec, SourceLocation BaseLoc) { |
| 6346 | if (getDLLAttr( |
| 6347 | BaseTemplateSpec->getSpecializedTemplate()->getTemplatedDecl())) { |
| 6348 | |
| 6349 | return; |
| 6350 | } |
| 6351 | |
| 6352 | auto TSK = BaseTemplateSpec->getSpecializationKind(); |
| 6353 | if (!getDLLAttr(BaseTemplateSpec) && |
| 6354 | (TSK == TSK_Undeclared || TSK == TSK_ExplicitInstantiationDeclaration || |
| 6355 | TSK == TSK_ImplicitInstantiation)) { |
| 6356 | |
| 6357 | |
| 6358 | |
| 6359 | auto *NewAttr = cast<InheritableAttr>(ClassAttr->clone(getASTContext())); |
| 6360 | NewAttr->setInherited(true); |
| 6361 | BaseTemplateSpec->addAttr(NewAttr); |
| 6362 | |
| 6363 | |
| 6364 | |
| 6365 | if (auto *ImportAttr = dyn_cast<DLLImportAttr>(NewAttr)) |
| 6366 | ImportAttr->setPropagatedToBaseTemplate(); |
| 6367 | |
| 6368 | |
| 6369 | |
| 6370 | |
| 6371 | if (TSK != TSK_Undeclared) |
| 6372 | checkClassLevelDLLAttribute(BaseTemplateSpec); |
| 6373 | |
| 6374 | return; |
| 6375 | } |
| 6376 | |
| 6377 | if (getDLLAttr(BaseTemplateSpec)) { |
| 6378 | |
| 6379 | |
| 6380 | |
| 6381 | return; |
| 6382 | } |
| 6383 | |
| 6384 | |
| 6385 | |
| 6386 | |
| 6387 | Diag(BaseLoc, diag::warn_attribute_dll_instantiated_base_class) |
| 6388 | << BaseTemplateSpec->isExplicitSpecialization(); |
| 6389 | Diag(ClassAttr->getLocation(), diag::note_attribute); |
| 6390 | if (BaseTemplateSpec->isExplicitSpecialization()) { |
| 6391 | Diag(BaseTemplateSpec->getLocation(), |
| 6392 | diag::note_template_class_explicit_specialization_was_here) |
| 6393 | << BaseTemplateSpec; |
| 6394 | } else { |
| 6395 | Diag(BaseTemplateSpec->getPointOfInstantiation(), |
| 6396 | diag::note_template_class_instantiation_was_here) |
| 6397 | << BaseTemplateSpec; |
| 6398 | } |
| 6399 | } |
| 6400 | |
| 6401 | |
| 6402 | |
| 6403 | |
| 6404 | |
| 6405 | |
| 6406 | |
| 6407 | |
| 6408 | Sema::DefaultedFunctionKind |
| 6409 | Sema::getDefaultedFunctionKind(const FunctionDecl *FD) { |
| 6410 | if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 6411 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(FD)) { |
| 6412 | if (Ctor->isDefaultConstructor()) |
| 6413 | return Sema::CXXDefaultConstructor; |
| 6414 | |
| 6415 | if (Ctor->isCopyConstructor()) |
| 6416 | return Sema::CXXCopyConstructor; |
| 6417 | |
| 6418 | if (Ctor->isMoveConstructor()) |
| 6419 | return Sema::CXXMoveConstructor; |
| 6420 | } |
| 6421 | |
| 6422 | if (MD->isCopyAssignmentOperator()) |
| 6423 | return Sema::CXXCopyAssignment; |
| 6424 | |
| 6425 | if (MD->isMoveAssignmentOperator()) |
| 6426 | return Sema::CXXMoveAssignment; |
| 6427 | |
| 6428 | if (isa<CXXDestructorDecl>(FD)) |
| 6429 | return Sema::CXXDestructor; |
| 6430 | } |
| 6431 | |
| 6432 | switch (FD->getDeclName().getCXXOverloadedOperator()) { |
| 6433 | case OO_EqualEqual: |
| 6434 | return DefaultedComparisonKind::Equal; |
| 6435 | |
| 6436 | case OO_ExclaimEqual: |
| 6437 | return DefaultedComparisonKind::NotEqual; |
| 6438 | |
| 6439 | case OO_Spaceship: |
| 6440 | |
| 6441 | if (!getLangOpts().CPlusPlus20) |
| 6442 | break; |
| 6443 | return DefaultedComparisonKind::ThreeWay; |
| 6444 | |
| 6445 | case OO_Less: |
| 6446 | case OO_LessEqual: |
| 6447 | case OO_Greater: |
| 6448 | case OO_GreaterEqual: |
| 6449 | |
| 6450 | if (!getLangOpts().CPlusPlus20) |
| 6451 | break; |
| 6452 | return DefaultedComparisonKind::Relational; |
| 6453 | |
| 6454 | default: |
| 6455 | break; |
| 6456 | } |
| 6457 | |
| 6458 | |
| 6459 | return DefaultedFunctionKind(); |
| 6460 | } |
| 6461 | |
| 6462 | static void DefineDefaultedFunction(Sema &S, FunctionDecl *FD, |
| 6463 | SourceLocation DefaultLoc) { |
| 6464 | Sema::DefaultedFunctionKind DFK = S.getDefaultedFunctionKind(FD); |
| 6465 | if (DFK.isComparison()) |
| 6466 | return S.DefineDefaultedComparison(DefaultLoc, FD, DFK.asComparison()); |
| 6467 | |
| 6468 | switch (DFK.asSpecialMember()) { |
| 6469 | case Sema::CXXDefaultConstructor: |
| 6470 | S.DefineImplicitDefaultConstructor(DefaultLoc, |
| 6471 | cast<CXXConstructorDecl>(FD)); |
| 6472 | break; |
| 6473 | case Sema::CXXCopyConstructor: |
| 6474 | S.DefineImplicitCopyConstructor(DefaultLoc, cast<CXXConstructorDecl>(FD)); |
| 6475 | break; |
| 6476 | case Sema::CXXCopyAssignment: |
| 6477 | S.DefineImplicitCopyAssignment(DefaultLoc, cast<CXXMethodDecl>(FD)); |
| 6478 | break; |
| 6479 | case Sema::CXXDestructor: |
| 6480 | S.DefineImplicitDestructor(DefaultLoc, cast<CXXDestructorDecl>(FD)); |
| 6481 | break; |
| 6482 | case Sema::CXXMoveConstructor: |
| 6483 | S.DefineImplicitMoveConstructor(DefaultLoc, cast<CXXConstructorDecl>(FD)); |
| 6484 | break; |
| 6485 | case Sema::CXXMoveAssignment: |
| 6486 | S.DefineImplicitMoveAssignment(DefaultLoc, cast<CXXMethodDecl>(FD)); |
| 6487 | break; |
| 6488 | case Sema::CXXInvalid: |
| 6489 | llvm_unreachable("Invalid special member."); |
| 6490 | } |
| 6491 | } |
| 6492 | |
| 6493 | |
| 6494 | |
| 6495 | static bool canPassInRegisters(Sema &S, CXXRecordDecl *D, |
| 6496 | TargetInfo::CallingConvKind CCK) { |
| 6497 | if (D->isDependentType() || D->isInvalidDecl()) |
| 6498 | return false; |
| 6499 | |
| 6500 | |
| 6501 | |
| 6502 | if (CCK == TargetInfo::CCK_ClangABI4OrPS4) |
| 6503 | return !D->hasNonTrivialDestructorForCall() && |
| 6504 | !D->hasNonTrivialCopyConstructorForCall(); |
| 6505 | |
| 6506 | if (CCK == TargetInfo::CCK_MicrosoftWin64) { |
| 6507 | bool CopyCtorIsTrivial = false, CopyCtorIsTrivialForCall = false; |
| 6508 | bool DtorIsTrivialForCall = false; |
| 6509 | |
| 6510 | |
| 6511 | |
| 6512 | |
| 6513 | |
| 6514 | |
| 6515 | |
| 6516 | if (D->needsImplicitCopyConstructor()) { |
| 6517 | if (!D->defaultedCopyConstructorIsDeleted()) { |
| 6518 | if (D->hasTrivialCopyConstructor()) |
| 6519 | CopyCtorIsTrivial = true; |
| 6520 | if (D->hasTrivialCopyConstructorForCall()) |
| 6521 | CopyCtorIsTrivialForCall = true; |
| 6522 | } |
| 6523 | } else { |
| 6524 | for (const CXXConstructorDecl *CD : D->ctors()) { |
| 6525 | if (CD->isCopyConstructor() && !CD->isDeleted()) { |
| 6526 | if (CD->isTrivial()) |
| 6527 | CopyCtorIsTrivial = true; |
| 6528 | if (CD->isTrivialForCall()) |
| 6529 | CopyCtorIsTrivialForCall = true; |
| 6530 | } |
| 6531 | } |
| 6532 | } |
| 6533 | |
| 6534 | if (D->needsImplicitDestructor()) { |
| 6535 | if (!D->defaultedDestructorIsDeleted() && |
| 6536 | D->hasTrivialDestructorForCall()) |
| 6537 | DtorIsTrivialForCall = true; |
| 6538 | } else if (const auto *DD = D->getDestructor()) { |
| 6539 | if (!DD->isDeleted() && DD->isTrivialForCall()) |
| 6540 | DtorIsTrivialForCall = true; |
| 6541 | } |
| 6542 | |
| 6543 | |
| 6544 | if (CopyCtorIsTrivialForCall && DtorIsTrivialForCall) |
| 6545 | return true; |
| 6546 | |
| 6547 | |
| 6548 | |
| 6549 | |
| 6550 | |
| 6551 | |
| 6552 | |
| 6553 | |
| 6554 | |
| 6555 | |
| 6556 | |
| 6557 | bool isAArch64 = S.Context.getTargetInfo().getTriple().isAArch64(); |
| 6558 | uint64_t TypeSize = isAArch64 ? 128 : 64; |
| 6559 | |
| 6560 | if (CopyCtorIsTrivial && |
| 6561 | S.getASTContext().getTypeSize(D->getTypeForDecl()) <= TypeSize) |
| 6562 | return true; |
| 6563 | return false; |
| 6564 | } |
| 6565 | |
| 6566 | |
| 6567 | |
| 6568 | |
| 6569 | |
| 6570 | bool HasNonDeletedCopyOrMove = false; |
| 6571 | |
| 6572 | if (D->needsImplicitCopyConstructor() && |
| 6573 | !D->defaultedCopyConstructorIsDeleted()) { |
| 6574 | if (!D->hasTrivialCopyConstructorForCall()) |
| 6575 | return false; |
| 6576 | HasNonDeletedCopyOrMove = true; |
| 6577 | } |
| 6578 | |
| 6579 | if (S.getLangOpts().CPlusPlus11 && D->needsImplicitMoveConstructor() && |
| 6580 | !D->defaultedMoveConstructorIsDeleted()) { |
| 6581 | if (!D->hasTrivialMoveConstructorForCall()) |
| 6582 | return false; |
| 6583 | HasNonDeletedCopyOrMove = true; |
| 6584 | } |
| 6585 | |
| 6586 | if (D->needsImplicitDestructor() && !D->defaultedDestructorIsDeleted() && |
| 6587 | !D->hasTrivialDestructorForCall()) |
| 6588 | return false; |
| 6589 | |
| 6590 | for (const CXXMethodDecl *MD : D->methods()) { |
| 6591 | if (MD->isDeleted()) |
| 6592 | continue; |
| 6593 | |
| 6594 | auto *CD = dyn_cast<CXXConstructorDecl>(MD); |
| 6595 | if (CD && CD->isCopyOrMoveConstructor()) |
| 6596 | HasNonDeletedCopyOrMove = true; |
| 6597 | else if (!isa<CXXDestructorDecl>(MD)) |
| 6598 | continue; |
| 6599 | |
| 6600 | if (!MD->isTrivialForCall()) |
| 6601 | return false; |
| 6602 | } |
| 6603 | |
| 6604 | return HasNonDeletedCopyOrMove; |
| 6605 | } |
| 6606 | |
| 6607 | |
| 6608 | |
| 6609 | |
| 6610 | |
| 6611 | |
| 6612 | static bool |
| 6613 | ReportOverrides(Sema &S, unsigned DiagID, const CXXMethodDecl *MD, |
| 6614 | llvm::function_ref<bool(const CXXMethodDecl *)> Report) { |
| 6615 | bool IssuedDiagnostic = false; |
| 6616 | for (const CXXMethodDecl *O : MD->overridden_methods()) { |
| 6617 | if (Report(O)) { |
| 6618 | if (!IssuedDiagnostic) { |
| 6619 | S.Diag(MD->getLocation(), DiagID) << MD->getDeclName(); |
| 6620 | IssuedDiagnostic = true; |
| 6621 | } |
| 6622 | S.Diag(O->getLocation(), diag::note_overridden_virtual_function); |
| 6623 | } |
| 6624 | } |
| 6625 | return IssuedDiagnostic; |
| 6626 | } |
| 6627 | |
| 6628 | |
| 6629 | |
| 6630 | |
| 6631 | |
| 6632 | |
| 6633 | |
| 6634 | |
| 6635 | void Sema::CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record) { |
| 6636 | if (!Record) |
| 6637 | return; |
| 6638 | |
| 6639 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 6640 | AbstractUsageInfo Info(*this, Record); |
| 6641 | CheckAbstractClassUsage(Info, Record); |
| 6642 | } |
| 6643 | |
| 6644 | |
| 6645 | |
| 6646 | |
| 6647 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 6648 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor() && |
| 6649 | !Record->isLambda()) { |
| 6650 | bool Complained = false; |
| 6651 | for (const auto *F : Record->fields()) { |
| 6652 | if (F->hasInClassInitializer() || F->isUnnamedBitfield()) |
| 6653 | continue; |
| 6654 | |
| 6655 | if (F->getType()->isReferenceType() || |
| 6656 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
| 6657 | if (!Complained) { |
| 6658 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 6659 | << Record->getTagKind() << Record; |
| 6660 | Complained = true; |
| 6661 | } |
| 6662 | |
| 6663 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 6664 | << F->getType()->isReferenceType() |
| 6665 | << F->getDeclName(); |
| 6666 | } |
| 6667 | } |
| 6668 | } |
| 6669 | |
| 6670 | if (Record->getIdentifier()) { |
| 6671 | |
| 6672 | |
| 6673 | |
| 6674 | |
| 6675 | |
| 6676 | |
| 6677 | |
| 6678 | |
| 6679 | DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
| 6680 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 6681 | ++I) { |
| 6682 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 6683 | if (((isa<FieldDecl>(D) || isa<UnresolvedUsingValueDecl>(D)) && |
| 6684 | Record->hasUserDeclaredConstructor()) || |
| 6685 | isa<IndirectFieldDecl>(D)) { |
| 6686 | Diag((*I)->getLocation(), diag::err_member_name_of_class) |
| 6687 | << D->getDeclName(); |
| 6688 | break; |
| 6689 | } |
| 6690 | } |
| 6691 | } |
| 6692 | |
| 6693 | |
| 6694 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
| 6695 | CXXDestructorDecl *dtor = Record->getDestructor(); |
| 6696 | if ((!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) && |
| 6697 | !Record->hasAttr<FinalAttr>()) |
| 6698 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 6699 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 6700 | } |
| 6701 | |
| 6702 | if (Record->isAbstract()) { |
| 6703 | if (FinalAttr *FA = Record->getAttr<FinalAttr>()) { |
| 6704 | Diag(Record->getLocation(), diag::warn_abstract_final_class) |
| 6705 | << FA->isSpelledAsSealed(); |
| 6706 | DiagnoseAbstractType(Record); |
| 6707 | } |
| 6708 | } |
| 6709 | |
| 6710 | |
| 6711 | if (!Record->hasAttr<FinalAttr>()) { |
| 6712 | if (const CXXDestructorDecl *dtor = Record->getDestructor()) { |
| 6713 | if (const FinalAttr *FA = dtor->getAttr<FinalAttr>()) { |
| 6714 | Diag(FA->getLocation(), diag::warn_final_dtor_non_final_class) |
| 6715 | << FA->isSpelledAsSealed() |
| 6716 | << FixItHint::CreateInsertion( |
| 6717 | getLocForEndOfToken(Record->getLocation()), |
| 6718 | (FA->isSpelledAsSealed() ? " sealed" : " final")); |
| 6719 | Diag(Record->getLocation(), |
| 6720 | diag::note_final_dtor_non_final_class_silence) |
| 6721 | << Context.getRecordType(Record) << FA->isSpelledAsSealed(); |
| 6722 | } |
| 6723 | } |
| 6724 | } |
| 6725 | |
| 6726 | |
| 6727 | if (Record->hasAttr<TrivialABIAttr>()) |
| 6728 | checkIllFormedTrivialABIStruct(*Record); |
| 6729 | |
| 6730 | |
| 6731 | |
| 6732 | bool HasTrivialABI = Record->hasAttr<TrivialABIAttr>(); |
| 6733 | |
| 6734 | if (HasTrivialABI) |
| 6735 | Record->setHasTrivialSpecialMemberForCall(); |
| 6736 | |
| 6737 | |
| 6738 | |
| 6739 | |
| 6740 | llvm::SmallVector<FunctionDecl*, 5> DefaultedSecondaryComparisons; |
| 6741 | |
| 6742 | |
| 6743 | |
| 6744 | |
| 6745 | auto CheckCompletedMemberFunction = [&](CXXMethodDecl *MD) { |
| 6746 | |
| 6747 | if (MD->getStorageClass() == SC_Static) { |
| 6748 | if (ReportOverrides(*this, diag::err_static_overrides_virtual, MD, |
| 6749 | [](const CXXMethodDecl *) { return true; })) |
| 6750 | return; |
| 6751 | } |
| 6752 | |
| 6753 | |
| 6754 | |
| 6755 | if (ReportOverrides(*this, |
| 6756 | MD->isDeleted() ? diag::err_deleted_override |
| 6757 | : diag::err_non_deleted_override, |
| 6758 | MD, [&](const CXXMethodDecl *V) { |
| 6759 | return MD->isDeleted() != V->isDeleted(); |
| 6760 | })) { |
| 6761 | if (MD->isDefaulted() && MD->isDeleted()) |
| 6762 | |
| 6763 | DiagnoseDeletedDefaultedFunction(MD); |
| 6764 | return; |
| 6765 | } |
| 6766 | |
| 6767 | |
| 6768 | |
| 6769 | if (ReportOverrides(*this, |
| 6770 | MD->isConsteval() ? diag::err_consteval_override |
| 6771 | : diag::err_non_consteval_override, |
| 6772 | MD, [&](const CXXMethodDecl *V) { |
| 6773 | return MD->isConsteval() != V->isConsteval(); |
| 6774 | })) { |
| 6775 | if (MD->isDefaulted() && MD->isDeleted()) |
| 6776 | |
| 6777 | DiagnoseDeletedDefaultedFunction(MD); |
| 6778 | return; |
| 6779 | } |
| 6780 | }; |
| 6781 | |
| 6782 | auto CheckForDefaultedFunction = [&](FunctionDecl *FD) -> bool { |
| 6783 | if (!FD || FD->isInvalidDecl() || !FD->isExplicitlyDefaulted()) |
| 6784 | return false; |
| 6785 | |
| 6786 | DefaultedFunctionKind DFK = getDefaultedFunctionKind(FD); |
| 6787 | if (DFK.asComparison() == DefaultedComparisonKind::NotEqual || |
| 6788 | DFK.asComparison() == DefaultedComparisonKind::Relational) { |
| 6789 | DefaultedSecondaryComparisons.push_back(FD); |
| 6790 | return true; |
| 6791 | } |
| 6792 | |
| 6793 | CheckExplicitlyDefaultedFunction(S, FD); |
| 6794 | return false; |
| 6795 | }; |
| 6796 | |
| 6797 | auto CompleteMemberFunction = [&](CXXMethodDecl *M) { |
| 6798 | |
| 6799 | bool Incomplete = CheckForDefaultedFunction(M); |
| 6800 | |
| 6801 | |
| 6802 | if (Record->isDependentType()) |
| 6803 | return; |
| 6804 | |
| 6805 | |
| 6806 | |
| 6807 | CXXSpecialMember CSM = getSpecialMember(M); |
| 6808 | if (!M->isImplicit() && !M->isUserProvided()) { |
| 6809 | if (CSM != CXXInvalid) { |
| 6810 | M->setTrivial(SpecialMemberIsTrivial(M, CSM)); |
| 6811 | |
| 6812 | Record->finishedDefaultedOrDeletedMember(M); |
| 6813 | M->setTrivialForCall( |
| 6814 | HasTrivialABI || |
| 6815 | SpecialMemberIsTrivial(M, CSM, TAH_ConsiderTrivialABI)); |
| 6816 | Record->setTrivialForCallFlags(M); |
| 6817 | } |
| 6818 | } |
| 6819 | |
| 6820 | |
| 6821 | |
| 6822 | if ((CSM == CXXCopyConstructor || CSM == CXXMoveConstructor || |
| 6823 | CSM == CXXDestructor) && M->isUserProvided()) { |
| 6824 | M->setTrivialForCall(HasTrivialABI); |
| 6825 | Record->setTrivialForCallFlags(M); |
| 6826 | } |
| 6827 | |
| 6828 | if (!M->isInvalidDecl() && M->isExplicitlyDefaulted() && |
| 6829 | M->hasAttr<DLLExportAttr>()) { |
| 6830 | if (getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015) && |
| 6831 | M->isTrivial() && |
| 6832 | (CSM == CXXDefaultConstructor || CSM == CXXCopyConstructor || |
| 6833 | CSM == CXXDestructor)) |
| 6834 | M->dropAttr<DLLExportAttr>(); |
| 6835 | |
| 6836 | if (M->hasAttr<DLLExportAttr>()) { |
| 6837 | |
| 6838 | DelayedDllExportMemberFunctions.push_back(M); |
| 6839 | } |
| 6840 | } |
| 6841 | |
| 6842 | |
| 6843 | |
| 6844 | |
| 6845 | if (M->isDefaulted() && M->isConstexpr() && M->size_overridden_methods()) |
| 6846 | DefineDefaultedFunction(*this, M, M->getLocation()); |
| 6847 | |
| 6848 | if (!Incomplete) |
| 6849 | CheckCompletedMemberFunction(M); |
| 6850 | }; |
| 6851 | |
| 6852 | |
| 6853 | |
| 6854 | |
| 6855 | |
| 6856 | |
| 6857 | if (CXXDestructorDecl *Dtor = Record->getDestructor()) |
| 6858 | CompleteMemberFunction(Dtor); |
| 6859 | |
| 6860 | bool HasMethodWithOverrideControl = false, |
| 6861 | HasOverridingMethodWithoutOverrideControl = false; |
| 6862 | for (auto *D : Record->decls()) { |
| 6863 | if (auto *M = dyn_cast<CXXMethodDecl>(D)) { |
| 6864 | |
| 6865 | |
| 6866 | if (!Record->isDependentType()) { |
| 6867 | |
| 6868 | |
| 6869 | if (!M->isStatic()) |
| 6870 | DiagnoseHiddenVirtualMethods(M); |
| 6871 | if (M->hasAttr<OverrideAttr>()) |
| 6872 | HasMethodWithOverrideControl = true; |
| 6873 | else if (M->size_overridden_methods() > 0) |
| 6874 | HasOverridingMethodWithoutOverrideControl = true; |
| 6875 | } |
| 6876 | |
| 6877 | if (!isa<CXXDestructorDecl>(M)) |
| 6878 | CompleteMemberFunction(M); |
| 6879 | } else if (auto *F = dyn_cast<FriendDecl>(D)) { |
| 6880 | CheckForDefaultedFunction( |
| 6881 | dyn_cast_or_null<FunctionDecl>(F->getFriendDecl())); |
| 6882 | } |
| 6883 | } |
| 6884 | |
| 6885 | if (HasOverridingMethodWithoutOverrideControl) { |
| 6886 | bool HasInconsistentOverrideControl = HasMethodWithOverrideControl; |
| 6887 | for (auto *M : Record->methods()) |
| 6888 | DiagnoseAbsenceOfOverrideControl(M, HasInconsistentOverrideControl); |
| 6889 | } |
| 6890 | |
| 6891 | |
| 6892 | for (FunctionDecl *FD : DefaultedSecondaryComparisons) { |
| 6893 | CheckExplicitlyDefaultedFunction(S, FD); |
| 6894 | |
| 6895 | |
| 6896 | if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 6897 | CheckCompletedMemberFunction(MD); |
| 6898 | } |
| 6899 | |
| 6900 | |
| 6901 | |
| 6902 | |
| 6903 | |
| 6904 | |
| 6905 | |
| 6906 | |
| 6907 | |
| 6908 | |
| 6909 | |
| 6910 | |
| 6911 | |
| 6912 | |
| 6913 | if (Record->isMsStruct(Context) && !Context.getLangOpts().MSBitfields && |
| 6914 | (Record->isPolymorphic() || Record->getNumBases())) { |
| 6915 | Diag(Record->getLocation(), diag::warn_cxx_ms_struct); |
| 6916 | } |
| 6917 | |
| 6918 | checkClassLevelDLLAttribute(Record); |
| 6919 | checkClassLevelCodeSegAttribute(Record); |
| 6920 | |
| 6921 | bool ClangABICompat4 = |
| 6922 | Context.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver4; |
| 6923 | TargetInfo::CallingConvKind CCK = |
| 6924 | Context.getTargetInfo().getCallingConvKind(ClangABICompat4); |
| 6925 | bool CanPass = canPassInRegisters(*this, Record, CCK); |
| 6926 | |
| 6927 | |
| 6928 | |
| 6929 | if (Record->getArgPassingRestrictions() != RecordDecl::APK_CanNeverPassInRegs) |
| 6930 | Record->setArgPassingRestrictions(CanPass |
| 6931 | ? RecordDecl::APK_CanPassInRegs |
| 6932 | : RecordDecl::APK_CannotPassInRegs); |
| 6933 | |
| 6934 | |
| 6935 | |
| 6936 | |
| 6937 | |
| 6938 | if (Context.getTargetInfo().getCXXABI().areArgsDestroyedLeftToRightInCallee()) |
| 6939 | Record->setParamDestroyedInCallee(true); |
| 6940 | else if (Record->hasNonTrivialDestructor()) |
| 6941 | Record->setParamDestroyedInCallee(CanPass); |
| 6942 | |
| 6943 | if (getLangOpts().ForceEmitVTables) { |
| 6944 | |
| 6945 | |
| 6946 | MarkVTableUsed(Record->getInnerLocStart(), Record); |
| 6947 | } |
| 6948 | |
| 6949 | if (getLangOpts().CUDA) { |
| 6950 | if (Record->hasAttr<CUDADeviceBuiltinSurfaceTypeAttr>()) |
| 6951 | checkCUDADeviceBuiltinSurfaceClassTemplate(*this, Record); |
| 6952 | else if (Record->hasAttr<CUDADeviceBuiltinTextureTypeAttr>()) |
| 6953 | checkCUDADeviceBuiltinTextureClassTemplate(*this, Record); |
| 6954 | } |
| 6955 | } |
| 6956 | |
| 6957 | |
| 6958 | |
| 6959 | |
| 6960 | |
| 6961 | |
| 6962 | |
| 6963 | |
| 6964 | |
| 6965 | |
| 6966 | static Sema::SpecialMemberOverloadResult lookupCallFromSpecialMember( |
| 6967 | Sema &S, CXXRecordDecl *Class, Sema::CXXSpecialMember CSM, |
| 6968 | unsigned FieldQuals, bool ConstRHS) { |
| 6969 | unsigned LHSQuals = 0; |
| 6970 | if (CSM == Sema::CXXCopyAssignment || CSM == Sema::CXXMoveAssignment) |
| 6971 | LHSQuals = FieldQuals; |
| 6972 | |
| 6973 | unsigned RHSQuals = FieldQuals; |
| 6974 | if (CSM == Sema::CXXDefaultConstructor || CSM == Sema::CXXDestructor) |
| 6975 | RHSQuals = 0; |
| 6976 | else if (ConstRHS) |
| 6977 | RHSQuals |= Qualifiers::Const; |
| 6978 | |
| 6979 | return S.LookupSpecialMember(Class, CSM, |
| 6980 | RHSQuals & Qualifiers::Const, |
| 6981 | RHSQuals & Qualifiers::Volatile, |
| 6982 | false, |
| 6983 | LHSQuals & Qualifiers::Const, |
| 6984 | LHSQuals & Qualifiers::Volatile); |
| 6985 | } |
| 6986 | |
| 6987 | class Sema::InheritedConstructorInfo { |
| 6988 | Sema &S; |
| 6989 | SourceLocation UseLoc; |
| 6990 | |
| 6991 | |
| 6992 | |
| 6993 | |
| 6994 | llvm::DenseMap<CXXRecordDecl *, ConstructorUsingShadowDecl *> |
| 6995 | InheritedFromBases; |
| 6996 | |
| 6997 | public: |
| 6998 | InheritedConstructorInfo(Sema &S, SourceLocation UseLoc, |
| 6999 | ConstructorUsingShadowDecl *Shadow) |
| 7000 | : S(S), UseLoc(UseLoc) { |
| 7001 | bool DiagnosedMultipleConstructedBases = false; |
| 7002 | CXXRecordDecl *ConstructedBase = nullptr; |
| 7003 | BaseUsingDecl *ConstructedBaseIntroducer = nullptr; |
| 7004 | |
| 7005 | |
| 7006 | |
| 7007 | for (auto *D : Shadow->redecls()) { |
| 7008 | auto *DShadow = cast<ConstructorUsingShadowDecl>(D); |
| 7009 | auto *DNominatedBase = DShadow->getNominatedBaseClass(); |
| 7010 | auto *DConstructedBase = DShadow->getConstructedBaseClass(); |
| 7011 | |
| 7012 | InheritedFromBases.insert( |
| 7013 | std::make_pair(DNominatedBase->getCanonicalDecl(), |
| 7014 | DShadow->getNominatedBaseClassShadowDecl())); |
| 7015 | if (DShadow->constructsVirtualBase()) |
| 7016 | InheritedFromBases.insert( |
| 7017 | std::make_pair(DConstructedBase->getCanonicalDecl(), |
| 7018 | DShadow->getConstructedBaseClassShadowDecl())); |
| 7019 | else |
| 7020 | assert(DNominatedBase == DConstructedBase); |
| 7021 | |
| 7022 | |
| 7023 | |
| 7024 | |
| 7025 | if (!ConstructedBase) { |
| 7026 | ConstructedBase = DConstructedBase; |
| 7027 | ConstructedBaseIntroducer = D->getIntroducer(); |
| 7028 | } else if (ConstructedBase != DConstructedBase && |
| 7029 | !Shadow->isInvalidDecl()) { |
| 7030 | if (!DiagnosedMultipleConstructedBases) { |
| 7031 | S.Diag(UseLoc, diag::err_ambiguous_inherited_constructor) |
| 7032 | << Shadow->getTargetDecl(); |
| 7033 | S.Diag(ConstructedBaseIntroducer->getLocation(), |
| 7034 | diag::note_ambiguous_inherited_constructor_using) |
| 7035 | << ConstructedBase; |
| 7036 | DiagnosedMultipleConstructedBases = true; |
| 7037 | } |
| 7038 | S.Diag(D->getIntroducer()->getLocation(), |
| 7039 | diag::note_ambiguous_inherited_constructor_using) |
| 7040 | << DConstructedBase; |
| 7041 | } |
| 7042 | } |
| 7043 | |
| 7044 | if (DiagnosedMultipleConstructedBases) |
| 7045 | Shadow->setInvalidDecl(); |
| 7046 | } |
| 7047 | |
| 7048 | |
| 7049 | |
| 7050 | |
| 7051 | std::pair<CXXConstructorDecl *, bool> |
| 7052 | findConstructorForBase(CXXRecordDecl *Base, CXXConstructorDecl *Ctor) const { |
| 7053 | auto It = InheritedFromBases.find(Base->getCanonicalDecl()); |
| 7054 | if (It == InheritedFromBases.end()) |
| 7055 | return std::make_pair(nullptr, false); |
| 7056 | |
| 7057 | |
| 7058 | if (It->second) |
| 7059 | return std::make_pair( |
| 7060 | S.findInheritingConstructor(UseLoc, Ctor, It->second), |
| 7061 | It->second->constructsVirtualBase()); |
| 7062 | |
| 7063 | |
| 7064 | return std::make_pair(Ctor, false); |
| 7065 | } |
| 7066 | }; |
| 7067 | |
| 7068 | |
| 7069 | |
| 7070 | static bool |
| 7071 | specialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 7072 | Sema::CXXSpecialMember CSM, unsigned Quals, |
| 7073 | bool ConstRHS, |
| 7074 | CXXConstructorDecl *InheritedCtor = nullptr, |
| 7075 | Sema::InheritedConstructorInfo *Inherited = nullptr) { |
| 7076 | |
| 7077 | |
| 7078 | if (InheritedCtor) { |
| 7079 | assert(CSM == Sema::CXXDefaultConstructor); |
| 7080 | auto BaseCtor = |
| 7081 | Inherited->findConstructorForBase(ClassDecl, InheritedCtor).first; |
| 7082 | if (BaseCtor) |
| 7083 | return BaseCtor->isConstexpr(); |
| 7084 | } |
| 7085 | |
| 7086 | if (CSM == Sema::CXXDefaultConstructor) |
| 7087 | return ClassDecl->hasConstexprDefaultConstructor(); |
| 7088 | if (CSM == Sema::CXXDestructor) |
| 7089 | return ClassDecl->hasConstexprDestructor(); |
| 7090 | |
| 7091 | Sema::SpecialMemberOverloadResult SMOR = |
| 7092 | lookupCallFromSpecialMember(S, ClassDecl, CSM, Quals, ConstRHS); |
| 7093 | if (!SMOR.getMethod()) |
| 7094 | |
| 7095 | |
| 7096 | return true; |
| 7097 | return SMOR.getMethod()->isConstexpr(); |
| 7098 | } |
| 7099 | |
| 7100 | |
| 7101 | |
| 7102 | static bool defaultedSpecialMemberIsConstexpr( |
| 7103 | Sema &S, CXXRecordDecl *ClassDecl, Sema::CXXSpecialMember CSM, |
| 7104 | bool ConstArg, CXXConstructorDecl *InheritedCtor = nullptr, |
| 7105 | Sema::InheritedConstructorInfo *Inherited = nullptr) { |
| 7106 | if (!S.getLangOpts().CPlusPlus11) |
| 7107 | return false; |
| 7108 | |
| 7109 | |
| 7110 | |
| 7111 | bool Ctor = true; |
| 7112 | switch (CSM) { |
| 7113 | case Sema::CXXDefaultConstructor: |
| 7114 | if (Inherited) |
| 7115 | break; |
| 7116 | |
| 7117 | |
| 7118 | |
| 7119 | |
| 7120 | |
| 7121 | |
| 7122 | return ClassDecl->defaultedDefaultConstructorIsConstexpr(); |
| 7123 | |
| 7124 | case Sema::CXXCopyConstructor: |
| 7125 | case Sema::CXXMoveConstructor: |
| 7126 | |
| 7127 | break; |
| 7128 | |
| 7129 | case Sema::CXXCopyAssignment: |
| 7130 | case Sema::CXXMoveAssignment: |
| 7131 | if (!S.getLangOpts().CPlusPlus14) |
| 7132 | return false; |
| 7133 | |
| 7134 | Ctor = false; |
| 7135 | break; |
| 7136 | |
| 7137 | case Sema::CXXDestructor: |
| 7138 | return ClassDecl->defaultedDestructorIsConstexpr(); |
| 7139 | |
| 7140 | case Sema::CXXInvalid: |
| 7141 | return false; |
| 7142 | } |
| 7143 | |
| 7144 | |
| 7145 | |
| 7146 | |
| 7147 | |
| 7148 | |
| 7149 | |
| 7150 | |
| 7151 | if (Ctor && ClassDecl->isUnion()) |
| 7152 | return CSM == Sema::CXXDefaultConstructor |
| 7153 | ? ClassDecl->hasInClassInitializer() || |
| 7154 | !ClassDecl->hasVariantMembers() |
| 7155 | : true; |
| 7156 | |
| 7157 | |
| 7158 | if (Ctor && ClassDecl->getNumVBases()) |
| 7159 | return false; |
| 7160 | |
| 7161 | |
| 7162 | |
| 7163 | if (!Ctor && !ClassDecl->isLiteral()) |
| 7164 | return false; |
| 7165 | |
| 7166 | |
| 7167 | |
| 7168 | |
| 7169 | |
| 7170 | for (const auto &B : ClassDecl->bases()) { |
| 7171 | const RecordType *BaseType = B.getType()->getAs<RecordType>(); |
| 7172 | if (!BaseType) continue; |
| 7173 | |
| 7174 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 7175 | if (!specialMemberIsConstexpr(S, BaseClassDecl, CSM, 0, ConstArg, |
| 7176 | InheritedCtor, Inherited)) |
| 7177 | return false; |
| 7178 | } |
| 7179 | |
| 7180 | |
| 7181 | |
| 7182 | |
| 7183 | |
| 7184 | |
| 7185 | |
| 7186 | |
| 7187 | for (const auto *F : ClassDecl->fields()) { |
| 7188 | if (F->isInvalidDecl()) |
| 7189 | continue; |
| 7190 | if (CSM == Sema::CXXDefaultConstructor && F->hasInClassInitializer()) |
| 7191 | continue; |
| 7192 | QualType BaseType = S.Context.getBaseElementType(F->getType()); |
| 7193 | if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { |
| 7194 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 7195 | if (!specialMemberIsConstexpr(S, FieldRecDecl, CSM, |
| 7196 | BaseType.getCVRQualifiers(), |
| 7197 | ConstArg && !F->isMutable())) |
| 7198 | return false; |
| 7199 | } else if (CSM == Sema::CXXDefaultConstructor) { |
| 7200 | return false; |
| 7201 | } |
| 7202 | } |
| 7203 | |
| 7204 | |
| 7205 | return true; |
| 7206 | } |
| 7207 | |
| 7208 | namespace { |
| 7209 | |
| 7210 | |
| 7211 | struct ComputingExceptionSpec { |
| 7212 | Sema &S; |
| 7213 | |
| 7214 | ComputingExceptionSpec(Sema &S, FunctionDecl *FD, SourceLocation Loc) |
| 7215 | : S(S) { |
| 7216 | Sema::CodeSynthesisContext Ctx; |
| 7217 | Ctx.Kind = Sema::CodeSynthesisContext::ExceptionSpecEvaluation; |
| 7218 | Ctx.PointOfInstantiation = Loc; |
| 7219 | Ctx.Entity = FD; |
| 7220 | S.pushCodeSynthesisContext(Ctx); |
| 7221 | } |
| 7222 | ~ComputingExceptionSpec() { |
| 7223 | S.popCodeSynthesisContext(); |
| 7224 | } |
| 7225 | }; |
| 7226 | } |
| 7227 | |
| 7228 | static Sema::ImplicitExceptionSpecification |
| 7229 | ComputeDefaultedSpecialMemberExceptionSpec( |
| 7230 | Sema &S, SourceLocation Loc, CXXMethodDecl *MD, Sema::CXXSpecialMember CSM, |
| 7231 | Sema::InheritedConstructorInfo *ICI); |
| 7232 | |
| 7233 | static Sema::ImplicitExceptionSpecification |
| 7234 | ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc, |
| 7235 | FunctionDecl *FD, |
| 7236 | Sema::DefaultedComparisonKind DCK); |
| 7237 | |
| 7238 | static Sema::ImplicitExceptionSpecification |
| 7239 | computeImplicitExceptionSpec(Sema &S, SourceLocation Loc, FunctionDecl *FD) { |
| 7240 | auto DFK = S.getDefaultedFunctionKind(FD); |
| 7241 | if (DFK.isSpecialMember()) |
| 7242 | return ComputeDefaultedSpecialMemberExceptionSpec( |
| 7243 | S, Loc, cast<CXXMethodDecl>(FD), DFK.asSpecialMember(), nullptr); |
| 7244 | if (DFK.isComparison()) |
| 7245 | return ComputeDefaultedComparisonExceptionSpec(S, Loc, FD, |
| 7246 | DFK.asComparison()); |
| 7247 | |
| 7248 | auto *CD = cast<CXXConstructorDecl>(FD); |
| 7249 | assert(CD->getInheritedConstructor() && |
| 7250 | "only defaulted functions and inherited constructors have implicit " |
| 7251 | "exception specs"); |
| 7252 | Sema::InheritedConstructorInfo ICI( |
| 7253 | S, Loc, CD->getInheritedConstructor().getShadowDecl()); |
| 7254 | return ComputeDefaultedSpecialMemberExceptionSpec( |
| 7255 | S, Loc, CD, Sema::CXXDefaultConstructor, &ICI); |
| 7256 | } |
| 7257 | |
| 7258 | static FunctionProtoType::ExtProtoInfo getImplicitMethodEPI(Sema &S, |
| 7259 | CXXMethodDecl *MD) { |
| 7260 | FunctionProtoType::ExtProtoInfo EPI; |
| 7261 | |
| 7262 | |
| 7263 | EPI.ExceptionSpec.Type = EST_Unevaluated; |
| 7264 | EPI.ExceptionSpec.SourceDecl = MD; |
| 7265 | |
| 7266 | |
| 7267 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv( |
| 7268 | S.Context.getDefaultCallingConvention(false, |
| 7269 | true)); |
| 7270 | return EPI; |
| 7271 | } |
| 7272 | |
| 7273 | void Sema::EvaluateImplicitExceptionSpec(SourceLocation Loc, FunctionDecl *FD) { |
| 7274 | const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>(); |
| 7275 | if (FPT->getExceptionSpecType() != EST_Unevaluated) |
| 7276 | return; |
| 7277 | |
| 7278 | |
| 7279 | auto IES = computeImplicitExceptionSpec(*this, Loc, FD); |
| 7280 | auto ESI = IES.getExceptionSpec(); |
| 7281 | |
| 7282 | |
| 7283 | UpdateExceptionSpec(FD, ESI); |
| 7284 | } |
| 7285 | |
| 7286 | void Sema::CheckExplicitlyDefaultedFunction(Scope *S, FunctionDecl *FD) { |
| 7287 | assert(FD->isExplicitlyDefaulted() && "not explicitly-defaulted"); |
| 7288 | |
| 7289 | DefaultedFunctionKind DefKind = getDefaultedFunctionKind(FD); |
| 7290 | if (!DefKind) { |
| 7291 | assert(FD->getDeclContext()->isDependentContext()); |
| 7292 | return; |
| 7293 | } |
| 7294 | |
| 7295 | if (DefKind.isComparison()) |
| 7296 | UnusedPrivateFields.clear(); |
| 7297 | |
| 7298 | if (DefKind.isSpecialMember() |
| 7299 | ? CheckExplicitlyDefaultedSpecialMember(cast<CXXMethodDecl>(FD), |
| 7300 | DefKind.asSpecialMember()) |
| 7301 | : CheckExplicitlyDefaultedComparison(S, FD, DefKind.asComparison())) |
| 7302 | FD->setInvalidDecl(); |
| 7303 | } |
| 7304 | |
| 7305 | bool Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD, |
| 7306 | CXXSpecialMember CSM) { |
| 7307 | CXXRecordDecl *RD = MD->getParent(); |
| 7308 | |
| 7309 | assert(MD->isExplicitlyDefaulted() && CSM != CXXInvalid && |
| 7310 | "not an explicitly-defaulted special member"); |
| 7311 | |
| 7312 | |
| 7313 | if (RD->isDependentType()) |
| 7314 | return false; |
| 7315 | |
| 7316 | |
| 7317 | |
| 7318 | bool First = MD == MD->getCanonicalDecl(); |
| 7319 | |
| 7320 | bool HadError = false; |
| 7321 | |
| 7322 | |
| 7323 | |
| 7324 | |
| 7325 | |
| 7326 | |
| 7327 | |
| 7328 | |
| 7329 | |
| 7330 | |
| 7331 | |
| 7332 | bool DeleteOnTypeMismatch = getLangOpts().CPlusPlus20 && First; |
| 7333 | bool ShouldDeleteForTypeMismatch = false; |
| 7334 | unsigned ExpectedParams = 1; |
| 7335 | if (CSM == CXXDefaultConstructor || CSM == CXXDestructor) |
| 7336 | ExpectedParams = 0; |
| 7337 | if (MD->getNumParams() != ExpectedParams) { |
| 7338 | |
| 7339 | |
| 7340 | |
| 7341 | Diag(MD->getLocation(), diag::err_defaulted_special_member_params) |
| 7342 | << CSM << MD->getSourceRange(); |
| 7343 | HadError = true; |
| 7344 | } else if (MD->isVariadic()) { |
| 7345 | if (DeleteOnTypeMismatch) |
| 7346 | ShouldDeleteForTypeMismatch = true; |
| 7347 | else { |
| 7348 | Diag(MD->getLocation(), diag::err_defaulted_special_member_variadic) |
| 7349 | << CSM << MD->getSourceRange(); |
| 7350 | HadError = true; |
| 7351 | } |
| 7352 | } |
| 7353 | |
| 7354 | const FunctionProtoType *Type = MD->getType()->getAs<FunctionProtoType>(); |
| 7355 | |
| 7356 | bool CanHaveConstParam = false; |
| 7357 | if (CSM == CXXCopyConstructor) |
| 7358 | CanHaveConstParam = RD->implicitCopyConstructorHasConstParam(); |
| 7359 | else if (CSM == CXXCopyAssignment) |
| 7360 | CanHaveConstParam = RD->implicitCopyAssignmentHasConstParam(); |
| 7361 | |
| 7362 | QualType ReturnType = Context.VoidTy; |
| 7363 | if (CSM == CXXCopyAssignment || CSM == CXXMoveAssignment) { |
| 7364 | |
| 7365 | ReturnType = Type->getReturnType(); |
| 7366 | |
| 7367 | QualType DeclType = Context.getTypeDeclType(RD); |
| 7368 | DeclType = Context.getAddrSpaceQualType(DeclType, MD->getMethodQualifiers().getAddressSpace()); |
| 7369 | QualType ExpectedReturnType = Context.getLValueReferenceType(DeclType); |
| 7370 | |
| 7371 | if (!Context.hasSameType(ReturnType, ExpectedReturnType)) { |
| 7372 | Diag(MD->getLocation(), diag::err_defaulted_special_member_return_type) |
| 7373 | << (CSM == CXXMoveAssignment) << ExpectedReturnType; |
| 7374 | HadError = true; |
| 7375 | } |
| 7376 | |
| 7377 | |
| 7378 | if (Type->getMethodQuals().hasConst() || Type->getMethodQuals().hasVolatile()) { |
| 7379 | if (DeleteOnTypeMismatch) |
| 7380 | ShouldDeleteForTypeMismatch = true; |
| 7381 | else { |
| 7382 | Diag(MD->getLocation(), diag::err_defaulted_special_member_quals) |
| 7383 | << (CSM == CXXMoveAssignment) << getLangOpts().CPlusPlus14; |
| 7384 | HadError = true; |
| 7385 | } |
| 7386 | } |
| 7387 | } |
| 7388 | |
| 7389 | |
| 7390 | QualType ArgType = ExpectedParams ? Type->getParamType(0) : QualType(); |
| 7391 | bool HasConstParam = false; |
| 7392 | if (ExpectedParams && ArgType->isReferenceType()) { |
| 7393 | |
| 7394 | QualType ReferentType = ArgType->getPointeeType(); |
| 7395 | HasConstParam = ReferentType.isConstQualified(); |
| 7396 | |
| 7397 | if (ReferentType.isVolatileQualified()) { |
| 7398 | if (DeleteOnTypeMismatch) |
| 7399 | ShouldDeleteForTypeMismatch = true; |
| 7400 | else { |
| 7401 | Diag(MD->getLocation(), |
| 7402 | diag::err_defaulted_special_member_volatile_param) << CSM; |
| 7403 | HadError = true; |
| 7404 | } |
| 7405 | } |
| 7406 | |
| 7407 | if (HasConstParam && !CanHaveConstParam) { |
| 7408 | if (DeleteOnTypeMismatch) |
| 7409 | ShouldDeleteForTypeMismatch = true; |
| 7410 | else if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) { |
| 7411 | Diag(MD->getLocation(), |
| 7412 | diag::err_defaulted_special_member_copy_const_param) |
| 7413 | << (CSM == CXXCopyAssignment); |
| 7414 | |
| 7415 | HadError = true; |
| 7416 | } else { |
| 7417 | Diag(MD->getLocation(), |
| 7418 | diag::err_defaulted_special_member_move_const_param) |
| 7419 | << (CSM == CXXMoveAssignment); |
| 7420 | HadError = true; |
| 7421 | } |
| 7422 | } |
| 7423 | } else if (ExpectedParams) { |
| 7424 | |
| 7425 | |
| 7426 | assert(CSM == CXXCopyAssignment && "unexpected non-ref argument"); |
| 7427 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
| 7428 | HadError = true; |
| 7429 | } |
| 7430 | |
| 7431 | |
| 7432 | |
| 7433 | |
| 7434 | |
| 7435 | |
| 7436 | |
| 7437 | |
| 7438 | |
| 7439 | |
| 7440 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, RD, CSM, |
| 7441 | HasConstParam); |
| 7442 | if ((getLangOpts().CPlusPlus20 || |
| 7443 | (getLangOpts().CPlusPlus14 ? !isa<CXXDestructorDecl>(MD) |
| 7444 | : isa<CXXConstructorDecl>(MD))) && |
| 7445 | MD->isConstexpr() && !Constexpr && |
| 7446 | MD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
| 7447 | Diag(MD->getBeginLoc(), MD->isConsteval() |
| 7448 | ? diag::err_incorrect_defaulted_consteval |
| 7449 | : diag::err_incorrect_defaulted_constexpr) |
| 7450 | << CSM; |
| 7451 | |
| 7452 | HadError = true; |
| 7453 | } |
| 7454 | |
| 7455 | if (First) { |
| 7456 | |
| 7457 | |
| 7458 | |
| 7459 | |
| 7460 | MD->setConstexprKind(Constexpr ? (MD->isConsteval() |
| 7461 | ? ConstexprSpecKind::Consteval |
| 7462 | : ConstexprSpecKind::Constexpr) |
| 7463 | : ConstexprSpecKind::Unspecified); |
| 7464 | |
| 7465 | if (!Type->hasExceptionSpec()) { |
| 7466 | |
| 7467 | |
| 7468 | |
| 7469 | |
| 7470 | FunctionProtoType::ExtProtoInfo EPI = Type->getExtProtoInfo(); |
| 7471 | EPI.ExceptionSpec.Type = EST_Unevaluated; |
| 7472 | EPI.ExceptionSpec.SourceDecl = MD; |
| 7473 | MD->setType(Context.getFunctionType(ReturnType, |
| 7474 | llvm::makeArrayRef(&ArgType, |
| 7475 | ExpectedParams), |
| 7476 | EPI)); |
| 7477 | } |
| 7478 | } |
| 7479 | |
| 7480 | if (ShouldDeleteForTypeMismatch || ShouldDeleteSpecialMember(MD, CSM)) { |
| 7481 | if (First) { |
| 7482 | SetDeclDeleted(MD, MD->getLocation()); |
| 7483 | if (!inTemplateInstantiation() && !HadError) { |
| 7484 | Diag(MD->getLocation(), diag::warn_defaulted_method_deleted) << CSM; |
| 7485 | if (ShouldDeleteForTypeMismatch) { |
| 7486 | Diag(MD->getLocation(), diag::note_deleted_type_mismatch) << CSM; |
| 7487 | } else { |
| 7488 | ShouldDeleteSpecialMember(MD, CSM, nullptr, true); |
| 7489 | } |
| 7490 | } |
| 7491 | if (ShouldDeleteForTypeMismatch && !HadError) { |
| 7492 | Diag(MD->getLocation(), |
| 7493 | diag::warn_cxx17_compat_defaulted_method_type_mismatch) << CSM; |
| 7494 | } |
| 7495 | } else { |
| 7496 | |
| 7497 | |
| 7498 | |
| 7499 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) << CSM; |
| 7500 | assert(!ShouldDeleteForTypeMismatch && "deleted non-first decl"); |
| 7501 | ShouldDeleteSpecialMember(MD, CSM, nullptr, true); |
| 7502 | HadError = true; |
| 7503 | } |
| 7504 | } |
| 7505 | |
| 7506 | return HadError; |
| 7507 | } |
| 7508 | |
| 7509 | namespace { |
| 7510 | |
| 7511 | |
| 7512 | |
| 7513 | |
| 7514 | |
| 7515 | |
| 7516 | |
| 7517 | |
| 7518 | |
| 7519 | |
| 7520 | |
| 7521 | |
| 7522 | template<typename Derived, typename ResultList, typename Result, |
| 7523 | typename Subobject> |
| 7524 | class DefaultedComparisonVisitor { |
| 7525 | public: |
| 7526 | using DefaultedComparisonKind = Sema::DefaultedComparisonKind; |
| 7527 | |
| 7528 | DefaultedComparisonVisitor(Sema &S, CXXRecordDecl *RD, FunctionDecl *FD, |
| 7529 | DefaultedComparisonKind DCK) |
| 7530 | : S(S), RD(RD), FD(FD), DCK(DCK) { |
| 7531 | if (auto *Info = FD->getDefaultedFunctionInfo()) { |
| 7532 | |
| 7533 | |
| 7534 | Fns.assign(Info->getUnqualifiedLookups().begin(), |
| 7535 | Info->getUnqualifiedLookups().end()); |
| 7536 | } |
| 7537 | } |
| 7538 | |
| 7539 | ResultList visit() { |
| 7540 | |
| 7541 | QualType ParamLvalType = |
| 7542 | FD->getParamDecl(0)->getType().getNonReferenceType(); |
| 7543 | |
| 7544 | ResultList Results; |
| 7545 | |
| 7546 | switch (DCK) { |
| 7547 | case DefaultedComparisonKind::None: |
| 7548 | llvm_unreachable("not a defaulted comparison"); |
| 7549 | |
| 7550 | case DefaultedComparisonKind::Equal: |
| 7551 | case DefaultedComparisonKind::ThreeWay: |
| 7552 | getDerived().visitSubobjects(Results, RD, ParamLvalType.getQualifiers()); |
| 7553 | return Results; |
| 7554 | |
| 7555 | case DefaultedComparisonKind::NotEqual: |
| 7556 | case DefaultedComparisonKind::Relational: |
| 7557 | Results.add(getDerived().visitExpandedSubobject( |
| 7558 | ParamLvalType, getDerived().getCompleteObject())); |
| 7559 | return Results; |
| 7560 | } |
| 7561 | llvm_unreachable(""); |
| 7562 | } |
| 7563 | |
| 7564 | protected: |
| 7565 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
| 7566 | |
| 7567 | |
| 7568 | |
| 7569 | |
| 7570 | |
| 7571 | bool visitSubobjects(ResultList &Results, CXXRecordDecl *Record, |
| 7572 | Qualifiers Quals) { |
| 7573 | |
| 7574 | |
| 7575 | for (CXXBaseSpecifier &Base : Record->bases()) |
| 7576 | if (Results.add(getDerived().visitSubobject( |
| 7577 | S.Context.getQualifiedType(Base.getType(), Quals), |
| 7578 | getDerived().getBase(&Base)))) |
| 7579 | return true; |
| 7580 | |
| 7581 | |
| 7582 | for (FieldDecl *Field : Record->fields()) { |
| 7583 | |
| 7584 | if (Field->isAnonymousStructOrUnion()) { |
| 7585 | if (visitSubobjects(Results, Field->getType()->getAsCXXRecordDecl(), |
| 7586 | Quals)) |
| 7587 | return true; |
| 7588 | continue; |
| 7589 | } |
| 7590 | |
| 7591 | |
| 7592 | Qualifiers FieldQuals = Quals; |
| 7593 | if (Field->isMutable()) |
| 7594 | FieldQuals.removeConst(); |
| 7595 | QualType FieldType = |
| 7596 | S.Context.getQualifiedType(Field->getType(), FieldQuals); |
| 7597 | |
| 7598 | if (Results.add(getDerived().visitSubobject( |
| 7599 | FieldType, getDerived().getField(Field)))) |
| 7600 | return true; |
| 7601 | } |
| 7602 | |
| 7603 | |
| 7604 | return false; |
| 7605 | } |
| 7606 | |
| 7607 | Result visitSubobject(QualType Type, Subobject Subobj) { |
| 7608 | |
| 7609 | const ArrayType *AT = S.Context.getAsArrayType(Type); |
| 7610 | if (auto *CAT = dyn_cast_or_null<ConstantArrayType>(AT)) |
| 7611 | return getDerived().visitSubobjectArray(CAT->getElementType(), |
| 7612 | CAT->getSize(), Subobj); |
| 7613 | return getDerived().visitExpandedSubobject(Type, Subobj); |
| 7614 | } |
| 7615 | |
| 7616 | Result visitSubobjectArray(QualType Type, const llvm::APInt &Size, |
| 7617 | Subobject Subobj) { |
| 7618 | return getDerived().visitSubobject(Type, Subobj); |
| 7619 | } |
| 7620 | |
| 7621 | protected: |
| 7622 | Sema &S; |
| 7623 | CXXRecordDecl *RD; |
| 7624 | FunctionDecl *FD; |
| 7625 | DefaultedComparisonKind DCK; |
| 7626 | UnresolvedSet<16> Fns; |
| 7627 | }; |
| 7628 | |
| 7629 | |
| 7630 | |
| 7631 | struct DefaultedComparisonInfo { |
| 7632 | bool Deleted = false; |
| 7633 | bool Constexpr = true; |
| 7634 | ComparisonCategoryType Category = ComparisonCategoryType::StrongOrdering; |
| 7635 | |
| 7636 | static DefaultedComparisonInfo deleted() { |
| 7637 | DefaultedComparisonInfo Deleted; |
| 7638 | Deleted.Deleted = true; |
| 7639 | return Deleted; |
| 7640 | } |
| 7641 | |
| 7642 | bool add(const DefaultedComparisonInfo &R) { |
| 7643 | Deleted |= R.Deleted; |
| 7644 | Constexpr &= R.Constexpr; |
| 7645 | Category = commonComparisonType(Category, R.Category); |
| 7646 | return Deleted; |
| 7647 | } |
| 7648 | }; |
| 7649 | |
| 7650 | |
| 7651 | |
| 7652 | struct DefaultedComparisonSubobject { |
| 7653 | enum { CompleteObject, Member, Base } Kind; |
| 7654 | NamedDecl *Decl; |
| 7655 | SourceLocation Loc; |
| 7656 | }; |
| 7657 | |
| 7658 | |
| 7659 | |
| 7660 | class DefaultedComparisonAnalyzer |
| 7661 | : public DefaultedComparisonVisitor<DefaultedComparisonAnalyzer, |
| 7662 | DefaultedComparisonInfo, |
| 7663 | DefaultedComparisonInfo, |
| 7664 | DefaultedComparisonSubobject> { |
| 7665 | public: |
| 7666 | enum DiagnosticKind { NoDiagnostics, ExplainDeleted, ExplainConstexpr }; |
| 7667 | |
| 7668 | private: |
| 7669 | DiagnosticKind Diagnose; |
| 7670 | |
| 7671 | public: |
| 7672 | using Base = DefaultedComparisonVisitor; |
| 7673 | using Result = DefaultedComparisonInfo; |
| 7674 | using Subobject = DefaultedComparisonSubobject; |
| 7675 | |
| 7676 | friend Base; |
| 7677 | |
| 7678 | DefaultedComparisonAnalyzer(Sema &S, CXXRecordDecl *RD, FunctionDecl *FD, |
| 7679 | DefaultedComparisonKind DCK, |
| 7680 | DiagnosticKind Diagnose = NoDiagnostics) |
| 7681 | : Base(S, RD, FD, DCK), Diagnose(Diagnose) {} |
| 7682 | |
| 7683 | Result visit() { |
| 7684 | if ((DCK == DefaultedComparisonKind::Equal || |
| 7685 | DCK == DefaultedComparisonKind::ThreeWay) && |
| 7686 | RD->hasVariantMembers()) { |
| 7687 | |
| 7688 | |
| 7689 | |
| 7690 | if (Diagnose == ExplainDeleted) { |
| 7691 | S.Diag(FD->getLocation(), diag::note_defaulted_comparison_union) |
| 7692 | << FD << RD->isUnion() << RD; |
| 7693 | } |
| 7694 | return Result::deleted(); |
| 7695 | } |
| 7696 | |
| 7697 | return Base::visit(); |
| 7698 | } |
| 7699 | |
| 7700 | private: |
| 7701 | Subobject getCompleteObject() { |
| 7702 | return Subobject{Subobject::CompleteObject, RD, FD->getLocation()}; |
| 7703 | } |
| 7704 | |
| 7705 | Subobject getBase(CXXBaseSpecifier *Base) { |
| 7706 | return Subobject{Subobject::Base, Base->getType()->getAsCXXRecordDecl(), |
| 7707 | Base->getBaseTypeLoc()}; |
| 7708 | } |
| 7709 | |
| 7710 | Subobject getField(FieldDecl *Field) { |
| 7711 | return Subobject{Subobject::Member, Field, Field->getLocation()}; |
| 7712 | } |
| 7713 | |
| 7714 | Result visitExpandedSubobject(QualType Type, Subobject Subobj) { |
| 7715 | |
| 7716 | |
| 7717 | |
| 7718 | if (Type->isReferenceType()) { |
| 7719 | if (Diagnose == ExplainDeleted) { |
| 7720 | S.Diag(Subobj.Loc, diag::note_defaulted_comparison_reference_member) |
| 7721 | << FD << RD; |
| 7722 | } |
| 7723 | return Result::deleted(); |
| 7724 | } |
| 7725 | |
| 7726 | |
| 7727 | OpaqueValueExpr Xi(FD->getLocation(), Type, VK_LValue); |
| 7728 | Expr *Args[] = {&Xi, &Xi}; |
| 7729 | |
| 7730 | |
| 7731 | OverloadedOperatorKind OO = FD->getOverloadedOperator(); |
| 7732 | assert(OO != OO_None && "not an overloaded operator!"); |
| 7733 | return visitBinaryOperator(OO, Args, Subobj); |
| 7734 | } |
| 7735 | |
| 7736 | Result |
| 7737 | visitBinaryOperator(OverloadedOperatorKind OO, ArrayRef<Expr *> Args, |
| 7738 | Subobject Subobj, |
| 7739 | OverloadCandidateSet *SpaceshipCandidates = nullptr) { |
| 7740 | |
| 7741 | |
| 7742 | |
| 7743 | OverloadCandidateSet CandidateSet( |
| 7744 | FD->getLocation(), OverloadCandidateSet::CSK_Operator, |
| 7745 | OverloadCandidateSet::OperatorRewriteInfo( |
| 7746 | OO, !SpaceshipCandidates)); |
| 7747 | |
| 7748 | |
| 7749 | |
| 7750 | |
| 7751 | CandidateSet.exclude(FD); |
| 7752 | |
| 7753 | if (Args[0]->getType()->isOverloadableType()) |
| 7754 | S.LookupOverloadedBinOp(CandidateSet, OO, Fns, Args); |
| 7755 | else |
| 7756 | |
| 7757 | |
| 7758 | |
| 7759 | S.AddBuiltinOperatorCandidates(OO, FD->getLocation(), Args, CandidateSet); |
| 7760 | |
| 7761 | Result R; |
| 7762 | |
| 7763 | OverloadCandidateSet::iterator Best; |
| 7764 | switch (CandidateSet.BestViableFunction(S, FD->getLocation(), Best)) { |
| 7765 | case OR_Success: { |
| 7766 | |
| 7767 | |
| 7768 | |
| 7769 | |
| 7770 | if ((DCK == DefaultedComparisonKind::NotEqual || |
| 7771 | DCK == DefaultedComparisonKind::Relational) && |
| 7772 | !Best->RewriteKind) { |
| 7773 | if (Diagnose == ExplainDeleted) { |
| 7774 | S.Diag(Best->Function->getLocation(), |
| 7775 | diag::note_defaulted_comparison_not_rewritten_callee) |
| 7776 | << FD; |
| 7777 | } |
| 7778 | return Result::deleted(); |
| 7779 | } |
| 7780 | |
| 7781 | |
| 7782 | |
| 7783 | |
| 7784 | |
| 7785 | |
| 7786 | |
| 7787 | |
| 7788 | CXXRecordDecl *ArgClass = Args[0]->getType()->getAsCXXRecordDecl(); |
| 7789 | if (ArgClass && Best->FoundDecl.getDecl() && |
| 7790 | Best->FoundDecl.getDecl()->isCXXClassMember()) { |
| 7791 | QualType ObjectType = Subobj.Kind == Subobject::Member |
| 7792 | ? Args[0]->getType() |
| 7793 | : S.Context.getRecordType(RD); |
| 7794 | if (!S.isMemberAccessibleForDeletion( |
| 7795 | ArgClass, Best->FoundDecl, ObjectType, Subobj.Loc, |
| 7796 | Diagnose == ExplainDeleted |
| 7797 | ? S.PDiag(diag::note_defaulted_comparison_inaccessible) |
| 7798 | << FD << Subobj.Kind << Subobj.Decl |
| 7799 | : S.PDiag())) |
| 7800 | return Result::deleted(); |
| 7801 | } |
| 7802 | |
| 7803 | bool NeedsDeducing = |
| 7804 | OO == OO_Spaceship && FD->getReturnType()->isUndeducedAutoType(); |
| 7805 | |
| 7806 | if (FunctionDecl *BestFD = Best->Function) { |
| 7807 | |
| 7808 | |
| 7809 | |
| 7810 | |
| 7811 | assert(!BestFD->isDeleted() && "wrong overload resolution result"); |
| 7812 | |
| 7813 | if (Diagnose == ExplainConstexpr && !BestFD->isConstexpr()) { |
| 7814 | if (Subobj.Kind != Subobject::CompleteObject) |
| 7815 | S.Diag(Subobj.Loc, diag::note_defaulted_comparison_not_constexpr) |
| 7816 | << Subobj.Kind << Subobj.Decl; |
| 7817 | S.Diag(BestFD->getLocation(), |
| 7818 | diag::note_defaulted_comparison_not_constexpr_here); |
| 7819 | |
| 7820 | return Result::deleted(); |
| 7821 | } |
| 7822 | R.Constexpr &= BestFD->isConstexpr(); |
| 7823 | |
| 7824 | if (NeedsDeducing) { |
| 7825 | |
| 7826 | |
| 7827 | |
| 7828 | |
| 7829 | if (BestFD->getReturnType()->isUndeducedType() && |
| 7830 | S.DeduceReturnType(BestFD, FD->getLocation(), |
| 7831 | false)) { |
| 7832 | |
| 7833 | |
| 7834 | |
| 7835 | if (Diagnose == NoDiagnostics) { |
| 7836 | S.Diag( |
| 7837 | FD->getLocation(), |
| 7838 | diag::err_defaulted_comparison_cannot_deduce_undeduced_auto) |
| 7839 | << Subobj.Kind << Subobj.Decl; |
| 7840 | S.Diag( |
| 7841 | Subobj.Loc, |
| 7842 | diag::note_defaulted_comparison_cannot_deduce_undeduced_auto) |
| 7843 | << Subobj.Kind << Subobj.Decl; |
| 7844 | S.Diag(BestFD->getLocation(), |
| 7845 | diag::note_defaulted_comparison_cannot_deduce_callee) |
| 7846 | << Subobj.Kind << Subobj.Decl; |
| 7847 | } |
| 7848 | return Result::deleted(); |
| 7849 | } |
| 7850 | auto *Info = S.Context.CompCategories.lookupInfoForType( |
| 7851 | BestFD->getCallResultType()); |
| 7852 | if (!Info) { |
| 7853 | if (Diagnose == ExplainDeleted) { |
| 7854 | S.Diag(Subobj.Loc, diag::note_defaulted_comparison_cannot_deduce) |
| 7855 | << Subobj.Kind << Subobj.Decl |
| 7856 | << BestFD->getCallResultType().withoutLocalFastQualifiers(); |
| 7857 | S.Diag(BestFD->getLocation(), |
| 7858 | diag::note_defaulted_comparison_cannot_deduce_callee) |
| 7859 | << Subobj.Kind << Subobj.Decl; |
| 7860 | } |
| 7861 | return Result::deleted(); |
| 7862 | } |
| 7863 | R.Category = Info->Kind; |
| 7864 | } |
| 7865 | } else { |
| 7866 | QualType T = Best->BuiltinParamTypes[0]; |
| 7867 | assert(T == Best->BuiltinParamTypes[1] && |
| 7868 | "builtin comparison for different types?"); |
| 7869 | assert(Best->BuiltinParamTypes[2].isNull() && |
| 7870 | "invalid builtin comparison"); |
| 7871 | |
| 7872 | if (NeedsDeducing) { |
| 7873 | Optional<ComparisonCategoryType> Cat = |
| 7874 | getComparisonCategoryForBuiltinCmp(T); |
| 7875 | assert(Cat && "no category for builtin comparison?"); |
| 7876 | R.Category = *Cat; |
| 7877 | } |
| 7878 | } |
| 7879 | |
| 7880 | |
| 7881 | |
| 7882 | break; |
| 7883 | } |
| 7884 | |
| 7885 | case OR_Ambiguous: |
| 7886 | if (Diagnose == ExplainDeleted) { |
| 7887 | unsigned Kind = 0; |
| 7888 | if (FD->getOverloadedOperator() == OO_Spaceship && OO != OO_Spaceship) |
| 7889 | Kind = OO == OO_EqualEqual ? 1 : 2; |
| 7890 | CandidateSet.NoteCandidates( |
| 7891 | PartialDiagnosticAt( |
| 7892 | Subobj.Loc, S.PDiag(diag::note_defaulted_comparison_ambiguous) |
| 7893 | << FD << Kind << Subobj.Kind << Subobj.Decl), |
| 7894 | S, OCD_AmbiguousCandidates, Args); |
| 7895 | } |
| 7896 | R = Result::deleted(); |
| 7897 | break; |
| 7898 | |
| 7899 | case OR_Deleted: |
| 7900 | if (Diagnose == ExplainDeleted) { |
| 7901 | if ((DCK == DefaultedComparisonKind::NotEqual || |
| 7902 | DCK == DefaultedComparisonKind::Relational) && |
| 7903 | !Best->RewriteKind) { |
| 7904 | S.Diag(Best->Function->getLocation(), |
| 7905 | diag::note_defaulted_comparison_not_rewritten_callee) |
| 7906 | << FD; |
| 7907 | } else { |
| 7908 | S.Diag(Subobj.Loc, |
| 7909 | diag::note_defaulted_comparison_calls_deleted) |
| 7910 | << FD << Subobj.Kind << Subobj.Decl; |
| 7911 | S.NoteDeletedFunction(Best->Function); |
| 7912 | } |
| 7913 | } |
| 7914 | R = Result::deleted(); |
| 7915 | break; |
| 7916 | |
| 7917 | case OR_No_Viable_Function: |
| 7918 | |
| 7919 | |
| 7920 | if (OO == OO_Spaceship && |
| 7921 | S.Context.CompCategories.lookupInfoForType(FD->getReturnType())) { |
| 7922 | |
| 7923 | |
| 7924 | if (!R.add(visitBinaryOperator(OO_EqualEqual, Args, Subobj, |
| 7925 | &CandidateSet))) |
| 7926 | R.add(visitBinaryOperator(OO_Less, Args, Subobj, &CandidateSet)); |
| 7927 | break; |
| 7928 | } |
| 7929 | |
| 7930 | if (Diagnose == ExplainDeleted) { |
| 7931 | S.Diag(Subobj.Loc, diag::note_defaulted_comparison_no_viable_function) |
| 7932 | << FD << Subobj.Kind << Subobj.Decl; |
| 7933 | |
| 7934 | |
| 7935 | |
| 7936 | if (SpaceshipCandidates) { |
| 7937 | SpaceshipCandidates->NoteCandidates( |
| 7938 | S, Args, |
| 7939 | SpaceshipCandidates->CompleteCandidates(S, OCD_AllCandidates, |
| 7940 | Args, FD->getLocation())); |
| 7941 | S.Diag(Subobj.Loc, |
| 7942 | diag::note_defaulted_comparison_no_viable_function_synthesized) |
| 7943 | << (OO == OO_EqualEqual ? 0 : 1); |
| 7944 | } |
| 7945 | |
| 7946 | CandidateSet.NoteCandidates( |
| 7947 | S, Args, |
| 7948 | CandidateSet.CompleteCandidates(S, OCD_AllCandidates, Args, |
| 7949 | FD->getLocation())); |
| 7950 | } |
| 7951 | R = Result::deleted(); |
| 7952 | break; |
| 7953 | } |
| 7954 | |
| 7955 | return R; |
| 7956 | } |
| 7957 | }; |
| 7958 | |
| 7959 | |
| 7960 | struct StmtListResult { |
| 7961 | bool IsInvalid = false; |
| 7962 | llvm::SmallVector<Stmt*, 16> Stmts; |
| 7963 | |
| 7964 | bool add(const StmtResult &S) { |
| 7965 | IsInvalid |= S.isInvalid(); |
| 7966 | if (IsInvalid) |
| 7967 | return true; |
| 7968 | Stmts.push_back(S.get()); |
| 7969 | return false; |
| 7970 | } |
| 7971 | }; |
| 7972 | |
| 7973 | |
| 7974 | |
| 7975 | class DefaultedComparisonSynthesizer |
| 7976 | : public DefaultedComparisonVisitor<DefaultedComparisonSynthesizer, |
| 7977 | StmtListResult, StmtResult, |
| 7978 | std::pair<ExprResult, ExprResult>> { |
| 7979 | SourceLocation Loc; |
| 7980 | unsigned ArrayDepth = 0; |
| 7981 | |
| 7982 | public: |
| 7983 | using Base = DefaultedComparisonVisitor; |
| 7984 | using ExprPair = std::pair<ExprResult, ExprResult>; |
| 7985 | |
| 7986 | friend Base; |
| 7987 | |
| 7988 | DefaultedComparisonSynthesizer(Sema &S, CXXRecordDecl *RD, FunctionDecl *FD, |
| 7989 | DefaultedComparisonKind DCK, |
| 7990 | SourceLocation BodyLoc) |
| 7991 | : Base(S, RD, FD, DCK), Loc(BodyLoc) {} |
| 7992 | |
| 7993 | |
| 7994 | StmtResult build() { |
| 7995 | Sema::CompoundScopeRAII CompoundScope(S); |
| 7996 | |
| 7997 | StmtListResult Stmts = visit(); |
| 7998 | if (Stmts.IsInvalid) |
| 7999 | return StmtError(); |
| 8000 | |
| 8001 | ExprResult RetVal; |
| 8002 | switch (DCK) { |
| 8003 | case DefaultedComparisonKind::None: |
| 8004 | llvm_unreachable("not a defaulted comparison"); |
| 8005 | |
| 8006 | case DefaultedComparisonKind::Equal: { |
| 8007 | |
| 8008 | |
| 8009 | |
| 8010 | |
| 8011 | |
| 8012 | |
| 8013 | |
| 8014 | |
| 8015 | auto OldStmts = std::move(Stmts.Stmts); |
| 8016 | Stmts.Stmts.clear(); |
| 8017 | ExprResult CmpSoFar; |
| 8018 | |
| 8019 | auto FinishCmp = [&] { |
| 8020 | if (Expr *Prior = CmpSoFar.get()) { |
| 8021 | |
| 8022 | if (RetVal.isUnset() && Stmts.Stmts.empty()) |
| 8023 | RetVal = CmpSoFar; |
| 8024 | |
| 8025 | else if (Stmts.add(buildIfNotCondReturnFalse(Prior))) |
| 8026 | return true; |
| 8027 | CmpSoFar = ExprResult(); |
| 8028 | } |
| 8029 | return false; |
| 8030 | }; |
| 8031 | for (Stmt *EAsStmt : llvm::reverse(OldStmts)) { |
| 8032 | Expr *E = dyn_cast<Expr>(EAsStmt); |
| 8033 | if (!E) { |
| 8034 | |
| 8035 | if (FinishCmp() || Stmts.add(EAsStmt)) |
| 8036 | return StmtError(); |
| 8037 | continue; |
| 8038 | } |
| 8039 | |
| 8040 | if (CmpSoFar.isUnset()) { |
| 8041 | CmpSoFar = E; |
| 8042 | continue; |
| 8043 | } |
| 8044 | CmpSoFar = S.CreateBuiltinBinOp(Loc, BO_LAnd, E, CmpSoFar.get()); |
| 8045 | if (CmpSoFar.isInvalid()) |
| 8046 | return StmtError(); |
| 8047 | } |
| 8048 | if (FinishCmp()) |
| 8049 | return StmtError(); |
| 8050 | std::reverse(Stmts.Stmts.begin(), Stmts.Stmts.end()); |
| 8051 | |
| 8052 | if (RetVal.isUnset()) |
| 8053 | RetVal = S.ActOnCXXBoolLiteral(Loc, tok::kw_true); |
| 8054 | break; |
| 8055 | } |
| 8056 | |
| 8057 | case DefaultedComparisonKind::ThreeWay: { |
| 8058 | |
| 8059 | |
| 8060 | QualType StrongOrdering = S.CheckComparisonCategoryType( |
| 8061 | ComparisonCategoryType::StrongOrdering, Loc, |
| 8062 | Sema::ComparisonCategoryUsage::DefaultedOperator); |
| 8063 | if (StrongOrdering.isNull()) |
| 8064 | return StmtError(); |
| 8065 | VarDecl *EqualVD = S.Context.CompCategories.getInfoForType(StrongOrdering) |
| 8066 | .getValueInfo(ComparisonCategoryResult::Equal) |
| 8067 | ->VD; |
| 8068 | RetVal = getDecl(EqualVD); |
| 8069 | if (RetVal.isInvalid()) |
| 8070 | return StmtError(); |
| 8071 | RetVal = buildStaticCastToR(RetVal.get()); |
| 8072 | break; |
| 8073 | } |
| 8074 | |
| 8075 | case DefaultedComparisonKind::NotEqual: |
| 8076 | case DefaultedComparisonKind::Relational: |
| 8077 | RetVal = cast<Expr>(Stmts.Stmts.pop_back_val()); |
| 8078 | break; |
| 8079 | } |
| 8080 | |
| 8081 | |
| 8082 | if (RetVal.isInvalid()) |
| 8083 | return StmtError(); |
| 8084 | StmtResult ReturnStmt = S.BuildReturnStmt(Loc, RetVal.get()); |
| 8085 | if (ReturnStmt.isInvalid()) |
| 8086 | return StmtError(); |
| 8087 | Stmts.Stmts.push_back(ReturnStmt.get()); |
| 8088 | |
| 8089 | return S.ActOnCompoundStmt(Loc, Loc, Stmts.Stmts, false); |
| 8090 | } |
| 8091 | |
| 8092 | private: |
| 8093 | ExprResult getDecl(ValueDecl *VD) { |
| 8094 | return S.BuildDeclarationNameExpr( |
| 8095 | CXXScopeSpec(), DeclarationNameInfo(VD->getDeclName(), Loc), VD); |
| 8096 | } |
| 8097 | |
| 8098 | ExprResult getParam(unsigned I) { |
| 8099 | ParmVarDecl *PD = FD->getParamDecl(I); |
| 8100 | return getDecl(PD); |
| 8101 | } |
| 8102 | |
| 8103 | ExprPair getCompleteObject() { |
| 8104 | unsigned Param = 0; |
| 8105 | ExprResult LHS; |
| 8106 | if (isa<CXXMethodDecl>(FD)) { |
| 8107 | |
| 8108 | LHS = S.ActOnCXXThis(Loc); |
| 8109 | if (!LHS.isInvalid()) |
| 8110 | LHS = S.CreateBuiltinUnaryOp(Loc, UO_Deref, LHS.get()); |
| 8111 | } else { |
| 8112 | LHS = getParam(Param++); |
| 8113 | } |
| 8114 | ExprResult RHS = getParam(Param++); |
| 8115 | assert(Param == FD->getNumParams()); |
| 8116 | return {LHS, RHS}; |
| 8117 | } |
| 8118 | |
| 8119 | ExprPair getBase(CXXBaseSpecifier *Base) { |
| 8120 | ExprPair Obj = getCompleteObject(); |
| 8121 | if (Obj.first.isInvalid() || Obj.second.isInvalid()) |
| 8122 | return {ExprError(), ExprError()}; |
| 8123 | CXXCastPath Path = {Base}; |
| 8124 | return {S.ImpCastExprToType(Obj.first.get(), Base->getType(), |
| 8125 | CK_DerivedToBase, VK_LValue, &Path), |
| 8126 | S.ImpCastExprToType(Obj.second.get(), Base->getType(), |
| 8127 | CK_DerivedToBase, VK_LValue, &Path)}; |
| 8128 | } |
| 8129 | |
| 8130 | ExprPair getField(FieldDecl *Field) { |
| 8131 | ExprPair Obj = getCompleteObject(); |
| 8132 | if (Obj.first.isInvalid() || Obj.second.isInvalid()) |
| 8133 | return {ExprError(), ExprError()}; |
| 8134 | |
| 8135 | DeclAccessPair Found = DeclAccessPair::make(Field, Field->getAccess()); |
| 8136 | DeclarationNameInfo NameInfo(Field->getDeclName(), Loc); |
| 8137 | return {S.BuildFieldReferenceExpr(Obj.first.get(), false, Loc, |
| 8138 | CXXScopeSpec(), Field, Found, NameInfo), |
| 8139 | S.BuildFieldReferenceExpr(Obj.second.get(), false, Loc, |
| 8140 | CXXScopeSpec(), Field, Found, NameInfo)}; |
| 8141 | } |
| 8142 | |
| 8143 | |
| 8144 | |
| 8145 | |
| 8146 | StmtResult buildIfNotCondReturnFalse(ExprResult Cond) { |
| 8147 | if (Cond.isInvalid()) |
| 8148 | return StmtError(); |
| 8149 | |
| 8150 | ExprResult NotCond = S.CreateBuiltinUnaryOp(Loc, UO_LNot, Cond.get()); |
| 8151 | if (NotCond.isInvalid()) |
| 8152 | return StmtError(); |
| 8153 | |
| 8154 | ExprResult False = S.ActOnCXXBoolLiteral(Loc, tok::kw_false); |
| 8155 | assert(!False.isInvalid() && "should never fail"); |
| 8156 | StmtResult ReturnFalse = S.BuildReturnStmt(Loc, False.get()); |
| 8157 | if (ReturnFalse.isInvalid()) |
| 8158 | return StmtError(); |
| 8159 | |
| 8160 | return S.ActOnIfStmt(Loc, false, Loc, nullptr, |
| 8161 | S.ActOnCondition(nullptr, Loc, NotCond.get(), |
| 8162 | Sema::ConditionKind::Boolean), |
| 8163 | Loc, ReturnFalse.get(), SourceLocation(), nullptr); |
| 8164 | } |
| 8165 | |
| 8166 | StmtResult visitSubobjectArray(QualType Type, llvm::APInt Size, |
| 8167 | ExprPair Subobj) { |
| 8168 | QualType SizeType = S.Context.getSizeType(); |
| 8169 | Size = Size.zextOrTrunc(S.Context.getTypeSize(SizeType)); |
| 8170 | |
| 8171 | |
| 8172 | IdentifierInfo *IterationVarName = nullptr; |
| 8173 | { |
| 8174 | SmallString<8> Str; |
| 8175 | llvm::raw_svector_ostream OS(Str); |
| 8176 | OS << "i" << ArrayDepth; |
| 8177 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 8178 | } |
| 8179 | VarDecl *IterationVar = VarDecl::Create( |
| 8180 | S.Context, S.CurContext, Loc, Loc, IterationVarName, SizeType, |
| 8181 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), SC_None); |
| 8182 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
| 8183 | IterationVar->setInit( |
| 8184 | IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
| 8185 | Stmt *Init = new (S.Context) DeclStmt(DeclGroupRef(IterationVar), Loc, Loc); |
| 8186 | |
| 8187 | auto IterRef = [&] { |
| 8188 | ExprResult Ref = S.BuildDeclarationNameExpr( |
| 8189 | CXXScopeSpec(), DeclarationNameInfo(IterationVarName, Loc), |
| 8190 | IterationVar); |
| 8191 | assert(!Ref.isInvalid() && "can't reference our own variable?"); |
| 8192 | return Ref.get(); |
| 8193 | }; |
| 8194 | |
| 8195 | |
| 8196 | ExprResult Cond = S.CreateBuiltinBinOp( |
| 8197 | Loc, BO_NE, IterRef(), |
| 8198 | IntegerLiteral::Create(S.Context, Size, SizeType, Loc)); |
| 8199 | assert(!Cond.isInvalid() && "should never fail"); |
| 8200 | |
| 8201 | |
| 8202 | ExprResult Inc = S.CreateBuiltinUnaryOp(Loc, UO_PreInc, IterRef()); |
| 8203 | assert(!Inc.isInvalid() && "should never fail"); |
| 8204 | |
| 8205 | |
| 8206 | auto Index = [&](ExprResult E) { |
| 8207 | if (E.isInvalid()) |
| 8208 | return ExprError(); |
| 8209 | return S.CreateBuiltinArraySubscriptExpr(E.get(), Loc, IterRef(), Loc); |
| 8210 | }; |
| 8211 | Subobj.first = Index(Subobj.first); |
| 8212 | Subobj.second = Index(Subobj.second); |
| 8213 | |
| 8214 | |
| 8215 | ++ArrayDepth; |
| 8216 | StmtResult Substmt = visitSubobject(Type, Subobj); |
| 8217 | --ArrayDepth; |
| 8218 | |
| 8219 | if (Substmt.isInvalid()) |
| 8220 | return StmtError(); |
| 8221 | |
| 8222 | |
| 8223 | |
| 8224 | |
| 8225 | if (Expr *ElemCmp = dyn_cast<Expr>(Substmt.get())) { |
| 8226 | assert(DCK == DefaultedComparisonKind::Equal && |
| 8227 | "should have non-expression statement"); |
| 8228 | Substmt = buildIfNotCondReturnFalse(ElemCmp); |
| 8229 | if (Substmt.isInvalid()) |
| 8230 | return StmtError(); |
| 8231 | } |
| 8232 | |
| 8233 | |
| 8234 | return S.ActOnForStmt(Loc, Loc, Init, |
| 8235 | S.ActOnCondition(nullptr, Loc, Cond.get(), |
| 8236 | Sema::ConditionKind::Boolean), |
| 8237 | S.MakeFullDiscardedValueExpr(Inc.get()), Loc, |
| 8238 | Substmt.get()); |
| 8239 | } |
| 8240 | |
| 8241 | StmtResult visitExpandedSubobject(QualType Type, ExprPair Obj) { |
| 8242 | if (Obj.first.isInvalid() || Obj.second.isInvalid()) |
| 8243 | return StmtError(); |
| 8244 | |
| 8245 | OverloadedOperatorKind OO = FD->getOverloadedOperator(); |
| 8246 | BinaryOperatorKind Opc = BinaryOperator::getOverloadedOpcode(OO); |
| 8247 | ExprResult Op; |
| 8248 | if (Type->isOverloadableType()) |
| 8249 | Op = S.CreateOverloadedBinOp(Loc, Opc, Fns, Obj.first.get(), |
| 8250 | Obj.second.get(), true, |
| 8251 | true, FD); |
| 8252 | else |
| 8253 | Op = S.CreateBuiltinBinOp(Loc, Opc, Obj.first.get(), Obj.second.get()); |
| 8254 | if (Op.isInvalid()) |
| 8255 | return StmtError(); |
| 8256 | |
| 8257 | switch (DCK) { |
| 8258 | case DefaultedComparisonKind::None: |
| 8259 | llvm_unreachable("not a defaulted comparison"); |
| 8260 | |
| 8261 | case DefaultedComparisonKind::Equal: |
| 8262 | |
| 8263 | |
| 8264 | Op = S.PerformContextuallyConvertToBool(Op.get()); |
| 8265 | if (Op.isInvalid()) |
| 8266 | return StmtError(); |
| 8267 | return Op.get(); |
| 8268 | |
| 8269 | case DefaultedComparisonKind::ThreeWay: { |
| 8270 | |
| 8271 | |
| 8272 | |
| 8273 | QualType R = FD->getReturnType(); |
| 8274 | Op = buildStaticCastToR(Op.get()); |
| 8275 | if (Op.isInvalid()) |
| 8276 | return StmtError(); |
| 8277 | |
| 8278 | |
| 8279 | IdentifierInfo *Name = &S.Context.Idents.get("cmp"); |
| 8280 | VarDecl *VD = |
| 8281 | VarDecl::Create(S.Context, S.CurContext, Loc, Loc, Name, R, |
| 8282 | S.Context.getTrivialTypeSourceInfo(R, Loc), SC_None); |
| 8283 | S.AddInitializerToDecl(VD, Op.get(), false); |
| 8284 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(VD), Loc, Loc); |
| 8285 | |
| 8286 | |
| 8287 | ExprResult VDRef = getDecl(VD); |
| 8288 | if (VDRef.isInvalid()) |
| 8289 | return StmtError(); |
| 8290 | llvm::APInt ZeroVal(S.Context.getIntWidth(S.Context.IntTy), 0); |
| 8291 | Expr *Zero = |
| 8292 | IntegerLiteral::Create(S.Context, ZeroVal, S.Context.IntTy, Loc); |
| 8293 | ExprResult Comp; |
| 8294 | if (VDRef.get()->getType()->isOverloadableType()) |
| 8295 | Comp = S.CreateOverloadedBinOp(Loc, BO_NE, Fns, VDRef.get(), Zero, true, |
| 8296 | true, FD); |
| 8297 | else |
| 8298 | Comp = S.CreateBuiltinBinOp(Loc, BO_NE, VDRef.get(), Zero); |
| 8299 | if (Comp.isInvalid()) |
| 8300 | return StmtError(); |
| 8301 | Sema::ConditionResult Cond = S.ActOnCondition( |
| 8302 | nullptr, Loc, Comp.get(), Sema::ConditionKind::Boolean); |
| 8303 | if (Cond.isInvalid()) |
| 8304 | return StmtError(); |
| 8305 | |
| 8306 | |
| 8307 | VDRef = getDecl(VD); |
| 8308 | if (VDRef.isInvalid()) |
| 8309 | return StmtError(); |
| 8310 | StmtResult ReturnStmt = S.BuildReturnStmt(Loc, VDRef.get()); |
| 8311 | if (ReturnStmt.isInvalid()) |
| 8312 | return StmtError(); |
| 8313 | |
| 8314 | |
| 8315 | return S.ActOnIfStmt(Loc, false, Loc, InitStmt, Cond, Loc, |
| 8316 | ReturnStmt.get(), |
| 8317 | SourceLocation(), nullptr); |
| 8318 | } |
| 8319 | |
| 8320 | case DefaultedComparisonKind::NotEqual: |
| 8321 | case DefaultedComparisonKind::Relational: |
| 8322 | |
| 8323 | |
| 8324 | return Op.get(); |
| 8325 | } |
| 8326 | llvm_unreachable(""); |
| 8327 | } |
| 8328 | |
| 8329 | |
| 8330 | ExprResult buildStaticCastToR(Expr *E) { |
| 8331 | QualType R = FD->getReturnType(); |
| 8332 | assert(!R->isUndeducedType() && "type should have been deduced already"); |
| 8333 | |
| 8334 | |
| 8335 | if (E->isPRValue() && S.Context.hasSameType(E->getType(), R)) |
| 8336 | return E; |
| 8337 | return S.BuildCXXNamedCast(Loc, tok::kw_static_cast, |
| 8338 | S.Context.getTrivialTypeSourceInfo(R, Loc), E, |
| 8339 | SourceRange(Loc, Loc), SourceRange(Loc, Loc)); |
| 8340 | } |
| 8341 | }; |
| 8342 | } |
| 8343 | |
| 8344 | |
| 8345 | |
| 8346 | static void lookupOperatorsForDefaultedComparison(Sema &Self, Scope *S, |
| 8347 | UnresolvedSetImpl &Operators, |
| 8348 | OverloadedOperatorKind Op) { |
| 8349 | auto Lookup = [&](OverloadedOperatorKind OO) { |
| 8350 | Self.LookupOverloadedOperatorName(OO, S, Operators); |
| 8351 | }; |
| 8352 | |
| 8353 | |
| 8354 | Lookup(Op); |
| 8355 | |
| 8356 | if (OverloadedOperatorKind ExtraOp = getRewrittenOverloadedOperator(Op)) |
| 8357 | Lookup(ExtraOp); |
| 8358 | |
| 8359 | |
| 8360 | |
| 8361 | |
| 8362 | |
| 8363 | if (Op == OO_Spaceship) { |
| 8364 | Lookup(OO_ExclaimEqual); |
| 8365 | Lookup(OO_Less); |
| 8366 | Lookup(OO_EqualEqual); |
| 8367 | } |
| 8368 | } |
| 8369 | |
| 8370 | bool Sema::CheckExplicitlyDefaultedComparison(Scope *S, FunctionDecl *FD, |
| 8371 | DefaultedComparisonKind DCK) { |
| 8372 | assert(DCK != DefaultedComparisonKind::None && "not a defaulted comparison"); |
| 8373 | |
| 8374 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalDeclContext()); |
| 8375 | assert(RD && "defaulted comparison is not defaulted in a class"); |
| 8376 | |
| 8377 | |
| 8378 | |
| 8379 | if (S) { |
| 8380 | UnresolvedSet<32> Operators; |
| 8381 | lookupOperatorsForDefaultedComparison(*this, S, Operators, |
| 8382 | FD->getOverloadedOperator()); |
| 8383 | FD->setDefaultedFunctionInfo(FunctionDecl::DefaultedFunctionInfo::Create( |
| 8384 | Context, Operators.pairs())); |
| 8385 | } |
| 8386 | |
| 8387 | |
| 8388 | |
| 8389 | |
| 8390 | |
| 8391 | |
| 8392 | |
| 8393 | |
| 8394 | QualType ExpectedParmType1 = Context.getRecordType(RD); |
| 8395 | QualType ExpectedParmType2 = |
| 8396 | Context.getLValueReferenceType(ExpectedParmType1.withConst()); |
| 8397 | if (isa<CXXMethodDecl>(FD)) |
| 8398 | ExpectedParmType1 = ExpectedParmType2; |
| 8399 | for (const ParmVarDecl *Param : FD->parameters()) { |
| 8400 | if (!Param->getType()->isDependentType() && |
| 8401 | !Context.hasSameType(Param->getType(), ExpectedParmType1) && |
| 8402 | !Context.hasSameType(Param->getType(), ExpectedParmType2)) { |
| 8403 | |
| 8404 | |
| 8405 | if (!FD->isImplicit()) { |
| 8406 | Diag(FD->getLocation(), diag::err_defaulted_comparison_param) |
| 8407 | << (int)DCK << Param->getType() << ExpectedParmType1 |
| 8408 | << !isa<CXXMethodDecl>(FD) |
| 8409 | << ExpectedParmType2 << Param->getSourceRange(); |
| 8410 | } |
| 8411 | return true; |
| 8412 | } |
| 8413 | } |
| 8414 | if (FD->getNumParams() == 2 && |
| 8415 | !Context.hasSameType(FD->getParamDecl(0)->getType(), |
| 8416 | FD->getParamDecl(1)->getType())) { |
| 8417 | if (!FD->isImplicit()) { |
| 8418 | Diag(FD->getLocation(), diag::err_defaulted_comparison_param_mismatch) |
| 8419 | << (int)DCK |
| 8420 | << FD->getParamDecl(0)->getType() |
| 8421 | << FD->getParamDecl(0)->getSourceRange() |
| 8422 | << FD->getParamDecl(1)->getType() |
| 8423 | << FD->getParamDecl(1)->getSourceRange(); |
| 8424 | } |
| 8425 | return true; |
| 8426 | } |
| 8427 | |
| 8428 | |
| 8429 | if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 8430 | assert(!MD->isStatic() && "comparison function cannot be a static member"); |
| 8431 | if (!MD->isConst()) { |
| 8432 | SourceLocation InsertLoc; |
| 8433 | if (FunctionTypeLoc Loc = MD->getFunctionTypeLoc()) |
| 8434 | InsertLoc = getLocForEndOfToken(Loc.getRParenLoc()); |
| 8435 | |
| 8436 | |
| 8437 | if (!MD->isImplicit()) { |
| 8438 | Diag(MD->getLocation(), diag::err_defaulted_comparison_non_const) |
| 8439 | << (int)DCK << FixItHint::CreateInsertion(InsertLoc, " const"); |
| 8440 | } |
| 8441 | |
| 8442 | |
| 8443 | const auto *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 8444 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8445 | EPI.TypeQuals.addConst(); |
| 8446 | MD->setType(Context.getFunctionType(FPT->getReturnType(), |
| 8447 | FPT->getParamTypes(), EPI)); |
| 8448 | } |
| 8449 | } else { |
| 8450 | |
| 8451 | assert(FD->getFriendObjectKind() && "expected a friend declaration"); |
| 8452 | } |
| 8453 | |
| 8454 | |
| 8455 | |
| 8456 | |
| 8457 | if (DCK != DefaultedComparisonKind::ThreeWay && |
| 8458 | !FD->getDeclaredReturnType()->isDependentType() && |
| 8459 | !Context.hasSameType(FD->getDeclaredReturnType(), Context.BoolTy)) { |
| 8460 | Diag(FD->getLocation(), diag::err_defaulted_comparison_return_type_not_bool) |
| 8461 | << (int)DCK << FD->getDeclaredReturnType() << Context.BoolTy |
| 8462 | << FD->getReturnTypeSourceRange(); |
| 8463 | return true; |
| 8464 | } |
| 8465 | |
| 8466 | |
| 8467 | |
| 8468 | if (DCK == DefaultedComparisonKind::ThreeWay && |
| 8469 | FD->getDeclaredReturnType()->getContainedDeducedType() && |
| 8470 | !Context.hasSameType(FD->getDeclaredReturnType(), |
| 8471 | Context.getAutoDeductType())) { |
| 8472 | Diag(FD->getLocation(), |
| 8473 | diag::err_defaulted_comparison_deduced_return_type_not_auto) |
| 8474 | << (int)DCK << FD->getDeclaredReturnType() << Context.AutoDeductTy |
| 8475 | << FD->getReturnTypeSourceRange(); |
| 8476 | return true; |
| 8477 | } |
| 8478 | |
| 8479 | |
| 8480 | |
| 8481 | if (RD->isDependentType()) |
| 8482 | return false; |
| 8483 | |
| 8484 | |
| 8485 | DefaultedComparisonInfo Info = |
| 8486 | DefaultedComparisonAnalyzer(*this, RD, FD, DCK).visit(); |
| 8487 | |
| 8488 | bool First = FD == FD->getCanonicalDecl(); |
| 8489 | |
| 8490 | |
| 8491 | |
| 8492 | if (Info.Deleted) { |
| 8493 | if (!First) { |
| 8494 | |
| 8495 | |
| 8496 | |
| 8497 | |
| 8498 | |
| 8499 | |
| 8500 | Diag(FD->getLocation(), diag::err_non_first_default_compare_deletes) |
| 8501 | << FD->isImplicit() << (int)DCK; |
| 8502 | DefaultedComparisonAnalyzer(*this, RD, FD, DCK, |
| 8503 | DefaultedComparisonAnalyzer::ExplainDeleted) |
| 8504 | .visit(); |
| 8505 | return true; |
| 8506 | } |
| 8507 | |
| 8508 | SetDeclDeleted(FD, FD->getLocation()); |
| 8509 | if (!inTemplateInstantiation() && !FD->isImplicit()) { |
| 8510 | Diag(FD->getLocation(), diag::warn_defaulted_comparison_deleted) |
| 8511 | << (int)DCK; |
| 8512 | DefaultedComparisonAnalyzer(*this, RD, FD, DCK, |
| 8513 | DefaultedComparisonAnalyzer::ExplainDeleted) |
| 8514 | .visit(); |
| 8515 | } |
| 8516 | return false; |
| 8517 | } |
| 8518 | |
| 8519 | |
| 8520 | |
| 8521 | if (DCK == DefaultedComparisonKind::ThreeWay && |
| 8522 | FD->getDeclaredReturnType()->isUndeducedAutoType()) { |
| 8523 | SourceLocation RetLoc = FD->getReturnTypeSourceRange().getBegin(); |
| 8524 | if (RetLoc.isInvalid()) |
| 8525 | RetLoc = FD->getBeginLoc(); |
| 8526 | |
| 8527 | |
| 8528 | QualType Cat = CheckComparisonCategoryType( |
| 8529 | Info.Category, RetLoc, ComparisonCategoryUsage::DefaultedOperator); |
| 8530 | if (Cat.isNull()) |
| 8531 | return true; |
| 8532 | Context.adjustDeducedFunctionResultType( |
| 8533 | FD, SubstAutoType(FD->getDeclaredReturnType(), Cat)); |
| 8534 | } |
| 8535 | |
| 8536 | |
| 8537 | |
| 8538 | |
| 8539 | |
| 8540 | |
| 8541 | |
| 8542 | |
| 8543 | |
| 8544 | if (FD->isConstexpr()) { |
| 8545 | if (CheckConstexprReturnType(*this, FD, CheckConstexprKind::Diagnose) && |
| 8546 | CheckConstexprParameterTypes(*this, FD, CheckConstexprKind::Diagnose) && |
| 8547 | !Info.Constexpr) { |
| 8548 | Diag(FD->getBeginLoc(), |
| 8549 | diag::err_incorrect_defaulted_comparison_constexpr) |
| 8550 | << FD->isImplicit() << (int)DCK << FD->isConsteval(); |
| 8551 | DefaultedComparisonAnalyzer(*this, RD, FD, DCK, |
| 8552 | DefaultedComparisonAnalyzer::ExplainConstexpr) |
| 8553 | .visit(); |
| 8554 | } |
| 8555 | } |
| 8556 | |
| 8557 | |
| 8558 | |
| 8559 | |
| 8560 | |
| 8561 | |
| 8562 | if (First && !FD->isConstexpr() && Info.Constexpr) |
| 8563 | FD->setConstexprKind(ConstexprSpecKind::Constexpr); |
| 8564 | |
| 8565 | |
| 8566 | |
| 8567 | |
| 8568 | |
| 8569 | if (FD->getExceptionSpecType() == EST_None) { |
| 8570 | auto *FPT = FD->getType()->castAs<FunctionProtoType>(); |
| 8571 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8572 | EPI.ExceptionSpec.Type = EST_Unevaluated; |
| 8573 | EPI.ExceptionSpec.SourceDecl = FD; |
| 8574 | FD->setType(Context.getFunctionType(FPT->getReturnType(), |
| 8575 | FPT->getParamTypes(), EPI)); |
| 8576 | } |
| 8577 | |
| 8578 | return false; |
| 8579 | } |
| 8580 | |
| 8581 | void Sema::DeclareImplicitEqualityComparison(CXXRecordDecl *RD, |
| 8582 | FunctionDecl *Spaceship) { |
| 8583 | Sema::CodeSynthesisContext Ctx; |
| 8584 | Ctx.Kind = Sema::CodeSynthesisContext::DeclaringImplicitEqualityComparison; |
| 8585 | Ctx.PointOfInstantiation = Spaceship->getEndLoc(); |
| 8586 | Ctx.Entity = Spaceship; |
| 8587 | pushCodeSynthesisContext(Ctx); |
| 8588 | |
| 8589 | if (FunctionDecl *EqualEqual = SubstSpaceshipAsEqualEqual(RD, Spaceship)) |
| 8590 | EqualEqual->setImplicit(); |
| 8591 | |
| 8592 | popCodeSynthesisContext(); |
| 8593 | } |
| 8594 | |
| 8595 | void Sema::DefineDefaultedComparison(SourceLocation UseLoc, FunctionDecl *FD, |
| 8596 | DefaultedComparisonKind DCK) { |
| 8597 | assert(FD->isDefaulted() && !FD->isDeleted() && |
| 8598 | !FD->doesThisDeclarationHaveABody()); |
| 8599 | if (FD->willHaveBody() || FD->isInvalidDecl()) |
| 8600 | return; |
| 8601 | |
| 8602 | SynthesizedFunctionScope Scope(*this, FD); |
| 8603 | |
| 8604 | |
| 8605 | Scope.addContextNote(UseLoc); |
| 8606 | |
| 8607 | { |
| 8608 | |
| 8609 | CXXRecordDecl *RD = cast<CXXRecordDecl>(FD->getLexicalParent()); |
| 8610 | SourceLocation BodyLoc = |
| 8611 | FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation(); |
| 8612 | StmtResult Body = |
| 8613 | DefaultedComparisonSynthesizer(*this, RD, FD, DCK, BodyLoc).build(); |
| 8614 | if (Body.isInvalid()) { |
| 8615 | FD->setInvalidDecl(); |
| 8616 | return; |
| 8617 | } |
| 8618 | FD->setBody(Body.get()); |
| 8619 | FD->markUsed(Context); |
| 8620 | } |
| 8621 | |
| 8622 | |
| 8623 | |
| 8624 | ResolveExceptionSpec(UseLoc, FD->getType()->castAs<FunctionProtoType>()); |
| 8625 | |
| 8626 | if (ASTMutationListener *L = getASTMutationListener()) |
| 8627 | L->CompletedImplicitDefinition(FD); |
| 8628 | } |
| 8629 | |
| 8630 | static Sema::ImplicitExceptionSpecification |
| 8631 | ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc, |
| 8632 | FunctionDecl *FD, |
| 8633 | Sema::DefaultedComparisonKind DCK) { |
| 8634 | ComputingExceptionSpec CES(S, FD, Loc); |
| 8635 | Sema::ImplicitExceptionSpecification ExceptSpec(S); |
| 8636 | |
| 8637 | if (FD->isInvalidDecl()) |
| 8638 | return ExceptSpec; |
| 8639 | |
| 8640 | |
| 8641 | |
| 8642 | if (FD->hasBody()) { |
| 8643 | ExceptSpec.CalledStmt(FD->getBody()); |
| 8644 | } else { |
| 8645 | |
| 8646 | |
| 8647 | |
| 8648 | |
| 8649 | |
| 8650 | |
| 8651 | |
| 8652 | |
| 8653 | Sema::SynthesizedFunctionScope Scope(S, FD); |
| 8654 | EnterExpressionEvaluationContext Context( |
| 8655 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
| 8656 | |
| 8657 | CXXRecordDecl *RD = cast<CXXRecordDecl>(FD->getLexicalParent()); |
| 8658 | SourceLocation BodyLoc = |
| 8659 | FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation(); |
| 8660 | StmtResult Body = |
| 8661 | DefaultedComparisonSynthesizer(S, RD, FD, DCK, BodyLoc).build(); |
| 8662 | if (!Body.isInvalid()) |
| 8663 | ExceptSpec.CalledStmt(Body.get()); |
| 8664 | |
| 8665 | |
| 8666 | |
| 8667 | |
| 8668 | |
| 8669 | } |
| 8670 | |
| 8671 | return ExceptSpec; |
| 8672 | } |
| 8673 | |
| 8674 | void Sema::CheckDelayedMemberExceptionSpecs() { |
| 8675 | decltype(DelayedOverridingExceptionSpecChecks) Overriding; |
| 8676 | decltype(DelayedEquivalentExceptionSpecChecks) Equivalent; |
| 8677 | |
| 8678 | std::swap(Overriding, DelayedOverridingExceptionSpecChecks); |
| 8679 | std::swap(Equivalent, DelayedEquivalentExceptionSpecChecks); |
| 8680 | |
| 8681 | |
| 8682 | |
| 8683 | for (auto &Check : Overriding) |
| 8684 | CheckOverridingFunctionExceptionSpec(Check.first, Check.second); |
| 8685 | |
| 8686 | |
| 8687 | |
| 8688 | for (auto &Check : Equivalent) |
| 8689 | CheckEquivalentExceptionSpec(Check.second, Check.first); |
| 8690 | } |
| 8691 | |
| 8692 | namespace { |
| 8693 | |
| 8694 | |
| 8695 | template<typename Derived> |
| 8696 | struct SpecialMemberVisitor { |
| 8697 | Sema &S; |
| 8698 | CXXMethodDecl *MD; |
| 8699 | Sema::CXXSpecialMember CSM; |
| 8700 | Sema::InheritedConstructorInfo *ICI; |
| 8701 | |
| 8702 | |
| 8703 | bool IsConstructor = false, IsAssignment = false, ConstArg = false; |
| 8704 | |
| 8705 | SpecialMemberVisitor(Sema &S, CXXMethodDecl *MD, Sema::CXXSpecialMember CSM, |
| 8706 | Sema::InheritedConstructorInfo *ICI) |
| 8707 | : S(S), MD(MD), CSM(CSM), ICI(ICI) { |
| 8708 | switch (CSM) { |
| 8709 | case Sema::CXXDefaultConstructor: |
| 8710 | case Sema::CXXCopyConstructor: |
| 8711 | case Sema::CXXMoveConstructor: |
| 8712 | IsConstructor = true; |
| 8713 | break; |
| 8714 | case Sema::CXXCopyAssignment: |
| 8715 | case Sema::CXXMoveAssignment: |
| 8716 | IsAssignment = true; |
| 8717 | break; |
| 8718 | case Sema::CXXDestructor: |
| 8719 | break; |
| 8720 | case Sema::CXXInvalid: |
| 8721 | llvm_unreachable("invalid special member kind"); |
| 8722 | } |
| 8723 | |
| 8724 | if (MD->getNumParams()) { |
| 8725 | if (const ReferenceType *RT = |
| 8726 | MD->getParamDecl(0)->getType()->getAs<ReferenceType>()) |
| 8727 | ConstArg = RT->getPointeeType().isConstQualified(); |
| 8728 | } |
| 8729 | } |
| 8730 | |
| 8731 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
| 8732 | |
| 8733 | |
| 8734 | bool isMove() const { |
| 8735 | return CSM == Sema::CXXMoveConstructor || CSM == Sema::CXXMoveAssignment; |
| 8736 | } |
| 8737 | |
| 8738 | |
| 8739 | Sema::SpecialMemberOverloadResult lookupIn(CXXRecordDecl *Class, |
| 8740 | unsigned Quals, bool IsMutable) { |
| 8741 | return lookupCallFromSpecialMember(S, Class, CSM, Quals, |
| 8742 | ConstArg && !IsMutable); |
| 8743 | } |
| 8744 | |
| 8745 | |
| 8746 | |
| 8747 | Sema::SpecialMemberOverloadResult lookupInheritedCtor(CXXRecordDecl *Class) { |
| 8748 | if (!ICI) |
| 8749 | return {}; |
| 8750 | assert(CSM == Sema::CXXDefaultConstructor); |
| 8751 | auto *BaseCtor = |
| 8752 | cast<CXXConstructorDecl>(MD)->getInheritedConstructor().getConstructor(); |
| 8753 | if (auto *MD = ICI->findConstructorForBase(Class, BaseCtor).first) |
| 8754 | return MD; |
| 8755 | return {}; |
| 8756 | } |
| 8757 | |
| 8758 | |
| 8759 | typedef llvm::PointerUnion<CXXBaseSpecifier*, FieldDecl*> Subobject; |
| 8760 | |
| 8761 | |
| 8762 | static SourceLocation getSubobjectLoc(Subobject Subobj) { |
| 8763 | |
| 8764 | |
| 8765 | if (auto *B = Subobj.dyn_cast<CXXBaseSpecifier*>()) |
| 8766 | return B->getBaseTypeLoc(); |
| 8767 | else |
| 8768 | return Subobj.get<FieldDecl*>()->getLocation(); |
| 8769 | } |
| 8770 | |
| 8771 | enum BasesToVisit { |
| 8772 | |
| 8773 | VisitNonVirtualBases, |
| 8774 | |
| 8775 | VisitDirectBases, |
| 8776 | |
| 8777 | |
| 8778 | VisitPotentiallyConstructedBases, |
| 8779 | |
| 8780 | VisitAllBases |
| 8781 | }; |
| 8782 | |
| 8783 | |
| 8784 | bool visit(BasesToVisit Bases) { |
| 8785 | CXXRecordDecl *RD = MD->getParent(); |
| 8786 | |
| 8787 | if (Bases == VisitPotentiallyConstructedBases) |
| 8788 | Bases = RD->isAbstract() ? VisitNonVirtualBases : VisitAllBases; |
| 8789 | |
| 8790 | for (auto &B : RD->bases()) |
| 8791 | if ((Bases == VisitDirectBases || !B.isVirtual()) && |
| 8792 | getDerived().visitBase(&B)) |
| 8793 | return true; |
| 8794 | |
| 8795 | if (Bases == VisitAllBases) |
| 8796 | for (auto &B : RD->vbases()) |
| 8797 | if (getDerived().visitBase(&B)) |
| 8798 | return true; |
| 8799 | |
| 8800 | for (auto *F : RD->fields()) |
| 8801 | if (!F->isInvalidDecl() && !F->isUnnamedBitfield() && |
| 8802 | getDerived().visitField(F)) |
| 8803 | return true; |
| 8804 | |
| 8805 | return false; |
| 8806 | } |
| 8807 | }; |
| 8808 | } |
| 8809 | |
| 8810 | namespace { |
| 8811 | struct SpecialMemberDeletionInfo |
| 8812 | : SpecialMemberVisitor<SpecialMemberDeletionInfo> { |
| 8813 | bool Diagnose; |
| 8814 | |
| 8815 | SourceLocation Loc; |
| 8816 | |
| 8817 | bool AllFieldsAreConst; |
| 8818 | |
| 8819 | SpecialMemberDeletionInfo(Sema &S, CXXMethodDecl *MD, |
| 8820 | Sema::CXXSpecialMember CSM, |
| 8821 | Sema::InheritedConstructorInfo *ICI, bool Diagnose) |
| 8822 | : SpecialMemberVisitor(S, MD, CSM, ICI), Diagnose(Diagnose), |
| 8823 | Loc(MD->getLocation()), AllFieldsAreConst(true) {} |
| 8824 | |
| 8825 | bool inUnion() const { return MD->getParent()->isUnion(); } |
| 8826 | |
| 8827 | Sema::CXXSpecialMember getEffectiveCSM() { |
| 8828 | return ICI ? Sema::CXXInvalid : CSM; |
| 8829 | } |
| 8830 | |
| 8831 | bool shouldDeleteForVariantObjCPtrMember(FieldDecl *FD, QualType FieldType); |
| 8832 | |
| 8833 | bool visitBase(CXXBaseSpecifier *Base) { return shouldDeleteForBase(Base); } |
| 8834 | bool visitField(FieldDecl *Field) { return shouldDeleteForField(Field); } |
| 8835 | |
| 8836 | bool shouldDeleteForBase(CXXBaseSpecifier *Base); |
| 8837 | bool shouldDeleteForField(FieldDecl *FD); |
| 8838 | bool shouldDeleteForAllConstMembers(); |
| 8839 | |
| 8840 | bool shouldDeleteForClassSubobject(CXXRecordDecl *Class, Subobject Subobj, |
| 8841 | unsigned Quals); |
| 8842 | bool shouldDeleteForSubobjectCall(Subobject Subobj, |
| 8843 | Sema::SpecialMemberOverloadResult SMOR, |
| 8844 | bool IsDtorCallInCtor); |
| 8845 | |
| 8846 | bool isAccessible(Subobject Subobj, CXXMethodDecl *D); |
| 8847 | }; |
| 8848 | } |
| 8849 | |
| 8850 | |
| 8851 | |
| 8852 | bool SpecialMemberDeletionInfo::isAccessible(Subobject Subobj, |
| 8853 | CXXMethodDecl *target) { |
| 8854 | |
| 8855 | |
| 8856 | QualType objectTy; |
| 8857 | AccessSpecifier access = target->getAccess(); |
| 8858 | if (CXXBaseSpecifier *base = Subobj.dyn_cast<CXXBaseSpecifier*>()) { |
| 8859 | objectTy = S.Context.getTypeDeclType(MD->getParent()); |
| 8860 | access = CXXRecordDecl::MergeAccess(base->getAccessSpecifier(), access); |
| 8861 | |
| 8862 | |
| 8863 | } else { |
| 8864 | objectTy = S.Context.getTypeDeclType(target->getParent()); |
| 8865 | } |
| 8866 | |
| 8867 | return S.isMemberAccessibleForDeletion( |
| 8868 | target->getParent(), DeclAccessPair::make(target, access), objectTy); |
| 8869 | } |
| 8870 | |
| 8871 | |
| 8872 | |
| 8873 | bool SpecialMemberDeletionInfo::shouldDeleteForSubobjectCall( |
| 8874 | Subobject Subobj, Sema::SpecialMemberOverloadResult SMOR, |
| 8875 | bool IsDtorCallInCtor) { |
| 8876 | CXXMethodDecl *Decl = SMOR.getMethod(); |
| 8877 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| 8878 | |
| 8879 | int DiagKind = -1; |
| 8880 | |
| 8881 | if (SMOR.getKind() == Sema::SpecialMemberOverloadResult::NoMemberOrDeleted) |
| 8882 | DiagKind = !Decl ? 0 : 1; |
| 8883 | else if (SMOR.getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 8884 | DiagKind = 2; |
| 8885 | else if (!isAccessible(Subobj, Decl)) |
| 8886 | DiagKind = 3; |
| 8887 | else if (!IsDtorCallInCtor && Field && Field->getParent()->isUnion() && |
| 8888 | !Decl->isTrivial()) { |
| 8889 | |
| 8890 | |
| 8891 | |
| 8892 | |
| 8893 | |
| 8894 | DiagKind = 4; |
| 8895 | } |
| 8896 | |
| 8897 | if (DiagKind == -1) |
| 8898 | return false; |
| 8899 | |
| 8900 | if (Diagnose) { |
| 8901 | if (Field) { |
| 8902 | S.Diag(Field->getLocation(), |
| 8903 | diag::note_deleted_special_member_class_subobject) |
| 8904 | << getEffectiveCSM() << MD->getParent() << true |
| 8905 | << Field << DiagKind << IsDtorCallInCtor << false; |
| 8906 | } else { |
| 8907 | CXXBaseSpecifier *Base = Subobj.get<CXXBaseSpecifier*>(); |
| 8908 | S.Diag(Base->getBeginLoc(), |
| 8909 | diag::note_deleted_special_member_class_subobject) |
| 8910 | << getEffectiveCSM() << MD->getParent() << false |
| 8911 | << Base->getType() << DiagKind << IsDtorCallInCtor |
| 8912 | << false; |
| 8913 | } |
| 8914 | |
| 8915 | if (DiagKind == 1) |
| 8916 | S.NoteDeletedFunction(Decl); |
| 8917 | |
| 8918 | } |
| 8919 | |
| 8920 | return true; |
| 8921 | } |
| 8922 | |
| 8923 | |
| 8924 | |
| 8925 | bool SpecialMemberDeletionInfo::shouldDeleteForClassSubobject( |
| 8926 | CXXRecordDecl *Class, Subobject Subobj, unsigned Quals) { |
| 8927 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| 8928 | bool IsMutable = Field && Field->isMutable(); |
| 8929 | |
| 8930 | |
| 8931 | |
| 8932 | |
| 8933 | |
| 8934 | |
| 8935 | |
| 8936 | |
| 8937 | |
| 8938 | |
| 8939 | |
| 8940 | |
| 8941 | |
| 8942 | |
| 8943 | |
| 8944 | if (!(CSM == Sema::CXXDefaultConstructor && |
| 8945 | Field && Field->hasInClassInitializer()) && |
| 8946 | shouldDeleteForSubobjectCall(Subobj, lookupIn(Class, Quals, IsMutable), |
| 8947 | false)) |
| 8948 | return true; |
| 8949 | |
| 8950 | |
| 8951 | |
| 8952 | |
| 8953 | if (IsConstructor) { |
| 8954 | Sema::SpecialMemberOverloadResult SMOR = |
| 8955 | S.LookupSpecialMember(Class, Sema::CXXDestructor, |
| 8956 | false, false, false, false, false); |
| 8957 | if (shouldDeleteForSubobjectCall(Subobj, SMOR, true)) |
| 8958 | return true; |
| 8959 | } |
| 8960 | |
| 8961 | return false; |
| 8962 | } |
| 8963 | |
| 8964 | bool SpecialMemberDeletionInfo::shouldDeleteForVariantObjCPtrMember( |
| 8965 | FieldDecl *FD, QualType FieldType) { |
| 8966 | |
| 8967 | |
| 8968 | |
| 8969 | if (!FieldType.hasNonTrivialObjCLifetime()) |
| 8970 | return false; |
| 8971 | |
| 8972 | |
| 8973 | |
| 8974 | if (CSM == Sema::CXXDefaultConstructor && FD->hasInClassInitializer()) |
| 8975 | return false; |
| 8976 | |
| 8977 | if (Diagnose) { |
| 8978 | auto *ParentClass = cast<CXXRecordDecl>(FD->getParent()); |
| 8979 | S.Diag(FD->getLocation(), |
| 8980 | diag::note_deleted_special_member_class_subobject) |
| 8981 | << getEffectiveCSM() << ParentClass << true |
| 8982 | << FD << 4 << false << true; |
| 8983 | } |
| 8984 | |
| 8985 | return true; |
| 8986 | } |
| 8987 | |
| 8988 | |
| 8989 | |
| 8990 | bool SpecialMemberDeletionInfo::shouldDeleteForBase(CXXBaseSpecifier *Base) { |
| 8991 | CXXRecordDecl *BaseClass = Base->getType()->getAsCXXRecordDecl(); |
| 8992 | |
| 8993 | |
| 8994 | if (!BaseClass) |
| 8995 | return false; |
| 8996 | |
| 8997 | |
| 8998 | Sema::SpecialMemberOverloadResult SMOR = lookupInheritedCtor(BaseClass); |
| 8999 | if (auto *BaseCtor = SMOR.getMethod()) { |
| 9000 | |
| 9001 | |
| 9002 | |
| 9003 | |
| 9004 | if (BaseCtor->isDeleted() && Diagnose) { |
| 9005 | S.Diag(Base->getBeginLoc(), |
| 9006 | diag::note_deleted_special_member_class_subobject) |
| 9007 | << getEffectiveCSM() << MD->getParent() << false |
| 9008 | << Base->getType() << 1 << false |
| 9009 | << false; |
| 9010 | S.NoteDeletedFunction(BaseCtor); |
| 9011 | } |
| 9012 | return BaseCtor->isDeleted(); |
| 9013 | } |
| 9014 | return shouldDeleteForClassSubobject(BaseClass, Base, 0); |
| 9015 | } |
| 9016 | |
| 9017 | |
| 9018 | |
| 9019 | bool SpecialMemberDeletionInfo::shouldDeleteForField(FieldDecl *FD) { |
| 9020 | QualType FieldType = S.Context.getBaseElementType(FD->getType()); |
| 9021 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 9022 | |
| 9023 | if (inUnion() && shouldDeleteForVariantObjCPtrMember(FD, FieldType)) |
| 9024 | return true; |
| 9025 | |
| 9026 | if (CSM == Sema::CXXDefaultConstructor) { |
| 9027 | |
| 9028 | |
| 9029 | if (FieldType->isReferenceType() && !FD->hasInClassInitializer()) { |
| 9030 | if (Diagnose) |
| 9031 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
| 9032 | << !!ICI << MD->getParent() << FD << FieldType << 0; |
| 9033 | return true; |
| 9034 | } |
| 9035 | |
| 9036 | |
| 9037 | |
| 9038 | |
| 9039 | if (!inUnion() && FieldType.isConstQualified() && |
| 9040 | !FD->hasInClassInitializer() && |
| 9041 | (!FieldRecord || !FieldRecord->hasUserProvidedDefaultConstructor())) { |
| 9042 | if (Diagnose) |
| 9043 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
| 9044 | << !!ICI << MD->getParent() << FD << FD->getType() << 1; |
| 9045 | return true; |
| 9046 | } |
| 9047 | |
| 9048 | if (inUnion() && !FieldType.isConstQualified()) |
| 9049 | AllFieldsAreConst = false; |
| 9050 | } else if (CSM == Sema::CXXCopyConstructor) { |
| 9051 | |
| 9052 | |
| 9053 | if (FieldType->isRValueReferenceType()) { |
| 9054 | if (Diagnose) |
| 9055 | S.Diag(FD->getLocation(), diag::note_deleted_copy_ctor_rvalue_reference) |
| 9056 | << MD->getParent() << FD << FieldType; |
| 9057 | return true; |
| 9058 | } |
| 9059 | } else if (IsAssignment) { |
| 9060 | |
| 9061 | if (FieldType->isReferenceType()) { |
| 9062 | if (Diagnose) |
| 9063 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
| 9064 | << isMove() << MD->getParent() << FD << FieldType << 0; |
| 9065 | return true; |
| 9066 | } |
| 9067 | if (!FieldRecord && FieldType.isConstQualified()) { |
| 9068 | |
| 9069 | |
| 9070 | if (Diagnose) |
| 9071 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
| 9072 | << isMove() << MD->getParent() << FD << FD->getType() << 1; |
| 9073 | return true; |
| 9074 | } |
| 9075 | } |
| 9076 | |
| 9077 | if (FieldRecord) { |
| 9078 | |
| 9079 | if (!inUnion() && FieldRecord->isUnion() && |
| 9080 | FieldRecord->isAnonymousStructOrUnion()) { |
| 9081 | bool AllVariantFieldsAreConst = true; |
| 9082 | |
| 9083 | |
| 9084 | for (auto *UI : FieldRecord->fields()) { |
| 9085 | QualType UnionFieldType = S.Context.getBaseElementType(UI->getType()); |
| 9086 | |
| 9087 | if (shouldDeleteForVariantObjCPtrMember(&*UI, UnionFieldType)) |
| 9088 | return true; |
| 9089 | |
| 9090 | if (!UnionFieldType.isConstQualified()) |
| 9091 | AllVariantFieldsAreConst = false; |
| 9092 | |
| 9093 | CXXRecordDecl *UnionFieldRecord = UnionFieldType->getAsCXXRecordDecl(); |
| 9094 | if (UnionFieldRecord && |
| 9095 | shouldDeleteForClassSubobject(UnionFieldRecord, UI, |
| 9096 | UnionFieldType.getCVRQualifiers())) |
| 9097 | return true; |
| 9098 | } |
| 9099 | |
| 9100 | |
| 9101 | if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst && |
| 9102 | !FieldRecord->field_empty()) { |
| 9103 | if (Diagnose) |
| 9104 | S.Diag(FieldRecord->getLocation(), |
| 9105 | diag::note_deleted_default_ctor_all_const) |
| 9106 | << !!ICI << MD->getParent() << 1; |
| 9107 | return true; |
| 9108 | } |
| 9109 | |
| 9110 | |
| 9111 | |
| 9112 | return false; |
| 9113 | } |
| 9114 | |
| 9115 | if (shouldDeleteForClassSubobject(FieldRecord, FD, |
| 9116 | FieldType.getCVRQualifiers())) |
| 9117 | return true; |
| 9118 | } |
| 9119 | |
| 9120 | return false; |
| 9121 | } |
| 9122 | |
| 9123 | |
| 9124 | |
| 9125 | |
| 9126 | bool SpecialMemberDeletionInfo::shouldDeleteForAllConstMembers() { |
| 9127 | |
| 9128 | |
| 9129 | if (CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst) { |
| 9130 | bool AnyFields = false; |
| 9131 | for (auto *F : MD->getParent()->fields()) |
| 9132 | if ((AnyFields = !F->isUnnamedBitfield())) |
| 9133 | break; |
| 9134 | if (!AnyFields) |
| 9135 | return false; |
| 9136 | if (Diagnose) |
| 9137 | S.Diag(MD->getParent()->getLocation(), |
| 9138 | diag::note_deleted_default_ctor_all_const) |
| 9139 | << !!ICI << MD->getParent() << 0; |
| 9140 | return true; |
| 9141 | } |
| 9142 | return false; |
| 9143 | } |
| 9144 | |
| 9145 | |
| 9146 | |
| 9147 | |
| 9148 | bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 9149 | InheritedConstructorInfo *ICI, |
| 9150 | bool Diagnose) { |
| 9151 | if (MD->isInvalidDecl()) |
| 9152 | return false; |
| 9153 | CXXRecordDecl *RD = MD->getParent(); |
| 9154 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
| 9155 | if (!LangOpts.CPlusPlus11 || RD->isInvalidDecl()) |
| 9156 | return false; |
| 9157 | |
| 9158 | |
| 9159 | |
| 9160 | |
| 9161 | |
| 9162 | |
| 9163 | if (RD->isLambda() && !RD->lambdaIsDefaultConstructibleAndAssignable() && |
| 9164 | (CSM == CXXDefaultConstructor || CSM == CXXCopyAssignment)) { |
| 9165 | if (Diagnose) |
| 9166 | Diag(RD->getLocation(), diag::note_lambda_decl); |
| 9167 | return true; |
| 9168 | } |
| 9169 | |
| 9170 | |
| 9171 | |
| 9172 | |
| 9173 | if (CSM != CXXDefaultConstructor && CSM != CXXDestructor && |
| 9174 | RD->isAnonymousStructOrUnion()) |
| 9175 | return false; |
| 9176 | |
| 9177 | |
| 9178 | |
| 9179 | |
| 9180 | |
| 9181 | if (MD->isImplicit() && |
| 9182 | (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment)) { |
| 9183 | CXXMethodDecl *UserDeclaredMove = nullptr; |
| 9184 | |
| 9185 | |
| 9186 | |
| 9187 | |
| 9188 | bool DeletesOnlyMatchingCopy = |
| 9189 | getLangOpts().MSVCCompat && |
| 9190 | !getLangOpts().isCompatibleWithMSVC(LangOptions::MSVC2015); |
| 9191 | |
| 9192 | if (RD->hasUserDeclaredMoveConstructor() && |
| 9193 | (!DeletesOnlyMatchingCopy || CSM == CXXCopyConstructor)) { |
| 9194 | if (!Diagnose) return true; |
| 9195 | |
| 9196 | |
| 9197 | for (auto *I : RD->ctors()) { |
| 9198 | if (I->isMoveConstructor()) { |
| 9199 | UserDeclaredMove = I; |
| 9200 | break; |
| 9201 | } |
| 9202 | } |
| 9203 | assert(UserDeclaredMove); |
| 9204 | } else if (RD->hasUserDeclaredMoveAssignment() && |
| 9205 | (!DeletesOnlyMatchingCopy || CSM == CXXCopyAssignment)) { |
| 9206 | if (!Diagnose) return true; |
| 9207 | |
| 9208 | |
| 9209 | for (auto *I : RD->methods()) { |
| 9210 | if (I->isMoveAssignmentOperator()) { |
| 9211 | UserDeclaredMove = I; |
| 9212 | break; |
| 9213 | } |
| 9214 | } |
| 9215 | assert(UserDeclaredMove); |
| 9216 | } |
| 9217 | |
| 9218 | if (UserDeclaredMove) { |
| 9219 | Diag(UserDeclaredMove->getLocation(), |
| 9220 | diag::note_deleted_copy_user_declared_move) |
| 9221 | << (CSM == CXXCopyAssignment) << RD |
| 9222 | << UserDeclaredMove->isMoveAssignmentOperator(); |
| 9223 | return true; |
| 9224 | } |
| 9225 | } |
| 9226 | |
| 9227 | |
| 9228 | ContextRAII MethodContext(*this, MD); |
| 9229 | |
| 9230 | |
| 9231 | |
| 9232 | |
| 9233 | if (CSM == CXXDestructor && MD->isVirtual()) { |
| 9234 | FunctionDecl *OperatorDelete = nullptr; |
| 9235 | DeclarationName Name = |
| 9236 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 9237 | if (FindDeallocationFunction(MD->getLocation(), MD->getParent(), Name, |
| 9238 | OperatorDelete, false)) { |
| 9239 | if (Diagnose) |
| 9240 | Diag(RD->getLocation(), diag::note_deleted_dtor_no_operator_delete); |
| 9241 | return true; |
| 9242 | } |
| 9243 | } |
| 9244 | |
| 9245 | SpecialMemberDeletionInfo SMI(*this, MD, CSM, ICI, Diagnose); |
| 9246 | |
| 9247 | |
| 9248 | |
| 9249 | |
| 9250 | |
| 9251 | |
| 9252 | |
| 9253 | if (SMI.visit(SMI.IsAssignment ? SMI.VisitDirectBases |
| 9254 | : SMI.VisitPotentiallyConstructedBases)) |
| 9255 | return true; |
| 9256 | |
| 9257 | if (SMI.shouldDeleteForAllConstMembers()) |
| 9258 | return true; |
| 9259 | |
| 9260 | if (getLangOpts().CUDA) { |
| 9261 | |
| 9262 | |
| 9263 | |
| 9264 | |
| 9265 | |
| 9266 | |
| 9267 | assert(ICI || CSM == getSpecialMember(MD)); |
| 9268 | auto RealCSM = CSM; |
| 9269 | if (ICI) |
| 9270 | RealCSM = getSpecialMember(MD); |
| 9271 | |
| 9272 | return inferCUDATargetForImplicitSpecialMember(RD, RealCSM, MD, |
| 9273 | SMI.ConstArg, Diagnose); |
| 9274 | } |
| 9275 | |
| 9276 | return false; |
| 9277 | } |
| 9278 | |
| 9279 | void Sema::DiagnoseDeletedDefaultedFunction(FunctionDecl *FD) { |
| 9280 | DefaultedFunctionKind DFK = getDefaultedFunctionKind(FD); |
| 9281 | assert(DFK && "not a defaultable function"); |
| 9282 | assert(FD->isDefaulted() && FD->isDeleted() && "not defaulted and deleted"); |
| 9283 | |
| 9284 | if (DFK.isSpecialMember()) { |
| 9285 | ShouldDeleteSpecialMember(cast<CXXMethodDecl>(FD), DFK.asSpecialMember(), |
| 9286 | nullptr, true); |
| 9287 | } else { |
| 9288 | DefaultedComparisonAnalyzer( |
| 9289 | *this, cast<CXXRecordDecl>(FD->getLexicalDeclContext()), FD, |
| 9290 | DFK.asComparison(), DefaultedComparisonAnalyzer::ExplainDeleted) |
| 9291 | .visit(); |
| 9292 | } |
| 9293 | } |
| 9294 | |
| 9295 | |
| 9296 | |
| 9297 | |
| 9298 | |
| 9299 | |
| 9300 | |
| 9301 | |
| 9302 | |
| 9303 | |
| 9304 | |
| 9305 | |
| 9306 | static bool findTrivialSpecialMember(Sema &S, CXXRecordDecl *RD, |
| 9307 | Sema::CXXSpecialMember CSM, unsigned Quals, |
| 9308 | bool ConstRHS, |
| 9309 | Sema::TrivialABIHandling TAH, |
| 9310 | CXXMethodDecl **Selected) { |
| 9311 | if (Selected) |
| 9312 | *Selected = nullptr; |
| 9313 | |
| 9314 | switch (CSM) { |
| 9315 | case Sema::CXXInvalid: |
| 9316 | llvm_unreachable("not a special member"); |
| 9317 | |
| 9318 | case Sema::CXXDefaultConstructor: |
| 9319 | |
| 9320 | |
| 9321 | |
| 9322 | |
| 9323 | |
| 9324 | if (RD->hasTrivialDefaultConstructor()) |
| 9325 | return true; |
| 9326 | |
| 9327 | if (Selected) { |
| 9328 | |
| 9329 | |
| 9330 | |
| 9331 | CXXConstructorDecl *DefCtor = nullptr; |
| 9332 | if (RD->needsImplicitDefaultConstructor()) |
| 9333 | S.DeclareImplicitDefaultConstructor(RD); |
| 9334 | for (auto *CI : RD->ctors()) { |
| 9335 | if (!CI->isDefaultConstructor()) |
| 9336 | continue; |
| 9337 | DefCtor = CI; |
| 9338 | if (!DefCtor->isUserProvided()) |
| 9339 | break; |
| 9340 | } |
| 9341 | |
| 9342 | *Selected = DefCtor; |
| 9343 | } |
| 9344 | |
| 9345 | return false; |
| 9346 | |
| 9347 | case Sema::CXXDestructor: |
| 9348 | |
| 9349 | |
| 9350 | |
| 9351 | if (RD->hasTrivialDestructor() || |
| 9352 | (TAH == Sema::TAH_ConsiderTrivialABI && |
| 9353 | RD->hasTrivialDestructorForCall())) |
| 9354 | return true; |
| 9355 | |
| 9356 | if (Selected) { |
| 9357 | if (RD->needsImplicitDestructor()) |
| 9358 | S.DeclareImplicitDestructor(RD); |
| 9359 | *Selected = RD->getDestructor(); |
| 9360 | } |
| 9361 | |
| 9362 | return false; |
| 9363 | |
| 9364 | case Sema::CXXCopyConstructor: |
| 9365 | |
| 9366 | |
| 9367 | |
| 9368 | if (RD->hasTrivialCopyConstructor() || |
| 9369 | (TAH == Sema::TAH_ConsiderTrivialABI && |
| 9370 | RD->hasTrivialCopyConstructorForCall())) { |
| 9371 | if (Quals == Qualifiers::Const) |
| 9372 | |
| 9373 | |
| 9374 | return true; |
| 9375 | } else if (!Selected) { |
| 9376 | return false; |
| 9377 | } |
| 9378 | |
| 9379 | |
| 9380 | |
| 9381 | |
| 9382 | |
| 9383 | goto NeedOverloadResolution; |
| 9384 | |
| 9385 | case Sema::CXXCopyAssignment: |
| 9386 | |
| 9387 | |
| 9388 | |
| 9389 | |
| 9390 | if (RD->hasTrivialCopyAssignment()) { |
| 9391 | if (Quals == Qualifiers::Const) |
| 9392 | return true; |
| 9393 | } else if (!Selected) { |
| 9394 | return false; |
| 9395 | } |
| 9396 | |
| 9397 | |
| 9398 | goto NeedOverloadResolution; |
| 9399 | |
| 9400 | case Sema::CXXMoveConstructor: |
| 9401 | case Sema::CXXMoveAssignment: |
| 9402 | NeedOverloadResolution: |
| 9403 | Sema::SpecialMemberOverloadResult SMOR = |
| 9404 | lookupCallFromSpecialMember(S, RD, CSM, Quals, ConstRHS); |
| 9405 | |
| 9406 | |
| 9407 | |
| 9408 | |
| 9409 | |
| 9410 | if (SMOR.getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 9411 | return true; |
| 9412 | |
| 9413 | if (!SMOR.getMethod()) { |
| 9414 | assert(SMOR.getKind() == |
| 9415 | Sema::SpecialMemberOverloadResult::NoMemberOrDeleted); |
| 9416 | return false; |
| 9417 | } |
| 9418 | |
| 9419 | |
| 9420 | |
| 9421 | if (Selected) |
| 9422 | *Selected = SMOR.getMethod(); |
| 9423 | |
| 9424 | if (TAH == Sema::TAH_ConsiderTrivialABI && |
| 9425 | (CSM == Sema::CXXCopyConstructor || CSM == Sema::CXXMoveConstructor)) |
| 9426 | return SMOR.getMethod()->isTrivialForCall(); |
| 9427 | return SMOR.getMethod()->isTrivial(); |
| 9428 | } |
| 9429 | |
| 9430 | llvm_unreachable("unknown special method kind"); |
| 9431 | } |
| 9432 | |
| 9433 | static CXXConstructorDecl *findUserDeclaredCtor(CXXRecordDecl *RD) { |
| 9434 | for (auto *CI : RD->ctors()) |
| 9435 | if (!CI->isImplicit()) |
| 9436 | return CI; |
| 9437 | |
| 9438 | |
| 9439 | typedef CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> tmpl_iter; |
| 9440 | for (tmpl_iter TI(RD->decls_begin()), TE(RD->decls_end()); TI != TE; ++TI) { |
| 9441 | if (CXXConstructorDecl *CD = |
| 9442 | dyn_cast<CXXConstructorDecl>(TI->getTemplatedDecl())) |
| 9443 | return CD; |
| 9444 | } |
| 9445 | |
| 9446 | return nullptr; |
| 9447 | } |
| 9448 | |
| 9449 | |
| 9450 | |
| 9451 | enum TrivialSubobjectKind { |
| 9452 | |
| 9453 | TSK_BaseClass, |
| 9454 | |
| 9455 | TSK_Field, |
| 9456 | |
| 9457 | TSK_CompleteObject |
| 9458 | }; |
| 9459 | |
| 9460 | |
| 9461 | static bool checkTrivialSubobjectCall(Sema &S, SourceLocation SubobjLoc, |
| 9462 | QualType SubType, bool ConstRHS, |
| 9463 | Sema::CXXSpecialMember CSM, |
| 9464 | TrivialSubobjectKind Kind, |
| 9465 | Sema::TrivialABIHandling TAH, bool Diagnose) { |
| 9466 | CXXRecordDecl *SubRD = SubType->getAsCXXRecordDecl(); |
| 9467 | if (!SubRD) |
| 9468 | return true; |
| 9469 | |
| 9470 | CXXMethodDecl *Selected; |
| 9471 | if (findTrivialSpecialMember(S, SubRD, CSM, SubType.getCVRQualifiers(), |
| 9472 | ConstRHS, TAH, Diagnose ? &Selected : nullptr)) |
| 9473 | return true; |
| 9474 | |
| 9475 | if (Diagnose) { |
| 9476 | if (ConstRHS) |
| 9477 | SubType.addConst(); |
| 9478 | |
| 9479 | if (!Selected && CSM == Sema::CXXDefaultConstructor) { |
| 9480 | S.Diag(SubobjLoc, diag::note_nontrivial_no_def_ctor) |
| 9481 | << Kind << SubType.getUnqualifiedType(); |
| 9482 | if (CXXConstructorDecl *CD = findUserDeclaredCtor(SubRD)) |
| 9483 | S.Diag(CD->getLocation(), diag::note_user_declared_ctor); |
| 9484 | } else if (!Selected) |
| 9485 | S.Diag(SubobjLoc, diag::note_nontrivial_no_copy) |
| 9486 | << Kind << SubType.getUnqualifiedType() << CSM << SubType; |
| 9487 | else if (Selected->isUserProvided()) { |
| 9488 | if (Kind == TSK_CompleteObject) |
| 9489 | S.Diag(Selected->getLocation(), diag::note_nontrivial_user_provided) |
| 9490 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 9491 | else { |
| 9492 | S.Diag(SubobjLoc, diag::note_nontrivial_user_provided) |
| 9493 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 9494 | S.Diag(Selected->getLocation(), diag::note_declared_at); |
| 9495 | } |
| 9496 | } else { |
| 9497 | if (Kind != TSK_CompleteObject) |
| 9498 | S.Diag(SubobjLoc, diag::note_nontrivial_subobject) |
| 9499 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 9500 | |
| 9501 | |
| 9502 | S.SpecialMemberIsTrivial(Selected, CSM, Sema::TAH_IgnoreTrivialABI, |
| 9503 | Diagnose); |
| 9504 | } |
| 9505 | } |
| 9506 | |
| 9507 | return false; |
| 9508 | } |
| 9509 | |
| 9510 | |
| 9511 | |
| 9512 | static bool checkTrivialClassMembers(Sema &S, CXXRecordDecl *RD, |
| 9513 | Sema::CXXSpecialMember CSM, |
| 9514 | bool ConstArg, |
| 9515 | Sema::TrivialABIHandling TAH, |
| 9516 | bool Diagnose) { |
| 9517 | for (const auto *FI : RD->fields()) { |
| 9518 | if (FI->isInvalidDecl() || FI->isUnnamedBitfield()) |
| 9519 | continue; |
| 9520 | |
| 9521 | QualType FieldType = S.Context.getBaseElementType(FI->getType()); |
| 9522 | |
| 9523 | |
| 9524 | if (FI->isAnonymousStructOrUnion()) { |
| 9525 | if (!checkTrivialClassMembers(S, FieldType->getAsCXXRecordDecl(), |
| 9526 | CSM, ConstArg, TAH, Diagnose)) |
| 9527 | return false; |
| 9528 | continue; |
| 9529 | } |
| 9530 | |
| 9531 | |
| 9532 | |
| 9533 | |
| 9534 | |
| 9535 | if (CSM == Sema::CXXDefaultConstructor && FI->hasInClassInitializer()) { |
| 9536 | if (Diagnose) |
| 9537 | S.Diag(FI->getLocation(), diag::note_nontrivial_default_member_init) |
| 9538 | << FI; |
| 9539 | return false; |
| 9540 | } |
| 9541 | |
| 9542 | |
| 9543 | |
| 9544 | |
| 9545 | |
| 9546 | if (FieldType.hasNonTrivialObjCLifetime()) { |
| 9547 | if (Diagnose) |
| 9548 | S.Diag(FI->getLocation(), diag::note_nontrivial_objc_ownership) |
| 9549 | << RD << FieldType.getObjCLifetime(); |
| 9550 | return false; |
| 9551 | } |
| 9552 | |
| 9553 | bool ConstRHS = ConstArg && !FI->isMutable(); |
| 9554 | if (!checkTrivialSubobjectCall(S, FI->getLocation(), FieldType, ConstRHS, |
| 9555 | CSM, TSK_Field, TAH, Diagnose)) |
| 9556 | return false; |
| 9557 | } |
| 9558 | |
| 9559 | return true; |
| 9560 | } |
| 9561 | |
| 9562 | |
| 9563 | |
| 9564 | void Sema::DiagnoseNontrivial(const CXXRecordDecl *RD, CXXSpecialMember CSM) { |
| 9565 | QualType Ty = Context.getRecordType(RD); |
| 9566 | |
| 9567 | bool ConstArg = (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment); |
| 9568 | checkTrivialSubobjectCall(*this, RD->getLocation(), Ty, ConstArg, CSM, |
| 9569 | TSK_CompleteObject, TAH_IgnoreTrivialABI, |
| 9570 | true); |
| 9571 | } |
| 9572 | |
| 9573 | |
| 9574 | |
| 9575 | |
| 9576 | bool Sema::SpecialMemberIsTrivial(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 9577 | TrivialABIHandling TAH, bool Diagnose) { |
| 9578 | assert(!MD->isUserProvided() && CSM != CXXInvalid && "not special enough"); |
| 9579 | |
| 9580 | CXXRecordDecl *RD = MD->getParent(); |
| 9581 | |
| 9582 | bool ConstArg = false; |
| 9583 | |
| 9584 | |
| 9585 | |
| 9586 | |
| 9587 | switch (CSM) { |
| 9588 | case CXXDefaultConstructor: |
| 9589 | case CXXDestructor: |
| 9590 | |
| 9591 | break; |
| 9592 | |
| 9593 | case CXXCopyConstructor: |
| 9594 | case CXXCopyAssignment: { |
| 9595 | |
| 9596 | ConstArg = true; |
| 9597 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
| 9598 | const ReferenceType *RT = Param0->getType()->getAs<ReferenceType>(); |
| 9599 | if (!RT || RT->getPointeeType().getCVRQualifiers() != Qualifiers::Const) { |
| 9600 | if (Diagnose) |
| 9601 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 9602 | << Param0->getSourceRange() << Param0->getType() |
| 9603 | << Context.getLValueReferenceType( |
| 9604 | Context.getRecordType(RD).withConst()); |
| 9605 | return false; |
| 9606 | } |
| 9607 | break; |
| 9608 | } |
| 9609 | |
| 9610 | case CXXMoveConstructor: |
| 9611 | case CXXMoveAssignment: { |
| 9612 | |
| 9613 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
| 9614 | const RValueReferenceType *RT = |
| 9615 | Param0->getType()->getAs<RValueReferenceType>(); |
| 9616 | if (!RT || RT->getPointeeType().getCVRQualifiers()) { |
| 9617 | if (Diagnose) |
| 9618 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 9619 | << Param0->getSourceRange() << Param0->getType() |
| 9620 | << Context.getRValueReferenceType(Context.getRecordType(RD)); |
| 9621 | return false; |
| 9622 | } |
| 9623 | break; |
| 9624 | } |
| 9625 | |
| 9626 | case CXXInvalid: |
| 9627 | llvm_unreachable("not a special member"); |
| 9628 | } |
| 9629 | |
| 9630 | if (MD->getMinRequiredArguments() < MD->getNumParams()) { |
| 9631 | if (Diagnose) |
| 9632 | Diag(MD->getParamDecl(MD->getMinRequiredArguments())->getLocation(), |
| 9633 | diag::note_nontrivial_default_arg) |
| 9634 | << MD->getParamDecl(MD->getMinRequiredArguments())->getSourceRange(); |
| 9635 | return false; |
| 9636 | } |
| 9637 | if (MD->isVariadic()) { |
| 9638 | if (Diagnose) |
| 9639 | Diag(MD->getLocation(), diag::note_nontrivial_variadic); |
| 9640 | return false; |
| 9641 | } |
| 9642 | |
| 9643 | |
| 9644 | |
| 9645 | |
| 9646 | |
| 9647 | |
| 9648 | |
| 9649 | |
| 9650 | |
| 9651 | |
| 9652 | for (const auto &BI : RD->bases()) |
| 9653 | if (!checkTrivialSubobjectCall(*this, BI.getBeginLoc(), BI.getType(), |
| 9654 | ConstArg, CSM, TSK_BaseClass, TAH, Diagnose)) |
| 9655 | return false; |
| 9656 | |
| 9657 | |
| 9658 | |
| 9659 | |
| 9660 | |
| 9661 | |
| 9662 | |
| 9663 | |
| 9664 | |
| 9665 | |
| 9666 | |
| 9667 | |
| 9668 | |
| 9669 | if (!checkTrivialClassMembers(*this, RD, CSM, ConstArg, TAH, Diagnose)) |
| 9670 | return false; |
| 9671 | |
| 9672 | |
| 9673 | |
| 9674 | |
| 9675 | if (CSM == CXXDestructor && MD->isVirtual()) { |
| 9676 | if (Diagnose) |
| 9677 | Diag(MD->getLocation(), diag::note_nontrivial_virtual_dtor) << RD; |
| 9678 | return false; |
| 9679 | } |
| 9680 | |
| 9681 | |
| 9682 | |
| 9683 | |
| 9684 | if (CSM != CXXDestructor && MD->getParent()->isDynamicClass()) { |
| 9685 | if (!Diagnose) |
| 9686 | return false; |
| 9687 | |
| 9688 | if (RD->getNumVBases()) { |
| 9689 | |
| 9690 | |
| 9691 | CXXBaseSpecifier &BS = *RD->vbases_begin(); |
| 9692 | assert(BS.isVirtual()); |
| 9693 | Diag(BS.getBeginLoc(), diag::note_nontrivial_has_virtual) << RD << 1; |
| 9694 | return false; |
| 9695 | } |
| 9696 | |
| 9697 | |
| 9698 | for (const auto *MI : RD->methods()) { |
| 9699 | if (MI->isVirtual()) { |
| 9700 | SourceLocation MLoc = MI->getBeginLoc(); |
| 9701 | Diag(MLoc, diag::note_nontrivial_has_virtual) << RD << 0; |
| 9702 | return false; |
| 9703 | } |
| 9704 | } |
| 9705 | |
| 9706 | llvm_unreachable("dynamic class with no vbases and no virtual functions"); |
| 9707 | } |
| 9708 | |
| 9709 | |
| 9710 | return true; |
| 9711 | } |
| 9712 | |
| 9713 | namespace { |
| 9714 | struct FindHiddenVirtualMethod { |
| 9715 | Sema *S; |
| 9716 | CXXMethodDecl *Method; |
| 9717 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
| 9718 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 9719 | |
| 9720 | private: |
| 9721 | |
| 9722 | static bool CheckMostOverridenMethods( |
| 9723 | const CXXMethodDecl *MD, |
| 9724 | const llvm::SmallPtrSetImpl<const CXXMethodDecl *> &Methods) { |
| 9725 | if (MD->size_overridden_methods() == 0) |
| 9726 | return Methods.count(MD->getCanonicalDecl()); |
| 9727 | for (const CXXMethodDecl *O : MD->overridden_methods()) |
| 9728 | if (CheckMostOverridenMethods(O, Methods)) |
| 9729 | return true; |
| 9730 | return false; |
| 9731 | } |
| 9732 | |
| 9733 | public: |
| 9734 | |
| 9735 | |
| 9736 | |
| 9737 | bool operator()(const CXXBaseSpecifier *Specifier, CXXBasePath &Path) { |
| 9738 | RecordDecl *BaseRecord = |
| 9739 | Specifier->getType()->castAs<RecordType>()->getDecl(); |
| 9740 | |
| 9741 | DeclarationName Name = Method->getDeclName(); |
| 9742 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 9743 | |
| 9744 | bool foundSameNameMethod = false; |
| 9745 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
| 9746 | for (Path.Decls = BaseRecord->lookup(Name).begin(); |
| 9747 | Path.Decls != DeclContext::lookup_iterator(); ++Path.Decls) { |
| 9748 | NamedDecl *D = *Path.Decls; |
| 9749 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 9750 | MD = MD->getCanonicalDecl(); |
| 9751 | foundSameNameMethod = true; |
| 9752 | |
| 9753 | if (!MD->isVirtual()) |
| 9754 | continue; |
| 9755 | |
| 9756 | |
| 9757 | |
| 9758 | |
| 9759 | |
| 9760 | |
| 9761 | |
| 9762 | |
| 9763 | |
| 9764 | |
| 9765 | if (!S->IsOverload(Method, MD, false)) |
| 9766 | return true; |
| 9767 | |
| 9768 | if (!CheckMostOverridenMethods(MD, OverridenAndUsingBaseMethods)) |
| 9769 | overloadedMethods.push_back(MD); |
| 9770 | } |
| 9771 | } |
| 9772 | |
| 9773 | if (foundSameNameMethod) |
| 9774 | OverloadedMethods.append(overloadedMethods.begin(), |
| 9775 | overloadedMethods.end()); |
| 9776 | return foundSameNameMethod; |
| 9777 | } |
| 9778 | }; |
| 9779 | } |
| 9780 | |
| 9781 | |
| 9782 | static void AddMostOverridenMethods(const CXXMethodDecl *MD, |
| 9783 | llvm::SmallPtrSetImpl<const CXXMethodDecl *>& Methods) { |
| 9784 | if (MD->size_overridden_methods() == 0) |
| 9785 | Methods.insert(MD->getCanonicalDecl()); |
| 9786 | else |
| 9787 | for (const CXXMethodDecl *O : MD->overridden_methods()) |
| 9788 | AddMostOverridenMethods(O, Methods); |
| 9789 | } |
| 9790 | |
| 9791 | |
| 9792 | |
| 9793 | void Sema::FindHiddenVirtualMethods(CXXMethodDecl *MD, |
| 9794 | SmallVectorImpl<CXXMethodDecl*> &OverloadedMethods) { |
| 9795 | if (!MD->getDeclName().isIdentifier()) |
| 9796 | return; |
| 9797 | |
| 9798 | CXXBasePaths Paths(true, |
| 9799 | false, |
| 9800 | false); |
| 9801 | FindHiddenVirtualMethod FHVM; |
| 9802 | FHVM.Method = MD; |
| 9803 | FHVM.S = this; |
| 9804 | |
| 9805 | |
| 9806 | |
| 9807 | CXXRecordDecl *DC = MD->getParent(); |
| 9808 | DeclContext::lookup_result R = DC->lookup(MD->getDeclName()); |
| 9809 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 9810 | NamedDecl *ND = *I; |
| 9811 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*I)) |
| 9812 | ND = shad->getTargetDecl(); |
| 9813 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 9814 | AddMostOverridenMethods(MD, FHVM.OverridenAndUsingBaseMethods); |
| 9815 | } |
| 9816 | |
| 9817 | if (DC->lookupInBases(FHVM, Paths)) |
| 9818 | OverloadedMethods = FHVM.OverloadedMethods; |
| 9819 | } |
| 9820 | |
| 9821 | void Sema::NoteHiddenVirtualMethods(CXXMethodDecl *MD, |
| 9822 | SmallVectorImpl<CXXMethodDecl*> &OverloadedMethods) { |
| 9823 | for (unsigned i = 0, e = OverloadedMethods.size(); i != e; ++i) { |
| 9824 | CXXMethodDecl *overloadedMD = OverloadedMethods[i]; |
| 9825 | PartialDiagnostic PD = PDiag( |
| 9826 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 9827 | HandleFunctionTypeMismatch(PD, MD->getType(), overloadedMD->getType()); |
| 9828 | Diag(overloadedMD->getLocation(), PD); |
| 9829 | } |
| 9830 | } |
| 9831 | |
| 9832 | |
| 9833 | |
| 9834 | void Sema::DiagnoseHiddenVirtualMethods(CXXMethodDecl *MD) { |
| 9835 | if (MD->isInvalidDecl()) |
| 9836 | return; |
| 9837 | |
| 9838 | if (Diags.isIgnored(diag::warn_overloaded_virtual, MD->getLocation())) |
| 9839 | return; |
| 9840 | |
| 9841 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 9842 | FindHiddenVirtualMethods(MD, OverloadedMethods); |
| 9843 | if (!OverloadedMethods.empty()) { |
| 9844 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 9845 | << MD << (OverloadedMethods.size() > 1); |
| 9846 | |
| 9847 | NoteHiddenVirtualMethods(MD, OverloadedMethods); |
| 9848 | } |
| 9849 | } |
| 9850 | |
| 9851 | void Sema::checkIllFormedTrivialABIStruct(CXXRecordDecl &RD) { |
| 9852 | auto PrintDiagAndRemoveAttr = [&](unsigned N) { |
| 9853 | |
| 9854 | if (!isTemplateInstantiation(RD.getTemplateSpecializationKind())) { |
| 9855 | Diag(RD.getAttr<TrivialABIAttr>()->getLocation(), |
| 9856 | diag::ext_cannot_use_trivial_abi) << &RD; |
| 9857 | Diag(RD.getAttr<TrivialABIAttr>()->getLocation(), |
| 9858 | diag::note_cannot_use_trivial_abi_reason) << &RD << N; |
| 9859 | } |
| 9860 | RD.dropAttr<TrivialABIAttr>(); |
| 9861 | }; |
| 9862 | |
| 9863 | |
| 9864 | auto HasNonDeletedCopyOrMoveConstructor = [&]() { |
| 9865 | |
| 9866 | |
| 9867 | if (RD.isDependentType()) |
| 9868 | return true; |
| 9869 | if (RD.needsImplicitCopyConstructor() && |
| 9870 | !RD.defaultedCopyConstructorIsDeleted()) |
| 9871 | return true; |
| 9872 | if (RD.needsImplicitMoveConstructor() && |
| 9873 | !RD.defaultedMoveConstructorIsDeleted()) |
| 9874 | return true; |
| 9875 | for (const CXXConstructorDecl *CD : RD.ctors()) |
| 9876 | if (CD->isCopyOrMoveConstructor() && !CD->isDeleted()) |
| 9877 | return true; |
| 9878 | return false; |
| 9879 | }; |
| 9880 | |
| 9881 | if (!HasNonDeletedCopyOrMoveConstructor()) { |
| 9882 | PrintDiagAndRemoveAttr(0); |
| 9883 | return; |
| 9884 | } |
| 9885 | |
| 9886 | |
| 9887 | if (RD.isPolymorphic()) { |
| 9888 | PrintDiagAndRemoveAttr(1); |
| 9889 | return; |
| 9890 | } |
| 9891 | |
| 9892 | for (const auto &B : RD.bases()) { |
| 9893 | |
| 9894 | |
| 9895 | if (!B.getType()->isDependentType() && |
| 9896 | !B.getType()->getAsCXXRecordDecl()->canPassInRegisters()) { |
| 9897 | PrintDiagAndRemoveAttr(2); |
| 9898 | return; |
| 9899 | } |
| 9900 | |
| 9901 | if (B.isVirtual()) { |
| 9902 | PrintDiagAndRemoveAttr(3); |
| 9903 | return; |
| 9904 | } |
| 9905 | } |
| 9906 | |
| 9907 | for (const auto *FD : RD.fields()) { |
| 9908 | |
| 9909 | |
| 9910 | QualType FT = FD->getType(); |
| 9911 | if (FT.getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 9912 | PrintDiagAndRemoveAttr(4); |
| 9913 | return; |
| 9914 | } |
| 9915 | |
| 9916 | if (const auto *RT = FT->getBaseElementTypeUnsafe()->getAs<RecordType>()) |
| 9917 | if (!RT->isDependentType() && |
| 9918 | !cast<CXXRecordDecl>(RT->getDecl())->canPassInRegisters()) { |
| 9919 | PrintDiagAndRemoveAttr(5); |
| 9920 | return; |
| 9921 | } |
| 9922 | } |
| 9923 | } |
| 9924 | |
| 9925 | void Sema::ActOnFinishCXXMemberSpecification( |
| 9926 | Scope *S, SourceLocation RLoc, Decl *TagDecl, SourceLocation LBrac, |
| 9927 | SourceLocation RBrac, const ParsedAttributesView &AttrList) { |
| 9928 | if (!TagDecl) |
| 9929 | return; |
| 9930 | |
| 9931 | AdjustDeclIfTemplate(TagDecl); |
| 9932 | |
| 9933 | for (const ParsedAttr &AL : AttrList) { |
| 9934 | if (AL.getKind() != ParsedAttr::AT_Visibility) |
| 9935 | continue; |
| 9936 | AL.setInvalid(); |
| 9937 | Diag(AL.getLoc(), diag::warn_attribute_after_definition_ignored) << AL; |
| 9938 | } |
| 9939 | |
| 9940 | ActOnFields(S, RLoc, TagDecl, llvm::makeArrayRef( |
| 9941 | |
| 9942 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
| 9943 | FieldCollector->getCurNumFields()), LBrac, RBrac, AttrList); |
| 9944 | |
| 9945 | CheckCompletedCXXClass(S, cast<CXXRecordDecl>(TagDecl)); |
| 9946 | } |
| 9947 | |
| 9948 | |
| 9949 | |
| 9950 | static void findImplicitlyDeclaredEqualityComparisons( |
| 9951 | ASTContext &Ctx, CXXRecordDecl *RD, |
| 9952 | llvm::SmallVectorImpl<FunctionDecl *> &Spaceships) { |
| 9953 | DeclarationName EqEq = Ctx.DeclarationNames.getCXXOperatorName(OO_EqualEqual); |
| 9954 | if (!RD->lookup(EqEq).empty()) |
| 9955 | |
| 9956 | return; |
| 9957 | |
| 9958 | |
| 9959 | for (FriendDecl *Friend : RD->friends()) { |
| 9960 | FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Friend->getFriendDecl()); |
| 9961 | if (!FD) continue; |
| 9962 | |
| 9963 | if (FD->getOverloadedOperator() == OO_EqualEqual) { |
| 9964 | |
| 9965 | Spaceships.clear(); |
| 9966 | return; |
| 9967 | } |
| 9968 | |
| 9969 | if (FD->getOverloadedOperator() == OO_Spaceship && |
| 9970 | FD->isExplicitlyDefaulted()) |
| 9971 | Spaceships.push_back(FD); |
| 9972 | } |
| 9973 | |
| 9974 | |
| 9975 | DeclarationName Cmp = Ctx.DeclarationNames.getCXXOperatorName(OO_Spaceship); |
| 9976 | for (NamedDecl *ND : RD->lookup(Cmp)) { |
| 9977 | |
| 9978 | |
| 9979 | |
| 9980 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 9981 | if (FD->isExplicitlyDefaulted()) |
| 9982 | Spaceships.push_back(FD); |
| 9983 | } |
| 9984 | } |
| 9985 | |
| 9986 | |
| 9987 | |
| 9988 | |
| 9989 | |
| 9990 | |
| 9991 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
| 9992 | |
| 9993 | |
| 9994 | |
| 9995 | if (!ClassDecl->isDependentType()) { |
| 9996 | if (ClassDecl->needsImplicitDefaultConstructor()) { |
| 9997 | ++getASTContext().NumImplicitDefaultConstructors; |
| 9998 | |
| 9999 | if (ClassDecl->hasInheritedConstructor()) |
| 10000 | DeclareImplicitDefaultConstructor(ClassDecl); |
| 10001 | } |
| 10002 | |
| 10003 | if (ClassDecl->needsImplicitCopyConstructor()) { |
| 10004 | ++getASTContext().NumImplicitCopyConstructors; |
| 10005 | |
| 10006 | |
| 10007 | |
| 10008 | |
| 10009 | if (ClassDecl->needsOverloadResolutionForCopyConstructor() || |
| 10010 | ClassDecl->hasInheritedConstructor()) |
| 10011 | DeclareImplicitCopyConstructor(ClassDecl); |
| 10012 | |
| 10013 | |
| 10014 | |
| 10015 | |
| 10016 | |
| 10017 | else if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 10018 | (ClassDecl->hasUserDeclaredMoveConstructor() || |
| 10019 | ClassDecl->needsOverloadResolutionForMoveConstructor() || |
| 10020 | ClassDecl->hasUserDeclaredMoveAssignment() || |
| 10021 | ClassDecl->needsOverloadResolutionForMoveAssignment())) |
| 10022 | DeclareImplicitCopyConstructor(ClassDecl); |
| 10023 | } |
| 10024 | |
| 10025 | if (getLangOpts().CPlusPlus11 && |
| 10026 | ClassDecl->needsImplicitMoveConstructor()) { |
| 10027 | ++getASTContext().NumImplicitMoveConstructors; |
| 10028 | |
| 10029 | if (ClassDecl->needsOverloadResolutionForMoveConstructor() || |
| 10030 | ClassDecl->hasInheritedConstructor()) |
| 10031 | DeclareImplicitMoveConstructor(ClassDecl); |
| 10032 | } |
| 10033 | |
| 10034 | if (ClassDecl->needsImplicitCopyAssignment()) { |
| 10035 | ++getASTContext().NumImplicitCopyAssignmentOperators; |
| 10036 | |
| 10037 | |
| 10038 | |
| 10039 | |
| 10040 | |
| 10041 | if (ClassDecl->isDynamicClass() || |
| 10042 | ClassDecl->needsOverloadResolutionForCopyAssignment() || |
| 10043 | ClassDecl->hasInheritedAssignment()) |
| 10044 | DeclareImplicitCopyAssignment(ClassDecl); |
| 10045 | } |
| 10046 | |
| 10047 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveAssignment()) { |
| 10048 | ++getASTContext().NumImplicitMoveAssignmentOperators; |
| 10049 | |
| 10050 | |
| 10051 | if (ClassDecl->isDynamicClass() || |
| 10052 | ClassDecl->needsOverloadResolutionForMoveAssignment() || |
| 10053 | ClassDecl->hasInheritedAssignment()) |
| 10054 | DeclareImplicitMoveAssignment(ClassDecl); |
| 10055 | } |
| 10056 | |
| 10057 | if (ClassDecl->needsImplicitDestructor()) { |
| 10058 | ++getASTContext().NumImplicitDestructors; |
| 10059 | |
| 10060 | |
| 10061 | |
| 10062 | |
| 10063 | |
| 10064 | if (ClassDecl->isDynamicClass() || |
| 10065 | ClassDecl->needsOverloadResolutionForDestructor()) |
| 10066 | DeclareImplicitDestructor(ClassDecl); |
| 10067 | } |
| 10068 | } |
| 10069 | |
| 10070 | |
| 10071 | |
| 10072 | |
| 10073 | |
| 10074 | |
| 10075 | |
| 10076 | |
| 10077 | |
| 10078 | |
| 10079 | if (getLangOpts().CPlusPlus20 && !inTemplateInstantiation()) { |
| 10080 | llvm::SmallVector<FunctionDecl *, 4> DefaultedSpaceships; |
| 10081 | findImplicitlyDeclaredEqualityComparisons(Context, ClassDecl, |
| 10082 | DefaultedSpaceships); |
| 10083 | for (auto *FD : DefaultedSpaceships) |
| 10084 | DeclareImplicitEqualityComparison(ClassDecl, FD); |
| 10085 | } |
| 10086 | } |
| 10087 | |
| 10088 | unsigned |
| 10089 | Sema::ActOnReenterTemplateScope(Decl *D, |
| 10090 | llvm::function_ref<Scope *()> EnterScope) { |
| 10091 | if (!D) |
| 10092 | return 0; |
| 10093 | AdjustDeclIfTemplate(D); |
| 10094 | |
| 10095 | |
| 10096 | |
| 10097 | SmallVector<TemplateParameterList *, 4> ParameterLists; |
| 10098 | DeclContext *LookupDC = dyn_cast<DeclContext>(D); |
| 10099 | |
| 10100 | if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 10101 | for (unsigned i = 0; i < DD->getNumTemplateParameterLists(); ++i) |
| 10102 | ParameterLists.push_back(DD->getTemplateParameterList(i)); |
| 10103 | |
| 10104 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 10105 | if (FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
| 10106 | ParameterLists.push_back(FTD->getTemplateParameters()); |
| 10107 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 10108 | LookupDC = VD->getDeclContext(); |
| 10109 | |
| 10110 | if (VarTemplateDecl *VTD = VD->getDescribedVarTemplate()) |
| 10111 | ParameterLists.push_back(VTD->getTemplateParameters()); |
| 10112 | else if (auto *PSD = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) |
| 10113 | ParameterLists.push_back(PSD->getTemplateParameters()); |
| 10114 | } |
| 10115 | } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 10116 | for (unsigned i = 0; i < TD->getNumTemplateParameterLists(); ++i) |
| 10117 | ParameterLists.push_back(TD->getTemplateParameterList(i)); |
| 10118 | |
| 10119 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD)) { |
| 10120 | if (ClassTemplateDecl *CTD = RD->getDescribedClassTemplate()) |
| 10121 | ParameterLists.push_back(CTD->getTemplateParameters()); |
| 10122 | else if (auto *PSD = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 10123 | ParameterLists.push_back(PSD->getTemplateParameters()); |
| 10124 | } |
| 10125 | } |
| 10126 | |
| 10127 | |
| 10128 | unsigned Count = 0; |
| 10129 | Scope *InnermostTemplateScope = nullptr; |
| 10130 | for (TemplateParameterList *Params : ParameterLists) { |
| 10131 | |
| 10132 | |
| 10133 | if (Params->size() == 0) |
| 10134 | continue; |
| 10135 | |
| 10136 | InnermostTemplateScope = EnterScope(); |
| 10137 | for (NamedDecl *Param : *Params) { |
| 10138 | if (Param->getDeclName()) { |
| 10139 | InnermostTemplateScope->AddDecl(Param); |
| 10140 | IdResolver.AddDecl(Param); |
| 10141 | } |
| 10142 | } |
| 10143 | ++Count; |
| 10144 | } |
| 10145 | |
| 10146 | |
| 10147 | if (InnermostTemplateScope) { |
| 10148 | assert(LookupDC && "no enclosing DeclContext for template lookup"); |
| 10149 | EnterTemplatedContext(InnermostTemplateScope, LookupDC); |
| 10150 | } |
| 10151 | |
| 10152 | return Count; |
| 10153 | } |
| 10154 | |
| 10155 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
| 10156 | if (!RecordD) return; |
| 10157 | AdjustDeclIfTemplate(RecordD); |
| 10158 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
| 10159 | PushDeclContext(S, Record); |
| 10160 | } |
| 10161 | |
| 10162 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
| 10163 | if (!RecordD) return; |
| 10164 | PopDeclContext(); |
| 10165 | } |
| 10166 | |
| 10167 | |
| 10168 | |
| 10169 | |
| 10170 | void Sema::ActOnReenterCXXMethodParameter(Scope *S, ParmVarDecl *Param) { |
| 10171 | if (!Param) |
| 10172 | return; |
| 10173 | |
| 10174 | S->AddDecl(Param); |
| 10175 | if (Param->getDeclName()) |
| 10176 | IdResolver.AddDecl(Param); |
| 10177 | } |
| 10178 | |
| 10179 | |
| 10180 | |
| 10181 | |
| 10182 | |
| 10183 | |
| 10184 | |
| 10185 | |
| 10186 | |
| 10187 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
| 10188 | } |
| 10189 | |
| 10190 | |
| 10191 | |
| 10192 | |
| 10193 | |
| 10194 | |
| 10195 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
| 10196 | if (!ParamD) |
| 10197 | return; |
| 10198 | |
| 10199 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
| 10200 | |
| 10201 | S->AddDecl(Param); |
| 10202 | if (Param->getDeclName()) |
| 10203 | IdResolver.AddDecl(Param); |
| 10204 | } |
| 10205 | |
| 10206 | |
| 10207 | |
| 10208 | |
| 10209 | |
| 10210 | |
| 10211 | |
| 10212 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
| 10213 | if (!MethodD) |
| 10214 | return; |
| 10215 | |
| 10216 | AdjustDeclIfTemplate(MethodD); |
| 10217 | |
| 10218 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
| 10219 | |
| 10220 | |
| 10221 | |
| 10222 | |
| 10223 | |
| 10224 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 10225 | CheckConstructor(Constructor); |
| 10226 | |
| 10227 | |
| 10228 | if (!Method->isInvalidDecl()) |
| 10229 | CheckCXXDefaultArguments(Method); |
| 10230 | } |
| 10231 | |
| 10232 | |
| 10233 | |
| 10234 | static void checkMethodTypeQualifiers(Sema &S, Declarator &D, unsigned DiagID) { |
| 10235 | const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
| 10236 | if (FTI.hasMethodTypeQualifiers() && !D.isInvalidType()) { |
| 10237 | bool DiagOccured = false; |
| 10238 | FTI.MethodQualifiers->forEachQualifier( |
| 10239 | [DiagID, &S, &DiagOccured](DeclSpec::TQ, StringRef QualName, |
| 10240 | SourceLocation SL) { |
| 10241 | |
| 10242 | |
| 10243 | |
| 10244 | |
| 10245 | S.Diag(SL, DiagID) << QualName << SourceRange(SL); |
| 10246 | DiagOccured = true; |
| 10247 | }); |
| 10248 | if (DiagOccured) |
| 10249 | D.setInvalidType(); |
| 10250 | } |
| 10251 | } |
| 10252 | |
| 10253 | |
| 10254 | |
| 10255 | |
| 10256 | |
| 10257 | |
| 10258 | |
| 10259 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
| 10260 | StorageClass &SC) { |
| 10261 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
| 10262 | |
| 10263 | |
| 10264 | |
| 10265 | |
| 10266 | |
| 10267 | |
| 10268 | if (isVirtual) { |
| 10269 | if (!D.isInvalidType()) |
| 10270 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 10271 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 10272 | << SourceRange(D.getIdentifierLoc()); |
| 10273 | D.setInvalidType(); |
| 10274 | } |
| 10275 | if (SC == SC_Static) { |
| 10276 | if (!D.isInvalidType()) |
| 10277 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 10278 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 10279 | << SourceRange(D.getIdentifierLoc()); |
| 10280 | D.setInvalidType(); |
| 10281 | SC = SC_None; |
| 10282 | } |
| 10283 | |
| 10284 | if (unsigned TypeQuals = D.getDeclSpec().getTypeQualifiers()) { |
| 10285 | diagnoseIgnoredQualifiers( |
| 10286 | diag::err_constructor_return_type, TypeQuals, SourceLocation(), |
| 10287 | D.getDeclSpec().getConstSpecLoc(), D.getDeclSpec().getVolatileSpecLoc(), |
| 10288 | D.getDeclSpec().getRestrictSpecLoc(), |
| 10289 | D.getDeclSpec().getAtomicSpecLoc()); |
| 10290 | D.setInvalidType(); |
| 10291 | } |
| 10292 | |
| 10293 | checkMethodTypeQualifiers(*this, D, diag::err_invalid_qualified_constructor); |
| 10294 | |
| 10295 | |
| 10296 | |
| 10297 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
| 10298 | if (FTI.hasRefQualifier()) { |
| 10299 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 10300 | << FTI.RefQualifierIsLValueRef |
| 10301 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 10302 | D.setInvalidType(); |
| 10303 | } |
| 10304 | |
| 10305 | |
| 10306 | |
| 10307 | |
| 10308 | const FunctionProtoType *Proto = R->castAs<FunctionProtoType>(); |
| 10309 | if (Proto->getReturnType() == Context.VoidTy && !D.isInvalidType()) |
| 10310 | return R; |
| 10311 | |
| 10312 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 10313 | EPI.TypeQuals = Qualifiers(); |
| 10314 | EPI.RefQualifier = RQ_None; |
| 10315 | |
| 10316 | return Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(), EPI); |
| 10317 | } |
| 10318 | |
| 10319 | |
| 10320 | |
| 10321 | |
| 10322 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
| 10323 | CXXRecordDecl *ClassDecl |
| 10324 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 10325 | if (!ClassDecl) |
| 10326 | return Constructor->setInvalidDecl(); |
| 10327 | |
| 10328 | |
| 10329 | |
| 10330 | |
| 10331 | |
| 10332 | |
| 10333 | if (!Constructor->isInvalidDecl() && |
| 10334 | Constructor->hasOneParamOrDefaultArgs() && |
| 10335 | Constructor->getTemplateSpecializationKind() != |
| 10336 | TSK_ImplicitInstantiation) { |
| 10337 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 10338 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 10339 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
| 10340 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
| 10341 | const char *ConstRef |
| 10342 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 10343 | : " const &"; |
| 10344 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
| 10345 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
| 10346 | |
| 10347 | |
| 10348 | |
| 10349 | Constructor->setInvalidDecl(); |
| 10350 | } |
| 10351 | } |
| 10352 | } |
| 10353 | |
| 10354 | |
| 10355 | |
| 10356 | |
| 10357 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
| 10358 | CXXRecordDecl *RD = Destructor->getParent(); |
| 10359 | |
| 10360 | if (!Destructor->getOperatorDelete() && Destructor->isVirtual()) { |
| 10361 | SourceLocation Loc; |
| 10362 | |
| 10363 | if (!Destructor->isImplicit()) |
| 10364 | Loc = Destructor->getLocation(); |
| 10365 | else |
| 10366 | Loc = RD->getLocation(); |
| 10367 | |
| 10368 | |
| 10369 | if (FunctionDecl *OperatorDelete = |
| 10370 | FindDeallocationFunctionForDestructor(Loc, RD)) { |
| 10371 | Expr *ThisArg = nullptr; |
| 10372 | |
| 10373 | |
| 10374 | |
| 10375 | |
| 10376 | if (OperatorDelete->isDestroyingOperatorDelete()) { |
| 10377 | QualType ParamType = OperatorDelete->getParamDecl(0)->getType(); |
| 10378 | if (!declaresSameEntity(ParamType->getAsCXXRecordDecl(), RD)) { |
| 10379 | |
| 10380 | |
| 10381 | |
| 10382 | ContextRAII SwitchContext(*this, Destructor); |
| 10383 | ExprResult This = |
| 10384 | ActOnCXXThis(OperatorDelete->getParamDecl(0)->getLocation()); |
| 10385 | assert(!This.isInvalid() && "couldn't form 'this' expr in dtor?"); |
| 10386 | This = PerformImplicitConversion(This.get(), ParamType, AA_Passing); |
| 10387 | if (This.isInvalid()) { |
| 10388 | |
| 10389 | |
| 10390 | Diag(Loc, diag::note_implicit_delete_this_in_destructor_here); |
| 10391 | return true; |
| 10392 | } |
| 10393 | ThisArg = This.get(); |
| 10394 | } |
| 10395 | } |
| 10396 | |
| 10397 | DiagnoseUseOfDecl(OperatorDelete, Loc); |
| 10398 | MarkFunctionReferenced(Loc, OperatorDelete); |
| 10399 | Destructor->setOperatorDelete(OperatorDelete, ThisArg); |
| 10400 | } |
| 10401 | } |
| 10402 | |
| 10403 | return false; |
| 10404 | } |
| 10405 | |
| 10406 | |
| 10407 | |
| 10408 | |
| 10409 | |
| 10410 | |
| 10411 | |
| 10412 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
| 10413 | StorageClass& SC) { |
| 10414 | |
| 10415 | |
| 10416 | |
| 10417 | |
| 10418 | |
| 10419 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
| 10420 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
| 10421 | Diag(D.getIdentifierLoc(), diag::ext_destructor_typedef_name) |
| 10422 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
| 10423 | else if (const TemplateSpecializationType *TST = |
| 10424 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 10425 | if (TST->isTypeAlias()) |
| 10426 | Diag(D.getIdentifierLoc(), diag::ext_destructor_typedef_name) |
| 10427 | << DeclaratorType << 1; |
| 10428 | |
| 10429 | |
| 10430 | |
| 10431 | |
| 10432 | |
| 10433 | |
| 10434 | |
| 10435 | |
| 10436 | |
| 10437 | if (SC == SC_Static) { |
| 10438 | if (!D.isInvalidType()) |
| 10439 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 10440 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 10441 | << SourceRange(D.getIdentifierLoc()) |
| 10442 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 10443 | |
| 10444 | SC = SC_None; |
| 10445 | } |
| 10446 | if (!D.isInvalidType()) { |
| 10447 | |
| 10448 | |
| 10449 | |
| 10450 | |
| 10451 | |
| 10452 | |
| 10453 | |
| 10454 | |
| 10455 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 10456 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 10457 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 10458 | << SourceRange(D.getIdentifierLoc()); |
| 10459 | else if (unsigned TypeQuals = D.getDeclSpec().getTypeQualifiers()) { |
| 10460 | diagnoseIgnoredQualifiers(diag::err_destructor_return_type, TypeQuals, |
| 10461 | SourceLocation(), |
| 10462 | D.getDeclSpec().getConstSpecLoc(), |
| 10463 | D.getDeclSpec().getVolatileSpecLoc(), |
| 10464 | D.getDeclSpec().getRestrictSpecLoc(), |
| 10465 | D.getDeclSpec().getAtomicSpecLoc()); |
| 10466 | D.setInvalidType(); |
| 10467 | } |
| 10468 | } |
| 10469 | |
| 10470 | checkMethodTypeQualifiers(*this, D, diag::err_invalid_qualified_destructor); |
| 10471 | |
| 10472 | |
| 10473 | |
| 10474 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
| 10475 | if (FTI.hasRefQualifier()) { |
| 10476 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 10477 | << FTI.RefQualifierIsLValueRef |
| 10478 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 10479 | D.setInvalidType(); |
| 10480 | } |
| 10481 | |
| 10482 | |
| 10483 | if (FTIHasNonVoidParameters(FTI)) { |
| 10484 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 10485 | |
| 10486 | |
| 10487 | FTI.freeParams(); |
| 10488 | D.setInvalidType(); |
| 10489 | } |
| 10490 | |
| 10491 | |
| 10492 | if (FTI.isVariadic) { |
| 10493 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
| 10494 | D.setInvalidType(); |
| 10495 | } |
| 10496 | |
| 10497 | |
| 10498 | |
| 10499 | |
| 10500 | |
| 10501 | if (!D.isInvalidType()) |
| 10502 | return R; |
| 10503 | |
| 10504 | const FunctionProtoType *Proto = R->castAs<FunctionProtoType>(); |
| 10505 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 10506 | EPI.Variadic = false; |
| 10507 | EPI.TypeQuals = Qualifiers(); |
| 10508 | EPI.RefQualifier = RQ_None; |
| 10509 | return Context.getFunctionType(Context.VoidTy, None, EPI); |
| 10510 | } |
| 10511 | |
| 10512 | static void extendLeft(SourceRange &R, SourceRange Before) { |
| 10513 | if (Before.isInvalid()) |
| 10514 | return; |
| 10515 | R.setBegin(Before.getBegin()); |
| 10516 | if (R.getEnd().isInvalid()) |
| 10517 | R.setEnd(Before.getEnd()); |
| 10518 | } |
| 10519 | |
| 10520 | static void extendRight(SourceRange &R, SourceRange After) { |
| 10521 | if (After.isInvalid()) |
| 10522 | return; |
| 10523 | if (R.getBegin().isInvalid()) |
| 10524 | R.setBegin(After.getBegin()); |
| 10525 | R.setEnd(After.getEnd()); |
| 10526 | } |
| 10527 | |
| 10528 | |
| 10529 | |
| 10530 | |
| 10531 | |
| 10532 | |
| 10533 | |
| 10534 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
| 10535 | StorageClass& SC) { |
| 10536 | |
| 10537 | |
| 10538 | |
| 10539 | |
| 10540 | if (SC == SC_Static) { |
| 10541 | if (!D.isInvalidType()) |
| 10542 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 10543 | << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 10544 | << D.getName().getSourceRange(); |
| 10545 | D.setInvalidType(); |
| 10546 | SC = SC_None; |
| 10547 | } |
| 10548 | |
| 10549 | TypeSourceInfo *ConvTSI = nullptr; |
| 10550 | QualType ConvType = |
| 10551 | GetTypeFromParser(D.getName().ConversionFunctionId, &ConvTSI); |
| 10552 | |
| 10553 | const DeclSpec &DS = D.getDeclSpec(); |
| 10554 | if (DS.hasTypeSpecifier() && !D.isInvalidType()) { |
| 10555 | |
| 10556 | |
| 10557 | |
| 10558 | |
| 10559 | |
| 10560 | |
| 10561 | |
| 10562 | |
| 10563 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 10564 | << SourceRange(DS.getTypeSpecTypeLoc()) |
| 10565 | << SourceRange(D.getIdentifierLoc()); |
| 10566 | D.setInvalidType(); |
| 10567 | } else if (DS.getTypeQualifiers() && !D.isInvalidType()) { |
| 10568 | |
| 10569 | |
| 10570 | |
| 10571 | |
| 10572 | |
| 10573 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 10574 | << SourceRange(D.getIdentifierLoc()) << 0; |
| 10575 | D.setInvalidType(); |
| 10576 | } |
| 10577 | |
| 10578 | const auto *Proto = R->castAs<FunctionProtoType>(); |
| 10579 | |
| 10580 | |
| 10581 | if (Proto->getNumParams() > 0) { |
| 10582 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 10583 | |
| 10584 | |
| 10585 | D.getFunctionTypeInfo().freeParams(); |
| 10586 | D.setInvalidType(); |
| 10587 | } else if (Proto->isVariadic()) { |
| 10588 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
| 10589 | D.setInvalidType(); |
| 10590 | } |
| 10591 | |
| 10592 | |
| 10593 | |
| 10594 | if (Proto->getReturnType() != ConvType) { |
| 10595 | bool NeedsTypedef = false; |
| 10596 | SourceRange Before, After; |
| 10597 | |
| 10598 | |
| 10599 | bool PastFunctionChunk = false; |
| 10600 | for (auto &Chunk : D.type_objects()) { |
| 10601 | switch (Chunk.Kind) { |
| 10602 | case DeclaratorChunk::Function: |
| 10603 | if (!PastFunctionChunk) { |
| 10604 | if (Chunk.Fun.HasTrailingReturnType) { |
| 10605 | TypeSourceInfo *TRT = nullptr; |
| 10606 | GetTypeFromParser(Chunk.Fun.getTrailingReturnType(), &TRT); |
| 10607 | if (TRT) extendRight(After, TRT->getTypeLoc().getSourceRange()); |
| 10608 | } |
| 10609 | PastFunctionChunk = true; |
| 10610 | break; |
| 10611 | } |
| 10612 | LLVM_FALLTHROUGH; |
| 10613 | case DeclaratorChunk::Array: |
| 10614 | NeedsTypedef = true; |
| 10615 | extendRight(After, Chunk.getSourceRange()); |
| 10616 | break; |
| 10617 | |
| 10618 | case DeclaratorChunk::Pointer: |
| 10619 | case DeclaratorChunk::BlockPointer: |
| 10620 | case DeclaratorChunk::Reference: |
| 10621 | case DeclaratorChunk::MemberPointer: |
| 10622 | case DeclaratorChunk::Pipe: |
| 10623 | extendLeft(Before, Chunk.getSourceRange()); |
| 10624 | break; |
| 10625 | |
| 10626 | case DeclaratorChunk::Paren: |
| 10627 | extendLeft(Before, Chunk.Loc); |
| 10628 | extendRight(After, Chunk.EndLoc); |
| 10629 | break; |
| 10630 | } |
| 10631 | } |
| 10632 | |
| 10633 | SourceLocation Loc = Before.isValid() ? Before.getBegin() : |
| 10634 | After.isValid() ? After.getBegin() : |
| 10635 | D.getIdentifierLoc(); |
| 10636 | auto &&DB = Diag(Loc, diag::err_conv_function_with_complex_decl); |
| 10637 | DB << Before << After; |
| 10638 | |
| 10639 | if (!NeedsTypedef) { |
| 10640 | DB << 0; |
| 10641 | |
| 10642 | |
| 10643 | if (After.isInvalid() && ConvTSI) { |
| 10644 | SourceLocation InsertLoc = |
| 10645 | getLocForEndOfToken(ConvTSI->getTypeLoc().getEndLoc()); |
| 10646 | DB << FixItHint::CreateInsertion(InsertLoc, " ") |
| 10647 | << FixItHint::CreateInsertionFromRange( |
| 10648 | InsertLoc, CharSourceRange::getTokenRange(Before)) |
| 10649 | << FixItHint::CreateRemoval(Before); |
| 10650 | } |
| 10651 | } else if (!Proto->getReturnType()->isDependentType()) { |
| 10652 | DB << 1 << Proto->getReturnType(); |
| 10653 | } else if (getLangOpts().CPlusPlus11) { |
| 10654 | DB << 2 << Proto->getReturnType(); |
| 10655 | } else { |
| 10656 | DB << 3; |
| 10657 | } |
| 10658 | |
| 10659 | |
| 10660 | |
| 10661 | |
| 10662 | |
| 10663 | |
| 10664 | |
| 10665 | ConvType = Proto->getReturnType(); |
| 10666 | } |
| 10667 | |
| 10668 | |
| 10669 | |
| 10670 | |
| 10671 | if (ConvType->isArrayType()) { |
| 10672 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 10673 | ConvType = Context.getPointerType(ConvType); |
| 10674 | D.setInvalidType(); |
| 10675 | } else if (ConvType->isFunctionType()) { |
| 10676 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 10677 | ConvType = Context.getPointerType(ConvType); |
| 10678 | D.setInvalidType(); |
| 10679 | } |
| 10680 | |
| 10681 | |
| 10682 | |
| 10683 | |
| 10684 | if (D.isInvalidType()) |
| 10685 | R = Context.getFunctionType(ConvType, None, Proto->getExtProtoInfo()); |
| 10686 | |
| 10687 | |
| 10688 | if (DS.hasExplicitSpecifier() && !getLangOpts().CPlusPlus20) |
| 10689 | Diag(DS.getExplicitSpecLoc(), |
| 10690 | getLangOpts().CPlusPlus11 |
| 10691 | ? diag::warn_cxx98_compat_explicit_conversion_functions |
| 10692 | : diag::ext_explicit_conversion_functions) |
| 10693 | << SourceRange(DS.getExplicitSpecRange()); |
| 10694 | } |
| 10695 | |
| 10696 | |
| 10697 | |
| 10698 | |
| 10699 | |
| 10700 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
| 10701 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 10702 | |
| 10703 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
| 10704 | |
| 10705 | |
| 10706 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
| 10707 | |
| 10708 | |
| 10709 | |
| 10710 | |
| 10711 | |
| 10712 | |
| 10713 | QualType ClassType |
| 10714 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 10715 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
| 10716 | ConvType = ConvTypeRef->getPointeeType(); |
| 10717 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 10718 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
| 10719 | ; |
| 10720 | else if (Conversion->size_overridden_methods() != 0) |
| 10721 | ; |
| 10722 | else if (ConvType->isRecordType()) { |
| 10723 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 10724 | if (ConvType == ClassType) |
| 10725 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
| 10726 | << ClassType; |
| 10727 | else if (IsDerivedFrom(Conversion->getLocation(), ClassType, ConvType)) |
| 10728 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
| 10729 | << ClassType << ConvType; |
| 10730 | } else if (ConvType->isVoidType()) { |
| 10731 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
| 10732 | << ClassType << ConvType; |
| 10733 | } |
| 10734 | |
| 10735 | if (FunctionTemplateDecl *ConversionTemplate |
| 10736 | = Conversion->getDescribedFunctionTemplate()) |
| 10737 | return ConversionTemplate; |
| 10738 | |
| 10739 | return Conversion; |
| 10740 | } |
| 10741 | |
| 10742 | namespace { |
| 10743 | |
| 10744 | |
| 10745 | struct BadSpecifierDiagnoser { |
| 10746 | BadSpecifierDiagnoser(Sema &S, SourceLocation Loc, unsigned DiagID) |
| 10747 | : S(S), Diagnostic(S.Diag(Loc, DiagID)) {} |
| 10748 | ~BadSpecifierDiagnoser() { |
| 10749 | Diagnostic << Specifiers; |
| 10750 | } |
| 10751 | |
| 10752 | template<typename T> void check(SourceLocation SpecLoc, T Spec) { |
| 10753 | return check(SpecLoc, DeclSpec::getSpecifierName(Spec)); |
| 10754 | } |
| 10755 | void check(SourceLocation SpecLoc, DeclSpec::TST Spec) { |
| 10756 | return check(SpecLoc, |
| 10757 | DeclSpec::getSpecifierName(Spec, S.getPrintingPolicy())); |
| 10758 | } |
| 10759 | void check(SourceLocation SpecLoc, const char *Spec) { |
| 10760 | if (SpecLoc.isInvalid()) return; |
| 10761 | Diagnostic << SourceRange(SpecLoc, SpecLoc); |
| 10762 | if (!Specifiers.empty()) Specifiers += " "; |
| 10763 | Specifiers += Spec; |
| 10764 | } |
| 10765 | |
| 10766 | Sema &S; |
| 10767 | Sema::SemaDiagnosticBuilder Diagnostic; |
| 10768 | std::string Specifiers; |
| 10769 | }; |
| 10770 | } |
| 10771 | |
| 10772 | |
| 10773 | |
| 10774 | |
| 10775 | |
| 10776 | void Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R, |
| 10777 | StorageClass &SC) { |
| 10778 | TemplateName GuidedTemplate = D.getName().TemplateName.get().get(); |
| 10779 | TemplateDecl *GuidedTemplateDecl = GuidedTemplate.getAsTemplateDecl(); |
| 10780 | assert(GuidedTemplateDecl && "missing template decl for deduction guide"); |
| 10781 | |
| 10782 | |
| 10783 | |
| 10784 | |
| 10785 | if (!CurContext->getRedeclContext()->Equals( |
| 10786 | GuidedTemplateDecl->getDeclContext()->getRedeclContext())) { |
| 10787 | Diag(D.getIdentifierLoc(), diag::err_deduction_guide_wrong_scope) |
| 10788 | << GuidedTemplateDecl; |
| 10789 | Diag(GuidedTemplateDecl->getLocation(), diag::note_template_decl_here); |
| 10790 | } |
| 10791 | |
| 10792 | auto &DS = D.getMutableDeclSpec(); |
| 10793 | |
| 10794 | if (DS.hasTypeSpecifier() || DS.getTypeQualifiers() || |
| 10795 | DS.getStorageClassSpecLoc().isValid() || DS.isInlineSpecified() || |
| 10796 | DS.isNoreturnSpecified() || DS.hasConstexprSpecifier()) { |
| 10797 | BadSpecifierDiagnoser Diagnoser( |
| 10798 | *this, D.getIdentifierLoc(), |
| 10799 | diag::err_deduction_guide_invalid_specifier); |
| 10800 | |
| 10801 | Diagnoser.check(DS.getStorageClassSpecLoc(), DS.getStorageClassSpec()); |
| 10802 | DS.ClearStorageClassSpecs(); |
| 10803 | SC = SC_None; |
| 10804 | |
| 10805 | |
| 10806 | Diagnoser.check(DS.getInlineSpecLoc(), "inline"); |
| 10807 | Diagnoser.check(DS.getNoreturnSpecLoc(), "_Noreturn"); |
| 10808 | Diagnoser.check(DS.getConstexprSpecLoc(), "constexpr"); |
| 10809 | DS.ClearConstexprSpec(); |
| 10810 | |
| 10811 | Diagnoser.check(DS.getConstSpecLoc(), "const"); |
| 10812 | Diagnoser.check(DS.getRestrictSpecLoc(), "__restrict"); |
| 10813 | Diagnoser.check(DS.getVolatileSpecLoc(), "volatile"); |
| 10814 | Diagnoser.check(DS.getAtomicSpecLoc(), "_Atomic"); |
| 10815 | Diagnoser.check(DS.getUnalignedSpecLoc(), "__unaligned"); |
| 10816 | DS.ClearTypeQualifiers(); |
| 10817 | |
| 10818 | Diagnoser.check(DS.getTypeSpecComplexLoc(), DS.getTypeSpecComplex()); |
| 10819 | Diagnoser.check(DS.getTypeSpecSignLoc(), DS.getTypeSpecSign()); |
| 10820 | Diagnoser.check(DS.getTypeSpecWidthLoc(), DS.getTypeSpecWidth()); |
| 10821 | Diagnoser.check(DS.getTypeSpecTypeLoc(), DS.getTypeSpecType()); |
| 10822 | DS.ClearTypeSpecType(); |
| 10823 | } |
| 10824 | |
| 10825 | if (D.isInvalidType()) |
| 10826 | return; |
| 10827 | |
| 10828 | |
| 10829 | bool FoundFunction = false; |
| 10830 | for (const DeclaratorChunk &Chunk : llvm::reverse(D.type_objects())) { |
| 10831 | if (Chunk.Kind == DeclaratorChunk::Paren) |
| 10832 | continue; |
| 10833 | if (Chunk.Kind != DeclaratorChunk::Function || FoundFunction) { |
| 10834 | Diag(D.getDeclSpec().getBeginLoc(), |
| 10835 | diag::err_deduction_guide_with_complex_decl) |
| 10836 | << D.getSourceRange(); |
| 10837 | break; |
| 10838 | } |
| 10839 | if (!Chunk.Fun.hasTrailingReturnType()) { |
| 10840 | Diag(D.getName().getBeginLoc(), |
| 10841 | diag::err_deduction_guide_no_trailing_return_type); |
| 10842 | break; |
| 10843 | } |
| 10844 | |
| 10845 | |
| 10846 | |
| 10847 | ParsedType TrailingReturnType = Chunk.Fun.getTrailingReturnType(); |
| 10848 | TypeSourceInfo *TSI = nullptr; |
| 10849 | QualType RetTy = GetTypeFromParser(TrailingReturnType, &TSI); |
| 10850 | assert(TSI && "deduction guide has valid type but invalid return type?"); |
| 10851 | bool AcceptableReturnType = false; |
| 10852 | bool MightInstantiateToSpecialization = false; |
| 10853 | if (auto RetTST = |
| 10854 | TSI->getTypeLoc().getAs<TemplateSpecializationTypeLoc>()) { |
| 10855 | TemplateName SpecifiedName = RetTST.getTypePtr()->getTemplateName(); |
| 10856 | bool TemplateMatches = |
| 10857 | Context.hasSameTemplateName(SpecifiedName, GuidedTemplate); |
| 10858 | if (SpecifiedName.getKind() == TemplateName::Template && TemplateMatches) |
| 10859 | AcceptableReturnType = true; |
| 10860 | else { |
| 10861 | |
| 10862 | |
| 10863 | auto *TD = SpecifiedName.getAsTemplateDecl(); |
| 10864 | MightInstantiateToSpecialization = !(TD && isa<ClassTemplateDecl>(TD) && |
| 10865 | !TemplateMatches); |
| 10866 | } |
| 10867 | } else if (!RetTy.hasQualifiers() && RetTy->isDependentType()) { |
| 10868 | MightInstantiateToSpecialization = true; |
| 10869 | } |
| 10870 | |
| 10871 | if (!AcceptableReturnType) { |
| 10872 | Diag(TSI->getTypeLoc().getBeginLoc(), |
| 10873 | diag::err_deduction_guide_bad_trailing_return_type) |
| 10874 | << GuidedTemplate << TSI->getType() |
| 10875 | << MightInstantiateToSpecialization |
| 10876 | << TSI->getTypeLoc().getSourceRange(); |
| 10877 | } |
| 10878 | |
| 10879 | |
| 10880 | |
| 10881 | FoundFunction = true; |
| 10882 | } |
| 10883 | |
| 10884 | if (D.isFunctionDefinition()) |
| 10885 | Diag(D.getIdentifierLoc(), diag::err_deduction_guide_defines_function); |
| 10886 | } |
| 10887 | |
| 10888 | |
| 10889 | |
| 10890 | |
| 10891 | |
| 10892 | |
| 10893 | |
| 10894 | static void DiagnoseNamespaceInlineMismatch(Sema &S, SourceLocation KeywordLoc, |
| 10895 | SourceLocation Loc, |
| 10896 | IdentifierInfo *II, bool *IsInline, |
| 10897 | NamespaceDecl *PrevNS) { |
| 10898 | assert(*IsInline != PrevNS->isInline()); |
| 10899 | |
| 10900 | if (PrevNS->isInline()) |
| 10901 | |
| 10902 | |
| 10903 | S.Diag(Loc, diag::warn_inline_namespace_reopened_noninline) |
| 10904 | << FixItHint::CreateInsertion(KeywordLoc, "inline "); |
| 10905 | else |
| 10906 | S.Diag(Loc, diag::err_inline_namespace_mismatch); |
| 10907 | |
| 10908 | S.Diag(PrevNS->getLocation(), diag::note_previous_definition); |
| 10909 | *IsInline = PrevNS->isInline(); |
| 10910 | } |
| 10911 | |
| 10912 | |
| 10913 | |
| 10914 | Decl *Sema::ActOnStartNamespaceDef( |
| 10915 | Scope *NamespcScope, SourceLocation InlineLoc, SourceLocation NamespaceLoc, |
| 10916 | SourceLocation IdentLoc, IdentifierInfo *II, SourceLocation LBrace, |
| 10917 | const ParsedAttributesView &AttrList, UsingDirectiveDecl *&UD) { |
| 10918 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 10919 | |
| 10920 | SourceLocation Loc = II ? IdentLoc : LBrace; |
| 10921 | bool IsInline = InlineLoc.isValid(); |
| 10922 | bool IsInvalid = false; |
| 10923 | bool IsStd = false; |
| 10924 | bool AddToKnown = false; |
| 10925 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 10926 | |
| 10927 | NamespaceDecl *PrevNS = nullptr; |
| 10928 | if (II) { |
| 10929 | |
| 10930 | |
| 10931 | |
| 10932 | |
| 10933 | |
| 10934 | |
| 10935 | |
| 10936 | |
| 10937 | |
| 10938 | |
| 10939 | |
| 10940 | LookupResult R(*this, II, IdentLoc, LookupOrdinaryName, |
| 10941 | ForExternalRedeclaration); |
| 10942 | LookupQualifiedName(R, CurContext->getRedeclContext()); |
| 10943 | NamedDecl *PrevDecl = |
| 10944 | R.isSingleResult() ? R.getRepresentativeDecl() : nullptr; |
| 10945 | PrevNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl); |
| 10946 | |
| 10947 | if (PrevNS) { |
| 10948 | |
| 10949 | if (IsInline != PrevNS->isInline()) |
| 10950 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, Loc, II, |
| 10951 | &IsInline, PrevNS); |
| 10952 | } else if (PrevDecl) { |
| 10953 | |
| 10954 | Diag(Loc, diag::err_redefinition_different_kind) |
| 10955 | << II; |
| 10956 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 10957 | IsInvalid = true; |
| 10958 | |
| 10959 | } else if (II->isStr("std") && |
| 10960 | CurContext->getRedeclContext()->isTranslationUnit()) { |
| 10961 | |
| 10962 | |
| 10963 | PrevNS = getStdNamespace(); |
| 10964 | IsStd = true; |
| 10965 | AddToKnown = !IsInline; |
| 10966 | } else { |
| 10967 | |
| 10968 | AddToKnown = !IsInline; |
| 10969 | } |
| 10970 | } else { |
| 10971 | |
| 10972 | |
| 10973 | |
| 10974 | DeclContext *Parent = CurContext->getRedeclContext(); |
| 10975 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 10976 | PrevNS = TU->getAnonymousNamespace(); |
| 10977 | } else { |
| 10978 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 10979 | PrevNS = ND->getAnonymousNamespace(); |
| 10980 | } |
| 10981 | |
| 10982 | if (PrevNS && IsInline != PrevNS->isInline()) |
| 10983 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, NamespaceLoc, II, |
| 10984 | &IsInline, PrevNS); |
| 10985 | } |
| 10986 | |
| 10987 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, IsInline, |
| 10988 | StartLoc, Loc, II, PrevNS); |
| 10989 | if (IsInvalid) |
| 10990 | Namespc->setInvalidDecl(); |
| 10991 | |
| 10992 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 10993 | AddPragmaAttributes(DeclRegionScope, Namespc); |
| 10994 | |
| 10995 | |
| 10996 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
| 10997 | PushNamespaceVisibilityAttr(Attr, Loc); |
| 10998 | |
| 10999 | if (IsStd) |
| 11000 | StdNamespace = Namespc; |
| 11001 | if (AddToKnown) |
| 11002 | KnownNamespaces[Namespc] = false; |
| 11003 | |
| 11004 | if (II) { |
| 11005 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 11006 | } else { |
| 11007 | |
| 11008 | DeclContext *Parent = CurContext->getRedeclContext(); |
| 11009 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 11010 | TU->setAnonymousNamespace(Namespc); |
| 11011 | } else { |
| 11012 | cast<NamespaceDecl>(Parent)->setAnonymousNamespace(Namespc); |
| 11013 | } |
| 11014 | |
| 11015 | CurContext->addDecl(Namespc); |
| 11016 | |
| 11017 | |
| 11018 | |
| 11019 | |
| 11020 | |
| 11021 | |
| 11022 | |
| 11023 | |
| 11024 | |
| 11025 | |
| 11026 | |
| 11027 | |
| 11028 | |
| 11029 | |
| 11030 | |
| 11031 | |
| 11032 | |
| 11033 | if (!PrevNS) { |
| 11034 | UD = UsingDirectiveDecl::Create(Context, Parent, |
| 11035 | LBrace, |
| 11036 | SourceLocation(), |
| 11037 | NestedNameSpecifierLoc(), |
| 11038 | SourceLocation(), |
| 11039 | Namespc, |
| 11040 | Parent); |
| 11041 | UD->setImplicit(); |
| 11042 | Parent->addDecl(UD); |
| 11043 | } |
| 11044 | } |
| 11045 | |
| 11046 | ActOnDocumentableDecl(Namespc); |
| 11047 | |
| 11048 | |
| 11049 | |
| 11050 | |
| 11051 | |
| 11052 | |
| 11053 | PushDeclContext(NamespcScope, Namespc); |
| 11054 | return Namespc; |
| 11055 | } |
| 11056 | |
| 11057 | |
| 11058 | |
| 11059 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 11060 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 11061 | return AD->getNamespace(); |
| 11062 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 11063 | } |
| 11064 | |
| 11065 | |
| 11066 | |
| 11067 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
| 11068 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 11069 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 11070 | Namespc->setRBraceLoc(RBrace); |
| 11071 | PopDeclContext(); |
| 11072 | if (Namespc->hasAttr<VisibilityAttr>()) |
| 11073 | PopPragmaVisibility(true, RBrace); |
| 11074 | |
| 11075 | if (DeferredExportedNamespaces.erase(Namespc)) |
| 11076 | Dcl->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported); |
| 11077 | } |
| 11078 | |
| 11079 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 11080 | return cast_or_null<CXXRecordDecl>( |
| 11081 | StdBadAlloc.get(Context.getExternalSource())); |
| 11082 | } |
| 11083 | |
| 11084 | EnumDecl *Sema::getStdAlignValT() const { |
| 11085 | return cast_or_null<EnumDecl>(StdAlignValT.get(Context.getExternalSource())); |
| 11086 | } |
| 11087 | |
| 11088 | NamespaceDecl *Sema::getStdNamespace() const { |
| 11089 | return cast_or_null<NamespaceDecl>( |
| 11090 | StdNamespace.get(Context.getExternalSource())); |
| 11091 | } |
| 11092 | |
| 11093 | NamespaceDecl *Sema::lookupStdExperimentalNamespace() { |
| 11094 | if (!StdExperimentalNamespaceCache) { |
| 11095 | if (auto Std = getStdNamespace()) { |
| 11096 | LookupResult Result(*this, &PP.getIdentifierTable().get("experimental"), |
| 11097 | SourceLocation(), LookupNamespaceName); |
| 11098 | if (!LookupQualifiedName(Result, Std) || |
| 11099 | !(StdExperimentalNamespaceCache = |
| 11100 | Result.getAsSingle<NamespaceDecl>())) |
| 11101 | Result.suppressDiagnostics(); |
| 11102 | } |
| 11103 | } |
| 11104 | return StdExperimentalNamespaceCache; |
| 11105 | } |
| 11106 | |
| 11107 | namespace { |
| 11108 | |
| 11109 | enum UnsupportedSTLSelect { |
| 11110 | USS_InvalidMember, |
| 11111 | USS_MissingMember, |
| 11112 | USS_NonTrivial, |
| 11113 | USS_Other |
| 11114 | }; |
| 11115 | |
| 11116 | struct InvalidSTLDiagnoser { |
| 11117 | Sema &S; |
| 11118 | SourceLocation Loc; |
| 11119 | QualType TyForDiags; |
| 11120 | |
| 11121 | QualType operator()(UnsupportedSTLSelect Sel = USS_Other, StringRef Name = "", |
| 11122 | const VarDecl *VD = nullptr) { |
| 11123 | { |
| 11124 | auto D = S.Diag(Loc, diag::err_std_compare_type_not_supported) |
| 11125 | << TyForDiags << ((int)Sel); |
| 11126 | if (Sel == USS_InvalidMember || Sel == USS_MissingMember) { |
| 11127 | assert(!Name.empty()); |
| 11128 | D << Name; |
| 11129 | } |
| 11130 | } |
| 11131 | if (Sel == USS_InvalidMember) { |
| 11132 | S.Diag(VD->getLocation(), diag::note_var_declared_here) |
| 11133 | << VD << VD->getSourceRange(); |
| 11134 | } |
| 11135 | return QualType(); |
| 11136 | } |
| 11137 | }; |
| 11138 | } |
| 11139 | |
| 11140 | QualType Sema::CheckComparisonCategoryType(ComparisonCategoryType Kind, |
| 11141 | SourceLocation Loc, |
| 11142 | ComparisonCategoryUsage Usage) { |
| 11143 | assert(getLangOpts().CPlusPlus && |
| 11144 | "Looking for comparison category type outside of C++."); |
| 11145 | |
| 11146 | |
| 11147 | |
| 11148 | auto TyForDiags = [&](ComparisonCategoryInfo *Info) { |
| 11149 | auto *NNS = |
| 11150 | NestedNameSpecifier::Create(Context, nullptr, getStdNamespace()); |
| 11151 | return Context.getElaboratedType(ETK_None, NNS, Info->getType()); |
| 11152 | }; |
| 11153 | |
| 11154 | |
| 11155 | |
| 11156 | ComparisonCategoryInfo *Info = Context.CompCategories.lookupInfo(Kind); |
| 11157 | if (Info && FullyCheckedComparisonCategories[static_cast<unsigned>(Kind)]) { |
| 11158 | |
| 11159 | |
| 11160 | if (RequireCompleteType(Loc, TyForDiags(Info), diag::err_incomplete_type)) |
| 11161 | return QualType(); |
| 11162 | |
| 11163 | return Info->getType(); |
| 11164 | } |
| 11165 | |
| 11166 | |
| 11167 | if (!Info) { |
| 11168 | std::string NameForDiags = "std::"; |
| 11169 | NameForDiags += ComparisonCategories::getCategoryString(Kind); |
| 11170 | Diag(Loc, diag::err_implied_comparison_category_type_not_found) |
| 11171 | << NameForDiags << (int)Usage; |
| 11172 | return QualType(); |
| 11173 | } |
| 11174 | |
| 11175 | assert(Info->Kind == Kind); |
| 11176 | assert(Info->Record); |
| 11177 | |
| 11178 | |
| 11179 | |
| 11180 | if (Info->Record->hasDefinition()) |
| 11181 | Info->Record = Info->Record->getDefinition(); |
| 11182 | |
| 11183 | if (RequireCompleteType(Loc, TyForDiags(Info), diag::err_incomplete_type)) |
| 11184 | return QualType(); |
| 11185 | |
| 11186 | InvalidSTLDiagnoser UnsupportedSTLError{*this, Loc, TyForDiags(Info)}; |
| 11187 | |
| 11188 | if (!Info->Record->isTriviallyCopyable()) |
| 11189 | return UnsupportedSTLError(USS_NonTrivial); |
| 11190 | |
| 11191 | for (const CXXBaseSpecifier &BaseSpec : Info->Record->bases()) { |
| 11192 | CXXRecordDecl *Base = BaseSpec.getType()->getAsCXXRecordDecl(); |
| 11193 | |
| 11194 | if (Base->isEmpty()) |
| 11195 | continue; |
| 11196 | |
| 11197 | return UnsupportedSTLError(); |
| 11198 | } |
| 11199 | |
| 11200 | |
| 11201 | |
| 11202 | |
| 11203 | |
| 11204 | auto FIt = Info->Record->field_begin(), FEnd = Info->Record->field_end(); |
| 11205 | if (std::distance(FIt, FEnd) != 1 || |
| 11206 | !FIt->getType()->isIntegralOrEnumerationType()) { |
| 11207 | return UnsupportedSTLError(); |
| 11208 | } |
| 11209 | |
| 11210 | |
| 11211 | for (ComparisonCategoryResult CCR : |
| 11212 | ComparisonCategories::getPossibleResultsForType(Kind)) { |
| 11213 | StringRef MemName = ComparisonCategories::getResultString(CCR); |
| 11214 | ComparisonCategoryInfo::ValueInfo *ValInfo = Info->lookupValueInfo(CCR); |
| 11215 | |
| 11216 | if (!ValInfo) |
| 11217 | return UnsupportedSTLError(USS_MissingMember, MemName); |
| 11218 | |
| 11219 | VarDecl *VD = ValInfo->VD; |
| 11220 | assert(VD && "should not be null!"); |
| 11221 | |
| 11222 | |
| 11223 | |
| 11224 | |
| 11225 | if (!VD->isStaticDataMember() || |
| 11226 | !VD->isUsableInConstantExpressions(Context)) |
| 11227 | return UnsupportedSTLError(USS_InvalidMember, MemName, VD); |
| 11228 | |
| 11229 | |
| 11230 | |
| 11231 | |
| 11232 | if (!ValInfo->hasValidIntValue()) |
| 11233 | return UnsupportedSTLError(); |
| 11234 | |
| 11235 | MarkVariableReferenced(Loc, VD); |
| 11236 | } |
| 11237 | |
| 11238 | |
| 11239 | |
| 11240 | FullyCheckedComparisonCategories[static_cast<unsigned>(Kind)] = true; |
| 11241 | return Info->getType(); |
| 11242 | } |
| 11243 | |
| 11244 | |
| 11245 | |
| 11246 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
| 11247 | if (!StdNamespace) { |
| 11248 | |
| 11249 | StdNamespace = NamespaceDecl::Create(Context, |
| 11250 | Context.getTranslationUnitDecl(), |
| 11251 | false, |
| 11252 | SourceLocation(), SourceLocation(), |
| 11253 | &PP.getIdentifierTable().get("std"), |
| 11254 | nullptr); |
| 11255 | getStdNamespace()->setImplicit(true); |
| 11256 | } |
| 11257 | |
| 11258 | return getStdNamespace(); |
| 11259 | } |
| 11260 | |
| 11261 | bool Sema::isStdInitializerList(QualType Ty, QualType *Element) { |
| 11262 | assert(getLangOpts().CPlusPlus && |
| 11263 | "Looking for std::initializer_list outside of C++."); |
| 11264 | |
| 11265 | |
| 11266 | |
| 11267 | |
| 11268 | if (!StdNamespace) |
| 11269 | return false; |
| 11270 | |
| 11271 | ClassTemplateDecl *Template = nullptr; |
| 11272 | const TemplateArgument *Arguments = nullptr; |
| 11273 | |
| 11274 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 11275 | |
| 11276 | ClassTemplateSpecializationDecl *Specialization = |
| 11277 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 11278 | if (!Specialization) |
| 11279 | return false; |
| 11280 | |
| 11281 | Template = Specialization->getSpecializedTemplate(); |
| 11282 | Arguments = Specialization->getTemplateArgs().data(); |
| 11283 | } else if (const TemplateSpecializationType *TST = |
| 11284 | Ty->getAs<TemplateSpecializationType>()) { |
| 11285 | Template = dyn_cast_or_null<ClassTemplateDecl>( |
| 11286 | TST->getTemplateName().getAsTemplateDecl()); |
| 11287 | Arguments = TST->getArgs(); |
| 11288 | } |
| 11289 | if (!Template) |
| 11290 | return false; |
| 11291 | |
| 11292 | if (!StdInitializerList) { |
| 11293 | |
| 11294 | CXXRecordDecl *TemplateClass = Template->getTemplatedDecl(); |
| 11295 | if (TemplateClass->getIdentifier() != |
| 11296 | &PP.getIdentifierTable().get("initializer_list") || |
| 11297 | !getStdNamespace()->InEnclosingNamespaceSetOf( |
| 11298 | TemplateClass->getDeclContext())) |
| 11299 | return false; |
| 11300 | |
| 11301 | |
| 11302 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 11303 | if (Params->getMinRequiredArguments() != 1) |
| 11304 | return false; |
| 11305 | if (!isa<TemplateTypeParmDecl>(Params->getParam(0))) |
| 11306 | return false; |
| 11307 | |
| 11308 | |
| 11309 | StdInitializerList = Template; |
| 11310 | } |
| 11311 | |
| 11312 | if (Template->getCanonicalDecl() != StdInitializerList->getCanonicalDecl()) |
| 11313 | return false; |
| 11314 | |
| 11315 | |
| 11316 | if (Element) |
| 11317 | *Element = Arguments[0].getAsType(); |
| 11318 | return true; |
| 11319 | } |
| 11320 | |
| 11321 | static ClassTemplateDecl *LookupStdInitializerList(Sema &S, SourceLocation Loc){ |
| 11322 | NamespaceDecl *Std = S.getStdNamespace(); |
| 11323 | if (!Std) { |
| 11324 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 11325 | return nullptr; |
| 11326 | } |
| 11327 | |
| 11328 | LookupResult Result(S, &S.PP.getIdentifierTable().get("initializer_list"), |
| 11329 | Loc, Sema::LookupOrdinaryName); |
| 11330 | if (!S.LookupQualifiedName(Result, Std)) { |
| 11331 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 11332 | return nullptr; |
| 11333 | } |
| 11334 | ClassTemplateDecl *Template = Result.getAsSingle<ClassTemplateDecl>(); |
| 11335 | if (!Template) { |
| 11336 | Result.suppressDiagnostics(); |
| 11337 | |
| 11338 | NamedDecl *Found = *Result.begin(); |
| 11339 | S.Diag(Found->getLocation(), diag::err_malformed_std_initializer_list); |
| 11340 | return nullptr; |
| 11341 | } |
| 11342 | |
| 11343 | |
| 11344 | |
| 11345 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 11346 | if (Params->getMinRequiredArguments() != 1 || |
| 11347 | !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
| 11348 | S.Diag(Template->getLocation(), diag::err_malformed_std_initializer_list); |
| 11349 | return nullptr; |
| 11350 | } |
| 11351 | |
| 11352 | return Template; |
| 11353 | } |
| 11354 | |
| 11355 | QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) { |
| 11356 | if (!StdInitializerList) { |
| 11357 | StdInitializerList = LookupStdInitializerList(*this, Loc); |
| 11358 | if (!StdInitializerList) |
| 11359 | return QualType(); |
| 11360 | } |
| 11361 | |
| 11362 | TemplateArgumentListInfo Args(Loc, Loc); |
| 11363 | Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element), |
| 11364 | Context.getTrivialTypeSourceInfo(Element, |
| 11365 | Loc))); |
| 11366 | return Context.getCanonicalType( |
| 11367 | CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args)); |
| 11368 | } |
| 11369 | |
| 11370 | bool Sema::isInitListConstructor(const FunctionDecl *Ctor) { |
| 11371 | |
| 11372 | |
| 11373 | |
| 11374 | |
| 11375 | |
| 11376 | if (!Ctor->hasOneParamOrDefaultArgs()) |
| 11377 | return false; |
| 11378 | |
| 11379 | QualType ArgType = Ctor->getParamDecl(0)->getType(); |
| 11380 | if (const ReferenceType *RT = ArgType->getAs<ReferenceType>()) |
| 11381 | ArgType = RT->getPointeeType().getUnqualifiedType(); |
| 11382 | |
| 11383 | return isStdInitializerList(ArgType, nullptr); |
| 11384 | } |
| 11385 | |
| 11386 | |
| 11387 | |
| 11388 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 11389 | switch (CurContext->getDeclKind()) { |
| 11390 | case Decl::TranslationUnit: |
| 11391 | return true; |
| 11392 | case Decl::LinkageSpec: |
| 11393 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 11394 | default: |
| 11395 | return false; |
| 11396 | } |
| 11397 | } |
| 11398 | |
| 11399 | namespace { |
| 11400 | |
| 11401 | |
| 11402 | class NamespaceValidatorCCC final : public CorrectionCandidateCallback { |
| 11403 | public: |
| 11404 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 11405 | if (NamedDecl *ND = candidate.getCorrectionDecl()) |
| 11406 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 11407 | return false; |
| 11408 | } |
| 11409 | |
| 11410 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 11411 | return std::make_unique<NamespaceValidatorCCC>(*this); |
| 11412 | } |
| 11413 | }; |
| 11414 | |
| 11415 | } |
| 11416 | |
| 11417 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 11418 | CXXScopeSpec &SS, |
| 11419 | SourceLocation IdentLoc, |
| 11420 | IdentifierInfo *Ident) { |
| 11421 | R.clear(); |
| 11422 | NamespaceValidatorCCC CCC{}; |
| 11423 | if (TypoCorrection Corrected = |
| 11424 | S.CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), Sc, &SS, CCC, |
| 11425 | Sema::CTK_ErrorRecovery)) { |
| 11426 | if (DeclContext *DC = S.computeDeclContext(SS, false)) { |
| 11427 | std::string CorrectedStr(Corrected.getAsString(S.getLangOpts())); |
| 11428 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| 11429 | Ident->getName().equals(CorrectedStr); |
| 11430 | S.diagnoseTypo(Corrected, |
| 11431 | S.PDiag(diag::err_using_directive_member_suggest) |
| 11432 | << Ident << DC << DroppedSpecifier << SS.getRange(), |
| 11433 | S.PDiag(diag::note_namespace_defined_here)); |
| 11434 | } else { |
| 11435 | S.diagnoseTypo(Corrected, |
| 11436 | S.PDiag(diag::err_using_directive_suggest) << Ident, |
| 11437 | S.PDiag(diag::note_namespace_defined_here)); |
| 11438 | } |
| 11439 | R.addDecl(Corrected.getFoundDecl()); |
| 11440 | return true; |
| 11441 | } |
| 11442 | return false; |
| 11443 | } |
| 11444 | |
| 11445 | Decl *Sema::ActOnUsingDirective(Scope *S, SourceLocation UsingLoc, |
| 11446 | SourceLocation NamespcLoc, CXXScopeSpec &SS, |
| 11447 | SourceLocation IdentLoc, |
| 11448 | IdentifierInfo *NamespcName, |
| 11449 | const ParsedAttributesView &AttrList) { |
| 11450 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 11451 | assert(NamespcName && "Invalid NamespcName."); |
| 11452 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
| 11453 | |
| 11454 | |
| 11455 | while (S->isTemplateParamScope()) |
| 11456 | S = S->getParent(); |
| 11457 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
| 11458 | |
| 11459 | UsingDirectiveDecl *UDir = nullptr; |
| 11460 | NestedNameSpecifier *Qualifier = nullptr; |
| 11461 | if (SS.isSet()) |
| 11462 | Qualifier = SS.getScopeRep(); |
| 11463 | |
| 11464 | |
| 11465 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 11466 | LookupParsedName(R, S, &SS); |
| 11467 | if (R.isAmbiguous()) |
| 11468 | return nullptr; |
| 11469 | |
| 11470 | if (R.empty()) { |
| 11471 | R.clear(); |
| 11472 | |
| 11473 | |
| 11474 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 11475 | NamespcName->isStr("std")) { |
| 11476 | Diag(IdentLoc, diag::ext_using_undefined_std); |
| 11477 | R.addDecl(getOrCreateStdNamespace()); |
| 11478 | R.resolveKind(); |
| 11479 | } |
| 11480 | |
| 11481 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
| 11482 | } |
| 11483 | |
| 11484 | if (!R.empty()) { |
| 11485 | NamedDecl *Named = R.getRepresentativeDecl(); |
| 11486 | NamespaceDecl *NS = R.getAsSingle<NamespaceDecl>(); |
| 11487 | assert(NS && "expected namespace decl"); |
| 11488 | |
| 11489 | |
| 11490 | DiagnoseUseOfDecl(Named, IdentLoc); |
| 11491 | |
| 11492 | |
| 11493 | |
| 11494 | |
| 11495 | |
| 11496 | |
| 11497 | |
| 11498 | |
| 11499 | |
| 11500 | |
| 11501 | |
| 11502 | |
| 11503 | |
| 11504 | DeclContext *CommonAncestor = NS; |
| 11505 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 11506 | CommonAncestor = CommonAncestor->getParent(); |
| 11507 | |
| 11508 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
| 11509 | SS.getWithLocInContext(Context), |
| 11510 | IdentLoc, Named, CommonAncestor); |
| 11511 | |
| 11512 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
| 11513 | !SourceMgr.isInMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
| 11514 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 11515 | } |
| 11516 | |
| 11517 | PushUsingDirective(S, UDir); |
| 11518 | } else { |
| 11519 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
| 11520 | } |
| 11521 | |
| 11522 | if (UDir) |
| 11523 | ProcessDeclAttributeList(S, UDir, AttrList); |
| 11524 | |
| 11525 | return UDir; |
| 11526 | } |
| 11527 | |
| 11528 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 11529 | |
| 11530 | |
| 11531 | |
| 11532 | DeclContext *Ctx = S->getEntity(); |
| 11533 | if (Ctx && !Ctx->isFunctionOrMethod()) |
| 11534 | Ctx->addDecl(UDir); |
| 11535 | else |
| 11536 | |
| 11537 | |
| 11538 | S->PushUsingDirective(UDir); |
| 11539 | } |
| 11540 | |
| 11541 | Decl *Sema::ActOnUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 11542 | SourceLocation UsingLoc, |
| 11543 | SourceLocation TypenameLoc, CXXScopeSpec &SS, |
| 11544 | UnqualifiedId &Name, |
| 11545 | SourceLocation EllipsisLoc, |
| 11546 | const ParsedAttributesView &AttrList) { |
| 11547 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
| 11548 | |
| 11549 | if (SS.isEmpty()) { |
| 11550 | Diag(Name.getBeginLoc(), diag::err_using_requires_qualname); |
| 11551 | return nullptr; |
| 11552 | } |
| 11553 | |
| 11554 | switch (Name.getKind()) { |
| 11555 | case UnqualifiedIdKind::IK_ImplicitSelfParam: |
| 11556 | case UnqualifiedIdKind::IK_Identifier: |
| 11557 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
| 11558 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
| 11559 | case UnqualifiedIdKind::IK_ConversionFunctionId: |
| 11560 | break; |
| 11561 | |
| 11562 | case UnqualifiedIdKind::IK_ConstructorName: |
| 11563 | case UnqualifiedIdKind::IK_ConstructorTemplateId: |
| 11564 | |
| 11565 | Diag(Name.getBeginLoc(), |
| 11566 | getLangOpts().CPlusPlus11 |
| 11567 | ? diag::warn_cxx98_compat_using_decl_constructor |
| 11568 | : diag::err_using_decl_constructor) |
| 11569 | << SS.getRange(); |
| 11570 | |
| 11571 | if (getLangOpts().CPlusPlus11) break; |
| 11572 | |
| 11573 | return nullptr; |
| 11574 | |
| 11575 | case UnqualifiedIdKind::IK_DestructorName: |
| 11576 | Diag(Name.getBeginLoc(), diag::err_using_decl_destructor) << SS.getRange(); |
| 11577 | return nullptr; |
| 11578 | |
| 11579 | case UnqualifiedIdKind::IK_TemplateId: |
| 11580 | Diag(Name.getBeginLoc(), diag::err_using_decl_template_id) |
| 11581 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
| 11582 | return nullptr; |
| 11583 | |
| 11584 | case UnqualifiedIdKind::IK_DeductionGuideName: |
| 11585 | llvm_unreachable("cannot parse qualified deduction guide name"); |
| 11586 | } |
| 11587 | |
| 11588 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 11589 | DeclarationName TargetName = TargetNameInfo.getName(); |
| 11590 | if (!TargetName) |
| 11591 | return nullptr; |
| 11592 | |
| 11593 | |
| 11594 | if (UsingLoc.isInvalid()) { |
| 11595 | Diag(Name.getBeginLoc(), getLangOpts().CPlusPlus11 |
| 11596 | ? diag::err_access_decl |
| 11597 | : diag::warn_access_decl_deprecated) |
| 11598 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
| 11599 | } |
| 11600 | |
| 11601 | if (EllipsisLoc.isInvalid()) { |
| 11602 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 11603 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 11604 | return nullptr; |
| 11605 | } else { |
| 11606 | if (!SS.getScopeRep()->containsUnexpandedParameterPack() && |
| 11607 | !TargetNameInfo.containsUnexpandedParameterPack()) { |
| 11608 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 11609 | << SourceRange(SS.getBeginLoc(), TargetNameInfo.getEndLoc()); |
| 11610 | EllipsisLoc = SourceLocation(); |
| 11611 | } |
| 11612 | } |
| 11613 | |
| 11614 | NamedDecl *UD = |
| 11615 | BuildUsingDeclaration(S, AS, UsingLoc, TypenameLoc.isValid(), TypenameLoc, |
| 11616 | SS, TargetNameInfo, EllipsisLoc, AttrList, |
| 11617 | false, |
| 11618 | AttrList.hasAttribute(ParsedAttr::AT_UsingIfExists)); |
| 11619 | if (UD) |
| 11620 | PushOnScopeChains(UD, S, false); |
| 11621 | |
| 11622 | return UD; |
| 11623 | } |
| 11624 | |
| 11625 | Decl *Sema::ActOnUsingEnumDeclaration(Scope *S, AccessSpecifier AS, |
| 11626 | SourceLocation UsingLoc, |
| 11627 | SourceLocation EnumLoc, |
| 11628 | const DeclSpec &DS) { |
| 11629 | switch (DS.getTypeSpecType()) { |
| 11630 | case DeclSpec::TST_error: |
| 11631 | |
| 11632 | return nullptr; |
| 11633 | |
| 11634 | case DeclSpec::TST_enum: |
| 11635 | break; |
| 11636 | |
| 11637 | case DeclSpec::TST_typename: |
| 11638 | Diag(DS.getTypeSpecTypeLoc(), diag::err_using_enum_is_dependent); |
| 11639 | return nullptr; |
| 11640 | |
| 11641 | default: |
| 11642 | llvm_unreachable("unexpected DeclSpec type"); |
| 11643 | } |
| 11644 | |
| 11645 | |
| 11646 | auto *Enum = cast<EnumDecl>(DS.getRepAsDecl()); |
| 11647 | if (auto *Def = Enum->getDefinition()) |
| 11648 | Enum = Def; |
| 11649 | |
| 11650 | auto *UD = BuildUsingEnumDeclaration(S, AS, UsingLoc, EnumLoc, |
| 11651 | DS.getTypeSpecTypeNameLoc(), Enum); |
| 11652 | if (UD) |
| 11653 | PushOnScopeChains(UD, S, false); |
| 11654 | |
| 11655 | return UD; |
| 11656 | } |
| 11657 | |
| 11658 | |
| 11659 | |
| 11660 | |
| 11661 | static bool |
| 11662 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2) { |
| 11663 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) |
| 11664 | return true; |
| 11665 | |
| 11666 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| 11667 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) |
| 11668 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 11669 | TD2->getUnderlyingType()); |
| 11670 | |
| 11671 | |
| 11672 | |
| 11673 | if (isa<UnresolvedUsingIfExistsDecl>(D1) && |
| 11674 | isa<UnresolvedUsingIfExistsDecl>(D2)) |
| 11675 | return true; |
| 11676 | |
| 11677 | return false; |
| 11678 | } |
| 11679 | |
| 11680 | |
| 11681 | |
| 11682 | |
| 11683 | bool Sema::CheckUsingShadowDecl(BaseUsingDecl *BUD, NamedDecl *Orig, |
| 11684 | const LookupResult &Previous, |
| 11685 | UsingShadowDecl *&PrevShadow) { |
| 11686 | |
| 11687 | |
| 11688 | |
| 11689 | |
| 11690 | |
| 11691 | |
| 11692 | |
| 11693 | |
| 11694 | |
| 11695 | |
| 11696 | |
| 11697 | |
| 11698 | |
| 11699 | |
| 11700 | |
| 11701 | |
| 11702 | |
| 11703 | |
| 11704 | |
| 11705 | if (!getLangOpts().CPlusPlus11 && CurContext->isRecord()) |
| 11706 | if (auto *Using = dyn_cast<UsingDecl>(BUD)) { |
| 11707 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 11708 | |
| 11709 | |
| 11710 | if (isa<EnumDecl>(OrigDC)) |
| 11711 | OrigDC = OrigDC->getParent(); |
| 11712 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 11713 | while (OrigRec->isAnonymousStructOrUnion()) |
| 11714 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 11715 | |
| 11716 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 11717 | if (OrigDC == CurContext) { |
| 11718 | Diag(Using->getLocation(), |
| 11719 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 11720 | << Using->getQualifierLoc().getSourceRange(); |
| 11721 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 11722 | Using->setInvalidDecl(); |
| 11723 | return true; |
| 11724 | } |
| 11725 | |
| 11726 | Diag(Using->getQualifierLoc().getBeginLoc(), |
| 11727 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 11728 | << Using->getQualifier() << cast<CXXRecordDecl>(CurContext) |
| 11729 | << Using->getQualifierLoc().getSourceRange(); |
| 11730 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 11731 | Using->setInvalidDecl(); |
| 11732 | return true; |
| 11733 | } |
| 11734 | } |
| 11735 | |
| 11736 | if (Previous.empty()) return false; |
| 11737 | |
| 11738 | NamedDecl *Target = Orig; |
| 11739 | if (isa<UsingShadowDecl>(Target)) |
| 11740 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 11741 | |
| 11742 | |
| 11743 | |
| 11744 | |
| 11745 | |
| 11746 | |
| 11747 | NamedDecl *NonTag = nullptr, *Tag = nullptr; |
| 11748 | bool FoundEquivalentDecl = false; |
| 11749 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 11750 | I != E; ++I) { |
| 11751 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 11752 | |
| 11753 | |
| 11754 | |
| 11755 | if (isa<UsingDecl>(D) || isa<UsingPackDecl>(D) || isa<UsingEnumDecl>(D)) |
| 11756 | continue; |
| 11757 | |
| 11758 | if (auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 11759 | |
| 11760 | |
| 11761 | |
| 11762 | if (RD->isInjectedClassName() && !isa<FieldDecl>(Target) && |
| 11763 | !isa<IndirectFieldDecl>(Target) && |
| 11764 | !isa<UnresolvedUsingValueDecl>(Target) && |
| 11765 | DiagnoseClassNameShadow( |
| 11766 | CurContext, |
| 11767 | DeclarationNameInfo(BUD->getDeclName(), BUD->getLocation()))) |
| 11768 | return true; |
| 11769 | } |
| 11770 | |
| 11771 | if (IsEquivalentForUsingDecl(Context, D, Target)) { |
| 11772 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(*I)) |
| 11773 | PrevShadow = Shadow; |
| 11774 | FoundEquivalentDecl = true; |
| 11775 | } else if (isEquivalentInternalLinkageDeclaration(D, Target)) { |
| 11776 | |
| 11777 | |
| 11778 | FoundEquivalentDecl = true; |
| 11779 | } |
| 11780 | |
| 11781 | if (isVisible(D)) |
| 11782 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 11783 | } |
| 11784 | |
| 11785 | if (FoundEquivalentDecl) |
| 11786 | return false; |
| 11787 | |
| 11788 | |
| 11789 | |
| 11790 | if (isa<UnresolvedUsingIfExistsDecl>(Target) != |
| 11791 | (isa_and_nonnull<UnresolvedUsingIfExistsDecl>(NonTag))) { |
| 11792 | if (!NonTag && !Tag) |
| 11793 | return false; |
| 11794 | Diag(BUD->getLocation(), diag::err_using_decl_conflict); |
| 11795 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 11796 | Diag((NonTag ? NonTag : Tag)->getLocation(), |
| 11797 | diag::note_using_decl_conflict); |
| 11798 | BUD->setInvalidDecl(); |
| 11799 | return true; |
| 11800 | } |
| 11801 | |
| 11802 | if (FunctionDecl *FD = Target->getAsFunction()) { |
| 11803 | NamedDecl *OldDecl = nullptr; |
| 11804 | switch (CheckOverload(nullptr, FD, Previous, OldDecl, |
| 11805 | true)) { |
| 11806 | case Ovl_Overload: |
| 11807 | return false; |
| 11808 | |
| 11809 | case Ovl_NonFunction: |
| 11810 | Diag(BUD->getLocation(), diag::err_using_decl_conflict); |
| 11811 | break; |
| 11812 | |
| 11813 | |
| 11814 | case Ovl_Match: |
| 11815 | |
| 11816 | |
| 11817 | |
| 11818 | if (CurContext->isRecord()) |
| 11819 | return true; |
| 11820 | |
| 11821 | |
| 11822 | Diag(BUD->getLocation(), diag::err_using_decl_conflict); |
| 11823 | break; |
| 11824 | } |
| 11825 | |
| 11826 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 11827 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 11828 | BUD->setInvalidDecl(); |
| 11829 | return true; |
| 11830 | } |
| 11831 | |
| 11832 | |
| 11833 | |
| 11834 | if (isa<TagDecl>(Target)) { |
| 11835 | |
| 11836 | if (!Tag) return false; |
| 11837 | |
| 11838 | Diag(BUD->getLocation(), diag::err_using_decl_conflict); |
| 11839 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 11840 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 11841 | BUD->setInvalidDecl(); |
| 11842 | return true; |
| 11843 | } |
| 11844 | |
| 11845 | |
| 11846 | if (!NonTag) return false; |
| 11847 | |
| 11848 | Diag(BUD->getLocation(), diag::err_using_decl_conflict); |
| 11849 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 11850 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 11851 | BUD->setInvalidDecl(); |
| 11852 | return true; |
| 11853 | } |
| 11854 | |
| 11855 | |
| 11856 | static bool isVirtualDirectBase(CXXRecordDecl *Derived, CXXRecordDecl *Base) { |
| 11857 | if (!Derived->getNumVBases()) |
| 11858 | return false; |
| 11859 | for (auto &B : Derived->bases()) |
| 11860 | if (B.getType()->getAsCXXRecordDecl() == Base) |
| 11861 | return B.isVirtual(); |
| 11862 | llvm_unreachable("not a direct base class"); |
| 11863 | } |
| 11864 | |
| 11865 | |
| 11866 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, BaseUsingDecl *BUD, |
| 11867 | NamedDecl *Orig, |
| 11868 | UsingShadowDecl *PrevDecl) { |
| 11869 | |
| 11870 | NamedDecl *Target = Orig; |
| 11871 | if (isa<UsingShadowDecl>(Target)) { |
| 11872 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 11873 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
| 11874 | } |
| 11875 | |
| 11876 | NamedDecl *NonTemplateTarget = Target; |
| 11877 | if (auto *TargetTD = dyn_cast<TemplateDecl>(Target)) |
| 11878 | NonTemplateTarget = TargetTD->getTemplatedDecl(); |
| 11879 | |
| 11880 | UsingShadowDecl *Shadow; |
| 11881 | if (NonTemplateTarget && isa<CXXConstructorDecl>(NonTemplateTarget)) { |
| 11882 | UsingDecl *Using = cast<UsingDecl>(BUD); |
| 11883 | bool IsVirtualBase = |
| 11884 | isVirtualDirectBase(cast<CXXRecordDecl>(CurContext), |
| 11885 | Using->getQualifier()->getAsRecordDecl()); |
| 11886 | Shadow = ConstructorUsingShadowDecl::Create( |
| 11887 | Context, CurContext, Using->getLocation(), Using, Orig, IsVirtualBase); |
| 11888 | } else { |
| 11889 | Shadow = UsingShadowDecl::Create(Context, CurContext, BUD->getLocation(), |
| 11890 | Target->getDeclName(), BUD, Target); |
| 11891 | } |
| 11892 | BUD->addShadowDecl(Shadow); |
| 11893 | |
| 11894 | Shadow->setAccess(BUD->getAccess()); |
| 11895 | if (Orig->isInvalidDecl() || BUD->isInvalidDecl()) |
| 11896 | Shadow->setInvalidDecl(); |
| 11897 | |
| 11898 | Shadow->setPreviousDecl(PrevDecl); |
| 11899 | |
| 11900 | if (S) |
| 11901 | PushOnScopeChains(Shadow, S); |
| 11902 | else |
| 11903 | CurContext->addDecl(Shadow); |
| 11904 | |
| 11905 | |
| 11906 | return Shadow; |
| 11907 | } |
| 11908 | |
| 11909 | |
| 11910 | |
| 11911 | |
| 11912 | |
| 11913 | |
| 11914 | |
| 11915 | |
| 11916 | |
| 11917 | |
| 11918 | |
| 11919 | |
| 11920 | |
| 11921 | |
| 11922 | |
| 11923 | |
| 11924 | |
| 11925 | |
| 11926 | |
| 11927 | |
| 11928 | |
| 11929 | |
| 11930 | |
| 11931 | |
| 11932 | |
| 11933 | |
| 11934 | |
| 11935 | |
| 11936 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
| 11937 | if (Shadow->getDeclName().getNameKind() == |
| 11938 | DeclarationName::CXXConversionFunctionName) |
| 11939 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 11940 | |
| 11941 | |
| 11942 | Shadow->getDeclContext()->removeDecl(Shadow); |
| 11943 | |
| 11944 | |
| 11945 | if (S) { |
| 11946 | S->RemoveDecl(Shadow); |
| 11947 | IdResolver.RemoveDecl(Shadow); |
| 11948 | } |
| 11949 | |
| 11950 | |
| 11951 | Shadow->getIntroducer()->removeShadowDecl(Shadow); |
| 11952 | |
| 11953 | |
| 11954 | |
| 11955 | } |
| 11956 | |
| 11957 | |
| 11958 | static CXXBaseSpecifier *findDirectBaseWithType(CXXRecordDecl *Derived, |
| 11959 | QualType DesiredBase, |
| 11960 | bool &AnyDependentBases) { |
| 11961 | |
| 11962 | CanQualType CanonicalDesiredBase = DesiredBase->getCanonicalTypeUnqualified() |
| 11963 | .getUnqualifiedType(); |
| 11964 | for (auto &Base : Derived->bases()) { |
| 11965 | CanQualType BaseType = Base.getType()->getCanonicalTypeUnqualified(); |
| 11966 | if (CanonicalDesiredBase == BaseType) |
| 11967 | return &Base; |
| 11968 | if (BaseType->isDependentType()) |
| 11969 | AnyDependentBases = true; |
| 11970 | } |
| 11971 | return nullptr; |
| 11972 | } |
| 11973 | |
| 11974 | namespace { |
| 11975 | class UsingValidatorCCC final : public CorrectionCandidateCallback { |
| 11976 | public: |
| 11977 | UsingValidatorCCC(bool HasTypenameKeyword, bool IsInstantiation, |
| 11978 | NestedNameSpecifier *NNS, CXXRecordDecl *RequireMemberOf) |
| 11979 | : HasTypenameKeyword(HasTypenameKeyword), |
| 11980 | IsInstantiation(IsInstantiation), OldNNS(NNS), |
| 11981 | RequireMemberOf(RequireMemberOf) {} |
| 11982 | |
| 11983 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
| 11984 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 11985 | |
| 11986 | |
| 11987 | if (!ND || isa<NamespaceDecl>(ND)) |
| 11988 | return false; |
| 11989 | |
| 11990 | |
| 11991 | if (Candidate.WillReplaceSpecifier() && !Candidate.getCorrectionSpecifier()) |
| 11992 | return false; |
| 11993 | |
| 11994 | |
| 11995 | |
| 11996 | |
| 11997 | if (RequireMemberOf) { |
| 11998 | auto *FoundRecord = dyn_cast<CXXRecordDecl>(ND); |
| 11999 | if (FoundRecord && FoundRecord->isInjectedClassName()) { |
| 12000 | |
| 12001 | |
| 12002 | ASTContext &Ctx = ND->getASTContext(); |
| 12003 | if (!Ctx.getLangOpts().CPlusPlus11) |
| 12004 | return false; |
| 12005 | QualType FoundType = Ctx.getRecordType(FoundRecord); |
| 12006 | |
| 12007 | |
| 12008 | |
| 12009 | |
| 12010 | NestedNameSpecifier *Specifier = |
| 12011 | Candidate.WillReplaceSpecifier() |
| 12012 | ? Candidate.getCorrectionSpecifier() |
| 12013 | : OldNNS; |
| 12014 | if (!Specifier->getAsType() || |
| 12015 | !Ctx.hasSameType(QualType(Specifier->getAsType(), 0), FoundType)) |
| 12016 | return false; |
| 12017 | |
| 12018 | |
| 12019 | |
| 12020 | bool AnyDependentBases = false; |
| 12021 | if (!findDirectBaseWithType(RequireMemberOf, |
| 12022 | Ctx.getRecordType(FoundRecord), |
| 12023 | AnyDependentBases) && |
| 12024 | !AnyDependentBases) |
| 12025 | return false; |
| 12026 | } else { |
| 12027 | auto *RD = dyn_cast<CXXRecordDecl>(ND->getDeclContext()); |
| 12028 | if (!RD || RequireMemberOf->isProvablyNotDerivedFrom(RD)) |
| 12029 | return false; |
| 12030 | |
| 12031 | |
| 12032 | } |
| 12033 | } else { |
| 12034 | auto *FoundRecord = dyn_cast<CXXRecordDecl>(ND); |
| 12035 | if (FoundRecord && FoundRecord->isInjectedClassName()) |
| 12036 | return false; |
| 12037 | } |
| 12038 | |
| 12039 | if (isa<TypeDecl>(ND)) |
| 12040 | return HasTypenameKeyword || !IsInstantiation; |
| 12041 | |
| 12042 | return !HasTypenameKeyword; |
| 12043 | } |
| 12044 | |
| 12045 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 12046 | return std::make_unique<UsingValidatorCCC>(*this); |
| 12047 | } |
| 12048 | |
| 12049 | private: |
| 12050 | bool HasTypenameKeyword; |
| 12051 | bool IsInstantiation; |
| 12052 | NestedNameSpecifier *OldNNS; |
| 12053 | CXXRecordDecl *RequireMemberOf; |
| 12054 | }; |
| 12055 | } |
| 12056 | |
| 12057 | |
| 12058 | |
| 12059 | |
| 12060 | |
| 12061 | |
| 12062 | void Sema::FilterUsingLookup(Scope *S, LookupResult &Previous) { |
| 12063 | |
| 12064 | LookupResult::Filter F = Previous.makeFilter(); |
| 12065 | while (F.hasNext()) { |
| 12066 | NamedDecl *D = F.next(); |
| 12067 | if (!isDeclInScope(D, CurContext, S)) |
| 12068 | F.erase(); |
| 12069 | |
| 12070 | |
| 12071 | |
| 12072 | |
| 12073 | else if (!CurContext->isFunctionOrMethod() && D->isLocalExternDecl() && |
| 12074 | !(D->getIdentifierNamespace() & Decl::IDNS_Ordinary)) |
| 12075 | F.erase(); |
| 12076 | } |
| 12077 | F.done(); |
| 12078 | } |
| 12079 | |
| 12080 | |
| 12081 | |
| 12082 | |
| 12083 | |
| 12084 | |
| 12085 | NamedDecl *Sema::BuildUsingDeclaration( |
| 12086 | Scope *S, AccessSpecifier AS, SourceLocation UsingLoc, |
| 12087 | bool HasTypenameKeyword, SourceLocation TypenameLoc, CXXScopeSpec &SS, |
| 12088 | DeclarationNameInfo NameInfo, SourceLocation EllipsisLoc, |
| 12089 | const ParsedAttributesView &AttrList, bool IsInstantiation, |
| 12090 | bool IsUsingIfExists) { |
| 12091 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 12092 | SourceLocation IdentLoc = NameInfo.getLoc(); |
| 12093 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
| 12094 | |
| 12095 | |
| 12096 | |
| 12097 | |
| 12098 | |
| 12099 | |
| 12100 | DeclarationNameInfo UsingName = NameInfo; |
| 12101 | if (UsingName.getName().getNameKind() == DeclarationName::CXXConstructorName) |
| 12102 | if (auto *RD = dyn_cast<CXXRecordDecl>(CurContext)) |
| 12103 | UsingName.setName(Context.DeclarationNames.getCXXConstructorName( |
| 12104 | Context.getCanonicalType(Context.getRecordType(RD)))); |
| 12105 | |
| 12106 | |
| 12107 | LookupResult Previous(*this, UsingName, LookupUsingDeclName, |
| 12108 | ForVisibleRedeclaration); |
| 12109 | Previous.setHideTags(false); |
| 12110 | if (S) { |
| 12111 | LookupName(Previous, S); |
| 12112 | |
| 12113 | FilterUsingLookup(S, Previous); |
| 12114 | } else { |
| 12115 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 12116 | if (CurContext->isRecord()) |
| 12117 | LookupQualifiedName(Previous, CurContext); |
| 12118 | else { |
| 12119 | |
| 12120 | |
| 12121 | |
| 12122 | |
| 12123 | |
| 12124 | |
| 12125 | |
| 12126 | |
| 12127 | |
| 12128 | |
| 12129 | |
| 12130 | |
| 12131 | |
| 12132 | } |
| 12133 | } |
| 12134 | |
| 12135 | |
| 12136 | if (CheckUsingDeclRedeclaration(UsingLoc, HasTypenameKeyword, |
| 12137 | SS, IdentLoc, Previous)) |
| 12138 | return nullptr; |
| 12139 | |
| 12140 | |
| 12141 | if (IsUsingIfExists && UsingName.getName().getNameKind() == |
| 12142 | DeclarationName::CXXConstructorName) { |
| 12143 | Diag(UsingLoc, diag::err_using_if_exists_on_ctor); |
| 12144 | return nullptr; |
| 12145 | } |
| 12146 | |
| 12147 | DeclContext *LookupContext = computeDeclContext(SS); |
| 12148 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
| 12149 | if (!LookupContext || EllipsisLoc.isValid()) { |
| 12150 | NamedDecl *D; |
| 12151 | |
| 12152 | if (!LookupContext && CheckUsingDeclQualifier(UsingLoc, HasTypenameKeyword, |
| 12153 | SS, NameInfo, IdentLoc)) |
| 12154 | return nullptr; |
| 12155 | |
| 12156 | if (HasTypenameKeyword) { |
| 12157 | |
| 12158 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 12159 | UsingLoc, TypenameLoc, |
| 12160 | QualifierLoc, |
| 12161 | IdentLoc, NameInfo.getName(), |
| 12162 | EllipsisLoc); |
| 12163 | } else { |
| 12164 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 12165 | QualifierLoc, NameInfo, EllipsisLoc); |
| 12166 | } |
| 12167 | D->setAccess(AS); |
| 12168 | CurContext->addDecl(D); |
| 12169 | ProcessDeclAttributeList(S, D, AttrList); |
| 12170 | return D; |
| 12171 | } |
| 12172 | |
| 12173 | auto Build = [&](bool Invalid) { |
| 12174 | UsingDecl *UD = |
| 12175 | UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
| 12176 | UsingName, HasTypenameKeyword); |
| 12177 | UD->setAccess(AS); |
| 12178 | CurContext->addDecl(UD); |
| 12179 | ProcessDeclAttributeList(S, UD, AttrList); |
| 12180 | UD->setInvalidDecl(Invalid); |
| 12181 | return UD; |
| 12182 | }; |
| 12183 | auto BuildInvalid = [&]{ return Build(true); }; |
| 12184 | auto BuildValid = [&]{ return Build(false); }; |
| 12185 | |
| 12186 | if (RequireCompleteDeclContext(SS, LookupContext)) |
| 12187 | return BuildInvalid(); |
| 12188 | |
| 12189 | |
| 12190 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
| 12191 | |
| 12192 | |
| 12193 | |
| 12194 | |
| 12195 | |
| 12196 | if (!IsInstantiation) |
| 12197 | R.setHideTags(false); |
| 12198 | |
| 12199 | |
| 12200 | |
| 12201 | if (CurContext->isRecord()) { |
| 12202 | R.setBaseObjectType( |
| 12203 | Context.getTypeDeclType(cast<CXXRecordDecl>(CurContext))); |
| 12204 | } |
| 12205 | |
| 12206 | LookupQualifiedName(R, LookupContext); |
| 12207 | |
| 12208 | |
| 12209 | if (CheckUsingDeclQualifier(UsingLoc, HasTypenameKeyword, SS, NameInfo, |
| 12210 | IdentLoc, &R)) |
| 12211 | return nullptr; |
| 12212 | |
| 12213 | if (R.empty() && IsUsingIfExists) |
| 12214 | R.addDecl(UnresolvedUsingIfExistsDecl::Create(Context, CurContext, UsingLoc, |
| 12215 | UsingName.getName()), |
| 12216 | AS_public); |
| 12217 | |
| 12218 | |
| 12219 | |
| 12220 | |
| 12221 | |
| 12222 | if (R.empty() && |
| 12223 | NameInfo.getName().getNameKind() != DeclarationName::CXXConstructorName) { |
| 12224 | |
| 12225 | |
| 12226 | |
| 12227 | auto *II = NameInfo.getName().getAsIdentifierInfo(); |
| 12228 | if (getLangOpts().CPlusPlus14 && II && II->isStr("gets") && |
| 12229 | CurContext->isStdNamespace() && |
| 12230 | isa<TranslationUnitDecl>(LookupContext) && |
| 12231 | getSourceManager().isInSystemHeader(UsingLoc)) |
| 12232 | return nullptr; |
| 12233 | UsingValidatorCCC CCC(HasTypenameKeyword, IsInstantiation, SS.getScopeRep(), |
| 12234 | dyn_cast<CXXRecordDecl>(CurContext)); |
| 12235 | if (TypoCorrection Corrected = |
| 12236 | CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, CCC, |
| 12237 | CTK_ErrorRecovery)) { |
| 12238 | |
| 12239 | |
| 12240 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest) |
| 12241 | << NameInfo.getName() << LookupContext << 0 |
| 12242 | << SS.getRange()); |
| 12243 | |
| 12244 | |
| 12245 | |
| 12246 | NamedDecl *ND = Corrected.getCorrectionDecl(); |
| 12247 | if (!ND) |
| 12248 | return BuildInvalid(); |
| 12249 | |
| 12250 | |
| 12251 | auto *RD = dyn_cast<CXXRecordDecl>(ND); |
| 12252 | if (RD && RD->isInjectedClassName()) { |
| 12253 | |
| 12254 | RD = cast<CXXRecordDecl>(RD->getParent()); |
| 12255 | |
| 12256 | |
| 12257 | if (Corrected.WillReplaceSpecifier()) { |
| 12258 | NestedNameSpecifierLocBuilder Builder; |
| 12259 | Builder.MakeTrivial(Context, Corrected.getCorrectionSpecifier(), |
| 12260 | QualifierLoc.getSourceRange()); |
| 12261 | QualifierLoc = Builder.getWithLocInContext(Context); |
| 12262 | } |
| 12263 | |
| 12264 | |
| 12265 | |
| 12266 | auto *CurClass = cast<CXXRecordDecl>(CurContext); |
| 12267 | UsingName.setName(Context.DeclarationNames.getCXXConstructorName( |
| 12268 | Context.getCanonicalType(Context.getRecordType(CurClass)))); |
| 12269 | UsingName.setNamedTypeInfo(nullptr); |
| 12270 | for (auto *Ctor : LookupConstructors(RD)) |
| 12271 | R.addDecl(Ctor); |
| 12272 | R.resolveKind(); |
| 12273 | } else { |
| 12274 | |
| 12275 | |
| 12276 | UsingName.setName(ND->getDeclName()); |
| 12277 | R.addDecl(ND); |
| 12278 | } |
| 12279 | } else { |
| 12280 | Diag(IdentLoc, diag::err_no_member) |
| 12281 | << NameInfo.getName() << LookupContext << SS.getRange(); |
| 12282 | return BuildInvalid(); |
| 12283 | } |
| 12284 | } |
| 12285 | |
| 12286 | if (R.isAmbiguous()) |
| 12287 | return BuildInvalid(); |
| 12288 | |
| 12289 | if (HasTypenameKeyword) { |
| 12290 | |
| 12291 | if (!R.getAsSingle<TypeDecl>() && |
| 12292 | !R.getAsSingle<UnresolvedUsingIfExistsDecl>()) { |
| 12293 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 12294 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 12295 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 12296 | diag::note_using_decl_target); |
| 12297 | return BuildInvalid(); |
| 12298 | } |
| 12299 | } else { |
| 12300 | |
| 12301 | |
| 12302 | |
| 12303 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
| 12304 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 12305 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
| 12306 | return BuildInvalid(); |
| 12307 | } |
| 12308 | } |
| 12309 | |
| 12310 | |
| 12311 | |
| 12312 | if (R.getAsSingle<NamespaceDecl>()) { |
| 12313 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 12314 | << SS.getRange(); |
| 12315 | return BuildInvalid(); |
| 12316 | } |
| 12317 | |
| 12318 | UsingDecl *UD = BuildValid(); |
| 12319 | |
| 12320 | |
| 12321 | if (UsingName.getName().getNameKind() == |
| 12322 | DeclarationName::CXXConstructorName) { |
| 12323 | |
| 12324 | |
| 12325 | R.suppressDiagnostics(); |
| 12326 | if (CheckInheritingConstructorUsingDecl(UD)) |
| 12327 | return UD; |
| 12328 | } |
| 12329 | |
| 12330 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 12331 | UsingShadowDecl *PrevDecl = nullptr; |
| 12332 | if (!CheckUsingShadowDecl(UD, *I, Previous, PrevDecl)) |
| 12333 | BuildUsingShadowDecl(S, UD, *I, PrevDecl); |
| 12334 | } |
| 12335 | |
| 12336 | return UD; |
| 12337 | } |
| 12338 | |
| 12339 | NamedDecl *Sema::BuildUsingEnumDeclaration(Scope *S, AccessSpecifier AS, |
| 12340 | SourceLocation UsingLoc, |
| 12341 | SourceLocation EnumLoc, |
| 12342 | SourceLocation NameLoc, |
| 12343 | EnumDecl *ED) { |
| 12344 | bool Invalid = false; |
| 12345 | |
| 12346 | if (CurContext->getRedeclContext()->isRecord()) { |
| 12347 | |
| 12348 | DeclarationNameInfo UsingEnumName(ED->getDeclName(), NameLoc); |
| 12349 | LookupResult Previous(*this, UsingEnumName, LookupUsingDeclName, |
| 12350 | ForVisibleRedeclaration); |
| 12351 | |
| 12352 | LookupName(Previous, S); |
| 12353 | |
| 12354 | for (NamedDecl *D : Previous) |
| 12355 | if (UsingEnumDecl *UED = dyn_cast<UsingEnumDecl>(D)) |
| 12356 | if (UED->getEnumDecl() == ED) { |
| 12357 | Diag(UsingLoc, diag::err_using_enum_decl_redeclaration) |
| 12358 | << SourceRange(EnumLoc, NameLoc); |
| 12359 | Diag(D->getLocation(), diag::note_using_enum_decl) << 1; |
| 12360 | Invalid = true; |
| 12361 | break; |
| 12362 | } |
| 12363 | } |
| 12364 | |
| 12365 | if (RequireCompleteEnumDecl(ED, NameLoc)) |
| 12366 | Invalid = true; |
| 12367 | |
| 12368 | UsingEnumDecl *UD = UsingEnumDecl::Create(Context, CurContext, UsingLoc, |
| 12369 | EnumLoc, NameLoc, ED); |
| 12370 | UD->setAccess(AS); |
| 12371 | CurContext->addDecl(UD); |
| 12372 | |
| 12373 | if (Invalid) { |
| 12374 | UD->setInvalidDecl(); |
| 12375 | return UD; |
| 12376 | } |
| 12377 | |
| 12378 | |
| 12379 | for (EnumConstantDecl *EC : ED->enumerators()) { |
| 12380 | UsingShadowDecl *PrevDecl = nullptr; |
| 12381 | DeclarationNameInfo DNI(EC->getDeclName(), EC->getLocation()); |
| 12382 | LookupResult Previous(*this, DNI, LookupOrdinaryName, |
| 12383 | ForVisibleRedeclaration); |
| 12384 | LookupName(Previous, S); |
| 12385 | FilterUsingLookup(S, Previous); |
| 12386 | |
| 12387 | if (!CheckUsingShadowDecl(UD, EC, Previous, PrevDecl)) |
| 12388 | BuildUsingShadowDecl(S, UD, EC, PrevDecl); |
| 12389 | } |
| 12390 | |
| 12391 | return UD; |
| 12392 | } |
| 12393 | |
| 12394 | NamedDecl *Sema::BuildUsingPackDecl(NamedDecl *InstantiatedFrom, |
| 12395 | ArrayRef<NamedDecl *> Expansions) { |
| 12396 | assert(isa<UnresolvedUsingValueDecl>(InstantiatedFrom) || |
| 12397 | isa<UnresolvedUsingTypenameDecl>(InstantiatedFrom) || |
| 12398 | isa<UsingPackDecl>(InstantiatedFrom)); |
| 12399 | |
| 12400 | auto *UPD = |
| 12401 | UsingPackDecl::Create(Context, CurContext, InstantiatedFrom, Expansions); |
| 12402 | UPD->setAccess(InstantiatedFrom->getAccess()); |
| 12403 | CurContext->addDecl(UPD); |
| 12404 | return UPD; |
| 12405 | } |
| 12406 | |
| 12407 | |
| 12408 | bool Sema::CheckInheritingConstructorUsingDecl(UsingDecl *UD) { |
| 12409 | assert(!UD->hasTypename() && "expecting a constructor name"); |
| 12410 | |
| 12411 | const Type *SourceType = UD->getQualifier()->getAsType(); |
| 12412 | assert(SourceType && |
| 12413 | "Using decl naming constructor doesn't have type in scope spec."); |
| 12414 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 12415 | |
| 12416 | |
| 12417 | bool AnyDependentBases = false; |
| 12418 | auto *Base = findDirectBaseWithType(TargetClass, QualType(SourceType, 0), |
| 12419 | AnyDependentBases); |
| 12420 | if (!Base && !AnyDependentBases) { |
| 12421 | Diag(UD->getUsingLoc(), |
| 12422 | diag::err_using_decl_constructor_not_in_direct_base) |
| 12423 | << UD->getNameInfo().getSourceRange() |
| 12424 | << QualType(SourceType, 0) << TargetClass; |
| 12425 | UD->setInvalidDecl(); |
| 12426 | return true; |
| 12427 | } |
| 12428 | |
| 12429 | if (Base) |
| 12430 | Base->setInheritConstructors(); |
| 12431 | |
| 12432 | return false; |
| 12433 | } |
| 12434 | |
| 12435 | |
| 12436 | |
| 12437 | |
| 12438 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 12439 | bool HasTypenameKeyword, |
| 12440 | const CXXScopeSpec &SS, |
| 12441 | SourceLocation NameLoc, |
| 12442 | const LookupResult &Prev) { |
| 12443 | NestedNameSpecifier *Qual = SS.getScopeRep(); |
| 12444 | |
| 12445 | |
| 12446 | |
| 12447 | |
| 12448 | |
| 12449 | |
| 12450 | |
| 12451 | |
| 12452 | if (!CurContext->getRedeclContext()->isRecord()) { |
| 12453 | |
| 12454 | |
| 12455 | |
| 12456 | |
| 12457 | |
| 12458 | if (Qual->isDependent() && !HasTypenameKeyword) { |
| 12459 | for (auto *D : Prev) { |
| 12460 | if (!isa<TypeDecl>(D) && !isa<UsingDecl>(D) && !isa<UsingPackDecl>(D)) { |
| 12461 | bool OldCouldBeEnumerator = |
| 12462 | isa<UnresolvedUsingValueDecl>(D) || isa<EnumConstantDecl>(D); |
| 12463 | Diag(NameLoc, |
| 12464 | OldCouldBeEnumerator ? diag::err_redefinition |
| 12465 | : diag::err_redefinition_different_kind) |
| 12466 | << Prev.getLookupName(); |
| 12467 | Diag(D->getLocation(), diag::note_previous_definition); |
| 12468 | return true; |
| 12469 | } |
| 12470 | } |
| 12471 | } |
| 12472 | return false; |
| 12473 | } |
| 12474 | |
| 12475 | const NestedNameSpecifier *CNNS = |
| 12476 | Context.getCanonicalNestedNameSpecifier(Qual); |
| 12477 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 12478 | NamedDecl *D = *I; |
| 12479 | |
| 12480 | bool DTypename; |
| 12481 | NestedNameSpecifier *DQual; |
| 12482 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 12483 | DTypename = UD->hasTypename(); |
| 12484 | DQual = UD->getQualifier(); |
| 12485 | } else if (UnresolvedUsingValueDecl *UD |
| 12486 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 12487 | DTypename = false; |
| 12488 | DQual = UD->getQualifier(); |
| 12489 | } else if (UnresolvedUsingTypenameDecl *UD |
| 12490 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 12491 | DTypename = true; |
| 12492 | DQual = UD->getQualifier(); |
| 12493 | } else continue; |
| 12494 | |
| 12495 | |
| 12496 | |
| 12497 | if (HasTypenameKeyword != DTypename) continue; |
| 12498 | |
| 12499 | |
| 12500 | |
| 12501 | |
| 12502 | if (CNNS != Context.getCanonicalNestedNameSpecifier(DQual)) |
| 12503 | continue; |
| 12504 | |
| 12505 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
| 12506 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
| 12507 | return true; |
| 12508 | } |
| 12509 | |
| 12510 | return false; |
| 12511 | } |
| 12512 | |
| 12513 | |
| 12514 | |
| 12515 | |
| 12516 | |
| 12517 | |
| 12518 | |
| 12519 | |
| 12520 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, bool HasTypename, |
| 12521 | const CXXScopeSpec &SS, |
| 12522 | const DeclarationNameInfo &NameInfo, |
| 12523 | SourceLocation NameLoc, |
| 12524 | const LookupResult *R, const UsingDecl *UD) { |
| 12525 | DeclContext *NamedContext = computeDeclContext(SS); |
| 12526 | assert(bool(NamedContext) == (R || UD) && !(R && UD) && |
| 12527 | "resolvable context must have exactly one set of decls"); |
| 12528 | |
| 12529 | |
| 12530 | |
| 12531 | bool Cxx20Enumerator = false; |
| 12532 | if (NamedContext) { |
| 12533 | EnumConstantDecl *EC = nullptr; |
| 12534 | if (R) |
| 12535 | EC = R->getAsSingle<EnumConstantDecl>(); |
| 12536 | else if (UD && UD->shadow_size() == 1) |
| 12537 | EC = dyn_cast<EnumConstantDecl>(UD->shadow_begin()->getTargetDecl()); |
| 12538 | if (EC) |
| 12539 | Cxx20Enumerator = getLangOpts().CPlusPlus20; |
| 12540 | |
| 12541 | if (auto *ED = dyn_cast<EnumDecl>(NamedContext)) { |
| 12542 | |
| 12543 | |
| 12544 | |
| 12545 | if (EC && R && ED->isScoped()) |
| 12546 | Diag(SS.getBeginLoc(), |
| 12547 | getLangOpts().CPlusPlus20 |
| 12548 | ? diag::warn_cxx17_compat_using_decl_scoped_enumerator |
| 12549 | : diag::ext_using_decl_scoped_enumerator) |
| 12550 | << SS.getRange(); |
| 12551 | |
| 12552 | |
| 12553 | NamedContext = ED->getDeclContext(); |
| 12554 | } |
| 12555 | } |
| 12556 | |
| 12557 | if (!CurContext->isRecord()) { |
| 12558 | |
| 12559 | |
| 12560 | |
| 12561 | |
| 12562 | |
| 12563 | |
| 12564 | |
| 12565 | |
| 12566 | |
| 12567 | if (NamedContext ? !NamedContext->getRedeclContext()->isRecord() |
| 12568 | : !HasTypename) |
| 12569 | return false; |
| 12570 | |
| 12571 | Diag(NameLoc, |
| 12572 | Cxx20Enumerator |
| 12573 | ? diag::warn_cxx17_compat_using_decl_class_member_enumerator |
| 12574 | : diag::err_using_decl_can_not_refer_to_class_member) |
| 12575 | << SS.getRange(); |
| 12576 | |
| 12577 | if (Cxx20Enumerator) |
| 12578 | return false; |
| 12579 | |
| 12580 | auto *RD = NamedContext |
| 12581 | ? cast<CXXRecordDecl>(NamedContext->getRedeclContext()) |
| 12582 | : nullptr; |
| 12583 | if (RD && !RequireCompleteDeclContext(const_cast<CXXScopeSpec &>(SS), RD)) { |
| 12584 | |
| 12585 | |
| 12586 | if (!R) { |
| 12587 | |
| 12588 | |
| 12589 | } else if (R->getAsSingle<TypeDecl>()) { |
| 12590 | if (getLangOpts().CPlusPlus11) { |
| 12591 | |
| 12592 | Diag(SS.getBeginLoc(), diag::note_using_decl_class_member_workaround) |
| 12593 | << 0 |
| 12594 | << FixItHint::CreateInsertion(SS.getBeginLoc(), |
| 12595 | NameInfo.getName().getAsString() + |
| 12596 | " = "); |
| 12597 | } else { |
| 12598 | |
| 12599 | SourceLocation InsertLoc = getLocForEndOfToken(NameInfo.getEndLoc()); |
| 12600 | Diag(InsertLoc, diag::note_using_decl_class_member_workaround) |
| 12601 | << 1 |
| 12602 | << FixItHint::CreateReplacement(UsingLoc, "typedef") |
| 12603 | << FixItHint::CreateInsertion( |
| 12604 | InsertLoc, " " + NameInfo.getName().getAsString()); |
| 12605 | } |
| 12606 | } else if (R->getAsSingle<VarDecl>()) { |
| 12607 | |
| 12608 | |
| 12609 | FixItHint FixIt; |
| 12610 | if (getLangOpts().CPlusPlus11) { |
| 12611 | |
| 12612 | FixIt = FixItHint::CreateReplacement( |
| 12613 | UsingLoc, "auto &" + NameInfo.getName().getAsString() + " = "); |
| 12614 | } |
| 12615 | |
| 12616 | Diag(UsingLoc, diag::note_using_decl_class_member_workaround) |
| 12617 | << 2 |
| 12618 | << FixIt; |
| 12619 | } else if (R->getAsSingle<EnumConstantDecl>()) { |
| 12620 | |
| 12621 | |
| 12622 | |
| 12623 | FixItHint FixIt; |
| 12624 | if (getLangOpts().CPlusPlus11) { |
| 12625 | |
| 12626 | FixIt = FixItHint::CreateReplacement( |
| 12627 | UsingLoc, |
| 12628 | "constexpr auto " + NameInfo.getName().getAsString() + " = "); |
| 12629 | } |
| 12630 | |
| 12631 | Diag(UsingLoc, diag::note_using_decl_class_member_workaround) |
| 12632 | << (getLangOpts().CPlusPlus11 ? 4 : 3) |
| 12633 | << FixIt; |
| 12634 | } |
| 12635 | } |
| 12636 | |
| 12637 | return true; |
| 12638 | } |
| 12639 | |
| 12640 | |
| 12641 | if (!NamedContext) { |
| 12642 | |
| 12643 | |
| 12644 | |
| 12645 | |
| 12646 | |
| 12647 | |
| 12648 | return false; |
| 12649 | } |
| 12650 | |
| 12651 | |
| 12652 | if (!NamedContext->isRecord()) { |
| 12653 | |
| 12654 | |
| 12655 | Diag(SS.getBeginLoc(), |
| 12656 | Cxx20Enumerator |
| 12657 | ? diag::warn_cxx17_compat_using_decl_non_member_enumerator |
| 12658 | : diag::err_using_decl_nested_name_specifier_is_not_class) |
| 12659 | << SS.getScopeRep() << SS.getRange(); |
| 12660 | |
| 12661 | if (Cxx20Enumerator) |
| 12662 | return false; |
| 12663 | |
| 12664 | return true; |
| 12665 | } |
| 12666 | |
| 12667 | if (!NamedContext->isDependentContext() && |
| 12668 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 12669 | return true; |
| 12670 | |
| 12671 | if (getLangOpts().CPlusPlus11) { |
| 12672 | |
| 12673 | |
| 12674 | |
| 12675 | |
| 12676 | |
| 12677 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 12678 | cast<CXXRecordDecl>(NamedContext))) { |
| 12679 | |
| 12680 | if (Cxx20Enumerator) { |
| 12681 | Diag(NameLoc, diag::warn_cxx17_compat_using_decl_non_member_enumerator) |
| 12682 | << SS.getRange(); |
| 12683 | return false; |
| 12684 | } |
| 12685 | |
| 12686 | if (CurContext == NamedContext) { |
| 12687 | Diag(SS.getBeginLoc(), |
| 12688 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 12689 | << SS.getRange(); |
| 12690 | return !getLangOpts().CPlusPlus20; |
| 12691 | } |
| 12692 | |
| 12693 | if (!cast<CXXRecordDecl>(NamedContext)->isInvalidDecl()) { |
| 12694 | Diag(SS.getBeginLoc(), |
| 12695 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 12696 | << SS.getScopeRep() << cast<CXXRecordDecl>(CurContext) |
| 12697 | << SS.getRange(); |
| 12698 | } |
| 12699 | return true; |
| 12700 | } |
| 12701 | |
| 12702 | return false; |
| 12703 | } |
| 12704 | |
| 12705 | |
| 12706 | |
| 12707 | |
| 12708 | |
| 12709 | |
| 12710 | |
| 12711 | |
| 12712 | |
| 12713 | |
| 12714 | |
| 12715 | |
| 12716 | |
| 12717 | |
| 12718 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> Bases; |
| 12719 | auto Collect = [&Bases](const CXXRecordDecl *Base) { |
| 12720 | Bases.insert(Base); |
| 12721 | return true; |
| 12722 | }; |
| 12723 | |
| 12724 | |
| 12725 | if (!cast<CXXRecordDecl>(CurContext)->forallBases(Collect)) |
| 12726 | return false; |
| 12727 | |
| 12728 | |
| 12729 | |
| 12730 | auto IsNotBase = [&Bases](const CXXRecordDecl *Base) { |
| 12731 | return !Bases.count(Base); |
| 12732 | }; |
| 12733 | |
| 12734 | |
| 12735 | |
| 12736 | if (Bases.count(cast<CXXRecordDecl>(NamedContext)) || |
| 12737 | !cast<CXXRecordDecl>(NamedContext)->forallBases(IsNotBase)) |
| 12738 | return false; |
| 12739 | |
| 12740 | Diag(SS.getRange().getBegin(), |
| 12741 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 12742 | << SS.getScopeRep() |
| 12743 | << cast<CXXRecordDecl>(CurContext) |
| 12744 | << SS.getRange(); |
| 12745 | |
| 12746 | return true; |
| 12747 | } |
| 12748 | |
| 12749 | Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS, |
| 12750 | MultiTemplateParamsArg TemplateParamLists, |
| 12751 | SourceLocation UsingLoc, UnqualifiedId &Name, |
| 12752 | const ParsedAttributesView &AttrList, |
| 12753 | TypeResult Type, Decl *DeclFromDeclSpec) { |
| 12754 | |
| 12755 | while (S->isTemplateParamScope()) |
| 12756 | S = S->getParent(); |
| 12757 | assert((S->getFlags() & Scope::DeclScope) && |
| 12758 | "got alias-declaration outside of declaration scope"); |
| 12759 | |
| 12760 | if (Type.isInvalid()) |
| 12761 | return nullptr; |
| 12762 | |
| 12763 | bool Invalid = false; |
| 12764 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 12765 | TypeSourceInfo *TInfo = nullptr; |
| 12766 | GetTypeFromParser(Type.get(), &TInfo); |
| 12767 | |
| 12768 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 12769 | return nullptr; |
| 12770 | |
| 12771 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
| 12772 | UPPC_DeclarationType)) { |
| 12773 | Invalid = true; |
| 12774 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 12775 | TInfo->getTypeLoc().getBeginLoc()); |
| 12776 | } |
| 12777 | |
| 12778 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
| 12779 | TemplateParamLists.size() |
| 12780 | ? forRedeclarationInCurContext() |
| 12781 | : ForVisibleRedeclaration); |
| 12782 | LookupName(Previous, S); |
| 12783 | |
| 12784 | |
| 12785 | if (Previous.isSingleResult() && |
| 12786 | Previous.getFoundDecl()->isTemplateParameter()) { |
| 12787 | DiagnoseTemplateParameterShadow(Name.StartLocation,Previous.getFoundDecl()); |
| 12788 | Previous.clear(); |
| 12789 | } |
| 12790 | |
| 12791 | assert(Name.Kind == UnqualifiedIdKind::IK_Identifier && |
| 12792 | "name in alias declaration must be an identifier"); |
| 12793 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 12794 | Name.StartLocation, |
| 12795 | Name.Identifier, TInfo); |
| 12796 | |
| 12797 | NewTD->setAccess(AS); |
| 12798 | |
| 12799 | if (Invalid) |
| 12800 | NewTD->setInvalidDecl(); |
| 12801 | |
| 12802 | ProcessDeclAttributeList(S, NewTD, AttrList); |
| 12803 | AddPragmaAttributes(S, NewTD); |
| 12804 | |
| 12805 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 12806 | Invalid |= NewTD->isInvalidDecl(); |
| 12807 | |
| 12808 | bool Redeclaration = false; |
| 12809 | |
| 12810 | NamedDecl *NewND; |
| 12811 | if (TemplateParamLists.size()) { |
| 12812 | TypeAliasTemplateDecl *OldDecl = nullptr; |
| 12813 | TemplateParameterList *OldTemplateParams = nullptr; |
| 12814 | |
| 12815 | if (TemplateParamLists.size() != 1) { |
| 12816 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
| 12817 | << SourceRange(TemplateParamLists[1]->getTemplateLoc(), |
| 12818 | TemplateParamLists[TemplateParamLists.size()-1]->getRAngleLoc()); |
| 12819 | } |
| 12820 | TemplateParameterList *TemplateParams = TemplateParamLists[0]; |
| 12821 | |
| 12822 | |
| 12823 | if (CheckTemplateDeclScope(S, TemplateParams)) |
| 12824 | return nullptr; |
| 12825 | |
| 12826 | |
| 12827 | FilterLookupForScope(Previous, CurContext, S, false, |
| 12828 | false); |
| 12829 | if (!Previous.empty()) { |
| 12830 | Redeclaration = true; |
| 12831 | |
| 12832 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 12833 | if (!OldDecl && !Invalid) { |
| 12834 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 12835 | << Name.Identifier; |
| 12836 | |
| 12837 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 12838 | if (OldD->getLocation().isValid()) |
| 12839 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 12840 | |
| 12841 | Invalid = true; |
| 12842 | } |
| 12843 | |
| 12844 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 12845 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 12846 | OldDecl->getTemplateParameters(), |
| 12847 | true, |
| 12848 | TPL_TemplateMatch)) |
| 12849 | OldTemplateParams = |
| 12850 | OldDecl->getMostRecentDecl()->getTemplateParameters(); |
| 12851 | else |
| 12852 | Invalid = true; |
| 12853 | |
| 12854 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 12855 | if (!Invalid && |
| 12856 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 12857 | NewTD->getUnderlyingType())) { |
| 12858 | |
| 12859 | |
| 12860 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 12861 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 12862 | if (OldTD->getLocation().isValid()) |
| 12863 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 12864 | Invalid = true; |
| 12865 | } |
| 12866 | } |
| 12867 | } |
| 12868 | |
| 12869 | |
| 12870 | |
| 12871 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 12872 | TPC_TypeAliasTemplate)) |
| 12873 | return nullptr; |
| 12874 | |
| 12875 | TypeAliasTemplateDecl *NewDecl = |
| 12876 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 12877 | Name.Identifier, TemplateParams, |
| 12878 | NewTD); |
| 12879 | NewTD->setDescribedAliasTemplate(NewDecl); |
| 12880 | |
| 12881 | NewDecl->setAccess(AS); |
| 12882 | |
| 12883 | if (Invalid) |
| 12884 | NewDecl->setInvalidDecl(); |
| 12885 | else if (OldDecl) { |
| 12886 | NewDecl->setPreviousDecl(OldDecl); |
| 12887 | CheckRedeclarationModuleOwnership(NewDecl, OldDecl); |
| 12888 | } |
| 12889 | |
| 12890 | NewND = NewDecl; |
| 12891 | } else { |
| 12892 | if (auto *TD = dyn_cast_or_null<TagDecl>(DeclFromDeclSpec)) { |
| 12893 | setTagNameForLinkagePurposes(TD, NewTD); |
| 12894 | handleTagNumbering(TD, S); |
| 12895 | } |
| 12896 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 12897 | NewND = NewTD; |
| 12898 | } |
| 12899 | |
| 12900 | PushOnScopeChains(NewND, S); |
| 12901 | ActOnDocumentableDecl(NewND); |
| 12902 | return NewND; |
| 12903 | } |
| 12904 | |
| 12905 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, SourceLocation NamespaceLoc, |
| 12906 | SourceLocation AliasLoc, |
| 12907 | IdentifierInfo *Alias, CXXScopeSpec &SS, |
| 12908 | SourceLocation IdentLoc, |
| 12909 | IdentifierInfo *Ident) { |
| 12910 | |
| 12911 | |
| 12912 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 12913 | LookupParsedName(R, S, &SS); |
| 12914 | |
| 12915 | if (R.isAmbiguous()) |
| 12916 | return nullptr; |
| 12917 | |
| 12918 | if (R.empty()) { |
| 12919 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
| 12920 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
| 12921 | return nullptr; |
| 12922 | } |
| 12923 | } |
| 12924 | assert(!R.isAmbiguous() && !R.empty()); |
| 12925 | NamedDecl *ND = R.getRepresentativeDecl(); |
| 12926 | |
| 12927 | |
| 12928 | LookupResult PrevR(*this, Alias, AliasLoc, LookupOrdinaryName, |
| 12929 | ForVisibleRedeclaration); |
| 12930 | LookupName(PrevR, S); |
| 12931 | |
| 12932 | |
| 12933 | if (PrevR.isSingleResult() && PrevR.getFoundDecl()->isTemplateParameter()) { |
| 12934 | DiagnoseTemplateParameterShadow(AliasLoc, PrevR.getFoundDecl()); |
| 12935 | PrevR.clear(); |
| 12936 | } |
| 12937 | |
| 12938 | |
| 12939 | FilterLookupForScope(PrevR, CurContext, S, false, |
| 12940 | false); |
| 12941 | |
| 12942 | |
| 12943 | NamespaceAliasDecl *Prev = nullptr; |
| 12944 | if (PrevR.isSingleResult()) { |
| 12945 | NamedDecl *PrevDecl = PrevR.getRepresentativeDecl(); |
| 12946 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
| 12947 | |
| 12948 | |
| 12949 | if (AD->getNamespace()->Equals(getNamespaceDecl(ND))) { |
| 12950 | Prev = AD; |
| 12951 | } else if (isVisible(PrevDecl)) { |
| 12952 | Diag(AliasLoc, diag::err_redefinition_different_namespace_alias) |
| 12953 | << Alias; |
| 12954 | Diag(AD->getLocation(), diag::note_previous_namespace_alias) |
| 12955 | << AD->getNamespace(); |
| 12956 | return nullptr; |
| 12957 | } |
| 12958 | } else if (isVisible(PrevDecl)) { |
| 12959 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl->getUnderlyingDecl()) |
| 12960 | ? diag::err_redefinition |
| 12961 | : diag::err_redefinition_different_kind; |
| 12962 | Diag(AliasLoc, DiagID) << Alias; |
| 12963 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 12964 | return nullptr; |
| 12965 | } |
| 12966 | } |
| 12967 | |
| 12968 | |
| 12969 | DiagnoseUseOfDecl(ND, IdentLoc); |
| 12970 | |
| 12971 | NamespaceAliasDecl *AliasDecl = |
| 12972 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 12973 | Alias, SS.getWithLocInContext(Context), |
| 12974 | IdentLoc, ND); |
| 12975 | if (Prev) |
| 12976 | AliasDecl->setPreviousDecl(Prev); |
| 12977 | |
| 12978 | PushOnScopeChains(AliasDecl, S); |
| 12979 | return AliasDecl; |
| 12980 | } |
| 12981 | |
| 12982 | namespace { |
| 12983 | struct SpecialMemberExceptionSpecInfo |
| 12984 | : SpecialMemberVisitor<SpecialMemberExceptionSpecInfo> { |
| 12985 | SourceLocation Loc; |
| 12986 | Sema::ImplicitExceptionSpecification ExceptSpec; |
| 12987 | |
| 12988 | SpecialMemberExceptionSpecInfo(Sema &S, CXXMethodDecl *MD, |
| 12989 | Sema::CXXSpecialMember CSM, |
| 12990 | Sema::InheritedConstructorInfo *ICI, |
| 12991 | SourceLocation Loc) |
| 12992 | : SpecialMemberVisitor(S, MD, CSM, ICI), Loc(Loc), ExceptSpec(S) {} |
| 12993 | |
| 12994 | bool visitBase(CXXBaseSpecifier *Base); |
| 12995 | bool visitField(FieldDecl *FD); |
| 12996 | |
| 12997 | void visitClassSubobject(CXXRecordDecl *Class, Subobject Subobj, |
| 12998 | unsigned Quals); |
| 12999 | |
| 13000 | void visitSubobjectCall(Subobject Subobj, |
| 13001 | Sema::SpecialMemberOverloadResult SMOR); |
| 13002 | }; |
| 13003 | } |
| 13004 | |
| 13005 | bool SpecialMemberExceptionSpecInfo::visitBase(CXXBaseSpecifier *Base) { |
| 13006 | auto *RT = Base->getType()->getAs<RecordType>(); |
| 13007 | if (!RT) |
| 13008 | return false; |
| 13009 | |
| 13010 | auto *BaseClass = cast<CXXRecordDecl>(RT->getDecl()); |
| 13011 | Sema::SpecialMemberOverloadResult SMOR = lookupInheritedCtor(BaseClass); |
| 13012 | if (auto *BaseCtor = SMOR.getMethod()) { |
| 13013 | visitSubobjectCall(Base, BaseCtor); |
| 13014 | return false; |
| 13015 | } |
| 13016 | |
| 13017 | visitClassSubobject(BaseClass, Base, 0); |
| 13018 | return false; |
| 13019 | } |
| 13020 | |
| 13021 | bool SpecialMemberExceptionSpecInfo::visitField(FieldDecl *FD) { |
| 13022 | if (CSM == Sema::CXXDefaultConstructor && FD->hasInClassInitializer()) { |
| 13023 | Expr *E = FD->getInClassInitializer(); |
| 13024 | if (!E) |
| 13025 | |
| 13026 | |
| 13027 | |
| 13028 | |
| 13029 | |
| 13030 | E = S.BuildCXXDefaultInitExpr(Loc, FD).get(); |
| 13031 | if (E) |
| 13032 | ExceptSpec.CalledExpr(E); |
| 13033 | } else if (auto *RT = S.Context.getBaseElementType(FD->getType()) |
| 13034 | ->getAs<RecordType>()) { |
| 13035 | visitClassSubobject(cast<CXXRecordDecl>(RT->getDecl()), FD, |
| 13036 | FD->getType().getCVRQualifiers()); |
| 13037 | } |
| 13038 | return false; |
| 13039 | } |
| 13040 | |
| 13041 | void SpecialMemberExceptionSpecInfo::visitClassSubobject(CXXRecordDecl *Class, |
| 13042 | Subobject Subobj, |
| 13043 | unsigned Quals) { |
| 13044 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| 13045 | bool IsMutable = Field && Field->isMutable(); |
| 13046 | visitSubobjectCall(Subobj, lookupIn(Class, Quals, IsMutable)); |
| 13047 | } |
| 13048 | |
| 13049 | void SpecialMemberExceptionSpecInfo::visitSubobjectCall( |
| 13050 | Subobject Subobj, Sema::SpecialMemberOverloadResult SMOR) { |
| 13051 | |
| 13052 | |
| 13053 | if (CXXMethodDecl *MD = SMOR.getMethod()) |
| 13054 | ExceptSpec.CalledDecl(getSubobjectLoc(Subobj), MD); |
| 13055 | } |
| 13056 | |
| 13057 | bool Sema::tryResolveExplicitSpecifier(ExplicitSpecifier &ExplicitSpec) { |
| 13058 | llvm::APSInt Result; |
| 13059 | ExprResult Converted = CheckConvertedConstantExpression( |
| 13060 | ExplicitSpec.getExpr(), Context.BoolTy, Result, CCEK_ExplicitBool); |
| 13061 | ExplicitSpec.setExpr(Converted.get()); |
| 13062 | if (Converted.isUsable() && !Converted.get()->isValueDependent()) { |
| 13063 | ExplicitSpec.setKind(Result.getBoolValue() |
| 13064 | ? ExplicitSpecKind::ResolvedTrue |
| 13065 | : ExplicitSpecKind::ResolvedFalse); |
| 13066 | return true; |
| 13067 | } |
| 13068 | ExplicitSpec.setKind(ExplicitSpecKind::Unresolved); |
| 13069 | return false; |
| 13070 | } |
| 13071 | |
| 13072 | ExplicitSpecifier Sema::ActOnExplicitBoolSpecifier(Expr *ExplicitExpr) { |
| 13073 | ExplicitSpecifier ES(ExplicitExpr, ExplicitSpecKind::Unresolved); |
| 13074 | if (!ExplicitExpr->isTypeDependent()) |
| 13075 | tryResolveExplicitSpecifier(ES); |
| 13076 | return ES; |
| 13077 | } |
| 13078 | |
| 13079 | static Sema::ImplicitExceptionSpecification |
| 13080 | ComputeDefaultedSpecialMemberExceptionSpec( |
| 13081 | Sema &S, SourceLocation Loc, CXXMethodDecl *MD, Sema::CXXSpecialMember CSM, |
| 13082 | Sema::InheritedConstructorInfo *ICI) { |
| 13083 | ComputingExceptionSpec CES(S, MD, Loc); |
| 13084 | |
| 13085 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 13086 | |
| 13087 | |
| 13088 | |
| 13089 | |
| 13090 | SpecialMemberExceptionSpecInfo Info(S, MD, CSM, ICI, MD->getLocation()); |
| 13091 | if (ClassDecl->isInvalidDecl()) |
| 13092 | return Info.ExceptSpec; |
| 13093 | |
| 13094 | |
| 13095 | |
| 13096 | |
| 13097 | if (S.RequireCompleteType(MD->getLocation(), |
| 13098 | S.Context.getRecordType(ClassDecl), |
| 13099 | diag::err_exception_spec_incomplete_type)) |
| 13100 | return Info.ExceptSpec; |
| 13101 | |
| 13102 | |
| 13103 | |
| 13104 | |
| 13105 | |
| 13106 | |
| 13107 | |
| 13108 | |
| 13109 | |
| 13110 | |
| 13111 | |
| 13112 | |
| 13113 | |
| 13114 | |
| 13115 | |
| 13116 | |
| 13117 | Info.visit(Info.IsConstructor ? Info.VisitPotentiallyConstructedBases |
| 13118 | : Info.VisitAllBases); |
| 13119 | |
| 13120 | return Info.ExceptSpec; |
| 13121 | } |
| 13122 | |
| 13123 | namespace { |
| 13124 | |
| 13125 | struct DeclaringSpecialMember { |
| 13126 | Sema &S; |
| 13127 | Sema::SpecialMemberDecl D; |
| 13128 | Sema::ContextRAII SavedContext; |
| 13129 | bool WasAlreadyBeingDeclared; |
| 13130 | |
| 13131 | DeclaringSpecialMember(Sema &S, CXXRecordDecl *RD, Sema::CXXSpecialMember CSM) |
| 13132 | : S(S), D(RD, CSM), SavedContext(S, RD) { |
| 13133 | WasAlreadyBeingDeclared = !S.SpecialMembersBeingDeclared.insert(D).second; |
| 13134 | if (WasAlreadyBeingDeclared) |
| 13135 | |
| 13136 | |
| 13137 | S.SpecialMemberCache.clear(); |
| 13138 | else { |
| 13139 | |
| 13140 | |
| 13141 | Sema::CodeSynthesisContext Ctx; |
| 13142 | Ctx.Kind = Sema::CodeSynthesisContext::DeclaringSpecialMember; |
| 13143 | |
| 13144 | |
| 13145 | |
| 13146 | |
| 13147 | |
| 13148 | |
| 13149 | Ctx.PointOfInstantiation = RD->getLocation(); |
| 13150 | Ctx.Entity = RD; |
| 13151 | Ctx.SpecialMember = CSM; |
| 13152 | S.pushCodeSynthesisContext(Ctx); |
| 13153 | } |
| 13154 | } |
| 13155 | ~DeclaringSpecialMember() { |
| 13156 | if (!WasAlreadyBeingDeclared) { |
| 13157 | S.SpecialMembersBeingDeclared.erase(D); |
| 13158 | S.popCodeSynthesisContext(); |
| 13159 | } |
| 13160 | } |
| 13161 | |
| 13162 | |
| 13163 | bool isAlreadyBeingDeclared() const { |
| 13164 | return WasAlreadyBeingDeclared; |
| 13165 | } |
| 13166 | }; |
| 13167 | } |
| 13168 | |
| 13169 | void Sema::CheckImplicitSpecialMemberDeclaration(Scope *S, FunctionDecl *FD) { |
| 13170 | |
| 13171 | |
| 13172 | DeclarationName Name = FD->getDeclName(); |
| 13173 | LookupResult R(*this, Name, SourceLocation(), LookupOrdinaryName, |
| 13174 | ForExternalRedeclaration); |
| 13175 | for (auto *D : FD->getParent()->lookup(Name)) |
| 13176 | if (auto *Acceptable = R.getAcceptableDecl(D)) |
| 13177 | R.addDecl(Acceptable); |
| 13178 | R.resolveKind(); |
| 13179 | R.suppressDiagnostics(); |
| 13180 | |
| 13181 | CheckFunctionDeclaration(S, FD, R, false); |
| 13182 | } |
| 13183 | |
| 13184 | void Sema::setupImplicitSpecialMemberType(CXXMethodDecl *SpecialMem, |
| 13185 | QualType ResultTy, |
| 13186 | ArrayRef<QualType> Args) { |
| 13187 | |
| 13188 | FunctionProtoType::ExtProtoInfo EPI = getImplicitMethodEPI(*this, SpecialMem); |
| 13189 | |
| 13190 | LangAS AS = getDefaultCXXMethodAddrSpace(); |
| 13191 | if (AS != LangAS::Default) { |
| 13192 | EPI.TypeQuals.addAddressSpace(AS); |
| 13193 | } |
| 13194 | |
| 13195 | auto QT = Context.getFunctionType(ResultTy, Args, EPI); |
| 13196 | SpecialMem->setType(QT); |
| 13197 | |
| 13198 | |
| 13199 | |
| 13200 | |
| 13201 | if (inTemplateInstantiation() && |
| 13202 | cast<CXXRecordDecl>(SpecialMem->getParent())->isLambda()) { |
| 13203 | TypeSourceInfo *TSI = |
| 13204 | Context.getTrivialTypeSourceInfo(SpecialMem->getType()); |
| 13205 | SpecialMem->setTypeSourceInfo(TSI); |
| 13206 | } |
| 13207 | } |
| 13208 | |
| 13209 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 13210 | CXXRecordDecl *ClassDecl) { |
| 13211 | |
| 13212 | |
| 13213 | |
| 13214 | |
| 13215 | |
| 13216 | |
| 13217 | assert(ClassDecl->needsImplicitDefaultConstructor() && |
| 13218 | "Should not build implicit default constructor!"); |
| 13219 | |
| 13220 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDefaultConstructor); |
| 13221 | if (DSM.isAlreadyBeingDeclared()) |
| 13222 | return nullptr; |
| 13223 | |
| 13224 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 13225 | CXXDefaultConstructor, |
| 13226 | false); |
| 13227 | |
| 13228 | |
| 13229 | CanQualType ClassType |
| 13230 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 13231 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 13232 | DeclarationName Name |
| 13233 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
| 13234 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 13235 | CXXConstructorDecl *DefaultCon = CXXConstructorDecl::Create( |
| 13236 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 13237 | nullptr, ExplicitSpecifier(), |
| 13238 | true, true, |
| 13239 | Constexpr ? ConstexprSpecKind::Constexpr |
| 13240 | : ConstexprSpecKind::Unspecified); |
| 13241 | DefaultCon->setAccess(AS_public); |
| 13242 | DefaultCon->setDefaulted(); |
| 13243 | |
| 13244 | if (getLangOpts().CUDA) { |
| 13245 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXDefaultConstructor, |
| 13246 | DefaultCon, |
| 13247 | false, |
| 13248 | false); |
| 13249 | } |
| 13250 | |
| 13251 | setupImplicitSpecialMemberType(DefaultCon, Context.VoidTy, None); |
| 13252 | |
| 13253 | |
| 13254 | |
| 13255 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
| 13256 | |
| 13257 | |
| 13258 | ++getASTContext().NumImplicitDefaultConstructorsDeclared; |
| 13259 | |
| 13260 | Scope *S = getScopeForContext(ClassDecl); |
| 13261 | CheckImplicitSpecialMemberDeclaration(S, DefaultCon); |
| 13262 | |
| 13263 | if (ShouldDeleteSpecialMember(DefaultCon, CXXDefaultConstructor)) |
| 13264 | SetDeclDeleted(DefaultCon, ClassLoc); |
| 13265 | |
| 13266 | if (S) |
| 13267 | PushOnScopeChains(DefaultCon, S, false); |
| 13268 | ClassDecl->addDecl(DefaultCon); |
| 13269 | |
| 13270 | return DefaultCon; |
| 13271 | } |
| 13272 | |
| 13273 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 13274 | CXXConstructorDecl *Constructor) { |
| 13275 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
| 13276 | !Constructor->doesThisDeclarationHaveABody() && |
| 13277 | !Constructor->isDeleted()) && |
| 13278 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
| 13279 | if (Constructor->willHaveBody() || Constructor->isInvalidDecl()) |
| 13280 | return; |
| 13281 | |
| 13282 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 13283 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
| 13284 | |
| 13285 | SynthesizedFunctionScope Scope(*this, Constructor); |
| 13286 | |
| 13287 | |
| 13288 | |
| 13289 | ResolveExceptionSpec(CurrentLocation, |
| 13290 | Constructor->getType()->castAs<FunctionProtoType>()); |
| 13291 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 13292 | |
| 13293 | |
| 13294 | Scope.addContextNote(CurrentLocation); |
| 13295 | |
| 13296 | if (SetCtorInitializers(Constructor, false)) { |
| 13297 | Constructor->setInvalidDecl(); |
| 13298 | return; |
| 13299 | } |
| 13300 | |
| 13301 | SourceLocation Loc = Constructor->getEndLoc().isValid() |
| 13302 | ? Constructor->getEndLoc() |
| 13303 | : Constructor->getLocation(); |
| 13304 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
| 13305 | Constructor->markUsed(Context); |
| 13306 | |
| 13307 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 13308 | L->CompletedImplicitDefinition(Constructor); |
| 13309 | } |
| 13310 | |
| 13311 | DiagnoseUninitializedFields(*this, Constructor); |
| 13312 | } |
| 13313 | |
| 13314 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
| 13315 | |
| 13316 | CheckDelayedMemberExceptionSpecs(); |
| 13317 | } |
| 13318 | |
| 13319 | |
| 13320 | |
| 13321 | CXXConstructorDecl * |
| 13322 | Sema::findInheritingConstructor(SourceLocation Loc, |
| 13323 | CXXConstructorDecl *BaseCtor, |
| 13324 | ConstructorUsingShadowDecl *Shadow) { |
| 13325 | CXXRecordDecl *Derived = Shadow->getParent(); |
| 13326 | SourceLocation UsingLoc = Shadow->getLocation(); |
| 13327 | |
| 13328 | |
| 13329 | |
| 13330 | |
| 13331 | DeclarationName Name = BaseCtor->getDeclName(); |
| 13332 | |
| 13333 | |
| 13334 | |
| 13335 | for (NamedDecl *Ctor : Derived->lookup(Name)) |
| 13336 | if (declaresSameEntity(cast<CXXConstructorDecl>(Ctor) |
| 13337 | ->getInheritedConstructor() |
| 13338 | .getConstructor(), |
| 13339 | BaseCtor)) |
| 13340 | return cast<CXXConstructorDecl>(Ctor); |
| 13341 | |
| 13342 | DeclarationNameInfo NameInfo(Name, UsingLoc); |
| 13343 | TypeSourceInfo *TInfo = |
| 13344 | Context.getTrivialTypeSourceInfo(BaseCtor->getType(), UsingLoc); |
| 13345 | FunctionProtoTypeLoc ProtoLoc = |
| 13346 | TInfo->getTypeLoc().IgnoreParens().castAs<FunctionProtoTypeLoc>(); |
| 13347 | |
| 13348 | |
| 13349 | |
| 13350 | InheritedConstructorInfo ICI(*this, Loc, Shadow); |
| 13351 | |
| 13352 | bool Constexpr = |
| 13353 | BaseCtor->isConstexpr() && |
| 13354 | defaultedSpecialMemberIsConstexpr(*this, Derived, CXXDefaultConstructor, |
| 13355 | false, BaseCtor, &ICI); |
| 13356 | |
| 13357 | CXXConstructorDecl *DerivedCtor = CXXConstructorDecl::Create( |
| 13358 | Context, Derived, UsingLoc, NameInfo, TInfo->getType(), TInfo, |
| 13359 | BaseCtor->getExplicitSpecifier(), true, |
| 13360 | true, |
| 13361 | Constexpr ? BaseCtor->getConstexprKind() : ConstexprSpecKind::Unspecified, |
| 13362 | InheritedConstructor(Shadow, BaseCtor), |
| 13363 | BaseCtor->getTrailingRequiresClause()); |
| 13364 | if (Shadow->isInvalidDecl()) |
| 13365 | DerivedCtor->setInvalidDecl(); |
| 13366 | |
| 13367 | |
| 13368 | const FunctionProtoType *FPT = TInfo->getType()->castAs<FunctionProtoType>(); |
| 13369 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 13370 | EPI.ExceptionSpec.Type = EST_Unevaluated; |
| 13371 | EPI.ExceptionSpec.SourceDecl = DerivedCtor; |
| 13372 | DerivedCtor->setType(Context.getFunctionType(FPT->getReturnType(), |
| 13373 | FPT->getParamTypes(), EPI)); |
| 13374 | |
| 13375 | |
| 13376 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
| 13377 | for (unsigned I = 0, N = FPT->getNumParams(); I != N; ++I) { |
| 13378 | TypeSourceInfo *TInfo = |
| 13379 | Context.getTrivialTypeSourceInfo(FPT->getParamType(I), UsingLoc); |
| 13380 | ParmVarDecl *PD = ParmVarDecl::Create( |
| 13381 | Context, DerivedCtor, UsingLoc, UsingLoc, nullptr, |
| 13382 | FPT->getParamType(I), TInfo, SC_None, nullptr); |
| 13383 | PD->setScopeInfo(0, I); |
| 13384 | PD->setImplicit(); |
| 13385 | |
| 13386 | |
| 13387 | mergeDeclAttributes(PD, BaseCtor->getParamDecl(I)); |
| 13388 | ParamDecls.push_back(PD); |
| 13389 | ProtoLoc.setParam(I, PD); |
| 13390 | } |
| 13391 | |
| 13392 | |
| 13393 | assert(!BaseCtor->isDeleted() && "should not use deleted constructor"); |
| 13394 | DerivedCtor->setAccess(BaseCtor->getAccess()); |
| 13395 | DerivedCtor->setParams(ParamDecls); |
| 13396 | Derived->addDecl(DerivedCtor); |
| 13397 | |
| 13398 | if (ShouldDeleteSpecialMember(DerivedCtor, CXXDefaultConstructor, &ICI)) |
| 13399 | SetDeclDeleted(DerivedCtor, UsingLoc); |
| 13400 | |
| 13401 | return DerivedCtor; |
| 13402 | } |
| 13403 | |
| 13404 | void Sema::NoteDeletedInheritingConstructor(CXXConstructorDecl *Ctor) { |
| 13405 | InheritedConstructorInfo ICI(*this, Ctor->getLocation(), |
| 13406 | Ctor->getInheritedConstructor().getShadowDecl()); |
| 13407 | ShouldDeleteSpecialMember(Ctor, CXXDefaultConstructor, &ICI, |
| 13408 | true); |
| 13409 | } |
| 13410 | |
| 13411 | void Sema::DefineInheritingConstructor(SourceLocation CurrentLocation, |
| 13412 | CXXConstructorDecl *Constructor) { |
| 13413 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 13414 | assert(Constructor->getInheritedConstructor() && |
| 13415 | !Constructor->doesThisDeclarationHaveABody() && |
| 13416 | !Constructor->isDeleted()); |
| 13417 | if (Constructor->willHaveBody() || Constructor->isInvalidDecl()) |
| 13418 | return; |
| 13419 | |
| 13420 | |
| 13421 | |
| 13422 | SynthesizedFunctionScope Scope(*this, Constructor); |
| 13423 | |
| 13424 | |
| 13425 | |
| 13426 | ResolveExceptionSpec(CurrentLocation, |
| 13427 | Constructor->getType()->castAs<FunctionProtoType>()); |
| 13428 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 13429 | |
| 13430 | |
| 13431 | Scope.addContextNote(CurrentLocation); |
| 13432 | |
| 13433 | ConstructorUsingShadowDecl *Shadow = |
| 13434 | Constructor->getInheritedConstructor().getShadowDecl(); |
| 13435 | CXXConstructorDecl *InheritedCtor = |
| 13436 | Constructor->getInheritedConstructor().getConstructor(); |
| 13437 | |
| 13438 | |
| 13439 | |
| 13440 | |
| 13441 | |
| 13442 | |
| 13443 | InheritedConstructorInfo ICI(*this, CurrentLocation, Shadow); |
| 13444 | CXXRecordDecl *RD = Shadow->getParent(); |
| 13445 | SourceLocation InitLoc = Shadow->getLocation(); |
| 13446 | |
| 13447 | |
| 13448 | |
| 13449 | SmallVector<CXXCtorInitializer*, 8> Inits; |
| 13450 | for (bool VBase : {false, true}) { |
| 13451 | for (CXXBaseSpecifier &B : VBase ? RD->vbases() : RD->bases()) { |
| 13452 | if (B.isVirtual() != VBase) |
| 13453 | continue; |
| 13454 | |
| 13455 | auto *BaseRD = B.getType()->getAsCXXRecordDecl(); |
| 13456 | if (!BaseRD) |
| 13457 | continue; |
| 13458 | |
| 13459 | auto BaseCtor = ICI.findConstructorForBase(BaseRD, InheritedCtor); |
| 13460 | if (!BaseCtor.first) |
| 13461 | continue; |
| 13462 | |
| 13463 | MarkFunctionReferenced(CurrentLocation, BaseCtor.first); |
| 13464 | ExprResult Init = new (Context) CXXInheritedCtorInitExpr( |
| 13465 | InitLoc, B.getType(), BaseCtor.first, VBase, BaseCtor.second); |
| 13466 | |
| 13467 | auto *TInfo = Context.getTrivialTypeSourceInfo(B.getType(), InitLoc); |
| 13468 | Inits.push_back(new (Context) CXXCtorInitializer( |
| 13469 | Context, TInfo, VBase, InitLoc, Init.get(), InitLoc, |
| 13470 | SourceLocation())); |
| 13471 | } |
| 13472 | } |
| 13473 | |
| 13474 | |
| 13475 | |
| 13476 | |
| 13477 | if (SetCtorInitializers(Constructor, false, Inits)) { |
| 13478 | Constructor->setInvalidDecl(); |
| 13479 | return; |
| 13480 | } |
| 13481 | |
| 13482 | Constructor->setBody(new (Context) CompoundStmt(InitLoc)); |
| 13483 | Constructor->markUsed(Context); |
| 13484 | |
| 13485 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 13486 | L->CompletedImplicitDefinition(Constructor); |
| 13487 | } |
| 13488 | |
| 13489 | DiagnoseUninitializedFields(*this, Constructor); |
| 13490 | } |
| 13491 | |
| 13492 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 13493 | |
| 13494 | |
| 13495 | |
| 13496 | |
| 13497 | assert(ClassDecl->needsImplicitDestructor()); |
| 13498 | |
| 13499 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDestructor); |
| 13500 | if (DSM.isAlreadyBeingDeclared()) |
| 13501 | return nullptr; |
| 13502 | |
| 13503 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 13504 | CXXDestructor, |
| 13505 | false); |
| 13506 | |
| 13507 | |
| 13508 | CanQualType ClassType |
| 13509 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 13510 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 13511 | DeclarationName Name |
| 13512 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
| 13513 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 13514 | CXXDestructorDecl *Destructor = |
| 13515 | CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
| 13516 | QualType(), nullptr, true, |
| 13517 | true, |
| 13518 | Constexpr ? ConstexprSpecKind::Constexpr |
| 13519 | : ConstexprSpecKind::Unspecified); |
| 13520 | Destructor->setAccess(AS_public); |
| 13521 | Destructor->setDefaulted(); |
| 13522 | |
| 13523 | if (getLangOpts().CUDA) { |
| 13524 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXDestructor, |
| 13525 | Destructor, |
| 13526 | false, |
| 13527 | false); |
| 13528 | } |
| 13529 | |
| 13530 | setupImplicitSpecialMemberType(Destructor, Context.VoidTy, None); |
| 13531 | |
| 13532 | |
| 13533 | |
| 13534 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
| 13535 | Destructor->setTrivialForCall(ClassDecl->hasAttr<TrivialABIAttr>() || |
| 13536 | ClassDecl->hasTrivialDestructorForCall()); |
| 13537 | |
| 13538 | |
| 13539 | ++getASTContext().NumImplicitDestructorsDeclared; |
| 13540 | |
| 13541 | Scope *S = getScopeForContext(ClassDecl); |
| 13542 | CheckImplicitSpecialMemberDeclaration(S, Destructor); |
| 13543 | |
| 13544 | |
| 13545 | |
| 13546 | |
| 13547 | if (ClassDecl->isCompleteDefinition() && |
| 13548 | ShouldDeleteSpecialMember(Destructor, CXXDestructor)) |
| 13549 | SetDeclDeleted(Destructor, ClassLoc); |
| 13550 | |
| 13551 | |
| 13552 | if (S) |
| 13553 | PushOnScopeChains(Destructor, S, false); |
| 13554 | ClassDecl->addDecl(Destructor); |
| 13555 | |
| 13556 | return Destructor; |
| 13557 | } |
| 13558 | |
| 13559 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
| 13560 | CXXDestructorDecl *Destructor) { |
| 13561 | assert((Destructor->isDefaulted() && |
| 13562 | !Destructor->doesThisDeclarationHaveABody() && |
| 13563 | !Destructor->isDeleted()) && |
| 13564 | "DefineImplicitDestructor - call it for implicit default dtor"); |
| 13565 | if (Destructor->willHaveBody() || Destructor->isInvalidDecl()) |
| 13566 | return; |
| 13567 | |
| 13568 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
| 13569 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
| 13570 | |
| 13571 | SynthesizedFunctionScope Scope(*this, Destructor); |
| 13572 | |
| 13573 | |
| 13574 | |
| 13575 | ResolveExceptionSpec(CurrentLocation, |
| 13576 | Destructor->getType()->castAs<FunctionProtoType>()); |
| 13577 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 13578 | |
| 13579 | |
| 13580 | Scope.addContextNote(CurrentLocation); |
| 13581 | |
| 13582 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 13583 | Destructor->getParent()); |
| 13584 | |
| 13585 | if (CheckDestructor(Destructor)) { |
| 13586 | Destructor->setInvalidDecl(); |
| 13587 | return; |
| 13588 | } |
| 13589 | |
| 13590 | SourceLocation Loc = Destructor->getEndLoc().isValid() |
| 13591 | ? Destructor->getEndLoc() |
| 13592 | : Destructor->getLocation(); |
| 13593 | Destructor->setBody(new (Context) CompoundStmt(Loc)); |
| 13594 | Destructor->markUsed(Context); |
| 13595 | |
| 13596 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 13597 | L->CompletedImplicitDefinition(Destructor); |
| 13598 | } |
| 13599 | } |
| 13600 | |
| 13601 | void Sema::CheckCompleteDestructorVariant(SourceLocation CurrentLocation, |
| 13602 | CXXDestructorDecl *Destructor) { |
| 13603 | if (Destructor->isInvalidDecl()) |
| 13604 | return; |
| 13605 | |
| 13606 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
| 13607 | assert(Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 13608 | "implicit complete dtors unneeded outside MS ABI"); |
| 13609 | assert(ClassDecl->getNumVBases() > 0 && |
| 13610 | "complete dtor only exists for classes with vbases"); |
| 13611 | |
| 13612 | SynthesizedFunctionScope Scope(*this, Destructor); |
| 13613 | |
| 13614 | |
| 13615 | Scope.addContextNote(CurrentLocation); |
| 13616 | |
| 13617 | MarkVirtualBaseDestructorsReferenced(Destructor->getLocation(), ClassDecl); |
| 13618 | } |
| 13619 | |
| 13620 | |
| 13621 | |
| 13622 | void Sema::ActOnFinishCXXMemberDecls() { |
| 13623 | |
| 13624 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 13625 | if (Record->isInvalidDecl()) { |
| 13626 | DelayedOverridingExceptionSpecChecks.clear(); |
| 13627 | DelayedEquivalentExceptionSpecChecks.clear(); |
| 13628 | return; |
| 13629 | } |
| 13630 | checkForMultipleExportedDefaultConstructors(*this, Record); |
| 13631 | } |
| 13632 | } |
| 13633 | |
| 13634 | void Sema::ActOnFinishCXXNonNestedClass() { |
| 13635 | referenceDLLExportedClassMethods(); |
| 13636 | |
| 13637 | if (!DelayedDllExportMemberFunctions.empty()) { |
| 13638 | SmallVector<CXXMethodDecl*, 4> WorkList; |
| 13639 | std::swap(DelayedDllExportMemberFunctions, WorkList); |
| 13640 | for (CXXMethodDecl *M : WorkList) { |
| 13641 | DefineDefaultedFunction(*this, M, M->getLocation()); |
| 13642 | |
| 13643 | |
| 13644 | |
| 13645 | |
| 13646 | if (M->getParent()->getTemplateSpecializationKind() != |
| 13647 | TSK_ExplicitInstantiationDefinition) |
| 13648 | ActOnFinishInlineFunctionDef(M); |
| 13649 | } |
| 13650 | } |
| 13651 | } |
| 13652 | |
| 13653 | void Sema::referenceDLLExportedClassMethods() { |
| 13654 | if (!DelayedDllExportClasses.empty()) { |
| 13655 | |
| 13656 | |
| 13657 | SmallVector<CXXRecordDecl *, 4> WorkList; |
| 13658 | std::swap(DelayedDllExportClasses, WorkList); |
| 13659 | for (CXXRecordDecl *Class : WorkList) |
| 13660 | ReferenceDllExportedMembers(*this, Class); |
| 13661 | } |
| 13662 | } |
| 13663 | |
| 13664 | void Sema::AdjustDestructorExceptionSpec(CXXDestructorDecl *Destructor) { |
| 13665 | assert(getLangOpts().CPlusPlus11 && |
| 13666 | "adjusting dtor exception specs was introduced in c++11"); |
| 13667 | |
| 13668 | if (Destructor->isDependentContext()) |
| 13669 | return; |
| 13670 | |
| 13671 | |
| 13672 | |
| 13673 | |
| 13674 | |
| 13675 | const auto *DtorType = Destructor->getType()->castAs<FunctionProtoType>(); |
| 13676 | if (DtorType->hasExceptionSpec()) |
| 13677 | return; |
| 13678 | |
| 13679 | |
| 13680 | |
| 13681 | |
| 13682 | FunctionProtoType::ExtProtoInfo EPI = DtorType->getExtProtoInfo(); |
| 13683 | EPI.ExceptionSpec.Type = EST_Unevaluated; |
| 13684 | EPI.ExceptionSpec.SourceDecl = Destructor; |
| 13685 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
| 13686 | |
| 13687 | |
| 13688 | |
| 13689 | |
| 13690 | |
| 13691 | |
| 13692 | } |
| 13693 | |
| 13694 | namespace { |
| 13695 | |
| 13696 | |
| 13697 | |
| 13698 | class ExprBuilder { |
| 13699 | ExprBuilder(const ExprBuilder&) = delete; |
| 13700 | ExprBuilder &operator=(const ExprBuilder&) = delete; |
| 13701 | |
| 13702 | protected: |
| 13703 | static Expr *assertNotNull(Expr *E) { |
| 13704 | assert(E && "Expression construction must not fail."); |
| 13705 | return E; |
| 13706 | } |
| 13707 | |
| 13708 | public: |
| 13709 | ExprBuilder() {} |
| 13710 | virtual ~ExprBuilder() {} |
| 13711 | |
| 13712 | virtual Expr *build(Sema &S, SourceLocation Loc) const = 0; |
| 13713 | }; |
| 13714 | |
| 13715 | class RefBuilder: public ExprBuilder { |
| 13716 | VarDecl *Var; |
| 13717 | QualType VarType; |
| 13718 | |
| 13719 | public: |
| 13720 | Expr *build(Sema &S, SourceLocation Loc) const override { |
| 13721 | return assertNotNull(S.BuildDeclRefExpr(Var, VarType, VK_LValue, Loc)); |
| 13722 | } |
| 13723 | |
| 13724 | RefBuilder(VarDecl *Var, QualType VarType) |
| 13725 | : Var(Var), VarType(VarType) {} |
| 13726 | }; |
| 13727 | |
| 13728 | class ThisBuilder: public ExprBuilder { |
| 13729 | public: |
| 13730 | Expr *build(Sema &S, SourceLocation Loc) const override { |
| 13731 | return assertNotNull(S.ActOnCXXThis(Loc).getAs<Expr>()); |
| 13732 | } |
| 13733 | }; |
| 13734 | |
| 13735 | class CastBuilder: public ExprBuilder { |
| 13736 | const ExprBuilder &Builder; |
| 13737 | QualType Type; |
| 13738 | ExprValueKind Kind; |
| 13739 | const CXXCastPath &Path; |
| 13740 | |
| 13741 | public: |
| 13742 | Expr *build(Sema &S, SourceLocation Loc) const override { |
| 13743 | return assertNotNull(S.ImpCastExprToType(Builder.build(S, Loc), Type, |
| 13744 | CK_UncheckedDerivedToBase, Kind, |
| 13745 | &Path).get()); |
| 13746 | } |
| 13747 | |
| 13748 | CastBuilder(const ExprBuilder &Builder, QualType Type, ExprValueKind Kind, |
| 13749 | const CXXCastPath &Path) |
| 13750 | : Builder(Builder), Type(Type), Kind(Kind), Path(Path) {} |
| 13751 | }; |
| 13752 | |
| 13753 | class DerefBuilder: public ExprBuilder { |
| 13754 | const ExprBuilder &Builder; |
| 13755 | |
| 13756 | public: |
| 13757 | Expr *build(Sema &S, SourceLocation Loc) const override { |
| 13758 | return assertNotNull( |
| 13759 | S.CreateBuiltinUnaryOp(Loc, UO_Deref, Builder.build(S, Loc)).get()); |
| 13760 | } |
| 13761 | |
| 13762 | DerefBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 13763 | }; |
| 13764 | |
| 13765 | class MemberBuilder: public ExprBuilder { |
| 13766 | const ExprBuilder &Builder; |
| 13767 | QualType Type; |
| 13768 | CXXScopeSpec SS; |
| 13769 | bool IsArrow; |
| 13770 | LookupResult &MemberLookup; |
| 13771 | |
| 13772 | public: |
| 13773 | Expr *build(Sema &S, SourceLocation Loc) const override { |
| 13774 | return assertNotNull(S.BuildMemberReferenceExpr( |
| 13775 | Builder.build(S, Loc), Type, Loc, IsArrow, SS, SourceLocation(), |
| 13776 | nullptr, MemberLookup, nullptr, nullptr).get()); |
| 13777 | } |
| 13778 | |
| 13779 | MemberBuilder(const ExprBuilder &Builder, QualType Type, bool IsArrow, |
| 13780 | LookupResult &MemberLookup) |
| 13781 | : Builder(Builder), Type(Type), IsArrow(IsArrow), |
| 13782 | MemberLookup(MemberLookup) {} |
| 13783 | }; |
| 13784 | |
| 13785 | class MoveCastBuilder: public ExprBuilder { |
| 13786 | const ExprBuilder &Builder; |
| 13787 | |
| 13788 | public: |
| 13789 | Expr *build(Sema &S, SourceLocation Loc) const override { |
| 13790 | return assertNotNull(CastForMoving(S, Builder.build(S, Loc))); |
| 13791 | } |
| 13792 | |
| 13793 | MoveCastBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 13794 | }; |
| 13795 | |
| 13796 | class LvalueConvBuilder: public ExprBuilder { |
| 13797 | const ExprBuilder &Builder; |
| 13798 | |
| 13799 | public: |
| 13800 | Expr *build(Sema &S, SourceLocation Loc) const override { |
| 13801 | return assertNotNull( |
| 13802 | S.DefaultLvalueConversion(Builder.build(S, Loc)).get()); |
| 13803 | } |
| 13804 | |
| 13805 | LvalueConvBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 13806 | }; |
| 13807 | |
| 13808 | class SubscriptBuilder: public ExprBuilder { |
| 13809 | const ExprBuilder &Base; |
| 13810 | const ExprBuilder &Index; |
| 13811 | |
| 13812 | public: |
| 13813 | Expr *build(Sema &S, SourceLocation Loc) const override { |
| 13814 | return assertNotNull(S.CreateBuiltinArraySubscriptExpr( |
| 13815 | Base.build(S, Loc), Loc, Index.build(S, Loc), Loc).get()); |
| 13816 | } |
| 13817 | |
| 13818 | SubscriptBuilder(const ExprBuilder &Base, const ExprBuilder &Index) |
| 13819 | : Base(Base), Index(Index) {} |
| 13820 | }; |
| 13821 | |
| 13822 | } |
| 13823 | |
| 13824 | |
| 13825 | |
| 13826 | |
| 13827 | |
| 13828 | static StmtResult |
| 13829 | buildMemcpyForAssignmentOp(Sema &S, SourceLocation Loc, QualType T, |
| 13830 | const ExprBuilder &ToB, const ExprBuilder &FromB) { |
| 13831 | |
| 13832 | QualType SizeType = S.Context.getSizeType(); |
| 13833 | llvm::APInt Size(S.Context.getTypeSize(SizeType), |
| 13834 | S.Context.getTypeSizeInChars(T).getQuantity()); |
| 13835 | |
| 13836 | |
| 13837 | |
| 13838 | |
| 13839 | Expr *From = FromB.build(S, Loc); |
| 13840 | From = UnaryOperator::Create( |
| 13841 | S.Context, From, UO_AddrOf, S.Context.getPointerType(From->getType()), |
| 13842 | VK_PRValue, OK_Ordinary, Loc, false, S.CurFPFeatureOverrides()); |
| 13843 | Expr *To = ToB.build(S, Loc); |
| 13844 | To = UnaryOperator::Create( |
| 13845 | S.Context, To, UO_AddrOf, S.Context.getPointerType(To->getType()), |
| 13846 | VK_PRValue, OK_Ordinary, Loc, false, S.CurFPFeatureOverrides()); |
| 13847 | |
| 13848 | const Type *E = T->getBaseElementTypeUnsafe(); |
| 13849 | bool NeedsCollectableMemCpy = |
| 13850 | E->isRecordType() && |
| 13851 | E->castAs<RecordType>()->getDecl()->hasObjectMember(); |
| 13852 | |
| 13853 | |
| 13854 | StringRef MemCpyName = NeedsCollectableMemCpy ? |
| 13855 | "__builtin_objc_memmove_collectable" : |
| 13856 | "__builtin_memcpy"; |
| 13857 | LookupResult R(S, &S.Context.Idents.get(MemCpyName), Loc, |
| 13858 | Sema::LookupOrdinaryName); |
| 13859 | S.LookupName(R, S.TUScope, true); |
| 13860 | |
| 13861 | FunctionDecl *MemCpy = R.getAsSingle<FunctionDecl>(); |
| 13862 | if (!MemCpy) |
| 13863 | |
| 13864 | |
| 13865 | return StmtError(); |
| 13866 | |
| 13867 | ExprResult MemCpyRef = S.BuildDeclRefExpr(MemCpy, S.Context.BuiltinFnTy, |
| 13868 | VK_PRValue, Loc, nullptr); |
| 13869 | assert(MemCpyRef.isUsable() && "Builtin reference cannot fail"); |
| 13870 | |
| 13871 | Expr *CallArgs[] = { |
| 13872 | To, From, IntegerLiteral::Create(S.Context, Size, SizeType, Loc) |
| 13873 | }; |
| 13874 | ExprResult Call = S.BuildCallExpr(nullptr, MemCpyRef.get(), |
| 13875 | Loc, CallArgs, Loc); |
| 13876 | |
| 13877 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 13878 | return Call.getAs<Stmt>(); |
| 13879 | } |
| 13880 | |
| 13881 | |
| 13882 | |
| 13883 | |
| 13884 | |
| 13885 | |
| 13886 | |
| 13887 | |
| 13888 | |
| 13889 | |
| 13890 | |
| 13891 | |
| 13892 | |
| 13893 | |
| 13894 | |
| 13895 | |
| 13896 | |
| 13897 | |
| 13898 | |
| 13899 | |
| 13900 | |
| 13901 | |
| 13902 | |
| 13903 | |
| 13904 | |
| 13905 | |
| 13906 | |
| 13907 | |
| 13908 | static StmtResult |
| 13909 | buildSingleCopyAssignRecursively(Sema &S, SourceLocation Loc, QualType T, |
| 13910 | const ExprBuilder &To, const ExprBuilder &From, |
| 13911 | bool CopyingBaseSubobject, bool Copying, |
| 13912 | unsigned Depth = 0) { |
| 13913 | |
| 13914 | |
| 13915 | |
| 13916 | |
| 13917 | |
| 13918 | |
| 13919 | |
| 13920 | |
| 13921 | |
| 13922 | |
| 13923 | |
| 13924 | |
| 13925 | |
| 13926 | |
| 13927 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 13928 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 13929 | |
| 13930 | |
| 13931 | DeclarationName Name |
| 13932 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 13933 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 13934 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 13935 | |
| 13936 | |
| 13937 | |
| 13938 | if (!S.getLangOpts().CPlusPlus11) { |
| 13939 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 13940 | while (F.hasNext()) { |
| 13941 | NamedDecl *D = F.next(); |
| 13942 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 13943 | if (Method->isCopyAssignmentOperator() || |
| 13944 | (!Copying && Method->isMoveAssignmentOperator())) |
| 13945 | continue; |
| 13946 | |
| 13947 | F.erase(); |
| 13948 | } |
| 13949 | F.done(); |
| 13950 | } |
| 13951 | |
| 13952 | |
| 13953 | |
| 13954 | |
| 13955 | |
| 13956 | |
| 13957 | |
| 13958 | |
| 13959 | |
| 13960 | |
| 13961 | |
| 13962 | |
| 13963 | if (CopyingBaseSubobject) { |
| 13964 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 13965 | L != LEnd; ++L) { |
| 13966 | if (L.getAccess() == AS_protected) |
| 13967 | L.setAccess(AS_public); |
| 13968 | } |
| 13969 | } |
| 13970 | |
| 13971 | |
| 13972 | |
| 13973 | |
| 13974 | CXXScopeSpec SS; |
| 13975 | const Type *CanonicalT = S.Context.getCanonicalType(T.getTypePtr()); |
| 13976 | SS.MakeTrivial(S.Context, |
| 13977 | NestedNameSpecifier::Create(S.Context, nullptr, false, |
| 13978 | CanonicalT), |
| 13979 | Loc); |
| 13980 | |
| 13981 | |
| 13982 | ExprResult OpEqualRef |
| 13983 | = S.BuildMemberReferenceExpr(To.build(S, Loc), T, Loc, false, |
| 13984 | SS, SourceLocation(), |
| 13985 | nullptr, |
| 13986 | OpLookup, |
| 13987 | nullptr, nullptr, |
| 13988 | true); |
| 13989 | if (OpEqualRef.isInvalid()) |
| 13990 | return StmtError(); |
| 13991 | |
| 13992 | |
| 13993 | |
| 13994 | Expr *FromInst = From.build(S, Loc); |
| 13995 | ExprResult Call = S.BuildCallToMemberFunction(nullptr, |
| 13996 | OpEqualRef.getAs<Expr>(), |
| 13997 | Loc, FromInst, Loc); |
| 13998 | if (Call.isInvalid()) |
| 13999 | return StmtError(); |
| 14000 | |
| 14001 | |
| 14002 | |
| 14003 | CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Call.get()); |
| 14004 | if (CE && CE->getMethodDecl()->isTrivial() && Depth) |
| 14005 | return StmtResult((Stmt*)nullptr); |
| 14006 | |
| 14007 | |
| 14008 | |
| 14009 | return S.ActOnExprStmt(Call); |
| 14010 | } |
| 14011 | |
| 14012 | |
| 14013 | |
| 14014 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 14015 | if (!ArrayTy) { |
| 14016 | ExprResult Assignment = S.CreateBuiltinBinOp( |
| 14017 | Loc, BO_Assign, To.build(S, Loc), From.build(S, Loc)); |
| 14018 | if (Assignment.isInvalid()) |
| 14019 | return StmtError(); |
| 14020 | return S.ActOnExprStmt(Assignment); |
| 14021 | } |
| 14022 | |
| 14023 | |
| 14024 | |
| 14025 | |
| 14026 | |
| 14027 | |
| 14028 | |
| 14029 | |
| 14030 | |
| 14031 | QualType SizeType = S.Context.getSizeType(); |
| 14032 | |
| 14033 | |
| 14034 | IdentifierInfo *IterationVarName = nullptr; |
| 14035 | { |
| 14036 | SmallString<8> Str; |
| 14037 | llvm::raw_svector_ostream OS(Str); |
| 14038 | OS << "__i" << Depth; |
| 14039 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 14040 | } |
| 14041 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
| 14042 | IterationVarName, SizeType, |
| 14043 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
| 14044 | SC_None); |
| 14045 | |
| 14046 | |
| 14047 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
| 14048 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
| 14049 | |
| 14050 | |
| 14051 | RefBuilder IterationVarRef(IterationVar, SizeType); |
| 14052 | LvalueConvBuilder IterationVarRefRVal(IterationVarRef); |
| 14053 | |
| 14054 | |
| 14055 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 14056 | |
| 14057 | |
| 14058 | SubscriptBuilder FromIndexCopy(From, IterationVarRefRVal); |
| 14059 | MoveCastBuilder FromIndexMove(FromIndexCopy); |
| 14060 | const ExprBuilder *FromIndex; |
| 14061 | if (Copying) |
| 14062 | FromIndex = &FromIndexCopy; |
| 14063 | else |
| 14064 | FromIndex = &FromIndexMove; |
| 14065 | |
| 14066 | SubscriptBuilder ToIndex(To, IterationVarRefRVal); |
| 14067 | |
| 14068 | |
| 14069 | StmtResult Copy = |
| 14070 | buildSingleCopyAssignRecursively(S, Loc, ArrayTy->getElementType(), |
| 14071 | ToIndex, *FromIndex, CopyingBaseSubobject, |
| 14072 | Copying, Depth + 1); |
| 14073 | |
| 14074 | if (Copy.isInvalid() || !Copy.get()) |
| 14075 | return Copy; |
| 14076 | |
| 14077 | |
| 14078 | llvm::APInt Upper |
| 14079 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
| 14080 | Expr *Comparison = BinaryOperator::Create( |
| 14081 | S.Context, IterationVarRefRVal.build(S, Loc), |
| 14082 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), BO_NE, |
| 14083 | S.Context.BoolTy, VK_PRValue, OK_Ordinary, Loc, |
| 14084 | S.CurFPFeatureOverrides()); |
| 14085 | |
| 14086 | |
| 14087 | |
| 14088 | |
| 14089 | Expr *Increment = UnaryOperator::Create( |
| 14090 | S.Context, IterationVarRef.build(S, Loc), UO_PreInc, SizeType, VK_LValue, |
| 14091 | OK_Ordinary, Loc, Upper.isMaxValue(), S.CurFPFeatureOverrides()); |
| 14092 | |
| 14093 | |
| 14094 | return S.ActOnForStmt( |
| 14095 | Loc, Loc, InitStmt, |
| 14096 | S.ActOnCondition(nullptr, Loc, Comparison, Sema::ConditionKind::Boolean), |
| 14097 | S.MakeFullDiscardedValueExpr(Increment), Loc, Copy.get()); |
| 14098 | } |
| 14099 | |
| 14100 | static StmtResult |
| 14101 | buildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
| 14102 | const ExprBuilder &To, const ExprBuilder &From, |
| 14103 | bool CopyingBaseSubobject, bool Copying) { |
| 14104 | |
| 14105 | if (T->isArrayType() && !T.isConstQualified() && !T.isVolatileQualified() && |
| 14106 | T.isTriviallyCopyableType(S.Context)) |
| 14107 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 14108 | |
| 14109 | StmtResult Result(buildSingleCopyAssignRecursively(S, Loc, T, To, From, |
| 14110 | CopyingBaseSubobject, |
| 14111 | Copying, 0)); |
| 14112 | |
| 14113 | |
| 14114 | |
| 14115 | if (!Result.isInvalid() && !Result.get()) |
| 14116 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 14117 | |
| 14118 | return Result; |
| 14119 | } |
| 14120 | |
| 14121 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 14122 | |
| 14123 | |
| 14124 | |
| 14125 | |
| 14126 | assert(ClassDecl->needsImplicitCopyAssignment()); |
| 14127 | |
| 14128 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyAssignment); |
| 14129 | if (DSM.isAlreadyBeingDeclared()) |
| 14130 | return nullptr; |
| 14131 | |
| 14132 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 14133 | LangAS AS = getDefaultCXXMethodAddrSpace(); |
| 14134 | if (AS != LangAS::Default) |
| 14135 | ArgType = Context.getAddrSpaceQualType(ArgType, AS); |
| 14136 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 14137 | bool Const = ClassDecl->implicitCopyAssignmentHasConstParam(); |
| 14138 | if (Const) |
| 14139 | ArgType = ArgType.withConst(); |
| 14140 | |
| 14141 | ArgType = Context.getLValueReferenceType(ArgType); |
| 14142 | |
| 14143 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 14144 | CXXCopyAssignment, |
| 14145 | Const); |
| 14146 | |
| 14147 | |
| 14148 | |
| 14149 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 14150 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 14151 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 14152 | CXXMethodDecl *CopyAssignment = CXXMethodDecl::Create( |
| 14153 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 14154 | nullptr, SC_None, |
| 14155 | true, |
| 14156 | Constexpr ? ConstexprSpecKind::Constexpr : ConstexprSpecKind::Unspecified, |
| 14157 | SourceLocation()); |
| 14158 | CopyAssignment->setAccess(AS_public); |
| 14159 | CopyAssignment->setDefaulted(); |
| 14160 | CopyAssignment->setImplicit(); |
| 14161 | |
| 14162 | if (getLangOpts().CUDA) { |
| 14163 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXCopyAssignment, |
| 14164 | CopyAssignment, |
| 14165 | Const, |
| 14166 | false); |
| 14167 | } |
| 14168 | |
| 14169 | setupImplicitSpecialMemberType(CopyAssignment, RetType, ArgType); |
| 14170 | |
| 14171 | |
| 14172 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 14173 | ClassLoc, ClassLoc, |
| 14174 | nullptr, ArgType, |
| 14175 | nullptr, SC_None, |
| 14176 | nullptr); |
| 14177 | CopyAssignment->setParams(FromParam); |
| 14178 | |
| 14179 | CopyAssignment->setTrivial( |
| 14180 | ClassDecl->needsOverloadResolutionForCopyAssignment() |
| 14181 | ? SpecialMemberIsTrivial(CopyAssignment, CXXCopyAssignment) |
| 14182 | : ClassDecl->hasTrivialCopyAssignment()); |
| 14183 | |
| 14184 | |
| 14185 | ++getASTContext().NumImplicitCopyAssignmentOperatorsDeclared; |
| 14186 | |
| 14187 | Scope *S = getScopeForContext(ClassDecl); |
| 14188 | CheckImplicitSpecialMemberDeclaration(S, CopyAssignment); |
| 14189 | |
| 14190 | if (ShouldDeleteSpecialMember(CopyAssignment, CXXCopyAssignment)) { |
| 14191 | ClassDecl->setImplicitCopyAssignmentIsDeleted(); |
| 14192 | SetDeclDeleted(CopyAssignment, ClassLoc); |
| 14193 | } |
| 14194 | |
| 14195 | if (S) |
| 14196 | PushOnScopeChains(CopyAssignment, S, false); |
| 14197 | ClassDecl->addDecl(CopyAssignment); |
| 14198 | |
| 14199 | return CopyAssignment; |
| 14200 | } |
| 14201 | |
| 14202 | |
| 14203 | |
| 14204 | |
| 14205 | static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp) { |
| 14206 | assert(CopyOp->isImplicit()); |
| 14207 | |
| 14208 | CXXRecordDecl *RD = CopyOp->getParent(); |
| 14209 | CXXMethodDecl *UserDeclaredOperation = nullptr; |
| 14210 | |
| 14211 | |
| 14212 | |
| 14213 | if (RD->hasUserDeclaredDestructor()) { |
| 14214 | UserDeclaredOperation = RD->getDestructor(); |
| 14215 | } else if (!isa<CXXConstructorDecl>(CopyOp) && |
| 14216 | RD->hasUserDeclaredCopyConstructor() && |
| 14217 | !S.getLangOpts().MSVCCompat) { |
| 14218 | |
| 14219 | for (auto *I : RD->ctors()) { |
| 14220 | if (I->isCopyConstructor()) { |
| 14221 | UserDeclaredOperation = I; |
| 14222 | break; |
| 14223 | } |
| 14224 | } |
| 14225 | assert(UserDeclaredOperation); |
| 14226 | } else if (isa<CXXConstructorDecl>(CopyOp) && |
| 14227 | RD->hasUserDeclaredCopyAssignment() && |
| 14228 | !S.getLangOpts().MSVCCompat) { |
| 14229 | |
| 14230 | for (auto *I : RD->methods()) { |
| 14231 | if (I->isCopyAssignmentOperator()) { |
| 14232 | UserDeclaredOperation = I; |
| 14233 | break; |
| 14234 | } |
| 14235 | } |
| 14236 | assert(UserDeclaredOperation); |
| 14237 | } |
| 14238 | |
| 14239 | if (UserDeclaredOperation) { |
| 14240 | bool UDOIsUserProvided = UserDeclaredOperation->isUserProvided(); |
| 14241 | bool UDOIsDestructor = isa<CXXDestructorDecl>(UserDeclaredOperation); |
| 14242 | bool IsCopyAssignment = !isa<CXXConstructorDecl>(CopyOp); |
| 14243 | unsigned DiagID = |
| 14244 | (UDOIsUserProvided && UDOIsDestructor) |
| 14245 | ? diag::warn_deprecated_copy_with_user_provided_dtor |
| 14246 | : (UDOIsUserProvided && !UDOIsDestructor) |
| 14247 | ? diag::warn_deprecated_copy_with_user_provided_copy |
| 14248 | : (!UDOIsUserProvided && UDOIsDestructor) |
| 14249 | ? diag::warn_deprecated_copy_with_dtor |
| 14250 | : diag::warn_deprecated_copy; |
| 14251 | S.Diag(UserDeclaredOperation->getLocation(), DiagID) |
| 14252 | << RD << IsCopyAssignment; |
| 14253 | } |
| 14254 | } |
| 14255 | |
| 14256 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 14257 | CXXMethodDecl *CopyAssignOperator) { |
| 14258 | assert((CopyAssignOperator->isDefaulted() && |
| 14259 | CopyAssignOperator->isOverloadedOperator() && |
| 14260 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
| 14261 | !CopyAssignOperator->doesThisDeclarationHaveABody() && |
| 14262 | !CopyAssignOperator->isDeleted()) && |
| 14263 | "DefineImplicitCopyAssignment called for wrong function"); |
| 14264 | if (CopyAssignOperator->willHaveBody() || CopyAssignOperator->isInvalidDecl()) |
| 14265 | return; |
| 14266 | |
| 14267 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 14268 | if (ClassDecl->isInvalidDecl()) { |
| 14269 | CopyAssignOperator->setInvalidDecl(); |
| 14270 | return; |
| 14271 | } |
| 14272 | |
| 14273 | SynthesizedFunctionScope Scope(*this, CopyAssignOperator); |
| 14274 | |
| 14275 | |
| 14276 | |
| 14277 | ResolveExceptionSpec(CurrentLocation, |
| 14278 | CopyAssignOperator->getType()->castAs<FunctionProtoType>()); |
| 14279 | |
| 14280 | |
| 14281 | Scope.addContextNote(CurrentLocation); |
| 14282 | |
| 14283 | |
| 14284 | |
| 14285 | |
| 14286 | |
| 14287 | if (getLangOpts().CPlusPlus11 && CopyAssignOperator->isImplicit()) |
| 14288 | diagnoseDeprecatedCopyOperation(*this, CopyAssignOperator); |
| 14289 | |
| 14290 | |
| 14291 | |
| 14292 | |
| 14293 | |
| 14294 | |
| 14295 | |
| 14296 | |
| 14297 | |
| 14298 | |
| 14299 | SmallVector<Stmt*, 8> Statements; |
| 14300 | |
| 14301 | |
| 14302 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 14303 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 14304 | QualType OtherRefType = Other->getType(); |
| 14305 | if (const LValueReferenceType *OtherRef |
| 14306 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 14307 | OtherRefType = OtherRef->getPointeeType(); |
| 14308 | OtherQuals = OtherRefType.getQualifiers(); |
| 14309 | } |
| 14310 | |
| 14311 | |
| 14312 | SourceLocation Loc = CopyAssignOperator->getEndLoc().isValid() |
| 14313 | ? CopyAssignOperator->getEndLoc() |
| 14314 | : CopyAssignOperator->getLocation(); |
| 14315 | |
| 14316 | |
| 14317 | RefBuilder OtherRef(Other, OtherRefType); |
| 14318 | |
| 14319 | |
| 14320 | ThisBuilder This; |
| 14321 | |
| 14322 | |
| 14323 | bool Invalid = false; |
| 14324 | for (auto &Base : ClassDecl->bases()) { |
| 14325 | |
| 14326 | |
| 14327 | QualType BaseType = Base.getType().getUnqualifiedType(); |
| 14328 | if (!BaseType->isRecordType()) { |
| 14329 | Invalid = true; |
| 14330 | continue; |
| 14331 | } |
| 14332 | |
| 14333 | CXXCastPath BasePath; |
| 14334 | BasePath.push_back(&Base); |
| 14335 | |
| 14336 | |
| 14337 | |
| 14338 | CastBuilder From(OtherRef, Context.getQualifiedType(BaseType, OtherQuals), |
| 14339 | VK_LValue, BasePath); |
| 14340 | |
| 14341 | |
| 14342 | DerefBuilder DerefThis(This); |
| 14343 | CastBuilder To(DerefThis, |
| 14344 | Context.getQualifiedType( |
| 14345 | BaseType, CopyAssignOperator->getMethodQualifiers()), |
| 14346 | VK_LValue, BasePath); |
| 14347 | |
| 14348 | |
| 14349 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, BaseType, |
| 14350 | To, From, |
| 14351 | true, |
| 14352 | true); |
| 14353 | if (Copy.isInvalid()) { |
| 14354 | CopyAssignOperator->setInvalidDecl(); |
| 14355 | return; |
| 14356 | } |
| 14357 | |
| 14358 | |
| 14359 | Statements.push_back(Copy.getAs<Expr>()); |
| 14360 | } |
| 14361 | |
| 14362 | |
| 14363 | for (auto *Field : ClassDecl->fields()) { |
| 14364 | |
| 14365 | |
| 14366 | if (Field->isUnnamedBitfield() || Field->getParent()->isUnion()) |
| 14367 | continue; |
| 14368 | |
| 14369 | if (Field->isInvalidDecl()) { |
| 14370 | Invalid = true; |
| 14371 | continue; |
| 14372 | } |
| 14373 | |
| 14374 | |
| 14375 | if (Field->getType()->isReferenceType()) { |
| 14376 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 14377 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 14378 | Diag(Field->getLocation(), diag::note_declared_at); |
| 14379 | Invalid = true; |
| 14380 | continue; |
| 14381 | } |
| 14382 | |
| 14383 | |
| 14384 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 14385 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 14386 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 14387 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 14388 | Diag(Field->getLocation(), diag::note_declared_at); |
| 14389 | Invalid = true; |
| 14390 | continue; |
| 14391 | } |
| 14392 | |
| 14393 | |
| 14394 | if (Field->isZeroLengthBitField(Context)) |
| 14395 | continue; |
| 14396 | |
| 14397 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 14398 | if (FieldType->isIncompleteArrayType()) { |
| 14399 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 14400 | "Incomplete array type is not valid"); |
| 14401 | continue; |
| 14402 | } |
| 14403 | |
| 14404 | |
| 14405 | CXXScopeSpec SS; |
| 14406 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 14407 | LookupMemberName); |
| 14408 | MemberLookup.addDecl(Field); |
| 14409 | MemberLookup.resolveKind(); |
| 14410 | |
| 14411 | MemberBuilder From(OtherRef, OtherRefType, false, MemberLookup); |
| 14412 | |
| 14413 | MemberBuilder To(This, getCurrentThisType(), true, MemberLookup); |
| 14414 | |
| 14415 | |
| 14416 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, FieldType, |
| 14417 | To, From, |
| 14418 | false, |
| 14419 | true); |
| 14420 | if (Copy.isInvalid()) { |
| 14421 | CopyAssignOperator->setInvalidDecl(); |
| 14422 | return; |
| 14423 | } |
| 14424 | |
| 14425 | |
| 14426 | Statements.push_back(Copy.getAs<Stmt>()); |
| 14427 | } |
| 14428 | |
| 14429 | if (!Invalid) { |
| 14430 | |
| 14431 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This.build(*this, Loc)); |
| 14432 | |
| 14433 | StmtResult Return = BuildReturnStmt(Loc, ThisObj.get()); |
| 14434 | if (Return.isInvalid()) |
| 14435 | Invalid = true; |
| 14436 | else |
| 14437 | Statements.push_back(Return.getAs<Stmt>()); |
| 14438 | } |
| 14439 | |
| 14440 | if (Invalid) { |
| 14441 | CopyAssignOperator->setInvalidDecl(); |
| 14442 | return; |
| 14443 | } |
| 14444 | |
| 14445 | StmtResult Body; |
| 14446 | { |
| 14447 | CompoundScopeRAII CompoundScope(*this); |
| 14448 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
| 14449 | false); |
| 14450 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 14451 | } |
| 14452 | CopyAssignOperator->setBody(Body.getAs<Stmt>()); |
| 14453 | CopyAssignOperator->markUsed(Context); |
| 14454 | |
| 14455 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 14456 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 14457 | } |
| 14458 | } |
| 14459 | |
| 14460 | CXXMethodDecl *Sema::DeclareImplicitMoveAssignment(CXXRecordDecl *ClassDecl) { |
| 14461 | assert(ClassDecl->needsImplicitMoveAssignment()); |
| 14462 | |
| 14463 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveAssignment); |
| 14464 | if (DSM.isAlreadyBeingDeclared()) |
| 14465 | return nullptr; |
| 14466 | |
| 14467 | |
| 14468 | |
| 14469 | |
| 14470 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 14471 | LangAS AS = getDefaultCXXMethodAddrSpace(); |
| 14472 | if (AS != LangAS::Default) |
| 14473 | ArgType = Context.getAddrSpaceQualType(ArgType, AS); |
| 14474 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 14475 | ArgType = Context.getRValueReferenceType(ArgType); |
| 14476 | |
| 14477 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 14478 | CXXMoveAssignment, |
| 14479 | false); |
| 14480 | |
| 14481 | |
| 14482 | |
| 14483 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 14484 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 14485 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 14486 | CXXMethodDecl *MoveAssignment = CXXMethodDecl::Create( |
| 14487 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 14488 | nullptr, SC_None, |
| 14489 | true, |
| 14490 | Constexpr ? ConstexprSpecKind::Constexpr : ConstexprSpecKind::Unspecified, |
| 14491 | SourceLocation()); |
| 14492 | MoveAssignment->setAccess(AS_public); |
| 14493 | MoveAssignment->setDefaulted(); |
| 14494 | MoveAssignment->setImplicit(); |
| 14495 | |
| 14496 | if (getLangOpts().CUDA) { |
| 14497 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXMoveAssignment, |
| 14498 | MoveAssignment, |
| 14499 | false, |
| 14500 | false); |
| 14501 | } |
| 14502 | |
| 14503 | setupImplicitSpecialMemberType(MoveAssignment, RetType, ArgType); |
| 14504 | |
| 14505 | |
| 14506 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveAssignment, |
| 14507 | ClassLoc, ClassLoc, |
| 14508 | nullptr, ArgType, |
| 14509 | nullptr, SC_None, |
| 14510 | nullptr); |
| 14511 | MoveAssignment->setParams(FromParam); |
| 14512 | |
| 14513 | MoveAssignment->setTrivial( |
| 14514 | ClassDecl->needsOverloadResolutionForMoveAssignment() |
| 14515 | ? SpecialMemberIsTrivial(MoveAssignment, CXXMoveAssignment) |
| 14516 | : ClassDecl->hasTrivialMoveAssignment()); |
| 14517 | |
| 14518 | |
| 14519 | ++getASTContext().NumImplicitMoveAssignmentOperatorsDeclared; |
| 14520 | |
| 14521 | Scope *S = getScopeForContext(ClassDecl); |
| 14522 | CheckImplicitSpecialMemberDeclaration(S, MoveAssignment); |
| 14523 | |
| 14524 | if (ShouldDeleteSpecialMember(MoveAssignment, CXXMoveAssignment)) { |
| 14525 | ClassDecl->setImplicitMoveAssignmentIsDeleted(); |
| 14526 | SetDeclDeleted(MoveAssignment, ClassLoc); |
| 14527 | } |
| 14528 | |
| 14529 | if (S) |
| 14530 | PushOnScopeChains(MoveAssignment, S, false); |
| 14531 | ClassDecl->addDecl(MoveAssignment); |
| 14532 | |
| 14533 | return MoveAssignment; |
| 14534 | } |
| 14535 | |
| 14536 | |
| 14537 | |
| 14538 | |
| 14539 | static void checkMoveAssignmentForRepeatedMove(Sema &S, CXXRecordDecl *Class, |
| 14540 | SourceLocation CurrentLocation) { |
| 14541 | assert(!Class->isDependentContext() && "should not define dependent move"); |
| 14542 | |
| 14543 | |
| 14544 | |
| 14545 | |
| 14546 | |
| 14547 | if (Class->getNumVBases() == 0 || Class->hasTrivialMoveAssignment() || |
| 14548 | Class->getNumBases() < 2) |
| 14549 | return; |
| 14550 | |
| 14551 | llvm::SmallVector<CXXBaseSpecifier *, 16> Worklist; |
| 14552 | typedef llvm::DenseMap<CXXRecordDecl*, CXXBaseSpecifier*> VBaseMap; |
| 14553 | VBaseMap VBases; |
| 14554 | |
| 14555 | for (auto &BI : Class->bases()) { |
| 14556 | Worklist.push_back(&BI); |
| 14557 | while (!Worklist.empty()) { |
| 14558 | CXXBaseSpecifier *BaseSpec = Worklist.pop_back_val(); |
| 14559 | CXXRecordDecl *Base = BaseSpec->getType()->getAsCXXRecordDecl(); |
| 14560 | |
| 14561 | |
| 14562 | |
| 14563 | if (!Base->hasNonTrivialMoveAssignment()) |
| 14564 | continue; |
| 14565 | |
| 14566 | |
| 14567 | if (!BaseSpec->isVirtual() && !Base->getNumVBases()) |
| 14568 | continue; |
| 14569 | |
| 14570 | |
| 14571 | |
| 14572 | Sema::SpecialMemberOverloadResult SMOR = |
| 14573 | S.LookupSpecialMember(Base, Sema::CXXMoveAssignment, |
| 14574 | false, false, |
| 14575 | true, false, |
| 14576 | false); |
| 14577 | if (!SMOR.getMethod() || SMOR.getMethod()->isTrivial() || |
| 14578 | !SMOR.getMethod()->isMoveAssignmentOperator()) |
| 14579 | continue; |
| 14580 | |
| 14581 | if (BaseSpec->isVirtual()) { |
| 14582 | |
| 14583 | |
| 14584 | |
| 14585 | |
| 14586 | |
| 14587 | CXXBaseSpecifier *&Existing = |
| 14588 | VBases.insert(std::make_pair(Base->getCanonicalDecl(), &BI)) |
| 14589 | .first->second; |
| 14590 | if (Existing && Existing != &BI) { |
| 14591 | S.Diag(CurrentLocation, diag::warn_vbase_moved_multiple_times) |
| 14592 | << Class << Base; |
| 14593 | S.Diag(Existing->getBeginLoc(), diag::note_vbase_moved_here) |
| 14594 | << (Base->getCanonicalDecl() == |
| 14595 | Existing->getType()->getAsCXXRecordDecl()->getCanonicalDecl()) |
| 14596 | << Base << Existing->getType() << Existing->getSourceRange(); |
| 14597 | S.Diag(BI.getBeginLoc(), diag::note_vbase_moved_here) |
| 14598 | << (Base->getCanonicalDecl() == |
| 14599 | BI.getType()->getAsCXXRecordDecl()->getCanonicalDecl()) |
| 14600 | << Base << BI.getType() << BaseSpec->getSourceRange(); |
| 14601 | |
| 14602 | |
| 14603 | Existing = nullptr; |
| 14604 | } |
| 14605 | } else { |
| 14606 | |
| 14607 | |
| 14608 | |
| 14609 | if (!SMOR.getMethod()->isDefaulted()) |
| 14610 | continue; |
| 14611 | |
| 14612 | |
| 14613 | for (auto &BI : Base->bases()) |
| 14614 | Worklist.push_back(&BI); |
| 14615 | } |
| 14616 | } |
| 14617 | } |
| 14618 | } |
| 14619 | |
| 14620 | void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, |
| 14621 | CXXMethodDecl *MoveAssignOperator) { |
| 14622 | assert((MoveAssignOperator->isDefaulted() && |
| 14623 | MoveAssignOperator->isOverloadedOperator() && |
| 14624 | MoveAssignOperator->getOverloadedOperator() == OO_Equal && |
| 14625 | !MoveAssignOperator->doesThisDeclarationHaveABody() && |
| 14626 | !MoveAssignOperator->isDeleted()) && |
| 14627 | "DefineImplicitMoveAssignment called for wrong function"); |
| 14628 | if (MoveAssignOperator->willHaveBody() || MoveAssignOperator->isInvalidDecl()) |
| 14629 | return; |
| 14630 | |
| 14631 | CXXRecordDecl *ClassDecl = MoveAssignOperator->getParent(); |
| 14632 | if (ClassDecl->isInvalidDecl()) { |
| 14633 | MoveAssignOperator->setInvalidDecl(); |
| 14634 | return; |
| 14635 | } |
| 14636 | |
| 14637 | |
| 14638 | |
| 14639 | |
| 14640 | |
| 14641 | |
| 14642 | |
| 14643 | |
| 14644 | |
| 14645 | |
| 14646 | |
| 14647 | checkMoveAssignmentForRepeatedMove(*this, ClassDecl, CurrentLocation); |
| 14648 | |
| 14649 | SynthesizedFunctionScope Scope(*this, MoveAssignOperator); |
| 14650 | |
| 14651 | |
| 14652 | |
| 14653 | ResolveExceptionSpec(CurrentLocation, |
| 14654 | MoveAssignOperator->getType()->castAs<FunctionProtoType>()); |
| 14655 | |
| 14656 | |
| 14657 | Scope.addContextNote(CurrentLocation); |
| 14658 | |
| 14659 | |
| 14660 | SmallVector<Stmt*, 8> Statements; |
| 14661 | |
| 14662 | |
| 14663 | ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); |
| 14664 | QualType OtherRefType = |
| 14665 | Other->getType()->castAs<RValueReferenceType>()->getPointeeType(); |
| 14666 | |
| 14667 | |
| 14668 | SourceLocation Loc = MoveAssignOperator->getEndLoc().isValid() |
| 14669 | ? MoveAssignOperator->getEndLoc() |
| 14670 | : MoveAssignOperator->getLocation(); |
| 14671 | |
| 14672 | |
| 14673 | RefBuilder OtherRef(Other, OtherRefType); |
| 14674 | |
| 14675 | MoveCastBuilder MoveOther(OtherRef); |
| 14676 | |
| 14677 | |
| 14678 | ThisBuilder This; |
| 14679 | |
| 14680 | |
| 14681 | bool Invalid = false; |
| 14682 | for (auto &Base : ClassDecl->bases()) { |
| 14683 | |
| 14684 | |
| 14685 | |
| 14686 | |
| 14687 | |
| 14688 | |
| 14689 | |
| 14690 | |
| 14691 | |
| 14692 | |
| 14693 | QualType BaseType = Base.getType().getUnqualifiedType(); |
| 14694 | if (!BaseType->isRecordType()) { |
| 14695 | Invalid = true; |
| 14696 | continue; |
| 14697 | } |
| 14698 | |
| 14699 | CXXCastPath BasePath; |
| 14700 | BasePath.push_back(&Base); |
| 14701 | |
| 14702 | |
| 14703 | |
| 14704 | CastBuilder From(OtherRef, BaseType, VK_XValue, BasePath); |
| 14705 | |
| 14706 | |
| 14707 | DerefBuilder DerefThis(This); |
| 14708 | |
| 14709 | |
| 14710 | CastBuilder To(DerefThis, |
| 14711 | Context.getQualifiedType( |
| 14712 | BaseType, MoveAssignOperator->getMethodQualifiers()), |
| 14713 | VK_LValue, BasePath); |
| 14714 | |
| 14715 | |
| 14716 | StmtResult Move = buildSingleCopyAssign(*this, Loc, BaseType, |
| 14717 | To, From, |
| 14718 | true, |
| 14719 | false); |
| 14720 | if (Move.isInvalid()) { |
| 14721 | MoveAssignOperator->setInvalidDecl(); |
| 14722 | return; |
| 14723 | } |
| 14724 | |
| 14725 | |
| 14726 | Statements.push_back(Move.getAs<Expr>()); |
| 14727 | } |
| 14728 | |
| 14729 | |
| 14730 | for (auto *Field : ClassDecl->fields()) { |
| 14731 | |
| 14732 | |
| 14733 | if (Field->isUnnamedBitfield() || Field->getParent()->isUnion()) |
| 14734 | continue; |
| 14735 | |
| 14736 | if (Field->isInvalidDecl()) { |
| 14737 | Invalid = true; |
| 14738 | continue; |
| 14739 | } |
| 14740 | |
| 14741 | |
| 14742 | if (Field->getType()->isReferenceType()) { |
| 14743 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 14744 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 14745 | Diag(Field->getLocation(), diag::note_declared_at); |
| 14746 | Invalid = true; |
| 14747 | continue; |
| 14748 | } |
| 14749 | |
| 14750 | |
| 14751 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 14752 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 14753 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 14754 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 14755 | Diag(Field->getLocation(), diag::note_declared_at); |
| 14756 | Invalid = true; |
| 14757 | continue; |
| 14758 | } |
| 14759 | |
| 14760 | |
| 14761 | if (Field->isZeroLengthBitField(Context)) |
| 14762 | continue; |
| 14763 | |
| 14764 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 14765 | if (FieldType->isIncompleteArrayType()) { |
| 14766 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 14767 | "Incomplete array type is not valid"); |
| 14768 | continue; |
| 14769 | } |
| 14770 | |
| 14771 | |
| 14772 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 14773 | LookupMemberName); |
| 14774 | MemberLookup.addDecl(Field); |
| 14775 | MemberLookup.resolveKind(); |
| 14776 | MemberBuilder From(MoveOther, OtherRefType, |
| 14777 | false, MemberLookup); |
| 14778 | MemberBuilder To(This, getCurrentThisType(), |
| 14779 | true, MemberLookup); |
| 14780 | |
| 14781 | assert(!From.build(*this, Loc)->isLValue() && |
| 14782 | "Member reference with rvalue base must be rvalue except for reference " |
| 14783 | "members, which aren't allowed for move assignment."); |
| 14784 | |
| 14785 | |
| 14786 | StmtResult Move = buildSingleCopyAssign(*this, Loc, FieldType, |
| 14787 | To, From, |
| 14788 | false, |
| 14789 | false); |
| 14790 | if (Move.isInvalid()) { |
| 14791 | MoveAssignOperator->setInvalidDecl(); |
| 14792 | return; |
| 14793 | } |
| 14794 | |
| 14795 | |
| 14796 | Statements.push_back(Move.getAs<Stmt>()); |
| 14797 | } |
| 14798 | |
| 14799 | if (!Invalid) { |
| 14800 | |
| 14801 | ExprResult ThisObj = |
| 14802 | CreateBuiltinUnaryOp(Loc, UO_Deref, This.build(*this, Loc)); |
| 14803 | |
| 14804 | StmtResult Return = BuildReturnStmt(Loc, ThisObj.get()); |
| 14805 | if (Return.isInvalid()) |
| 14806 | Invalid = true; |
| 14807 | else |
| 14808 | Statements.push_back(Return.getAs<Stmt>()); |
| 14809 | } |
| 14810 | |
| 14811 | if (Invalid) { |
| 14812 | MoveAssignOperator->setInvalidDecl(); |
| 14813 | return; |
| 14814 | } |
| 14815 | |
| 14816 | StmtResult Body; |
| 14817 | { |
| 14818 | CompoundScopeRAII CompoundScope(*this); |
| 14819 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
| 14820 | false); |
| 14821 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 14822 | } |
| 14823 | MoveAssignOperator->setBody(Body.getAs<Stmt>()); |
| 14824 | MoveAssignOperator->markUsed(Context); |
| 14825 | |
| 14826 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 14827 | L->CompletedImplicitDefinition(MoveAssignOperator); |
| 14828 | } |
| 14829 | } |
| 14830 | |
| 14831 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 14832 | CXXRecordDecl *ClassDecl) { |
| 14833 | |
| 14834 | |
| 14835 | |
| 14836 | assert(ClassDecl->needsImplicitCopyConstructor()); |
| 14837 | |
| 14838 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyConstructor); |
| 14839 | if (DSM.isAlreadyBeingDeclared()) |
| 14840 | return nullptr; |
| 14841 | |
| 14842 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 14843 | QualType ArgType = ClassType; |
| 14844 | bool Const = ClassDecl->implicitCopyConstructorHasConstParam(); |
| 14845 | if (Const) |
| 14846 | ArgType = ArgType.withConst(); |
| 14847 | |
| 14848 | LangAS AS = getDefaultCXXMethodAddrSpace(); |
| 14849 | if (AS != LangAS::Default) |
| 14850 | ArgType = Context.getAddrSpaceQualType(ArgType, AS); |
| 14851 | |
| 14852 | ArgType = Context.getLValueReferenceType(ArgType); |
| 14853 | |
| 14854 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 14855 | CXXCopyConstructor, |
| 14856 | Const); |
| 14857 | |
| 14858 | DeclarationName Name |
| 14859 | = Context.DeclarationNames.getCXXConstructorName( |
| 14860 | Context.getCanonicalType(ClassType)); |
| 14861 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 14862 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 14863 | |
| 14864 | |
| 14865 | |
| 14866 | CXXConstructorDecl *CopyConstructor = CXXConstructorDecl::Create( |
| 14867 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), nullptr, |
| 14868 | ExplicitSpecifier(), |
| 14869 | true, |
| 14870 | true, |
| 14871 | Constexpr ? ConstexprSpecKind::Constexpr |
| 14872 | : ConstexprSpecKind::Unspecified); |
| 14873 | CopyConstructor->setAccess(AS_public); |
| 14874 | CopyConstructor->setDefaulted(); |
| 14875 | |
| 14876 | if (getLangOpts().CUDA) { |
| 14877 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXCopyConstructor, |
| 14878 | CopyConstructor, |
| 14879 | Const, |
| 14880 | false); |
| 14881 | } |
| 14882 | |
| 14883 | setupImplicitSpecialMemberType(CopyConstructor, Context.VoidTy, ArgType); |
| 14884 | |
| 14885 | |
| 14886 | |
| 14887 | |
| 14888 | TypeSourceInfo *TSI = nullptr; |
| 14889 | if (inTemplateInstantiation() && ClassDecl->isLambda()) |
| 14890 | TSI = Context.getTrivialTypeSourceInfo(ArgType); |
| 14891 | |
| 14892 | |
| 14893 | ParmVarDecl *FromParam = |
| 14894 | ParmVarDecl::Create(Context, CopyConstructor, ClassLoc, ClassLoc, |
| 14895 | nullptr, ArgType, |
| 14896 | TSI, SC_None, nullptr); |
| 14897 | CopyConstructor->setParams(FromParam); |
| 14898 | |
| 14899 | CopyConstructor->setTrivial( |
| 14900 | ClassDecl->needsOverloadResolutionForCopyConstructor() |
| 14901 | ? SpecialMemberIsTrivial(CopyConstructor, CXXCopyConstructor) |
| 14902 | : ClassDecl->hasTrivialCopyConstructor()); |
| 14903 | |
| 14904 | CopyConstructor->setTrivialForCall( |
| 14905 | ClassDecl->hasAttr<TrivialABIAttr>() || |
| 14906 | (ClassDecl->needsOverloadResolutionForCopyConstructor() |
| 14907 | ? SpecialMemberIsTrivial(CopyConstructor, CXXCopyConstructor, |
| 14908 | TAH_ConsiderTrivialABI) |
| 14909 | : ClassDecl->hasTrivialCopyConstructorForCall())); |
| 14910 | |
| 14911 | |
| 14912 | ++getASTContext().NumImplicitCopyConstructorsDeclared; |
| 14913 | |
| 14914 | Scope *S = getScopeForContext(ClassDecl); |
| 14915 | CheckImplicitSpecialMemberDeclaration(S, CopyConstructor); |
| 14916 | |
| 14917 | if (ShouldDeleteSpecialMember(CopyConstructor, CXXCopyConstructor)) { |
| 14918 | ClassDecl->setImplicitCopyConstructorIsDeleted(); |
| 14919 | SetDeclDeleted(CopyConstructor, ClassLoc); |
| 14920 | } |
| 14921 | |
| 14922 | if (S) |
| 14923 | PushOnScopeChains(CopyConstructor, S, false); |
| 14924 | ClassDecl->addDecl(CopyConstructor); |
| 14925 | |
| 14926 | return CopyConstructor; |
| 14927 | } |
| 14928 | |
| 14929 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 14930 | CXXConstructorDecl *CopyConstructor) { |
| 14931 | assert((CopyConstructor->isDefaulted() && |
| 14932 | CopyConstructor->isCopyConstructor() && |
| 14933 | !CopyConstructor->doesThisDeclarationHaveABody() && |
| 14934 | !CopyConstructor->isDeleted()) && |
| 14935 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
| 14936 | if (CopyConstructor->willHaveBody() || CopyConstructor->isInvalidDecl()) |
| 14937 | return; |
| 14938 | |
| 14939 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
| 14940 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
| 14941 | |
| 14942 | SynthesizedFunctionScope Scope(*this, CopyConstructor); |
| 14943 | |
| 14944 | |
| 14945 | |
| 14946 | ResolveExceptionSpec(CurrentLocation, |
| 14947 | CopyConstructor->getType()->castAs<FunctionProtoType>()); |
| 14948 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 14949 | |
| 14950 | |
| 14951 | Scope.addContextNote(CurrentLocation); |
| 14952 | |
| 14953 | |
| 14954 | |
| 14955 | |
| 14956 | |
| 14957 | if (getLangOpts().CPlusPlus11 && CopyConstructor->isImplicit()) |
| 14958 | diagnoseDeprecatedCopyOperation(*this, CopyConstructor); |
| 14959 | |
| 14960 | if (SetCtorInitializers(CopyConstructor, false)) { |
| 14961 | CopyConstructor->setInvalidDecl(); |
| 14962 | } else { |
| 14963 | SourceLocation Loc = CopyConstructor->getEndLoc().isValid() |
| 14964 | ? CopyConstructor->getEndLoc() |
| 14965 | : CopyConstructor->getLocation(); |
| 14966 | Sema::CompoundScopeRAII CompoundScope(*this); |
| 14967 | CopyConstructor->setBody( |
| 14968 | ActOnCompoundStmt(Loc, Loc, None, false).getAs<Stmt>()); |
| 14969 | CopyConstructor->markUsed(Context); |
| 14970 | } |
| 14971 | |
| 14972 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 14973 | L->CompletedImplicitDefinition(CopyConstructor); |
| 14974 | } |
| 14975 | } |
| 14976 | |
| 14977 | CXXConstructorDecl *Sema::DeclareImplicitMoveConstructor( |
| 14978 | CXXRecordDecl *ClassDecl) { |
| 14979 | assert(ClassDecl->needsImplicitMoveConstructor()); |
| 14980 | |
| 14981 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveConstructor); |
| 14982 | if (DSM.isAlreadyBeingDeclared()) |
| 14983 | return nullptr; |
| 14984 | |
| 14985 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 14986 | |
| 14987 | QualType ArgType = ClassType; |
| 14988 | LangAS AS = getDefaultCXXMethodAddrSpace(); |
| 14989 | if (AS != LangAS::Default) |
| 14990 | ArgType = Context.getAddrSpaceQualType(ClassType, AS); |
| 14991 | ArgType = Context.getRValueReferenceType(ArgType); |
| 14992 | |
| 14993 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 14994 | CXXMoveConstructor, |
| 14995 | false); |
| 14996 | |
| 14997 | DeclarationName Name |
| 14998 | = Context.DeclarationNames.getCXXConstructorName( |
| 14999 | Context.getCanonicalType(ClassType)); |
| 15000 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 15001 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 15002 | |
| 15003 | |
| 15004 | |
| 15005 | |
| 15006 | CXXConstructorDecl *MoveConstructor = CXXConstructorDecl::Create( |
| 15007 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), nullptr, |
| 15008 | ExplicitSpecifier(), |
| 15009 | true, |
| 15010 | true, |
| 15011 | Constexpr ? ConstexprSpecKind::Constexpr |
| 15012 | : ConstexprSpecKind::Unspecified); |
| 15013 | MoveConstructor->setAccess(AS_public); |
| 15014 | MoveConstructor->setDefaulted(); |
| 15015 | |
| 15016 | if (getLangOpts().CUDA) { |
| 15017 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXMoveConstructor, |
| 15018 | MoveConstructor, |
| 15019 | false, |
| 15020 | false); |
| 15021 | } |
| 15022 | |
| 15023 | setupImplicitSpecialMemberType(MoveConstructor, Context.VoidTy, ArgType); |
| 15024 | |
| 15025 | |
| 15026 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveConstructor, |
| 15027 | ClassLoc, ClassLoc, |
| 15028 | nullptr, |
| 15029 | ArgType, nullptr, |
| 15030 | SC_None, nullptr); |
| 15031 | MoveConstructor->setParams(FromParam); |
| 15032 | |
| 15033 | MoveConstructor->setTrivial( |
| 15034 | ClassDecl->needsOverloadResolutionForMoveConstructor() |
| 15035 | ? SpecialMemberIsTrivial(MoveConstructor, CXXMoveConstructor) |
| 15036 | : ClassDecl->hasTrivialMoveConstructor()); |
| 15037 | |
| 15038 | MoveConstructor->setTrivialForCall( |
| 15039 | ClassDecl->hasAttr<TrivialABIAttr>() || |
| 15040 | (ClassDecl->needsOverloadResolutionForMoveConstructor() |
| 15041 | ? SpecialMemberIsTrivial(MoveConstructor, CXXMoveConstructor, |
| 15042 | TAH_ConsiderTrivialABI) |
| 15043 | : ClassDecl->hasTrivialMoveConstructorForCall())); |
| 15044 | |
| 15045 | |
| 15046 | ++getASTContext().NumImplicitMoveConstructorsDeclared; |
| 15047 | |
| 15048 | Scope *S = getScopeForContext(ClassDecl); |
| 15049 | CheckImplicitSpecialMemberDeclaration(S, MoveConstructor); |
| 15050 | |
| 15051 | if (ShouldDeleteSpecialMember(MoveConstructor, CXXMoveConstructor)) { |
| 15052 | ClassDecl->setImplicitMoveConstructorIsDeleted(); |
| 15053 | SetDeclDeleted(MoveConstructor, ClassLoc); |
| 15054 | } |
| 15055 | |
| 15056 | if (S) |
| 15057 | PushOnScopeChains(MoveConstructor, S, false); |
| 15058 | ClassDecl->addDecl(MoveConstructor); |
| 15059 | |
| 15060 | return MoveConstructor; |
| 15061 | } |
| 15062 | |
| 15063 | void Sema::DefineImplicitMoveConstructor(SourceLocation CurrentLocation, |
| 15064 | CXXConstructorDecl *MoveConstructor) { |
| 15065 | assert((MoveConstructor->isDefaulted() && |
| 15066 | MoveConstructor->isMoveConstructor() && |
| 15067 | !MoveConstructor->doesThisDeclarationHaveABody() && |
| 15068 | !MoveConstructor->isDeleted()) && |
| 15069 | "DefineImplicitMoveConstructor - call it for implicit move ctor"); |
| 15070 | if (MoveConstructor->willHaveBody() || MoveConstructor->isInvalidDecl()) |
| 15071 | return; |
| 15072 | |
| 15073 | CXXRecordDecl *ClassDecl = MoveConstructor->getParent(); |
| 15074 | assert(ClassDecl && "DefineImplicitMoveConstructor - invalid constructor"); |
| 15075 | |
| 15076 | SynthesizedFunctionScope Scope(*this, MoveConstructor); |
| 15077 | |
| 15078 | |
| 15079 | |
| 15080 | ResolveExceptionSpec(CurrentLocation, |
| 15081 | MoveConstructor->getType()->castAs<FunctionProtoType>()); |
| 15082 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 15083 | |
| 15084 | |
| 15085 | Scope.addContextNote(CurrentLocation); |
| 15086 | |
| 15087 | if (SetCtorInitializers(MoveConstructor, false)) { |
| 15088 | MoveConstructor->setInvalidDecl(); |
| 15089 | } else { |
| 15090 | SourceLocation Loc = MoveConstructor->getEndLoc().isValid() |
| 15091 | ? MoveConstructor->getEndLoc() |
| 15092 | : MoveConstructor->getLocation(); |
| 15093 | Sema::CompoundScopeRAII CompoundScope(*this); |
| 15094 | MoveConstructor->setBody(ActOnCompoundStmt( |
| 15095 | Loc, Loc, None, false).getAs<Stmt>()); |
| 15096 | MoveConstructor->markUsed(Context); |
| 15097 | } |
| 15098 | |
| 15099 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 15100 | L->CompletedImplicitDefinition(MoveConstructor); |
| 15101 | } |
| 15102 | } |
| 15103 | |
| 15104 | bool Sema::isImplicitlyDeleted(FunctionDecl *FD) { |
| 15105 | return FD->isDeleted() && FD->isDefaulted() && isa<CXXMethodDecl>(FD); |
| 15106 | } |
| 15107 | |
| 15108 | void Sema::DefineImplicitLambdaToFunctionPointerConversion( |
| 15109 | SourceLocation CurrentLocation, |
| 15110 | CXXConversionDecl *Conv) { |
| 15111 | SynthesizedFunctionScope Scope(*this, Conv); |
| 15112 | assert(!Conv->getReturnType()->isUndeducedType()); |
| 15113 | |
| 15114 | QualType ConvRT = Conv->getType()->castAs<FunctionType>()->getReturnType(); |
| 15115 | CallingConv CC = |
| 15116 | ConvRT->getPointeeType()->castAs<FunctionType>()->getCallConv(); |
| 15117 | |
| 15118 | CXXRecordDecl *Lambda = Conv->getParent(); |
| 15119 | FunctionDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 15120 | FunctionDecl *Invoker = Lambda->getLambdaStaticInvoker(CC); |
| 15121 | |
| 15122 | if (auto *TemplateArgs = Conv->getTemplateSpecializationArgs()) { |
| 15123 | CallOp = InstantiateFunctionDeclaration( |
| 15124 | CallOp->getDescribedFunctionTemplate(), TemplateArgs, CurrentLocation); |
| 15125 | if (!CallOp) |
| 15126 | return; |
| 15127 | |
| 15128 | Invoker = InstantiateFunctionDeclaration( |
| 15129 | Invoker->getDescribedFunctionTemplate(), TemplateArgs, CurrentLocation); |
| 15130 | if (!Invoker) |
| 15131 | return; |
| 15132 | } |
| 15133 | |
| 15134 | if (CallOp->isInvalidDecl()) |
| 15135 | return; |
| 15136 | |
| 15137 | |
| 15138 | |
| 15139 | |
| 15140 | |
| 15141 | |
| 15142 | MarkFunctionReferenced(CurrentLocation, CallOp); |
| 15143 | |
| 15144 | |
| 15145 | |
| 15146 | |
| 15147 | Invoker->markUsed(Context); |
| 15148 | Invoker->setReferenced(); |
| 15149 | Invoker->setType(Conv->getReturnType()->getPointeeType()); |
| 15150 | Invoker->setBody(new (Context) CompoundStmt(Conv->getLocation())); |
| 15151 | |
| 15152 | |
| 15153 | Expr *FunctionRef = BuildDeclRefExpr(Invoker, Invoker->getType(), |
| 15154 | VK_LValue, Conv->getLocation()); |
| 15155 | assert(FunctionRef && "Can't refer to __invoke function?"); |
| 15156 | Stmt *Return = BuildReturnStmt(Conv->getLocation(), FunctionRef).get(); |
| 15157 | Conv->setBody(CompoundStmt::Create(Context, Return, Conv->getLocation(), |
| 15158 | Conv->getLocation())); |
| 15159 | Conv->markUsed(Context); |
| 15160 | Conv->setReferenced(); |
| 15161 | |
| 15162 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 15163 | L->CompletedImplicitDefinition(Conv); |
| 15164 | L->CompletedImplicitDefinition(Invoker); |
| 15165 | } |
| 15166 | } |
| 15167 | |
| 15168 | |
| 15169 | |
| 15170 | void Sema::DefineImplicitLambdaToBlockPointerConversion( |
| 15171 | SourceLocation CurrentLocation, |
| 15172 | CXXConversionDecl *Conv) |
| 15173 | { |
| 15174 | assert(!Conv->getParent()->isGenericLambda()); |
| 15175 | |
| 15176 | SynthesizedFunctionScope Scope(*this, Conv); |
| 15177 | |
| 15178 | |
| 15179 | Expr *This = ActOnCXXThis(CurrentLocation).get(); |
| 15180 | Expr *DerefThis =CreateBuiltinUnaryOp(CurrentLocation, UO_Deref, This).get(); |
| 15181 | |
| 15182 | ExprResult BuildBlock = BuildBlockForLambdaConversion(CurrentLocation, |
| 15183 | Conv->getLocation(), |
| 15184 | Conv, DerefThis); |
| 15185 | |
| 15186 | |
| 15187 | |
| 15188 | |
| 15189 | |
| 15190 | if (!BuildBlock.isInvalid() && !getLangOpts().ObjCAutoRefCount) |
| 15191 | BuildBlock = ImplicitCastExpr::Create( |
| 15192 | Context, BuildBlock.get()->getType(), CK_CopyAndAutoreleaseBlockObject, |
| 15193 | BuildBlock.get(), nullptr, VK_PRValue, FPOptionsOverride()); |
| 15194 | |
| 15195 | if (BuildBlock.isInvalid()) { |
| 15196 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| 15197 | Conv->setInvalidDecl(); |
| 15198 | return; |
| 15199 | } |
| 15200 | |
| 15201 | |
| 15202 | |
| 15203 | StmtResult Return = BuildReturnStmt(Conv->getLocation(), BuildBlock.get()); |
| 15204 | if (Return.isInvalid()) { |
| 15205 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| 15206 | Conv->setInvalidDecl(); |
| 15207 | return; |
| 15208 | } |
| 15209 | |
| 15210 | |
| 15211 | Stmt *ReturnS = Return.get(); |
| 15212 | Conv->setBody(CompoundStmt::Create(Context, ReturnS, Conv->getLocation(), |
| 15213 | Conv->getLocation())); |
| 15214 | Conv->markUsed(Context); |
| 15215 | |
| 15216 | |
| 15217 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 15218 | L->CompletedImplicitDefinition(Conv); |
| 15219 | } |
| 15220 | } |
| 15221 | |
| 15222 | |
| 15223 | |
| 15224 | static bool hasOneRealArgument(MultiExprArg Args) { |
| 15225 | switch (Args.size()) { |
| 15226 | case 0: |
| 15227 | return false; |
| 15228 | |
| 15229 | default: |
| 15230 | if (!Args[1]->isDefaultArgument()) |
| 15231 | return false; |
| 15232 | |
| 15233 | LLVM_FALLTHROUGH; |
| 15234 | case 1: |
| 15235 | return !Args[0]->isDefaultArgument(); |
| 15236 | } |
| 15237 | |
| 15238 | return false; |
| 15239 | } |
| 15240 | |
| 15241 | ExprResult |
| 15242 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 15243 | NamedDecl *FoundDecl, |
| 15244 | CXXConstructorDecl *Constructor, |
| 15245 | MultiExprArg ExprArgs, |
| 15246 | bool HadMultipleCandidates, |
| 15247 | bool IsListInitialization, |
| 15248 | bool IsStdInitListInitialization, |
| 15249 | bool RequiresZeroInit, |
| 15250 | unsigned ConstructKind, |
| 15251 | SourceRange ParenRange) { |
| 15252 | bool Elidable = false; |
| 15253 | |
| 15254 | |
| 15255 | |
| 15256 | |
| 15257 | |
| 15258 | |
| 15259 | |
| 15260 | |
| 15261 | |
| 15262 | |
| 15263 | |
| 15264 | if (ConstructKind == CXXConstructExpr::CK_Complete && Constructor && |
| 15265 | |
| 15266 | |
| 15267 | |
| 15268 | |
| 15269 | |
| 15270 | Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) { |
| 15271 | Expr *SubExpr = ExprArgs[0]; |
| 15272 | |
| 15273 | |
| 15274 | |
| 15275 | |
| 15276 | Elidable = SubExpr->isTemporaryObject( |
| 15277 | Context, cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 15278 | } |
| 15279 | |
| 15280 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, |
| 15281 | FoundDecl, Constructor, |
| 15282 | Elidable, ExprArgs, HadMultipleCandidates, |
| 15283 | IsListInitialization, |
| 15284 | IsStdInitListInitialization, RequiresZeroInit, |
| 15285 | ConstructKind, ParenRange); |
| 15286 | } |
| 15287 | |
| 15288 | ExprResult |
| 15289 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 15290 | NamedDecl *FoundDecl, |
| 15291 | CXXConstructorDecl *Constructor, |
| 15292 | bool Elidable, |
| 15293 | MultiExprArg ExprArgs, |
| 15294 | bool HadMultipleCandidates, |
| 15295 | bool IsListInitialization, |
| 15296 | bool IsStdInitListInitialization, |
| 15297 | bool RequiresZeroInit, |
| 15298 | unsigned ConstructKind, |
| 15299 | SourceRange ParenRange) { |
| 15300 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>(FoundDecl)) { |
| 15301 | Constructor = findInheritingConstructor(ConstructLoc, Constructor, Shadow); |
| 15302 | if (DiagnoseUseOfDecl(Constructor, ConstructLoc)) |
| 15303 | return ExprError(); |
| 15304 | } |
| 15305 | |
| 15306 | return BuildCXXConstructExpr( |
| 15307 | ConstructLoc, DeclInitType, Constructor, Elidable, ExprArgs, |
| 15308 | HadMultipleCandidates, IsListInitialization, IsStdInitListInitialization, |
| 15309 | RequiresZeroInit, ConstructKind, ParenRange); |
| 15310 | } |
| 15311 | |
| 15312 | |
| 15313 | |
| 15314 | ExprResult |
| 15315 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 15316 | CXXConstructorDecl *Constructor, |
| 15317 | bool Elidable, |
| 15318 | MultiExprArg ExprArgs, |
| 15319 | bool HadMultipleCandidates, |
| 15320 | bool IsListInitialization, |
| 15321 | bool IsStdInitListInitialization, |
| 15322 | bool RequiresZeroInit, |
| 15323 | unsigned ConstructKind, |
| 15324 | SourceRange ParenRange) { |
| 15325 | assert(declaresSameEntity( |
| 15326 | Constructor->getParent(), |
| 15327 | DeclInitType->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) && |
| 15328 | "given constructor for wrong type"); |
| 15329 | MarkFunctionReferenced(ConstructLoc, Constructor); |
| 15330 | if (getLangOpts().CUDA && !CheckCUDACall(ConstructLoc, Constructor)) |
| 15331 | return ExprError(); |
| 15332 | if (getLangOpts().SYCLIsDevice && |
| 15333 | !checkSYCLDeviceFunction(ConstructLoc, Constructor)) |
| 15334 | return ExprError(); |
| 15335 | |
| 15336 | return CheckForImmediateInvocation( |
| 15337 | CXXConstructExpr::Create( |
| 15338 | Context, DeclInitType, ConstructLoc, Constructor, Elidable, ExprArgs, |
| 15339 | HadMultipleCandidates, IsListInitialization, |
| 15340 | IsStdInitListInitialization, RequiresZeroInit, |
| 15341 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 15342 | ParenRange), |
| 15343 | Constructor); |
| 15344 | } |
| 15345 | |
| 15346 | ExprResult Sema::BuildCXXDefaultInitExpr(SourceLocation Loc, FieldDecl *Field) { |
| 15347 | assert(Field->hasInClassInitializer()); |
| 15348 | |
| 15349 | |
| 15350 | if (Field->getInClassInitializer()) |
| 15351 | return CXXDefaultInitExpr::Create(Context, Loc, Field, CurContext); |
| 15352 | |
| 15353 | |
| 15354 | if (Field->isInvalidDecl()) |
| 15355 | return ExprError(); |
| 15356 | |
| 15357 | |
| 15358 | |
| 15359 | CXXRecordDecl *ParentRD = cast<CXXRecordDecl>(Field->getParent()); |
| 15360 | |
| 15361 | if (isTemplateInstantiation(ParentRD->getTemplateSpecializationKind())) { |
| 15362 | CXXRecordDecl *ClassPattern = ParentRD->getTemplateInstantiationPattern(); |
| 15363 | DeclContext::lookup_result Lookup = |
| 15364 | ClassPattern->lookup(Field->getDeclName()); |
| 15365 | |
| 15366 | FieldDecl *Pattern = nullptr; |
| 15367 | for (auto L : Lookup) { |
| 15368 | if (isa<FieldDecl>(L)) { |
| 15369 | Pattern = cast<FieldDecl>(L); |
| 15370 | break; |
| 15371 | } |
| 15372 | } |
| 15373 | assert(Pattern && "We must have set the Pattern!"); |
| 15374 | |
| 15375 | if (!Pattern->hasInClassInitializer() || |
| 15376 | InstantiateInClassInitializer(Loc, Field, Pattern, |
| 15377 | getTemplateInstantiationArgs(Field))) { |
| 15378 | |
| 15379 | Field->setInvalidDecl(); |
| 15380 | return ExprError(); |
| 15381 | } |
| 15382 | return CXXDefaultInitExpr::Create(Context, Loc, Field, CurContext); |
| 15383 | } |
| 15384 | |
| 15385 | |
| 15386 | |
| 15387 | |
| 15388 | |
| 15389 | |
| 15390 | |
| 15391 | |
| 15392 | |
| 15393 | |
| 15394 | |
| 15395 | |
| 15396 | |
| 15397 | |
| 15398 | |
| 15399 | RecordDecl *OutermostClass = ParentRD->getOuterLexicalRecordContext(); |
| 15400 | Diag(Loc, diag::err_default_member_initializer_not_yet_parsed) |
| 15401 | << OutermostClass << Field; |
| 15402 | Diag(Field->getEndLoc(), |
| 15403 | diag::note_default_member_initializer_not_yet_parsed); |
| 15404 | |
| 15405 | if (!isSFINAEContext()) |
| 15406 | Field->setInvalidDecl(); |
| 15407 | return ExprError(); |
| 15408 | } |
| 15409 | |
| 15410 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
| 15411 | if (VD->isInvalidDecl()) return; |
| 15412 | |
| 15413 | |
| 15414 | if (VD->getInit() && VD->getInit()->containsErrors()) |
| 15415 | return; |
| 15416 | |
| 15417 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
| 15418 | if (ClassDecl->isInvalidDecl()) return; |
| 15419 | if (ClassDecl->hasIrrelevantDestructor()) return; |
| 15420 | if (ClassDecl->isDependentContext()) return; |
| 15421 | |
| 15422 | if (VD->isNoDestroy(getASTContext())) |
| 15423 | return; |
| 15424 | |
| 15425 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
| 15426 | |
| 15427 | |
| 15428 | |
| 15429 | |
| 15430 | if (!VD->getType()->isArrayType()) { |
| 15431 | MarkFunctionReferenced(VD->getLocation(), Destructor); |
| 15432 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 15433 | PDiag(diag::err_access_dtor_var) |
| 15434 | << VD->getDeclName() << VD->getType()); |
| 15435 | DiagnoseUseOfDecl(Destructor, VD->getLocation()); |
| 15436 | } |
| 15437 | |
| 15438 | if (Destructor->isTrivial()) return; |
| 15439 | |
| 15440 | |
| 15441 | |
| 15442 | if (Destructor->isConstexpr()) { |
| 15443 | bool HasConstantInit = false; |
| 15444 | if (VD->getInit() && !VD->getInit()->isValueDependent()) |
| 15445 | HasConstantInit = VD->evaluateValue(); |
| 15446 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| 15447 | if (!VD->evaluateDestruction(Notes) && VD->isConstexpr() && |
| 15448 | HasConstantInit) { |
| 15449 | Diag(VD->getLocation(), |
| 15450 | diag::err_constexpr_var_requires_const_destruction) << VD; |
| 15451 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 15452 | Diag(Notes[I].first, Notes[I].second); |
| 15453 | } |
| 15454 | } |
| 15455 | |
| 15456 | if (!VD->hasGlobalStorage()) return; |
| 15457 | |
| 15458 | |
| 15459 | |
| 15460 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 15461 | |
| 15462 | |
| 15463 | if (!VD->isStaticLocal()) |
| 15464 | Diag(VD->getLocation(), diag::warn_global_destructor); |
| 15465 | } |
| 15466 | |
| 15467 | |
| 15468 | |
| 15469 | |
| 15470 | |
| 15471 | |
| 15472 | bool Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 15473 | QualType DeclInitType, MultiExprArg ArgsPtr, |
| 15474 | SourceLocation Loc, |
| 15475 | SmallVectorImpl<Expr *> &ConvertedArgs, |
| 15476 | bool AllowExplicit, |
| 15477 | bool IsListInitialization) { |
| 15478 | |
| 15479 | unsigned NumArgs = ArgsPtr.size(); |
| 15480 | Expr **Args = ArgsPtr.data(); |
| 15481 | |
| 15482 | const auto *Proto = Constructor->getType()->castAs<FunctionProtoType>(); |
| 15483 | unsigned NumParams = Proto->getNumParams(); |
| 15484 | |
| 15485 | |
| 15486 | if (NumArgs < NumParams) |
| 15487 | ConvertedArgs.reserve(NumParams); |
| 15488 | else |
| 15489 | ConvertedArgs.reserve(NumArgs); |
| 15490 | |
| 15491 | VariadicCallType CallType = |
| 15492 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 15493 | SmallVector<Expr *, 8> AllArgs; |
| 15494 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 15495 | Proto, 0, |
| 15496 | llvm::makeArrayRef(Args, NumArgs), |
| 15497 | AllArgs, |
| 15498 | CallType, AllowExplicit, |
| 15499 | IsListInitialization); |
| 15500 | ConvertedArgs.append(AllArgs.begin(), AllArgs.end()); |
| 15501 | |
| 15502 | DiagnoseSentinelCalls(Constructor, Loc, AllArgs); |
| 15503 | |
| 15504 | CheckConstructorCall(Constructor, DeclInitType, |
| 15505 | llvm::makeArrayRef(AllArgs.data(), AllArgs.size()), |
| 15506 | Proto, Loc); |
| 15507 | |
| 15508 | return Invalid; |
| 15509 | } |
| 15510 | |
| 15511 | static inline bool |
| 15512 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 15513 | const FunctionDecl *FnDecl) { |
| 15514 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
| 15515 | if (isa<NamespaceDecl>(DC)) { |
| 15516 | return SemaRef.Diag(FnDecl->getLocation(), |
| 15517 | diag::err_operator_new_delete_declared_in_namespace) |
| 15518 | << FnDecl->getDeclName(); |
| 15519 | } |
| 15520 | |
| 15521 | if (isa<TranslationUnitDecl>(DC) && |
| 15522 | FnDecl->getStorageClass() == SC_Static) { |
| 15523 | return SemaRef.Diag(FnDecl->getLocation(), |
| 15524 | diag::err_operator_new_delete_declared_static) |
| 15525 | << FnDecl->getDeclName(); |
| 15526 | } |
| 15527 | |
| 15528 | return false; |
| 15529 | } |
| 15530 | |
| 15531 | static CanQualType RemoveAddressSpaceFromPtr(Sema &SemaRef, |
| 15532 | const PointerType *PtrTy) { |
| 15533 | auto &Ctx = SemaRef.Context; |
| 15534 | Qualifiers PtrQuals = PtrTy->getPointeeType().getQualifiers(); |
| 15535 | PtrQuals.removeAddressSpace(); |
| 15536 | return Ctx.getPointerType(Ctx.getCanonicalType(Ctx.getQualifiedType( |
| 15537 | PtrTy->getPointeeType().getUnqualifiedType(), PtrQuals))); |
| 15538 | } |
| 15539 | |
| 15540 | static inline bool |
| 15541 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 15542 | CanQualType ExpectedResultType, |
| 15543 | CanQualType ExpectedFirstParamType, |
| 15544 | unsigned DependentParamTypeDiag, |
| 15545 | unsigned InvalidParamTypeDiag) { |
| 15546 | QualType ResultType = |
| 15547 | FnDecl->getType()->castAs<FunctionType>()->getReturnType(); |
| 15548 | |
| 15549 | if (SemaRef.getLangOpts().OpenCLCPlusPlus) { |
| 15550 | |
| 15551 | |
| 15552 | if (const auto *PtrTy = ResultType->getAs<PointerType>()) |
| 15553 | ResultType = RemoveAddressSpaceFromPtr(SemaRef, PtrTy); |
| 15554 | |
| 15555 | if (auto ExpectedPtrTy = ExpectedResultType->getAs<PointerType>()) |
| 15556 | ExpectedResultType = RemoveAddressSpaceFromPtr(SemaRef, ExpectedPtrTy); |
| 15557 | } |
| 15558 | |
| 15559 | |
| 15560 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) { |
| 15561 | |
| 15562 | |
| 15563 | return SemaRef.Diag( |
| 15564 | FnDecl->getLocation(), |
| 15565 | ResultType->isDependentType() |
| 15566 | ? diag::err_operator_new_delete_dependent_result_type |
| 15567 | : diag::err_operator_new_delete_invalid_result_type) |
| 15568 | << FnDecl->getDeclName() << ExpectedResultType; |
| 15569 | } |
| 15570 | |
| 15571 | |
| 15572 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 15573 | return SemaRef.Diag(FnDecl->getLocation(), |
| 15574 | diag::err_operator_new_delete_template_too_few_parameters) |
| 15575 | << FnDecl->getDeclName(); |
| 15576 | |
| 15577 | |
| 15578 | if (FnDecl->getNumParams() == 0) |
| 15579 | return SemaRef.Diag(FnDecl->getLocation(), |
| 15580 | diag::err_operator_new_delete_too_few_parameters) |
| 15581 | << FnDecl->getDeclName(); |
| 15582 | |
| 15583 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 15584 | if (SemaRef.getLangOpts().OpenCLCPlusPlus) { |
| 15585 | |
| 15586 | |
| 15587 | if (const auto *PtrTy = |
| 15588 | FnDecl->getParamDecl(0)->getType()->getAs<PointerType>()) |
| 15589 | FirstParamType = RemoveAddressSpaceFromPtr(SemaRef, PtrTy); |
| 15590 | |
| 15591 | if (auto ExpectedPtrTy = ExpectedFirstParamType->getAs<PointerType>()) |
| 15592 | ExpectedFirstParamType = |
| 15593 | RemoveAddressSpaceFromPtr(SemaRef, ExpectedPtrTy); |
| 15594 | } |
| 15595 | |
| 15596 | |
| 15597 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
| 15598 | ExpectedFirstParamType) { |
| 15599 | |
| 15600 | |
| 15601 | |
| 15602 | return SemaRef.Diag(FnDecl->getLocation(), FirstParamType->isDependentType() |
| 15603 | ? DependentParamTypeDiag |
| 15604 | : InvalidParamTypeDiag) |
| 15605 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 15606 | } |
| 15607 | |
| 15608 | return false; |
| 15609 | } |
| 15610 | |
| 15611 | static bool |
| 15612 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 15613 | |
| 15614 | |
| 15615 | |
| 15616 | |
| 15617 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 15618 | return true; |
| 15619 | |
| 15620 | CanQualType SizeTy = |
| 15621 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 15622 | |
| 15623 | |
| 15624 | |
| 15625 | |
| 15626 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 15627 | SizeTy, |
| 15628 | diag::err_operator_new_dependent_param_type, |
| 15629 | diag::err_operator_new_param_type)) |
| 15630 | return true; |
| 15631 | |
| 15632 | |
| 15633 | |
| 15634 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
| 15635 | return SemaRef.Diag(FnDecl->getLocation(), |
| 15636 | diag::err_operator_new_default_arg) |
| 15637 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 15638 | |
| 15639 | return false; |
| 15640 | } |
| 15641 | |
| 15642 | static bool |
| 15643 | CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { |
| 15644 | |
| 15645 | |
| 15646 | |
| 15647 | |
| 15648 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 15649 | return true; |
| 15650 | |
| 15651 | auto *MD = dyn_cast<CXXMethodDecl>(FnDecl); |
| 15652 | |
| 15653 | |
| 15654 | |
| 15655 | |
| 15656 | |
| 15657 | CanQualType ExpectedFirstParamType = |
| 15658 | MD && MD->isDestroyingOperatorDelete() |
| 15659 | ? SemaRef.Context.getCanonicalType(SemaRef.Context.getPointerType( |
| 15660 | SemaRef.Context.getRecordType(MD->getParent()))) |
| 15661 | : SemaRef.Context.VoidPtrTy; |
| 15662 | |
| 15663 | |
| 15664 | |
| 15665 | if (CheckOperatorNewDeleteTypes( |
| 15666 | SemaRef, FnDecl, SemaRef.Context.VoidTy, ExpectedFirstParamType, |
| 15667 | diag::err_operator_delete_dependent_param_type, |
| 15668 | diag::err_operator_delete_param_type)) |
| 15669 | return true; |
| 15670 | |
| 15671 | |
| 15672 | |
| 15673 | if (MD && !MD->getParent()->isDependentContext() && |
| 15674 | MD->isDestroyingOperatorDelete() && |
| 15675 | !SemaRef.isUsualDeallocationFunction(MD)) { |
| 15676 | SemaRef.Diag(MD->getLocation(), |
| 15677 | diag::err_destroying_operator_delete_not_usual); |
| 15678 | return true; |
| 15679 | } |
| 15680 | |
| 15681 | return false; |
| 15682 | } |
| 15683 | |
| 15684 | |
| 15685 | |
| 15686 | |
| 15687 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
| 15688 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
| 15689 | "Expected an overloaded operator declaration"); |
| 15690 | |
| 15691 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 15692 | |
| 15693 | |
| 15694 | |
| 15695 | |
| 15696 | |
| 15697 | |
| 15698 | |
| 15699 | if (Op == OO_Delete || Op == OO_Array_Delete) |
| 15700 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
| 15701 | |
| 15702 | if (Op == OO_New || Op == OO_Array_New) |
| 15703 | return CheckOperatorNewDeclaration(*this, FnDecl); |
| 15704 | |
| 15705 | |
| 15706 | |
| 15707 | |
| 15708 | |
| 15709 | |
| 15710 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 15711 | if (MethodDecl->isStatic()) |
| 15712 | return Diag(FnDecl->getLocation(), |
| 15713 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
| 15714 | } else { |
| 15715 | bool ClassOrEnumParam = false; |
| 15716 | for (auto Param : FnDecl->parameters()) { |
| 15717 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 15718 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 15719 | ParamType->isEnumeralType()) { |
| 15720 | ClassOrEnumParam = true; |
| 15721 | break; |
| 15722 | } |
| 15723 | } |
| 15724 | |
| 15725 | if (!ClassOrEnumParam) |
| 15726 | return Diag(FnDecl->getLocation(), |
| 15727 | diag::err_operator_overload_needs_class_or_enum) |
| 15728 | << FnDecl->getDeclName(); |
| 15729 | } |
| 15730 | |
| 15731 | |
| 15732 | |
| 15733 | |
| 15734 | |
| 15735 | |
| 15736 | |
| 15737 | if (Op != OO_Call) { |
| 15738 | for (auto Param : FnDecl->parameters()) { |
| 15739 | if (Param->hasDefaultArg()) |
| 15740 | return Diag(Param->getLocation(), |
| 15741 | diag::err_operator_overload_default_arg) |
| 15742 | << FnDecl->getDeclName() << Param->getDefaultArgRange(); |
| 15743 | } |
| 15744 | } |
| 15745 | |
| 15746 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 15747 | { false, false, false } |
| 15748 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 15749 | , { Unary, Binary, MemberOnly } |
| 15750 | #include "clang/Basic/OperatorKinds.def" |
| 15751 | }; |
| 15752 | |
| 15753 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 15754 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 15755 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
| 15756 | |
| 15757 | |
| 15758 | |
| 15759 | |
| 15760 | |
| 15761 | unsigned NumParams = FnDecl->getNumParams() |
| 15762 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
| 15763 | if (Op != OO_Call && |
| 15764 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 15765 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 15766 | (NumParams < 1) || (NumParams > 2))) { |
| 15767 | |
| 15768 | unsigned ErrorKind; |
| 15769 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
| 15770 | ErrorKind = 2; |
| 15771 | } else if (CanBeUnaryOperator) { |
| 15772 | ErrorKind = 0; |
| 15773 | } else { |
| 15774 | assert(CanBeBinaryOperator && |
| 15775 | "All non-call overloaded operators are unary or binary!"); |
| 15776 | ErrorKind = 1; |
| 15777 | } |
| 15778 | |
| 15779 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
| 15780 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
| 15781 | } |
| 15782 | |
| 15783 | |
| 15784 | if (Op != OO_Call && |
| 15785 | FnDecl->getType()->castAs<FunctionProtoType>()->isVariadic()) { |
| 15786 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
| 15787 | << FnDecl->getDeclName(); |
| 15788 | } |
| 15789 | |
| 15790 | |
| 15791 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 15792 | return Diag(FnDecl->getLocation(), |
| 15793 | diag::err_operator_overload_must_be_member) |
| 15794 | << FnDecl->getDeclName(); |
| 15795 | } |
| 15796 | |
| 15797 | |
| 15798 | |
| 15799 | |
| 15800 | |
| 15801 | |
| 15802 | |
| 15803 | |
| 15804 | |
| 15805 | |
| 15806 | |
| 15807 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 15808 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 15809 | QualType ParamType = LastParam->getType(); |
| 15810 | |
| 15811 | if (!ParamType->isSpecificBuiltinType(BuiltinType::Int) && |
| 15812 | !ParamType->isDependentType()) |
| 15813 | return Diag(LastParam->getLocation(), |
| 15814 | diag::err_operator_overload_post_incdec_must_be_int) |
| 15815 | << LastParam->getType() << (Op == OO_MinusMinus); |
| 15816 | } |
| 15817 | |
| 15818 | return false; |
| 15819 | } |
| 15820 | |
| 15821 | static bool |
| 15822 | checkLiteralOperatorTemplateParameterList(Sema &SemaRef, |
| 15823 | FunctionTemplateDecl *TpDecl) { |
| 15824 | TemplateParameterList *TemplateParams = TpDecl->getTemplateParameters(); |
| 15825 | |
| 15826 | |
| 15827 | if (TemplateParams->size() == 1) { |
| 15828 | NonTypeTemplateParmDecl *PmDecl = |
| 15829 | dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(0)); |
| 15830 | |
| 15831 | |
| 15832 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 15833 | SemaRef.Context.hasSameType(PmDecl->getType(), SemaRef.Context.CharTy)) |
| 15834 | return false; |
| 15835 | |
| 15836 | |
| 15837 | |
| 15838 | |
| 15839 | |
| 15840 | |
| 15841 | |
| 15842 | |
| 15843 | if (SemaRef.getLangOpts().CPlusPlus20 && |
| 15844 | !PmDecl->isTemplateParameterPack() && |
| 15845 | (PmDecl->getType()->isRecordType() || |
| 15846 | PmDecl->getType()->getAs<DeducedTemplateSpecializationType>())) |
| 15847 | return false; |
| 15848 | } else if (TemplateParams->size() == 2) { |
| 15849 | TemplateTypeParmDecl *PmType = |
| 15850 | dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(0)); |
| 15851 | NonTypeTemplateParmDecl *PmArgs = |
| 15852 | dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(1)); |
| 15853 | |
| 15854 | |
| 15855 | |
| 15856 | if (PmType && PmArgs && !PmType->isTemplateParameterPack() && |
| 15857 | PmArgs->isTemplateParameterPack()) { |
| 15858 | const TemplateTypeParmType *TArgs = |
| 15859 | PmArgs->getType()->getAs<TemplateTypeParmType>(); |
| 15860 | if (TArgs && TArgs->getDepth() == PmType->getDepth() && |
| 15861 | TArgs->getIndex() == PmType->getIndex()) { |
| 15862 | if (!SemaRef.inTemplateInstantiation()) |
| 15863 | SemaRef.Diag(TpDecl->getLocation(), |
| 15864 | diag::ext_string_literal_operator_template); |
| 15865 | return false; |
| 15866 | } |
| 15867 | } |
| 15868 | } |
| 15869 | |
| 15870 | SemaRef.Diag(TpDecl->getTemplateParameters()->getSourceRange().getBegin(), |
| 15871 | diag::err_literal_operator_template) |
| 15872 | << TpDecl->getTemplateParameters()->getSourceRange(); |
| 15873 | return true; |
| 15874 | } |
| 15875 | |
| 15876 | |
| 15877 | |
| 15878 | |
| 15879 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 15880 | if (isa<CXXMethodDecl>(FnDecl)) { |
| 15881 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 15882 | << FnDecl->getDeclName(); |
| 15883 | return true; |
| 15884 | } |
| 15885 | |
| 15886 | if (FnDecl->isExternC()) { |
| 15887 | Diag(FnDecl->getLocation(), diag::err_literal_operator_extern_c); |
| 15888 | if (const LinkageSpecDecl *LSD = |
| 15889 | FnDecl->getDeclContext()->getExternCContext()) |
| 15890 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 15891 | return true; |
| 15892 | } |
| 15893 | |
| 15894 | |
| 15895 | FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate(); |
| 15896 | |
| 15897 | |
| 15898 | if (!TpDecl) |
| 15899 | TpDecl = FnDecl->getPrimaryTemplate(); |
| 15900 | |
| 15901 | |
| 15902 | |
| 15903 | |
| 15904 | |
| 15905 | |
| 15906 | if (TpDecl) { |
| 15907 | if (FnDecl->param_size() != 0) { |
| 15908 | Diag(FnDecl->getLocation(), |
| 15909 | diag::err_literal_operator_template_with_params); |
| 15910 | return true; |
| 15911 | } |
| 15912 | |
| 15913 | if (checkLiteralOperatorTemplateParameterList(*this, TpDecl)) |
| 15914 | return true; |
| 15915 | |
| 15916 | } else if (FnDecl->param_size() == 1) { |
| 15917 | const ParmVarDecl *Param = FnDecl->getParamDecl(0); |
| 15918 | |
| 15919 | QualType ParamType = Param->getType().getUnqualifiedType(); |
| 15920 | |
| 15921 | |
| 15922 | |
| 15923 | if (ParamType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 15924 | ParamType->isSpecificBuiltinType(BuiltinType::LongDouble) || |
| 15925 | Context.hasSameType(ParamType, Context.CharTy) || |
| 15926 | Context.hasSameType(ParamType, Context.WideCharTy) || |
| 15927 | Context.hasSameType(ParamType, Context.Char8Ty) || |
| 15928 | Context.hasSameType(ParamType, Context.Char16Ty) || |
| 15929 | Context.hasSameType(ParamType, Context.Char32Ty)) { |
| 15930 | } else if (const PointerType *Ptr = ParamType->getAs<PointerType>()) { |
| 15931 | QualType InnerType = Ptr->getPointeeType(); |
| 15932 | |
| 15933 | |
| 15934 | if (!(Context.hasSameType(InnerType.getUnqualifiedType(), |
| 15935 | Context.CharTy) && |
| 15936 | InnerType.isConstQualified() && !InnerType.isVolatileQualified())) { |
| 15937 | Diag(Param->getSourceRange().getBegin(), |
| 15938 | diag::err_literal_operator_param) |
| 15939 | << ParamType << "'const char *'" << Param->getSourceRange(); |
| 15940 | return true; |
| 15941 | } |
| 15942 | |
| 15943 | } else if (ParamType->isRealFloatingType()) { |
| 15944 | Diag(Param->getSourceRange().getBegin(), diag::err_literal_operator_param) |
| 15945 | << ParamType << Context.LongDoubleTy << Param->getSourceRange(); |
| 15946 | return true; |
| 15947 | |
| 15948 | } else if (ParamType->isIntegerType()) { |
| 15949 | Diag(Param->getSourceRange().getBegin(), diag::err_literal_operator_param) |
| 15950 | << ParamType << Context.UnsignedLongLongTy << Param->getSourceRange(); |
| 15951 | return true; |
| 15952 | |
| 15953 | } else { |
| 15954 | Diag(Param->getSourceRange().getBegin(), |
| 15955 | diag::err_literal_operator_invalid_param) |
| 15956 | << ParamType << Param->getSourceRange(); |
| 15957 | return true; |
| 15958 | } |
| 15959 | |
| 15960 | } else if (FnDecl->param_size() == 2) { |
| 15961 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 15962 | |
| 15963 | |
| 15964 | |
| 15965 | QualType FirstParamType = (*Param)->getType().getUnqualifiedType(); |
| 15966 | |
| 15967 | |
| 15968 | |
| 15969 | const PointerType *PT = FirstParamType->getAs<PointerType>(); |
| 15970 | |
| 15971 | if (!PT) { |
| 15972 | Diag((*Param)->getSourceRange().getBegin(), |
| 15973 | diag::err_literal_operator_param) |
| 15974 | << FirstParamType << "'const char *'" << (*Param)->getSourceRange(); |
| 15975 | return true; |
| 15976 | } |
| 15977 | |
| 15978 | QualType PointeeType = PT->getPointeeType(); |
| 15979 | |
| 15980 | if (!PointeeType.isConstQualified() || PointeeType.isVolatileQualified()) { |
| 15981 | Diag((*Param)->getSourceRange().getBegin(), |
| 15982 | diag::err_literal_operator_param) |
| 15983 | << FirstParamType << "'const char *'" << (*Param)->getSourceRange(); |
| 15984 | return true; |
| 15985 | } |
| 15986 | |
| 15987 | QualType InnerType = PointeeType.getUnqualifiedType(); |
| 15988 | |
| 15989 | |
| 15990 | |
| 15991 | if (!(Context.hasSameType(InnerType, Context.CharTy) || |
| 15992 | Context.hasSameType(InnerType, Context.WideCharTy) || |
| 15993 | Context.hasSameType(InnerType, Context.Char8Ty) || |
| 15994 | Context.hasSameType(InnerType, Context.Char16Ty) || |
| 15995 | Context.hasSameType(InnerType, Context.Char32Ty))) { |
| 15996 | Diag((*Param)->getSourceRange().getBegin(), |
| 15997 | diag::err_literal_operator_param) |
| 15998 | << FirstParamType << "'const char *'" << (*Param)->getSourceRange(); |
| 15999 | return true; |
| 16000 | } |
| 16001 | |
| 16002 | |
| 16003 | ++Param; |
| 16004 | |
| 16005 | |
| 16006 | QualType SecondParamType = (*Param)->getType().getUnqualifiedType(); |
| 16007 | if (!Context.hasSameType(SecondParamType, Context.getSizeType())) { |
| 16008 | Diag((*Param)->getSourceRange().getBegin(), |
| 16009 | diag::err_literal_operator_param) |
| 16010 | << SecondParamType << Context.getSizeType() |
| 16011 | << (*Param)->getSourceRange(); |
| 16012 | return true; |
| 16013 | } |
| 16014 | } else { |
| 16015 | Diag(FnDecl->getLocation(), diag::err_literal_operator_bad_param_count); |
| 16016 | return true; |
| 16017 | } |
| 16018 | |
| 16019 | |
| 16020 | |
| 16021 | |
| 16022 | |
| 16023 | for (auto Param : FnDecl->parameters()) { |
| 16024 | if (Param->hasDefaultArg()) { |
| 16025 | Diag(Param->getDefaultArgRange().getBegin(), |
| 16026 | diag::err_literal_operator_default_argument) |
| 16027 | << Param->getDefaultArgRange(); |
| 16028 | break; |
| 16029 | } |
| 16030 | } |
| 16031 | |
| 16032 | StringRef LiteralName |
| 16033 | = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); |
| 16034 | if (LiteralName[0] != '_' && |
| 16035 | !getSourceManager().isInSystemHeader(FnDecl->getLocation())) { |
| 16036 | |
| 16037 | |
| 16038 | |
| 16039 | Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved) |
| 16040 | << StringLiteralParser::isValidUDSuffix(getLangOpts(), LiteralName); |
| 16041 | } |
| 16042 | |
| 16043 | return false; |
| 16044 | } |
| 16045 | |
| 16046 | |
| 16047 | |
| 16048 | |
| 16049 | |
| 16050 | |
| 16051 | |
| 16052 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 16053 | Expr *LangStr, |
| 16054 | SourceLocation LBraceLoc) { |
| 16055 | StringLiteral *Lit = cast<StringLiteral>(LangStr); |
| 16056 | if (!Lit->isAscii()) { |
| 16057 | Diag(LangStr->getExprLoc(), diag::err_language_linkage_spec_not_ascii) |
| 16058 | << LangStr->getSourceRange(); |
| 16059 | return nullptr; |
| 16060 | } |
| 16061 | |
| 16062 | StringRef Lang = Lit->getString(); |
| 16063 | LinkageSpecDecl::LanguageIDs Language; |
| 16064 | if (Lang == "C") |
| 16065 | Language = LinkageSpecDecl::lang_c; |
| 16066 | else if (Lang == "C++") |
| 16067 | Language = LinkageSpecDecl::lang_cxx; |
| 16068 | else { |
| 16069 | Diag(LangStr->getExprLoc(), diag::err_language_linkage_spec_unknown) |
| 16070 | << LangStr->getSourceRange(); |
| 16071 | return nullptr; |
| 16072 | } |
| 16073 | |
| 16074 | |
| 16075 | |
| 16076 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, ExternLoc, |
| 16077 | LangStr->getExprLoc(), Language, |
| 16078 | LBraceLoc.isValid()); |
| 16079 | CurContext->addDecl(D); |
| 16080 | PushDeclContext(S, D); |
| 16081 | return D; |
| 16082 | } |
| 16083 | |
| 16084 | |
| 16085 | |
| 16086 | |
| 16087 | |
| 16088 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 16089 | Decl *LinkageSpec, |
| 16090 | SourceLocation RBraceLoc) { |
| 16091 | if (RBraceLoc.isValid()) { |
| 16092 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 16093 | LSDecl->setRBraceLoc(RBraceLoc); |
| 16094 | } |
| 16095 | PopDeclContext(); |
| 16096 | return LinkageSpec; |
| 16097 | } |
| 16098 | |
| 16099 | Decl *Sema::ActOnEmptyDeclaration(Scope *S, |
| 16100 | const ParsedAttributesView &AttrList, |
| 16101 | SourceLocation SemiLoc) { |
| 16102 | Decl *ED = EmptyDecl::Create(Context, CurContext, SemiLoc); |
| 16103 | |
| 16104 | |
| 16105 | ProcessDeclAttributeList(S, ED, AttrList); |
| 16106 | |
| 16107 | CurContext->addDecl(ED); |
| 16108 | return ED; |
| 16109 | } |
| 16110 | |
| 16111 | |
| 16112 | |
| 16113 | |
| 16114 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
| 16115 | TypeSourceInfo *TInfo, |
| 16116 | SourceLocation StartLoc, |
| 16117 | SourceLocation Loc, |
| 16118 | IdentifierInfo *Name) { |
| 16119 | bool Invalid = false; |
| 16120 | QualType ExDeclType = TInfo->getType(); |
| 16121 | |
| 16122 | |
| 16123 | if (ExDeclType->isArrayType()) |
| 16124 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 16125 | else if (ExDeclType->isFunctionType()) |
| 16126 | ExDeclType = Context.getPointerType(ExDeclType); |
| 16127 | |
| 16128 | |
| 16129 | |
| 16130 | |
| 16131 | |
| 16132 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
| 16133 | Diag(Loc, diag::err_catch_rvalue_ref); |
| 16134 | Invalid = true; |
| 16135 | } |
| 16136 | |
| 16137 | if (ExDeclType->isVariablyModifiedType()) { |
| 16138 | Diag(Loc, diag::err_catch_variably_modified) << ExDeclType; |
| 16139 | Invalid = true; |
| 16140 | } |
| 16141 | |
| 16142 | QualType BaseType = ExDeclType; |
| 16143 | int Mode = 0; |
| 16144 | unsigned DK = diag::err_catch_incomplete; |
| 16145 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 16146 | BaseType = Ptr->getPointeeType(); |
| 16147 | Mode = 1; |
| 16148 | DK = diag::err_catch_incomplete_ptr; |
| 16149 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
| 16150 | |
| 16151 | BaseType = Ref->getPointeeType(); |
| 16152 | Mode = 2; |
| 16153 | DK = diag::err_catch_incomplete_ref; |
| 16154 | } |
| 16155 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
| 16156 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
| 16157 | Invalid = true; |
| 16158 | |
| 16159 | if (!Invalid && Mode != 1 && BaseType->isSizelessType()) { |
| 16160 | Diag(Loc, diag::err_catch_sizeless) << (Mode == 2 ? 1 : 0) << BaseType; |
| 16161 | Invalid = true; |
| 16162 | } |
| 16163 | |
| 16164 | if (!Invalid && !ExDeclType->isDependentType() && |
| 16165 | RequireNonAbstractType(Loc, ExDeclType, |
| 16166 | diag::err_abstract_type_in_decl, |
| 16167 | AbstractVariableType)) |
| 16168 | Invalid = true; |
| 16169 | |
| 16170 | |
| 16171 | |
| 16172 | if (!Invalid && getLangOpts().ObjC) { |
| 16173 | QualType T = ExDeclType; |
| 16174 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 16175 | T = RT->getPointeeType(); |
| 16176 | |
| 16177 | if (T->isObjCObjectType()) { |
| 16178 | Diag(Loc, diag::err_objc_object_catch); |
| 16179 | Invalid = true; |
| 16180 | } else if (T->isObjCObjectPointerType()) { |
| 16181 | |
| 16182 | if (getLangOpts().ObjCRuntime.isFragile()) |
| 16183 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
| 16184 | } |
| 16185 | } |
| 16186 | |
| 16187 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
| 16188 | ExDeclType, TInfo, SC_None); |
| 16189 | ExDecl->setExceptionVariable(true); |
| 16190 | |
| 16191 | |
| 16192 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(ExDecl)) |
| 16193 | Invalid = true; |
| 16194 | |
| 16195 | if (!Invalid && !ExDeclType->isDependentType()) { |
| 16196 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
| 16197 | |
| 16198 | EnterExpressionEvaluationContext scope( |
| 16199 | *this, ExpressionEvaluationContext::PotentiallyEvaluated); |
| 16200 | |
| 16201 | |
| 16202 | |
| 16203 | |
| 16204 | |
| 16205 | |
| 16206 | |
| 16207 | |
| 16208 | |
| 16209 | |
| 16210 | QualType initType = Context.getExceptionObjectType(ExDeclType); |
| 16211 | |
| 16212 | InitializedEntity entity = |
| 16213 | InitializedEntity::InitializeVariable(ExDecl); |
| 16214 | InitializationKind initKind = |
| 16215 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 16216 | |
| 16217 | Expr *opaqueValue = |
| 16218 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
| 16219 | InitializationSequence sequence(*this, entity, initKind, opaqueValue); |
| 16220 | ExprResult result = sequence.Perform(*this, entity, initKind, opaqueValue); |
| 16221 | if (result.isInvalid()) |
| 16222 | Invalid = true; |
| 16223 | else { |
| 16224 | |
| 16225 | |
| 16226 | CXXConstructExpr *construct = result.getAs<CXXConstructExpr>(); |
| 16227 | if (!construct->getConstructor()->isTrivial()) { |
| 16228 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 16229 | ExDecl->setInit(init); |
| 16230 | } |
| 16231 | |
| 16232 | |
| 16233 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 16234 | } |
| 16235 | } |
| 16236 | } |
| 16237 | |
| 16238 | if (Invalid) |
| 16239 | ExDecl->setInvalidDecl(); |
| 16240 | |
| 16241 | return ExDecl; |
| 16242 | } |
| 16243 | |
| 16244 | |
| 16245 | |
| 16246 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
| 16247 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 16248 | bool Invalid = D.isInvalidType(); |
| 16249 | |
| 16250 | |
| 16251 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 16252 | UPPC_ExceptionType)) { |
| 16253 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 16254 | D.getIdentifierLoc()); |
| 16255 | Invalid = true; |
| 16256 | } |
| 16257 | |
| 16258 | IdentifierInfo *II = D.getIdentifier(); |
| 16259 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
| 16260 | LookupOrdinaryName, |
| 16261 | ForVisibleRedeclaration)) { |
| 16262 | |
| 16263 | |
| 16264 | |
| 16265 | assert(!S->isDeclScope(PrevDecl)); |
| 16266 | if (isDeclInScope(PrevDecl, CurContext, S)) { |
| 16267 | Diag(D.getIdentifierLoc(), diag::err_redefinition) |
| 16268 | << D.getIdentifier(); |
| 16269 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 16270 | Invalid = true; |
| 16271 | } else if (PrevDecl->isTemplateParameter()) |
| 16272 | |
| 16273 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 16274 | } |
| 16275 | |
| 16276 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
| 16277 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 16278 | << D.getCXXScopeSpec().getRange(); |
| 16279 | Invalid = true; |
| 16280 | } |
| 16281 | |
| 16282 | VarDecl *ExDecl = BuildExceptionDeclaration( |
| 16283 | S, TInfo, D.getBeginLoc(), D.getIdentifierLoc(), D.getIdentifier()); |
| 16284 | if (Invalid) |
| 16285 | ExDecl->setInvalidDecl(); |
| 16286 | |
| 16287 | |
| 16288 | if (II) |
| 16289 | PushOnScopeChains(ExDecl, S); |
| 16290 | else |
| 16291 | CurContext->addDecl(ExDecl); |
| 16292 | |
| 16293 | ProcessDeclAttributes(S, ExDecl, D); |
| 16294 | return ExDecl; |
| 16295 | } |
| 16296 | |
| 16297 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
| 16298 | Expr *AssertExpr, |
| 16299 | Expr *AssertMessageExpr, |
| 16300 | SourceLocation RParenLoc) { |
| 16301 | StringLiteral *AssertMessage = |
| 16302 | AssertMessageExpr ? cast<StringLiteral>(AssertMessageExpr) : nullptr; |
| 16303 | |
| 16304 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 16305 | return nullptr; |
| 16306 | |
| 16307 | return BuildStaticAssertDeclaration(StaticAssertLoc, AssertExpr, |
| 16308 | AssertMessage, RParenLoc, false); |
| 16309 | } |
| 16310 | |
| 16311 | Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
| 16312 | Expr *AssertExpr, |
| 16313 | StringLiteral *AssertMessage, |
| 16314 | SourceLocation RParenLoc, |
| 16315 | bool Failed) { |
| 16316 | assert(AssertExpr != nullptr && "Expected non-null condition"); |
| 16317 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent() && |
| 16318 | !Failed) { |
| 16319 | |
| 16320 | |
| 16321 | ExprResult Converted = PerformContextuallyConvertToBool(AssertExpr); |
| 16322 | if (Converted.isInvalid()) |
| 16323 | Failed = true; |
| 16324 | |
| 16325 | ExprResult FullAssertExpr = |
| 16326 | ActOnFinishFullExpr(Converted.get(), StaticAssertLoc, |
| 16327 | false, |
| 16328 | true); |
| 16329 | if (FullAssertExpr.isInvalid()) |
| 16330 | Failed = true; |
| 16331 | else |
| 16332 | AssertExpr = FullAssertExpr.get(); |
| 16333 | |
| 16334 | llvm::APSInt Cond; |
| 16335 | if (!Failed && VerifyIntegerConstantExpression( |
| 16336 | AssertExpr, &Cond, |
| 16337 | diag::err_static_assert_expression_is_not_constant) |
| 16338 | .isInvalid()) |
| 16339 | Failed = true; |
| 16340 | |
| 16341 | if (!Failed && !Cond) { |
| 16342 | SmallString<256> MsgBuffer; |
| 16343 | llvm::raw_svector_ostream Msg(MsgBuffer); |
| 16344 | if (AssertMessage) |
| 16345 | AssertMessage->printPretty(Msg, nullptr, getPrintingPolicy()); |
| 16346 | |
| 16347 | Expr *InnerCond = nullptr; |
| 16348 | std::string InnerCondDescription; |
| 16349 | std::tie(InnerCond, InnerCondDescription) = |
| 16350 | findFailedBooleanCondition(Converted.get()); |
| 16351 | if (InnerCond && isa<ConceptSpecializationExpr>(InnerCond)) { |
| 16352 | |
| 16353 | |
| 16354 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
| 16355 | << !AssertMessage << Msg.str() << AssertExpr->getSourceRange(); |
| 16356 | ConstraintSatisfaction Satisfaction; |
| 16357 | if (!CheckConstraintSatisfaction(InnerCond, Satisfaction)) |
| 16358 | DiagnoseUnsatisfiedConstraint(Satisfaction); |
| 16359 | } else if (InnerCond && !isa<CXXBoolLiteralExpr>(InnerCond) |
| 16360 | && !isa<IntegerLiteral>(InnerCond)) { |
| 16361 | Diag(StaticAssertLoc, diag::err_static_assert_requirement_failed) |
| 16362 | << InnerCondDescription << !AssertMessage |
| 16363 | << Msg.str() << InnerCond->getSourceRange(); |
| 16364 | } else { |
| 16365 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
| 16366 | << !AssertMessage << Msg.str() << AssertExpr->getSourceRange(); |
| 16367 | } |
| 16368 | Failed = true; |
| 16369 | } |
| 16370 | } else { |
| 16371 | ExprResult FullAssertExpr = ActOnFinishFullExpr(AssertExpr, StaticAssertLoc, |
| 16372 | false, |
| 16373 | true); |
| 16374 | if (FullAssertExpr.isInvalid()) |
| 16375 | Failed = true; |
| 16376 | else |
| 16377 | AssertExpr = FullAssertExpr.get(); |
| 16378 | } |
| 16379 | |
| 16380 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
| 16381 | AssertExpr, AssertMessage, RParenLoc, |
| 16382 | Failed); |
| 16383 | |
| 16384 | CurContext->addDecl(Decl); |
| 16385 | return Decl; |
| 16386 | } |
| 16387 | |
| 16388 | |
| 16389 | |
| 16390 | |
| 16391 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation LocStart, |
| 16392 | SourceLocation FriendLoc, |
| 16393 | TypeSourceInfo *TSInfo) { |
| 16394 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 16395 | |
| 16396 | QualType T = TSInfo->getType(); |
| 16397 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
| 16398 | |
| 16399 | |
| 16400 | |
| 16401 | |
| 16402 | |
| 16403 | |
| 16404 | if (!CodeSynthesisContexts.empty()) { |
| 16405 | |
| 16406 | |
| 16407 | |
| 16408 | } else { |
| 16409 | if (!T->isElaboratedTypeSpecifier()) { |
| 16410 | |
| 16411 | |
| 16412 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 16413 | RecordDecl *RD = RT->getDecl(); |
| 16414 | |
| 16415 | SmallString<16> InsertionText(" "); |
| 16416 | InsertionText += RD->getKindName(); |
| 16417 | |
| 16418 | Diag(TypeRange.getBegin(), |
| 16419 | getLangOpts().CPlusPlus11 ? |
| 16420 | diag::warn_cxx98_compat_unelaborated_friend_type : |
| 16421 | diag::ext_unelaborated_friend_type) |
| 16422 | << (unsigned) RD->getTagKind() |
| 16423 | << T |
| 16424 | << FixItHint::CreateInsertion(getLocForEndOfToken(FriendLoc), |
| 16425 | InsertionText); |
| 16426 | } else { |
| 16427 | Diag(FriendLoc, |
| 16428 | getLangOpts().CPlusPlus11 ? |
| 16429 | diag::warn_cxx98_compat_nonclass_type_friend : |
| 16430 | diag::ext_nonclass_type_friend) |
| 16431 | << T |
| 16432 | << TypeRange; |
| 16433 | } |
| 16434 | } else if (T->getAs<EnumType>()) { |
| 16435 | Diag(FriendLoc, |
| 16436 | getLangOpts().CPlusPlus11 ? |
| 16437 | diag::warn_cxx98_compat_enum_friend : |
| 16438 | diag::ext_enum_friend) |
| 16439 | << T |
| 16440 | << TypeRange; |
| 16441 | } |
| 16442 | |
| 16443 | |
| 16444 | |
| 16445 | |
| 16446 | |
| 16447 | |
| 16448 | |
| 16449 | if (getLangOpts().CPlusPlus11 && LocStart != FriendLoc) |
| 16450 | Diag(FriendLoc, diag::err_friend_not_first_in_declaration) << T; |
| 16451 | } |
| 16452 | |
| 16453 | |
| 16454 | |
| 16455 | |
| 16456 | return FriendDecl::Create(Context, CurContext, |
| 16457 | TSInfo->getTypeLoc().getBeginLoc(), TSInfo, |
| 16458 | FriendLoc); |
| 16459 | } |
| 16460 | |
| 16461 | |
| 16462 | |
| 16463 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 16464 | unsigned TagSpec, SourceLocation TagLoc, |
| 16465 | CXXScopeSpec &SS, IdentifierInfo *Name, |
| 16466 | SourceLocation NameLoc, |
| 16467 | const ParsedAttributesView &Attr, |
| 16468 | MultiTemplateParamsArg TempParamLists) { |
| 16469 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 16470 | |
| 16471 | bool IsMemberSpecialization = false; |
| 16472 | bool Invalid = false; |
| 16473 | |
| 16474 | if (TemplateParameterList *TemplateParams = |
| 16475 | MatchTemplateParametersToScopeSpecifier( |
| 16476 | TagLoc, NameLoc, SS, nullptr, TempParamLists, true, |
| 16477 | IsMemberSpecialization, Invalid)) { |
| 16478 | if (TemplateParams->size() > 0) { |
| 16479 | |
| 16480 | if (Invalid) |
| 16481 | return nullptr; |
| 16482 | |
| 16483 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, SS, Name, |
| 16484 | NameLoc, Attr, TemplateParams, AS_public, |
| 16485 | SourceLocation(), |
| 16486 | FriendLoc, TempParamLists.size() - 1, |
| 16487 | TempParamLists.data()).get(); |
| 16488 | } else { |
| 16489 | |
| 16490 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 16491 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 16492 | IsMemberSpecialization = true; |
| 16493 | } |
| 16494 | } |
| 16495 | |
| 16496 | if (Invalid) return nullptr; |
| 16497 | |
| 16498 | bool isAllExplicitSpecializations = true; |
| 16499 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
| 16500 | if (TempParamLists[I]->size()) { |
| 16501 | isAllExplicitSpecializations = false; |
| 16502 | break; |
| 16503 | } |
| 16504 | } |
| 16505 | |
| 16506 | |
| 16507 | |
| 16508 | |
| 16509 | |
| 16510 | |
| 16511 | if (isAllExplicitSpecializations) { |
| 16512 | if (SS.isEmpty()) { |
| 16513 | bool Owned = false; |
| 16514 | bool IsDependent = false; |
| 16515 | return ActOnTag(S, TagSpec, TUK_Friend, TagLoc, SS, Name, NameLoc, |
| 16516 | Attr, AS_public, |
| 16517 | SourceLocation(), |
| 16518 | MultiTemplateParamsArg(), Owned, IsDependent, |
| 16519 | SourceLocation(), |
| 16520 | false, |
| 16521 | TypeResult(), |
| 16522 | false, |
| 16523 | false); |
| 16524 | } |
| 16525 | |
| 16526 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
| 16527 | ElaboratedTypeKeyword Keyword |
| 16528 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 16529 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
| 16530 | *Name, NameLoc); |
| 16531 | if (T.isNull()) |
| 16532 | return nullptr; |
| 16533 | |
| 16534 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 16535 | if (isa<DependentNameType>(T)) { |
| 16536 | DependentNameTypeLoc TL = |
| 16537 | TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
| 16538 | TL.setElaboratedKeywordLoc(TagLoc); |
| 16539 | TL.setQualifierLoc(QualifierLoc); |
| 16540 | TL.setNameLoc(NameLoc); |
| 16541 | } else { |
| 16542 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
| 16543 | TL.setElaboratedKeywordLoc(TagLoc); |
| 16544 | TL.setQualifierLoc(QualifierLoc); |
| 16545 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(NameLoc); |
| 16546 | } |
| 16547 | |
| 16548 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 16549 | TSI, FriendLoc, TempParamLists); |
| 16550 | Friend->setAccess(AS_public); |
| 16551 | CurContext->addDecl(Friend); |
| 16552 | return Friend; |
| 16553 | } |
| 16554 | |
| 16555 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 16556 | |
| 16557 | |
| 16558 | |
| 16559 | |
| 16560 | |
| 16561 | |
| 16562 | Diag(NameLoc, diag::warn_template_qualified_friend_unsupported) |
| 16563 | << SS.getScopeRep() << SS.getRange() << cast<CXXRecordDecl>(CurContext); |
| 16564 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 16565 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 16566 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 16567 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
| 16568 | TL.setElaboratedKeywordLoc(TagLoc); |
| 16569 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 16570 | TL.setNameLoc(NameLoc); |
| 16571 | |
| 16572 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 16573 | TSI, FriendLoc, TempParamLists); |
| 16574 | Friend->setAccess(AS_public); |
| 16575 | Friend->setUnsupportedFriend(true); |
| 16576 | CurContext->addDecl(Friend); |
| 16577 | return Friend; |
| 16578 | } |
| 16579 | |
| 16580 | |
| 16581 | |
| 16582 | |
| 16583 | |
| 16584 | |
| 16585 | |
| 16586 | |
| 16587 | |
| 16588 | |
| 16589 | |
| 16590 | |
| 16591 | |
| 16592 | |
| 16593 | |
| 16594 | |
| 16595 | |
| 16596 | |
| 16597 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
| 16598 | MultiTemplateParamsArg TempParams) { |
| 16599 | SourceLocation Loc = DS.getBeginLoc(); |
| 16600 | |
| 16601 | assert(DS.isFriendSpecified()); |
| 16602 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 16603 | |
| 16604 | |
| 16605 | |
| 16606 | |
| 16607 | |
| 16608 | |
| 16609 | |
| 16610 | |
| 16611 | |
| 16612 | |
| 16613 | |
| 16614 | if (DS.getTypeQualifiers()) { |
| 16615 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 16616 | Diag(DS.getConstSpecLoc(), diag::err_friend_decl_spec) << "const"; |
| 16617 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 16618 | Diag(DS.getVolatileSpecLoc(), diag::err_friend_decl_spec) << "volatile"; |
| 16619 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
| 16620 | Diag(DS.getRestrictSpecLoc(), diag::err_friend_decl_spec) << "restrict"; |
| 16621 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 16622 | Diag(DS.getAtomicSpecLoc(), diag::err_friend_decl_spec) << "_Atomic"; |
| 16623 | if (DS.getTypeQualifiers() & DeclSpec::TQ_unaligned) |
| 16624 | Diag(DS.getUnalignedSpecLoc(), diag::err_friend_decl_spec) << "__unaligned"; |
| 16625 | } |
| 16626 | |
| 16627 | |
| 16628 | |
| 16629 | |
| 16630 | Declarator TheDeclarator(DS, DeclaratorContext::Member); |
| 16631 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 16632 | QualType T = TSI->getType(); |
| 16633 | if (TheDeclarator.isInvalidType()) |
| 16634 | return nullptr; |
| 16635 | |
| 16636 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 16637 | return nullptr; |
| 16638 | |
| 16639 | |
| 16640 | |
| 16641 | |
| 16642 | |
| 16643 | |
| 16644 | |
| 16645 | |
| 16646 | |
| 16647 | |
| 16648 | |
| 16649 | |
| 16650 | |
| 16651 | |
| 16652 | |
| 16653 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
| 16654 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 16655 | << DS.getSourceRange(); |
| 16656 | return nullptr; |
| 16657 | } |
| 16658 | |
| 16659 | |
| 16660 | |
| 16661 | |
| 16662 | |
| 16663 | |
| 16664 | |
| 16665 | |
| 16666 | |
| 16667 | |
| 16668 | |
| 16669 | |
| 16670 | Decl *D; |
| 16671 | if (!TempParams.empty()) |
| 16672 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
| 16673 | TempParams, |
| 16674 | TSI, |
| 16675 | DS.getFriendSpecLoc()); |
| 16676 | else |
| 16677 | D = CheckFriendTypeDecl(Loc, DS.getFriendSpecLoc(), TSI); |
| 16678 | |
| 16679 | if (!D) |
| 16680 | return nullptr; |
| 16681 | |
| 16682 | D->setAccess(AS_public); |
| 16683 | CurContext->addDecl(D); |
| 16684 | |
| 16685 | return D; |
| 16686 | } |
| 16687 | |
| 16688 | NamedDecl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, |
| 16689 | MultiTemplateParamsArg TemplateParams) { |
| 16690 | const DeclSpec &DS = D.getDeclSpec(); |
| 16691 | |
| 16692 | assert(DS.isFriendSpecified()); |
| 16693 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 16694 | |
| 16695 | SourceLocation Loc = D.getIdentifierLoc(); |
| 16696 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 16697 | |
| 16698 | |
| 16699 | |
| 16700 | |
| 16701 | |
| 16702 | |
| 16703 | |
| 16704 | |
| 16705 | |
| 16706 | |
| 16707 | |
| 16708 | if (!TInfo->getType()->isFunctionType()) { |
| 16709 | Diag(Loc, diag::err_unexpected_friend); |
| 16710 | |
| 16711 | |
| 16712 | |
| 16713 | return nullptr; |
| 16714 | } |
| 16715 | |
| 16716 | |
| 16717 | |
| 16718 | |
| 16719 | |
| 16720 | |
| 16721 | |
| 16722 | |
| 16723 | |
| 16724 | |
| 16725 | |
| 16726 | |
| 16727 | |
| 16728 | |
| 16729 | |
| 16730 | |
| 16731 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 16732 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 16733 | assert(NameInfo.getName()); |
| 16734 | |
| 16735 | |
| 16736 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 16737 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 16738 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 16739 | return nullptr; |
| 16740 | |
| 16741 | |
| 16742 | |
| 16743 | DeclContext *DC; |
| 16744 | Scope *DCScope = S; |
| 16745 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
| 16746 | ForExternalRedeclaration); |
| 16747 | |
| 16748 | |
| 16749 | |
| 16750 | |
| 16751 | |
| 16752 | FunctionDecl *FunctionContainingLocalClass = nullptr; |
| 16753 | if ((SS.isInvalid() || !SS.isSet()) && |
| 16754 | (FunctionContainingLocalClass = |
| 16755 | cast<CXXRecordDecl>(CurContext)->isLocalClass())) { |
| 16756 | |
| 16757 | |
| 16758 | |
| 16759 | |
| 16760 | |
| 16761 | |
| 16762 | |
| 16763 | |
| 16764 | |
| 16765 | |
| 16766 | |
| 16767 | DCScope = S->getFnParent(); |
| 16768 | |
| 16769 | |
| 16770 | Previous.clear(LookupLocalFriendName); |
| 16771 | LookupName(Previous, S, false); |
| 16772 | |
| 16773 | if (!Previous.empty()) { |
| 16774 | |
| 16775 | |
| 16776 | |
| 16777 | DC = Previous.getRepresentativeDecl()->getDeclContext(); |
| 16778 | } else { |
| 16779 | |
| 16780 | |
| 16781 | DC = FunctionContainingLocalClass; |
| 16782 | } |
| 16783 | adjustContextForLocalExternDecl(DC); |
| 16784 | |
| 16785 | |
| 16786 | |
| 16787 | |
| 16788 | |
| 16789 | if (D.isFunctionDefinition()) { |
| 16790 | Diag(NameInfo.getBeginLoc(), diag::err_friend_def_in_local_class); |
| 16791 | } |
| 16792 | |
| 16793 | |
| 16794 | |
| 16795 | |
| 16796 | } else if (SS.isInvalid() || !SS.isSet()) { |
| 16797 | |
| 16798 | |
| 16799 | |
| 16800 | |
| 16801 | |
| 16802 | |
| 16803 | bool isTemplateId = |
| 16804 | D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId; |
| 16805 | |
| 16806 | |
| 16807 | DC = CurContext; |
| 16808 | |
| 16809 | |
| 16810 | |
| 16811 | |
| 16812 | |
| 16813 | |
| 16814 | |
| 16815 | |
| 16816 | while (DC->isRecord()) |
| 16817 | DC = DC->getParent(); |
| 16818 | |
| 16819 | DeclContext *LookupDC = DC; |
| 16820 | while (LookupDC->isTransparentContext()) |
| 16821 | LookupDC = LookupDC->getParent(); |
| 16822 | |
| 16823 | while (true) { |
| 16824 | LookupQualifiedName(Previous, LookupDC); |
| 16825 | |
| 16826 | if (!Previous.empty()) { |
| 16827 | DC = LookupDC; |
| 16828 | break; |
| 16829 | } |
| 16830 | |
| 16831 | if (isTemplateId) { |
| 16832 | if (isa<TranslationUnitDecl>(LookupDC)) break; |
| 16833 | } else { |
| 16834 | if (LookupDC->isFileContext()) break; |
| 16835 | } |
| 16836 | LookupDC = LookupDC->getParent(); |
| 16837 | } |
| 16838 | |
| 16839 | DCScope = getScopeForDeclContext(S, DC); |
| 16840 | |
| 16841 | |
| 16842 | |
| 16843 | |
| 16844 | } else if (!SS.getScopeRep()->isDependent()) { |
| 16845 | DC = computeDeclContext(SS); |
| 16846 | if (!DC) return nullptr; |
| 16847 | |
| 16848 | if (RequireCompleteDeclContext(SS, DC)) return nullptr; |
| 16849 | |
| 16850 | LookupQualifiedName(Previous, DC); |
| 16851 | |
| 16852 | |
| 16853 | |
| 16854 | if (DC->Equals(CurContext)) |
| 16855 | Diag(DS.getFriendSpecLoc(), |
| 16856 | getLangOpts().CPlusPlus11 ? |
| 16857 | diag::warn_cxx98_compat_friend_is_member : |
| 16858 | diag::err_friend_is_member); |
| 16859 | |
| 16860 | if (D.isFunctionDefinition()) { |
| 16861 | |
| 16862 | |
| 16863 | |
| 16864 | |
| 16865 | |
| 16866 | |
| 16867 | |
| 16868 | |
| 16869 | SemaDiagnosticBuilder DB |
| 16870 | = Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def); |
| 16871 | |
| 16872 | DB << SS.getScopeRep(); |
| 16873 | if (DC->isFileContext()) |
| 16874 | DB << FixItHint::CreateRemoval(SS.getRange()); |
| 16875 | SS.clear(); |
| 16876 | } |
| 16877 | |
| 16878 | |
| 16879 | |
| 16880 | |
| 16881 | |
| 16882 | |
| 16883 | } else { |
| 16884 | if (D.isFunctionDefinition()) { |
| 16885 | |
| 16886 | |
| 16887 | |
| 16888 | |
| 16889 | Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def) |
| 16890 | << SS.getScopeRep(); |
| 16891 | } |
| 16892 | |
| 16893 | DC = CurContext; |
| 16894 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
| 16895 | } |
| 16896 | |
| 16897 | if (!DC->isRecord()) { |
| 16898 | int DiagArg = -1; |
| 16899 | switch (D.getName().getKind()) { |
| 16900 | case UnqualifiedIdKind::IK_ConstructorTemplateId: |
| 16901 | case UnqualifiedIdKind::IK_ConstructorName: |
| 16902 | DiagArg = 0; |
| 16903 | break; |
| 16904 | case UnqualifiedIdKind::IK_DestructorName: |
| 16905 | DiagArg = 1; |
| 16906 | break; |
| 16907 | case UnqualifiedIdKind::IK_ConversionFunctionId: |
| 16908 | DiagArg = 2; |
| 16909 | break; |
| 16910 | case UnqualifiedIdKind::IK_DeductionGuideName: |
| 16911 | DiagArg = 3; |
| 16912 | break; |
| 16913 | case UnqualifiedIdKind::IK_Identifier: |
| 16914 | case UnqualifiedIdKind::IK_ImplicitSelfParam: |
| 16915 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
| 16916 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
| 16917 | case UnqualifiedIdKind::IK_TemplateId: |
| 16918 | break; |
| 16919 | } |
| 16920 | |
| 16921 | if (DiagArg >= 0) { |
| 16922 | Diag(Loc, diag::err_introducing_special_friend) << DiagArg; |
| 16923 | return nullptr; |
| 16924 | } |
| 16925 | } |
| 16926 | |
| 16927 | |
| 16928 | |
| 16929 | |
| 16930 | Scope FakeDCScope(S, Scope::DeclScope, Diags); |
| 16931 | if (!DCScope) { |
| 16932 | FakeDCScope.setEntity(DC); |
| 16933 | DCScope = &FakeDCScope; |
| 16934 | } |
| 16935 | |
| 16936 | bool AddToScope = true; |
| 16937 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, TInfo, Previous, |
| 16938 | TemplateParams, AddToScope); |
| 16939 | if (!ND) return nullptr; |
| 16940 | |
| 16941 | assert(ND->getLexicalDeclContext() == CurContext); |
| 16942 | |
| 16943 | |
| 16944 | |
| 16945 | DC = ND->getDeclContext(); |
| 16946 | |
| 16947 | |
| 16948 | |
| 16949 | |
| 16950 | |
| 16951 | |
| 16952 | |
| 16953 | if (!CurContext->isDependentContext()) { |
| 16954 | DC = DC->getRedeclContext(); |
| 16955 | DC->makeDeclVisibleInContext(ND); |
| 16956 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 16957 | PushOnScopeChains(ND, EnclosingScope, false); |
| 16958 | } |
| 16959 | |
| 16960 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
| 16961 | D.getIdentifierLoc(), ND, |
| 16962 | DS.getFriendSpecLoc()); |
| 16963 | FrD->setAccess(AS_public); |
| 16964 | CurContext->addDecl(FrD); |
| 16965 | |
| 16966 | if (ND->isInvalidDecl()) { |
| 16967 | FrD->setInvalidDecl(); |
| 16968 | } else { |
| 16969 | if (DC->isRecord()) CheckFriendAccess(ND); |
| 16970 | |
| 16971 | FunctionDecl *FD; |
| 16972 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 16973 | FD = FTD->getTemplatedDecl(); |
| 16974 | else |
| 16975 | FD = cast<FunctionDecl>(ND); |
| 16976 | |
| 16977 | |
| 16978 | |
| 16979 | |
| 16980 | |
| 16981 | if (functionDeclHasDefaultArgument(FD)) { |
| 16982 | |
| 16983 | |
| 16984 | |
| 16985 | if (D.isRedeclaration()) { |
| 16986 | Diag(FD->getLocation(), diag::err_friend_decl_with_def_arg_redeclared); |
| 16987 | Diag(Previous.getRepresentativeDecl()->getLocation(), |
| 16988 | diag::note_previous_declaration); |
| 16989 | } else if (!D.isFunctionDefinition()) |
| 16990 | Diag(FD->getLocation(), diag::err_friend_decl_with_def_arg_must_be_def); |
| 16991 | } |
| 16992 | |
| 16993 | |
| 16994 | if (FD->getNumTemplateParameterLists() && SS.isValid()) { |
| 16995 | Diag(FD->getLocation(), diag::warn_template_qualified_friend_unsupported) |
| 16996 | << SS.getScopeRep() << SS.getRange() |
| 16997 | << cast<CXXRecordDecl>(CurContext); |
| 16998 | FrD->setUnsupportedFriend(true); |
| 16999 | } |
| 17000 | } |
| 17001 | |
| 17002 | warnOnReservedIdentifier(ND); |
| 17003 | |
| 17004 | return ND; |
| 17005 | } |
| 17006 | |
| 17007 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 17008 | AdjustDeclIfTemplate(Dcl); |
| 17009 | |
| 17010 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(Dcl); |
| 17011 | if (!Fn) { |
| 17012 | Diag(DelLoc, diag::err_deleted_non_function); |
| 17013 | return; |
| 17014 | } |
| 17015 | |
| 17016 | |
| 17017 | Fn->setWillHaveBody(false); |
| 17018 | |
| 17019 | if (const FunctionDecl *Prev = Fn->getPreviousDecl()) { |
| 17020 | |
| 17021 | |
| 17022 | if ((Prev->getTemplateSpecializationKind() != TSK_ExplicitSpecialization || |
| 17023 | Prev->getPreviousDecl()) && |
| 17024 | !Prev->isDefined()) { |
| 17025 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 17026 | Diag(Prev->getLocation().isInvalid() ? DelLoc : Prev->getLocation(), |
| 17027 | Prev->isImplicit() ? diag::note_previous_implicit_declaration |
| 17028 | : diag::note_previous_declaration); |
| 17029 | |
| 17030 | |
| 17031 | Fn->setInvalidDecl(); |
| 17032 | return; |
| 17033 | } |
| 17034 | |
| 17035 | |
| 17036 | |
| 17037 | |
| 17038 | |
| 17039 | Fn = Fn->getCanonicalDecl(); |
| 17040 | } |
| 17041 | |
| 17042 | |
| 17043 | if (const InheritableAttr *DLLAttr = getDLLAttr(Fn)) { |
| 17044 | Diag(Fn->getLocation(), diag::err_attribute_dll_deleted) << DLLAttr; |
| 17045 | Fn->setInvalidDecl(); |
| 17046 | } |
| 17047 | |
| 17048 | |
| 17049 | |
| 17050 | if (Fn->isMain()) |
| 17051 | Diag(DelLoc, diag::err_deleted_main); |
| 17052 | |
| 17053 | |
| 17054 | |
| 17055 | Fn->setImplicitlyInline(); |
| 17056 | Fn->setDeletedAsWritten(); |
| 17057 | } |
| 17058 | |
| 17059 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
| 17060 | if (!Dcl || Dcl->isInvalidDecl()) |
| 17061 | return; |
| 17062 | |
| 17063 | auto *FD = dyn_cast<FunctionDecl>(Dcl); |
| 17064 | if (!FD) { |
| 17065 | if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Dcl)) { |
| 17066 | if (getDefaultedFunctionKind(FTD->getTemplatedDecl()).isComparison()) { |
| 17067 | Diag(DefaultLoc, diag::err_defaulted_comparison_template); |
| 17068 | return; |
| 17069 | } |
| 17070 | } |
| 17071 | |
| 17072 | Diag(DefaultLoc, diag::err_default_special_members) |
| 17073 | << getLangOpts().CPlusPlus20; |
| 17074 | return; |
| 17075 | } |
| 17076 | |
| 17077 | |
| 17078 | DefaultedFunctionKind DefKind = getDefaultedFunctionKind(FD); |
| 17079 | if (!DefKind && |
| 17080 | |
| 17081 | |
| 17082 | |
| 17083 | (!FD->isDependentContext() || |
| 17084 | (!isa<CXXConstructorDecl>(FD) && |
| 17085 | FD->getDeclName().getCXXOverloadedOperator() != OO_Equal))) { |
| 17086 | Diag(DefaultLoc, diag::err_default_special_members) |
| 17087 | << getLangOpts().CPlusPlus20; |
| 17088 | return; |
| 17089 | } |
| 17090 | |
| 17091 | if (DefKind.isComparison() && |
| 17092 | !isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { |
| 17093 | Diag(FD->getLocation(), diag::err_defaulted_comparison_out_of_class) |
| 17094 | << (int)DefKind.asComparison(); |
| 17095 | return; |
| 17096 | } |
| 17097 | |
| 17098 | |
| 17099 | |
| 17100 | if (DefKind.isComparison() && |
| 17101 | DefKind.asComparison() != DefaultedComparisonKind::ThreeWay) { |
| 17102 | Diag(DefaultLoc, getLangOpts().CPlusPlus20 |
| 17103 | ? diag::warn_cxx17_compat_defaulted_comparison |
| 17104 | : diag::ext_defaulted_comparison); |
| 17105 | } |
| 17106 | |
| 17107 | FD->setDefaulted(); |
| 17108 | FD->setExplicitlyDefaulted(); |
| 17109 | |
| 17110 | |
| 17111 | if (FD->isDependentContext()) |
| 17112 | return; |
| 17113 | |
| 17114 | |
| 17115 | |
| 17116 | |
| 17117 | FD->setWillHaveBody(false); |
| 17118 | |
| 17119 | |
| 17120 | |
| 17121 | |
| 17122 | if (DefKind.isComparison()) |
| 17123 | return; |
| 17124 | auto *MD = cast<CXXMethodDecl>(FD); |
| 17125 | |
| 17126 | const FunctionDecl *Primary = FD; |
| 17127 | if (const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern()) |
| 17128 | |
| 17129 | |
| 17130 | Primary = Pattern; |
| 17131 | |
| 17132 | |
| 17133 | |
| 17134 | |
| 17135 | if (Primary->getCanonicalDecl()->isDefaulted()) |
| 17136 | return; |
| 17137 | |
| 17138 | |
| 17139 | |
| 17140 | if (CheckExplicitlyDefaultedSpecialMember(MD, DefKind.asSpecialMember())) |
| 17141 | MD->setInvalidDecl(); |
| 17142 | else |
| 17143 | DefineDefaultedFunction(*this, MD, DefaultLoc); |
| 17144 | } |
| 17145 | |
| 17146 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 17147 | for (Stmt *SubStmt : S->children()) { |
| 17148 | if (!SubStmt) |
| 17149 | continue; |
| 17150 | if (isa<ReturnStmt>(SubStmt)) |
| 17151 | Self.Diag(SubStmt->getBeginLoc(), |
| 17152 | diag::err_return_in_constructor_handler); |
| 17153 | if (!isa<Expr>(SubStmt)) |
| 17154 | SearchForReturnInStmt(Self, SubStmt); |
| 17155 | } |
| 17156 | } |
| 17157 | |
| 17158 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 17159 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 17160 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 17161 | SearchForReturnInStmt(*this, Handler); |
| 17162 | } |
| 17163 | } |
| 17164 | |
| 17165 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
| 17166 | const CXXMethodDecl *Old) { |
| 17167 | const auto *NewFT = New->getType()->castAs<FunctionProtoType>(); |
| 17168 | const auto *OldFT = Old->getType()->castAs<FunctionProtoType>(); |
| 17169 | |
| 17170 | if (OldFT->hasExtParameterInfos()) { |
| 17171 | for (unsigned I = 0, E = OldFT->getNumParams(); I != E; ++I) |
| 17172 | |
| 17173 | |
| 17174 | if (OldFT->getExtParameterInfo(I).isNoEscape() && |
| 17175 | !NewFT->getExtParameterInfo(I).isNoEscape()) { |
| 17176 | Diag(New->getParamDecl(I)->getLocation(), |
| 17177 | diag::warn_overriding_method_missing_noescape); |
| 17178 | Diag(Old->getParamDecl(I)->getLocation(), |
| 17179 | diag::note_overridden_marked_noescape); |
| 17180 | } |
| 17181 | } |
| 17182 | |
| 17183 | |
| 17184 | const auto *OldCSA = Old->getAttr<CodeSegAttr>(); |
| 17185 | const auto *NewCSA = New->getAttr<CodeSegAttr>(); |
| 17186 | if ((NewCSA || OldCSA) && |
| 17187 | (!OldCSA || !NewCSA || NewCSA->getName() != OldCSA->getName())) { |
| 17188 | Diag(New->getLocation(), diag::err_mismatched_code_seg_override); |
| 17189 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 17190 | return true; |
| 17191 | } |
| 17192 | |
| 17193 | CallingConv NewCC = NewFT->getCallConv(), OldCC = OldFT->getCallConv(); |
| 17194 | |
| 17195 | |
| 17196 | if (NewCC == OldCC) |
| 17197 | return false; |
| 17198 | |
| 17199 | |
| 17200 | |
| 17201 | |
| 17202 | |
| 17203 | if (New->getStorageClass() == SC_Static) |
| 17204 | return false; |
| 17205 | |
| 17206 | Diag(New->getLocation(), |
| 17207 | diag::err_conflicting_overriding_cc_attributes) |
| 17208 | << New->getDeclName() << New->getType() << Old->getType(); |
| 17209 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 17210 | return true; |
| 17211 | } |
| 17212 | |
| 17213 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
| 17214 | const CXXMethodDecl *Old) { |
| 17215 | QualType NewTy = New->getType()->castAs<FunctionType>()->getReturnType(); |
| 17216 | QualType OldTy = Old->getType()->castAs<FunctionType>()->getReturnType(); |
| 17217 | |
| 17218 | if (Context.hasSameType(NewTy, OldTy) || |
| 17219 | NewTy->isDependentType() || OldTy->isDependentType()) |
| 17220 | return false; |
| 17221 | |
| 17222 | |
| 17223 | QualType NewClassTy, OldClassTy; |
| 17224 | |
| 17225 | |
| 17226 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 17227 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
| 17228 | NewClassTy = NewPT->getPointeeType(); |
| 17229 | OldClassTy = OldPT->getPointeeType(); |
| 17230 | } |
| 17231 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 17232 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 17233 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 17234 | NewClassTy = NewRT->getPointeeType(); |
| 17235 | OldClassTy = OldRT->getPointeeType(); |
| 17236 | } |
| 17237 | } |
| 17238 | } |
| 17239 | |
| 17240 | |
| 17241 | if (NewClassTy.isNull()) { |
| 17242 | Diag(New->getLocation(), |
| 17243 | diag::err_different_return_type_for_overriding_virtual_function) |
| 17244 | << New->getDeclName() << NewTy << OldTy |
| 17245 | << New->getReturnTypeSourceRange(); |
| 17246 | Diag(Old->getLocation(), diag::note_overridden_virtual_function) |
| 17247 | << Old->getReturnTypeSourceRange(); |
| 17248 | |
| 17249 | return true; |
| 17250 | } |
| 17251 | |
| 17252 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
| 17253 | |
| 17254 | |
| 17255 | |
| 17256 | |
| 17257 | |
| 17258 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 17259 | if (!RT->isBeingDefined() && |
| 17260 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 17261 | diag::err_covariant_return_incomplete, |
| 17262 | New->getDeclName())) |
| 17263 | return true; |
| 17264 | } |
| 17265 | |
| 17266 | |
| 17267 | if (!IsDerivedFrom(New->getLocation(), NewClassTy, OldClassTy)) { |
| 17268 | Diag(New->getLocation(), diag::err_covariant_return_not_derived) |
| 17269 | << New->getDeclName() << NewTy << OldTy |
| 17270 | << New->getReturnTypeSourceRange(); |
| 17271 | Diag(Old->getLocation(), diag::note_overridden_virtual_function) |
| 17272 | << Old->getReturnTypeSourceRange(); |
| 17273 | return true; |
| 17274 | } |
| 17275 | |
| 17276 | |
| 17277 | if (CheckDerivedToBaseConversion( |
| 17278 | NewClassTy, OldClassTy, |
| 17279 | diag::err_covariant_return_inaccessible_base, |
| 17280 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 17281 | New->getLocation(), New->getReturnTypeSourceRange(), |
| 17282 | New->getDeclName(), nullptr)) { |
| 17283 | |
| 17284 | |
| 17285 | |
| 17286 | |
| 17287 | Diag(Old->getLocation(), diag::note_overridden_virtual_function) |
| 17288 | << Old->getReturnTypeSourceRange(); |
| 17289 | return true; |
| 17290 | } |
| 17291 | } |
| 17292 | |
| 17293 | |
| 17294 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
| 17295 | Diag(New->getLocation(), |
| 17296 | diag::err_covariant_return_type_different_qualifications) |
| 17297 | << New->getDeclName() << NewTy << OldTy |
| 17298 | << New->getReturnTypeSourceRange(); |
| 17299 | Diag(Old->getLocation(), diag::note_overridden_virtual_function) |
| 17300 | << Old->getReturnTypeSourceRange(); |
| 17301 | return true; |
| 17302 | } |
| 17303 | |
| 17304 | |
| 17305 | |
| 17306 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 17307 | Diag(New->getLocation(), |
| 17308 | diag::err_covariant_return_type_class_type_more_qualified) |
| 17309 | << New->getDeclName() << NewTy << OldTy |
| 17310 | << New->getReturnTypeSourceRange(); |
| 17311 | Diag(Old->getLocation(), diag::note_overridden_virtual_function) |
| 17312 | << Old->getReturnTypeSourceRange(); |
| 17313 | return true; |
| 17314 | } |
| 17315 | |
| 17316 | return false; |
| 17317 | } |
| 17318 | |
| 17319 | |
| 17320 | |
| 17321 | |
| 17322 | |
| 17323 | |
| 17324 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| 17325 | SourceLocation EndLoc = InitRange.getEnd(); |
| 17326 | if (EndLoc.isValid()) |
| 17327 | Method->setRangeEnd(EndLoc); |
| 17328 | |
| 17329 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 17330 | Method->setPure(); |
| 17331 | return false; |
| 17332 | } |
| 17333 | |
| 17334 | if (!Method->isInvalidDecl()) |
| 17335 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 17336 | << Method->getDeclName() << InitRange; |
| 17337 | return true; |
| 17338 | } |
| 17339 | |
| 17340 | void Sema::ActOnPureSpecifier(Decl *D, SourceLocation ZeroLoc) { |
| 17341 | if (D->getFriendObjectKind()) |
| 17342 | Diag(D->getLocation(), diag::err_pure_friend); |
| 17343 | else if (auto *M = dyn_cast<CXXMethodDecl>(D)) |
| 17344 | CheckPureMethod(M, ZeroLoc); |
| 17345 | else |
| 17346 | Diag(D->getLocation(), diag::err_illegal_initializer); |
| 17347 | } |
| 17348 | |
| 17349 | |
| 17350 | |
| 17351 | static bool isNonlocalVariable(const Decl *D) { |
| 17352 | if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(D)) |
| 17353 | return Var->hasGlobalStorage(); |
| 17354 | |
| 17355 | return false; |
| 17356 | } |
| 17357 | |
| 17358 | |
| 17359 | |
| 17360 | |
| 17361 | |
| 17362 | |
| 17363 | |
| 17364 | |
| 17365 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
| 17366 | |
| 17367 | if (!D || D->isInvalidDecl()) |
| 17368 | return; |
| 17369 | |
| 17370 | |
| 17371 | |
| 17372 | |
| 17373 | |
| 17374 | if (S && D->isOutOfLine()) |
| 17375 | EnterDeclaratorContext(S, D->getDeclContext()); |
| 17376 | |
| 17377 | |
| 17378 | |
| 17379 | |
| 17380 | if (isNonlocalVariable(D)) |
| 17381 | PushExpressionEvaluationContext( |
| 17382 | ExpressionEvaluationContext::PotentiallyEvaluated, D); |
| 17383 | } |
| 17384 | |
| 17385 | |
| 17386 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
| 17387 | |
| 17388 | if (!D || D->isInvalidDecl()) |
| 17389 | return; |
| 17390 | |
| 17391 | if (isNonlocalVariable(D)) |
| 17392 | PopExpressionEvaluationContext(); |
| 17393 | |
| 17394 | if (S && D->isOutOfLine()) |
| 17395 | ExitDeclaratorContext(S); |
| 17396 | } |
| 17397 | |
| 17398 | |
| 17399 | |
| 17400 | |
| 17401 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
| 17402 | |
| 17403 | |
| 17404 | |
| 17405 | |
| 17406 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 17407 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 17408 | |
| 17409 | Decl *Dcl = ActOnDeclarator(S, D); |
| 17410 | if (!Dcl) |
| 17411 | return true; |
| 17412 | |
| 17413 | if (isa<FunctionDecl>(Dcl)) { |
| 17414 | Diag(Dcl->getLocation(), diag::err_invalid_use_of_function_type) |
| 17415 | << D.getSourceRange(); |
| 17416 | return true; |
| 17417 | } |
| 17418 | |
| 17419 | return Dcl; |
| 17420 | } |
| 17421 | |
| 17422 | void Sema::LoadExternalVTableUses() { |
| 17423 | if (!ExternalSource) |
| 17424 | return; |
| 17425 | |
| 17426 | SmallVector<ExternalVTableUse, 4> VTables; |
| 17427 | ExternalSource->ReadUsedVTables(VTables); |
| 17428 | SmallVector<VTableUse, 4> NewUses; |
| 17429 | for (unsigned I = 0, N = VTables.size(); I != N; ++I) { |
| 17430 | llvm::DenseMap<CXXRecordDecl *, bool>::iterator Pos |
| 17431 | = VTablesUsed.find(VTables[I].Record); |
| 17432 | |
| 17433 | if (Pos != VTablesUsed.end()) { |
| 17434 | if (!Pos->second && VTables[I].DefinitionRequired) |
| 17435 | Pos->second = true; |
| 17436 | continue; |
| 17437 | } |
| 17438 | |
| 17439 | VTablesUsed[VTables[I].Record] = VTables[I].DefinitionRequired; |
| 17440 | NewUses.push_back(VTableUse(VTables[I].Record, VTables[I].Location)); |
| 17441 | } |
| 17442 | |
| 17443 | VTableUses.insert(VTableUses.begin(), NewUses.begin(), NewUses.end()); |
| 17444 | } |
| 17445 | |
| 17446 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 17447 | bool DefinitionRequired) { |
| 17448 | |
| 17449 | |
| 17450 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 17451 | CurContext->isDependentContext() || isUnevaluatedContext()) |
| 17452 | return; |
| 17453 | |
| 17454 | |
| 17455 | if (TUKind != TU_Prefix && LangOpts.OpenMP && LangOpts.OpenMPIsDevice && |
| 17456 | !isInOpenMPDeclareTargetContext() && |
| 17457 | !isInOpenMPTargetExecutionDirective()) { |
| 17458 | if (!DefinitionRequired) |
| 17459 | MarkVirtualMembersReferenced(Loc, Class); |
| 17460 | return; |
| 17461 | } |
| 17462 | |
| 17463 | |
| 17464 | LoadExternalVTableUses(); |
| 17465 | Class = Class->getCanonicalDecl(); |
| 17466 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 17467 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 17468 | if (!Pos.second) { |
| 17469 | |
| 17470 | |
| 17471 | |
| 17472 | if (DefinitionRequired && !Pos.first->second) { |
| 17473 | Pos.first->second = true; |
| 17474 | } else { |
| 17475 | |
| 17476 | return; |
| 17477 | } |
| 17478 | } else { |
| 17479 | |
| 17480 | |
| 17481 | |
| 17482 | |
| 17483 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 17484 | CXXDestructorDecl *DD = Class->getDestructor(); |
| 17485 | if (DD && DD->isVirtual() && !DD->isDeleted()) { |
| 17486 | if (Class->hasUserDeclaredDestructor() && !DD->isDefined()) { |
| 17487 | |
| 17488 | |
| 17489 | |
| 17490 | ContextRAII SavedContext(*this, DD); |
| 17491 | CheckDestructor(DD); |
| 17492 | } else { |
| 17493 | MarkFunctionReferenced(Loc, Class->getDestructor()); |
| 17494 | } |
| 17495 | } |
| 17496 | } |
| 17497 | } |
| 17498 | |
| 17499 | |
| 17500 | |
| 17501 | |
| 17502 | if (Class->isLocalClass()) |
| 17503 | MarkVirtualMembersReferenced(Loc, Class); |
| 17504 | else |
| 17505 | VTableUses.push_back(std::make_pair(Class, Loc)); |
| 17506 | } |
| 17507 | |
| 17508 | bool Sema::DefineUsedVTables() { |
| 17509 | LoadExternalVTableUses(); |
| 17510 | if (VTableUses.empty()) |
| 17511 | return false; |
| 17512 | |
| 17513 | |
| 17514 | |
| 17515 | |
| 17516 | |
| 17517 | bool DefinedAnything = false; |
| 17518 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
| 17519 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
| 17520 | if (!Class) |
| 17521 | continue; |
| 17522 | TemplateSpecializationKind ClassTSK = |
| 17523 | Class->getTemplateSpecializationKind(); |
| 17524 | |
| 17525 | SourceLocation Loc = VTableUses[I].second; |
| 17526 | |
| 17527 | bool DefineVTable = true; |
| 17528 | |
| 17529 | |
| 17530 | |
| 17531 | |
| 17532 | const CXXMethodDecl *KeyFunction = Context.getCurrentKeyFunction(Class); |
| 17533 | if (KeyFunction && !KeyFunction->hasBody()) { |
| 17534 | |
| 17535 | DefineVTable = false; |
| 17536 | TemplateSpecializationKind TSK = |
| 17537 | KeyFunction->getTemplateSpecializationKind(); |
| 17538 | assert(TSK != TSK_ExplicitInstantiationDefinition && |
| 17539 | TSK != TSK_ImplicitInstantiation && |
| 17540 | "Instantiations don't have key functions"); |
| 17541 | (void)TSK; |
| 17542 | } else if (!KeyFunction) { |
| 17543 | |
| 17544 | |
| 17545 | |
| 17546 | |
| 17547 | bool IsExplicitInstantiationDeclaration = |
| 17548 | ClassTSK == TSK_ExplicitInstantiationDeclaration; |
| 17549 | for (auto R : Class->redecls()) { |
| 17550 | TemplateSpecializationKind TSK |
| 17551 | = cast<CXXRecordDecl>(R)->getTemplateSpecializationKind(); |
| 17552 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 17553 | IsExplicitInstantiationDeclaration = true; |
| 17554 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 17555 | IsExplicitInstantiationDeclaration = false; |
| 17556 | break; |
| 17557 | } |
| 17558 | } |
| 17559 | |
| 17560 | if (IsExplicitInstantiationDeclaration) |
| 17561 | DefineVTable = false; |
| 17562 | } |
| 17563 | |
| 17564 | |
| 17565 | |
| 17566 | |
| 17567 | if (!DefineVTable) { |
| 17568 | MarkVirtualMemberExceptionSpecsNeeded(Loc, Class); |
| 17569 | continue; |
| 17570 | } |
| 17571 | |
| 17572 | |
| 17573 | |
| 17574 | |
| 17575 | DefinedAnything = true; |
| 17576 | MarkVirtualMembersReferenced(Loc, Class); |
| 17577 | CXXRecordDecl *Canonical = Class->getCanonicalDecl(); |
| 17578 | if (VTablesUsed[Canonical]) |
| 17579 | Consumer.HandleVTable(Class); |
| 17580 | |
| 17581 | |
| 17582 | |
| 17583 | |
| 17584 | if (Context.getTargetInfo().getCXXABI().hasKeyFunctions() && |
| 17585 | Class->isExternallyVisible() && ClassTSK != TSK_ImplicitInstantiation) { |
| 17586 | const FunctionDecl *KeyFunctionDef = nullptr; |
| 17587 | if (!KeyFunction || (KeyFunction->hasBody(KeyFunctionDef) && |
| 17588 | KeyFunctionDef->isInlined())) { |
| 17589 | Diag(Class->getLocation(), |
| 17590 | ClassTSK == TSK_ExplicitInstantiationDefinition |
| 17591 | ? diag::warn_weak_template_vtable |
| 17592 | : diag::warn_weak_vtable) |
| 17593 | << Class; |
| 17594 | } |
| 17595 | } |
| 17596 | } |
| 17597 | VTableUses.clear(); |
| 17598 | |
| 17599 | return DefinedAnything; |
| 17600 | } |
| 17601 | |
| 17602 | void Sema::MarkVirtualMemberExceptionSpecsNeeded(SourceLocation Loc, |
| 17603 | const CXXRecordDecl *RD) { |
| 17604 | for (const auto *I : RD->methods()) |
| 17605 | if (I->isVirtual() && !I->isPure()) |
| 17606 | ResolveExceptionSpec(Loc, I->getType()->castAs<FunctionProtoType>()); |
| 17607 | } |
| 17608 | |
| 17609 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 17610 | const CXXRecordDecl *RD, |
| 17611 | bool ConstexprOnly) { |
| 17612 | |
| 17613 | CXXFinalOverriderMap FinalOverriders; |
| 17614 | RD->getFinalOverriders(FinalOverriders); |
| 17615 | for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(), |
| 17616 | E = FinalOverriders.end(); |
| 17617 | I != E; ++I) { |
| 17618 | for (OverridingMethods::const_iterator OI = I->second.begin(), |
| 17619 | OE = I->second.end(); |
| 17620 | OI != OE; ++OI) { |
| 17621 | assert(OI->second.size() > 0 && "no final overrider"); |
| 17622 | CXXMethodDecl *Overrider = OI->second.front().Method; |
| 17623 | |
| 17624 | |
| 17625 | |
| 17626 | if (!Overrider->isPure() && (!ConstexprOnly || Overrider->isConstexpr())) |
| 17627 | MarkFunctionReferenced(Loc, Overrider); |
| 17628 | } |
| 17629 | } |
| 17630 | |
| 17631 | |
| 17632 | if (RD->getNumVBases() == 0) |
| 17633 | return; |
| 17634 | |
| 17635 | for (const auto &I : RD->bases()) { |
| 17636 | const auto *Base = |
| 17637 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
| 17638 | if (Base->getNumVBases() == 0) |
| 17639 | continue; |
| 17640 | MarkVirtualMembersReferenced(Loc, Base); |
| 17641 | } |
| 17642 | } |
| 17643 | |
| 17644 | |
| 17645 | |
| 17646 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 17647 | if (!getLangOpts().CPlusPlus) |
| 17648 | return; |
| 17649 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
| 17650 | SmallVector<ObjCIvarDecl*, 8> ivars; |
| 17651 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 17652 | if (ivars.empty()) |
| 17653 | return; |
| 17654 | SmallVector<CXXCtorInitializer*, 32> AllToInit; |
| 17655 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 17656 | FieldDecl *Field = ivars[i]; |
| 17657 | if (Field->isInvalidDecl()) |
| 17658 | continue; |
| 17659 | |
| 17660 | CXXCtorInitializer *Member; |
| 17661 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 17662 | InitializationKind InitKind = |
| 17663 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 17664 | |
| 17665 | InitializationSequence InitSeq(*this, InitEntity, InitKind, None); |
| 17666 | ExprResult MemberInit = |
| 17667 | InitSeq.Perform(*this, InitEntity, InitKind, None); |
| 17668 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
| 17669 | |
| 17670 | |
| 17671 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 17672 | continue; |
| 17673 | |
| 17674 | Member = |
| 17675 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 17676 | SourceLocation(), |
| 17677 | MemberInit.getAs<Expr>(), |
| 17678 | SourceLocation()); |
| 17679 | AllToInit.push_back(Member); |
| 17680 | |
| 17681 | |
| 17682 | if (const RecordType *RecordTy = |
| 17683 | Context.getBaseElementType(Field->getType()) |
| 17684 | ->getAs<RecordType>()) { |
| 17685 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 17686 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
| 17687 | MarkFunctionReferenced(Field->getLocation(), Destructor); |
| 17688 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 17689 | PDiag(diag::err_access_dtor_ivar) |
| 17690 | << Context.getBaseElementType(Field->getType())); |
| 17691 | } |
| 17692 | } |
| 17693 | } |
| 17694 | ObjCImplementation->setIvarInitializers(Context, |
| 17695 | AllToInit.data(), AllToInit.size()); |
| 17696 | } |
| 17697 | } |
| 17698 | |
| 17699 | static |
| 17700 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 17701 | llvm::SmallPtrSet<CXXConstructorDecl*, 4> &Valid, |
| 17702 | llvm::SmallPtrSet<CXXConstructorDecl*, 4> &Invalid, |
| 17703 | llvm::SmallPtrSet<CXXConstructorDecl*, 4> &Current, |
| 17704 | Sema &S) { |
| 17705 | if (Ctor->isInvalidDecl()) |
| 17706 | return; |
| 17707 | |
| 17708 | CXXConstructorDecl *Target = Ctor->getTargetConstructor(); |
| 17709 | |
| 17710 | |
| 17711 | |
| 17712 | if (Target) { |
| 17713 | const FunctionDecl *FNTarget = nullptr; |
| 17714 | (void)Target->hasBody(FNTarget); |
| 17715 | Target = const_cast<CXXConstructorDecl*>( |
| 17716 | cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 17717 | } |
| 17718 | |
| 17719 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 17720 | |
| 17721 | *TCanonical = Target? Target->getCanonicalDecl() : nullptr; |
| 17722 | |
| 17723 | if (!Current.insert(Canonical).second) |
| 17724 | return; |
| 17725 | |
| 17726 | |
| 17727 | if (!Target || !Target->isDelegatingConstructor() || |
| 17728 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
| 17729 | Valid.insert(Current.begin(), Current.end()); |
| 17730 | Current.clear(); |
| 17731 | |
| 17732 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 17733 | Current.count(TCanonical)) { |
| 17734 | |
| 17735 | if (!Invalid.count(TCanonical)) { |
| 17736 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
| 17737 | diag::warn_delegating_ctor_cycle) |
| 17738 | << Ctor; |
| 17739 | |
| 17740 | |
| 17741 | if (TCanonical != Canonical) |
| 17742 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 17743 | |
| 17744 | CXXConstructorDecl *C = Target; |
| 17745 | while (C->getCanonicalDecl() != Canonical) { |
| 17746 | const FunctionDecl *FNTarget = nullptr; |
| 17747 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 17748 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 17749 | |
| 17750 | C = const_cast<CXXConstructorDecl*>( |
| 17751 | cast<CXXConstructorDecl>(FNTarget)); |
| 17752 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 17753 | } |
| 17754 | } |
| 17755 | |
| 17756 | Invalid.insert(Current.begin(), Current.end()); |
| 17757 | Current.clear(); |
| 17758 | } else { |
| 17759 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 17760 | } |
| 17761 | } |
| 17762 | |
| 17763 | |
| 17764 | void Sema::CheckDelegatingCtorCycles() { |
| 17765 | llvm::SmallPtrSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 17766 | |
| 17767 | for (DelegatingCtorDeclsType::iterator |
| 17768 | I = DelegatingCtorDecls.begin(ExternalSource), |
| 17769 | E = DelegatingCtorDecls.end(); |
| 17770 | I != E; ++I) |
| 17771 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
| 17772 | |
| 17773 | for (auto CI = Invalid.begin(), CE = Invalid.end(); CI != CE; ++CI) |
| 17774 | (*CI)->setInvalidDecl(); |
| 17775 | } |
| 17776 | |
| 17777 | namespace { |
| 17778 | |
| 17779 | class FindCXXThisExpr : public RecursiveASTVisitor<FindCXXThisExpr> { |
| 17780 | Sema &S; |
| 17781 | |
| 17782 | public: |
| 17783 | explicit FindCXXThisExpr(Sema &S) : S(S) { } |
| 17784 | |
| 17785 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 17786 | S.Diag(E->getLocation(), diag::err_this_static_member_func) |
| 17787 | << E->isImplicit(); |
| 17788 | return false; |
| 17789 | } |
| 17790 | }; |
| 17791 | } |
| 17792 | |
| 17793 | bool Sema::checkThisInStaticMemberFunctionType(CXXMethodDecl *Method) { |
| 17794 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 17795 | if (!TSInfo) |
| 17796 | return false; |
| 17797 | |
| 17798 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 17799 | FunctionProtoTypeLoc ProtoTL = TL.getAs<FunctionProtoTypeLoc>(); |
| 17800 | if (!ProtoTL) |
| 17801 | return false; |
| 17802 | |
| 17803 | |
| 17804 | |
| 17805 | |
| 17806 | |
| 17807 | |
| 17808 | |
| 17809 | |
| 17810 | const FunctionProtoType *Proto = ProtoTL.getTypePtr(); |
| 17811 | FindCXXThisExpr Finder(*this); |
| 17812 | |
| 17813 | |
| 17814 | if (Proto->hasTrailingReturn() && |
| 17815 | !Finder.TraverseTypeLoc(ProtoTL.getReturnLoc())) |
| 17816 | return true; |
| 17817 | |
| 17818 | |
| 17819 | if (checkThisInStaticMemberFunctionExceptionSpec(Method)) |
| 17820 | return true; |
| 17821 | |
| 17822 | |
| 17823 | if (Expr *E = Method->getTrailingRequiresClause()) |
| 17824 | if (!Finder.TraverseStmt(E)) |
| 17825 | return true; |
| 17826 | |
| 17827 | return checkThisInStaticMemberFunctionAttributes(Method); |
| 17828 | } |
| 17829 | |
| 17830 | bool Sema::checkThisInStaticMemberFunctionExceptionSpec(CXXMethodDecl *Method) { |
| 17831 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 17832 | if (!TSInfo) |
| 17833 | return false; |
| 17834 | |
| 17835 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 17836 | FunctionProtoTypeLoc ProtoTL = TL.getAs<FunctionProtoTypeLoc>(); |
| 17837 | if (!ProtoTL) |
| 17838 | return false; |
| 17839 | |
| 17840 | const FunctionProtoType *Proto = ProtoTL.getTypePtr(); |
| 17841 | FindCXXThisExpr Finder(*this); |
| 17842 | |
| 17843 | switch (Proto->getExceptionSpecType()) { |
| 17844 | case EST_Unparsed: |
| 17845 | case EST_Uninstantiated: |
| 17846 | case EST_Unevaluated: |
| 17847 | case EST_BasicNoexcept: |
| 17848 | case EST_NoThrow: |
| 17849 | case EST_DynamicNone: |
| 17850 | case EST_MSAny: |
| 17851 | case EST_None: |
| 17852 | break; |
| 17853 | |
| 17854 | case EST_DependentNoexcept: |
| 17855 | case EST_NoexceptFalse: |
| 17856 | case EST_NoexceptTrue: |
| 17857 | if (!Finder.TraverseStmt(Proto->getNoexceptExpr())) |
| 17858 | return true; |
| 17859 | LLVM_FALLTHROUGH; |
| 17860 | |
| 17861 | case EST_Dynamic: |
| 17862 | for (const auto &E : Proto->exceptions()) { |
| 17863 | if (!Finder.TraverseType(E)) |
| 17864 | return true; |
| 17865 | } |
| 17866 | break; |
| 17867 | } |
| 17868 | |
| 17869 | return false; |
| 17870 | } |
| 17871 | |
| 17872 | bool Sema::checkThisInStaticMemberFunctionAttributes(CXXMethodDecl *Method) { |
| 17873 | FindCXXThisExpr Finder(*this); |
| 17874 | |
| 17875 | |
| 17876 | for (const auto *A : Method->attrs()) { |
| 17877 | |
| 17878 | Expr *Arg = nullptr; |
| 17879 | ArrayRef<Expr *> Args; |
| 17880 | if (const auto *G = dyn_cast<GuardedByAttr>(A)) |
| 17881 | Arg = G->getArg(); |
| 17882 | else if (const auto *G = dyn_cast<PtGuardedByAttr>(A)) |
| 17883 | Arg = G->getArg(); |
| 17884 | else if (const auto *AA = dyn_cast<AcquiredAfterAttr>(A)) |
| 17885 | Args = llvm::makeArrayRef(AA->args_begin(), AA->args_size()); |
| 17886 | else if (const auto *AB = dyn_cast<AcquiredBeforeAttr>(A)) |
| 17887 | Args = llvm::makeArrayRef(AB->args_begin(), AB->args_size()); |
| 17888 | else if (const auto *ETLF = dyn_cast<ExclusiveTrylockFunctionAttr>(A)) { |
| 17889 | Arg = ETLF->getSuccessValue(); |
| 17890 | Args = llvm::makeArrayRef(ETLF->args_begin(), ETLF->args_size()); |
| 17891 | } else if (const auto *STLF = dyn_cast<SharedTrylockFunctionAttr>(A)) { |
| 17892 | Arg = STLF->getSuccessValue(); |
| 17893 | Args = llvm::makeArrayRef(STLF->args_begin(), STLF->args_size()); |
| 17894 | } else if (const auto *LR = dyn_cast<LockReturnedAttr>(A)) |
| 17895 | Arg = LR->getArg(); |
| 17896 | else if (const auto *LE = dyn_cast<LocksExcludedAttr>(A)) |
| 17897 | Args = llvm::makeArrayRef(LE->args_begin(), LE->args_size()); |
| 17898 | else if (const auto *RC = dyn_cast<RequiresCapabilityAttr>(A)) |
| 17899 | Args = llvm::makeArrayRef(RC->args_begin(), RC->args_size()); |
| 17900 | else if (const auto *AC = dyn_cast<AcquireCapabilityAttr>(A)) |
| 17901 | Args = llvm::makeArrayRef(AC->args_begin(), AC->args_size()); |
| 17902 | else if (const auto *AC = dyn_cast<TryAcquireCapabilityAttr>(A)) |
| 17903 | Args = llvm::makeArrayRef(AC->args_begin(), AC->args_size()); |
| 17904 | else if (const auto *RC = dyn_cast<ReleaseCapabilityAttr>(A)) |
| 17905 | Args = llvm::makeArrayRef(RC->args_begin(), RC->args_size()); |
| 17906 | |
| 17907 | if (Arg && !Finder.TraverseStmt(Arg)) |
| 17908 | return true; |
| 17909 | |
| 17910 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 17911 | if (!Finder.TraverseStmt(Args[I])) |
| 17912 | return true; |
| 17913 | } |
| 17914 | } |
| 17915 | |
| 17916 | return false; |
| 17917 | } |
| 17918 | |
| 17919 | void Sema::checkExceptionSpecification( |
| 17920 | bool IsTopLevel, ExceptionSpecificationType EST, |
| 17921 | ArrayRef<ParsedType> DynamicExceptions, |
| 17922 | ArrayRef<SourceRange> DynamicExceptionRanges, Expr *NoexceptExpr, |
| 17923 | SmallVectorImpl<QualType> &Exceptions, |
| 17924 | FunctionProtoType::ExceptionSpecInfo &ESI) { |
| 17925 | Exceptions.clear(); |
| 17926 | ESI.Type = EST; |
| 17927 | if (EST == EST_Dynamic) { |
| 17928 | Exceptions.reserve(DynamicExceptions.size()); |
| 17929 | for (unsigned ei = 0, ee = DynamicExceptions.size(); ei != ee; ++ei) { |
| 17930 | |
| 17931 | QualType ET = GetTypeFromParser(DynamicExceptions[ei]); |
| 17932 | |
| 17933 | if (IsTopLevel) { |
| 17934 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 17935 | collectUnexpandedParameterPacks(ET, Unexpanded); |
| 17936 | if (!Unexpanded.empty()) { |
| 17937 | DiagnoseUnexpandedParameterPacks( |
| 17938 | DynamicExceptionRanges[ei].getBegin(), UPPC_ExceptionType, |
| 17939 | Unexpanded); |
| 17940 | continue; |
| 17941 | } |
| 17942 | } |
| 17943 | |
| 17944 | |
| 17945 | |
| 17946 | if (!CheckSpecifiedExceptionType(ET, DynamicExceptionRanges[ei])) |
| 17947 | Exceptions.push_back(ET); |
| 17948 | } |
| 17949 | ESI.Exceptions = Exceptions; |
| 17950 | return; |
| 17951 | } |
| 17952 | |
| 17953 | if (isComputedNoexcept(EST)) { |
| 17954 | assert((NoexceptExpr->isTypeDependent() || |
| 17955 | NoexceptExpr->getType()->getCanonicalTypeUnqualified() == |
| 17956 | Context.BoolTy) && |
| 17957 | "Parser should have made sure that the expression is boolean"); |
| 17958 | if (IsTopLevel && DiagnoseUnexpandedParameterPack(NoexceptExpr)) { |
| 17959 | ESI.Type = EST_BasicNoexcept; |
| 17960 | return; |
| 17961 | } |
| 17962 | |
| 17963 | ESI.NoexceptExpr = NoexceptExpr; |
| 17964 | return; |
| 17965 | } |
| 17966 | } |
| 17967 | |
| 17968 | void Sema::actOnDelayedExceptionSpecification(Decl *MethodD, |
| 17969 | ExceptionSpecificationType EST, |
| 17970 | SourceRange SpecificationRange, |
| 17971 | ArrayRef<ParsedType> DynamicExceptions, |
| 17972 | ArrayRef<SourceRange> DynamicExceptionRanges, |
| 17973 | Expr *NoexceptExpr) { |
| 17974 | if (!MethodD) |
| 17975 | return; |
| 17976 | |
| 17977 | |
| 17978 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(MethodD)) |
| 17979 | MethodD = FunTmpl->getTemplatedDecl(); |
| 17980 | |
| 17981 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(MethodD); |
| 17982 | if (!Method) |
| 17983 | return; |
| 17984 | |
| 17985 | |
| 17986 | llvm::SmallVector<QualType, 4> Exceptions; |
| 17987 | FunctionProtoType::ExceptionSpecInfo ESI; |
| 17988 | checkExceptionSpecification(true, EST, DynamicExceptions, |
| 17989 | DynamicExceptionRanges, NoexceptExpr, Exceptions, |
| 17990 | ESI); |
| 17991 | |
| 17992 | |
| 17993 | Context.adjustExceptionSpec(Method, ESI, true); |
| 17994 | |
| 17995 | if (Method->isStatic()) |
| 17996 | checkThisInStaticMemberFunctionExceptionSpec(Method); |
| 17997 | |
| 17998 | if (Method->isVirtual()) { |
| 17999 | |
| 18000 | for (const CXXMethodDecl *O : Method->overridden_methods()) |
| 18001 | CheckOverridingFunctionExceptionSpec(Method, O); |
| 18002 | } |
| 18003 | } |
| 18004 | |
| 18005 | |
| 18006 | |
| 18007 | MSPropertyDecl *Sema::HandleMSProperty(Scope *S, RecordDecl *Record, |
| 18008 | SourceLocation DeclStart, Declarator &D, |
| 18009 | Expr *BitWidth, |
| 18010 | InClassInitStyle InitStyle, |
| 18011 | AccessSpecifier AS, |
| 18012 | const ParsedAttr &MSPropertyAttr) { |
| 18013 | IdentifierInfo *II = D.getIdentifier(); |
| 18014 | if (!II) { |
| 18015 | Diag(DeclStart, diag::err_anonymous_property); |
| 18016 | return nullptr; |
| 18017 | } |
| 18018 | SourceLocation Loc = D.getIdentifierLoc(); |
| 18019 | |
| 18020 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 18021 | QualType T = TInfo->getType(); |
| 18022 | if (getLangOpts().CPlusPlus) { |
| 18023 | CheckExtraCXXDefaultArguments(D); |
| 18024 | |
| 18025 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 18026 | UPPC_DataMemberType)) { |
| 18027 | D.setInvalidType(); |
| 18028 | T = Context.IntTy; |
| 18029 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
| 18030 | } |
| 18031 | } |
| 18032 | |
| 18033 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| 18034 | |
| 18035 | if (D.getDeclSpec().isInlineSpecified()) |
| 18036 | Diag(D.getDeclSpec().getInlineSpecLoc(), diag::err_inline_non_function) |
| 18037 | << getLangOpts().CPlusPlus17; |
| 18038 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 18039 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 18040 | diag::err_invalid_thread) |
| 18041 | << DeclSpec::getSpecifierName(TSCS); |
| 18042 | |
| 18043 | |
| 18044 | NamedDecl *PrevDecl = nullptr; |
| 18045 | LookupResult Previous(*this, II, Loc, LookupMemberName, |
| 18046 | ForVisibleRedeclaration); |
| 18047 | LookupName(Previous, S); |
| 18048 | switch (Previous.getResultKind()) { |
| 18049 | case LookupResult::Found: |
| 18050 | case LookupResult::FoundUnresolvedValue: |
| 18051 | PrevDecl = Previous.getAsSingle<NamedDecl>(); |
| 18052 | break; |
| 18053 | |
| 18054 | case LookupResult::FoundOverloaded: |
| 18055 | PrevDecl = Previous.getRepresentativeDecl(); |
| 18056 | break; |
| 18057 | |
| 18058 | case LookupResult::NotFound: |
| 18059 | case LookupResult::NotFoundInCurrentInstantiation: |
| 18060 | case LookupResult::Ambiguous: |
| 18061 | break; |
| 18062 | } |
| 18063 | |
| 18064 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 18065 | |
| 18066 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 18067 | |
| 18068 | PrevDecl = nullptr; |
| 18069 | } |
| 18070 | |
| 18071 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
| 18072 | PrevDecl = nullptr; |
| 18073 | |
| 18074 | SourceLocation TSSL = D.getBeginLoc(); |
| 18075 | MSPropertyDecl *NewPD = |
| 18076 | MSPropertyDecl::Create(Context, Record, Loc, II, T, TInfo, TSSL, |
| 18077 | MSPropertyAttr.getPropertyDataGetter(), |
| 18078 | MSPropertyAttr.getPropertyDataSetter()); |
| 18079 | ProcessDeclAttributes(TUScope, NewPD, D); |
| 18080 | NewPD->setAccess(AS); |
| 18081 | |
| 18082 | if (NewPD->isInvalidDecl()) |
| 18083 | Record->setInvalidDecl(); |
| 18084 | |
| 18085 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 18086 | NewPD->setModulePrivate(); |
| 18087 | |
| 18088 | if (NewPD->isInvalidDecl() && PrevDecl) { |
| 18089 | |
| 18090 | |
| 18091 | } else if (II) { |
| 18092 | PushOnScopeChains(NewPD, S); |
| 18093 | } else |
| 18094 | Record->addDecl(NewPD); |
| 18095 | |
| 18096 | return NewPD; |
| 18097 | } |
| 18098 | |
| 18099 | void Sema::ActOnStartFunctionDeclarationDeclarator( |
| 18100 | Declarator &Declarator, unsigned TemplateParameterDepth) { |
| 18101 | auto &Info = InventedParameterInfos.emplace_back(); |
| 18102 | TemplateParameterList *ExplicitParams = nullptr; |
| 18103 | ArrayRef<TemplateParameterList *> ExplicitLists = |
| 18104 | Declarator.getTemplateParameterLists(); |
| 18105 | if (!ExplicitLists.empty()) { |
| 18106 | bool IsMemberSpecialization, IsInvalid; |
| 18107 | ExplicitParams = MatchTemplateParametersToScopeSpecifier( |
| 18108 | Declarator.getBeginLoc(), Declarator.getIdentifierLoc(), |
| 18109 | Declarator.getCXXScopeSpec(), nullptr, |
| 18110 | ExplicitLists, false, IsMemberSpecialization, IsInvalid, |
| 18111 | true); |
| 18112 | } |
| 18113 | if (ExplicitParams) { |
| 18114 | Info.AutoTemplateParameterDepth = ExplicitParams->getDepth(); |
| 18115 | for (NamedDecl *Param : *ExplicitParams) |
| 18116 | Info.TemplateParams.push_back(Param); |
| 18117 | Info.NumExplicitTemplateParams = ExplicitParams->size(); |
| 18118 | } else { |
| 18119 | Info.AutoTemplateParameterDepth = TemplateParameterDepth; |
| 18120 | Info.NumExplicitTemplateParams = 0; |
| 18121 | } |
| 18122 | } |
| 18123 | |
| 18124 | void Sema::ActOnFinishFunctionDeclarationDeclarator(Declarator &Declarator) { |
| 18125 | auto &FSI = InventedParameterInfos.back(); |
| 18126 | if (FSI.TemplateParams.size() > FSI.NumExplicitTemplateParams) { |
| 18127 | if (FSI.NumExplicitTemplateParams != 0) { |
| 18128 | TemplateParameterList *ExplicitParams = |
| 18129 | Declarator.getTemplateParameterLists().back(); |
| 18130 | Declarator.setInventedTemplateParameterList( |
| 18131 | TemplateParameterList::Create( |
| 18132 | Context, ExplicitParams->getTemplateLoc(), |
| 18133 | ExplicitParams->getLAngleLoc(), FSI.TemplateParams, |
| 18134 | ExplicitParams->getRAngleLoc(), |
| 18135 | ExplicitParams->getRequiresClause())); |
| 18136 | } else { |
| 18137 | Declarator.setInventedTemplateParameterList( |
| 18138 | TemplateParameterList::Create( |
| 18139 | Context, SourceLocation(), SourceLocation(), FSI.TemplateParams, |
| 18140 | SourceLocation(), nullptr)); |
| 18141 | } |
| 18142 | } |
| 18143 | InventedParameterInfos.pop_back(); |
| 18144 | } |