PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Compilers/VisualBasic/Portable/Symbols/Retargeting/RetargetingModuleSymbol.vb

https://gitlab.com/sharadag/Roslyn
Visual Basic | 281 lines | 183 code | 49 blank | 49 comment | 0 complexity | a45b1b9fe53617ff40dc5ca039dfb4fa MD5 | raw file
  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.
  2. Imports System.Collections.Concurrent
  3. Imports System.Collections.Generic
  4. Imports System.Collections.Immutable
  5. Imports System.Collections.ObjectModel
  6. Imports System.Runtime.InteropServices
  7. Imports Microsoft.Cci
  8. Imports Microsoft.CodeAnalysis.Text
  9. Imports Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE
  10. Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
  11. Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
  12. Imports TypeKind = Microsoft.CodeAnalysis.TypeKind
  13. Imports System.Globalization
  14. Imports System.Threading
  15. Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols.Retargeting
  16. ''' <summary>
  17. ''' Represents a primary module of a <see cref="RetargetingAssemblySymbol"/>. Essentially this is a wrapper around
  18. ''' another <see cref="SourceModuleSymbol"/> that is responsible for retargeting symbols from one assembly to another.
  19. ''' It can retarget symbols for multiple assemblies at the same time.
  20. '''
  21. ''' Here is how retargeting is implemented in general:
  22. ''' - Symbols from underlying module are substituted with retargeting symbols.
  23. ''' - Symbols from referenced assemblies that can be reused as is (i.e. don't have to be retargeted) are
  24. ''' used as is.
  25. ''' - Symbols from referenced assemblies that must be retargeted are substituted with result of retargeting.
  26. ''' </summary>
  27. Friend NotInheritable Class RetargetingModuleSymbol
  28. Inherits NonMissingModuleSymbol
  29. ''' <summary>
  30. ''' Owning <see cref="RetargetingAssemblySymbol"/>.
  31. ''' </summary>
  32. Private ReadOnly _retargetingAssembly As RetargetingAssemblySymbol
  33. ''' <summary>
  34. ''' The underlying <see cref="ModuleSymbol"/>, cannot be another <see cref="RetargetingModuleSymbol"/>.
  35. ''' </summary>
  36. Private ReadOnly _underlyingModule As SourceModuleSymbol
  37. ''' <summary>
  38. ''' The map that captures information about what assembly should be retargeted
  39. ''' to what assembly. Key is the <see cref="AssemblySymbol"/> referenced by the underlying module,
  40. ''' value is the corresponding <see cref="AssemblySymbol"/> referenced by this module, and corresponding
  41. ''' retargeting map for symbols.
  42. ''' </summary>
  43. Private ReadOnly _retargetingAssemblyMap As New Dictionary(Of AssemblySymbol, DestinationData)()
  44. Private Structure DestinationData
  45. Public [To] As AssemblySymbol
  46. Public SymbolMap As ConcurrentDictionary(Of NamedTypeSymbol, NamedTypeSymbol)
  47. End Structure
  48. Friend ReadOnly RetargetingTranslator As RetargetingSymbolTranslator
  49. ''' <summary>
  50. ''' Retargeted custom attributes
  51. ''' </summary>
  52. Private _lazyCustomAttributes As ImmutableArray(Of VisualBasicAttributeData)
  53. ''' <summary>
  54. ''' Constructor.
  55. ''' </summary>
  56. ''' <param name="retargetingAssembly">
  57. ''' Owning assembly.
  58. ''' </param>
  59. ''' <param name="underlyingModule">
  60. ''' Underlying <see cref="ModuleSymbol"/>, cannot be another <see cref="RetargetingModuleSymbol"/>.
  61. ''' </param>
  62. ''' <remarks></remarks>
  63. Public Sub New(retargetingAssembly As RetargetingAssemblySymbol, underlyingModule As SourceModuleSymbol)
  64. Debug.Assert(retargetingAssembly IsNot Nothing)
  65. Debug.Assert(underlyingModule IsNot Nothing)
  66. _retargetingAssembly = retargetingAssembly
  67. _underlyingModule = underlyingModule
  68. RetargetingTranslator = New RetargetingSymbolTranslator(Me)
  69. Me._createRetargetingMethod = AddressOf CreateRetargetingMethod
  70. Me._createRetargetingNamespace = AddressOf CreateRetargetingNamespace
  71. Me._createRetargetingNamedType = AddressOf CreateRetargetingNamedType
  72. Me._createRetargetingField = AddressOf CreateRetargetingField
  73. Me._createRetargetingTypeParameter = AddressOf CreateRetargetingTypeParameter
  74. Me._createRetargetingProperty = AddressOf CreateRetargetingProperty
  75. Me._createRetargetingEvent = AddressOf CreateRetargetingEvent
  76. End Sub
  77. Friend Overrides ReadOnly Property Ordinal As Integer
  78. Get
  79. Debug.Assert(_underlyingModule.Ordinal = 0)
  80. Return 0
  81. End Get
  82. End Property
  83. Friend Overrides ReadOnly Property Machine As System.Reflection.PortableExecutable.Machine
  84. Get
  85. Return _underlyingModule.Machine
  86. End Get
  87. End Property
  88. Friend Overrides ReadOnly Property Bit32Required As Boolean
  89. Get
  90. Return _underlyingModule.Bit32Required
  91. End Get
  92. End Property
  93. ''' <summary>
  94. ''' The underlying <see cref="ModuleSymbol"/>, cannot be another <see cref="RetargetingModuleSymbol"/>.
  95. ''' </summary>
  96. Public ReadOnly Property UnderlyingModule As SourceModuleSymbol
  97. Get
  98. Return _underlyingModule
  99. End Get
  100. End Property
  101. Public Overrides ReadOnly Property ContainingSymbol As Symbol
  102. Get
  103. Return _retargetingAssembly
  104. End Get
  105. End Property
  106. Public Overrides ReadOnly Property ContainingAssembly As AssemblySymbol
  107. Get
  108. Return _retargetingAssembly
  109. End Get
  110. End Property
  111. Public Overloads Overrides Function GetAttributes() As ImmutableArray(Of VisualBasicAttributeData)
  112. Return RetargetingTranslator.GetRetargetedAttributes(_underlyingModule, _lazyCustomAttributes)
  113. End Function
  114. Public Overrides ReadOnly Property Name As String
  115. Get
  116. Return _underlyingModule.Name
  117. End Get
  118. End Property
  119. Public Overrides ReadOnly Property MetadataName As String
  120. Get
  121. Return _underlyingModule.MetadataName
  122. End Get
  123. End Property
  124. Public Overrides ReadOnly Property GlobalNamespace As NamespaceSymbol
  125. Get
  126. Return RetargetingTranslator.Retarget(_underlyingModule.GlobalNamespace)
  127. End Get
  128. End Property
  129. Public Overrides ReadOnly Property Locations As ImmutableArray(Of Location)
  130. Get
  131. Return _underlyingModule.Locations
  132. End Get
  133. End Property
  134. ''' <summary>
  135. ''' A helper method for ReferenceManager to set AssemblySymbols for assemblies
  136. ''' referenced by this module.
  137. ''' </summary>
  138. Friend Overrides Sub SetReferences(
  139. moduleReferences As ModuleReferences(Of AssemblySymbol),
  140. Optional originatingSourceAssemblyDebugOnly As SourceAssemblySymbol = Nothing)
  141. MyBase.SetReferences(moduleReferences, originatingSourceAssemblyDebugOnly)
  142. ' Build the retargeting map
  143. _retargetingAssemblyMap.Clear()
  144. Dim underlyingBoundReferences As ImmutableArray(Of AssemblySymbol) = _underlyingModule.GetReferencedAssemblySymbols()
  145. Dim referencedAssemblySymbols As ImmutableArray(Of AssemblySymbol) = moduleReferences.Symbols
  146. Dim referencedAssemblies As ImmutableArray(Of AssemblyIdentity) = moduleReferences.Identities
  147. Debug.Assert(referencedAssemblySymbols.Length = referencedAssemblies.Length)
  148. Debug.Assert(referencedAssemblySymbols.Length <= underlyingBoundReferences.Length) ' Linked references are filtered out.
  149. Dim i As Integer = -1
  150. Dim j As Integer = -1
  151. Do
  152. i += 1
  153. j += 1
  154. If i >= referencedAssemblySymbols.Length Then
  155. Exit Do
  156. End If
  157. ' Skip linked assemblies for source module
  158. While underlyingBoundReferences(j).IsLinked
  159. j += 1
  160. End While
  161. #If DEBUG Then
  162. Dim identityComparer = _underlyingModule.DeclaringCompilation.Options.AssemblyIdentityComparer
  163. Dim definitionIdentity = If(referencedAssemblySymbols(i) Is originatingSourceAssemblyDebugOnly,
  164. New AssemblyIdentity(name:=originatingSourceAssemblyDebugOnly.Name),
  165. referencedAssemblySymbols(i).Identity)
  166. Debug.Assert(identityComparer.Compare(referencedAssemblies(i), definitionIdentity) <> AssemblyIdentityComparer.ComparisonResult.NotEquivalent)
  167. Debug.Assert(identityComparer.Compare(referencedAssemblies(i), underlyingBoundReferences(j).Identity) <> AssemblyIdentityComparer.ComparisonResult.NotEquivalent)
  168. #End If
  169. If referencedAssemblySymbols(i) IsNot underlyingBoundReferences(j) Then
  170. Dim destinationData As DestinationData = Nothing
  171. If Not _retargetingAssemblyMap.TryGetValue(underlyingBoundReferences(j), destinationData) Then
  172. Dim symbolMap = New ConcurrentDictionary(Of NamedTypeSymbol, NamedTypeSymbol)()
  173. _retargetingAssemblyMap.Add(underlyingBoundReferences(j),
  174. New DestinationData With {.To = referencedAssemblySymbols(i), .SymbolMap = symbolMap})
  175. Else
  176. Debug.Assert(destinationData.To Is referencedAssemblySymbols(i))
  177. End If
  178. End If
  179. Loop
  180. #If DEBUG Then
  181. While (j < underlyingBoundReferences.Length AndAlso underlyingBoundReferences(j).IsLinked)
  182. j += 1
  183. End While
  184. Debug.Assert(j = underlyingBoundReferences.Length)
  185. #End If
  186. End Sub
  187. Friend Overrides ReadOnly Property TypeNames As ICollection(Of String)
  188. Get
  189. Return _underlyingModule.TypeNames
  190. End Get
  191. End Property
  192. Friend Overrides ReadOnly Property NamespaceNames As ICollection(Of String)
  193. Get
  194. Return _underlyingModule.NamespaceNames
  195. End Get
  196. End Property
  197. Friend Overrides ReadOnly Property MightContainExtensionMethods As Boolean
  198. Get
  199. Return _underlyingModule.MightContainExtensionMethods
  200. End Get
  201. End Property
  202. Friend Overrides ReadOnly Property HasAssemblyCompilationRelaxationsAttribute As Boolean
  203. Get
  204. Return _underlyingModule.HasAssemblyCompilationRelaxationsAttribute
  205. End Get
  206. End Property
  207. Friend Overrides ReadOnly Property HasAssemblyRuntimeCompatibilityAttribute As Boolean
  208. Get
  209. Return _underlyingModule.HasAssemblyRuntimeCompatibilityAttribute
  210. End Get
  211. End Property
  212. Friend Overrides ReadOnly Property DefaultMarshallingCharSet As CharSet?
  213. Get
  214. Return _underlyingModule.DefaultMarshallingCharSet
  215. End Get
  216. End Property
  217. ''' <remarks>
  218. ''' This is for perf, not for correctness.
  219. ''' </remarks>
  220. Friend Overrides ReadOnly Property DeclaringCompilation As VisualBasicCompilation
  221. Get
  222. Return Nothing
  223. End Get
  224. End Property
  225. Public Overrides Function GetDocumentationCommentXml(Optional preferredCulture As CultureInfo = Nothing, Optional expandIncludes As Boolean = False, Optional cancellationToken As CancellationToken = Nothing) As String
  226. Return _underlyingModule.GetDocumentationCommentXml(preferredCulture, expandIncludes, cancellationToken)
  227. End Function
  228. Public Overrides Function GetMetadata() As ModuleMetadata
  229. Return _underlyingModule.GetMetadata()
  230. End Function
  231. End Class
  232. End Namespace