Skip to content

Commit

Permalink
emit Assembly Attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmisek committed Apr 22, 2024
1 parent db041ee commit 68a7b0a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
25 changes: 13 additions & 12 deletions src/Peachpie.CodeAnalysis/CommandLine/PhpCommandLineParser.cs
Expand Up @@ -11,7 +11,7 @@
using System.Threading.Tasks;
using System.IO;
using Pchp.CodeAnalysis.Utilities;
using Devsense.PHP.Syntax.Ast;
using AST = Devsense.PHP.Syntax.Ast;
using Devsense.PHP.Syntax;

namespace Pchp.CodeAnalysis.CommandLine
Expand Down Expand Up @@ -167,7 +167,7 @@ internal override CommandLineArguments CommonParse(IEnumerable<string> args, str
var additionalFiles = new List<CommandLineSourceFile>();
var embeddedFiles = new List<CommandLineSourceFile>();
var managedResources = new List<ResourceDescription>();
var globalAttributes = new List<AttributeElement>();
var assemblyAttributes = new List<AST.IAttributeElement>();
var defines = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string outputDirectory = baseDirectory;
string subDirectory = null;
Expand Down Expand Up @@ -721,7 +721,7 @@ internal override CommandLineArguments CommonParse(IEnumerable<string> args, str

if (TryParseAttributeSpec(value.Trim(), out var attr))
{
globalAttributes.Add(attr);
assemblyAttributes.Add(attr);
}
else
{
Expand Down Expand Up @@ -864,6 +864,7 @@ internal override CommandLineArguments CommonParse(IEnumerable<string> args, str
Autoload_PSR4 = autoload_psr4,
Autoload_ClassMapFiles = autoload_classmapfiles,
Autoload_Files = autoload_files,
AssemblyAttributes = assemblyAttributes,
};

if (debugPlus)
Expand Down Expand Up @@ -1016,12 +1017,12 @@ private static void ParseDefine(string value, Dictionary<string, string> defines
}
}

private static bool TryParseAttributeSpec(string value, out AttributeElement attr)
private static bool TryParseAttributeSpec(string value, out AST.IAttributeElement attr)
{
attr = null;

ReadOnlySpan<char> fqn;
var signature = new List<ActualParam>();
var signature = new List<AST.ActualParam>();
var span = Devsense.PHP.Text.Span.Invalid;

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

bool ConsumeArg(ref ReadOnlySpan<char> text, out ActualParam p)
bool ConsumeArg(ref ReadOnlySpan<char> text, out AST.ActualParam p)
{
p = default(ActualParam);
p = default(AST.ActualParam);

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

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

p = new ActualParam(
p = new AST.ActualParam(
Devsense.PHP.Text.Span.Invalid,
new StringLiteral(Devsense.PHP.Text.Span.Invalid, str.ToString())
new AST.StringLiteral(Devsense.PHP.Text.Span.Invalid, str.ToString())
);
return true;
}
Expand Down Expand Up @@ -1104,10 +1105,10 @@ bool ConsumeArg(ref ReadOnlySpan<char> text, out ActualParam p)
}

//
attr = new AttributeElement(
attr = new AST.AttributeElement(
span,
new ClassTypeRef(span, QualifiedName.Parse(fqn.Trim().ToString().Replace('.', QualifiedName.Separator), true)),
new CallSignature(signature, span)
new AST.ClassTypeRef(span, QualifiedName.Parse(fqn.Trim().ToString().Replace('.', QualifiedName.Separator), true)),
new AST.CallSignature(signature, span)
);
return true;
}
Expand Down
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using System.Collections.Immutable;
using Roslyn.Utilities;
using AST = Devsense.PHP.Syntax.Ast;

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

/// <summary>
/// Source module attributes.
/// </summary>
public IReadOnlyList<AST.IAttributeElement> AssemblyAttributes { get; internal set; }

///// <summary>
///// Flags applied to the top-level binder created for each syntax tree in the compilation
///// as well as for the binder of global imports.
Expand Down Expand Up @@ -277,6 +283,7 @@ public sealed class PhpCompilationOptions : CompilationOptions, IEquatable<PhpCo
Autoload_ClassMapFiles = other.Autoload_ClassMapFiles;
Autoload_Files = other.Autoload_Files;
Autoload_PSR4 = other.Autoload_PSR4;
AssemblyAttributes = other.AssemblyAttributes;
}

public override string Language => Constants.PhpLanguageName;
Expand Down
22 changes: 13 additions & 9 deletions src/Peachpie.CodeAnalysis/Semantics/SemanticsBinder.cs
Expand Up @@ -301,7 +301,7 @@ protected ImmutableArray<BoundArgument> BindLambdaUseArguments(IList<AST.FormalP
}
}

protected Location GetLocation(AST.ITreeNode expr) => ContainingFile.GetLocation(expr);
protected Location GetLocation(AST.ITreeNode expr) => ContainingFile?.GetLocation(expr);

#endregion

Expand All @@ -320,21 +320,25 @@ public ImmutableArray<AttributeData> BindAttributes(IReadOnlyList<AST.IAttribute
{
foreach (var a in g.Attributes)
{
var attribute = new SourceCustomAttribute(
DeclaringCompilation,
Self,
GetLocation(a),
BoundTypeRefFactory.CreateFromTypeRef(a.ClassRef, this, Self),
BindArguments(a.CallSignature.Parameters));

attrs.Add(attribute);
attrs.Add(BindAttribute(a));
}
}


return attrs.ToImmutableArray();
}

public AttributeData BindAttribute(AST.IAttributeElement a)
{
return new SourceCustomAttribute(
DeclaringCompilation,
Self,
GetLocation(a),
BoundTypeRefFactory.CreateFromTypeRef(a.ClassRef, this, Self),
BindArguments(a.CallSignature.Parameters)
);
}

#endregion

public virtual void WithTryScopes(IEnumerable<TryCatchEdge> tryScopes) { }
Expand Down
12 changes: 12 additions & 0 deletions src/Peachpie.CodeAnalysis/Symbols/Source/SourceModuleSymbol.cs
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using System.Collections.Immutable;
using System.Diagnostics;
using Pchp.CodeAnalysis.Semantics;

namespace Pchp.CodeAnalysis.Symbols
{
Expand Down Expand Up @@ -162,6 +163,17 @@ IEnumerable<AttributeData> CreateAttributesToEmit()
ImmutableArray<KeyValuePair<string, TypedConstant>>.Empty);
}
}

// user assembly attributes
if (DeclaringCompilation.Options.AssemblyAttributes != null)
{
var binder = new SemanticsBinder(DeclaringCompilation, file: null);

foreach (var attr in DeclaringCompilation.Options.AssemblyAttributes)
{
yield return binder.BindAttribute(attr);
}
}

//
yield break;
Expand Down

0 comments on commit 68a7b0a

Please sign in to comment.