1- // Licensed to the .NET Foundation under one or more agreements.
1+ // Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System ;
@@ -49,6 +49,35 @@ Compilation comp
4949 IdentifierName ( "n" ) ) ) ) )
5050 ) ;
5151
52+ StatementSyntax arrLast = IfStatement
53+ (
54+ InvocationExpression
55+ (
56+ ParseName ( "Silk.NET.Core.Contexts.DefaultNativeContext.TryCreate" ) ,
57+ ArgumentList
58+ (
59+ SeparatedList
60+ (
61+ new [ ]
62+ {
63+ Argument ( IdentifierName ( "name" ) ) ,
64+ Argument
65+ (
66+ null ,
67+ Token ( SyntaxKind . OutKeyword ) ,
68+ DeclarationExpression
69+ (
70+ IdentifierName ( "DefaultNativeContext" ) ,
71+ SingleVariableDesignation ( Identifier ( "context" ) )
72+ )
73+ )
74+ }
75+ )
76+ )
77+ ) ,
78+ ReturnStatement ( IdentifierName ( "context" ) )
79+ ) ;
80+
5281 foreach ( var ( attSymbol , attId , lib , @override ) in overrides . OrderBy ( x => x . Item2 ) )
5382 {
5483 var name = NameGenerator . Name ( $ "OVERRIDE_{ attId } ") ;
@@ -61,6 +90,17 @@ Compilation comp
6190 var matchId = ( int ) x2 . ConstructorArguments [ 0 ] . Value ! ;
6291 return matchId != attId ;
6392 } ) ) . ToArray ( ) , comp . SyntaxTrees ? . FirstOrDefault ( ) ? . IsNet5OrGreater ( ) ?? false ) ) ) ;
93+
94+ arrLast = IfStatement
95+ (
96+ BinaryExpression
97+ (
98+ SyntaxKind . EqualsExpression , IdentifierName ( "name" ) ,
99+ LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( lib ) )
100+ ) , ReturnStatement ( ObjectCreationExpression ( IdentifierName ( name ) , ArgumentList ( ) , null ) ) ,
101+ ElseClause ( arrLast )
102+ ) ;
103+
64104 last = IfStatement
65105 (
66106 BinaryExpression
@@ -85,6 +125,78 @@ Compilation comp
85125 )
86126 )
87127 . WithBody ( Block ( last ) )
128+ . WithAttributeLists (
129+ SingletonList
130+ (
131+ AttributeList
132+ (
133+ SingletonSeparatedList
134+ (
135+ Attribute
136+ (
137+ ParseName ( "System.ObsoleteAttribute" ) ,
138+ AttributeArgumentList
139+ (
140+ SingletonSeparatedList
141+ (
142+ AttributeArgument
143+ (
144+ LiteralExpression ( SyntaxKind . StringLiteralExpression ,
145+ Literal ( "This function is obsolete! Please use the string[] overload!" )
146+ )
147+ )
148+ )
149+ )
150+ )
151+ )
152+ )
153+ ) )
154+ ) ;
155+
156+ //wrap arrLast into its foreach loop
157+ //eg `foreach(string name in n)`
158+ arrLast = ForEachStatement ( PredefinedType ( Token ( SyntaxKind . StringKeyword ) ) , Identifier ( "name" ) , IdentifierName ( "n" ) , Block ( arrLast ) ) ;
159+
160+ members . Add
161+ (
162+ MethodDeclaration ( IdentifierName ( "INativeContext" ) , Identifier ( "CreateDefaultContext" ) )
163+ . WithModifiers ( TokenList ( Token ( SyntaxKind . PublicKeyword ) , Token ( SyntaxKind . StaticKeyword ) ) )
164+ . WithParameterList
165+ (
166+ ParameterList
167+ (
168+ SingletonSeparatedList
169+ ( Parameter ( Identifier ( "n" ) ) . WithType ( ArrayType ( PredefinedType ( Token ( SyntaxKind . StringKeyword ) ) , SingletonList ( ArrayRankSpecifier ( ) ) ) ) )
170+ )
171+ )
172+ . WithBody
173+ (
174+ Block
175+ (
176+ arrLast ,
177+ ThrowStatement
178+ (
179+ ObjectCreationExpression ( IdentifierName ( "System.IO.FileNotFoundException" ) )
180+ . WithArgumentList
181+ (
182+ ArgumentList
183+ (
184+ SingletonSeparatedList
185+ (
186+ Argument
187+ (
188+ LiteralExpression
189+ (
190+ SyntaxKind . StringLiteralExpression ,
191+ Literal ( "Could not load from any of the possible library names! Please make sure that the library is installed and in the right place!" )
192+ )
193+ )
194+ )
195+ )
196+ )
197+ )
198+ )
199+ )
88200 ) ;
89201 }
90202
@@ -94,7 +206,7 @@ Compilation comp
94206 foreach ( var attribute in attributes )
95207 {
96208 if ( attribute . AttributeClass is null ) continue ;
97-
209+
98210 if ( _nativeContextAttributes . TryGetValue ( attribute . AttributeClass , out var f ) )
99211 {
100212 var v = f ( attribute . ConstructorArguments ) ;
0 commit comments