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

Code before:
void InitListChecker::CheckMatrixType(const InitializedEntity &Entity,
                                      InitListExpr *IList, QualType DeclType,
                                      unsigned &Index,
                                      InitListExpr *StructuredList,
                                      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;

Review comment (suggestion):
I'm not sure how much of a concern this should be since there are no c\c++ initalizer lists for matrix types yet. But the reason I had wanted to do this here where it effects the AST is because my understanding was the default ordering for c\c++ was going to be column major. If we make this change here then we essentially decided for everyone that the AST order will be row major.

Code after:
void InitListChecker::CheckMatrixType(const InitializedEntity &Entity,
                                      InitListExpr *IList, QualType DeclType,
                                      unsigned &Index,
                                      InitListExpr *StructuredList,
                                      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;
  }