CSharpCodeGenerator.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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 GenerateParameterDeclarationExpression (CodeParameterDeclarationExpression e)
  953. {
  954. OutputAttributes (e.CustomAttributes, null, true);
  955. OutputDirection (e.Direction);
  956. OutputType (e.Type);
  957. Output.Write (' ');
  958. Output.Write (GetSafeName (e.Name));
  959. }
  960. protected override void GenerateTypeOfExpression (CodeTypeOfExpression e)
  961. {
  962. Output.Write ("typeof(");
  963. OutputType (e.Type);
  964. Output.Write (")");
  965. }
  966. /*
  967. * ICodeGenerator
  968. */
  969. protected override string CreateEscapedIdentifier (string value)
  970. {
  971. if (value == null)
  972. throw new NullReferenceException ("Argument identifier is null.");
  973. return GetSafeName (value);
  974. }
  975. protected override string CreateValidIdentifier (string value)
  976. {
  977. if (value == null)
  978. throw new NullReferenceException ();
  979. if (keywordsTable == null)
  980. FillKeywordTable ();
  981. if (keywordsTable.Contains (value))
  982. return "_" + value;
  983. else
  984. return value;
  985. }
  986. protected override string GetTypeOutput (CodeTypeReference type)
  987. {
  988. string typeOutput = null;
  989. if (type.ArrayElementType != null) {
  990. typeOutput = GetTypeOutput (type.ArrayElementType);
  991. } else {
  992. typeOutput = DetermineTypeOutput (type);
  993. }
  994. int rank = type.ArrayRank;
  995. if (rank > 0) {
  996. typeOutput += '[';
  997. for (--rank; rank > 0; --rank) {
  998. typeOutput += ',';
  999. }
  1000. typeOutput += ']';
  1001. }
  1002. return typeOutput;
  1003. }
  1004. private string DetermineTypeOutput (CodeTypeReference type)
  1005. {
  1006. string typeOutput = null;
  1007. string baseType = type.BaseType;
  1008. switch (baseType.ToLower (System.Globalization.CultureInfo.InvariantCulture)) {
  1009. case "system.int32":
  1010. typeOutput = "int";
  1011. break;
  1012. case "system.int64":
  1013. typeOutput = "long";
  1014. break;
  1015. case "system.int16":
  1016. typeOutput = "short";
  1017. break;
  1018. case "system.boolean":
  1019. typeOutput = "bool";
  1020. break;
  1021. case "system.char":
  1022. typeOutput = "char";
  1023. break;
  1024. case "system.string":
  1025. typeOutput = "string";
  1026. break;
  1027. case "system.object":
  1028. typeOutput = "object";
  1029. break;
  1030. case "system.void":
  1031. typeOutput = "void";
  1032. break;
  1033. #if NET_2_0
  1034. case "system.byte":
  1035. typeOutput = "byte";
  1036. break;
  1037. case "system.sbyte":
  1038. typeOutput = "sbyte";
  1039. break;
  1040. case "system.decimal":
  1041. typeOutput = "decimal";
  1042. break;
  1043. case "system.double":
  1044. typeOutput = "double";
  1045. break;
  1046. case "system.single":
  1047. typeOutput = "float";
  1048. break;
  1049. case "system.uint16":
  1050. typeOutput = "ushort";
  1051. break;
  1052. case "system.uint32":
  1053. typeOutput = "uint";
  1054. break;
  1055. case "system.uint64":
  1056. typeOutput = "ulong";
  1057. break;
  1058. #endif
  1059. default:
  1060. #if NET_2_0
  1061. StringBuilder sb = new StringBuilder (baseType.Length);
  1062. if (type.Options == CodeTypeReferenceOptions.GlobalReference) {
  1063. sb.Append ("global::");
  1064. }
  1065. int lastProcessedChar = 0;
  1066. for (int i = 0; i < baseType.Length; i++) {
  1067. char currentChar = baseType[i];
  1068. if (currentChar != '+' && currentChar != '.') {
  1069. if (currentChar == '`') {
  1070. sb.Append (CreateEscapedIdentifier (baseType.Substring (
  1071. lastProcessedChar, i - lastProcessedChar)));
  1072. // skip ` character
  1073. i++;
  1074. // determine number of type arguments to output
  1075. int typeArgCount = baseType[i] - '0';
  1076. // output type arguments
  1077. OutputTypeArguments (type.TypeArguments, sb, typeArgCount);
  1078. // skip type argument indicator
  1079. i++;
  1080. // if next character is . or +, then append .
  1081. if ((i < baseType.Length) && ((baseType[i] == '+') || (baseType[i] == '.'))) {
  1082. sb.Append ('.');
  1083. // skip character that we just processed
  1084. i++;
  1085. }
  1086. // save postion of last processed character
  1087. lastProcessedChar = i;
  1088. }
  1089. } else {
  1090. sb.Append (CreateEscapedIdentifier (baseType.Substring (
  1091. lastProcessedChar, i - lastProcessedChar)));
  1092. sb.Append ('.');
  1093. // skip separator
  1094. i++;
  1095. // save postion of last processed character
  1096. lastProcessedChar = i;
  1097. }
  1098. }
  1099. // add characters that have not yet been processed
  1100. if (lastProcessedChar < baseType.Length) {
  1101. sb.Append (CreateEscapedIdentifier (baseType.Substring (lastProcessedChar)));
  1102. }
  1103. typeOutput = sb.ToString ();
  1104. #else
  1105. typeOutput = GetSafeName (baseType);
  1106. typeOutput = typeOutput.Replace ('+', '.');
  1107. #endif
  1108. break;
  1109. }
  1110. return typeOutput;
  1111. }
  1112. protected override bool IsValidIdentifier (string identifier)
  1113. {
  1114. if (keywordsTable == null)
  1115. FillKeywordTable ();
  1116. return !keywordsTable.Contains (identifier);
  1117. }
  1118. protected override bool Supports (GeneratorSupport supports)
  1119. {
  1120. return true;
  1121. }
  1122. #if NET_2_0
  1123. protected override void GenerateDirectives (CodeDirectiveCollection directives)
  1124. {
  1125. foreach (CodeDirective d in directives) {
  1126. if (d is CodeChecksumPragma) {
  1127. GenerateCodeChecksumPragma ((CodeChecksumPragma)d);
  1128. continue;
  1129. }
  1130. if (d is CodeRegionDirective) {
  1131. GenerateCodeRegionDirective ((CodeRegionDirective)d);
  1132. continue;
  1133. }
  1134. throw new NotImplementedException ("Unknown CodeDirective");
  1135. }
  1136. }
  1137. void GenerateCodeChecksumPragma (CodeChecksumPragma pragma)
  1138. {
  1139. Output.Write ("#pragma checksum \"");
  1140. Output.Write (pragma.FileName);
  1141. Output.Write ("\" \"");
  1142. Output.Write (pragma.ChecksumAlgorithmId.ToString ("B"));
  1143. Output.Write ("\" \"");
  1144. if (pragma.ChecksumData != null) {
  1145. foreach (byte b in pragma.ChecksumData) {
  1146. Output.Write (b.ToString ("X2"));
  1147. }
  1148. }
  1149. Output.WriteLine ("\"");
  1150. }
  1151. void GenerateCodeRegionDirective (CodeRegionDirective region)
  1152. {
  1153. switch (region.RegionMode) {
  1154. case CodeRegionMode.Start:
  1155. Output.Write ("#region ");
  1156. Output.WriteLine (region.RegionText);
  1157. return;
  1158. case CodeRegionMode.End:
  1159. Output.WriteLine ("#endregion");
  1160. return;
  1161. }
  1162. }
  1163. void GenerateGenericsParameters (CodeTypeParameterCollection parameters)
  1164. {
  1165. int count = parameters.Count;
  1166. if (count == 0)
  1167. return;
  1168. Output.Write ('<');
  1169. for (int i = 0; i < count - 1; ++i) {
  1170. Output.Write (parameters [i].Name);
  1171. Output.Write (", ");
  1172. }
  1173. Output.Write (parameters [count - 1].Name);
  1174. Output.Write ('>');
  1175. }
  1176. void GenerateGenericsConstraints (CodeTypeParameterCollection parameters)
  1177. {
  1178. int count = parameters.Count;
  1179. if (count == 0)
  1180. return;
  1181. ++Indent;
  1182. foreach (CodeTypeParameter p in parameters) {
  1183. if (p.Constraints.Count == 0)
  1184. continue;
  1185. Output.WriteLine ();
  1186. Output.Write ("where ");
  1187. Output.Write (p.Name);
  1188. Output.Write (" : ");
  1189. bool is_first = true;
  1190. foreach (CodeTypeReference r in p.Constraints) {
  1191. if (is_first)
  1192. is_first = false;
  1193. else
  1194. Output.Write (", ");
  1195. OutputType (r);
  1196. }
  1197. if (p.HasConstructorConstraint) {
  1198. if (!is_first)
  1199. Output.Write (", ");
  1200. Output.Write ("new ()");
  1201. }
  1202. }
  1203. --Indent;
  1204. }
  1205. string GetTypeArguments (CodeTypeReferenceCollection collection)
  1206. {
  1207. StringBuilder sb = new StringBuilder (" <");
  1208. foreach (CodeTypeReference r in collection) {
  1209. sb.Append (GetTypeOutput (r));
  1210. sb.Append (", ");
  1211. }
  1212. sb.Length--;
  1213. sb [sb.Length - 1] = '>';
  1214. return sb.ToString ();
  1215. }
  1216. private void OutputTypeArguments (CodeTypeReferenceCollection typeArguments, StringBuilder sb, int count)
  1217. {
  1218. if (count == 0) {
  1219. return;
  1220. }
  1221. sb.Append ('<');
  1222. // write first type argument
  1223. sb.Append (GetTypeOutput (typeArguments[0]));
  1224. // subsequent type argument are prefixed by ', ' separator
  1225. for (int i = 1; i < count; i++) {
  1226. sb.Append (", ");
  1227. sb.Append (GetTypeOutput (typeArguments[i]));
  1228. }
  1229. sb.Append ('>');
  1230. }
  1231. #endif
  1232. #if false
  1233. //[MonoTODO]
  1234. public override void ValidateIdentifier (string identifier)
  1235. {
  1236. }
  1237. #endif
  1238. private string GetSafeName (string id)
  1239. {
  1240. if (keywordsTable == null) {
  1241. FillKeywordTable ();
  1242. }
  1243. if (keywordsTable.Contains (id)) {
  1244. return "@" + id;
  1245. } else {
  1246. return id;
  1247. }
  1248. }
  1249. static void FillKeywordTable ()
  1250. {
  1251. keywordsTable = new Hashtable ();
  1252. foreach (string keyword in keywords) {
  1253. keywordsTable.Add (keyword, keyword);
  1254. }
  1255. }
  1256. private static Hashtable keywordsTable;
  1257. private static string[] keywords = new string[] {
  1258. "abstract","event","new","struct","as","explicit","null","switch","base","extern",
  1259. "this","false","operator","throw","break","finally","out","true",
  1260. "fixed","override","try","case","params","typeof","catch","for",
  1261. "private","foreach","protected","checked","goto","public",
  1262. "unchecked","class","if","readonly","unsafe","const","implicit","ref",
  1263. "continue","in","return","using","virtual","default",
  1264. "interface","sealed","volatile","delegate","internal","do","is",
  1265. "sizeof","while","lock","stackalloc","else","static","enum",
  1266. "namespace",
  1267. "object","bool","byte","float","uint","char","ulong","ushort",
  1268. "decimal","int","sbyte","short","double","long","string","void",
  1269. #if NET_2_0
  1270. "partial", "yield", "where"
  1271. #endif
  1272. };
  1273. }
  1274. }