Skip to content

Commit dbd1144

Browse files
RobertvanderHulstcpyrgas
andauthored
Dev (#1730)
* [Build] Add support for _Parameter2 etc in WriteCodeFragment to support Attributes from Verify * [CodeDomProvider] Add support for generating AssemblyCustomAttributes stored in the CodeCompileUnit object * [CodeDomProvider] Added some END ... lines * [Docs] Fixed typos in DBServer Filter and Deleted topics * [Docs] Updated comment in Chinese help --------- Co-authored-by: cpyrgas <[email protected]>
1 parent 1bc1b5b commit dbd1144

File tree

4 files changed

+120
-26
lines changed

4 files changed

+120
-26
lines changed

src/Compiler/src/Compiler/XSharpBuildTask/WriteCodeFragment.cs

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
//
2-
using Microsoft.Build.Framework;
1+
using Microsoft.Build.Framework;
32
using Microsoft.Build.Utilities;
43
using System;
54
using System.Collections;
65
using System.Collections.Generic;
6+
using System.Collections.Specialized;
7+
using System.Globalization;
78
using System.IO;
8-
using System.Runtime.CompilerServices;
9-
using System.Runtime.InteropServices;
109
using System.Text;
1110
// Disable errors about public methods and properties that are not part of a declared API
1211
#pragma warning disable RS0016
@@ -58,17 +57,103 @@ private string GenerateAttributes()
5857
var sb = new StringBuilder();
5958
foreach (var att in AssemblyAttributes)
6059
{
61-
var name = att.ItemSpec;
62-
if (name.EndsWith("Attribute"))
60+
var attName = att.ItemSpec;
61+
62+
var customMetadata = att.CloneCustomMetadata();
63+
List<object> orderedParameters = new List<object>(new object[customMetadata.Count + 1] /* max possible slots needed */);
64+
var namedParameters = new NameValueCollection();
65+
66+
foreach (DictionaryEntry entry in customMetadata)
6367
{
64-
name = name.Substring(0, name.Length - "Attribute".Length);
68+
string name = (string)entry.Key;
69+
string value = (string)entry.Value;
70+
if (name.StartsWith("_Parameter", StringComparison.OrdinalIgnoreCase))
71+
{
72+
73+
if (!Int32.TryParse(name.Substring("_Parameter".Length), out var index))
74+
{
75+
base.Log.LogError("Invalid parameter name: " + name);
76+
return null;
77+
}
78+
79+
if (index > orderedParameters.Count || index < 1)
80+
{
81+
base.Log.LogError("Invalid parameter number: " + name);
82+
return null;
83+
}
84+
85+
// "_Parameter01" and "_Parameter1" would overwrite each other
86+
87+
orderedParameters[index - 1] = value;
88+
}
89+
else
90+
{
91+
namedParameters.Add(name, value);
92+
93+
}
6594
}
66-
var value = att.GetMetadata("_Parameter1");
67-
sb.Append("[Assembly: " + name + "(\"");
68-
sb.Append(value);
69-
sb.AppendLine("\")]");
95+
sb.Append("[Assembly: ");
96+
sb.Append(attName + "(");
97+
bool encounteredNull = false;
98+
int iPars = 0;
99+
for (int i = 0; i < orderedParameters.Count; i++)
100+
{
101+
if (orderedParameters[i] == null)
102+
{
103+
// All subsequent args should be null, else a slot was missed
104+
encounteredNull = true;
105+
continue;
106+
}
107+
if (encounteredNull)
108+
{
109+
base.Log.LogError("Parameter {0} follows null parameter", i + 1);
110+
return null;
111+
}
112+
if (iPars > 0)
113+
{
114+
sb.Append(", ");
115+
}
116+
var value = orderedParameters[i];
117+
sb.Append(Obj2String(value));
118+
iPars++;
119+
}
120+
foreach (var name in namedParameters.AllKeys)
121+
{
122+
if (iPars > 0)
123+
{
124+
sb.Append(", ");
125+
}
126+
sb.Append(name + " := ");
127+
sb.Append(Obj2String(namedParameters[name]));
128+
iPars++;
129+
}
130+
sb.Append(")]");
131+
sb.AppendLine();
70132
}
71133
return sb.ToString();
72134
}
135+
string Obj2String(object o)
136+
{
137+
switch (o)
138+
{
139+
case short si:
140+
return si.ToString(CultureInfo.InvariantCulture);
141+
case ushort usi:
142+
return usi.ToString(CultureInfo.InvariantCulture);
143+
case int i:
144+
return i.ToString(CultureInfo.InvariantCulture);
145+
case uint ui:
146+
return ui.ToString(CultureInfo.InvariantCulture);
147+
case long i64:
148+
return i64.ToString(CultureInfo.InvariantCulture);
149+
case ulong ui64:
150+
return ui64.ToString(CultureInfo.InvariantCulture);
151+
case string s:
152+
return '"' + s + '"';
153+
case null:
154+
return "null";
155+
}
156+
return o.ToString();
157+
}
73158
}
74159
}

src/DocChinese/Rdd.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,7 +2020,7 @@
20202020
FUNCTION IgnoreDeleted()
20212021
LOCAL oDBSales AS Sales
20222022
oDBSales := Sales{}
2023-
oDBSales:SetFilter(!oDBSales:Deleted)
2023+
oDBSales:SetFilter("!Deleted()")
20242024
oDBSales:GoTop()
20252025
.
20262026
. &lt;处理记录&gt;
@@ -2484,9 +2484,9 @@
24842484
FUNCTION IgnoreDeleted()
24852485
LOCAL oDBSales AS Sales
24862486
oDBSales := Sales{}
2487-
oDBSales:SetFilter(!oDBSales:Deleted)
2487+
oDBSales:SetFilter("!Deleted()")
24882488
oDBSales:GoTop() // 移动指针以调用过滤器
2489-
? oDBSales:Filter // !oDBSales:Deleted
2489+
? oDBSales:Filter // !Deleted()
24902490
.
24912491
. &lt;处理记录&gt;
24922492
.

src/Docs/Rdd.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,7 @@
17771777
FUNCTION IgnoreDeleted()
17781778
LOCAL oDBSales AS Sales
17791779
oDBSales := Sales{}
1780-
oDBSales:SetFilter(!oDBSales:Deleted)
1780+
oDBSales:SetFilter("!Deleted()")
17811781
oDBSales:GoTop()
17821782
.
17831783
. &lt;Process records&gt;
@@ -2191,9 +2191,9 @@
21912191
FUNCTION IgnoreDeleted()
21922192
LOCAL oDBSales AS Sales
21932193
oDBSales := Sales{}
2194-
oDBSales:SetFilter(!oDBSales:Deleted)
2194+
oDBSales:SetFilter("!Deleted()")
21952195
oDBSales:GoTop() // move pointer to invoke filter
2196-
? oDBSales:Filter // !oDBSales:Deleted
2196+
? oDBSales:Filter // !Deleted()
21972197
.
21982198
. &lt;Process records&gt;
21992199
.

src/VisualStudio/CodeDomProvider/XSharpCodeGenerator.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private void ReadSettings()
216216
ws = useTabs ? "\t" : " ";
217217
keywordBEGIN = FormatKeyword("BEGIN ");
218218
keywordEND = FormatKeyword("END ");
219-
keywordDELEGATE = FormatKeyword("DELEGATE ");
219+
keywordDELEGATE = FormatKeyword("DELEGATE ");
220220
keywordCLASS = FormatKeyword("CLASS ");
221221
keywordINHERIT = FormatKeyword("INHERIT ");
222222
keywordIMPLEMENTS = FormatKeyword("IMPLEMENTS ");
@@ -302,7 +302,7 @@ protected override string CreateEscapedIdentifier(string value)
302302
#endif
303303
return value;
304304
}
305-
305+
306306
protected override string CreateValidIdentifier(string value)
307307
{
308308
// Is is a reserved Keyword ?
@@ -426,7 +426,7 @@ protected override void GenerateAttachEventStatement(CodeAttachEventStatement e)
426426

427427
protected override void GenerateAttributeDeclarationsEnd(CodeAttributeDeclarationCollection attributes)
428428
{
429-
this.Output.Write("] ;");
429+
this.Output.Write("] ");
430430
}
431431

432432
protected override void GenerateAttributeDeclarationsStart(CodeAttributeDeclarationCollection attributes)
@@ -569,6 +569,7 @@ protected override void GenerateConstructor(CodeConstructor e, CodeTypeDeclarati
569569
}
570570
this.Indent++;
571571
this.GenerateStatements(e.Statements);
572+
base.Output.Write(keywordEND + keywordCONSTRUCTOR);
572573
}
573574
this.Indent--;
574575
if (e.HasEndingTrivia())
@@ -601,14 +602,14 @@ protected override void GenerateDelegateInvokeExpression(CodeDelegateInvokeExpre
601602

602603
protected override void GenerateEntryPointMethod(CodeEntryPointMethod e, CodeTypeDeclaration c)
603604
{
604-
// save the parameters and delay the code generation until the end of the
605+
// save the parameters and delay the code generation until the end of the
605606
// compile unit, where ImplementEntryPointMethod() is called
606607
entryPoint = e;
607608
entryPointType = c;
608609
return;
609610
}
610611
private void ImplementEntryPointMethod(CodeEntryPointMethod e, CodeTypeDeclaration c)
611-
{
612+
{
612613
if (e == null || c == null)
613614
return;
614615
this.GenerateCommentStatements(e.Comments);
@@ -622,6 +623,7 @@ private void ImplementEntryPointMethod(CodeEntryPointMethod e, CodeTypeDeclarati
622623
base.Output.WriteLine();
623624
this.Indent++;
624625
this.GenerateStatements(e.Statements);
626+
base.Output.Write(keywordEND + keywordFUNCTION);
625627
this.Indent--;
626628
base.Output.WriteLine();
627629
if (e.HasEndingTrivia())
@@ -972,7 +974,7 @@ protected override void GenerateMethod(CodeMemberMethod e, CodeTypeDeclaration c
972974
}
973975
this.Indent--;
974976
// close the method.
975-
this.Output.Write("END METHOD");
977+
this.Output.Write(keywordEND+keywordMETHOD);
976978
base.Output.WriteLine();
977979
if (e.HasEndingTrivia())
978980
{
@@ -1083,6 +1085,13 @@ protected override void GenerateCompileUnitStart(CodeCompileUnit e)
10831085
this.Output.WriteLine("// </auto-generated>");
10841086
this.Output.WriteLine("//------------------------------------------------------------------------------");
10851087
}
1088+
1089+
if (e.AssemblyCustomAttributes.Count > 0)
1090+
{
1091+
GenerateAttributes(e.AssemblyCustomAttributes, "assembly");
1092+
Output.WriteLine();
1093+
}
1094+
10861095
}
10871096
protected override void GenerateCompileUnitEnd(CodeCompileUnit e)
10881097
{
@@ -1164,7 +1173,7 @@ private bool WriteTrivia(CodeObject o, bool ending = false)
11641173

11651174
}
11661175
if (! string.IsNullOrEmpty(trivia))
1167-
{
1176+
{
11681177
WriteTriviaWorker(trivia);
11691178
return true;
11701179
}
@@ -1678,7 +1687,7 @@ private void WriteEscapedChar(char value)
16781687
base.Output.Write("\\u");
16791688
int num = value;
16801689
base.Output.Write(num.ToString("X4", CultureInfo.InvariantCulture));
1681-
1690+
16821691
}
16831692

16841693

@@ -2326,7 +2335,7 @@ protected override void OutputMemberAccessModifier(MemberAttributes attributes)
23262335
base.Output.Write(keywordPUBLIC);
23272336
else if (publicKeyword == 1)
23282337
base.Output.Write(keywordEXPORT);
2329-
else
2338+
else
23302339
base.Output.Write("");
23312340
break;
23322341
case MemberAttributes.Family: // A member that is accessible within the family of its class and derived classes.

0 commit comments

Comments
 (0)