Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

replaced CairoAssert by FunctionCall #1057

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions src/ast/cairoNodes/cairoAssert.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/ast/cairoNodes/export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './cairoAssert';
export * from './cairoFunctionDefinition';
export * from './cairoContract';
export * from './cairoTempVarStatement';
1 change: 0 additions & 1 deletion src/ast/cairoNodes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './cairoAssert';
export * from './cairoContract';
export * from './cairoGeneratedFunctionDefinition';
export * from './cairoFunctionDefinition';
Expand Down
11 changes: 1 addition & 10 deletions src/ast/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ import {
StatementWithChildren,
ASTNodeWithChildren,
} from 'solc-typed-ast';
import {
CairoAssert,
CairoContract,
CairoFunctionDefinition,
CairoTempVarStatement,
} from './cairoNodes';
import { CairoContract, CairoFunctionDefinition, CairoTempVarStatement } from './cairoNodes';

import { AST } from './ast';
import { CairoGeneratedFunctionDefinition } from './cairoNodes/cairoGeneratedFunctionDefinition';
Expand Down Expand Up @@ -106,7 +101,6 @@ export abstract class ASTVisitor<T> {
// Expression
else if (node instanceof Assignment) res = this.visitAssignment(node, ast);
else if (node instanceof BinaryOperation) res = this.visitBinaryOperation(node, ast);
else if (node instanceof CairoAssert) res = this.visitCairoAssert(node, ast);
else if (node instanceof Conditional) res = this.visitConditional(node, ast);
else if (node instanceof ElementaryTypeNameExpression)
res = this.visitElementaryTypeNameExpression(node, ast);
Expand Down Expand Up @@ -349,9 +343,6 @@ export abstract class ASTVisitor<T> {
visitSourceUnit(node: SourceUnit, ast: AST): T {
return this.visitASTNodeWithChildren(node, ast);
}
visitCairoAssert(node: CairoAssert, ast: AST): T {
return this.visitExpression(node, ast);
}
visitTypeName(node: TypeName, ast: AST): T {
return this.commonVisit(node, ast);
}
Expand Down
3 changes: 0 additions & 3 deletions src/cairoWriter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import {
WhileStatement,
} from 'solc-typed-ast';
import {
CairoAssert,
CairoContract,
CairoFunctionDefinition,
CairoGeneratedFunctionDefinition,
Expand All @@ -69,7 +68,6 @@ import {
AssignmentWriter,
BinaryOperationWriter,
BlockWriter,
CairoAssertWriter,
CairoContractWriter,
CairoFunctionDefinitionWriter,
CairoTempVarWriter,
Expand Down Expand Up @@ -104,7 +102,6 @@ export const CairoASTMapping = (ast: AST, throwOnUnimplemented: boolean) =>
[BinaryOperation, new BinaryOperationWriter(ast, throwOnUnimplemented)],
[Block, new BlockWriter(ast, throwOnUnimplemented)],
[Break, new NotImplementedWriter(ast, throwOnUnimplemented)],
[CairoAssert, new CairoAssertWriter(ast, throwOnUnimplemented)],
[CairoContract, new CairoContractWriter(ast, throwOnUnimplemented)],
[CairoFunctionDefinition, new CairoFunctionDefinitionWriter(ast, throwOnUnimplemented)],
[
Expand Down
11 changes: 0 additions & 11 deletions src/cairoWriter/writers/cairoAssertWriter.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/cairoWriter/writers/expressionStatementWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
FunctionCallKind,
SrcDesc,
} from 'solc-typed-ast';
import { CairoAssert } from '../../ast/cairoNodes';
import { CairoASTNodeWriter } from '../base';
import { getDocumentation } from '../utils';

Expand All @@ -17,8 +16,7 @@ export class ExpressionStatementWriter extends CairoASTNodeWriter {
if (
(node.vExpression instanceof FunctionCall &&
node.vExpression.kind !== FunctionCallKind.StructConstructorCall) ||
node.vExpression instanceof Assignment ||
node.vExpression instanceof CairoAssert
node.vExpression instanceof Assignment
) {
return [[documentation, `${writer.write(node.vExpression)};`].join('\n')];
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/cairoWriter/writers/functionCallWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export class FunctionCallWriter extends CairoASTNodeWriter {
const func = writer.write(node.vExpression);
switch (node.kind) {
case FunctionCallKind.FunctionCall: {
if (node.vExpression instanceof MemberAccess) {
if (['assert', 'require', 'revert'].includes(func)) {
return [`assert(${args}${node.vArguments.length === 1 ? ", 'Assertion error'" : ''})`];
cicr99 marked this conversation as resolved.
Show resolved Hide resolved
} else if (node.vExpression instanceof MemberAccess) {
// check if we're calling a member of a contract
const nodeType = safeGetNodeType(node.vExpression.vExpression, this.ast.inference);
if (
Expand Down
1 change: 0 additions & 1 deletion src/cairoWriter/writers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './assignmentWriter';
export * from './binaryOperationWriter';
export * from './blockWriter';
export * from './cairoAssertWriter';
export * from './cairoContractWriter';
export * from './cairoFunctionDefinitionWriter';
export * from './cairoGeneratedFunctionDefinitionWriter';
Expand Down
28 changes: 8 additions & 20 deletions src/passes/builtinHandler/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import {
ExpressionStatement,
ExternalReferenceType,
FunctionCall,
Literal,
Return,
} from 'solc-typed-ast';
import { AST } from '../../ast/ast';
import { CairoAssert } from '../../ast/cairoNodes';
import { ASTMapper } from '../../ast/mapper';
import { createBoolLiteral } from '../../utils/nodeTemplates';
import { cloneASTNode } from '../../export';

export class Require extends ASTMapper {
// Function to add passes that should have been run before this pass
Expand All @@ -28,7 +27,7 @@ export class Require extends ASTMapper {
return;
}

// Since the cairoAssert is not null, we have a require/revert/assert function call at hand
// Since cairoAssert is not null, this is a require/revert/assert function call
assert(expressionNode instanceof FunctionCall);

ast.replaceNode(node, cairoAssert);
Expand All @@ -50,33 +49,22 @@ export class Require extends ASTMapper {
}

if (expression.vIdentifier === 'require' || expression.vIdentifier === 'assert') {
const requireMessage =
expression.vArguments[1] instanceof Literal ? expression.vArguments[1].value : null;

return new ExpressionStatement(
ast.reserveId(),
expression.src,
new CairoAssert(
ast.reserveId(),
expression.src,
expression.vArguments[0],
requireMessage,
expression.raw,
),
cloneASTNode(expression, ast),
);
} else if (expression.vIdentifier === 'revert') {
const revertMessage =
expression.vArguments[0] instanceof Literal ? expression.vArguments[0].value : null;

return new ExpressionStatement(
ast.reserveId(),
expression.src,
new CairoAssert(
new FunctionCall(
ast.reserveId(),
expression.src,
createBoolLiteral(false, ast),
revertMessage,
expression.raw,
expression.typeString,
expression.kind,
expression.vExpression,
[createBoolLiteral(false, ast), ...expression.vArguments],
),
);
}
Expand Down
10 changes: 2 additions & 8 deletions src/passes/references/expectedLocationAnalyser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
VariableDeclarationStatement,
} from 'solc-typed-ast';
import { AST } from '../../ast/ast';
import { CairoAssert } from '../../ast/cairoNodes';
import { ASTMapper } from '../../ast/mapper';
import { locationIfComplexType } from '../../cairoUtilFuncGen/base';
import { printNode } from '../../utils/astPrinter';
Expand Down Expand Up @@ -139,9 +138,9 @@ export class ExpectedLocationAnalyser extends ASTMapper {

const parameterTypes = getParameterTypes(node, ast);
// When calling `push`, the function receives two parameters nonetheless the argument is just one
// This does not explode because javascript does not gives an index out of range exception
// This does not explode because javascript does not give an index out of range exception
node.vArguments.forEach((arg, index) => {
// Solc 0.7.0 types push and pop as you would expect, 0.8.0 adds an extra initial argument
// Solc 0.7.0 types push and pop as expected, 0.8.0 adds an extra initial argument
const paramIndex = index + parameterTypes.length - node.vArguments.length;
const t = parameterTypes[paramIndex];
if (t instanceof PointerType) {
Expand Down Expand Up @@ -320,11 +319,6 @@ export class ExpectedLocationAnalyser extends ASTMapper {
this.visitStatement(node, ast);
}

visitCairoAssert(node: CairoAssert, ast: AST): void {
esdras-santos marked this conversation as resolved.
Show resolved Hide resolved
this.expectedLocations.set(node.vExpression, DataLocation.Default);
this.visitExpression(node, ast);
}

visitIfStatement(node: IfStatement, ast: AST): void {
this.expectedLocations.set(node.vCondition, DataLocation.Default);
this.visitStatement(node, ast);
Expand Down
10 changes: 0 additions & 10 deletions src/solWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
SrcDesc,
} from 'solc-typed-ast';
import {
CairoAssert,
CairoContract,
CairoFunctionDefinition,
CairoGeneratedFunctionDefinition,
Expand Down Expand Up @@ -108,21 +107,12 @@ class CairoImportFunctionDefinitionSolWriter extends ASTNodeWriter {
}
}

class CairoAssertSolWriter extends ASTNodeWriter {
writeInner(node: CairoAssert, writer: ASTWriter): SrcDesc {
const result: SrcDesc = [];
result.push(`<cairo information> assert ${writer.write(node.vExpression)} = 1`);
return result;
}
}

const CairoExtendedASTWriterMapping = (printStubs: boolean) =>
new Map<ASTNodeConstructor<ASTNode>, ASTNodeWriter>([
[CairoContract, new CairoContractSolWriter()],
[CairoFunctionDefinition, new CairoFunctionDefinitionSolWriter(printStubs)],
[CairoGeneratedFunctionDefinition, new CairoGeneratedFunctionDefinitionSolWriter()],
[CairoImportFunctionDefinition, new CairoImportFunctionDefinitionSolWriter()],
[CairoAssert, new CairoAssertSolWriter()],
]);

export const CairoToSolASTWriterMapping = (printStubs: boolean) =>
Expand Down
6 changes: 2 additions & 4 deletions src/utils/astChecking.ts
cicr99 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ import {
} from 'solc-typed-ast';
import { pp } from 'solc-typed-ast/dist/misc/index';
import { AST } from '../ast/ast';
import { CairoAssert, CairoTempVarStatement } from '../ast/cairoNodes';
import { CairoTempVarStatement } from '../ast/cairoNodes';
import { ASTMapper } from '../ast/mapper';
import { printNode } from './astPrinter';
import { InsaneASTError } from './errors';
import { safeGetNodeType } from './nodeTypeProcessing';
import { isNameless } from './utils';

// This is the solc-typed-ast AST checking code, with additions for CairoAssert and CairoContract
// This is the solc-typed-ast AST checking code, with additions for CairoContract

/**
* Helper function to check if the node/nodes `arg` is in the `ASTContext` `ctx`.
Expand Down Expand Up @@ -667,8 +667,6 @@ export function checkSane(unit: SourceUnit, ctx: ASTContext): void {
} else if (node instanceof UnaryOperation) {
checkVFieldCtx(node, 'vSubExpression', ctx);
checkDirectChildren(node, 'vSubExpression');
} else if (node instanceof CairoAssert) {
checkDirectChildren(node, 'vExpression');
} else if (node instanceof CairoTempVarStatement) {
// Not being checked because this node does not get affected by any
// other ast pass
Expand Down
10 changes: 1 addition & 9 deletions src/utils/cloning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
WhileStatement,
} from 'solc-typed-ast';
import { AST } from '../ast/ast';
import { CairoAssert, CairoFunctionDefinition } from '../ast/cairoNodes';
import { CairoFunctionDefinition } from '../ast/cairoNodes';
import { printNode } from './astPrinter';
import { NotSupportedYetError, TranspileFailedError } from './errors';
import { createParameterList } from './nodeTemplates';
Expand Down Expand Up @@ -104,14 +104,6 @@ function cloneASTNodeImpl<T extends ASTNode>(
cloneASTNodeImpl(node.vRightExpression, ast, remappedIds),
node.raw,
);
} else if (node instanceof CairoAssert) {
newNode = new CairoAssert(
replaceId(node.id, ast, remappedIds),
node.src,
cloneASTNodeImpl(node.vExpression, ast, remappedIds),
node.assertMessage,
node.raw,
);
} else if (node instanceof ElementaryTypeNameExpression) {
newNode = new ElementaryTypeNameExpression(
replaceId(node.id, ast, remappedIds),
Expand Down
1 change: 0 additions & 1 deletion src/utils/nodeTypeProcessing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import { TranspileFailedError } from './errors';
import { error } from './formatting';
import { getContainingSourceUnit } from './utils';
import { getNodeType, getNodeTypeInCtx } from './typeStrings/typeStringParser';
import { CairoAssert } from '../ast/cairoNodes'; // eslint-disable-line

/*
Normal function calls and struct constructors require different methods for
Expand Down
6 changes: 3 additions & 3 deletions tests/compilation/compilation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ const expectedResults = new Map<string, ResultType>(
['deleteUses.sol', 'Success'],
['enums.sol', 'Success'],
['enums7.sol', 'Success'],
['errorHandling/assert.sol', 'Success'],
['errorHandling/require.sol', 'Success'],
['errorHandling/revert.sol', 'Success'],
['errorHandling/assertHandling.sol', 'Success'],
['errorHandling/requireHandling.sol', 'Success'],
['errorHandling/revertHandling.sol', 'Success'],
['events.sol', 'Success'],
['expressionSplitter/assignSimple.sol', 'Success'],
['expressionSplitter/funcCallSimple.sol', 'Success'],
Expand Down
cicr99 marked this conversation as resolved.
Show resolved Hide resolved
File renamed without changes.
7 changes: 4 additions & 3 deletions tests/compilation/passingContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ export const passingContracts = [
// 'tests/compilation/contracts/deleteUses.sol',
'tests/compilation/contracts/enums.sol',
// 'tests/compilation/contracts/enums7.sol',
// 'tests/compilation/contracts/errorHandling/assert.sol',
// 'tests/compilation/contracts/errorHandling/require.sol',
// 'tests/compilation/contracts/errorHandling/revert.sol',
'tests/compilation/contracts/errorHandling/assertHandling.sol',
'tests/compilation/contracts/errorHandling/requireHandling.sol',
// TODO: uncomment, when input checks are handled
// 'tests/compilation/contracts/errorHandling/revertHandling.sol',
// 'tests/compilation/contracts/events.sol',
// 'tests/compilation/contracts/expressionSplitter/assignSimple.sol',
// 'tests/compilation/contracts/expressionSplitter/funcCallSimple.sol',
Expand Down