CSharpCodeGenerator.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. //
  2. // Mono.CSharp CSharpCodeProvider Class implementation
  3. //
  4. // Author:
  5. // Daniel Stodden ([email protected])
  6. // Marek Safar ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc.
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. namespace Mono.CSharp
  31. {
  32. using System;
  33. using System.CodeDom;
  34. using System.CodeDom.Compiler;
  35. using System.Globalization;
  36. using System.IO;
  37. using System.Reflection;
  38. using System.Collections;
  39. using System.Text;
  40. internal class CSharpCodeGenerator
  41. : CodeGenerator
  42. {
  43. // It is used for beautiful "for" syntax
  44. bool dont_write_semicolon;
  45. //
  46. // Constructors
  47. //
  48. public CSharpCodeGenerator()
  49. {
  50. dont_write_semicolon = false;
  51. }
  52. //
  53. // Properties
  54. //
  55. protected override string NullToken {
  56. get {
  57. return "null";
  58. }
  59. }
  60. //
  61. // Methods
  62. //
  63. protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression expression)
  64. {
  65. //
  66. // This tries to replicate MS behavior as good as
  67. // possible.
  68. //
  69. // The Code-Array stuff in ms.net seems to be broken
  70. // anyways, or I'm too stupid to understand it.
  71. //
  72. // I'm sick of it. If you try to develop array
  73. // creations, test them on windows. If it works there
  74. // but not in mono, drop me a note. I'd be especially
  75. // interested in jagged-multidimensional combinations
  76. // with proper initialization :}
  77. //
  78. TextWriter output = Output;
  79. output.Write ("new ");
  80. CodeExpressionCollection initializers = expression.Initializers;
  81. CodeTypeReference createType = expression.CreateType;
  82. if (initializers.Count > 0) {
  83. OutputType (createType);
  84. if (expression.CreateType.ArrayRank == 0) {
  85. output.Write ("[]");
  86. }
  87. OutputStartBrace ();
  88. ++Indent;
  89. OutputExpressionList (initializers, true);
  90. --Indent;
  91. output.Write ("}");
  92. } else {
  93. CodeTypeReference arrayType = createType.ArrayElementType;
  94. while (arrayType != null) {
  95. createType = arrayType;
  96. arrayType = arrayType.ArrayElementType;
  97. }
  98. OutputType (createType);
  99. output.Write ('[');
  100. CodeExpression size = expression.SizeExpression;
  101. if (size != null)
  102. GenerateExpression (size);
  103. else
  104. output.Write (expression.Size);
  105. output.Write(']');
  106. }
  107. }
  108. protected override void GenerateBaseReferenceExpression (CodeBaseReferenceExpression expression)
  109. {
  110. Output.Write ("base");
  111. }
  112. protected override void GenerateCastExpression (CodeCastExpression expression)
  113. {
  114. TextWriter output = Output;
  115. output.Write ("((");
  116. OutputType (expression.TargetType);
  117. output.Write (")(");
  118. GenerateExpression (expression.Expression);
  119. output.Write ("))");
  120. }
  121. protected override void GenerateCompileUnitStart (CodeCompileUnit compileUnit)
  122. {
  123. GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
  124. GenerateComment (new CodeComment (" <autogenerated>"));
  125. GenerateComment (new CodeComment (" This code was generated by a tool."));
  126. GenerateComment (new CodeComment (" Mono Runtime Version: " + System.Environment.Version));
  127. GenerateComment (new CodeComment (""));
  128. GenerateComment (new CodeComment (" Changes to this file may cause incorrect behavior and will be lost if "));
  129. GenerateComment (new CodeComment (" the code is regenerated."));
  130. GenerateComment (new CodeComment (" </autogenerated>"));
  131. GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
  132. Output.WriteLine ();
  133. base.GenerateCompileUnitStart (compileUnit);
  134. }
  135. protected override void GenerateCompileUnit (CodeCompileUnit compileUnit)
  136. {
  137. GenerateCompileUnitStart (compileUnit);
  138. if (compileUnit.AssemblyCustomAttributes.Count > 0) {
  139. OutputAttributes (compileUnit.AssemblyCustomAttributes,
  140. "assembly: ", false);
  141. Output.WriteLine ("");
  142. }
  143. foreach (CodeNamespace ns in compileUnit.Namespaces)
  144. GenerateNamespace (ns);
  145. GenerateCompileUnitEnd (compileUnit);
  146. }
  147. protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression expression)
  148. {
  149. TextWriter output = Output;
  150. output.Write ("new ");
  151. OutputType (expression.DelegateType);
  152. output.Write ('(');
  153. CodeExpression targetObject = expression.TargetObject;
  154. if (targetObject != null) {
  155. GenerateExpression (targetObject);
  156. Output.Write ('.');
  157. }
  158. output.Write (GetSafeName (expression.MethodName));
  159. output.Write (')');
  160. }
  161. protected override void GenerateFieldReferenceExpression (CodeFieldReferenceExpression expression)
  162. {
  163. CodeExpression targetObject = expression.TargetObject;
  164. if (targetObject != null) {
  165. GenerateExpression (targetObject);
  166. Output.Write ('.');
  167. }
  168. Output.Write (GetSafeName (expression.FieldName));
  169. }
  170. protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression expression)
  171. {
  172. Output.Write (GetSafeName (expression.ParameterName));
  173. }
  174. protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression expression)
  175. {
  176. Output.Write (GetSafeName (expression.VariableName));
  177. }
  178. protected override void GenerateIndexerExpression (CodeIndexerExpression expression)
  179. {
  180. TextWriter output = Output;
  181. GenerateExpression (expression.TargetObject);
  182. output.Write ('[');
  183. OutputExpressionList (expression.Indices);
  184. output.Write (']');
  185. }
  186. protected override void GenerateArrayIndexerExpression (CodeArrayIndexerExpression expression)
  187. {
  188. TextWriter output = Output;
  189. GenerateExpression (expression.TargetObject);
  190. output.Write ('[');
  191. OutputExpressionList (expression.Indices);
  192. output.Write (']');
  193. }
  194. protected override void GenerateSnippetExpression (CodeSnippetExpression expression)
  195. {
  196. Output.Write (expression.Value);
  197. }
  198. protected override void GenerateMethodInvokeExpression (CodeMethodInvokeExpression expression)
  199. {
  200. TextWriter output = Output;
  201. GenerateMethodReferenceExpression (expression.Method);
  202. output.Write ('(');
  203. OutputExpressionList (expression.Parameters);
  204. output.Write (')');
  205. }
  206. protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression expression)
  207. {
  208. if (expression.TargetObject != null)
  209. {
  210. GenerateExpression (expression.TargetObject);
  211. Output.Write ('.');
  212. };
  213. Output.Write (GetSafeName (expression.MethodName));
  214. #if NET_2_0
  215. if (expression.TypeArguments.Count > 0)
  216. Output.Write (GetTypeArguments (expression.TypeArguments));
  217. #endif
  218. }
  219. protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression expression)
  220. {
  221. if (expression.TargetObject != null) {
  222. GenerateExpression (expression.TargetObject);
  223. Output.Write ('.');
  224. }
  225. Output.Write (GetSafeName (expression.EventName));
  226. }
  227. protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression expression)
  228. {
  229. GenerateExpression (expression.TargetObject);
  230. Output.Write ('(');
  231. OutputExpressionList (expression.Parameters);
  232. Output.Write (')');
  233. }
  234. protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression expression)
  235. {
  236. Output.Write ("new ");
  237. OutputType (expression.CreateType);
  238. Output.Write ('(');
  239. OutputExpressionList (expression.Parameters);
  240. Output.Write (')');
  241. }
  242. protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression expression)
  243. {
  244. CodeExpression targetObject = expression.TargetObject;
  245. if (targetObject != null) {
  246. GenerateExpression (targetObject);
  247. Output.Write ('.');
  248. }
  249. Output.Write (GetSafeName (expression.PropertyName ));
  250. }
  251. protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression expression)
  252. {
  253. Output.Write ("value");
  254. }
  255. protected override void GenerateThisReferenceExpression (CodeThisReferenceExpression expression)
  256. {
  257. Output.Write ("this");
  258. }
  259. protected override void GenerateExpressionStatement (CodeExpressionStatement statement)
  260. {
  261. GenerateExpression (statement.Expression);
  262. if (dont_write_semicolon)
  263. return;
  264. Output.WriteLine(';');
  265. }
  266. protected override void GenerateIterationStatement (CodeIterationStatement statement)
  267. {
  268. TextWriter output = Output;
  269. dont_write_semicolon = true;
  270. output.Write ("for (");
  271. GenerateStatement (statement.InitStatement);
  272. output.Write ("; ");
  273. GenerateExpression (statement.TestExpression);
  274. output.Write ("; ");
  275. GenerateStatement (statement.IncrementStatement);
  276. output.Write (")");
  277. dont_write_semicolon = false;
  278. OutputStartBrace ();
  279. ++Indent;
  280. GenerateStatements (statement.Statements);
  281. --Indent;
  282. output.WriteLine ('}');
  283. }
  284. protected override void GenerateThrowExceptionStatement (CodeThrowExceptionStatement statement)
  285. {
  286. Output.Write ("throw");
  287. if (statement.ToThrow != null) {
  288. Output.Write (' ');
  289. GenerateExpression (statement.ToThrow);
  290. }
  291. Output.WriteLine (";");
  292. }
  293. protected override void GenerateComment (CodeComment comment)
  294. {
  295. TextWriter output = Output;
  296. string commentChars = null;
  297. if (comment.DocComment) {
  298. commentChars = "///";
  299. } else {
  300. commentChars = "//";
  301. }
  302. output.Write (commentChars);
  303. output.Write (' ');
  304. string text = comment.Text;
  305. for (int i = 0; i < text.Length; i++) {
  306. output.Write (text[i]);
  307. if (text[i] == '\r') {
  308. if (i < (text.Length - 1) && text[i + 1] == '\n') {
  309. continue;
  310. }
  311. output.Write (commentChars);
  312. } else if (text[i] == '\n') {
  313. output.Write (commentChars);
  314. }
  315. }
  316. output.WriteLine ();
  317. }
  318. protected override void GenerateMethodReturnStatement (CodeMethodReturnStatement statement)
  319. {
  320. TextWriter output = Output;
  321. if (statement.Expression != null) {
  322. output.Write ("return ");
  323. GenerateExpression (statement.Expression);
  324. output.WriteLine (";");
  325. } else {
  326. output.WriteLine ("return;");
  327. }
  328. }
  329. protected override void GenerateConditionStatement (CodeConditionStatement statement)
  330. {
  331. TextWriter output = Output;
  332. output.Write ("if (");
  333. GenerateExpression (statement.Condition);
  334. output.Write (")");
  335. OutputStartBrace ();
  336. ++Indent;
  337. GenerateStatements (statement.TrueStatements);
  338. --Indent;
  339. CodeStatementCollection falses = statement.FalseStatements;
  340. if (falses.Count > 0) {
  341. output.Write ('}');
  342. if (Options.ElseOnClosing)
  343. output.Write (' ');
  344. else
  345. output.WriteLine ();
  346. output.Write ("else");
  347. OutputStartBrace ();
  348. ++Indent;
  349. GenerateStatements (falses);
  350. --Indent;
  351. }
  352. output.WriteLine ('}');
  353. }
  354. protected override void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement statement)
  355. {
  356. TextWriter output = Output;
  357. CodeGeneratorOptions options = Options;
  358. output.Write ("try");
  359. OutputStartBrace ();
  360. ++Indent;
  361. GenerateStatements (statement.TryStatements);
  362. --Indent;
  363. foreach (CodeCatchClause clause in statement.CatchClauses) {
  364. output.Write ('}');
  365. if (options.ElseOnClosing)
  366. output.Write (' ');
  367. else
  368. output.WriteLine ();
  369. output.Write ("catch (");
  370. OutputTypeNamePair (clause.CatchExceptionType, GetSafeName(clause.LocalName));
  371. output.Write (")");
  372. OutputStartBrace ();
  373. ++Indent;
  374. GenerateStatements (clause.Statements);
  375. --Indent;
  376. }
  377. CodeStatementCollection finallies = statement.FinallyStatements;
  378. if (finallies.Count > 0) {
  379. output.Write ('}');
  380. if (options.ElseOnClosing)
  381. output.Write (' ');
  382. else
  383. output.WriteLine ();
  384. output.Write ("finally");
  385. OutputStartBrace ();
  386. ++Indent;
  387. GenerateStatements (finallies);
  388. --Indent;
  389. }
  390. output.WriteLine('}');
  391. }
  392. protected override void GenerateAssignStatement (CodeAssignStatement statement)
  393. {
  394. TextWriter output = Output;
  395. GenerateExpression (statement.Left);
  396. output.Write (" = ");
  397. GenerateExpression (statement.Right);
  398. if (dont_write_semicolon)
  399. return;
  400. output.WriteLine (';');
  401. }
  402. protected override void GenerateAttachEventStatement (CodeAttachEventStatement statement)
  403. {
  404. TextWriter output = Output;
  405. GenerateEventReferenceExpression (statement.Event);
  406. output.Write (" += ");
  407. GenerateExpression (statement.Listener);
  408. output.WriteLine (';');
  409. }
  410. protected override void GenerateRemoveEventStatement (CodeRemoveEventStatement statement)
  411. {
  412. TextWriter output = Output;
  413. GenerateEventReferenceExpression (statement.Event);
  414. output.Write (" -= ");
  415. GenerateExpression (statement.Listener);
  416. output.WriteLine (';');
  417. }
  418. protected override void GenerateGotoStatement (CodeGotoStatement statement)
  419. {
  420. TextWriter output = Output;
  421. output.Write ("goto ");
  422. output.Write (GetSafeName (statement.Label));
  423. output.WriteLine (";");
  424. }
  425. protected override void GenerateLabeledStatement (CodeLabeledStatement statement)
  426. {
  427. Indent--;
  428. Output.Write (statement.Label);
  429. Output.WriteLine (":");
  430. Indent++;
  431. if (statement.Statement != null) {
  432. GenerateStatement (statement.Statement);
  433. }
  434. }
  435. protected override void GenerateVariableDeclarationStatement (CodeVariableDeclarationStatement statement)
  436. {
  437. TextWriter output = Output;
  438. OutputTypeNamePair (statement.Type, GetSafeName (statement.Name));
  439. CodeExpression initExpression = statement.InitExpression;
  440. if (initExpression != null) {
  441. output.Write (" = ");
  442. GenerateExpression (initExpression);
  443. }
  444. if (!dont_write_semicolon) {
  445. output.WriteLine (';');
  446. }
  447. }
  448. protected override void GenerateLinePragmaStart (CodeLinePragma linePragma)
  449. {
  450. Output.WriteLine ();
  451. Output.Write ("#line ");
  452. Output.Write (linePragma.LineNumber);
  453. Output.Write (" \"");
  454. Output.Write (linePragma.FileName);
  455. Output.Write ("\"");
  456. Output.WriteLine ();
  457. }
  458. protected override void GenerateLinePragmaEnd (CodeLinePragma linePragma)
  459. {
  460. Output.WriteLine ();
  461. Output.WriteLine ("#line default");
  462. }
  463. protected override void GenerateEvent (CodeMemberEvent eventRef, CodeTypeDeclaration declaration)
  464. {
  465. if (IsCurrentDelegate || IsCurrentEnum) {
  466. return;
  467. }
  468. OutputAttributes (eventRef.CustomAttributes, null, false);
  469. if (eventRef.PrivateImplementationType == null) {
  470. OutputMemberAccessModifier (eventRef.Attributes);
  471. }
  472. Output.Write ("event ");
  473. if (eventRef.PrivateImplementationType != null) {
  474. OutputTypeNamePair (eventRef.Type,
  475. eventRef.PrivateImplementationType.BaseType + "." +
  476. eventRef.Name);
  477. } else {
  478. OutputTypeNamePair (eventRef.Type, GetSafeName (eventRef.Name));
  479. }
  480. Output.WriteLine (';');
  481. }
  482. protected override void GenerateField (CodeMemberField field)
  483. {
  484. if (IsCurrentDelegate || IsCurrentInterface) {
  485. return;
  486. }
  487. TextWriter output = Output;
  488. OutputAttributes (field.CustomAttributes, null, false);
  489. if (IsCurrentEnum) {
  490. Output.Write (GetSafeName (field.Name));
  491. } else {
  492. MemberAttributes attributes = field.Attributes;
  493. OutputMemberAccessModifier (attributes);
  494. OutputVTableModifier (attributes);
  495. OutputFieldScopeModifier (attributes);
  496. OutputTypeNamePair (field.Type, GetSafeName (field.Name));
  497. }
  498. CodeExpression initExpression = field.InitExpression;
  499. if (initExpression != null) {
  500. output.Write (" = ");
  501. GenerateExpression (initExpression);
  502. }
  503. if (IsCurrentEnum)
  504. output.WriteLine (',');
  505. else
  506. output.WriteLine (';');
  507. }
  508. protected override void GenerateSnippetMember (CodeSnippetTypeMember member)
  509. {
  510. Output.Write (member.Text);
  511. }
  512. protected override void GenerateEntryPointMethod (CodeEntryPointMethod method,
  513. CodeTypeDeclaration declaration)
  514. {
  515. #if NET_2_0
  516. OutputAttributes (method.CustomAttributes, null, false);
  517. #endif
  518. Output.Write ("public static ");
  519. #if NET_2_0
  520. OutputType (method.ReturnType);
  521. #else
  522. Output.Write ("void");
  523. #endif
  524. Output.Write (" Main()");
  525. OutputStartBrace ();
  526. Indent++;
  527. GenerateStatements (method.Statements);
  528. Indent--;
  529. Output.WriteLine ("}");
  530. }
  531. protected override void GenerateMethod (CodeMemberMethod method,
  532. CodeTypeDeclaration declaration)
  533. {
  534. if (IsCurrentDelegate || IsCurrentEnum) {
  535. return;
  536. }
  537. TextWriter output = Output;
  538. OutputAttributes (method.CustomAttributes, null, false);
  539. OutputAttributes (method.ReturnTypeCustomAttributes,
  540. "return: ", false);
  541. MemberAttributes attributes = method.Attributes;
  542. if (!IsCurrentInterface) {
  543. if (method.PrivateImplementationType == null) {
  544. OutputMemberAccessModifier (attributes);
  545. OutputVTableModifier (attributes);
  546. OutputMemberScopeModifier (attributes);
  547. }
  548. } else {
  549. OutputVTableModifier (attributes);
  550. }
  551. OutputType (method.ReturnType);
  552. output.Write (' ');
  553. CodeTypeReference privateType = method.PrivateImplementationType;
  554. if (privateType != null) {
  555. output.Write (privateType.BaseType);
  556. output.Write ('.');
  557. }
  558. output.Write (GetSafeName (method.Name));
  559. #if NET_2_0
  560. GenerateGenericsParameters (method.TypeParameters);
  561. #endif
  562. output.Write ('(');
  563. OutputParameters (method.Parameters);
  564. output.Write (')');
  565. #if NET_2_0
  566. GenerateGenericsConstraints (method.TypeParameters);
  567. #endif
  568. if ((attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract || declaration.IsInterface)
  569. output.WriteLine (';');
  570. else {
  571. OutputStartBrace ();
  572. ++Indent;
  573. GenerateStatements (method.Statements);
  574. --Indent;
  575. output.WriteLine ('}');
  576. }
  577. }
  578. protected override void GenerateProperty (CodeMemberProperty property,
  579. CodeTypeDeclaration declaration)
  580. {
  581. if (IsCurrentDelegate || IsCurrentEnum) {
  582. return;
  583. }
  584. TextWriter output = Output;
  585. OutputAttributes (property.CustomAttributes, null, false);
  586. MemberAttributes attributes = property.Attributes;
  587. if (!IsCurrentInterface) {
  588. if (property.PrivateImplementationType == null) {
  589. OutputMemberAccessModifier (attributes);
  590. OutputVTableModifier (attributes);
  591. OutputMemberScopeModifier (attributes);
  592. }
  593. } else {
  594. OutputVTableModifier (attributes);
  595. }
  596. OutputType (property.Type);
  597. output.Write (' ');
  598. if (!IsCurrentInterface && property.PrivateImplementationType != null) {
  599. output.Write (property.PrivateImplementationType.BaseType);
  600. output.Write ('.');
  601. }
  602. // only consider property indexer if name is Item (case-insensitive
  603. // comparison) AND property has parameters
  604. if (string.Compare(property.Name, "Item", true, CultureInfo.InvariantCulture) == 0 && property.Parameters.Count > 0) {
  605. output.Write ("this[");
  606. OutputParameters(property.Parameters);
  607. output.Write(']');
  608. } else {
  609. output.Write (property.Name);
  610. }
  611. OutputStartBrace ();
  612. ++Indent;
  613. if (declaration.IsInterface)
  614. {
  615. if (property.HasGet) output.WriteLine("get;");
  616. if (property.HasSet) output.WriteLine("set;");
  617. }
  618. else
  619. {
  620. if (property.HasGet)
  621. {
  622. output.Write ("get");
  623. OutputStartBrace ();
  624. ++Indent;
  625. GenerateStatements (property.GetStatements);
  626. --Indent;
  627. output.WriteLine ('}');
  628. }
  629. if (property.HasSet)
  630. {
  631. output.Write ("set");
  632. OutputStartBrace ();
  633. ++Indent;
  634. GenerateStatements (property.SetStatements);
  635. --Indent;
  636. output.WriteLine ('}');
  637. }
  638. }
  639. --Indent;
  640. output.WriteLine ('}');
  641. }
  642. protected override void GenerateConstructor (CodeConstructor constructor, CodeTypeDeclaration declaration)
  643. {
  644. if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface) {
  645. return;
  646. }
  647. OutputAttributes (constructor.CustomAttributes, null, false);
  648. OutputMemberAccessModifier (constructor.Attributes);
  649. Output.Write (GetSafeName (CurrentTypeName) + "(");
  650. OutputParameters (constructor.Parameters);
  651. Output.Write (")");
  652. if (constructor.BaseConstructorArgs.Count > 0) {
  653. Output.WriteLine (" : ");
  654. Indent += 2;
  655. Output.Write ("base(");
  656. OutputExpressionList (constructor.BaseConstructorArgs);
  657. Output.Write (')');
  658. Indent -= 2;
  659. }
  660. if (constructor.ChainedConstructorArgs.Count > 0) {
  661. Output.WriteLine (" : ");
  662. Indent += 2;
  663. Output.Write("this(");
  664. OutputExpressionList (constructor.ChainedConstructorArgs);
  665. Output.Write(')');
  666. Indent -= 2;
  667. }
  668. OutputStartBrace ();
  669. Indent++;
  670. GenerateStatements (constructor.Statements);
  671. Indent--;
  672. Output.WriteLine ('}');
  673. }
  674. protected override void GenerateTypeConstructor (CodeTypeConstructor constructor)
  675. {
  676. if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface) {
  677. return;
  678. }
  679. #if NET_2_0
  680. OutputAttributes (constructor.CustomAttributes, null, false);
  681. #endif
  682. Output.Write ("static " + GetSafeName (CurrentTypeName) + "()");
  683. OutputStartBrace ();
  684. Indent++;
  685. GenerateStatements (constructor.Statements);
  686. Indent--;
  687. Output.WriteLine ('}');
  688. }
  689. protected override void GenerateTypeStart(CodeTypeDeclaration declaration)
  690. {
  691. TextWriter output = Output;
  692. OutputAttributes (declaration.CustomAttributes, null, false);
  693. if (!IsCurrentDelegate) {
  694. OutputTypeAttributes (declaration);
  695. output.Write (GetSafeName (declaration.Name));
  696. #if NET_2_0
  697. GenerateGenericsParameters (declaration.TypeParameters);
  698. #endif
  699. IEnumerator enumerator = declaration.BaseTypes.GetEnumerator ();
  700. if (enumerator.MoveNext ()) {
  701. CodeTypeReference type = (CodeTypeReference) enumerator.Current;
  702. output.Write (" : ");
  703. OutputType (type);
  704. while (enumerator.MoveNext ()) {
  705. type = (CodeTypeReference) enumerator.Current;
  706. output.Write (", ");
  707. OutputType (type);
  708. }
  709. }
  710. #if NET_2_0
  711. GenerateGenericsConstraints (declaration.TypeParameters);
  712. #endif
  713. OutputStartBrace ();
  714. ++Indent;
  715. } else {
  716. if ((declaration.TypeAttributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) {
  717. output.Write ("public ");
  718. }
  719. CodeTypeDelegate delegateDecl = (CodeTypeDelegate) declaration;
  720. output.Write ("delegate ");
  721. OutputType (delegateDecl.ReturnType);
  722. output.Write (" ");
  723. output.Write (GetSafeName (declaration.Name));
  724. output.Write ("(");
  725. OutputParameters (delegateDecl.Parameters);
  726. output.WriteLine (");");
  727. }
  728. }
  729. protected override void GenerateTypeEnd (CodeTypeDeclaration declaration)
  730. {
  731. if (!IsCurrentDelegate) {
  732. --Indent;
  733. Output.WriteLine ("}");
  734. }
  735. }
  736. protected override void GenerateNamespaceStart (CodeNamespace ns)
  737. {
  738. TextWriter output = Output;
  739. string name = ns.Name;
  740. if (name != null && name.Length != 0) {
  741. output.Write ("namespace ");
  742. output.Write (GetSafeName (name));
  743. OutputStartBrace ();
  744. ++Indent;
  745. }
  746. }
  747. protected override void GenerateNamespaceEnd (CodeNamespace ns)
  748. {
  749. string name = ns.Name;
  750. if (name != null && name.Length != 0) {
  751. --Indent;
  752. Output.WriteLine ("}");
  753. }
  754. }
  755. protected override void GenerateNamespaceImport (CodeNamespaceImport import)
  756. {
  757. TextWriter output = Output;
  758. output.Write ("using ");
  759. output.Write (GetSafeName (import.Namespace));
  760. output.WriteLine (';');
  761. }
  762. protected override void GenerateAttributeDeclarationsStart (CodeAttributeDeclarationCollection attributes)
  763. {
  764. Output.Write ('[');
  765. }
  766. protected override void GenerateAttributeDeclarationsEnd (CodeAttributeDeclarationCollection attributes)
  767. {
  768. Output.Write (']');
  769. }
  770. private void OutputStartBrace ()
  771. {
  772. if (Options.BracingStyle == "C") {
  773. Output.WriteLine ("");
  774. Output.WriteLine ("{");
  775. } else {
  776. Output.WriteLine (" {");
  777. }
  778. }
  779. private void OutputAttributes (CodeAttributeDeclarationCollection attributes, string prefix, bool inline)
  780. {
  781. foreach (CodeAttributeDeclaration att in attributes) {
  782. GenerateAttributeDeclarationsStart (attributes);
  783. if (prefix != null) {
  784. Output.Write (prefix);
  785. }
  786. OutputAttributeDeclaration (att);
  787. GenerateAttributeDeclarationsEnd (attributes);
  788. if (inline) {
  789. Output.Write (" ");
  790. } else {
  791. Output.WriteLine ();
  792. }
  793. }
  794. }
  795. private void OutputAttributeDeclaration (CodeAttributeDeclaration attribute)
  796. {
  797. Output.Write (attribute.Name.Replace ('+', '.'));
  798. Output.Write ('(');
  799. IEnumerator enumerator = attribute.Arguments.GetEnumerator ();
  800. if (enumerator.MoveNext ()) {
  801. CodeAttributeArgument argument = (CodeAttributeArgument) enumerator.Current;
  802. OutputAttributeArgument (argument);
  803. while (enumerator.MoveNext ()) {
  804. Output.Write (", ");
  805. argument = (CodeAttributeArgument) enumerator.Current;
  806. OutputAttributeArgument (argument);
  807. }
  808. }
  809. Output.Write (')');
  810. }
  811. protected override void OutputType (CodeTypeReference type)
  812. {
  813. Output.Write (GetTypeOutput (type));
  814. }
  815. private void OutputVTableModifier (MemberAttributes attributes)
  816. {
  817. if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New) {
  818. Output.Write ("new ");
  819. }
  820. }
  821. protected override void OutputFieldScopeModifier (MemberAttributes attributes)
  822. {
  823. switch (attributes & MemberAttributes.ScopeMask) {
  824. case MemberAttributes.Static:
  825. Output.Write ("static ");
  826. break;
  827. case MemberAttributes.Const:
  828. Output.Write ("const ");
  829. break;
  830. }
  831. }
  832. #if NET_2_0
  833. // Note: this method should in fact be private as in .NET 2.0, the
  834. // CSharpCodeGenerator no longer derives from CodeGenerator but we
  835. // still need to make this change.
  836. protected override void OutputMemberAccessModifier (MemberAttributes attributes)
  837. {
  838. switch (attributes & MemberAttributes.AccessMask) {
  839. case MemberAttributes.Assembly:
  840. case MemberAttributes.FamilyAndAssembly:
  841. Output.Write ("internal ");
  842. break;
  843. case MemberAttributes.Family:
  844. Output.Write ("protected ");
  845. break;
  846. case MemberAttributes.FamilyOrAssembly:
  847. Output.Write ("protected internal ");
  848. break;
  849. case MemberAttributes.Private:
  850. Output.Write ("private ");
  851. break;
  852. case MemberAttributes.Public:
  853. Output.Write ("public ");
  854. break;
  855. }
  856. }
  857. // Note: this method should in fact be private as in .NET 2.0, the
  858. // CSharpCodeGenerator no longer derives from CodeGenerator but we
  859. // still need to make this change.
  860. protected override void OutputMemberScopeModifier (MemberAttributes attributes)
  861. {
  862. switch (attributes & MemberAttributes.ScopeMask) {
  863. case MemberAttributes.Abstract:
  864. Output.Write ("abstract ");
  865. break;
  866. case MemberAttributes.Final:
  867. // do nothing
  868. break;
  869. case MemberAttributes.Static:
  870. Output.Write ("static ");
  871. break;
  872. case MemberAttributes.Override:
  873. Output.Write ("override ");
  874. break;
  875. default:
  876. MemberAttributes access = attributes & MemberAttributes.AccessMask;
  877. if (access == MemberAttributes.Assembly || access == MemberAttributes.Family || access == MemberAttributes.Public) {
  878. Output.Write ("virtual ");
  879. }
  880. break;
  881. }
  882. }
  883. #endif
  884. private void OutputTypeAttributes (CodeTypeDeclaration declaration)
  885. {
  886. TextWriter output = Output;
  887. TypeAttributes attributes = declaration.TypeAttributes;
  888. switch (attributes & TypeAttributes.VisibilityMask) {
  889. case TypeAttributes.Public:
  890. case TypeAttributes.NestedPublic:
  891. output.Write ("public ");
  892. break;
  893. case TypeAttributes.NestedPrivate:
  894. output.Write ("private ");
  895. break;
  896. #if NET_2_0
  897. case TypeAttributes.NotPublic:
  898. case TypeAttributes.NestedFamANDAssem:
  899. case TypeAttributes.NestedAssembly:
  900. output.Write ("internal ");
  901. break;
  902. case TypeAttributes.NestedFamily:
  903. output.Write ("protected ");
  904. break;
  905. case TypeAttributes.NestedFamORAssem:
  906. output.Write ("protected internal ");
  907. break;
  908. #endif
  909. }
  910. if (declaration.IsStruct) {
  911. #if NET_2_0
  912. if (declaration.IsPartial) {
  913. output.Write ("partial ");
  914. }
  915. #endif
  916. output.Write ("struct ");
  917. } else if (declaration.IsEnum) {
  918. output.Write ("enum ");
  919. } else {
  920. if ((attributes & TypeAttributes.Interface) != 0) {
  921. #if NET_2_0
  922. if (declaration.IsPartial) {
  923. output.Write ("partial ");
  924. }
  925. #endif
  926. output.Write ("interface ");
  927. } else {
  928. if ((attributes & TypeAttributes.Sealed) != 0)
  929. output.Write ("sealed ");
  930. if ((attributes & TypeAttributes.Abstract) != 0)
  931. output.Write ("abstract ");
  932. #if NET_2_0
  933. if (declaration.IsPartial) {
  934. output.Write ("partial ");
  935. }
  936. #endif
  937. output.Write ("class ");
  938. }
  939. }
  940. }
  941. [MonoTODO ("Implement missing special characters")]
  942. protected override string QuoteSnippetString (string value)
  943. {
  944. // FIXME: this is weird, but works.
  945. string output = value.Replace ("\\", "\\\\");
  946. output = output.Replace ("\"", "\\\"");
  947. output = output.Replace ("\t", "\\t");
  948. output = output.Replace ("\r", "\\r");
  949. output = output.Replace ("\n", "\\n");
  950. return "\"" + output + "\"";
  951. }
  952. protected override void GeneratePrimitiveExpression(CodePrimitiveExpression e)
  953. {
  954. if (e.Value is char) {
  955. this.GenerateCharValue ((char) e.Value);
  956. #if NET_2_0
  957. } else if (e.Value is ushort) {
  958. ushort uc = (ushort) e.Value;
  959. Output.Write (uc.ToString(CultureInfo.InvariantCulture));
  960. } else if (e.Value is uint) {
  961. uint ui = (uint) e.Value;
  962. Output.Write (ui.ToString(CultureInfo.InvariantCulture));
  963. Output.Write ("u");
  964. } else if (e.Value is ulong) {
  965. ulong ul = (ulong) e.Value;
  966. Output.Write (ul.ToString(CultureInfo.InvariantCulture));
  967. Output.Write ("ul");
  968. } else if (e.Value is sbyte) {
  969. sbyte sb = (sbyte) e.Value;
  970. Output.Write (sb.ToString(CultureInfo.InvariantCulture));
  971. #endif
  972. } else {
  973. base.GeneratePrimitiveExpression (e);
  974. }
  975. }
  976. private void GenerateCharValue (char c)
  977. {
  978. Output.Write ('\'');
  979. switch (c) {
  980. case '\0':
  981. Output.Write ("\\0");
  982. break;
  983. case '\t':
  984. Output.Write ("\\t");
  985. break;
  986. case '\n':
  987. Output.Write ("\\n");
  988. break;
  989. case '\r':
  990. Output.Write ("\\r");
  991. break;
  992. case '"':
  993. Output.Write ("\\\"");
  994. break;
  995. case '\'':
  996. Output.Write ("\\'");
  997. break;
  998. case '\\':
  999. Output.Write ("\\\\");
  1000. break;
  1001. case '\u2028':
  1002. Output.Write ("\\u");
  1003. #if NET_2_0
  1004. Output.Write (((int) c).ToString ("X4", CultureInfo.InvariantCulture));
  1005. #else
  1006. Output.Write (((int) c).ToString (CultureInfo.InvariantCulture));
  1007. #endif
  1008. break;
  1009. case '\u2029':
  1010. Output.Write ("\\u");
  1011. #if NET_2_0
  1012. Output.Write (((int) c).ToString ("X4", CultureInfo.InvariantCulture));
  1013. #else
  1014. Output.Write (((int) c).ToString (CultureInfo.InvariantCulture));
  1015. #endif
  1016. break;
  1017. default:
  1018. Output.Write (c);
  1019. break;
  1020. }
  1021. Output.Write ('\'');
  1022. }
  1023. protected override void GenerateSingleFloatValue (float f)
  1024. {
  1025. base.GenerateSingleFloatValue (f);
  1026. base.Output.Write ('F');
  1027. }
  1028. protected override void GenerateDecimalValue (decimal d)
  1029. {
  1030. base.GenerateDecimalValue (d);
  1031. base.Output.Write ('m');
  1032. }
  1033. protected override void GenerateParameterDeclarationExpression (CodeParameterDeclarationExpression e)
  1034. {
  1035. OutputAttributes (e.CustomAttributes, null, true);
  1036. OutputDirection (e.Direction);
  1037. OutputType (e.Type);
  1038. Output.Write (' ');
  1039. Output.Write (GetSafeName (e.Name));
  1040. }
  1041. protected override void GenerateTypeOfExpression (CodeTypeOfExpression e)
  1042. {
  1043. Output.Write ("typeof(");
  1044. OutputType (e.Type);
  1045. Output.Write (")");
  1046. }
  1047. /*
  1048. * ICodeGenerator
  1049. */
  1050. protected override string CreateEscapedIdentifier (string value)
  1051. {
  1052. if (value == null)
  1053. throw new NullReferenceException ("Argument identifier is null.");
  1054. return GetSafeName (value);
  1055. }
  1056. protected override string CreateValidIdentifier (string value)
  1057. {
  1058. if (value == null)
  1059. throw new NullReferenceException ();
  1060. if (keywordsTable == null)
  1061. FillKeywordTable ();
  1062. if (keywordsTable.Contains (value))
  1063. return "_" + value;
  1064. else
  1065. return value;
  1066. }
  1067. protected override string GetTypeOutput (CodeTypeReference type)
  1068. {
  1069. string typeOutput = null;
  1070. if (type.ArrayElementType != null) {
  1071. typeOutput = GetTypeOutput (type.ArrayElementType);
  1072. } else {
  1073. typeOutput = DetermineTypeOutput (type);
  1074. }
  1075. int rank = type.ArrayRank;
  1076. if (rank > 0) {
  1077. typeOutput += '[';
  1078. for (--rank; rank > 0; --rank) {
  1079. typeOutput += ',';
  1080. }
  1081. typeOutput += ']';
  1082. }
  1083. return typeOutput;
  1084. }
  1085. private string DetermineTypeOutput (CodeTypeReference type)
  1086. {
  1087. string typeOutput = null;
  1088. string baseType = type.BaseType;
  1089. switch (baseType.ToLower (System.Globalization.CultureInfo.InvariantCulture)) {
  1090. case "system.int32":
  1091. typeOutput = "int";
  1092. break;
  1093. case "system.int64":
  1094. typeOutput = "long";
  1095. break;
  1096. case "system.int16":
  1097. typeOutput = "short";
  1098. break;
  1099. case "system.boolean":
  1100. typeOutput = "bool";
  1101. break;
  1102. case "system.char":
  1103. typeOutput = "char";
  1104. break;
  1105. case "system.string":
  1106. typeOutput = "string";
  1107. break;
  1108. case "system.object":
  1109. typeOutput = "object";
  1110. break;
  1111. case "system.void":
  1112. typeOutput = "void";
  1113. break;
  1114. #if NET_2_0
  1115. case "system.byte":
  1116. typeOutput = "byte";
  1117. break;
  1118. case "system.sbyte":
  1119. typeOutput = "sbyte";
  1120. break;
  1121. case "system.decimal":
  1122. typeOutput = "decimal";
  1123. break;
  1124. case "system.double":
  1125. typeOutput = "double";
  1126. break;
  1127. case "system.single":
  1128. typeOutput = "float";
  1129. break;
  1130. case "system.uint16":
  1131. typeOutput = "ushort";
  1132. break;
  1133. case "system.uint32":
  1134. typeOutput = "uint";
  1135. break;
  1136. case "system.uint64":
  1137. typeOutput = "ulong";
  1138. break;
  1139. #endif
  1140. default:
  1141. #if NET_2_0
  1142. StringBuilder sb = new StringBuilder (baseType.Length);
  1143. if (type.Options == CodeTypeReferenceOptions.GlobalReference) {
  1144. sb.Append ("global::");
  1145. }
  1146. int lastProcessedChar = 0;
  1147. for (int i = 0; i < baseType.Length; i++) {
  1148. char currentChar = baseType[i];
  1149. if (currentChar != '+' && currentChar != '.') {
  1150. if (currentChar == '`') {
  1151. sb.Append (CreateEscapedIdentifier (baseType.Substring (
  1152. lastProcessedChar, i - lastProcessedChar)));
  1153. // skip ` character
  1154. i++;
  1155. // determine number of type arguments to output
  1156. int typeArgCount = baseType[i] - '0';
  1157. // output type arguments
  1158. OutputTypeArguments (type.TypeArguments, sb, typeArgCount);
  1159. // skip type argument indicator
  1160. i++;
  1161. // if next character is . or +, then append .
  1162. if ((i < baseType.Length) && ((baseType[i] == '+') || (baseType[i] == '.'))) {
  1163. sb.Append ('.');
  1164. // skip character that we just processed
  1165. i++;
  1166. }
  1167. // save postion of last processed character
  1168. lastProcessedChar = i;
  1169. }
  1170. } else {
  1171. sb.Append (CreateEscapedIdentifier (baseType.Substring (
  1172. lastProcessedChar, i - lastProcessedChar)));
  1173. sb.Append ('.');
  1174. // skip separator
  1175. i++;
  1176. // save postion of last processed character
  1177. lastProcessedChar = i;
  1178. }
  1179. }
  1180. // add characters that have not yet been processed
  1181. if (lastProcessedChar < baseType.Length) {
  1182. sb.Append (CreateEscapedIdentifier (baseType.Substring (lastProcessedChar)));
  1183. }
  1184. typeOutput = sb.ToString ();
  1185. #else
  1186. typeOutput = GetSafeName (baseType);
  1187. typeOutput = typeOutput.Replace ('+', '.');
  1188. #endif
  1189. break;
  1190. }
  1191. return typeOutput;
  1192. }
  1193. protected override bool IsValidIdentifier (string identifier)
  1194. {
  1195. if (keywordsTable == null)
  1196. FillKeywordTable ();
  1197. return !keywordsTable.Contains (identifier);
  1198. }
  1199. protected override bool Supports (GeneratorSupport supports)
  1200. {
  1201. return true;
  1202. }
  1203. #if NET_2_0
  1204. protected override void GenerateDirectives (CodeDirectiveCollection directives)
  1205. {
  1206. foreach (CodeDirective d in directives) {
  1207. if (d is CodeChecksumPragma) {
  1208. GenerateCodeChecksumPragma ((CodeChecksumPragma)d);
  1209. continue;
  1210. }
  1211. if (d is CodeRegionDirective) {
  1212. GenerateCodeRegionDirective ((CodeRegionDirective)d);
  1213. continue;
  1214. }
  1215. throw new NotImplementedException ("Unknown CodeDirective");
  1216. }
  1217. }
  1218. void GenerateCodeChecksumPragma (CodeChecksumPragma pragma)
  1219. {
  1220. Output.Write ("#pragma checksum \"");
  1221. Output.Write (pragma.FileName);
  1222. Output.Write ("\" \"");
  1223. Output.Write (pragma.ChecksumAlgorithmId.ToString ("B"));
  1224. Output.Write ("\" \"");
  1225. if (pragma.ChecksumData != null) {
  1226. foreach (byte b in pragma.ChecksumData) {
  1227. Output.Write (b.ToString ("X2"));
  1228. }
  1229. }
  1230. Output.WriteLine ("\"");
  1231. }
  1232. void GenerateCodeRegionDirective (CodeRegionDirective region)
  1233. {
  1234. switch (region.RegionMode) {
  1235. case CodeRegionMode.Start:
  1236. Output.Write ("#region ");
  1237. Output.WriteLine (region.RegionText);
  1238. return;
  1239. case CodeRegionMode.End:
  1240. Output.WriteLine ("#endregion");
  1241. return;
  1242. }
  1243. }
  1244. void GenerateGenericsParameters (CodeTypeParameterCollection parameters)
  1245. {
  1246. int count = parameters.Count;
  1247. if (count == 0)
  1248. return;
  1249. Output.Write ('<');
  1250. for (int i = 0; i < count - 1; ++i) {
  1251. Output.Write (parameters [i].Name);
  1252. Output.Write (", ");
  1253. }
  1254. Output.Write (parameters [count - 1].Name);
  1255. Output.Write ('>');
  1256. }
  1257. void GenerateGenericsConstraints (CodeTypeParameterCollection parameters)
  1258. {
  1259. int count = parameters.Count;
  1260. if (count == 0)
  1261. return;
  1262. ++Indent;
  1263. foreach (CodeTypeParameter p in parameters) {
  1264. if (p.Constraints.Count == 0)
  1265. continue;
  1266. Output.WriteLine ();
  1267. Output.Write ("where ");
  1268. Output.Write (p.Name);
  1269. Output.Write (" : ");
  1270. bool is_first = true;
  1271. foreach (CodeTypeReference r in p.Constraints) {
  1272. if (is_first)
  1273. is_first = false;
  1274. else
  1275. Output.Write (", ");
  1276. OutputType (r);
  1277. }
  1278. if (p.HasConstructorConstraint) {
  1279. if (!is_first)
  1280. Output.Write (", ");
  1281. Output.Write ("new ()");
  1282. }
  1283. }
  1284. --Indent;
  1285. }
  1286. string GetTypeArguments (CodeTypeReferenceCollection collection)
  1287. {
  1288. StringBuilder sb = new StringBuilder (" <");
  1289. foreach (CodeTypeReference r in collection) {
  1290. sb.Append (GetTypeOutput (r));
  1291. sb.Append (", ");
  1292. }
  1293. sb.Length--;
  1294. sb [sb.Length - 1] = '>';
  1295. return sb.ToString ();
  1296. }
  1297. private void OutputTypeArguments (CodeTypeReferenceCollection typeArguments, StringBuilder sb, int count)
  1298. {
  1299. if (count == 0) {
  1300. return;
  1301. }
  1302. sb.Append ('<');
  1303. // write first type argument
  1304. sb.Append (GetTypeOutput (typeArguments[0]));
  1305. // subsequent type argument are prefixed by ', ' separator
  1306. for (int i = 1; i < count; i++) {
  1307. sb.Append (", ");
  1308. sb.Append (GetTypeOutput (typeArguments[i]));
  1309. }
  1310. sb.Append ('>');
  1311. }
  1312. #endif
  1313. #if false
  1314. //[MonoTODO]
  1315. public override void ValidateIdentifier (string identifier)
  1316. {
  1317. }
  1318. #endif
  1319. private string GetSafeName (string id)
  1320. {
  1321. if (keywordsTable == null) {
  1322. FillKeywordTable ();
  1323. }
  1324. if (keywordsTable.Contains (id)) {
  1325. return "@" + id;
  1326. } else {
  1327. return id;
  1328. }
  1329. }
  1330. static void FillKeywordTable ()
  1331. {
  1332. keywordsTable = new Hashtable ();
  1333. foreach (string keyword in keywords) {
  1334. keywordsTable.Add (keyword, keyword);
  1335. }
  1336. }
  1337. private static Hashtable keywordsTable;
  1338. private static string[] keywords = new string[] {
  1339. "abstract","event","new","struct","as","explicit","null","switch","base","extern",
  1340. "this","false","operator","throw","break","finally","out","true",
  1341. "fixed","override","try","case","params","typeof","catch","for",
  1342. "private","foreach","protected","checked","goto","public",
  1343. "unchecked","class","if","readonly","unsafe","const","implicit","ref",
  1344. "continue","in","return","using","virtual","default",
  1345. "interface","sealed","volatile","delegate","internal","do","is",
  1346. "sizeof","while","lock","stackalloc","else","static","enum",
  1347. "namespace",
  1348. "object","bool","byte","float","uint","char","ulong","ushort",
  1349. "decimal","int","sbyte","short","double","long","string","void",
  1350. #if NET_2_0
  1351. "partial", "yield", "where"
  1352. #endif
  1353. };
  1354. }
  1355. }