forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeDomContext.cs
More file actions
294 lines (253 loc) · 10.2 KB
/
CodeDomContext.cs
File metadata and controls
294 lines (253 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2013 FUJIWARA, Yusuke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion -- License Terms --
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security;
using System.Text;
using MsgPack.Serialization.AbstractSerializers;
namespace MsgPack.Serialization.CodeDomSerializers
{
internal class CodeDomContext : SerializerGenerationContext<CodeDomConstruct>, ISerializerCodeGenerationContext
{
public static readonly CodeCatchClause[] EmptyCatches = new CodeCatchClause[ 0 ];
public const string ConditionalExpressionHelperMethodName = "__Conditional";
public const string ConditionalExpressionHelperConditionParameterName = "condition";
public const string ConditionalExpressionHelperWhenTrueParameterName = "whenTrue";
public const string ConditionalExpressionHelperWhenFalseParameterName = "whenFalse";
private readonly Dictionary<SerializerFieldKey, string> _dependentSerializers = new Dictionary<SerializerFieldKey, string>();
private readonly Dictionary<RuntimeFieldHandle, string> _cachedFieldInfos = new Dictionary<RuntimeFieldHandle, string>();
private readonly Dictionary<RuntimeMethodHandle, string> _cachedMethodBases = new Dictionary<RuntimeMethodHandle, string>();
private readonly Dictionary<Type, CodeTypeDeclaration> _declaringTypes = new Dictionary<Type, CodeTypeDeclaration>();
private readonly SerializerCodeGenerationConfiguration _configuration;
private CodeTypeDeclaration _buildingType;
/// <summary>
/// Gets the current <see cref="CodeTypeDeclaration"/>.
/// </summary>
/// <value>
/// The current <see cref="CodeTypeDeclaration"/>.
/// </value>
public CodeTypeDeclaration DeclaringType
{
get { return this._buildingType; }
}
public CodeDomContext( SerializationContext context, SerializerCodeGenerationConfiguration configuration )
: base( context )
{
this._configuration = configuration;
}
public string GetSerializerFieldName( Type targetType, EnumMemberSerializationMethod enumSerializationMethod )
{
var key = new SerializerFieldKey( targetType, enumSerializationMethod );
string fieldName;
if ( !this._dependentSerializers.TryGetValue( key, out fieldName ) )
{
fieldName = "_serializer" + this._dependentSerializers.Count.ToString( CultureInfo.InvariantCulture );
this._dependentSerializers.Add( key, fieldName );
}
return fieldName;
}
public Dictionary<SerializerFieldKey, String> GetDependentSerializers()
{
return this._dependentSerializers;
}
public string GetCachedFieldInfoName( FieldInfo field )
{
var key = field.FieldHandle;
string fieldName;
if ( !this._cachedFieldInfos.TryGetValue( key, out fieldName ) )
{
fieldName = "_field" + field.DeclaringType.Name + "_" + field.Name + this._cachedFieldInfos.Count.ToString( CultureInfo.InvariantCulture );
this._cachedFieldInfos.Add( key, fieldName );
}
return fieldName;
}
public Dictionary<RuntimeFieldHandle, String> GetCachedFieldInfos()
{
return this._cachedFieldInfos;
}
public string GetCachedMethodBaseName( MethodBase method )
{
var key = method.MethodHandle;
string fieldName;
if ( !this._cachedMethodBases.TryGetValue( key, out fieldName ) )
{
fieldName = "_methodBase" + method.DeclaringType.Name + "_" + method.Name + this._cachedMethodBases.Count.ToString( CultureInfo.InvariantCulture );
this._cachedMethodBases.Add( key, fieldName );
}
return fieldName;
}
public Dictionary<RuntimeMethodHandle, String> GetCachedMethodBases()
{
return this._cachedMethodBases;
}
private readonly Dictionary<string, int> _uniqueVariableSuffixes = new Dictionary<string, int>();
/// <summary>
/// Determines that whether built-in serializer for specified type exists or not.
/// </summary>
/// <param name="type">The type for check.</param>
/// <returns>
/// <c>true</c> if built-in serializer for specified type exists; <c>false</c>, otherwise.
/// </returns>
/// <exception cref="System.NotImplementedException"></exception>
public bool BuiltInSerializerExists( Type type )
{
if ( type == null )
{
throw new ArgumentNullException( "type" );
}
return type.IsArray || SerializerRepository.Default.Contains( type );
}
/// <summary>
/// Gets a unique name of a local variable.
/// </summary>
/// <param name="prefix">The prefix of the variable.</param>
/// <returns>A unique name of a local variable.</returns>
public string GetUniqueVariableName( string prefix )
{
int counter;
if ( !this._uniqueVariableSuffixes.TryGetValue( prefix, out counter ) )
{
this._uniqueVariableSuffixes.Add( prefix, 0 );
return prefix;
}
this._uniqueVariableSuffixes[ prefix ] = counter + 1;
return prefix + counter.ToString( CultureInfo.InvariantCulture );
}
/// <summary>
/// Gets a value indicating whether the generated serializers will be internal to MsgPack library itself.
/// </summary>
/// <value>
/// <c>true</c> if the generated serializers are internal to MsgPack library itself; otherwise, <c>false</c>.
/// </value>
/// <remarks>
/// When you use MsgPack in Unity3D, you can import the library in source code form to your assets.
/// And, you may also import generated serializers together, then the generated serializers and MsgPack library will be same assembly ultimately.
/// It causes compilation error because some of overriding members have accessbility <c>FamilyOrAssembly</c>(<c>protected internal</c> in C#),
/// so the generated source code must have the accessibility when and only when they will be same assembly as MsgPack library itself.
/// </remarks>
public bool IsInternalToMsgPackLibrary { get { return this._configuration.IsInternalToMsgPackLibrary; } }
/// <summary>
/// Resets internal states for new type.
/// </summary>
/// <param name="targetType">Type of the target.</param>
protected override void ResetCore( Type targetType )
{
var declaringType = new CodeTypeDeclaration( IdentifierUtility.EscapeTypeName( targetType ) + "Serializer" );
declaringType.BaseTypes.Add(
targetType.GetIsEnum()
? typeof( EnumMessagePackSerializer<> ).MakeGenericType( targetType )
: typeof( MessagePackSerializer<> ).MakeGenericType( targetType ) );
declaringType.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference( typeof( GeneratedCodeAttribute ) ),
new CodeAttributeArgument( new CodePrimitiveExpression( "MsgPack.Serialization.CodeDomSerializers.CodeDomSerializerBuilder" ) ),
new CodeAttributeArgument( new CodePrimitiveExpression( this.GetType().Assembly.GetName().Version.ToString() ) )
)
);
declaringType.CustomAttributes.Add(
new CodeAttributeDeclaration( new CodeTypeReference( typeof( DebuggerNonUserCodeAttribute ) ) )
);
this._declaringTypes.Add( targetType, declaringType );
this._dependentSerializers.Clear();
this._cachedFieldInfos.Clear();
this._cachedMethodBases.Clear();
this._buildingType = declaringType;
this.Packer = CodeDomConstruct.Parameter( typeof( Packer ), "packer" );
this.PackToTarget = CodeDomConstruct.Parameter( targetType, "objectTree" );
this.Unpacker = CodeDomConstruct.Parameter( typeof( Unpacker ), "unpacker" );
this.UnpackToTarget = CodeDomConstruct.Parameter( targetType, "collection" );
}
/// <summary>
/// Resets internal states for new method.
/// </summary>
public void ResetMethodContext()
{
this._uniqueVariableSuffixes.Clear();
}
/// <summary>
/// Generates codes for this context.
/// </summary>
/// <returns>The path of generated files.</returns>
#if !NETFX_35
[SecuritySafeCritical]
#endif // !NETFX_35
public IEnumerable<string> Generate()
{
using ( var provider = CodeDomProvider.CreateProvider( this._configuration.Language ) )
{
var options =
new CodeGeneratorOptions
{
BlankLinesBetweenMembers = true,
ElseOnClosing = false,
IndentString = this._configuration.CodeIndentString,
VerbatimOrder = false
};
var directory =
Path.Combine(
this._configuration.OutputDirectory,
this._configuration.Namespace.Replace( Type.Delimiter, Path.DirectorySeparatorChar )
);
Directory.CreateDirectory( directory );
var result = new List<string>( _declaringTypes.Count );
foreach ( var declaringType in _declaringTypes )
{
var typeFileName = declaringType.Value.Name;
if ( declaringType.Value.TypeParameters.Count > 0 )
{
typeFileName += "`" + declaringType.Value.TypeParameters.Count.ToString( CultureInfo.InvariantCulture );
}
typeFileName += "." + provider.FileExtension;
var cn = new CodeNamespace( this._configuration.Namespace );
cn.Types.Add( declaringType.Value );
var cu = new CodeCompileUnit();
cu.Namespaces.Add( cn );
var filePath = Path.Combine( directory, typeFileName );
result.Add( filePath );
using ( var writer = new StreamWriter( filePath, false, Encoding.UTF8 ) )
{
provider.GenerateCodeFromCompileUnit( cu, writer, options );
}
}
return result;
}
}
/// <summary>
/// Creates the <see cref="CodeCompileUnit"/> for on-the-fly code generation for execution.
/// </summary>
/// <returns>
/// The newly created <see cref="CodeCompileUnit"/> for on-the-fly code generation for execution.
/// </returns>
public CodeCompileUnit CreateCodeCompileUnit()
{
var cn = new CodeNamespace( this._configuration.Namespace );
cn.Types.Add( this._buildingType );
var cu = new CodeCompileUnit();
cu.Namespaces.Add( cn );
return cu;
}
}
}