Merge pull request #1156 from felfert/master
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / MapCodeGenerator.cs
index d6e6ec5476fabfd7b77304b2998d5789925b2103..1887ab24723f765700b0594cbb1fe3f8c8ad10b6 100644 (file)
 using System.CodeDom;
 using System.CodeDom.Compiler;
 using System.Collections;
-#if NET_2_0
 using System.ComponentModel;
 using System.Diagnostics;
-#endif
 using System.Globalization;
 using System.Xml.Schema;
 
@@ -44,29 +42,32 @@ namespace System.Xml.Serialization {
        internal class MapCodeGenerator {
 
                CodeNamespace codeNamespace;
-               CodeCompileUnit codeCompileUnit;
+//             CodeCompileUnit codeCompileUnit;
                CodeAttributeDeclarationCollection includeMetadata;
-               XmlTypeMapping exportedAnyType = null;
+               XmlTypeMapping exportedAnyType;
                protected bool includeArrayTypes;
-               ICodeGenerator codeGen;
+               CodeDomProvider codeProvider;
                CodeGenerationOptions options;
+               CodeIdentifiers identifiers;
 
                Hashtable exportedMaps = new Hashtable ();
                Hashtable includeMaps = new Hashtable ();
 
                public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeGenerationOptions options)
                {
-                       this.codeCompileUnit = codeCompileUnit;
+//                     this.codeCompileUnit = codeCompileUnit;
                        this.codeNamespace = codeNamespace;
                        this.options = options;
+                       this.identifiers = new CodeIdentifiers ();
                }
 
-               public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, ICodeGenerator codeGen, CodeGenerationOptions options, Hashtable mappings)
+               public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings)
                {
-                       this.codeCompileUnit = codeCompileUnit;
+//                     this.codeCompileUnit = codeCompileUnit;
                        this.codeNamespace = codeNamespace;
-                       this.codeGen = codeGen;
                        this.options = options;
+                       this.codeProvider = codeProvider;
+                       this.identifiers = new CodeIdentifiers ((codeProvider.LanguageOptions & LanguageOptions.CaseInsensitive) == 0);
 //                     this.mappings = mappings;
                }
 
@@ -92,18 +93,18 @@ namespace System.Xml.Serialization {
                        ExportMembersMapCode (dummyClass, (ClassMap)xmlMembersMapping.ObjectMap, xmlMembersMapping.Namespace, null);
                }
 
-               public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
+               public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping, bool isTopLevel)
                {
-                       ExportMapCode (xmlTypeMapping);
+                       ExportMapCode (xmlTypeMapping, isTopLevel);
                        RemoveInclude (xmlTypeMapping);
                }
 
-               void ExportMapCode (XmlTypeMapping map)
+               void ExportMapCode (XmlTypeMapping map, bool isTopLevel)
                {
                        switch (map.TypeData.SchemaType)
                        {
                                case SchemaTypes.Enum:
-                                       ExportEnumCode (map);
+                                       ExportEnumCode (map, isTopLevel);
                                        break;
 
                                case SchemaTypes.Array:
@@ -111,7 +112,7 @@ namespace System.Xml.Serialization {
                                        break;
 
                                case SchemaTypes.Class:
-                                       ExportClassCode (map);
+                                       ExportClassCode (map, isTopLevel);
                                        break;
 
                                case SchemaTypes.XmlSerializable:
@@ -122,16 +123,16 @@ namespace System.Xml.Serialization {
                        }
                }
 
-               void ExportClassCode (XmlTypeMapping map)
+               void ExportClassCode (XmlTypeMapping map, bool isTopLevel)
                {
                        CodeTypeDeclaration codeClass;
-                       
                        if (IsMapExported (map)) {
-                               // Regenerate attributes, since things may have changed
                                codeClass = GetMapDeclaration (map);
                                if (codeClass != null) {
-                                       codeClass.CustomAttributes = null;
-                                       GenerateClass (map, codeClass);
+                                       // Regenerate attributes, since things may have changed
+                                       codeClass.CustomAttributes.Clear ();
+                                       AddClassAttributes (codeClass);
+                                       GenerateClass (map, codeClass, isTopLevel);
                                        ExportDerivedTypeAttributes (map, codeClass);
                                }
                                return;
@@ -143,7 +144,7 @@ namespace System.Xml.Serialization {
                                SetMapExported (map, null);
                                foreach (XmlTypeMapping dmap in exportedAnyType.DerivedTypes) {
                                        if (IsMapExported (dmap) || !dmap.IncludeInSchema) continue;
-                                       ExportTypeMapping (dmap);
+                                       ExportTypeMapping (dmap, false);
                                        AddInclude (dmap);
                                }
                                return;
@@ -155,40 +156,20 @@ namespace System.Xml.Serialization {
                        AddCodeType (codeClass, map.Documentation);
                        codeClass.Attributes = MemberAttributes.Public;
 
-#if NET_2_0
-                       codeClass.IsPartial = CodeGenerator.Supports(GeneratorSupport.PartialTypes);
-
-                       CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
-                               new CodeTypeReference (typeof(GeneratedCodeAttribute)));
-                       generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
-                               new CodePrimitiveExpression ("System.Xml")));
-                       generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
-                               new CodePrimitiveExpression ("2.0.50727.42")));
-                       codeClass.CustomAttributes.Add (generatedCodeAttribute);
-
-                       codeClass.CustomAttributes.Add (new CodeAttributeDeclaration (
-                               new CodeTypeReference (typeof (SerializableAttribute))));
-                       codeClass.CustomAttributes.Add (new CodeAttributeDeclaration (
-                               new CodeTypeReference (typeof (DebuggerStepThroughAttribute))));
+                       codeClass.IsPartial = CodeProvider.Supports(GeneratorSupport.PartialTypes);
+                       AddClassAttributes (codeClass);
 
-                       CodeAttributeDeclaration designerCategoryAttribute = new CodeAttributeDeclaration (
-                               new CodeTypeReference (typeof(DesignerCategoryAttribute)));
-                       designerCategoryAttribute.Arguments.Add (new CodeAttributeArgument (
-                               new CodePrimitiveExpression ("code")));
-                       codeClass.CustomAttributes.Add (designerCategoryAttribute);
-#endif
-
-                       GenerateClass (map, codeClass);
+                       GenerateClass (map, codeClass, isTopLevel);
                        ExportDerivedTypeAttributes (map, codeClass);
                        
                        ExportMembersMapCode (codeClass, (ClassMap)map.ObjectMap, map.XmlTypeNamespace, map.BaseMap);
 
                        if (map.BaseMap != null && map.BaseMap.TypeData.SchemaType != SchemaTypes.XmlNode)
                        {
-                               CodeTypeReference ctr = new CodeTypeReference (map.BaseMap.TypeData.FullTypeName);
+                               CodeTypeReference ctr = GetDomType (map.BaseMap.TypeData, false);
                                codeClass.BaseTypes.Add (ctr);
                                if (map.BaseMap.IncludeInSchema) {
-                                       ExportMapCode (map.BaseMap);
+                                       ExportMapCode (map.BaseMap, false);
                                        AddInclude (map.BaseMap);
                                }
                        }
@@ -211,16 +192,26 @@ namespace System.Xml.Serialization {
                                if (codeClass.CustomAttributes == null) 
                                        codeClass.CustomAttributes = new CodeAttributeDeclarationCollection ();
 
-                               ExportMapCode (tm);
+                               ExportMapCode (tm, false);
                                ExportDerivedTypes (tm, codeClass);
                        }
                }
 
                void ExportMembersMapCode (CodeTypeDeclaration codeClass, ClassMap map, string defaultNamespace, XmlTypeMapping baseMap)
                {
+                       ICollection attributes = map.AttributeMembers;
+                       ICollection members = map.ElementMembers;
+
+                       // collect names
+                       if (attributes != null)
+                               foreach (XmlTypeMapMemberAttribute attr in attributes)
+                                       identifiers.AddUnique (attr.Name, attr);
+                       if (members != null)
+                               foreach (XmlTypeMapMemberElement member in members)
+                                       identifiers.AddUnique (member.Name, member);
+
                        // Write attributes
 
-                       ICollection attributes = map.AttributeMembers;
                        if (attributes != null) {
                                foreach (XmlTypeMapMemberAttribute attr in attributes) {
                                        if (baseMap != null && DefinedInBaseMap (baseMap, attr)) continue;
@@ -228,7 +219,7 @@ namespace System.Xml.Serialization {
                                }
                        }
 
-                       ICollection members = map.ElementMembers;
+                       members = map.ElementMembers;
                                
                        if (members != null)
                        {
@@ -277,12 +268,12 @@ namespace System.Xml.Serialization {
 
                CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, TypeData type, string name)
                {
-                       return CreateFieldMember (codeClass, GetDomType (type), name, System.DBNull.Value, null, null);
+                       return CreateFieldMember (codeClass, GetDomType (type, false), name, System.DBNull.Value, null, null);
                }
 
                CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMember member)
                {
-                       return CreateFieldMember (codeClass, GetDomType (member.TypeData), member.Name, member.DefaultValue, member.TypeData, member.Documentation);
+                       return CreateFieldMember (codeClass, GetDomType (member.TypeData, member.RequiresNullable), member.Name, member.DefaultValue, member.TypeData, member.Documentation);
                }
                
                CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, CodeTypeReference type, string name, object defaultValue, TypeData defaultType, string documentation)
@@ -291,7 +282,7 @@ namespace System.Xml.Serialization {
                        CodeTypeMember codeProp = null;
 
                        if ((options & CodeGenerationOptions.GenerateProperties) > 0) {
-                               string field = CodeIdentifier.MakeCamel (name + "Field");
+                               string field = identifiers.AddUnique (CodeIdentifier.MakeCamel (name + "Field"), name);
                                codeField = new CodeMemberField (type, field);
                                codeField.Attributes = MemberAttributes.Private;
                                codeClass.Members.Add (codeField);
@@ -332,13 +323,13 @@ namespace System.Xml.Serialization {
                        if (attributes.Count > 0) codeField.CustomAttributes = attributes;
 
                        if (attinfo.MappedType != null) {
-                               ExportMapCode (attinfo.MappedType);
+                               ExportMapCode (attinfo.MappedType, false);
                                RemoveInclude (attinfo.MappedType);
                        }
 
                        if (attinfo.TypeData.IsValueType && attinfo.IsOptionalValueType)
                        {
-                               codeField = CreateFieldMember (codeClass, typeof(bool), attinfo.Name + "Specified");
+                               codeField = CreateFieldMember (codeClass, typeof(bool), identifiers.MakeUnique (attinfo.Name + "Specified"));
                                codeField.Attributes = MemberAttributes.Public;
                                GenerateSpecifierMember (codeField);
                        }
@@ -361,7 +352,7 @@ namespace System.Xml.Serialization {
                        
                        if (member.TypeData.IsValueType && member.IsOptionalValueType)
                        {
-                               codeField = CreateFieldMember (codeClass, typeof(bool), member.Name + "Specified");
+                               codeField = CreateFieldMember (codeClass, typeof(bool), identifiers.MakeUnique (member.Name + "Specified"));
                                codeField.Attributes = MemberAttributes.Public;
                                GenerateSpecifierMember (codeField);
                        }
@@ -380,14 +371,15 @@ namespace System.Xml.Serialization {
                        
                        foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
                        {
-                               if (ExportExtraElementAttributes (attributes, einfo, defaultNamespace, defaultType))
-                                       continue;
-
-                               GenerateElementInfoMember (attributes, member, einfo, defaultType, defaultNamespace, addAlwaysAttr, forceUseMemberName);
                                if (einfo.MappedType != null) {
-                                       ExportMapCode (einfo.MappedType);
+                                       ExportMapCode (einfo.MappedType, false);
                                        RemoveInclude (einfo.MappedType);
                                }
+
+                               if (ExportExtraElementAttributes (attributes, einfo, defaultNamespace, defaultType))
+                                       continue;
+
+                               GenerateElementInfoMember (attributes, member, einfo, defaultType, defaultNamespace, addAlwaysAttr, forceUseMemberName | addAlwaysAttr);
                        }
 
                        GenerateElementMember (attributes, member);
@@ -444,7 +436,7 @@ namespace System.Xml.Serialization {
                                if (ainfo.MappedType != null) {
                                        if (!IsMapExported (ainfo.MappedType) && includeArrayTypes)
                                                AddInclude (ainfo.MappedType);
-                                       ExportMapCode (ainfo.MappedType);
+                                       ExportMapCode (ainfo.MappedType, false);
                                }
                        }
 
@@ -463,7 +455,7 @@ namespace System.Xml.Serialization {
                                if (ainfo.MappedType != null) {
                                        if (!IsMapExported (ainfo.MappedType) && includeArrayTypes)
                                                AddInclude (ainfo.MappedType);
-                                       ExportMapCode (ainfo.MappedType);
+                                       ExportMapCode (ainfo.MappedType, false);
                                }
                        }
                }
@@ -481,7 +473,7 @@ namespace System.Xml.Serialization {
                        return false;
                }
 
-               void ExportEnumCode (XmlTypeMapping map)
+               void ExportEnumCode (XmlTypeMapping map, bool isTopLevel)
                {
                        if (IsMapExported (map)) return;
 
@@ -496,20 +488,18 @@ namespace System.Xml.Serialization {
                        if (emap.IsFlags)
                                codeEnum.CustomAttributes.Add (new CodeAttributeDeclaration ("System.FlagsAttribute"));
 
-#if NET_2_0
                        CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
                                new CodeTypeReference (typeof(GeneratedCodeAttribute)));
                        generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
                                new CodePrimitiveExpression ("System.Xml")));
                        generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
-                               new CodePrimitiveExpression ("2.0.50727.42")));
+                               new CodePrimitiveExpression (Consts.FxFileVersion)));
                        codeEnum.CustomAttributes.Add (generatedCodeAttribute);
 
                        codeEnum.CustomAttributes.Add (new CodeAttributeDeclaration (
                                new CodeTypeReference (typeof (SerializableAttribute))));
-#endif
 
-                       GenerateEnum (map, codeEnum);
+                       GenerateEnum (map, codeEnum, isTopLevel);
                        
                        int flag = 1;
                        foreach (EnumMap.EnumMapMember emem in emap.Members)
@@ -603,11 +593,35 @@ namespace System.Xml.Serialization {
                        AddComments (type, comments);
                        codeNamespace.Types.Add (type);
                }
+
+               void AddClassAttributes (CodeTypeDeclaration codeClass)
+               {
+                       CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
+                               new CodeTypeReference (typeof (GeneratedCodeAttribute)));
+                       generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
+                               new CodePrimitiveExpression ("System.Xml")));
+                       generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
+                               new CodePrimitiveExpression (Consts.FxFileVersion)));
+                       codeClass.CustomAttributes.Add (generatedCodeAttribute);
+
+                       codeClass.CustomAttributes.Add (new CodeAttributeDeclaration (
+                               new CodeTypeReference (typeof (SerializableAttribute))));
+                       codeClass.CustomAttributes.Add (new CodeAttributeDeclaration (
+                               new CodeTypeReference (typeof (DebuggerStepThroughAttribute))));
+
+                       CodeAttributeDeclaration designerCategoryAttribute = new CodeAttributeDeclaration (
+                               new CodeTypeReference (typeof (DesignerCategoryAttribute)));
+                       designerCategoryAttribute.Arguments.Add (new CodeAttributeArgument (
+                               new CodePrimitiveExpression ("code")));
+                       codeClass.CustomAttributes.Add (designerCategoryAttribute);
+               }
                
-               CodeTypeReference GetDomType (TypeData data)
+               CodeTypeReference GetDomType (TypeData data, bool requiresNullable)
                {
+                       if (data.IsValueType && (data.IsNullable || requiresNullable))
+                               return new CodeTypeReference ("System.Nullable", new CodeTypeReference (data.FullTypeName));
                        if (data.SchemaType == SchemaTypes.Array)
-                               return new CodeTypeReference (GetDomType (data.ListItemTypeData),1);
+                               return new CodeTypeReference (GetDomType (data.ListItemTypeData, false),1);
                        else
                                return new CodeTypeReference (data.FullTypeName);
                }
@@ -616,22 +630,20 @@ namespace System.Xml.Serialization {
 
                #region Private Properties
 
-#if NET_2_0
-               private ICodeGenerator CodeGenerator {
+               private CodeDomProvider CodeProvider {
                        get {
-                               if (codeGen == null) {
-                                       codeGen = new CSharpCodeProvider ().CreateGenerator ();
+                               if (codeProvider == null) {
+                                       codeProvider = new CSharpCodeProvider ();
                                }
-                               return codeGen;
+                               return codeProvider;
                        }
                }
-#endif
 
                #endregion
 
                #region Overridable methods
 
-               protected virtual void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass)
+               protected virtual void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass, bool isTopLevel)
                {
                }
                
@@ -652,15 +664,16 @@ namespace System.Xml.Serialization {
                                        throw new InvalidOperationException ("Type " + typeData.TypeName + " not supported");
 
                                IFormattable defaultValueFormattable = defaultValue as IFormattable;
-                               CodeFieldReferenceExpression fref = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (typeData.FullTypeName), defaultValueFormattable != null ? defaultValueFormattable.ToString(null, CultureInfo.InvariantCulture) : defaultValue.ToString ());
+                               CodeFieldReferenceExpression fref = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (GetDomType (typeData, false)), defaultValueFormattable != null ? defaultValueFormattable.ToString(null, CultureInfo.InvariantCulture) : defaultValue.ToString ());
                                CodeAttributeArgument arg = new CodeAttributeArgument (fref);
                                AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", arg);
-                               internalField.InitExpression = fref;
+                               //internalField.InitExpression = fref;
                        }
                        else
                        {
+                               defaultValue = defaultValue is decimal ? (object) ('"' + ((decimal) defaultValue).ToString (CultureInfo.InvariantCulture) + '"') : defaultValue;
                                AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", GetArg (defaultValue));
-                               internalField.InitExpression = new CodePrimitiveExpression (defaultValue);
+                               //internalField.InitExpression = new CodePrimitiveExpression (defaultValue);
                        }
                }
                
@@ -691,8 +704,8 @@ namespace System.Xml.Serialization {
                protected virtual void GenerateUnnamedAnyElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, string defaultNamespace)
                {
                }
-               
-               protected virtual void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum)
+
+               protected virtual void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum, bool isTopLevel)
                {
                }