PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System/Microsoft.VisualBasic/VBCodeGenerator.cs

https://bitbucket.org/puffnfresh/mono-dependency-analysis
C# | 1754 lines | 1457 code | 224 blank | 73 comment | 295 complexity | 1a54c0cbda3ff890bd2ee76eca270263 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense, Apache-2.0, LGPL-2.0
  1. //
  2. // Microsoft.VisualBasic.VBCodeGenerator.cs
  3. //
  4. // Author:
  5. // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
  6. // (partially based on CSharpCodeGenerator)
  7. // Jochen Wezel (jwezel@compumaster.de)
  8. // Frederik Carlier (frederik.carlier@carlier-online.be)
  9. // Rolf Bjarne Kvinge (RKvinge@novell.com)
  10. //
  11. // (C) 2003 Andreas Nahr
  12. // (C) 2003 Jochen Wezel (http://www.compumaster.de)
  13. //
  14. // Modifications:
  15. // 2003-11-06 JW: some corrections regarding missing spaces in generated code (e. g. "Property ")
  16. // 2003-11-06 JW: QuoteSnippetString implemented
  17. // 2003-11-08 JW: automatically add Microsoft.VisualBasic
  18. // 2003-11-12 JW: some corrections to allow correct compilation
  19. // 2003-11-28 JW: implementing code differences into current build of this file
  20. // 2003-12-10 JW: added "String." for the ChrW method because mbas doesn't support it without the String currently / TODO: remove it ASAP!
  21. // 2007-04-13 FC: Added support for the IdentityInequality operator when comparing against Nothing
  22. //
  23. // Permission is hereby granted, free of charge, to any person obtaining
  24. // a copy of this software and associated documentation files (the
  25. // "Software"), to deal in the Software without restriction, including
  26. // without limitation the rights to use, copy, modify, merge, publish,
  27. // distribute, sublicense, and/or sell copies of the Software, and to
  28. // permit persons to whom the Software is furnished to do so, subject to
  29. // the following conditions:
  30. //
  31. // The above copyright notice and this permission notice shall be
  32. // included in all copies or substantial portions of the Software.
  33. //
  34. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  35. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  36. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  37. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  38. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  39. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  40. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  41. //
  42. using System;
  43. using System.Globalization;
  44. using System.Text;
  45. using System.Text.RegularExpressions;
  46. using System.CodeDom;
  47. using System.CodeDom.Compiler;
  48. using System.IO;
  49. using System.Reflection;
  50. using System.Collections;
  51. namespace Microsoft.VisualBasic
  52. {
  53. internal class VBCodeGenerator : CodeGenerator
  54. {
  55. #if NET_2_0
  56. private string [] Keywords = new string [] {
  57. "AddHandler", "AddressOf", "Alias", "And",
  58. "AndAlso", "Ansi", "As", "Assembly",
  59. "Auto", "Boolean", "ByRef", "Byte",
  60. "ByVal", "Call", "Case", "Catch",
  61. "CBool", "CByte", "CChar", "CDate",
  62. "CDec", "CDbl", "Char", "CInt",
  63. "Class", "CLng", "CObj", "Const",
  64. "CShort", "CSng", "CStr", "CType",
  65. "Date", "Decimal", "Declare", "Default",
  66. "Delegate", "Dim", "DirectCast", "Do",
  67. "Double", "Each", "Else", "ElseIf",
  68. "End", "Enum", "Erase", "Error",
  69. "Event", "Exit", "False", "Finally",
  70. "For", "Friend", "Function", "Get",
  71. "GetType", "Global", "GoSub", "GoTo", "Handles",
  72. "If", "Implements", "Imports", "In",
  73. "Inherits", "Integer", "Interface", "Is",
  74. "Let", "Lib", "Like", "Long",
  75. "Loop", "Me", "Mod", "Module",
  76. "MustInherit", "MustOverride", "MyBase", "MyClass",
  77. "Namespace", "New", "Next", "Not",
  78. "Nothing", "NotInheritable", "NotOverridable", "Object",
  79. "On", "Option", "Optional", "Or",
  80. "OrElse", "Overloads", "Overridable", "Overrides",
  81. "ParamArray", "Partial", "Preserve", "Private", "Property",
  82. "Protected", "Public", "RaiseEvent", "ReadOnly",
  83. "ReDim", "REM", "RemoveHandler", "Resume",
  84. "Return", "Select", "Set", "Shadows",
  85. "Shared", "Short", "Single", "Static",
  86. "Step", "Stop", "String", "Structure",
  87. "Sub", "SyncLock", "Then", "Throw",
  88. "To", "True", "Try", "TypeOf",
  89. "Unicode", "Until", "Variant", "When",
  90. "While", "With", "WithEvents", "WriteOnly",
  91. "Xor"
  92. };
  93. #else
  94. private string [] Keywords = new string [] {
  95. "AddHandler", "AddressOf", "Alias", "And",
  96. "AndAlso", "Ansi", "As", "Assembly",
  97. "Auto", "Boolean", "ByRef", "Byte",
  98. "ByVal", "Call", "Case", "Catch",
  99. "CBool", "CByte", "CChar", "CDate",
  100. "CDec", "CDbl", "Char", "CInt",
  101. "Class", "CLng", "CObj", "Const",
  102. "CShort", "CSng", "CStr", "CType",
  103. "Date", "Decimal", "Declare", "Default",
  104. "Delegate", "Dim", "DirectCast", "Do",
  105. "Double", "Each", "Else", "ElseIf",
  106. "End", "Enum", "Erase", "Error",
  107. "Event", "Exit", "False", "Finally",
  108. "For", "Friend", "Function", "Get",
  109. "GetType", "GoSub", "GoTo", "Handles",
  110. "If", "Implements", "Imports", "In",
  111. "Inherits", "Integer", "Interface", "Is",
  112. "Let", "Lib", "Like", "Long",
  113. "Loop", "Me", "Mod", "Module",
  114. "MustInherit", "MustOverride", "MyBase", "MyClass",
  115. "Namespace", "New", "Next", "Not",
  116. "Nothing", "NotInheritable", "NotOverridable", "Object",
  117. "On", "Option", "Optional", "Or",
  118. "OrElse", "Overloads", "Overridable", "Overrides",
  119. "ParamArray", "Preserve", "Private", "Property",
  120. "Protected", "Public", "RaiseEvent", "ReadOnly",
  121. "ReDim", "REM", "RemoveHandler", "Resume",
  122. "Return", "Select", "Set", "Shadows",
  123. "Shared", "Short", "Single", "Static",
  124. "Step", "Stop", "String", "Structure",
  125. "Sub", "SyncLock", "Then", "Throw",
  126. "To", "True", "Try", "TypeOf",
  127. "Unicode", "Until", "Variant", "When",
  128. "While", "With", "WithEvents", "WriteOnly",
  129. "Xor"
  130. };
  131. #endif
  132. public VBCodeGenerator()
  133. {
  134. }
  135. protected override string NullToken {
  136. get {
  137. return "Nothing";
  138. }
  139. }
  140. protected override void ContinueOnNewLine (string st)
  141. {
  142. Output.Write (st);
  143. Output.WriteLine (" _");
  144. }
  145. protected override void GenerateBinaryOperatorExpression (CodeBinaryOperatorExpression e)
  146. {
  147. // We need to special case for comparisons against null;
  148. // in Visual Basic the "Not (Expr) Is Nothing" construct is used
  149. bool null_comparison = false;
  150. bool reverse = false;
  151. if (e.Operator == CodeBinaryOperatorType.IdentityInequality) {
  152. CodePrimitiveExpression nothing;
  153. nothing = e.Left as CodePrimitiveExpression;
  154. if (nothing == null) {
  155. nothing = e.Right as CodePrimitiveExpression;
  156. } else {
  157. reverse = true;
  158. }
  159. null_comparison = nothing != null && nothing.Value == null;
  160. }
  161. if (null_comparison) {
  162. TextWriter output = Output;
  163. output.Write ("(Not (");
  164. GenerateExpression (reverse ? e.Right : e.Left);
  165. output.Write (") Is ");
  166. GenerateExpression (reverse ? e.Left : e.Right);
  167. output.Write (')');
  168. } else {
  169. base.GenerateBinaryOperatorExpression (e);
  170. }
  171. }
  172. protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression expression)
  173. {
  174. TextWriter output = Output;
  175. output.Write ("New ");
  176. CodeExpressionCollection initializers = expression.Initializers;
  177. CodeTypeReference createType = expression.CreateType;
  178. if (initializers.Count > 0) {
  179. OutputType (createType);
  180. output.Write ("() {");
  181. ++Indent;
  182. OutputExpressionList (initializers);
  183. --Indent;
  184. output.Write ("}");
  185. } else {
  186. CodeTypeReference arrayType = createType.ArrayElementType;
  187. while (arrayType != null) {
  188. createType = arrayType;
  189. arrayType = arrayType.ArrayElementType;
  190. }
  191. OutputType (createType);
  192. output.Write ("((");
  193. CodeExpression size = expression.SizeExpression;
  194. if (size != null)
  195. GenerateExpression (size);
  196. else
  197. output.Write (expression.Size);
  198. output.Write (") - 1) {}");
  199. }
  200. }
  201. protected override void GenerateBaseReferenceExpression (CodeBaseReferenceExpression expression)
  202. {
  203. Output.Write ("MyBase");
  204. }
  205. protected override void GenerateCastExpression (CodeCastExpression expression)
  206. {
  207. TextWriter output = Output;
  208. // ENHANCE: Use a DirectCast if it is known that expression.Expression is no Value-Type
  209. output.Write ("CType(");
  210. GenerateExpression (expression.Expression);
  211. output.Write (", ");
  212. OutputType (expression.TargetType);
  213. output.Write (")");
  214. }
  215. private bool AsBool (object datavalue)
  216. {
  217. return datavalue != null && datavalue is bool && (bool) datavalue;
  218. }
  219. private string OnOff (bool datavalue)
  220. {
  221. return datavalue ? "On" : "Off";
  222. }
  223. protected override void GenerateCompileUnitStart (CodeCompileUnit compileUnit)
  224. {
  225. GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
  226. GenerateComment (new CodeComment (" <autogenerated>"));
  227. GenerateComment (new CodeComment (" This code was generated by a tool."));
  228. GenerateComment (new CodeComment (" Mono Runtime Version: " + System.Environment.Version));
  229. GenerateComment (new CodeComment (""));
  230. GenerateComment (new CodeComment (" Changes to this file may cause incorrect behavior and will be lost if "));
  231. GenerateComment (new CodeComment (" the code is regenerated."));
  232. GenerateComment (new CodeComment (" </autogenerated>"));
  233. GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
  234. Output.WriteLine ();
  235. if (AsBool (compileUnit.UserData ["AllowLateBound"])) {
  236. Output.WriteLine("Option Explicit {0}", OnOff (AsBool (compileUnit.UserData ["RequireVariableDeclaration"])));
  237. Output.WriteLine("Option Strict Off");
  238. } else {
  239. Output.WriteLine("Option Explicit On"); // Strict On implies Explicit On
  240. Output.WriteLine("Option Strict On");
  241. }
  242. Output.WriteLine ();
  243. }
  244. protected override void GenerateCompileUnit (CodeCompileUnit compileUnit)
  245. {
  246. GenerateCompileUnitStart (compileUnit);
  247. OutputAttributes (compileUnit.AssemblyCustomAttributes,
  248. "Assembly: ", LineHandling.NewLine);
  249. GenerateNamespaces (compileUnit);
  250. GenerateCompileUnitEnd (compileUnit);
  251. }
  252. protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression expression)
  253. {
  254. TextWriter output = Output;
  255. output.Write ("AddressOf ");
  256. CodeExpression targetObject = expression.TargetObject;
  257. if (targetObject != null) {
  258. GenerateExpression (targetObject);
  259. Output.Write ('.');
  260. }
  261. output.Write (expression.MethodName);
  262. }
  263. protected override void GenerateFieldReferenceExpression (CodeFieldReferenceExpression expression)
  264. {
  265. CodeExpression targetObject = expression.TargetObject;
  266. if (targetObject != null) {
  267. GenerateExpression (targetObject);
  268. Output.Write ('.');
  269. }
  270. Output.Write (CreateEscapedIdentifier (expression.FieldName));
  271. }
  272. protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression expression)
  273. {
  274. Output.Write (CreateEscapedIdentifier (expression.ParameterName));
  275. }
  276. protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression expression)
  277. {
  278. Output.Write (CreateEscapedIdentifier (expression.VariableName));
  279. }
  280. protected override void GenerateIndexerExpression (CodeIndexerExpression expression)
  281. {
  282. TextWriter output = Output;
  283. GenerateExpression (expression.TargetObject);
  284. output.Write ('(');
  285. OutputExpressionList (expression.Indices);
  286. output.Write (')');
  287. }
  288. protected override void GenerateArrayIndexerExpression (CodeArrayIndexerExpression expression)
  289. {
  290. TextWriter output = Output;
  291. GenerateExpression (expression.TargetObject);
  292. output.Write ("(");
  293. OutputExpressionList (expression.Indices);
  294. output.Write (')');
  295. }
  296. protected override void GenerateSnippetExpression (CodeSnippetExpression expression)
  297. {
  298. Output.Write (expression.Value);
  299. }
  300. protected override void GenerateMethodInvokeExpression (CodeMethodInvokeExpression expression)
  301. {
  302. TextWriter output = Output;
  303. GenerateMethodReferenceExpression (expression.Method);
  304. output.Write ('(');
  305. OutputExpressionList (expression.Parameters);
  306. output.Write (')');
  307. }
  308. protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression expression)
  309. {
  310. if (expression.TargetObject != null) {
  311. GenerateExpression (expression.TargetObject);
  312. Output.Write ('.');
  313. }
  314. Output.Write (CreateEscapedIdentifier (expression.MethodName));
  315. }
  316. protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression expression)
  317. {
  318. if (expression.TargetObject != null) {
  319. GenerateExpression (expression.TargetObject);
  320. Output.Write ('.');
  321. if (expression.TargetObject is CodeThisReferenceExpression) {
  322. // We're actually creating a reference to a compiler-generated field here...
  323. Output.Write (expression.EventName + "Event");
  324. } else {
  325. Output.Write (CreateEscapedIdentifier (expression.EventName));
  326. }
  327. } else {
  328. Output.Write (CreateEscapedIdentifier (expression.EventName + "Event"));
  329. }
  330. }
  331. protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression expression)
  332. {
  333. CodeEventReferenceExpression ev = expression.TargetObject as CodeEventReferenceExpression;
  334. #if ONLY_1_1
  335. Output.Write ("RaiseEvent ");
  336. #endif
  337. if (ev != null) {
  338. #if NET_2_0
  339. Output.Write ("RaiseEvent ");
  340. #endif
  341. if (ev.TargetObject != null && !(ev.TargetObject is CodeThisReferenceExpression)) {
  342. GenerateExpression (ev.TargetObject);
  343. Output.Write (".");
  344. }
  345. Output.Write (ev.EventName);
  346. } else if (expression.TargetObject != null) {
  347. GenerateExpression (expression.TargetObject);
  348. }
  349. Output.Write ('(');
  350. OutputExpressionList (expression.Parameters);
  351. Output.Write (')');
  352. }
  353. protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression expression)
  354. {
  355. Output.Write( "New " );
  356. OutputType (expression.CreateType);
  357. Output.Write ('(');
  358. OutputExpressionList (expression.Parameters);
  359. Output.Write (')');
  360. }
  361. protected override void GenerateParameterDeclarationExpression (CodeParameterDeclarationExpression e)
  362. {
  363. OutputAttributes (e.CustomAttributes, null, LineHandling.InLine);
  364. OutputDirection (e.Direction);
  365. OutputTypeNamePair (e.Type, e.Name);
  366. }
  367. protected override void GeneratePrimitiveExpression (CodePrimitiveExpression e)
  368. {
  369. if (e.Value is char) {
  370. char c = (char) e.Value;
  371. int ch = (int) c;
  372. #if NET_2_0
  373. Output.Write("Global.Microsoft.VisualBasic.ChrW(" + ch.ToString(CultureInfo.InvariantCulture) + ")");
  374. } else if (e.Value is ushort) {
  375. ushort uc = (ushort) e.Value;
  376. Output.Write (uc.ToString(CultureInfo.InvariantCulture));
  377. Output.Write ("US");
  378. } else if (e.Value is uint) {
  379. uint ui = (uint) e.Value;
  380. Output.Write (ui.ToString(CultureInfo.InvariantCulture));
  381. Output.Write ("UI");
  382. } else if (e.Value is ulong) {
  383. ulong ul = (ulong) e.Value;
  384. Output.Write (ul.ToString(CultureInfo.InvariantCulture));
  385. Output.Write ("UL");
  386. } else if (e.Value is sbyte) {
  387. sbyte sb = (sbyte) e.Value;
  388. Output.Write ("CSByte(");
  389. Output.Write (sb.ToString(CultureInfo.InvariantCulture));
  390. Output.Write (')');
  391. #else
  392. Output.Write("Microsoft.VisualBasic.ChrW(" + ch.ToString(CultureInfo.InvariantCulture) + ")");
  393. #endif
  394. } else {
  395. base.GeneratePrimitiveExpression(e);
  396. }
  397. }
  398. protected override void GenerateSingleFloatValue (float s)
  399. {
  400. base.GenerateSingleFloatValue (s);
  401. base.Output.Write ('!');
  402. }
  403. protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression expression)
  404. {
  405. if (expression.TargetObject != null) {
  406. GenerateMemberReferenceExpression (expression.TargetObject, expression.PropertyName);
  407. } else {
  408. Output.Write (CreateEscapedIdentifier (expression.PropertyName));
  409. }
  410. }
  411. protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression expression)
  412. {
  413. Output.Write ("Value");
  414. }
  415. protected override void GenerateThisReferenceExpression (CodeThisReferenceExpression expression)
  416. {
  417. Output.Write ("Me");
  418. }
  419. protected override void GenerateExpressionStatement (CodeExpressionStatement statement)
  420. {
  421. GenerateExpression (statement.Expression);
  422. Output.WriteLine (); //start new line
  423. }
  424. protected override void GenerateIterationStatement (CodeIterationStatement statement)
  425. {
  426. TextWriter output = Output;
  427. GenerateStatement (statement.InitStatement);
  428. output.Write ("Do While ");
  429. GenerateExpression (statement.TestExpression);
  430. output.WriteLine ();
  431. Indent++;
  432. GenerateStatements (statement.Statements);
  433. GenerateStatement (statement.IncrementStatement);
  434. Indent--;
  435. output.WriteLine ("Loop");
  436. }
  437. protected override void GenerateThrowExceptionStatement (CodeThrowExceptionStatement statement)
  438. {
  439. Output.Write ("Throw");
  440. if (statement.ToThrow != null) {
  441. Output.Write (' ');
  442. GenerateExpression (statement.ToThrow);
  443. }
  444. Output.WriteLine ();
  445. }
  446. protected override void GenerateComment (CodeComment comment)
  447. {
  448. TextWriter output = Output;
  449. string commentChars = null;
  450. if (comment.DocComment) {
  451. commentChars = "'''";
  452. } else {
  453. commentChars = "'";
  454. }
  455. output.Write (commentChars);
  456. string text = comment.Text;
  457. for (int i = 0; i < text.Length; i++) {
  458. output.Write (text [i]);
  459. if (text[i] == '\r') {
  460. if (i < (text.Length - 1) && text [i + 1] == '\n') {
  461. continue;
  462. }
  463. output.Write (commentChars);
  464. } else if (text [i] == '\n') {
  465. output.Write (commentChars);
  466. }
  467. }
  468. output.WriteLine ();
  469. }
  470. protected override void GenerateMethodReturnStatement (CodeMethodReturnStatement statement)
  471. {
  472. TextWriter output = Output;
  473. if (statement.Expression != null) {
  474. output.Write ("Return ");
  475. GenerateExpression (statement.Expression);
  476. output.WriteLine ();
  477. } else {
  478. output.WriteLine ("Return");
  479. }
  480. }
  481. protected override void GenerateConditionStatement (CodeConditionStatement statement)
  482. {
  483. TextWriter output = Output;
  484. output.Write ("If ");
  485. GenerateExpression (statement.Condition);
  486. output.WriteLine (" Then");
  487. ++Indent;
  488. GenerateStatements (statement.TrueStatements);
  489. --Indent;
  490. CodeStatementCollection falses = statement.FalseStatements;
  491. if (falses.Count > 0) {
  492. output.WriteLine ("Else");
  493. ++Indent;
  494. GenerateStatements (falses);
  495. --Indent;
  496. }
  497. else {
  498. if (Options.ElseOnClosing)
  499. output.WriteLine ("Else");
  500. }
  501. output.WriteLine ("End If");
  502. }
  503. protected override void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement statement)
  504. {
  505. TextWriter output = Output;
  506. output.WriteLine ("Try ");
  507. ++Indent;
  508. GenerateStatements (statement.TryStatements);
  509. --Indent;
  510. foreach (CodeCatchClause clause in statement.CatchClauses) {
  511. output.Write ("Catch ");
  512. OutputTypeNamePair (clause.CatchExceptionType, clause.LocalName);
  513. output.WriteLine ();
  514. ++Indent;
  515. GenerateStatements (clause.Statements);
  516. --Indent;
  517. }
  518. CodeStatementCollection finallies = statement.FinallyStatements;
  519. if (finallies.Count > 0) {
  520. output.WriteLine ("Finally");
  521. ++Indent;
  522. GenerateStatements (finallies);
  523. --Indent;
  524. }
  525. output.WriteLine("End Try");
  526. }
  527. protected override void GenerateAssignStatement (CodeAssignStatement statement)
  528. {
  529. TextWriter output = Output;
  530. GenerateExpression (statement.Left);
  531. output.Write (" = ");
  532. GenerateExpression (statement.Right);
  533. output.WriteLine ();
  534. }
  535. protected override void GenerateAttachEventStatement (CodeAttachEventStatement statement)
  536. {
  537. TextWriter output = Output;
  538. Output.Write ("AddHandler ");
  539. if (statement.Event.TargetObject != null) {
  540. GenerateEventReferenceExpression (statement.Event);
  541. } else {
  542. Output.Write (CreateEscapedIdentifier (statement.Event.EventName));
  543. }
  544. Output.Write ( ", ");
  545. GenerateExpression (statement.Listener);
  546. output.WriteLine ();
  547. }
  548. protected override void GenerateRemoveEventStatement (CodeRemoveEventStatement statement)
  549. {
  550. TextWriter output = Output;
  551. Output.Write ("RemoveHandler ");
  552. if (statement.Event.TargetObject != null) {
  553. GenerateEventReferenceExpression (statement.Event);
  554. } else {
  555. Output.Write (CreateEscapedIdentifier (statement.Event.EventName));
  556. }
  557. Output.Write ( ", ");
  558. GenerateExpression (statement.Listener);
  559. output.WriteLine ();
  560. }
  561. protected override void GenerateGotoStatement (CodeGotoStatement statement)
  562. {
  563. TextWriter output = Output;
  564. output.Write ("goto ");
  565. output.Write (statement.Label);
  566. output.WriteLine ();
  567. }
  568. protected override void GenerateLabeledStatement (CodeLabeledStatement statement)
  569. {
  570. TextWriter output = Output;
  571. Indent--;
  572. output.WriteLine (statement.Label + ":");
  573. Indent++;
  574. if (statement.Statement != null) {
  575. GenerateStatement (statement.Statement);
  576. }
  577. }
  578. protected override void GenerateTypeOfExpression (CodeTypeOfExpression e)
  579. {
  580. TextWriter output = Output;
  581. output.Write ("GetType(");
  582. OutputType (e.Type);
  583. output.Write (")");
  584. }
  585. protected override void GenerateVariableDeclarationStatement( CodeVariableDeclarationStatement statement )
  586. {
  587. TextWriter output = Output;
  588. output.Write ("Dim ");
  589. OutputTypeNamePair (statement.Type, statement.Name);
  590. CodeExpression initExpression = statement.InitExpression;
  591. if (initExpression != null) {
  592. output.Write (" = ");
  593. GenerateExpression (initExpression);
  594. }
  595. output.WriteLine();
  596. }
  597. protected override void GenerateLinePragmaStart (CodeLinePragma linePragma)
  598. {
  599. Output.WriteLine ();
  600. Output.Write ("#ExternalSource(\"");
  601. Output.Write (linePragma.FileName);
  602. Output.Write ("\",");
  603. Output.Write (linePragma.LineNumber);
  604. Output.WriteLine (")");
  605. Output.WriteLine ("");
  606. }
  607. protected override void GenerateLinePragmaEnd (CodeLinePragma linePragma)
  608. {
  609. Output.WriteLine ("#End ExternalSource");
  610. }
  611. protected override void GenerateEvent (CodeMemberEvent eventRef, CodeTypeDeclaration declaration)
  612. {
  613. if (IsCurrentDelegate || IsCurrentEnum)
  614. return;
  615. TextWriter output = Output;
  616. OutputAttributes (eventRef.CustomAttributes, null,
  617. LineHandling.ContinueLine);
  618. OutputMemberAccessModifier (eventRef.Attributes);
  619. output.Write ("Event ");
  620. OutputTypeNamePair (eventRef.Type, GetEventName(eventRef));
  621. #if NET_2_0
  622. if (eventRef.ImplementationTypes.Count > 0) {
  623. OutputImplementationTypes (eventRef.ImplementationTypes, eventRef.Name);
  624. } else if (eventRef.PrivateImplementationType != null) {
  625. output.Write (" Implements ");
  626. OutputType (eventRef.PrivateImplementationType);
  627. output.Write ('.');
  628. output.Write (eventRef.Name);
  629. }
  630. #endif
  631. output.WriteLine ();
  632. }
  633. protected override void GenerateField (CodeMemberField field)
  634. {
  635. if (IsCurrentDelegate || IsCurrentInterface)
  636. return;
  637. TextWriter output = Output;
  638. OutputAttributes (field.CustomAttributes, null,
  639. LineHandling.ContinueLine);
  640. if (IsCurrentEnum) {
  641. output.Write (field.Name);
  642. } else {
  643. MemberAttributes attributes = field.Attributes;
  644. OutputMemberAccessModifier (attributes);
  645. OutputVTableModifier (attributes);
  646. OutputFieldScopeModifier (attributes);
  647. OutputTypeNamePair (field.Type, field.Name);
  648. }
  649. CodeExpression initExpression = field.InitExpression;
  650. if (initExpression != null) {
  651. output.Write (" = ");
  652. GenerateExpression (initExpression);
  653. }
  654. output.WriteLine();
  655. }
  656. protected override void GenerateSnippetMember (CodeSnippetTypeMember member)
  657. {
  658. Output.Write (member.Text);
  659. }
  660. protected override void GenerateEntryPointMethod (CodeEntryPointMethod method, CodeTypeDeclaration declaration)
  661. {
  662. #if NET_2_0
  663. OutputAttributes (method.CustomAttributes, null,
  664. LineHandling.ContinueLine);
  665. #endif
  666. Output.WriteLine ("Public Shared Sub Main()");
  667. Indent++;
  668. GenerateStatements (method.Statements);
  669. Indent--;
  670. Output.WriteLine ("End Sub");
  671. }
  672. [MonoTODO ("partially implemented")]
  673. protected override void GenerateMethod (CodeMemberMethod method, CodeTypeDeclaration declaration)
  674. {
  675. if (IsCurrentDelegate || IsCurrentEnum)
  676. return;
  677. bool isSub = method.ReturnType.BaseType == typeof(void).FullName;
  678. TextWriter output = Output;
  679. OutputAttributes (method.CustomAttributes, null,
  680. LineHandling.ContinueLine);
  681. MemberAttributes attributes = method.Attributes;
  682. if (!IsCurrentInterface) {
  683. if (method.PrivateImplementationType == null) {
  684. OutputMemberAccessModifier (attributes);
  685. if (IsOverloaded (method, declaration)) {
  686. output.Write ("Overloads ");
  687. }
  688. }
  689. OutputVTableModifier (attributes);
  690. OutputMemberScopeModifier (attributes);
  691. } else {
  692. OutputVTableModifier (attributes);
  693. }
  694. if (isSub)
  695. output.Write ("Sub ");
  696. else
  697. output.Write ("Function ");
  698. output.Write (GetMethodName(method));
  699. #if NET_2_0
  700. OutputTypeParameters (method.TypeParameters);
  701. #endif
  702. output.Write ('(');
  703. OutputParameters (method.Parameters);
  704. output.Write (')');
  705. if (!isSub) {
  706. output.Write (" As ");
  707. OutputAttributes (method.ReturnTypeCustomAttributes, null,
  708. LineHandling.InLine);
  709. OutputType (method.ReturnType);
  710. }
  711. if (method.ImplementationTypes.Count > 0) {
  712. OutputImplementationTypes (method.ImplementationTypes, method.Name);
  713. } else if (method.PrivateImplementationType != null) {
  714. output.Write (" Implements ");
  715. OutputType (method.PrivateImplementationType);
  716. output.Write ('.');
  717. output.Write (method.Name);
  718. }
  719. output.WriteLine ();
  720. if (!IsCurrentInterface) {
  721. if ((attributes & MemberAttributes.ScopeMask) != MemberAttributes.Abstract) {
  722. ++Indent;
  723. GenerateStatements (method.Statements);
  724. --Indent;
  725. if (isSub)
  726. output.WriteLine ("End Sub");
  727. else
  728. output.WriteLine ("End Function");
  729. }
  730. }
  731. }
  732. protected override void GenerateProperty (CodeMemberProperty property, CodeTypeDeclaration declaration)
  733. {
  734. if (IsCurrentDelegate || IsCurrentEnum)
  735. return;
  736. TextWriter output = Output;
  737. OutputAttributes (property.CustomAttributes, null,
  738. LineHandling.ContinueLine);
  739. MemberAttributes attributes = property.Attributes;
  740. if (!IsCurrentInterface) {
  741. if (property.PrivateImplementationType == null) {
  742. OutputMemberAccessModifier (attributes);
  743. if (IsOverloaded (property, declaration)) {
  744. output.Write ("Overloads ");
  745. }
  746. }
  747. OutputVTableModifier (attributes);
  748. OutputMemberScopeModifier (attributes);
  749. } else {
  750. OutputVTableModifier (attributes);
  751. }
  752. // mark property as default property if we're dealing with an indexer
  753. if (string.Compare (GetPropertyName(property), "Item", true, CultureInfo.InvariantCulture) == 0 && property.Parameters.Count > 0) {
  754. output.Write ("Default ");
  755. }
  756. if (property.HasGet && (!property.HasSet))
  757. output.Write ("ReadOnly " );
  758. if (property.HasSet && (!property.HasGet))
  759. output.Write ("WriteOnly " );
  760. output.Write ("Property ");
  761. Output.Write (GetPropertyName (property));
  762. #if NET_2_0
  763. // in .NET 2.0, always output parantheses (whether or not there
  764. // are any parameters to output
  765. Output.Write ('(');
  766. OutputParameters (property.Parameters);
  767. Output.Write (')');
  768. #else
  769. if (property.Parameters.Count > 0) {
  770. Output.Write ('(');
  771. OutputParameters (property.Parameters);
  772. Output.Write (')');
  773. }
  774. #endif
  775. Output.Write (" As ");
  776. Output.Write (GetTypeOutput(property.Type));
  777. if (property.ImplementationTypes.Count > 0) {
  778. OutputImplementationTypes (property.ImplementationTypes, property.Name);
  779. } else if (property.PrivateImplementationType != null) {
  780. output.Write (" Implements ");
  781. OutputType (property.PrivateImplementationType);
  782. output.Write ('.');
  783. output.Write (property.Name);
  784. }
  785. output.WriteLine ();
  786. if (!IsCurrentInterface) {
  787. ++Indent;
  788. if (property.HasGet) {
  789. output.WriteLine ("Get");
  790. if (!IsAbstract (property.Attributes)) {
  791. ++Indent;
  792. GenerateStatements (property.GetStatements);
  793. --Indent;
  794. output.WriteLine ("End Get");
  795. }
  796. }
  797. if (property.HasSet) {
  798. output.WriteLine ("Set");
  799. if (!IsAbstract (property.Attributes)) {
  800. ++Indent;
  801. GenerateStatements (property.SetStatements);
  802. --Indent;
  803. output.WriteLine ("End Set");
  804. }
  805. }
  806. --Indent;
  807. output.WriteLine ("End Property");
  808. }
  809. }
  810. protected override void GenerateConstructor (CodeConstructor constructor, CodeTypeDeclaration declaration)
  811. {
  812. if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface)
  813. return;
  814. OutputAttributes (constructor.CustomAttributes, null,
  815. LineHandling.ContinueLine);
  816. OutputMemberAccessModifier (constructor.Attributes);
  817. Output.Write ("Sub New(");
  818. OutputParameters (constructor.Parameters);
  819. Output.WriteLine (")");
  820. Indent++;
  821. // check if ctor passes args on to other ctor in class
  822. CodeExpressionCollection ctorArgs = constructor.ChainedConstructorArgs;
  823. if (ctorArgs.Count > 0) {
  824. Output.Write ("Me.New(");
  825. OutputExpressionList (ctorArgs);
  826. Output.WriteLine (")");
  827. } else {
  828. // check if ctor passes args on to ctor in base class
  829. ctorArgs = constructor.BaseConstructorArgs;
  830. if (ctorArgs.Count > 0) {
  831. Output.Write ("MyBase.New(");
  832. OutputExpressionList (ctorArgs);
  833. Output.WriteLine (")");
  834. #if NET_2_0
  835. } else if (IsCurrentClass) {
  836. #else
  837. } else {
  838. #endif
  839. // call default base ctor
  840. Output.WriteLine ("MyBase.New");
  841. }
  842. }
  843. GenerateStatements (constructor.Statements);
  844. Indent--;
  845. Output.WriteLine ("End Sub");
  846. }
  847. protected override void GenerateTypeConstructor (CodeTypeConstructor constructor)
  848. {
  849. if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface)
  850. return;
  851. #if NET_2_0
  852. OutputAttributes (constructor.CustomAttributes, null,
  853. LineHandling.ContinueLine);
  854. #endif
  855. Output.WriteLine ("Shared Sub New()");
  856. Indent++;
  857. GenerateStatements (constructor.Statements);
  858. Indent--;
  859. Output.WriteLine ("End Sub");
  860. }
  861. [MonoTODO ("partially implemented")]
  862. protected override void GenerateTypeStart (CodeTypeDeclaration declaration)
  863. {
  864. TextWriter output = Output;
  865. OutputAttributes (declaration.CustomAttributes, null,
  866. LineHandling.ContinueLine);
  867. TypeAttributes attributes = declaration.TypeAttributes;
  868. if (IsCurrentDelegate) {
  869. CodeTypeDelegate delegateDecl = (CodeTypeDelegate) declaration;
  870. if ((attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) {
  871. output.Write ("Public ");
  872. }
  873. bool isSub = delegateDecl.ReturnType.BaseType == typeof (void).FullName;
  874. if (isSub) {
  875. output.Write ("Delegate Sub ");
  876. } else {
  877. output.Write ("Delegate Function ");
  878. }
  879. output.Write (CreateEscapedIdentifier (delegateDecl.Name));
  880. #if NET_2_0
  881. OutputTypeParameters (delegateDecl.TypeParameters);
  882. #endif
  883. output.Write ("(");
  884. OutputParameters (delegateDecl.Parameters);
  885. Output.Write (")");
  886. if (!isSub) {
  887. Output.Write (" As ");
  888. OutputType (delegateDecl.ReturnType);
  889. }
  890. Output.WriteLine ("");
  891. } else {
  892. OutputTypeAttributes (declaration);
  893. output.Write (CreateEscapedIdentifier (declaration.Name));
  894. #if NET_2_0
  895. OutputTypeParameters (declaration.TypeParameters);
  896. #endif
  897. if (IsCurrentEnum) {
  898. if (declaration.BaseTypes.Count > 0) {
  899. output.Write (" As ");
  900. OutputType (declaration.BaseTypes[0]);
  901. }
  902. output.WriteLine ();
  903. ++Indent;
  904. } else {
  905. ++Indent;
  906. bool firstInherits = true;
  907. bool firstImplements = true;
  908. for (int i = 0; i < declaration.BaseTypes.Count; i++) {
  909. // a struct can only implement interfaces
  910. // an interface can only inherit from other interface
  911. CodeTypeReference typeRef = declaration.BaseTypes[i];
  912. if (firstInherits && !declaration.IsStruct && !typeRef.IsInterface) {
  913. output.WriteLine ();
  914. output.Write ("Inherits ");
  915. firstInherits = false;
  916. } else if (!declaration.IsInterface && firstImplements) {
  917. output.WriteLine ();
  918. output.Write ("Implements ");
  919. firstImplements = false;
  920. } else {
  921. output.Write (", ");
  922. }
  923. OutputType (typeRef);
  924. }
  925. output.WriteLine ();
  926. }
  927. }
  928. }
  929. protected override void GenerateTypeEnd (CodeTypeDeclaration declaration)
  930. {
  931. if (IsCurrentDelegate) {
  932. return;
  933. }
  934. string output = string.Empty;
  935. --Indent;
  936. if (declaration.IsStruct)
  937. output = "End Structure";
  938. if (declaration.IsInterface)
  939. output = "End Interface";
  940. if (declaration.IsEnum)
  941. output = "End Enum";
  942. if (declaration.IsClass)
  943. output = "End Class";
  944. Output.WriteLine (output);
  945. }
  946. protected override void GenerateNamespace(CodeNamespace ns)
  947. {
  948. GenerateNamespaceImports (ns);
  949. Output.WriteLine ();
  950. GenerateCommentStatements (ns.Comments);
  951. GenerateNamespaceStart (ns);
  952. GenerateTypes (ns);
  953. GenerateNamespaceEnd (ns);
  954. }
  955. protected override void GenerateNamespaceStart (CodeNamespace ns)
  956. {
  957. TextWriter output = Output;
  958. string name = ns.Name;
  959. if (name != null && name != string.Empty) {
  960. output.Write ("Namespace ");
  961. output.WriteLine (name);
  962. ++Indent;
  963. }
  964. }
  965. protected override void GenerateNamespaceEnd (CodeNamespace ns)
  966. {
  967. string name = ns.Name;
  968. if (name != null && name != string.Empty) {
  969. --Indent;
  970. Output.WriteLine ("End Namespace");
  971. }
  972. }
  973. protected override void GenerateNamespaceImport (CodeNamespaceImport import)
  974. {
  975. TextWriter output = Output;
  976. output.Write ("Imports ");
  977. output.Write (import.Namespace);
  978. output.WriteLine ();
  979. }
  980. protected override void GenerateAttributeDeclarationsStart (CodeAttributeDeclarationCollection attributes)
  981. {
  982. Output.Write ('<');
  983. }
  984. protected override void GenerateAttributeDeclarationsEnd (CodeAttributeDeclarationCollection attributes)
  985. {
  986. Output.Write (">");
  987. }
  988. private void OutputAttributes (CodeAttributeDeclarationCollection attributes, string prefix, LineHandling lineHandling) {
  989. if (attributes.Count == 0) {
  990. return;
  991. }
  992. GenerateAttributeDeclarationsStart (attributes);
  993. IEnumerator enumerator = attributes.GetEnumerator ();
  994. if (enumerator.MoveNext ()) {
  995. CodeAttributeDeclaration att = (CodeAttributeDeclaration) enumerator.Current;
  996. if (prefix != null) {
  997. Output.Write (prefix);
  998. }
  999. OutputAttributeDeclaration (att);
  1000. while (enumerator.MoveNext ()) {
  1001. Output.Write (", ");
  1002. if (lineHandling != LineHandling.InLine) {
  1003. ContinueOnNewLine ("");
  1004. Output.Write (" ");
  1005. }
  1006. att = (CodeAttributeDeclaration) enumerator.Current;
  1007. if (prefix != null) {
  1008. Output.Write (prefix);
  1009. }
  1010. OutputAttributeDeclaration (att);
  1011. }
  1012. }
  1013. GenerateAttributeDeclarationsEnd (attributes);
  1014. Output.Write (" ");
  1015. switch (lineHandling) {
  1016. case LineHandling.ContinueLine:
  1017. ContinueOnNewLine ("");
  1018. break;
  1019. case LineHandling.NewLine:
  1020. Output.WriteLine ();
  1021. break;
  1022. }
  1023. }
  1024. protected override void OutputAttributeArgument (CodeAttributeArgument argument)
  1025. {
  1026. string name = argument.Name;
  1027. if (name != null && name.Length > 0) {
  1028. Output.Write (name);
  1029. Output.Write (":=");
  1030. }
  1031. GenerateExpression (argument.Value);
  1032. }
  1033. private void OutputAttributeDeclaration (CodeAttributeDeclaration attribute)
  1034. {
  1035. Output.Write (attribute.Name.Replace ('+', '.'));
  1036. Output.Write ('(');
  1037. IEnumerator enumerator = attribute.Arguments.GetEnumerator ();
  1038. if (enumerator.MoveNext ()) {
  1039. CodeAttributeArgument argument = (CodeAttributeArgument) enumerator.Current;
  1040. OutputAttributeArgument (argument);
  1041. while (enumerator.MoveNext ()) {
  1042. Output.Write (", ");
  1043. argument = (CodeAttributeArgument) enumerator.Current;
  1044. OutputAttributeArgument (argument);
  1045. }
  1046. }
  1047. Output.Write (')');
  1048. }
  1049. protected override void OutputDirection (FieldDirection direction)
  1050. {
  1051. switch (direction) {
  1052. case FieldDirection.In:
  1053. Output.Write ("ByVal ");
  1054. break;
  1055. case FieldDirection.Out:
  1056. case FieldDirection.Ref:
  1057. Output.Write ("ByRef ");
  1058. break;
  1059. }
  1060. }
  1061. protected override void OutputFieldScopeModifier (MemberAttributes attributes)
  1062. {
  1063. switch (attributes & MemberAttributes.ScopeMask) {
  1064. case MemberAttributes.Static:
  1065. Output.Write ("Shared ");
  1066. break;
  1067. case MemberAttributes.Const:
  1068. Output.Write ("Const ");
  1069. break;
  1070. }
  1071. }
  1072. private void OutputImplementationTypes (CodeTypeReferenceCollection implementationTypes, string member)
  1073. {
  1074. IEnumerator enumerator = implementationTypes.GetEnumerator ();
  1075. if (enumerator.MoveNext ()) {
  1076. Output.Write (" Implements ");
  1077. CodeTypeReference typeReference = (CodeTypeReference) enumerator.Current;
  1078. OutputType (typeReference);
  1079. Output.Write ('.');
  1080. OutputIdentifier (member);
  1081. while (enumerator.MoveNext ()) {
  1082. Output.Write (" , ");
  1083. typeReference = (CodeTypeReference) enumerator.Current;
  1084. OutputType (typeReference);
  1085. Output.Write ('.');
  1086. OutputIdentifier (member);
  1087. }
  1088. }
  1089. }
  1090. protected override void OutputMemberAccessModifier (MemberAttributes attributes)
  1091. {
  1092. switch (attributes & MemberAttributes.AccessMask) {
  1093. case MemberAttributes.Assembly:
  1094. case MemberAttributes.FamilyAndAssembly:
  1095. Output.Write ("Friend ");
  1096. break;
  1097. case MemberAttributes.Family:
  1098. Output.Write ("Protected ");
  1099. break;
  1100. case MemberAttributes.FamilyOrAssembly:
  1101. Output.Write ("Protected Friend ");
  1102. break;
  1103. case MemberAttributes.Private:
  1104. Output.Write ("Private ");
  1105. break;
  1106. case MemberAttributes.Public:
  1107. Output.Write ("Public ");
  1108. break;
  1109. }
  1110. }
  1111. private void OutputVTableModifier (MemberAttributes attributes)
  1112. {
  1113. if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New)
  1114. Output.Write ("Shadows ");
  1115. }
  1116. protected override void OutputMemberScopeModifier (MemberAttributes attributes)
  1117. {
  1118. switch (attributes & MemberAttributes.ScopeMask) {
  1119. case MemberAttributes.Abstract:
  1120. Output.Write ("MustOverride ");
  1121. break;
  1122. case MemberAttributes.Final:
  1123. // do nothing
  1124. break;
  1125. case MemberAttributes.Static:
  1126. Output.Write ("Shared ");
  1127. break;
  1128. case MemberAttributes.Override:
  1129. Output.Write ("Overrides ");
  1130. break;
  1131. case MemberAttributes.Overloaded:
  1132. // based on http://gendotnet.com/Code%20Gen%20Articles/codedom.htm
  1133. Output.Write ("Overloads ");
  1134. MemberAttributes access_ovl = attributes & MemberAttributes.AccessMask;
  1135. if (access_ovl == MemberAttributes.Public || access_ovl == MemberAttributes.Family)
  1136. Output.Write ("Overridable ");
  1137. break;
  1138. default:
  1139. //
  1140. // FUNNY! if the scope value is
  1141. // rubbish (0 or >Const), and access
  1142. // is public, protected make it
  1143. // "virtual".
  1144. //
  1145. // i'm not sure whether this is 100%
  1146. // correct, but it seems to be MS
  1147. // behavior.
  1148. //
  1149. // On MS.NET 2.0, internal properties
  1150. // are also marked "virtual".
  1151. //
  1152. MemberAttributes access = attributes & MemberAttributes.AccessMask;
  1153. if (access == MemberAttributes.Public ||
  1154. #if NET_2_0
  1155. access == MemberAttributes.Family || access == MemberAttributes.Assembly)
  1156. #else
  1157. access == MemberAttributes.Family)
  1158. #endif
  1159. Output.Write ("Overridable ");
  1160. break;
  1161. }
  1162. }
  1163. protected override void OutputOperator (CodeBinaryOperatorType op)
  1164. {
  1165. switch (op) {
  1166. case CodeBinaryOperatorType.Add:
  1167. Output.Write ("+");
  1168. break;
  1169. case CodeBinaryOperatorType.Subtract:
  1170. Output.Write ("-");
  1171. break;
  1172. case CodeBinaryOperatorType.Multiply:
  1173. Output.Write ("*");
  1174. break;
  1175. case CodeBinaryOperatorType.Divide:
  1176. Output.Write ("/");
  1177. break;
  1178. case CodeBinaryOperatorType.Modulus:
  1179. Output.Write ("Mod");
  1180. break;
  1181. case CodeBinaryOperatorType.Assign:
  1182. Output.Write ("=");
  1183. break;
  1184. case CodeBinaryOperatorType.IdentityInequality:
  1185. Output.Write ("<>");
  1186. break;
  1187. case CodeBinaryOperatorType.IdentityEquality:
  1188. Output.Write ("Is");
  1189. break;
  1190. case CodeBinaryOperatorType.ValueEquality:
  1191. Output.Write ("=");
  1192. break;
  1193. case CodeBinaryOperatorType.BitwiseOr:
  1194. Output.Write ("Or");
  1195. break;
  1196. case CodeBinaryOperatorType.BitwiseAnd:
  1197. Output.Write ("And");
  1198. break;
  1199. case CodeBinaryOperatorType.BooleanOr:
  1200. Output.Write ("OrElse");
  1201. break;
  1202. case CodeBinaryOperatorType.BooleanAnd:
  1203. Output.Write ("AndAlso");
  1204. break;
  1205. case CodeBinaryOperatorType.LessThan:
  1206. Output.Write ("<");
  1207. break;
  1208. case CodeBinaryOperatorType.LessThanOrEqual:
  1209. Output.Write ("<=");
  1210. break;
  1211. case CodeBinaryOperatorType.GreaterThan:
  1212. Output.Write (">");
  1213. break;
  1214. case CodeBinaryOperatorType.GreaterThanOrEqual:
  1215. Output.Write (">=");
  1216. break;
  1217. }
  1218. }
  1219. private void OutputTypeAttributes (CodeTypeDeclaration declaration)
  1220. {
  1221. TextWriter output = Output;
  1222. TypeAttributes attributes = declaration.TypeAttributes;
  1223. #if NET_2_0
  1224. if (declaration.IsPartial)
  1225. output.Write ("Partial ");
  1226. #endif
  1227. switch (attributes & TypeAttributes.VisibilityMask) {
  1228. case TypeAttributes.Public:
  1229. case TypeAttributes.NestedPublic:
  1230. output.Write ("Public ");
  1231. break;
  1232. case TypeAttributes.NestedPrivate:
  1233. output.Write ("Private ");
  1234. break;
  1235. #if NET_2_0
  1236. case TypeAttributes.NotPublic:
  1237. case TypeAttributes.NestedFamANDAssem:
  1238. case TypeAttributes.NestedAssembly:
  1239. output.Write ("Friend ");
  1240. break;
  1241. case TypeAttributes.NestedFamily:
  1242. output.Write ("Protected ");
  1243. break;
  1244. case TypeAttributes.NestedFamORAssem:
  1245. output.Write ("Protected Friend ");
  1246. break;
  1247. #endif
  1248. }
  1249. if (declaration.IsStruct) {
  1250. output.Write ("Structure ");
  1251. } else if (declaration.IsEnum) {
  1252. output.Write ("Enum ");
  1253. } else {
  1254. if ((attributes & TypeAttributes.Interface) != 0) {
  1255. output.Write ("Interface ");
  1256. } else {
  1257. if ((attributes & TypeAttributes.Sealed) != 0)
  1258. output.Write ("NotInheritable ");
  1259. if ((attributes & TypeAttributes.Abstract) != 0)
  1260. output.Write ("MustInherit ");
  1261. output.Write ("Class ");
  1262. }
  1263. }
  1264. }
  1265. #if NET_2_0
  1266. void OutputTypeParameters (CodeTypeParameterCollection parameters)
  1267. {
  1268. int count = parameters.Count;
  1269. if (count == 0)
  1270. return;
  1271. Output.Write ("(Of ");
  1272. for (int i = 0; i < count; ++i) {
  1273. if (i > 0)
  1274. Output.Write (", ");
  1275. CodeTypeParameter p = parameters [i];
  1276. Output.Write (p.Name);
  1277. OutputTypeParameterConstraints (p);
  1278. }
  1279. Output.Write (')');
  1280. }
  1281. void OutputTypeParameterConstraints (CodeTypeParameter parameter)
  1282. {
  1283. int constraint_count = parameter.Constraints.Count +
  1284. (parameter.HasConstructorConstraint ? 1 : 0);
  1285. if (constraint_count == 0)
  1286. return;
  1287. Output.Write (" As ");
  1288. if (constraint_count > 1)
  1289. Output.Write (" {");
  1290. for (int i = 0; i < parameter.Constraints.Count; i++) {
  1291. if (i > 0)
  1292. Output.Write (", ");
  1293. OutputType (parameter.Constraints [i]);
  1294. }
  1295. if (parameter.HasConstructorConstraint) {
  1296. if (constraint_count > 1)
  1297. Output.Write (", ");
  1298. Output.Write ("New");
  1299. }
  1300. if (constraint_count > 1)
  1301. Output.Write ("}");
  1302. }
  1303. #endif
  1304. protected override void OutputTypeNamePair (CodeTypeReference typeRef, String name)
  1305. {
  1306. #if NET_2_0
  1307. if (name.Length == 0)
  1308. name = "__exception";
  1309. #endif
  1310. Output.Write (CreateEscapedIdentifier(name) + " As " + GetTypeOutput (typeRef));
  1311. }
  1312. protected override void OutputType (CodeTypeReference type)
  1313. {
  1314. Output.Write (GetTypeOutput (type));
  1315. }
  1316. protected override string QuoteSnippetString (string value)
  1317. {
  1318. StringBuilder mySBuilder = new StringBuilder(value.Length);
  1319. mySBuilder.Append ("\"");
  1320. bool inQuotes = true;
  1321. for (int MyCounter = 0; MyCounter < value.Length; MyCounter++) {
  1322. if (value[MyCounter] == 34) //quotation mark
  1323. {
  1324. if (!inQuotes) {
  1325. mySBuilder.Append ("&\"");
  1326. inQuotes = true;
  1327. }
  1328. mySBuilder.Append (value[MyCounter]);
  1329. mySBuilder.Append (value[MyCounter]);
  1330. }
  1331. else if (value[MyCounter] >= 32) //standard ansi/unicode characters
  1332. {
  1333. if (!inQuotes) {
  1334. mySBuilder.Append ("&\"");
  1335. inQuotes = true;
  1336. }
  1337. mySBuilder.Append (value[MyCounter]);
  1338. }
  1339. else //special chars, e.g. line break
  1340. {
  1341. if (inQuotes) {
  1342. mySBuilder.Append ("\"");
  1343. inQuotes = false;
  1344. }
  1345. mySBuilder.Append ("&Microsoft.VisualBasic.ChrW(");
  1346. mySBuilder.Append ((int)value[MyCounter]);
  1347. mySBuilder.Append (")");
  1348. }
  1349. }
  1350. if (inQuotes)
  1351. mySBuilder.Append ("\"");
  1352. return mySBuilder.ToString();
  1353. }
  1354. private void GenerateMemberReferenceExpression (CodeExpression targetObject, string memberName)
  1355. {
  1356. GenerateExpression (targetObject);
  1357. Output.Write ('.');
  1358. Output.Write (memberName);
  1359. }
  1360. /*
  1361. * ICodeGenerator
  1362. */
  1363. protected override string CreateEscapedIdentifier (string value)
  1364. {
  1365. for (int x = 0; x < Keywords.Length; x++)
  1366. if (value.ToLower().Equals (Keywords[x].ToLower()))
  1367. return "[" + value + "]";
  1368. return value;
  1369. }
  1370. protected override string CreateValidIdentifier (string value)
  1371. {
  1372. for (int x = 0; x < Keywords.Length; x++)
  1373. if (value.ToLower().Equals (Keywords[x].ToLower()))
  1374. return "_" + value;
  1375. return value;
  1376. }
  1377. protected override string GetTypeOutput (CodeTypeReference type)
  1378. {
  1379. string output;
  1380. CodeTypeReference arrayType;
  1381. arrayType = type.ArrayElementType;
  1382. if (arrayType != null)
  1383. output = GetTypeOutput (arrayType);
  1384. else {
  1385. switch (type.BaseType) {
  1386. case "System.DateTime":
  1387. output = "Date";
  1388. break;
  1389. case "System.Decimal":
  1390. output = "Decimal";
  1391. break;
  1392. case "System.Double":
  1393. output = "Double";
  1394. break;
  1395. case "System.Single":
  1396. output = "Single";
  1397. break;
  1398. case "System.Byte":
  1399. output = "Byte";
  1400. break;
  1401. case "System.Int32":
  1402. output = "Integer";
  1403. break;
  1404. case "System.Int64":
  1405. output = "Long";
  1406. break;
  1407. case "System.Int16":
  1408. output = "Short";
  1409. break;
  1410. case "System.Boolean":
  1411. output = "Boolean";
  1412. break;
  1413. case "System.Char":
  1414. output = "Char";
  1415. break;
  1416. case "System.String":
  1417. output = "String";
  1418. break;
  1419. case "System.Object":
  1420. output = "Object";
  1421. break;
  1422. #if NET_2_0
  1423. case "System.SByte":
  1424. output = "SByte";
  1425. break;
  1426. case "System.UInt16":
  1427. output = "UShort";
  1428. break;
  1429. case "System.UInt32":
  1430. output = "UInteger";
  1431. break;
  1432. case "System.UInt64":
  1433. output = "ULong";
  1434. break;
  1435. #endif
  1436. default:
  1437. output = type.BaseType.Replace('+', '.');
  1438. output = CreateEscapedIdentifier (output);
  1439. break;
  1440. }
  1441. }
  1442. int rank = type.ArrayRank;
  1443. if (rank > 0) {
  1444. output += "(";
  1445. for (--rank; rank > 0; --rank)
  1446. output += ",";
  1447. output += ")";
  1448. }
  1449. return output;
  1450. }
  1451. protected override bool IsValidIdentifier (string identifier)
  1452. {
  1453. for (int x = 0; x < Keywords.Length; x++)
  1454. if (identifier.ToLower().Equals (Keywords[x].ToLower()))
  1455. return false;
  1456. return true;
  1457. }
  1458. protected override bool Supports (GeneratorSupport supports)
  1459. {
  1460. return true;
  1461. }
  1462. private bool IsOverloaded (CodeMemberProperty property, CodeTypeDeclaration type)
  1463. {
  1464. if ((property.Attributes & MemberAttributes.Overloaded) == MemberAttributes.Overloaded) {
  1465. return true;
  1466. }
  1467. foreach (CodeTypeMember member in type.Members) {
  1468. CodeMemberProperty p = member as CodeMemberProperty;
  1469. if (p == null) {
  1470. // member is not a property
  1471. continue;
  1472. }
  1473. if (p != property && p.Name == property.Name && p.PrivateImplementationType == null)
  1474. return true;
  1475. }
  1476. return false;
  1477. }
  1478. private bool IsOverloaded (CodeMemberMethod method, CodeTypeDeclaration type)
  1479. {
  1480. if ((method.Attributes & MemberAttributes.Overloaded) == MemberAttributes.Overloaded) {
  1481. return true;
  1482. }
  1483. foreach (CodeTypeMember member in type.Members) {
  1484. CodeMemberMethod m = member as CodeMemberMethod;
  1485. if (m == null) {
  1486. // member is not a method
  1487. continue;
  1488. }
  1489. if (!(m is CodeTypeConstructor) && !(m is CodeConstructor) && m != method && m.Name == method.Name && m.PrivateImplementationType == null)
  1490. return true;
  1491. }
  1492. return false;
  1493. }
  1494. private string GetEventName (CodeMemberEvent evt)
  1495. {
  1496. #if NET_2_0
  1497. if (evt.PrivateImplementationType == null)
  1498. return evt.Name;
  1499. string baseType = evt.PrivateImplementationType.BaseType.Replace ('.', '_');
  1500. return baseType + "_" + evt.Name;
  1501. #else
  1502. return evt.Name;
  1503. #endif
  1504. }
  1505. private string GetMethodName (CodeMemberMethod method)
  1506. {
  1507. if (method.PrivateImplementationType == null)
  1508. return method.Name;
  1509. string baseType = method.PrivateImplementationType.BaseType.Replace ('.', '_');
  1510. return baseType + "_" + method.Name;
  1511. }
  1512. private string GetPropertyName (CodeMemberProperty property)
  1513. {
  1514. if (property.PrivateImplementationType == null)
  1515. return property.Name;
  1516. string baseType = property.PrivateImplementationType.BaseType.Replace ('.', '_');
  1517. return baseType + "_" + property.Name;
  1518. }
  1519. static bool IsAbstract (MemberAttributes attributes)
  1520. {
  1521. return (attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract;
  1522. }
  1523. private enum LineHandling
  1524. {
  1525. InLine,
  1526. ContinueLine,
  1527. NewLine
  1528. }
  1529. }
  1530. }