CodeGenerator.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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 if (currentType is CodeTypeDelegate)
  760. output.Write ("delegate ");
  761. else {
  762. if ((attributes & TypeAttributes.Sealed) != 0)
  763. output.Write ("sealed ");
  764. if ((attributes & TypeAttributes.Abstract) != 0)
  765. output.Write ("abstract ");
  766. output.Write ("class ");
  767. }
  768. }
  769. }
  770. protected virtual void OutputTypeNamePair (CodeTypeReference type,
  771. string name)
  772. {
  773. OutputType (type);
  774. output.Write (' ');
  775. output.Write (name);
  776. }
  777. protected abstract string QuoteSnippetString (string value);
  778. /*
  779. * ICodeGenerator
  780. */
  781. protected abstract string CreateEscapedIdentifier (string value);
  782. string ICodeGenerator.CreateEscapedIdentifier (string value)
  783. {
  784. return CreateEscapedIdentifier (value);
  785. }
  786. protected abstract string CreateValidIdentifier (string value);
  787. string ICodeGenerator.CreateValidIdentifier (string value)
  788. {
  789. return CreateValidIdentifier (value);
  790. }
  791. private void InitOutput (TextWriter output, CodeGeneratorOptions options)
  792. {
  793. if (options == null)
  794. options = new CodeGeneratorOptions ();
  795. this.output = new IndentedTextWriter (output, options.IndentString);
  796. this.options = options;
  797. }
  798. void ICodeGenerator.GenerateCodeFromCompileUnit (CodeCompileUnit compileUnit,
  799. TextWriter output,
  800. CodeGeneratorOptions options)
  801. {
  802. InitOutput (output, options);
  803. GenerateCompileUnit (compileUnit);
  804. }
  805. void ICodeGenerator.GenerateCodeFromExpression (CodeExpression expression,
  806. TextWriter output,
  807. CodeGeneratorOptions options)
  808. {
  809. InitOutput (output, options);
  810. GenerateExpression (expression);
  811. }
  812. void ICodeGenerator.GenerateCodeFromNamespace (CodeNamespace ns,
  813. TextWriter output,
  814. CodeGeneratorOptions options)
  815. {
  816. InitOutput (output, options);
  817. GenerateNamespace (ns);
  818. }
  819. void ICodeGenerator.GenerateCodeFromStatement (CodeStatement statement,
  820. TextWriter output,
  821. CodeGeneratorOptions options)
  822. {
  823. InitOutput (output, options);
  824. GenerateStatement (statement);
  825. }
  826. void ICodeGenerator.GenerateCodeFromType (CodeTypeDeclaration type,
  827. TextWriter output,
  828. CodeGeneratorOptions options)
  829. {
  830. InitOutput (output, options);
  831. GenerateType (type);
  832. }
  833. private void GenerateType (CodeTypeDeclaration type)
  834. {
  835. CodeTypeDelegate del = type as CodeTypeDelegate;
  836. if (del != null)
  837. GenerateDelegate (del);
  838. else
  839. GenerateNonDelegateType (type);
  840. }
  841. private void GenerateDelegate (CodeTypeDelegate type)
  842. {
  843. CodeTypeDeclaration prevType = this.currentType;
  844. this.currentType = type;
  845. InitOutput (output, options);
  846. foreach (CodeCommentStatement statement in type.Comments)
  847. GenerateCommentStatement (statement);
  848. GenerateTypeStart (type);
  849. OutputParameters (type.Parameters);
  850. GenerateTypeEnd (type);
  851. this.currentType = prevType;
  852. }
  853. private void GenerateNonDelegateType (CodeTypeDeclaration type)
  854. {
  855. CodeTypeDeclaration prevType = this.currentType;
  856. this.currentType = type;
  857. InitOutput (output, options);
  858. foreach (CodeCommentStatement statement in type.Comments)
  859. GenerateCommentStatement (statement);
  860. GenerateTypeStart (type);
  861. CodeTypeMember [] members = new CodeTypeMember [type.Members.Count];
  862. type.Members.CopyTo (members, 0);
  863. int[] order = new int[members.Length];
  864. for (int n=0; n<members.Length; n++)
  865. order[n] = Array.IndexOf(memberTypes, members[n].GetType()) * members.Length + n;
  866. Array.Sort (order, members);
  867. // WARNING: if anything is missing in the foreach loop and you add it, add the type in
  868. // its corresponding place in CodeTypeMemberComparer class (below)
  869. foreach (CodeTypeMember member in members)
  870. {
  871. CodeTypeMember prevMember = this.currentMember;
  872. this.currentMember = member;
  873. if (options.BlankLinesBetweenMembers)
  874. output.WriteLine ();
  875. foreach (CodeCommentStatement statement in member.Comments)
  876. GenerateCommentStatement (statement);
  877. CodeMemberEvent eventm = member as CodeMemberEvent;
  878. if (eventm != null)
  879. {
  880. GenerateEvent (eventm, type);
  881. continue;
  882. }
  883. CodeMemberField field = member as CodeMemberField;
  884. if (field != null)
  885. {
  886. GenerateField (field);
  887. continue;
  888. }
  889. CodeEntryPointMethod epmethod = member as CodeEntryPointMethod;
  890. if (epmethod != null)
  891. {
  892. GenerateEntryPointMethod (epmethod, type);
  893. continue;
  894. }
  895. CodeTypeConstructor typeCtor = member as CodeTypeConstructor;
  896. if (typeCtor != null)
  897. {
  898. GenerateTypeConstructor (typeCtor);
  899. continue;
  900. }
  901. CodeConstructor ctor = member as CodeConstructor;
  902. if (ctor != null)
  903. {
  904. GenerateConstructor (ctor, type);
  905. continue;
  906. }
  907. CodeMemberMethod method = member as CodeMemberMethod;
  908. if (method != null)
  909. {
  910. GenerateMethod (method, type);
  911. continue;
  912. }
  913. CodeMemberProperty property = member as CodeMemberProperty;
  914. if (property != null)
  915. {
  916. GenerateProperty (property, type);
  917. continue;
  918. }
  919. CodeSnippetTypeMember snippet = member as CodeSnippetTypeMember;
  920. if (snippet != null)
  921. {
  922. GenerateSnippetMember (snippet);
  923. continue;
  924. }
  925. CodeTypeDeclaration subtype = member as CodeTypeDeclaration;
  926. if (subtype != null)
  927. {
  928. GenerateType (subtype);
  929. continue;
  930. }
  931. this.currentMember = prevMember;
  932. }
  933. GenerateTypeEnd (type);
  934. this.currentType = prevType;
  935. }
  936. protected abstract string GetTypeOutput (CodeTypeReference type);
  937. string ICodeGenerator.GetTypeOutput (CodeTypeReference type)
  938. {
  939. return GetTypeOutput (type);
  940. }
  941. protected abstract bool IsValidIdentifier (string value);
  942. bool ICodeGenerator.IsValidIdentifier (string value)
  943. {
  944. return IsValidIdentifier (value);
  945. }
  946. public static bool IsValidLanguageIndependentIdentifier (string value)
  947. {
  948. if (value == null)
  949. return false;
  950. if (value.Equals (string.Empty))
  951. return false;
  952. switch (char.GetUnicodeCategory (value[0]))
  953. {
  954. case UnicodeCategory.LetterNumber:
  955. case UnicodeCategory.LowercaseLetter:
  956. case UnicodeCategory.TitlecaseLetter:
  957. case UnicodeCategory.UppercaseLetter:
  958. case UnicodeCategory.OtherLetter:
  959. case UnicodeCategory.ModifierLetter:
  960. case UnicodeCategory.ConnectorPunctuation:
  961. if (value.Length > 1)
  962. {
  963. for (int x = 0; x < value.Length; x++)
  964. {
  965. switch (char.GetUnicodeCategory (value[x]))
  966. {
  967. case UnicodeCategory.LetterNumber:
  968. case UnicodeCategory.LowercaseLetter:
  969. case UnicodeCategory.TitlecaseLetter:
  970. case UnicodeCategory.UppercaseLetter:
  971. case UnicodeCategory.OtherLetter:
  972. case UnicodeCategory.ModifierLetter:
  973. case UnicodeCategory.ConnectorPunctuation:
  974. case UnicodeCategory.DecimalDigitNumber:
  975. case UnicodeCategory.NonSpacingMark:
  976. case UnicodeCategory.SpacingCombiningMark:
  977. case UnicodeCategory.Format:
  978. return true;
  979. }
  980. return false;
  981. }
  982. }
  983. else
  984. return true;
  985. break;
  986. }
  987. return false;
  988. }
  989. protected abstract bool Supports (GeneratorSupport supports);
  990. bool ICodeGenerator.Supports (GeneratorSupport value)
  991. {
  992. return Supports (value);
  993. }
  994. protected virtual void ValidateIdentifier (string value)
  995. {
  996. if (!(IsValidIdentifier (value)))
  997. throw new ArgumentException ("Identifier is invalid", "value");
  998. }
  999. [MonoTODO]
  1000. public static void ValidateIdentifiers (CodeObject e)
  1001. {
  1002. throw new NotImplementedException();
  1003. }
  1004. void ICodeGenerator.ValidateIdentifier (string value)
  1005. {
  1006. ValidateIdentifier (value);
  1007. }
  1008. // The position in the array determines the order in which those
  1009. // kind of CodeTypeMembers are generated. Less is more ;-)
  1010. static Type [] memberTypes = { typeof (CodeMemberField),
  1011. typeof (CodeSnippetTypeMember),
  1012. typeof (CodeTypeConstructor),
  1013. typeof (CodeConstructor),
  1014. typeof (CodeMemberProperty),
  1015. typeof (CodeMemberEvent),
  1016. typeof (CodeMemberMethod),
  1017. typeof (CodeTypeDeclaration),
  1018. typeof (CodeEntryPointMethod)
  1019. };
  1020. }
  1021. }