📄 compiler.vb
字号:
InternalException.Dump(xml, ex, False) xml.Close() xml = Nothing Catch 'Just do nothing. End Try#Else Compiler.Report.WriteLine("Unexpected error: " & ex.Message & vb.vbNewLine & ex.StackTrace)#End If End Sub ''' <summary> ''' Returns true if the specified MethodInfo is a valid candidate to a Main function. ''' </summary> ''' <param name="method"></param> ''' <returns></returns> ''' <remarks></remarks> Function IsMainMethod(ByVal method As Reflection.MethodInfo) As Boolean 'Only static methods If method.IsStatic = False Then Return False 'Only methods called 'Main' If vbnc.Helper.CompareName(method.Name, "Main") = False Then Return False 'Only methods with no return type or Integer return type If method.ReturnType IsNot Nothing AndAlso method.ReturnType IsNot Compiler.TypeCache.System_Void AndAlso method.ReturnType IsNot Compiler.TypeCache.System_Int32 Then Return False 'Only methods with no parameters or methods with one String() parameter Dim params() As ParameterInfo params = method.GetParameters If params.Length = 0 Then Return True If params.Length > 1 Then Return False If params(0).ParameterType Is Compiler.TypeCache.System_String_Array AndAlso params(0).IsOptional = False AndAlso params(0).IsOut = False Then Return True Return False End Function ReadOnly Property Logo() As String Get Dim result As New System.Text.StringBuilder Dim FileVersion As Diagnostics.FileVersionInfo = Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location)#If DEBUG Then result.AppendLine(FileVersion.ProductName & " version " & FileVersion.FileVersion & " (last write time: " & IO.File.GetLastWriteTime(FileVersion.FileName).ToString("dd/MM/yyyy HH:mm:ss") & ")")#Else result.AppendLine(FileVersion.ProductName & " version " & FileVersion.FileVersion)#End If result.AppendLine(FileVersion.LegalCopyright) result.AppendLine() Return result.ToString End Get End Property ReadOnly Property Help() As String Get Dim result As New System.Text.StringBuilder result.AppendLine("") result.AppendLine(" Visual Basic .NET Compiler Options ") result.AppendLine("") result.AppendLine(" >>> Output file options >>>") result.AppendLine("/out:<filename> Sets the file name of the output executable or library.") result.AppendLine("/target:exe Create a console application (this is the default). (Short form: /t)") result.AppendLine("/target:winexe Create a Windows application.") result.AppendLine("/target:library Create a library assembly.") result.AppendLine("/target:module Create a module that can be added to an assembly.") result.AppendLine("") result.AppendLine(" >>> Input files options >>>") result.AppendLine("/addmodule:<filename> Reference metadata from the specified module.") result.AppendLine("/recurse:<wildcard> Include all files in the current directory and subdirectories according to the wildcard specifications.") result.AppendLine("/reference:<file_list> Reference metadata from the specified assembly. (Short form: /r)") result.AppendLine("") result.AppendLine(" >>> Resources options >>>") result.AppendLine("/linkresource:<resinfo>Links the specified file as an external assembly resource. resinfo:<file>[,<name>[,public|private]] (Short form: /linkres)") result.AppendLine("/resource:<resinfo> Adds the specified file as an embedded assembly resource. resinfo:<file>[,<name>[,public|private]] (Short form: /res)") result.AppendLine("/win32icon:<file> Specifies a Win32 icon file (.ico) for the default Win32 resources.") result.AppendLine("/win32resource:<file> Specifies a Win32 resource file (.res).") result.AppendLine("") result.AppendLine(" >>> Debug and code generation options >>>") result.AppendLine("/optimize[+|-] Enable optimizations.") result.AppendLine("/removeintchecks[+|-] Remove integer checks. Default off.") result.AppendLine("/debug[+|-] Emit debugging information.") result.AppendLine("/debug:full Emit full debugging information (default).") result.AppendLine("/debug:pdbonly Emit PDB file only.") result.AppendLine("") result.AppendLine(" >>> Errors and warnings options >>>") result.AppendLine("/nowarn Disable warnings.") result.AppendLine("/warnaserror[+|-] Treat warnings as errors.") result.AppendLine("") result.AppendLine(" >>> Language options >>>") result.AppendLine("/define:<list> Declare global conditional compilation symbol(s). list:name=value,... (Also: /d)") result.AppendLine("/imports:<list> Declare global Imports for namespaces in referenced metadata files. list:namespace,...") result.AppendLine("/optionexplicit[+|-] Require explicit declaration of variables.") result.AppendLine("/optionstrict[+|-] Enforce strict language semantics.") result.AppendLine("/rootnamespace:<string>sets the root Namespace for all type declarations.") result.AppendLine("/optioncompare:binary Do binary string comparisons. (Default)") result.AppendLine("/optioncompare:text Do text string comparisons.") result.AppendLine("") result.AppendLine(" >>> Various options >>> ") result.AppendLine("/help Show this help message. (Also: /?)") result.AppendLine("/nologo Do not show the compiler copyright banner.") result.AppendLine("/quiet Specifies a quiet mode - only errors will be shown.") result.AppendLine("/verbose Show verbose messages.") result.AppendLine("/noconfig Disable the automatic inclusion of the vbnc.rsp response file.") result.AppendLine("") result.AppendLine(" >>> Advanced options >>>") result.AppendLine("/baseaddress:<number> Specifies the base address of the library or module (in hex).") result.AppendLine("/bugreport:<file> Create bug report file.") result.AppendLine("/codepage:<number> Specifies the codepage to use when opening source files.") result.AppendLine("/delaysign[+|-] Specifies whether to delay-sign the assembly using only the public portion of the strong name key.") result.AppendLine("/keycontainer:<string> Specifies a strong name key container. *Not supported yet.*") result.AppendLine("/keyfile:<file> Specifies a strong name key file. *Not supported yet.*") result.AppendLine("/libpath:<path_list> Lists the directories to search for metadata references. (Delimited by semi-colons.") result.AppendLine("/main:<class> Specifies the entry method of the assembly. Can be a Main sub or function, or a class that inherits from System.Windows.Forms.Form. (Also: /m)") result.AppendLine("/netcf Specifies the .NET Compact Framework as the target. *Not supported*.") result.AppendLine("/sdkpath:<path> where the .Net Framework (mscorlib.dll) is located.") result.AppendLine("/utf8output[+|-] Emit the output from the compiler in UTF8 encoding. *Not supported yet*")#If DEBUG Then result.AppendLine("/dump Dump the various outputs. Only avaliable in debug builds.")#End If Return result.ToString End Get End Property ''' <summary> ''' Writes the logo of the compiler to the console. ''' </summary> ''' <remarks></remarks> Sub ShowLogo() Compiler.Report.WriteLine(Logo) End Sub ''' <summary> ''' Writes the help of the compier to the console. ''' </summary> ''' <remarks></remarks> Sub ShowHelp() ShowLogo() Compiler.Report.WriteLine(Help) End Sub Private Function AddResources() As Boolean Dim result As Boolean = True For Each r As Resource In CommandLine.Resources Dim resourceDescription As String = "" Dim resourceFile As String = IO.Path.GetFileName(r.Filename) Dim resourceName As String = IO.Path.GetFileName(r.Filename) Dim attrib As System.Reflection.ResourceAttributes If r.Public Then attrib = ResourceAttributes.Public Else attrib = ResourceAttributes.Private End If Dim reader As System.Resources.IResourceReader Select Case IO.Path.GetExtension(r.Filename).ToLowerInvariant Case ".resx" reader = Nothing 'New System.Resources.ResXResourceReader(r.Filename) Case ".resources" reader = New System.Resources.ResourceReader(r.Filename) Case Else reader = Nothing End Select 'Report.WriteLine("Defining resource, FileName=" & r.Filename & ", Identifier=" & r.Identifier & ", reader is nothing=" & (reader Is Nothing).ToString()) If reader IsNot Nothing Then Dim writer As System.Resources.IResourceWriter = ModuleBuilder.DefineResource(resourceName, resourceDescription, attrib) For Each resource As System.Collections.DictionaryEntry In reader 'Report.WriteLine(">" & resource.Key.ToString & "=" & resource.Value.ToString) writer.AddResource(resource.Key.ToString, resource.Value) Next reader.Dispose() Else 'Report.WriteLine(">Writing ManifestResource") ModuleBuilder.DefineManifestResource(resourceName, New IO.FileStream(r.Filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read), attrib) End If Next Return result End Function ''' <summary> ''' Sets the entry point / Main function of the assembly ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Private Function SetMain() As Boolean Dim result As Boolean = True If False AndAlso vbnc.Helper.IsOnMono AndAlso CommandLine.Files.Count > 100 Then Report.WriteLine("Skipped setting Main method") Return True End If Try If CommandLine.Target = vbnc.CommandLine.Targets.Library Then Return True If CommandLine.Target = vbnc.CommandLine.Targets.Module Then Return True 'Find the main function Dim lstMethods As New Generic.List(Of MethodInfo) Dim mainClass As TypeDeclaration = Nothing result = FindMainClass(mainClass) AndAlso result result = FindMainMethod(mainClass, lstMethods) AndAlso result If result = False Then Return result If lstMethods.Count = 0 AndAlso CommandLine.Target = vbnc.CommandLine.Targets.Winexe AndAlso mainClass IsNot Nothing Then 'In this case we need to create our own main method Dim mainBuilder As MethodBuilder Dim formConstructor As ConstructorDeclaration Dim ilGen As ILGenerator formConstructor = mainClass.DefaultInstanceConstructor If formConstructor IsNot Nothing Then mainBuilder = mainClass.TypeBuilder.DefineMethod("Main", MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.HideBySig, Nothing, Type.EmptyTypes) ilGen = mainBuilder.GetILGenerator() ilGen.Emit(OpCodes.Newobj, formConstructor.ConstructorBuilder) ilGen.Emit(OpCodes.Call, TypeCache.System_Windows_Forms_Application__Run) ilGen.Emit(OpCodes.Ret) lstMethods.Add(mainBuilder) End If End If 'Set the entry point of the assembly If lstMethods.Count > 1 Then Report.ShowMessage(Messages.VBNC30738, theAss.Name) Return False ElseIf lstMethods.Count = 0 Then Report.ShowMessage(Messages.VBNC30420, theAss.Name) Return False Else Dim entryMethod As MethodBuilder Dim entryMethodDescriptor As MethodDescriptor entryMethodDescriptor = TryCast(lstMethods(0), MethodDescriptor) If entryMethodDescriptor IsNot Nothing Then entryMethod = entryMethodDescriptor.Declaration.MethodBuilder Else entryMethod = DirectCast(lstMethods(0), MethodBuilder) End If entryMethod.SetCustomAttribute(TypeCache.System_STAThreadAttribute__ctor, New Byte() {}) AssemblyBuilder.SetEntryPoint(entryMethod) End If Catch ex As Exception vbnc.Helper.NotImplementedYet(ex.Message) Throw End Try Return result End Function Function FindMainClass(ByRef Result As TypeDeclaration) As Boolean 'Dim mainClasses As ArrayList Dim mainClass As TypeDeclaration If CommandLine.Main = "" Then Result = Nothing Return True End If mainClass = theAss.FindType(CommandLine.Main) If mainClass Is Nothing Then Report.ShowMessage(Messages.VBNC90013, CommandLine.Main, "0") Result = Nothing Return False End If 'Result = DirectCast(mainClasses(0), TypeDescriptor) Result = mainClass Return True End Function Function FindMainMethod(ByVal MainClass As TypeDeclaration, ByVal Result As Generic.List(Of MethodInfo)) As Boolean Dim tps() As TypeDeclaration Dim methods As Reflection.MethodInfo() If MainClass Is Nothing Then tps = theAss.Types Else tps = New TypeDeclaration() {MainClass} End If Result.Clear() For Each t As TypeDeclaration In tps methods = t.TypeDescriptor.GetMethods() For Each m As Reflection.MethodInfo In methods If IsMainMethod(m) Then Result.Add(m) Next Next Return True End Function ''' <summary> ''' Returns the directory where the system assemblies are installed ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Private Function GetSystemDir() As String Dim assemblies() As Reflection.Assembly = AppDomain.CurrentDomain.GetAssemblies For Each a As Reflection.Assembly In assemblies Dim codebase As String = a.Location If codebase.EndsWith("corlib.dll") Then Return codebase.Substring(0, codebase.LastIndexOf(System.IO.Path.DirectorySeparatorChar)) End If Next Throw New InternalException("Cannot compute the system directory.") Return "" End Function '#Region " IDisposable Support " ' Private disposed As Boolean ' ' IDisposable ' Private Overloads Sub Dispose(ByVal disposing As Boolean) ' If Not Me.disposed Then ' If disposing Then ' '#If DEBUG Then ' ' Report.Flush() ' '#End If ' End If ' End If ' Me.disposed = True ' End Sub ' ' This code added by Visual Basic to correctly implement the disposable pattern. ' Public Overloads Sub Dispose() Implements IDisposable.Dispose ' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. ' Dispose(True) ' GC.SuppressFinalize(Me) ' End Sub ' Protected Overrides Sub Finalize() ' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. ' Dispose(False) ' MyBase.Finalize() ' End Sub '#End RegionEnd Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -