CodeGenerator.cs 31 KB

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