tools/clang/lib/AST/ASTContextHLSL.cpp
/// <summary>Adds up-front support for HLSL vector types (just the template declaration).</summary>
Adds up-front support for HLSL vector types (just the template declaration).
Adds up - front support for HLSL vector types ( just the template declaration ) .
void hlsl::AddHLSLVectorTemplate(ASTContext& context, ClassTemplateDecl** vectorTemplateDecl)
{
  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& colCountParamId = context.Idents.get(StringRef("element_count"), tok::TokenKind::identifier);
  NonTypeTemplateParmDecl* colCountTemplateParamDecl = NonTypeTemplateParmDecl::Create(
    context, currentDeclContext, NoLoc, NoLoc,
    FirstTemplateDepth, FirstParamPosition + 1, &colCountParamId, intType, ParameterPackFalse, nullptr);
  colCountTemplateParamDecl->setDefaultArgument(literalIntFour);
  NamedDecl* templateParameters[] =
  {
    elementTemplateParamDecl, colCountTemplateParamDecl
  };
  TemplateParameterList* templateParameterList = TemplateParameterList::Create(
    context, NoLoc, NoLoc, templateParameters, _countof(templateParameters), NoLoc);
  IdentifierInfo& vectorId = context.Idents.get(StringRef("vector"), tok::TokenKind::identifier);
  CXXRecordDecl* templateRecordDecl = CXXRecordDecl::Create(
    context, TagDecl::TagKind::TTK_Class, currentDeclContext, NoLoc, NoLoc, &vectorId,
    nullptr, DelayTypeCreationTrue);
  ClassTemplateDecl* classTemplateDecl = ClassTemplateDecl::Create(
    context, currentDeclContext, NoLoc, DeclarationName(&vectorId),
    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();
  AddHLSLHandleField(context, templateRecordDecl, QualType(GetHLSLObjectHandleType(context)));
  const unsigned int templateDepth = 0;
  QualType resultType = context.getTemplateTypeParmType(
    templateDepth, 0, ParameterPackFalse, elementTemplateParamDecl);
  QualType refResultType = context.getConstType(context.getLValueReferenceType(resultType));
  CXXMethodDecl* functionDecl = CreateObjectFunctionDeclarationWithParams(
    context, templateRecordDecl, refResultType,
    ArrayRef<QualType>(context.UnsignedIntTy), ArrayRef<StringRef>(StringRef("index")),
    context.DeclarationNames.getCXXOperatorName(OO_Subscript), ForConstTrue);
  AddHLSLVectorSubscriptAttr(functionDecl, context);
  resultType = context.getLValueReferenceType(resultType);
  functionDecl = CreateObjectFunctionDeclarationWithParams(
    context, templateRecordDecl, resultType,
    ArrayRef<QualType>(context.UnsignedIntTy), ArrayRef<StringRef>(StringRef("index")),
    context.DeclarationNames.getCXXOperatorName(OO_Subscript), ForConstFalse);
  AddHLSLVectorSubscriptAttr(functionDecl, context);
  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 vector handle cannot be looked up");
#endif
  *vectorTemplateDecl = classTemplateDecl;
}
void hlsl :: AddHLSLVectorTemplate ( ASTContext & context , ClassTemplateDecl * * vectorTemplateDecl ) { 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 & colCountParamId = context . Idents . get ( StringRef ( " " ) , tok :: TokenKind :: identifier ) ; NonTypeTemplateParmDecl * colCountTemplateParamDecl = NonTypeTemplateParmDecl :: Create ( context , currentDeclContext , NoLoc , NoLoc , FirstTemplateDepth , FirstParamPosition + 1 , & colCountParamId , intType , ParameterPackFalse , nullptr ) ; colCountTemplateParamDecl -> setDefaultArgument ( literalIntFour ) ; NamedDecl * templateParameters [ ] = { elementTemplateParamDecl , colCountTemplateParamDecl } ; TemplateParameterList * templateParameterList = TemplateParameterList :: Create ( context , NoLoc , NoLoc , templateParameters , _countof ( templateParameters ) , NoLoc ) ; IdentifierInfo & vectorId = context . Idents . get ( StringRef ( " " ) , tok :: TokenKind :: identifier ) ; CXXRecordDecl * templateRecordDecl = CXXRecordDecl :: Create ( context , TagDecl :: TagKind :: TTK_Class , currentDeclContext , NoLoc , NoLoc , & vectorId , nullptr , DelayTypeCreationTrue ) ; ClassTemplateDecl * classTemplateDecl = ClassTemplateDecl :: Create ( context , currentDeclContext , NoLoc , DeclarationName ( & vectorId ) , 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 ( ) ; AddHLSLHandleField ( context , templateRecordDecl , QualType ( GetHLSLObjectHandleType ( context ) ) ) ; const unsigned int templateDepth = 0 ; QualType resultType = context . getTemplateTypeParmType ( templateDepth , 0 , ParameterPackFalse , elementTemplateParamDecl ) ; QualType refResultType = context . getConstType ( context . getLValueReferenceType ( resultType ) ) ; CXXMethodDecl * functionDecl = CreateObjectFunctionDeclarationWithParams ( context , templateRecordDecl , refResultType , ArrayRef < QualType > ( context . UnsignedIntTy ) , ArrayRef < StringRef > ( StringRef ( " " ) ) , context . DeclarationNames . getCXXOperatorName ( OO_Subscript ) , ForConstTrue ) ; AddHLSLVectorSubscriptAttr ( functionDecl , context ) ; resultType = context . getLValueReferenceType ( resultType ) ; functionDecl = CreateObjectFunctionDeclarationWithParams ( context , templateRecordDecl , resultType , ArrayRef < QualType > ( context . UnsignedIntTy ) , ArrayRef < StringRef > ( StringRef ( " " ) ) , context . DeclarationNames . getCXXOperatorName ( OO_Subscript ) , ForConstFalse ) ; AddHLSLVectorSubscriptAttr ( functionDecl , context ) ; 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 * vectorTemplateDecl = classTemplateDecl ; }
<summary>Adds up-front support for HLSL vector types (just the template declaration).</summary>
<summary > Adds up - front support for HLSL vector types ( just the template declaration ) . < / summary >
// Create a vector template declaration in translation unit scope. // template<typename element, int element_count> vector { ... } // Requesting the class name specialization will fault in required types. // Add an 'h' field to hold the handle. // Add an operator[]. The operator ranges from zero to colcount-1, and returns a scalar. // ForConstTrue: // ForConstFalse: // Both declarations need to be present for correct handling. // Verify that we can read the field member from the template record.