PageRenderTime 69ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/LinqToCodedomProject/LinqToCodedom/Generator/VBCodeGenerator.cs

#
C# | 2572 lines | 2422 code | 147 blank | 3 comment | 481 complexity | c5f764d5deec43e770fd9b48223036ce MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. using System;
  2. using System.CodeDom;
  3. using System.CodeDom.Compiler;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Collections.Specialized;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Runtime;
  12. using System.Security;
  13. using System.Security.Permissions;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. namespace LinqToCodedom.Generator
  17. {
  18. public class VBCodeGenerator : CodeCompiler
  19. {
  20. // Fields
  21. private static readonly string[][] keywords;
  22. private const GeneratorSupport LanguageSupport = (GeneratorSupport.DeclareIndexerProperties | GeneratorSupport.GenericTypeDeclaration | GeneratorSupport.GenericTypeReference | GeneratorSupport.PartialTypes | GeneratorSupport.Resources | GeneratorSupport.Win32Resources | GeneratorSupport.ComplexExpressions | GeneratorSupport.PublicStaticMembers | GeneratorSupport.MultipleInterfaceMembers | GeneratorSupport.NestedTypes | GeneratorSupport.ChainedConstructorArguments | GeneratorSupport.ReferenceParameters | GeneratorSupport.ParameterAttributes | GeneratorSupport.AssemblyAttributes | GeneratorSupport.DeclareEvents | GeneratorSupport.DeclareInterfaces | GeneratorSupport.DeclareDelegates | GeneratorSupport.DeclareEnums | GeneratorSupport.DeclareValueTypes | GeneratorSupport.ReturnTypeAttributes | GeneratorSupport.TryCatchStatements | GeneratorSupport.StaticConstructors | GeneratorSupport.MultidimensionalArrays | GeneratorSupport.GotoStatements | GeneratorSupport.EntryPointMethod | GeneratorSupport.ArraysOfArrays);
  23. private const int MaxLineLength = 80;
  24. private static volatile Regex outputReg;
  25. private IDictionary<string, string> provOptions;
  26. private int statementDepth;
  27. // Methods
  28. static VBCodeGenerator()
  29. {
  30. string[][] strArray = new string[16][];
  31. strArray[1] = new string[] { "as", "do", "if", "in", "is", "me", "of", "on", "or", "to" };
  32. strArray[2] = new string[] { "and", "dim", "end", "for", "get", "let", "lib", "mod", "new", "not", "rem", "set", "sub", "try", "xor" };
  33. strArray[3] = new string[] {
  34. "ansi", "auto", "byte", "call", "case", "cdbl", "cdec", "char", "cint", "clng", "cobj", "csng", "cstr", "date", "each", "else",
  35. "enum", "exit", "goto", "like", "long", "loop", "next", "step", "stop", "then", "true", "wend", "when", "with"
  36. };
  37. strArray[4] = new string[] {
  38. "alias", "byref", "byval", "catch", "cbool", "cbyte", "cchar", "cdate", "class", "const", "ctype", "cuint", "culng", "endif", "erase", "error",
  39. "event", "false", "gosub", "isnot", "redim", "sbyte", "short", "throw", "ulong", "until", "using", "while"
  40. };
  41. strArray[5] = new string[] {
  42. "csbyte", "cshort", "double", "elseif", "friend", "global", "module", "mybase", "object", "option", "orelse", "public", "resume", "return", "select", "shared",
  43. "single", "static", "string", "typeof", "ushort"
  44. };
  45. strArray[6] = new string[] {
  46. "andalso", "boolean", "cushort", "decimal", "declare", "default", "finally", "gettype", "handles", "imports", "integer", "myclass", "nothing", "partial", "private", "shadows",
  47. "trycast", "unicode", "variant"
  48. };
  49. strArray[7] = new string[] { "assembly", "continue", "delegate", "function", "inherits", "operator", "optional", "preserve", "property", "readonly", "synclock", "uinteger", "widening" };
  50. strArray[8] = new string[] { "addressof", "interface", "namespace", "narrowing", "overloads", "overrides", "protected", "structure", "writeonly" };
  51. strArray[9] = new string[] { "addhandler", "directcast", "implements", "paramarray", "raiseevent", "withevents" };
  52. strArray[10] = new string[] { "mustinherit", "overridable" };
  53. strArray[11] = new string[] { "mustoverride" };
  54. strArray[12] = new string[] { "removehandler" };
  55. strArray[13] = new string[] { "class_finalize", "notinheritable", "notoverridable" };
  56. strArray[15] = new string[] { "class_initialize" };
  57. keywords = strArray;
  58. }
  59. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  60. internal VBCodeGenerator()
  61. {
  62. }
  63. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  64. internal VBCodeGenerator(IDictionary<string, string> providerOptions)
  65. {
  66. this.provOptions = providerOptions;
  67. }
  68. protected bool AllowLateBound(CodeCompileUnit e)
  69. {
  70. object obj2 = e.UserData["AllowLateBound"];
  71. if ((obj2 != null) && (obj2 is bool))
  72. {
  73. return (bool)obj2;
  74. }
  75. return true;
  76. }
  77. private static void AppendEscapedChar(StringBuilder b, char value)
  78. {
  79. b.Append("&Global.Microsoft.VisualBasic.ChrW(");
  80. b.Append(((int)value).ToString(CultureInfo.InvariantCulture));
  81. b.Append(")");
  82. }
  83. protected override string CmdArgsFromParameters(CompilerParameters options)
  84. {
  85. string str3;
  86. using (StringEnumerator enumerator = options.ReferencedAssemblies.GetEnumerator())
  87. {
  88. while (enumerator.MoveNext())
  89. {
  90. if (string.IsNullOrEmpty(enumerator.Current))
  91. {
  92. throw new ArgumentException(SR.GetString("NullOrEmpty_Value_in_Property", new object[] { "ReferencedAssemblies" }), "options");
  93. }
  94. }
  95. }
  96. StringBuilder builder = new StringBuilder(128);
  97. if (options.GenerateExecutable)
  98. {
  99. builder.Append("/t:exe ");
  100. if ((options.MainClass != null) && (options.MainClass.Length > 0))
  101. {
  102. builder.Append("/main:");
  103. builder.Append(options.MainClass);
  104. builder.Append(" ");
  105. }
  106. }
  107. else
  108. {
  109. builder.Append("/t:library ");
  110. }
  111. builder.Append("/utf8output ");
  112. string coreAssemblyFileName = options.CoreAssemblyFileName;
  113. if (string.IsNullOrWhiteSpace(options.CoreAssemblyFileName) && CodeDomProvider.TryGetProbableCoreAssemblyFilePath(options, out str3))
  114. {
  115. coreAssemblyFileName = str3;
  116. }
  117. if (!string.IsNullOrWhiteSpace(coreAssemblyFileName))
  118. {
  119. string path = coreAssemblyFileName.Trim();
  120. string directoryName = Path.GetDirectoryName(path);
  121. builder.Append("/nostdlib ");
  122. builder.Append("/sdkpath:\"").Append(directoryName).Append("\" ");
  123. builder.Append("/R:\"").Append(path).Append("\" ");
  124. }
  125. foreach (string str6 in options.ReferencedAssemblies)
  126. {
  127. string fileName = Path.GetFileName(str6);
  128. if ((string.Compare(fileName, "Microsoft.VisualBasic.dll", StringComparison.OrdinalIgnoreCase) != 0) && (string.Compare(fileName, "mscorlib.dll", StringComparison.OrdinalIgnoreCase) != 0))
  129. {
  130. builder.Append("/R:");
  131. builder.Append("\"");
  132. builder.Append(str6);
  133. builder.Append("\"");
  134. builder.Append(" ");
  135. }
  136. }
  137. builder.Append("/out:");
  138. builder.Append("\"");
  139. builder.Append(options.OutputAssembly);
  140. builder.Append("\"");
  141. builder.Append(" ");
  142. if (options.IncludeDebugInformation)
  143. {
  144. builder.Append("/D:DEBUG=1 ");
  145. builder.Append("/debug+ ");
  146. }
  147. else
  148. {
  149. builder.Append("/debug- ");
  150. }
  151. if (options.Win32Resource != null)
  152. {
  153. builder.Append("/win32resource:\"" + options.Win32Resource + "\" ");
  154. }
  155. foreach (string str8 in options.EmbeddedResources)
  156. {
  157. builder.Append("/res:\"");
  158. builder.Append(str8);
  159. builder.Append("\" ");
  160. }
  161. foreach (string str9 in options.LinkedResources)
  162. {
  163. builder.Append("/linkres:\"");
  164. builder.Append(str9);
  165. builder.Append("\" ");
  166. }
  167. if (options.TreatWarningsAsErrors)
  168. {
  169. builder.Append("/warnaserror+ ");
  170. }
  171. if (options.CompilerOptions != null)
  172. {
  173. builder.Append(options.CompilerOptions + " ");
  174. }
  175. return builder.ToString();
  176. }
  177. protected override void ContinueOnNewLine(string st)
  178. {
  179. base.Output.Write(st);
  180. base.Output.WriteLine(" _");
  181. }
  182. protected override string CreateEscapedIdentifier(string name)
  183. {
  184. if (IsKeyword(name))
  185. {
  186. return ("[" + name + "]");
  187. }
  188. return name;
  189. }
  190. protected override string CreateValidIdentifier(string name)
  191. {
  192. if (IsKeyword(name))
  193. {
  194. return ("_" + name);
  195. }
  196. return name;
  197. }
  198. private void EnsureInDoubleQuotes(ref bool fInDoubleQuotes, StringBuilder b)
  199. {
  200. if (!fInDoubleQuotes)
  201. {
  202. b.Append("&\"");
  203. fInDoubleQuotes = true;
  204. }
  205. }
  206. private void EnsureNotInDoubleQuotes(ref bool fInDoubleQuotes, StringBuilder b)
  207. {
  208. if (fInDoubleQuotes)
  209. {
  210. b.Append("\"");
  211. fInDoubleQuotes = false;
  212. }
  213. }
  214. protected override CompilerResults FromFileBatch(CompilerParameters options, string[] fileNames)
  215. {
  216. if (options == null)
  217. {
  218. throw new ArgumentNullException("options");
  219. }
  220. if (fileNames == null)
  221. {
  222. throw new ArgumentNullException("fileNames");
  223. }
  224. new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
  225. string outputFile = null;
  226. int nativeReturnValue = 0;
  227. CompilerResults results = new CompilerResults(options.TempFiles);
  228. new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Assert();
  229. try
  230. {
  231. results.Evidence = options.Evidence;
  232. }
  233. finally
  234. {
  235. CodeAccessPermission.RevertAssert();
  236. }
  237. bool flag = false;
  238. string fileExtension = options.GenerateExecutable ? "exe" : "dll";
  239. string str3 = '.' + fileExtension;
  240. if ((options.OutputAssembly == null) || (options.OutputAssembly.Length == 0))
  241. {
  242. options.OutputAssembly = results.TempFiles.AddExtension(fileExtension, !options.GenerateInMemory);
  243. new FileStream(options.OutputAssembly, FileMode.Create, FileAccess.ReadWrite).Close();
  244. flag = true;
  245. }
  246. string outputAssembly = options.OutputAssembly;
  247. if (!Path.GetExtension(outputAssembly).Equals(str3, StringComparison.OrdinalIgnoreCase))
  248. {
  249. outputAssembly = outputAssembly + str3;
  250. }
  251. string str5 = "pdb";
  252. if ((options.CompilerOptions != null) && (options.CompilerOptions.IndexOf("/debug:pdbonly", StringComparison.OrdinalIgnoreCase) != -1))
  253. {
  254. results.TempFiles.AddExtension(str5, true);
  255. }
  256. else
  257. {
  258. results.TempFiles.AddExtension(str5);
  259. }
  260. string cmdArgs = this.CmdArgsFromParameters(options) + " " + CodeCompiler.JoinStringArray(fileNames, " ");
  261. string responseFileCmdArgs = this.GetResponseFileCmdArgs(options, cmdArgs);
  262. string trueArgs = null;
  263. if (responseFileCmdArgs != null)
  264. {
  265. trueArgs = cmdArgs;
  266. cmdArgs = responseFileCmdArgs;
  267. }
  268. base.Compile(options, RedistVersionInfo.GetCompilerPath(this.provOptions, this.CompilerName), this.CompilerName, cmdArgs, ref outputFile, ref nativeReturnValue, trueArgs);
  269. results.NativeCompilerReturnValue = nativeReturnValue;
  270. if ((nativeReturnValue != 0) || (options.WarningLevel > 0))
  271. {
  272. byte[] bytes = ReadAllBytes(outputFile, FileShare.ReadWrite);
  273. foreach (string str10 in Regex.Split(Encoding.UTF8.GetString(bytes), @"\r\n"))
  274. {
  275. results.Output.Add(str10);
  276. this.ProcessCompilerOutputLine(results, str10);
  277. }
  278. if ((nativeReturnValue != 0) && flag)
  279. {
  280. File.Delete(outputAssembly);
  281. }
  282. }
  283. if (!results.Errors.HasErrors && options.GenerateInMemory)
  284. {
  285. FileStream stream = new FileStream(outputAssembly, FileMode.Open, FileAccess.Read, FileShare.Read);
  286. try
  287. {
  288. int length = (int)stream.Length;
  289. byte[] buffer = new byte[length];
  290. stream.Read(buffer, 0, length);
  291. new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Assert();
  292. try
  293. {
  294. results.CompiledAssembly = Assembly.Load(buffer, null, options.Evidence);
  295. }
  296. finally
  297. {
  298. CodeAccessPermission.RevertAssert();
  299. }
  300. return results;
  301. }
  302. finally
  303. {
  304. stream.Close();
  305. }
  306. }
  307. results.PathToAssembly = outputAssembly;
  308. return results;
  309. }
  310. protected override void GenerateArgumentReferenceExpression(CodeArgumentReferenceExpression e)
  311. {
  312. this.OutputIdentifier(e.ParameterName);
  313. }
  314. protected override void GenerateArrayCreateExpression(CodeArrayCreateExpression e)
  315. {
  316. base.Output.Write("New ");
  317. CodeExpressionCollection initializers = e.Initializers;
  318. if (initializers.Count > 0)
  319. {
  320. string typeOutput = this.GetTypeOutput(e.CreateType);
  321. base.Output.Write(typeOutput);
  322. if (typeOutput.IndexOf('(') == -1)
  323. {
  324. base.Output.Write("()");
  325. }
  326. base.Output.Write(" {");
  327. base.Indent++;
  328. this.OutputExpressionList(initializers);
  329. base.Indent--;
  330. base.Output.Write("}");
  331. }
  332. else
  333. {
  334. string str2 = this.GetTypeOutput(e.CreateType);
  335. int index = str2.IndexOf('(');
  336. if (index == -1)
  337. {
  338. base.Output.Write(str2);
  339. base.Output.Write('(');
  340. }
  341. else
  342. {
  343. base.Output.Write(str2.Substring(0, index + 1));
  344. }
  345. if (e.SizeExpression != null)
  346. {
  347. base.Output.Write("(");
  348. base.GenerateExpression(e.SizeExpression);
  349. base.Output.Write(") - 1");
  350. }
  351. else
  352. {
  353. base.Output.Write((int)(e.Size - 1));
  354. }
  355. if (index == -1)
  356. {
  357. base.Output.Write(')');
  358. }
  359. else
  360. {
  361. base.Output.Write(str2.Substring(index + 1));
  362. }
  363. base.Output.Write(" {}");
  364. }
  365. }
  366. protected override void GenerateArrayIndexerExpression(CodeArrayIndexerExpression e)
  367. {
  368. base.GenerateExpression(e.TargetObject);
  369. base.Output.Write("(");
  370. bool flag = true;
  371. foreach (CodeExpression expression in e.Indices)
  372. {
  373. if (flag)
  374. {
  375. flag = false;
  376. }
  377. else
  378. {
  379. base.Output.Write(", ");
  380. }
  381. base.GenerateExpression(expression);
  382. }
  383. base.Output.Write(")");
  384. }
  385. protected override void GenerateAssignStatement(CodeAssignStatement e)
  386. {
  387. base.GenerateExpression(e.Left);
  388. base.Output.Write(" = ");
  389. base.GenerateExpression(e.Right);
  390. base.Output.WriteLine("");
  391. }
  392. protected override void GenerateAttachEventStatement(CodeAttachEventStatement e)
  393. {
  394. base.Output.Write("AddHandler ");
  395. this.GenerateFormalEventReferenceExpression(e.Event);
  396. base.Output.Write(", ");
  397. base.GenerateExpression(e.Listener);
  398. base.Output.WriteLine("");
  399. }
  400. protected override void GenerateAttributeDeclarationsEnd(CodeAttributeDeclarationCollection attributes)
  401. {
  402. base.Output.Write(">");
  403. }
  404. protected override void GenerateAttributeDeclarationsStart(CodeAttributeDeclarationCollection attributes)
  405. {
  406. base.Output.Write("<");
  407. }
  408. protected override void GenerateBaseReferenceExpression(CodeBaseReferenceExpression e)
  409. {
  410. base.Output.Write("MyBase");
  411. }
  412. protected override void GenerateBinaryOperatorExpression(CodeBinaryOperatorExpression e)
  413. {
  414. if (e.Operator != CodeBinaryOperatorType.IdentityInequality)
  415. {
  416. base.GenerateBinaryOperatorExpression(e);
  417. }
  418. else if ((e.Right is CodePrimitiveExpression) && (((CodePrimitiveExpression)e.Right).Value == null))
  419. {
  420. this.GenerateNotIsNullExpression(e.Left);
  421. }
  422. else if ((e.Left is CodePrimitiveExpression) && (((CodePrimitiveExpression)e.Left).Value == null))
  423. {
  424. this.GenerateNotIsNullExpression(e.Right);
  425. }
  426. else
  427. {
  428. base.GenerateBinaryOperatorExpression(e);
  429. }
  430. }
  431. protected override void GenerateCastExpression(CodeCastExpression e)
  432. {
  433. base.Output.Write("CType(");
  434. base.GenerateExpression(e.Expression);
  435. base.Output.Write(",");
  436. this.OutputType(e.TargetType);
  437. this.OutputArrayPostfix(e.TargetType);
  438. base.Output.Write(")");
  439. }
  440. private void GenerateChecksumPragma(CodeChecksumPragma checksumPragma)
  441. {
  442. base.Output.Write("#ExternalChecksum(\"");
  443. base.Output.Write(checksumPragma.FileName);
  444. base.Output.Write("\",\"");
  445. base.Output.Write(checksumPragma.ChecksumAlgorithmId.ToString("B", CultureInfo.InvariantCulture));
  446. base.Output.Write("\",\"");
  447. if (checksumPragma.ChecksumData != null)
  448. {
  449. foreach (byte num in checksumPragma.ChecksumData)
  450. {
  451. base.Output.Write(num.ToString("X2", CultureInfo.InvariantCulture));
  452. }
  453. }
  454. base.Output.WriteLine("\")");
  455. }
  456. private void GenerateCodeRegionDirective(CodeRegionDirective regionDirective)
  457. {
  458. if (!this.IsGeneratingStatements())
  459. {
  460. if (regionDirective.RegionMode == CodeRegionMode.Start)
  461. {
  462. base.Output.Write("#Region \"");
  463. base.Output.Write(regionDirective.RegionText);
  464. base.Output.WriteLine("\"");
  465. }
  466. else if (regionDirective.RegionMode == CodeRegionMode.End)
  467. {
  468. base.Output.WriteLine("#End Region");
  469. }
  470. }
  471. }
  472. protected override void GenerateComment(CodeComment e)
  473. {
  474. string str = e.DocComment ? "'''" : "'";
  475. base.Output.Write(str);
  476. string text = e.Text;
  477. for (int i = 0; i < text.Length; i++)
  478. {
  479. base.Output.Write(text[i]);
  480. if (text[i] == '\r')
  481. {
  482. if ((i < (text.Length - 1)) && (text[i + 1] == '\n'))
  483. {
  484. base.Output.Write('\n');
  485. i++;
  486. }
  487. ((IndentedTextWriter)base.Output).InternalOutputTabs();
  488. base.Output.Write(str);
  489. }
  490. else if (text[i] == '\n')
  491. {
  492. ((IndentedTextWriter)base.Output).InternalOutputTabs();
  493. base.Output.Write(str);
  494. }
  495. else if (((text[i] == '\u2028') || (text[i] == '\u2029')) || (text[i] == '\x0085'))
  496. {
  497. base.Output.Write(str);
  498. }
  499. }
  500. base.Output.WriteLine();
  501. }
  502. protected override void GenerateCommentStatements(CodeCommentStatementCollection e)
  503. {
  504. foreach (CodeCommentStatement statement in e)
  505. {
  506. if (!this.IsDocComment(statement))
  507. {
  508. this.GenerateCommentStatement(statement);
  509. }
  510. }
  511. foreach (CodeCommentStatement statement2 in e)
  512. {
  513. if (this.IsDocComment(statement2))
  514. {
  515. this.GenerateCommentStatement(statement2);
  516. }
  517. }
  518. }
  519. protected override void GenerateCompileUnit(CodeCompileUnit e)
  520. {
  521. this.GenerateCompileUnitStart(e);
  522. SortedList list = new SortedList(StringComparer.OrdinalIgnoreCase);
  523. foreach (CodeNamespace namespace2 in e.Namespaces)
  524. {
  525. namespace2.UserData["GenerateImports"] = false;
  526. foreach (CodeNamespaceImport import in namespace2.Imports)
  527. {
  528. if (!list.Contains(import.Namespace))
  529. {
  530. list.Add(import.Namespace, import.Namespace);
  531. }
  532. }
  533. }
  534. foreach (string str in list.Keys)
  535. {
  536. base.Output.Write("Imports ");
  537. this.OutputIdentifier(str);
  538. base.Output.WriteLine("");
  539. }
  540. if (e.AssemblyCustomAttributes.Count > 0)
  541. {
  542. this.OutputAttributes(e.AssemblyCustomAttributes, false, "Assembly: ", true);
  543. }
  544. base.GenerateNamespaces(e);
  545. this.GenerateCompileUnitEnd(e);
  546. }
  547. protected override void GenerateCompileUnitStart(CodeCompileUnit e)
  548. {
  549. base.GenerateCompileUnitStart(e);
  550. base.Output.WriteLine("'------------------------------------------------------------------------------");
  551. base.Output.Write("' <");
  552. base.Output.WriteLine(SR.GetString("AutoGen_Comment_Line1"));
  553. base.Output.Write("' ");
  554. base.Output.WriteLine(SR.GetString("AutoGen_Comment_Line2"));
  555. base.Output.Write("' ");
  556. base.Output.Write(SR.GetString("AutoGen_Comment_Line3"));
  557. base.Output.WriteLine(Environment.Version.ToString());
  558. base.Output.WriteLine("'");
  559. base.Output.Write("' ");
  560. base.Output.WriteLine(SR.GetString("AutoGen_Comment_Line4"));
  561. base.Output.Write("' ");
  562. base.Output.WriteLine(SR.GetString("AutoGen_Comment_Line5"));
  563. base.Output.Write("' </");
  564. base.Output.WriteLine(SR.GetString("AutoGen_Comment_Line1"));
  565. base.Output.WriteLine("'------------------------------------------------------------------------------");
  566. base.Output.WriteLine("");
  567. if (this.AllowLateBound(e))
  568. {
  569. base.Output.WriteLine("Option Strict Off");
  570. }
  571. else
  572. {
  573. base.Output.WriteLine("Option Strict On");
  574. }
  575. if (!this.RequireVariableDeclaration(e))
  576. {
  577. base.Output.WriteLine("Option Explicit Off");
  578. }
  579. else
  580. {
  581. base.Output.WriteLine("Option Explicit On");
  582. }
  583. base.Output.WriteLine();
  584. }
  585. protected override void GenerateConditionStatement(CodeConditionStatement e)
  586. {
  587. base.Output.Write("If ");
  588. base.GenerateExpression(e.Condition);
  589. base.Output.WriteLine(" Then");
  590. base.Indent++;
  591. this.GenerateVBStatements(e.TrueStatements);
  592. base.Indent--;
  593. if (e.FalseStatements.Count > 0)
  594. {
  595. base.Output.Write("Else");
  596. base.Output.WriteLine("");
  597. base.Indent++;
  598. this.GenerateVBStatements(e.FalseStatements);
  599. base.Indent--;
  600. }
  601. base.Output.WriteLine("End If");
  602. }
  603. protected override void GenerateConstructor(CodeConstructor e, CodeTypeDeclaration c)
  604. {
  605. if (base.IsCurrentClass || base.IsCurrentStruct)
  606. {
  607. if (e.CustomAttributes.Count > 0)
  608. {
  609. this.OutputAttributes(e.CustomAttributes, false);
  610. }
  611. this.OutputMemberAccessModifier(e.Attributes);
  612. base.Output.Write("Sub New(");
  613. this.OutputParameters(e.Parameters);
  614. base.Output.WriteLine(")");
  615. base.Indent++;
  616. CodeExpressionCollection baseConstructorArgs = e.BaseConstructorArgs;
  617. CodeExpressionCollection chainedConstructorArgs = e.ChainedConstructorArgs;
  618. if (chainedConstructorArgs.Count > 0)
  619. {
  620. base.Output.Write("Me.New(");
  621. this.OutputExpressionList(chainedConstructorArgs);
  622. base.Output.Write(")");
  623. base.Output.WriteLine("");
  624. }
  625. else if (baseConstructorArgs.Count > 0)
  626. {
  627. base.Output.Write("MyBase.New(");
  628. this.OutputExpressionList(baseConstructorArgs);
  629. base.Output.Write(")");
  630. base.Output.WriteLine("");
  631. }
  632. else if (base.IsCurrentClass)
  633. {
  634. base.Output.WriteLine("MyBase.New");
  635. }
  636. this.GenerateVBStatements(e.Statements);
  637. base.Indent--;
  638. base.Output.WriteLine("End Sub");
  639. }
  640. }
  641. protected override void GenerateDecimalValue(decimal d)
  642. {
  643. base.Output.Write(d.ToString(CultureInfo.InvariantCulture));
  644. base.Output.Write('D');
  645. }
  646. protected override void GenerateDefaultValueExpression(CodeDefaultValueExpression e)
  647. {
  648. base.Output.Write("CType(Nothing, " + this.GetTypeOutput(e.Type) + ")");
  649. }
  650. protected override void GenerateDelegateCreateExpression(CodeDelegateCreateExpression e)
  651. {
  652. base.Output.Write("AddressOf ");
  653. base.GenerateExpression(e.TargetObject);
  654. base.Output.Write(".");
  655. this.OutputIdentifier(e.MethodName);
  656. }
  657. protected override void GenerateDelegateInvokeExpression(CodeDelegateInvokeExpression e)
  658. {
  659. if (e.TargetObject != null)
  660. {
  661. if (e.TargetObject is CodeEventReferenceExpression)
  662. {
  663. base.Output.Write("RaiseEvent ");
  664. this.GenerateFormalEventReferenceExpression((CodeEventReferenceExpression)e.TargetObject);
  665. }
  666. else
  667. {
  668. base.GenerateExpression(e.TargetObject);
  669. }
  670. }
  671. if (e.Parameters.Count > 0)
  672. {
  673. base.Output.Write("(");
  674. this.OutputExpressionList(e.Parameters);
  675. base.Output.Write(")");
  676. }
  677. }
  678. protected override void GenerateDirectionExpression(CodeDirectionExpression e)
  679. {
  680. base.GenerateExpression(e.Expression);
  681. }
  682. protected override void GenerateDirectives(CodeDirectiveCollection directives)
  683. {
  684. for (int i = 0; i < directives.Count; i++)
  685. {
  686. CodeDirective directive = directives[i];
  687. if (directive is CodeChecksumPragma)
  688. {
  689. this.GenerateChecksumPragma((CodeChecksumPragma)directive);
  690. }
  691. else if (directive is CodeRegionDirective)
  692. {
  693. this.GenerateCodeRegionDirective((CodeRegionDirective)directive);
  694. }
  695. }
  696. }
  697. protected override void GenerateDoubleValue(double d)
  698. {
  699. if (double.IsNaN(d))
  700. {
  701. base.Output.Write("Double.NaN");
  702. }
  703. else if (double.IsNegativeInfinity(d))
  704. {
  705. base.Output.Write("Double.NegativeInfinity");
  706. }
  707. else if (double.IsPositiveInfinity(d))
  708. {
  709. base.Output.Write("Double.PositiveInfinity");
  710. }
  711. else
  712. {
  713. base.Output.Write(d.ToString("R", CultureInfo.InvariantCulture));
  714. base.Output.Write("R");
  715. }
  716. }
  717. protected override void GenerateEntryPointMethod(CodeEntryPointMethod e, CodeTypeDeclaration c)
  718. {
  719. if (e.CustomAttributes.Count > 0)
  720. {
  721. this.OutputAttributes(e.CustomAttributes, false);
  722. }
  723. base.Output.WriteLine("Public Shared Sub Main()");
  724. base.Indent++;
  725. this.GenerateVBStatements(e.Statements);
  726. base.Indent--;
  727. base.Output.WriteLine("End Sub");
  728. }
  729. protected override void GenerateEvent(CodeMemberEvent e, CodeTypeDeclaration c)
  730. {
  731. if (!base.IsCurrentDelegate && !base.IsCurrentEnum)
  732. {
  733. if (e.CustomAttributes.Count > 0)
  734. {
  735. this.OutputAttributes(e.CustomAttributes, false);
  736. }
  737. string name = e.Name;
  738. if (e.PrivateImplementationType != null)
  739. {
  740. string str2 = this.GetBaseTypeOutput(e.PrivateImplementationType).Replace('.', '_');
  741. e.Name = str2 + "_" + e.Name;
  742. }
  743. this.OutputMemberAccessModifier(e.Attributes);
  744. base.Output.Write("Event ");
  745. this.OutputTypeNamePair(e.Type, e.Name);
  746. if (e.ImplementationTypes.Count > 0)
  747. {
  748. base.Output.Write(" Implements ");
  749. bool flag = true;
  750. foreach (CodeTypeReference reference in e.ImplementationTypes)
  751. {
  752. if (flag)
  753. {
  754. flag = false;
  755. }
  756. else
  757. {
  758. base.Output.Write(" , ");
  759. }
  760. this.OutputType(reference);
  761. base.Output.Write(".");
  762. this.OutputIdentifier(name);
  763. }
  764. }
  765. else if (e.PrivateImplementationType != null)
  766. {
  767. base.Output.Write(" Implements ");
  768. this.OutputType(e.PrivateImplementationType);
  769. base.Output.Write(".");
  770. this.OutputIdentifier(name);
  771. }
  772. base.Output.WriteLine("");
  773. }
  774. }
  775. protected override void GenerateEventReferenceExpression(CodeEventReferenceExpression e)
  776. {
  777. if (e.TargetObject != null)
  778. {
  779. bool flag = e.TargetObject is CodeThisReferenceExpression;
  780. base.GenerateExpression(e.TargetObject);
  781. base.Output.Write(".");
  782. if (flag)
  783. {
  784. base.Output.Write(e.EventName + "Event");
  785. }
  786. else
  787. {
  788. base.Output.Write(e.EventName);
  789. }
  790. }
  791. else
  792. {
  793. this.OutputIdentifier(e.EventName + "Event");
  794. }
  795. }
  796. protected override void GenerateExpressionStatement(CodeExpressionStatement e)
  797. {
  798. base.GenerateExpression(e.Expression);
  799. base.Output.WriteLine("");
  800. }
  801. protected override void GenerateField(CodeMemberField e)
  802. {
  803. if (!base.IsCurrentDelegate && !base.IsCurrentInterface)
  804. {
  805. if (base.IsCurrentEnum)
  806. {
  807. if (e.CustomAttributes.Count > 0)
  808. {
  809. this.OutputAttributes(e.CustomAttributes, false);
  810. }
  811. this.OutputIdentifier(e.Name);
  812. if (e.InitExpression != null)
  813. {
  814. base.Output.Write(" = ");
  815. base.GenerateExpression(e.InitExpression);
  816. }
  817. base.Output.WriteLine("");
  818. }
  819. else
  820. {
  821. if (e.CustomAttributes.Count > 0)
  822. {
  823. this.OutputAttributes(e.CustomAttributes, false);
  824. }
  825. this.OutputMemberAccessModifier(e.Attributes);
  826. this.OutputVTableModifier(e.Attributes);
  827. this.OutputFieldScopeModifier(e.Attributes);
  828. if (this.GetUserData(e, "WithEvents", false))
  829. {
  830. base.Output.Write("WithEvents ");
  831. }
  832. this.OutputTypeNamePair(e.Type, e.Name);
  833. if (e.InitExpression != null)
  834. {
  835. base.Output.Write(" = ");
  836. base.GenerateExpression(e.InitExpression);
  837. }
  838. base.Output.WriteLine("");
  839. }
  840. }
  841. }
  842. protected override void GenerateFieldReferenceExpression(CodeFieldReferenceExpression e)
  843. {
  844. if (e.TargetObject != null)
  845. {
  846. base.GenerateExpression(e.TargetObject);
  847. base.Output.Write(".");
  848. }
  849. this.OutputIdentifier(e.FieldName);
  850. }
  851. private void GenerateFormalEventReferenceExpression(CodeEventReferenceExpression e)
  852. {
  853. if ((e.TargetObject != null) && !(e.TargetObject is CodeThisReferenceExpression))
  854. {
  855. base.GenerateExpression(e.TargetObject);
  856. base.Output.Write(".");
  857. }
  858. this.OutputIdentifier(e.EventName);
  859. }
  860. protected override void GenerateGotoStatement(CodeGotoStatement e)
  861. {
  862. base.Output.Write("goto ");
  863. base.Output.WriteLine(e.Label);
  864. }
  865. protected override void GenerateIndexerExpression(CodeIndexerExpression e)
  866. {
  867. base.GenerateExpression(e.TargetObject);
  868. if (e.TargetObject is CodeBaseReferenceExpression)
  869. {
  870. base.Output.Write(".Item");
  871. }
  872. base.Output.Write("(");
  873. bool flag = true;
  874. foreach (CodeExpression expression in e.Indices)
  875. {
  876. if (flag)
  877. {
  878. flag = false;
  879. }
  880. else
  881. {
  882. base.Output.Write(", ");
  883. }
  884. base.GenerateExpression(expression);
  885. }
  886. base.Output.Write(")");
  887. }
  888. protected override void GenerateIterationStatement(CodeIterationStatement e)
  889. {
  890. base.GenerateStatement(e.InitStatement);
  891. base.Output.Write("Do While ");
  892. base.GenerateExpression(e.TestExpression);
  893. base.Output.WriteLine("");
  894. base.Indent++;
  895. this.GenerateVBStatements(e.Statements);
  896. base.GenerateStatement(e.IncrementStatement);
  897. base.Indent--;
  898. base.Output.WriteLine("Loop");
  899. }
  900. protected override void GenerateLabeledStatement(CodeLabeledStatement e)
  901. {
  902. base.Indent--;
  903. base.Output.Write(e.Label);
  904. base.Output.WriteLine(":");
  905. base.Indent++;
  906. if (e.Statement != null)
  907. {
  908. base.GenerateStatement(e.Statement);
  909. }
  910. }
  911. protected override void GenerateLinePragmaEnd(CodeLinePragma e)
  912. {
  913. base.Output.WriteLine("");
  914. base.Output.WriteLine("#End ExternalSource");
  915. }
  916. protected override void GenerateLinePragmaStart(CodeLinePragma e)
  917. {
  918. base.Output.WriteLine("");
  919. base.Output.Write("#ExternalSource(\"");
  920. base.Output.Write(e.FileName);
  921. base.Output.Write("\",");
  922. base.Output.Write(e.LineNumber);
  923. base.Output.WriteLine(")");
  924. }
  925. protected override void GenerateMethod(CodeMemberMethod e, CodeTypeDeclaration c)
  926. {
  927. if ((base.IsCurrentClass || base.IsCurrentStruct) || base.IsCurrentInterface)
  928. {
  929. if (e.CustomAttributes.Count > 0)
  930. {
  931. this.OutputAttributes(e.CustomAttributes, false);
  932. }
  933. string name = e.Name;
  934. if (e.PrivateImplementationType != null)
  935. {
  936. string str2 = this.GetBaseTypeOutput(e.PrivateImplementationType).Replace('.', '_');
  937. e.Name = str2 + "_" + e.Name;
  938. }
  939. if (!base.IsCurrentInterface)
  940. {
  941. if (e.PrivateImplementationType == null)
  942. {
  943. this.OutputMemberAccessModifier(e.Attributes);
  944. if (this.MethodIsOverloaded(e, c))
  945. {
  946. base.Output.Write("Overloads ");
  947. }
  948. }
  949. this.OutputVTableModifier(e.Attributes);
  950. this.OutputMemberScopeModifier(e.Attributes);
  951. }
  952. else
  953. {
  954. this.OutputVTableModifier(e.Attributes);
  955. }
  956. bool flag = false;
  957. if ((e.ReturnType.BaseType.Length == 0) || (string.Compare(e.ReturnType.BaseType, typeof(void).FullName, StringComparison.OrdinalIgnoreCase) == 0))
  958. {
  959. flag = true;
  960. }
  961. if (flag)
  962. {
  963. base.Output.Write("Sub ");
  964. }
  965. else
  966. {
  967. base.Output.Write("Function ");
  968. }
  969. this.OutputIdentifier(e.Name);
  970. this.OutputTypeParameters(e.TypeParameters);
  971. base.Output.Write("(");
  972. this.OutputParameters(e.Parameters);
  973. base.Output.Write(")");
  974. if (!flag)
  975. {
  976. base.Output.Write(" As ");
  977. if (e.ReturnTypeCustomAttributes.Count > 0)
  978. {
  979. this.OutputAttributes(e.ReturnTypeCustomAttributes, true);
  980. }
  981. this.OutputType(e.ReturnType);
  982. this.OutputArrayPostfix(e.ReturnType);
  983. }
  984. if (e.ImplementationTypes.Count > 0)
  985. {
  986. base.Output.Write(" Implements ");
  987. bool flag2 = true;
  988. foreach (CodeTypeReference reference in e.ImplementationTypes)
  989. {
  990. if (flag2)
  991. {
  992. flag2 = false;
  993. }
  994. else
  995. {
  996. base.Output.Write(" , ");
  997. }
  998. this.OutputType(reference);
  999. base.Output.Write(".");
  1000. this.OutputIdentifier(name);
  1001. }
  1002. }
  1003. else if (e.PrivateImplementationType != null)
  1004. {
  1005. base.Output.Write(" Implements ");
  1006. this.OutputType(e.PrivateImplementationType);
  1007. base.Output.Write(".");
  1008. this.OutputIdentifier(name);
  1009. }
  1010. base.Output.WriteLine("");
  1011. if (!base.IsCurrentInterface && ((e.Attributes & MemberAttributes.ScopeMask) != MemberAttributes.Abstract))
  1012. {
  1013. base.Indent++;
  1014. this.GenerateVBStatements(e.Statements);
  1015. base.Indent--;
  1016. if (flag)
  1017. {
  1018. base.Output.WriteLine("End Sub");
  1019. }
  1020. else
  1021. {
  1022. base.Output.WriteLine("End Function");
  1023. }
  1024. }
  1025. e.Name = name;
  1026. }
  1027. }
  1028. protected override void GenerateMethodInvokeExpression(CodeMethodInvokeExpression e)
  1029. {
  1030. this.GenerateMethodReferenceExpression(e.Method);
  1031. if (e.Parameters.Count > 0)
  1032. {
  1033. base.Output.Write("(");
  1034. this.OutputExpressionList(e.Parameters);
  1035. base.Output.Write(")");
  1036. }
  1037. }
  1038. protected override void GenerateMethodReferenceExpression(CodeMethodReferenceExpression e)
  1039. {
  1040. if (e.TargetObject != null)
  1041. {
  1042. base.GenerateExpression(e.TargetObject);
  1043. base.Output.Write(".");
  1044. base.Output.Write(e.MethodName);
  1045. }
  1046. else
  1047. {
  1048. this.OutputIdentifier(e.MethodName);
  1049. }
  1050. if (e.TypeArguments.Count > 0)
  1051. {
  1052. base.Output.Write(this.GetTypeArgumentsOutput(e.TypeArguments));
  1053. }
  1054. }
  1055. protected override void GenerateMethodReturnStatement(CodeMethodReturnStatement e)
  1056. {
  1057. if (e.Expression != null)
  1058. {
  1059. base.Output.Write("Return ");
  1060. base.GenerateExpression(e.Expression);
  1061. base.Output.WriteLine("");
  1062. }
  1063. else
  1064. {
  1065. base.Output.WriteLine("Return");
  1066. }
  1067. }
  1068. protected override void GenerateNamespace(CodeNamespace e)
  1069. {
  1070. if (this.GetUserData(e, "GenerateImports", true))
  1071. {
  1072. base.GenerateNamespaceImports(e);
  1073. }
  1074. base.Output.WriteLine();
  1075. this.GenerateCommentStatements(e.Comments);
  1076. this.GenerateNamespaceStart(e);
  1077. base.GenerateTypes(e);
  1078. this.GenerateNamespaceEnd(e);
  1079. }
  1080. protected override void GenerateNamespaceEnd(CodeNamespace e)
  1081. {
  1082. if ((e.Name != null) && (e.Name.Length > 0))
  1083. {
  1084. base.Indent--;
  1085. base.Output.WriteLine("End Namespace");
  1086. }
  1087. }
  1088. protected override void GenerateNamespaceImport(CodeNamespaceImport e)
  1089. {
  1090. base.Output.Write("Imports ");
  1091. this.OutputIdentifier(e.Namespace);
  1092. base.Output.WriteLine("");
  1093. }
  1094. protected override void GenerateNamespaceStart(CodeNamespace e)
  1095. {
  1096. if ((e.Name != null) && (e.Name.Length > 0))
  1097. {
  1098. base.Output.Write("Namespace ");
  1099. string[] strArray = e.Name.Split(new char[] { '.' });
  1100. this.OutputIdentifier(strArray[0]);
  1101. for (int i = 1; i < strArray.Length; i++)
  1102. {
  1103. base.Output.Write(".");
  1104. this.OutputIdentifier(strArray[i]);
  1105. }
  1106. base.Output.WriteLine();
  1107. base.Indent++;
  1108. }
  1109. }
  1110. private void GenerateNotIsNullExpression(CodeExpression e)
  1111. {
  1112. base.Output.Write("(Not (");
  1113. base.GenerateExpression(e);
  1114. base.Output.Write(") Is ");
  1115. base.Output.Write(this.NullToken);
  1116. base.Output.Write(")");
  1117. }
  1118. protected override void GenerateObjectCreateExpression(CodeObjectCreateExpression e)
  1119. {
  1120. base.Output.Write("New ");
  1121. this.OutputType(e.CreateType);
  1122. base.Output.Write("(");
  1123. this.OutputExpressionList(e.Parameters);
  1124. base.Output.Write(")");
  1125. }
  1126. protected override void GenerateParameterDeclarationExpression(CodeParameterDeclarationExpression e)
  1127. {
  1128. if (e.CustomAttributes.Count > 0)
  1129. {
  1130. this.OutputAttributes(e.CustomAttributes, true);
  1131. }
  1132. this.OutputDirection(e.Direction);
  1133. this.OutputTypeNamePair(e.Type, e.Name);
  1134. }
  1135. protected override void GeneratePrimitiveExpression(CodePrimitiveExpression e)
  1136. {
  1137. if (e.Value is char)
  1138. {
  1139. base.Output.Write("Global.Microsoft.VisualBasic.ChrW(" + ((IConvertible)e.Value).ToInt32(CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture) + ")");
  1140. }
  1141. else if (e.Value is sbyte)
  1142. {
  1143. base.Output.Write("CSByte(");
  1144. sbyte num2 = (sbyte)e.Value;
  1145. base.Output.Write(num2.ToString(CultureInfo.InvariantCulture));
  1146. base.Output.Write(")");
  1147. }
  1148. else if (e.Value is ushort)
  1149. {
  1150. ushort num3 = (ushort)e.Value;
  1151. base.Output.Write(num3.ToString(CultureInfo.InvariantCulture));
  1152. base.Output.Write("US");
  1153. }
  1154. else if (e.Value is uint)
  1155. {
  1156. uint num4 = (uint)e.Value;
  1157. base.Output.Write(num4.ToString(CultureInfo.InvariantCulture));
  1158. base.Output.Write("UI");
  1159. }
  1160. else if (e.Value is ulong)
  1161. {
  1162. ulong num5 = (ulong)e.Value;
  1163. base.Output.Write(num5.ToString(CultureInfo.InvariantCulture));
  1164. base.Output.Write("UL");
  1165. }
  1166. else
  1167. {
  1168. base.GeneratePrimitiveExpression(e);
  1169. }
  1170. }
  1171. protected override void GenerateProperty(CodeMemberProperty e, CodeTypeDeclaration c)
  1172. {
  1173. if ((base.IsCurrentClass || base.IsCurrentStruct) || base.IsCurrentInterface)
  1174. {
  1175. if (e.CustomAttributes.Count > 0)
  1176. {
  1177. this.OutputAttributes(e.CustomAttributes, false);
  1178. }
  1179. string name = e.Name;
  1180. if (e.PrivateImplementationType != null)
  1181. {
  1182. string str2 = this.GetBaseTypeOutput(e.PrivateImplementationType).Replace('.', '_');
  1183. e.Name = str2 + "_" + e.Name;
  1184. }
  1185. if (!base.IsCurrentInterface)
  1186. {
  1187. if (e.PrivateImplementationType == null)
  1188. {
  1189. this.OutputMemberAccessModifier(e.Attributes);
  1190. if (this.PropertyIsOverloaded(e, c))
  1191. {

Large files files are truncated, but you can click here to view the full file