[clang] [HLSL] Implement output parameter (PR #101083)
John McCall via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 15 17:28:05 PDT 2024
================
@@ -1121,3 +1121,99 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
}
return false;
}
+
+bool SemaHLSL::CheckCompatibleParameterABI(FunctionDecl *New,
+ FunctionDecl *Old) {
+ if (New->getNumParams() != Old->getNumParams())
+ return true;
+
+ bool HadError = false;
+
+ for (unsigned i = 0, e = New->getNumParams(); i != e; ++i) {
+ ParmVarDecl *NewParam = New->getParamDecl(i);
+ ParmVarDecl *OldParam = Old->getParamDecl(i);
+
+ // HLSL parameter declarations for inout and out must match between
+ // declarations. In HLSL inout and out are ambiguous at the call site,
+ // but have different calling behavior, so you cannot overload a
+ // method based on a difference between inout and out annotations.
+ const auto *NDAttr = NewParam->getAttr<HLSLParamModifierAttr>();
+ unsigned NSpellingIdx = (NDAttr ? NDAttr->getSpellingListIndex() : 0);
+ const auto *ODAttr = OldParam->getAttr<HLSLParamModifierAttr>();
+ unsigned OSpellingIdx = (ODAttr ? ODAttr->getSpellingListIndex() : 0);
+
+ if (NSpellingIdx != OSpellingIdx) {
+ SemaRef.Diag(NewParam->getLocation(),
+ diag::err_hlsl_param_qualifier_mismatch)
+ << NDAttr << NewParam;
+ SemaRef.Diag(OldParam->getLocation(), diag::note_previous_declaration_as)
+ << ODAttr;
+ HadError = true;
+ }
+ }
+ return HadError;
+}
+
+ExprResult SemaHLSL::ActOnOutParamExpr(ParmVarDecl *Param, Expr *Arg) {
+ assert(Param->hasAttr<HLSLParamModifierAttr>() &&
+ "We should not get here without a parameter modifier expression");
+ const auto *Attr = Param->getAttr<HLSLParamModifierAttr>();
+ if (Attr->getABI() == ParameterABI::Ordinary)
+ return ExprResult(Arg);
+
+ bool IsInOut = Attr->getABI() == ParameterABI::HLSLInOut;
+ if (!Arg->isLValue()) {
+ SemaRef.Diag(Arg->getBeginLoc(), diag::error_hlsl_inout_lvalue)
+ << Arg << (IsInOut ? 1 : 0);
+ return ExprError();
+ }
+
+ ASTContext &Ctx = SemaRef.getASTContext();
+
+ QualType Ty = Param->getType().getNonLValueExprType(Ctx);
+
+ // HLSL allows implicit conversions from scalars to vectors, but not the
+ // inverse, so we need to disallow `inout` with scalar->vector or
+ // scalar->matrix conversions.
+ if (Arg->getType()->isScalarType() != Ty->isScalarType()) {
+ SemaRef.Diag(Arg->getBeginLoc(), diag::error_hlsl_inout_scalar_extension)
+ << Arg << (IsInOut ? 1 : 0);
+ return ExprError();
+ }
+
+ bool RequiresConversion =
+ Ty.getUnqualifiedType() != Arg->getType().getUnqualifiedType();
----------------
rjmccall wrote:
This is actually doing a non-canonical comparison, which is not what you want; the test case is just writing one of these with a `typedef` of the type of the other. There's a `ASTContext::hasSameUnqualifiedType` that you should use.
https://github.com/llvm/llvm-project/pull/101083
More information about the cfe-commits
mailing list