Skip to content

Commit c6a4cb4

Browse files
authored
Remove IronPython.Compiler.Ast.Arg (#1175)
* Remove IronPython.Compiler.Ast.Arg * Get rid of IList overloads * Fix starred argument * Fix test_ast
1 parent c3f0499 commit c6a4cb4

17 files changed

+271
-275
lines changed

Src/IronPython/Compiler/Ast/CallExpression.cs

Lines changed: 96 additions & 92 deletions
Large diffs are not rendered by default.

Src/IronPython/Compiler/Ast/ClassDefinition.cs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ namespace IronPython.Compiler.Ast {
2727

2828
public class ClassDefinition : ScopeStatement {
2929
private readonly string _name;
30-
private readonly Arg[] _bases;
31-
private readonly Arg[] _keywords;
30+
private readonly Expression[] _bases;
31+
private readonly Keyword[] _keywords;
3232

3333
private LightLambdaExpression? _dlrBody; // the transformed body including all of our initialization, etc...
3434

@@ -37,10 +37,10 @@ public class ClassDefinition : ScopeStatement {
3737
private static readonly MSAst.ParameterExpression _parentContextParam = Ast.Parameter(typeof(CodeContext), "$parentContext");
3838
private static readonly MSAst.Expression _tupleExpression = MSAst.Expression.Call(AstMethods.GetClosureTupleFromContext, _parentContextParam);
3939

40-
public ClassDefinition(string name, IReadOnlyList<Arg>? bases, IReadOnlyList<Arg>? keywords, Statement? body = null) {
40+
public ClassDefinition(string name, IReadOnlyList<Expression>? bases, IReadOnlyList<Keyword>? keywords, Statement? body = null) {
4141
_name = name;
42-
_bases = bases?.ToArray() ?? Array.Empty<Arg>();
43-
_keywords = keywords?.ToArray() ?? Array.Empty<Arg>();
42+
_bases = bases?.ToArray() ?? Array.Empty<Expression>();
43+
_keywords = keywords?.ToArray() ?? Array.Empty<Keyword>();
4444
Body = body ?? EmptyStatement.PreCompiledInstance;
4545
}
4646

@@ -50,9 +50,9 @@ public ClassDefinition(string name, IReadOnlyList<Arg>? bases, IReadOnlyList<Arg
5050

5151
public override string Name => _name;
5252

53-
public IReadOnlyList<Arg> Bases => _bases;
53+
public IReadOnlyList<Expression> Bases => _bases;
5454

55-
public IReadOnlyList<Arg> Keywords => _keywords;
55+
public IReadOnlyList<Keyword> Keywords => _keywords;
5656

5757
public Statement Body { get; set; }
5858

@@ -194,42 +194,35 @@ public override MSAst.Expression Reduce() {
194194
classDef = AddDecorators(classDef, Decorators);
195195

196196
return GlobalParent.AddDebugInfoAndVoid(
197-
AssignValue(Parent.GetVariableExpression(PythonVariable!), classDef),
197+
AssignValue(Parent.GetVariableExpression(PythonVariable!), classDef),
198198
new SourceSpan(
199199
GlobalParent.IndexToLocation(StartIndex),
200200
GlobalParent.IndexToLocation(HeaderIndex)
201201
)
202202
);
203203

204204
// Compare to: CallExpression.Reduce.__UnpackListHelper
205-
static MSAst.Expression UnpackBasesHelper(IReadOnlyList<Arg> bases) {
206-
if (bases.Count == 0) {
205+
static MSAst.Expression UnpackBasesHelper(ReadOnlySpan<Expression> bases) {
206+
if (bases.Length == 0) {
207207
return Expression.Call(AstMethods.MakeEmptyTuple);
208-
} else if (bases.All(arg => arg.ArgumentInfo.Kind is ArgumentType.Simple)) {
209-
return Expression.Call(AstMethods.MakeTuple,
210-
Expression.NewArrayInit(
211-
typeof(object),
212-
ToObjectArray(bases.Select(arg => arg.Expression).ToList())
213-
)
214-
);
215-
} else {
216-
var expressions = new ReadOnlyCollectionBuilder<MSAst.Expression>(bases.Count + 2);
217-
var varExpr = Expression.Variable(typeof(PythonList), "$coll");
218-
expressions.Add(Expression.Assign(varExpr, Expression.Call(AstMethods.MakeEmptyList)));
219-
foreach (var arg in bases) {
220-
if (arg.ArgumentInfo.Kind == ArgumentType.List) {
221-
expressions.Add(Expression.Call(AstMethods.ListExtend, varExpr, AstUtils.Convert(arg.Expression, typeof(object))));
222-
} else {
223-
expressions.Add(Expression.Call(AstMethods.ListAppend, varExpr, AstUtils.Convert(arg.Expression, typeof(object))));
224-
}
208+
}
209+
foreach (var arg in bases) {
210+
if (arg is StarredExpression) {
211+
return Expression.Call(AstMethods.ListToTuple,
212+
Expression.UnpackSequenceHelper<PythonList>(bases, AstMethods.MakeEmptyList, AstMethods.ListAppend, AstMethods.ListExtend)
213+
);
225214
}
226-
expressions.Add(Expression.Call(AstMethods.ListToTuple, varExpr));
227-
return Expression.Block(typeof(PythonTuple), new MSAst.ParameterExpression[] { varExpr }, expressions);
228215
}
216+
return Expression.Call(AstMethods.MakeTuple,
217+
Expression.NewArrayInit(
218+
typeof(object),
219+
ToObjectArray(bases)
220+
)
221+
);
229222
}
230223

231224
// Compare to: CallExpression.Reduce.__UnpackDictHelper
232-
static MSAst.Expression UnpackKeywordsHelper(MSAst.Expression context, IReadOnlyList<Arg> kwargs) {
225+
static MSAst.Expression UnpackKeywordsHelper(MSAst.Expression context, IReadOnlyList<Keyword> kwargs) {
233226
if (kwargs.Count == 0) {
234227
return AstUtils.Constant(null, typeof(PythonDictionary));
235228
}
@@ -238,7 +231,7 @@ static MSAst.Expression UnpackKeywordsHelper(MSAst.Expression context, IReadOnly
238231
var varExpr = Expression.Variable(typeof(PythonDictionary), "$dict");
239232
expressions.Add(Expression.Assign(varExpr, Expression.Call(AstMethods.MakeEmptyDict)));
240233
foreach (var arg in kwargs) {
241-
if (arg.ArgumentInfo.Kind == ArgumentType.Dictionary) {
234+
if (arg.Name is null) {
242235
expressions.Add(Expression.Call(AstMethods.DictMerge, context, varExpr, AstUtils.Convert(arg.Expression, typeof(object))));
243236
} else {
244237
expressions.Add(Expression.Call(AstMethods.DictMergeOne, context, varExpr, AstUtils.Constant(arg.Name, typeof(object)), AstUtils.Convert(arg.Expression, typeof(object))));
@@ -346,10 +339,10 @@ public override void Walk(PythonWalker walker) {
346339
decorator.Walk(walker);
347340
}
348341
}
349-
foreach (Arg b in _bases) {
342+
foreach (var b in _bases) {
350343
b.Walk(walker);
351344
}
352-
foreach (Arg b in _keywords) {
345+
foreach (var b in _keywords) {
353346
b.Walk(walker);
354347
}
355348
Body.Walk(walker);

Src/IronPython/Compiler/Ast/Expression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace IronPython.Compiler.Ast {
2121
public abstract class Expression : Node {
2222
internal static readonly Expression[] EmptyArray = Array.Empty<Expression>();
2323

24-
protected internal static MSAst.BlockExpression UnpackSequenceHelper<T>(IList<Expression> items, MethodInfo makeEmpty, MethodInfo append, MethodInfo extend) {
25-
var expressions = new ReadOnlyCollectionBuilder<MSAst.Expression>(items.Count + 2);
24+
protected internal static MSAst.BlockExpression UnpackSequenceHelper<T>(ReadOnlySpan<Expression> items, MethodInfo makeEmpty, MethodInfo append, MethodInfo extend) {
25+
var expressions = new ReadOnlyCollectionBuilder<MSAst.Expression>(items.Length + 2);
2626
var varExpr = Expression.Variable(typeof(T), "$coll");
2727
expressions.Add(Expression.Assign(varExpr, Expression.Call(makeEmpty)));
2828
foreach (var item in items) {

Src/IronPython/Compiler/Ast/FlowChecker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ public override bool Walk(ClassDefinition node) {
328328
} else {
329329
// analyze the class definition itself (it is visited while analyzing parent scope):
330330
Define(node.Name);
331-
foreach (Arg e in node.Bases) {
331+
foreach (var e in node.Bases) {
332332
e.Walk(this);
333333
}
334-
foreach (Arg e in node.Keywords) {
334+
foreach (var e in node.Keywords) {
335335
e.Walk(this);
336336
}
337337
return false;

Src/IronPython/Compiler/Ast/IndexExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override MSAst.Expression Reduce() {
3232

3333
private MSAst.Expression[] GetActionArgumentsForGetOrDelete() {
3434
if (Index is TupleExpression te && te.IsExpandable) {
35-
return ArrayUtils.Insert(Target, te.Items);
35+
return ArrayUtils.Insert(Target, te.UnsafeItems);
3636
}
3737

3838
if (Index is SliceExpression se) {
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

@@ -7,30 +7,16 @@
77
using Microsoft.Scripting.Actions;
88

99
namespace IronPython.Compiler.Ast {
10-
public class Arg : Node {
11-
private static readonly Argument _listTypeArgument = new(ArgumentType.List);
12-
private static readonly Argument _dictTypeArgument = new(ArgumentType.Dictionary);
13-
14-
public Arg(Expression expression) : this(null, expression) { }
15-
16-
public Arg(string? name, Expression expression) {
10+
public class Keyword : Node {
11+
public Keyword(string? name, Expression expression) {
1712
Name = name;
1813
Expression = expression;
19-
20-
ArgumentInfo = name switch {
21-
null => Argument.Simple,
22-
"*" => _listTypeArgument,
23-
"**" => _dictTypeArgument,
24-
_ => new Argument(name)
25-
};
2614
}
2715

2816
public string? Name { get; }
2917

3018
public Expression Expression { get; }
3119

32-
internal Argument ArgumentInfo { get; }
33-
3420
public override string ToString() {
3521
return base.ToString() + ":" + Name;
3622
}

Src/IronPython/Compiler/Ast/ListExpression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public override MSAst.Expression Reduce() {
2020
}
2121

2222
if (HasStarredExpression) {
23-
return UnpackSequenceHelper<PythonList>(Items, AstMethods.MakeEmptyList, AstMethods.ListAppend, AstMethods.ListExtend);
23+
return UnpackSequenceHelper<PythonList>(_items, AstMethods.MakeEmptyList, AstMethods.ListAppend, AstMethods.ListExtend);
2424
}
2525

2626
return Call(
2727
AstMethods.MakeListNoCopy, // method
2828
NewArrayInit( // parameters
2929
typeof(object),
30-
ToObjectArray(Items)
30+
ToObjectArray(_items)
3131
)
3232
);
3333
}

Src/IronPython/Compiler/Ast/Node.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ internal virtual string GetDocumentation(Statement/*!*/ stmt) {
164164

165165
#region Transformation Helpers
166166

167-
internal static MSAst.Expression[] ToObjectArray(IList<Expression> expressions) {
168-
MSAst.Expression[] to = new MSAst.Expression[expressions.Count];
169-
for (int i = 0; i < expressions.Count; i++) {
167+
internal static MSAst.Expression[] ToObjectArray(ReadOnlySpan<Expression> expressions) {
168+
MSAst.Expression[] to = new MSAst.Expression[expressions.Length];
169+
for (int i = 0; i < expressions.Length; i++) {
170170
to[i] = AstUtils.Convert(expressions[i], typeof(object));
171171
}
172172
return to;

Src/IronPython/Compiler/Ast/PythonNameBinder.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ public override bool Walk(ClassDefinition node) {
206206
node.PythonVariable = DefineName(node.Name);
207207

208208
// Base references are in the outer context
209-
foreach (Arg b in node.Bases) b.Walk(this);
209+
foreach (var b in node.Bases) b.Walk(this);
210210

211-
foreach (Arg a in node.Keywords) a.Walk(this);
211+
foreach (var a in node.Keywords) a.Walk(this);
212212

213213
// process the decorators in the outer context
214214
if (node.Decorators != null) {
@@ -333,11 +333,6 @@ public override bool Walk(AndExpression node) {
333333
node.Parent = _currentScope;
334334
return base.Walk(node);
335335
}
336-
// Arg
337-
public override bool Walk(Arg node) {
338-
node.Parent = _currentScope;
339-
return base.Walk(node);
340-
}
341336
// AssertStatement
342337
public override bool Walk(AssertStatement node) {
343338
node.Parent = _currentScope;
@@ -413,6 +408,11 @@ public override bool Walk(IndexExpression node) {
413408
node.Parent = _currentScope;
414409
return base.Walk(node);
415410
}
411+
// Keyword
412+
public override bool Walk(Keyword node) {
413+
node.Parent = _currentScope;
414+
return base.Walk(node);
415+
}
416416
// LambdaExpression
417417
public override bool Walk(LambdaExpression node) {
418418
node.Parent = _currentScope;
@@ -836,8 +836,7 @@ public override bool Walk(CallExpression node) {
836836
if (node.Target is NameExpression nameExpr && nameExpr.Name == "super" && _currentScope is FunctionDefinition func) {
837837
_currentScope.Reference("__class__");
838838
if (node.Args.Count == 0 && node.Kwargs.Count == 0 && func.ParameterNames.Length > 0) {
839-
node.ImplicitArgs.Add(new Arg(new NameExpression("__class__")));
840-
node.ImplicitArgs.Add(new Arg(new NameExpression(func.ParameterNames[0])));
839+
node.SetImplicitArgs(new NameExpression("__class__"), new NameExpression(func.ParameterNames[0]));
841840
}
842841
}
843842
return base.Walk(node);

Src/IronPython/Compiler/Ast/PythonWalker.Generated.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public virtual void PostWalk(ListExpression node) { }
7272
public virtual bool Walk(MemberExpression node) { return true; }
7373
public virtual void PostWalk(MemberExpression node) { }
7474

75-
7675
// NameExpression
7776
public virtual bool Walk(NameExpression node) { return true; }
7877
public virtual void PostWalk(NameExpression node) { }
@@ -213,10 +212,6 @@ public virtual void PostWalk(WhileStatement node) { }
213212
public virtual bool Walk(WithStatement node) { return true; }
214213
public virtual void PostWalk(WithStatement node) { }
215214

216-
// Arg
217-
public virtual bool Walk(Arg node) { return true; }
218-
public virtual void PostWalk(Arg node) { }
219-
220215
// ComprehensionFor
221216
public virtual bool Walk(ComprehensionFor node) { return true; }
222217
public virtual void PostWalk(ComprehensionFor node) { }
@@ -233,6 +228,10 @@ public virtual void PostWalk(DottedName node) { }
233228
public virtual bool Walk(IfStatementTest node) { return true; }
234229
public virtual void PostWalk(IfStatementTest node) { }
235230

231+
// Keyword
232+
public virtual bool Walk(Keyword node) { return true; }
233+
public virtual void PostWalk(Keyword node) { }
234+
236235
// ModuleName
237236
public virtual bool Walk(ModuleName node) { return true; }
238237
public virtual void PostWalk(ModuleName node) { }
@@ -460,10 +459,6 @@ public override void PostWalk(WhileStatement node) { }
460459
public override bool Walk(WithStatement node) { return false; }
461460
public override void PostWalk(WithStatement node) { }
462461

463-
// Arg
464-
public override bool Walk(Arg node) { return false; }
465-
public override void PostWalk(Arg node) { }
466-
467462
// ComprehensionFor
468463
public override bool Walk(ComprehensionFor node) { return false; }
469464
public override void PostWalk(ComprehensionFor node) { }
@@ -480,6 +475,10 @@ public override void PostWalk(DottedName node) { }
480475
public override bool Walk(IfStatementTest node) { return false; }
481476
public override void PostWalk(IfStatementTest node) { }
482477

478+
// Keyword
479+
public override bool Walk(Keyword node) { return false; }
480+
public override void PostWalk(Keyword node) { }
481+
483482
// ModuleName
484483
public override bool Walk(ModuleName node) { return false; }
485484
public override void PostWalk(ModuleName node) { }

0 commit comments

Comments
 (0)