| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "clang/AST/ASTContext.h" |
| 14 | #include "clang/AST/DeclObjC.h" |
| 15 | #include "clang/AST/ExprCXX.h" |
| 16 | #include "clang/AST/ExprObjC.h" |
| 17 | #include "clang/AST/ExprOpenMP.h" |
| 18 | #include "clang/AST/TypeLoc.h" |
| 19 | #include "clang/Basic/TargetInfo.h" |
| 20 | #include "clang/Sema/Designator.h" |
| 21 | #include "clang/Sema/Initialization.h" |
| 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/SemaInternal.h" |
| 24 | #include "llvm/ADT/APInt.h" |
| 25 | #include "llvm/ADT/SmallString.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | |
| 29 | using namespace clang; |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | static bool IsWideCharCompatible(QualType T, ASTContext &Context) { |
| 38 | if (Context.typesAreCompatible(Context.getWideCharType(), T)) |
| 39 | return true; |
| 40 | if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) { |
| 41 | return Context.typesAreCompatible(Context.Char16Ty, T) || |
| 42 | Context.typesAreCompatible(Context.Char32Ty, T); |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | enum StringInitFailureKind { |
| 48 | SIF_None, |
| 49 | SIF_NarrowStringIntoWideChar, |
| 50 | SIF_WideStringIntoChar, |
| 51 | SIF_IncompatWideStringIntoWideChar, |
| 52 | SIF_UTF8StringIntoPlainChar, |
| 53 | SIF_PlainStringIntoUTF8Char, |
| 54 | SIF_Other |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 62 | ASTContext &Context) { |
| 63 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 64 | return SIF_Other; |
| 65 | |
| 66 | |
| 67 | Init = Init->IgnoreParens(); |
| 68 | |
| 69 | |
| 70 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 71 | return SIF_None; |
| 72 | |
| 73 | |
| 74 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
| 75 | if (!SL) |
| 76 | return SIF_Other; |
| 77 | |
| 78 | const QualType ElemTy = |
| 79 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
| 80 | |
| 81 | switch (SL->getKind()) { |
| 82 | case StringLiteral::UTF8: |
| 83 | |
| 84 | if (ElemTy->isChar8Type()) |
| 85 | return SIF_None; |
| 86 | LLVM_FALLTHROUGH; |
| 87 | case StringLiteral::Ascii: |
| 88 | |
| 89 | |
| 90 | if (ElemTy->isCharType()) |
| 91 | return (SL->getKind() == StringLiteral::UTF8 && |
| 92 | Context.getLangOpts().Char8) |
| 93 | ? SIF_UTF8StringIntoPlainChar |
| 94 | : SIF_None; |
| 95 | if (ElemTy->isChar8Type()) |
| 96 | return SIF_PlainStringIntoUTF8Char; |
| 97 | if (IsWideCharCompatible(ElemTy, Context)) |
| 98 | return SIF_NarrowStringIntoWideChar; |
| 99 | return SIF_Other; |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | case StringLiteral::UTF16: |
| 106 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 107 | return SIF_None; |
| 108 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
| 109 | return SIF_WideStringIntoChar; |
| 110 | if (IsWideCharCompatible(ElemTy, Context)) |
| 111 | return SIF_IncompatWideStringIntoWideChar; |
| 112 | return SIF_Other; |
| 113 | case StringLiteral::UTF32: |
| 114 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 115 | return SIF_None; |
| 116 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
| 117 | return SIF_WideStringIntoChar; |
| 118 | if (IsWideCharCompatible(ElemTy, Context)) |
| 119 | return SIF_IncompatWideStringIntoWideChar; |
| 120 | return SIF_Other; |
| 121 | case StringLiteral::Wide: |
| 122 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 123 | return SIF_None; |
| 124 | if (ElemTy->isCharType() || ElemTy->isChar8Type()) |
| 125 | return SIF_WideStringIntoChar; |
| 126 | if (IsWideCharCompatible(ElemTy, Context)) |
| 127 | return SIF_IncompatWideStringIntoWideChar; |
| 128 | return SIF_Other; |
| 129 | } |
| 130 | |
| 131 | llvm_unreachable("missed a StringLiteral kind?"); |
| 132 | } |
| 133 | |
| 134 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 135 | ASTContext &Context) { |
| 136 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 137 | if (!arrayType) |
| 138 | return SIF_Other; |
| 139 | return IsStringInit(init, arrayType, Context); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | |
| 144 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
| 145 | while (true) { |
| 146 | E->setType(Ty); |
| 147 | E->setValueKind(VK_RValue); |
| 148 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) { |
| 149 | break; |
| 150 | } else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 151 | E = PE->getSubExpr(); |
| 152 | } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 153 | getOpcode() == UO_Extension", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 153, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(UO->getOpcode() == UO_Extension); |
| 154 | E = UO->getSubExpr(); |
| 155 | } else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) { |
| 156 | E = GSE->getResultExpr(); |
| 157 | } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(E)) { |
| 158 | E = CE->getChosenSubExpr(); |
| 159 | } else { |
| 160 | llvm_unreachable("unexpected expr in string literal init"); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | static void updateGNUCompoundLiteralRValue(Expr *E) { |
| 168 | while (true) { |
| 169 | E->setValueKind(VK_RValue); |
| 170 | if (isa<CompoundLiteralExpr>(E)) { |
| 171 | break; |
| 172 | } else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 173 | E = PE->getSubExpr(); |
| 174 | } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 175 | getOpcode() == UO_Extension", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 175, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(UO->getOpcode() == UO_Extension); |
| 176 | E = UO->getSubExpr(); |
| 177 | } else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) { |
| 178 | E = GSE->getResultExpr(); |
| 179 | } else if (ChooseExpr *CE = dyn_cast<ChooseExpr>(E)) { |
| 180 | E = CE->getChosenSubExpr(); |
| 181 | } else { |
| 182 | llvm_unreachable("unexpected expr in array compound literal init"); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 188 | Sema &S) { |
| 189 | |
| 190 | auto *ConstantArrayTy = |
| 191 | cast<ConstantArrayType>(Str->getType()->getAsArrayTypeUnsafe()); |
| 192 | uint64_t StrLength = ConstantArrayTy->getSize().getZExtValue(); |
| 193 | |
| 194 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
| 195 | |
| 196 | |
| 197 | llvm::APInt ConstVal(32, StrLength); |
| 198 | |
| 199 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 200 | ConstVal, |
| 201 | ArrayType::Normal, 0); |
| 202 | updateStringLiteralType(Str, DeclT); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | if (S.getLangOpts().CPlusPlus) { |
| 212 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | if (SL->isPascal()) |
| 218 | StrLength--; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | if (StrLength > CAT->getSize().getZExtValue()) |
| 223 | S.Diag(Str->getBeginLoc(), |
| 224 | diag::err_initializer_string_for_char_array_too_long) |
| 225 | << Str->getSourceRange(); |
| 226 | } else { |
| 227 | |
| 228 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 229 | S.Diag(Str->getBeginLoc(), |
| 230 | diag::ext_initializer_string_for_char_array_too_long) |
| 231 | << Str->getSourceRange(); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | updateStringLiteralType(Str, DeclT); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | namespace { |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | class InitListChecker { |
| 275 | Sema &SemaRef; |
| 276 | bool hadError; |
| 277 | bool VerifyOnly; |
| 278 | bool TreatUnavailableAsInvalid; |
| 279 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 280 | InitListExpr *FullyStructuredList; |
| 281 | |
| 282 | void CheckImplicitInitList(const InitializedEntity &Entity, |
| 283 | InitListExpr *ParentIList, QualType T, |
| 284 | unsigned &Index, InitListExpr *StructuredList, |
| 285 | unsigned &StructuredIndex); |
| 286 | void CheckExplicitInitList(const InitializedEntity &Entity, |
| 287 | InitListExpr *IList, QualType &T, |
| 288 | InitListExpr *StructuredList, |
| 289 | bool TopLevelObject = false); |
| 290 | void CheckListElementTypes(const InitializedEntity &Entity, |
| 291 | InitListExpr *IList, QualType &DeclType, |
| 292 | bool SubobjectIsDesignatorContext, |
| 293 | unsigned &Index, |
| 294 | InitListExpr *StructuredList, |
| 295 | unsigned &StructuredIndex, |
| 296 | bool TopLevelObject = false); |
| 297 | void CheckSubElementType(const InitializedEntity &Entity, |
| 298 | InitListExpr *IList, QualType ElemType, |
| 299 | unsigned &Index, |
| 300 | InitListExpr *StructuredList, |
| 301 | unsigned &StructuredIndex); |
| 302 | void CheckComplexType(const InitializedEntity &Entity, |
| 303 | InitListExpr *IList, QualType DeclType, |
| 304 | unsigned &Index, |
| 305 | InitListExpr *StructuredList, |
| 306 | unsigned &StructuredIndex); |
| 307 | void CheckScalarType(const InitializedEntity &Entity, |
| 308 | InitListExpr *IList, QualType DeclType, |
| 309 | unsigned &Index, |
| 310 | InitListExpr *StructuredList, |
| 311 | unsigned &StructuredIndex); |
| 312 | void CheckReferenceType(const InitializedEntity &Entity, |
| 313 | InitListExpr *IList, QualType DeclType, |
| 314 | unsigned &Index, |
| 315 | InitListExpr *StructuredList, |
| 316 | unsigned &StructuredIndex); |
| 317 | void CheckVectorType(const InitializedEntity &Entity, |
| 318 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
| 319 | InitListExpr *StructuredList, |
| 320 | unsigned &StructuredIndex); |
| 321 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
| 322 | InitListExpr *IList, QualType DeclType, |
| 323 | CXXRecordDecl::base_class_range Bases, |
| 324 | RecordDecl::field_iterator Field, |
| 325 | bool SubobjectIsDesignatorContext, unsigned &Index, |
| 326 | InitListExpr *StructuredList, |
| 327 | unsigned &StructuredIndex, |
| 328 | bool TopLevelObject = false); |
| 329 | void CheckArrayType(const InitializedEntity &Entity, |
| 330 | InitListExpr *IList, QualType &DeclType, |
| 331 | llvm::APSInt elementIndex, |
| 332 | bool SubobjectIsDesignatorContext, unsigned &Index, |
| 333 | InitListExpr *StructuredList, |
| 334 | unsigned &StructuredIndex); |
| 335 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
| 336 | InitListExpr *IList, DesignatedInitExpr *DIE, |
| 337 | unsigned DesigIdx, |
| 338 | QualType &CurrentObjectType, |
| 339 | RecordDecl::field_iterator *NextField, |
| 340 | llvm::APSInt *NextElementIndex, |
| 341 | unsigned &Index, |
| 342 | InitListExpr *StructuredList, |
| 343 | unsigned &StructuredIndex, |
| 344 | bool FinishSubobjectInit, |
| 345 | bool TopLevelObject); |
| 346 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 347 | QualType CurrentObjectType, |
| 348 | InitListExpr *StructuredList, |
| 349 | unsigned StructuredIndex, |
| 350 | SourceRange InitRange, |
| 351 | bool IsFullyOverwritten = false); |
| 352 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 353 | unsigned &StructuredIndex, |
| 354 | Expr *expr); |
| 355 | int numArrayElements(QualType DeclType); |
| 356 | int numStructUnionElements(QualType DeclType); |
| 357 | |
| 358 | static ExprResult PerformEmptyInit(Sema &SemaRef, |
| 359 | SourceLocation Loc, |
| 360 | const InitializedEntity &Entity, |
| 361 | bool VerifyOnly, |
| 362 | bool TreatUnavailableAsInvalid); |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | void FillInEmptyInitForBase(unsigned Init, const CXXBaseSpecifier &Base, |
| 384 | const InitializedEntity &ParentEntity, |
| 385 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 386 | bool FillWithNoInit); |
| 387 | void FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
| 388 | const InitializedEntity &ParentEntity, |
| 389 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 390 | bool FillWithNoInit = false); |
| 391 | void FillInEmptyInitializations(const InitializedEntity &Entity, |
| 392 | InitListExpr *ILE, bool &RequiresSecondPass, |
| 393 | InitListExpr *OuterILE, unsigned OuterIndex, |
| 394 | bool FillWithNoInit = false); |
| 395 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 396 | Expr *InitExpr, FieldDecl *Field, |
| 397 | bool TopLevelObject); |
| 398 | void CheckEmptyInitializable(const InitializedEntity &Entity, |
| 399 | SourceLocation Loc); |
| 400 | |
| 401 | public: |
| 402 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 403 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 404 | bool TreatUnavailableAsInvalid); |
| 405 | bool HadError() { return hadError; } |
| 406 | |
| 407 | |
| 408 | |
| 409 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 410 | }; |
| 411 | |
| 412 | } |
| 413 | |
| 414 | ExprResult InitListChecker::PerformEmptyInit(Sema &SemaRef, |
| 415 | SourceLocation Loc, |
| 416 | const InitializedEntity &Entity, |
| 417 | bool VerifyOnly, |
| 418 | bool TreatUnavailableAsInvalid) { |
| 419 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 420 | true); |
| 421 | MultiExprArg SubInit; |
| 422 | Expr *InitExpr; |
| 423 | InitListExpr DummyInitList(SemaRef.Context, Loc, None, Loc); |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | bool EmptyInitList = SemaRef.getLangOpts().CPlusPlus11 && |
| 430 | Entity.getType()->getBaseElementTypeUnsafe()->isRecordType(); |
| 431 | if (EmptyInitList) { |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | InitExpr = VerifyOnly ? &DummyInitList : new (SemaRef.Context) |
| 443 | InitListExpr(SemaRef.Context, Loc, None, Loc); |
| 444 | InitExpr->setType(SemaRef.Context.VoidTy); |
| 445 | SubInit = InitExpr; |
| 446 | Kind = InitializationKind::CreateCopy(Loc, Loc); |
| 447 | } else { |
| 448 | |
| 449 | |
| 450 | } |
| 451 | |
| 452 | InitializationSequence InitSeq(SemaRef, Entity, Kind, SubInit); |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | if (!InitSeq && EmptyInitList && InitSeq.getFailureKind() == |
| 459 | InitializationSequence::FK_ExplicitConstructor) { |
| 460 | OverloadCandidateSet::iterator Best; |
| 461 | OverloadingResult O = |
| 462 | InitSeq.getFailedCandidateSet() |
| 463 | .BestViableFunction(SemaRef, Kind.getLocation(), Best); |
| 464 | (void)O; |
| 465 | (0) . __assert_fail ("O == OR_Success && \"Inconsistent overload resolution\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 465, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(O == OR_Success && "Inconsistent overload resolution"); |
| 466 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 467 | CXXRecordDecl *R = CtorDecl->getParent(); |
| 468 | |
| 469 | if (CtorDecl->getMinRequiredArguments() == 0 && |
| 470 | CtorDecl->isExplicit() && R->getDeclName() && |
| 471 | SemaRef.SourceMgr.isInSystemHeader(CtorDecl->getLocation())) { |
| 472 | bool IsInStd = false; |
| 473 | for (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(R->getDeclContext()); |
| 474 | ND && !IsInStd; ND = dyn_cast<NamespaceDecl>(ND->getParent())) { |
| 475 | if (SemaRef.getStdNamespace()->InEnclosingNamespaceSetOf(ND)) |
| 476 | IsInStd = true; |
| 477 | } |
| 478 | |
| 479 | if (IsInStd && llvm::StringSwitch<bool>(R->getName()) |
| 480 | .Cases("basic_string", "deque", "forward_list", true) |
| 481 | .Cases("list", "map", "multimap", "multiset", true) |
| 482 | .Cases("priority_queue", "queue", "set", "stack", true) |
| 483 | .Cases("unordered_map", "unordered_set", "vector", true) |
| 484 | .Default(false)) { |
| 485 | InitSeq.InitializeFrom( |
| 486 | SemaRef, Entity, |
| 487 | InitializationKind::CreateValue(Loc, Loc, Loc, true), |
| 488 | MultiExprArg(), , |
| 489 | TreatUnavailableAsInvalid); |
| 490 | |
| 491 | |
| 492 | if (!VerifyOnly) { |
| 493 | SemaRef.Diag(CtorDecl->getLocation(), |
| 494 | diag::warn_invalid_initializer_from_system_header); |
| 495 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 496 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 497 | diag::note_used_in_initialization_here); |
| 498 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) |
| 499 | SemaRef.Diag(Loc, diag::note_used_in_initialization_here); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | if (!InitSeq) { |
| 505 | if (!VerifyOnly) { |
| 506 | InitSeq.Diagnose(SemaRef, Entity, Kind, SubInit); |
| 507 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 508 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 509 | diag::note_in_omitted_aggregate_initializer) |
| 510 | << << Entity.getDecl(); |
| 511 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 512 | bool IsTrailingArrayNewMember = |
| 513 | Entity.getParent() && |
| 514 | Entity.getParent()->isVariableLengthArrayNew(); |
| 515 | SemaRef.Diag(Loc, diag::note_in_omitted_aggregate_initializer) |
| 516 | << (IsTrailingArrayNewMember ? 2 : ) |
| 517 | << Entity.getElementIndex(); |
| 518 | } |
| 519 | } |
| 520 | return ExprError(); |
| 521 | } |
| 522 | |
| 523 | return VerifyOnly ? ExprResult(static_cast<Expr *>(nullptr)) |
| 524 | : InitSeq.Perform(SemaRef, Entity, Kind, SubInit); |
| 525 | } |
| 526 | |
| 527 | void InitListChecker::CheckEmptyInitializable(const InitializedEntity &Entity, |
| 528 | SourceLocation Loc) { |
| 529 | (0) . __assert_fail ("VerifyOnly && \"CheckEmptyInitializable is only inteded for verification mode.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 530, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VerifyOnly && |
| 530 | (0) . __assert_fail ("VerifyOnly && \"CheckEmptyInitializable is only inteded for verification mode.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 530, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "CheckEmptyInitializable is only inteded for verification mode."); |
| 531 | if (PerformEmptyInit(SemaRef, Loc, Entity, , |
| 532 | TreatUnavailableAsInvalid).isInvalid()) |
| 533 | hadError = true; |
| 534 | } |
| 535 | |
| 536 | void InitListChecker::FillInEmptyInitForBase( |
| 537 | unsigned Init, const CXXBaseSpecifier &Base, |
| 538 | const InitializedEntity &ParentEntity, InitListExpr *ILE, |
| 539 | bool &RequiresSecondPass, bool FillWithNoInit) { |
| 540 | (0) . __assert_fail ("Init < ILE->getNumInits() && \"should have been expanded\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 540, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Init < ILE->getNumInits() && "should have been expanded"); |
| 541 | |
| 542 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 543 | SemaRef.Context, &Base, false, &ParentEntity); |
| 544 | |
| 545 | if (!ILE->getInit(Init)) { |
| 546 | ExprResult BaseInit = |
| 547 | FillWithNoInit |
| 548 | ? new (SemaRef.Context) NoInitExpr(Base.getType()) |
| 549 | : PerformEmptyInit(SemaRef, ILE->getEndLoc(), BaseEntity, |
| 550 | false, TreatUnavailableAsInvalid); |
| 551 | if (BaseInit.isInvalid()) { |
| 552 | hadError = true; |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | ILE->setInit(Init, BaseInit.getAs<Expr>()); |
| 557 | } else if (InitListExpr *InnerILE = |
| 558 | dyn_cast<InitListExpr>(ILE->getInit(Init))) { |
| 559 | FillInEmptyInitializations(BaseEntity, InnerILE, RequiresSecondPass, |
| 560 | ILE, Init, FillWithNoInit); |
| 561 | } else if (DesignatedInitUpdateExpr *InnerDIUE = |
| 562 | dyn_cast<DesignatedInitUpdateExpr>(ILE->getInit(Init))) { |
| 563 | FillInEmptyInitializations(BaseEntity, InnerDIUE->getUpdater(), |
| 564 | RequiresSecondPass, ILE, Init, |
| 565 | ); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
| 570 | const InitializedEntity &ParentEntity, |
| 571 | InitListExpr *ILE, |
| 572 | bool &RequiresSecondPass, |
| 573 | bool FillWithNoInit) { |
| 574 | SourceLocation Loc = ILE->getEndLoc(); |
| 575 | unsigned NumInits = ILE->getNumInits(); |
| 576 | InitializedEntity MemberEntity |
| 577 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 578 | |
| 579 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) |
| 580 | if (!RType->getDecl()->isUnion()) |
| 581 | (0) . __assert_fail ("Init < NumInits && \"This ILE should have been expanded\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 581, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Init < NumInits && "This ILE should have been expanded"); |
| 582 | |
| 583 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 584 | if (FillWithNoInit) { |
| 585 | Expr *Filler = new (SemaRef.Context) NoInitExpr(Field->getType()); |
| 586 | if (Init < NumInits) |
| 587 | ILE->setInit(Init, Filler); |
| 588 | else |
| 589 | ILE->updateInit(SemaRef.Context, Init, Filler); |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | if (Field->hasInClassInitializer()) { |
| 597 | ExprResult DIE = SemaRef.BuildCXXDefaultInitExpr(Loc, Field); |
| 598 | if (DIE.isInvalid()) { |
| 599 | hadError = true; |
| 600 | return; |
| 601 | } |
| 602 | SemaRef.checkInitializerLifetime(MemberEntity, DIE.get()); |
| 603 | if (Init < NumInits) |
| 604 | ILE->setInit(Init, DIE.get()); |
| 605 | else { |
| 606 | ILE->updateInit(SemaRef.Context, Init, DIE.get()); |
| 607 | RequiresSecondPass = true; |
| 608 | } |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | if (Field->getType()->isReferenceType()) { |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 618 | << Field->getType() |
| 619 | << ILE->getSyntacticForm()->getSourceRange(); |
| 620 | SemaRef.Diag(Field->getLocation(), |
| 621 | diag::note_uninit_reference_member); |
| 622 | hadError = true; |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | ExprResult MemberInit = PerformEmptyInit(SemaRef, Loc, MemberEntity, |
| 627 | , |
| 628 | TreatUnavailableAsInvalid); |
| 629 | if (MemberInit.isInvalid()) { |
| 630 | hadError = true; |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | if (hadError) { |
| 635 | |
| 636 | } else if (Init < NumInits) { |
| 637 | ILE->setInit(Init, MemberInit.getAs<Expr>()); |
| 638 | } else if (!isa<ImplicitValueInitExpr>(MemberInit.get())) { |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | ILE->updateInit(SemaRef.Context, Init, MemberInit.getAs<Expr>()); |
| 644 | RequiresSecondPass = true; |
| 645 | } |
| 646 | } else if (InitListExpr *InnerILE |
| 647 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 648 | FillInEmptyInitializations(MemberEntity, InnerILE, |
| 649 | RequiresSecondPass, ILE, Init, FillWithNoInit); |
| 650 | else if (DesignatedInitUpdateExpr *InnerDIUE |
| 651 | = dyn_cast<DesignatedInitUpdateExpr>(ILE->getInit(Init))) |
| 652 | FillInEmptyInitializations(MemberEntity, InnerDIUE->getUpdater(), |
| 653 | RequiresSecondPass, ILE, Init, |
| 654 | ); |
| 655 | } |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | void |
| 661 | InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity, |
| 662 | InitListExpr *ILE, |
| 663 | bool &RequiresSecondPass, |
| 664 | InitListExpr *OuterILE, |
| 665 | unsigned OuterIndex, |
| 666 | bool FillWithNoInit) { |
| 667 | (0) . __assert_fail ("(ILE->getType() != SemaRef.Context.VoidTy) && \"Should not have void type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 668, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((ILE->getType() != SemaRef.Context.VoidTy) && |
| 668 | (0) . __assert_fail ("(ILE->getType() != SemaRef.Context.VoidTy) && \"Should not have void type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 668, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Should not have void type"); |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | struct UpdateOuterILEWithUpdatedInit { |
| 678 | InitListExpr *Outer; |
| 679 | unsigned OuterIndex; |
| 680 | ~UpdateOuterILEWithUpdatedInit() { |
| 681 | if (Outer) |
| 682 | Outer->setInit(OuterIndex, Outer->getInit(OuterIndex)); |
| 683 | } |
| 684 | } UpdateOuterRAII = {OuterILE, OuterIndex}; |
| 685 | |
| 686 | |
| 687 | |
| 688 | if (ILE->isTransparent()) |
| 689 | return; |
| 690 | |
| 691 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
| 692 | const RecordDecl *RDecl = RType->getDecl(); |
| 693 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
| 694 | FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(), |
| 695 | Entity, ILE, RequiresSecondPass, FillWithNoInit); |
| 696 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 697 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
| 698 | for (auto *Field : RDecl->fields()) { |
| 699 | if (Field->hasInClassInitializer()) { |
| 700 | FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass, |
| 701 | FillWithNoInit); |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | } else { |
| 706 | |
| 707 | |
| 708 | |
| 709 | unsigned NumElems = numStructUnionElements(ILE->getType()); |
| 710 | if (RDecl->hasFlexibleArrayMember()) |
| 711 | ++NumElems; |
| 712 | if (ILE->getNumInits() < NumElems) |
| 713 | ILE->resizeInits(SemaRef.Context, NumElems); |
| 714 | |
| 715 | unsigned Init = 0; |
| 716 | |
| 717 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RDecl)) { |
| 718 | for (auto &Base : CXXRD->bases()) { |
| 719 | if (hadError) |
| 720 | return; |
| 721 | |
| 722 | FillInEmptyInitForBase(Init, Base, Entity, ILE, RequiresSecondPass, |
| 723 | FillWithNoInit); |
| 724 | ++Init; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | for (auto *Field : RDecl->fields()) { |
| 729 | if (Field->isUnnamedBitfield()) |
| 730 | continue; |
| 731 | |
| 732 | if (hadError) |
| 733 | return; |
| 734 | |
| 735 | FillInEmptyInitForField(Init, Field, Entity, ILE, RequiresSecondPass, |
| 736 | FillWithNoInit); |
| 737 | if (hadError) |
| 738 | return; |
| 739 | |
| 740 | ++Init; |
| 741 | |
| 742 | |
| 743 | if (RDecl->isUnion()) |
| 744 | break; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | QualType ElementType; |
| 752 | |
| 753 | InitializedEntity ElementEntity = Entity; |
| 754 | unsigned NumInits = ILE->getNumInits(); |
| 755 | unsigned NumElements = NumInits; |
| 756 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
| 757 | ElementType = AType->getElementType(); |
| 758 | if (const auto *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 759 | NumElements = CAType->getSize().getZExtValue(); |
| 760 | |
| 761 | |
| 762 | if (Entity.isVariableLengthArrayNew()) |
| 763 | ++NumElements; |
| 764 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
| 765 | 0, Entity); |
| 766 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
| 767 | ElementType = VType->getElementType(); |
| 768 | NumElements = VType->getNumElements(); |
| 769 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
| 770 | 0, Entity); |
| 771 | } else |
| 772 | ElementType = ILE->getType(); |
| 773 | |
| 774 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
| 775 | if (hadError) |
| 776 | return; |
| 777 | |
| 778 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 779 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
| 780 | ElementEntity.setElementIndex(Init); |
| 781 | |
| 782 | if (Init >= NumInits && ILE->hasArrayFiller()) |
| 783 | return; |
| 784 | |
| 785 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr); |
| 786 | if (!InitExpr && Init < NumInits && ILE->hasArrayFiller()) |
| 787 | ILE->setInit(Init, ILE->getArrayFiller()); |
| 788 | else if (!InitExpr && !ILE->hasArrayFiller()) { |
| 789 | Expr *Filler = nullptr; |
| 790 | |
| 791 | if (FillWithNoInit) |
| 792 | Filler = new (SemaRef.Context) NoInitExpr(ElementType); |
| 793 | else { |
| 794 | ExprResult ElementInit = |
| 795 | PerformEmptyInit(SemaRef, ILE->getEndLoc(), ElementEntity, |
| 796 | false, TreatUnavailableAsInvalid); |
| 797 | if (ElementInit.isInvalid()) { |
| 798 | hadError = true; |
| 799 | return; |
| 800 | } |
| 801 | |
| 802 | Filler = ElementInit.getAs<Expr>(); |
| 803 | } |
| 804 | |
| 805 | if (hadError) { |
| 806 | |
| 807 | } else if (Init < NumInits) { |
| 808 | |
| 809 | |
| 810 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 811 | ILE->setArrayFiller(Filler); |
| 812 | else |
| 813 | ILE->setInit(Init, Filler); |
| 814 | } else { |
| 815 | |
| 816 | |
| 817 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 818 | ILE->setArrayFiller(Filler); |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | if (!isa<ImplicitValueInitExpr>(Filler) && !isa<NoInitExpr>(Filler)) { |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | ILE->updateInit(SemaRef.Context, Init, Filler); |
| 828 | RequiresSecondPass = true; |
| 829 | } |
| 830 | } |
| 831 | } else if (InitListExpr *InnerILE |
| 832 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
| 833 | FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass, |
| 834 | ILE, Init, FillWithNoInit); |
| 835 | else if (DesignatedInitUpdateExpr *InnerDIUE |
| 836 | = dyn_cast_or_null<DesignatedInitUpdateExpr>(InitExpr)) |
| 837 | FillInEmptyInitializations(ElementEntity, InnerDIUE->getUpdater(), |
| 838 | RequiresSecondPass, ILE, Init, |
| 839 | ); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 844 | InitListExpr *IL, QualType &T, |
| 845 | bool VerifyOnly, |
| 846 | bool TreatUnavailableAsInvalid) |
| 847 | : SemaRef(S), VerifyOnly(VerifyOnly), |
| 848 | TreatUnavailableAsInvalid(TreatUnavailableAsInvalid) { |
| 849 | |
| 850 | |
| 851 | |
| 852 | hadError = false; |
| 853 | |
| 854 | FullyStructuredList = |
| 855 | getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange()); |
| 856 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
| 857 | ); |
| 858 | |
| 859 | if (!hadError && !VerifyOnly) { |
| 860 | bool RequiresSecondPass = false; |
| 861 | FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass, |
| 862 | , ); |
| 863 | if (RequiresSecondPass && !hadError) |
| 864 | FillInEmptyInitializations(Entity, FullyStructuredList, |
| 865 | RequiresSecondPass, nullptr, 0); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | int InitListChecker::numArrayElements(QualType DeclType) { |
| 870 | |
| 871 | int maxElements = 0x7FFFFFFF; |
| 872 | if (const ConstantArrayType *CAT = |
| 873 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
| 874 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 875 | } |
| 876 | return maxElements; |
| 877 | } |
| 878 | |
| 879 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
| 880 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
| 881 | int InitializableMembers = 0; |
| 882 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(structDecl)) |
| 883 | InitializableMembers += CXXRD->getNumBases(); |
| 884 | for (const auto *Field : structDecl->fields()) |
| 885 | if (!Field->isUnnamedBitfield()) |
| 886 | ++InitializableMembers; |
| 887 | |
| 888 | if (structDecl->isUnion()) |
| 889 | return std::min(InitializableMembers, 1); |
| 890 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
| 891 | } |
| 892 | |
| 893 | |
| 894 | |
| 895 | static bool isIdiomaticBraceElisionEntity(const InitializedEntity &Entity) { |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | if (Entity.getKind() != InitializedEntity::EK_Member || !Entity.getParent()) |
| 907 | return false; |
| 908 | |
| 909 | auto *ParentRD = |
| 910 | Entity.getParent()->getType()->castAs<RecordType>()->getDecl(); |
| 911 | if (CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ParentRD)) |
| 912 | if (CXXRD->getNumBases()) |
| 913 | return false; |
| 914 | |
| 915 | auto FieldIt = ParentRD->field_begin(); |
| 916 | (0) . __assert_fail ("FieldIt != ParentRD->field_end() && \"no fields but have initializer for member?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 917, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FieldIt != ParentRD->field_end() && |
| 917 | (0) . __assert_fail ("FieldIt != ParentRD->field_end() && \"no fields but have initializer for member?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 917, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "no fields but have initializer for member?"); |
| 918 | return ++FieldIt == ParentRD->field_end(); |
| 919 | } |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
| 928 | InitListExpr *ParentIList, |
| 929 | QualType T, unsigned &Index, |
| 930 | InitListExpr *StructuredList, |
| 931 | unsigned &StructuredIndex) { |
| 932 | int maxElements = 0; |
| 933 | |
| 934 | if (T->isArrayType()) |
| 935 | maxElements = numArrayElements(T); |
| 936 | else if (T->isRecordType()) |
| 937 | maxElements = numStructUnionElements(T); |
| 938 | else if (T->isVectorType()) |
| 939 | maxElements = T->getAs<VectorType>()->getNumElements(); |
| 940 | else |
| 941 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
| 942 | |
| 943 | if (maxElements == 0) { |
| 944 | if (!VerifyOnly) |
| 945 | SemaRef.Diag(ParentIList->getInit(Index)->getBeginLoc(), |
| 946 | diag::err_implicit_empty_initializer); |
| 947 | ++Index; |
| 948 | hadError = true; |
| 949 | return; |
| 950 | } |
| 951 | |
| 952 | |
| 953 | InitListExpr *StructuredSubobjectInitList = getStructuredSubobjectInit( |
| 954 | ParentIList, Index, T, StructuredList, StructuredIndex, |
| 955 | SourceRange(ParentIList->getInit(Index)->getBeginLoc(), |
| 956 | ParentIList->getSourceRange().getEnd())); |
| 957 | unsigned StructuredSubobjectInitIndex = 0; |
| 958 | |
| 959 | |
| 960 | unsigned StartIndex = Index; |
| 961 | CheckListElementTypes(Entity, ParentIList, T, |
| 962 | , Index, |
| 963 | StructuredSubobjectInitList, |
| 964 | StructuredSubobjectInitIndex); |
| 965 | |
| 966 | if (!VerifyOnly) { |
| 967 | StructuredSubobjectInitList->setType(T); |
| 968 | |
| 969 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
| 970 | |
| 971 | |
| 972 | if (EndIndex < ParentIList->getNumInits() && |
| 973 | ParentIList->getInit(EndIndex)) { |
| 974 | SourceLocation EndLoc |
| 975 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 976 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 977 | } |
| 978 | |
| 979 | |
| 980 | if ((T->isArrayType() || T->isRecordType()) && |
| 981 | !ParentIList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()) && |
| 982 | !isIdiomaticBraceElisionEntity(Entity)) { |
| 983 | SemaRef.Diag(StructuredSubobjectInitList->getBeginLoc(), |
| 984 | diag::warn_missing_braces) |
| 985 | << StructuredSubobjectInitList->getSourceRange() |
| 986 | << FixItHint::CreateInsertion( |
| 987 | StructuredSubobjectInitList->getBeginLoc(), "{") |
| 988 | << FixItHint::CreateInsertion( |
| 989 | SemaRef.getLocForEndOfToken( |
| 990 | StructuredSubobjectInitList->getEndLoc()), |
| 991 | "}"); |
| 992 | } |
| 993 | |
| 994 | |
| 995 | auto *CXXRD = T->getAsCXXRecordDecl(); |
| 996 | if (CXXRD && CXXRD->hasUserDeclaredConstructor()) { |
| 997 | SemaRef.Diag(StructuredSubobjectInitList->getBeginLoc(), |
| 998 | diag::warn_cxx2a_compat_aggregate_init_with_ctors) |
| 999 | << StructuredSubobjectInitList->getSourceRange() << T; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | static void warnBracedScalarInit(Sema &S, const InitializedEntity &Entity, |
| 1007 | SourceRange Braces) { |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | if (S.inTemplateInstantiation()) |
| 1012 | return; |
| 1013 | |
| 1014 | unsigned DiagID = 0; |
| 1015 | |
| 1016 | switch (Entity.getKind()) { |
| 1017 | case InitializedEntity::EK_VectorElement: |
| 1018 | case InitializedEntity::EK_ComplexElement: |
| 1019 | case InitializedEntity::EK_ArrayElement: |
| 1020 | case InitializedEntity::EK_Parameter: |
| 1021 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 1022 | case InitializedEntity::EK_Result: |
| 1023 | |
| 1024 | DiagID = diag::warn_braces_around_scalar_init; |
| 1025 | break; |
| 1026 | |
| 1027 | case InitializedEntity::EK_Member: |
| 1028 | |
| 1029 | |
| 1030 | if (Entity.getParent()) |
| 1031 | DiagID = diag::warn_braces_around_scalar_init; |
| 1032 | break; |
| 1033 | |
| 1034 | case InitializedEntity::EK_Variable: |
| 1035 | case InitializedEntity::EK_LambdaCapture: |
| 1036 | |
| 1037 | |
| 1038 | break; |
| 1039 | |
| 1040 | case InitializedEntity::EK_New: |
| 1041 | case InitializedEntity::EK_Temporary: |
| 1042 | case InitializedEntity::EK_CompoundLiteralInit: |
| 1043 | |
| 1044 | break; |
| 1045 | |
| 1046 | case InitializedEntity::EK_RelatedResult: |
| 1047 | |
| 1048 | break; |
| 1049 | |
| 1050 | case InitializedEntity::EK_Exception: |
| 1051 | case InitializedEntity::EK_Base: |
| 1052 | case InitializedEntity::EK_Delegating: |
| 1053 | case InitializedEntity::EK_BlockElement: |
| 1054 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
| 1055 | case InitializedEntity::EK_Binding: |
| 1056 | case InitializedEntity::EK_StmtExprResult: |
| 1057 | llvm_unreachable("unexpected braced scalar init"); |
| 1058 | } |
| 1059 | |
| 1060 | if (DiagID) { |
| 1061 | S.Diag(Braces.getBegin(), DiagID) |
| 1062 | << Braces |
| 1063 | << FixItHint::CreateRemoval(Braces.getBegin()) |
| 1064 | << FixItHint::CreateRemoval(Braces.getEnd()); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
| 1074 | InitListExpr *IList, QualType &T, |
| 1075 | InitListExpr *StructuredList, |
| 1076 | bool TopLevelObject) { |
| 1077 | if (!VerifyOnly) { |
| 1078 | SyntacticToSemantic[IList] = StructuredList; |
| 1079 | StructuredList->setSyntacticForm(IList); |
| 1080 | } |
| 1081 | |
| 1082 | unsigned Index = 0, StructuredIndex = 0; |
| 1083 | CheckListElementTypes(Entity, IList, T, , |
| 1084 | Index, StructuredList, StructuredIndex, TopLevelObject); |
| 1085 | if (!VerifyOnly) { |
| 1086 | QualType ExprTy = T; |
| 1087 | if (!ExprTy->isArrayType()) |
| 1088 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
| 1089 | IList->setType(ExprTy); |
| 1090 | StructuredList->setType(ExprTy); |
| 1091 | } |
| 1092 | if (hadError) |
| 1093 | return; |
| 1094 | |
| 1095 | if (Index < IList->getNumInits()) { |
| 1096 | |
| 1097 | if (VerifyOnly) { |
| 1098 | if (SemaRef.getLangOpts().CPlusPlus || |
| 1099 | (SemaRef.getLangOpts().OpenCL && |
| 1100 | IList->getType()->isVectorType())) { |
| 1101 | hadError = true; |
| 1102 | } |
| 1103 | return; |
| 1104 | } |
| 1105 | |
| 1106 | if (StructuredIndex == 1 && |
| 1107 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 1108 | SIF_None) { |
| 1109 | unsigned DK = diag::ext_excess_initializers_in_char_array_initializer; |
| 1110 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 1111 | DK = diag::err_excess_initializers_in_char_array_initializer; |
| 1112 | hadError = true; |
| 1113 | } |
| 1114 | |
| 1115 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1116 | << IList->getInit(Index)->getSourceRange(); |
| 1117 | } else if (!T->isIncompleteType()) { |
| 1118 | |
| 1119 | |
| 1120 | QualType CurrentObjectType = StructuredList->getType(); |
| 1121 | int initKind = |
| 1122 | CurrentObjectType->isArrayType()? 0 : |
| 1123 | CurrentObjectType->isVectorType()? 1 : |
| 1124 | CurrentObjectType->isScalarType()? 2 : |
| 1125 | CurrentObjectType->isUnionType()? 3 : |
| 1126 | 4; |
| 1127 | |
| 1128 | unsigned DK = diag::ext_excess_initializers; |
| 1129 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 1130 | DK = diag::err_excess_initializers; |
| 1131 | hadError = true; |
| 1132 | } |
| 1133 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
| 1134 | DK = diag::err_excess_initializers; |
| 1135 | hadError = true; |
| 1136 | } |
| 1137 | |
| 1138 | SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK) |
| 1139 | << initKind << IList->getInit(Index)->getSourceRange(); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | if (!VerifyOnly) { |
| 1144 | if (T->isScalarType() && IList->getNumInits() == 1 && |
| 1145 | !isa<InitListExpr>(IList->getInit(0))) |
| 1146 | warnBracedScalarInit(SemaRef, Entity, IList->getSourceRange()); |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | auto *CXXRD = T->getAsCXXRecordDecl(); |
| 1151 | if (CXXRD && CXXRD->hasUserDeclaredConstructor()) { |
| 1152 | |
| 1153 | |
| 1154 | bool HasEquivCtor = false; |
| 1155 | if (IList->getNumInits() == 0) { |
| 1156 | auto *CD = SemaRef.LookupDefaultConstructor(CXXRD); |
| 1157 | HasEquivCtor = CD && !CD->isDeleted(); |
| 1158 | } |
| 1159 | |
| 1160 | if (!HasEquivCtor) { |
| 1161 | SemaRef.Diag(IList->getBeginLoc(), |
| 1162 | diag::warn_cxx2a_compat_aggregate_init_with_ctors) |
| 1163 | << IList->getSourceRange() << T; |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
| 1170 | InitListExpr *IList, |
| 1171 | QualType &DeclType, |
| 1172 | bool SubobjectIsDesignatorContext, |
| 1173 | unsigned &Index, |
| 1174 | InitListExpr *StructuredList, |
| 1175 | unsigned &StructuredIndex, |
| 1176 | bool TopLevelObject) { |
| 1177 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 1178 | |
| 1179 | |
| 1180 | CheckComplexType(Entity, IList, DeclType, Index, |
| 1181 | StructuredList, StructuredIndex); |
| 1182 | } else if (DeclType->isScalarType()) { |
| 1183 | CheckScalarType(Entity, IList, DeclType, Index, |
| 1184 | StructuredList, StructuredIndex); |
| 1185 | } else if (DeclType->isVectorType()) { |
| 1186 | CheckVectorType(Entity, IList, DeclType, Index, |
| 1187 | StructuredList, StructuredIndex); |
| 1188 | } else if (DeclType->isRecordType()) { |
| 1189 | (0) . __assert_fail ("DeclType->isAggregateType() && \"non-aggregate records should be handed in CheckSubElementType\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 1190, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DeclType->isAggregateType() && |
| 1190 | (0) . __assert_fail ("DeclType->isAggregateType() && \"non-aggregate records should be handed in CheckSubElementType\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 1190, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "non-aggregate records should be handed in CheckSubElementType"); |
| 1191 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1192 | auto Bases = |
| 1193 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 1194 | CXXRecordDecl::base_class_iterator()); |
| 1195 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 1196 | Bases = CXXRD->bases(); |
| 1197 | CheckStructUnionTypes(Entity, IList, DeclType, Bases, RD->field_begin(), |
| 1198 | SubobjectIsDesignatorContext, Index, StructuredList, |
| 1199 | StructuredIndex, TopLevelObject); |
| 1200 | } else if (DeclType->isArrayType()) { |
| 1201 | llvm::APSInt Zero( |
| 1202 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 1203 | false); |
| 1204 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 1205 | SubobjectIsDesignatorContext, Index, |
| 1206 | StructuredList, StructuredIndex); |
| 1207 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 1208 | |
| 1209 | ++Index; |
| 1210 | if (!VerifyOnly) |
| 1211 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1212 | << DeclType; |
| 1213 | hadError = true; |
| 1214 | } else if (DeclType->isReferenceType()) { |
| 1215 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 1216 | StructuredList, StructuredIndex); |
| 1217 | } else if (DeclType->isObjCObjectType()) { |
| 1218 | if (!VerifyOnly) |
| 1219 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_objc_class) << DeclType; |
| 1220 | hadError = true; |
| 1221 | } else if (DeclType->isOCLIntelSubgroupAVCType()) { |
| 1222 | |
| 1223 | CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1224 | StructuredIndex); |
| 1225 | } else { |
| 1226 | if (!VerifyOnly) |
| 1227 | SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type) |
| 1228 | << DeclType; |
| 1229 | hadError = true; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
| 1234 | InitListExpr *IList, |
| 1235 | QualType ElemType, |
| 1236 | unsigned &Index, |
| 1237 | InitListExpr *StructuredList, |
| 1238 | unsigned &StructuredIndex) { |
| 1239 | Expr *expr = IList->getInit(Index); |
| 1240 | |
| 1241 | if (ElemType->isReferenceType()) |
| 1242 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 1243 | StructuredList, StructuredIndex); |
| 1244 | |
| 1245 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 1246 | if (SubInitList->getNumInits() == 1 && |
| 1247 | IsStringInit(SubInitList->getInit(0), ElemType, SemaRef.Context) == |
| 1248 | SIF_None) { |
| 1249 | expr = SubInitList->getInit(0); |
| 1250 | } else if (!SemaRef.getLangOpts().CPlusPlus) { |
| 1251 | InitListExpr *InnerStructuredList |
| 1252 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 1253 | StructuredList, StructuredIndex, |
| 1254 | SubInitList->getSourceRange(), true); |
| 1255 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 1256 | InnerStructuredList); |
| 1257 | |
| 1258 | if (!hadError && !VerifyOnly) { |
| 1259 | bool RequiresSecondPass = false; |
| 1260 | FillInEmptyInitializations(Entity, InnerStructuredList, |
| 1261 | RequiresSecondPass, StructuredList, |
| 1262 | StructuredIndex); |
| 1263 | if (RequiresSecondPass && !hadError) |
| 1264 | FillInEmptyInitializations(Entity, InnerStructuredList, |
| 1265 | RequiresSecondPass, StructuredList, |
| 1266 | StructuredIndex); |
| 1267 | } |
| 1268 | ++StructuredIndex; |
| 1269 | ++Index; |
| 1270 | return; |
| 1271 | } |
| 1272 | |
| 1273 | } else if (isa<ImplicitValueInitExpr>(expr)) { |
| 1274 | |
| 1275 | |
| 1276 | (0) . __assert_fail ("SemaRef.Context.hasSameType(expr->getType(), ElemType) && \"found implicit initialization for the wrong type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 1277, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SemaRef.Context.hasSameType(expr->getType(), ElemType) && |
| 1277 | (0) . __assert_fail ("SemaRef.Context.hasSameType(expr->getType(), ElemType) && \"found implicit initialization for the wrong type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 1277, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "found implicit initialization for the wrong type"); |
| 1278 | if (!VerifyOnly) |
| 1279 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1280 | ++Index; |
| 1281 | return; |
| 1282 | } |
| 1283 | |
| 1284 | if (SemaRef.getLangOpts().CPlusPlus) { |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | InitializationKind Kind = |
| 1291 | InitializationKind::CreateCopy(expr->getBeginLoc(), SourceLocation()); |
| 1292 | InitializationSequence Seq(SemaRef, Entity, Kind, expr, |
| 1293 | true); |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | if (Seq || isa<InitListExpr>(expr)) { |
| 1302 | if (!VerifyOnly) { |
| 1303 | ExprResult Result = |
| 1304 | Seq.Perform(SemaRef, Entity, Kind, expr); |
| 1305 | if (Result.isInvalid()) |
| 1306 | hadError = true; |
| 1307 | |
| 1308 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1309 | Result.getAs<Expr>()); |
| 1310 | } else if (!Seq) |
| 1311 | hadError = true; |
| 1312 | ++Index; |
| 1313 | return; |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | } else if (ElemType->isScalarType() || ElemType->isAtomicType()) { |
| 1318 | |
| 1319 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 1320 | StructuredList, StructuredIndex); |
| 1321 | } else if (const ArrayType *arrayType = |
| 1322 | SemaRef.Context.getAsArrayType(ElemType)) { |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
| 1328 | if (!VerifyOnly) { |
| 1329 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 1330 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1331 | } |
| 1332 | ++Index; |
| 1333 | return; |
| 1334 | } |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | } else { |
| 1339 | (0) . __assert_fail ("(ElemType->isRecordType() || ElemType->isVectorType() || ElemType->isOpenCLSpecificType()) && \"Unexpected type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 1340, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((ElemType->isRecordType() || ElemType->isVectorType() || |
| 1340 | (0) . __assert_fail ("(ElemType->isRecordType() || ElemType->isVectorType() || ElemType->isOpenCLSpecificType()) && \"Unexpected type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 1340, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ElemType->isOpenCLSpecificType()) && "Unexpected type"); |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | ExprResult ExprRes = expr; |
| 1351 | if (SemaRef.CheckSingleAssignmentConstraints( |
| 1352 | ElemType, ExprRes, !VerifyOnly) != Sema::Incompatible) { |
| 1353 | if (ExprRes.isInvalid()) |
| 1354 | hadError = true; |
| 1355 | else { |
| 1356 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
| 1357 | if (ExprRes.isInvalid()) |
| 1358 | hadError = true; |
| 1359 | } |
| 1360 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1361 | ExprRes.getAs<Expr>()); |
| 1362 | ++Index; |
| 1363 | return; |
| 1364 | } |
| 1365 | ExprRes.get(); |
| 1366 | |
| 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | |
| 1371 | |
| 1372 | |
| 1373 | |
| 1374 | |
| 1375 | |
| 1376 | if ((!SemaRef.getLangOpts().OpenCL && ElemType->isVectorType()) || |
| 1377 | ElemType->isAggregateType()) { |
| 1378 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 1379 | StructuredIndex); |
| 1380 | ++StructuredIndex; |
| 1381 | } else { |
| 1382 | if (!VerifyOnly) { |
| 1383 | |
| 1384 | |
| 1385 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
| 1386 | ); |
| 1387 | } |
| 1388 | hadError = true; |
| 1389 | ++Index; |
| 1390 | ++StructuredIndex; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 1395 | InitListExpr *IList, QualType DeclType, |
| 1396 | unsigned &Index, |
| 1397 | InitListExpr *StructuredList, |
| 1398 | unsigned &StructuredIndex) { |
| 1399 | (0) . __assert_fail ("Index == 0 && \"Index in explicit init list must be zero\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 1399, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Index == 0 && "Index in explicit init list must be zero"); |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | |
| 1404 | |
| 1405 | |
| 1406 | |
| 1407 | |
| 1408 | if (IList->getNumInits() != 2) |
| 1409 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1410 | StructuredIndex); |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
| 1415 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_complex_component_init) |
| 1416 | << IList->getSourceRange(); |
| 1417 | |
| 1418 | |
| 1419 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1420 | InitializedEntity ElementEntity = |
| 1421 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1422 | |
| 1423 | for (unsigned i = 0; i < 2; ++i) { |
| 1424 | ElementEntity.setElementIndex(Index); |
| 1425 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1426 | StructuredList, StructuredIndex); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
| 1431 | InitListExpr *IList, QualType DeclType, |
| 1432 | unsigned &Index, |
| 1433 | InitListExpr *StructuredList, |
| 1434 | unsigned &StructuredIndex) { |
| 1435 | if (Index >= IList->getNumInits()) { |
| 1436 | if (!VerifyOnly) |
| 1437 | SemaRef.Diag(IList->getBeginLoc(), |
| 1438 | SemaRef.getLangOpts().CPlusPlus11 |
| 1439 | ? diag::warn_cxx98_compat_empty_scalar_initializer |
| 1440 | : diag::err_empty_scalar_initializer) |
| 1441 | << IList->getSourceRange(); |
| 1442 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
| 1443 | ++Index; |
| 1444 | ++StructuredIndex; |
| 1445 | return; |
| 1446 | } |
| 1447 | |
| 1448 | Expr *expr = IList->getInit(Index); |
| 1449 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
| 1450 | |
| 1451 | |
| 1452 | if (!VerifyOnly) |
| 1453 | SemaRef.Diag(SubIList->getBeginLoc(), |
| 1454 | diag::ext_many_braces_around_scalar_init) |
| 1455 | << SubIList->getSourceRange(); |
| 1456 | |
| 1457 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1458 | StructuredIndex); |
| 1459 | return; |
| 1460 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 1461 | if (!VerifyOnly) |
| 1462 | SemaRef.Diag(expr->getBeginLoc(), diag::err_designator_for_scalar_init) |
| 1463 | << DeclType << expr->getSourceRange(); |
| 1464 | hadError = true; |
| 1465 | ++Index; |
| 1466 | ++StructuredIndex; |
| 1467 | return; |
| 1468 | } |
| 1469 | |
| 1470 | if (VerifyOnly) { |
| 1471 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
| 1472 | hadError = true; |
| 1473 | ++Index; |
| 1474 | return; |
| 1475 | } |
| 1476 | |
| 1477 | ExprResult Result = |
| 1478 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
| 1479 | ); |
| 1480 | |
| 1481 | Expr *ResultExpr = nullptr; |
| 1482 | |
| 1483 | if (Result.isInvalid()) |
| 1484 | hadError = true; |
| 1485 | else { |
| 1486 | ResultExpr = Result.getAs<Expr>(); |
| 1487 | |
| 1488 | if (ResultExpr != expr) { |
| 1489 | |
| 1490 | IList->setInit(Index, ResultExpr); |
| 1491 | } |
| 1492 | } |
| 1493 | if (hadError) |
| 1494 | ++StructuredIndex; |
| 1495 | else |
| 1496 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1497 | ++Index; |
| 1498 | } |
| 1499 | |
| 1500 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1501 | InitListExpr *IList, QualType DeclType, |
| 1502 | unsigned &Index, |
| 1503 | InitListExpr *StructuredList, |
| 1504 | unsigned &StructuredIndex) { |
| 1505 | if (Index >= IList->getNumInits()) { |
| 1506 | |
| 1507 | |
| 1508 | |
| 1509 | |
| 1510 | if (!VerifyOnly) |
| 1511 | SemaRef.Diag(IList->getBeginLoc(), |
| 1512 | diag::err_init_reference_member_uninitialized) |
| 1513 | << DeclType << IList->getSourceRange(); |
| 1514 | hadError = true; |
| 1515 | ++Index; |
| 1516 | ++StructuredIndex; |
| 1517 | return; |
| 1518 | } |
| 1519 | |
| 1520 | Expr *expr = IList->getInit(Index); |
| 1521 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
| 1522 | if (!VerifyOnly) |
| 1523 | SemaRef.Diag(IList->getBeginLoc(), diag::err_init_non_aggr_init_list) |
| 1524 | << DeclType << IList->getSourceRange(); |
| 1525 | hadError = true; |
| 1526 | ++Index; |
| 1527 | ++StructuredIndex; |
| 1528 | return; |
| 1529 | } |
| 1530 | |
| 1531 | if (VerifyOnly) { |
| 1532 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
| 1533 | hadError = true; |
| 1534 | ++Index; |
| 1535 | return; |
| 1536 | } |
| 1537 | |
| 1538 | ExprResult Result = |
| 1539 | SemaRef.PerformCopyInitialization(Entity, expr->getBeginLoc(), expr, |
| 1540 | ); |
| 1541 | |
| 1542 | if (Result.isInvalid()) |
| 1543 | hadError = true; |
| 1544 | |
| 1545 | expr = Result.getAs<Expr>(); |
| 1546 | IList->setInit(Index, expr); |
| 1547 | |
| 1548 | if (hadError) |
| 1549 | ++StructuredIndex; |
| 1550 | else |
| 1551 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1552 | ++Index; |
| 1553 | } |
| 1554 | |
| 1555 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
| 1556 | InitListExpr *IList, QualType DeclType, |
| 1557 | unsigned &Index, |
| 1558 | InitListExpr *StructuredList, |
| 1559 | unsigned &StructuredIndex) { |
| 1560 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1561 | unsigned maxElements = VT->getNumElements(); |
| 1562 | unsigned numEltsInit = 0; |
| 1563 | QualType elementType = VT->getElementType(); |
| 1564 | |
| 1565 | if (Index >= IList->getNumInits()) { |
| 1566 | |
| 1567 | if (VerifyOnly) |
| 1568 | CheckEmptyInitializable( |
| 1569 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1570 | IList->getEndLoc()); |
| 1571 | return; |
| 1572 | } |
| 1573 | |
| 1574 | if (!SemaRef.getLangOpts().OpenCL) { |
| 1575 | |
| 1576 | |
| 1577 | Expr *Init = IList->getInit(Index); |
| 1578 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
| 1579 | if (VerifyOnly) { |
| 1580 | if (!SemaRef.CanPerformCopyInitialization(Entity, Init)) |
| 1581 | hadError = true; |
| 1582 | ++Index; |
| 1583 | return; |
| 1584 | } |
| 1585 | |
| 1586 | ExprResult Result = |
| 1587 | SemaRef.PerformCopyInitialization(Entity, Init->getBeginLoc(), Init, |
| 1588 | ); |
| 1589 | |
| 1590 | Expr *ResultExpr = nullptr; |
| 1591 | if (Result.isInvalid()) |
| 1592 | hadError = true; |
| 1593 | else { |
| 1594 | ResultExpr = Result.getAs<Expr>(); |
| 1595 | |
| 1596 | if (ResultExpr != Init) { |
| 1597 | |
| 1598 | IList->setInit(Index, ResultExpr); |
| 1599 | } |
| 1600 | } |
| 1601 | if (hadError) |
| 1602 | ++StructuredIndex; |
| 1603 | else |
| 1604 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1605 | ResultExpr); |
| 1606 | ++Index; |
| 1607 | return; |
| 1608 | } |
| 1609 | |
| 1610 | InitializedEntity ElementEntity = |
| 1611 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1612 | |
| 1613 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1614 | |
| 1615 | if (Index >= IList->getNumInits()) { |
| 1616 | if (VerifyOnly) |
| 1617 | CheckEmptyInitializable(ElementEntity, IList->getEndLoc()); |
| 1618 | break; |
| 1619 | } |
| 1620 | |
| 1621 | ElementEntity.setElementIndex(Index); |
| 1622 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1623 | StructuredList, StructuredIndex); |
| 1624 | } |
| 1625 | |
| 1626 | if (VerifyOnly) |
| 1627 | return; |
| 1628 | |
| 1629 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1630 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1631 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1632 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1633 | |
| 1634 | |
| 1635 | |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | |
| 1640 | |
| 1641 | |
| 1642 | |
| 1643 | SemaRef.Diag(IList->getBeginLoc(), |
| 1644 | diag::warn_neon_vector_initializer_non_portable); |
| 1645 | |
| 1646 | const char *typeCode; |
| 1647 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1648 | |
| 1649 | if (elementType->isFloatingType()) |
| 1650 | typeCode = "f"; |
| 1651 | else if (elementType->isSignedIntegerType()) |
| 1652 | typeCode = "s"; |
| 1653 | else if (elementType->isUnsignedIntegerType()) |
| 1654 | typeCode = "u"; |
| 1655 | else |
| 1656 | llvm_unreachable("Invalid element type!"); |
| 1657 | |
| 1658 | SemaRef.Diag(IList->getBeginLoc(), |
| 1659 | SemaRef.Context.getTypeSize(VT) > 64 |
| 1660 | ? diag::note_neon_vector_initializer_non_portable_q |
| 1661 | : diag::note_neon_vector_initializer_non_portable) |
| 1662 | << typeCode << typeSize; |
| 1663 | } |
| 1664 | |
| 1665 | return; |
| 1666 | } |
| 1667 | |
| 1668 | InitializedEntity ElementEntity = |
| 1669 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1670 | |
| 1671 | |
| 1672 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1673 | |
| 1674 | if (Index >= IList->getNumInits()) |
| 1675 | break; |
| 1676 | |
| 1677 | ElementEntity.setElementIndex(Index); |
| 1678 | |
| 1679 | QualType IType = IList->getInit(Index)->getType(); |
| 1680 | if (!IType->isVectorType()) { |
| 1681 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1682 | StructuredList, StructuredIndex); |
| 1683 | ++numEltsInit; |
| 1684 | } else { |
| 1685 | QualType VecType; |
| 1686 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1687 | unsigned numIElts = IVT->getNumElements(); |
| 1688 | |
| 1689 | if (IType->isExtVectorType()) |
| 1690 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1691 | else |
| 1692 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
| 1693 | IVT->getVectorKind()); |
| 1694 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1695 | StructuredList, StructuredIndex); |
| 1696 | numEltsInit += numIElts; |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | |
| 1701 | if (numEltsInit != maxElements) { |
| 1702 | if (!VerifyOnly) |
| 1703 | SemaRef.Diag(IList->getBeginLoc(), |
| 1704 | diag::err_vector_incorrect_num_initializers) |
| 1705 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1706 | hadError = true; |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
| 1711 | InitListExpr *IList, QualType &DeclType, |
| 1712 | llvm::APSInt elementIndex, |
| 1713 | bool SubobjectIsDesignatorContext, |
| 1714 | unsigned &Index, |
| 1715 | InitListExpr *StructuredList, |
| 1716 | unsigned &StructuredIndex) { |
| 1717 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1718 | |
| 1719 | |
| 1720 | if (Index < IList->getNumInits()) { |
| 1721 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1722 | SIF_None) { |
| 1723 | |
| 1724 | |
| 1725 | |
| 1726 | |
| 1727 | |
| 1728 | if (!VerifyOnly) { |
| 1729 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1730 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1731 | IList->getInit(Index)); |
| 1732 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1733 | } |
| 1734 | ++Index; |
| 1735 | return; |
| 1736 | } |
| 1737 | } |
| 1738 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
| 1739 | |
| 1740 | |
| 1741 | |
| 1742 | if (!VerifyOnly) |
| 1743 | SemaRef.Diag(VAT->getSizeExpr()->getBeginLoc(), |
| 1744 | diag::err_variable_object_no_init) |
| 1745 | << VAT->getSizeExpr()->getSourceRange(); |
| 1746 | hadError = true; |
| 1747 | ++Index; |
| 1748 | ++StructuredIndex; |
| 1749 | return; |
| 1750 | } |
| 1751 | |
| 1752 | |
| 1753 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1754 | elementIndex.isUnsigned()); |
| 1755 | bool maxElementsKnown = false; |
| 1756 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
| 1757 | maxElements = CAT->getSize(); |
| 1758 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
| 1759 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
| 1760 | maxElementsKnown = true; |
| 1761 | } |
| 1762 | |
| 1763 | QualType elementType = arrayType->getElementType(); |
| 1764 | while (Index < IList->getNumInits()) { |
| 1765 | Expr *Init = IList->getInit(Index); |
| 1766 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
| 1767 | |
| 1768 | |
| 1769 | |
| 1770 | if (!SubobjectIsDesignatorContext) |
| 1771 | return; |
| 1772 | |
| 1773 | |
| 1774 | |
| 1775 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
| 1776 | DeclType, nullptr, &elementIndex, Index, |
| 1777 | StructuredList, StructuredIndex, true, |
| 1778 | false)) { |
| 1779 | hadError = true; |
| 1780 | continue; |
| 1781 | } |
| 1782 | |
| 1783 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
| 1784 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
| 1785 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
| 1786 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
| 1787 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
| 1788 | |
| 1789 | |
| 1790 | |
| 1791 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1792 | maxElements = elementIndex; |
| 1793 | |
| 1794 | continue; |
| 1795 | } |
| 1796 | |
| 1797 | |
| 1798 | |
| 1799 | if (maxElementsKnown && elementIndex == maxElements) |
| 1800 | break; |
| 1801 | |
| 1802 | InitializedEntity ElementEntity = |
| 1803 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
| 1804 | Entity); |
| 1805 | |
| 1806 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1807 | StructuredList, StructuredIndex); |
| 1808 | ++elementIndex; |
| 1809 | |
| 1810 | |
| 1811 | |
| 1812 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1813 | maxElements = elementIndex; |
| 1814 | } |
| 1815 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
| 1816 | |
| 1817 | |
| 1818 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
| 1819 | if (maxElements == Zero && !Entity.isVariableLengthArrayNew()) { |
| 1820 | |
| 1821 | |
| 1822 | SemaRef.Diag(IList->getBeginLoc(), diag::ext_typecheck_zero_array_size); |
| 1823 | } |
| 1824 | |
| 1825 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
| 1826 | ArrayType::Normal, 0); |
| 1827 | } |
| 1828 | if (!hadError && VerifyOnly) { |
| 1829 | |
| 1830 | |
| 1831 | |
| 1832 | |
| 1833 | |
| 1834 | if ((maxElementsKnown && elementIndex < maxElements) || |
| 1835 | Entity.isVariableLengthArrayNew()) |
| 1836 | CheckEmptyInitializable( |
| 1837 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1838 | IList->getEndLoc()); |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1843 | Expr *InitExpr, |
| 1844 | FieldDecl *Field, |
| 1845 | bool TopLevelObject) { |
| 1846 | |
| 1847 | unsigned FlexArrayDiag; |
| 1848 | if (isa<InitListExpr>(InitExpr) && |
| 1849 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1850 | |
| 1851 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1852 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
| 1853 | |
| 1854 | |
| 1855 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1856 | } else if (!TopLevelObject) { |
| 1857 | |
| 1858 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1859 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1860 | |
| 1861 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1862 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1863 | |
| 1864 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1865 | } else { |
| 1866 | |
| 1867 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1868 | } |
| 1869 | |
| 1870 | if (!VerifyOnly) { |
| 1871 | SemaRef.Diag(InitExpr->getBeginLoc(), FlexArrayDiag) |
| 1872 | << InitExpr->getBeginLoc(); |
| 1873 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1874 | << Field; |
| 1875 | } |
| 1876 | |
| 1877 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1878 | } |
| 1879 | |
| 1880 | |
| 1881 | |
| 1882 | |
| 1883 | |
| 1884 | |
| 1885 | |
| 1886 | |
| 1887 | |
| 1888 | static bool hasAccessibleDestructor(QualType ElementType, SourceLocation Loc, |
| 1889 | Sema &SemaRef) { |
| 1890 | auto *CXXRD = ElementType->getAsCXXRecordDecl(); |
| 1891 | if (!CXXRD) |
| 1892 | return false; |
| 1893 | |
| 1894 | CXXDestructorDecl *Destructor = SemaRef.LookupDestructor(CXXRD); |
| 1895 | SemaRef.CheckDestructorAccess(Loc, Destructor, |
| 1896 | SemaRef.PDiag(diag::err_access_dtor_temp) |
| 1897 | << ElementType); |
| 1898 | SemaRef.MarkFunctionReferenced(Loc, Destructor); |
| 1899 | if (SemaRef.DiagnoseUseOfDecl(Destructor, Loc)) |
| 1900 | return true; |
| 1901 | return false; |
| 1902 | } |
| 1903 | |
| 1904 | void InitListChecker::CheckStructUnionTypes( |
| 1905 | const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType, |
| 1906 | CXXRecordDecl::base_class_range Bases, RecordDecl::field_iterator Field, |
| 1907 | bool SubobjectIsDesignatorContext, unsigned &Index, |
| 1908 | InitListExpr *StructuredList, unsigned &StructuredIndex, |
| 1909 | bool TopLevelObject) { |
| 1910 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
| 1911 | |
| 1912 | |
| 1913 | |
| 1914 | if (structDecl->isInvalidDecl()) { |
| 1915 | |
| 1916 | ++Index; |
| 1917 | hadError = true; |
| 1918 | return; |
| 1919 | } |
| 1920 | |
| 1921 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1922 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1923 | |
| 1924 | if (!VerifyOnly) |
| 1925 | for (FieldDecl *FD : RD->fields()) { |
| 1926 | QualType ET = SemaRef.Context.getBaseElementType(FD->getType()); |
| 1927 | if (hasAccessibleDestructor(ET, IList->getEndLoc(), SemaRef)) { |
| 1928 | hadError = true; |
| 1929 | return; |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1935 | if (VerifyOnly) |
| 1936 | return; |
| 1937 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1938 | Field != FieldEnd; ++Field) { |
| 1939 | if (Field->hasInClassInitializer()) { |
| 1940 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1941 | |
| 1942 | return; |
| 1943 | } |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | |
| 1948 | |
| 1949 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1950 | Field != FieldEnd; ++Field) { |
| 1951 | if (!Field->isUnnamedBitfield()) { |
| 1952 | if (VerifyOnly) |
| 1953 | CheckEmptyInitializable( |
| 1954 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 1955 | IList->getEndLoc()); |
| 1956 | else |
| 1957 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1958 | break; |
| 1959 | } |
| 1960 | } |
| 1961 | return; |
| 1962 | } |
| 1963 | |
| 1964 | bool InitializedSomething = false; |
| 1965 | |
| 1966 | |
| 1967 | for (auto &Base : Bases) { |
| 1968 | Expr *Init = Index < IList->getNumInits() ? IList->getInit(Index) : nullptr; |
| 1969 | |
| 1970 | |
| 1971 | |
| 1972 | if (Init && isa<DesignatedInitExpr>(Init)) |
| 1973 | Init = nullptr; |
| 1974 | |
| 1975 | SourceLocation InitLoc = Init ? Init->getBeginLoc() : IList->getEndLoc(); |
| 1976 | InitializedEntity BaseEntity = InitializedEntity::InitializeBase( |
| 1977 | SemaRef.Context, &Base, false, &Entity); |
| 1978 | if (Init) { |
| 1979 | CheckSubElementType(BaseEntity, IList, Base.getType(), Index, |
| 1980 | StructuredList, StructuredIndex); |
| 1981 | InitializedSomething = true; |
| 1982 | } else if (VerifyOnly) { |
| 1983 | CheckEmptyInitializable(BaseEntity, InitLoc); |
| 1984 | } |
| 1985 | |
| 1986 | if (!VerifyOnly) |
| 1987 | if (hasAccessibleDestructor(Base.getType(), InitLoc, SemaRef)) { |
| 1988 | hadError = true; |
| 1989 | return; |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | |
| 1994 | |
| 1995 | |
| 1996 | |
| 1997 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1998 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1999 | bool CheckForMissingFields = |
| 2000 | !IList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()); |
| 2001 | bool HasDesignatedInit = false; |
| 2002 | |
| 2003 | while (Index < IList->getNumInits()) { |
| 2004 | Expr *Init = IList->getInit(Index); |
| 2005 | SourceLocation InitLoc = Init->getBeginLoc(); |
| 2006 | |
| 2007 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
| 2008 | |
| 2009 | |
| 2010 | |
| 2011 | if (!SubobjectIsDesignatorContext) |
| 2012 | return; |
| 2013 | |
| 2014 | HasDesignatedInit = true; |
| 2015 | |
| 2016 | |
| 2017 | |
| 2018 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
| 2019 | DeclType, &Field, nullptr, Index, |
| 2020 | StructuredList, StructuredIndex, |
| 2021 | true, TopLevelObject)) |
| 2022 | hadError = true; |
| 2023 | else if (!VerifyOnly) { |
| 2024 | |
| 2025 | RecordDecl::field_iterator F = RD->field_begin(); |
| 2026 | while (std::next(F) != Field) |
| 2027 | ++F; |
| 2028 | QualType ET = SemaRef.Context.getBaseElementType(F->getType()); |
| 2029 | if (hasAccessibleDestructor(ET, InitLoc, SemaRef)) { |
| 2030 | hadError = true; |
| 2031 | return; |
| 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | InitializedSomething = true; |
| 2036 | |
| 2037 | |
| 2038 | |
| 2039 | CheckForMissingFields = false; |
| 2040 | continue; |
| 2041 | } |
| 2042 | |
| 2043 | if (Field == FieldEnd) { |
| 2044 | |
| 2045 | break; |
| 2046 | } |
| 2047 | |
| 2048 | |
| 2049 | if (InitializedSomething && DeclType->isUnionType()) |
| 2050 | break; |
| 2051 | |
| 2052 | |
| 2053 | if (Field->getType()->isIncompleteArrayType()) |
| 2054 | break; |
| 2055 | |
| 2056 | if (Field->isUnnamedBitfield()) { |
| 2057 | |
| 2058 | ++Field; |
| 2059 | continue; |
| 2060 | } |
| 2061 | |
| 2062 | |
| 2063 | bool InvalidUse; |
| 2064 | if (VerifyOnly) |
| 2065 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
| 2066 | else |
| 2067 | InvalidUse = SemaRef.DiagnoseUseOfDecl( |
| 2068 | *Field, IList->getInit(Index)->getBeginLoc()); |
| 2069 | if (InvalidUse) { |
| 2070 | ++Index; |
| 2071 | ++Field; |
| 2072 | hadError = true; |
| 2073 | continue; |
| 2074 | } |
| 2075 | |
| 2076 | if (!VerifyOnly) { |
| 2077 | QualType ET = SemaRef.Context.getBaseElementType(Field->getType()); |
| 2078 | if (hasAccessibleDestructor(ET, InitLoc, SemaRef)) { |
| 2079 | hadError = true; |
| 2080 | return; |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | InitializedEntity MemberEntity = |
| 2085 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 2086 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 2087 | StructuredList, StructuredIndex); |
| 2088 | InitializedSomething = true; |
| 2089 | |
| 2090 | if (DeclType->isUnionType() && !VerifyOnly) { |
| 2091 | |
| 2092 | StructuredList->setInitializedFieldInUnion(*Field); |
| 2093 | } |
| 2094 | |
| 2095 | ++Field; |
| 2096 | } |
| 2097 | |
| 2098 | |
| 2099 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 2100 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 2101 | !DeclType->isUnionType()) { |
| 2102 | |
| 2103 | |
| 2104 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 2105 | it != end; ++it) { |
| 2106 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
| 2107 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 2108 | diag::warn_missing_field_initializers) << *it; |
| 2109 | break; |
| 2110 | } |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | |
| 2115 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 2116 | !Field->getType()->isIncompleteArrayType()) { |
| 2117 | |
| 2118 | for (; Field != FieldEnd && !hadError; ++Field) { |
| 2119 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
| 2120 | CheckEmptyInitializable( |
| 2121 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 2122 | IList->getEndLoc()); |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | |
| 2127 | if (!VerifyOnly) { |
| 2128 | |
| 2129 | |
| 2130 | RecordDecl::field_iterator I = HasDesignatedInit ? RD->field_begin() |
| 2131 | : Field; |
| 2132 | for (RecordDecl::field_iterator E = RD->field_end(); I != E; ++I) { |
| 2133 | QualType ET = SemaRef.Context.getBaseElementType(I->getType()); |
| 2134 | if (hasAccessibleDestructor(ET, IList->getEndLoc(), SemaRef)) { |
| 2135 | hadError = true; |
| 2136 | return; |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
| 2142 | Index >= IList->getNumInits()) |
| 2143 | return; |
| 2144 | |
| 2145 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
| 2146 | TopLevelObject)) { |
| 2147 | hadError = true; |
| 2148 | ++Index; |
| 2149 | return; |
| 2150 | } |
| 2151 | |
| 2152 | InitializedEntity MemberEntity = |
| 2153 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 2154 | |
| 2155 | if (isa<InitListExpr>(IList->getInit(Index))) |
| 2156 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 2157 | StructuredList, StructuredIndex); |
| 2158 | else |
| 2159 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
| 2160 | StructuredList, StructuredIndex); |
| 2161 | } |
| 2162 | |
| 2163 | |
| 2164 | |
| 2165 | |
| 2166 | |
| 2167 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
| 2168 | DesignatedInitExpr *DIE, |
| 2169 | unsigned DesigIdx, |
| 2170 | IndirectFieldDecl *IndirectField) { |
| 2171 | typedef DesignatedInitExpr::Designator Designator; |
| 2172 | |
| 2173 | |
| 2174 | SmallVector<Designator, 4> Replacements; |
| 2175 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 2176 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 2177 | if (PI + 1 == PE) |
| 2178 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 2179 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 2180 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 2181 | else |
| 2182 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 2183 | SourceLocation(), SourceLocation())); |
| 2184 | (*PI)", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 2184, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<FieldDecl>(*PI)); |
| 2185 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
| 2186 | } |
| 2187 | |
| 2188 | |
| 2189 | |
| 2190 | |
| 2191 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
| 2192 | &Replacements[0] + Replacements.size()); |
| 2193 | } |
| 2194 | |
| 2195 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 2196 | DesignatedInitExpr *DIE) { |
| 2197 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 2198 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 2199 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 2200 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 2201 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators(), |
| 2202 | IndexExprs, |
| 2203 | DIE->getEqualOrColonLoc(), |
| 2204 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 2205 | } |
| 2206 | |
| 2207 | namespace { |
| 2208 | |
| 2209 | |
| 2210 | |
| 2211 | class FieldInitializerValidatorCCC final : public CorrectionCandidateCallback { |
| 2212 | public: |
| 2213 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 2214 | : Record(RD) {} |
| 2215 | |
| 2216 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 2217 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 2218 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 2219 | } |
| 2220 | |
| 2221 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 2222 | return llvm::make_unique<FieldInitializerValidatorCCC>(*this); |
| 2223 | } |
| 2224 | |
| 2225 | private: |
| 2226 | RecordDecl *Record; |
| 2227 | }; |
| 2228 | |
| 2229 | } |
| 2230 | |
| 2231 | |
| 2232 | |
| 2233 | |
| 2234 | |
| 2235 | |
| 2236 | |
| 2237 | |
| 2238 | |
| 2239 | |
| 2240 | |
| 2241 | |
| 2242 | |
| 2243 | |
| 2244 | |
| 2245 | |
| 2246 | |
| 2247 | |
| 2248 | |
| 2249 | |
| 2250 | |
| 2251 | |
| 2252 | |
| 2253 | |
| 2254 | |
| 2255 | |
| 2256 | |
| 2257 | |
| 2258 | |
| 2259 | |
| 2260 | |
| 2261 | |
| 2262 | |
| 2263 | |
| 2264 | |
| 2265 | |
| 2266 | bool |
| 2267 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
| 2268 | InitListExpr *IList, |
| 2269 | DesignatedInitExpr *DIE, |
| 2270 | unsigned DesigIdx, |
| 2271 | QualType &CurrentObjectType, |
| 2272 | RecordDecl::field_iterator *NextField, |
| 2273 | llvm::APSInt *NextElementIndex, |
| 2274 | unsigned &Index, |
| 2275 | InitListExpr *StructuredList, |
| 2276 | unsigned &StructuredIndex, |
| 2277 | bool FinishSubobjectInit, |
| 2278 | bool TopLevelObject) { |
| 2279 | if (DesigIdx == DIE->size()) { |
| 2280 | |
| 2281 | bool prevHadError = hadError; |
| 2282 | |
| 2283 | |
| 2284 | |
| 2285 | |
| 2286 | unsigned OldIndex = Index; |
| 2287 | IList->setInit(OldIndex, DIE->getInit()); |
| 2288 | |
| 2289 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
| 2290 | StructuredList, StructuredIndex); |
| 2291 | |
| 2292 | |
| 2293 | |
| 2294 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 2295 | DIE->setInit(IList->getInit(OldIndex)); |
| 2296 | IList->setInit(OldIndex, DIE); |
| 2297 | |
| 2298 | return hadError && !prevHadError; |
| 2299 | } |
| 2300 | |
| 2301 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
| 2302 | bool IsFirstDesignator = (DesigIdx == 0); |
| 2303 | if (!VerifyOnly) { |
| 2304 | (0) . __assert_fail ("(IsFirstDesignator || StructuredList) && \"Need a non-designated initializer list to start from\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 2305, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((IsFirstDesignator || StructuredList) && |
| 2305 | (0) . __assert_fail ("(IsFirstDesignator || StructuredList) && \"Need a non-designated initializer list to start from\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 2305, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Need a non-designated initializer list to start from"); |
| 2306 | |
| 2307 | |
| 2308 | |
| 2309 | if (IsFirstDesignator) |
| 2310 | StructuredList = SyntacticToSemantic.lookup(IList); |
| 2311 | else { |
| 2312 | Expr *ExistingInit = StructuredIndex < StructuredList->getNumInits() ? |
| 2313 | StructuredList->getInit(StructuredIndex) : nullptr; |
| 2314 | if (!ExistingInit && StructuredList->hasArrayFiller()) |
| 2315 | ExistingInit = StructuredList->getArrayFiller(); |
| 2316 | |
| 2317 | if (!ExistingInit) |
| 2318 | StructuredList = getStructuredSubobjectInit( |
| 2319 | IList, Index, CurrentObjectType, StructuredList, StructuredIndex, |
| 2320 | SourceRange(D->getBeginLoc(), DIE->getEndLoc())); |
| 2321 | else if (InitListExpr *Result = dyn_cast<InitListExpr>(ExistingInit)) |
| 2322 | StructuredList = Result; |
| 2323 | else { |
| 2324 | if (DesignatedInitUpdateExpr *E = |
| 2325 | dyn_cast<DesignatedInitUpdateExpr>(ExistingInit)) |
| 2326 | StructuredList = E->getUpdater(); |
| 2327 | else { |
| 2328 | DesignatedInitUpdateExpr *DIUE = new (SemaRef.Context) |
| 2329 | DesignatedInitUpdateExpr(SemaRef.Context, D->getBeginLoc(), |
| 2330 | ExistingInit, DIE->getEndLoc()); |
| 2331 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, DIUE); |
| 2332 | StructuredList = DIUE->getUpdater(); |
| 2333 | } |
| 2334 | |
| 2335 | |
| 2336 | |
| 2337 | |
| 2338 | |
| 2339 | |
| 2340 | |
| 2341 | |
| 2342 | |
| 2343 | if (ExistingInit->getSourceRange().isValid()) { |
| 2344 | |
| 2345 | |
| 2346 | |
| 2347 | |
| 2348 | |
| 2349 | |
| 2350 | |
| 2351 | |
| 2352 | |
| 2353 | |
| 2354 | |
| 2355 | SemaRef.Diag(D->getBeginLoc(), |
| 2356 | diag::warn_subobject_initializer_overrides) |
| 2357 | << SourceRange(D->getBeginLoc(), DIE->getEndLoc()); |
| 2358 | |
| 2359 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
| 2360 | diag::note_previous_initializer) |
| 2361 | << << ExistingInit->getSourceRange(); |
| 2362 | } |
| 2363 | } |
| 2364 | } |
| 2365 | (0) . __assert_fail ("StructuredList && \"Expected a structured initializer list\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 2365, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(StructuredList && "Expected a structured initializer list"); |
| 2366 | } |
| 2367 | |
| 2368 | if (D->isFieldDesignator()) { |
| 2369 | |
| 2370 | |
| 2371 | |
| 2372 | |
| 2373 | |
| 2374 | |
| 2375 | |
| 2376 | |
| 2377 | |
| 2378 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
| 2379 | if (!RT) { |
| 2380 | SourceLocation Loc = D->getDotLoc(); |
| 2381 | if (Loc.isInvalid()) |
| 2382 | Loc = D->getFieldLoc(); |
| 2383 | if (!VerifyOnly) |
| 2384 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 2385 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
| 2386 | ++Index; |
| 2387 | return true; |
| 2388 | } |
| 2389 | |
| 2390 | FieldDecl *KnownField = D->getField(); |
| 2391 | if (!KnownField) { |
| 2392 | IdentifierInfo *FieldName = D->getFieldName(); |
| 2393 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 2394 | for (NamedDecl *ND : Lookup) { |
| 2395 | if (auto *FD = dyn_cast<FieldDecl>(ND)) { |
| 2396 | KnownField = FD; |
| 2397 | break; |
| 2398 | } |
| 2399 | if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) { |
| 2400 | |
| 2401 | if (VerifyOnly) |
| 2402 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
| 2403 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD); |
| 2404 | D = DIE->getDesignator(DesigIdx); |
| 2405 | KnownField = cast<FieldDecl>(*IFD->chain_begin()); |
| 2406 | break; |
| 2407 | } |
| 2408 | } |
| 2409 | if (!KnownField) { |
| 2410 | if (VerifyOnly) { |
| 2411 | ++Index; |
| 2412 | return true; |
| 2413 | } |
| 2414 | |
| 2415 | |
| 2416 | if (!Lookup.empty()) { |
| 2417 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 2418 | << FieldName; |
| 2419 | SemaRef.Diag(Lookup.front()->getLocation(), |
| 2420 | diag::note_field_designator_found); |
| 2421 | ++Index; |
| 2422 | return true; |
| 2423 | } |
| 2424 | |
| 2425 | |
| 2426 | |
| 2427 | FieldInitializerValidatorCCC CCC(RT->getDecl()); |
| 2428 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 2429 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 2430 | Sema::LookupMemberName, , , CCC, |
| 2431 | Sema::CTK_ErrorRecovery, RT->getDecl())) { |
| 2432 | SemaRef.diagnoseTypo( |
| 2433 | Corrected, |
| 2434 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
| 2435 | << FieldName << CurrentObjectType); |
| 2436 | KnownField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
| 2437 | hadError = true; |
| 2438 | } else { |
| 2439 | |
| 2440 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 2441 | << FieldName << CurrentObjectType; |
| 2442 | ++Index; |
| 2443 | return true; |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | unsigned FieldIndex = 0; |
| 2449 | |
| 2450 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 2451 | FieldIndex = CXXRD->getNumBases(); |
| 2452 | |
| 2453 | for (auto *FI : RT->getDecl()->fields()) { |
| 2454 | if (FI->isUnnamedBitfield()) |
| 2455 | continue; |
| 2456 | if (declaresSameEntity(KnownField, FI)) { |
| 2457 | KnownField = FI; |
| 2458 | break; |
| 2459 | } |
| 2460 | ++FieldIndex; |
| 2461 | } |
| 2462 | |
| 2463 | RecordDecl::field_iterator Field = |
| 2464 | RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField)); |
| 2465 | |
| 2466 | |
| 2467 | |
| 2468 | if (RT->getDecl()->isUnion()) { |
| 2469 | FieldIndex = 0; |
| 2470 | if (!VerifyOnly) { |
| 2471 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
| 2472 | if (CurrentField && !declaresSameEntity(CurrentField, *Field)) { |
| 2473 | (0) . __assert_fail ("StructuredList->getNumInits() == 1 && \"A union should never have more than one initializer!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 2474, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(StructuredList->getNumInits() == 1 |
| 2474 | (0) . __assert_fail ("StructuredList->getNumInits() == 1 && \"A union should never have more than one initializer!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 2474, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> && "A union should never have more than one initializer!"); |
| 2475 | |
| 2476 | Expr *ExistingInit = StructuredList->getInit(0); |
| 2477 | if (ExistingInit) { |
| 2478 | |
| 2479 | SemaRef.Diag(D->getFieldLoc(), |
| 2480 | diag::warn_initializer_overrides) |
| 2481 | << D->getSourceRange(); |
| 2482 | SemaRef.Diag(ExistingInit->getBeginLoc(), |
| 2483 | diag::note_previous_initializer) |
| 2484 | << |
| 2485 | << ExistingInit->getSourceRange(); |
| 2486 | } |
| 2487 | |
| 2488 | |
| 2489 | StructuredList->resizeInits(SemaRef.Context, 0); |
| 2490 | StructuredList->setInitializedFieldInUnion(nullptr); |
| 2491 | } |
| 2492 | |
| 2493 | StructuredList->setInitializedFieldInUnion(*Field); |
| 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | |
| 2498 | bool InvalidUse; |
| 2499 | if (VerifyOnly) |
| 2500 | InvalidUse = !SemaRef.CanUseDecl(*Field, TreatUnavailableAsInvalid); |
| 2501 | else |
| 2502 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
| 2503 | if (InvalidUse) { |
| 2504 | ++Index; |
| 2505 | return true; |
| 2506 | } |
| 2507 | |
| 2508 | if (!VerifyOnly) { |
| 2509 | |
| 2510 | D->setField(*Field); |
| 2511 | |
| 2512 | |
| 2513 | |
| 2514 | if (FieldIndex >= StructuredList->getNumInits()) |
| 2515 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 2516 | } |
| 2517 | |
| 2518 | |
| 2519 | if (Field->getType()->isIncompleteArrayType()) { |
| 2520 | bool Invalid = false; |
| 2521 | if ((DesigIdx + 1) != DIE->size()) { |
| 2522 | |
| 2523 | |
| 2524 | if (!VerifyOnly) { |
| 2525 | DesignatedInitExpr::Designator *NextD |
| 2526 | = DIE->getDesignator(DesigIdx + 1); |
| 2527 | SemaRef.Diag(NextD->getBeginLoc(), |
| 2528 | diag::err_designator_into_flexible_array_member) |
| 2529 | << SourceRange(NextD->getBeginLoc(), DIE->getEndLoc()); |
| 2530 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 2531 | << *Field; |
| 2532 | } |
| 2533 | Invalid = true; |
| 2534 | } |
| 2535 | |
| 2536 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2537 | !isa<StringLiteral>(DIE->getInit())) { |
| 2538 | |
| 2539 | if (!VerifyOnly) { |
| 2540 | SemaRef.Diag(DIE->getInit()->getBeginLoc(), |
| 2541 | diag::err_flexible_array_init_needs_braces) |
| 2542 | << DIE->getInit()->getSourceRange(); |
| 2543 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 2544 | << *Field; |
| 2545 | } |
| 2546 | Invalid = true; |
| 2547 | } |
| 2548 | |
| 2549 | |
| 2550 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
| 2551 | TopLevelObject)) |
| 2552 | Invalid = true; |
| 2553 | |
| 2554 | if (Invalid) { |
| 2555 | ++Index; |
| 2556 | return true; |
| 2557 | } |
| 2558 | |
| 2559 | |
| 2560 | bool prevHadError = hadError; |
| 2561 | unsigned newStructuredIndex = FieldIndex; |
| 2562 | unsigned OldIndex = Index; |
| 2563 | IList->setInit(Index, DIE->getInit()); |
| 2564 | |
| 2565 | InitializedEntity MemberEntity = |
| 2566 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 2567 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 2568 | StructuredList, newStructuredIndex); |
| 2569 | |
| 2570 | IList->setInit(OldIndex, DIE); |
| 2571 | if (hadError && !prevHadError) { |
| 2572 | ++Field; |
| 2573 | ++FieldIndex; |
| 2574 | if (NextField) |
| 2575 | *NextField = Field; |
| 2576 | StructuredIndex = FieldIndex; |
| 2577 | return true; |
| 2578 | } |
| 2579 | } else { |
| 2580 | |
| 2581 | QualType FieldType = Field->getType(); |
| 2582 | unsigned newStructuredIndex = FieldIndex; |
| 2583 | |
| 2584 | InitializedEntity MemberEntity = |
| 2585 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 2586 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 2587 | FieldType, nullptr, nullptr, Index, |
| 2588 | StructuredList, newStructuredIndex, |
| 2589 | FinishSubobjectInit, false)) |
| 2590 | return true; |
| 2591 | } |
| 2592 | |
| 2593 | |
| 2594 | |
| 2595 | ++Field; |
| 2596 | ++FieldIndex; |
| 2597 | |
| 2598 | |
| 2599 | |
| 2600 | if (IsFirstDesignator) { |
| 2601 | if (NextField) |
| 2602 | *NextField = Field; |
| 2603 | StructuredIndex = FieldIndex; |
| 2604 | return false; |
| 2605 | } |
| 2606 | |
| 2607 | if (!FinishSubobjectInit) |
| 2608 | return false; |
| 2609 | |
| 2610 | |
| 2611 | if (RT->getDecl()->isUnion()) |
| 2612 | return hadError; |
| 2613 | |
| 2614 | |
| 2615 | bool prevHadError = hadError; |
| 2616 | |
| 2617 | auto NoBases = |
| 2618 | CXXRecordDecl::base_class_range(CXXRecordDecl::base_class_iterator(), |
| 2619 | CXXRecordDecl::base_class_iterator()); |
| 2620 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, NoBases, Field, |
| 2621 | false, Index, StructuredList, FieldIndex); |
| 2622 | return hadError && !prevHadError; |
| 2623 | } |
| 2624 | |
| 2625 | |
| 2626 | |
| 2627 | |
| 2628 | |
| 2629 | |
| 2630 | |
| 2631 | |
| 2632 | |
| 2633 | |
| 2634 | |
| 2635 | |
| 2636 | |
| 2637 | |
| 2638 | |
| 2639 | |
| 2640 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
| 2641 | if (!AT) { |
| 2642 | if (!VerifyOnly) |
| 2643 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2644 | << CurrentObjectType; |
| 2645 | ++Index; |
| 2646 | return true; |
| 2647 | } |
| 2648 | |
| 2649 | Expr *IndexExpr = nullptr; |
| 2650 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2651 | if (D->isArrayDesignator()) { |
| 2652 | IndexExpr = DIE->getArrayIndex(*D); |
| 2653 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
| 2654 | DesignatedEndIndex = DesignatedStartIndex; |
| 2655 | } else { |
| 2656 | (0) . __assert_fail ("D->isArrayRangeDesignator() && \"Need array-range designator\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 2656, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
| 2657 | |
| 2658 | DesignatedStartIndex = |
| 2659 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
| 2660 | DesignatedEndIndex = |
| 2661 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
| 2662 | IndexExpr = DIE->getArrayRangeEnd(*D); |
| 2663 | |
| 2664 | |
| 2665 | |
| 2666 | |
| 2667 | |
| 2668 | |
| 2669 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
| 2670 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
| 2671 | FullyStructuredList->sawArrayRangeDesignator(); |
| 2672 | } |
| 2673 | |
| 2674 | if (isa<ConstantArrayType>(AT)) { |
| 2675 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
| 2676 | DesignatedStartIndex |
| 2677 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 2678 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2679 | DesignatedEndIndex |
| 2680 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 2681 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2682 | if (DesignatedEndIndex >= MaxElements) { |
| 2683 | if (!VerifyOnly) |
| 2684 | SemaRef.Diag(IndexExpr->getBeginLoc(), |
| 2685 | diag::err_array_designator_too_large) |
| 2686 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2687 | << IndexExpr->getSourceRange(); |
| 2688 | ++Index; |
| 2689 | return true; |
| 2690 | } |
| 2691 | } else { |
| 2692 | unsigned DesignatedIndexBitWidth = |
| 2693 | ConstantArrayType::getMaxSizeBits(SemaRef.Context); |
| 2694 | DesignatedStartIndex = |
| 2695 | DesignatedStartIndex.extOrTrunc(DesignatedIndexBitWidth); |
| 2696 | DesignatedEndIndex = |
| 2697 | DesignatedEndIndex.extOrTrunc(DesignatedIndexBitWidth); |
| 2698 | DesignatedStartIndex.setIsUnsigned(true); |
| 2699 | DesignatedEndIndex.setIsUnsigned(true); |
| 2700 | } |
| 2701 | |
| 2702 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2703 | |
| 2704 | |
| 2705 | ASTContext &Context = SemaRef.Context; |
| 2706 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2707 | |
| 2708 | |
| 2709 | QualType CharTy = AT->getElementType(); |
| 2710 | |
| 2711 | |
| 2712 | QualType PromotedCharTy = CharTy; |
| 2713 | if (CharTy->isPromotableIntegerType()) |
| 2714 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2715 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2716 | |
| 2717 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2718 | |
| 2719 | uint64_t StrLen = SL->getLength(); |
| 2720 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2721 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2722 | StructuredList->resizeInits(Context, StrLen); |
| 2723 | |
| 2724 | |
| 2725 | |
| 2726 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2727 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2728 | Expr *Init = new (Context) IntegerLiteral( |
| 2729 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
| 2730 | if (CharTy != PromotedCharTy) |
| 2731 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
| 2732 | Init, nullptr, VK_RValue); |
| 2733 | StructuredList->updateInit(Context, i, Init); |
| 2734 | } |
| 2735 | } else { |
| 2736 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2737 | std::string Str; |
| 2738 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2739 | |
| 2740 | |
| 2741 | uint64_t StrLen = Str.size(); |
| 2742 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2743 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2744 | StructuredList->resizeInits(Context, StrLen); |
| 2745 | |
| 2746 | |
| 2747 | |
| 2748 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2749 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2750 | Expr *Init = new (Context) IntegerLiteral( |
| 2751 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
| 2752 | if (CharTy != PromotedCharTy) |
| 2753 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
| 2754 | Init, nullptr, VK_RValue); |
| 2755 | StructuredList->updateInit(Context, i, Init); |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | |
| 2761 | |
| 2762 | if (!VerifyOnly && |
| 2763 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
| 2764 | StructuredList->resizeInits(SemaRef.Context, |
| 2765 | DesignatedEndIndex.getZExtValue() + 1); |
| 2766 | |
| 2767 | |
| 2768 | |
| 2769 | |
| 2770 | |
| 2771 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2772 | unsigned OldIndex = Index; |
| 2773 | |
| 2774 | InitializedEntity ElementEntity = |
| 2775 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 2776 | |
| 2777 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2778 | |
| 2779 | QualType ElementType = AT->getElementType(); |
| 2780 | Index = OldIndex; |
| 2781 | |
| 2782 | ElementEntity.setElementIndex(ElementIndex); |
| 2783 | if (CheckDesignatedInitializer( |
| 2784 | ElementEntity, IList, DIE, DesigIdx + 1, ElementType, nullptr, |
| 2785 | nullptr, Index, StructuredList, ElementIndex, |
| 2786 | FinishSubobjectInit && (DesignatedStartIndex == DesignatedEndIndex), |
| 2787 | false)) |
| 2788 | return true; |
| 2789 | |
| 2790 | |
| 2791 | ++DesignatedStartIndex; |
| 2792 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2793 | } |
| 2794 | |
| 2795 | |
| 2796 | |
| 2797 | if (IsFirstDesignator) { |
| 2798 | if (NextElementIndex) |
| 2799 | *NextElementIndex = DesignatedStartIndex; |
| 2800 | StructuredIndex = ElementIndex; |
| 2801 | return false; |
| 2802 | } |
| 2803 | |
| 2804 | if (!FinishSubobjectInit) |
| 2805 | return false; |
| 2806 | |
| 2807 | |
| 2808 | bool prevHadError = hadError; |
| 2809 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
| 2810 | , Index, |
| 2811 | StructuredList, ElementIndex); |
| 2812 | return hadError && !prevHadError; |
| 2813 | } |
| 2814 | |
| 2815 | |
| 2816 | |
| 2817 | InitListExpr * |
| 2818 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2819 | QualType CurrentObjectType, |
| 2820 | InitListExpr *StructuredList, |
| 2821 | unsigned StructuredIndex, |
| 2822 | SourceRange InitRange, |
| 2823 | bool IsFullyOverwritten) { |
| 2824 | if (VerifyOnly) |
| 2825 | return nullptr; |
| 2826 | Expr *ExistingInit = nullptr; |
| 2827 | if (!StructuredList) |
| 2828 | ExistingInit = SyntacticToSemantic.lookup(IList); |
| 2829 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2830 | ExistingInit = StructuredList->getInit(StructuredIndex); |
| 2831 | |
| 2832 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2833 | |
| 2834 | |
| 2835 | |
| 2836 | |
| 2837 | |
| 2838 | |
| 2839 | |
| 2840 | |
| 2841 | if (!IsFullyOverwritten) |
| 2842 | return Result; |
| 2843 | |
| 2844 | if (ExistingInit) { |
| 2845 | |
| 2846 | |
| 2847 | |
| 2848 | |
| 2849 | |
| 2850 | |
| 2851 | |
| 2852 | |
| 2853 | |
| 2854 | |
| 2855 | |
| 2856 | SemaRef.Diag(InitRange.getBegin(), |
| 2857 | diag::warn_subobject_initializer_overrides) |
| 2858 | << InitRange; |
| 2859 | SemaRef.Diag(ExistingInit->getBeginLoc(), diag::note_previous_initializer) |
| 2860 | << << ExistingInit->getSourceRange(); |
| 2861 | } |
| 2862 | |
| 2863 | InitListExpr *Result |
| 2864 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 2865 | InitRange.getBegin(), None, |
| 2866 | InitRange.getEnd()); |
| 2867 | |
| 2868 | QualType ResultType = CurrentObjectType; |
| 2869 | if (!ResultType->isArrayType()) |
| 2870 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2871 | Result->setType(ResultType); |
| 2872 | |
| 2873 | |
| 2874 | unsigned NumElements = 0; |
| 2875 | unsigned NumInits = 0; |
| 2876 | bool GotNumInits = false; |
| 2877 | if (!StructuredList) { |
| 2878 | NumInits = IList->getNumInits(); |
| 2879 | GotNumInits = true; |
| 2880 | } else if (Index < IList->getNumInits()) { |
| 2881 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
| 2882 | NumInits = SubList->getNumInits(); |
| 2883 | GotNumInits = true; |
| 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | if (const ArrayType *AType |
| 2888 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2889 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2890 | NumElements = CAType->getSize().getZExtValue(); |
| 2891 | |
| 2892 | |
| 2893 | if (GotNumInits && NumElements > NumInits) |
| 2894 | NumElements = 0; |
| 2895 | } |
| 2896 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
| 2897 | NumElements = VType->getNumElements(); |
| 2898 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
| 2899 | RecordDecl *RDecl = RType->getDecl(); |
| 2900 | if (RDecl->isUnion()) |
| 2901 | NumElements = 1; |
| 2902 | else |
| 2903 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
| 2904 | } |
| 2905 | |
| 2906 | Result->reserveInits(SemaRef.Context, NumElements); |
| 2907 | |
| 2908 | |
| 2909 | |
| 2910 | if (StructuredList) |
| 2911 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
| 2912 | else { |
| 2913 | Result->setSyntacticForm(IList); |
| 2914 | SyntacticToSemantic[IList] = Result; |
| 2915 | } |
| 2916 | |
| 2917 | return Result; |
| 2918 | } |
| 2919 | |
| 2920 | |
| 2921 | |
| 2922 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2923 | unsigned &StructuredIndex, |
| 2924 | Expr *expr) { |
| 2925 | |
| 2926 | if (!StructuredList) |
| 2927 | return; |
| 2928 | |
| 2929 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2930 | StructuredIndex, expr)) { |
| 2931 | |
| 2932 | |
| 2933 | |
| 2934 | |
| 2935 | |
| 2936 | |
| 2937 | |
| 2938 | if (PrevInit->getSourceRange().isValid()) { |
| 2939 | SemaRef.Diag(expr->getBeginLoc(), diag::warn_initializer_overrides) |
| 2940 | << expr->getSourceRange(); |
| 2941 | |
| 2942 | SemaRef.Diag(PrevInit->getBeginLoc(), diag::note_previous_initializer) |
| 2943 | << << PrevInit->getSourceRange(); |
| 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | ++StructuredIndex; |
| 2948 | } |
| 2949 | |
| 2950 | |
| 2951 | |
| 2952 | |
| 2953 | |
| 2954 | |
| 2955 | |
| 2956 | |
| 2957 | static ExprResult |
| 2958 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
| 2959 | SourceLocation Loc = Index->getBeginLoc(); |
| 2960 | |
| 2961 | |
| 2962 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2963 | if (Result.isInvalid()) |
| 2964 | return Result; |
| 2965 | |
| 2966 | if (Value.isSigned() && Value.isNegative()) |
| 2967 | return S.Diag(Loc, diag::err_array_designator_negative) |
| 2968 | << Value.toString(10) << Index->getSourceRange(); |
| 2969 | |
| 2970 | Value.setIsUnsigned(true); |
| 2971 | return Result; |
| 2972 | } |
| 2973 | |
| 2974 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
| 2975 | SourceLocation Loc, |
| 2976 | bool GNUSyntax, |
| 2977 | ExprResult Init) { |
| 2978 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2979 | |
| 2980 | bool Invalid = false; |
| 2981 | SmallVector<ASTDesignator, 32> Designators; |
| 2982 | SmallVector<Expr *, 32> InitExpressions; |
| 2983 | |
| 2984 | |
| 2985 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2986 | const Designator &D = Desig.getDesignator(Idx); |
| 2987 | switch (D.getKind()) { |
| 2988 | case Designator::FieldDesignator: |
| 2989 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
| 2990 | D.getFieldLoc())); |
| 2991 | break; |
| 2992 | |
| 2993 | case Designator::ArrayDesignator: { |
| 2994 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2995 | llvm::APSInt IndexValue; |
| 2996 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2997 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
| 2998 | if (!Index) |
| 2999 | Invalid = true; |
| 3000 | else { |
| 3001 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
| 3002 | D.getLBracketLoc(), |
| 3003 | D.getRBracketLoc())); |
| 3004 | InitExpressions.push_back(Index); |
| 3005 | } |
| 3006 | break; |
| 3007 | } |
| 3008 | |
| 3009 | case Designator::ArrayRangeDesignator: { |
| 3010 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 3011 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 3012 | llvm::APSInt StartValue; |
| 3013 | llvm::APSInt EndValue; |
| 3014 | bool StartDependent = StartIndex->isTypeDependent() || |
| 3015 | StartIndex->isValueDependent(); |
| 3016 | bool EndDependent = EndIndex->isTypeDependent() || |
| 3017 | EndIndex->isValueDependent(); |
| 3018 | if (!StartDependent) |
| 3019 | StartIndex = |
| 3020 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
| 3021 | if (!EndDependent) |
| 3022 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
| 3023 | |
| 3024 | if (!StartIndex || !EndIndex) |
| 3025 | Invalid = true; |
| 3026 | else { |
| 3027 | |
| 3028 | if (StartDependent || EndDependent) { |
| 3029 | |
| 3030 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
| 3031 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
| 3032 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
| 3033 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
| 3034 | |
| 3035 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
| 3036 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
| 3037 | << StartValue.toString(10) << EndValue.toString(10) |
| 3038 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 3039 | Invalid = true; |
| 3040 | } else { |
| 3041 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
| 3042 | D.getLBracketLoc(), |
| 3043 | D.getEllipsisLoc(), |
| 3044 | D.getRBracketLoc())); |
| 3045 | InitExpressions.push_back(StartIndex); |
| 3046 | InitExpressions.push_back(EndIndex); |
| 3047 | } |
| 3048 | } |
| 3049 | break; |
| 3050 | } |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | if (Invalid || Init.isInvalid()) |
| 3055 | return ExprError(); |
| 3056 | |
| 3057 | |
| 3058 | Desig.ClearExprs(*this); |
| 3059 | |
| 3060 | DesignatedInitExpr *DIE |
| 3061 | = DesignatedInitExpr::Create(Context, |
| 3062 | Designators, |
| 3063 | InitExpressions, Loc, GNUSyntax, |
| 3064 | Init.getAs<Expr>()); |
| 3065 | |
| 3066 | if (!getLangOpts().C99) |
| 3067 | Diag(DIE->getBeginLoc(), diag::ext_designated_init) |
| 3068 | << DIE->getSourceRange(); |
| 3069 | |
| 3070 | return DIE; |
| 3071 | } |
| 3072 | |
| 3073 | |
| 3074 | |
| 3075 | |
| 3076 | |
| 3077 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
| 3078 | const InitializedEntity &Parent) |
| 3079 | : Parent(&Parent), Index(Index) |
| 3080 | { |
| 3081 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 3082 | Kind = EK_ArrayElement; |
| 3083 | Type = AT->getElementType(); |
| 3084 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
| 3085 | Kind = EK_VectorElement; |
| 3086 | Type = VT->getElementType(); |
| 3087 | } else { |
| 3088 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 3089 | (0) . __assert_fail ("CT && \"Unexpected type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3089, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CT && "Unexpected type"); |
| 3090 | Kind = EK_ComplexElement; |
| 3091 | Type = CT->getElementType(); |
| 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | InitializedEntity |
| 3096 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 3097 | const CXXBaseSpecifier *Base, |
| 3098 | bool IsInheritedVirtualBase, |
| 3099 | const InitializedEntity *Parent) { |
| 3100 | InitializedEntity Result; |
| 3101 | Result.Kind = EK_Base; |
| 3102 | Result.Parent = Parent; |
| 3103 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 3104 | if (IsInheritedVirtualBase) |
| 3105 | Result.Base |= 0x01; |
| 3106 | |
| 3107 | Result.Type = Base->getType(); |
| 3108 | return Result; |
| 3109 | } |
| 3110 | |
| 3111 | DeclarationName InitializedEntity::getName() const { |
| 3112 | switch (getKind()) { |
| 3113 | case EK_Parameter: |
| 3114 | case EK_Parameter_CF_Audited: { |
| 3115 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3116 | return (D ? D->getDeclName() : DeclarationName()); |
| 3117 | } |
| 3118 | |
| 3119 | case EK_Variable: |
| 3120 | case EK_Member: |
| 3121 | case EK_Binding: |
| 3122 | return Variable.VariableOrMember->getDeclName(); |
| 3123 | |
| 3124 | case EK_LambdaCapture: |
| 3125 | return DeclarationName(Capture.VarID); |
| 3126 | |
| 3127 | case EK_Result: |
| 3128 | case EK_StmtExprResult: |
| 3129 | case EK_Exception: |
| 3130 | case EK_New: |
| 3131 | case EK_Temporary: |
| 3132 | case EK_Base: |
| 3133 | case EK_Delegating: |
| 3134 | case EK_ArrayElement: |
| 3135 | case EK_VectorElement: |
| 3136 | case EK_ComplexElement: |
| 3137 | case EK_BlockElement: |
| 3138 | case EK_LambdaToBlockConversionBlockElement: |
| 3139 | case EK_CompoundLiteralInit: |
| 3140 | case EK_RelatedResult: |
| 3141 | return DeclarationName(); |
| 3142 | } |
| 3143 | |
| 3144 | llvm_unreachable("Invalid EntityKind!"); |
| 3145 | } |
| 3146 | |
| 3147 | ValueDecl *InitializedEntity::getDecl() const { |
| 3148 | switch (getKind()) { |
| 3149 | case EK_Variable: |
| 3150 | case EK_Member: |
| 3151 | case EK_Binding: |
| 3152 | return Variable.VariableOrMember; |
| 3153 | |
| 3154 | case EK_Parameter: |
| 3155 | case EK_Parameter_CF_Audited: |
| 3156 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 3157 | |
| 3158 | case EK_Result: |
| 3159 | case EK_StmtExprResult: |
| 3160 | case EK_Exception: |
| 3161 | case EK_New: |
| 3162 | case EK_Temporary: |
| 3163 | case EK_Base: |
| 3164 | case EK_Delegating: |
| 3165 | case EK_ArrayElement: |
| 3166 | case EK_VectorElement: |
| 3167 | case EK_ComplexElement: |
| 3168 | case EK_BlockElement: |
| 3169 | case EK_LambdaToBlockConversionBlockElement: |
| 3170 | case EK_LambdaCapture: |
| 3171 | case EK_CompoundLiteralInit: |
| 3172 | case EK_RelatedResult: |
| 3173 | return nullptr; |
| 3174 | } |
| 3175 | |
| 3176 | llvm_unreachable("Invalid EntityKind!"); |
| 3177 | } |
| 3178 | |
| 3179 | bool InitializedEntity::allowsNRVO() const { |
| 3180 | switch (getKind()) { |
| 3181 | case EK_Result: |
| 3182 | case EK_Exception: |
| 3183 | return LocAndNRVO.NRVO; |
| 3184 | |
| 3185 | case EK_StmtExprResult: |
| 3186 | case EK_Variable: |
| 3187 | case EK_Parameter: |
| 3188 | case EK_Parameter_CF_Audited: |
| 3189 | case EK_Member: |
| 3190 | case EK_Binding: |
| 3191 | case EK_New: |
| 3192 | case EK_Temporary: |
| 3193 | case EK_CompoundLiteralInit: |
| 3194 | case EK_Base: |
| 3195 | case EK_Delegating: |
| 3196 | case EK_ArrayElement: |
| 3197 | case EK_VectorElement: |
| 3198 | case EK_ComplexElement: |
| 3199 | case EK_BlockElement: |
| 3200 | case EK_LambdaToBlockConversionBlockElement: |
| 3201 | case EK_LambdaCapture: |
| 3202 | case EK_RelatedResult: |
| 3203 | break; |
| 3204 | } |
| 3205 | |
| 3206 | return false; |
| 3207 | } |
| 3208 | |
| 3209 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
| 3210 | assert(getParent() != this); |
| 3211 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 3212 | for (unsigned I = 0; I != Depth; ++I) |
| 3213 | OS << "`-"; |
| 3214 | |
| 3215 | switch (getKind()) { |
| 3216 | case EK_Variable: OS << "Variable"; break; |
| 3217 | case EK_Parameter: OS << "Parameter"; break; |
| 3218 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 3219 | break; |
| 3220 | case EK_Result: OS << "Result"; break; |
| 3221 | case EK_StmtExprResult: OS << "StmtExprResult"; break; |
| 3222 | case EK_Exception: OS << "Exception"; break; |
| 3223 | case EK_Member: OS << "Member"; break; |
| 3224 | case EK_Binding: OS << "Binding"; break; |
| 3225 | case EK_New: OS << "New"; break; |
| 3226 | case EK_Temporary: OS << "Temporary"; break; |
| 3227 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
| 3228 | case EK_RelatedResult: OS << "RelatedResult"; break; |
| 3229 | case EK_Base: OS << "Base"; break; |
| 3230 | case EK_Delegating: OS << "Delegating"; break; |
| 3231 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 3232 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 3233 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 3234 | case EK_BlockElement: OS << "Block"; break; |
| 3235 | case EK_LambdaToBlockConversionBlockElement: |
| 3236 | OS << "Block (lambda)"; |
| 3237 | break; |
| 3238 | case EK_LambdaCapture: |
| 3239 | OS << "LambdaCapture "; |
| 3240 | OS << DeclarationName(Capture.VarID); |
| 3241 | break; |
| 3242 | } |
| 3243 | |
| 3244 | if (auto *D = getDecl()) { |
| 3245 | OS << " "; |
| 3246 | D->printQualifiedName(OS); |
| 3247 | } |
| 3248 | |
| 3249 | OS << " '" << getType().getAsString() << "'\n"; |
| 3250 | |
| 3251 | return Depth + 1; |
| 3252 | } |
| 3253 | |
| 3254 | LLVM_DUMP_METHOD void InitializedEntity::dump() const { |
| 3255 | dumpImpl(llvm::errs()); |
| 3256 | } |
| 3257 | |
| 3258 | |
| 3259 | |
| 3260 | |
| 3261 | |
| 3262 | void InitializationSequence::Step::Destroy() { |
| 3263 | switch (Kind) { |
| 3264 | case SK_ResolveAddressOfOverloadedFunction: |
| 3265 | case SK_CastDerivedToBaseRValue: |
| 3266 | case SK_CastDerivedToBaseXValue: |
| 3267 | case SK_CastDerivedToBaseLValue: |
| 3268 | case SK_BindReference: |
| 3269 | case SK_BindReferenceToTemporary: |
| 3270 | case SK_FinalCopy: |
| 3271 | case SK_ExtraneousCopyToTemporary: |
| 3272 | case SK_UserConversion: |
| 3273 | case SK_QualificationConversionRValue: |
| 3274 | case SK_QualificationConversionXValue: |
| 3275 | case SK_QualificationConversionLValue: |
| 3276 | case SK_AtomicConversion: |
| 3277 | case SK_LValueToRValue: |
| 3278 | case SK_ListInitialization: |
| 3279 | case SK_UnwrapInitList: |
| 3280 | case SK_RewrapInitList: |
| 3281 | case SK_ConstructorInitialization: |
| 3282 | case SK_ConstructorInitializationFromList: |
| 3283 | case SK_ZeroInitialization: |
| 3284 | case SK_CAssignment: |
| 3285 | case SK_StringInit: |
| 3286 | case SK_ObjCObjectConversion: |
| 3287 | case SK_ArrayLoopIndex: |
| 3288 | case SK_ArrayLoopInit: |
| 3289 | case SK_ArrayInit: |
| 3290 | case SK_GNUArrayInit: |
| 3291 | case SK_ParenthesizedArrayInit: |
| 3292 | case SK_PassByIndirectCopyRestore: |
| 3293 | case SK_PassByIndirectRestore: |
| 3294 | case SK_ProduceObjCObject: |
| 3295 | case SK_StdInitializerList: |
| 3296 | case SK_StdInitializerListConstructorCall: |
| 3297 | case SK_OCLSamplerInit: |
| 3298 | case SK_OCLZeroOpaqueType: |
| 3299 | break; |
| 3300 | |
| 3301 | case SK_ConversionSequence: |
| 3302 | case SK_ConversionSequenceNoNarrowing: |
| 3303 | delete ICS; |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | bool InitializationSequence::isDirectReferenceBinding() const { |
| 3308 | |
| 3309 | for (auto I = Steps.rbegin(); I != Steps.rend(); ++I) { |
| 3310 | if (I->Kind == SK_BindReference) |
| 3311 | return true; |
| 3312 | if (I->Kind == SK_BindReferenceToTemporary) |
| 3313 | return false; |
| 3314 | } |
| 3315 | return false; |
| 3316 | } |
| 3317 | |
| 3318 | bool InitializationSequence::isAmbiguous() const { |
| 3319 | if (!Failed()) |
| 3320 | return false; |
| 3321 | |
| 3322 | switch (getFailureKind()) { |
| 3323 | case FK_TooManyInitsForReference: |
| 3324 | case FK_ParenthesizedListInitForReference: |
| 3325 | case FK_ArrayNeedsInitList: |
| 3326 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 3327 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 3328 | case FK_NarrowStringIntoWideCharArray: |
| 3329 | case FK_WideStringIntoCharArray: |
| 3330 | case FK_IncompatWideStringIntoWideChar: |
| 3331 | case FK_PlainStringIntoUTF8Char: |
| 3332 | case FK_UTF8StringIntoPlainChar: |
| 3333 | case FK_AddressOfOverloadFailed: |
| 3334 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 3335 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 3336 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 3337 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 3338 | case FK_RValueReferenceBindingToLValue: |
| 3339 | case FK_ReferenceInitDropsQualifiers: |
| 3340 | case FK_ReferenceInitFailed: |
| 3341 | case FK_ConversionFailed: |
| 3342 | case FK_ConversionFromPropertyFailed: |
| 3343 | case FK_TooManyInitsForScalar: |
| 3344 | case FK_ParenthesizedListInitForScalar: |
| 3345 | case FK_ReferenceBindingToInitList: |
| 3346 | case FK_InitListBadDestinationType: |
| 3347 | case FK_DefaultInitOfConst: |
| 3348 | case FK_Incomplete: |
| 3349 | case FK_ArrayTypeMismatch: |
| 3350 | case FK_NonConstantArrayInit: |
| 3351 | case FK_ListInitializationFailed: |
| 3352 | case FK_VariableLengthArrayHasInitializer: |
| 3353 | case FK_PlaceholderType: |
| 3354 | case FK_ExplicitConstructor: |
| 3355 | case FK_AddressOfUnaddressableFunction: |
| 3356 | return false; |
| 3357 | |
| 3358 | case FK_ReferenceInitOverloadFailed: |
| 3359 | case FK_UserConversionOverloadFailed: |
| 3360 | case FK_ConstructorOverloadFailed: |
| 3361 | case FK_ListConstructorOverloadFailed: |
| 3362 | return FailedOverloadResult == OR_Ambiguous; |
| 3363 | } |
| 3364 | |
| 3365 | llvm_unreachable("Invalid EntityKind!"); |
| 3366 | } |
| 3367 | |
| 3368 | bool InitializationSequence::isConstructorInitialization() const { |
| 3369 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 3370 | } |
| 3371 | |
| 3372 | void |
| 3373 | InitializationSequence |
| 3374 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 3375 | DeclAccessPair Found, |
| 3376 | bool HadMultipleCandidates) { |
| 3377 | Step S; |
| 3378 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 3379 | S.Type = Function->getType(); |
| 3380 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
| 3381 | S.Function.Function = Function; |
| 3382 | S.Function.FoundDecl = Found; |
| 3383 | Steps.push_back(S); |
| 3384 | } |
| 3385 | |
| 3386 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
| 3387 | ExprValueKind VK) { |
| 3388 | Step S; |
| 3389 | switch (VK) { |
| 3390 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 3391 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 3392 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
| 3393 | } |
| 3394 | S.Type = BaseType; |
| 3395 | Steps.push_back(S); |
| 3396 | } |
| 3397 | |
| 3398 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
| 3399 | bool BindingTemporary) { |
| 3400 | Step S; |
| 3401 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 3402 | S.Type = T; |
| 3403 | Steps.push_back(S); |
| 3404 | } |
| 3405 | |
| 3406 | void InitializationSequence::AddFinalCopy(QualType T) { |
| 3407 | Step S; |
| 3408 | S.Kind = SK_FinalCopy; |
| 3409 | S.Type = T; |
| 3410 | Steps.push_back(S); |
| 3411 | } |
| 3412 | |
| 3413 | void InitializationSequence::(QualType T) { |
| 3414 | Step S; |
| 3415 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 3416 | S.Type = T; |
| 3417 | Steps.push_back(S); |
| 3418 | } |
| 3419 | |
| 3420 | void |
| 3421 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 3422 | DeclAccessPair FoundDecl, |
| 3423 | QualType T, |
| 3424 | bool HadMultipleCandidates) { |
| 3425 | Step S; |
| 3426 | S.Kind = SK_UserConversion; |
| 3427 | S.Type = T; |
| 3428 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
| 3429 | S.Function.Function = Function; |
| 3430 | S.Function.FoundDecl = FoundDecl; |
| 3431 | Steps.push_back(S); |
| 3432 | } |
| 3433 | |
| 3434 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
| 3435 | ExprValueKind VK) { |
| 3436 | Step S; |
| 3437 | S.Kind = SK_QualificationConversionRValue; |
| 3438 | switch (VK) { |
| 3439 | case VK_RValue: |
| 3440 | S.Kind = SK_QualificationConversionRValue; |
| 3441 | break; |
| 3442 | case VK_XValue: |
| 3443 | S.Kind = SK_QualificationConversionXValue; |
| 3444 | break; |
| 3445 | case VK_LValue: |
| 3446 | S.Kind = SK_QualificationConversionLValue; |
| 3447 | break; |
| 3448 | } |
| 3449 | S.Type = Ty; |
| 3450 | Steps.push_back(S); |
| 3451 | } |
| 3452 | |
| 3453 | void InitializationSequence::AddAtomicConversionStep(QualType Ty) { |
| 3454 | Step S; |
| 3455 | S.Kind = SK_AtomicConversion; |
| 3456 | S.Type = Ty; |
| 3457 | Steps.push_back(S); |
| 3458 | } |
| 3459 | |
| 3460 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 3461 | (0) . __assert_fail ("!Ty.hasQualifiers() && \"rvalues may not have qualifiers\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3461, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 3462 | |
| 3463 | Step S; |
| 3464 | S.Kind = SK_LValueToRValue; |
| 3465 | S.Type = Ty; |
| 3466 | Steps.push_back(S); |
| 3467 | } |
| 3468 | |
| 3469 | void InitializationSequence::AddConversionSequenceStep( |
| 3470 | const ImplicitConversionSequence &ICS, QualType T, |
| 3471 | bool TopLevelOfInitList) { |
| 3472 | Step S; |
| 3473 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 3474 | : SK_ConversionSequence; |
| 3475 | S.Type = T; |
| 3476 | S.ICS = new ImplicitConversionSequence(ICS); |
| 3477 | Steps.push_back(S); |
| 3478 | } |
| 3479 | |
| 3480 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 3481 | Step S; |
| 3482 | S.Kind = SK_ListInitialization; |
| 3483 | S.Type = T; |
| 3484 | Steps.push_back(S); |
| 3485 | } |
| 3486 | |
| 3487 | void InitializationSequence::AddConstructorInitializationStep( |
| 3488 | DeclAccessPair FoundDecl, CXXConstructorDecl *Constructor, QualType T, |
| 3489 | bool HadMultipleCandidates, bool FromInitList, bool AsInitList) { |
| 3490 | Step S; |
| 3491 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
| 3492 | : SK_ConstructorInitializationFromList |
| 3493 | : SK_ConstructorInitialization; |
| 3494 | S.Type = T; |
| 3495 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
| 3496 | S.Function.Function = Constructor; |
| 3497 | S.Function.FoundDecl = FoundDecl; |
| 3498 | Steps.push_back(S); |
| 3499 | } |
| 3500 | |
| 3501 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 3502 | Step S; |
| 3503 | S.Kind = SK_ZeroInitialization; |
| 3504 | S.Type = T; |
| 3505 | Steps.push_back(S); |
| 3506 | } |
| 3507 | |
| 3508 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 3509 | Step S; |
| 3510 | S.Kind = SK_CAssignment; |
| 3511 | S.Type = T; |
| 3512 | Steps.push_back(S); |
| 3513 | } |
| 3514 | |
| 3515 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 3516 | Step S; |
| 3517 | S.Kind = SK_StringInit; |
| 3518 | S.Type = T; |
| 3519 | Steps.push_back(S); |
| 3520 | } |
| 3521 | |
| 3522 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 3523 | Step S; |
| 3524 | S.Kind = SK_ObjCObjectConversion; |
| 3525 | S.Type = T; |
| 3526 | Steps.push_back(S); |
| 3527 | } |
| 3528 | |
| 3529 | void InitializationSequence::AddArrayInitStep(QualType T, bool IsGNUExtension) { |
| 3530 | Step S; |
| 3531 | S.Kind = IsGNUExtension ? SK_GNUArrayInit : SK_ArrayInit; |
| 3532 | S.Type = T; |
| 3533 | Steps.push_back(S); |
| 3534 | } |
| 3535 | |
| 3536 | void InitializationSequence::AddArrayInitLoopStep(QualType T, QualType EltT) { |
| 3537 | Step S; |
| 3538 | S.Kind = SK_ArrayLoopIndex; |
| 3539 | S.Type = EltT; |
| 3540 | Steps.insert(Steps.begin(), S); |
| 3541 | |
| 3542 | S.Kind = SK_ArrayLoopInit; |
| 3543 | S.Type = T; |
| 3544 | Steps.push_back(S); |
| 3545 | } |
| 3546 | |
| 3547 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 3548 | Step S; |
| 3549 | S.Kind = SK_ParenthesizedArrayInit; |
| 3550 | S.Type = T; |
| 3551 | Steps.push_back(S); |
| 3552 | } |
| 3553 | |
| 3554 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 3555 | bool shouldCopy) { |
| 3556 | Step s; |
| 3557 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 3558 | : SK_PassByIndirectRestore); |
| 3559 | s.Type = type; |
| 3560 | Steps.push_back(s); |
| 3561 | } |
| 3562 | |
| 3563 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 3564 | Step S; |
| 3565 | S.Kind = SK_ProduceObjCObject; |
| 3566 | S.Type = T; |
| 3567 | Steps.push_back(S); |
| 3568 | } |
| 3569 | |
| 3570 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 3571 | Step S; |
| 3572 | S.Kind = SK_StdInitializerList; |
| 3573 | S.Type = T; |
| 3574 | Steps.push_back(S); |
| 3575 | } |
| 3576 | |
| 3577 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3578 | Step S; |
| 3579 | S.Kind = SK_OCLSamplerInit; |
| 3580 | S.Type = T; |
| 3581 | Steps.push_back(S); |
| 3582 | } |
| 3583 | |
| 3584 | void InitializationSequence::AddOCLZeroOpaqueTypeStep(QualType T) { |
| 3585 | Step S; |
| 3586 | S.Kind = SK_OCLZeroOpaqueType; |
| 3587 | S.Type = T; |
| 3588 | Steps.push_back(S); |
| 3589 | } |
| 3590 | |
| 3591 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3592 | InitListExpr *Syntactic) { |
| 3593 | (0) . __assert_fail ("Syntactic->getNumInits() == 1 && \"Can only rewrap trivial init lists.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3594, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Syntactic->getNumInits() == 1 && |
| 3594 | (0) . __assert_fail ("Syntactic->getNumInits() == 1 && \"Can only rewrap trivial init lists.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3594, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Can only rewrap trivial init lists."); |
| 3595 | Step S; |
| 3596 | S.Kind = SK_UnwrapInitList; |
| 3597 | S.Type = Syntactic->getInit(0)->getType(); |
| 3598 | Steps.insert(Steps.begin(), S); |
| 3599 | |
| 3600 | S.Kind = SK_RewrapInitList; |
| 3601 | S.Type = T; |
| 3602 | S.WrappingSyntacticList = Syntactic; |
| 3603 | Steps.push_back(S); |
| 3604 | } |
| 3605 | |
| 3606 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
| 3607 | OverloadingResult Result) { |
| 3608 | setSequenceKind(FailedSequence); |
| 3609 | this->Failure = Failure; |
| 3610 | this->FailedOverloadResult = Result; |
| 3611 | } |
| 3612 | |
| 3613 | |
| 3614 | |
| 3615 | |
| 3616 | |
| 3617 | |
| 3618 | static bool |
| 3619 | maybeRecoverWithZeroInitialization(Sema &S, InitializationSequence &Sequence, |
| 3620 | const InitializedEntity &Entity) { |
| 3621 | if (Entity.getKind() != InitializedEntity::EK_Variable) |
| 3622 | return false; |
| 3623 | |
| 3624 | VarDecl *VD = cast<VarDecl>(Entity.getDecl()); |
| 3625 | if (VD->getInit() || VD->getEndLoc().isMacroID()) |
| 3626 | return false; |
| 3627 | |
| 3628 | QualType VariableTy = VD->getType().getCanonicalType(); |
| 3629 | SourceLocation Loc = S.getLocForEndOfToken(VD->getEndLoc()); |
| 3630 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 3631 | if (!Init.empty()) { |
| 3632 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3633 | Sequence.SetZeroInitializationFixit(Init, Loc); |
| 3634 | return true; |
| 3635 | } |
| 3636 | return false; |
| 3637 | } |
| 3638 | |
| 3639 | static void MaybeProduceObjCObject(Sema &S, |
| 3640 | InitializationSequence &Sequence, |
| 3641 | const InitializedEntity &Entity) { |
| 3642 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
| 3643 | |
| 3644 | |
| 3645 | |
| 3646 | if (Entity.isParameterKind()) { |
| 3647 | if (!Entity.isParameterConsumed()) |
| 3648 | return; |
| 3649 | |
| 3650 | (0) . __assert_fail ("Entity.getType()->isObjCRetainableType() && \"consuming an object of unretainable type?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3651, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Entity.getType()->isObjCRetainableType() && |
| 3651 | (0) . __assert_fail ("Entity.getType()->isObjCRetainableType() && \"consuming an object of unretainable type?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3651, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "consuming an object of unretainable type?"); |
| 3652 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3653 | |
| 3654 | |
| 3655 | |
| 3656 | |
| 3657 | |
| 3658 | } else if (Entity.getKind() == InitializedEntity::EK_Result || |
| 3659 | Entity.getKind() == InitializedEntity::EK_StmtExprResult) { |
| 3660 | if (!Entity.getType()->isObjCRetainableType()) |
| 3661 | return; |
| 3662 | |
| 3663 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3664 | } |
| 3665 | } |
| 3666 | |
| 3667 | static void TryListInitialization(Sema &S, |
| 3668 | const InitializedEntity &Entity, |
| 3669 | const InitializationKind &Kind, |
| 3670 | InitListExpr *InitList, |
| 3671 | InitializationSequence &Sequence, |
| 3672 | bool TreatUnavailableAsInvalid); |
| 3673 | |
| 3674 | |
| 3675 | |
| 3676 | |
| 3677 | |
| 3678 | |
| 3679 | static bool TryInitializerListConstruction(Sema &S, |
| 3680 | InitListExpr *List, |
| 3681 | QualType DestType, |
| 3682 | InitializationSequence &Sequence, |
| 3683 | bool TreatUnavailableAsInvalid) { |
| 3684 | QualType E; |
| 3685 | if (!S.isStdInitializerList(DestType, &E)) |
| 3686 | return false; |
| 3687 | |
| 3688 | if (!S.isCompleteType(List->getExprLoc(), E)) { |
| 3689 | Sequence.setIncompleteTypeFailure(E); |
| 3690 | return true; |
| 3691 | } |
| 3692 | |
| 3693 | |
| 3694 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3695 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3696 | List->getNumInits()), |
| 3697 | clang::ArrayType::Normal, 0); |
| 3698 | InitializedEntity HiddenArray = |
| 3699 | InitializedEntity::InitializeTemporary(ArrayType); |
| 3700 | InitializationKind Kind = InitializationKind::CreateDirectList( |
| 3701 | List->getExprLoc(), List->getBeginLoc(), List->getEndLoc()); |
| 3702 | TryListInitialization(S, HiddenArray, Kind, List, Sequence, |
| 3703 | TreatUnavailableAsInvalid); |
| 3704 | if (Sequence) |
| 3705 | Sequence.AddStdInitializerListConstructionStep(DestType); |
| 3706 | return true; |
| 3707 | } |
| 3708 | |
| 3709 | |
| 3710 | |
| 3711 | |
| 3712 | |
| 3713 | static bool hasCopyOrMoveCtorParam(ASTContext &Ctx, |
| 3714 | const ConstructorInfo &Info) { |
| 3715 | if (Info.Constructor->getNumParams() == 0) |
| 3716 | return false; |
| 3717 | |
| 3718 | QualType ParmT = |
| 3719 | Info.Constructor->getParamDecl(0)->getType().getNonReferenceType(); |
| 3720 | QualType ClassT = |
| 3721 | Ctx.getRecordType(cast<CXXRecordDecl>(Info.FoundDecl->getDeclContext())); |
| 3722 | |
| 3723 | return Ctx.hasSameUnqualifiedType(ParmT, ClassT); |
| 3724 | } |
| 3725 | |
| 3726 | static OverloadingResult |
| 3727 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
| 3728 | MultiExprArg Args, |
| 3729 | OverloadCandidateSet &CandidateSet, |
| 3730 | QualType DestType, |
| 3731 | DeclContext::lookup_result Ctors, |
| 3732 | OverloadCandidateSet::iterator &Best, |
| 3733 | bool CopyInitializing, bool AllowExplicit, |
| 3734 | bool OnlyListConstructors, bool IsListInit, |
| 3735 | bool SecondStepOfCopyInit = false) { |
| 3736 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByConstructor); |
| 3737 | |
| 3738 | for (NamedDecl *D : Ctors) { |
| 3739 | auto Info = getConstructorInfo(D); |
| 3740 | if (!Info.Constructor || Info.Constructor->isInvalidDecl()) |
| 3741 | continue; |
| 3742 | |
| 3743 | if (!AllowExplicit && Info.Constructor->isExplicit()) |
| 3744 | continue; |
| 3745 | |
| 3746 | if (OnlyListConstructors && !S.isInitListConstructor(Info.Constructor)) |
| 3747 | continue; |
| 3748 | |
| 3749 | |
| 3750 | |
| 3751 | |
| 3752 | |
| 3753 | |
| 3754 | |
| 3755 | |
| 3756 | |
| 3757 | |
| 3758 | |
| 3759 | |
| 3760 | bool SuppressUserConversions = |
| 3761 | SecondStepOfCopyInit || |
| 3762 | (IsListInit && Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
| 3763 | hasCopyOrMoveCtorParam(S.Context, Info)); |
| 3764 | |
| 3765 | if (Info.ConstructorTmpl) |
| 3766 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
| 3767 | nullptr, Args, |
| 3768 | CandidateSet, SuppressUserConversions); |
| 3769 | else { |
| 3770 | |
| 3771 | |
| 3772 | |
| 3773 | |
| 3774 | |
| 3775 | |
| 3776 | |
| 3777 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
| 3778 | Args.size() == 1 && |
| 3779 | hasCopyOrMoveCtorParam(S.Context, Info); |
| 3780 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, Args, |
| 3781 | CandidateSet, SuppressUserConversions, |
| 3782 | , |
| 3783 | AllowExplicitConv); |
| 3784 | } |
| 3785 | } |
| 3786 | |
| 3787 | |
| 3788 | |
| 3789 | |
| 3790 | |
| 3791 | |
| 3792 | |
| 3793 | |
| 3794 | |
| 3795 | |
| 3796 | |
| 3797 | |
| 3798 | if (S.getLangOpts().CPlusPlus17 && Args.size() == 1 && |
| 3799 | !SecondStepOfCopyInit) { |
| 3800 | Expr *Initializer = Args[0]; |
| 3801 | auto *SourceRD = Initializer->getType()->getAsCXXRecordDecl(); |
| 3802 | if (SourceRD && S.isCompleteType(DeclLoc, Initializer->getType())) { |
| 3803 | const auto &Conversions = SourceRD->getVisibleConversionFunctions(); |
| 3804 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 3805 | NamedDecl *D = *I; |
| 3806 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3807 | D = D->getUnderlyingDecl(); |
| 3808 | |
| 3809 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3810 | CXXConversionDecl *Conv; |
| 3811 | if (ConvTemplate) |
| 3812 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3813 | else |
| 3814 | Conv = cast<CXXConversionDecl>(D); |
| 3815 | |
| 3816 | if ((AllowExplicit && !CopyInitializing) || !Conv->isExplicit()) { |
| 3817 | if (ConvTemplate) |
| 3818 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
| 3819 | ActingDC, Initializer, DestType, |
| 3820 | CandidateSet, AllowExplicit, |
| 3821 | ); |
| 3822 | else |
| 3823 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer, |
| 3824 | DestType, CandidateSet, AllowExplicit, |
| 3825 | ); |
| 3826 | } |
| 3827 | } |
| 3828 | } |
| 3829 | } |
| 3830 | |
| 3831 | |
| 3832 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3833 | } |
| 3834 | |
| 3835 | |
| 3836 | |
| 3837 | |
| 3838 | |
| 3839 | |
| 3840 | |
| 3841 | |
| 3842 | |
| 3843 | |
| 3844 | |
| 3845 | static void TryConstructorInitialization(Sema &S, |
| 3846 | const InitializedEntity &Entity, |
| 3847 | const InitializationKind &Kind, |
| 3848 | MultiExprArg Args, QualType DestType, |
| 3849 | QualType DestArrayType, |
| 3850 | InitializationSequence &Sequence, |
| 3851 | bool IsListInit = false, |
| 3852 | bool IsInitListCopy = false) { |
| 3853 | (0) . __assert_fail ("((!IsListInit && !IsInitListCopy) || (Args.size() == 1 && isa(Args[0]))) && \"IsListInit/IsInitListCopy must come with a single initializer list \" \"argument.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3856, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(((!IsListInit && !IsInitListCopy) || |
| 3854 | (0) . __assert_fail ("((!IsListInit && !IsInitListCopy) || (Args.size() == 1 && isa(Args[0]))) && \"IsListInit/IsInitListCopy must come with a single initializer list \" \"argument.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3856, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
| 3855 | (0) . __assert_fail ("((!IsListInit && !IsInitListCopy) || (Args.size() == 1 && isa(Args[0]))) && \"IsListInit/IsInitListCopy must come with a single initializer list \" \"argument.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3856, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "IsListInit/IsInitListCopy must come with a single initializer list " |
| 3856 | (0) . __assert_fail ("((!IsListInit && !IsInitListCopy) || (Args.size() == 1 && isa(Args[0]))) && \"IsListInit/IsInitListCopy must come with a single initializer list \" \"argument.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3856, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "argument."); |
| 3857 | InitListExpr *ILE = |
| 3858 | (IsListInit || IsInitListCopy) ? cast<InitListExpr>(Args[0]) : nullptr; |
| 3859 | MultiExprArg UnwrappedArgs = |
| 3860 | ILE ? MultiExprArg(ILE->getInits(), ILE->getNumInits()) : Args; |
| 3861 | |
| 3862 | |
| 3863 | if (!S.isCompleteType(Kind.getLocation(), DestType)) { |
| 3864 | Sequence.setIncompleteTypeFailure(DestType); |
| 3865 | return; |
| 3866 | } |
| 3867 | |
| 3868 | |
| 3869 | |
| 3870 | |
| 3871 | |
| 3872 | |
| 3873 | |
| 3874 | |
| 3875 | |
| 3876 | |
| 3877 | if (S.getLangOpts().CPlusPlus17 && |
| 3878 | Entity.getKind() != InitializedEntity::EK_Base && |
| 3879 | Entity.getKind() != InitializedEntity::EK_Delegating && |
| 3880 | Entity.getKind() != |
| 3881 | InitializedEntity::EK_LambdaToBlockConversionBlockElement && |
| 3882 | UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isRValue() && |
| 3883 | S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { |
| 3884 | |
| 3885 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 3886 | if (ILE) |
| 3887 | Sequence.RewrapReferenceInitList(DestType, ILE); |
| 3888 | return; |
| 3889 | } |
| 3890 | |
| 3891 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3892 | (0) . __assert_fail ("DestRecordType && \"Constructor initialization requires record type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3892, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DestRecordType && "Constructor initialization requires record type"); |
| 3893 | CXXRecordDecl *DestRecordDecl |
| 3894 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3895 | |
| 3896 | |
| 3897 | |
| 3898 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3899 | |
| 3900 | |
| 3901 | |
| 3902 | bool AllowExplicit = Kind.AllowExplicit() || IsListInit; |
| 3903 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
| 3904 | |
| 3905 | |
| 3906 | |
| 3907 | |
| 3908 | DeclContext::lookup_result Ctors = S.LookupConstructors(DestRecordDecl); |
| 3909 | |
| 3910 | OverloadingResult Result = OR_No_Viable_Function; |
| 3911 | OverloadCandidateSet::iterator Best; |
| 3912 | bool AsInitializerList = false; |
| 3913 | |
| 3914 | |
| 3915 | |
| 3916 | |
| 3917 | |
| 3918 | |
| 3919 | |
| 3920 | |
| 3921 | |
| 3922 | |
| 3923 | if (IsListInit) { |
| 3924 | AsInitializerList = true; |
| 3925 | |
| 3926 | |
| 3927 | |
| 3928 | if (!(UnwrappedArgs.empty() && DestRecordDecl->hasDefaultConstructor())) |
| 3929 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
| 3930 | CandidateSet, DestType, Ctors, Best, |
| 3931 | CopyInitialization, AllowExplicit, |
| 3932 | , |
| 3933 | IsListInit); |
| 3934 | } |
| 3935 | |
| 3936 | |
| 3937 | |
| 3938 | |
| 3939 | |
| 3940 | |
| 3941 | if (Result == OR_No_Viable_Function) { |
| 3942 | AsInitializerList = false; |
| 3943 | Result = ResolveConstructorOverload(S, Kind.getLocation(), UnwrappedArgs, |
| 3944 | CandidateSet, DestType, Ctors, Best, |
| 3945 | CopyInitialization, AllowExplicit, |
| 3946 | , |
| 3947 | IsListInit); |
| 3948 | } |
| 3949 | if (Result) { |
| 3950 | Sequence.SetOverloadFailure(IsListInit ? |
| 3951 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3952 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 3953 | Result); |
| 3954 | return; |
| 3955 | } |
| 3956 | |
| 3957 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3958 | |
| 3959 | |
| 3960 | |
| 3961 | if (auto *CD = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3962 | |
| 3963 | QualType ConvType = CD->getConversionType(); |
| 3964 | (0) . __assert_fail ("S.Context.hasSameUnqualifiedType(ConvType, DestType) && \"should not have selected this conversion function\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3965, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(S.Context.hasSameUnqualifiedType(ConvType, DestType) && |
| 3965 | (0) . __assert_fail ("S.Context.hasSameUnqualifiedType(ConvType, DestType) && \"should not have selected this conversion function\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 3965, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "should not have selected this conversion function"); |
| 3966 | Sequence.AddUserConversionStep(CD, Best->FoundDecl, ConvType, |
| 3967 | HadMultipleCandidates); |
| 3968 | if (!S.Context.hasSameType(ConvType, DestType)) |
| 3969 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 3970 | if (IsListInit) |
| 3971 | Sequence.RewrapReferenceInitList(Entity.getType(), ILE); |
| 3972 | return; |
| 3973 | } |
| 3974 | |
| 3975 | |
| 3976 | |
| 3977 | |
| 3978 | |
| 3979 | |
| 3980 | |
| 3981 | |
| 3982 | |
| 3983 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 3984 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3985 | Entity.getType().isConstQualified()) { |
| 3986 | if (!CtorDecl->getParent()->allowConstDefaultInit()) { |
| 3987 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 3988 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3989 | return; |
| 3990 | } |
| 3991 | } |
| 3992 | |
| 3993 | |
| 3994 | |
| 3995 | |
| 3996 | if (IsListInit && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 3997 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3998 | return; |
| 3999 | } |
| 4000 | |
| 4001 | |
| 4002 | |
| 4003 | Sequence.AddConstructorInitializationStep( |
| 4004 | Best->FoundDecl, CtorDecl, DestArrayType, HadMultipleCandidates, |
| 4005 | IsListInit | IsInitListCopy, AsInitializerList); |
| 4006 | } |
| 4007 | |
| 4008 | static bool |
| 4009 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 4010 | Expr *Initializer, |
| 4011 | QualType &SourceType, |
| 4012 | QualType &UnqualifiedSourceType, |
| 4013 | QualType UnqualifiedTargetType, |
| 4014 | InitializationSequence &Sequence) { |
| 4015 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 4016 | S.Context.OverloadTy) { |
| 4017 | DeclAccessPair Found; |
| 4018 | bool HadMultipleCandidates = false; |
| 4019 | if (FunctionDecl *Fn |
| 4020 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 4021 | UnqualifiedTargetType, |
| 4022 | false, Found, |
| 4023 | &HadMultipleCandidates)) { |
| 4024 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 4025 | HadMultipleCandidates); |
| 4026 | SourceType = Fn->getType(); |
| 4027 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 4028 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 4029 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4030 | return true; |
| 4031 | } |
| 4032 | } |
| 4033 | return false; |
| 4034 | } |
| 4035 | |
| 4036 | static void TryReferenceInitializationCore(Sema &S, |
| 4037 | const InitializedEntity &Entity, |
| 4038 | const InitializationKind &Kind, |
| 4039 | Expr *Initializer, |
| 4040 | QualType cv1T1, QualType T1, |
| 4041 | Qualifiers T1Quals, |
| 4042 | QualType cv2T2, QualType T2, |
| 4043 | Qualifiers T2Quals, |
| 4044 | InitializationSequence &Sequence); |
| 4045 | |
| 4046 | static void TryValueInitialization(Sema &S, |
| 4047 | const InitializedEntity &Entity, |
| 4048 | const InitializationKind &Kind, |
| 4049 | InitializationSequence &Sequence, |
| 4050 | InitListExpr *InitList = nullptr); |
| 4051 | |
| 4052 | |
| 4053 | static void TryReferenceListInitialization(Sema &S, |
| 4054 | const InitializedEntity &Entity, |
| 4055 | const InitializationKind &Kind, |
| 4056 | InitListExpr *InitList, |
| 4057 | InitializationSequence &Sequence, |
| 4058 | bool TreatUnavailableAsInvalid) { |
| 4059 | |
| 4060 | if (!S.getLangOpts().CPlusPlus11) { |
| 4061 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4062 | return; |
| 4063 | } |
| 4064 | |
| 4065 | if (Entity.getKind() == InitializedEntity::EK_CompoundLiteralInit) { |
| 4066 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 4067 | return; |
| 4068 | } |
| 4069 | |
| 4070 | QualType DestType = Entity.getType(); |
| 4071 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4072 | Qualifiers T1Quals; |
| 4073 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 4074 | |
| 4075 | |
| 4076 | |
| 4077 | |
| 4078 | |
| 4079 | |
| 4080 | |
| 4081 | if (InitList->getNumInits() == 1) { |
| 4082 | Expr *Initializer = InitList->getInit(0); |
| 4083 | QualType cv2T2 = Initializer->getType(); |
| 4084 | Qualifiers T2Quals; |
| 4085 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 4086 | |
| 4087 | |
| 4088 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4089 | T1, Sequence)) |
| 4090 | return; |
| 4091 | |
| 4092 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
| 4093 | bool dummy1, dummy2, dummy3; |
| 4094 | Sema::ReferenceCompareResult RefRelationship |
| 4095 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 4096 | dummy2, dummy3); |
| 4097 | if (RefRelationship >= Sema::Ref_Related) { |
| 4098 | |
| 4099 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4100 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4101 | if (Sequence) |
| 4102 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 4103 | return; |
| 4104 | } |
| 4105 | |
| 4106 | |
| 4107 | if (Sequence.step_begin() != Sequence.step_end()) |
| 4108 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 4109 | } |
| 4110 | |
| 4111 | |
| 4112 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4113 | |
| 4114 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence, |
| 4115 | TreatUnavailableAsInvalid); |
| 4116 | if (Sequence) { |
| 4117 | if (DestType->isRValueReferenceType() || |
| 4118 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 4119 | Sequence.AddReferenceBindingStep(cv1T1, ); |
| 4120 | else |
| 4121 | Sequence.SetFailed( |
| 4122 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4123 | } |
| 4124 | } |
| 4125 | |
| 4126 | |
| 4127 | static void TryListInitialization(Sema &S, |
| 4128 | const InitializedEntity &Entity, |
| 4129 | const InitializationKind &Kind, |
| 4130 | InitListExpr *InitList, |
| 4131 | InitializationSequence &Sequence, |
| 4132 | bool TreatUnavailableAsInvalid) { |
| 4133 | QualType DestType = Entity.getType(); |
| 4134 | |
| 4135 | |
| 4136 | |
| 4137 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
| 4138 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 4139 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 4140 | return; |
| 4141 | } |
| 4142 | if (DestType->isReferenceType()) { |
| 4143 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence, |
| 4144 | TreatUnavailableAsInvalid); |
| 4145 | return; |
| 4146 | } |
| 4147 | |
| 4148 | if (DestType->isRecordType() && |
| 4149 | !S.isCompleteType(InitList->getBeginLoc(), DestType)) { |
| 4150 | Sequence.setIncompleteTypeFailure(DestType); |
| 4151 | return; |
| 4152 | } |
| 4153 | |
| 4154 | |
| 4155 | |
| 4156 | |
| 4157 | |
| 4158 | |
| 4159 | |
| 4160 | |
| 4161 | |
| 4162 | |
| 4163 | |
| 4164 | |
| 4165 | if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1) { |
| 4166 | if (DestType->isRecordType()) { |
| 4167 | QualType InitType = InitList->getInit(0)->getType(); |
| 4168 | if (S.Context.hasSameUnqualifiedType(InitType, DestType) || |
| 4169 | S.IsDerivedFrom(InitList->getBeginLoc(), InitType, DestType)) { |
| 4170 | Expr *InitListAsExpr = InitList; |
| 4171 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
| 4172 | DestType, Sequence, |
| 4173 | , |
| 4174 | ); |
| 4175 | return; |
| 4176 | } |
| 4177 | } |
| 4178 | if (const ArrayType *DestAT = S.Context.getAsArrayType(DestType)) { |
| 4179 | Expr *SubInit[1] = {InitList->getInit(0)}; |
| 4180 | if (!isa<VariableArrayType>(DestAT) && |
| 4181 | IsStringInit(SubInit[0], DestAT, S.Context) == SIF_None) { |
| 4182 | InitializationKind SubKind = |
| 4183 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4184 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4185 | InitList->getLBraceLoc(), |
| 4186 | InitList->getRBraceLoc()) |
| 4187 | : Kind; |
| 4188 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 4189 | true, |
| 4190 | TreatUnavailableAsInvalid); |
| 4191 | |
| 4192 | |
| 4193 | |
| 4194 | |
| 4195 | if (Sequence) { |
| 4196 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4197 | return; |
| 4198 | } |
| 4199 | } |
| 4200 | } |
| 4201 | } |
| 4202 | |
| 4203 | |
| 4204 | |
| 4205 | if ((DestType->isRecordType() && !DestType->isAggregateType()) || |
| 4206 | (S.getLangOpts().CPlusPlus11 && |
| 4207 | S.isStdInitializerList(DestType, nullptr))) { |
| 4208 | if (S.getLangOpts().CPlusPlus11) { |
| 4209 | |
| 4210 | |
| 4211 | |
| 4212 | if (InitList->getNumInits() == 0) { |
| 4213 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 4214 | if (RD->hasDefaultConstructor()) { |
| 4215 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 4216 | return; |
| 4217 | } |
| 4218 | } |
| 4219 | |
| 4220 | |
| 4221 | |
| 4222 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence, |
| 4223 | TreatUnavailableAsInvalid)) |
| 4224 | return; |
| 4225 | |
| 4226 | |
| 4227 | Expr *InitListAsExpr = InitList; |
| 4228 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
| 4229 | DestType, Sequence, ); |
| 4230 | } else |
| 4231 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 4232 | return; |
| 4233 | } |
| 4234 | |
| 4235 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
| 4236 | InitList->getNumInits() == 1) { |
| 4237 | Expr *E = InitList->getInit(0); |
| 4238 | |
| 4239 | |
| 4240 | |
| 4241 | |
| 4242 | |
| 4243 | |
| 4244 | auto *ET = DestType->getAs<EnumType>(); |
| 4245 | if (S.getLangOpts().CPlusPlus17 && |
| 4246 | Kind.getKind() == InitializationKind::IK_DirectList && |
| 4247 | ET && ET->getDecl()->isFixed() && |
| 4248 | !S.Context.hasSameUnqualifiedType(E->getType(), DestType) && |
| 4249 | (E->getType()->isIntegralOrEnumerationType() || |
| 4250 | E->getType()->isFloatingType())) { |
| 4251 | |
| 4252 | |
| 4253 | |
| 4254 | |
| 4255 | |
| 4256 | |
| 4257 | |
| 4258 | |
| 4259 | ImplicitConversionSequence ICS; |
| 4260 | ICS.setStandard(); |
| 4261 | ICS.Standard.setAsIdentityConversion(); |
| 4262 | if (!E->isRValue()) |
| 4263 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4264 | |
| 4265 | |
| 4266 | |
| 4267 | ICS.Standard.Second = E->getType()->isFloatingType() |
| 4268 | ? ICK_Floating_Integral |
| 4269 | : ICK_Integral_Conversion; |
| 4270 | ICS.Standard.setFromType(E->getType()); |
| 4271 | ICS.Standard.setToType(0, E->getType()); |
| 4272 | ICS.Standard.setToType(1, DestType); |
| 4273 | ICS.Standard.setToType(2, DestType); |
| 4274 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2), |
| 4275 | ); |
| 4276 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4277 | return; |
| 4278 | } |
| 4279 | |
| 4280 | |
| 4281 | |
| 4282 | |
| 4283 | |
| 4284 | |
| 4285 | |
| 4286 | |
| 4287 | |
| 4288 | |
| 4289 | |
| 4290 | |
| 4291 | |
| 4292 | |
| 4293 | if (InitList->getInit(0)->getType()->isRecordType()) { |
| 4294 | InitializationKind SubKind = |
| 4295 | Kind.getKind() == InitializationKind::IK_DirectList |
| 4296 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 4297 | InitList->getLBraceLoc(), |
| 4298 | InitList->getRBraceLoc()) |
| 4299 | : Kind; |
| 4300 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 4301 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 4302 | , |
| 4303 | TreatUnavailableAsInvalid); |
| 4304 | if (Sequence) |
| 4305 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 4306 | return; |
| 4307 | } |
| 4308 | } |
| 4309 | |
| 4310 | InitListChecker CheckInitList(S, Entity, InitList, |
| 4311 | DestType, , TreatUnavailableAsInvalid); |
| 4312 | if (CheckInitList.HadError()) { |
| 4313 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 4314 | return; |
| 4315 | } |
| 4316 | |
| 4317 | |
| 4318 | Sequence.AddListInitializationStep(DestType); |
| 4319 | } |
| 4320 | |
| 4321 | |
| 4322 | |
| 4323 | static OverloadingResult TryRefInitWithConversionFunction( |
| 4324 | Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, |
| 4325 | Expr *Initializer, bool AllowRValues, bool IsLValueRef, |
| 4326 | InitializationSequence &Sequence) { |
| 4327 | QualType DestType = Entity.getType(); |
| 4328 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4329 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 4330 | QualType cv2T2 = Initializer->getType(); |
| 4331 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 4332 | |
| 4333 | bool DerivedToBase; |
| 4334 | bool ObjCConversion; |
| 4335 | bool ObjCLifetimeConversion; |
| 4336 | (0) . __assert_fail ("!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, DerivedToBase, ObjCConversion, ObjCLifetimeConversion) && \"Must have incompatible references when binding via conversion\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4339, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, |
| 4337 | (0) . __assert_fail ("!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, DerivedToBase, ObjCConversion, ObjCLifetimeConversion) && \"Must have incompatible references when binding via conversion\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4339, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> DerivedToBase, ObjCConversion, |
| 4338 | (0) . __assert_fail ("!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, DerivedToBase, ObjCConversion, ObjCLifetimeConversion) && \"Must have incompatible references when binding via conversion\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4339, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ObjCLifetimeConversion) && |
| 4339 | (0) . __assert_fail ("!S.CompareReferenceRelationship(Initializer->getBeginLoc(), T1, T2, DerivedToBase, ObjCConversion, ObjCLifetimeConversion) && \"Must have incompatible references when binding via conversion\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4339, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Must have incompatible references when binding via conversion"); |
| 4340 | (void)DerivedToBase; |
| 4341 | (void)ObjCConversion; |
| 4342 | (void)ObjCLifetimeConversion; |
| 4343 | |
| 4344 | |
| 4345 | |
| 4346 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 4347 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
| 4348 | |
| 4349 | |
| 4350 | |
| 4351 | |
| 4352 | |
| 4353 | bool AllowExplicitCtors = false; |
| 4354 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 4355 | |
| 4356 | const RecordType *T1RecordType = nullptr; |
| 4357 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 4358 | S.isCompleteType(Kind.getLocation(), T1)) { |
| 4359 | |
| 4360 | |
| 4361 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
| 4362 | |
| 4363 | for (NamedDecl *D : S.LookupConstructors(T1RecordDecl)) { |
| 4364 | auto Info = getConstructorInfo(D); |
| 4365 | if (!Info.Constructor) |
| 4366 | continue; |
| 4367 | |
| 4368 | if (!Info.Constructor->isInvalidDecl() && |
| 4369 | Info.Constructor->isConvertingConstructor(AllowExplicitCtors)) { |
| 4370 | if (Info.ConstructorTmpl) |
| 4371 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
| 4372 | nullptr, |
| 4373 | Initializer, CandidateSet, |
| 4374 | ); |
| 4375 | else |
| 4376 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
| 4377 | Initializer, CandidateSet, |
| 4378 | ); |
| 4379 | } |
| 4380 | } |
| 4381 | } |
| 4382 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 4383 | return OR_No_Viable_Function; |
| 4384 | |
| 4385 | const RecordType *T2RecordType = nullptr; |
| 4386 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 4387 | S.isCompleteType(Kind.getLocation(), T2)) { |
| 4388 | |
| 4389 | |
| 4390 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 4391 | |
| 4392 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4393 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 4394 | NamedDecl *D = *I; |
| 4395 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4396 | if (isa<UsingShadowDecl>(D)) |
| 4397 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4398 | |
| 4399 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4400 | CXXConversionDecl *Conv; |
| 4401 | if (ConvTemplate) |
| 4402 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4403 | else |
| 4404 | Conv = cast<CXXConversionDecl>(D); |
| 4405 | |
| 4406 | |
| 4407 | |
| 4408 | |
| 4409 | |
| 4410 | |
| 4411 | |
| 4412 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
| 4413 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 4414 | if (ConvTemplate) |
| 4415 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
| 4416 | ActingDC, Initializer, |
| 4417 | DestType, CandidateSet, |
| 4418 | |
| 4419 | false); |
| 4420 | else |
| 4421 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
| 4422 | Initializer, DestType, CandidateSet, |
| 4423 | ); |
| 4424 | } |
| 4425 | } |
| 4426 | } |
| 4427 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 4428 | return OR_No_Viable_Function; |
| 4429 | |
| 4430 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
| 4431 | |
| 4432 | |
| 4433 | OverloadCandidateSet::iterator Best; |
| 4434 | if (OverloadingResult Result |
| 4435 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) |
| 4436 | return Result; |
| 4437 | |
| 4438 | FunctionDecl *Function = Best->Function; |
| 4439 | |
| 4440 | |
| 4441 | Function->setReferenced(); |
| 4442 | |
| 4443 | |
| 4444 | QualType cv3T3; |
| 4445 | if (isa<CXXConversionDecl>(Function)) |
| 4446 | cv3T3 = Function->getReturnType(); |
| 4447 | else |
| 4448 | cv3T3 = T1; |
| 4449 | |
| 4450 | ExprValueKind VK = VK_RValue; |
| 4451 | if (cv3T3->isLValueReferenceType()) |
| 4452 | VK = VK_LValue; |
| 4453 | else if (const auto *RRef = cv3T3->getAs<RValueReferenceType>()) |
| 4454 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
| 4455 | cv3T3 = cv3T3.getNonLValueExprType(S.Context); |
| 4456 | |
| 4457 | |
| 4458 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4459 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, cv3T3, |
| 4460 | HadMultipleCandidates); |
| 4461 | |
| 4462 | |
| 4463 | |
| 4464 | bool NewDerivedToBase = false; |
| 4465 | bool NewObjCConversion = false; |
| 4466 | bool NewObjCLifetimeConversion = false; |
| 4467 | Sema::ReferenceCompareResult NewRefRelationship |
| 4468 | = S.CompareReferenceRelationship(DeclLoc, T1, cv3T3, |
| 4469 | NewDerivedToBase, NewObjCConversion, |
| 4470 | NewObjCLifetimeConversion); |
| 4471 | |
| 4472 | |
| 4473 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 4474 | (0) . __assert_fail ("!isa(Function) && \"should not have conversion after constructor\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4475, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!isa<CXXConstructorDecl>(Function) && |
| 4475 | (0) . __assert_fail ("!isa(Function) && \"should not have conversion after constructor\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4475, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "should not have conversion after constructor"); |
| 4476 | |
| 4477 | ImplicitConversionSequence ICS; |
| 4478 | ICS.setStandard(); |
| 4479 | ICS.Standard = Best->FinalConversion; |
| 4480 | Sequence.AddConversionSequenceStep(ICS, ICS.Standard.getToType(2)); |
| 4481 | |
| 4482 | |
| 4483 | |
| 4484 | cv3T3 = ICS.Standard.getToType(2); |
| 4485 | VK = VK_RValue; |
| 4486 | } |
| 4487 | |
| 4488 | |
| 4489 | |
| 4490 | |
| 4491 | |
| 4492 | |
| 4493 | |
| 4494 | QualType cv1T4 = S.Context.getQualifiedType(cv3T3, cv1T1.getQualifiers()); |
| 4495 | if (cv1T4.getQualifiers() != cv3T3.getQualifiers()) |
| 4496 | Sequence.AddQualificationConversionStep(cv1T4, VK); |
| 4497 | Sequence.AddReferenceBindingStep(cv1T4, VK == VK_RValue); |
| 4498 | VK = IsLValueRef ? VK_LValue : VK_XValue; |
| 4499 | |
| 4500 | if (NewDerivedToBase) |
| 4501 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK); |
| 4502 | else if (NewObjCConversion) |
| 4503 | Sequence.AddObjCObjectConversionStep(cv1T1); |
| 4504 | |
| 4505 | return OR_Success; |
| 4506 | } |
| 4507 | |
| 4508 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4509 | const InitializedEntity &Entity, |
| 4510 | Expr *CurInitExpr); |
| 4511 | |
| 4512 | |
| 4513 | static void TryReferenceInitialization(Sema &S, |
| 4514 | const InitializedEntity &Entity, |
| 4515 | const InitializationKind &Kind, |
| 4516 | Expr *Initializer, |
| 4517 | InitializationSequence &Sequence) { |
| 4518 | QualType DestType = Entity.getType(); |
| 4519 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 4520 | Qualifiers T1Quals; |
| 4521 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 4522 | QualType cv2T2 = Initializer->getType(); |
| 4523 | Qualifiers T2Quals; |
| 4524 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 4525 | |
| 4526 | |
| 4527 | |
| 4528 | |
| 4529 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 4530 | T1, Sequence)) |
| 4531 | return; |
| 4532 | |
| 4533 | |
| 4534 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 4535 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 4536 | } |
| 4537 | |
| 4538 | |
| 4539 | |
| 4540 | |
| 4541 | static bool isNonReferenceableGLValue(Expr *E) { |
| 4542 | return E->refersToBitField() || E->refersToVectorElement(); |
| 4543 | } |
| 4544 | |
| 4545 | |
| 4546 | static void TryReferenceInitializationCore(Sema &S, |
| 4547 | const InitializedEntity &Entity, |
| 4548 | const InitializationKind &Kind, |
| 4549 | Expr *Initializer, |
| 4550 | QualType cv1T1, QualType T1, |
| 4551 | Qualifiers T1Quals, |
| 4552 | QualType cv2T2, QualType T2, |
| 4553 | Qualifiers T2Quals, |
| 4554 | InitializationSequence &Sequence) { |
| 4555 | QualType DestType = Entity.getType(); |
| 4556 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
| 4557 | |
| 4558 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 4559 | bool isRValueRef = !isLValueRef; |
| 4560 | bool DerivedToBase = false; |
| 4561 | bool ObjCConversion = false; |
| 4562 | bool ObjCLifetimeConversion = false; |
| 4563 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
| 4564 | Sema::ReferenceCompareResult RefRelationship |
| 4565 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
| 4566 | ObjCConversion, ObjCLifetimeConversion); |
| 4567 | |
| 4568 | |
| 4569 | |
| 4570 | |
| 4571 | |
| 4572 | |
| 4573 | |
| 4574 | |
| 4575 | |
| 4576 | |
| 4577 | OverloadingResult ConvOvlResult = OR_Success; |
| 4578 | bool T1Function = T1->isFunctionType(); |
| 4579 | if (isLValueRef || T1Function) { |
| 4580 | if (InitCategory.isLValue() && !isNonReferenceableGLValue(Initializer) && |
| 4581 | (RefRelationship == Sema::Ref_Compatible || |
| 4582 | (Kind.isCStyleOrFunctionalCast() && |
| 4583 | RefRelationship == Sema::Ref_Related))) { |
| 4584 | |
| 4585 | |
| 4586 | if (T1Quals != T2Quals) |
| 4587 | |
| 4588 | |
| 4589 | |
| 4590 | Sequence.AddQualificationConversionStep( |
| 4591 | S.Context.getQualifiedType(T2, T1Quals), |
| 4592 | Initializer->getValueKind()); |
| 4593 | if (DerivedToBase) |
| 4594 | Sequence.AddDerivedToBaseCastStep(cv1T1, VK_LValue); |
| 4595 | else if (ObjCConversion) |
| 4596 | Sequence.AddObjCObjectConversionStep(cv1T1); |
| 4597 | |
| 4598 | |
| 4599 | |
| 4600 | |
| 4601 | Sequence.AddReferenceBindingStep(cv1T1, false); |
| 4602 | return; |
| 4603 | } |
| 4604 | |
| 4605 | |
| 4606 | |
| 4607 | |
| 4608 | |
| 4609 | |
| 4610 | |
| 4611 | |
| 4612 | |
| 4613 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 4614 | (isLValueRef || InitCategory.isRValue())) { |
| 4615 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 4616 | S, Entity, Kind, Initializer, isRValueRef, |
| 4617 | isLValueRef, Sequence); |
| 4618 | if (ConvOvlResult == OR_Success) |
| 4619 | return; |
| 4620 | if (ConvOvlResult != OR_No_Viable_Function) |
| 4621 | Sequence.SetOverloadFailure( |
| 4622 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4623 | ConvOvlResult); |
| 4624 | } |
| 4625 | } |
| 4626 | |
| 4627 | |
| 4628 | |
| 4629 | |
| 4630 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
| 4631 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4632 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4633 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4634 | Sequence.SetOverloadFailure( |
| 4635 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4636 | ConvOvlResult); |
| 4637 | else if (!InitCategory.isLValue()) |
| 4638 | Sequence.SetFailed( |
| 4639 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 4640 | else { |
| 4641 | InitializationSequence::FailureKind FK; |
| 4642 | switch (RefRelationship) { |
| 4643 | case Sema::Ref_Compatible: |
| 4644 | if (Initializer->refersToBitField()) |
| 4645 | FK = InitializationSequence:: |
| 4646 | FK_NonConstLValueReferenceBindingToBitfield; |
| 4647 | else if (Initializer->refersToVectorElement()) |
| 4648 | FK = InitializationSequence:: |
| 4649 | FK_NonConstLValueReferenceBindingToVectorElement; |
| 4650 | else |
| 4651 | llvm_unreachable("unexpected kind of compatible initializer"); |
| 4652 | break; |
| 4653 | case Sema::Ref_Related: |
| 4654 | FK = InitializationSequence::FK_ReferenceInitDropsQualifiers; |
| 4655 | break; |
| 4656 | case Sema::Ref_Incompatible: |
| 4657 | FK = InitializationSequence:: |
| 4658 | FK_NonConstLValueReferenceBindingToUnrelated; |
| 4659 | break; |
| 4660 | } |
| 4661 | Sequence.SetFailed(FK); |
| 4662 | } |
| 4663 | return; |
| 4664 | } |
| 4665 | |
| 4666 | |
| 4667 | |
| 4668 | |
| 4669 | |
| 4670 | |
| 4671 | |
| 4672 | |
| 4673 | if (!T1Function && |
| 4674 | (RefRelationship == Sema::Ref_Compatible || |
| 4675 | (Kind.isCStyleOrFunctionalCast() && |
| 4676 | RefRelationship == Sema::Ref_Related)) && |
| 4677 | ((InitCategory.isXValue() && !isNonReferenceableGLValue(Initializer)) || |
| 4678 | (InitCategory.isPRValue() && |
| 4679 | (S.getLangOpts().CPlusPlus17 || T2->isRecordType() || |
| 4680 | T2->isArrayType())))) { |
| 4681 | ExprValueKind ValueKind = InitCategory.isXValue() ? VK_XValue : VK_RValue; |
| 4682 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
| 4683 | |
| 4684 | |
| 4685 | |
| 4686 | |
| 4687 | |
| 4688 | |
| 4689 | |
| 4690 | |
| 4691 | |
| 4692 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
| 4693 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
| 4694 | else if (S.getLangOpts().CPlusPlus11) |
| 4695 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
| 4696 | } |
| 4697 | |
| 4698 | |
| 4699 | |
| 4700 | |
| 4701 | |
| 4702 | |
| 4703 | |
| 4704 | auto T1QualsIgnoreAS = T1Quals; |
| 4705 | auto T2QualsIgnoreAS = T2Quals; |
| 4706 | if (T1Quals.getAddressSpace() != T2Quals.getAddressSpace()) { |
| 4707 | T1QualsIgnoreAS.removeAddressSpace(); |
| 4708 | T2QualsIgnoreAS.removeAddressSpace(); |
| 4709 | } |
| 4710 | QualType cv1T4 = S.Context.getQualifiedType(cv2T2, T1QualsIgnoreAS); |
| 4711 | if (T1QualsIgnoreAS != T2QualsIgnoreAS) |
| 4712 | Sequence.AddQualificationConversionStep(cv1T4, ValueKind); |
| 4713 | Sequence.AddReferenceBindingStep(cv1T4, ValueKind == VK_RValue); |
| 4714 | ValueKind = isLValueRef ? VK_LValue : VK_XValue; |
| 4715 | |
| 4716 | if (T1Quals.getAddressSpace() != T2Quals.getAddressSpace()) { |
| 4717 | auto T4Quals = cv1T4.getQualifiers(); |
| 4718 | T4Quals.addAddressSpace(T1Quals.getAddressSpace()); |
| 4719 | QualType cv1T4WithAS = S.Context.getQualifiedType(T2, T4Quals); |
| 4720 | Sequence.AddQualificationConversionStep(cv1T4WithAS, ValueKind); |
| 4721 | } |
| 4722 | |
| 4723 | |
| 4724 | |
| 4725 | if (DerivedToBase) |
| 4726 | Sequence.AddDerivedToBaseCastStep(cv1T1, ValueKind); |
| 4727 | else if (ObjCConversion) |
| 4728 | Sequence.AddObjCObjectConversionStep(cv1T1); |
| 4729 | return; |
| 4730 | } |
| 4731 | |
| 4732 | |
| 4733 | |
| 4734 | |
| 4735 | |
| 4736 | |
| 4737 | |
| 4738 | if (T2->isRecordType()) { |
| 4739 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 4740 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 4741 | S, Entity, Kind, Initializer, true, |
| 4742 | isLValueRef, Sequence); |
| 4743 | if (ConvOvlResult) |
| 4744 | Sequence.SetOverloadFailure( |
| 4745 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4746 | ConvOvlResult); |
| 4747 | |
| 4748 | return; |
| 4749 | } |
| 4750 | |
| 4751 | if (RefRelationship == Sema::Ref_Compatible && |
| 4752 | isRValueRef && InitCategory.isLValue()) { |
| 4753 | Sequence.SetFailed( |
| 4754 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4755 | return; |
| 4756 | } |
| 4757 | |
| 4758 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4759 | return; |
| 4760 | } |
| 4761 | |
| 4762 | |
| 4763 | |
| 4764 | |
| 4765 | |
| 4766 | |
| 4767 | |
| 4768 | |
| 4769 | QualType cv1T1IgnoreAS = |
| 4770 | T1Quals.hasAddressSpace() |
| 4771 | ? S.Context.getQualifiedType(T1, T1Quals.withoutAddressSpace()) |
| 4772 | : cv1T1; |
| 4773 | |
| 4774 | InitializedEntity TempEntity = |
| 4775 | InitializedEntity::InitializeTemporary(cv1T1IgnoreAS); |
| 4776 | |
| 4777 | |
| 4778 | |
| 4779 | ImplicitConversionSequence ICS |
| 4780 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
| 4781 | , |
| 4782 | , |
| 4783 | , |
| 4784 | .isCStyleOrFunctionalCast(), |
| 4785 | ); |
| 4786 | |
| 4787 | if (ICS.isBad()) { |
| 4788 | |
| 4789 | |
| 4790 | |
| 4791 | |
| 4792 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4793 | Sequence.SetOverloadFailure( |
| 4794 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4795 | ConvOvlResult); |
| 4796 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4797 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 4798 | else |
| 4799 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
| 4800 | return; |
| 4801 | } else { |
| 4802 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
| 4803 | } |
| 4804 | |
| 4805 | |
| 4806 | |
| 4807 | |
| 4808 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4809 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
| 4810 | if ((RefRelationship == Sema::Ref_Related && |
| 4811 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) || |
| 4812 | !T1Quals.isAddressSpaceSupersetOf(T2Quals)) { |
| 4813 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4814 | return; |
| 4815 | } |
| 4816 | |
| 4817 | |
| 4818 | |
| 4819 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
| 4820 | InitCategory.isLValue()) { |
| 4821 | Sequence.SetFailed( |
| 4822 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4823 | return; |
| 4824 | } |
| 4825 | |
| 4826 | Sequence.AddReferenceBindingStep(cv1T1IgnoreAS, ); |
| 4827 | |
| 4828 | if (T1Quals.hasAddressSpace()) |
| 4829 | Sequence.AddQualificationConversionStep(cv1T1, isLValueRef ? VK_LValue |
| 4830 | : VK_XValue); |
| 4831 | } |
| 4832 | |
| 4833 | |
| 4834 | |
| 4835 | static void TryStringLiteralInitialization(Sema &S, |
| 4836 | const InitializedEntity &Entity, |
| 4837 | const InitializationKind &Kind, |
| 4838 | Expr *Initializer, |
| 4839 | InitializationSequence &Sequence) { |
| 4840 | Sequence.AddStringInitStep(Entity.getType()); |
| 4841 | } |
| 4842 | |
| 4843 | |
| 4844 | static void TryValueInitialization(Sema &S, |
| 4845 | const InitializedEntity &Entity, |
| 4846 | const InitializationKind &Kind, |
| 4847 | InitializationSequence &Sequence, |
| 4848 | InitListExpr *InitList) { |
| 4849 | (0) . __assert_fail ("(!InitList || InitList->getNumInits() == 0) && \"Shouldn't use value-init for non-empty init lists\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4850, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!InitList || InitList->getNumInits() == 0) && |
| 4850 | (0) . __assert_fail ("(!InitList || InitList->getNumInits() == 0) && \"Shouldn't use value-init for non-empty init lists\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4850, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Shouldn't use value-init for non-empty init lists"); |
| 4851 | |
| 4852 | |
| 4853 | |
| 4854 | |
| 4855 | QualType T = Entity.getType(); |
| 4856 | |
| 4857 | |
| 4858 | T = S.Context.getBaseElementType(T); |
| 4859 | |
| 4860 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4861 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 4862 | bool NeedZeroInitialization = true; |
| 4863 | |
| 4864 | |
| 4865 | |
| 4866 | |
| 4867 | |
| 4868 | |
| 4869 | |
| 4870 | |
| 4871 | |
| 4872 | |
| 4873 | |
| 4874 | |
| 4875 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4876 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
| 4877 | NeedZeroInitialization = false; |
| 4878 | |
| 4879 | |
| 4880 | |
| 4881 | |
| 4882 | |
| 4883 | |
| 4884 | |
| 4885 | if (NeedZeroInitialization) |
| 4886 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4887 | |
| 4888 | |
| 4889 | |
| 4890 | |
| 4891 | |
| 4892 | |
| 4893 | |
| 4894 | |
| 4895 | |
| 4896 | |
| 4897 | |
| 4898 | if (!S.getLangOpts().CPlusPlus11 && |
| 4899 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4900 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4901 | return; |
| 4902 | } |
| 4903 | |
| 4904 | |
| 4905 | |
| 4906 | |
| 4907 | Expr *InitListAsExpr = InitList; |
| 4908 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
| 4909 | bool InitListSyntax = InitList; |
| 4910 | |
| 4911 | |
| 4912 | |
| 4913 | return TryConstructorInitialization( |
| 4914 | S, Entity, Kind, Args, T, Entity.getType(), Sequence, InitListSyntax); |
| 4915 | } |
| 4916 | } |
| 4917 | |
| 4918 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4919 | } |
| 4920 | |
| 4921 | |
| 4922 | static void TryDefaultInitialization(Sema &S, |
| 4923 | const InitializedEntity &Entity, |
| 4924 | const InitializationKind &Kind, |
| 4925 | InitializationSequence &Sequence) { |
| 4926 | assert(Kind.getKind() == InitializationKind::IK_Default); |
| 4927 | |
| 4928 | |
| 4929 | |
| 4930 | |
| 4931 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 4932 | |
| 4933 | |
| 4934 | |
| 4935 | |
| 4936 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
| 4937 | TryConstructorInitialization(S, Entity, Kind, None, DestType, |
| 4938 | Entity.getType(), Sequence); |
| 4939 | return; |
| 4940 | } |
| 4941 | |
| 4942 | |
| 4943 | |
| 4944 | |
| 4945 | |
| 4946 | |
| 4947 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
| 4948 | if (!maybeRecoverWithZeroInitialization(S, Sequence, Entity)) |
| 4949 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 4950 | return; |
| 4951 | } |
| 4952 | |
| 4953 | |
| 4954 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4955 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4956 | return; |
| 4957 | } |
| 4958 | } |
| 4959 | |
| 4960 | |
| 4961 | |
| 4962 | |
| 4963 | static void TryUserDefinedConversion(Sema &S, |
| 4964 | QualType DestType, |
| 4965 | const InitializationKind &Kind, |
| 4966 | Expr *Initializer, |
| 4967 | InitializationSequence &Sequence, |
| 4968 | bool TopLevelOfInitList) { |
| 4969 | (0) . __assert_fail ("!DestType->isReferenceType() && \"References are handled elsewhere\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4969, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4970 | QualType SourceType = Initializer->getType(); |
| 4971 | (0) . __assert_fail ("(DestType->isRecordType() || SourceType->isRecordType()) && \"Must have a class type to perform a user-defined conversion\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4972, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4972 | (0) . __assert_fail ("(DestType->isRecordType() || SourceType->isRecordType()) && \"Must have a class type to perform a user-defined conversion\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 4972, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Must have a class type to perform a user-defined conversion"); |
| 4973 | |
| 4974 | |
| 4975 | |
| 4976 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 4977 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
| 4978 | |
| 4979 | |
| 4980 | |
| 4981 | bool AllowExplicit = Kind.AllowExplicit(); |
| 4982 | |
| 4983 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4984 | |
| 4985 | |
| 4986 | CXXRecordDecl *DestRecordDecl |
| 4987 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 4988 | |
| 4989 | |
| 4990 | if (S.isCompleteType(Kind.getLocation(), DestType)) { |
| 4991 | for (NamedDecl *D : S.LookupConstructors(DestRecordDecl)) { |
| 4992 | auto Info = getConstructorInfo(D); |
| 4993 | if (!Info.Constructor) |
| 4994 | continue; |
| 4995 | |
| 4996 | if (!Info.Constructor->isInvalidDecl() && |
| 4997 | Info.Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4998 | if (Info.ConstructorTmpl) |
| 4999 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
| 5000 | nullptr, |
| 5001 | Initializer, CandidateSet, |
| 5002 | ); |
| 5003 | else |
| 5004 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
| 5005 | Initializer, CandidateSet, |
| 5006 | ); |
| 5007 | } |
| 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | |
| 5012 | SourceLocation DeclLoc = Initializer->getBeginLoc(); |
| 5013 | |
| 5014 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 5015 | |
| 5016 | |
| 5017 | |
| 5018 | |
| 5019 | |
| 5020 | if (S.isCompleteType(DeclLoc, SourceType)) { |
| 5021 | CXXRecordDecl *SourceRecordDecl |
| 5022 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
| 5023 | |
| 5024 | const auto &Conversions = |
| 5025 | SourceRecordDecl->getVisibleConversionFunctions(); |
| 5026 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 5027 | NamedDecl *D = *I; |
| 5028 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5029 | if (isa<UsingShadowDecl>(D)) |
| 5030 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5031 | |
| 5032 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5033 | CXXConversionDecl *Conv; |
| 5034 | if (ConvTemplate) |
| 5035 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5036 | else |
| 5037 | Conv = cast<CXXConversionDecl>(D); |
| 5038 | |
| 5039 | if (AllowExplicit || !Conv->isExplicit()) { |
| 5040 | if (ConvTemplate) |
| 5041 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
| 5042 | ActingDC, Initializer, DestType, |
| 5043 | CandidateSet, AllowExplicit); |
| 5044 | else |
| 5045 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
| 5046 | Initializer, DestType, CandidateSet, |
| 5047 | AllowExplicit); |
| 5048 | } |
| 5049 | } |
| 5050 | } |
| 5051 | } |
| 5052 | |
| 5053 | |
| 5054 | OverloadCandidateSet::iterator Best; |
| 5055 | if (OverloadingResult Result |
| 5056 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
| 5057 | Sequence.SetOverloadFailure( |
| 5058 | InitializationSequence::FK_UserConversionOverloadFailed, |
| 5059 | Result); |
| 5060 | return; |
| 5061 | } |
| 5062 | |
| 5063 | FunctionDecl *Function = Best->Function; |
| 5064 | Function->setReferenced(); |
| 5065 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 5066 | |
| 5067 | if (isa<CXXConstructorDecl>(Function)) { |
| 5068 | |
| 5069 | |
| 5070 | |
| 5071 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 5072 | DestType.getUnqualifiedType(), |
| 5073 | HadMultipleCandidates); |
| 5074 | |
| 5075 | |
| 5076 | |
| 5077 | |
| 5078 | |
| 5079 | |
| 5080 | |
| 5081 | |
| 5082 | |
| 5083 | |
| 5084 | |
| 5085 | |
| 5086 | |
| 5087 | |
| 5088 | |
| 5089 | |
| 5090 | |
| 5091 | |
| 5092 | if (!S.getLangOpts().CPlusPlus17) |
| 5093 | Sequence.AddFinalCopy(DestType); |
| 5094 | else if (DestType.hasQualifiers()) |
| 5095 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 5096 | return; |
| 5097 | } |
| 5098 | |
| 5099 | |
| 5100 | QualType ConvType = Function->getCallResultType(); |
| 5101 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 5102 | HadMultipleCandidates); |
| 5103 | |
| 5104 | if (ConvType->getAs<RecordType>()) { |
| 5105 | |
| 5106 | |
| 5107 | |
| 5108 | |
| 5109 | |
| 5110 | |
| 5111 | |
| 5112 | |
| 5113 | |
| 5114 | if (!S.getLangOpts().CPlusPlus17 || |
| 5115 | Function->getReturnType()->isReferenceType() || |
| 5116 | !S.Context.hasSameUnqualifiedType(ConvType, DestType)) |
| 5117 | Sequence.AddFinalCopy(DestType); |
| 5118 | else if (!S.Context.hasSameType(ConvType, DestType)) |
| 5119 | Sequence.AddQualificationConversionStep(DestType, VK_RValue); |
| 5120 | return; |
| 5121 | } |
| 5122 | |
| 5123 | |
| 5124 | |
| 5125 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 5126 | Best->FinalConversion.Third) { |
| 5127 | ImplicitConversionSequence ICS; |
| 5128 | ICS.setStandard(); |
| 5129 | ICS.Standard = Best->FinalConversion; |
| 5130 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
| 5131 | } |
| 5132 | } |
| 5133 | |
| 5134 | |
| 5135 | |
| 5136 | |
| 5137 | |
| 5138 | |
| 5139 | |
| 5140 | |
| 5141 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 5142 | const InitializedEntity &Entity, |
| 5143 | const Expr *Init) { |
| 5144 | return S.getLangOpts().CPlusPlus11 && |
| 5145 | Entity.getKind() == InitializedEntity::EK_Result && |
| 5146 | Entity.getType()->isPointerType() && |
| 5147 | isa<CXXBoolLiteralExpr>(Init) && |
| 5148 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 5149 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 5150 | } |
| 5151 | |
| 5152 | |
| 5153 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 5154 | |
| 5155 | |
| 5156 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 5157 | bool isAddressOf, bool &isWeakAccess) { |
| 5158 | |
| 5159 | e = e->IgnoreParens(); |
| 5160 | |
| 5161 | |
| 5162 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 5163 | if (op->getOpcode() == UO_AddrOf) |
| 5164 | return isInvalidICRSource(C, op->getSubExpr(), true, |
| 5165 | isWeakAccess); |
| 5166 | |
| 5167 | |
| 5168 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 5169 | switch (ce->getCastKind()) { |
| 5170 | case CK_Dependent: |
| 5171 | case CK_BitCast: |
| 5172 | case CK_LValueBitCast: |
| 5173 | case CK_NoOp: |
| 5174 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
| 5175 | |
| 5176 | case CK_ArrayToPointerDecay: |
| 5177 | return IIK_nonscalar; |
| 5178 | |
| 5179 | case CK_NullToPointer: |
| 5180 | return IIK_okay; |
| 5181 | |
| 5182 | default: |
| 5183 | break; |
| 5184 | } |
| 5185 | |
| 5186 | |
| 5187 | } else if (isa<DeclRefExpr>(e)) { |
| 5188 | |
| 5189 | |
| 5190 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 5191 | isWeakAccess = true; |
| 5192 | |
| 5193 | if (!isAddressOf) return IIK_nonlocal; |
| 5194 | |
| 5195 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 5196 | if (!var) return IIK_nonlocal; |
| 5197 | |
| 5198 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
| 5199 | |
| 5200 | |
| 5201 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
| 5202 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 5203 | isWeakAccess)) |
| 5204 | return iik; |
| 5205 | |
| 5206 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
| 5207 | |
| 5208 | |
| 5209 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 5210 | return IIK_nonscalar; |
| 5211 | |
| 5212 | |
| 5213 | } else { |
| 5214 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 5215 | ? IIK_okay : IIK_nonlocal); |
| 5216 | } |
| 5217 | |
| 5218 | return IIK_nonlocal; |
| 5219 | } |
| 5220 | |
| 5221 | |
| 5222 | |
| 5223 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 5224 | isRValue()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 5224, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(src->isRValue()); |
| 5225 | bool isWeakAccess = false; |
| 5226 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 5227 | |
| 5228 | |
| 5229 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 5230 | S.Cleanup.setExprNeedsCleanups(true); |
| 5231 | |
| 5232 | if (iik == IIK_okay) return; |
| 5233 | |
| 5234 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 5235 | << ((unsigned) iik - 1) |
| 5236 | << src->getSourceRange(); |
| 5237 | } |
| 5238 | |
| 5239 | |
| 5240 | |
| 5241 | static bool hasCompatibleArrayTypes(ASTContext &Context, const ArrayType *Dest, |
| 5242 | const ArrayType *Source) { |
| 5243 | |
| 5244 | |
| 5245 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 5246 | return true; |
| 5247 | |
| 5248 | |
| 5249 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 5250 | return false; |
| 5251 | |
| 5252 | |
| 5253 | |
| 5254 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 5255 | } |
| 5256 | |
| 5257 | static bool tryObjCWritebackConversion(Sema &S, |
| 5258 | InitializationSequence &Sequence, |
| 5259 | const InitializedEntity &Entity, |
| 5260 | Expr *Initializer) { |
| 5261 | bool ArrayDecay = false; |
| 5262 | QualType ArgType = Initializer->getType(); |
| 5263 | QualType ArgPointee; |
| 5264 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 5265 | ArrayDecay = true; |
| 5266 | ArgPointee = ArgArrayType->getElementType(); |
| 5267 | ArgType = S.Context.getPointerType(ArgPointee); |
| 5268 | } |
| 5269 | |
| 5270 | |
| 5271 | QualType ConvertedArgType; |
| 5272 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 5273 | ConvertedArgType)) |
| 5274 | return false; |
| 5275 | |
| 5276 | |
| 5277 | |
| 5278 | bool ShouldCopy = true; |
| 5279 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5280 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 5281 | |
| 5282 | |
| 5283 | if (ArrayDecay || Initializer->isGLValue()) { |
| 5284 | ImplicitConversionSequence ICS; |
| 5285 | ICS.setStandard(); |
| 5286 | ICS.Standard.setAsIdentityConversion(); |
| 5287 | |
| 5288 | QualType ResultType; |
| 5289 | if (ArrayDecay) { |
| 5290 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 5291 | ResultType = S.Context.getPointerType(ArgPointee); |
| 5292 | } else { |
| 5293 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 5294 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 5295 | } |
| 5296 | |
| 5297 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 5298 | } |
| 5299 | |
| 5300 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 5301 | return true; |
| 5302 | } |
| 5303 | |
| 5304 | static bool TryOCLSamplerInitialization(Sema &S, |
| 5305 | InitializationSequence &Sequence, |
| 5306 | QualType DestType, |
| 5307 | Expr *Initializer) { |
| 5308 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 5309 | (!Initializer->isIntegerConstantExpr(S.Context) && |
| 5310 | !Initializer->getType()->isSamplerT())) |
| 5311 | return false; |
| 5312 | |
| 5313 | Sequence.AddOCLSamplerInitStep(DestType); |
| 5314 | return true; |
| 5315 | } |
| 5316 | |
| 5317 | static bool IsZeroInitializer(Expr *Initializer, Sema &S) { |
| 5318 | return Initializer->isIntegerConstantExpr(S.getASTContext()) && |
| 5319 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) == 0); |
| 5320 | } |
| 5321 | |
| 5322 | static bool TryOCLZeroOpaqueTypeInitialization(Sema &S, |
| 5323 | InitializationSequence &Sequence, |
| 5324 | QualType DestType, |
| 5325 | Expr *Initializer) { |
| 5326 | if (!S.getLangOpts().OpenCL) |
| 5327 | return false; |
| 5328 | |
| 5329 | |
| 5330 | |
| 5331 | |
| 5332 | |
| 5333 | |
| 5334 | |
| 5335 | |
| 5336 | |
| 5337 | if (DestType->isEventT() || DestType->isQueueT()) { |
| 5338 | if (!IsZeroInitializer(Initializer, S)) |
| 5339 | return false; |
| 5340 | |
| 5341 | Sequence.AddOCLZeroOpaqueTypeStep(DestType); |
| 5342 | return true; |
| 5343 | } |
| 5344 | |
| 5345 | |
| 5346 | |
| 5347 | |
| 5348 | if (S.getOpenCLOptions().isEnabled( |
| 5349 | "cl_intel_device_side_avc_motion_estimation") && |
| 5350 | DestType->isOCLIntelSubgroupAVCType()) { |
| 5351 | if (DestType->isOCLIntelSubgroupAVCMcePayloadType() || |
| 5352 | DestType->isOCLIntelSubgroupAVCMceResultType()) |
| 5353 | return false; |
| 5354 | if (!IsZeroInitializer(Initializer, S)) |
| 5355 | return false; |
| 5356 | |
| 5357 | Sequence.AddOCLZeroOpaqueTypeStep(DestType); |
| 5358 | return true; |
| 5359 | } |
| 5360 | |
| 5361 | return false; |
| 5362 | } |
| 5363 | |
| 5364 | InitializationSequence::InitializationSequence(Sema &S, |
| 5365 | const InitializedEntity &Entity, |
| 5366 | const InitializationKind &Kind, |
| 5367 | MultiExprArg Args, |
| 5368 | bool TopLevelOfInitList, |
| 5369 | bool TreatUnavailableAsInvalid) |
| 5370 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
| 5371 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList, |
| 5372 | TreatUnavailableAsInvalid); |
| 5373 | } |
| 5374 | |
| 5375 | |
| 5376 | |
| 5377 | static bool isExprAnUnaddressableFunction(Sema &S, const Expr *E) { |
| 5378 | auto *DRE = dyn_cast<DeclRefExpr>(E); |
| 5379 | if (!DRE || !isa<FunctionDecl>(DRE->getDecl())) |
| 5380 | return false; |
| 5381 | |
| 5382 | return !S.checkAddressOfFunctionIsAvailable( |
| 5383 | cast<FunctionDecl>(DRE->getDecl())); |
| 5384 | } |
| 5385 | |
| 5386 | |
| 5387 | |
| 5388 | static bool canPerformArrayCopy(const InitializedEntity &Entity) { |
| 5389 | switch (Entity.getKind()) { |
| 5390 | case InitializedEntity::EK_LambdaCapture: |
| 5391 | |
| 5392 | |
| 5393 | |
| 5394 | return true; |
| 5395 | |
| 5396 | case InitializedEntity::EK_Variable: |
| 5397 | |
| 5398 | |
| 5399 | |
| 5400 | return isa<DecompositionDecl>(Entity.getDecl()); |
| 5401 | |
| 5402 | case InitializedEntity::EK_Member: |
| 5403 | |
| 5404 | |
| 5405 | |
| 5406 | return Entity.isImplicitMemberInitializer(); |
| 5407 | |
| 5408 | case InitializedEntity::EK_ArrayElement: |
| 5409 | |
| 5410 | |
| 5411 | if (auto *E = Entity.getParent()) |
| 5412 | return canPerformArrayCopy(*E); |
| 5413 | break; |
| 5414 | |
| 5415 | default: |
| 5416 | break; |
| 5417 | } |
| 5418 | |
| 5419 | return false; |
| 5420 | } |
| 5421 | |
| 5422 | void InitializationSequence::InitializeFrom(Sema &S, |
| 5423 | const InitializedEntity &Entity, |
| 5424 | const InitializationKind &Kind, |
| 5425 | MultiExprArg Args, |
| 5426 | bool TopLevelOfInitList, |
| 5427 | bool TreatUnavailableAsInvalid) { |
| 5428 | ASTContext &Context = S.Context; |
| 5429 | |
| 5430 | |
| 5431 | |
| 5432 | |
| 5433 | |
| 5434 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 5435 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 5436 | |
| 5437 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 5438 | if (result.isInvalid()) { |
| 5439 | SetFailed(FK_PlaceholderType); |
| 5440 | return; |
| 5441 | } |
| 5442 | Args[I] = result.get(); |
| 5443 | } |
| 5444 | |
| 5445 | |
| 5446 | |
| 5447 | |
| 5448 | |
| 5449 | |
| 5450 | |
| 5451 | QualType DestType = Entity.getType(); |
| 5452 | |
| 5453 | if (DestType->isDependentType() || |
| 5454 | Expr::hasAnyTypeDependentArguments(Args)) { |
| 5455 | SequenceKind = DependentSequence; |
| 5456 | return; |
| 5457 | } |
| 5458 | |
| 5459 | |
| 5460 | setSequenceKind(NormalSequence); |
| 5461 | |
| 5462 | QualType SourceType; |
| 5463 | Expr *Initializer = nullptr; |
| 5464 | if (Args.size() == 1) { |
| 5465 | Initializer = Args[0]; |
| 5466 | if (S.getLangOpts().ObjC) { |
| 5467 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getBeginLoc(), |
| 5468 | DestType, Initializer->getType(), |
| 5469 | Initializer) || |
| 5470 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 5471 | Args[0] = Initializer; |
| 5472 | } |
| 5473 | if (!isa<InitListExpr>(Initializer)) |
| 5474 | SourceType = Initializer->getType(); |
| 5475 | } |
| 5476 | |
| 5477 | |
| 5478 | |
| 5479 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 5480 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 5481 | TryListInitialization(S, Entity, Kind, InitList, *this, |
| 5482 | TreatUnavailableAsInvalid); |
| 5483 | return; |
| 5484 | } |
| 5485 | } |
| 5486 | |
| 5487 | |
| 5488 | if (DestType->isReferenceType()) { |
| 5489 | |
| 5490 | |
| 5491 | |
| 5492 | |
| 5493 | |
| 5494 | if (Args.size() != 1) |
| 5495 | SetFailed(FK_TooManyInitsForReference); |
| 5496 | |
| 5497 | |
| 5498 | |
| 5499 | |
| 5500 | else if (isa<InitListExpr>(Args[0])) |
| 5501 | SetFailed(FK_ParenthesizedListInitForReference); |
| 5502 | else |
| 5503 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
| 5504 | return; |
| 5505 | } |
| 5506 | |
| 5507 | |
| 5508 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 5509 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
| 5510 | TryValueInitialization(S, Entity, Kind, *this); |
| 5511 | return; |
| 5512 | } |
| 5513 | |
| 5514 | |
| 5515 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 5516 | TryDefaultInitialization(S, Entity, Kind, *this); |
| 5517 | return; |
| 5518 | } |
| 5519 | |
| 5520 | |
| 5521 | |
| 5522 | |
| 5523 | |
| 5524 | |
| 5525 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 5526 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 5527 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 5528 | return; |
| 5529 | } |
| 5530 | |
| 5531 | if (Initializer) { |
| 5532 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 5533 | case SIF_None: |
| 5534 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 5535 | return; |
| 5536 | case SIF_NarrowStringIntoWideChar: |
| 5537 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 5538 | return; |
| 5539 | case SIF_WideStringIntoChar: |
| 5540 | SetFailed(FK_WideStringIntoCharArray); |
| 5541 | return; |
| 5542 | case SIF_IncompatWideStringIntoWideChar: |
| 5543 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 5544 | return; |
| 5545 | case SIF_PlainStringIntoUTF8Char: |
| 5546 | SetFailed(FK_PlainStringIntoUTF8Char); |
| 5547 | return; |
| 5548 | case SIF_UTF8StringIntoPlainChar: |
| 5549 | SetFailed(FK_UTF8StringIntoPlainChar); |
| 5550 | return; |
| 5551 | case SIF_Other: |
| 5552 | break; |
| 5553 | } |
| 5554 | } |
| 5555 | |
| 5556 | |
| 5557 | |
| 5558 | if (Initializer && isa<ConstantArrayType>(DestAT) && |
| 5559 | S.Context.hasSameUnqualifiedType(Initializer->getType(), |
| 5560 | Entity.getType()) && |
| 5561 | canPerformArrayCopy(Entity)) { |
| 5562 | |
| 5563 | if (Initializer->getValueKind() == VK_RValue) { |
| 5564 | AddArrayInitStep(DestType, ); |
| 5565 | return; |
| 5566 | } |
| 5567 | |
| 5568 | |
| 5569 | InitializedEntity Element = |
| 5570 | InitializedEntity::InitializeElement(S.Context, 0, Entity); |
| 5571 | QualType InitEltT = |
| 5572 | Context.getAsArrayType(Initializer->getType())->getElementType(); |
| 5573 | OpaqueValueExpr OVE(Initializer->getExprLoc(), InitEltT, |
| 5574 | Initializer->getValueKind(), |
| 5575 | Initializer->getObjectKind()); |
| 5576 | Expr *OVEAsExpr = &OVE; |
| 5577 | InitializeFrom(S, Element, Kind, OVEAsExpr, TopLevelOfInitList, |
| 5578 | TreatUnavailableAsInvalid); |
| 5579 | if (!Failed()) |
| 5580 | AddArrayInitLoopStep(Entity.getType(), InitEltT); |
| 5581 | return; |
| 5582 | } |
| 5583 | |
| 5584 | |
| 5585 | |
| 5586 | |
| 5587 | if (!S.getLangOpts().CPlusPlus && Initializer && |
| 5588 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 5589 | Initializer->getType()->isArrayType()) { |
| 5590 | const ArrayType *SourceAT |
| 5591 | = Context.getAsArrayType(Initializer->getType()); |
| 5592 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
| 5593 | SetFailed(FK_ArrayTypeMismatch); |
| 5594 | else if (Initializer->HasSideEffects(S.Context)) |
| 5595 | SetFailed(FK_NonConstantArrayInit); |
| 5596 | else { |
| 5597 | AddArrayInitStep(DestType, ); |
| 5598 | } |
| 5599 | } |
| 5600 | |
| 5601 | |
| 5602 | else if (S.getLangOpts().CPlusPlus && |
| 5603 | Entity.getKind() == InitializedEntity::EK_Member && |
| 5604 | Initializer && isa<InitListExpr>(Initializer)) { |
| 5605 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 5606 | *this, TreatUnavailableAsInvalid); |
| 5607 | AddParenthesizedArrayInitStep(DestType); |
| 5608 | } else if (DestAT->getElementType()->isCharType()) |
| 5609 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
| 5610 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 5611 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
| 5612 | else |
| 5613 | SetFailed(FK_ArrayNeedsInitList); |
| 5614 | |
| 5615 | return; |
| 5616 | } |
| 5617 | |
| 5618 | |
| 5619 | |
| 5620 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
| 5621 | Entity.isParameterKind(); |
| 5622 | |
| 5623 | |
| 5624 | |
| 5625 | if (!S.getLangOpts().CPlusPlus) { |
| 5626 | |
| 5627 | if (allowObjCWritebackConversion && |
| 5628 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
| 5629 | return; |
| 5630 | } |
| 5631 | |
| 5632 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 5633 | return; |
| 5634 | |
| 5635 | if (TryOCLZeroOpaqueTypeInitialization(S, *this, DestType, Initializer)) |
| 5636 | return; |
| 5637 | |
| 5638 | |
| 5639 | AddCAssignmentStep(DestType); |
| 5640 | MaybeProduceObjCObject(S, *this, Entity); |
| 5641 | return; |
| 5642 | } |
| 5643 | |
| 5644 | assert(S.getLangOpts().CPlusPlus); |
| 5645 | |
| 5646 | |
| 5647 | if (DestType->isRecordType()) { |
| 5648 | |
| 5649 | |
| 5650 | |
| 5651 | |
| 5652 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 5653 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 5654 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 5655 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, DestType)))) |
| 5656 | TryConstructorInitialization(S, Entity, Kind, Args, |
| 5657 | DestType, DestType, *this); |
| 5658 | |
| 5659 | |
| 5660 | |
| 5661 | |
| 5662 | |
| 5663 | |
| 5664 | else |
| 5665 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
| 5666 | TopLevelOfInitList); |
| 5667 | return; |
| 5668 | } |
| 5669 | |
| 5670 | (0) . __assert_fail ("Args.size() >= 1 && \"Zero-argument case handled above\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 5670, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Args.size() >= 1 && "Zero-argument case handled above"); |
| 5671 | |
| 5672 | |
| 5673 | if (Args.size() > 1) { |
| 5674 | SetFailed(FK_TooManyInitsForScalar); |
| 5675 | return; |
| 5676 | } else if (isa<InitListExpr>(Args[0])) { |
| 5677 | SetFailed(FK_ParenthesizedListInitForScalar); |
| 5678 | return; |
| 5679 | } |
| 5680 | |
| 5681 | |
| 5682 | |
| 5683 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
| 5684 | |
| 5685 | |
| 5686 | bool NeedAtomicConversion = false; |
| 5687 | if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) { |
| 5688 | if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) || |
| 5689 | S.IsDerivedFrom(Initializer->getBeginLoc(), SourceType, |
| 5690 | Atomic->getValueType())) { |
| 5691 | DestType = Atomic->getValueType(); |
| 5692 | NeedAtomicConversion = true; |
| 5693 | } |
| 5694 | } |
| 5695 | |
| 5696 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
| 5697 | TopLevelOfInitList); |
| 5698 | MaybeProduceObjCObject(S, *this, Entity); |
| 5699 | if (!Failed() && NeedAtomicConversion) |
| 5700 | AddAtomicConversionStep(Entity.getType()); |
| 5701 | return; |
| 5702 | } |
| 5703 | |
| 5704 | |
| 5705 | |
| 5706 | |
| 5707 | |
| 5708 | |
| 5709 | |
| 5710 | ImplicitConversionSequence ICS |
| 5711 | = S.TryImplicitConversion(Initializer, DestType, |
| 5712 | , |
| 5713 | false, |
| 5714 | false, |
| 5715 | .isCStyleOrFunctionalCast(), |
| 5716 | allowObjCWritebackConversion); |
| 5717 | |
| 5718 | if (ICS.isStandard() && |
| 5719 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 5720 | |
| 5721 | |
| 5722 | |
| 5723 | |
| 5724 | bool ShouldCopy = true; |
| 5725 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 5726 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 5727 | |
| 5728 | |
| 5729 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 5730 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5731 | ImplicitConversionSequence LvalueICS; |
| 5732 | LvalueICS.setStandard(); |
| 5733 | LvalueICS.Standard.setAsIdentityConversion(); |
| 5734 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 5735 | LvalueICS.Standard.First = ICS.Standard.First; |
| 5736 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
| 5737 | } |
| 5738 | |
| 5739 | AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy); |
| 5740 | } else if (ICS.isBad()) { |
| 5741 | DeclAccessPair dap; |
| 5742 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 5743 | AddZeroInitializationStep(Entity.getType()); |
| 5744 | } else if (Initializer->getType() == Context.OverloadTy && |
| 5745 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 5746 | false, dap)) |
| 5747 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 5748 | else if (Initializer->getType()->isFunctionType() && |
| 5749 | isExprAnUnaddressableFunction(S, Initializer)) |
| 5750 | SetFailed(InitializationSequence::FK_AddressOfUnaddressableFunction); |
| 5751 | else |
| 5752 | SetFailed(InitializationSequence::FK_ConversionFailed); |
| 5753 | } else { |
| 5754 | AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
| 5755 | |
| 5756 | MaybeProduceObjCObject(S, *this, Entity); |
| 5757 | } |
| 5758 | } |
| 5759 | |
| 5760 | InitializationSequence::~InitializationSequence() { |
| 5761 | for (auto &S : Steps) |
| 5762 | S.Destroy(); |
| 5763 | } |
| 5764 | |
| 5765 | |
| 5766 | |
| 5767 | |
| 5768 | static Sema::AssignmentAction |
| 5769 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
| 5770 | switch(Entity.getKind()) { |
| 5771 | case InitializedEntity::EK_Variable: |
| 5772 | case InitializedEntity::EK_New: |
| 5773 | case InitializedEntity::EK_Exception: |
| 5774 | case InitializedEntity::EK_Base: |
| 5775 | case InitializedEntity::EK_Delegating: |
| 5776 | return Sema::AA_Initializing; |
| 5777 | |
| 5778 | case InitializedEntity::EK_Parameter: |
| 5779 | if (Entity.getDecl() && |
| 5780 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5781 | return Sema::AA_Sending; |
| 5782 | |
| 5783 | return Sema::AA_Passing; |
| 5784 | |
| 5785 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5786 | if (Entity.getDecl() && |
| 5787 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 5788 | return Sema::AA_Sending; |
| 5789 | |
| 5790 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
| 5791 | |
| 5792 | case InitializedEntity::EK_Result: |
| 5793 | case InitializedEntity::EK_StmtExprResult: |
| 5794 | return Sema::AA_Returning; |
| 5795 | |
| 5796 | case InitializedEntity::EK_Temporary: |
| 5797 | case InitializedEntity::EK_RelatedResult: |
| 5798 | |
| 5799 | return Sema::AA_Casting; |
| 5800 | |
| 5801 | case InitializedEntity::EK_Member: |
| 5802 | case InitializedEntity::EK_Binding: |
| 5803 | case InitializedEntity::EK_ArrayElement: |
| 5804 | case InitializedEntity::EK_VectorElement: |
| 5805 | case InitializedEntity::EK_ComplexElement: |
| 5806 | case InitializedEntity::EK_BlockElement: |
| 5807 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
| 5808 | case InitializedEntity::EK_LambdaCapture: |
| 5809 | case InitializedEntity::EK_CompoundLiteralInit: |
| 5810 | return Sema::AA_Initializing; |
| 5811 | } |
| 5812 | |
| 5813 | llvm_unreachable("Invalid EntityKind!"); |
| 5814 | } |
| 5815 | |
| 5816 | |
| 5817 | |
| 5818 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
| 5819 | switch (Entity.getKind()) { |
| 5820 | case InitializedEntity::EK_ArrayElement: |
| 5821 | case InitializedEntity::EK_Member: |
| 5822 | case InitializedEntity::EK_Result: |
| 5823 | case InitializedEntity::EK_StmtExprResult: |
| 5824 | case InitializedEntity::EK_New: |
| 5825 | case InitializedEntity::EK_Variable: |
| 5826 | case InitializedEntity::EK_Base: |
| 5827 | case InitializedEntity::EK_Delegating: |
| 5828 | case InitializedEntity::EK_VectorElement: |
| 5829 | case InitializedEntity::EK_ComplexElement: |
| 5830 | case InitializedEntity::EK_Exception: |
| 5831 | case InitializedEntity::EK_BlockElement: |
| 5832 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
| 5833 | case InitializedEntity::EK_LambdaCapture: |
| 5834 | case InitializedEntity::EK_CompoundLiteralInit: |
| 5835 | return false; |
| 5836 | |
| 5837 | case InitializedEntity::EK_Parameter: |
| 5838 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5839 | case InitializedEntity::EK_Temporary: |
| 5840 | case InitializedEntity::EK_RelatedResult: |
| 5841 | case InitializedEntity::EK_Binding: |
| 5842 | return true; |
| 5843 | } |
| 5844 | |
| 5845 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5846 | } |
| 5847 | |
| 5848 | |
| 5849 | |
| 5850 | static bool shouldDestroyEntity(const InitializedEntity &Entity) { |
| 5851 | switch (Entity.getKind()) { |
| 5852 | case InitializedEntity::EK_Result: |
| 5853 | case InitializedEntity::EK_StmtExprResult: |
| 5854 | case InitializedEntity::EK_New: |
| 5855 | case InitializedEntity::EK_Base: |
| 5856 | case InitializedEntity::EK_Delegating: |
| 5857 | case InitializedEntity::EK_VectorElement: |
| 5858 | case InitializedEntity::EK_ComplexElement: |
| 5859 | case InitializedEntity::EK_BlockElement: |
| 5860 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
| 5861 | case InitializedEntity::EK_LambdaCapture: |
| 5862 | return false; |
| 5863 | |
| 5864 | case InitializedEntity::EK_Member: |
| 5865 | case InitializedEntity::EK_Binding: |
| 5866 | case InitializedEntity::EK_Variable: |
| 5867 | case InitializedEntity::EK_Parameter: |
| 5868 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5869 | case InitializedEntity::EK_Temporary: |
| 5870 | case InitializedEntity::EK_ArrayElement: |
| 5871 | case InitializedEntity::EK_Exception: |
| 5872 | case InitializedEntity::EK_CompoundLiteralInit: |
| 5873 | case InitializedEntity::EK_RelatedResult: |
| 5874 | return true; |
| 5875 | } |
| 5876 | |
| 5877 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5878 | } |
| 5879 | |
| 5880 | |
| 5881 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 5882 | Expr *Initializer) { |
| 5883 | switch (Entity.getKind()) { |
| 5884 | case InitializedEntity::EK_Result: |
| 5885 | case InitializedEntity::EK_StmtExprResult: |
| 5886 | return Entity.getReturnLoc(); |
| 5887 | |
| 5888 | case InitializedEntity::EK_Exception: |
| 5889 | return Entity.getThrowLoc(); |
| 5890 | |
| 5891 | case InitializedEntity::EK_Variable: |
| 5892 | case InitializedEntity::EK_Binding: |
| 5893 | return Entity.getDecl()->getLocation(); |
| 5894 | |
| 5895 | case InitializedEntity::EK_LambdaCapture: |
| 5896 | return Entity.getCaptureLoc(); |
| 5897 | |
| 5898 | case InitializedEntity::EK_ArrayElement: |
| 5899 | case InitializedEntity::EK_Member: |
| 5900 | case InitializedEntity::EK_Parameter: |
| 5901 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 5902 | case InitializedEntity::EK_Temporary: |
| 5903 | case InitializedEntity::EK_New: |
| 5904 | case InitializedEntity::EK_Base: |
| 5905 | case InitializedEntity::EK_Delegating: |
| 5906 | case InitializedEntity::EK_VectorElement: |
| 5907 | case InitializedEntity::EK_ComplexElement: |
| 5908 | case InitializedEntity::EK_BlockElement: |
| 5909 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
| 5910 | case InitializedEntity::EK_CompoundLiteralInit: |
| 5911 | case InitializedEntity::EK_RelatedResult: |
| 5912 | return Initializer->getBeginLoc(); |
| 5913 | } |
| 5914 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5915 | } |
| 5916 | |
| 5917 | |
| 5918 | |
| 5919 | |
| 5920 | |
| 5921 | |
| 5922 | |
| 5923 | |
| 5924 | |
| 5925 | |
| 5926 | |
| 5927 | |
| 5928 | |
| 5929 | |
| 5930 | |
| 5931 | |
| 5932 | |
| 5933 | |
| 5934 | |
| 5935 | |
| 5936 | |
| 5937 | static ExprResult CopyObject(Sema &S, |
| 5938 | QualType T, |
| 5939 | const InitializedEntity &Entity, |
| 5940 | ExprResult CurInit, |
| 5941 | bool ) { |
| 5942 | if (CurInit.isInvalid()) |
| 5943 | return CurInit; |
| 5944 | |
| 5945 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
| 5946 | CXXRecordDecl *Class = nullptr; |
| 5947 | if (const RecordType *Record = T->getAs<RecordType>()) |
| 5948 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 5949 | if (!Class) |
| 5950 | return CurInit; |
| 5951 | |
| 5952 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
| 5953 | |
| 5954 | |
| 5955 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
| 5956 | return CurInit; |
| 5957 | |
| 5958 | |
| 5959 | |
| 5960 | |
| 5961 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| 5962 | DeclContext::lookup_result Ctors = S.LookupConstructors(Class); |
| 5963 | |
| 5964 | OverloadCandidateSet::iterator Best; |
| 5965 | switch (ResolveConstructorOverload( |
| 5966 | S, Loc, CurInitExpr, CandidateSet, T, Ctors, Best, |
| 5967 | , , |
| 5968 | , , |
| 5969 | )) { |
| 5970 | case OR_Success: |
| 5971 | break; |
| 5972 | |
| 5973 | case OR_No_Viable_Function: |
| 5974 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5975 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5976 | : diag::err_temp_copy_no_viable) |
| 5977 | << (int)Entity.getKind() << CurInitExpr->getType() |
| 5978 | << CurInitExpr->getSourceRange(); |
| 5979 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
| 5980 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
| 5981 | return ExprError(); |
| 5982 | return CurInit; |
| 5983 | |
| 5984 | case OR_Ambiguous: |
| 5985 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
| 5986 | << (int)Entity.getKind() << CurInitExpr->getType() |
| 5987 | << CurInitExpr->getSourceRange(); |
| 5988 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
| 5989 | return ExprError(); |
| 5990 | |
| 5991 | case OR_Deleted: |
| 5992 | S.Diag(Loc, diag::err_temp_copy_deleted) |
| 5993 | << (int)Entity.getKind() << CurInitExpr->getType() |
| 5994 | << CurInitExpr->getSourceRange(); |
| 5995 | S.NoteDeletedFunction(Best->Function); |
| 5996 | return ExprError(); |
| 5997 | } |
| 5998 | |
| 5999 | bool HadMultipleCandidates = CandidateSet.size() > 1; |
| 6000 | |
| 6001 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
| 6002 | SmallVector<Expr*, 8> ConstructorArgs; |
| 6003 | CurInit.get(); |
| 6004 | |
| 6005 | S.CheckConstructorAccess(Loc, Constructor, Best->FoundDecl, Entity, |
| 6006 | IsExtraneousCopy); |
| 6007 | |
| 6008 | if (IsExtraneousCopy) { |
| 6009 | |
| 6010 | |
| 6011 | |
| 6012 | |
| 6013 | |
| 6014 | |
| 6015 | |
| 6016 | |
| 6017 | |
| 6018 | |
| 6019 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 6020 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 6021 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 6022 | diag::err_call_incomplete_argument)) |
| 6023 | break; |
| 6024 | |
| 6025 | |
| 6026 | |
| 6027 | |
| 6028 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 6029 | } |
| 6030 | |
| 6031 | return CurInitExpr; |
| 6032 | } |
| 6033 | |
| 6034 | |
| 6035 | |
| 6036 | |
| 6037 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
| 6038 | return ExprError(); |
| 6039 | |
| 6040 | |
| 6041 | |
| 6042 | |
| 6043 | |
| 6044 | |
| 6045 | |
| 6046 | |
| 6047 | |
| 6048 | |
| 6049 | |
| 6050 | |
| 6051 | |
| 6052 | |
| 6053 | |
| 6054 | |
| 6055 | |
| 6056 | |
| 6057 | |
| 6058 | |
| 6059 | bool Elidable = |
| 6060 | CurInitExpr->isTemporaryObject(S.Context, Class) && |
| 6061 | S.Context.hasSameUnqualifiedType( |
| 6062 | Best->Function->getParamDecl(0)->getType().getNonReferenceType(), |
| 6063 | CurInitExpr->getType()); |
| 6064 | |
| 6065 | |
| 6066 | CurInit = S.BuildCXXConstructExpr(Loc, T, Best->FoundDecl, Constructor, |
| 6067 | Elidable, |
| 6068 | ConstructorArgs, |
| 6069 | HadMultipleCandidates, |
| 6070 | false, |
| 6071 | false, |
| 6072 | false, |
| 6073 | CXXConstructExpr::CK_Complete, |
| 6074 | SourceRange()); |
| 6075 | |
| 6076 | |
| 6077 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 6078 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
| 6079 | return CurInit; |
| 6080 | } |
| 6081 | |
| 6082 | |
| 6083 | |
| 6084 | |
| 6085 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 6086 | const InitializedEntity &Entity, |
| 6087 | Expr *CurInitExpr) { |
| 6088 | assert(S.getLangOpts().CPlusPlus11); |
| 6089 | |
| 6090 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 6091 | if (!Record) |
| 6092 | return; |
| 6093 | |
| 6094 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 6095 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
| 6096 | return; |
| 6097 | |
| 6098 | |
| 6099 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| 6100 | DeclContext::lookup_result Ctors = |
| 6101 | S.LookupConstructors(cast<CXXRecordDecl>(Record->getDecl())); |
| 6102 | |
| 6103 | |
| 6104 | OverloadCandidateSet::iterator Best; |
| 6105 | OverloadingResult OR = ResolveConstructorOverload( |
| 6106 | S, Loc, CurInitExpr, CandidateSet, CurInitExpr->getType(), Ctors, Best, |
| 6107 | , , |
| 6108 | , , |
| 6109 | ); |
| 6110 | |
| 6111 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 6112 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 6113 | << CurInitExpr->getSourceRange(); |
| 6114 | |
| 6115 | switch (OR) { |
| 6116 | case OR_Success: |
| 6117 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
| 6118 | Best->FoundDecl, Entity, Diag); |
| 6119 | |
| 6120 | break; |
| 6121 | |
| 6122 | case OR_No_Viable_Function: |
| 6123 | S.Diag(Loc, Diag); |
| 6124 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
| 6125 | break; |
| 6126 | |
| 6127 | case OR_Ambiguous: |
| 6128 | S.Diag(Loc, Diag); |
| 6129 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
| 6130 | break; |
| 6131 | |
| 6132 | case OR_Deleted: |
| 6133 | S.Diag(Loc, Diag); |
| 6134 | S.NoteDeletedFunction(Best->Function); |
| 6135 | break; |
| 6136 | } |
| 6137 | } |
| 6138 | |
| 6139 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 6140 | const InitializedEntity &Entity) { |
| 6141 | if (Entity.isParameterKind() && Entity.getDecl()) { |
| 6142 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 6143 | return; |
| 6144 | |
| 6145 | if (Entity.getDecl()->getDeclName()) |
| 6146 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 6147 | << Entity.getDecl()->getDeclName(); |
| 6148 | else |
| 6149 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 6150 | } |
| 6151 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 6152 | Entity.getMethodDecl()) |
| 6153 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 6154 | diag::note_method_return_type_change) |
| 6155 | << Entity.getMethodDecl()->getDeclName(); |
| 6156 | } |
| 6157 | |
| 6158 | |
| 6159 | |
| 6160 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 6161 | const InitializationKind &Kind, |
| 6162 | unsigned NumArgs) { |
| 6163 | switch (Entity.getKind()) { |
| 6164 | case InitializedEntity::EK_Temporary: |
| 6165 | case InitializedEntity::EK_CompoundLiteralInit: |
| 6166 | case InitializedEntity::EK_RelatedResult: |
| 6167 | break; |
| 6168 | default: |
| 6169 | return false; |
| 6170 | } |
| 6171 | |
| 6172 | switch (Kind.getKind()) { |
| 6173 | case InitializationKind::IK_DirectList: |
| 6174 | return true; |
| 6175 | |
| 6176 | case InitializationKind::IK_Direct: |
| 6177 | case InitializationKind::IK_Value: |
| 6178 | return NumArgs != 1; |
| 6179 | default: |
| 6180 | return false; |
| 6181 | } |
| 6182 | } |
| 6183 | |
| 6184 | static ExprResult |
| 6185 | PerformConstructorInitialization(Sema &S, |
| 6186 | const InitializedEntity &Entity, |
| 6187 | const InitializationKind &Kind, |
| 6188 | MultiExprArg Args, |
| 6189 | const InitializationSequence::Step& Step, |
| 6190 | bool &ConstructorInitRequiresZeroInit, |
| 6191 | bool IsListInitialization, |
| 6192 | bool IsStdInitListInitialization, |
| 6193 | SourceLocation LBraceLoc, |
| 6194 | SourceLocation RBraceLoc) { |
| 6195 | unsigned NumArgs = Args.size(); |
| 6196 | CXXConstructorDecl *Constructor |
| 6197 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 6198 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 6199 | |
| 6200 | |
| 6201 | SmallVector<Expr*, 8> ConstructorArgs; |
| 6202 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 6203 | ? Kind.getEqualLoc() |
| 6204 | : Kind.getLocation(); |
| 6205 | |
| 6206 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 6207 | |
| 6208 | |
| 6209 | |
| 6210 | (0) . __assert_fail ("Constructor->getParent() && \"No parent class for constructor.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 6210, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Constructor->getParent() && "No parent class for constructor."); |
| 6211 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
| 6212 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
| 6213 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 6214 | } |
| 6215 | |
| 6216 | ExprResult CurInit((Expr *)nullptr); |
| 6217 | |
| 6218 | |
| 6219 | |
| 6220 | |
| 6221 | |
| 6222 | |
| 6223 | |
| 6224 | bool AllowExplicitConv = |
| 6225 | Kind.AllowExplicit() && !Kind.isCopyInit() && Args.size() == 1 && |
| 6226 | hasCopyOrMoveCtorParam(S.Context, |
| 6227 | getConstructorInfo(Step.Function.FoundDecl)); |
| 6228 | |
| 6229 | |
| 6230 | |
| 6231 | if (S.CompleteConstructorCall(Constructor, Args, |
| 6232 | Loc, ConstructorArgs, |
| 6233 | AllowExplicitConv, |
| 6234 | IsListInitialization)) |
| 6235 | return ExprError(); |
| 6236 | |
| 6237 | |
| 6238 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
| 6239 | |
| 6240 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6241 | return ExprError(); |
| 6242 | |
| 6243 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6244 | if (!TSInfo) |
| 6245 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
| 6246 | SourceRange ParenOrBraceRange = |
| 6247 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 6248 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 6249 | : Kind.getParenOrBraceRange(); |
| 6250 | |
| 6251 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>( |
| 6252 | Step.Function.FoundDecl.getDecl())) { |
| 6253 | Constructor = S.findInheritingConstructor(Loc, Constructor, Shadow); |
| 6254 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 6255 | return ExprError(); |
| 6256 | } |
| 6257 | S.MarkFunctionReferenced(Loc, Constructor); |
| 6258 | |
| 6259 | CurInit = CXXTemporaryObjectExpr::Create( |
| 6260 | S.Context, Constructor, |
| 6261 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
| 6262 | ConstructorArgs, ParenOrBraceRange, HadMultipleCandidates, |
| 6263 | IsListInitialization, IsStdInitListInitialization, |
| 6264 | ConstructorInitRequiresZeroInit); |
| 6265 | } else { |
| 6266 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 6267 | CXXConstructExpr::CK_Complete; |
| 6268 | |
| 6269 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6270 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 6271 | CXXConstructExpr::CK_VirtualBase : |
| 6272 | CXXConstructExpr::CK_NonVirtualBase; |
| 6273 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 6274 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 6275 | } |
| 6276 | |
| 6277 | |
| 6278 | |
| 6279 | SourceRange ParenOrBraceRange; |
| 6280 | if (IsListInitialization) |
| 6281 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 6282 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
| 6283 | ParenOrBraceRange = Kind.getParenOrBraceRange(); |
| 6284 | |
| 6285 | |
| 6286 | |
| 6287 | if (Entity.allowsNRVO()) |
| 6288 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
| 6289 | Step.Function.FoundDecl, |
| 6290 | Constructor, , |
| 6291 | ConstructorArgs, |
| 6292 | HadMultipleCandidates, |
| 6293 | IsListInitialization, |
| 6294 | IsStdInitListInitialization, |
| 6295 | ConstructorInitRequiresZeroInit, |
| 6296 | ConstructKind, |
| 6297 | ParenOrBraceRange); |
| 6298 | else |
| 6299 | CurInit = S.BuildCXXConstructExpr(Loc, Step.Type, |
| 6300 | Step.Function.FoundDecl, |
| 6301 | Constructor, |
| 6302 | ConstructorArgs, |
| 6303 | HadMultipleCandidates, |
| 6304 | IsListInitialization, |
| 6305 | IsStdInitListInitialization, |
| 6306 | ConstructorInitRequiresZeroInit, |
| 6307 | ConstructKind, |
| 6308 | ParenOrBraceRange); |
| 6309 | } |
| 6310 | if (CurInit.isInvalid()) |
| 6311 | return ExprError(); |
| 6312 | |
| 6313 | |
| 6314 | S.CheckConstructorAccess(Loc, Constructor, Step.Function.FoundDecl, Entity); |
| 6315 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 6316 | return ExprError(); |
| 6317 | |
| 6318 | if (shouldBindAsTemporary(Entity)) |
| 6319 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
| 6320 | |
| 6321 | return CurInit; |
| 6322 | } |
| 6323 | |
| 6324 | namespace { |
| 6325 | enum LifetimeKind { |
| 6326 | |
| 6327 | |
| 6328 | LK_FullExpression, |
| 6329 | |
| 6330 | |
| 6331 | |
| 6332 | LK_Extended, |
| 6333 | |
| 6334 | |
| 6335 | |
| 6336 | LK_New, |
| 6337 | |
| 6338 | |
| 6339 | |
| 6340 | LK_Return, |
| 6341 | |
| 6342 | |
| 6343 | |
| 6344 | LK_StmtExprResult, |
| 6345 | |
| 6346 | |
| 6347 | |
| 6348 | LK_MemInitializer, |
| 6349 | }; |
| 6350 | using LifetimeResult = |
| 6351 | llvm::PointerIntPair<const InitializedEntity *, 3, LifetimeKind>; |
| 6352 | } |
| 6353 | |
| 6354 | |
| 6355 | |
| 6356 | |
| 6357 | static LifetimeResult getEntityLifetime( |
| 6358 | const InitializedEntity *Entity, |
| 6359 | const InitializedEntity *InitField = nullptr) { |
| 6360 | |
| 6361 | switch (Entity->getKind()) { |
| 6362 | case InitializedEntity::EK_Variable: |
| 6363 | |
| 6364 | return {Entity, LK_Extended}; |
| 6365 | |
| 6366 | case InitializedEntity::EK_Member: |
| 6367 | |
| 6368 | if (Entity->getParent()) |
| 6369 | return getEntityLifetime(Entity->getParent(), Entity); |
| 6370 | |
| 6371 | |
| 6372 | |
| 6373 | |
| 6374 | |
| 6375 | |
| 6376 | |
| 6377 | |
| 6378 | |
| 6379 | |
| 6380 | |
| 6381 | |
| 6382 | |
| 6383 | return {Entity, Entity->isDefaultMemberInitializer() ? LK_Extended |
| 6384 | : LK_MemInitializer}; |
| 6385 | |
| 6386 | case InitializedEntity::EK_Binding: |
| 6387 | |
| 6388 | |
| 6389 | return {Entity, LK_Extended}; |
| 6390 | |
| 6391 | case InitializedEntity::EK_Parameter: |
| 6392 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 6393 | |
| 6394 | |
| 6395 | |
| 6396 | return {nullptr, LK_FullExpression}; |
| 6397 | |
| 6398 | case InitializedEntity::EK_Result: |
| 6399 | |
| 6400 | |
| 6401 | |
| 6402 | return {nullptr, LK_Return}; |
| 6403 | |
| 6404 | case InitializedEntity::EK_StmtExprResult: |
| 6405 | |
| 6406 | |
| 6407 | return {nullptr, LK_StmtExprResult}; |
| 6408 | |
| 6409 | case InitializedEntity::EK_New: |
| 6410 | |
| 6411 | |
| 6412 | |
| 6413 | return {nullptr, LK_New}; |
| 6414 | |
| 6415 | case InitializedEntity::EK_Temporary: |
| 6416 | case InitializedEntity::EK_CompoundLiteralInit: |
| 6417 | case InitializedEntity::EK_RelatedResult: |
| 6418 | |
| 6419 | |
| 6420 | |
| 6421 | return {nullptr, LK_FullExpression}; |
| 6422 | |
| 6423 | case InitializedEntity::EK_ArrayElement: |
| 6424 | |
| 6425 | return getEntityLifetime(Entity->getParent(), InitField); |
| 6426 | |
| 6427 | case InitializedEntity::EK_Base: |
| 6428 | |
| 6429 | if (Entity->getParent()) |
| 6430 | return getEntityLifetime(Entity->getParent(), InitField); |
| 6431 | return {InitField, LK_MemInitializer}; |
| 6432 | |
| 6433 | case InitializedEntity::EK_Delegating: |
| 6434 | |
| 6435 | |
| 6436 | |
| 6437 | |
| 6438 | return {InitField, LK_MemInitializer}; |
| 6439 | |
| 6440 | case InitializedEntity::EK_BlockElement: |
| 6441 | case InitializedEntity::EK_LambdaToBlockConversionBlockElement: |
| 6442 | case InitializedEntity::EK_LambdaCapture: |
| 6443 | case InitializedEntity::EK_VectorElement: |
| 6444 | case InitializedEntity::EK_ComplexElement: |
| 6445 | return {nullptr, LK_FullExpression}; |
| 6446 | |
| 6447 | case InitializedEntity::EK_Exception: |
| 6448 | |
| 6449 | return {nullptr, LK_FullExpression}; |
| 6450 | } |
| 6451 | llvm_unreachable("unknown entity kind"); |
| 6452 | } |
| 6453 | |
| 6454 | namespace { |
| 6455 | enum ReferenceKind { |
| 6456 | |
| 6457 | RK_ReferenceBinding, |
| 6458 | |
| 6459 | |
| 6460 | RK_StdInitializerList, |
| 6461 | }; |
| 6462 | |
| 6463 | |
| 6464 | |
| 6465 | |
| 6466 | |
| 6467 | |
| 6468 | using Local = Expr*; |
| 6469 | |
| 6470 | |
| 6471 | |
| 6472 | |
| 6473 | struct IndirectLocalPathEntry { |
| 6474 | enum EntryKind { |
| 6475 | DefaultInit, |
| 6476 | AddressOf, |
| 6477 | VarInit, |
| 6478 | LValToRVal, |
| 6479 | LifetimeBoundCall, |
| 6480 | } Kind; |
| 6481 | Expr *E; |
| 6482 | const Decl *D = nullptr; |
| 6483 | IndirectLocalPathEntry() {} |
| 6484 | IndirectLocalPathEntry(EntryKind K, Expr *E) : Kind(K), E(E) {} |
| 6485 | IndirectLocalPathEntry(EntryKind K, Expr *E, const Decl *D) |
| 6486 | : Kind(K), E(E), D(D) {} |
| 6487 | }; |
| 6488 | |
| 6489 | using IndirectLocalPath = llvm::SmallVectorImpl<IndirectLocalPathEntry>; |
| 6490 | |
| 6491 | struct RevertToOldSizeRAII { |
| 6492 | IndirectLocalPath &Path; |
| 6493 | unsigned OldSize = Path.size(); |
| 6494 | RevertToOldSizeRAII(IndirectLocalPath &Path) : Path(Path) {} |
| 6495 | ~RevertToOldSizeRAII() { Path.resize(OldSize); } |
| 6496 | }; |
| 6497 | |
| 6498 | using LocalVisitor = llvm::function_ref<bool(IndirectLocalPath &Path, Local L, |
| 6499 | ReferenceKind RK)>; |
| 6500 | } |
| 6501 | |
| 6502 | static bool isVarOnPath(IndirectLocalPath &Path, VarDecl *VD) { |
| 6503 | for (auto E : Path) |
| 6504 | if (E.Kind == IndirectLocalPathEntry::VarInit && E.D == VD) |
| 6505 | return true; |
| 6506 | return false; |
| 6507 | } |
| 6508 | |
| 6509 | static bool pathContainsInit(IndirectLocalPath &Path) { |
| 6510 | return llvm::any_of(Path, [=](IndirectLocalPathEntry E) { |
| 6511 | return E.Kind == IndirectLocalPathEntry::DefaultInit || |
| 6512 | E.Kind == IndirectLocalPathEntry::VarInit; |
| 6513 | }); |
| 6514 | } |
| 6515 | |
| 6516 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6517 | Expr *Init, LocalVisitor Visit, |
| 6518 | bool RevisitSubinits); |
| 6519 | |
| 6520 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6521 | Expr *Init, ReferenceKind RK, |
| 6522 | LocalVisitor Visit); |
| 6523 | |
| 6524 | static bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) { |
| 6525 | const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); |
| 6526 | if (!TSI) |
| 6527 | return false; |
| 6528 | |
| 6529 | |
| 6530 | |
| 6531 | AttributedTypeLoc ATL; |
| 6532 | for (TypeLoc TL = TSI->getTypeLoc(); |
| 6533 | (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); |
| 6534 | TL = ATL.getModifiedLoc()) { |
| 6535 | if (ATL.getAttrAs<LifetimeBoundAttr>()) |
| 6536 | return true; |
| 6537 | } |
| 6538 | return false; |
| 6539 | } |
| 6540 | |
| 6541 | static void visitLifetimeBoundArguments(IndirectLocalPath &Path, Expr *Call, |
| 6542 | LocalVisitor Visit) { |
| 6543 | const FunctionDecl *Callee; |
| 6544 | ArrayRef<Expr*> Args; |
| 6545 | |
| 6546 | if (auto *CE = dyn_cast<CallExpr>(Call)) { |
| 6547 | Callee = CE->getDirectCallee(); |
| 6548 | Args = llvm::makeArrayRef(CE->getArgs(), CE->getNumArgs()); |
| 6549 | } else { |
| 6550 | auto *CCE = cast<CXXConstructExpr>(Call); |
| 6551 | Callee = CCE->getConstructor(); |
| 6552 | Args = llvm::makeArrayRef(CCE->getArgs(), CCE->getNumArgs()); |
| 6553 | } |
| 6554 | if (!Callee) |
| 6555 | return; |
| 6556 | |
| 6557 | Expr *ObjectArg = nullptr; |
| 6558 | if (isa<CXXOperatorCallExpr>(Call) && Callee->isCXXInstanceMember()) { |
| 6559 | ObjectArg = Args[0]; |
| 6560 | Args = Args.slice(1); |
| 6561 | } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Call)) { |
| 6562 | ObjectArg = MCE->getImplicitObjectArgument(); |
| 6563 | } |
| 6564 | |
| 6565 | auto VisitLifetimeBoundArg = [&](const Decl *D, Expr *Arg) { |
| 6566 | Path.push_back({IndirectLocalPathEntry::LifetimeBoundCall, Arg, D}); |
| 6567 | if (Arg->isGLValue()) |
| 6568 | visitLocalsRetainedByReferenceBinding(Path, Arg, RK_ReferenceBinding, |
| 6569 | Visit); |
| 6570 | else |
| 6571 | visitLocalsRetainedByInitializer(Path, Arg, Visit, true); |
| 6572 | Path.pop_back(); |
| 6573 | }; |
| 6574 | |
| 6575 | if (ObjectArg && implicitObjectParamIsLifetimeBound(Callee)) |
| 6576 | VisitLifetimeBoundArg(Callee, ObjectArg); |
| 6577 | |
| 6578 | for (unsigned I = 0, |
| 6579 | N = std::min<unsigned>(Callee->getNumParams(), Args.size()); |
| 6580 | I != N; ++I) { |
| 6581 | if (Callee->getParamDecl(I)->hasAttr<LifetimeBoundAttr>()) |
| 6582 | VisitLifetimeBoundArg(Callee->getParamDecl(I), Args[I]); |
| 6583 | } |
| 6584 | } |
| 6585 | |
| 6586 | |
| 6587 | |
| 6588 | static void visitLocalsRetainedByReferenceBinding(IndirectLocalPath &Path, |
| 6589 | Expr *Init, ReferenceKind RK, |
| 6590 | LocalVisitor Visit) { |
| 6591 | RevertToOldSizeRAII RAII(Path); |
| 6592 | |
| 6593 | |
| 6594 | Expr *Old; |
| 6595 | do { |
| 6596 | Old = Init; |
| 6597 | |
| 6598 | if (auto *FE = dyn_cast<FullExpr>(Init)) |
| 6599 | Init = FE->getSubExpr(); |
| 6600 | |
| 6601 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 6602 | |
| 6603 | if (ILE->isTransparent()) |
| 6604 | Init = ILE->getInit(0); |
| 6605 | } |
| 6606 | |
| 6607 | |
| 6608 | |
| 6609 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
| 6610 | |
| 6611 | |
| 6612 | |
| 6613 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 6614 | if (CE->getSubExpr()->isGLValue()) |
| 6615 | Init = CE->getSubExpr(); |
| 6616 | |
| 6617 | |
| 6618 | |
| 6619 | if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Init)) { |
| 6620 | Init = ASE->getBase(); |
| 6621 | auto *ICE = dyn_cast<ImplicitCastExpr>(Init); |
| 6622 | if (ICE && ICE->getCastKind() == CK_ArrayToPointerDecay) |
| 6623 | Init = ICE->getSubExpr(); |
| 6624 | else |
| 6625 | |
| 6626 | |
| 6627 | return visitLocalsRetainedByInitializer(Path, Init, Visit, true); |
| 6628 | } |
| 6629 | |
| 6630 | |
| 6631 | |
| 6632 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
| 6633 | Path.push_back( |
| 6634 | {IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
| 6635 | Init = DIE->getExpr(); |
| 6636 | } |
| 6637 | } while (Init != Old); |
| 6638 | |
| 6639 | if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 6640 | if (Visit(Path, Local(MTE), RK)) |
| 6641 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), Visit, |
| 6642 | true); |
| 6643 | } |
| 6644 | |
| 6645 | if (isa<CallExpr>(Init)) |
| 6646 | return visitLifetimeBoundArguments(Path, Init, Visit); |
| 6647 | |
| 6648 | switch (Init->getStmtClass()) { |
| 6649 | case Stmt::DeclRefExprClass: { |
| 6650 | |
| 6651 | |
| 6652 | auto *DRE = cast<DeclRefExpr>(Init); |
| 6653 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6654 | if (VD && VD->hasLocalStorage() && |
| 6655 | !DRE->refersToEnclosingVariableOrCapture()) { |
| 6656 | if (!VD->getType()->isReferenceType()) { |
| 6657 | Visit(Path, Local(DRE), RK); |
| 6658 | } else if (isa<ParmVarDecl>(DRE->getDecl())) { |
| 6659 | |
| 6660 | |
| 6661 | break; |
| 6662 | } else if (VD->getInit() && !isVarOnPath(Path, VD)) { |
| 6663 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6664 | visitLocalsRetainedByReferenceBinding(Path, VD->getInit(), |
| 6665 | RK_ReferenceBinding, Visit); |
| 6666 | } |
| 6667 | } |
| 6668 | break; |
| 6669 | } |
| 6670 | |
| 6671 | case Stmt::UnaryOperatorClass: { |
| 6672 | |
| 6673 | |
| 6674 | |
| 6675 | const UnaryOperator *U = cast<UnaryOperator>(Init); |
| 6676 | if (U->getOpcode() == UO_Deref) |
| 6677 | visitLocalsRetainedByInitializer(Path, U->getSubExpr(), Visit, true); |
| 6678 | break; |
| 6679 | } |
| 6680 | |
| 6681 | case Stmt::OMPArraySectionExprClass: { |
| 6682 | visitLocalsRetainedByInitializer( |
| 6683 | Path, cast<OMPArraySectionExpr>(Init)->getBase(), Visit, true); |
| 6684 | break; |
| 6685 | } |
| 6686 | |
| 6687 | case Stmt::ConditionalOperatorClass: |
| 6688 | case Stmt::BinaryConditionalOperatorClass: { |
| 6689 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6690 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6691 | visitLocalsRetainedByReferenceBinding(Path, C->getTrueExpr(), RK, Visit); |
| 6692 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6693 | visitLocalsRetainedByReferenceBinding(Path, C->getFalseExpr(), RK, Visit); |
| 6694 | break; |
| 6695 | } |
| 6696 | |
| 6697 | |
| 6698 | |
| 6699 | default: |
| 6700 | break; |
| 6701 | } |
| 6702 | } |
| 6703 | |
| 6704 | |
| 6705 | |
| 6706 | static void visitLocalsRetainedByInitializer(IndirectLocalPath &Path, |
| 6707 | Expr *Init, LocalVisitor Visit, |
| 6708 | bool RevisitSubinits) { |
| 6709 | RevertToOldSizeRAII RAII(Path); |
| 6710 | |
| 6711 | Expr *Old; |
| 6712 | do { |
| 6713 | Old = Init; |
| 6714 | |
| 6715 | |
| 6716 | |
| 6717 | if (auto *DIE = dyn_cast<CXXDefaultInitExpr>(Init)) { |
| 6718 | Path.push_back({IndirectLocalPathEntry::DefaultInit, DIE, DIE->getField()}); |
| 6719 | Init = DIE->getExpr(); |
| 6720 | } |
| 6721 | |
| 6722 | if (auto *FE = dyn_cast<FullExpr>(Init)) |
| 6723 | Init = FE->getSubExpr(); |
| 6724 | |
| 6725 | |
| 6726 | Init = const_cast<Expr *>(Init->skipRValueSubobjectAdjustments()); |
| 6727 | |
| 6728 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 6729 | Init = BTE->getSubExpr(); |
| 6730 | |
| 6731 | Init = Init->IgnoreParens(); |
| 6732 | |
| 6733 | |
| 6734 | if (auto *CE = dyn_cast<CastExpr>(Init)) { |
| 6735 | switch (CE->getCastKind()) { |
| 6736 | case CK_LValueToRValue: |
| 6737 | |
| 6738 | |
| 6739 | Path.push_back({IndirectLocalPathEntry::LValToRVal, CE}); |
| 6740 | return visitLocalsRetainedByReferenceBinding( |
| 6741 | Path, Init, RK_ReferenceBinding, |
| 6742 | [&](IndirectLocalPath &Path, Local L, ReferenceKind RK) -> bool { |
| 6743 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 6744 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 6745 | if (VD && VD->getType().isConstQualified() && VD->getInit() && |
| 6746 | !isVarOnPath(Path, VD)) { |
| 6747 | Path.push_back({IndirectLocalPathEntry::VarInit, DRE, VD}); |
| 6748 | visitLocalsRetainedByInitializer(Path, VD->getInit(), Visit, true); |
| 6749 | } |
| 6750 | } else if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L)) { |
| 6751 | if (MTE->getType().isConstQualified()) |
| 6752 | visitLocalsRetainedByInitializer(Path, MTE->GetTemporaryExpr(), |
| 6753 | Visit, true); |
| 6754 | } |
| 6755 | return false; |
| 6756 | }); |
| 6757 | |
| 6758 | |
| 6759 | |
| 6760 | |
| 6761 | |
| 6762 | case CK_NoOp: |
| 6763 | case CK_BitCast: |
| 6764 | case CK_BaseToDerived: |
| 6765 | case CK_DerivedToBase: |
| 6766 | case CK_UncheckedDerivedToBase: |
| 6767 | case CK_Dynamic: |
| 6768 | case CK_ToUnion: |
| 6769 | case CK_UserDefinedConversion: |
| 6770 | case CK_ConstructorConversion: |
| 6771 | case CK_IntegralToPointer: |
| 6772 | case CK_PointerToIntegral: |
| 6773 | case CK_VectorSplat: |
| 6774 | case CK_IntegralCast: |
| 6775 | case CK_CPointerToObjCPointerCast: |
| 6776 | case CK_BlockPointerToObjCPointerCast: |
| 6777 | case CK_AnyPointerToBlockPointerCast: |
| 6778 | case CK_AddressSpaceConversion: |
| 6779 | break; |
| 6780 | |
| 6781 | case CK_ArrayToPointerDecay: |
| 6782 | |
| 6783 | |
| 6784 | Path.push_back({IndirectLocalPathEntry::AddressOf, CE}); |
| 6785 | return visitLocalsRetainedByReferenceBinding(Path, CE->getSubExpr(), |
| 6786 | RK_ReferenceBinding, Visit); |
| 6787 | |
| 6788 | default: |
| 6789 | return; |
| 6790 | } |
| 6791 | |
| 6792 | Init = CE->getSubExpr(); |
| 6793 | } |
| 6794 | } while (Old != Init); |
| 6795 | |
| 6796 | |
| 6797 | |
| 6798 | |
| 6799 | if (auto *ILE = dyn_cast<CXXStdInitializerListExpr>(Init)) |
| 6800 | return visitLocalsRetainedByReferenceBinding(Path, ILE->getSubExpr(), |
| 6801 | RK_StdInitializerList, Visit); |
| 6802 | |
| 6803 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 6804 | |
| 6805 | |
| 6806 | |
| 6807 | if (!RevisitSubinits) |
| 6808 | return; |
| 6809 | |
| 6810 | if (ILE->isTransparent()) |
| 6811 | return visitLocalsRetainedByInitializer(Path, ILE->getInit(0), Visit, |
| 6812 | RevisitSubinits); |
| 6813 | |
| 6814 | if (ILE->getType()->isArrayType()) { |
| 6815 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
| 6816 | visitLocalsRetainedByInitializer(Path, ILE->getInit(I), Visit, |
| 6817 | RevisitSubinits); |
| 6818 | return; |
| 6819 | } |
| 6820 | |
| 6821 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
| 6822 | (0) . __assert_fail ("RD->isAggregate() && \"aggregate init on non-aggregate\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 6822, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 6823 | |
| 6824 | |
| 6825 | |
| 6826 | |
| 6827 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 6828 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
| 6829 | visitLocalsRetainedByReferenceBinding(Path, ILE->getInit(0), |
| 6830 | RK_ReferenceBinding, Visit); |
| 6831 | else { |
| 6832 | unsigned Index = 0; |
| 6833 | for (const auto *I : RD->fields()) { |
| 6834 | if (Index >= ILE->getNumInits()) |
| 6835 | break; |
| 6836 | if (I->isUnnamedBitfield()) |
| 6837 | continue; |
| 6838 | Expr *SubInit = ILE->getInit(Index); |
| 6839 | if (I->getType()->isReferenceType()) |
| 6840 | visitLocalsRetainedByReferenceBinding(Path, SubInit, |
| 6841 | RK_ReferenceBinding, Visit); |
| 6842 | else |
| 6843 | |
| 6844 | |
| 6845 | |
| 6846 | visitLocalsRetainedByInitializer(Path, SubInit, Visit, |
| 6847 | RevisitSubinits); |
| 6848 | ++Index; |
| 6849 | } |
| 6850 | } |
| 6851 | } |
| 6852 | return; |
| 6853 | } |
| 6854 | |
| 6855 | |
| 6856 | |
| 6857 | if (auto *LE = dyn_cast<LambdaExpr>(Init)) { |
| 6858 | for (Expr *E : LE->capture_inits()) { |
| 6859 | if (!E) |
| 6860 | continue; |
| 6861 | if (E->isGLValue()) |
| 6862 | visitLocalsRetainedByReferenceBinding(Path, E, RK_ReferenceBinding, |
| 6863 | Visit); |
| 6864 | else |
| 6865 | visitLocalsRetainedByInitializer(Path, E, Visit, true); |
| 6866 | } |
| 6867 | } |
| 6868 | |
| 6869 | if (isa<CallExpr>(Init) || isa<CXXConstructExpr>(Init)) |
| 6870 | return visitLifetimeBoundArguments(Path, Init, Visit); |
| 6871 | |
| 6872 | switch (Init->getStmtClass()) { |
| 6873 | case Stmt::UnaryOperatorClass: { |
| 6874 | auto *UO = cast<UnaryOperator>(Init); |
| 6875 | |
| 6876 | |
| 6877 | if (UO->getOpcode() == UO_AddrOf) { |
| 6878 | |
| 6879 | |
| 6880 | |
| 6881 | if (isa<MaterializeTemporaryExpr>(UO->getSubExpr())) |
| 6882 | return; |
| 6883 | |
| 6884 | Path.push_back({IndirectLocalPathEntry::AddressOf, UO}); |
| 6885 | visitLocalsRetainedByReferenceBinding(Path, UO->getSubExpr(), |
| 6886 | RK_ReferenceBinding, Visit); |
| 6887 | } |
| 6888 | break; |
| 6889 | } |
| 6890 | |
| 6891 | case Stmt::BinaryOperatorClass: { |
| 6892 | |
| 6893 | auto *BO = cast<BinaryOperator>(Init); |
| 6894 | BinaryOperatorKind BOK = BO->getOpcode(); |
| 6895 | if (!BO->getType()->isPointerType() || (BOK != BO_Add && BOK != BO_Sub)) |
| 6896 | break; |
| 6897 | |
| 6898 | if (BO->getLHS()->getType()->isPointerType()) |
| 6899 | visitLocalsRetainedByInitializer(Path, BO->getLHS(), Visit, true); |
| 6900 | else if (BO->getRHS()->getType()->isPointerType()) |
| 6901 | visitLocalsRetainedByInitializer(Path, BO->getRHS(), Visit, true); |
| 6902 | break; |
| 6903 | } |
| 6904 | |
| 6905 | case Stmt::ConditionalOperatorClass: |
| 6906 | case Stmt::BinaryConditionalOperatorClass: { |
| 6907 | auto *C = cast<AbstractConditionalOperator>(Init); |
| 6908 | |
| 6909 | |
| 6910 | if (!C->getTrueExpr()->getType()->isVoidType()) |
| 6911 | visitLocalsRetainedByInitializer(Path, C->getTrueExpr(), Visit, true); |
| 6912 | if (!C->getFalseExpr()->getType()->isVoidType()) |
| 6913 | visitLocalsRetainedByInitializer(Path, C->getFalseExpr(), Visit, true); |
| 6914 | break; |
| 6915 | } |
| 6916 | |
| 6917 | case Stmt::BlockExprClass: |
| 6918 | if (cast<BlockExpr>(Init)->getBlockDecl()->hasCaptures()) { |
| 6919 | |
| 6920 | Visit(Path, Local(cast<BlockExpr>(Init)), RK_ReferenceBinding); |
| 6921 | } |
| 6922 | break; |
| 6923 | |
| 6924 | case Stmt::AddrLabelExprClass: |
| 6925 | |
| 6926 | Visit(Path, Local(cast<AddrLabelExpr>(Init)), RK_ReferenceBinding); |
| 6927 | break; |
| 6928 | |
| 6929 | default: |
| 6930 | break; |
| 6931 | } |
| 6932 | } |
| 6933 | |
| 6934 | |
| 6935 | |
| 6936 | static bool shouldLifetimeExtendThroughPath(const IndirectLocalPath &Path) { |
| 6937 | for (auto Elem : Path) { |
| 6938 | if (Elem.Kind != IndirectLocalPathEntry::DefaultInit) |
| 6939 | return false; |
| 6940 | } |
| 6941 | return true; |
| 6942 | } |
| 6943 | |
| 6944 | |
| 6945 | static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I, |
| 6946 | Expr *E) { |
| 6947 | for (unsigned N = Path.size(); I != N; ++I) { |
| 6948 | switch (Path[I].Kind) { |
| 6949 | case IndirectLocalPathEntry::AddressOf: |
| 6950 | case IndirectLocalPathEntry::LValToRVal: |
| 6951 | case IndirectLocalPathEntry::LifetimeBoundCall: |
| 6952 | |
| 6953 | |
| 6954 | break; |
| 6955 | |
| 6956 | case IndirectLocalPathEntry::DefaultInit: |
| 6957 | case IndirectLocalPathEntry::VarInit: |
| 6958 | return Path[I].E->getSourceRange(); |
| 6959 | } |
| 6960 | } |
| 6961 | return E->getSourceRange(); |
| 6962 | } |
| 6963 | |
| 6964 | void Sema::checkInitializerLifetime(const InitializedEntity &Entity, |
| 6965 | Expr *Init) { |
| 6966 | LifetimeResult LR = getEntityLifetime(&Entity); |
| 6967 | LifetimeKind LK = LR.getInt(); |
| 6968 | const InitializedEntity *ExtendingEntity = LR.getPointer(); |
| 6969 | |
| 6970 | |
| 6971 | |
| 6972 | if (LK == LK_FullExpression) |
| 6973 | return; |
| 6974 | |
| 6975 | auto TemporaryVisitor = [&](IndirectLocalPath &Path, Local L, |
| 6976 | ReferenceKind RK) -> bool { |
| 6977 | SourceRange DiagRange = nextPathEntryRange(Path, 0, L); |
| 6978 | SourceLocation DiagLoc = DiagRange.getBegin(); |
| 6979 | |
| 6980 | switch (LK) { |
| 6981 | case LK_FullExpression: |
| 6982 | llvm_unreachable("already handled this"); |
| 6983 | |
| 6984 | case LK_Extended: { |
| 6985 | auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L); |
| 6986 | if (!MTE) { |
| 6987 | |
| 6988 | |
| 6989 | |
| 6990 | |
| 6991 | |
| 6992 | return false; |
| 6993 | } |
| 6994 | |
| 6995 | |
| 6996 | if (Path.empty()) { |
| 6997 | |
| 6998 | |
| 6999 | MTE->setExtendingDecl(ExtendingEntity->getDecl(), |
| 7000 | ExtendingEntity->allocateManglingNumber()); |
| 7001 | |
| 7002 | return true; |
| 7003 | } |
| 7004 | |
| 7005 | if (shouldLifetimeExtendThroughPath(Path)) { |
| 7006 | |
| 7007 | |
| 7008 | |
| 7009 | |
| 7010 | |
| 7011 | |
| 7012 | Diag(DiagLoc, diag::warn_unsupported_lifetime_extension) |
| 7013 | << RK << DiagRange; |
| 7014 | } else { |
| 7015 | |
| 7016 | |
| 7017 | |
| 7018 | if (pathContainsInit(Path)) |
| 7019 | return false; |
| 7020 | |
| 7021 | Diag(DiagLoc, diag::warn_dangling_variable) |
| 7022 | << RK << !Entity.getParent() |
| 7023 | << ExtendingEntity->getDecl()->isImplicit() |
| 7024 | << ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange; |
| 7025 | } |
| 7026 | break; |
| 7027 | } |
| 7028 | |
| 7029 | case LK_MemInitializer: { |
| 7030 | if (isa<MaterializeTemporaryExpr>(L)) { |
| 7031 | |
| 7032 | |
| 7033 | |
| 7034 | if (auto *ExtendingDecl = |
| 7035 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 7036 | bool IsSubobjectMember = ExtendingEntity != &Entity; |
| 7037 | Diag(DiagLoc, shouldLifetimeExtendThroughPath(Path) |
| 7038 | ? diag::err_dangling_member |
| 7039 | : diag::warn_dangling_member) |
| 7040 | << ExtendingDecl << IsSubobjectMember << RK << DiagRange; |
| 7041 | |
| 7042 | |
| 7043 | |
| 7044 | if (Path.empty() || |
| 7045 | Path.back().Kind != IndirectLocalPathEntry::DefaultInit) { |
| 7046 | Diag(ExtendingDecl->getLocation(), |
| 7047 | diag::note_lifetime_extending_member_declared_here) |
| 7048 | << RK << IsSubobjectMember; |
| 7049 | } |
| 7050 | } else { |
| 7051 | |
| 7052 | |
| 7053 | |
| 7054 | |
| 7055 | |
| 7056 | |
| 7057 | return false; |
| 7058 | } |
| 7059 | } else { |
| 7060 | |
| 7061 | |
| 7062 | |
| 7063 | if (pathContainsInit(Path)) |
| 7064 | return false; |
| 7065 | |
| 7066 | auto *DRE = dyn_cast<DeclRefExpr>(L); |
| 7067 | auto *VD = DRE ? dyn_cast<VarDecl>(DRE->getDecl()) : nullptr; |
| 7068 | if (!VD) { |
| 7069 | |
| 7070 | |
| 7071 | return false; |
| 7072 | } |
| 7073 | |
| 7074 | if (auto *Member = |
| 7075 | ExtendingEntity ? ExtendingEntity->getDecl() : nullptr) { |
| 7076 | bool IsPointer = Member->getType()->isAnyPointerType(); |
| 7077 | Diag(DiagLoc, IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 7078 | : diag::warn_bind_ref_member_to_parameter) |
| 7079 | << Member << VD << isa<ParmVarDecl>(VD) << DiagRange; |
| 7080 | Diag(Member->getLocation(), |
| 7081 | diag::note_ref_or_ptr_member_declared_here) |
| 7082 | << (unsigned)IsPointer; |
| 7083 | } |
| 7084 | } |
| 7085 | break; |
| 7086 | } |
| 7087 | |
| 7088 | case LK_New: |
| 7089 | if (isa<MaterializeTemporaryExpr>(L)) { |
| 7090 | Diag(DiagLoc, RK == RK_ReferenceBinding |
| 7091 | ? diag::warn_new_dangling_reference |
| 7092 | : diag::warn_new_dangling_initializer_list) |
| 7093 | << !Entity.getParent() << DiagRange; |
| 7094 | } else { |
| 7095 | |
| 7096 | return false; |
| 7097 | } |
| 7098 | break; |
| 7099 | |
| 7100 | case LK_Return: |
| 7101 | case LK_StmtExprResult: |
| 7102 | if (auto *DRE = dyn_cast<DeclRefExpr>(L)) { |
| 7103 | |
| 7104 | |
| 7105 | if (LK == LK_StmtExprResult) |
| 7106 | return false; |
| 7107 | Diag(DiagLoc, diag::warn_ret_stack_addr_ref) |
| 7108 | << Entity.getType()->isReferenceType() << DRE->getDecl() |
| 7109 | << isa<ParmVarDecl>(DRE->getDecl()) << DiagRange; |
| 7110 | } else if (isa<BlockExpr>(L)) { |
| 7111 | Diag(DiagLoc, diag::err_ret_local_block) << DiagRange; |
| 7112 | } else if (isa<AddrLabelExpr>(L)) { |
| 7113 | |
| 7114 | |
| 7115 | if (LK == LK_StmtExprResult) |
| 7116 | return false; |
| 7117 | Diag(DiagLoc, diag::warn_ret_addr_label) << DiagRange; |
| 7118 | } else { |
| 7119 | Diag(DiagLoc, diag::warn_ret_local_temp_addr_ref) |
| 7120 | << Entity.getType()->isReferenceType() << DiagRange; |
| 7121 | } |
| 7122 | break; |
| 7123 | } |
| 7124 | |
| 7125 | for (unsigned I = 0; I != Path.size(); ++I) { |
| 7126 | auto Elem = Path[I]; |
| 7127 | |
| 7128 | switch (Elem.Kind) { |
| 7129 | case IndirectLocalPathEntry::AddressOf: |
| 7130 | case IndirectLocalPathEntry::LValToRVal: |
| 7131 | |
| 7132 | |
| 7133 | break; |
| 7134 | |
| 7135 | case IndirectLocalPathEntry::LifetimeBoundCall: |
| 7136 | |
| 7137 | break; |
| 7138 | |
| 7139 | case IndirectLocalPathEntry::DefaultInit: { |
| 7140 | auto *FD = cast<FieldDecl>(Elem.D); |
| 7141 | Diag(FD->getLocation(), diag::note_init_with_default_member_initalizer) |
| 7142 | << FD << nextPathEntryRange(Path, I + 1, L); |
| 7143 | break; |
| 7144 | } |
| 7145 | |
| 7146 | case IndirectLocalPathEntry::VarInit: |
| 7147 | const VarDecl *VD = cast<VarDecl>(Elem.D); |
| 7148 | Diag(VD->getLocation(), diag::note_local_var_initializer) |
| 7149 | << VD->getType()->isReferenceType() |
| 7150 | << VD->isImplicit() << VD->getDeclName() |
| 7151 | << nextPathEntryRange(Path, I + 1, L); |
| 7152 | break; |
| 7153 | } |
| 7154 | } |
| 7155 | |
| 7156 | |
| 7157 | |
| 7158 | return false; |
| 7159 | }; |
| 7160 | |
| 7161 | llvm::SmallVector<IndirectLocalPathEntry, 8> Path; |
| 7162 | if (Init->isGLValue()) |
| 7163 | visitLocalsRetainedByReferenceBinding(Path, Init, RK_ReferenceBinding, |
| 7164 | TemporaryVisitor); |
| 7165 | else |
| 7166 | visitLocalsRetainedByInitializer(Path, Init, TemporaryVisitor, false); |
| 7167 | } |
| 7168 | |
| 7169 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 7170 | const ImplicitConversionSequence &ICS, |
| 7171 | QualType PreNarrowingType, |
| 7172 | QualType EntityType, |
| 7173 | const Expr *PostInit); |
| 7174 | |
| 7175 | |
| 7176 | static void CheckMoveOnConstruction(Sema &S, const Expr *InitExpr, |
| 7177 | bool IsReturnStmt) { |
| 7178 | if (!InitExpr) |
| 7179 | return; |
| 7180 | |
| 7181 | if (S.inTemplateInstantiation()) |
| 7182 | return; |
| 7183 | |
| 7184 | QualType DestType = InitExpr->getType(); |
| 7185 | if (!DestType->isRecordType()) |
| 7186 | return; |
| 7187 | |
| 7188 | unsigned DiagID = 0; |
| 7189 | if (IsReturnStmt) { |
| 7190 | const CXXConstructExpr *CCE = |
| 7191 | dyn_cast<CXXConstructExpr>(InitExpr->IgnoreParens()); |
| 7192 | if (!CCE || CCE->getNumArgs() != 1) |
| 7193 | return; |
| 7194 | |
| 7195 | if (!CCE->getConstructor()->isCopyOrMoveConstructor()) |
| 7196 | return; |
| 7197 | |
| 7198 | InitExpr = CCE->getArg(0)->IgnoreImpCasts(); |
| 7199 | } |
| 7200 | |
| 7201 | |
| 7202 | const CallExpr *CE = dyn_cast<CallExpr>(InitExpr->IgnoreParens()); |
| 7203 | if (!CE || !CE->isCallToStdMove()) |
| 7204 | return; |
| 7205 | |
| 7206 | const Expr *Arg = CE->getArg(0)->IgnoreImplicit(); |
| 7207 | |
| 7208 | if (IsReturnStmt) { |
| 7209 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg->IgnoreParenImpCasts()); |
| 7210 | if (!DRE || DRE->refersToEnclosingVariableOrCapture()) |
| 7211 | return; |
| 7212 | |
| 7213 | const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7214 | if (!VD || !VD->hasLocalStorage()) |
| 7215 | return; |
| 7216 | |
| 7217 | |
| 7218 | if (VD->hasAttr<BlocksAttr>()) |
| 7219 | return; |
| 7220 | |
| 7221 | QualType SourceType = VD->getType(); |
| 7222 | if (!SourceType->isRecordType()) |
| 7223 | return; |
| 7224 | |
| 7225 | if (!S.Context.hasSameUnqualifiedType(DestType, SourceType)) { |
| 7226 | return; |
| 7227 | } |
| 7228 | |
| 7229 | |
| 7230 | |
| 7231 | if (isa<ParmVarDecl>(VD)) |
| 7232 | DiagID = diag::warn_redundant_move_on_return; |
| 7233 | else |
| 7234 | DiagID = diag::warn_pessimizing_move_on_return; |
| 7235 | } else { |
| 7236 | DiagID = diag::warn_pessimizing_move_on_initialization; |
| 7237 | const Expr *ArgStripped = Arg->IgnoreImplicit()->IgnoreParens(); |
| 7238 | if (!ArgStripped->isRValue() || !ArgStripped->getType()->isRecordType()) |
| 7239 | return; |
| 7240 | } |
| 7241 | |
| 7242 | S.Diag(CE->getBeginLoc(), DiagID); |
| 7243 | |
| 7244 | |
| 7245 | |
| 7246 | SourceLocation CallBegin = CE->getCallee()->getBeginLoc(); |
| 7247 | if (CallBegin.isMacroID()) |
| 7248 | return; |
| 7249 | SourceLocation RParen = CE->getRParenLoc(); |
| 7250 | if (RParen.isMacroID()) |
| 7251 | return; |
| 7252 | SourceLocation LParen; |
| 7253 | SourceLocation ArgLoc = Arg->getBeginLoc(); |
| 7254 | |
| 7255 | |
| 7256 | |
| 7257 | |
| 7258 | while (ArgLoc.isMacroID() && |
| 7259 | S.getSourceManager().isAtStartOfImmediateMacroExpansion(ArgLoc)) { |
| 7260 | ArgLoc = S.getSourceManager().getImmediateExpansionRange(ArgLoc).getBegin(); |
| 7261 | } |
| 7262 | |
| 7263 | if (LParen.isMacroID()) |
| 7264 | return; |
| 7265 | |
| 7266 | LParen = ArgLoc.getLocWithOffset(-1); |
| 7267 | |
| 7268 | S.Diag(CE->getBeginLoc(), diag::note_remove_move) |
| 7269 | << FixItHint::CreateRemoval(SourceRange(CallBegin, LParen)) |
| 7270 | << FixItHint::CreateRemoval(SourceRange(RParen, RParen)); |
| 7271 | } |
| 7272 | |
| 7273 | static void CheckForNullPointerDereference(Sema &S, const Expr *E) { |
| 7274 | |
| 7275 | |
| 7276 | |
| 7277 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 7278 | if (UO->getOpcode() == UO_Deref && |
| 7279 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7280 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) { |
| 7281 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7282 | S.PDiag(diag::warn_binding_null_to_reference) |
| 7283 | << UO->getSubExpr()->getSourceRange()); |
| 7284 | } |
| 7285 | } |
| 7286 | |
| 7287 | MaterializeTemporaryExpr * |
| 7288 | Sema::CreateMaterializeTemporaryExpr(QualType T, Expr *Temporary, |
| 7289 | bool BoundToLvalueReference) { |
| 7290 | auto MTE = new (Context) |
| 7291 | MaterializeTemporaryExpr(T, Temporary, BoundToLvalueReference); |
| 7292 | |
| 7293 | |
| 7294 | |
| 7295 | |
| 7296 | |
| 7297 | |
| 7298 | |
| 7299 | Cleanup.setExprNeedsCleanups(false); |
| 7300 | return MTE; |
| 7301 | } |
| 7302 | |
| 7303 | ExprResult Sema::TemporaryMaterializationConversion(Expr *E) { |
| 7304 | |
| 7305 | |
| 7306 | |
| 7307 | |
| 7308 | if (!E->isRValue() || !getLangOpts().CPlusPlus11) |
| 7309 | return E; |
| 7310 | |
| 7311 | |
| 7312 | |
| 7313 | |
| 7314 | QualType T = E->getType(); |
| 7315 | if (RequireCompleteType(E->getExprLoc(), T, diag::err_incomplete_type)) |
| 7316 | return ExprError(); |
| 7317 | |
| 7318 | return CreateMaterializeTemporaryExpr(E->getType(), E, false); |
| 7319 | } |
| 7320 | |
| 7321 | ExprResult Sema::PerformQualificationConversion(Expr *E, QualType Ty, |
| 7322 | ExprValueKind VK, |
| 7323 | CheckedConversionKind CCK) { |
| 7324 | CastKind CK = (Ty.getAddressSpace() != E->getType().getAddressSpace()) |
| 7325 | ? CK_AddressSpaceConversion |
| 7326 | : CK_NoOp; |
| 7327 | return ImpCastExprToType(E, Ty, CK, VK, , CCK); |
| 7328 | } |
| 7329 | |
| 7330 | ExprResult InitializationSequence::Perform(Sema &S, |
| 7331 | const InitializedEntity &Entity, |
| 7332 | const InitializationKind &Kind, |
| 7333 | MultiExprArg Args, |
| 7334 | QualType *ResultType) { |
| 7335 | if (Failed()) { |
| 7336 | Diagnose(S, Entity, Kind, Args); |
| 7337 | return ExprError(); |
| 7338 | } |
| 7339 | if (!ZeroInitializationFixit.empty()) { |
| 7340 | unsigned DiagID = diag::err_default_init_const; |
| 7341 | if (Decl *D = Entity.getDecl()) |
| 7342 | if (S.getLangOpts().MSVCCompat && D->hasAttr<SelectAnyAttr>()) |
| 7343 | DiagID = diag::ext_default_init_const; |
| 7344 | |
| 7345 | |
| 7346 | |
| 7347 | |
| 7348 | QualType DestType = Entity.getType(); |
| 7349 | S.Diag(Kind.getLocation(), DiagID) |
| 7350 | << DestType << (bool)DestType->getAs<RecordType>() |
| 7351 | << FixItHint::CreateInsertion(ZeroInitializationFixitLoc, |
| 7352 | ZeroInitializationFixit); |
| 7353 | } |
| 7354 | |
| 7355 | if (getKind() == DependentSequence) { |
| 7356 | |
| 7357 | |
| 7358 | |
| 7359 | if (ResultType && !Entity.getType()->isDependentType() && |
| 7360 | Args.size() == 1) { |
| 7361 | QualType DeclType = Entity.getType(); |
| 7362 | if (const IncompleteArrayType *ArrayT |
| 7363 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 7364 | |
| 7365 | |
| 7366 | |
| 7367 | |
| 7368 | |
| 7369 | |
| 7370 | |
| 7371 | if (isa<InitListExpr>((Expr *)Args[0])) { |
| 7372 | SourceRange Brackets; |
| 7373 | |
| 7374 | |
| 7375 | if (auto *DD = dyn_cast_or_null<DeclaratorDecl>(Entity.getDecl())) { |
| 7376 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 7377 | TypeLoc TL = TInfo->getTypeLoc(); |
| 7378 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 7379 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 7380 | Brackets = ArrayLoc.getBracketsRange(); |
| 7381 | } |
| 7382 | } |
| 7383 | |
| 7384 | *ResultType |
| 7385 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 7386 | , |
| 7387 | ArrayT->getSizeModifier(), |
| 7388 | ArrayT->getIndexTypeCVRQualifiers(), |
| 7389 | Brackets); |
| 7390 | } |
| 7391 | |
| 7392 | } |
| 7393 | } |
| 7394 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 7395 | !Kind.isExplicitCast()) { |
| 7396 | |
| 7397 | SourceRange ParenRange = Kind.getParenOrBraceRange(); |
| 7398 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
| 7399 | Args); |
| 7400 | } |
| 7401 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 7402 | Kind.isExplicitCast() || |
| 7403 | Kind.getKind() == InitializationKind::IK_DirectList); |
| 7404 | return ExprResult(Args[0]); |
| 7405 | } |
| 7406 | |
| 7407 | |
| 7408 | if (Steps.empty()) |
| 7409 | return ExprResult((Expr *)nullptr); |
| 7410 | |
| 7411 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
| 7412 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
| 7413 | !Entity.isParameterKind()) { |
| 7414 | |
| 7415 | |
| 7416 | |
| 7417 | Expr *Init = Args[0]; |
| 7418 | S.Diag(Init->getBeginLoc(), diag::warn_cxx98_compat_reference_list_init) |
| 7419 | << Init->getSourceRange(); |
| 7420 | } |
| 7421 | |
| 7422 | |
| 7423 | QualType ETy = Entity.getType(); |
| 7424 | Qualifiers TyQualifiers = ETy.getQualifiers(); |
| 7425 | bool HasGlobalAS = TyQualifiers.hasAddressSpace() && |
| 7426 | TyQualifiers.getAddressSpace() == LangAS::opencl_global; |
| 7427 | |
| 7428 | if (S.getLangOpts().OpenCLVersion >= 200 && |
| 7429 | ETy->isAtomicType() && !HasGlobalAS && |
| 7430 | Entity.getKind() == InitializedEntity::EK_Variable && Args.size() > 0) { |
| 7431 | S.Diag(Args[0]->getBeginLoc(), diag::err_opencl_atomic_init) |
| 7432 | << 1 |
| 7433 | << SourceRange(Entity.getDecl()->getBeginLoc(), Args[0]->getEndLoc()); |
| 7434 | return ExprError(); |
| 7435 | } |
| 7436 | |
| 7437 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 7438 | |
| 7439 | |
| 7440 | |
| 7441 | if (ResultType) |
| 7442 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
| 7443 | Entity.getType(); |
| 7444 | |
| 7445 | ExprResult CurInit((Expr *)nullptr); |
| 7446 | SmallVector<Expr*, 4> ArrayLoopCommonExprs; |
| 7447 | |
| 7448 | |
| 7449 | |
| 7450 | |
| 7451 | switch (Steps.front().Kind) { |
| 7452 | case SK_ResolveAddressOfOverloadedFunction: |
| 7453 | case SK_CastDerivedToBaseRValue: |
| 7454 | case SK_CastDerivedToBaseXValue: |
| 7455 | case SK_CastDerivedToBaseLValue: |
| 7456 | case SK_BindReference: |
| 7457 | case SK_BindReferenceToTemporary: |
| 7458 | case SK_FinalCopy: |
| 7459 | case SK_ExtraneousCopyToTemporary: |
| 7460 | case SK_UserConversion: |
| 7461 | case SK_QualificationConversionLValue: |
| 7462 | case SK_QualificationConversionXValue: |
| 7463 | case SK_QualificationConversionRValue: |
| 7464 | case SK_AtomicConversion: |
| 7465 | case SK_LValueToRValue: |
| 7466 | case SK_ConversionSequence: |
| 7467 | case SK_ConversionSequenceNoNarrowing: |
| 7468 | case SK_ListInitialization: |
| 7469 | case SK_UnwrapInitList: |
| 7470 | case SK_RewrapInitList: |
| 7471 | case SK_CAssignment: |
| 7472 | case SK_StringInit: |
| 7473 | case SK_ObjCObjectConversion: |
| 7474 | case SK_ArrayLoopIndex: |
| 7475 | case SK_ArrayLoopInit: |
| 7476 | case SK_ArrayInit: |
| 7477 | case SK_GNUArrayInit: |
| 7478 | case SK_ParenthesizedArrayInit: |
| 7479 | case SK_PassByIndirectCopyRestore: |
| 7480 | case SK_PassByIndirectRestore: |
| 7481 | case SK_ProduceObjCObject: |
| 7482 | case SK_StdInitializerList: |
| 7483 | case SK_OCLSamplerInit: |
| 7484 | case SK_OCLZeroOpaqueType: { |
| 7485 | assert(Args.size() == 1); |
| 7486 | CurInit = Args[0]; |
| 7487 | if (!CurInit.get()) return ExprError(); |
| 7488 | break; |
| 7489 | } |
| 7490 | |
| 7491 | case SK_ConstructorInitialization: |
| 7492 | case SK_ConstructorInitializationFromList: |
| 7493 | case SK_StdInitializerListConstructorCall: |
| 7494 | case SK_ZeroInitialization: |
| 7495 | break; |
| 7496 | } |
| 7497 | |
| 7498 | |
| 7499 | |
| 7500 | |
| 7501 | EnterExpressionEvaluationContext Evaluated( |
| 7502 | S, EnterExpressionEvaluationContext::InitList, |
| 7503 | CurInit.get() && isa<InitListExpr>(CurInit.get())); |
| 7504 | |
| 7505 | |
| 7506 | |
| 7507 | |
| 7508 | auto checkAbstractType = [&](QualType T) -> bool { |
| 7509 | if (Entity.getKind() == InitializedEntity::EK_Base || |
| 7510 | Entity.getKind() == InitializedEntity::EK_Delegating) |
| 7511 | return false; |
| 7512 | return S.RequireNonAbstractType(Kind.getLocation(), T, |
| 7513 | diag::err_allocation_of_abstract_type); |
| 7514 | }; |
| 7515 | |
| 7516 | |
| 7517 | |
| 7518 | bool ConstructorInitRequiresZeroInit = false; |
| 7519 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 7520 | Step != StepEnd; ++Step) { |
| 7521 | if (CurInit.isInvalid()) |
| 7522 | return ExprError(); |
| 7523 | |
| 7524 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
| 7525 | |
| 7526 | switch (Step->Kind) { |
| 7527 | case SK_ResolveAddressOfOverloadedFunction: |
| 7528 | |
| 7529 | |
| 7530 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
| 7531 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 7532 | return ExprError(); |
| 7533 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
| 7534 | Step->Function.FoundDecl, |
| 7535 | Step->Function.Function); |
| 7536 | break; |
| 7537 | |
| 7538 | case SK_CastDerivedToBaseRValue: |
| 7539 | case SK_CastDerivedToBaseXValue: |
| 7540 | case SK_CastDerivedToBaseLValue: { |
| 7541 | |
| 7542 | |
| 7543 | |
| 7544 | CXXCastPath BasePath; |
| 7545 | |
| 7546 | |
| 7547 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 7548 | if (S.CheckDerivedToBaseConversion( |
| 7549 | SourceType, Step->Type, CurInit.get()->getBeginLoc(), |
| 7550 | CurInit.get()->getSourceRange(), &BasePath, IgnoreBaseAccess)) |
| 7551 | return ExprError(); |
| 7552 | |
| 7553 | ExprValueKind VK = |
| 7554 | Step->Kind == SK_CastDerivedToBaseLValue ? |
| 7555 | VK_LValue : |
| 7556 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
| 7557 | VK_XValue : |
| 7558 | VK_RValue); |
| 7559 | CurInit = |
| 7560 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 7561 | CurInit.get(), &BasePath, VK); |
| 7562 | break; |
| 7563 | } |
| 7564 | |
| 7565 | case SK_BindReference: |
| 7566 | |
| 7567 | |
| 7568 | |
| 7569 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
| 7570 | return ExprError(); |
| 7571 | |
| 7572 | |
| 7573 | |
| 7574 | |
| 7575 | if (CurInit.get()->getType()->isFunctionProtoType()) { |
| 7576 | if (auto *DRE = dyn_cast<DeclRefExpr>(CurInit.get()->IgnoreParens())) { |
| 7577 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 7578 | if (!S.checkAddressOfFunctionIsAvailable(FD, , |
| 7579 | DRE->getBeginLoc())) |
| 7580 | return ExprError(); |
| 7581 | } |
| 7582 | } |
| 7583 | } |
| 7584 | |
| 7585 | CheckForNullPointerDereference(S, CurInit.get()); |
| 7586 | break; |
| 7587 | |
| 7588 | case SK_BindReferenceToTemporary: { |
| 7589 | |
| 7590 | (0) . __assert_fail ("CurInit.get()->isRValue() && \"not a temporary\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 7590, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurInit.get()->isRValue() && "not a temporary"); |
| 7591 | |
| 7592 | |
| 7593 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
| 7594 | return ExprError(); |
| 7595 | |
| 7596 | |
| 7597 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
| 7598 | Step->Type, CurInit.get(), Entity.getType()->isLValueReferenceType()); |
| 7599 | CurInit = MTE; |
| 7600 | |
| 7601 | |
| 7602 | |
| 7603 | if (MTE->getStorageDuration() == SD_Automatic && |
| 7604 | MTE->getType().isDestructedType()) |
| 7605 | S.Cleanup.setExprNeedsCleanups(true); |
| 7606 | break; |
| 7607 | } |
| 7608 | |
| 7609 | case SK_FinalCopy: |
| 7610 | if (checkAbstractType(Step->Type)) |
| 7611 | return ExprError(); |
| 7612 | |
| 7613 | |
| 7614 | |
| 7615 | |
| 7616 | |
| 7617 | |
| 7618 | if (!shouldBindAsTemporary(Entity)) |
| 7619 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
| 7620 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
| 7621 | ); |
| 7622 | break; |
| 7623 | |
| 7624 | case SK_ExtraneousCopyToTemporary: |
| 7625 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
| 7626 | ); |
| 7627 | break; |
| 7628 | |
| 7629 | case SK_UserConversion: { |
| 7630 | |
| 7631 | |
| 7632 | CastKind CastKind; |
| 7633 | FunctionDecl *Fn = Step->Function.Function; |
| 7634 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
| 7635 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
| 7636 | bool CreatedObject = false; |
| 7637 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
| 7638 | |
| 7639 | SmallVector<Expr*, 8> ConstructorArgs; |
| 7640 | SourceLocation Loc = CurInit.get()->getBeginLoc(); |
| 7641 | |
| 7642 | |
| 7643 | |
| 7644 | Expr *Arg = CurInit.get(); |
| 7645 | if (S.CompleteConstructorCall(Constructor, |
| 7646 | MultiExprArg(&Arg, 1), |
| 7647 | Loc, ConstructorArgs)) |
| 7648 | return ExprError(); |
| 7649 | |
| 7650 | |
| 7651 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, |
| 7652 | FoundFn, Constructor, |
| 7653 | ConstructorArgs, |
| 7654 | HadMultipleCandidates, |
| 7655 | false, |
| 7656 | false, |
| 7657 | false, |
| 7658 | CXXConstructExpr::CK_Complete, |
| 7659 | SourceRange()); |
| 7660 | if (CurInit.isInvalid()) |
| 7661 | return ExprError(); |
| 7662 | |
| 7663 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, FoundFn, |
| 7664 | Entity); |
| 7665 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7666 | return ExprError(); |
| 7667 | |
| 7668 | CastKind = CK_ConstructorConversion; |
| 7669 | CreatedObject = true; |
| 7670 | } else { |
| 7671 | |
| 7672 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
| 7673 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
| 7674 | FoundFn); |
| 7675 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 7676 | return ExprError(); |
| 7677 | |
| 7678 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 7679 | HadMultipleCandidates); |
| 7680 | if (CurInit.isInvalid()) |
| 7681 | return ExprError(); |
| 7682 | |
| 7683 | CastKind = CK_UserDefinedConversion; |
| 7684 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
| 7685 | } |
| 7686 | |
| 7687 | if (CreatedObject && checkAbstractType(CurInit.get()->getType())) |
| 7688 | return ExprError(); |
| 7689 | |
| 7690 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 7691 | CastKind, CurInit.get(), nullptr, |
| 7692 | CurInit.get()->getValueKind()); |
| 7693 | |
| 7694 | if (shouldBindAsTemporary(Entity)) |
| 7695 | |
| 7696 | |
| 7697 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
| 7698 | else if (CreatedObject && shouldDestroyEntity(Entity)) { |
| 7699 | |
| 7700 | |
| 7701 | |
| 7702 | |
| 7703 | QualType T = CurInit.get()->getType(); |
| 7704 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 7705 | CXXDestructorDecl *Destructor |
| 7706 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
| 7707 | S.CheckDestructorAccess(CurInit.get()->getBeginLoc(), Destructor, |
| 7708 | S.PDiag(diag::err_access_dtor_temp) << T); |
| 7709 | S.MarkFunctionReferenced(CurInit.get()->getBeginLoc(), Destructor); |
| 7710 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getBeginLoc())) |
| 7711 | return ExprError(); |
| 7712 | } |
| 7713 | } |
| 7714 | break; |
| 7715 | } |
| 7716 | |
| 7717 | case SK_QualificationConversionLValue: |
| 7718 | case SK_QualificationConversionXValue: |
| 7719 | case SK_QualificationConversionRValue: { |
| 7720 | |
| 7721 | ExprValueKind VK = |
| 7722 | Step->Kind == SK_QualificationConversionLValue |
| 7723 | ? VK_LValue |
| 7724 | : (Step->Kind == SK_QualificationConversionXValue ? VK_XValue |
| 7725 | : VK_RValue); |
| 7726 | CurInit = S.PerformQualificationConversion(CurInit.get(), Step->Type, VK); |
| 7727 | break; |
| 7728 | } |
| 7729 | |
| 7730 | case SK_AtomicConversion: { |
| 7731 | (0) . __assert_fail ("CurInit.get()->isRValue() && \"cannot convert glvalue to atomic\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 7731, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic"); |
| 7732 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 7733 | CK_NonAtomicToAtomic, VK_RValue); |
| 7734 | break; |
| 7735 | } |
| 7736 | |
| 7737 | case SK_LValueToRValue: { |
| 7738 | (0) . __assert_fail ("CurInit.get()->isGLValue() && \"cannot load from a prvalue\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 7738, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
| 7739 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 7740 | CK_LValueToRValue, CurInit.get(), |
| 7741 | , VK_RValue); |
| 7742 | break; |
| 7743 | } |
| 7744 | |
| 7745 | case SK_ConversionSequence: |
| 7746 | case SK_ConversionSequenceNoNarrowing: { |
| 7747 | if (const auto *FromPtrType = |
| 7748 | CurInit.get()->getType()->getAs<PointerType>()) { |
| 7749 | if (const auto *ToPtrType = Step->Type->getAs<PointerType>()) { |
| 7750 | if (FromPtrType->getPointeeType()->hasAttr(attr::NoDeref) && |
| 7751 | !ToPtrType->getPointeeType()->hasAttr(attr::NoDeref)) { |
| 7752 | S.Diag(CurInit.get()->getExprLoc(), |
| 7753 | diag::warn_noderef_to_dereferenceable_pointer) |
| 7754 | << CurInit.get()->getSourceRange(); |
| 7755 | } |
| 7756 | } |
| 7757 | } |
| 7758 | |
| 7759 | Sema::CheckedConversionKind CCK |
| 7760 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 7761 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 7762 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 7763 | : Sema::CCK_ImplicitConversion; |
| 7764 | ExprResult CurInitExprRes = |
| 7765 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
| 7766 | getAssignmentAction(Entity), CCK); |
| 7767 | if (CurInitExprRes.isInvalid()) |
| 7768 | return ExprError(); |
| 7769 | |
| 7770 | S.DiscardMisalignedMemberAddress(Step->Type.getTypePtr(), CurInit.get()); |
| 7771 | |
| 7772 | CurInit = CurInitExprRes; |
| 7773 | |
| 7774 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
| 7775 | S.getLangOpts().CPlusPlus) |
| 7776 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 7777 | CurInit.get()); |
| 7778 | |
| 7779 | break; |
| 7780 | } |
| 7781 | |
| 7782 | case SK_ListInitialization: { |
| 7783 | if (checkAbstractType(Step->Type)) |
| 7784 | return ExprError(); |
| 7785 | |
| 7786 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
| 7787 | |
| 7788 | |
| 7789 | QualType Ty = Step->Type; |
| 7790 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
| 7791 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
| 7792 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 7793 | InitListChecker PerformInitList(S, InitEntity, |
| 7794 | InitList, Ty, , |
| 7795 | ); |
| 7796 | if (PerformInitList.HadError()) |
| 7797 | return ExprError(); |
| 7798 | |
| 7799 | |
| 7800 | |
| 7801 | |
| 7802 | if (ResultType && |
| 7803 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
| 7804 | if ((*ResultType)->isRValueReferenceType()) |
| 7805 | Ty = S.Context.getRValueReferenceType(Ty); |
| 7806 | else if ((*ResultType)->isLValueReferenceType()) |
| 7807 | Ty = S.Context.getLValueReferenceType(Ty, |
| 7808 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 7809 | *ResultType = Ty; |
| 7810 | } |
| 7811 | |
| 7812 | InitListExpr *StructuredInitList = |
| 7813 | PerformInitList.getFullyStructuredList(); |
| 7814 | CurInit.get(); |
| 7815 | CurInit = shouldBindAsTemporary(InitEntity) |
| 7816 | ? S.MaybeBindToTemporary(StructuredInitList) |
| 7817 | : StructuredInitList; |
| 7818 | break; |
| 7819 | } |
| 7820 | |
| 7821 | case SK_ConstructorInitializationFromList: { |
| 7822 | if (checkAbstractType(Step->Type)) |
| 7823 | return ExprError(); |
| 7824 | |
| 7825 | |
| 7826 | |
| 7827 | |
| 7828 | |
| 7829 | |
| 7830 | |
| 7831 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7832 | Entity.getType().getNonReferenceType()); |
| 7833 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 7834 | (0) . __assert_fail ("Args.size() == 1 && \"expected a single argument for list init\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 7834, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Args.size() == 1 && "expected a single argument for list init"); |
| 7835 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 7836 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 7837 | << InitList->getSourceRange(); |
| 7838 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
| 7839 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 7840 | Entity, |
| 7841 | Kind, Arg, *Step, |
| 7842 | ConstructorInitRequiresZeroInit, |
| 7843 | , |
| 7844 | , |
| 7845 | InitList->getLBraceLoc(), |
| 7846 | InitList->getRBraceLoc()); |
| 7847 | break; |
| 7848 | } |
| 7849 | |
| 7850 | case SK_UnwrapInitList: |
| 7851 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
| 7852 | break; |
| 7853 | |
| 7854 | case SK_RewrapInitList: { |
| 7855 | Expr *E = CurInit.get(); |
| 7856 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 7857 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
| 7858 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
| 7859 | ILE->setSyntacticForm(Syntactic); |
| 7860 | ILE->setType(E->getType()); |
| 7861 | ILE->setValueKind(E->getValueKind()); |
| 7862 | CurInit = ILE; |
| 7863 | break; |
| 7864 | } |
| 7865 | |
| 7866 | case SK_ConstructorInitialization: |
| 7867 | case SK_StdInitializerListConstructorCall: { |
| 7868 | if (checkAbstractType(Step->Type)) |
| 7869 | return ExprError(); |
| 7870 | |
| 7871 | |
| 7872 | |
| 7873 | |
| 7874 | |
| 7875 | |
| 7876 | |
| 7877 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 7878 | Entity.getType().getNonReferenceType()); |
| 7879 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 7880 | bool IsStdInitListInit = |
| 7881 | Step->Kind == SK_StdInitializerListConstructorCall; |
| 7882 | Expr *Source = CurInit.get(); |
| 7883 | SourceRange Range = Kind.hasParenOrBraceRange() |
| 7884 | ? Kind.getParenOrBraceRange() |
| 7885 | : SourceRange(); |
| 7886 | CurInit = PerformConstructorInitialization( |
| 7887 | S, UseTemporary ? TempEntity : Entity, Kind, |
| 7888 | Source ? MultiExprArg(Source) : Args, *Step, |
| 7889 | ConstructorInitRequiresZeroInit, |
| 7890 | IsStdInitListInit, |
| 7891 | IsStdInitListInit, |
| 7892 | Range.getBegin(), |
| 7893 | Range.getEnd()); |
| 7894 | break; |
| 7895 | } |
| 7896 | |
| 7897 | case SK_ZeroInitialization: { |
| 7898 | step_iterator NextStep = Step; |
| 7899 | ++NextStep; |
| 7900 | if (NextStep != StepEnd && |
| 7901 | (NextStep->Kind == SK_ConstructorInitialization || |
| 7902 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
| 7903 | |
| 7904 | |
| 7905 | ConstructorInitRequiresZeroInit = true; |
| 7906 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 7907 | S.getLangOpts().CPlusPlus && |
| 7908 | !Kind.isImplicitValueInit()) { |
| 7909 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 7910 | if (!TSInfo) |
| 7911 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
| 7912 | Kind.getRange().getBegin()); |
| 7913 | |
| 7914 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
| 7915 | Entity.getType().getNonLValueExprType(S.Context), TSInfo, |
| 7916 | Kind.getRange().getEnd()); |
| 7917 | } else { |
| 7918 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
| 7919 | } |
| 7920 | break; |
| 7921 | } |
| 7922 | |
| 7923 | case SK_CAssignment: { |
| 7924 | QualType SourceType = CurInit.get()->getType(); |
| 7925 | |
| 7926 | |
| 7927 | ExprResult InitialCurInit = CurInit; |
| 7928 | ExprResult Result = CurInit; |
| 7929 | Sema::AssignConvertType ConvTy = |
| 7930 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 7931 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
| 7932 | if (Result.isInvalid()) |
| 7933 | return ExprError(); |
| 7934 | CurInit = Result; |
| 7935 | |
| 7936 | |
| 7937 | ExprResult CurInitExprRes = CurInit; |
| 7938 | if (ConvTy != Sema::Compatible && |
| 7939 | Entity.isParameterKind() && |
| 7940 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
| 7941 | == Sema::Compatible) |
| 7942 | ConvTy = Sema::Compatible; |
| 7943 | if (CurInitExprRes.isInvalid()) |
| 7944 | return ExprError(); |
| 7945 | CurInit = CurInitExprRes; |
| 7946 | |
| 7947 | bool Complained; |
| 7948 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 7949 | Step->Type, SourceType, |
| 7950 | InitialCurInit.get(), |
| 7951 | getAssignmentAction(Entity, true), |
| 7952 | &Complained)) { |
| 7953 | PrintInitLocationNote(S, Entity); |
| 7954 | return ExprError(); |
| 7955 | } else if (Complained) |
| 7956 | PrintInitLocationNote(S, Entity); |
| 7957 | break; |
| 7958 | } |
| 7959 | |
| 7960 | case SK_StringInit: { |
| 7961 | QualType Ty = Step->Type; |
| 7962 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
| 7963 | S.Context.getAsArrayType(Ty), S); |
| 7964 | break; |
| 7965 | } |
| 7966 | |
| 7967 | case SK_ObjCObjectConversion: |
| 7968 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 7969 | CK_ObjCObjectLValueCast, |
| 7970 | CurInit.get()->getValueKind()); |
| 7971 | break; |
| 7972 | |
| 7973 | case SK_ArrayLoopIndex: { |
| 7974 | Expr *Cur = CurInit.get(); |
| 7975 | Expr *BaseExpr = new (S.Context) |
| 7976 | OpaqueValueExpr(Cur->getExprLoc(), Cur->getType(), |
| 7977 | Cur->getValueKind(), Cur->getObjectKind(), Cur); |
| 7978 | Expr *IndexExpr = |
| 7979 | new (S.Context) ArrayInitIndexExpr(S.Context.getSizeType()); |
| 7980 | CurInit = S.CreateBuiltinArraySubscriptExpr( |
| 7981 | BaseExpr, Kind.getLocation(), IndexExpr, Kind.getLocation()); |
| 7982 | ArrayLoopCommonExprs.push_back(BaseExpr); |
| 7983 | break; |
| 7984 | } |
| 7985 | |
| 7986 | case SK_ArrayLoopInit: { |
| 7987 | (0) . __assert_fail ("!ArrayLoopCommonExprs.empty() && \"mismatched SK_ArrayLoopIndex and SK_ArrayLoopInit\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 7988, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!ArrayLoopCommonExprs.empty() && |
| 7988 | (0) . __assert_fail ("!ArrayLoopCommonExprs.empty() && \"mismatched SK_ArrayLoopIndex and SK_ArrayLoopInit\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 7988, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "mismatched SK_ArrayLoopIndex and SK_ArrayLoopInit"); |
| 7989 | Expr *Common = ArrayLoopCommonExprs.pop_back_val(); |
| 7990 | CurInit = new (S.Context) ArrayInitLoopExpr(Step->Type, Common, |
| 7991 | CurInit.get()); |
| 7992 | break; |
| 7993 | } |
| 7994 | |
| 7995 | case SK_GNUArrayInit: |
| 7996 | |
| 7997 | |
| 7998 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
| 7999 | << Step->Type << CurInit.get()->getType() |
| 8000 | << CurInit.get()->getSourceRange(); |
| 8001 | updateGNUCompoundLiteralRValue(CurInit.get()); |
| 8002 | LLVM_FALLTHROUGH; |
| 8003 | case SK_ArrayInit: |
| 8004 | |
| 8005 | |
| 8006 | if (ResultType) { |
| 8007 | if (const IncompleteArrayType *IncompleteDest |
| 8008 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 8009 | if (const ConstantArrayType *ConstantSource |
| 8010 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
| 8011 | *ResultType = S.Context.getConstantArrayType( |
| 8012 | IncompleteDest->getElementType(), |
| 8013 | ConstantSource->getSize(), |
| 8014 | ArrayType::Normal, 0); |
| 8015 | } |
| 8016 | } |
| 8017 | } |
| 8018 | break; |
| 8019 | |
| 8020 | case SK_ParenthesizedArrayInit: |
| 8021 | |
| 8022 | |
| 8023 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 8024 | << CurInit.get()->getSourceRange(); |
| 8025 | break; |
| 8026 | |
| 8027 | case SK_PassByIndirectCopyRestore: |
| 8028 | case SK_PassByIndirectRestore: |
| 8029 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 8030 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 8031 | CurInit.get(), Step->Type, |
| 8032 | Step->Kind == SK_PassByIndirectCopyRestore); |
| 8033 | break; |
| 8034 | |
| 8035 | case SK_ProduceObjCObject: |
| 8036 | CurInit = |
| 8037 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 8038 | CurInit.get(), nullptr, VK_RValue); |
| 8039 | break; |
| 8040 | |
| 8041 | case SK_StdInitializerList: { |
| 8042 | S.Diag(CurInit.get()->getExprLoc(), |
| 8043 | diag::warn_cxx98_compat_initializer_list_init) |
| 8044 | << CurInit.get()->getSourceRange(); |
| 8045 | |
| 8046 | |
| 8047 | MaterializeTemporaryExpr *MTE = S.CreateMaterializeTemporaryExpr( |
| 8048 | CurInit.get()->getType(), CurInit.get(), |
| 8049 | ); |
| 8050 | |
| 8051 | |
| 8052 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
| 8053 | |
| 8054 | |
| 8055 | |
| 8056 | if (shouldBindAsTemporary(Entity)) |
| 8057 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
| 8058 | break; |
| 8059 | } |
| 8060 | |
| 8061 | case SK_OCLSamplerInit: { |
| 8062 | |
| 8063 | |
| 8064 | |
| 8065 | |
| 8066 | |
| 8067 | |
| 8068 | |
| 8069 | |
| 8070 | |
| 8071 | |
| 8072 | |
| 8073 | |
| 8074 | |
| 8075 | |
| 8076 | |
| 8077 | (0) . __assert_fail ("Step->Type->isSamplerT() && \"Sampler initialization on non-sampler type.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8078, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Step->Type->isSamplerT() && |
| 8078 | (0) . __assert_fail ("Step->Type->isSamplerT() && \"Sampler initialization on non-sampler type.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8078, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Sampler initialization on non-sampler type."); |
| 8079 | Expr *Init = CurInit.get(); |
| 8080 | QualType SourceType = Init->getType(); |
| 8081 | |
| 8082 | if (Entity.isParameterKind()) { |
| 8083 | if (!SourceType->isSamplerT() && !SourceType->isIntegerType()) { |
| 8084 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 8085 | << SourceType; |
| 8086 | break; |
| 8087 | } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init)) { |
| 8088 | auto Var = cast<VarDecl>(DRE->getDecl()); |
| 8089 | |
| 8090 | |
| 8091 | if (!Var->hasGlobalStorage()) { |
| 8092 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 8093 | CK_LValueToRValue, Init, |
| 8094 | , VK_RValue); |
| 8095 | break; |
| 8096 | } |
| 8097 | |
| 8098 | |
| 8099 | |
| 8100 | |
| 8101 | |
| 8102 | |
| 8103 | if (!Var->getInit() || !isa<ImplicitCastExpr>(Var->getInit())) |
| 8104 | break; |
| 8105 | Init = cast<ImplicitCastExpr>(const_cast<Expr*>( |
| 8106 | Var->getInit()))->getSubExpr(); |
| 8107 | SourceType = Init->getType(); |
| 8108 | } |
| 8109 | } else { |
| 8110 | |
| 8111 | |
| 8112 | |
| 8113 | |
| 8114 | if (!Init->isConstantInitializer(S.Context, false)) |
| 8115 | break; |
| 8116 | |
| 8117 | if (!SourceType->isIntegerType() || |
| 8118 | 32 != S.Context.getIntWidth(SourceType)) { |
| 8119 | S.Diag(Kind.getLocation(), diag::err_sampler_initializer_not_integer) |
| 8120 | << SourceType; |
| 8121 | break; |
| 8122 | } |
| 8123 | |
| 8124 | Expr::EvalResult EVResult; |
| 8125 | Init->EvaluateAsInt(EVResult, S.Context); |
| 8126 | llvm::APSInt Result = EVResult.Val.getInt(); |
| 8127 | const uint64_t SamplerValue = Result.getLimitedValue(); |
| 8128 | |
| 8129 | |
| 8130 | |
| 8131 | |
| 8132 | |
| 8133 | |
| 8134 | unsigned AddressingMode = (0x0E & SamplerValue) >> 1; |
| 8135 | unsigned FilterMode = (0x30 & SamplerValue) >> 4; |
| 8136 | if (FilterMode != 1 && FilterMode != 2 && |
| 8137 | !S.getOpenCLOptions().isEnabled( |
| 8138 | "cl_intel_device_side_avc_motion_estimation")) |
| 8139 | S.Diag(Kind.getLocation(), |
| 8140 | diag::warn_sampler_initializer_invalid_bits) |
| 8141 | << "Filter Mode"; |
| 8142 | if (AddressingMode > 4) |
| 8143 | S.Diag(Kind.getLocation(), |
| 8144 | diag::warn_sampler_initializer_invalid_bits) |
| 8145 | << "Addressing Mode"; |
| 8146 | } |
| 8147 | |
| 8148 | |
| 8149 | |
| 8150 | CurInit = S.ImpCastExprToType(Init, S.Context.OCLSamplerTy, |
| 8151 | CK_IntToOCLSampler); |
| 8152 | break; |
| 8153 | } |
| 8154 | case SK_OCLZeroOpaqueType: { |
| 8155 | (0) . __assert_fail ("(Step->Type->isEventT() || Step->Type->isQueueT() || Step->Type->isOCLIntelSubgroupAVCType()) && \"Wrong type for initialization of OpenCL opaque type.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8157, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((Step->Type->isEventT() || Step->Type->isQueueT() || |
| 8156 | (0) . __assert_fail ("(Step->Type->isEventT() || Step->Type->isQueueT() || Step->Type->isOCLIntelSubgroupAVCType()) && \"Wrong type for initialization of OpenCL opaque type.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8157, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> Step->Type->isOCLIntelSubgroupAVCType()) && |
| 8157 | (0) . __assert_fail ("(Step->Type->isEventT() || Step->Type->isQueueT() || Step->Type->isOCLIntelSubgroupAVCType()) && \"Wrong type for initialization of OpenCL opaque type.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8157, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Wrong type for initialization of OpenCL opaque type."); |
| 8158 | |
| 8159 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 8160 | CK_ZeroToOCLOpaqueType, |
| 8161 | CurInit.get()->getValueKind()); |
| 8162 | break; |
| 8163 | } |
| 8164 | } |
| 8165 | } |
| 8166 | |
| 8167 | |
| 8168 | |
| 8169 | if (auto *Init = CurInit.get()) |
| 8170 | S.checkInitializerLifetime(Entity, Init); |
| 8171 | |
| 8172 | |
| 8173 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8174 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 8175 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 8176 | cast<FieldDecl>(Entity.getDecl()), |
| 8177 | CurInit.get()); |
| 8178 | |
| 8179 | |
| 8180 | if (const Expr *E = CurInit.get()) { |
| 8181 | CheckMoveOnConstruction(S, E, |
| 8182 | Entity.getKind() == InitializedEntity::EK_Result); |
| 8183 | } |
| 8184 | |
| 8185 | return CurInit; |
| 8186 | } |
| 8187 | |
| 8188 | |
| 8189 | |
| 8190 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 8191 | QualType T) { |
| 8192 | if (T->isReferenceType()) { |
| 8193 | S.Diag(Loc, diag::err_reference_without_init) |
| 8194 | << T.getNonReferenceType(); |
| 8195 | return true; |
| 8196 | } |
| 8197 | |
| 8198 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 8199 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 8200 | return false; |
| 8201 | |
| 8202 | for (const auto *FI : RD->fields()) { |
| 8203 | if (FI->isUnnamedBitfield()) |
| 8204 | continue; |
| 8205 | |
| 8206 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 8207 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8208 | return true; |
| 8209 | } |
| 8210 | } |
| 8211 | |
| 8212 | for (const auto &BI : RD->bases()) { |
| 8213 | if (DiagnoseUninitializedReference(S, BI.getBeginLoc(), BI.getType())) { |
| 8214 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 8215 | return true; |
| 8216 | } |
| 8217 | } |
| 8218 | |
| 8219 | return false; |
| 8220 | } |
| 8221 | |
| 8222 | |
| 8223 | |
| 8224 | |
| 8225 | |
| 8226 | |
| 8227 | |
| 8228 | |
| 8229 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 8230 | Expr *op) { |
| 8231 | QualType destType = entity.getType(); |
| 8232 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 8233 | op->getType()->isObjCObjectPointerType()) { |
| 8234 | |
| 8235 | |
| 8236 | |
| 8237 | S.EmitRelatedResultTypeNote(op); |
| 8238 | |
| 8239 | |
| 8240 | |
| 8241 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 8242 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 8243 | } |
| 8244 | } |
| 8245 | |
| 8246 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 8247 | InitListExpr *InitList) { |
| 8248 | QualType DestType = Entity.getType(); |
| 8249 | |
| 8250 | QualType E; |
| 8251 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 8252 | QualType ArrayType = S.Context.getConstantArrayType( |
| 8253 | E.withConst(), |
| 8254 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 8255 | InitList->getNumInits()), |
| 8256 | clang::ArrayType::Normal, 0); |
| 8257 | InitializedEntity HiddenArray = |
| 8258 | InitializedEntity::InitializeTemporary(ArrayType); |
| 8259 | return diagnoseListInit(S, HiddenArray, InitList); |
| 8260 | } |
| 8261 | |
| 8262 | if (DestType->isReferenceType()) { |
| 8263 | |
| 8264 | |
| 8265 | |
| 8266 | QualType T = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 8267 | diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList); |
| 8268 | SourceLocation Loc = InitList->getBeginLoc(); |
| 8269 | if (auto *D = Entity.getDecl()) |
| 8270 | Loc = D->getLocation(); |
| 8271 | S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T; |
| 8272 | return; |
| 8273 | } |
| 8274 | |
| 8275 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
| 8276 | , |
| 8277 | ); |
| 8278 | (0) . __assert_fail ("DiagnoseInitList.HadError() && \"Inconsistent init list check result.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8279, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DiagnoseInitList.HadError() && |
| 8279 | (0) . __assert_fail ("DiagnoseInitList.HadError() && \"Inconsistent init list check result.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8279, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Inconsistent init list check result."); |
| 8280 | } |
| 8281 | |
| 8282 | bool InitializationSequence::Diagnose(Sema &S, |
| 8283 | const InitializedEntity &Entity, |
| 8284 | const InitializationKind &Kind, |
| 8285 | ArrayRef<Expr *> Args) { |
| 8286 | if (!Failed()) |
| 8287 | return false; |
| 8288 | |
| 8289 | |
| 8290 | |
| 8291 | Expr *OnlyArg; |
| 8292 | if (Args.size() == 1) { |
| 8293 | auto *List = dyn_cast<InitListExpr>(Args[0]); |
| 8294 | if (List && List->getNumInits() == 1) |
| 8295 | OnlyArg = List->getInit(0); |
| 8296 | else |
| 8297 | OnlyArg = Args[0]; |
| 8298 | } |
| 8299 | else |
| 8300 | OnlyArg = nullptr; |
| 8301 | |
| 8302 | QualType DestType = Entity.getType(); |
| 8303 | switch (Failure) { |
| 8304 | case FK_TooManyInitsForReference: |
| 8305 | |
| 8306 | if (Args.empty()) { |
| 8307 | |
| 8308 | |
| 8309 | |
| 8310 | isReferenceType()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8311, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Kind.getKind() == InitializationKind::IK_Value || |
| 8311 | isReferenceType()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8311, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> DestType->isReferenceType()); |
| 8312 | bool Diagnosed = |
| 8313 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 8314 | (0) . __assert_fail ("Diagnosed && \"couldn't find uninitialized reference to diagnose\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8314, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 8315 | (void)Diagnosed; |
| 8316 | } else |
| 8317 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 8318 | << SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
| 8319 | break; |
| 8320 | case FK_ParenthesizedListInitForReference: |
| 8321 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8322 | << 1 << Entity.getType() << Args[0]->getSourceRange(); |
| 8323 | break; |
| 8324 | |
| 8325 | case FK_ArrayNeedsInitList: |
| 8326 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
| 8327 | break; |
| 8328 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8329 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 8330 | break; |
| 8331 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8332 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 8333 | break; |
| 8334 | case FK_NarrowStringIntoWideCharArray: |
| 8335 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 8336 | break; |
| 8337 | case FK_WideStringIntoCharArray: |
| 8338 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 8339 | break; |
| 8340 | case FK_IncompatWideStringIntoWideChar: |
| 8341 | S.Diag(Kind.getLocation(), |
| 8342 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 8343 | break; |
| 8344 | case FK_PlainStringIntoUTF8Char: |
| 8345 | S.Diag(Kind.getLocation(), |
| 8346 | diag::err_array_init_plain_string_into_char8_t); |
| 8347 | S.Diag(Args.front()->getBeginLoc(), |
| 8348 | diag::note_array_init_plain_string_into_char8_t) |
| 8349 | << FixItHint::CreateInsertion(Args.front()->getBeginLoc(), "u8"); |
| 8350 | break; |
| 8351 | case FK_UTF8StringIntoPlainChar: |
| 8352 | S.Diag(Kind.getLocation(), |
| 8353 | diag::err_array_init_utf8_string_into_char) |
| 8354 | << S.getLangOpts().CPlusPlus2a; |
| 8355 | break; |
| 8356 | case FK_ArrayTypeMismatch: |
| 8357 | case FK_NonConstantArrayInit: |
| 8358 | S.Diag(Kind.getLocation(), |
| 8359 | (Failure == FK_ArrayTypeMismatch |
| 8360 | ? diag::err_array_init_different_type |
| 8361 | : diag::err_array_init_non_constant_array)) |
| 8362 | << DestType.getNonReferenceType() |
| 8363 | << OnlyArg->getType() |
| 8364 | << Args[0]->getSourceRange(); |
| 8365 | break; |
| 8366 | |
| 8367 | case FK_VariableLengthArrayHasInitializer: |
| 8368 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 8369 | << Args[0]->getSourceRange(); |
| 8370 | break; |
| 8371 | |
| 8372 | case FK_AddressOfOverloadFailed: { |
| 8373 | DeclAccessPair Found; |
| 8374 | S.ResolveAddressOfOverloadedFunction(OnlyArg, |
| 8375 | DestType.getNonReferenceType(), |
| 8376 | true, |
| 8377 | Found); |
| 8378 | break; |
| 8379 | } |
| 8380 | |
| 8381 | case FK_AddressOfUnaddressableFunction: { |
| 8382 | auto *FD = cast<FunctionDecl>(cast<DeclRefExpr>(OnlyArg)->getDecl()); |
| 8383 | S.checkAddressOfFunctionIsAvailable(FD, , |
| 8384 | OnlyArg->getBeginLoc()); |
| 8385 | break; |
| 8386 | } |
| 8387 | |
| 8388 | case FK_ReferenceInitOverloadFailed: |
| 8389 | case FK_UserConversionOverloadFailed: |
| 8390 | switch (FailedOverloadResult) { |
| 8391 | case OR_Ambiguous: |
| 8392 | if (Failure == FK_UserConversionOverloadFailed) |
| 8393 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 8394 | << OnlyArg->getType() << DestType |
| 8395 | << Args[0]->getSourceRange(); |
| 8396 | else |
| 8397 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 8398 | << DestType << OnlyArg->getType() |
| 8399 | << Args[0]->getSourceRange(); |
| 8400 | |
| 8401 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
| 8402 | break; |
| 8403 | |
| 8404 | case OR_No_Viable_Function: |
| 8405 | if (!S.RequireCompleteType(Kind.getLocation(), |
| 8406 | DestType.getNonReferenceType(), |
| 8407 | diag::err_typecheck_nonviable_condition_incomplete, |
| 8408 | OnlyArg->getType(), Args[0]->getSourceRange())) |
| 8409 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 8410 | << (Entity.getKind() == InitializedEntity::EK_Result) |
| 8411 | << OnlyArg->getType() << Args[0]->getSourceRange() |
| 8412 | << DestType.getNonReferenceType(); |
| 8413 | |
| 8414 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
| 8415 | break; |
| 8416 | |
| 8417 | case OR_Deleted: { |
| 8418 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 8419 | << OnlyArg->getType() << DestType.getNonReferenceType() |
| 8420 | << Args[0]->getSourceRange(); |
| 8421 | OverloadCandidateSet::iterator Best; |
| 8422 | OverloadingResult Ovl |
| 8423 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
| 8424 | if (Ovl == OR_Deleted) { |
| 8425 | S.NoteDeletedFunction(Best->Function); |
| 8426 | } else { |
| 8427 | llvm_unreachable("Inconsistent overload resolution?"); |
| 8428 | } |
| 8429 | break; |
| 8430 | } |
| 8431 | |
| 8432 | case OR_Success: |
| 8433 | llvm_unreachable("Conversion did not fail!"); |
| 8434 | } |
| 8435 | break; |
| 8436 | |
| 8437 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 8438 | if (isa<InitListExpr>(Args[0])) { |
| 8439 | S.Diag(Kind.getLocation(), |
| 8440 | diag::err_lvalue_reference_bind_to_initlist) |
| 8441 | << DestType.getNonReferenceType().isVolatileQualified() |
| 8442 | << DestType.getNonReferenceType() |
| 8443 | << Args[0]->getSourceRange(); |
| 8444 | break; |
| 8445 | } |
| 8446 | LLVM_FALLTHROUGH; |
| 8447 | |
| 8448 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 8449 | S.Diag(Kind.getLocation(), |
| 8450 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 8451 | ? diag::err_lvalue_reference_bind_to_temporary |
| 8452 | : diag::err_lvalue_reference_bind_to_unrelated) |
| 8453 | << DestType.getNonReferenceType().isVolatileQualified() |
| 8454 | << DestType.getNonReferenceType() |
| 8455 | << OnlyArg->getType() |
| 8456 | << Args[0]->getSourceRange(); |
| 8457 | break; |
| 8458 | |
| 8459 | case FK_NonConstLValueReferenceBindingToBitfield: { |
| 8460 | |
| 8461 | FieldDecl *BitField = Args[0]->getSourceBitField(); |
| 8462 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
| 8463 | << DestType.isVolatileQualified() |
| 8464 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 8465 | << (BitField != nullptr) |
| 8466 | << Args[0]->getSourceRange(); |
| 8467 | if (BitField) |
| 8468 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 8469 | break; |
| 8470 | } |
| 8471 | |
| 8472 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8473 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 8474 | << DestType.isVolatileQualified() |
| 8475 | << Args[0]->getSourceRange(); |
| 8476 | break; |
| 8477 | |
| 8478 | case FK_RValueReferenceBindingToLValue: |
| 8479 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
| 8480 | << DestType.getNonReferenceType() << OnlyArg->getType() |
| 8481 | << Args[0]->getSourceRange(); |
| 8482 | break; |
| 8483 | |
| 8484 | case FK_ReferenceInitDropsQualifiers: { |
| 8485 | QualType SourceType = OnlyArg->getType(); |
| 8486 | QualType NonRefType = DestType.getNonReferenceType(); |
| 8487 | Qualifiers DroppedQualifiers = |
| 8488 | SourceType.getQualifiers() - NonRefType.getQualifiers(); |
| 8489 | |
| 8490 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 8491 | << SourceType |
| 8492 | << NonRefType |
| 8493 | << DroppedQualifiers.getCVRQualifiers() |
| 8494 | << Args[0]->getSourceRange(); |
| 8495 | break; |
| 8496 | } |
| 8497 | |
| 8498 | case FK_ReferenceInitFailed: |
| 8499 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 8500 | << DestType.getNonReferenceType() |
| 8501 | << DestType.getNonReferenceType()->isIncompleteType() |
| 8502 | << OnlyArg->isLValue() |
| 8503 | << OnlyArg->getType() |
| 8504 | << Args[0]->getSourceRange(); |
| 8505 | emitBadConversionNotes(S, Entity, Args[0]); |
| 8506 | break; |
| 8507 | |
| 8508 | case FK_ConversionFailed: { |
| 8509 | QualType FromType = OnlyArg->getType(); |
| 8510 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
| 8511 | << (int)Entity.getKind() |
| 8512 | << DestType |
| 8513 | << OnlyArg->isLValue() |
| 8514 | << FromType |
| 8515 | << Args[0]->getSourceRange(); |
| 8516 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 8517 | S.Diag(Kind.getLocation(), PDiag); |
| 8518 | emitBadConversionNotes(S, Entity, Args[0]); |
| 8519 | break; |
| 8520 | } |
| 8521 | |
| 8522 | case FK_ConversionFromPropertyFailed: |
| 8523 | |
| 8524 | break; |
| 8525 | |
| 8526 | case FK_TooManyInitsForScalar: { |
| 8527 | SourceRange R; |
| 8528 | |
| 8529 | auto *InitList = dyn_cast<InitListExpr>(Args[0]); |
| 8530 | if (InitList && InitList->getNumInits() >= 1) { |
| 8531 | R = SourceRange(InitList->getInit(0)->getEndLoc(), InitList->getEndLoc()); |
| 8532 | } else { |
| 8533 | (0) . __assert_fail ("Args.size() > 1 && \"Expected multiple initializers!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8533, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Args.size() > 1 && "Expected multiple initializers!"); |
| 8534 | R = SourceRange(Args.front()->getEndLoc(), Args.back()->getEndLoc()); |
| 8535 | } |
| 8536 | |
| 8537 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
| 8538 | if (Kind.isCStyleOrFunctionalCast()) |
| 8539 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 8540 | << R; |
| 8541 | else |
| 8542 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 8543 | << << R; |
| 8544 | break; |
| 8545 | } |
| 8546 | |
| 8547 | case FK_ParenthesizedListInitForScalar: |
| 8548 | S.Diag(Kind.getLocation(), diag::err_list_init_in_parens) |
| 8549 | << 0 << Entity.getType() << Args[0]->getSourceRange(); |
| 8550 | break; |
| 8551 | |
| 8552 | case FK_ReferenceBindingToInitList: |
| 8553 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 8554 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 8555 | break; |
| 8556 | |
| 8557 | case FK_InitListBadDestinationType: |
| 8558 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 8559 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 8560 | break; |
| 8561 | |
| 8562 | case FK_ListConstructorOverloadFailed: |
| 8563 | case FK_ConstructorOverloadFailed: { |
| 8564 | SourceRange ArgsRange; |
| 8565 | if (Args.size()) |
| 8566 | ArgsRange = |
| 8567 | SourceRange(Args.front()->getBeginLoc(), Args.back()->getEndLoc()); |
| 8568 | |
| 8569 | if (Failure == FK_ListConstructorOverloadFailed) { |
| 8570 | (0) . __assert_fail ("Args.size() == 1 && \"List construction from other than 1 argument.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8571, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Args.size() == 1 && |
| 8571 | (0) . __assert_fail ("Args.size() == 1 && \"List construction from other than 1 argument.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8571, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "List construction from other than 1 argument."); |
| 8572 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 8573 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
| 8574 | } |
| 8575 | |
| 8576 | |
| 8577 | |
| 8578 | switch (FailedOverloadResult) { |
| 8579 | case OR_Ambiguous: |
| 8580 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 8581 | << DestType << ArgsRange; |
| 8582 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
| 8583 | break; |
| 8584 | |
| 8585 | case OR_No_Viable_Function: |
| 8586 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 8587 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 8588 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 8589 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8590 | |
| 8591 | |
| 8592 | |
| 8593 | |
| 8594 | CXXConstructorDecl *Constructor |
| 8595 | = cast<CXXConstructorDecl>(S.CurContext); |
| 8596 | const CXXRecordDecl *InheritedFrom = nullptr; |
| 8597 | if (auto Inherited = Constructor->getInheritedConstructor()) |
| 8598 | InheritedFrom = Inherited.getShadowDecl()->getNominatedBaseClass(); |
| 8599 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 8600 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 8601 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
| 8602 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8603 | << |
| 8604 | << Entity.getType() |
| 8605 | << InheritedFrom; |
| 8606 | |
| 8607 | RecordDecl *BaseDecl |
| 8608 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 8609 | ->getDecl(); |
| 8610 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 8611 | << S.Context.getTagDeclType(BaseDecl); |
| 8612 | } else { |
| 8613 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 8614 | << (InheritedFrom ? 2 : Constructor->isImplicit() ? 1 : 0) |
| 8615 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8616 | << |
| 8617 | << Entity.getName() |
| 8618 | << InheritedFrom; |
| 8619 | S.Diag(Entity.getDecl()->getLocation(), |
| 8620 | diag::note_member_declared_at); |
| 8621 | |
| 8622 | if (const RecordType *Record |
| 8623 | = Entity.getType()->getAs<RecordType>()) |
| 8624 | S.Diag(Record->getDecl()->getLocation(), |
| 8625 | diag::note_previous_decl) |
| 8626 | << S.Context.getTagDeclType(Record->getDecl()); |
| 8627 | } |
| 8628 | break; |
| 8629 | } |
| 8630 | |
| 8631 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 8632 | << DestType << ArgsRange; |
| 8633 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
| 8634 | break; |
| 8635 | |
| 8636 | case OR_Deleted: { |
| 8637 | OverloadCandidateSet::iterator Best; |
| 8638 | OverloadingResult Ovl |
| 8639 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
| 8640 | if (Ovl != OR_Deleted) { |
| 8641 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8642 | << DestType << ArgsRange; |
| 8643 | llvm_unreachable("Inconsistent overload resolution?"); |
| 8644 | break; |
| 8645 | } |
| 8646 | |
| 8647 | |
| 8648 | |
| 8649 | |
| 8650 | if (S.isImplicitlyDeleted(Best->Function)) |
| 8651 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
| 8652 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
| 8653 | << DestType << ArgsRange; |
| 8654 | else |
| 8655 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 8656 | << DestType << ArgsRange; |
| 8657 | |
| 8658 | S.NoteDeletedFunction(Best->Function); |
| 8659 | break; |
| 8660 | } |
| 8661 | |
| 8662 | case OR_Success: |
| 8663 | llvm_unreachable("Conversion did not fail!"); |
| 8664 | } |
| 8665 | } |
| 8666 | break; |
| 8667 | |
| 8668 | case FK_DefaultInitOfConst: |
| 8669 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 8670 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 8671 | |
| 8672 | |
| 8673 | |
| 8674 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 8675 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 8676 | << (Constructor->getInheritedConstructor() ? 2 : |
| 8677 | Constructor->isImplicit() ? 1 : 0) |
| 8678 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 8679 | << |
| 8680 | << Entity.getName(); |
| 8681 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 8682 | << Entity.getName(); |
| 8683 | } else { |
| 8684 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 8685 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 8686 | } |
| 8687 | break; |
| 8688 | |
| 8689 | case FK_Incomplete: |
| 8690 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
| 8691 | diag::err_init_incomplete_type); |
| 8692 | break; |
| 8693 | |
| 8694 | case FK_ListInitializationFailed: { |
| 8695 | |
| 8696 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 8697 | diagnoseListInit(S, Entity, InitList); |
| 8698 | break; |
| 8699 | } |
| 8700 | |
| 8701 | case FK_PlaceholderType: { |
| 8702 | |
| 8703 | break; |
| 8704 | } |
| 8705 | |
| 8706 | case FK_ExplicitConstructor: { |
| 8707 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 8708 | << Args[0]->getSourceRange(); |
| 8709 | OverloadCandidateSet::iterator Best; |
| 8710 | OverloadingResult Ovl |
| 8711 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
| 8712 | (void)Ovl; |
| 8713 | (0) . __assert_fail ("Ovl == OR_Success && \"Inconsistent overload resolution\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 8713, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 8714 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 8715 | S.Diag(CtorDecl->getLocation(), |
| 8716 | diag::note_explicit_ctor_deduction_guide_here) << false; |
| 8717 | break; |
| 8718 | } |
| 8719 | } |
| 8720 | |
| 8721 | PrintInitLocationNote(S, Entity); |
| 8722 | return true; |
| 8723 | } |
| 8724 | |
| 8725 | void InitializationSequence::dump(raw_ostream &OS) const { |
| 8726 | switch (SequenceKind) { |
| 8727 | case FailedSequence: { |
| 8728 | OS << "Failed sequence: "; |
| 8729 | switch (Failure) { |
| 8730 | case FK_TooManyInitsForReference: |
| 8731 | OS << "too many initializers for reference"; |
| 8732 | break; |
| 8733 | |
| 8734 | case FK_ParenthesizedListInitForReference: |
| 8735 | OS << "parenthesized list init for reference"; |
| 8736 | break; |
| 8737 | |
| 8738 | case FK_ArrayNeedsInitList: |
| 8739 | OS << "array requires initializer list"; |
| 8740 | break; |
| 8741 | |
| 8742 | case FK_AddressOfUnaddressableFunction: |
| 8743 | OS << "address of unaddressable function was taken"; |
| 8744 | break; |
| 8745 | |
| 8746 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 8747 | OS << "array requires initializer list or string literal"; |
| 8748 | break; |
| 8749 | |
| 8750 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 8751 | OS << "array requires initializer list or wide string literal"; |
| 8752 | break; |
| 8753 | |
| 8754 | case FK_NarrowStringIntoWideCharArray: |
| 8755 | OS << "narrow string into wide char array"; |
| 8756 | break; |
| 8757 | |
| 8758 | case FK_WideStringIntoCharArray: |
| 8759 | OS << "wide string into char array"; |
| 8760 | break; |
| 8761 | |
| 8762 | case FK_IncompatWideStringIntoWideChar: |
| 8763 | OS << "incompatible wide string into wide char array"; |
| 8764 | break; |
| 8765 | |
| 8766 | case FK_PlainStringIntoUTF8Char: |
| 8767 | OS << "plain string literal into char8_t array"; |
| 8768 | break; |
| 8769 | |
| 8770 | case FK_UTF8StringIntoPlainChar: |
| 8771 | OS << "u8 string literal into char array"; |
| 8772 | break; |
| 8773 | |
| 8774 | case FK_ArrayTypeMismatch: |
| 8775 | OS << "array type mismatch"; |
| 8776 | break; |
| 8777 | |
| 8778 | case FK_NonConstantArrayInit: |
| 8779 | OS << "non-constant array initializer"; |
| 8780 | break; |
| 8781 | |
| 8782 | case FK_AddressOfOverloadFailed: |
| 8783 | OS << "address of overloaded function failed"; |
| 8784 | break; |
| 8785 | |
| 8786 | case FK_ReferenceInitOverloadFailed: |
| 8787 | OS << "overload resolution for reference initialization failed"; |
| 8788 | break; |
| 8789 | |
| 8790 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 8791 | OS << "non-const lvalue reference bound to temporary"; |
| 8792 | break; |
| 8793 | |
| 8794 | case FK_NonConstLValueReferenceBindingToBitfield: |
| 8795 | OS << "non-const lvalue reference bound to bit-field"; |
| 8796 | break; |
| 8797 | |
| 8798 | case FK_NonConstLValueReferenceBindingToVectorElement: |
| 8799 | OS << "non-const lvalue reference bound to vector element"; |
| 8800 | break; |
| 8801 | |
| 8802 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 8803 | OS << "non-const lvalue reference bound to unrelated type"; |
| 8804 | break; |
| 8805 | |
| 8806 | case FK_RValueReferenceBindingToLValue: |
| 8807 | OS << "rvalue reference bound to an lvalue"; |
| 8808 | break; |
| 8809 | |
| 8810 | case FK_ReferenceInitDropsQualifiers: |
| 8811 | OS << "reference initialization drops qualifiers"; |
| 8812 | break; |
| 8813 | |
| 8814 | case FK_ReferenceInitFailed: |
| 8815 | OS << "reference initialization failed"; |
| 8816 | break; |
| 8817 | |
| 8818 | case FK_ConversionFailed: |
| 8819 | OS << "conversion failed"; |
| 8820 | break; |
| 8821 | |
| 8822 | case FK_ConversionFromPropertyFailed: |
| 8823 | OS << "conversion from property failed"; |
| 8824 | break; |
| 8825 | |
| 8826 | case FK_TooManyInitsForScalar: |
| 8827 | OS << "too many initializers for scalar"; |
| 8828 | break; |
| 8829 | |
| 8830 | case FK_ParenthesizedListInitForScalar: |
| 8831 | OS << "parenthesized list init for reference"; |
| 8832 | break; |
| 8833 | |
| 8834 | case FK_ReferenceBindingToInitList: |
| 8835 | OS << "referencing binding to initializer list"; |
| 8836 | break; |
| 8837 | |
| 8838 | case FK_InitListBadDestinationType: |
| 8839 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 8840 | break; |
| 8841 | |
| 8842 | case FK_UserConversionOverloadFailed: |
| 8843 | OS << "overloading failed for user-defined conversion"; |
| 8844 | break; |
| 8845 | |
| 8846 | case FK_ConstructorOverloadFailed: |
| 8847 | OS << "constructor overloading failed"; |
| 8848 | break; |
| 8849 | |
| 8850 | case FK_DefaultInitOfConst: |
| 8851 | OS << "default initialization of a const variable"; |
| 8852 | break; |
| 8853 | |
| 8854 | case FK_Incomplete: |
| 8855 | OS << "initialization of incomplete type"; |
| 8856 | break; |
| 8857 | |
| 8858 | case FK_ListInitializationFailed: |
| 8859 | OS << "list initialization checker failure"; |
| 8860 | break; |
| 8861 | |
| 8862 | case FK_VariableLengthArrayHasInitializer: |
| 8863 | OS << "variable length array has an initializer"; |
| 8864 | break; |
| 8865 | |
| 8866 | case FK_PlaceholderType: |
| 8867 | OS << "initializer expression isn't contextually valid"; |
| 8868 | break; |
| 8869 | |
| 8870 | case FK_ListConstructorOverloadFailed: |
| 8871 | OS << "list constructor overloading failed"; |
| 8872 | break; |
| 8873 | |
| 8874 | case FK_ExplicitConstructor: |
| 8875 | OS << "list copy initialization chose explicit constructor"; |
| 8876 | break; |
| 8877 | } |
| 8878 | OS << '\n'; |
| 8879 | return; |
| 8880 | } |
| 8881 | |
| 8882 | case DependentSequence: |
| 8883 | OS << "Dependent sequence\n"; |
| 8884 | return; |
| 8885 | |
| 8886 | case NormalSequence: |
| 8887 | OS << "Normal sequence: "; |
| 8888 | break; |
| 8889 | } |
| 8890 | |
| 8891 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 8892 | if (S != step_begin()) { |
| 8893 | OS << " -> "; |
| 8894 | } |
| 8895 | |
| 8896 | switch (S->Kind) { |
| 8897 | case SK_ResolveAddressOfOverloadedFunction: |
| 8898 | OS << "resolve address of overloaded function"; |
| 8899 | break; |
| 8900 | |
| 8901 | case SK_CastDerivedToBaseRValue: |
| 8902 | OS << "derived-to-base (rvalue)"; |
| 8903 | break; |
| 8904 | |
| 8905 | case SK_CastDerivedToBaseXValue: |
| 8906 | OS << "derived-to-base (xvalue)"; |
| 8907 | break; |
| 8908 | |
| 8909 | case SK_CastDerivedToBaseLValue: |
| 8910 | OS << "derived-to-base (lvalue)"; |
| 8911 | break; |
| 8912 | |
| 8913 | case SK_BindReference: |
| 8914 | OS << "bind reference to lvalue"; |
| 8915 | break; |
| 8916 | |
| 8917 | case SK_BindReferenceToTemporary: |
| 8918 | OS << "bind reference to a temporary"; |
| 8919 | break; |
| 8920 | |
| 8921 | case SK_FinalCopy: |
| 8922 | OS << "final copy in class direct-initialization"; |
| 8923 | break; |
| 8924 | |
| 8925 | case SK_ExtraneousCopyToTemporary: |
| 8926 | OS << "extraneous C++03 copy to temporary"; |
| 8927 | break; |
| 8928 | |
| 8929 | case SK_UserConversion: |
| 8930 | OS << "user-defined conversion via " << *S->Function.Function; |
| 8931 | break; |
| 8932 | |
| 8933 | case SK_QualificationConversionRValue: |
| 8934 | OS << "qualification conversion (rvalue)"; |
| 8935 | break; |
| 8936 | |
| 8937 | case SK_QualificationConversionXValue: |
| 8938 | OS << "qualification conversion (xvalue)"; |
| 8939 | break; |
| 8940 | |
| 8941 | case SK_QualificationConversionLValue: |
| 8942 | OS << "qualification conversion (lvalue)"; |
| 8943 | break; |
| 8944 | |
| 8945 | case SK_AtomicConversion: |
| 8946 | OS << "non-atomic-to-atomic conversion"; |
| 8947 | break; |
| 8948 | |
| 8949 | case SK_LValueToRValue: |
| 8950 | OS << "load (lvalue to rvalue)"; |
| 8951 | break; |
| 8952 | |
| 8953 | case SK_ConversionSequence: |
| 8954 | OS << "implicit conversion sequence ("; |
| 8955 | S->ICS->dump(); |
| 8956 | OS << ")"; |
| 8957 | break; |
| 8958 | |
| 8959 | case SK_ConversionSequenceNoNarrowing: |
| 8960 | OS << "implicit conversion sequence with narrowing prohibited ("; |
| 8961 | S->ICS->dump(); |
| 8962 | OS << ")"; |
| 8963 | break; |
| 8964 | |
| 8965 | case SK_ListInitialization: |
| 8966 | OS << "list aggregate initialization"; |
| 8967 | break; |
| 8968 | |
| 8969 | case SK_UnwrapInitList: |
| 8970 | OS << "unwrap reference initializer list"; |
| 8971 | break; |
| 8972 | |
| 8973 | case SK_RewrapInitList: |
| 8974 | OS << "rewrap reference initializer list"; |
| 8975 | break; |
| 8976 | |
| 8977 | case SK_ConstructorInitialization: |
| 8978 | OS << "constructor initialization"; |
| 8979 | break; |
| 8980 | |
| 8981 | case SK_ConstructorInitializationFromList: |
| 8982 | OS << "list initialization via constructor"; |
| 8983 | break; |
| 8984 | |
| 8985 | case SK_ZeroInitialization: |
| 8986 | OS << "zero initialization"; |
| 8987 | break; |
| 8988 | |
| 8989 | case SK_CAssignment: |
| 8990 | OS << "C assignment"; |
| 8991 | break; |
| 8992 | |
| 8993 | case SK_StringInit: |
| 8994 | OS << "string initialization"; |
| 8995 | break; |
| 8996 | |
| 8997 | case SK_ObjCObjectConversion: |
| 8998 | OS << "Objective-C object conversion"; |
| 8999 | break; |
| 9000 | |
| 9001 | case SK_ArrayLoopIndex: |
| 9002 | OS << "indexing for array initialization loop"; |
| 9003 | break; |
| 9004 | |
| 9005 | case SK_ArrayLoopInit: |
| 9006 | OS << "array initialization loop"; |
| 9007 | break; |
| 9008 | |
| 9009 | case SK_ArrayInit: |
| 9010 | OS << "array initialization"; |
| 9011 | break; |
| 9012 | |
| 9013 | case SK_GNUArrayInit: |
| 9014 | OS << "array initialization (GNU extension)"; |
| 9015 | break; |
| 9016 | |
| 9017 | case SK_ParenthesizedArrayInit: |
| 9018 | OS << "parenthesized array initialization"; |
| 9019 | break; |
| 9020 | |
| 9021 | case SK_PassByIndirectCopyRestore: |
| 9022 | OS << "pass by indirect copy and restore"; |
| 9023 | break; |
| 9024 | |
| 9025 | case SK_PassByIndirectRestore: |
| 9026 | OS << "pass by indirect restore"; |
| 9027 | break; |
| 9028 | |
| 9029 | case SK_ProduceObjCObject: |
| 9030 | OS << "Objective-C object retension"; |
| 9031 | break; |
| 9032 | |
| 9033 | case SK_StdInitializerList: |
| 9034 | OS << "std::initializer_list from initializer list"; |
| 9035 | break; |
| 9036 | |
| 9037 | case SK_StdInitializerListConstructorCall: |
| 9038 | OS << "list initialization from std::initializer_list"; |
| 9039 | break; |
| 9040 | |
| 9041 | case SK_OCLSamplerInit: |
| 9042 | OS << "OpenCL sampler_t from integer constant"; |
| 9043 | break; |
| 9044 | |
| 9045 | case SK_OCLZeroOpaqueType: |
| 9046 | OS << "OpenCL opaque type from zero"; |
| 9047 | break; |
| 9048 | } |
| 9049 | |
| 9050 | OS << " [" << S->Type.getAsString() << ']'; |
| 9051 | } |
| 9052 | |
| 9053 | OS << '\n'; |
| 9054 | } |
| 9055 | |
| 9056 | void InitializationSequence::dump() const { |
| 9057 | dump(llvm::errs()); |
| 9058 | } |
| 9059 | |
| 9060 | static bool NarrowingErrs(const LangOptions &L) { |
| 9061 | return L.CPlusPlus11 && |
| 9062 | (!L.MicrosoftExt || L.isCompatibleWithMSVC(LangOptions::MSVC2015)); |
| 9063 | } |
| 9064 | |
| 9065 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 9066 | const ImplicitConversionSequence &ICS, |
| 9067 | QualType PreNarrowingType, |
| 9068 | QualType EntityType, |
| 9069 | const Expr *PostInit) { |
| 9070 | const StandardConversionSequence *SCS = nullptr; |
| 9071 | switch (ICS.getKind()) { |
| 9072 | case ImplicitConversionSequence::StandardConversion: |
| 9073 | SCS = &ICS.Standard; |
| 9074 | break; |
| 9075 | case ImplicitConversionSequence::UserDefinedConversion: |
| 9076 | SCS = &ICS.UserDefined.After; |
| 9077 | break; |
| 9078 | case ImplicitConversionSequence::AmbiguousConversion: |
| 9079 | case ImplicitConversionSequence::EllipsisConversion: |
| 9080 | case ImplicitConversionSequence::BadConversion: |
| 9081 | return; |
| 9082 | } |
| 9083 | |
| 9084 | |
| 9085 | APValue ConstantValue; |
| 9086 | QualType ConstantType; |
| 9087 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 9088 | ConstantType)) { |
| 9089 | case NK_Not_Narrowing: |
| 9090 | case NK_Dependent_Narrowing: |
| 9091 | |
| 9092 | return; |
| 9093 | |
| 9094 | case NK_Type_Narrowing: |
| 9095 | |
| 9096 | |
| 9097 | |
| 9098 | S.Diag(PostInit->getBeginLoc(), NarrowingErrs(S.getLangOpts()) |
| 9099 | ? diag::ext_init_list_type_narrowing |
| 9100 | : diag::warn_init_list_type_narrowing) |
| 9101 | << PostInit->getSourceRange() |
| 9102 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9103 | << EntityType.getLocalUnqualifiedType(); |
| 9104 | break; |
| 9105 | |
| 9106 | case NK_Constant_Narrowing: |
| 9107 | |
| 9108 | S.Diag(PostInit->getBeginLoc(), |
| 9109 | NarrowingErrs(S.getLangOpts()) |
| 9110 | ? diag::ext_init_list_constant_narrowing |
| 9111 | : diag::warn_init_list_constant_narrowing) |
| 9112 | << PostInit->getSourceRange() |
| 9113 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
| 9114 | << EntityType.getLocalUnqualifiedType(); |
| 9115 | break; |
| 9116 | |
| 9117 | case NK_Variable_Narrowing: |
| 9118 | |
| 9119 | S.Diag(PostInit->getBeginLoc(), |
| 9120 | NarrowingErrs(S.getLangOpts()) |
| 9121 | ? diag::ext_init_list_variable_narrowing |
| 9122 | : diag::warn_init_list_variable_narrowing) |
| 9123 | << PostInit->getSourceRange() |
| 9124 | << PreNarrowingType.getLocalUnqualifiedType() |
| 9125 | << EntityType.getLocalUnqualifiedType(); |
| 9126 | break; |
| 9127 | } |
| 9128 | |
| 9129 | SmallString<128> StaticCast; |
| 9130 | llvm::raw_svector_ostream OS(StaticCast); |
| 9131 | OS << "static_cast<"; |
| 9132 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 9133 | |
| 9134 | |
| 9135 | |
| 9136 | |
| 9137 | |
| 9138 | OS << *TT->getDecl(); |
| 9139 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
| 9140 | OS << BT->getName(S.getLangOpts()); |
| 9141 | else { |
| 9142 | |
| 9143 | |
| 9144 | return; |
| 9145 | } |
| 9146 | OS << ">("; |
| 9147 | S.Diag(PostInit->getBeginLoc(), diag::note_init_list_narrowing_silence) |
| 9148 | << PostInit->getSourceRange() |
| 9149 | << FixItHint::CreateInsertion(PostInit->getBeginLoc(), OS.str()) |
| 9150 | << FixItHint::CreateInsertion( |
| 9151 | S.getLocForEndOfToken(PostInit->getEndLoc()), ")"); |
| 9152 | } |
| 9153 | |
| 9154 | |
| 9155 | |
| 9156 | |
| 9157 | bool |
| 9158 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 9159 | ExprResult Init) { |
| 9160 | if (Init.isInvalid()) |
| 9161 | return false; |
| 9162 | |
| 9163 | Expr *InitE = Init.get(); |
| 9164 | (0) . __assert_fail ("InitE && \"No initialization expression\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 9164, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(InitE && "No initialization expression"); |
| 9165 | |
| 9166 | InitializationKind Kind = |
| 9167 | InitializationKind::CreateCopy(InitE->getBeginLoc(), SourceLocation()); |
| 9168 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
| 9169 | return !Seq.Failed(); |
| 9170 | } |
| 9171 | |
| 9172 | ExprResult |
| 9173 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 9174 | SourceLocation EqualLoc, |
| 9175 | ExprResult Init, |
| 9176 | bool TopLevelOfInitList, |
| 9177 | bool AllowExplicit) { |
| 9178 | if (Init.isInvalid()) |
| 9179 | return ExprError(); |
| 9180 | |
| 9181 | Expr *InitE = Init.get(); |
| 9182 | (0) . __assert_fail ("InitE && \"No initialization expression?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 9182, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(InitE && "No initialization expression?"); |
| 9183 | |
| 9184 | if (EqualLoc.isInvalid()) |
| 9185 | EqualLoc = InitE->getBeginLoc(); |
| 9186 | |
| 9187 | InitializationKind Kind = InitializationKind::CreateCopy( |
| 9188 | InitE->getBeginLoc(), EqualLoc, AllowExplicit); |
| 9189 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
| 9190 | |
| 9191 | |
| 9192 | const bool ShouldTrackCopy = |
| 9193 | Entity.isParameterKind() && Seq.isConstructorInitialization(); |
| 9194 | if (ShouldTrackCopy) { |
| 9195 | if (llvm::find(CurrentParameterCopyTypes, Entity.getType()) != |
| 9196 | CurrentParameterCopyTypes.end()) { |
| 9197 | Seq.SetOverloadFailure( |
| 9198 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 9199 | OR_No_Viable_Function); |
| 9200 | |
| 9201 | |
| 9202 | |
| 9203 | const auto LastStep = Seq.step_end() - 1; |
| 9204 | Kind == InitializationSequence..SK_ConstructorInitialization", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 9205, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(LastStep->Kind == |
| 9205 | Kind == InitializationSequence..SK_ConstructorInitialization", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 9205, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> InitializationSequence::SK_ConstructorInitialization); |
| 9206 | const FunctionDecl *Function = LastStep->Function.Function; |
| 9207 | auto Candidate = |
| 9208 | llvm::find_if(Seq.getFailedCandidateSet(), |
| 9209 | [Function](const OverloadCandidate &Candidate) -> bool { |
| 9210 | return Candidate.Viable && |
| 9211 | Candidate.Function == Function && |
| 9212 | Candidate.Conversions.size() > 0; |
| 9213 | }); |
| 9214 | if (Candidate != Seq.getFailedCandidateSet().end() && |
| 9215 | Function->getNumParams() > 0) { |
| 9216 | Candidate->Viable = false; |
| 9217 | Candidate->FailureKind = ovl_fail_bad_conversion; |
| 9218 | Candidate->Conversions[0].setBad(BadConversionSequence::no_conversion, |
| 9219 | InitE, |
| 9220 | Function->getParamDecl(0)->getType()); |
| 9221 | } |
| 9222 | } |
| 9223 | CurrentParameterCopyTypes.push_back(Entity.getType()); |
| 9224 | } |
| 9225 | |
| 9226 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
| 9227 | |
| 9228 | if (ShouldTrackCopy) |
| 9229 | CurrentParameterCopyTypes.pop_back(); |
| 9230 | |
| 9231 | return Result; |
| 9232 | } |
| 9233 | |
| 9234 | |
| 9235 | static bool isOrIsDerivedFromSpecializationOf(CXXRecordDecl *RD, |
| 9236 | ClassTemplateDecl *CTD) { |
| 9237 | auto NotSpecialization = [&] (const CXXRecordDecl *Candidate) { |
| 9238 | auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Candidate); |
| 9239 | return !CTSD || !declaresSameEntity(CTSD->getSpecializedTemplate(), CTD); |
| 9240 | }; |
| 9241 | return !(NotSpecialization(RD) && RD->forallBases(NotSpecialization)); |
| 9242 | } |
| 9243 | |
| 9244 | QualType Sema::DeduceTemplateSpecializationFromInitializer( |
| 9245 | TypeSourceInfo *TSInfo, const InitializedEntity &Entity, |
| 9246 | const InitializationKind &Kind, MultiExprArg Inits) { |
| 9247 | auto *DeducedTST = dyn_cast<DeducedTemplateSpecializationType>( |
| 9248 | TSInfo->getType()->getContainedDeducedType()); |
| 9249 | (0) . __assert_fail ("DeducedTST && \"not a deduced template specialization type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaInit.cpp", 9249, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DeducedTST && "not a deduced template specialization type"); |
| 9250 | |
| 9251 | auto TemplateName = DeducedTST->getTemplateName(); |
| 9252 | if (TemplateName.isDependent()) |
| 9253 | return Context.DependentTy; |
| 9254 | |
| 9255 | |
| 9256 | auto *Template = |
| 9257 | dyn_cast_or_null<ClassTemplateDecl>(TemplateName.getAsTemplateDecl()); |
| 9258 | if (!Template) { |
| 9259 | Diag(Kind.getLocation(), |
| 9260 | diag::err_deduced_non_class_template_specialization_type) |
| 9261 | << (int)getTemplateNameKindForDiagnostics(TemplateName) << TemplateName; |
| 9262 | if (auto *TD = TemplateName.getAsTemplateDecl()) |
| 9263 | Diag(TD->getLocation(), diag::note_template_decl_here); |
| 9264 | return QualType(); |
| 9265 | } |
| 9266 | |
| 9267 | |
| 9268 | if (Expr::hasAnyTypeDependentArguments(Inits)) { |
| 9269 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9270 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9271 | << TSInfo->getTypeLoc().getSourceRange() << 0; |
| 9272 | return Context.DependentTy; |
| 9273 | } |
| 9274 | |
| 9275 | |
| 9276 | |
| 9277 | |
| 9278 | |
| 9279 | |
| 9280 | |
| 9281 | |
| 9282 | |
| 9283 | |
| 9284 | |
| 9285 | |
| 9286 | |
| 9287 | DeclarationNameInfo NameInfo( |
| 9288 | Context.DeclarationNames.getCXXDeductionGuideName(Template), |
| 9289 | TSInfo->getTypeLoc().getEndLoc()); |
| 9290 | LookupResult Guides(*this, NameInfo, LookupOrdinaryName); |
| 9291 | LookupQualifiedName(Guides, Template->getDeclContext()); |
| 9292 | |
| 9293 | |
| 9294 | |
| 9295 | Guides.suppressDiagnostics(); |
| 9296 | |
| 9297 | |
| 9298 | InitListExpr *ListInit = |
| 9299 | (Inits.size() == 1 && Kind.getKind() != InitializationKind::IK_Direct) |
| 9300 | ? dyn_cast<InitListExpr>(Inits[0]) |
| 9301 | : nullptr; |
| 9302 | |
| 9303 | |
| 9304 | |
| 9305 | |
| 9306 | |
| 9307 | |
| 9308 | |
| 9309 | |
| 9310 | |
| 9311 | |
| 9312 | OverloadCandidateSet Candidates(Kind.getLocation(), |
| 9313 | OverloadCandidateSet::CSK_Normal); |
| 9314 | OverloadCandidateSet::iterator Best; |
| 9315 | |
| 9316 | bool HasAnyDeductionGuide = false; |
| 9317 | |
| 9318 | auto tryToResolveOverload = |
| 9319 | [&](bool OnlyListConstructors) -> OverloadingResult { |
| 9320 | Candidates.clear(OverloadCandidateSet::CSK_Normal); |
| 9321 | HasAnyDeductionGuide = false; |
| 9322 | |
| 9323 | for (auto I = Guides.begin(), E = Guides.end(); I != E; ++I) { |
| 9324 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 9325 | if (D->isInvalidDecl()) |
| 9326 | continue; |
| 9327 | |
| 9328 | auto *TD = dyn_cast<FunctionTemplateDecl>(D); |
| 9329 | auto *GD = dyn_cast_or_null<CXXDeductionGuideDecl>( |
| 9330 | TD ? TD->getTemplatedDecl() : dyn_cast<FunctionDecl>(D)); |
| 9331 | if (!GD) |
| 9332 | continue; |
| 9333 | |
| 9334 | if (!GD->isImplicit()) |
| 9335 | HasAnyDeductionGuide = true; |
| 9336 | |
| 9337 | |
| 9338 | |
| 9339 | |
| 9340 | |
| 9341 | |
| 9342 | if (Kind.isCopyInit() && !ListInit) { |
| 9343 | |
| 9344 | if (GD->isExplicit()) |
| 9345 | continue; |
| 9346 | |
| 9347 | |
| 9348 | |
| 9349 | |
| 9350 | if (GD->getMinRequiredArguments() > 1 || |
| 9351 | (GD->getNumParams() == 0 && !GD->isVariadic())) |
| 9352 | continue; |
| 9353 | } |
| 9354 | |
| 9355 | |
| 9356 | |
| 9357 | |
| 9358 | if (OnlyListConstructors && !isInitListConstructor(GD)) |
| 9359 | continue; |
| 9360 | |
| 9361 | |
| 9362 | |
| 9363 | |
| 9364 | |
| 9365 | |
| 9366 | |
| 9367 | |
| 9368 | |
| 9369 | |
| 9370 | |
| 9371 | |
| 9372 | |
| 9373 | bool SuppressUserConversions = Kind.isCopyInit(); |
| 9374 | |
| 9375 | if (TD) |
| 9376 | AddTemplateOverloadCandidate(TD, I.getPair(), nullptr, |
| 9377 | Inits, Candidates, |
| 9378 | SuppressUserConversions); |
| 9379 | else |
| 9380 | AddOverloadCandidate(GD, I.getPair(), Inits, Candidates, |
| 9381 | SuppressUserConversions); |
| 9382 | } |
| 9383 | return Candidates.BestViableFunction(*this, Kind.getLocation(), Best); |
| 9384 | }; |
| 9385 | |
| 9386 | OverloadingResult Result = OR_No_Viable_Function; |
| 9387 | |
| 9388 | |
| 9389 | |
| 9390 | if (ListInit) { |
| 9391 | bool TryListConstructors = true; |
| 9392 | |
| 9393 | |
| 9394 | |
| 9395 | if (!ListInit->getNumInits()) { |
| 9396 | for (NamedDecl *D : Guides) { |
| 9397 | auto *FD = dyn_cast<FunctionDecl>(D->getUnderlyingDecl()); |
| 9398 | if (FD && FD->getMinRequiredArguments() == 0) { |
| 9399 | TryListConstructors = false; |
| 9400 | break; |
| 9401 | } |
| 9402 | } |
| 9403 | } else if (ListInit->getNumInits() == 1) { |
| 9404 | |
| 9405 | |
| 9406 | |
| 9407 | |
| 9408 | |
| 9409 | Expr *E = ListInit->getInit(0); |
| 9410 | auto *RD = E->getType()->getAsCXXRecordDecl(); |
| 9411 | if (!isa<InitListExpr>(E) && RD && |
| 9412 | isCompleteType(Kind.getLocation(), E->getType()) && |
| 9413 | isOrIsDerivedFromSpecializationOf(RD, Template)) |
| 9414 | TryListConstructors = false; |
| 9415 | } |
| 9416 | |
| 9417 | if (TryListConstructors) |
| 9418 | Result = tryToResolveOverload(); |
| 9419 | |
| 9420 | |
| 9421 | Inits = MultiExprArg(ListInit->getInits(), ListInit->getNumInits()); |
| 9422 | } |
| 9423 | |
| 9424 | |
| 9425 | |
| 9426 | if (Result == OR_No_Viable_Function) |
| 9427 | Result = tryToResolveOverload(); |
| 9428 | |
| 9429 | switch (Result) { |
| 9430 | case OR_Ambiguous: |
| 9431 | Diag(Kind.getLocation(), diag::err_deduced_class_template_ctor_ambiguous) |
| 9432 | << TemplateName; |
| 9433 | |
| 9434 | |
| 9435 | |
| 9436 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Inits); |
| 9437 | return QualType(); |
| 9438 | |
| 9439 | case OR_No_Viable_Function: { |
| 9440 | CXXRecordDecl *Primary = |
| 9441 | cast<ClassTemplateDecl>(Template)->getTemplatedDecl(); |
| 9442 | bool Complete = |
| 9443 | isCompleteType(Kind.getLocation(), Context.getTypeDeclType(Primary)); |
| 9444 | Diag(Kind.getLocation(), |
| 9445 | Complete ? diag::err_deduced_class_template_ctor_no_viable |
| 9446 | : diag::err_deduced_class_template_incomplete) |
| 9447 | << TemplateName << !Guides.empty(); |
| 9448 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Inits); |
| 9449 | return QualType(); |
| 9450 | } |
| 9451 | |
| 9452 | case OR_Deleted: { |
| 9453 | Diag(Kind.getLocation(), diag::err_deduced_class_template_deleted) |
| 9454 | << TemplateName; |
| 9455 | NoteDeletedFunction(Best->Function); |
| 9456 | return QualType(); |
| 9457 | } |
| 9458 | |
| 9459 | case OR_Success: |
| 9460 | |
| 9461 | |
| 9462 | |
| 9463 | if (Kind.isCopyInit() && ListInit && |
| 9464 | cast<CXXDeductionGuideDecl>(Best->Function)->isExplicit()) { |
| 9465 | bool IsDeductionGuide = !Best->Function->isImplicit(); |
| 9466 | Diag(Kind.getLocation(), diag::err_deduced_class_template_explicit) |
| 9467 | << TemplateName << IsDeductionGuide; |
| 9468 | Diag(Best->Function->getLocation(), |
| 9469 | diag::note_explicit_ctor_deduction_guide_here) |
| 9470 | << IsDeductionGuide; |
| 9471 | return QualType(); |
| 9472 | } |
| 9473 | |
| 9474 | |
| 9475 | |
| 9476 | DiagnoseUseOfDecl(Best->Function, Kind.getLocation()); |
| 9477 | MarkFunctionReferenced(Kind.getLocation(), Best->Function); |
| 9478 | break; |
| 9479 | } |
| 9480 | |
| 9481 | |
| 9482 | |
| 9483 | |
| 9484 | QualType DeducedType = |
| 9485 | SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); |
| 9486 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9487 | diag::warn_cxx14_compat_class_template_argument_deduction) |
| 9488 | << TSInfo->getTypeLoc().getSourceRange() << 1 << DeducedType; |
| 9489 | |
| 9490 | |
| 9491 | |
| 9492 | if (!HasAnyDeductionGuide) { |
| 9493 | Diag(TSInfo->getTypeLoc().getBeginLoc(), |
| 9494 | diag::warn_ctad_maybe_unsupported) |
| 9495 | << TemplateName; |
| 9496 | Diag(Template->getLocation(), diag::note_suppress_ctad_maybe_unsupported); |
| 9497 | } |
| 9498 | |
| 9499 | return DeducedType; |
| 9500 | } |
| 9501 | |