Repository: llvm/llvm-project
File: clang/lib/Sema/SemaInit.cpp (C++)

Code before:
                                      unsigned &StructuredIndex) {
  if (!SemaRef.getLangOpts().HLSL)
    return;

  const ConstantMatrixType *MT = DeclType->castAs<ConstantMatrixType>();

  // For HLSL, the error reporting for this case is handled in SemaHLSL's
  // initializer list diagnostics. That means the execution should require
  // getNumElementsFlattened to equal getNumInits. In other words the execution
  // should never reach this point if this condition is not true".
  assert(IList->getNumInits() == MT->getNumElementsFlattened() &&
         "Inits must equal Matrix element count");

  QualType ElemTy = MT->getElementType();

  Index = 0;
  InitializedEntity ElemEnt =
      InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);

  while (Index < IList->getNumInits()) {
    // Not a sublist: just consume directly.
    // Note: In HLSL, elements of the InitListExpr are in row-major order, so no
    // change is needed to the Index.
    unsigned RowMajorIdx = Index;
    ElemEnt.setElementIndex(RowMajorIdx);
    CheckSubElementType(ElemEnt, IList, ElemTy, RowMajorIdx, StructuredList,
                        StructuredIndex);
    ++Index;
  }
}

void InitListChecker::CheckVectorType(const InitializedEntity &Entity,
                                      InitListExpr *IList, QualType DeclType,
                                      unsigned &Index,
                                      InitListExpr *StructuredList,
                                      unsigned &StructuredIndex) {
  const VectorType *VT = DeclType->castAs<VectorType>();
  unsigned maxElements = VT->getNumElements();
  unsigned numEltsInit = 0;
  QualType elementType = VT->getElementType();

  if (Index >= IList->getNumInits()) {
    // Make sure the element type can be value-initialized.
    CheckEmptyInitializable(
        InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity),
        IList->getEndLoc());
    return;
  }

  if (!SemaRef.getLangOpts().OpenCL && !SemaRef.getLangOpts().HLSL ) {
    // If the initializing element is a vector, try to copy-initialize

Review comment (suggestion):
```suggestion
    ElemEnt.setElementIndex(Index);
    CheckSubElementType(ElemEnt, IList, ElemTy, Index, StructuredList,
```

Code after:
                                      unsigned &StructuredIndex) {
  if (!SemaRef.getLangOpts().HLSL)
    return;

  const ConstantMatrixType *MT = DeclType->castAs<ConstantMatrixType>();

  // For HLSL, the error reporting for this case is handled in SemaHLSL's
  // initializer list diagnostics. That means the execution should require
  // getNumElementsFlattened to equal getNumInits. In other words the execution
  // should never reach this point if this condition is not true".
  assert(IList->getNumInits() == MT->getNumElementsFlattened() &&
         "Inits must equal Matrix element count");

  QualType ElemTy = MT->getElementType();

  Index = 0;
  InitializedEntity Element =
      InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);

  while (Index < IList->getNumInits()) {
    // Not a sublist: just consume directly.
    // Note: In HLSL, elements of the InitListExpr are in row-major order, so no
    // change is needed to the Index.
    Element.setElementIndex(Index);
    CheckSubElementType(Element, IList, ElemTy, Index, StructuredList,
                        StructuredIndex);
  }
}

void InitListChecker::CheckVectorType(const InitializedEntity &Entity,
                                      InitListExpr *IList, QualType DeclType,
                                      unsigned &Index,
                                      InitListExpr *StructuredList,
                                      unsigned &StructuredIndex) {
  const VectorType *VT = DeclType->castAs<VectorType>();
  unsigned maxElements = VT->getNumElements();
  unsigned numEltsInit = 0;
  QualType elementType = VT->getElementType();

  if (Index >= IList->getNumInits()) {
    // Make sure the element type can be value-initialized.
    CheckEmptyInitializable(
        InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity),
        IList->getEndLoc());
    return;
  }

  if (!SemaRef.getLangOpts().OpenCL && !SemaRef.getLangOpts().HLSL ) {
    // If the initializing element is a vector, try to copy-initialize
    // instead of breaking it apart (which is doomed to failure anyway).
    Expr *Init = IList->getInit(Index);