[clang] [HLSL] Disable implicit constructors for user-defined structs/classes (PR #194989)
Joshua Batista via cfe-commits
cfe-commits at lists.llvm.org
Fri May 1 13:09:55 PDT 2026
================
@@ -759,19 +759,40 @@ class CXXRecordDecl : public RecordDecl {
needsImplicitDefaultConstructor();
}
+ // Used by HLSL to determine if a record is a built-in implicit HLSL
+ // struct/class or a user-defined one. User-defined HLSL records cannot
+ // have ctors, dtors, or overloaded operators, while implicit built-in
+ // HLSL records such as resource classes can. It would be nice to use the
+ // isImplicit() methods to determine that, but this flag is not propagated
+ // to template-instanticated classes.
+ //
+ /// Determines whether this class has any user provided special members.
+ bool hasUserProvidedSpecialMembers() const {
+ return data().UserDeclaredSpecialMembers &
+ (SMF_MoveConstructor | SMF_MoveAssignment | SMF_Destructor |
+ SMF_CopyAssignment | SMF_CopyConstructor) ||
+ data().UserDeclaredConstructor ||
+ data().UserProvidedDefaultConstructor;
+ }
+
/// Determine if we need to declare a default constructor for
/// this class.
///
/// This value is used for lazy creation of default constructors.
bool needsImplicitDefaultConstructor() const {
- return (!data().UserDeclaredConstructor &&
- !(data().DeclaredSpecialMembers & SMF_DefaultConstructor) &&
- (!isLambda() || lambdaIsDefaultConstructibleAndAssignable())) ||
- // FIXME: Proposed fix to core wording issue: if a class inherits
- // a default constructor and doesn't explicitly declare one, one
- // is declared implicitly.
- (data().HasInheritedDefaultConstructor &&
- !(data().DeclaredSpecialMembers & SMF_DefaultConstructor));
+ return ((!data().UserDeclaredConstructor &&
+ !(data().DeclaredSpecialMembers & SMF_DefaultConstructor) &&
+ (!isLambda() || lambdaIsDefaultConstructibleAndAssignable())) ||
+ // FIXME: Proposed fix to core wording issue: if a class inherits
+ // a default constructor and doesn't explicitly declare one, one
+ // is declared implicitly.
+ (data().HasInheritedDefaultConstructor &&
+ !(data().DeclaredSpecialMembers & SMF_DefaultConstructor))) &&
+ // In HLSL, only built-in records like resources classes can have
+ // constructors.
+ (!getLangOpts().HLSL ||
+ (isLambda() && lambdaIsDefaultConstructibleAndAssignable()) ||
----------------
bob80905 wrote:
Can you explain in the comment above why this line is necessary?
Also, given that you are concerned about lambdas, would we need to change the `lambdaIsDefaultConstructibleAndAssignable` function? So that instead of return `getASTContext().getLangOpts().CPlusPlus20;`, it also checks for the HLSL language?
https://github.com/llvm/llvm-project/pull/194989
More information about the cfe-commits
mailing list