NamedTypeSymbol.vb 62.0 KB
Newer Older
1
' Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6 7

Imports System.Collections.Generic
Imports System.Collections.Immutable
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Threading
T
Tomas Matousek 已提交
8
Imports Microsoft.CodeAnalysis.PooledObjects
P
Pilchie 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Imports TypeKind = Microsoft.CodeAnalysis.TypeKind

Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols

    ''' <summary>
    ''' Represents a type other than an array, a type parameter.
    ''' </summary>
    Friend MustInherit Class NamedTypeSymbol
        Inherits TypeSymbol
        Implements INamedTypeSymbol

        ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        ' Changes to the public interface of this class should remain synchronized with the C# version.
        ' Do not make any changes to the public interface without making the corresponding change
        ' to the C# version.
        ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        ''' <summary>
        ''' Returns the arity of this type, or the number of type parameters it takes.
        ''' A non-generic type has zero arity.
        ''' </summary>
        Public MustOverride ReadOnly Property Arity As Integer

        ''' <summary>
        ''' Returns the type parameters that this type has. If this is a non-generic type,
        ''' returns an empty ImmutableArray.  
        ''' </summary>
        Public MustOverride ReadOnly Property TypeParameters As ImmutableArray(Of TypeParameterSymbol)

41
        ''' <summary>
42 43 44
        ''' Returns custom modifiers for the type argument that has been substituted for the type parameter. 
        ''' The modifiers correspond to the type argument at the same ordinal within the <see cref="TypeArgumentsNoUseSiteDiagnostics"/>
        ''' array.
45
        ''' </summary>
46
        Public MustOverride Function GetTypeArgumentCustomModifiers(ordinal As Integer) As ImmutableArray(Of CustomModifier)
47

48 49 50
        Friend Function GetEmptyTypeArgumentCustomModifiers(ordinal As Integer) As ImmutableArray(Of CustomModifier)
            If ordinal < 0 OrElse ordinal >= Me.Arity Then
                Throw New IndexOutOfRangeException()
51 52
            End If

53
            Return ImmutableArray(Of CustomModifier).Empty
54 55 56 57
        End Function

        Friend MustOverride ReadOnly Property HasTypeArgumentsCustomModifiers As Boolean

58 59 60 61 62
        ''' <summary>
        ''' Returns the type arguments that have been substituted for the type parameters. 
        ''' If nothing has been substituted for a give type parameters,
        ''' then the type parameter itself is consider the type argument.
        ''' </summary>
P
Pilchie 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
        Friend MustOverride ReadOnly Property TypeArgumentsNoUseSiteDiagnostics As ImmutableArray(Of TypeSymbol)

        Friend Function TypeArgumentsWithDefinitionUseSiteDiagnostics(<[In], Out> ByRef useSiteDiagnostics As HashSet(Of DiagnosticInfo)) As ImmutableArray(Of TypeSymbol)
            Dim result = TypeArgumentsNoUseSiteDiagnostics

            For Each typeArgument In result
                typeArgument.OriginalDefinition.AddUseSiteDiagnostics(useSiteDiagnostics)
            Next

            Return result
        End Function

        Friend Function TypeArgumentWithDefinitionUseSiteDiagnostics(index As Integer, <[In], Out> ByRef useSiteDiagnostics As HashSet(Of DiagnosticInfo)) As TypeSymbol
            Dim result = TypeArgumentsNoUseSiteDiagnostics(index)
            result.OriginalDefinition.AddUseSiteDiagnostics(useSiteDiagnostics)
            Return result
        End Function

        ''' <summary>
        ''' Returns the type symbol that this type was constructed from. This type symbol
        ''' has the same containing type, but has type arguments that are the same
        ''' as the type parameters (although its containing type might not).
        ''' </summary>
        Public MustOverride ReadOnly Property ConstructedFrom As NamedTypeSymbol

        ''' <summary>
        ''' For enum types, gets the underlying type. Returns null on all other
        ''' kinds of types.
        ''' </summary>
        Public Overridable ReadOnly Property EnumUnderlyingType As NamedTypeSymbol
            Get
                Return Nothing
            End Get
        End Property

        Public Overrides ReadOnly Property ContainingType As NamedTypeSymbol
            Get
                Return TryCast(Me.ContainingSymbol, NamedTypeSymbol)
            End Get
        End Property

        ''' <summary>
        ''' For implicitly declared delegate types returns the EventSymbol that caused this
        ''' delegate type to be generated.
        ''' For all other types returns null.
108 109
        ''' Note, the set of possible associated symbols might be expanded in the future to 
        ''' reflect changes in the languages.
P
Pilchie 已提交
110
        ''' </summary>
111
        Public Overridable ReadOnly Property AssociatedSymbol As Symbol
P
Pilchie 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            Get
                Return Nothing
            End Get
        End Property

        ''' <summary>
        ''' Returns True for one of the types from a set of Structure types if
        ''' that set represents a cycle. This property is intended for flow
        ''' analysis only since it is only implemented for source types,
        ''' and only returns True for one of the types within a cycle, not all.
        ''' </summary>
        Friend Overridable ReadOnly Property KnownCircularStruct As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Is this a NoPia local type explicitly declared in source, i.e.
        ''' top level type with a TypeIdentifier attribute on it?
        ''' </summary>
        Friend Overridable ReadOnly Property IsExplicitDefinitionOfNoPiaLocalType As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Returns true and a string from the first GuidAttribute on the type, 
        ''' the string might be null or an invalid guid representation. False, 
        ''' if there is no GuidAttribute with string argument.
        ''' </summary>
        Friend Overridable Function GetGuidString(ByRef guidString As String) As Boolean
            Return GetGuidStringDefaultImplementation(guidString)
        End Function

        ' Named types have the arity suffix added to the metadata name.
        Public Overrides ReadOnly Property MetadataName As String
            Get
                ' CLR generally allows names with dots, however some APIs like IMetaDataImport
                ' can only return full type names combined with namespaces. 
                ' see: http://msdn.microsoft.com/en-us/library/ms230143.aspx (IMetaDataImport::GetTypeDefProps)
                ' When working with such APIs, names with dots become ambiguous since metadata 
                ' consumer cannot figure where namespace ends and actual type name starts.
                ' Therefore it is a good practice to avoid type names with dots.
                Debug.Assert(Me.IsErrorType OrElse Not (TypeOf Me Is SourceNamedTypeSymbol) OrElse Not Name.Contains("."), "type name contains dots: " + Name)

159
                Return If(MangleName, MetadataHelpers.ComposeAritySuffixedMetadataName(Name, Arity), Name)
P
Pilchie 已提交
160 161 162
            End Get
        End Property

163
        ''' <summary>
C
Charles Stoner 已提交
164
        ''' True if the type itself Is excluded from code coverage instrumentation.
165 166 167 168 169 170 171 172
        ''' True for source types marked with <see cref="AttributeDescription.ExcludeFromCodeCoverageAttribute"/>.
        ''' </summary>
        Friend Overridable ReadOnly Property IsDirectlyExcludedFromCodeCoverage As Boolean
            Get
                Return False
            End Get
        End Property

P
Pilchie 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        ''' <summary>
        ''' Should the name returned by Name property be mangled with [`arity] suffix in order to get metadata name.
        ''' Must return False for a type with Arity == 0.
        ''' </summary>
        ''' <remarks>
        ''' Default implementation to force consideration of appropriate implementation for each new subclass
        ''' </remarks>
        Friend MustOverride ReadOnly Property MangleName As Boolean

        ''' <summary>
        '''  True if this symbol has a special name (metadata flag SpecialName is set).
        ''' </summary>
        Friend MustOverride ReadOnly Property HasSpecialName As Boolean

        ''' <summary>
        '''  True if this type is considered serializable (metadata flag Serializable is set).
        ''' </summary>
190
        Public MustOverride ReadOnly Property IsSerializable As Boolean Implements INamedTypeSymbol.IsSerializable
P
Pilchie 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

        ''' <summary>
        ''' Type layout information (ClassLayout metadata and layout kind flags).
        ''' </summary>
        Friend MustOverride ReadOnly Property Layout As TypeLayout

        ''' <summary>
        ''' The default charset used for type marshalling. 
        ''' Can be changed via <see cref="DefaultCharSetAttribute"/> applied on the containing module.
        ''' </summary>
        Protected ReadOnly Property DefaultMarshallingCharSet As CharSet
            Get
                Return If(EffectiveDefaultMarshallingCharSet, CharSet.Ansi)
            End Get
        End Property

        ''' <summary>
        ''' Marshalling charset of string data fields within the type (string formatting flags in metadata).
        ''' </summary>
        Friend MustOverride ReadOnly Property MarshallingCharSet As CharSet

        ''' <summary>
        ''' For delegate types, gets the delegate's invoke method.  Returns null on
214
        ''' all other kinds of types.  Note that it is possible to have an ill-formed 
P
Pilchie 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
        ''' delegate type imported from metadata which does not have an Invoke method.
        ''' Such a type will be classified as a delegate but its DelegateInvokeMethod
        ''' would be null.
        ''' </summary>
        Public Overridable ReadOnly Property DelegateInvokeMethod As MethodSymbol
            Get
                If TypeKind <> TypeKind.Delegate Then
                    Return Nothing
                End If

                Dim methods As ImmutableArray(Of Symbol) = GetMembers(WellKnownMemberNames.DelegateInvokeName)
                If methods.Length <> 1 Then
                    Return Nothing
                End If

                Dim method = TryCast(methods(0), MethodSymbol)

                'EDMAURER we used to also check 'method.IsOverridable' because section 13.6
                'of the CLI spec dictates that it be virtual, but real world
                'working metadata has been found that contains an Invoke method that is
                'marked as virtual but not newslot (both of those must be combined to
                'meet the definition of virtual). Rather than weaken the check
                'I've removed it, as the Dev10 C# compiler makes no check, and we don't
                'stand to gain anything by having it.
                'Return If(method IsNot Nothing AndAlso method.IsOverridable, method, Nothing)
                Return method
            End Get
        End Property

        ''' <summary>
        ''' Returns true if this type was declared as requiring a derived class;
        ''' i.e., declared with the "MustInherit" modifier. Always true for interfaces.
        ''' </summary>
        Public MustOverride ReadOnly Property IsMustInherit As Boolean

        ''' <summary>
        ''' Returns true if this type does not allow derived types; i.e., declared
        ''' with the NotInheritable modifier, or else declared as a Module, Structure,
        ''' Enum, or Delegate.
        ''' </summary>
        Public MustOverride ReadOnly Property IsNotInheritable As Boolean

        ''' <summary>
        ''' If this property returns false, it is certain that there are no extension
        ''' methods inside this type. If this property returns true, it is highly likely
        ''' (but not certain) that this type contains extension methods. This property allows
        ''' the search for extension methods to be narrowed much more quickly.
        ''' 
        ''' !!! Note that this property can mutate during lifetime of the symbol !!!
        ''' !!! from True to False, as we learn more about the type.             !!! 
        ''' </summary>
        Public MustOverride ReadOnly Property MightContainExtensionMethods As Boolean Implements INamedTypeSymbol.MightContainExtensionMethods

268 269 270 271 272
        ''' <summary>
        ''' Returns True if the type is marked by 'Microsoft.CodeAnalysis.Embedded' attribute. 
        ''' </summary>
        Friend MustOverride ReadOnly Property HasCodeAnalysisEmbeddedAttribute As Boolean

P
Pilchie 已提交
273 274 275
        ''' <summary>
        ''' Returns True if the type is marked by 'Microsoft.VisualBasic.Embedded' attribute. 
        ''' </summary>
276
        Friend MustOverride ReadOnly Property HasVisualBasicEmbeddedAttribute As Boolean
P
Pilchie 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289

        ''' <summary>
        ''' A Named type is an extensible interface if both the following are true:
        ''' (a) It is an interface type and
        ''' (b) It is either marked with 'TypeLibTypeAttribute( flags w/o TypeLibTypeFlags.FNonExtensible )' attribute OR
        '''     is marked with 'InterfaceTypeAttribute( flags with ComInterfaceType.InterfaceIsIDispatch )' attribute OR
        '''     inherits from an extensible interface type.
        ''' Member resolution for Extensible interfaces is late bound, i.e. members are resolved at run time by looking up the identifier on the actual run-time type of the expression. 
        ''' </summary>
        Friend MustOverride ReadOnly Property IsExtensibleInterfaceNoUseSiteDiagnostics As Boolean

        ''' <summary>
        ''' This method is an entry point for the Binder to collect extension methods with the given name
C
Charles Stoner 已提交
290
        ''' declared within this named type. Overridden by RetargetingNamedTypeSymbol.
P
Pilchie 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
        ''' </summary>
        Friend Overridable Sub AppendProbableExtensionMethods(name As String, methods As ArrayBuilder(Of MethodSymbol))
            If Me.MightContainExtensionMethods Then
                For Each member As Symbol In Me.GetMembers(name)
                    If member.Kind = SymbolKind.Method Then
                        Dim method = DirectCast(member, MethodSymbol)

                        If method.MayBeReducibleExtensionMethod Then
                            methods.Add(method)
                        End If
                    End If
                Next
            End If
        End Sub

        ''' <summary>
        ''' This method is called for a type within a namespace when we are building a map of extension methods 
        ''' for the whole (compilation merged or module level) namespace.
        ''' 
        ''' The 'appendThrough' parameter allows RetargetingNamespaceSymbol to delegate majority of the work 
        ''' to the underlying named type symbols, but still add RetargetingMethodSymbols to the map.
        ''' </summary>
        Friend Overridable Sub BuildExtensionMethodsMap(
            map As Dictionary(Of String, ArrayBuilder(Of MethodSymbol)),
            appendThrough As NamespaceSymbol
        )
            If Me.MightContainExtensionMethods Then
                Debug.Assert(False, "Possibly using inefficient implementation of AppendProbableExtensionMethods(map As Dictionary(Of String, ArrayBuilder(Of MethodSymbol)))")
                appendThrough.BuildExtensionMethodsMap(map,
                                         From name As String In Me.MemberNames
                                         Select New KeyValuePair(Of String, ImmutableArray(Of Symbol))(name, Me.GetMembers(name)))
            End If
        End Sub

        Friend Overridable Sub GetExtensionMethods(
            methods As ArrayBuilder(Of MethodSymbol),
            appendThrough As NamespaceSymbol,
            Name As String
        )
            If Me.MightContainExtensionMethods Then

                Dim candidates = Me.GetSimpleNonTypeMembers(Name)

                For Each member In candidates
C
Charles Stoner 已提交
335
                    appendThrough.AddMemberIfExtension(methods, member)
P
Pilchie 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
                Next
            End If
        End Sub

        ''' <summary>
        ''' This is an entry point for the Binder. Its purpose is to add names of viable extension methods declared 
        ''' in this type to nameSet parameter.
        ''' </summary>
        Friend Overridable Overloads Sub AddExtensionMethodLookupSymbolsInfo(nameSet As LookupSymbolsInfo,
                                                                              options As LookupOptions,
                                                                              originalBinder As Binder)
            AddExtensionMethodLookupSymbolsInfo(nameSet, options, originalBinder, appendThrough:=Me)
        End Sub

        ''' <summary>
        ''' Add names of viable extension methods declared in this type to nameSet parameter.
        ''' 
        ''' The 'appendThrough' parameter allows RetargetingNamedTypeSymbol to delegate majority of the work 
        ''' to the underlying named type symbol, but still perform viability check on RetargetingMethodSymbol.
        ''' </summary>
        Friend Overridable Overloads Sub AddExtensionMethodLookupSymbolsInfo(nameSet As LookupSymbolsInfo,
                                                                              options As LookupOptions,
                                                                              originalBinder As Binder,
                                                                              appendThrough As NamedTypeSymbol)
            If Me.MightContainExtensionMethods Then
                Debug.Assert(False, "Possibly using inefficient implementation of AppendExtensionMethodNames(nameSet As HashSet(Of String), options As LookupOptions, originalBinder As Binder, appendThrough As NamespaceOrTypeSymbol)")
                appendThrough.AddExtensionMethodLookupSymbolsInfo(nameSet, options, originalBinder,
                                                             From name As String In Me.MemberNames
                                                             Select New KeyValuePair(Of String, ImmutableArray(Of Symbol))(name, Me.GetMembers(name)))
            End If
        End Sub

        ''' <summary>
        ''' Get the instance constructors for this type.
        ''' </summary>
        Public ReadOnly Property InstanceConstructors As ImmutableArray(Of MethodSymbol)
            Get
                Return GetConstructors(Of MethodSymbol)(includeInstance:=True, includeShared:=False)
            End Get
        End Property

        ''' <summary>
        ''' Get the shared constructors for this type.
        ''' </summary>
        Public ReadOnly Property SharedConstructors As ImmutableArray(Of MethodSymbol)
            Get
                Return GetConstructors(Of MethodSymbol)(includeInstance:=False, includeShared:=True)
            End Get
        End Property

        ''' <summary>
        ''' Get the instance and shared constructors for this type.
        ''' </summary>
        Public ReadOnly Property Constructors As ImmutableArray(Of MethodSymbol)
            Get
                Return GetConstructors(Of MethodSymbol)(includeInstance:=True, includeShared:=True)
            End Get
        End Property

        Private Function GetConstructors(Of TMethodSymbol As {IMethodSymbol, Class})(includeInstance As Boolean, includeShared As Boolean) As ImmutableArray(Of TMethodSymbol)
            Debug.Assert(includeInstance OrElse includeShared)

            Dim instanceCandidates As ImmutableArray(Of Symbol) = If(includeInstance, GetMembers(WellKnownMemberNames.InstanceConstructorName), ImmutableArray(Of Symbol).Empty)
            Dim sharedCandidates As ImmutableArray(Of Symbol) = If(includeShared, GetMembers(WellKnownMemberNames.StaticConstructorName), ImmutableArray(Of Symbol).Empty)

            If instanceCandidates.IsEmpty AndAlso sharedCandidates.IsEmpty Then
                Return ImmutableArray(Of TMethodSymbol).Empty
            End If

            Dim constructors As ArrayBuilder(Of TMethodSymbol) = ArrayBuilder(Of TMethodSymbol).GetInstance()
            For Each candidate In instanceCandidates
                If candidate.Kind = SymbolKind.Method Then
                    Dim method As TMethodSymbol = TryCast(candidate, TMethodSymbol)
                    Debug.Assert(method IsNot Nothing)
                    Debug.Assert(method.MethodKind = MethodKind.Constructor)
                    constructors.Add(method)
                End If
            Next
            For Each candidate In sharedCandidates
                If candidate.Kind = SymbolKind.Method Then
                    Dim method As TMethodSymbol = TryCast(candidate, TMethodSymbol)
                    Debug.Assert(method IsNot Nothing)
                    Debug.Assert(method.MethodKind = MethodKind.StaticConstructor)
                    constructors.Add(method)
                End If
            Next
            Return constructors.ToImmutableAndFree()
        End Function


        ''' <summary>
        ''' Returns true if this type is known to be a reference type. It is never the case
        ''' that IsReferenceType and IsValueType both return true. However, for an unconstrained
        ''' type parameter, IsReferenceType and IsValueType will both return false.
        ''' </summary>
        Public Overrides ReadOnly Property IsReferenceType As Boolean
            Get
                ' TODO: Is this correct for VB Module?
                Return TypeKind <> TypeKind.Enum AndAlso TypeKind <> TypeKind.Structure AndAlso
                    TypeKind <> TypeKind.Error
            End Get
        End Property

        ''' <summary>
        ''' Returns true if this type is known to be a value type. It is never the case
        ''' that IsReferenceType and IsValueType both return true. However, for an unconstrained
        ''' type parameter, IsReferenceType and IsValueType will both return false.
        ''' </summary>
        Public Overrides ReadOnly Property IsValueType As Boolean
            Get
                ' TODO: Is this correct for VB Module?
                Return TypeKind = TypeKind.Enum OrElse TypeKind = TypeKind.Structure
            End Get
        End Property

        ''' <summary>
        ''' Returns True if this types has Arity >= 1 and Construct can be called. This is primarily useful
        ''' when deal with error cases.
        ''' </summary>
        Friend MustOverride ReadOnly Property CanConstruct As Boolean

        ''' <summary>
        ''' Returns a constructed type given its type arguments.
        ''' </summary>
        Public Function Construct(ParamArray typeArguments() As TypeSymbol) As NamedTypeSymbol
            Return Construct(typeArguments.AsImmutableOrNull())
        End Function

        ''' <summary>
        ''' Returns a constructed type given its type arguments.
        ''' </summary>
        Public Function Construct(typeArguments As IEnumerable(Of TypeSymbol)) As NamedTypeSymbol
            Return Construct(typeArguments.AsImmutableOrNull())
        End Function

        ''' <summary>
        ''' Construct a new type from this type, substituting the given type arguments for the 
        ''' type parameters. This method should only be called if this named type does not have
        ''' any substitutions applied for its own type arguments with exception of alpha-rename
        ''' substitution (although it's container might have substitutions applied).
        ''' </summary>
        ''' <param name="typeArguments">A set of type arguments to be applied. Must have the same length
        ''' as the number of type parameters that this type has.</param>
        Public MustOverride Function Construct(typeArguments As ImmutableArray(Of TypeSymbol)) As NamedTypeSymbol

        ''' <summary> Checks for validity of Construct(...) on this type with these type arguments. </summary>
        Protected Sub CheckCanConstructAndTypeArguments(typeArguments As ImmutableArray(Of TypeSymbol))
            'This helper is used by Public APIs to perform validation. This exception is part of the public
            'contract of Construct()
            If Not CanConstruct OrElse Me IsNot ConstructedFrom Then
                Throw New InvalidOperationException()
            End If

            ' Check type arguments
            typeArguments.CheckTypeArguments(Me.Arity)
        End Sub

        ''' <summary>
        ''' Construct a new type from this type definition, substituting the given type arguments for the 
        ''' type parameters. This method should only be called on the OriginalDefinition. Unlike previous 
        ''' Construct method, this overload supports type parameter substitution on this type and any number
        ''' of its containing types. See comments for TypeSubstitution type for more information.
        ''' </summary>
        Friend Function Construct(substitution As TypeSubstitution) As NamedTypeSymbol
            Debug.Assert(Me.IsDefinition)
            Debug.Assert(Me.IsOrInGenericType())

            If substitution Is Nothing Then
                Return Me
            End If

            Debug.Assert(substitution.IsValidToApplyTo(Me))

            ' Validate the map for use of alpha-renamed type parameters.
            substitution.ThrowIfSubstitutingToAlphaRenamedTypeParameter()

512
            Return DirectCast(InternalSubstituteTypeParameters(substitution).AsTypeSymbolOnly(), NamedTypeSymbol)
P
Pilchie 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
        End Function

        ''' <summary>
        ''' Returns an unbound generic type of this generic named type.
        ''' </summary>
        Public Function ConstructUnboundGenericType() As NamedTypeSymbol
            Return Me.AsUnboundGenericType()
        End Function

        ''' <summary>
        ''' Returns Default property name for the type.
        ''' If there is no default property name, then Nothing is returned.
        ''' </summary>
        Friend MustOverride ReadOnly Property DefaultPropertyName As String

        ''' <summary>
        ''' If this is a generic type instantiation or a nested type of a generic type instantiation,
        ''' return TypeSubstitution for this construction. Nothing otherwise.
        ''' Returned TypeSubstitution should target OriginalDefinition of the symbol.
        ''' </summary>
        Friend MustOverride ReadOnly Property TypeSubstitution As TypeSubstitution

        ' These properties of TypeRef, NamespaceOrType, or Symbol must be overridden.

        ''' <summary>
        ''' Gets the name of this symbol.
        ''' </summary>
        Public MustOverride Overrides ReadOnly Property Name As String

        ''' <summary>
        ''' Collection of names of members declared within this type.
        ''' </summary>
        Public MustOverride ReadOnly Property MemberNames As IEnumerable(Of String)

        ''' <summary>
        ''' Returns true if the type is a Script class. 
        ''' It might be an interactive submission class or a Script class in a csx file.
        ''' </summary>
        Public Overridable ReadOnly Property IsScriptClass As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Returns true if the type is a submission class. 
        ''' </summary>
C
Charles Stoner 已提交
560
        Public ReadOnly Property IsSubmissionClass As Boolean
P
Pilchie 已提交
561
            Get
C
Charles Stoner 已提交
562
                Return TypeKind = TypeKind.Submission
P
Pilchie 已提交
563 564 565
            End Get
        End Property

C
Charles Stoner 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        Friend Function GetScriptConstructor() As SynthesizedConstructorBase
            Debug.Assert(IsScriptClass)
            Return DirectCast(InstanceConstructors.Single(), SynthesizedConstructorBase)
        End Function

        Friend Function GetScriptInitializer() As SynthesizedInteractiveInitializerMethod
            Debug.Assert(IsScriptClass)
            Return DirectCast(GetMembers(SynthesizedInteractiveInitializerMethod.InitializerName).Single(), SynthesizedInteractiveInitializerMethod)
        End Function

        Friend Function GetScriptEntryPoint() As SynthesizedEntryPointSymbol
            Debug.Assert(IsScriptClass)
            Dim name = If(TypeKind = TypeKind.Submission, SynthesizedEntryPointSymbol.FactoryName, SynthesizedEntryPointSymbol.MainName)
            Return DirectCast(GetMembers(name).Single(), SynthesizedEntryPointSymbol)
        End Function

P
Pilchie 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
        ''' <summary>
        ''' Returns true if the type is the implicit class that holds onto invalid global members (like methods or
        ''' statements in a non script file).
        ''' </summary>
        Public Overridable ReadOnly Property IsImplicitClass As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Get all the members of this symbol.
        ''' </summary>
        ''' <returns>An ImmutableArray containing all the members of this symbol. If this symbol has no members,
        ''' returns an empty ImmutableArray. Never returns Nothing.</returns>
        Public MustOverride Overrides Function GetMembers() As ImmutableArray(Of Symbol)

        ''' <summary>
        ''' Get all the members of this symbol that have a particular name.
        ''' </summary>
        ''' <returns>An ImmutableArray containing all the members of this symbol with the given name. If there are
        ''' no members with this name, returns an empty ImmutableArray. Never returns Nothing.</returns>
        Public MustOverride Overrides Function GetMembers(name As String) As ImmutableArray(Of Symbol)

        ''' <summary>
        ''' Get all the members of this symbol that are types.
        ''' </summary>
        ''' <returns>An ImmutableArray containing all the types that are members of this symbol. If this symbol has no type members,
        ''' returns an empty ImmutableArray. Never returns Nothing.</returns>
        Public MustOverride Overrides Function GetTypeMembers() As ImmutableArray(Of NamedTypeSymbol)

        ''' <summary>
        ''' Get all the members of this symbol that are types that have a particular name, and any arity.
        ''' </summary>
        ''' <returns>An ImmutableArray containing all the types that are members of this symbol with the given name. 
        ''' If this symbol has no type members with this name,
        ''' returns an empty ImmutableArray. Never returns Nothing.</returns>
        Public MustOverride Overrides Function GetTypeMembers(name As String) As ImmutableArray(Of NamedTypeSymbol)

        ''' <summary>
        ''' Get all the members of this symbol that are types that have a particular name and arity.
        ''' </summary>
        ''' <returns>An ImmutableArray containing all the types that are members of this symbol with the given name and arity.
        ''' If this symbol has no type members with this name and arity,
        ''' returns an empty ImmutableArray. Never returns Nothing.</returns>
        Public MustOverride Overrides Function GetTypeMembers(name As String, arity As Integer) As ImmutableArray(Of NamedTypeSymbol)

        ''' <summary>
        ''' Get this accessibility that was declared on this symbol. For symbols that do
        ''' not have accessibility declared on them, returns NotApplicable.
        ''' </summary>
        Public MustOverride Overrides ReadOnly Property DeclaredAccessibility As Accessibility

        ''' <summary>
        ''' Supports visitor pattern. 
        ''' </summary>
        Friend Overrides Function Accept(Of TArgument, TResult)(visitor As VisualBasicSymbolVisitor(Of TArgument, TResult), arg As TArgument) As TResult
            Return visitor.VisitNamedType(Me, arg)
        End Function

        ' Only the compiler can created NamedTypeSymbols.
        Friend Sub New()
        End Sub

        ''' <summary>
        ''' Gets the kind of this symbol.
        ''' </summary>
        Public Overrides ReadOnly Property Kind As SymbolKind ' Cannot seal this method because of the ErrorSymbol.
            Get
                Return SymbolKind.NamedType
            End Get
        End Property

        ''' <summary>
        ''' Returns a flag indicating whether this symbol is ComImport.
        ''' </summary>
        ''' <remarks>
        ''' A type can me marked as a ComImport type in source by applying the <see cref="System.Runtime.InteropServices.ComImportAttribute"/>
        ''' </remarks>
        Friend MustOverride ReadOnly Property IsComImport As Boolean

        ''' <summary>
        ''' If CoClassAttribute was applied to the type returns the type symbol for the argument. 
        ''' Type symbol may be an error type if the type was not found. Otherwise returns Nothing
        ''' </summary>
        Friend MustOverride ReadOnly Property CoClassType As TypeSymbol

        ''' <summary>
670
        ''' Returns a sequence of preprocessor symbols specified in <see cref="ConditionalAttribute"/> applied on this symbol, or null if there are none.
P
Pilchie 已提交
671 672 673 674 675 676 677 678
        ''' </summary>
        Friend MustOverride Function GetAppliedConditionalSymbols() As ImmutableArray(Of String)

        ''' <summary>
        ''' Returns a flag indicating whether this symbol has at least one applied conditional attribute.
        ''' </summary>
        ''' <remarks>
        ''' Forces binding and decoding of attributes.
C
Charles Stoner 已提交
679
        ''' NOTE: Conditional symbols on base type must be inherited by derived type, but the native VB compiler doesn't do so. We maintain compatibility.
P
Pilchie 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
        ''' </remarks>
        Friend ReadOnly Property IsConditional As Boolean
            Get
                Return Me.GetAppliedConditionalSymbols().Any()
            End Get
        End Property

        Friend Overridable ReadOnly Property AreMembersImplicitlyDeclared As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary> 
        ''' Gets the associated <see cref="AttributeUsageInfo"/> for an attribute type.
        ''' </summary>
        Friend MustOverride Function GetAttributeUsageInfo() As AttributeUsageInfo

        ''' <summary>
        ''' Declaration security information associated with this type, or null if there is none.
        ''' </summary>
        Friend MustOverride Function GetSecurityInformation() As IEnumerable(Of Microsoft.Cci.SecurityAttribute)

        ''' <summary>
        ''' True if the type has declarative security information (HasSecurity flags).
        ''' </summary>
        Friend MustOverride ReadOnly Property HasDeclarativeSecurity As Boolean

        'This represents the declared base type and base interfaces, once bound. 
709 710
        Private _lazyDeclaredBase As NamedTypeSymbol = ErrorTypeSymbol.UnknownResultType
        Private _lazyDeclaredInterfaces As ImmutableArray(Of NamedTypeSymbol) = Nothing
P
Pilchie 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737

        ''' <summary>
        ''' NamedTypeSymbol calls derived implementations of this method when declared base type
        ''' is needed for the first time.
        ''' 
        ''' basesBeingResolved are passed if there are any types already have their bases resolved
        ''' so that the derived implementation could avoid infinite recursion
        ''' </summary>
        Friend MustOverride Function MakeDeclaredBase(basesBeingResolved As ConsList(Of Symbol), diagnostics As DiagnosticBag) As NamedTypeSymbol

        ''' <summary>
        ''' NamedTypeSymbol calls derived implementations of this method when declared interfaces
        ''' are needed for the first time.
        ''' 
        ''' basesBeingResolved are passed if there are any types already have their bases resolved
        ''' so that the derived implementation could avoid infinite recursion
        ''' </summary>
        Friend MustOverride Function MakeDeclaredInterfaces(basesBeingResolved As ConsList(Of Symbol), diagnostics As DiagnosticBag) As ImmutableArray(Of NamedTypeSymbol)

        ''' <summary>
        ''' Base type as "declared".
        ''' Declared base type may contain circularities.
        ''' 
        ''' If DeclaredBase must be accessed while other DeclaredBases are being resolved, 
        ''' the bases that are being resolved must be specified here to prevent potential infinite recursion.
        ''' </summary>
        Friend Overridable Function GetDeclaredBase(basesBeingResolved As ConsList(Of Symbol)) As NamedTypeSymbol
738
            If _lazyDeclaredBase Is ErrorTypeSymbol.UnknownResultType Then
P
Pilchie 已提交
739
                Dim diagnostics = DiagnosticBag.GetInstance()
740
                AtomicStoreReferenceAndDiagnostics(_lazyDeclaredBase, MakeDeclaredBase(basesBeingResolved, diagnostics), diagnostics, ErrorTypeSymbol.UnknownResultType)
P
Pilchie 已提交
741 742 743
                diagnostics.Free()
            End If

744
            Return _lazyDeclaredBase
P
Pilchie 已提交
745 746 747 748 749 750 751 752 753 754
        End Function

        Friend Overridable Function GetSimpleNonTypeMembers(name As String) As ImmutableArray(Of Symbol)
            Return GetMembers(name)
        End Function

        Private Sub AtomicStoreReferenceAndDiagnostics(Of T As Class)(ByRef variable As T,
                                                                     value As T,
                                                                     diagBag As DiagnosticBag,
                                                                     Optional comparand As T = Nothing)
755
            Debug.Assert(value IsNot comparand)
P
Pilchie 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789

            If diagBag Is Nothing OrElse diagBag.IsEmptyWithoutResolution Then
                Interlocked.CompareExchange(variable, value, comparand)
            Else
                Dim sourceModule = TryCast(Me.ContainingModule, SourceModuleSymbol)
                If sourceModule IsNot Nothing Then
                    sourceModule.AtomicStoreReferenceAndDiagnostics(variable, value, diagBag, CompilationStage.Declare, comparand)
                End If
            End If
        End Sub

        Friend Sub AtomicStoreArrayAndDiagnostics(Of T)(ByRef variable As ImmutableArray(Of T),
                                                             value As ImmutableArray(Of T),
                                                             diagBag As DiagnosticBag)
            Debug.Assert(Not value.IsDefault)

            If diagBag Is Nothing OrElse diagBag.IsEmptyWithoutResolution Then
                ImmutableInterlocked.InterlockedCompareExchange(variable, value, Nothing)
            Else
                Dim sourceModule = TryCast(Me.ContainingModule, SourceModuleSymbol)
                If sourceModule IsNot Nothing Then
                    sourceModule.AtomicStoreArrayAndDiagnostics(variable, value, diagBag, CompilationStage.Declare)
                End If
            End If
        End Sub

        ''' <summary>
        ''' Interfaces as "declared".
        ''' Declared interfaces may contain circularities.
        ''' 
        ''' If DeclaredInterfaces must be accessed while other DeclaredInterfaces are being resolved, 
        ''' the bases that are being resolved must be specified here to prevent potential infinite recursion.
        ''' </summary>
        Friend Overridable Function GetDeclaredInterfacesNoUseSiteDiagnostics(basesBeingResolved As ConsList(Of Symbol)) As ImmutableArray(Of NamedTypeSymbol)
790
            If _lazyDeclaredInterfaces.IsDefault Then
P
Pilchie 已提交
791
                Dim diagnostics = DiagnosticBag.GetInstance()
792
                AtomicStoreArrayAndDiagnostics(_lazyDeclaredInterfaces, MakeDeclaredInterfaces(basesBeingResolved, diagnostics), diagnostics)
P
Pilchie 已提交
793 794 795
                diagnostics.Free()
            End If

796
            Return _lazyDeclaredInterfaces
P
Pilchie 已提交
797 798 799 800 801 802 803 804 805 806 807 808 809
        End Function

        Friend Function GetDeclaredInterfacesWithDefinitionUseSiteDiagnostics(basesBeingResolved As ConsList(Of Symbol), <[In], Out> ByRef useSiteDiagnostics As HashSet(Of DiagnosticInfo)) As ImmutableArray(Of NamedTypeSymbol)
            Dim result = GetDeclaredInterfacesNoUseSiteDiagnostics(basesBeingResolved)

            For Each iface In result
                iface.OriginalDefinition.AddUseSiteDiagnostics(useSiteDiagnostics)
            Next

            Return result
        End Function

        Friend Function GetDirectBaseInterfacesNoUseSiteDiagnostics(basesBeingResolved As ConsList(Of Symbol)) As ImmutableArray(Of NamedTypeSymbol)
C
ChuckStoner 已提交
810
            If Me.TypeKind = TypeKind.Interface Then
P
Pilchie 已提交
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
                If basesBeingResolved Is Nothing Then
                    Return Me.InterfacesNoUseSiteDiagnostics
                Else
                    Return GetDeclaredBaseInterfacesSafe(basesBeingResolved)
                End If
            Else
                Return ImmutableArray(Of NamedTypeSymbol).Empty
            End If
        End Function

        Friend Overridable Function GetDeclaredBaseInterfacesSafe(basesBeingResolved As ConsList(Of Symbol)) As ImmutableArray(Of NamedTypeSymbol)
            Debug.Assert(basesBeingResolved.Any)
            If basesBeingResolved.Contains(Me) Then
                Return Nothing
            End If

            Return GetDeclaredInterfacesNoUseSiteDiagnostics(If(basesBeingResolved, ConsList(Of Symbol).Empty).Prepend(Me))
        End Function

        ''' <summary>
        ''' NamedTypeSymbol calls derived implementations of this method when acyclic base type
        ''' is needed for the first time.
        ''' This method typically calls GetDeclaredBase, filters for 
        ''' illegal cycles and other conditions before returning result as acyclic.
        ''' </summary>
        Friend MustOverride Function MakeAcyclicBaseType(diagnostics As DiagnosticBag) As NamedTypeSymbol

        ''' <summary>
        ''' NamedTypeSymbol calls derived implementations of this method when acyclic base interfaces
        ''' are needed for the first time.
        ''' This method typically calls GetDeclaredInterfaces, filters for 
        ''' illegal cycles and other conditions before returning result as acyclic.
        ''' </summary>
        Friend MustOverride Function MakeAcyclicInterfaces(diagnostics As DiagnosticBag) As ImmutableArray(Of NamedTypeSymbol)

846 847
        Private _lazyBaseType As NamedTypeSymbol = ErrorTypeSymbol.UnknownResultType
        Private _lazyInterfaces As ImmutableArray(Of NamedTypeSymbol)
P
Pilchie 已提交
848 849 850 851 852 853 854

        ''' <summary>
        ''' Base type. 
        ''' Could be Nothing for Interfaces or Object.
        ''' </summary>
        Friend NotOverridable Overrides ReadOnly Property BaseTypeNoUseSiteDiagnostics As NamedTypeSymbol
            Get
855
                If Me._lazyBaseType Is ErrorTypeSymbol.UnknownResultType Then
P
Pilchie 已提交
856 857 858 859 860 861 862 863 864
                    ' force resolution of bases in containing type
                    ' to make base resolution errors more deterministic
                    If ContainingType IsNot Nothing Then
                        Dim tmp = ContainingType.BaseTypeNoUseSiteDiagnostics
                    End If

                    Dim diagnostics = DiagnosticBag.GetInstance
                    Dim acyclicBase = Me.MakeAcyclicBaseType(diagnostics)

865
                    AtomicStoreReferenceAndDiagnostics(Me._lazyBaseType, acyclicBase, diagnostics, ErrorTypeSymbol.UnknownResultType)
P
Pilchie 已提交
866 867 868
                    diagnostics.Free()
                End If

869
                Return Me._lazyBaseType
P
Pilchie 已提交
870 871 872 873 874 875 876 877
            End Get
        End Property

        ''' <summary>
        ''' Interfaces that are implemented or inherited (if current type is interface itself).
        ''' </summary>
        Friend NotOverridable Overrides ReadOnly Property InterfacesNoUseSiteDiagnostics As ImmutableArray(Of NamedTypeSymbol)
            Get
878
                If Me._lazyInterfaces.IsDefault Then
P
Pilchie 已提交
879 880 881
                    Dim diagnostics As DiagnosticBag = DiagnosticBag.GetInstance
                    Dim acyclicInterfaces As ImmutableArray(Of NamedTypeSymbol) = Me.MakeAcyclicInterfaces(diagnostics)

882
                    AtomicStoreArrayAndDiagnostics(Me._lazyInterfaces, acyclicInterfaces, diagnostics)
P
Pilchie 已提交
883 884 885
                    diagnostics.Free()
                End If

886
                Return Me._lazyInterfaces
P
Pilchie 已提交
887 888 889 890 891 892 893 894 895 896 897 898
            End Get
        End Property

        ''' <summary>
        ''' Returns declared base type or actual base type if already known
        ''' This is only used by cycle detection code so that it can observe when cycles are broken 
        ''' while not forcing actual Base to be realized.
        ''' </summary>
        Friend Function GetBestKnownBaseType() As NamedTypeSymbol

            'NOTE: we can be at race with another thread here.
            ' the worst thing that can happen though, is that error on same cycle may be reported twice
C
Charles Stoner 已提交
899
            ' if two threads analyze the same cycle at the same time but start from different ends.
P
Pilchie 已提交
900 901 902
            '
            ' For now we decided that this is something we can live with.

903
            Dim base = Me._lazyBaseType
P
Pilchie 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916
            If base IsNot ErrorTypeSymbol.UnknownResultType Then
                Return base
            End If

            Return GetDeclaredBase(Nothing)
        End Function

        ''' <summary>
        ''' Returns declared interfaces or actual Interfaces if already known
        ''' This is only used by cycle detection code so that it can observe when cycles are broken 
        ''' while not forcing actual Interfaces to be realized.
        ''' </summary>
        Friend Function GetBestKnownInterfacesNoUseSiteDiagnostics() As ImmutableArray(Of NamedTypeSymbol)
917
            Dim interfaces = Me._lazyInterfaces
P
Pilchie 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
            If Not interfaces.IsDefault Then
                Return interfaces
            End If

            Return GetDeclaredInterfacesNoUseSiteDiagnostics(Nothing)
        End Function

        ''' <summary>
        ''' True iff this type or some containing type has type parameters.
        ''' </summary>
        Public ReadOnly Property IsGenericType As Boolean Implements INamedTypeSymbol.IsGenericType
            Get
                Dim p As NamedTypeSymbol = Me
                Do While p IsNot Nothing
                    If (p.Arity <> 0) Then
                        Return True
                    End If
                    p = p.ContainingType
                Loop
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Get the original definition of this symbol. If this symbol is derived from another
        ''' symbol by (say) type substitution, this gets the original symbol, as it was defined
        ''' in source or metadata.
        ''' </summary>
        Public Overridable Shadows ReadOnly Property OriginalDefinition As NamedTypeSymbol
            Get
                ' Default implements returns Me.
                Return Me
            End Get
        End Property

        Protected NotOverridable Overrides ReadOnly Property OriginalTypeSymbolDefinition As TypeSymbol
            Get
                Return Me.OriginalDefinition
            End Get
        End Property

        ''' <summary>
        ''' Should return full emitted namespace name for a top level type if the name 
        ''' might be different in case from containing namespace symbol full name, Nothing otherwise.
        ''' </summary>
        Friend Overridable Function GetEmittedNamespaceName() As String
            Return Nothing
        End Function

        ''' <summary>
        ''' Does this type implement all the members of the given interface. Does not include members
        ''' of interfaces that iface inherits, only direct members.
        ''' </summary>
        Friend Function ImplementsAllMembersOfInterface(iface As NamedTypeSymbol) As Boolean
            Dim implementationMap = ExplicitInterfaceImplementationMap

            For Each ifaceMember In iface.GetMembersUnordered()
                If ifaceMember.RequiresImplementation() AndAlso Not implementationMap.ContainsKey(ifaceMember) Then
                    Return False
                End If
            Next

            Return True
        End Function

        Friend Overrides Function GetUseSiteErrorInfo() As DiagnosticInfo
            If Me.IsDefinition Then
                Return MyBase.GetUseSiteErrorInfo()
            End If

            ' Doing check for constructed types here in order to share implementation across
            ' constructed non-error and error type symbols.

            ' Check definition.
            Dim definitionErrorInfo As DiagnosticInfo = DeriveUseSiteErrorInfoFromType(Me.OriginalDefinition)

            If definitionErrorInfo IsNot Nothing AndAlso definitionErrorInfo.Code = ERRID.ERR_UnsupportedType1 Then
                Return definitionErrorInfo
            End If

            ' Check type arguments.
            Dim argsErrorInfo As DiagnosticInfo = DeriveUseSiteErrorInfoFromTypeArguments()

            Return MergeUseSiteErrorInfo(definitionErrorInfo, argsErrorInfo)
        End Function

        Private Function DeriveUseSiteErrorInfoFromTypeArguments() As DiagnosticInfo
            Dim argsErrorInfo As DiagnosticInfo = Nothing

            For Each arg As TypeSymbol In Me.TypeArgumentsNoUseSiteDiagnostics
                Dim errorInfo As DiagnosticInfo = DeriveUseSiteErrorInfoFromType(arg)

                If errorInfo IsNot Nothing Then
                    If errorInfo.Code = ERRID.ERR_UnsupportedType1 Then
                        Return errorInfo
                    End If

                    If argsErrorInfo Is Nothing Then
                        argsErrorInfo = errorInfo
                    End If
                End If
            Next

1021 1022 1023
            If Me.HasTypeArgumentsCustomModifiers Then
                Dim modifiersErrorInfo As DiagnosticInfo = Nothing

1024 1025
                For i As Integer = 0 To Me.Arity - 1
                    modifiersErrorInfo = MergeUseSiteErrorInfo(modifiersErrorInfo, DeriveUseSiteErrorInfoFromCustomModifiers(Me.GetTypeArgumentCustomModifiers(i)))
1026 1027 1028 1029 1030
                Next

                Return MergeUseSiteErrorInfo(argsErrorInfo, modifiersErrorInfo)
            End If

P
Pilchie 已提交
1031 1032 1033 1034 1035
            Return argsErrorInfo
        End Function

        ''' <summary>
        ''' True if this is a reference to an <em>unbound</em> generic type.  These occur only
1036
        ''' within a <c>GetType</c> expression.  A generic type is considered <em>unbound</em>
P
Pilchie 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
        ''' if all of the type argument lists in its fully qualified name are empty.
        ''' Note that the type arguments of an unbound generic type will be returned as error
        ''' types because they do not really have type arguments.  An unbound generic type
        ''' yields null for its BaseType and an empty result for its Interfaces.
        ''' </summary>
        Public Overridable ReadOnly Property IsUnboundGenericType As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Force all declaration errors to be generated.
        ''' </summary>
        Friend MustOverride Overrides Sub GenerateDeclarationErrors(cancellationToken As CancellationToken)

        ''' <summary>
        ''' Return compiler generated nested types that are created at Declare phase, but not exposed through GetMembers and the like APIs.
        ''' Should return Nothing if there are no such types.
        ''' </summary>
        Friend Overridable Function GetSynthesizedNestedTypes() As IEnumerable(Of Microsoft.Cci.INestedTypeDefinition)
            Return Nothing
        End Function

        ''' <summary>
        ''' True if the type is a Windows runtime type.
        ''' </summary>
        ''' <remarks>
        ''' A type can me marked as a Windows runtime type in source by applying the WindowsRuntimeImportAttribute.
        ''' WindowsRuntimeImportAttribute is a pseudo custom attribute defined as an internal class in System.Runtime.InteropServices.WindowsRuntime namespace.
        ''' This is needed to mark Windows runtime types which are redefined in mscorlib.dll and System.Runtime.WindowsRuntime.dll.
        ''' These two assemblies are special as they implement the CLR's support for WinRT.
        ''' </remarks>
        Friend MustOverride ReadOnly Property IsWindowsRuntimeImport As Boolean

        '''  <summary>
        ''' True if the type should have its WinRT interfaces projected onto .NET types and
        ''' have missing .NET interface members added to the type.
        ''' </summary>
        Friend MustOverride ReadOnly Property ShouldAddWinRTMembers As Boolean

        ''' <summary>
        ''' Requires less computation than <see cref="TypeSymbol.TypeKind"/>== <see cref="TypeKind.Interface"/>.
        ''' </summary>
        ''' <remarks>
        ''' Metadata types need to compute their base types in order to know their TypeKinds, And that can lead
        ''' to cycles if base types are already being computed.
        ''' </remarks>
        ''' <returns>True if this Is an interface type.</returns>
        Friend MustOverride ReadOnly Property IsInterface As Boolean

1088 1089 1090 1091 1092
        ''' <summary>
        ''' Get synthesized WithEvents overrides that aren't returned by <see cref="GetMembers"/>
        ''' </summary>
        Friend MustOverride Function GetSynthesizedWithEventsOverrides() As IEnumerable(Of PropertySymbol)

P
Pilchie 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
#Region "INamedTypeSymbol"

        Private ReadOnly Property INamedTypeSymbol_Arity As Integer Implements INamedTypeSymbol.Arity
            Get
                Return Me.Arity
            End Get
        End Property

        Private ReadOnly Property INamedTypeSymbol_ConstructedFrom As INamedTypeSymbol Implements INamedTypeSymbol.ConstructedFrom
            Get
                Return Me.ConstructedFrom
            End Get
        End Property

        Private ReadOnly Property INamedTypeSymbol_DelegateInvokeMethod As IMethodSymbol Implements INamedTypeSymbol.DelegateInvokeMethod
            Get
                Return Me.DelegateInvokeMethod
            End Get
        End Property

        Private ReadOnly Property INamedTypeSymbol_EnumUnderlyingType As INamedTypeSymbol Implements INamedTypeSymbol.EnumUnderlyingType
            Get
                Return Me.EnumUnderlyingType
            End Get
        End Property

        Private ReadOnly Property INamedTypeSymbol_MemberNames As IEnumerable(Of String) Implements INamedTypeSymbol.MemberNames
            Get
                Return Me.MemberNames
            End Get
        End Property

        Private ReadOnly Property INamedTypeSymbol_IsUnboundGenericType As Boolean Implements INamedTypeSymbol.IsUnboundGenericType
            Get
                Return Me.IsUnboundGenericType
            End Get
        End Property

        Private ReadOnly Property INamedTypeSymbol_OriginalDefinition As INamedTypeSymbol Implements INamedTypeSymbol.OriginalDefinition
            Get
                Return Me.OriginalDefinition
            End Get
        End Property

1137 1138 1139 1140
        Private Function INamedTypeSymbol_GetTypeArgumentCustomModifiers(ordinal As Integer) As ImmutableArray(Of CustomModifier) Implements INamedTypeSymbol.GetTypeArgumentCustomModifiers
            Return GetTypeArgumentCustomModifiers(ordinal)
        End Function

P
Pilchie 已提交
1141 1142 1143 1144 1145 1146
        Private ReadOnly Property INamedTypeSymbol_TypeArguments As ImmutableArray(Of ITypeSymbol) Implements INamedTypeSymbol.TypeArguments
            Get
                Return StaticCast(Of ITypeSymbol).From(Me.TypeArgumentsNoUseSiteDiagnostics)
            End Get
        End Property

1147
        Private ReadOnly Property TypeArgumentNullableAnnotations As ImmutableArray(Of NullableAnnotation) Implements INamedTypeSymbol.TypeArgumentNullableAnnotations
1148
            Get
1149
                Return Me.TypeArgumentsNoUseSiteDiagnostics.SelectAsArray(Function(t) NullableAnnotation.None)
1150 1151 1152
            End Get
        End Property

P
Pilchie 已提交
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
        Private ReadOnly Property INamedTypeSymbol_TypeParameters As ImmutableArray(Of ITypeParameterSymbol) Implements INamedTypeSymbol.TypeParameters
            Get
                Return StaticCast(Of ITypeParameterSymbol).From(Me.TypeParameters)
            End Get
        End Property

        Private ReadOnly Property INamedTypeSymbol_IsScriptClass As Boolean Implements INamedTypeSymbol.IsScriptClass
            Get
                Return Me.IsScriptClass
            End Get
        End Property

        Private ReadOnly Property INamedTypeSymbol_IsImplicitClass As Boolean Implements INamedTypeSymbol.IsImplicitClass
            Get
                ' TODO (tomat):
                Return False
            End Get
        End Property

1172
        Private Function INamedTypeSymbol_Construct(ParamArray typeArguments() As ITypeSymbol) As INamedTypeSymbol Implements INamedTypeSymbol.Construct
1173
            Return Construct(ConstructTypeArguments(typeArguments))
1174 1175 1176
        End Function

        Private Function INamedTypeSymbol_Construct(typeArguments As ImmutableArray(Of ITypeSymbol), typeArgumentNullableAnnotations As ImmutableArray(Of CodeAnalysis.NullableAnnotation)) As INamedTypeSymbol Implements INamedTypeSymbol.Construct
1177
            Return Construct(ConstructTypeArguments(typeArguments, typeArgumentNullableAnnotations))
P
Pilchie 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
        End Function

        Private Function INamedTypeSymbol_ConstructUnboundGenericType() As INamedTypeSymbol Implements INamedTypeSymbol.ConstructUnboundGenericType
            Return ConstructUnboundGenericType()
        End Function

        Private ReadOnly Property INamedTypeSymbol_InstanceConstructors As ImmutableArray(Of IMethodSymbol) Implements INamedTypeSymbol.InstanceConstructors
            Get
                Return GetConstructors(Of IMethodSymbol)(includeInstance:=True, includeShared:=False)
            End Get
        End Property

        Private ReadOnly Property INamedTypeSymbol_StaticConstructors As ImmutableArray(Of IMethodSymbol) Implements INamedTypeSymbol.StaticConstructors
            Get
                Return GetConstructors(Of IMethodSymbol)(includeInstance:=False, includeShared:=True)
            End Get
        End Property

        Private ReadOnly Property INamedTypeSymbol_Constructors As ImmutableArray(Of IMethodSymbol) Implements INamedTypeSymbol.Constructors
            Get
                Return GetConstructors(Of IMethodSymbol)(includeInstance:=True, includeShared:=True)
            End Get
        End Property

1202
        Private ReadOnly Property INamedTypeSymbol_AssociatedSymbol As ISymbol Implements INamedTypeSymbol.AssociatedSymbol
P
Pilchie 已提交
1203
            Get
1204
                Return Me.AssociatedSymbol
P
Pilchie 已提交
1205 1206 1207
            End Get
        End Property

1208 1209 1210 1211 1212 1213
        Private ReadOnly Property INamedTypeSymbol_IsComImport As Boolean Implements INamedTypeSymbol.IsComImport
            Get
                Return IsComImport
            End Get
        End Property

P
Pilchie 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
#End Region

#Region "ISymbol"

        Protected Overrides ReadOnly Property ISymbol_IsAbstract As Boolean
            Get
                Return Me.IsMustInherit
            End Get
        End Property

        Protected Overrides ReadOnly Property ISymbol_IsSealed As Boolean
            Get
                Return Me.IsNotInheritable
            End Get
        End Property

V
VSadov 已提交
1230
        Private ReadOnly Property INamedTypeSymbol_TupleElements As ImmutableArray(Of IFieldSymbol) Implements INamedTypeSymbol.TupleElements
1231
            Get
V
VSadov 已提交
1232
                Return StaticCast(Of IFieldSymbol).From(TupleElements)
1233 1234 1235
            End Get
        End Property

V
VSadov 已提交
1236
        Private ReadOnly Property INamedTypeSymbol_TupleUnderlyingType As INamedTypeSymbol Implements INamedTypeSymbol.TupleUnderlyingType
1237
            Get
V
VSadov 已提交
1238
                Return TupleUnderlyingType
1239 1240 1241
            End Get
        End Property

P
Pilchie 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
        Public Overrides Sub Accept(visitor As SymbolVisitor)
            visitor.VisitNamedType(Me)
        End Sub

        Public Overrides Function Accept(Of TResult)(visitor As SymbolVisitor(Of TResult)) As TResult
            Return visitor.VisitNamedType(Me)
        End Function

        Public Overrides Sub Accept(visitor As VisualBasicSymbolVisitor)
            visitor.VisitNamedType(Me)
        End Sub

        Public Overrides Function Accept(Of TResult)(visitor As VisualBasicSymbolVisitor(Of TResult)) As TResult
            Return visitor.VisitNamedType(Me)
        End Function

#End Region

V
VSadov 已提交
1260 1261 1262 1263 1264 1265
        ''' <summary>
        ''' Verify if the given type can be used to back a tuple type 
        ''' and return cardinality of that tuple type in <paramref name="tupleCardinality"/>. 
        ''' </summary>
        ''' <param name="tupleCardinality">If method returns true, contains cardinality of the compatible tuple type.</param>
        ''' <returns></returns>
1266
        Public NotOverridable Overrides Function IsTupleCompatible(<Out> ByRef tupleCardinality As Integer) As Boolean
V
VSadov 已提交
1267 1268 1269 1270 1271 1272
            If IsTupleType Then
                tupleCardinality = 0
                Return False
            End If

            ' Should this be optimized for perf (caching for VT<0> to VT<7>, etc.)?
1273
            If Not IsUnboundGenericType AndAlso
V
VSadov 已提交
1274
                ContainingSymbol?.Kind = SymbolKind.Namespace AndAlso
V
VSadov 已提交
1275
                ContainingNamespace?.ContainingNamespace?.IsGlobalNamespace = True AndAlso
V
VSadov 已提交
1276
                Name = TupleTypeSymbol.TupleTypeName AndAlso
1277
                ContainingNamespace.Name = MetadataHelpers.SystemString Then
V
VSadov 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291

                Dim arity = Me.Arity

                If arity > 0 AndAlso arity < TupleTypeSymbol.RestPosition Then
                    tupleCardinality = arity
                    Return True
                ElseIf arity = TupleTypeSymbol.RestPosition AndAlso Not IsDefinition Then
                    ' Skip through "Rest" extensions
                    Dim typeToCheck As TypeSymbol = Me
                    Dim levelsOfNesting As Integer = 0

                    Do
                        levelsOfNesting += 1
                        typeToCheck = DirectCast(typeToCheck, NamedTypeSymbol).TypeArgumentsNoUseSiteDiagnostics(TupleTypeSymbol.RestPosition - 1)
1292
                    Loop While TypeSymbol.Equals(typeToCheck.OriginalDefinition, Me.OriginalDefinition, TypeCompareKind.ConsiderEverything) AndAlso Not typeToCheck.IsDefinition
V
VSadov 已提交
1293 1294 1295

                    If typeToCheck.IsTupleType Then
                        Dim underlying = typeToCheck.TupleUnderlyingType
1296
                        If underlying.Arity = TupleTypeSymbol.RestPosition AndAlso Not TypeSymbol.Equals(underlying.OriginalDefinition, Me.OriginalDefinition, TypeCompareKind.ConsiderEverything) Then
V
VSadov 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
                            tupleCardinality = 0
                            Return False
                        End If

                        tupleCardinality = (TupleTypeSymbol.RestPosition - 1) * levelsOfNesting + typeToCheck.TupleElementTypes.Length
                        Return True
                    End If

                    arity = If(TryCast(typeToCheck, NamedTypeSymbol)?.Arity, 0)

                    If arity > 0 AndAlso
                        arity < TupleTypeSymbol.RestPosition AndAlso
                        typeToCheck.IsTupleCompatible(tupleCardinality) Then
                        Debug.Assert(tupleCardinality < TupleTypeSymbol.RestPosition)
                        tupleCardinality += (TupleTypeSymbol.RestPosition - 1) * levelsOfNesting
                        Return True
                    End If
                End If
            End If

            tupleCardinality = 0
            Return False
        End Function

P
Pilchie 已提交
1321
    End Class
1322
End Namespace