CSharpCodeGenerator.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. //
  2. // Mono.CSharp CSharpCodeProvider Class implementation
  3. //
  4. // Author:
  5. // Daniel Stodden ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc.
  8. //
  9. namespace Mono.CSharp
  10. {
  11. using System;
  12. using System.CodeDom;
  13. using System.CodeDom.Compiler;
  14. using System.IO;
  15. using System.Reflection;
  16. using System.Collections;
  17. internal class CSharpCodeGenerator
  18. : CodeGenerator
  19. {
  20. //
  21. // Constructors
  22. //
  23. public CSharpCodeGenerator()
  24. {
  25. }
  26. //
  27. // Properties
  28. //
  29. protected override string NullToken {
  30. get {
  31. return "null";
  32. }
  33. }
  34. //
  35. // Methods
  36. //
  37. protected override void GenerateArrayCreateExpression( CodeArrayCreateExpression expression )
  38. {
  39. //
  40. // This tries to replicate MS behavior as good as
  41. // possible.
  42. //
  43. // The Code-Array stuff in ms.net seems to be broken
  44. // anyways, or I'm too stupid to understand it.
  45. //
  46. // I'm sick of it. If you try to develop array
  47. // creations, test them on windows. If it works there
  48. // but not in mono, drop me a note. I'd be especially
  49. // interested in jagged-multidimensional combinations
  50. // with proper initialization :}
  51. //
  52. TextWriter output = Output;
  53. output.Write( "new " );
  54. CodeExpressionCollection initializers = expression.Initializers;
  55. CodeTypeReference createType = expression.CreateType;
  56. if ( initializers.Count > 0 ) {
  57. OutputType( createType );
  58. output.WriteLine( " {" );
  59. ++Indent;
  60. OutputExpressionList( initializers, true );
  61. --Indent;
  62. output.Write( "}" );
  63. } else {
  64. CodeTypeReference arrayType = createType.ArrayElementType;
  65. while ( arrayType != null ) {
  66. createType = arrayType;
  67. arrayType = arrayType.ArrayElementType;
  68. }
  69. OutputType( createType );
  70. output.Write( '[' );
  71. CodeExpression size = expression.SizeExpression;
  72. if ( size != null )
  73. GenerateExpression( size );
  74. else
  75. output.Write( expression.Size );
  76. output.Write( ']' );
  77. }
  78. }
  79. protected override void GenerateBaseReferenceExpression( CodeBaseReferenceExpression expression )
  80. {
  81. Output.Write( "base" );
  82. }
  83. protected override void GenerateCastExpression( CodeCastExpression expression )
  84. {
  85. TextWriter output = Output;
  86. output.Write( "((" );
  87. OutputType( expression.TargetType );
  88. output.Write( ")(" );
  89. GenerateExpression( expression.Expression );
  90. output.Write( "))" );
  91. }
  92. protected override void GenerateCompileUnitStart( CodeCompileUnit compileUnit )
  93. {
  94. GenerateComment( new CodeComment( "------------------------------------------------------------------------------" ) );
  95. GenerateComment( new CodeComment( " <autogenerated>" ) );
  96. GenerateComment( new CodeComment( " This code was generated by a tool." ) );
  97. GenerateComment( new CodeComment( " Mono Runtime Version: " + System.Environment.Version ) );
  98. GenerateComment( new CodeComment( "" ) );
  99. GenerateComment( new CodeComment( " Changes to this file may cause incorrect behavior and will be lost if " ) );
  100. GenerateComment( new CodeComment( " the code is regenerated." ) );
  101. GenerateComment( new CodeComment( " </autogenerated>" ) );
  102. GenerateComment( new CodeComment( "------------------------------------------------------------------------------" ) );
  103. Output.WriteLine();
  104. }
  105. protected override void GenerateDelegateCreateExpression( CodeDelegateCreateExpression expression )
  106. {
  107. TextWriter output = Output;
  108. output.Write( "new " );
  109. OutputType( expression.DelegateType );
  110. output.Write( '(' );
  111. CodeExpression targetObject = expression.TargetObject;
  112. if ( targetObject != null ) {
  113. GenerateExpression( targetObject );
  114. Output.Write( '.' );
  115. }
  116. output.Write( expression.MethodName );
  117. output.Write( ')' );
  118. }
  119. protected override void GenerateFieldReferenceExpression( CodeFieldReferenceExpression expression )
  120. {
  121. CodeExpression targetObject = expression.TargetObject;
  122. if ( targetObject != null ) {
  123. GenerateExpression( targetObject );
  124. Output.Write( '.' );
  125. }
  126. Output.Write( expression.FieldName );
  127. }
  128. protected override void GenerateArgumentReferenceExpression( CodeArgumentReferenceExpression expression )
  129. {
  130. Output.Write( expression.ParameterName );
  131. }
  132. protected override void GenerateVariableReferenceExpression( CodeVariableReferenceExpression expression )
  133. {
  134. Output.Write( expression.VariableName );
  135. }
  136. protected override void GenerateIndexerExpression( CodeIndexerExpression expression )
  137. {
  138. TextWriter output = Output;
  139. GenerateExpression( expression.TargetObject );
  140. output.Write( '[' );
  141. OutputExpressionList( expression.Indices );
  142. output.Write( ']' );
  143. }
  144. protected override void GenerateArrayIndexerExpression( CodeArrayIndexerExpression expression )
  145. {
  146. TextWriter output = Output;
  147. GenerateExpression( expression.TargetObject );
  148. output.Write( '[' );
  149. OutputExpressionList( expression.Indices );
  150. output.Write( ']' );
  151. }
  152. protected override void GenerateSnippetExpression( CodeSnippetExpression expression )
  153. {
  154. Output.Write( expression.Value );
  155. }
  156. protected override void GenerateMethodInvokeExpression( CodeMethodInvokeExpression expression )
  157. {
  158. TextWriter output = Output;
  159. GenerateMethodReferenceExpression( expression.Method );
  160. output.Write( '(' );
  161. OutputExpressionList( expression.Parameters );
  162. output.Write( ')' );
  163. }
  164. protected override void GenerateMethodReferenceExpression( CodeMethodReferenceExpression expression )
  165. {
  166. GenerateExpression( expression.TargetObject );
  167. Output.Write( '.' );
  168. Output.Write( expression.MethodName );
  169. }
  170. protected override void GenerateEventReferenceExpression( CodeEventReferenceExpression expression )
  171. {
  172. GenerateExpression( expression.TargetObject );
  173. Output.Write( '.' );
  174. Output.Write( expression.EventName );
  175. }
  176. protected override void GenerateDelegateInvokeExpression( CodeDelegateInvokeExpression expression )
  177. {
  178. GenerateExpression( expression.TargetObject );
  179. Output.Write( '(' );
  180. OutputExpressionList( expression.Parameters );
  181. Output.Write( ')' );
  182. }
  183. protected override void GenerateObjectCreateExpression( CodeObjectCreateExpression expression )
  184. {
  185. Output.Write( "new " );
  186. OutputType( expression.CreateType );
  187. Output.Write( '(' );
  188. OutputExpressionList( expression.Parameters );
  189. Output.Write( ')' );
  190. }
  191. protected override void GeneratePropertyReferenceExpression( CodePropertyReferenceExpression expression )
  192. {
  193. GenerateMemberReferenceExpression( expression.TargetObject,
  194. expression.PropertyName );
  195. }
  196. protected override void GeneratePropertySetValueReferenceExpression( CodePropertySetValueReferenceExpression expression )
  197. {
  198. Output.Write ( "value" );
  199. }
  200. protected override void GenerateThisReferenceExpression( CodeThisReferenceExpression expression )
  201. {
  202. Output.Write( "this" );
  203. }
  204. protected override void GenerateExpressionStatement( CodeExpressionStatement statement )
  205. {
  206. GenerateExpression( statement.Expression );
  207. Output.WriteLine( ';' );
  208. }
  209. protected override void GenerateIterationStatement( CodeIterationStatement statement )
  210. {
  211. TextWriter output = Output;
  212. output.Write( "for (" );
  213. GenerateStatement( statement.InitStatement );
  214. output.Write( "; " );
  215. GenerateExpression( statement.TestExpression );
  216. output.Write( "; " );
  217. GenerateStatement( statement.IncrementStatement );
  218. output.Write( ") " );
  219. GenerateStatements( statement.Statements );
  220. }
  221. protected override void GenerateThrowExceptionStatement( CodeThrowExceptionStatement statement )
  222. {
  223. Output.Write( "throw " );
  224. GenerateExpression( statement.ToThrow );
  225. }
  226. protected override void GenerateComment( CodeComment comment )
  227. {
  228. TextWriter output = Output;
  229. if ( comment.DocComment )
  230. output.Write( "/// " );
  231. else
  232. output.Write( "// " );
  233. output.WriteLine( comment.Text );
  234. }
  235. protected override void GenerateMethodReturnStatement( CodeMethodReturnStatement statement )
  236. {
  237. TextWriter output = Output;
  238. output.Write( "return " );
  239. GenerateExpression( statement.Expression );
  240. output.Write( ";" );
  241. }
  242. protected override void GenerateConditionStatement( CodeConditionStatement statement )
  243. {
  244. TextWriter output = Output;
  245. output.Write( "if (" );
  246. GenerateExpression( statement.Condition );
  247. output.WriteLine( ") {" );
  248. ++Indent;
  249. GenerateStatements( statement.TrueStatements );
  250. --Indent;
  251. output.Write( '}' );
  252. CodeStatementCollection falses = statement.FalseStatements;
  253. if ( falses.Count > 0 ) {
  254. if ( Options.ElseOnClosing )
  255. output.Write( ' ' );
  256. else
  257. output.WriteLine();
  258. output.WriteLine( "else {" );
  259. ++Indent;
  260. GenerateStatements( falses );
  261. --Indent;
  262. output.WriteLine( '}' );
  263. }
  264. }
  265. protected override void GenerateTryCatchFinallyStatement( CodeTryCatchFinallyStatement statement )
  266. {
  267. TextWriter output = Output;
  268. CodeGeneratorOptions options = Options;
  269. output.WriteLine( "try {" );
  270. ++Indent;
  271. GenerateStatements( statement.TryStatements );
  272. --Indent;
  273. output.Write( '}' );
  274. foreach ( CodeCatchClause clause in statement.CatchClauses ) {
  275. if ( options.ElseOnClosing )
  276. output.Write( ' ' );
  277. else
  278. output.WriteLine();
  279. output.Write( "catch (" );
  280. OutputTypeNamePair( clause.CatchExceptionType, clause.LocalName );
  281. output.WriteLine( ") {" );
  282. ++Indent;
  283. GenerateStatements( clause.Statements );
  284. --Indent;
  285. output.Write( '}' );
  286. }
  287. CodeStatementCollection finallies = statement.FinallyStatements;
  288. if ( finallies.Count > 0 ) {
  289. if ( options.ElseOnClosing )
  290. output.Write( ' ' );
  291. else
  292. output.WriteLine();
  293. output.WriteLine( "finally {" );
  294. ++Indent;
  295. GenerateStatements( finallies );
  296. --Indent;
  297. output.WriteLine( '}' );
  298. }
  299. output.WriteLine();
  300. }
  301. protected override void GenerateAssignStatement( CodeAssignStatement statement )
  302. {
  303. TextWriter output = Output;
  304. GenerateExpression( statement.Left );
  305. output.Write( " = " );
  306. GenerateExpression( statement.Right );
  307. output.WriteLine( ';' );
  308. }
  309. protected override void GenerateAttachEventStatement( CodeAttachEventStatement statement )
  310. {
  311. TextWriter output = Output;
  312. GenerateEventReferenceExpression( statement.Event );
  313. output.Write( " += " );
  314. GenerateExpression( statement.Listener );
  315. output.WriteLine( ';' );
  316. }
  317. protected override void GenerateRemoveEventStatement( CodeRemoveEventStatement statement )
  318. {
  319. TextWriter output = Output;
  320. GenerateEventReferenceExpression( statement.Event );
  321. Output.Write( " -= " );
  322. GenerateExpression( statement.Listener );
  323. output.WriteLine( ';' );
  324. }
  325. protected override void GenerateGotoStatement( CodeGotoStatement statement )
  326. {
  327. TextWriter output = Output;
  328. output.Write( "goto " );
  329. output.Write( statement.Label );
  330. output.Write( ";" );
  331. }
  332. protected override void GenerateLabeledStatement( CodeLabeledStatement statement )
  333. {
  334. TextWriter output = Output;
  335. output.Write( statement.Label );
  336. GenerateStatement( statement.Statement );
  337. }
  338. protected override void GenerateVariableDeclarationStatement( CodeVariableDeclarationStatement statement )
  339. {
  340. TextWriter output = Output;
  341. OutputTypeNamePair( statement.Type, statement.Name );
  342. CodeExpression initExpression = statement.InitExpression;
  343. if ( initExpression != null ) {
  344. output.Write( " = " );
  345. GenerateExpression( initExpression );
  346. }
  347. output.WriteLine( ';' );
  348. }
  349. protected override void GenerateLinePragmaStart( CodeLinePragma linePragma )
  350. {
  351. Output.Write( "<GenerateLinePragmaStart>" );
  352. }
  353. protected override void GenerateLinePragmaEnd( CodeLinePragma linePragma )
  354. {
  355. Output.Write( "<GenerateLinePragmaEnd>" );
  356. }
  357. protected override void GenerateEvent( CodeMemberEvent eventRef, CodeTypeDeclaration declaration )
  358. {
  359. Output.Write( "<GenerateEvent>" );
  360. }
  361. protected override void GenerateField( CodeMemberField field )
  362. {
  363. TextWriter output = Output;
  364. if (field.CustomAttributes.Count > 0)
  365. OutputAttributeDeclarations( field.CustomAttributes );
  366. MemberAttributes attributes = field.Attributes;
  367. OutputMemberAccessModifier( attributes );
  368. OutputFieldScopeModifier( attributes );
  369. OutputTypeNamePair( field.Type, field.Name );
  370. CodeExpression initExpression = field.InitExpression;
  371. if ( initExpression != null ) {
  372. output.Write( " = " );
  373. GenerateExpression( initExpression );
  374. }
  375. output.WriteLine( ';' );
  376. }
  377. protected override void GenerateSnippetMember( CodeSnippetTypeMember member )
  378. {
  379. Output.Write( "<GenerateSnippetMember>" );
  380. }
  381. protected override void GenerateEntryPointMethod( CodeEntryPointMethod method,
  382. CodeTypeDeclaration declaration )
  383. {
  384. method.Name = "Main";
  385. GenerateMethod( method, declaration );
  386. }
  387. protected override void GenerateMethod( CodeMemberMethod method,
  388. CodeTypeDeclaration declaration )
  389. {
  390. TextWriter output = Output;
  391. if (method.CustomAttributes.Count > 0)
  392. OutputAttributeDeclarations( method.CustomAttributes );
  393. MemberAttributes attributes = method.Attributes;
  394. OutputMemberAccessModifier( attributes );
  395. OutputMemberScopeModifier( attributes );
  396. OutputType( method.ReturnType );
  397. output.Write( ' ' );
  398. CodeTypeReference privateType = method.PrivateImplementationType;
  399. if ( privateType != null ) {
  400. OutputType( privateType );
  401. output.Write( '.' );
  402. }
  403. output.Write( method.Name );
  404. output.Write( '(' );
  405. OutputParameters( method.Parameters );
  406. output.Write( ')' );
  407. if ( (attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract )
  408. output.WriteLine( ';' );
  409. else {
  410. output.WriteLine( " {" );
  411. ++Indent;
  412. GenerateStatements( method.Statements );
  413. --Indent;
  414. output.WriteLine( '}' );
  415. }
  416. }
  417. protected override void GenerateProperty( CodeMemberProperty property,
  418. CodeTypeDeclaration declaration )
  419. {
  420. TextWriter output = Output;
  421. if (property.CustomAttributes.Count > 0)
  422. OutputAttributeDeclarations( property.CustomAttributes );
  423. MemberAttributes attributes = property.Attributes;
  424. OutputMemberAccessModifier( attributes );
  425. OutputFieldScopeModifier( attributes );
  426. OutputTypeNamePair( property.Type, property.Name );
  427. output.WriteLine (" {");
  428. ++Indent;
  429. if (property.HasGet)
  430. {
  431. output.WriteLine ("get {");
  432. ++Indent;
  433. GenerateStatements (property.GetStatements);
  434. output.WriteLine ();
  435. output.WriteLine ("}");
  436. --Indent;
  437. }
  438. if (property.HasSet)
  439. {
  440. output.WriteLine ("set {");
  441. ++Indent;
  442. GenerateStatements (property.SetStatements);
  443. output.WriteLine ();
  444. output.WriteLine ("}");
  445. --Indent;
  446. }
  447. --Indent;
  448. output.WriteLine ("}");
  449. }
  450. protected override void GenerateConstructor( CodeConstructor constructor,
  451. CodeTypeDeclaration declaration )
  452. {
  453. Output.Write( "<GenerateConstructor>" );
  454. }
  455. protected override void GenerateTypeConstructor( CodeTypeConstructor constructor )
  456. {
  457. Output.Write( "<GenerateTypeConstructor>" );
  458. }
  459. protected override void GenerateTypeStart( CodeTypeDeclaration declaration )
  460. {
  461. TextWriter output = Output;
  462. TypeAttributes attributes = declaration.TypeAttributes;
  463. OutputTypeAttributes( attributes,
  464. declaration.IsStruct,
  465. declaration.IsEnum );
  466. output.Write( declaration.Name );
  467. output.Write( ' ' );
  468. IEnumerator enumerator = declaration.BaseTypes.GetEnumerator();
  469. if ( enumerator.MoveNext() ) {
  470. CodeTypeReference type = (CodeTypeReference)enumerator.Current;
  471. output.Write( ": " );
  472. OutputType( type );
  473. while ( enumerator.MoveNext() ) {
  474. type = (CodeTypeReference)enumerator.Current;
  475. output.Write( ", " );
  476. OutputType( type );
  477. }
  478. output.Write( ' ' );
  479. }
  480. output.WriteLine( "{" );
  481. ++Indent;
  482. }
  483. protected override void GenerateTypeEnd( CodeTypeDeclaration declaration )
  484. {
  485. --Indent;
  486. Output.WriteLine( "}" );
  487. }
  488. protected override void GenerateNamespaceStart( CodeNamespace ns )
  489. {
  490. TextWriter output = Output;
  491. string name = ns.Name;
  492. if ( name != null && name != "" ) {
  493. output.Write( "namespace " );
  494. output.Write( name );
  495. output.WriteLine( " {" );
  496. ++Indent;
  497. }
  498. }
  499. protected override void GenerateNamespaceEnd( CodeNamespace ns )
  500. {
  501. string name = ns.Name;
  502. if ( name != null && name != "" ) {
  503. --Indent;
  504. Output.WriteLine( "}" );
  505. }
  506. }
  507. protected override void GenerateNamespaceImport( CodeNamespaceImport import )
  508. {
  509. TextWriter output = Output;
  510. output.Write( "using " );
  511. output.Write( import.Namespace );
  512. output.WriteLine( ';' );
  513. }
  514. protected override void GenerateAttributeDeclarationsStart( CodeAttributeDeclarationCollection attributes )
  515. {
  516. Output.Write( '[' );
  517. }
  518. protected override void GenerateAttributeDeclarationsEnd( CodeAttributeDeclarationCollection attributes )
  519. {
  520. Output.WriteLine( ']' );
  521. }
  522. protected override void OutputType( CodeTypeReference type )
  523. {
  524. Output.Write( GetTypeOutput( type ) );
  525. }
  526. protected override string QuoteSnippetString( string value )
  527. {
  528. return "\"" + value + "\"";
  529. // FIXME: escape quotes
  530. }
  531. private void GenerateDeclaration( CodeTypeReference type, string name, CodeExpression initExpression )
  532. {
  533. TextWriter output = Output;
  534. OutputTypeNamePair( type, name );
  535. if ( initExpression != null ) {
  536. output.Write( " = " );
  537. GenerateExpression( initExpression );
  538. }
  539. output.WriteLine( ';' );
  540. }
  541. private void GenerateMemberReferenceExpression( CodeExpression targetObject, string memberName )
  542. {
  543. GenerateExpression( targetObject );
  544. Output.Write( '.' );
  545. Output.Write( memberName );
  546. }
  547. /*
  548. * ICodeGenerator
  549. */
  550. //[MonoTODO]
  551. protected override string CreateEscapedIdentifier( string value )
  552. {
  553. return value;
  554. }
  555. //[MonoTODO]
  556. protected override string CreateValidIdentifier( string value )
  557. {
  558. return value;
  559. }
  560. protected override string GetTypeOutput( CodeTypeReference type )
  561. {
  562. string output;
  563. CodeTypeReference arrayType;
  564. arrayType = type.ArrayElementType;
  565. if ( arrayType != null )
  566. output = GetTypeOutput( arrayType );
  567. else {
  568. switch ( type.BaseType ) {
  569. case "System.Decimal":
  570. output = "decimal";
  571. break;
  572. case "System.Double":
  573. output = "double";
  574. break;
  575. case "System.Single":
  576. output = "float";
  577. break;
  578. case "System.Byte":
  579. output = "byte";
  580. break;
  581. case "System.SByte":
  582. output = "sbyte";
  583. break;
  584. case "System.Int32":
  585. output = "int";
  586. break;
  587. case "System.UInt32":
  588. output = "uint";
  589. break;
  590. case "System.Int64":
  591. output = "long";
  592. break;
  593. case "System.UInt64":
  594. output = "ulong";
  595. break;
  596. case "System.Int16":
  597. output = "short";
  598. break;
  599. case "System.UInt16":
  600. output = "ushort";
  601. break;
  602. case "System.Boolean":
  603. output = "bool";
  604. break;
  605. case "System.Char":
  606. output = "char";
  607. break;
  608. case "System.String":
  609. output = "string";
  610. break;
  611. case "System.Object":
  612. output = "object";
  613. break;
  614. case "System.Void":
  615. output = "void";
  616. break;
  617. default:
  618. output = type.BaseType;
  619. break;
  620. }
  621. }
  622. int rank = type.ArrayRank;
  623. if ( rank > 0 ) {
  624. output += "[";
  625. for ( --rank; rank > 0; --rank )
  626. output += ",";
  627. output += "]";
  628. }
  629. return output;
  630. }
  631. protected override bool IsValidIdentifier( string identifier )
  632. {
  633. return true;
  634. }
  635. protected override bool Supports( GeneratorSupport supports )
  636. {
  637. if ( (supports & GeneratorSupport.Win32Resources) != 0 )
  638. return false;
  639. return true;
  640. }
  641. #if false
  642. //[MonoTODO]
  643. public override void ValidateIdentifier( string identifier )
  644. {
  645. }
  646. #endif
  647. }
  648. }