From 0d6312f3be66f4bff9eec9606228db3edc309e2c Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 15 May 2025 12:51:29 -0700 Subject: Add checking for hlsl register semantic. (#7118) * Add checking for hlsl register semantic. * Fix. * Fix test. * Fix switch error. * Fix tests. --- source/slang/slang-ast-type.cpp | 3 +- source/slang/slang-check-decl.cpp | 91 ++++++++++++++++++++++++++++++++++++ source/slang/slang-check-type.cpp | 12 ++++- source/slang/slang-diagnostic-defs.h | 7 +++ source/slang/slang.cpp | 7 ++- 5 files changed, 116 insertions(+), 4 deletions(-) (limited to 'source') diff --git a/source/slang/slang-ast-type.cpp b/source/slang/slang-ast-type.cpp index ff4cc0d10..18f3f90bc 100644 --- a/source/slang/slang-ast-type.cpp +++ b/source/slang/slang-ast-type.cpp @@ -396,8 +396,7 @@ Type* TypeType::_createCanonicalTypeOverride() void GenericDeclRefType::_toTextOverride(StringBuilder& out) { - // TODO: what is appropriate here? - out << toSlice(">"); + out << getDeclRef(); } Type* GenericDeclRefType::_createCanonicalTypeOverride() diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index e7ce8721a..f340bd6fd 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -95,6 +95,8 @@ struct SemanticsDeclAttributesVisitor : public SemanticsDeclVisitorBase, void checkVarDeclCommon(VarDeclBase* varDecl); + void checkHLSLRegisterSemantic(VarDeclBase* varDecl, HLSLRegisterSemantic* registerSematnic); + void visitVarDecl(VarDecl* varDecl) { checkVarDeclCommon(varDecl); } // Synthesize the constructor declaration for a struct during header visit, as we @@ -12606,6 +12608,10 @@ void SemanticsDeclAttributesVisitor::checkVarDeclCommon(VarDeclBase* varDecl) { hasPushConstAttr = true; } + else if (auto registerSemantic = as(modifier)) + { + checkHLSLRegisterSemantic(varDecl, registerSemantic); + } } if (hasSpecConstAttr && hasPushConstAttr) { @@ -12623,6 +12629,91 @@ void SemanticsDeclAttributesVisitor::checkVarDeclCommon(VarDeclBase* varDecl) } } +void SemanticsDeclAttributesVisitor::checkHLSLRegisterSemantic( + VarDeclBase* varDecl, + HLSLRegisterSemantic* registerSemantic) +{ + auto registerName = registerSemantic->registerName.getContent(); + if (registerName.getLength() < 1) + { + getSink()->diagnose( + registerSemantic->registerName.getLoc(), + Diagnostics::invalidHLSLRegisterName, + registerSemantic->registerName); + return; + } + + // Check to make sure the HLSL semantic register name is consistent with the resource type. + + auto varType = getType(m_astBuilder, DeclRef(varDecl)); + varType = unwrapModifiedType(unwrapArrayType(varType)); + bool isValid = true; + if (auto resType = as(varType)) + { + switch (registerName[0]) + { + case 't': + if (resType->getAccess() != SLANG_RESOURCE_ACCESS_READ) + isValid = false; + break; + case 'u': + if (resType->getAccess() == SLANG_RESOURCE_ACCESS_READ) + isValid = false; + break; + case 's': + if (!resType->isCombined()) + isValid = false; + break; + default: + isValid = false; + break; + } + } + else if ( + as(varType) || as(varType) || + as(varType)) + { + if (registerName[0] != 't') + { + isValid = false; + } + } + else if ( + as(varType) || + as(varType) || + as(varType) || as(varType) || + as(varType) || + as(varType)) + { + if (registerName[0] != 'u') + { + isValid = false; + } + } + else if (as(varType)) + { + if (registerName[0] != 's') + { + isValid = false; + } + } + else if (as(varType)) + { + if (registerName[0] != 'b') + { + isValid = false; + } + } + if (!isValid) + { + getSink()->diagnose( + registerSemantic->registerName.getLoc(), + Diagnostics::invalidHLSLRegisterNameForType, + registerName, + varType); + } +} + void SemanticsDeclAttributesVisitor::checkForwardDerivativeOfAttribute( FunctionDeclBase* funcDecl, ForwardDerivativeOfAttribute* attr) diff --git a/source/slang/slang-check-type.cpp b/source/slang/slang-check-type.cpp index d32903175..b5f5240a5 100644 --- a/source/slang/slang-check-type.cpp +++ b/source/slang/slang-check-type.cpp @@ -331,17 +331,27 @@ bool SemanticsVisitor::CoerceToProperTypeImpl( if (outProperType) args.add(ExtractGenericArgVal(valParam->initExpr)); } - else if (auto constraintParam = as(member)) + } + + for (Decl* member : genericDeclRef.getDecl()->members) + { + if (auto constraintParam = as(member)) { auto genericParam = as(constraintParam->sub.type)->getDeclRef(); if (!genericParam) return false; auto genericTypeParamDecl = as(genericParam.getDecl()); if (!genericTypeParamDecl) + { + diagSink->diagnose(typeExp.exp, Diagnostics::genericTypeNeedsArgs, typeExp); return false; + } auto defaultType = CheckProperType(genericTypeParamDecl->initType); if (!defaultType) + { + diagSink->diagnose(typeExp.exp, Diagnostics::genericTypeNeedsArgs, typeExp); return false; + } auto witness = tryGetSubtypeWitness(defaultType, CheckProperType(constraintParam->sup)); if (!witness) diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h index 808d867b6..6bedcfe3e 100644 --- a/source/slang/slang-diagnostic-defs.h +++ b/source/slang/slang-diagnostic-defs.h @@ -1366,6 +1366,13 @@ DIAGNOSTIC( Error, variableCannotBePushAndSpecializationConstant, "'$0' cannot be a push constant and a specialization constant at the same time") + +DIAGNOSTIC(31221, Error, invalidHLSLRegisterName, "invalid HLSL register name '$0'.") +DIAGNOSTIC( + 31222, + Error, + invalidHLSLRegisterNameForType, + "invalid HLSL register name '$0' for type '$1'.") // Enums DIAGNOSTIC(32000, Error, invalidEnumTagType, "invalid tag type for 'enum': '$0'") diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index c7f6c920c..7fc519c03 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -2784,7 +2784,12 @@ Type* ComponentType::getTypeFromString(String const& typeStr, DiagnosticSink* si SLANG_AST_BUILDER_RAII(linkage->getASTBuilder()); Expr* typeExpr = linkage->parseTermString(typeStr, scope); - type = checkProperType(linkage, TypeExp(typeExpr), sink); + SharedSemanticsContext sharedSemanticsContext(linkage, nullptr, sink); + SemanticsVisitor visitor(&sharedSemanticsContext); + type = visitor.TranslateTypeNode(typeExpr); + auto typeOut = visitor.tryCoerceToProperType(TypeExp(type)); + if (typeOut.type) + type = typeOut.type; if (type) { -- cgit v1.2.3