Skip to content

Commit a01116e

Browse files
committed
[generator] Remove GenerationInfo shared state.
1 parent 2d789b6 commit a01116e

File tree

6 files changed

+26
-66
lines changed

6 files changed

+26
-66
lines changed

tools/generator/CodeGenerationOptions.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
@@ -111,20 +112,23 @@ public string GetSafeIdentifier (string name)
111112
return name.Replace ('$', '_');
112113
}
113114

114-
Dictionary<string,string> short_file_names = new Dictionary<string, string> ();
115+
readonly Dictionary<string,string> short_file_names = new Dictionary<string, string> ();
115116

116117
public string GetFileName (string fullName)
117118
{
118119
if (!UseShortFileNames)
119120
return fullName;
120-
string s;
121-
if (short_file_names.TryGetValue (fullName, out s))
121+
122+
lock (short_file_names) {
123+
if (short_file_names.TryGetValue (fullName, out var s))
124+
return s;
125+
126+
s = short_file_names.Count.ToString ();
127+
short_file_names [fullName] = s;
128+
122129
return s;
123-
s = short_file_names.Count.ToString ();
124-
short_file_names [fullName] = s;
125-
return s;
130+
}
126131
}
127132
}
128-
129133
}
130134

tools/generator/CodeGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve
175175
if (gen.IsGeneratable)
176176
gen.Generate (opt, gen_info);
177177

178+
178179
ClassGen.GenerateTypeRegistrations (opt, gen_info);
179180
ClassGen.GenerateEnumList (gen_info);
180181

tools/generator/GenerationInfo.cs

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
@@ -13,71 +14,36 @@ public class GenerationInfo {
1314

1415
public GenerationInfo (string csdir, string javadir, string assembly)
1516
{
16-
this.csdir = csdir;
17-
this.javadir = javadir;
18-
this.assembly = assembly;
17+
CSharpDir = csdir;
18+
JavaDir = javadir;
19+
Assembly = assembly;
1920
}
2021

21-
string assembly;
22-
public string Assembly {
23-
get { return assembly; }
24-
}
25-
26-
string csdir;
27-
public string CSharpDir {
28-
get { return csdir; }
29-
}
30-
31-
string javadir;
32-
public string JavaDir {
33-
get { return javadir; }
34-
}
35-
36-
string member;
37-
public string CurrentMember {
38-
get { return typename + "." + member; }
39-
set { member = value; }
40-
}
41-
42-
string typename;
43-
public string CurrentType {
44-
get { return typename; }
45-
set { typename = value; }
46-
}
47-
48-
List<string> generated_files = new List<string> ();
49-
public IEnumerable<string> GeneratedFiles {
50-
get { return generated_files; }
51-
}
22+
public string Assembly { get; }
23+
public string CSharpDir { get; }
24+
public string JavaDir { get; }
25+
public ConcurrentBag<string> GeneratedFiles { get; } = new ConcurrentBag<string> ();
26+
public ConcurrentBag<string> Enums { get; } = new ConcurrentBag<string> ();
27+
public ConcurrentBag<KeyValuePair<string, string>> TypeRegistrations { get; } = new ConcurrentBag<KeyValuePair<string, string>> ();
5228

5329
public StreamWriter OpenStream (string name)
5430
{
5531
if (!Directory.Exists (CSharpDir))
5632
Directory.CreateDirectory (CSharpDir);
5733
string filename = Path.Combine (CSharpDir, name + ".cs");
5834

59-
generated_files.Add (filename);
6035
var sw = new StreamWriter (File.Create (filename));
36+
GeneratedFiles.Add (filename);
6137
return sw;
6238
}
6339

64-
List<string> enums = new List<string> ();
65-
public ICollection<string> Enums {
66-
get { return enums; }
67-
}
68-
69-
List<KeyValuePair<string, string>> type_registrations = new List<KeyValuePair<string, string>> ();
70-
public ICollection<KeyValuePair<string, string>> TypeRegistrations {
71-
get { return type_registrations; }
72-
}
73-
7440
internal void GenerateLibraryProjectFile (CodeGeneratorOptions options, IEnumerable<string> enumFiles, string path = null)
7541
{
7642
if (path == null) {
7743
var name = Assembly ?? "GeneratedFiles";
7844
int idx = name.IndexOf (',');
7945
name = idx < 0 ? name : name.Substring (0, idx);
80-
path = Path.Combine (csdir, name + ".projitems");
46+
path = Path.Combine (CSharpDir, name + ".projitems");
8147
}
8248

8349
var msbuild = XNamespace.Get ("http://schemas.microsoft.com/developer/msbuild/2003");

tools/generator/Java.Interop.Tools.Generator.ObjectModel/ClassGen.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Text;
6-
using System.Xml;
7-
85
using Java.Interop.Tools.TypeNameMappings;
9-
106
using Xamarin.Android.Binder;
117

12-
using MonoDroid.Utils;
13-
148
namespace MonoDroid.Generation
159
{
1610

@@ -244,8 +238,6 @@ public bool IsExplicitlyImplementedMethod (string sig)
244238

245239
public override void Generate (CodeGenerationOptions opt, GenerationInfo gen_info)
246240
{
247-
gen_info.CurrentType = FullName;
248-
249241
using (var sw = gen_info.OpenStream (opt.GetFileName (FullName))) {
250242
sw.WriteLine ("using System;");
251243
sw.WriteLine ("using System.Collections.Generic;");
@@ -269,7 +261,7 @@ public static void GenerateTypeRegistrations (CodeGenerationOptions opt, Generat
269261
using (var sw = gen_info.OpenStream (opt.GetFileName ("Java.Interop.__TypeRegistrations"))) {
270262

271263
Dictionary<string, List<KeyValuePair<string, string>>> mapping = new Dictionary<string, List<KeyValuePair<string, string>>> ();
272-
foreach (KeyValuePair<string, string> reg in gen_info.TypeRegistrations) {
264+
foreach (KeyValuePair<string, string> reg in gen_info.TypeRegistrations.OrderBy (p => p.Key, StringComparer.OrdinalIgnoreCase)) {
273265
int ls = reg.Key.LastIndexOf ('/');
274266
string package = ls >= 0 ? reg.Key.Substring (0, ls) : "";
275267

@@ -345,7 +337,7 @@ public static void GenerateTypeRegistrations (CodeGenerationOptions opt, Generat
345337
public static void GenerateEnumList (GenerationInfo gen_info)
346338
{
347339
using (var sw = new StreamWriter (File.Create (Path.Combine (gen_info.CSharpDir, "enumlist")))) {
348-
foreach (string e in gen_info.Enums)
340+
foreach (string e in gen_info.Enums.OrderBy (p => p, StringComparer.OrdinalIgnoreCase))
349341
sw.WriteLine (e);
350342
}
351343
}

tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,6 @@ protected void GenerateAnnotationAttribute (CodeGenerationOptions opt, Generatio
869869
var baseName = Namespace.Length > 0 ? FullName.Substring (Namespace.Length + 1) : FullName;
870870
var attrClassNameBase = baseName.Substring (TypeNamePrefix.Length) + "Attribute";
871871
var localFullName = Namespace + (Namespace.Length > 0 ? "." : string.Empty) + attrClassNameBase;
872-
gen_info.CurrentType = localFullName;
873872

874873
using (var sw = gen_info.OpenStream (opt.GetFileName (localFullName))) {
875874
sw.WriteLine ("using System;");

tools/generator/Java.Interop.Tools.Generator.ObjectModel/InterfaceGen.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ internal string GetArgsName (Method m)
188188

189189
public override void Generate (CodeGenerationOptions opt, GenerationInfo gen_info)
190190
{
191-
gen_info.CurrentType = FullName;
192-
193191
using (var sw = gen_info.OpenStream (opt.GetFileName (FullName))) {
194192
sw.WriteLine ("using System;");
195193
sw.WriteLine ("using System.Collections.Generic;");

0 commit comments

Comments
 (0)