Skip to content

Commit c44cbc0

Browse files
committed
Initial commit of Roslyn based code generator.
This is also prototype of opt-in model for .NET Core drops.
1 parent 4f5df65 commit c44cbc0

37 files changed

Lines changed: 7945 additions & 119 deletions

MsgPack.sln

Lines changed: 176 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
6+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
7+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
8+
<ProjectGuid>{B10240C1-BB07-47A3-B056-CE52B5194D86}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>MsgPack</RootNamespace>
12+
<AssemblyName>MsgPack.CodeGeneration.CSharp</AssemblyName>
13+
<DefaultLanguage>ja-JP</DefaultLanguage>
14+
<FileAlignment>512</FileAlignment>
15+
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
16+
<TargetFrameworkProfile>
17+
</TargetFrameworkProfile>
18+
<TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug\</OutputPath>
25+
<DefineConstants>TRACE;DEBUG;NETSTANDARD1_3;CSHARP</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
<DocumentationFile>bin\Debug\MsgPack.CodeGeneration.CSharp.XML</DocumentationFile>
29+
</PropertyGroup>
30+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
31+
<DebugType>pdbonly</DebugType>
32+
<Optimize>true</Optimize>
33+
<OutputPath>bin\Release\</OutputPath>
34+
<DefineConstants>TRACE;NETSTANDARD1_3;CSHARP</DefineConstants>
35+
<ErrorReport>prompt</ErrorReport>
36+
<WarningLevel>4</WarningLevel>
37+
<DocumentationFile>bin\Release\MsgPack.CodeGeneration.CSharp.XML</DocumentationFile>
38+
</PropertyGroup>
39+
<PropertyGroup>
40+
<SignAssembly>true</SignAssembly>
41+
</PropertyGroup>
42+
<PropertyGroup>
43+
<AssemblyOriginatorKeyFile>..\MsgPack.snk</AssemblyOriginatorKeyFile>
44+
</PropertyGroup>
45+
<ItemGroup>
46+
<!-- A reference to the entire .NET Framework is automatically included -->
47+
<None Include="..\MsgPack.snk">
48+
<Link>MsgPack.snk</Link>
49+
</None>
50+
<None Include="project.json" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<Compile Include="..\CommonAssemblyInfo.cs">
54+
<Link>Properties\CommonAssemblyInfo.cs</Link>
55+
</Compile>
56+
<Compile Include="Properties\AssemblyInfo.cs" />
57+
<Compile Include="Serialization\CodeTreeSerializers\CodeTreeConstruct.cs" />
58+
<Compile Include="Serialization\CodeTreeSerializers\CodeTreeContext.cs" />
59+
<Compile Include="Serialization\CodeTreeSerializers\CodeTreeSerializerBuilder.cs" />
60+
<Compile Include="Serialization\CSharpCodeTreeBasedSerializerCodeGenerator.cs" />
61+
<Compile Include="Serialization\CodeTreeSerializers\ExpressionCodeTreeConstruct.cs" />
62+
<Compile Include="Serialization\CodeTreeSerializers\ParameterCodeTreeConstruct.cs" />
63+
<Compile Include="Serialization\CodeTreeSerializers\StatementCodeTreeConstruct.cs" />
64+
<Compile Include="Serialization\CodeTreeSerializers\Syntax.cs" />
65+
<Compile Include="Serialization\CodeTreeSerializers\VariableCodeTreeConstruct.cs" />
66+
</ItemGroup>
67+
<ItemGroup>
68+
<ProjectReference Include="..\MsgPack.CodeGeneration.Core\MsgPack.CodeGeneration.Core.csproj">
69+
<Project>{efe532ea-77c8-4877-96c2-e79fd101b2ab}</Project>
70+
<Name>MsgPack.CodeGeneration.Core</Name>
71+
</ProjectReference>
72+
<ProjectReference Include="..\MsgPack.Core\MsgPack.Core.csproj">
73+
<Project>{08d9a49d-736e-4cad-919e-72ca8c1d4a9f}</Project>
74+
<Name>MsgPack.Core</Name>
75+
</ProjectReference>
76+
<ProjectReference Include="..\MsgPack.Serialization\MsgPack.Serialization.csproj">
77+
<Project>{153ced68-9905-4b1f-9790-f463840895e2}</Project>
78+
<Name>MsgPack.Serialization</Name>
79+
</ProjectReference>
80+
</ItemGroup>
81+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
82+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
83+
Other similar extension points exist, see Microsoft.Common.targets.
84+
<Target Name="BeforeBuild">
85+
</Target>
86+
<Target Name="AfterBuild">
87+
</Target>
88+
-->
89+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#region -- License Terms --
2+
//
3+
// MessagePack for CLI
4+
//
5+
// Copyright (C) 2016 FUJIWARA, Yusuke
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
#endregion -- License Terms --
20+
21+
using System.Reflection;
22+
using System.Runtime.CompilerServices;
23+
using System.Security;
24+
25+
[assembly: AssemblyTitle( "MessagePack for CLI(.NET/Mono)" )]
26+
[assembly: AssemblyDescription( "MessagePack for CLI(.NET/Mono) C# code generator library." )]
27+
28+
[assembly: AssemblyFileVersion( "0.7.2259.1047" )]
29+
30+
[assembly: AllowPartiallyTrustedCallers]
31+
32+
#if DEBUG || PERFORMANCE_TEST
33+
[assembly: InternalsVisibleTo( "MsgPack.CodeGeneration.CSharp.UnitTest, PublicKey=0024000004800000940000000602000000240000525341310004000001000100a967de8de9d45380b93a6aa56f64fc2cb2d3c9d4b400e00de01f31ba9e15cf5ca95926dbf8760cce413eabd711e23df0c133193a570da8a3bb1bdc00ef170fccb2bc033266fa5346442c9cf0b071133d5b484845eab17095652aeafeeb71193506b8294d9c8c91e3fd01cc50bdbc2d0eb78dd655bb8cd0bd3cdbbcb192549cb4" )]
34+
#endif
35+
36+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#region -- License Terms --
2+
//
3+
// MessagePack for CLI
4+
//
5+
// Copyright (C) 2016 FUJIWARA, Yusuke
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
#endregion -- License Terms --
20+
21+
using System;
22+
23+
using MsgPack.Serialization.CodeTreeSerializers;
24+
25+
namespace MsgPack.Serialization
26+
{
27+
/// <summary>
28+
/// Provides front-end for Roslyn Code Analyzer based, portable C# serializer source code generator.
29+
/// </summary>
30+
/// <remarks>
31+
/// You must call <see cref="Configure()"/> in advance to use Roslyn Code Analyzer based C# source code generation.
32+
/// If you miss it, Code DOM based legacy generator is used in desktop .NET Framework build (including Mono), or failed on other (.NET Core, etc.) based build.
33+
/// </remarks>
34+
public static class CSharpCodeTreeBasedSerializerCodeGenerator
35+
{
36+
// based on <system.codedom> in root web.config.
37+
private static readonly string[] LanguageIdentifiers =
38+
new[] { "C#", "CSharp", "CS" };
39+
40+
// TODO: VisualBasic are vb;vbs;visualbasic;vbscript
41+
42+
/// <summary>
43+
/// Configures C# generator and enables in <see cref="SerializerGenerator"/>.
44+
/// </summary>
45+
public static void Configure()
46+
{
47+
CodeTreeSerializerCodeGeneratorFactory.EnsureInitialized();
48+
CodeTreeSerializerCodeGeneratorFactory.RegisterFactory(
49+
LanguageIdentifiers,
50+
( context, configuration ) => new CodeTreeContext( context, configuration, "cs" ),
51+
context => type => new CodeTreeSerializerBuilder( type, type.GetCollectionTraits( CollectionTraitOptions.Full, context.CompatibilityOptions.AllowNonCollectionEnumerableTypes ) )
52+
);
53+
}
54+
}
55+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#region -- License Terms --
2+
//
3+
// MessagePack for CLI
4+
//
5+
// Copyright (C) 2016 FUJIWARA, Yusuke
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
#endregion -- License Terms --
20+
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Globalization;
24+
25+
using Microsoft.CodeAnalysis.CSharp.Syntax;
26+
27+
using MsgPack.Serialization.AbstractSerializers;
28+
29+
namespace MsgPack.Serialization.CodeTreeSerializers
30+
{
31+
internal abstract class CodeTreeConstruct : ICodeConstruct
32+
{
33+
public TypeDefinition ContextType { get; }
34+
35+
public virtual bool IsExpression => false;
36+
37+
public virtual bool IsStatement => false;
38+
39+
protected CodeTreeConstruct( TypeDefinition contextType )
40+
{
41+
this.ContextType = contextType;
42+
}
43+
44+
public virtual ParameterSyntax AsParameter()
45+
{
46+
throw new InvalidOperationException(
47+
String.Format( CultureInfo.CurrentCulture, "Cannot get '{0}' as parameter declaration.", this )
48+
);
49+
}
50+
51+
public virtual ExpressionSyntax AsExpression()
52+
{
53+
throw new InvalidOperationException(
54+
String.Format( CultureInfo.CurrentCulture, "Cannot get '{0}' as expression.", this )
55+
);
56+
}
57+
58+
public virtual IEnumerable<StatementSyntax> AsStatements()
59+
{
60+
throw new InvalidOperationException(
61+
String.Format( CultureInfo.CurrentCulture, "Cannot get '{0}' as statements.", this )
62+
);
63+
}
64+
65+
public static CodeTreeConstruct Expression( TypeDefinition contextType, ExpressionSyntax expression )
66+
=> new ExpressionCodeTreeConstruct( contextType, expression );
67+
68+
public static CodeTreeConstruct Statement( StatementSyntax statement )
69+
=> new StatementCodeTreeConstruct( new[] { statement } );
70+
71+
public static CodeTreeConstruct Statement( IEnumerable<StatementSyntax> statements )
72+
=> new StatementCodeTreeConstruct( statements );
73+
74+
public static CodeTreeConstruct Variable( TypeDefinition contextType, VariableDeclarationSyntax syntax )
75+
=> new VariableCodeTreeConstruct( contextType, syntax );
76+
77+
public static CodeTreeConstruct Parameter( TypeDefinition contextType, IdentifierNameSyntax syntax )
78+
=> new ParameterCodeTreeConstruct( contextType, syntax );
79+
}
80+
}

0 commit comments

Comments
 (0)