Skip to content

Commit 68a7b0a

Browse files
committed
emit Assembly Attributes
1 parent db041ee commit 68a7b0a

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

src/Peachpie.CodeAnalysis/CommandLine/PhpCommandLineParser.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using System.Threading.Tasks;
1212
using System.IO;
1313
using Pchp.CodeAnalysis.Utilities;
14-
using Devsense.PHP.Syntax.Ast;
14+
using AST = Devsense.PHP.Syntax.Ast;
1515
using Devsense.PHP.Syntax;
1616

1717
namespace Pchp.CodeAnalysis.CommandLine
@@ -167,7 +167,7 @@ internal override CommandLineArguments CommonParse(IEnumerable<string> args, str
167167
var additionalFiles = new List<CommandLineSourceFile>();
168168
var embeddedFiles = new List<CommandLineSourceFile>();
169169
var managedResources = new List<ResourceDescription>();
170-
var globalAttributes = new List<AttributeElement>();
170+
var assemblyAttributes = new List<AST.IAttributeElement>();
171171
var defines = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
172172
string outputDirectory = baseDirectory;
173173
string subDirectory = null;
@@ -721,7 +721,7 @@ internal override CommandLineArguments CommonParse(IEnumerable<string> args, str
721721

722722
if (TryParseAttributeSpec(value.Trim(), out var attr))
723723
{
724-
globalAttributes.Add(attr);
724+
assemblyAttributes.Add(attr);
725725
}
726726
else
727727
{
@@ -864,6 +864,7 @@ internal override CommandLineArguments CommonParse(IEnumerable<string> args, str
864864
Autoload_PSR4 = autoload_psr4,
865865
Autoload_ClassMapFiles = autoload_classmapfiles,
866866
Autoload_Files = autoload_files,
867+
AssemblyAttributes = assemblyAttributes,
867868
};
868869

869870
if (debugPlus)
@@ -1016,12 +1017,12 @@ private static void ParseDefine(string value, Dictionary<string, string> defines
10161017
}
10171018
}
10181019

1019-
private static bool TryParseAttributeSpec(string value, out AttributeElement attr)
1020+
private static bool TryParseAttributeSpec(string value, out AST.IAttributeElement attr)
10201021
{
10211022
attr = null;
10221023

10231024
ReadOnlySpan<char> fqn;
1024-
var signature = new List<ActualParam>();
1025+
var signature = new List<AST.ActualParam>();
10251026
var span = Devsense.PHP.Text.Span.Invalid;
10261027

10271028
// FQN("value1","value2")
@@ -1042,9 +1043,9 @@ bool ConsumeChar(ref ReadOnlySpan<char> text, char ch)
10421043
return false;
10431044
}
10441045

1045-
bool ConsumeArg(ref ReadOnlySpan<char> text, out ActualParam p)
1046+
bool ConsumeArg(ref ReadOnlySpan<char> text, out AST.ActualParam p)
10461047
{
1047-
p = default(ActualParam);
1048+
p = default(AST.ActualParam);
10481049

10491050
if (!ConsumeChar(ref text, '"')) return false;
10501051

@@ -1068,9 +1069,9 @@ bool ConsumeArg(ref ReadOnlySpan<char> text, out ActualParam p)
10681069
return false;
10691070
}
10701071

1071-
p = new ActualParam(
1072+
p = new AST.ActualParam(
10721073
Devsense.PHP.Text.Span.Invalid,
1073-
new StringLiteral(Devsense.PHP.Text.Span.Invalid, str.ToString())
1074+
new AST.StringLiteral(Devsense.PHP.Text.Span.Invalid, str.ToString())
10741075
);
10751076
return true;
10761077
}
@@ -1104,10 +1105,10 @@ bool ConsumeArg(ref ReadOnlySpan<char> text, out ActualParam p)
11041105
}
11051106

11061107
//
1107-
attr = new AttributeElement(
1108+
attr = new AST.AttributeElement(
11081109
span,
1109-
new ClassTypeRef(span, QualifiedName.Parse(fqn.Trim().ToString().Replace('.', QualifiedName.Separator), true)),
1110-
new CallSignature(signature, span)
1110+
new AST.ClassTypeRef(span, QualifiedName.Parse(fqn.Trim().ToString().Replace('.', QualifiedName.Separator), true)),
1111+
new AST.CallSignature(signature, span)
11111112
);
11121113
return true;
11131114
}

src/Peachpie.CodeAnalysis/Compilation/PhpCompilationOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using System.Collections.Immutable;
99
using Roslyn.Utilities;
10+
using AST = Devsense.PHP.Syntax.Ast;
1011

1112
namespace Pchp.CodeAnalysis
1213
{
@@ -106,6 +107,11 @@ public sealed class PhpCompilationOptions : CompilationOptions, IEquatable<PhpCo
106107
/// </summary>
107108
public override NullableContextOptions NullableContextOptions { get; protected set; }
108109

110+
/// <summary>
111+
/// Source module attributes.
112+
/// </summary>
113+
public IReadOnlyList<AST.IAttributeElement> AssemblyAttributes { get; internal set; }
114+
109115
///// <summary>
110116
///// Flags applied to the top-level binder created for each syntax tree in the compilation
111117
///// as well as for the binder of global imports.
@@ -277,6 +283,7 @@ private PhpCompilationOptions(PhpCompilationOptions other) : this(
277283
Autoload_ClassMapFiles = other.Autoload_ClassMapFiles;
278284
Autoload_Files = other.Autoload_Files;
279285
Autoload_PSR4 = other.Autoload_PSR4;
286+
AssemblyAttributes = other.AssemblyAttributes;
280287
}
281288

282289
public override string Language => Constants.PhpLanguageName;

src/Peachpie.CodeAnalysis/Semantics/SemanticsBinder.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ protected ImmutableArray<BoundArgument> BindLambdaUseArguments(IList<AST.FormalP
301301
}
302302
}
303303

304-
protected Location GetLocation(AST.ITreeNode expr) => ContainingFile.GetLocation(expr);
304+
protected Location GetLocation(AST.ITreeNode expr) => ContainingFile?.GetLocation(expr);
305305

306306
#endregion
307307

@@ -320,21 +320,25 @@ public ImmutableArray<AttributeData> BindAttributes(IReadOnlyList<AST.IAttribute
320320
{
321321
foreach (var a in g.Attributes)
322322
{
323-
var attribute = new SourceCustomAttribute(
324-
DeclaringCompilation,
325-
Self,
326-
GetLocation(a),
327-
BoundTypeRefFactory.CreateFromTypeRef(a.ClassRef, this, Self),
328-
BindArguments(a.CallSignature.Parameters));
329-
330-
attrs.Add(attribute);
323+
attrs.Add(BindAttribute(a));
331324
}
332325
}
333326

334327

335328
return attrs.ToImmutableArray();
336329
}
337330

331+
public AttributeData BindAttribute(AST.IAttributeElement a)
332+
{
333+
return new SourceCustomAttribute(
334+
DeclaringCompilation,
335+
Self,
336+
GetLocation(a),
337+
BoundTypeRefFactory.CreateFromTypeRef(a.ClassRef, this, Self),
338+
BindArguments(a.CallSignature.Parameters)
339+
);
340+
}
341+
338342
#endregion
339343

340344
public virtual void WithTryScopes(IEnumerable<TryCatchEdge> tryScopes) { }

src/Peachpie.CodeAnalysis/Symbols/Source/SourceModuleSymbol.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using System.Collections.Immutable;
88
using System.Diagnostics;
9+
using Pchp.CodeAnalysis.Semantics;
910

1011
namespace Pchp.CodeAnalysis.Symbols
1112
{
@@ -162,6 +163,17 @@ IEnumerable<AttributeData> CreateAttributesToEmit()
162163
ImmutableArray<KeyValuePair<string, TypedConstant>>.Empty);
163164
}
164165
}
166+
167+
// user assembly attributes
168+
if (DeclaringCompilation.Options.AssemblyAttributes != null)
169+
{
170+
var binder = new SemanticsBinder(DeclaringCompilation, file: null);
171+
172+
foreach (var attr in DeclaringCompilation.Options.AssemblyAttributes)
173+
{
174+
yield return binder.BindAttribute(attr);
175+
}
176+
}
165177

166178
//
167179
yield break;

0 commit comments

Comments
 (0)