From 071f1b6062b459928ebfd6f2f60a8d6ad021112b Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 14 Aug 2024 18:41:48 -0700 Subject: Variadic Generics Part 1: parsing and type checking. (#4833) --- source/slang/slang-check-decl.cpp | 44 +++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) (limited to 'source/slang/slang-check-decl.cpp') diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 2e5e13360..58fbb4689 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -1561,7 +1561,7 @@ namespace Slang // A variable with an explicit type is simpler, for the // most part. SemanticsVisitor subVisitor(withDeclToExcludeFromLookup(varDecl)); - TypeExp typeExp = subVisitor.CheckUsableType(varDecl->type); + TypeExp typeExp = subVisitor.CheckUsableType(varDecl->type, varDecl); varDecl->type = typeExp; if (varDecl->type.equals(m_astBuilder->getVoidType())) { @@ -7128,6 +7128,11 @@ namespace Slang { args.add(DeclRefType::create(astBuilder, astBuilder->getDirectDeclRef(genericTypeParamDecl))); } + else if (auto genericTypePackParamDecl = as(mm)) + { + auto packType = DeclRefType::create(astBuilder, astBuilder->getDirectDeclRef(genericTypePackParamDecl)); + args.add(packType); + } else if (auto genericValueParamDecl = as(mm)) { if (semantics) @@ -7574,7 +7579,7 @@ namespace Slang if(typeExpr.exp) { SemanticsVisitor subVisitor(withDeclToExcludeFromLookup(paramDecl)); - typeExpr = subVisitor.CheckUsableType(typeExpr); + typeExpr = subVisitor.CheckUsableType(typeExpr, paramDecl); paramDecl->type = typeExpr; checkMeshOutputDecl(paramDecl); } @@ -7622,6 +7627,27 @@ namespace Slang } } } + else if (isTypePack(paramDecl->type.type)) + { + // For now, we only allow parameter packs to be `const`. + bool hasConstModifier = false; + for (auto modifier : paramDecl->modifiers) + { + if (as(modifier) || as(modifier) || as(modifier) || as(modifier)) + { + getSink()->diagnose(modifier, Diagnostics::parameterPackMustBeConst); + } + else if (as(modifier)) + { + hasConstModifier = true; + } + } + if (!hasConstModifier) + { + auto constModifier = this->getASTBuilder()->create(); + addModifier(paramDecl, constModifier); + } + } // Only texture types are allowed to have memory qualifiers on parameters if(!paramDecl->type || paramDecl->type->astNodeType != ASTNodeType::TextureType) @@ -8413,7 +8439,7 @@ namespace Slang void SemanticsDeclHeaderVisitor::visitSubscriptDecl(SubscriptDecl* decl) { - decl->returnType = CheckUsableType(decl->returnType); + decl->returnType = CheckUsableType(decl->returnType, decl); visitAbstractStorageDeclCommon(decl); @@ -8423,7 +8449,7 @@ namespace Slang void SemanticsDeclHeaderVisitor::visitPropertyDecl(PropertyDecl* decl) { SemanticsVisitor subVisitor(withDeclToExcludeFromLookup(decl)); - decl->type = subVisitor.CheckUsableType(decl->type); + decl->type = subVisitor.CheckUsableType(decl->type, decl); visitAbstractStorageDeclCommon(decl); checkVisibility(decl); } @@ -8639,7 +8665,7 @@ namespace Slang return createDefaultSubstitutionsIfNeeded(m_astBuilder, this, extDeclRef).as(); } - if (!TryUnifyTypes(constraints, extDecl->targetType.Ptr(), type)) + if (!TryUnifyTypes(constraints, ValUnificationContext(), extDecl->targetType.Ptr(), type)) return DeclRef(); ConversionCost baseCost; @@ -9554,12 +9580,12 @@ namespace Slang outTypeList.add(type); } } - OrderedDictionary> getCanonicalGenericConstraints( + OrderedDictionary> getCanonicalGenericConstraints( ASTBuilder* astBuilder, DeclRef genericDecl) { - OrderedDictionary> genericConstraints; - for (auto mm : getMembersOfType(astBuilder, genericDecl)) + OrderedDictionary> genericConstraints; + for (auto mm : getMembersOfType(astBuilder, genericDecl)) { genericConstraints[mm.getDecl()] = List(); } @@ -9574,7 +9600,7 @@ namespace Slang constraintTypes->add(genericTypeConstraintDecl.getDecl()->getSup().type); } - OrderedDictionary> result; + OrderedDictionary> result; for (auto& constraints : genericConstraints) { List typeList; -- cgit v1.2.3