Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity.Design / System / Data / EntityModel / Emitters / Utils.cs
1 //---------------------------------------------------------------------
2 // <copyright file="Utils.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 using System;
11 using System.Collections.Specialized;
12 using System.Collections.Generic;
13 using System.Text;
14 using System.Data;
15 using System.Data.Metadata.Edm;
16 using System.Data.EntityModel.SchemaObjectModel;
17 using System.Data.Entity.Design.Common;
18 using System.Diagnostics;
19 using System.Reflection;
20
21
22 namespace System.Data.EntityModel.Emitters
23 {
24     /// <summary>
25     /// Summary description for Utils
26     /// </summary>
27     internal static class Utils
28     {
29         #region Static Fields
30
31         public const string AdoFrameworkNamespace = "System.Data.Objects";
32         public const string AdoFrameworkDataClassesNamespace = "System.Data.Objects.DataClasses";
33         public const string AdoFrameworkMetadataEdmNamespace = "System.Data.Metadata.Edm";
34         public const string AdoEntityClientNamespace = "System.Data.EntityClient";
35
36         public const string SetValidValueMethodName = "SetValidValue";
37         public const string ReportPropertyChangingMethodName = "ReportPropertyChanging";
38         public const string ReportPropertyChangedMethodName = "ReportPropertyChanged";
39         public const string GetValidValueMethodName = "GetValidValue";
40         public const string VerifyComplexObjectIsNotNullName = "VerifyComplexObjectIsNotNull";
41
42
43         // to guarantee uniqueness these must all be unique, begin with and end with an underscore and not contain internal underscores
44         private static string[] _privateMemberPrefixes = new string[(int)PrivateMemberPrefixId.Count]
45         {
46             "_",
47             "_Initialize_",
48             "PropertyInfo",
49             "_pi",
50         };
51
52         // suffix that is added to field names to create a boolean field used to indicate whether or
53         // not a complex property has been explicitly initialized 
54         private static string _complexPropertyInitializedSuffix = "Initialized";
55         private static List<KeyValuePair<string, Type>> _typeReservedNames = InitializeTypeReservedNames();
56
57         /// <summary>
58         /// Initialize some statics that cannot be initialized in member declaration...
59         /// </summary>
60         static List<KeyValuePair<string, Type>> InitializeTypeReservedNames()
61         {
62             Dictionary<string, Type> typeReservedNames = new Dictionary<string, Type>(StringComparer.Ordinal);
63             BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public 
64                     | BindingFlags.Instance | BindingFlags.Static;
65             foreach (MemberInfo member in TypeReference.ComplexTypeBaseClassType.GetMembers(bindingFlags))
66             {
67                 if (ShouldReserveName(member))
68                 {
69                     if (!typeReservedNames.ContainsKey(member.Name))
70                     {
71                         typeReservedNames.Add(member.Name, typeof(ComplexType));
72                     }
73                 }
74             }
75
76             foreach (MemberInfo member in TypeReference.EntityTypeBaseClassType.GetMembers(bindingFlags))
77             {
78                 if (ShouldReserveName(member))
79                 {
80                     if (typeReservedNames.ContainsKey(member.Name))
81                     {
82                         if (typeReservedNames[member.Name] == typeof(ComplexType))
83                         {
84                             // apply to all types
85                             typeReservedNames[member.Name] = null;
86                         }
87                     }
88                     else
89                     {
90                         typeReservedNames.Add(member.Name, typeof(EntityType));
91                     }
92                 }
93             }
94
95             List<KeyValuePair<string, Type>> pairs = new List<KeyValuePair<string, Type>>();
96             foreach (KeyValuePair<string, Type> pair in typeReservedNames)
97             {
98                 pairs.Add(pair);
99             }
100
101             return pairs;
102         }
103
104         private static bool ShouldReserveName(MemberInfo member)
105         {
106             if (member is EventInfo)
107             {
108                 return ShouldReserveName((EventInfo)member);
109             }
110             else if(member is FieldInfo)
111             {
112                 return ShouldReserveName((FieldInfo)member);
113             }
114             else if(member is MethodBase)
115             {
116                 return ShouldReserveName((MethodBase)member);
117             }
118             else if(member is PropertyInfo)
119             {
120                 return ShouldReserveName((PropertyInfo)member);
121             }
122             else
123             {
124                 Debug.Assert(member is Type, "Did you add a new type of member?");
125                 return ShouldReserveName((Type)member);
126             }
127
128         }
129
130         private static bool ShouldReserveName(EventInfo member)
131         {
132             bool hasNonPrivate = false;
133
134             MethodInfo miAdd = member.GetAddMethod();
135             if (miAdd != null)
136             {
137                 hasNonPrivate |= ShouldReserveName(miAdd, false);
138             }
139
140             MethodInfo miRemove = member.GetRemoveMethod();
141             if (miRemove != null)
142             {
143                 hasNonPrivate |= ShouldReserveName(miRemove, false);
144             }
145
146             return hasNonPrivate;
147         }
148
149         private static bool ShouldReserveName(PropertyInfo member)
150         {
151             bool hasNonPrivate = false;
152
153             MethodInfo miSet = member.GetSetMethod();
154             if (miSet != null)
155             {
156                 hasNonPrivate |= ShouldReserveName(miSet, false);
157             }
158
159             MethodInfo miGet = member.GetGetMethod();
160             if (miGet != null)
161             {
162                 hasNonPrivate |= ShouldReserveName(miGet, false);
163             }
164
165             return hasNonPrivate;
166         }
167
168         private static bool ShouldReserveName(FieldInfo member)
169         {
170             return !member.IsPrivate && !member.IsAssembly && 
171                 !member.IsSpecialName;
172         }
173
174         private static bool ShouldReserveName(MethodBase member, bool checkForSpecial)
175         {
176             return !member.IsPrivate && !member.IsAssembly &&
177                 (!checkForSpecial || !member.IsSpecialName);
178         }
179
180         private static bool ShouldReserveName(MethodBase member)
181         {
182             return ShouldReserveName(member, true);
183         }
184
185         private static bool ShouldReserveName(Type member)
186         {
187             // we don't want to keep types
188             return false;
189         }
190         #endregion
191
192         #region Public Methods
193         /// <summary>
194         /// 
195         /// </summary>
196         /// <param name="text"></param>
197         /// <returns></returns>
198         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
199         public static string CamelCase(string text)
200         {
201             if ( string.IsNullOrEmpty(text) )
202                 return text;
203
204             if ( text.Length == 1 )
205                 return text[0].ToString(System.Globalization.CultureInfo.InvariantCulture).ToLowerInvariant();
206
207             return text[0].ToString(System.Globalization.CultureInfo.InvariantCulture).ToLowerInvariant()+text.Substring(1);
208         }
209
210         public static string FixParameterName(string name)
211         {
212             // FxCop consider 'iD' as violation, we will change any property that is 'id'(case insensitive) to 'ID'
213             if (StringComparer.OrdinalIgnoreCase.Equals(name, "id"))
214             {
215                 // it is an abreviation not an acronym so it should be all lower case
216                 return "id";
217             }
218             return CamelCase(name);
219         }
220
221         /// <summary>
222         /// 
223         /// </summary>
224         /// <param name="propName"></param>
225         /// <returns></returns>
226         public static string FieldNameFromPropName(string propName)
227         {
228             return PrivateMemberPrefix(PrivateMemberPrefixId.Field)+propName;
229         }
230
231         /// <summary>
232         /// Generate the name of a field that is used to indicate whether a complex property has been explicitly initialized
233         /// </summary>
234         /// <param name="propName">Name of the property associated that with this field</param>
235         /// <returns>Generated field name</returns>
236         public static string ComplexPropertyInitializedNameFromPropName(string propName)
237         {
238             return FieldNameFromPropName(propName) + _complexPropertyInitializedSuffix;
239         }
240         
241         /// <summary>
242         /// get the prefix ussed for a private member
243         /// </summary>
244         /// <param name="id"></param>
245         /// <returns></returns>
246         public static string PrivateMemberPrefix(PrivateMemberPrefixId id)
247         {
248             return _privateMemberPrefixes[(int)id];
249         }
250
251         /// <summary>
252         /// 
253         /// </summary>
254         /// <param name="name"></param>
255         /// <returns></returns>
256         public static string FQAdoFrameworkName( string name )
257         {
258             return AdoFrameworkNamespace + "." + name;
259         }
260
261         /// <summary>
262         /// 
263         /// </summary>
264         /// <param name="name"></param>
265         /// <returns></returns>
266         public static string FQAdoFrameworkDataClassesName( string name )
267         {
268             return AdoFrameworkDataClassesNamespace + "." + name;
269         }
270
271         /// <summary>
272         /// 
273         /// </summary>
274         /// <param name="name">unqualifed name of the type</param>
275         /// <returns></returns>
276         public static string FQAdoFrameworkMetadataEdmName(string name)
277         {
278             return AdoFrameworkMetadataEdmNamespace + "." + name;
279         }
280
281         /// <summary>
282         /// 
283         /// </summary>
284         /// <param name="name"></param>
285         /// <returns></returns>
286         public static string FQAdoEntityClientName(string name)
287         {
288             return AdoEntityClientNamespace + "." + name;
289         }
290
291         /// <summary>
292         /// 
293         /// </summary>
294         /// <param name="element"></param>
295         /// <param name="modelType"></param>
296         /// <returns></returns>
297         public static bool TryGetPrimitiveTypeKind(EdmType type, out PrimitiveTypeKind modelType )
298         {
299             if (!MetadataUtil.IsPrimitiveType(type))
300             {
301                 // set it to something bogus because I have to
302                 modelType = PrimitiveTypeKind.Binary;
303                 return false;
304             }
305
306             modelType = ((PrimitiveType)type).PrimitiveTypeKind;
307             return true;
308         }
309
310         /// <summary>
311         /// 
312         /// </summary>
313         /// <param name="name"></param>
314         /// <returns></returns>
315         public static string[] SplitName(string name)
316         {
317             Debug.Assert(!string.IsNullOrEmpty(name), "name parameter is null or empty");
318
319             if ( name.Length > 0 && name[0] == '.' )
320                 return name.Substring(1).Split('.');
321
322             return name.Split('.');
323         }
324
325         public static string GetFullyQualifiedCodeGenerationAttributeName(string attribute)
326         {
327             return XmlConstants.CodeGenerationSchemaNamespace + ":" + attribute;
328         }
329
330         /// <summary>
331         /// check if a name is reserved for a type
332         /// </summary>
333         /// <param name="type">the object representing the schema type being defined</param>
334         /// <param name="name">the member name</param>
335         /// <returns>true if the name is reserved by the type</returns>
336         public static bool DoesTypeReserveMemberName(StructuralType type, string name, StringComparison comparison)
337         {
338             Type reservingType = null;
339             if (!TryGetReservedName(name,comparison, out reservingType))
340             {
341                 return false;
342             }
343
344             // if reserving types is null it means the name is reserved for all types.
345             if (reservingType == null)
346             {
347                 return true;
348             }
349
350             return (reservingType == type.GetType());
351         }
352
353         public static bool TryGetReservedName(string name, StringComparison comparison, out Type applyToSpecificType)
354         {
355             applyToSpecificType = null;
356             foreach(KeyValuePair<string, Type> pair in _typeReservedNames)
357             {
358                 if (pair.Key.Equals(name, comparison))
359                 {
360                     applyToSpecificType = pair.Value;
361                     return true;
362                 }
363             }
364
365             return false;
366         }
367
368
369         #endregion
370     }
371 }