diff options
Diffstat (limited to 'source/slang/slang-check-decl.cpp')
| -rw-r--r-- | source/slang/slang-check-decl.cpp | 244 |
1 files changed, 81 insertions, 163 deletions
diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 9a4ee9d71..fbf5332b8 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -744,7 +744,7 @@ namespace Slang void visitInheritanceDecl(InheritanceDecl* inheritanceDecl); - void diagnoseUndeclaredCapability(Decl* decl, const DiagnosticInfo& diagnosticInfo, const CapabilityConjunctionSet* failedAvailableSet); + void diagnoseUndeclaredCapability(Decl* decl, const DiagnosticInfo& diagnosticInfo, const CapabilityAtomSet& failedAtomsInsideAvailableSet); }; @@ -9932,11 +9932,17 @@ namespace Slang oldCaps); } } + + // if stmt inside parent, set the provenance tracker to the calling function + if(!decl) + decl = visitor->getParentFuncOfVisitor(); if (referencedDecl && decl) { - for (auto& capSet : nodeCaps.getExpandedAtoms()) + for (auto& capSet : nodeCaps.getAtomSets()) { - for (auto atom : capSet.getExpandedAtoms()) + auto elements = capSet.getElements<CapabilityAtom>(); + decl->capabilityRequirementProvenance.reserve(decl->capabilityRequirementProvenance.getCount()+elements.getCount()); + for (auto atom : elements) { decl->capabilityRequirementProvenance.addIfNotExists(atom, DeclReferenceWithLoc{ referencedDecl, referenceLoc }); } @@ -10008,9 +10014,9 @@ namespace Slang } if (!maybeRequireCapability) - targetCap = (CapabilitySet(CapabilityName::any_target).getTargetsThisIsMissingFromOther(set)); + targetCap = (CapabilitySet(CapabilityName::any_target).getTargetsThisHasButOtherDoesNot(set)); else - targetCap = (maybeRequireCapability->capabilitySet.getTargetsThisIsMissingFromOther(set)); + targetCap = (maybeRequireCapability->capabilitySet.getTargetsThisHasButOtherDoesNot(set)); } else { @@ -10024,10 +10030,8 @@ namespace Slang { diagnoseCapabilityErrors(Base::getSink(), outerContext.getOptionSet(), targetCase->body->loc, Diagnostics::conflictingCapabilityDueToStatement, bodyCap, "target_switch", oldCap); } - for (auto& conjunction : targetCap.getExpandedAtoms()) - set.unionWith(conjunction); + set.unionWith(targetCap); } - set.canonicalize(); handleReferenceFunc(stmt, set, stmt->loc); } @@ -10092,8 +10096,7 @@ namespace Slang { for (auto decoration : parent->getModifiersOfType<RequireCapabilityAttribute>()) { - for (auto& set : decoration->capabilitySet.getExpandedAtoms()) - localDeclaredCaps.unionWith(set); + localDeclaredCaps.unionWith(decoration->capabilitySet); } } else @@ -10102,13 +10105,8 @@ namespace Slang shouldBreak = true; } // Merge decl's capability declaration with the parent. - for (auto& localConjunction : localDeclaredCaps.getExpandedAtoms()) - { - if (declaredCaps.isIncompatibleWith(localConjunction)) - declaredCaps.unionWith(localConjunction); - else - declaredCaps.join(localDeclaredCaps); - } + declaredCaps.nonDestructiveJoin(localDeclaredCaps); + // If the parent already has inferred capability requirements, we should stop now // since that already covers transitive parents. if (shouldBreak) @@ -10127,27 +10125,37 @@ namespace Slang decl->inferredCapabilityRequirements = getDeclaredCapabilitySet(decl); } - void SemanticsDeclCapabilityVisitor::visitFunctionDeclBase(FunctionDeclBase* funcDecl) + template<typename ProcessFunc> + static inline void _dispatchCapabilitiesVisitorOfFunctionDecl(SemanticsVisitor* visitor, FunctionDeclBase* funcDecl, ProcessFunc propegateFuncForReferences) { + visitor->setParentFuncOfVisitor(funcDecl); + for (auto member : funcDecl->members) { - ensureDecl(member, DeclCheckState::CapabilityChecked); - _propagateRequirement(this, funcDecl->inferredCapabilityRequirements, funcDecl, member, member->inferredCapabilityRequirements, member->loc); + visitor->ensureDecl(member, DeclCheckState::CapabilityChecked); + _propagateRequirement(visitor, funcDecl->inferredCapabilityRequirements, funcDecl, member, member->inferredCapabilityRequirements, member->loc); } - visitReferencedDecls(*this, funcDecl->body, funcDecl->loc, funcDecl->findModifier<RequireCapabilityAttribute>(), [this, funcDecl](SyntaxNode* node, const CapabilitySet& nodeCaps, SourceLoc refLoc) - { - _propagateRequirement(this, funcDecl->inferredCapabilityRequirements, funcDecl, node, nodeCaps, refLoc); - }); + + visitReferencedDecls(*visitor, funcDecl->body, funcDecl->loc, funcDecl->findModifier<RequireCapabilityAttribute>(), propegateFuncForReferences); if (!isEffectivelyStatic(funcDecl)) { auto parentAggTypeDecl = getParentAggTypeDecl(funcDecl); if (parentAggTypeDecl) { - ensureDecl(parentAggTypeDecl, DeclCheckState::CapabilityChecked); - _propagateRequirement(this, funcDecl->inferredCapabilityRequirements, funcDecl, parentAggTypeDecl, parentAggTypeDecl->inferredCapabilityRequirements, funcDecl->loc); + visitor->ensureDecl(parentAggTypeDecl, DeclCheckState::CapabilityChecked); + _propagateRequirement(visitor, funcDecl->inferredCapabilityRequirements, funcDecl, parentAggTypeDecl, parentAggTypeDecl->inferredCapabilityRequirements, funcDecl->loc); } } + } + + void SemanticsDeclCapabilityVisitor::visitFunctionDeclBase(FunctionDeclBase* funcDecl) + { + _dispatchCapabilitiesVisitorOfFunctionDecl(this, funcDecl, + [this, funcDecl](SyntaxNode* node, const CapabilitySet& nodeCaps, SourceLoc refLoc) + { + _propagateRequirement(this, funcDecl->inferredCapabilityRequirements, funcDecl, node, nodeCaps, refLoc); + }); auto declaredCaps = getDeclaredCapabilitySet(funcDecl); @@ -10169,26 +10177,12 @@ namespace Slang } auto vis = getDeclVisibility(funcDecl); + + // If 0 capabilities were annotated on a function, capabilities are inferred from the function body if (declaredCaps.isEmpty()) { - // If the user has not declared any capabilities, - // we should diagnose a warning if any_target is not - // a super-set by exact atoms. - if (vis == DeclVisibility::Public && !funcDecl->inferredCapabilityRequirements.isEmpty()) - { - if (!getModuleDecl(funcDecl)->isInLegacyLanguage) - { - if (!funcDecl->inferredCapabilityRequirements.isExactSubset(getAnyPlatformCapabilitySet())) - { - diagnoseCapabilityErrors( - getSink(), - this->getOptionSet(), - funcDecl->loc, - Diagnostics::missingCapabilityRequirementOnPublicDecl, - funcDecl, funcDecl->inferredCapabilityRequirements); - } - } - } + declaredCaps = funcDecl->inferredCapabilityRequirements; + return; } else { @@ -10199,7 +10193,7 @@ namespace Slang // At a minimum we will propagate shader requirements to our // function from calling children in all cases so the parent // can enforce shader targets correctly and propagate to `main` - const CapabilityConjunctionSet* failedAvailableCapabilityConjunction = nullptr; + CapabilityAtomSet failedAvailableCapabilityConjunction; if (!CapabilitySet::checkCapabilityRequirement( declaredCaps, funcDecl->inferredCapabilityRequirements, @@ -10209,7 +10203,7 @@ namespace Slang funcDecl->inferredCapabilityRequirements = declaredCaps; } else - funcDecl->inferredCapabilityRequirements.simpleJoinWithSetMask(declaredCaps, CapabilityName::stage); + funcDecl->inferredCapabilityRequirements.nonDestructiveJoin(declaredCaps); } else { @@ -10241,7 +10235,7 @@ namespace Slang ensureDecl(requirementDecl, DeclCheckState::CapabilityChecked); ensureDecl(implDecl.declRefBase, DeclCheckState::CapabilityChecked); - const CapabilityConjunctionSet* failedAvailableCapabilityConjunction = nullptr; + CapabilityAtomSet failedAvailableCapabilityConjunction; if (!CapabilitySet::checkCapabilityRequirement( requirementDecl->inferredCapabilityRequirements, implDecl.getDecl()->inferredCapabilityRequirements, @@ -10303,7 +10297,7 @@ namespace Slang return defaultVis; } - void diagnoseCapabilityProvenance(CompilerOptionSet& optionSet, DiagnosticSink* sink, Decl* decl, CapabilityAtom missingAtom) + void diagnoseCapabilityProvenance(CompilerOptionSet& optionSet, DiagnosticSink* sink, Decl* decl, CapabilityAtom atomToFind, bool optionallyNeverPrintDecl) { HashSet<Decl*> printedDecls; auto thisModule = getModuleDecl(decl); @@ -10311,9 +10305,9 @@ namespace Slang while (declToPrint) { printedDecls.add(declToPrint); - if (auto provenance = declToPrint->capabilityRequirementProvenance.tryGetValue(missingAtom)) + if (auto provenance = declToPrint->capabilityRequirementProvenance.tryGetValue(atomToFind)) { - sink->diagnose(provenance->referenceLoc, Diagnostics::seeUsingOf, provenance->referencedDecl); + diagnoseCapabilityErrors(sink, optionSet, provenance->referenceLoc, Diagnostics::seeUsingOf, provenance->referencedDecl); declToPrint = provenance->referencedDecl; if (printedDecls.contains(declToPrint)) break; @@ -10332,54 +10326,17 @@ namespace Slang break; } } - if (declToPrint) + if (declToPrint && !optionallyNeverPrintDecl) { diagnoseCapabilityErrors(sink, optionSet, declToPrint->loc, Diagnostics::seeDefinitionOf, declToPrint); } } - // Print diagnostics tracing which referenced decls are not compatible with the given atom. - void diagnoseIncompatibleAtomProvenance(SemanticsVisitor* visitor, DiagnosticSink* sink, Decl* decl, CapabilityAtom incompatibleAtom, int traceLevels = 10) + void SemanticsDeclCapabilityVisitor::diagnoseUndeclaredCapability(Decl* decl, const DiagnosticInfo& diagnosticInfo, const CapabilityAtomSet& failedAtomsInsideAvailableSet) { - Decl* refDecl = nullptr; - SourceLoc loc; - HashSet<Decl*> printedDecls; - while (traceLevels > 0) - { - refDecl = nullptr; - visitReferencedDecls(*visitor, decl, decl->loc, decl->findModifier<RequireCapabilityAttribute>(), [&](SyntaxNode* node, const CapabilitySet& nodeCaps, SourceLoc refLoc) - { - if (nodeCaps.isIncompatibleWith(incompatibleAtom)) - { - if (auto referencedDecl = as<Decl>(node)) - { - refDecl = referencedDecl; - loc = refLoc; - } - else - diagnoseCapabilityErrors(sink, visitor->getOptionSet(), refLoc, Diagnostics::seeDefinitionOf, "statement"); - } - }); - if (!refDecl) - break; - if (printedDecls.add(refDecl)) - { - diagnoseCapabilityErrors(sink, visitor->getOptionSet(), loc, Diagnostics::seeUsingOf, refDecl); - decl = refDecl; - } - else - { - break; - } - traceLevels--; - } - } - - void SemanticsDeclCapabilityVisitor::diagnoseUndeclaredCapability(Decl* decl, const DiagnosticInfo& diagnosticInfo, const CapabilityConjunctionSet* failedAvailableSet) - { - if (decl->inferredCapabilityRequirements.getExpandedAtoms().getCount() == 0) + if (decl->inferredCapabilityRequirements.isEmpty()) return; - if(!failedAvailableSet) + if(failedAtomsInsideAvailableSet.isEmpty() || failedAtomsInsideAvailableSet.contains((UInt)CapabilityAtom::Invalid)) return; // There are two causes for why type checking failed on failedAvailableSet. @@ -10394,90 +10351,51 @@ namespace Slang // } // In this case we should diagnose error reporting printf isn't defined on a required target. // - // The second scenario is when the callee is using a capability that is not provided by the requirement. - // For example: - // [require(hlsl,b,c)] - // void caller() - // { - // useD(); // require capability (hlsl,d) - // } - // In this case we should report that useD() is using a capability that is not declared by caller. - // - // Now, we detect if we are case 1. - if (decl->inferredCapabilityRequirements.isIncompatibleWith(*failedAvailableSet)) + { - // Find the most derived atom that is leading to the incompatiblity. - for (Index i = failedAvailableSet->getExpandedAtoms().getCount() - 1; i >= 0; i--) + CapabilityAtom outFailedAtom{}; + if (hasTargetAtom(failedAtomsInsideAvailableSet, outFailedAtom)) { - auto atom = failedAvailableSet->getExpandedAtoms()[i]; - if (!isDirectChildOfAbstractAtom(atom)) - continue; - if (decl->inferredCapabilityRequirements.isIncompatibleWith(atom)) + diagnoseCapabilityErrors(getSink(), this->getOptionSet(), decl->loc, Diagnostics::declHasDependenciesNotCompatibleOnTarget, decl, outFailedAtom); + + // Anything defined on a non-failed target atom may be the culprit to why we fail having a target capability. + // Print out all possible culprits. + CapabilityAtomSet failedAtomSet; + failedAtomSet.add((UInt)outFailedAtom); + CapabilityAtomSet targetsNotUsedSet; + CapabilityAtomSet::calcSubtract(targetsNotUsedSet, getAtomSetOfTargets(), failedAtomSet); + + for (auto atom : targetsNotUsedSet) { - diagnoseCapabilityErrors(getSink(), this->getOptionSet(), decl->loc, Diagnostics::declHasDependenciesNotDefinedOnTarget, decl, atom); - diagnoseIncompatibleAtomProvenance(this, getSink(), decl, atom); - return; + CapabilityAtom formattedAtom = (CapabilityAtom)atom; + diagnoseCapabilityProvenance(this->getOptionSet(), getSink(), decl, formattedAtom, true); } + return; } - return; } - // If we reach here, we are case 2. + //// The second scenario is when the callee is using a capability that is not provided by the requirement. + //// For example: + //// [require(hlsl,b,c)] + //// void caller() + //// { + //// useD(); // require capability (hlsl,d) + //// } + //// In this case we should report that useD() is using a capability that is not declared by caller. + //// - CapabilityConjunctionSet* matchingRequirement = &decl->inferredCapabilityRequirements.getExpandedAtoms().getFirst(); - CapabilityAtom missingAtom = matchingRequirement->getExpandedAtoms().getFirst(); - if (missingAtom == CapabilityAtom::Invalid) - return; + //// If we reach here, we are case 2. - if (failedAvailableSet) + // We will produce all failed atoms. This is important since provenance of multiple atoms + // can come from multiple referenced items in a function body. + for (auto i : failedAtomsInsideAvailableSet) { - Int maxIntersectionCount = 0; - for (auto& usedSet : decl->inferredCapabilityRequirements.getExpandedAtoms()) - { - auto intersection = usedSet.countIntersectionWith(*failedAvailableSet); - if (intersection > maxIntersectionCount) - { - matchingRequirement = &usedSet; - maxIntersectionCount = intersection; - } - } - Index pos = 0; - for (Index i = 0; i < matchingRequirement->getExpandedAtoms().getCount(); i++) - { - auto atom = matchingRequirement->getExpandedAtoms()[i]; - while (pos < failedAvailableSet->getExpandedAtoms().getCount()) - { - if (failedAvailableSet->getExpandedAtoms()[pos] < atom) - pos++; - else - break; - } - - if (pos >= failedAvailableSet->getExpandedAtoms().getCount() || - failedAvailableSet->getExpandedAtoms()[pos] != atom) - { - missingAtom = atom; - break; - } - } - - // Select the most derived atom of `missingAtom`. - for (Index i = matchingRequirement->getExpandedAtoms().getCount() - 1; i >= 0 ; i--) - { - auto atom = matchingRequirement->getExpandedAtoms()[i]; - if (CapabilityConjunctionSet(atom).implies(missingAtom)) - { - missingAtom = atom; - break; - } - } + CapabilityAtom formattedAtom = (CapabilityAtom)i; + diagnoseCapabilityErrors(getSink(), this->getOptionSet(), decl->loc, diagnosticInfo, decl, formattedAtom); + // Print provenances. + diagnoseCapabilityProvenance(this->getOptionSet(), getSink(), decl, formattedAtom); } - - diagnoseCapabilityErrors(getSink(), this->getOptionSet(), decl->loc, diagnosticInfo, decl, missingAtom); - - // Print provenances. - diagnoseCapabilityProvenance(this->getOptionSet(), getSink(), decl, missingAtom); } } |
