Skip to content

Commit

Permalink
Add support for generics constraint, many bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
macsux committed Oct 26, 2024
1 parent 5a4194e commit 7c2f370
Show file tree
Hide file tree
Showing 50 changed files with 2,648 additions and 202 deletions.
5 changes: 3 additions & 2 deletions Rewrite/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsNotAsErrors>CS0618</WarningsNotAsErrors>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
Expand All @@ -17,10 +18,10 @@
</PropertyGroup>

<PropertyGroup Condition="'$(RELEASE_PUBLICATION)'!=''">
<RewriteRemoteVersion>0.9.7</RewriteRemoteVersion>
<RewriteRemoteVersion>0.9.10</RewriteRemoteVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(RELEASE_PUBLICATION)'==''">
<RewriteRemoteVersion>0.9.7</RewriteRemoteVersion>
<RewriteRemoteVersion>0.9.10</RewriteRemoteVersion>
</PropertyGroup>

<Import Project="Directory.Build.props.user" Condition="Exists('Directory.Build.props.user')"/>
Expand Down
104 changes: 83 additions & 21 deletions Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ public override Cs VisitCompilationUnit(Cs.CompilationUnit compilationUnit, Prin
return compilationUnit;
}

public override J? VisitClassDeclaration(Cs.ClassDeclaration classDeclaration, PrintOutputCapture<TState> p)
{
_delegate.VisitClassDeclaration(classDeclaration.ClassDeclarationCore, p);
return classDeclaration;
}

public override J? VisitMethodDeclaration(Cs.MethodDeclaration methodDeclaration, PrintOutputCapture<TState> p)
{
base.VisitMethodDeclaration(methodDeclaration, p);
if (methodDeclaration.MethodDeclarationCore.Body == null)
{
p.Append(";");
}
return methodDeclaration;
}

public override J? VisitAnnotatedStatement(Cs.AnnotatedStatement annotatedStatement, PrintOutputCapture<TState> p)
{
BeforeSyntax(annotatedStatement, CsSpace.Location.ANNOTATED_STATEMENT_PREFIX, p);
Expand Down Expand Up @@ -493,6 +509,38 @@ protected void VisitStatement(JRightPadded<Statement> paddedStat, CsRightPadded.
_delegate.PrintStatementTerminator(paddedStat.Element, p);
}

public override J? VisitTypeParameterConstraintClause(Cs.TypeParameterConstraintClause typeParameterConstraintClause, PrintOutputCapture<TState> p)
{
BeforeSyntax(typeParameterConstraintClause, CsSpace.Location.TYPE_PARAMETERS_CONSTRAINT_CLAUSE_PREFIX, p);
p.Append("where");
VisitRightPadded(typeParameterConstraintClause.Padding.TypeParameter, CsRightPadded.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER, p);
p.Append(":");
VisitContainer("", typeParameterConstraintClause.Padding.TypeParameterConstraints, CsContainer.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_CONSTRAINTS, ",", "", p);
AfterSyntax(typeParameterConstraintClause, p);
return typeParameterConstraintClause;
}

public override J? VisitClassOrStructConstraint(Cs.ClassOrStructConstraint classOrStructConstraint, PrintOutputCapture<TState> p)
{
BeforeSyntax(classOrStructConstraint, CsSpace.Location.TYPE_PARAMETERS_CONSTRAINT_PREFIX, p);
p.Append(classOrStructConstraint.Kind == Cs.ClassOrStructConstraint.TypeKind.Class ? "class" : "struct");
return classOrStructConstraint;
}

public override J? VisitConstructorConstraint(Cs.ConstructorConstraint constructorConstraint, PrintOutputCapture<TState> p)
{
BeforeSyntax(constructorConstraint, CsSpace.Location.TYPE_PARAMETERS_CONSTRAINT_PREFIX, p);
p.Append("new()");
return constructorConstraint;
}

public override J? VisitDefaultConstraint(Cs.DefaultConstraint defaultConstraint, PrintOutputCapture<TState> p)
{
BeforeSyntax(defaultConstraint, CsSpace.Location.TYPE_PARAMETERS_CONSTRAINT_PREFIX, p);
p.Append("default");
return defaultConstraint;
}


private class CSharpJavaPrinter(CSharpPrinter<TState> _parent) : JavaPrinter<TState>
{
Expand Down Expand Up @@ -523,8 +571,10 @@ private class CSharpJavaPrinter(CSharpPrinter<TState> _parent) : JavaPrinter<TSt
return base.Visit(tree, p);
}


public override J VisitClassDeclaration(J.ClassDeclaration classDecl, PrintOutputCapture<TState> p)
{
var csClassDeclaration = _parent.Cursor.Value as Cs.ClassDeclaration;
string kind = classDecl.Padding.DeclarationKind.KindType switch
{
J.ClassDeclaration.Kind.Type.Class => "class",
Expand Down Expand Up @@ -552,9 +602,11 @@ public override J VisitClassDeclaration(J.ClassDeclaration classDecl, PrintOutpu
VisitContainer("(", classDecl.Padding.PrimaryConstructor, JContainer.Location.RECORD_STATE_VECTOR, ",", ")",
p);
VisitLeftPadded(":", classDecl.Padding.Extends, JLeftPadded.Location.EXTENDS, p);
VisitContainer(classDecl.Padding.Extends == null ? ":" : ",",
classDecl.Padding.Implements, JContainer.Location.IMPLEMENTS, ",", null, p);
VisitContainer("permits", classDecl.Padding.Permits, JContainer.Location.PERMITS, ",", null, p);
VisitContainer(classDecl.Padding.Extends == null ? ":" : ",", classDecl.Padding.Implements, JContainer.Location.IMPLEMENTS, ",", null, p);
foreach (var typeParameterClause in csClassDeclaration?.TypeParameterConstraintClauses ?? [])
{
_parent.VisitTypeParameterConstraintClause(typeParameterClause, p);
}
Visit(classDecl.Body, p);
AfterSyntax(classDecl, p);
return classDecl;
Expand Down Expand Up @@ -584,9 +636,14 @@ public override J VisitBlock(J.Block block, PrintOutputCapture<TState> p)
{
p.Append("=>");
VisitStatements(block.Padding.Statements, JRightPadded.Location.BLOCK_STATEMENT, p);
if (block.Statements.FirstOrDefault() is not Cs.ExpressionStatement) // expression statements print their own semicolon
{
p.Append(";");
}

VisitSpace(block.End, Space.Location.BLOCK_END, p);
}
else if (block.Markers.FirstOrDefault(m => m is OmitBraces) == null)
else if (!block.Markers.OfType<OmitBraces>().Any())
{
p.Append('{');
VisitStatements(block.Padding.Statements, JRightPadded.Location.BLOCK_STATEMENT, p);
Expand All @@ -595,7 +652,15 @@ public override J VisitBlock(J.Block block, PrintOutputCapture<TState> p)
}
else
{
VisitStatements(block.Padding.Statements, JRightPadded.Location.BLOCK_STATEMENT, p);
if (block.Padding.Statements.Any())
{
VisitStatements(block.Padding.Statements, JRightPadded.Location.BLOCK_STATEMENT, p);
}
else
{
p.Append(";");
}

VisitSpace(block.End, Space.Location.BLOCK_END, p);
}

Expand Down Expand Up @@ -835,25 +900,22 @@ public override void PrintStatementTerminator(Statement s, PrintOutputCapture<TS
{
p.Append(';');
}
else if (s is Cs.PropertyDeclaration propertyDeclaration &&
(propertyDeclaration.Initializer != null ||
propertyDeclaration.Accessors.Markers.FirstOrDefault(m => m is SingleExpressionBlock) != null))
{
p.Append(';');
}
else if (s is J.ClassDeclaration classDeclaration &&
classDeclaration.Body.Markers.FirstOrDefault(m => m is OmitBraces) != null)
{
// class declaration without braces always requires a semicolon
p.Append(';');
}
else if (s is Cs.AnnotatedStatement annotatedStatement &&
annotatedStatement.Statement is J.ClassDeclaration innerClassDeclaration &&
innerClassDeclaration.Body.Markers.FirstOrDefault(m => m is OmitBraces) != null)
else if (s is Cs.PropertyDeclaration propertyDeclaration && propertyDeclaration.Initializer != null)
{
// class declaration without braces always requires a semicolon
p.Append(';');
}
// else if (s is J.ClassDeclaration classDeclaration && classDeclaration.Body.Markers.FirstOrDefault(m => m is OmitBraces) != null)
// {
// // class declaration without braces always requires a semicolon
// p.Append(';');
// }
// else if (s is Cs.AnnotatedStatement annotatedStatement &&
// annotatedStatement.Statement is J.ClassDeclaration innerClassDeclaration &&
// innerClassDeclaration.Body.Markers.FirstOrDefault(m => m is OmitBraces) != null)
// {
// // class declaration without braces always requires a semicolon
// p.Append(';');
// }
else
{
base.PrintStatementTerminator(s, p);
Expand Down
83 changes: 83 additions & 0 deletions Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,89 @@ public override bool IsAcceptable(SourceFile sourceFile, P p)
return lambda;
}

public virtual J? VisitClassDeclaration(Cs.ClassDeclaration classDeclaration, P p)
{
classDeclaration = classDeclaration.WithPrefix(VisitSpace(classDeclaration.Prefix, CsSpace.Location.CLASS_DECLARATION_PREFIX, p)!);
var tempStatement = (Statement) VisitStatement(classDeclaration, p);
if (tempStatement is not Cs.ClassDeclaration)
{
return tempStatement;
}
classDeclaration = (Cs.ClassDeclaration) tempStatement;
classDeclaration = classDeclaration.WithMarkers(VisitMarkers(classDeclaration.Markers, p));
classDeclaration = classDeclaration.WithClassDeclarationCore(VisitAndCast<J.ClassDeclaration>(classDeclaration.ClassDeclarationCore, p)!);
classDeclaration = classDeclaration.Padding.WithTypeParameterConstraintClauses(VisitContainer(classDeclaration.Padding.TypeParameterConstraintClauses, CsContainer.Location.CLASS_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES, p)!);
return classDeclaration;
}

public virtual J? VisitMethodDeclaration(Cs.MethodDeclaration methodDeclaration, P p)
{
methodDeclaration = methodDeclaration.WithPrefix(VisitSpace(methodDeclaration.Prefix, CsSpace.Location.METHOD_DECLARATION_PREFIX, p)!);
var tempStatement = (Statement) VisitStatement(methodDeclaration, p);
if (tempStatement is not Cs.MethodDeclaration)
{
return tempStatement;
}
methodDeclaration = (Cs.MethodDeclaration) tempStatement;
methodDeclaration = methodDeclaration.WithMarkers(VisitMarkers(methodDeclaration.Markers, p));
methodDeclaration = methodDeclaration.WithMethodDeclarationCore(VisitAndCast<J.MethodDeclaration>(methodDeclaration.MethodDeclarationCore, p)!);
methodDeclaration = methodDeclaration.Padding.WithTypeParameterConstraintClauses(VisitContainer(methodDeclaration.Padding.TypeParameterConstraintClauses, CsContainer.Location.METHOD_DECLARATION_TYPE_PARAMETER_CONSTRAINT_CLAUSES, p)!);
return methodDeclaration;
}

public virtual J? VisitTypeParameterConstraintClause(Cs.TypeParameterConstraintClause typeParameterConstraintClause, P p)
{
typeParameterConstraintClause = typeParameterConstraintClause.WithPrefix(VisitSpace(typeParameterConstraintClause.Prefix, CsSpace.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_PREFIX, p)!);
typeParameterConstraintClause = typeParameterConstraintClause.WithMarkers(VisitMarkers(typeParameterConstraintClause.Markers, p));
typeParameterConstraintClause = typeParameterConstraintClause.Padding.WithTypeParameter(VisitRightPadded(typeParameterConstraintClause.Padding.TypeParameter, CsRightPadded.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER, p)!);
typeParameterConstraintClause = typeParameterConstraintClause.Padding.WithTypeParameterConstraints(VisitContainer(typeParameterConstraintClause.Padding.TypeParameterConstraints, CsContainer.Location.TYPE_PARAMETER_CONSTRAINT_CLAUSE_TYPE_PARAMETER_CONSTRAINTS, p)!);
return typeParameterConstraintClause;
}

public virtual J? VisitTypeConstraint(Cs.TypeConstraint typeConstraint, P p)
{
typeConstraint = typeConstraint.WithPrefix(VisitSpace(typeConstraint.Prefix, CsSpace.Location.TYPE_CONSTRAINT_PREFIX, p)!);
typeConstraint = typeConstraint.WithMarkers(VisitMarkers(typeConstraint.Markers, p));
typeConstraint = typeConstraint.WithTypeExpression(VisitAndCast<TypeTree>(typeConstraint.TypeExpression, p)!);
return typeConstraint;
}

public virtual J? VisitAllowsConstraintClause(Cs.AllowsConstraintClause allowsConstraintClause, P p)
{
allowsConstraintClause = allowsConstraintClause.WithPrefix(VisitSpace(allowsConstraintClause.Prefix, CsSpace.Location.ALLOWS_CONSTRAINT_CLAUSE_PREFIX, p)!);
allowsConstraintClause = allowsConstraintClause.WithMarkers(VisitMarkers(allowsConstraintClause.Markers, p));
allowsConstraintClause = allowsConstraintClause.Padding.WithExpressions(VisitContainer(allowsConstraintClause.Padding.Expressions, CsContainer.Location.ALLOWS_CONSTRAINT_CLAUSE_EXPRESSIONS, p)!);
return allowsConstraintClause;
}

public virtual J? VisitRefStructConstraint(Cs.RefStructConstraint refStructConstraint, P p)
{
refStructConstraint = refStructConstraint.WithPrefix(VisitSpace(refStructConstraint.Prefix, CsSpace.Location.REF_STRUCT_CONSTRAINT_PREFIX, p)!);
refStructConstraint = refStructConstraint.WithMarkers(VisitMarkers(refStructConstraint.Markers, p));
return refStructConstraint;
}

public virtual J? VisitClassOrStructConstraint(Cs.ClassOrStructConstraint classOrStructConstraint, P p)
{
classOrStructConstraint = classOrStructConstraint.WithPrefix(VisitSpace(classOrStructConstraint.Prefix, CsSpace.Location.CLASS_OR_STRUCT_CONSTRAINT_PREFIX, p)!);
classOrStructConstraint = classOrStructConstraint.WithMarkers(VisitMarkers(classOrStructConstraint.Markers, p));
return classOrStructConstraint;
}

public virtual J? VisitConstructorConstraint(Cs.ConstructorConstraint constructorConstraint, P p)
{
constructorConstraint = constructorConstraint.WithPrefix(VisitSpace(constructorConstraint.Prefix, CsSpace.Location.CONSTRUCTOR_CONSTRAINT_PREFIX, p)!);
constructorConstraint = constructorConstraint.WithMarkers(VisitMarkers(constructorConstraint.Markers, p));
return constructorConstraint;
}

public virtual J? VisitDefaultConstraint(Cs.DefaultConstraint defaultConstraint, P p)
{
defaultConstraint = defaultConstraint.WithPrefix(VisitSpace(defaultConstraint.Prefix, CsSpace.Location.DEFAULT_CONSTRAINT_PREFIX, p)!);
defaultConstraint = defaultConstraint.WithMarkers(VisitMarkers(defaultConstraint.Markers, p));
return defaultConstraint;
}

protected virtual JContainer<J2>? VisitContainer<J2>(JContainer<J2>? container, CsContainer.Location loc, P p) where J2 : J
{
if (container == null) {
Expand Down
Loading

0 comments on commit 7c2f370

Please sign in to comment.