44using Microsoft . CodeAnalysis . CSharp . Syntax ;
55using Semmle . Extraction . Entities ;
66using System . IO ;
7+ using System ;
78
89namespace Semmle . Extraction . CSharp . Entities
910{
@@ -124,6 +125,17 @@ Symbol.ContainingSymbol is IMethodSymbol ms &&
124125 trapFile . param_location ( this , Context . CreateLocation ( ) ) ;
125126 }
126127
128+ if ( Symbol . HasExplicitDefaultValue && Context . Defines ( Symbol ) )
129+ {
130+ var defaultValueSyntax = GetDefaultValueFromSyntax ( Symbol ) ;
131+
132+ Action defaultValueExpressionCreation = defaultValueSyntax is not null
133+ ? ( ) => Expression . Create ( Context , defaultValueSyntax . Value , this , 0 )
134+ : ( ) => Expression . CreateGenerated ( Context , Symbol , this , 0 , Location ) ;
135+
136+ Context . PopulateLater ( defaultValueExpressionCreation ) ;
137+ }
138+
127139 if ( ! IsSourceDeclaration || ! Symbol . FromSource ( ) )
128140 return ;
129141
@@ -139,36 +151,28 @@ Symbol.ContainingSymbol is IMethodSymbol ms &&
139151 TypeMention . Create ( Context , syntax . Type ! , this , type ) ;
140152 }
141153 }
154+ }
142155
143- if ( Symbol . HasExplicitDefaultValue && Context . Defines ( Symbol ) )
156+ private static EqualsValueClauseSyntax ? GetDefaultValueFromSyntax ( IParameterSymbol symbol )
157+ {
158+ // This is a slight bug in the dbscheme
159+ // We should really define param_default(param, string)
160+ // And use parameter child #0 to encode the default expression.
161+ var defaultValue = GetParameterDefaultValue ( symbol ) ;
162+ if ( defaultValue is null )
144163 {
145- // This is a slight bug in the dbscheme
146- // We should really define param_default(param, string)
147- // And use parameter child #0 to encode the default expression.
148- var defaultValue = GetParameterDefaultValue ( Symbol ) ;
149- if ( defaultValue is null )
150- {
151- // In case this parameter belongs to an accessor of an indexer, we need
152- // to get the default value from the corresponding parameter belonging
153- // to the indexer itself
154- var method = ( IMethodSymbol ) Symbol . ContainingSymbol ;
155- if ( method is not null )
156- {
157- var i = method . Parameters . IndexOf ( Symbol ) ;
158- var indexer = ( IPropertySymbol ? ) method . AssociatedSymbol ;
159- if ( indexer is not null )
160- defaultValue = GetParameterDefaultValue ( indexer . Parameters [ i ] ) ;
161- }
162- }
163-
164- if ( defaultValue is not null )
164+ // In case this parameter belongs to an accessor of an indexer, we need
165+ // to get the default value from the corresponding parameter belonging
166+ // to the indexer itself
167+ if ( symbol . ContainingSymbol is IMethodSymbol method )
165168 {
166- Context . PopulateLater ( ( ) =>
167- {
168- Expression . Create ( Context , defaultValue . Value , this , 0 ) ;
169- } ) ;
169+ var i = method . Parameters . IndexOf ( symbol ) ;
170+ if ( method . AssociatedSymbol is IPropertySymbol indexer )
171+ defaultValue = GetParameterDefaultValue ( indexer . Parameters [ i ] ) ;
170172 }
171173 }
174+
175+ return defaultValue ;
172176 }
173177
174178 public override bool IsSourceDeclaration => Symbol . IsSourceDeclaration ( ) ;
0 commit comments