tools/clang/lib/AST/ASTContextHLSL.cpp
/// <summary>Adds up-front support for HLSL matrix types (just the template declaration).</summary>
Adds up-front support for HLSL matrix types (just the template declaration).
Adds up - front support for HLSL matrix types ( just the template declaration ) .
void hlsl::AddHLSLMatrixTemplate(ASTContext& context, ClassTemplateDecl* vectorTemplateDecl, ClassTemplateDecl** matrixTemplateDecl)
{
  DXASSERT_NOMSG(matrixTemplateDecl != nullptr);
  DXASSERT_NOMSG(vectorTemplateDecl != nullptr);
  DeclContext* currentDeclContext = context.getTranslationUnitDecl();
  IdentifierInfo& elementTemplateParamId = context.Idents.get(StringRef("element"), tok::TokenKind::identifier);
  TemplateTypeParmDecl *elementTemplateParamDecl = TemplateTypeParmDecl::Create(
    context, currentDeclContext, NoLoc, NoLoc,
    FirstTemplateDepth, FirstParamPosition, &elementTemplateParamId, TypenameFalse, ParameterPackFalse);
  elementTemplateParamDecl->setDefaultArgument(context.getTrivialTypeSourceInfo(context.FloatTy));
  QualType intType = context.IntTy;
  Expr *literalIntFour = IntegerLiteral::Create(
      context, llvm::APInt(context.getIntWidth(intType), 4), intType, NoLoc);
  IdentifierInfo& rowCountParamId = context.Idents.get(StringRef("row_count"), tok::TokenKind::identifier);
  NonTypeTemplateParmDecl* rowCountTemplateParamDecl = NonTypeTemplateParmDecl::Create(
    context, currentDeclContext, NoLoc, NoLoc,
    FirstTemplateDepth, FirstParamPosition + 1, &rowCountParamId, intType, ParameterPackFalse, context.getTrivialTypeSourceInfo(intType));
  rowCountTemplateParamDecl->setDefaultArgument(literalIntFour);
  IdentifierInfo& colCountParamId = context.Idents.get(StringRef("col_count"), tok::TokenKind::identifier);
  NonTypeTemplateParmDecl* colCountTemplateParamDecl = NonTypeTemplateParmDecl::Create(
    context, currentDeclContext, NoLoc, NoLoc,
    FirstTemplateDepth, FirstParamPosition + 2, &colCountParamId, intType, ParameterPackFalse, context.getTrivialTypeSourceInfo(intType));
  colCountTemplateParamDecl->setDefaultArgument(literalIntFour);
  NamedDecl* templateParameters[] =
  {
    elementTemplateParamDecl, rowCountTemplateParamDecl, colCountTemplateParamDecl
  };
  TemplateParameterList* templateParameterList = TemplateParameterList::Create(
    context, NoLoc, NoLoc, templateParameters, _countof(templateParameters), NoLoc);
  IdentifierInfo& matrixId = context.Idents.get(StringRef("matrix"), tok::TokenKind::identifier);
  CXXRecordDecl* templateRecordDecl = CXXRecordDecl::Create(
    context, TagDecl::TagKind::TTK_Class, currentDeclContext, NoLoc, NoLoc, &matrixId,
    nullptr, DelayTypeCreationTrue);
  ClassTemplateDecl* classTemplateDecl = ClassTemplateDecl::Create(
    context, currentDeclContext, NoLoc, DeclarationName(&matrixId),
    templateParameterList, templateRecordDecl, nullptr);
  templateRecordDecl->setDescribedClassTemplate(classTemplateDecl);
  QualType T = classTemplateDecl->getInjectedClassNameSpecialization();
  T = context.getInjectedClassNameType(templateRecordDecl, T);
  assert(T->isDependentType() && "Class template type is not dependent?");
  classTemplateDecl->setLexicalDeclContext(currentDeclContext);
  templateRecordDecl->setLexicalDeclContext(currentDeclContext);
  templateRecordDecl->startDefinition();
  QualType elementType = context.getTemplateTypeParmType(
       0, 0, ParameterPackFalse, elementTemplateParamDecl);
  Expr *sizeExpr = DeclRefExpr::Create(
      context, NestedNameSpecifierLoc(), NoLoc, rowCountTemplateParamDecl,
      false,
      DeclarationNameInfo(rowCountTemplateParamDecl->getDeclName(), NoLoc),
      intType, ExprValueKind::VK_RValue);
  Expr *rowSizeExpr = DeclRefExpr::Create(
      context, NestedNameSpecifierLoc(), NoLoc, colCountTemplateParamDecl,
      false,
      DeclarationNameInfo(colCountTemplateParamDecl->getDeclName(), NoLoc),
      intType, ExprValueKind::VK_RValue);
  QualType vectorType = context.getDependentSizedExtVectorType(
      elementType, rowSizeExpr, SourceLocation());
  QualType vectorArrayType = context.getDependentSizedArrayType(
      vectorType, sizeExpr, ArrayType::Normal, 0, SourceRange());
  AddHLSLHandleField(context, templateRecordDecl, vectorArrayType);
  const unsigned int templateDepth = 0;
  AddSubscriptOperator(context, templateDepth, elementTemplateParamDecl,
                       colCountTemplateParamDecl, context.UnsignedIntTy,
                       templateRecordDecl, vectorTemplateDecl, ForConstFalse);
  AddSubscriptOperator(context, templateDepth, elementTemplateParamDecl,
                       colCountTemplateParamDecl, context.UnsignedIntTy,
                       templateRecordDecl, vectorTemplateDecl, ForConstTrue);
  templateRecordDecl->completeDefinition();
  classTemplateDecl->setImplicit(true);
  templateRecordDecl->setImplicit(true);
  currentDeclContext->addDecl(classTemplateDecl);
  currentDeclContext->addDecl(templateRecordDecl);
#ifdef DBG
  DeclContext::lookup_result lookupResult = templateRecordDecl->lookup(
    DeclarationName(&context.Idents.get(StringRef("h"))));
  DXASSERT(!lookupResult.empty(), "otherwise matrix handle cannot be looked up");
#endif
  *matrixTemplateDecl = classTemplateDecl;
}
void hlsl :: AddHLSLMatrixTemplate ( ASTContext & context , ClassTemplateDecl * vectorTemplateDecl , ClassTemplateDecl * * matrixTemplateDecl ) { DXASSERT_NOMSG ( matrixTemplateDecl != nullptr ) ; DXASSERT_NOMSG ( vectorTemplateDecl != nullptr ) ; DeclContext * currentDeclContext = context . getTranslationUnitDecl ( ) ; IdentifierInfo & elementTemplateParamId = context . Idents . get ( StringRef ( " " ) , tok :: TokenKind :: identifier ) ; TemplateTypeParmDecl * elementTemplateParamDecl = TemplateTypeParmDecl :: Create ( context , currentDeclContext , NoLoc , NoLoc , FirstTemplateDepth , FirstParamPosition , & elementTemplateParamId , TypenameFalse , ParameterPackFalse ) ; elementTemplateParamDecl -> setDefaultArgument ( context . getTrivialTypeSourceInfo ( context . FloatTy ) ) ; QualType intType = context . IntTy ; Expr * literalIntFour = IntegerLiteral :: Create ( context , llvm :: APInt ( context . getIntWidth ( intType ) , 4 ) , intType , NoLoc ) ; IdentifierInfo & rowCountParamId = context . Idents . get ( StringRef ( " " ) , tok :: TokenKind :: identifier ) ; NonTypeTemplateParmDecl * rowCountTemplateParamDecl = NonTypeTemplateParmDecl :: Create ( context , currentDeclContext , NoLoc , NoLoc , FirstTemplateDepth , FirstParamPosition + 1 , & rowCountParamId , intType , ParameterPackFalse , context . getTrivialTypeSourceInfo ( intType ) ) ; rowCountTemplateParamDecl -> setDefaultArgument ( literalIntFour ) ; IdentifierInfo & colCountParamId = context . Idents . get ( StringRef ( " " ) , tok :: TokenKind :: identifier ) ; NonTypeTemplateParmDecl * colCountTemplateParamDecl = NonTypeTemplateParmDecl :: Create ( context , currentDeclContext , NoLoc , NoLoc , FirstTemplateDepth , FirstParamPosition + 2 , & colCountParamId , intType , ParameterPackFalse , context . getTrivialTypeSourceInfo ( intType ) ) ; colCountTemplateParamDecl -> setDefaultArgument ( literalIntFour ) ; NamedDecl * templateParameters [ ] = { elementTemplateParamDecl , rowCountTemplateParamDecl , colCountTemplateParamDecl } ; TemplateParameterList * templateParameterList = TemplateParameterList :: Create ( context , NoLoc , NoLoc , templateParameters , _countof ( templateParameters ) , NoLoc ) ; IdentifierInfo & matrixId = context . Idents . get ( StringRef ( " " ) , tok :: TokenKind :: identifier ) ; CXXRecordDecl * templateRecordDecl = CXXRecordDecl :: Create ( context , TagDecl :: TagKind :: TTK_Class , currentDeclContext , NoLoc , NoLoc , & matrixId , nullptr , DelayTypeCreationTrue ) ; ClassTemplateDecl * classTemplateDecl = ClassTemplateDecl :: Create ( context , currentDeclContext , NoLoc , DeclarationName ( & matrixId ) , templateParameterList , templateRecordDecl , nullptr ) ; templateRecordDecl -> setDescribedClassTemplate ( classTemplateDecl ) ; QualType T = classTemplateDecl -> getInjectedClassNameSpecialization ( ) ; T = context . getInjectedClassNameType ( templateRecordDecl , T ) ; assert ( T -> isDependentType ( ) && " " ) ; classTemplateDecl -> setLexicalDeclContext ( currentDeclContext ) ; templateRecordDecl -> setLexicalDeclContext ( currentDeclContext ) ; templateRecordDecl -> startDefinition ( ) ; QualType elementType = context . getTemplateTypeParmType ( 0 , 0 , ParameterPackFalse , elementTemplateParamDecl ) ; Expr * sizeExpr = DeclRefExpr :: Create ( context , NestedNameSpecifierLoc ( ) , NoLoc , rowCountTemplateParamDecl , false , DeclarationNameInfo ( rowCountTemplateParamDecl -> getDeclName ( ) , NoLoc ) , intType , ExprValueKind :: VK_RValue ) ; Expr * rowSizeExpr = DeclRefExpr :: Create ( context , NestedNameSpecifierLoc ( ) , NoLoc , colCountTemplateParamDecl , false , DeclarationNameInfo ( colCountTemplateParamDecl -> getDeclName ( ) , NoLoc ) , intType , ExprValueKind :: VK_RValue ) ; QualType vectorType = context . getDependentSizedExtVectorType ( elementType , rowSizeExpr , SourceLocation ( ) ) ; QualType vectorArrayType = context . getDependentSizedArrayType ( vectorType , sizeExpr , ArrayType :: Normal , 0 , SourceRange ( ) ) ; AddHLSLHandleField ( context , templateRecordDecl , vectorArrayType ) ; const unsigned int templateDepth = 0 ; AddSubscriptOperator ( context , templateDepth , elementTemplateParamDecl , colCountTemplateParamDecl , context . UnsignedIntTy , templateRecordDecl , vectorTemplateDecl , ForConstFalse ) ; AddSubscriptOperator ( context , templateDepth , elementTemplateParamDecl , colCountTemplateParamDecl , context . UnsignedIntTy , templateRecordDecl , vectorTemplateDecl , ForConstTrue ) ; templateRecordDecl -> completeDefinition ( ) ; classTemplateDecl -> setImplicit ( true ) ; templateRecordDecl -> setImplicit ( true ) ; currentDeclContext -> addDecl ( classTemplateDecl ) ; currentDeclContext -> addDecl ( templateRecordDecl ) ; #ifdef DBG DeclContext :: lookup_result lookupResult = templateRecordDecl -> lookup ( DeclarationName ( & context . Idents . get ( StringRef ( " " ) ) ) ) ; DXASSERT ( ! lookupResult . empty ( ) , " " ) ; #endif * matrixTemplateDecl = classTemplateDecl ; }
<summary>Adds up-front support for HLSL matrix types (just the template declaration).</summary>
<summary > Adds up - front support for HLSL matrix types ( just the template declaration ) . < / summary >
// Create a matrix template declaration in translation unit scope. // template<typename element, int row_count, int col_count> matrix { ... } // Requesting the class name specialization will fault in required types. // Add an 'h' field to hold the handle. // The type is vector<element, col>[row]. /*templateDepth*/ // Add an operator[]. The operator ranges from zero to rowcount-1, and returns a vector of colcount elements. // Both declarations need to be present for correct handling. // Verify that we can read the field member from the template record.