mono/mono — pull request #216 by ilkerde (2012-01-14 16:16:13+00:00)
Title: Better CS1529 Compliance for CSharp Code Generator

This pull request enhances the C# code generator in `System.dll` to obey [CS1529] rules when having unnamed (aka. full-scope) namespaces defined.

#### Purpose
The fix specifically adresses a common scenario for generating assembly meta-data files, i.e. `AssemblyInfo.cs`. A typical `AssemblyInfo.cs` has imports defined on top, then following attribute declarations.
Example:

~~~~
using System.Reflection;
[assembly: AssemblyTitle("CSFun")]
~~~~

However, the current Mono.CSharp.Compiler would've rendered it the other way around:

~~~~
[assembly: AssemblyTitle("CSFun")]
using System.Reflection;
~~~~

This in turn would yield to compile error [CS1529].

#### Changes
The strategy for the fix is to change the rendering on `GenerateCompileUnit` method call in code generators. It renders unnamed namespaces _before_ attribute declarations and named namespaces _after_ attribute declarations. Thus, any imports of unnamed namespaces will appear before attribute declarations, as desired. The fix involves changes in `System.CodeDom.Compiler.CodeGenerator` as well as in `Mono.CSharp.CSharpCodeGenerator`, including tests.

#### Notes
The fix strategy might render strange code when there's imports, attributes and types involved in an unnamed namespace. Example:

~~~~
using System.Reflection;
public class Fun {}
[assembly: AssemblyTitle("CSFun")]
~~~~

Obviously looking not very straight-forward. The expectation would be to have the type declaration rendered last. Nonetheless, this is a quite rare scenario and fixing it would involve a more serious change in generator classes. Hence, I didn't went to beautify this as well.

I'd like to have this changes reviewed by a peer, preferably someone experienced in the `System.CodeDom` area.

[CS1529]: http://msdn.microsoft.com/library/ad30sfht.aspx