Skip to content

Commit

Permalink
support for trailing comma in parameters list
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmisek committed Feb 23, 2025
1 parent b100228 commit 7499f84
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/Peachpie.CodeAnalysis/CodeGen/Graph/BoundExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3387,7 +3387,7 @@ internal override TypeSymbol Emit(CodeGenerator cg)
EmitThis(cg); // $this
cg.EmitCallerTypeHandle(); // scope
EmitStaticType(cg); // statictype : PhpTypeInfo
EmitCachedParametersArray(cg, ((LambdaFunctionExpr)PhpSyntax).Signature.FormalParams); // "parameters"
EmitCachedParametersArray(cg, ((LambdaFunctionExpr)PhpSyntax).Signature.FormalParamsFixed()); // "parameters"
EmitUseArray(cg); // "static"

return cg.EmitCall(ILOpCode.Call, cg.CoreMethods.Operators.BuildClosure_Context_IPhpCallable_Object_RuntimeTypeHandle_PhpTypeInfo_PhpArray_PhpArray);
Expand Down Expand Up @@ -3447,7 +3447,7 @@ void EmitUseArray(CodeGenerator cg)
}
}

void EmitNewParametersArray(CodeGenerator cg, FormalParam[] ps)
void EmitNewParametersArray(CodeGenerator cg, ReadOnlySpan<FormalParam> ps)
{
// new PhpArray(<count>){ ... }
cg.Builder.EmitIntConstant(ps.Length);
Expand All @@ -3472,7 +3472,7 @@ void EmitNewParametersArray(CodeGenerator cg, FormalParam[] ps)
/// Caches the array instance into an internal app-static field,
/// so repetitious creations only uses the existing instance.
/// </summary>
TypeSymbol EmitCachedParametersArray(CodeGenerator cg, FormalParam[] ps)
TypeSymbol EmitCachedParametersArray(CodeGenerator cg, ReadOnlySpan<FormalParam> ps)
{
if (ps == null || ps.Length == 0)
{
Expand Down
8 changes: 7 additions & 1 deletion src/Peachpie.CodeAnalysis/Semantics/SemanticsBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ protected BoundLambda BindLambda(AST.LambdaFunctionExpr expr)
captured.Add(v.VarName);
}

foreach (var p in fn.Signature.FormalParams)
foreach (var p in fn.Signature.FormalParamsFixed())
{
captured.Remove(p.Name.Name);
}
Expand Down Expand Up @@ -820,6 +820,12 @@ protected BoundExpression BindFunctionCall(AST.FunctionCall x, BoundExpression b
boundTarget = BindIsMemberOfChain(x.IsMemberOf, BoundAccess.Read/*Object?*/);
}

// callable convert syntax (...)
if (x.CallSignature.IsCallableConvert)
{
throw new NotImplementedException("callable convert");
}

BoundRoutineCall result;

if (x is AST.DirectFcnCall)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public override void GetDiagnostics(DiagnosticBag diagnostic)
}

// __autoload must have exactly one parameter
if (_syntax.Signature.FormalParams.Length != 1)
if (_syntax.Signature.FormalParamsFixed().Length != 1)
{
diagnostic.Add(this, _syntax.Signature.Span.ToTextSpan(), Errors.ErrorCode.ERR_MustTakeArgs, "Function", this.QualifiedName.ToString(), 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ static IReadOnlyCollection<VariableName> EnumerateCapturedVariables(ArrowFunctio
}

// remove vars specified in parameters
foreach (var p in fn.Signature.FormalParams)
foreach (var p in fn.Signature.FormalParamsFixed())
{
capturedvars.Remove(p.Name.Name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public override void GetDiagnostics(DiagnosticBag diagnostic)
}
if (_syntax.ReturnType != null)
{
// {0} cannot declare a return type
diagnostic.Add(this, _syntax.ReturnType.Span.ToTextSpan(), Errors.ErrorCode.ERR_CannotDeclareReturnType, _type.FullName.ToString(name, false));
//// {0} cannot declare a return type
//diagnostic.Add(this, _syntax.ReturnType.Span.ToTextSpan(), Errors.ErrorCode.ERR_CannotDeclareReturnType, _type.FullName.ToString(name, false)); // it seem it can
}
}
else if (name.IsCallName) // __call($name, $args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public virtual void GetDiagnostics(DiagnosticBag diagnostic)
{
// check mandatory behind and optional parameter
bool foundopt = false;
var ps = SyntaxSignature.FormalParams;
var ps = SyntaxSignature.FormalParamsFixed();
for (int i = 0; i < ps.Length; i++)
{
var p = ps[i];
Expand Down Expand Up @@ -225,7 +225,7 @@ public virtual void GetDiagnostics(DiagnosticBag diagnostic)
}
else if (p.TypeHint is PrimitiveTypeRef pt && (
pt.PrimitiveTypeName == PrimitiveTypeRef.PrimitiveType.callable ||
pt.PrimitiveTypeName == PrimitiveTypeRef.PrimitiveType.iterable ||
//pt.PrimitiveTypeName == PrimitiveTypeRef.PrimitiveType.iterable || // it seems it's allowed now
pt.PrimitiveTypeName == PrimitiveTypeRef.PrimitiveType.never))
{
diagnostic.Add(this, p, Errors.ErrorCode.ERR_PropertyTypeNotAllowed, this.ContainingType.PhpName(), p.Name, pt.PrimitiveTypeName.ToString());
Expand Down
15 changes: 15 additions & 0 deletions src/Peachpie.CodeAnalysis/Utilities/AstUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,20 @@ public static string SummaryOrDefault(this IDocBlock doc, string @default = null
? summary
: @default
;

/// <summary>
/// Trims trailing <c>,</c> parameter.
/// </summary>
public static ReadOnlySpan<FormalParam> FormalParamsFixed(this Signature sig)
{
var fp = sig.FormalParams.AsSpan();

if (fp.Length > 0 && fp[fp.Length - 1] == null)
{
fp = fp[..^1];
}

return fp;
}
}
}

0 comments on commit 7499f84

Please sign in to comment.