Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghuan committed Feb 25, 2021
1 parent eec80c5 commit 94210ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CSharp.lua/LuaSyntaxNodeTransform.Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ public override LuaSyntaxNode VisitConditionalAccessExpression(ConditionalAccess

if (isRoot) {
conditionalTemps_.Pop();
ReleaseTempIdentifier(temp);
AddReleaseTempIdentifier(temp);
}

if (IsReturnVoidConditionalAccessExpression(node)) {
Expand Down
32 changes: 21 additions & 11 deletions CSharp.lua/LuaSyntaxNodeTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public bool CheckTypeName(INamedTypeSymbol getNameTypeSymbol, out LuaIdentifierN
private readonly Stack<LuaBlockSyntax> blocks_ = new Stack<LuaBlockSyntax>();
private readonly Stack<LuaIfStatementSyntax> ifStatements_ = new Stack<LuaIfStatementSyntax>();
private readonly Stack<LuaSwitchAdapterStatementSyntax> switchs_ = new Stack<LuaSwitchAdapterStatementSyntax>();

private int releaseTempIdentifierCount_;
private int noImportTypeNameCounter_;
public bool IsNoImportTypeName => noImportTypeNameCounter_ > 0;
private int genericTypeCounter_;
Expand Down Expand Up @@ -189,6 +191,7 @@ public void PopBlock() {
if (block.TempCount > 0) {
Contract.Assert(CurFunction.TempCount >= block.TempCount);
CurFunction.TempCount -= block.TempCount;
releaseTempIdentifierCount_ = 0;
}
}

Expand Down Expand Up @@ -216,18 +219,24 @@ private LuaIdentifierNameSyntax GetTempIdentifier() {

private void ReleaseTempIdentifiers(int prevTempCount) {
int count = CurFunction.TempCount - prevTempCount;
for (int i = 0; i < count; ++i) {
Contract.Assert(CurBlock.TempCount >= 1 && CurFunction.TempCount >= 1);
--CurBlock.TempCount;
--CurFunction.TempCount;
}
PopTempCount(count);
}

private void PopTempCount(int count) {
Contract.Assert(CurBlock.TempCount >= count && CurFunction.TempCount >= count);
CurBlock.TempCount -= count;
CurFunction.TempCount -= count;
}

private void ReleaseTempIdentifier(LuaIdentifierNameSyntax tempName) {
Contract.Assert(CurBlock.TempCount >= 1 && CurFunction.TempCount >= 1);
Contract.Assert(LuaSyntaxNode.TempIdentifiers.Contains(tempName.ValueText));
--CurBlock.TempCount;
--CurFunction.TempCount;
private void AddReleaseTempIdentifier(LuaIdentifierNameSyntax tempName) {
++releaseTempIdentifierCount_;
}

private void ReleaseTempIdentifiers() {
if (releaseTempIdentifierCount_ > 0) {
PopTempCount(releaseTempIdentifierCount_);
releaseTempIdentifierCount_ = 0;
}
}

public LuaCompilationUnitSyntax VisitCompilationUnit(CompilationUnitSyntax node, bool isSingleFile = false) {
Expand Down Expand Up @@ -1535,6 +1544,7 @@ public override LuaSyntaxNode VisitReturnStatement(ReturnStatementSyntax node) {

public override LuaSyntaxNode VisitExpressionStatement(ExpressionStatementSyntax node) {
var expression = node.Expression.AcceptExpression(this);
ReleaseTempIdentifiers();
if (expression != LuaExpressionSyntax.EmptyExpression) {
if (expression is LuaLiteralExpressionSyntax) {
return new LuaShortCommentExpressionStatement(expression);
Expand Down Expand Up @@ -4297,7 +4307,7 @@ public override LuaSyntaxNode VisitBinaryExpression(BinaryExpressionSyntax node)
var typeSymbol = semanticModel_.GetTypeInfo(node.Left).Type;
bool isBool = typeSymbol != null && typeSymbol.IsBoolType();
if (!isBool) {
ReleaseTempIdentifier(temp);
AddReleaseTempIdentifier(temp);
return left.Binary(GetOperatorToken(node.OperatorToken), right);
}
}
Expand Down

0 comments on commit 94210ca

Please sign in to comment.