@@ -861,7 +861,6 @@ private void GenerateSyntaxWrapperHelper(in GeneratorExecutionContext context, I
861861 SyntaxFactory . IdentifierName ( "Type" ) ,
862862 } ) ) ) ) ) ) ) ) ) ) ) ;
863863
864- bool first = true ;
865864 foreach ( var node in wrapperTypes . OrderBy ( node => node . Name , StringComparer . OrdinalIgnoreCase ) )
866865 {
867866 if ( node . WrapperName is null )
@@ -924,8 +923,67 @@ private void GenerateSyntaxWrapperHelper(in GeneratorExecutionContext context, I
924923 continue ;
925924 }
926925
926+ if ( node . Name == nameof ( BaseObjectCreationExpressionSyntax ) )
927+ {
928+ // Prior to C# 9, ObjectCreationExpressionSyntax was the base type for all object creation
929+ // statements. If the BaseObjectCreationExpressionSyntax type isn't found at runtime, we fall back
930+ // to using this type instead.
931+ //
932+ // var objectCreationExpressionSyntaxType = csharpCodeAnalysisAssembly.GetType(BaseObjectCreationExpressionSyntaxWrapper.WrappedTypeName)
933+ // ?? csharpCodeAnalysisAssembly.GetType(BaseObjectCreationExpressionSyntaxWrapper.FallbackWrappedTypeName);
934+ LocalDeclarationStatementSyntax localStatement =
935+ SyntaxFactory . LocalDeclarationStatement ( SyntaxFactory . VariableDeclaration (
936+ type : SyntaxFactory . IdentifierName ( "var" ) ,
937+ variables : SyntaxFactory . SingletonSeparatedList ( SyntaxFactory . VariableDeclarator (
938+ identifier : SyntaxFactory . Identifier ( "objectCreationExpressionSyntaxType" ) ,
939+ argumentList : null ,
940+ initializer : SyntaxFactory . EqualsValueClause (
941+ SyntaxFactory . BinaryExpression (
942+ SyntaxKind . CoalesceExpression ,
943+ left : SyntaxFactory . InvocationExpression (
944+ expression : SyntaxFactory . MemberAccessExpression (
945+ SyntaxKind . SimpleMemberAccessExpression ,
946+ expression : SyntaxFactory . IdentifierName ( "csharpCodeAnalysisAssembly" ) ,
947+ name : SyntaxFactory . IdentifierName ( "GetType" ) ) ,
948+ argumentList : SyntaxFactory . ArgumentList ( SyntaxFactory . SingletonSeparatedList ( SyntaxFactory . Argument (
949+ SyntaxFactory . MemberAccessExpression (
950+ SyntaxKind . SimpleMemberAccessExpression ,
951+ expression : SyntaxFactory . IdentifierName ( node . WrapperName ) ,
952+ name : SyntaxFactory . IdentifierName ( "WrappedTypeName" ) ) ) ) ) ) ,
953+ right : SyntaxFactory . InvocationExpression (
954+ expression : SyntaxFactory . MemberAccessExpression (
955+ SyntaxKind . SimpleMemberAccessExpression ,
956+ expression : SyntaxFactory . IdentifierName ( "csharpCodeAnalysisAssembly" ) ,
957+ name : SyntaxFactory . IdentifierName ( "GetType" ) ) ,
958+ argumentList : SyntaxFactory . ArgumentList ( SyntaxFactory . SingletonSeparatedList ( SyntaxFactory . Argument (
959+ SyntaxFactory . MemberAccessExpression (
960+ SyntaxKind . SimpleMemberAccessExpression ,
961+ expression : SyntaxFactory . IdentifierName ( node . WrapperName ) ,
962+ name : SyntaxFactory . IdentifierName ( "FallbackWrappedTypeName" ) ) ) ) ) ) ) ) ) ) ) ) ;
963+
964+ // This is the first line of the statements that initialize 'builder', so start it with a blank line
965+ staticCtorStatements = staticCtorStatements . Add ( localStatement . WithLeadingBlankLine ( ) ) ;
966+
967+ // builder.Add(typeof(BaseObjectCreationExpressionSyntaxWrapper), objectCreationExpressionSyntaxType);
968+ staticCtorStatements = staticCtorStatements . Add ( SyntaxFactory . ExpressionStatement (
969+ SyntaxFactory . InvocationExpression (
970+ expression : SyntaxFactory . MemberAccessExpression (
971+ SyntaxKind . SimpleMemberAccessExpression ,
972+ expression : SyntaxFactory . IdentifierName ( "builder" ) ,
973+ name : SyntaxFactory . IdentifierName ( "Add" ) ) ,
974+ argumentList : SyntaxFactory . ArgumentList (
975+ SyntaxFactory . SeparatedList (
976+ new [ ]
977+ {
978+ SyntaxFactory . Argument ( SyntaxFactory . TypeOfExpression ( SyntaxFactory . IdentifierName ( node . WrapperName ) ) ) ,
979+ SyntaxFactory . Argument ( SyntaxFactory . IdentifierName ( "objectCreationExpressionSyntaxType" ) ) ,
980+ } ) ) ) ) ) ;
981+
982+ continue ;
983+ }
984+
927985 // builder.Add(typeof(ConstantPatternSyntaxWrapper), csharpCodeAnalysisAssembly.GetType(ConstantPatternSyntaxWrapper.WrappedTypeName));
928- ExpressionStatementSyntax statement = SyntaxFactory . ExpressionStatement (
986+ staticCtorStatements = staticCtorStatements . Add ( SyntaxFactory . ExpressionStatement (
929987 SyntaxFactory . InvocationExpression (
930988 expression : SyntaxFactory . MemberAccessExpression (
931989 SyntaxKind . SimpleMemberAccessExpression ,
@@ -947,15 +1005,7 @@ private void GenerateSyntaxWrapperHelper(in GeneratorExecutionContext context, I
9471005 SyntaxKind . SimpleMemberAccessExpression ,
9481006 expression : SyntaxFactory . IdentifierName ( node . WrapperName ) ,
9491007 name : SyntaxFactory . IdentifierName ( "WrappedTypeName" ) ) ) ) ) ) ) ,
950- } ) ) ) ) ;
951-
952- if ( first )
953- {
954- statement = statement . WithLeadingBlankLine ( ) ;
955- first = false ;
956- }
957-
958- staticCtorStatements = staticCtorStatements . Add ( statement ) ;
1008+ } ) ) ) ) ) ;
9591009 }
9601010
9611011 // WrappedTypes = builder.ToImmutable();
0 commit comments