Skip to content

Commit 919eb7f

Browse files
committed
Fix static being applied to classes without static methods/fields
1 parent 3145490 commit 919eb7f

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/Generator.Tests/Passes/TestPasses.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ public void TestCheckStaticClassPass()
7373
var staticStruct = AstContext.Class("TestCheckStaticStruct");
7474
var staticClassDeletedCtor = AstContext.Class("TestCheckStaticClassDeleted");
7575
var nonStaticClass = AstContext.Class("TestCheckNonStaticClass");
76+
var nonStaticEmptyClass = AstContext.Class("TestCommentsPass");
7677

7778
Assert.IsFalse(staticClass.IsStatic);
7879
Assert.IsFalse(staticStruct.IsStatic);
7980
Assert.IsFalse(staticClassDeletedCtor.IsStatic);
8081
Assert.IsFalse(nonStaticClass.IsStatic);
82+
Assert.IsFalse(nonStaticEmptyClass.IsStatic);
8183

8284
passBuilder.AddPass(new CheckStaticClassPass());
8385
passBuilder.RunPasses(pass => pass.VisitASTContext(AstContext));
@@ -87,6 +89,7 @@ public void TestCheckStaticClassPass()
8789
Assert.IsTrue(staticClassDeletedCtor.IsStatic, "`TestCheckStaticClassDeleted` should be static");
8890

8991
Assert.IsFalse(nonStaticClass.IsStatic, "`TestCheckNonStaticClass` should NOT be static, since it has a private data field with default ctor");
92+
Assert.IsFalse(nonStaticEmptyClass.IsStatic, "`TestCommentsPass` should NOT be static, since it doesn't have any static declarations");
9093
}
9194

9295
[Test]

src/Generator/Passes/CheckStaticClassPass.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ public override bool VisitClassDecl(Class @class)
7777
if (@class.IsDependent)
7878
return false;
7979

80+
// Polymorphic classes are currently not supported.
81+
// TODO: We could support this if the base class is also static, since it's composition then.
82+
if (@class.IsPolymorphic)
83+
return false;
84+
85+
// Make sure we have at least one accessible static method or field
86+
if (!@class.Methods.Any(m => m.Kind == CXXMethodKind.Normal && m.Access != AccessSpecifier.Private && m.IsStatic)
87+
&& @class.Variables.All(v => v.Access == AccessSpecifier.Private))
88+
return false;
89+
90+
// Check for any non-static fields or methods, in which case we
91+
// assume the class is not meant to be static.
92+
// Note: Static fields are represented as variables in the AST.
93+
if (@class.Fields.Count != 0)
94+
return false;
95+
96+
if (@class.Methods.Any(m => m.Kind == CXXMethodKind.Normal && !m.IsStatic))
97+
return false;
98+
8099
if (@class.Constructors.Any(m =>
81100
{
82101
// Implicit constructors are not user-defined, so assume this was unintentional.
@@ -97,13 +116,6 @@ public override bool VisitClassDecl(Class @class)
97116
{
98117
return false;
99118
}
100-
// Check for any non-static fields or methods, in which case we
101-
// assume the class is not meant to be static.
102-
// Note: Static fields are represented as variables in the AST.
103-
if (@class.Fields.Any() ||
104-
@class.Methods.Any(m => m.Kind == CXXMethodKind.Normal
105-
&& !m.IsStatic))
106-
return false;
107119

108120
// Check for any static function that return a pointer to the class.
109121
// If one exists, we assume it's a factory function and the class is

0 commit comments

Comments
 (0)