CodeGenerator.cs 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. //
  2. // System.CodeDom.Compiler.CodeGenerator.cs
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. // Daniel Stodden ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. // Andreas Nahr ([email protected])
  9. //
  10. // (C) 2001-2003 Ximian, Inc.
  11. //
  12. using System.Globalization;
  13. using System.CodeDom;
  14. using System.Reflection;
  15. using System.IO;
  16. using System.Collections;
  17. namespace System.CodeDom.Compiler {
  18. public abstract class CodeGenerator : ICodeGenerator
  19. {
  20. private IndentedTextWriter output;
  21. private CodeGeneratorOptions options;
  22. private CodeTypeMember currentMember;
  23. private CodeTypeDeclaration currentType;
  24. //
  25. // Constructors
  26. //
  27. protected CodeGenerator()
  28. {
  29. }
  30. //
  31. // Properties
  32. //
  33. protected CodeTypeMember CurrentMember {
  34. get {
  35. return currentMember;
  36. }
  37. }
  38. protected string CurrentMemberName {
  39. get {
  40. if (currentType == null)
  41. return null;
  42. return currentMember.Name;
  43. }
  44. }
  45. protected string CurrentTypeName {
  46. get {
  47. if (currentType == null)
  48. return null;
  49. return currentType.Name;
  50. }
  51. }
  52. protected int Indent {
  53. get {
  54. return output.Indent;
  55. }
  56. set {
  57. output.Indent = value;
  58. }
  59. }
  60. protected bool IsCurrentClass {
  61. get {
  62. if (currentType == null)
  63. return false;
  64. return currentType.IsClass;
  65. }
  66. }
  67. protected bool IsCurrentDelegate {
  68. get {
  69. return currentType is CodeTypeDelegate;
  70. }
  71. }
  72. protected bool IsCurrentEnum {
  73. get {
  74. if (currentType == null)
  75. return false;
  76. return currentType.IsEnum;
  77. }
  78. }
  79. protected bool IsCurrentInterface {
  80. get {
  81. if (currentType == null)
  82. return false;
  83. return currentType.IsInterface;
  84. }
  85. }
  86. protected bool IsCurrentStruct {
  87. get {
  88. if (currentType == null)
  89. return false;
  90. return currentType.IsStruct;
  91. }
  92. }
  93. protected abstract string NullToken {
  94. get;
  95. }
  96. protected CodeGeneratorOptions Options {
  97. get {
  98. return options;
  99. }
  100. }
  101. protected TextWriter Output {
  102. get {
  103. return output;
  104. }
  105. }
  106. //
  107. // Methods
  108. //
  109. protected virtual void ContinueOnNewLine (string st)
  110. {
  111. output.WriteLine (st);
  112. }
  113. /*
  114. * Code Generation methods
  115. */
  116. protected abstract void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression e);
  117. protected abstract void GenerateArrayCreateExpression (CodeArrayCreateExpression e);
  118. protected abstract void GenerateArrayIndexerExpression (CodeArrayIndexerExpression e);
  119. protected abstract void GenerateAssignStatement (CodeAssignStatement s);
  120. protected abstract void GenerateAttachEventStatement (CodeAttachEventStatement s);
  121. protected abstract void GenerateAttributeDeclarationsStart (CodeAttributeDeclarationCollection attributes);
  122. protected abstract void GenerateAttributeDeclarationsEnd (CodeAttributeDeclarationCollection attributes);
  123. protected abstract void GenerateBaseReferenceExpression (CodeBaseReferenceExpression e);
  124. protected virtual void GenerateBinaryOperatorExpression (CodeBinaryOperatorExpression e)
  125. {
  126. output.Write ('(');
  127. GenerateExpression (e.Left);
  128. output.Write (' ');
  129. OutputOperator (e.Operator);
  130. output.Write (' ');
  131. GenerateExpression (e.Right);
  132. output.Write (')');
  133. }
  134. protected abstract void GenerateCastExpression (CodeCastExpression e);
  135. protected abstract void GenerateComment (CodeComment comment);
  136. protected virtual void GenerateCommentStatement (CodeCommentStatement statement)
  137. {
  138. GenerateComment (statement.Comment);
  139. }
  140. protected virtual void GenerateCommentStatements (CodeCommentStatementCollection statements)
  141. {
  142. foreach (CodeCommentStatement comment in statements)
  143. GenerateCommentStatement (comment);
  144. }
  145. protected virtual void GenerateCompileUnit (CodeCompileUnit compileUnit)
  146. {
  147. GenerateCompileUnitStart (compileUnit);
  148. CodeAttributeDeclarationCollection attributes = compileUnit.AssemblyCustomAttributes;
  149. if (attributes.Count != 0) {
  150. foreach (CodeAttributeDeclaration att in attributes) {
  151. GenerateAttributeDeclarationsStart (attributes);
  152. output.Write ("assembly: ");
  153. OutputAttributeDeclaration (att);
  154. GenerateAttributeDeclarationsEnd (attributes);
  155. }
  156. }
  157. foreach (CodeNamespace ns in compileUnit.Namespaces)
  158. GenerateNamespace (ns);
  159. GenerateCompileUnitEnd (compileUnit);
  160. }
  161. protected virtual void GenerateCompileUnitEnd (CodeCompileUnit compileUnit)
  162. {
  163. }
  164. protected virtual void GenerateCompileUnitStart (CodeCompileUnit compileUnit)
  165. {
  166. }
  167. protected abstract void GenerateConditionStatement (CodeConditionStatement s);
  168. protected abstract void GenerateConstructor (CodeConstructor x, CodeTypeDeclaration d);
  169. protected virtual void GenerateDecimalValue (Decimal d)
  170. {
  171. Output.Write (d.ToString ());
  172. }
  173. protected abstract void GenerateDelegateCreateExpression (CodeDelegateCreateExpression e);
  174. protected abstract void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression e);
  175. protected virtual void GenerateDirectionExpression (CodeDirectionExpression e)
  176. {
  177. OutputDirection (e.Direction);
  178. output.Write (' ');
  179. GenerateExpression (e.Expression);
  180. }
  181. protected virtual void GenerateDoubleValue (Double d)
  182. {
  183. Output.Write (d.ToString ());
  184. }
  185. protected abstract void GenerateEntryPointMethod (CodeEntryPointMethod m, CodeTypeDeclaration d);
  186. protected abstract void GenerateEvent (CodeMemberEvent ev, CodeTypeDeclaration d);
  187. protected abstract void GenerateEventReferenceExpression (CodeEventReferenceExpression e);
  188. protected void GenerateExpression (CodeExpression e)
  189. {
  190. if (e == null)
  191. throw new ArgumentNullException ("Value cannot be null.");
  192. CodeArgumentReferenceExpression argref = e as CodeArgumentReferenceExpression;
  193. if (argref != null) {
  194. GenerateArgumentReferenceExpression (argref);
  195. return;
  196. }
  197. CodeArrayCreateExpression mkarray = e as CodeArrayCreateExpression;
  198. if (mkarray != null) {
  199. GenerateArrayCreateExpression (mkarray);
  200. return;
  201. }
  202. CodeArrayIndexerExpression arrayidx = e as CodeArrayIndexerExpression;
  203. if (arrayidx != null) {
  204. GenerateArrayIndexerExpression (arrayidx);
  205. return;
  206. }
  207. CodeBaseReferenceExpression baseref = e as CodeBaseReferenceExpression;
  208. if (baseref != null) {
  209. GenerateBaseReferenceExpression (baseref);
  210. return;
  211. }
  212. CodeBinaryOperatorExpression binary = e as CodeBinaryOperatorExpression;
  213. if (binary != null) {
  214. GenerateBinaryOperatorExpression (binary);
  215. return;
  216. }
  217. CodeCastExpression cast = e as CodeCastExpression;
  218. if (cast != null) {
  219. GenerateCastExpression (cast);
  220. return;
  221. }
  222. CodeDelegateCreateExpression mkdel = e as CodeDelegateCreateExpression;
  223. if (mkdel != null) {
  224. GenerateDelegateCreateExpression (mkdel);
  225. return;
  226. }
  227. CodeDelegateInvokeExpression delinvoke = e as CodeDelegateInvokeExpression;
  228. if (delinvoke != null) {
  229. GenerateDelegateInvokeExpression (delinvoke);
  230. return;
  231. }
  232. CodeDirectionExpression direction = e as CodeDirectionExpression;
  233. if (direction != null) {
  234. GenerateDirectionExpression (direction);
  235. return;
  236. }
  237. CodeEventReferenceExpression eventref = e as CodeEventReferenceExpression;
  238. if ( eventref != null ) {
  239. GenerateEventReferenceExpression( eventref );
  240. return;
  241. }
  242. CodeFieldReferenceExpression fieldref = e as CodeFieldReferenceExpression;
  243. if (fieldref != null) {
  244. GenerateFieldReferenceExpression (fieldref);
  245. return;
  246. }
  247. CodeIndexerExpression idx = e as CodeIndexerExpression;
  248. if (idx != null) {
  249. GenerateIndexerExpression (idx);
  250. return;
  251. }
  252. CodeMethodInvokeExpression methodinv = e as CodeMethodInvokeExpression;
  253. if (methodinv != null) {
  254. GenerateMethodInvokeExpression (methodinv);
  255. return;
  256. }
  257. CodeMethodReferenceExpression methodref = e as CodeMethodReferenceExpression;
  258. if (methodref != null) {
  259. GenerateMethodReferenceExpression (methodref);
  260. return;
  261. }
  262. CodeObjectCreateExpression objref = e as CodeObjectCreateExpression;
  263. if (objref != null) {
  264. GenerateObjectCreateExpression (objref);
  265. return;
  266. }
  267. CodeParameterDeclarationExpression param = e as CodeParameterDeclarationExpression;
  268. if (param != null) {
  269. GenerateParameterDeclarationExpression (param);
  270. return;
  271. }
  272. CodePrimitiveExpression primitive = e as CodePrimitiveExpression;
  273. if (primitive != null) {
  274. GeneratePrimitiveExpression (primitive);
  275. return;
  276. }
  277. CodePropertyReferenceExpression propref = e as CodePropertyReferenceExpression;
  278. if (propref != null) {
  279. GeneratePropertyReferenceExpression (propref);
  280. return;
  281. }
  282. CodePropertySetValueReferenceExpression propset = e as CodePropertySetValueReferenceExpression;
  283. if (propset != null) {
  284. GeneratePropertySetValueReferenceExpression (propset);
  285. return;
  286. }
  287. CodeSnippetExpression snippet = e as CodeSnippetExpression;
  288. if (snippet != null) {
  289. GenerateSnippetExpression (snippet);
  290. return;
  291. }
  292. CodeThisReferenceExpression thisref = e as CodeThisReferenceExpression;
  293. if (thisref != null) {
  294. GenerateThisReferenceExpression (thisref);
  295. return;
  296. }
  297. CodeTypeOfExpression typeOf = e as CodeTypeOfExpression;
  298. if (typeOf != null) {
  299. GenerateTypeOfExpression (typeOf);
  300. return;
  301. }
  302. CodeTypeReferenceExpression typeref = e as CodeTypeReferenceExpression;
  303. if (typeref != null) {
  304. GenerateTypeReferenceExpression (typeref);
  305. return;
  306. }
  307. CodeVariableReferenceExpression varref = e as CodeVariableReferenceExpression;
  308. if (varref != null) {
  309. GenerateVariableReferenceExpression (varref);
  310. return;
  311. }
  312. throw new ArgumentException ("Element type " + e + " is not supported.");
  313. }
  314. protected abstract void GenerateExpressionStatement (CodeExpressionStatement statement);
  315. protected abstract void GenerateField (CodeMemberField f);
  316. protected abstract void GenerateFieldReferenceExpression (CodeFieldReferenceExpression e);
  317. protected abstract void GenerateGotoStatement (CodeGotoStatement statement);
  318. protected abstract void GenerateIndexerExpression (CodeIndexerExpression e);
  319. protected abstract void GenerateIterationStatement (CodeIterationStatement s);
  320. protected abstract void GenerateLabeledStatement (CodeLabeledStatement statement);
  321. protected abstract void GenerateLinePragmaStart (CodeLinePragma p);
  322. protected abstract void GenerateLinePragmaEnd (CodeLinePragma p);
  323. protected abstract void GenerateMethod (CodeMemberMethod m, CodeTypeDeclaration d);
  324. protected abstract void GenerateMethodInvokeExpression (CodeMethodInvokeExpression e);
  325. protected abstract void GenerateMethodReferenceExpression (CodeMethodReferenceExpression e);
  326. protected abstract void GenerateMethodReturnStatement (CodeMethodReturnStatement e);
  327. protected virtual void GenerateNamespace (CodeNamespace ns)
  328. {
  329. foreach (CodeCommentStatement statement in ns.Comments)
  330. GenerateCommentStatement (statement);
  331. GenerateNamespaceStart (ns);
  332. foreach (CodeNamespaceImport import in ns.Imports)
  333. GenerateNamespaceImport (import);
  334. output.WriteLine();
  335. foreach (CodeTypeDeclaration type in ns.Types) {
  336. GenerateType (type);
  337. output.WriteLine();
  338. }
  339. GenerateNamespaceEnd (ns);
  340. }
  341. protected abstract void GenerateNamespaceStart (CodeNamespace ns);
  342. protected abstract void GenerateNamespaceEnd (CodeNamespace ns);
  343. protected abstract void GenerateNamespaceImport (CodeNamespaceImport i);
  344. protected void GenerateNamespaceImports (CodeNamespace e)
  345. {
  346. foreach (CodeNamespaceImport import in e.Imports)
  347. GenerateNamespaceImport (import);
  348. }
  349. protected void GenerateNamespaces (CodeCompileUnit e)
  350. {
  351. foreach (CodeNamespace ns in e.Namespaces)
  352. GenerateNamespace (ns);
  353. }
  354. protected abstract void GenerateObjectCreateExpression (CodeObjectCreateExpression e);
  355. protected virtual void GenerateParameterDeclarationExpression (CodeParameterDeclarationExpression e)
  356. {
  357. if (e.CustomAttributes != null && e.CustomAttributes.Count > 0)
  358. OutputAttributeDeclarations (e.CustomAttributes);
  359. OutputDirection (e.Direction);
  360. OutputType (e.Type);
  361. output.Write (' ');
  362. output.Write (e.Name);
  363. }
  364. protected virtual void GeneratePrimitiveExpression (CodePrimitiveExpression e)
  365. {
  366. if (e.Value == null) {
  367. output.Write (NullToken);
  368. return;
  369. }
  370. Type type = e.Value.GetType ();
  371. if (type == typeof (bool)) {
  372. output.Write (e.Value.ToString ().ToLower ());
  373. } else if (type == typeof (char)) {
  374. output.Write ("'" + e.Value.ToString () + "'");
  375. } else if (type == typeof (string)) {
  376. output.Write (QuoteSnippetString ((string) e.Value));
  377. } else if (type == typeof (byte) || type == typeof (sbyte) || type == typeof (short) ||
  378. type == typeof (int) || type == typeof (long) || type == typeof (float) ||
  379. type == typeof (double) || type == typeof (decimal)) {
  380. output.Write (e.Value.ToString ());
  381. } else {
  382. throw new ArgumentException ("Value type (" + type + ") is not a primitive type");
  383. }
  384. }
  385. protected abstract void GenerateProperty (CodeMemberProperty p, CodeTypeDeclaration d);
  386. protected abstract void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression e);
  387. protected abstract void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression e);
  388. protected abstract void GenerateRemoveEventStatement (CodeRemoveEventStatement statement);
  389. protected virtual void GenerateSingleFloatValue (Single s)
  390. {
  391. output.Write (s.ToString());
  392. }
  393. protected virtual void GenerateSnippetCompileUnit (CodeSnippetCompileUnit e)
  394. {
  395. output.WriteLine (e.Value);
  396. }
  397. protected abstract void GenerateSnippetExpression (CodeSnippetExpression e);
  398. protected abstract void GenerateSnippetMember (CodeSnippetTypeMember m);
  399. protected virtual void GenerateSnippetStatement (CodeSnippetStatement s)
  400. {
  401. output.WriteLine (s.Value);
  402. }
  403. protected void GenerateStatement (CodeStatement s)
  404. {
  405. bool handled = false;
  406. if (s.LinePragma != null)
  407. GenerateLinePragmaStart (s.LinePragma);
  408. CodeAssignStatement assign = s as CodeAssignStatement;
  409. if (assign != null) {
  410. GenerateAssignStatement (assign);
  411. handled = true;
  412. }
  413. CodeAttachEventStatement attach = s as CodeAttachEventStatement;
  414. if (attach != null) {
  415. GenerateAttachEventStatement (attach);
  416. handled = true;
  417. }
  418. CodeCommentStatement comment = s as CodeCommentStatement;
  419. if (comment != null) {
  420. GenerateCommentStatement (comment);
  421. handled = true;
  422. }
  423. CodeConditionStatement condition = s as CodeConditionStatement;
  424. if (condition != null) {
  425. GenerateConditionStatement (condition);
  426. handled = true;
  427. }
  428. CodeExpressionStatement expression = s as CodeExpressionStatement;
  429. if (expression != null) {
  430. GenerateExpressionStatement (expression);
  431. handled = true;
  432. }
  433. CodeGotoStatement gotostmt = s as CodeGotoStatement;
  434. if (gotostmt != null) {
  435. GenerateGotoStatement (gotostmt);
  436. handled = true;
  437. }
  438. CodeIterationStatement iteration = s as CodeIterationStatement;
  439. if (iteration != null) {
  440. GenerateIterationStatement (iteration);
  441. handled = true;
  442. }
  443. CodeLabeledStatement label = s as CodeLabeledStatement;
  444. if (label != null) {
  445. GenerateLabeledStatement (label);
  446. handled = true;
  447. }
  448. CodeMethodReturnStatement returnstmt = s as CodeMethodReturnStatement;
  449. if (returnstmt != null) {
  450. GenerateMethodReturnStatement (returnstmt);
  451. handled = true;
  452. }
  453. CodeRemoveEventStatement remove = s as CodeRemoveEventStatement;
  454. if (remove != null) {
  455. GenerateRemoveEventStatement (remove);
  456. handled = true;
  457. }
  458. CodeSnippetStatement snippet = s as CodeSnippetStatement;
  459. if (snippet != null) {
  460. GenerateSnippetStatement (snippet);
  461. handled = true;
  462. }
  463. CodeThrowExceptionStatement exception = s as CodeThrowExceptionStatement;
  464. if (exception != null) {
  465. GenerateThrowExceptionStatement (exception);
  466. handled = true;
  467. }
  468. CodeTryCatchFinallyStatement trycatch = s as CodeTryCatchFinallyStatement;
  469. if (trycatch != null) {
  470. GenerateTryCatchFinallyStatement (trycatch);
  471. handled = true;
  472. }
  473. CodeVariableDeclarationStatement declaration = s as CodeVariableDeclarationStatement;
  474. if (declaration != null) {
  475. GenerateVariableDeclarationStatement (declaration);
  476. handled = true;
  477. }
  478. if (!handled)
  479. throw new ArgumentException ("Element type " + s + " is not supported.");
  480. if (s.LinePragma != null)
  481. GenerateLinePragmaEnd (s.LinePragma);
  482. }
  483. protected void GenerateStatements (CodeStatementCollection c)
  484. {
  485. foreach (CodeStatement statement in c)
  486. GenerateStatement (statement);
  487. }
  488. protected abstract void GenerateThisReferenceExpression (CodeThisReferenceExpression e);
  489. protected abstract void GenerateThrowExceptionStatement (CodeThrowExceptionStatement s);
  490. protected abstract void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement s);
  491. protected abstract void GenerateTypeEnd (CodeTypeDeclaration declaration);
  492. protected abstract void GenerateTypeConstructor (CodeTypeConstructor constructor);
  493. protected virtual void GenerateTypeOfExpression (CodeTypeOfExpression e)
  494. {
  495. output.Write ("typeof(");
  496. OutputType (e.Type);
  497. output.Write (")");
  498. }
  499. protected virtual void GenerateTypeReferenceExpression (CodeTypeReferenceExpression e)
  500. {
  501. OutputType (e.Type);
  502. }
  503. protected void GenerateTypes (CodeNamespace e)
  504. {
  505. foreach (CodeTypeDeclaration type in e.Types)
  506. GenerateType (type);
  507. }
  508. protected abstract void GenerateTypeStart (CodeTypeDeclaration declaration);
  509. protected abstract void GenerateVariableDeclarationStatement (CodeVariableDeclarationStatement e);
  510. protected abstract void GenerateVariableReferenceExpression (CodeVariableReferenceExpression e);
  511. //
  512. // Other members
  513. //
  514. /*
  515. * Output Methods
  516. */
  517. protected virtual void OutputAttributeArgument (CodeAttributeArgument argument)
  518. {
  519. string name = argument.Name;
  520. if (name != null) {
  521. output.Write (name);
  522. output.Write ('=');
  523. }
  524. GenerateExpression (argument.Value);
  525. }
  526. private void OutputAttributeDeclaration (CodeAttributeDeclaration attribute)
  527. {
  528. output.Write (attribute.Name);
  529. output.Write ('(');
  530. IEnumerator enumerator = attribute.Arguments.GetEnumerator();
  531. if (enumerator.MoveNext()) {
  532. CodeAttributeArgument argument = (CodeAttributeArgument)enumerator.Current;
  533. OutputAttributeArgument (argument);
  534. while (enumerator.MoveNext()) {
  535. output.Write (',');
  536. argument = (CodeAttributeArgument)enumerator.Current;
  537. OutputAttributeArgument (argument);
  538. }
  539. }
  540. output.Write (')');
  541. }
  542. protected virtual void OutputAttributeDeclarations (CodeAttributeDeclarationCollection attributes)
  543. {
  544. GenerateAttributeDeclarationsStart (attributes);
  545. IEnumerator enumerator = attributes.GetEnumerator();
  546. if (enumerator.MoveNext()) {
  547. CodeAttributeDeclaration attribute = (CodeAttributeDeclaration)enumerator.Current;
  548. OutputAttributeDeclaration (attribute);
  549. while (enumerator.MoveNext()) {
  550. attribute = (CodeAttributeDeclaration)enumerator.Current;
  551. output.WriteLine (',');
  552. OutputAttributeDeclaration (attribute);
  553. }
  554. }
  555. GenerateAttributeDeclarationsEnd (attributes);
  556. }
  557. protected virtual void OutputDirection (FieldDirection direction)
  558. {
  559. switch (direction) {
  560. case FieldDirection.In:
  561. //output.Write ("in ");
  562. break;
  563. case FieldDirection.Out:
  564. output.Write ("out ");
  565. break;
  566. case FieldDirection.Ref:
  567. output.Write ("ref ");
  568. break;
  569. }
  570. }
  571. protected virtual void OutputExpressionList (CodeExpressionCollection expressions)
  572. {
  573. OutputExpressionList (expressions, false);
  574. }
  575. protected virtual void OutputExpressionList (CodeExpressionCollection expressions,
  576. bool newLineBetweenItems)
  577. {
  578. IEnumerator enumerator = expressions.GetEnumerator();
  579. if (enumerator.MoveNext()) {
  580. CodeExpression expression = (CodeExpression)enumerator.Current;
  581. GenerateExpression (expression);
  582. while (enumerator.MoveNext()) {
  583. expression = (CodeExpression)enumerator.Current;
  584. output.Write (',');
  585. if (newLineBetweenItems)
  586. output.WriteLine ();
  587. else
  588. output.Write (' ');
  589. GenerateExpression (expression);
  590. }
  591. }
  592. }
  593. protected virtual void OutputFieldScopeModifier (MemberAttributes attributes)
  594. {
  595. if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New)
  596. output.Write ("new ");
  597. switch (attributes & MemberAttributes.ScopeMask) {
  598. case MemberAttributes.Static:
  599. output.Write ("static ");
  600. break;
  601. case MemberAttributes.Const:
  602. output.Write ("const ");
  603. break;
  604. }
  605. }
  606. protected virtual void OutputIdentifier (string ident)
  607. {
  608. output.Write (ident);
  609. }
  610. protected virtual void OutputMemberAccessModifier (MemberAttributes attributes)
  611. {
  612. switch (attributes & MemberAttributes.AccessMask) {
  613. case MemberAttributes.Assembly:
  614. output.Write ("internal ");
  615. break;
  616. case MemberAttributes.FamilyAndAssembly:
  617. output.Write ("/* FamAndAssem */ internal ");
  618. break;
  619. case MemberAttributes.Family:
  620. output.Write ("protected ");
  621. break;
  622. case MemberAttributes.FamilyOrAssembly:
  623. output.Write ("protected internal ");
  624. break;
  625. case MemberAttributes.Private:
  626. output.Write ("private ");
  627. break;
  628. case MemberAttributes.Public:
  629. output.Write ("public ");
  630. break;
  631. }
  632. }
  633. protected virtual void OutputMemberScopeModifier (MemberAttributes attributes)
  634. {
  635. if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New)
  636. output.Write( "new " );
  637. switch (attributes & MemberAttributes.ScopeMask) {
  638. case MemberAttributes.Abstract:
  639. output.Write ("abstract ");
  640. break;
  641. case MemberAttributes.Final:
  642. // Do nothing
  643. break;
  644. case MemberAttributes.Static:
  645. output.Write ("static ");
  646. break;
  647. case MemberAttributes.Override:
  648. output.Write ("override ");
  649. break;
  650. default:
  651. //
  652. // FUNNY! if the scope value is
  653. // rubbish (0 or >Const), and access
  654. // is public or protected, make it
  655. // "virtual".
  656. //
  657. // i'm not sure whether this is 100%
  658. // correct, but it seems to be MS
  659. // behavior.
  660. //
  661. MemberAttributes access = attributes & MemberAttributes.AccessMask;
  662. if (access == MemberAttributes.Public ||
  663. access == MemberAttributes.Family)
  664. output.Write ("virtual ");
  665. break;
  666. }
  667. }
  668. protected virtual void OutputOperator (CodeBinaryOperatorType op)
  669. {
  670. switch (op) {
  671. case CodeBinaryOperatorType.Add:
  672. output.Write ("+");
  673. break;
  674. case CodeBinaryOperatorType.Subtract:
  675. output.Write ("-");
  676. break;
  677. case CodeBinaryOperatorType.Multiply:
  678. output.Write ("*");
  679. break;
  680. case CodeBinaryOperatorType.Divide:
  681. output.Write ("/");
  682. break;
  683. case CodeBinaryOperatorType.Modulus:
  684. output.Write ("%");
  685. break;
  686. case CodeBinaryOperatorType.Assign:
  687. output.Write ("=");
  688. break;
  689. case CodeBinaryOperatorType.IdentityInequality:
  690. output.Write ("!=");
  691. break;
  692. case CodeBinaryOperatorType.IdentityEquality:
  693. output.Write ("==");
  694. break;
  695. case CodeBinaryOperatorType.ValueEquality:
  696. output.Write ("==");
  697. break;
  698. case CodeBinaryOperatorType.BitwiseOr:
  699. output.Write ("|");
  700. break;
  701. case CodeBinaryOperatorType.BitwiseAnd:
  702. output.Write ("&");
  703. break;
  704. case CodeBinaryOperatorType.BooleanOr:
  705. output.Write ("||");
  706. break;
  707. case CodeBinaryOperatorType.BooleanAnd:
  708. output.Write ("&&");
  709. break;
  710. case CodeBinaryOperatorType.LessThan:
  711. output.Write ("<");
  712. break;
  713. case CodeBinaryOperatorType.LessThanOrEqual:
  714. output.Write ("<=");
  715. break;
  716. case CodeBinaryOperatorType.GreaterThan:
  717. output.Write (">");
  718. break;
  719. case CodeBinaryOperatorType.GreaterThanOrEqual:
  720. output.Write (">=");
  721. break;
  722. }
  723. }
  724. protected virtual void OutputParameters (CodeParameterDeclarationExpressionCollection parameters)
  725. {
  726. bool first = true;
  727. foreach (CodeParameterDeclarationExpression expr in parameters) {
  728. if (first)
  729. first = false;
  730. else
  731. output.Write (", ");
  732. GenerateExpression (expr);
  733. }
  734. }
  735. protected abstract void OutputType (CodeTypeReference t);
  736. protected virtual void OutputTypeAttributes (TypeAttributes attributes,
  737. bool isStruct,
  738. bool isEnum)
  739. {
  740. switch (attributes & TypeAttributes.VisibilityMask) {
  741. case TypeAttributes.NotPublic:
  742. // private by default
  743. break;
  744. case TypeAttributes.Public:
  745. case TypeAttributes.NestedPublic:
  746. output.Write ("public ");
  747. break;
  748. case TypeAttributes.NestedPrivate:
  749. output.Write ("private ");
  750. break;
  751. }
  752. if (isStruct)
  753. output.Write ("struct ");
  754. else if (isEnum)
  755. output.Write ("enum ");
  756. else {
  757. if ((attributes & TypeAttributes.Interface) != 0)
  758. output.Write ("interface ");
  759. else {
  760. if ((attributes & TypeAttributes.Sealed) != 0)
  761. output.Write ("sealed ");
  762. if ((attributes & TypeAttributes.Abstract) != 0)
  763. output.Write ("abstract ");
  764. output.Write ("class ");
  765. }
  766. }
  767. }
  768. protected virtual void OutputTypeNamePair (CodeTypeReference type,
  769. string name)
  770. {
  771. OutputType (type);
  772. output.Write (' ');
  773. output.Write (name);
  774. }
  775. protected abstract string QuoteSnippetString (string value);
  776. /*
  777. * ICodeGenerator
  778. */
  779. protected abstract string CreateEscapedIdentifier (string value);
  780. string ICodeGenerator.CreateEscapedIdentifier (string value)
  781. {
  782. return CreateEscapedIdentifier (value);
  783. }
  784. protected abstract string CreateValidIdentifier (string value);
  785. string ICodeGenerator.CreateValidIdentifier (string value)
  786. {
  787. return CreateValidIdentifier (value);
  788. }
  789. private void InitOutput (TextWriter output, CodeGeneratorOptions options)
  790. {
  791. if (options == null)
  792. options = new CodeGeneratorOptions ();
  793. this.output = new IndentedTextWriter (output, options.IndentString);
  794. this.options = options;
  795. }
  796. void ICodeGenerator.GenerateCodeFromCompileUnit (CodeCompileUnit compileUnit,
  797. TextWriter output,
  798. CodeGeneratorOptions options)
  799. {
  800. InitOutput (output, options);
  801. GenerateCompileUnit (compileUnit);
  802. }
  803. void ICodeGenerator.GenerateCodeFromExpression (CodeExpression expression,
  804. TextWriter output,
  805. CodeGeneratorOptions options)
  806. {
  807. InitOutput (output, options);
  808. GenerateExpression (expression);
  809. }
  810. void ICodeGenerator.GenerateCodeFromNamespace (CodeNamespace ns,
  811. TextWriter output,
  812. CodeGeneratorOptions options)
  813. {
  814. InitOutput (output, options);
  815. GenerateNamespace (ns);
  816. }
  817. void ICodeGenerator.GenerateCodeFromStatement (CodeStatement statement,
  818. TextWriter output,
  819. CodeGeneratorOptions options)
  820. {
  821. InitOutput (output, options);
  822. GenerateStatement (statement);
  823. }
  824. void ICodeGenerator.GenerateCodeFromType (CodeTypeDeclaration type,
  825. TextWriter output,
  826. CodeGeneratorOptions options)
  827. {
  828. InitOutput (output, options);
  829. GenerateType (type);
  830. }
  831. private void GenerateType (CodeTypeDeclaration type)
  832. {
  833. CodeTypeDeclaration prevType = this.currentType;
  834. this.currentType = type;
  835. InitOutput (output, options);
  836. foreach (CodeCommentStatement statement in type.Comments)
  837. GenerateCommentStatement (statement);
  838. GenerateTypeStart (type);
  839. CodeTypeMember [] members = new CodeTypeMember [type.Members.Count];
  840. type.Members.CopyTo (members, 0);
  841. int[] order = new int[members.Length];
  842. for (int n=0; n<members.Length; n++)
  843. order[n] = Array.IndexOf(memberTypes, members[n].GetType()) * members.Length + n;
  844. Array.Sort (order, members);
  845. // WARNING: if anything is missing in the foreach loop and you add it, add the type in
  846. // its corresponding place in CodeTypeMemberComparer class (below)
  847. foreach (CodeTypeMember member in members)
  848. {
  849. CodeTypeMember prevMember = this.currentMember;
  850. this.currentMember = member;
  851. if (options.BlankLinesBetweenMembers)
  852. output.WriteLine ();
  853. foreach (CodeCommentStatement statement in member.Comments)
  854. GenerateCommentStatement (statement);
  855. CodeMemberEvent eventm = member as CodeMemberEvent;
  856. if (eventm != null)
  857. {
  858. GenerateEvent (eventm, type);
  859. continue;
  860. }
  861. CodeMemberField field = member as CodeMemberField;
  862. if (field != null)
  863. {
  864. GenerateField (field);
  865. continue;
  866. }
  867. CodeEntryPointMethod epmethod = member as CodeEntryPointMethod;
  868. if (epmethod != null)
  869. {
  870. GenerateEntryPointMethod (epmethod, type);
  871. continue;
  872. }
  873. CodeTypeConstructor typeCtor = member as CodeTypeConstructor;
  874. if (typeCtor != null)
  875. {
  876. GenerateTypeConstructor (typeCtor);
  877. continue;
  878. }
  879. CodeConstructor ctor = member as CodeConstructor;
  880. if (ctor != null)
  881. {
  882. GenerateConstructor (ctor, type);
  883. continue;
  884. }
  885. CodeMemberMethod method = member as CodeMemberMethod;
  886. if (method != null)
  887. {
  888. GenerateMethod (method, type);
  889. continue;
  890. }
  891. CodeMemberProperty property = member as CodeMemberProperty;
  892. if (property != null)
  893. {
  894. GenerateProperty (property, type);
  895. continue;
  896. }
  897. CodeSnippetTypeMember snippet = member as CodeSnippetTypeMember;
  898. if (snippet != null)
  899. {
  900. GenerateSnippetMember (snippet);
  901. continue;
  902. }
  903. CodeTypeDeclaration subtype = member as CodeTypeDeclaration;
  904. if (subtype != null)
  905. {
  906. GenerateType (subtype);
  907. continue;
  908. }
  909. this.currentMember = prevMember;
  910. }
  911. GenerateTypeEnd (type);
  912. this.currentType = prevType;
  913. }
  914. protected abstract string GetTypeOutput (CodeTypeReference type);
  915. string ICodeGenerator.GetTypeOutput (CodeTypeReference type)
  916. {
  917. return GetTypeOutput (type);
  918. }
  919. protected abstract bool IsValidIdentifier (string value);
  920. bool ICodeGenerator.IsValidIdentifier (string value)
  921. {
  922. return IsValidIdentifier (value);
  923. }
  924. public static bool IsValidLanguageIndependentIdentifier (string value)
  925. {
  926. if (value == null)
  927. return false;
  928. if (value.Equals (string.Empty))
  929. return false;
  930. switch (char.GetUnicodeCategory (value[0]))
  931. {
  932. case UnicodeCategory.LetterNumber:
  933. case UnicodeCategory.LowercaseLetter:
  934. case UnicodeCategory.TitlecaseLetter:
  935. case UnicodeCategory.UppercaseLetter:
  936. case UnicodeCategory.OtherLetter:
  937. case UnicodeCategory.ModifierLetter:
  938. case UnicodeCategory.ConnectorPunctuation:
  939. if (value.Length > 1)
  940. {
  941. for (int x = 0; x < value.Length; x++)
  942. {
  943. switch (char.GetUnicodeCategory (value[x]))
  944. {
  945. case UnicodeCategory.LetterNumber:
  946. case UnicodeCategory.LowercaseLetter:
  947. case UnicodeCategory.TitlecaseLetter:
  948. case UnicodeCategory.UppercaseLetter:
  949. case UnicodeCategory.OtherLetter:
  950. case UnicodeCategory.ModifierLetter:
  951. case UnicodeCategory.ConnectorPunctuation:
  952. case UnicodeCategory.DecimalDigitNumber:
  953. case UnicodeCategory.NonSpacingMark:
  954. case UnicodeCategory.SpacingCombiningMark:
  955. case UnicodeCategory.Format:
  956. return true;
  957. }
  958. return false;
  959. }
  960. }
  961. else
  962. return true;
  963. break;
  964. }
  965. return false;
  966. }
  967. protected abstract bool Supports (GeneratorSupport supports);
  968. bool ICodeGenerator.Supports (GeneratorSupport value)
  969. {
  970. return Supports (value);
  971. }
  972. protected virtual void ValidateIdentifier (string value)
  973. {
  974. if (!(IsValidIdentifier (value)))
  975. throw new ArgumentException ("Identifier is invalid", "value");
  976. }
  977. [MonoTODO]
  978. public static void ValidateIdentifiers (CodeObject e)
  979. {
  980. throw new NotImplementedException();
  981. }
  982. void ICodeGenerator.ValidateIdentifier (string value)
  983. {
  984. ValidateIdentifier (value);
  985. }
  986. // The position in the array determines the order in which those
  987. // kind of CodeTypeMembers are generated. Less is more ;-)
  988. static Type [] memberTypes = { typeof (CodeMemberField),
  989. typeof (CodeSnippetTypeMember),
  990. typeof (CodeTypeConstructor),
  991. typeof (CodeConstructor),
  992. typeof (CodeMemberProperty),
  993. typeof (CodeMemberEvent),
  994. typeof (CodeMemberMethod),
  995. typeof (CodeTypeDeclaration),
  996. typeof (CodeEntryPointMethod)
  997. };
  998. }
  999. }