typecache.vb

来自「大名鼎鼎的mono是.NET平台的跨平台(支持linux」· VB 代码 · 共 1,053 行 · 第 1/5 页

VB
1,053
字号
' ' Visual Basic.Net Compiler' Copyright (C) 2004 - 2007 Rolf Bjarne Kvinge, RKvinge@novell.com' ' This library is free software; you can redistribute it and/or' modify it under the terms of the GNU Lesser General Public' License as published by the Free Software Foundation; either' version 2.1 of the License, or (at your option) any later version.' ' This library is distributed in the hope that it will be useful,' but WITHOUT ANY WARRANTY; without even the implied warranty of' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU' Lesser General Public License for more details.' ' You should have received a copy of the GNU Lesser General Public' License along with this library; if not, write to the Free Software' Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA' Public MustInherit Class TypeCacheBase    Private m_Compiler As Compiler    Sub New(ByVal Compiler As Compiler)        m_Compiler = Compiler    End Sub    ReadOnly Property Compiler() As Compiler        Get            Return m_Compiler        End Get    End Property#If DEBUG Then    'This method will generate the other partial TypeCache class.    Shared Sub Generate()        Dim path As String = "..\source\General\"        Dim file As String = path & "TypeCache.in"        Dim filename As String = IO.Path.GetFullPath(file)        Dim content As String = IO.File.ReadAllText(filename)        Dim lines As String() = content.Split(New String() {VB.vbCr, VB.vbLf, VB.vbCrLf}, StringSplitOptions.RemoveEmptyEntries)        Dim cecil As New System.Text.StringBuilder        Dim sre As New System.Text.StringBuilder        Dim all As New System.Text.StringBuilder        all.AppendLine(VB.Join(content.Substring(0, content.IndexOf("''") - 1).Split(New String() {VB.vbCr, VB.vbLf, VB.vbCrLf}, StringSplitOptions.RemoveEmptyEntries), Environment.NewLine))        sre.AppendLine("Public Partial Class TypeCache")        sre.AppendLine(Generate(lines, False))        sre.AppendLine("End Class")        all.AppendLine(sre.ToString)#If ENABLECECIL Then        cecil.AppendLine("#If ENABLECECIL Then")        cecil.AppendLine("Public Partial Class CecilTypeCache")        cecil.AppendLine(Generate(lines, True))        cecil.AppendLine("End Class")        cecil.AppendLine("#End If")        all.AppendLine(cecil.ToString)#End If        IO.File.WriteAllText(path & "TypeCache.Generated.vb", all.ToString)        IO.File.Copy(IO.Path.Combine(path, "TypeCache.vb"), IO.Path.Combine(path, "TypeCache.vb.old"), True)        Dim oldContents As String = IO.File.ReadAllText(IO.Path.Combine(path, "TypeCache.vb"))        Dim iStart As Integer = oldContents.IndexOf("'START" & " SRE") + ("'START " & "SRE").Length + 2        Dim iEnd As Integer = oldContents.IndexOf("'END SRE", iStart) - 2        oldContents = oldContents.Remove(iStart, iEnd - iStart)        oldContents = oldContents.Insert(iStart, all.ToString())        IO.File.WriteAllText(IO.Path.Combine(path, "TypeCache.vb"), oldContents)        System.Diagnostics.Debug.WriteLine("Written TypeCache.vb, saved to TypeCache.vb.old")    End Sub    Shared Function Generate(ByVal Lines As String(), ByVal Cecil As Boolean) As String        Dim variables As New System.Text.StringBuilder        Dim getters As New System.Text.StringBuilder        Dim vbtypes As New System.Text.StringBuilder        Dim vbmembers As New System.Text.StringBuilder        'Turns out using public fields instead of private fields with property getters is about 10% faster during bootstrapping.        'Quite possibly because there's quite more code to compile with the properties        Dim publicfields As Boolean = True        If publicfields Then            getters.AppendLine("    Protected Overrides Sub InitInternal ()")        End If        vbtypes.AppendLine("    Public Overrides Sub InitInternalVB()")        vbmembers.AppendLine("    Public Overrides Sub InitInternalVBMembers()")        For Each line As String In Lines            If line.StartsWith("'") Then Continue For            If line.Trim = "" Then Continue For            Dim splitted() As String = line.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)            Dim name, type, find, parameters As String            Dim param As Integer = Integer.MaxValue            Dim noparaminname As Boolean = False            Dim isVB As Boolean = False            Dim isVBMember As Boolean = False            parameters = Nothing            Select Case splitted(0)                Case "vbtype"                    name = splitted(1)                    name = name.Replace("Microsoft.VisualBasic.CompilerServices.", "MS_VB_CS_")                    name = name.Replace("Microsoft.VisualBasic.", "MS_VB_")                    name = name.Replace("""", "").Replace(".", "_").Replace("`", "").Replace("+", "_")                    If Cecil Then type = "Mono.Cecil.TypeDefinition" Else type = "System.Type"                    find = "GetVBType"                    parameters = splitted(1)                    isVB = True                Case "type"                    name = splitted(2).Replace("""", "").Replace(".", "_").Replace("`", "").Replace("+", "_")                    If Cecil Then type = "Mono.Cecil.TypeDefinition" Else type = "System.Type"                    find = "[GetType]"                    parameters = splitted(1) & ", " & splitted(2)#If ENABLECECIL Then                    If Cecil AndAlso splitted(2).IndexOf("+"c) > 0 Then                        Dim declaringtype As String = splitted(2).Substring(0, splitted(2).LastIndexOf("+"c)).Replace(".", "_").Replace("""", "")                        Dim nestedtype As String = """" & splitted(2).Substring(splitted(2).LastIndexOf("+"c) + 1)                        parameters = declaringtype & ", " & nestedtype                    End If#End If                Case "array"                    name = splitted(1).Replace("""", "").Replace(".", "_").Replace("`", "") & "_Array"                    If Cecil Then type = "Mono.Cecil.TypeReference" Else type = "System.Type"                    find = "GetArrayType"                    parameters = splitted(1)                Case "byref"                    name = splitted(1).Replace("""", "").Replace(".", "_").Replace("`", "") & "_ByRef"                    If Cecil Then type = "Mono.Cecil.TypeReference" Else type = "System.Type"                    find = "GetByRefType"                    parameters = splitted(1)                Case "method", "method2"                    name = splitted(1) & "__" & splitted(2).Replace("""", "").Replace(".", "_").Replace("`", "")                    If Cecil Then type = "Mono.Cecil.MethodDefinition" Else type = "System.Reflection.MethodInfo"                    If splitted(0) <> "method" Then noparaminname = True                    param = 3                    find = "GetMethod"                    parameters = splitted(1) & ", " & splitted(2)                    isVBMember = splitted(1).StartsWith("MS_")                Case "property"                    name = splitted(1) & "__" & splitted(2).Replace("""", "").Replace(".", "_").Replace("`", "")                    If Cecil Then type = "Mono.Cecil.PropertyDefinition" Else type = "System.Reflection.PropertyInfo"                    param = 3                    find = "GetProperty"                    parameters = splitted(1) & ", " & splitted(2)                    isVBMember = splitted(1).StartsWith("MS_")                Case "field"                    name = splitted(1) & "__" & splitted(2).Replace("""", "").Replace(".", "_").Replace("`", "")                    If Cecil Then type = "Mono.Cecil.FieldDefinition" Else type = "System.Reflection.FieldInfo"                    find = "GetField"                    parameters = splitted(1) & ", " & splitted(2)                    isVBMember = splitted(1).StartsWith("MS_")                Case "ctor"                    name = splitted(1) & "__ctor"                    If Cecil Then type = "Mono.Cecil.MethodDefinition" Else type = "System.Reflection.ConstructorInfo"                    param = 2                    find = "GetConstructor"                    parameters = splitted(1)                    isVBMember = splitted(1).StartsWith("MS_")                Case Else                    Helper.Stop()                    Throw New NotImplementedException(splitted(0))            End Select            For i As Integer = param To splitted.GetUpperBound(0)                If noparaminname = False Then                    Dim lastUScore As Integer                    Dim p As String = splitted(i)                    p = splitted(i)                    p = p.Replace("_ByRef", "")                    lastUScore = p.LastIndexOf("_"c) + 1                    name &= "_" & p.Substring(lastUScore)                End If                If parameters IsNot Nothing Then parameters &= ", "                parameters &= splitted(i)            Next            If publicfields Then                variables.AppendLine(String.Format("    Public {0} As {1}", name, type))            Else                variables.AppendLine(String.Format("    Private m_{0} As {1}", name, type))            End If            Dim text As String            If publicfields Then                text = String.Format("        {0} = {1}({2})", name, find, parameters)                If isVB Then                    vbtypes.AppendLine(text)                ElseIf isVBMember Then                    vbmembers.AppendLine(text)                Else                    getters.AppendLine(text)                End If            Else                getters.AppendLine(String.Format("    Public ReadOnly Property {0} As {1}", name, type))                getters.AppendLine(String.Format("        Get"))                getters.AppendLine(String.Format("            If m_{0} Is Nothing Then", name))                getters.AppendLine(String.Format("                m_{0} = {1}({2})", name, find, parameters))                getters.AppendLine(String.Format("            End If"))                getters.AppendLine(String.Format("            Return m_{0}", name))                getters.AppendLine(String.Format("        End Get"))                getters.AppendLine(String.Format("    End Property"))            End If

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?