Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup handling of flags in internal syntax nodes. #72130

Merged
merged 11 commits into from
Feb 19, 2024

Conversation

CyrusNajmabadi
Copy link
Member

Followup to #72104

Things i noticed and wanted to clean up, but weren't strictly necessary for the core changes in those other prs.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 15, 2024
@@ -24100,13 +24100,13 @@ internal abstract partial class DirectiveTriviaSyntax : StructuredTriviaSyntax
internal DirectiveTriviaSyntax(SyntaxKind kind, DiagnosticInfo[]? diagnostics, SyntaxAnnotation[]? annotations)
: base(kind, diagnostics, annotations)
{
this.flags |= NodeFlags.ContainsDirectives;
SetFlags(NodeFlags.ContainsDirectives);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had a mismash of callers either directly manipulating the flags, or calling the helpers. Now, everyone uses the helpers (teh core flags are not exposed now either).

@@ -175,7 +175,7 @@ internal static DirectiveStack ApplyDirectivesToListOrNode(GreenNode listOrNode,

internal virtual IList<DirectiveTriviaSyntax> GetDirectives()
{
if ((this.flags & NodeFlags.ContainsDirectives) != 0)
if (this.ContainsDirectives)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a tiny handful of cases were examining the flags, instead of the existing api that more easily and correctly checks these cases.

get => _nodeFlagsAndSlotCount.SmallSlotCount;
set => _nodeFlagsAndSlotCount.SmallSlotCount = value;
}
private NodeFlags Flags => _nodeFlagsAndSlotCount.NodeFlags;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now private. No need for anything outside of this type to access this value. All functionality is already exposed through clearer properties, or helper methods.

ContainsSkippedText = 1 << 8,
ContainsStructuredTrivia = 1 << 9,

InheritMask = IsNotMissing | ContainsAnnotations | ContainsAttributes | ContainsDiagnostics | ContainsDirectives | ContainsSkippedText | ContainsStructuredTrivia,
}

internal void SetFlags(NodeFlags flags)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: i wanted to make these protected. That works in C#, but vb has a few places taht call these directly on a node (odd).

this is also nice as we now only expose the ability to set/clear bits. not the ability to accidentally assign over the node-flags entirely.

}

internal bool IsMissing
{
get
{
// flag has reversed meaning hence "=="
return (this.flags & NodeFlags.IsNotMissing) == 0;
return (this.Flags & NodeFlags.IsNotMissing) == 0;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

properly renamed to represent that this is a property.

// Note: it's ok to examine SmallSlotCount here. All we're trying to do is make sure we have at least one
// child. And SmallSlotCount works both for small counts and large counts. This avoids an unnecessary
// virtual call for large list nodes.
while (node?._nodeFlagsAndSlotCount.SmallSlotCount > 0);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code was def odd (the direct use of _slotCount). but it's technically totally ok to do that. i've added docs to say why.

@@ -103,7 +103,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax

Friend Sub New(ByVal kind As SyntaxKind, empty As InternalSyntax.PunctuationSyntax)
MyBase.New(kind)
MyBase._slotCount = 1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vb overrode this, instead of using the existing SlotCount property. C# uses SlotCount. This brings the generated code in sync.

@@ -83,20 +83,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
End Get
End Property

Protected Overrides Function GetSlotCount() As Integer
Throw ExceptionUtilities.Unreachable
End Function
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic moved up. both vb and c# should share it.

Set(value As Integer)
Me.SlotCount = value
End Set
End Property
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

such a bizarre property. it existed just to forward to SlotCount... so i've removed it entirely.

}

/// <inheritdoc cref="NodeFlagsAndSlotCount.SmallSlotCount"/>>
private byte _slotCount
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this was private, i just inlined it into the tiny handful of cases in this code that referenced it.

get => _nodeFlagsAndSlotCount.SmallSlotCount;
set => _nodeFlagsAndSlotCount.SmallSlotCount = value;
}
internal NodeFlags Flags => _nodeFlagsAndSlotCount.NodeFlags;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to make this private. But VB has a couple of usages realted to caching tokens where it directly introspects this flag.

@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review February 15, 2024 23:12
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner February 15, 2024 23:12
@CyrusNajmabadi
Copy link
Member Author

@RikkiGibson @333fred ptal.

@CyrusNajmabadi
Copy link
Member Author

This it eh last flag-related pr i expect ot make for a while.

@RikkiGibson RikkiGibson self-assigned this Feb 15, 2024
@AlekseyTs
Copy link
Contributor

Done with review pass (commit 9)

@CyrusNajmabadi
Copy link
Member Author

I believe feedback is resolved.

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (commit 11)

@CyrusNajmabadi CyrusNajmabadi enabled auto-merge (squash) February 19, 2024 18:41
@CyrusNajmabadi CyrusNajmabadi merged commit 449a170 into dotnet:main Feb 19, 2024
30 checks passed
@ghost ghost added this to the Next milestone Feb 19, 2024
@CyrusNajmabadi CyrusNajmabadi deleted the flagCleanup branch February 19, 2024 19:31
@jjonescz jjonescz modified the milestones: Next, 17.10 P2 Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants