1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Generic;
7
using ICSharpCode.NRefactory;
8
using ICSharpCode.NRefactory.Ast;
9
using ICSharpCode.NRefactory.PrettyPrinter;
10
using ICSharpCode.SharpDevelop;
11
using ICSharpCode.SharpDevelop.Dom;
12
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
13
using ICSharpCode.SharpDevelop.Project;
14
using ICSharpCode.SharpDevelop.Project.Converter;
16
namespace CSharpBinding
18
public class VBNetToCSharpConverter : NRefactoryLanguageConverter
20
public override string TargetLanguageName {
22
return CSharpProjectBinding.LanguageName;
26
protected override IProject CreateProject(string targetProjectDirectory, IProject sourceProject)
28
CSharpProject project = (CSharpProject)base.CreateProject(targetProjectDirectory, sourceProject);
29
IProjectItemListProvider provider = (IProjectItemListProvider)project;
30
provider.AddProjectItem(new ReferenceProjectItem(project, "Microsoft.VisualBasic"));
32
FileProjectItem fileItem = new FileProjectItem(project, ItemType.Compile, "MyNamespaceSupportForCSharp.cs");
33
provider.AddProjectItem(fileItem);
35
File.WriteAllText(fileItem.FileName, CSharpMyNamespaceBuilder.BuildMyNamespaceCode((CompilableProject)sourceProject));
36
} catch (Exception ex) {
37
conversionLog.AppendLine(ex.ToString());
43
protected override void ConvertFile(FileProjectItem sourceItem, FileProjectItem targetItem)
45
ConvertFile(sourceItem, targetItem, ".vb", ".cs", SupportedLanguage.VBNet, new CSharpOutputVisitor());
48
protected override void CopyProperties(IProject sourceProject, IProject targetProject)
50
base.CopyProperties(sourceProject, targetProject);
52
CSharpProject project = (CSharpProject)targetProject;
54
// 1591 = missing XML comment - the VB compiler does not have this warning
55
// we disable it by default because many VB projects have XML documentation turned on
56
// even though only few members are commented
57
// (we replace existing NoWarn entries because VB and C# error codes don't match)
58
project.SetProperty("NoWarn", "1591");
60
project.ChangeProperty("DefineConstants",
61
v => v.Replace(',', ';'));
63
project.ChangeProperty("ProjectTypeGuids",
64
v => v.Replace(ProjectTypeGuids.VBNet, ProjectTypeGuids.CSharp, StringComparison.OrdinalIgnoreCase));
67
protected override void ConvertAst(CompilationUnit compilationUnit, List<ISpecial> specials, FileProjectItem sourceItem)
69
PreprocessingDirective.VBToCSharp(specials);
70
CompilableProject project = (CompilableProject)sourceItem.Project;
71
RemoveWindowsFormsSpecificCode(compilationUnit, specials, project.OutputType == OutputType.WinExe);
73
IProjectContent pc = ParserService.GetProjectContent(sourceItem.Project) ?? ParserService.CurrentProjectContent;
74
VBNetToCSharpConvertVisitor visitor = new VBNetToCSharpConvertVisitorWithMyFormsSupport(pc, ParserService.GetParseInformation(sourceItem.FileName), sourceItem.Project.RootNamespace);
76
// set project options
77
visitor.OptionInfer = (project.GetEvaluatedProperty("OptionInfer") ?? "Off")
78
.Equals("On", StringComparison.OrdinalIgnoreCase);
79
visitor.OptionStrict = (project.GetEvaluatedProperty("OptionStrict") ?? "Off")
80
.Equals("On", StringComparison.OrdinalIgnoreCase);
82
compilationUnit.AcceptVisitor(visitor, null);
85
void RemoveWindowsFormsSpecificCode(CompilationUnit compilationUnit, List<ISpecial> specials, bool keepCode)
87
for (int i = 0; i < specials.Count; i++) {
88
PreprocessingDirective ppd = specials[i] as PreprocessingDirective;
89
if (ppd != null && ppd.Cmd == "#if") {
90
if (ppd.Arg == "_MyType = \"WindowsForms\"") {
92
for (int j = i + 1; j < specials.Count; j++) {
93
ppd = specials[j] as PreprocessingDirective;
95
if (ppd.Cmd == "#if") {
97
} else if (ppd.Cmd == "#endif") {
101
// keep code, remove only the ifdef
102
specials.RemoveAt(j);
103
specials.RemoveAt(i);
105
// remove ifdef including the code
106
compilationUnit.AcceptVisitor(new RemoveMembersInRangeVisitor(
107
DomRegion.FromLocation(specials[i].StartPosition, specials[j].EndPosition)), null);
108
specials.RemoveRange(i, j - i + 1);