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

File modifier parsing #60823

Merged
merged 16 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/config/globalconfigs/Common.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dotnet_diagnostic.RS0006.severity = error
dotnet_diagnostic.RS0012.severity = warning
dotnet_diagnostic.RS0014.severity = warning
dotnet_diagnostic.RS0015.severity = warning
dotnet_diagnostic.RS0016.severity = error
dotnet_diagnostic.RS0016.severity = none # PROTOTYPE(ft): re-enable before feature merge
dotnet_diagnostic.RS0017.severity = error
dotnet_diagnostic.RS0018.severity = warning
dotnet_diagnostic.RS0022.severity = error
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7091,4 +7091,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="IDS_FeatureUnsignedRightShift" xml:space="preserve">
<value>unsigned right shift</value>
</data>
<data name="IDS_FeatureFileTypes" xml:space="preserve">
<value>file types</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ internal enum DeclarationModifiers : uint
Async = 1 << 20,
Ref = 1 << 21, // used only for structs

All = (1 << 23) - 1, // all modifiers
Unset = 1 << 23, // used when a modifiers value hasn't yet been computed

// PROTOTYPE(ft): leaving 22 free since required is using it
File = 1 << 23, // used only for types

All = (1 << 24) - 1, // all modifiers
Unset = 1 << 24, // used when a modifiers value hasn't yet been computed

AccessibilityMask = PrivateProtected | Private | Protected | Internal | ProtectedInternal | Public,
}
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/MessageID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ internal enum MessageID
IDS_FeatureUTF8StringLiterals = MessageBase + 12822,

IDS_FeatureUnsignedRightShift = MessageBase + 12823,

IDS_FeatureFileTypes = MessageBase + 12824, // PROTOTYPE(ft): finalize feature name
}

// Message IDs may refer to strings that need to be localized.
Expand Down Expand Up @@ -373,6 +375,7 @@ internal static LanguageVersion RequiredVersion(this MessageID feature)
case MessageID.IDS_FeatureCheckedUserDefinedOperators: // semantic check for declarations, parsing check for doc comments
case MessageID.IDS_FeatureUTF8StringLiterals: // semantic check
case MessageID.IDS_FeatureUnsignedRightShift: // semantic check for declarations and consumption, parsing check for doc comments
case MessageID.IDS_FeatureFileTypes: // semantic check
return LanguageVersion.Preview;

// C# 10.0 features.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 46 additions & 2 deletions src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ private static bool IsPossibleStartOfTypeDeclaration(SyntaxKind kind)
case SyntaxKind.SealedKeyword:
case SyntaxKind.StaticKeyword:
case SyntaxKind.UnsafeKeyword:
// PROTOTYPE(ft): it seems strange that we don't need this for any of the tests so far to pass.
// case SyntaxKind.FileKeyword:
case SyntaxKind.OpenBracketToken:
return true;
default:
Expand Down Expand Up @@ -1163,6 +1165,8 @@ internal static DeclarationModifiers GetModifier(SyntaxKind kind, SyntaxKind con
return DeclarationModifiers.Partial;
case SyntaxKind.AsyncKeyword:
return DeclarationModifiers.Async;
case SyntaxKind.FileKeyword:
return DeclarationModifiers.File;
}

goto default;
Expand All @@ -1173,6 +1177,7 @@ internal static DeclarationModifiers GetModifier(SyntaxKind kind, SyntaxKind con

private void ParseModifiers(SyntaxListBuilder tokens, bool forAccessors)
{
bool? isFollowedByType = null;
while (true)
{
var newMod = GetModifier(this.CurrentToken);
Expand Down Expand Up @@ -1243,6 +1248,46 @@ private void ParseModifiers(SyntaxListBuilder tokens, bool forAccessors)
break;
}

case DeclarationModifiers.File:
// 'file' is only a modifier if it is followed by an optional sequence of modifiers and then by a type keyword.
{
isFollowedByType ??= computeFollowedByType();
if (isFollowedByType.GetValueOrDefault())
{
// this is a file modifier.
modTok = ConvertToKeyword(EatToken());
break;
}

return;

bool computeFollowedByType()
{
var resetPoint = this.GetResetPoint();
try
{
while (true)
{
if (IsTypeDeclarationStart())
{
return true;
}
else if (!SyntaxFacts.IsKeywordKind(CurrentToken.ContextualKind))
{
return false;
}

EatToken();
}
}
finally
{
this.Reset(ref resetPoint);
this.Release(ref resetPoint);
}
}
}

case DeclarationModifiers.Async:
if (!ShouldAsyncBeTreatedAsModifier(parsingStatementNotDeclaration: false))
{
Expand Down Expand Up @@ -2661,8 +2706,7 @@ static bool isAcceptableNonDeclarationStatement(StatementSyntax statement, bool
private bool IsMisplacedModifier(SyntaxListBuilder modifiers, SyntaxList<AttributeListSyntax> attributes, TypeSyntax type, out MemberDeclarationSyntax result)
{
if (GetModifier(this.CurrentToken) != DeclarationModifiers.None &&
this.CurrentToken.ContextualKind != SyntaxKind.PartialKeyword &&
this.CurrentToken.ContextualKind != SyntaxKind.AsyncKeyword &&
this.CurrentToken.ContextualKind is not (SyntaxKind.PartialKeyword or SyntaxKind.AsyncKeyword or SyntaxKind.FileKeyword) &&
IsComplete(type))
{
var misplacedModifier = this.CurrentToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ internal static DeclarationModifiers CheckModifiers(
modifierErrors |= !Binder.CheckFeatureAvailability(errorLocation.SourceTree, MessageID.IDS_FeaturePrivateProtected, diagnostics, errorLocation);
}

if ((result & DeclarationModifiers.File) != 0)
{
modifierErrors |= !Binder.CheckFeatureAvailability(errorLocation.SourceTree, MessageID.IDS_FeatureFileTypes, diagnostics, errorLocation);
}

return result;
}

Expand Down Expand Up @@ -281,6 +286,8 @@ internal static string ConvertSingleModifierToSyntaxText(DeclarationModifiers mo
return SyntaxFacts.GetText(SyntaxKind.AsyncKeyword);
case DeclarationModifiers.Ref:
return SyntaxFacts.GetText(SyntaxKind.RefKeyword);
case DeclarationModifiers.File:
return SyntaxFacts.GetText(SyntaxKind.FileKeyword);
default:
throw ExceptionUtilities.UnexpectedValue(modifier);
}
Expand Down Expand Up @@ -328,6 +335,8 @@ private static DeclarationModifiers ToDeclarationModifier(SyntaxKind kind)
return DeclarationModifiers.Volatile;
case SyntaxKind.RefKeyword:
return DeclarationModifiers.Ref;
case SyntaxKind.FileKeyword:
return DeclarationModifiers.File;
default:
throw ExceptionUtilities.UnexpectedValue(kind);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ private DeclarationModifiers MakeModifiers(TypeKind typeKind, BindingDiagnosticB
{
Symbol containingSymbol = this.ContainingSymbol;
DeclarationModifiers defaultAccess;
var allowedModifiers = DeclarationModifiers.AccessibilityMask;
// PROTOTYPE(ft): confirm whether 'file' types can be nested
var allowedModifiers = DeclarationModifiers.AccessibilityMask | DeclarationModifiers.File;

if (containingSymbol.Kind == SymbolKind.Namespace)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/Syntax/SyntaxKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ public enum SyntaxKind : ushort
/// <summary>Represents <see langword="unmanaged"/>.</summary>
UnmanagedKeyword = 8446,

/// <summary>Represents <see langword="file"/>.</summary>
FileKeyword = 8447,

// when adding a contextual keyword following functions must be adapted:
// <see cref="SyntaxFacts.GetContextualKeywordKinds"/>
// <see cref="SyntaxFacts.IsContextualKeyword(SyntaxKind)"/>
Expand Down
7 changes: 6 additions & 1 deletion src/Compilers/CSharp/Portable/Syntax/SyntaxKindFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ public static SyntaxKind GetPreprocessorKeywordKind(string text)

public static IEnumerable<SyntaxKind> GetContextualKeywordKinds()
{
for (int i = (int)SyntaxKind.YieldKeyword; i <= (int)SyntaxKind.UnmanagedKeyword; i++)
for (int i = (int)SyntaxKind.YieldKeyword; i <= (int)SyntaxKind.FileKeyword; i++) // PROTOTYPE(ft): may conflict with required
{
yield return (SyntaxKind)i;
}
Expand Down Expand Up @@ -1191,6 +1191,7 @@ public static bool IsContextualKeyword(SyntaxKind kind)
case SyntaxKind.RecordKeyword:
case SyntaxKind.ManagedKeyword:
case SyntaxKind.UnmanagedKeyword:
case SyntaxKind.FileKeyword:
return true;
default:
return false;
Expand Down Expand Up @@ -1310,6 +1311,8 @@ public static SyntaxKind GetContextualKeywordKind(string text)
return SyntaxKind.ManagedKeyword;
case "unmanaged":
return SyntaxKind.UnmanagedKeyword;
case "file":
return SyntaxKind.FileKeyword;
default:
return SyntaxKind.None;
}
Expand Down Expand Up @@ -1751,6 +1754,8 @@ public static string GetText(SyntaxKind kind)
return "managed";
case SyntaxKind.UnmanagedKeyword:
return "unmanaged";
case SyntaxKind.FileKeyword:
return "file";
default:
return string.Empty;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading