PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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