pascalabcnet/SyntaxVisitors/VisitorsForLambdas/StandOutExprWithLambdaInForeachSequenceVisitor.cs
Александр Земляк 1f4b44c2c0
Исправление ошибок при перекомпиляции pcu файлов (#3398)
* Move yield string constants to StringConstants class

* Add GeneratedNamesManager class to store all generated names during unit compilation

* Make GeneratedNamesManager class non static

* Add Utils project to installer files

* Add GeneratedNamesManager usage in CapturedVariablesSubstitutionsManager

* Move GeneratedNamesManager initialization in CompilationUnit constructor

* More GeneratedNamesManager usage

* QuestionPointDesugarVisitor fix
2026-03-08 18:30:33 +03:00

58 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System.Collections.Generic;
using PascalABCCompiler.CoreUtils;
using PascalABCCompiler.SyntaxTree;
namespace PascalABCCompiler.SyntaxTreeConverters
{
// Первое предназначение - вынести последовательность из заголовка в foreach до foreach как отдельное присваивание
// Второе предназначение - переименовать все переменные, совпадающие по имени с типом T обобщенного класса, в котором находится метод, содержащий лямбду
public class StandOutExprWithLambdaInForeachSequenceAndNestedLambdasVisitor : BaseChangeVisitor
{
private readonly GeneratedNamesManager generatedNamesManager;
private StandOutExprWithLambdaInForeachSequenceAndNestedLambdasVisitor(GeneratedNamesManager generatedNamesManager)
{
this.generatedNamesManager = generatedNamesManager;
}
public static StandOutExprWithLambdaInForeachSequenceAndNestedLambdasVisitor Create(GeneratedNamesManager generatedNamesManager)
=> new StandOutExprWithLambdaInForeachSequenceAndNestedLambdasVisitor(generatedNamesManager);
//private ident_list ClassTemplateArgsOrNull = null; // если мы - в обобщенном классе, то это - его обобщенные параметры
public ident GenIdentName()
{
return new ident(generatedNamesManager.GenerateName("$GenContFE"));
}
public override void visit(foreach_stmt fe)
{
//if (fe.DescendantNodes().OfType<function_lambda_definition>().Count() > 0) // из-за #1984 убрал вообще условие. Пусть будет всегда
{
var id = GenIdentName();
id.Parent = fe;
var ass = new var_statement(id, fe.in_what, fe.in_what.source_context);
id.source_context = fe.in_what.source_context;
fe.in_what = id;
var l = new List<statement> { ass, fe };
//ReplaceStatement(fe, l);
ReplaceStatementUsingParent(fe, l);
}
base.visit(fe);
}
private int countNestedLambdas = 0;
public override void visit(function_lambda_definition fld)
{
countNestedLambdas += 1;
if (countNestedLambdas>10)
throw new SyntaxVisitors.SyntaxVisitorError("NESTED_LAMBDAS_MAXIMUM_10", fld.source_context);
ProcessNode(fld.proc_body);
countNestedLambdas -= 1;
}
}
}