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

New Cairo Gen #869

Merged
merged 197 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
197 commits
Select commit Hold shift + click to select a range
fa09655
Add nodes to handle imports and string defined functions
cicr99 Jan 5, 2023
94fc950
Merge branch 'develop' into nodes_for_cairo_functions
rodrigo-pino Jan 6, 2023
b3cf920
CairoTempvarStatment.ts -> cairoTempvarStatement.ts
rodrigo-pino Jan 6, 2023
3a25af6
functions for imports from abiEncodePacked
AlejandroLabourdette Jan 10, 2023
ca6838c
File rename
rodrigo-pino Jan 10, 2023
6f0b43b
added a general createImportFunDef to be used externally
AlejandroLabourdette Jan 11, 2023
8ad1e7b
fix not to add import if already exist
AlejandroLabourdette Jan 11, 2023
8b88e12
fix ast inconsistency after import nodes were added
AlejandroLabourdette Jan 11, 2023
6957328
better comments
AlejandroLabourdette Jan 11, 2023
f19d2a8
added abiDecode.ts imports
AlejandroLabourdette Jan 11, 2023
df6ae3f
added abiEncode.ts imports
AlejandroLabourdette Jan 11, 2023
1427fa1
added abiEncodeWithSelector.ts imports
AlejandroLabourdette Jan 11, 2023
ad7b5fa
added abiEncodeWithSignature.ts imports
AlejandroLabourdette Jan 11, 2023
2647791
added indexEncode.ts imports
AlejandroLabourdette Jan 11, 2023
d38773e
added calldataToMemory.ts imports
AlejandroLabourdette Jan 11, 2023
50bdc3b
added calldataToStorage.ts imports
AlejandroLabourdette Jan 11, 2023
a393e83
Merge branch 'import_utils' into new-cairo-gen
rodrigo-pino Jan 12, 2023
6a6d1f6
[wip] working things out
rodrigo-pino Jan 12, 2023
3a62fbb
Fix abi decoce
rodrigo-pino Jan 17, 2023
7cd52a9
Update all abi writers
rodrigo-pino Jan 17, 2023
ba91257
rewritten memory/ and storage/ scripts
AlejandroLabourdette Jan 17, 2023
9725727
Merge branch 'new-cairo-gen' into new-cairo-gen-alab
AlejandroLabourdette Jan 17, 2023
aa23682
rewritten calldata/ and inputArgCheck/ scripts
AlejandroLabourdette Jan 18, 2023
4987b44
Merge branch 'develop' into new-cairo-gen-rodro
rodrigo-pino Jan 18, 2023
b0b0218
Update cairo gen writing methods
rodrigo-pino Jan 18, 2023
851d0fd
Update function prunner and anotate implicits for new generated funct…
rodrigo-pino Jan 18, 2023
ff0d68d
fix funcsCalled references and direct imports fun call in memory/ sto…
AlejandroLabourdette Jan 19, 2023
84b9739
Merge branch 'new-cairo-gen' into new-cairo-gen-alab
AlejandroLabourdette Jan 19, 2023
d7ec09f
Add new writer for gen funcs
rodrigo-pino Jan 19, 2023
bd731b4
fix funcsCalled references and direct imports in calldata/ scripts
AlejandroLabourdette Jan 19, 2023
fd43828
Merge branch 'new-cairo-gen' of https://github.com/NethermindEth/warp…
rodrigo-pino Jan 20, 2023
0cc3728
Update abis
rodrigo-pino Jan 20, 2023
151e5aa
Update memory read and memory write
rodrigo-pino Jan 20, 2023
dd88042
Add new test
rodrigo-pino Jan 20, 2023
9df5d49
Delete dumb imports
rodrigo-pino Jan 20, 2023
94dfaf7
Minor fix
rodrigo-pino Jan 20, 2023
d25b50f
Remove unused constant
rodrigo-pino Jan 20, 2023
04f5236
Update nodes to parse implicit from code
rodrigo-pino Jan 20, 2023
11be228
Minor fixes
rodrigo-pino Jan 20, 2023
3d18a8c
Update all abi stuff
rodrigo-pino Jan 20, 2023
efe42e7
minor fixes
rodrigo-pino Jan 20, 2023
a7bfed6
Import deletion
rodrigo-pino Jan 20, 2023
40efefc
Register function definitions
AlejandroLabourdette Jan 23, 2023
90d2b01
Minor changes
rodrigo-pino Jan 23, 2023
231d0cd
removed generatedFunctions map and set getOrCreate as private
AlejandroLabourdette Jan 23, 2023
96f2a96
remove implicits commented
AlejandroLabourdette Jan 23, 2023
e9ac050
Merge branch 'new-cairo-gen' of https://github.com/NethermindEth/warp…
rodrigo-pino Jan 23, 2023
174f383
Some general fixes
rodrigo-pino Jan 23, 2023
9dc0f6b
compile errors in calldata/ memory/ and storage/
AlejandroLabourdette Jan 23, 2023
33c5d1e
Update abi
rodrigo-pino Jan 23, 2023
fda2e8d
refactor scripts: encodeToFelt.ts, event.ts and enumInputCheck.ts
AlejandroLabourdette Jan 23, 2023
a91b083
fix arguments calling gen functions
AlejandroLabourdette Jan 23, 2023
3800d1a
Remove unused stuff
rodrigo-pino Jan 23, 2023
69ab401
Merge branch 'develop' into new-cairo-gen
rodrigo-pino Jan 23, 2023
c04da44
Refactor import utils
rodrigo-pino Jan 24, 2023
74b48ee
Update warplib logic
rodrigo-pino Jan 25, 2023
3d25b20
Remove part of old import system
rodrigo-pino Jan 26, 2023
aadbf45
Add more options to new nodes
rodrigo-pino Jan 26, 2023
7a88b6e
Bug fix to raw string function
rodrigo-pino Jan 26, 2023
b093e3c
Update imports
rodrigo-pino Jan 26, 2023
a714709
General improvements to utilFuncGen
rodrigo-pino Jan 26, 2023
367feec
Bug fixing in InputCheck
rodrigo-pino Jan 26, 2023
5e5f40e
Improvemets to arrayConcat but still is wip
rodrigo-pino Jan 26, 2023
723ade7
Bug fixing in memory to calldata
rodrigo-pino Jan 26, 2023
279e05b
Update to cairo writer
rodrigo-pino Jan 26, 2023
b1b590f
Structural updates overall
rodrigo-pino Jan 26, 2023
1faf86c
Add new import functions
rodrigo-pino Jan 26, 2023
e5ce7ad
Minor updates to warplib
rodrigo-pino Jan 26, 2023
1fc97a1
Add an importFunction writer
rodrigo-pino Jan 26, 2023
699614b
Move parsing utilities to it's own file
rodrigo-pino Jan 26, 2023
df7fb11
Merge branch 'develop' into new-cairo-gen
rodrigo-pino Jan 26, 2023
89464d7
Overall improvements
rodrigo-pino Jan 27, 2023
ed17c21
Update half of storage scripts + minor things
rodrigo-pino Jan 27, 2023
e6fd627
Some tidying
rodrigo-pino Jan 27, 2023
1634754
Fix copyToStorage
rodrigo-pino Jan 28, 2023
ddea3c6
Fix dynArrayIndexAccess
rodrigo-pino Jan 28, 2023
9e69623
Modified the way dynArray.ts worked
rodrigo-pino Jan 28, 2023
de5702f
Update almost all storage (eventIndexAccess for last)
rodrigo-pino Jan 30, 2023
4783c0e
refactoring to importfuncGenerator
rodrigo-pino Jan 30, 2023
7a7ed87
Fix arrayConcat
rodrigo-pino Jan 30, 2023
1289db8
Refactor memory/arrayLiteral
rodrigo-pino Jan 31, 2023
dfd23d8
Fix memory implicit conversion
rodrigo-pino Jan 31, 2023
892697d
Fix memory member access
rodrigo-pino Jan 31, 2023
2e70fc3
Fix memoryRead and memoryStruct
rodrigo-pino Jan 31, 2023
2ba92dd
Some updates in util tools
rodrigo-pino Jan 31, 2023
e4cafc6
Fix memory to calldata
rodrigo-pino Feb 1, 2023
0adb087
Fix memory to storage
rodrigo-pino Feb 1, 2023
b7e421e
Minor upgrades
rodrigo-pino Feb 1, 2023
4e8c634
Fix calldata to memory
rodrigo-pino Feb 2, 2023
d029b8f
Fix calldata to storage
rodrigo-pino Feb 2, 2023
ba58139
Fix externalDynArrayStructConstructor
rodrigo-pino Feb 2, 2023
4c893c7
Big rework on calldata's implicit array conversion
rodrigo-pino Feb 2, 2023
827f5ab
nitpick in comments
rodrigo-pino Feb 2, 2023
ce99feb
Minor update to import funcs logic
rodrigo-pino Feb 3, 2023
97d9f43
Fix input checks
rodrigo-pino Feb 3, 2023
25c9b81
Fix memory dyn array length
rodrigo-pino Feb 3, 2023
ee7461d
Fix memory static index accesss
rodrigo-pino Feb 3, 2023
7470eec
Fix mapping storage index access
rodrigo-pino Feb 3, 2023
6f3eac2
Update all warplib for new imports
rodrigo-pino Feb 3, 2023
2bdb9be
Update all cairoUtilFunc gen
rodrigo-pino Feb 3, 2023
4092090
Update all builtin handlers
rodrigo-pino Feb 3, 2023
7fdaf34
Update bytesConverter
rodrigo-pino Feb 3, 2023
9c749cc
Update cairoUitlImporter
rodrigo-pino Feb 3, 2023
8cd5461
Update newToDeploy
rodrigo-pino Feb 3, 2023
62f4e92
Update reference imports
rodrigo-pino Feb 3, 2023
435e028
Minor update
rodrigo-pino Feb 3, 2023
d64f07e
A lot of lintiiiiii....iiing
rodrigo-pino Feb 3, 2023
aa1a0a6
More linting
rodrigo-pino Feb 3, 2023
1593137
Merge branch 'develop' into new-cairo-gen
rodrigo-pino Feb 3, 2023
794d197
Update for carmen
rodrigo-pino Feb 6, 2023
23d2bcd
A patch for import logic
rodrigo-pino Feb 6, 2023
b908f23
Improve warplib import gathering
rodrigo-pino Feb 6, 2023
5b79e0e
Bug fixing
rodrigo-pino Feb 7, 2023
8e01e1e
Update new cairo nodes
rodrigo-pino Feb 8, 2023
54f4135
Fixed calldata to memory bugs
rodrigo-pino Feb 8, 2023
fb58d6b
Fix infinite recursion
rodrigo-pino Feb 8, 2023
577853f
Update memory read and write imports signature
rodrigo-pino Feb 8, 2023
ec6d031
Several fixes and improvements overall
rodrigo-pino Feb 8, 2023
3d8b39e
Minor improvements in storage func gen
rodrigo-pino Feb 8, 2023
aa50f93
Several other bug fixes through all warp
rodrigo-pino Feb 8, 2023
32ed9f2
Add solidity writer for new nodes
rodrigo-pino Feb 8, 2023
030f870
Overall fixes improvements to function generation
rodrigo-pino Feb 8, 2023
d1b63ed
Other fixes and improvements
rodrigo-pino Feb 8, 2023
edda4cf
Minor fixes in abi packed encoding
rodrigo-pino Feb 9, 2023
c8a24ad
Fixes in encode to felt and memory member access
rodrigo-pino Feb 9, 2023
68a7f57
Had to patch :(
rodrigo-pino Feb 9, 2023
3a0833a
linting
rodrigo-pino Feb 9, 2023
7250eed
Adecuate fix to function prunning
rodrigo-pino Feb 9, 2023
9394b9b
Add missing imports
rodrigo-pino Feb 9, 2023
0905ad2
Fix event util func gen
rodrigo-pino Feb 9, 2023
84e6724
Further fixes to storage to memory
rodrigo-pino Feb 9, 2023
7eda29a
Fixes to source unit writer
rodrigo-pino Feb 9, 2023
faa911e
Minor bug fixing
rodrigo-pino Feb 9, 2023
7e960f9
Merge branch 'develop' into new-cairo-gen
rodrigo-pino Feb 9, 2023
83e841e
Fixed remaining compilation bugs
rodrigo-pino Feb 9, 2023
370eea5
Other minor fixes
rodrigo-pino Feb 9, 2023
780697a
Bug fix
rodrigo-pino Feb 9, 2023
9cab26d
Small fix
rodrigo-pino Feb 9, 2023
e493119
Linting
rodrigo-pino Feb 9, 2023
a78c30a
Fixes in array memory concats
rodrigo-pino Feb 9, 2023
57fa22e
Fixing runtime behaviour
rodrigo-pino Feb 10, 2023
d924323
Fix more runtime behaviour
rodrigo-pino Feb 10, 2023
5afe89a
Fixes fixes fixes fixes ...
rodrigo-pino Feb 10, 2023
5586711
Bf
rodrigo-pino Feb 10, 2023
c23b065
Remove logging
rodrigo-pino Feb 12, 2023
c313133
Bug fix in mapping index access
rodrigo-pino Feb 12, 2023
01e55af
Merge branch 'develop' into new-cairo-gen
rodrigo-pino Feb 12, 2023
dcb2300
Bug fixing in calldata to storage implicit array conversion
rodrigo-pino Feb 12, 2023
d36bf19
Big batch of small bug fixes
rodrigo-pino Feb 13, 2023
3f45869
Fix to memory to calldata
rodrigo-pino Feb 13, 2023
bb9af75
Fix encodeToFelt and other writers
rodrigo-pino Feb 13, 2023
28729f3
Linting
rodrigo-pino Feb 13, 2023
73fc22f
Bug fix in memory to storage semantics
rodrigo-pino Feb 13, 2023
1c496e7
Add patch for free structs
rodrigo-pino Feb 14, 2023
6d8a3f1
Linting
rodrigo-pino Feb 14, 2023
31a60c7
group imports
cicr99 Feb 14, 2023
3b9e1d4
:(
rodrigo-pino Feb 14, 2023
d05d95b
Minor bug fix in memory copy code
rodrigo-pino Feb 14, 2023
d9c09ba
Buf fixes + minor improvs
rodrigo-pino Feb 14, 2023
77f8801
Memory to storage minor fix.. .Again
rodrigo-pino Feb 14, 2023
4284d80
Hot fix to for free structs
rodrigo-pino Feb 14, 2023
a3fd5dd
Patching through
rodrigo-pino Feb 14, 2023
36d6251
Fixes for recursive reference types in storage to memory
rodrigo-pino Feb 14, 2023
6714c32
bug fix in memory to calldata
rodrigo-pino Feb 14, 2023
0450934
Big fix in storage delete
rodrigo-pino Feb 14, 2023
57341d3
Fix static to dynamic calldata conversion
rodrigo-pino Feb 14, 2023
0432cd5
Import
rodrigo-pino Feb 14, 2023
0114b91
Fix recursion bug in storage to memory
rodrigo-pino Feb 14, 2023
bfabf9e
minor bug fixes
rodrigo-pino Feb 15, 2023
5e454cd
I think, this commit is the commit
rodrigo-pino Feb 15, 2023
a529864
Minor fix to packed encoding
rodrigo-pino Feb 15, 2023
fb6d4f6
Remove console logs
rodrigo-pino Feb 15, 2023
659f406
Merge remote-tracking branch 'origin/group_imports' into new-cairo-gen
rodrigo-pino Feb 15, 2023
17de3b9
Merge branch 'develop' into new-cairo-gen
rodrigo-pino Feb 16, 2023
01f354a
Fix warplib inconsistencies
rodrigo-pino Feb 16, 2023
7e1d60f
Rename
rodrigo-pino Feb 16, 2023
0a3f0ee
Update testing
rodrigo-pino Feb 16, 2023
5209d6f
class rename
rodrigo-pino Feb 16, 2023
b464f61
Leftovers
rodrigo-pino Feb 16, 2023
49ac64c
Address reviews
rodrigo-pino Feb 20, 2023
50d1727
Merge branch 'develop' into new-cairo-gen
rodrigo-pino Feb 20, 2023
80450c9
Minor bug fix
rodrigo-pino Feb 20, 2023
4f571d4
Reviews
rodrigo-pino Feb 21, 2023
6e095ff
Linting
rodrigo-pino Feb 21, 2023
502c682
Review
rodrigo-pino Feb 24, 2023
a4e7e7f
Clean
rodrigo-pino Feb 24, 2023
c20f536
Remove ! and minor optimization
rodrigo-pino Feb 27, 2023
defb61c
Address review
rodrigo-pino Feb 27, 2023
739a08c
Solve with glob
rodrigo-pino Feb 27, 2023
47aa36e
Remove repeated code
rodrigo-pino Feb 28, 2023
78c2d58
Update import search logic
rodrigo-pino Feb 28, 2023
91c1530
Add comment
rodrigo-pino Feb 28, 2023
9914467
Dummy
rodrigo-pino Feb 28, 2023
8cce389
undummy
rodrigo-pino Feb 28, 2023
b96e568
Linting
rodrigo-pino Feb 28, 2023
5ea1e03
lint for lint
rodrigo-pino Feb 28, 2023
10c236f
fix errors in imports path
cicr99 Mar 1, 2023
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
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"eqeqeq": "error",
"no-unused-vars": "off",
"unicorn/filename-case": ["error", { "case": "camelCase" }],
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"@typescript-eslint/no-unused-vars": [
"error",
{ "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }
],
"@typescript-eslint/ban-ts-comment": ["error", { "ts-ignore": "allow-with-description" }]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The system works in the following way:

1. To start a Cairo Block add your Cairo code above a Solidity function with 3 forward slashes at the beginning of each line and the phrase `warp-cairo` at the top.
2. The user then uses a number of MACROS to interact with the transpiled contract.
3. The Soldiity function will then be replaced with the Cario function that is above it.
3. The Solidity function will then be replaced with the Cairo function that is above it.

The following MACROS are supported:

Expand Down
12 changes: 12 additions & 0 deletions exampleContracts/internalFunctions.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.6;

contract WARP {
function f(
uint256 x,
uint8 y,
uint8 z
) internal pure returns (bytes memory) {
return abi.encode(x, y, z);
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@
},
"dependencies": {
"@algorithm.ts/gcd": "^2.0.14",
"@types/glob": "^8.1.0",
"chalk": "^4.1.2",
"commander": "^9.1.0",
"eslint-plugin-unicorn": "^45.0.2",
"ethers": "^5.6.2",
"glob": "^8.1.0",
"keccak": "^3.0.2",
"peggy": "^1.2.0",
"prompts": "^2.4.2",
Expand Down
66 changes: 16 additions & 50 deletions src/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import { printNode } from '../utils/astPrinter';
import { TranspileFailedError } from '../utils/errors';
import { Implicits } from '../utils/implicits';
import { createBlock } from '../utils/nodeTemplates';
import { createImport } from '../utils/importFuncGenerator';
import { safeGetNodeType } from '../utils/nodeTypeProcessing';
import { getContainingSourceUnit, isExternalCall, mergeImports } from '../utils/utils';
import { CairoFunctionDefinition } from './cairoNodes';
import { getContainingSourceUnit, isExternalCall } from '../utils/utils';
import { CairoFunctionDefinition, CairoImportFunctionDefinition } from './cairoNodes';
import { ParameterInfo } from '../export';

/*
A centralised store of information required for transpilation, a reference
Expand All @@ -54,8 +56,6 @@ export class AST {
private cairoUtilFuncGen: Map<number, CairoUtilFuncGen> = new Map();

context: ASTContext;
// node requiring cairo import -> file to import from -> symbols to import
imports: Map<ASTNode, Map<string, Set<string>>> = new Map();
public inference: InferType;

readonly tempId = -1;
Expand All @@ -81,16 +81,6 @@ export class AST {
);
}

copyRegisteredImports(oldNode: ASTNode, newNode: ASTNode): void {
this.imports.set(
newNode,
mergeImports(
this.imports.get(oldNode) ?? new Map<string, Set<string>>(),
this.imports.get(newNode) ?? new Map<string, Set<string>>(),
),
);
}

extractToConstant(
node: Expression,
vType: TypeName,
Expand Down Expand Up @@ -175,20 +165,6 @@ export class AST {
return containingFunction.implicits;
}

getImports(sourceUnit: SourceUnit): Map<string, Set<string>> {
assert(
this.roots.includes(sourceUnit),
`Tried to get imports associated with ${printNode(
sourceUnit,
)}, which is not one of the roots of the AST`,
);
const reachableNodeImports = sourceUnit
.getChildren(true)
.map((node) => this.imports.get(node) ?? new Map<string, Set<string>>());
const utilFunctionImports = this.getUtilFuncGen(sourceUnit)?.getImports();
return mergeImports(utilFunctionImports, ...reachableNodeImports);
}

getUtilFuncGen(node: ASTNode): CairoUtilFuncGen {
const sourceUnit = node instanceof SourceUnit ? node : getContainingSourceUnit(node);
const gen = this.cairoUtilFuncGen.get(sourceUnit.id);
Expand Down Expand Up @@ -298,12 +274,15 @@ export class AST {
return child.id;
}

registerImport(node: ASTNode, location: string, name: string): void {
const nodeImports = this.imports.get(node) ?? new Map<string, Set<string>>();
const fileImports = nodeImports.get(location) ?? new Set<string>();
fileImports.add(name);
nodeImports.set(location, fileImports);
this.imports.set(node, nodeImports);
registerImport(
node: ASTNode,
location: string,
name: string,
inputs: ParameterInfo[],
outputs: ParameterInfo[],
options?: { acceptsRawDarray?: boolean; acceptsUnpackedStructArray?: boolean },
): CairoImportFunctionDefinition {
return createImport(location, name, node, this, inputs, outputs, options);
}

removeStatement(statement: Statement): void {
Expand All @@ -317,19 +296,9 @@ export class AST {
}

// Reference notes/astnodetypes.ts for exact restrictions on what can safely be replaced with what
replaceNode(
oldNode: Expression,
newNode: Expression,
parent?: ASTNode,
copyImports?: boolean,
): number;
replaceNode(
oldNode: Statement,
newNode: Statement,
parent?: ASTNode,
copyImports?: boolean,
): number;
replaceNode(oldNode: ASTNode, newNode: ASTNode, parent?: ASTNode, copyImports = true): number {
replaceNode(oldNode: Expression, newNode: Expression, parent?: ASTNode): number;
replaceNode(oldNode: Statement, newNode: Statement, parent?: ASTNode): number;
replaceNode(oldNode: ASTNode, newNode: ASTNode, parent?: ASTNode): number {
if (oldNode === newNode) {
console.log(`WARNING: Attempted to replace node ${printNode(oldNode)} with itself`);
return oldNode.id;
Expand All @@ -355,9 +324,6 @@ export class AST {
replaceNode(oldNode, newNode, parent);
this.context.unregister(oldNode);
this.setContextRecursive(newNode);
if (copyImports) {
this.copyRegisteredImports(oldNode, newNode);
}
return newNode.id;
}

Expand Down
37 changes: 37 additions & 0 deletions src/ast/cairoNodes/cairoBlockFunctionDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
FunctionKind,
FunctionStateMutability,
FunctionVisibility,
ParameterList,
} from 'solc-typed-ast';
import { FunctionStubKind } from './cairoFunctionDefinition';
import { CairoRawStringFunctionDefinition } from './cairoRawStringFunctionDefinition';

export class CairoBlockFunctionDefinition extends CairoRawStringFunctionDefinition {
constructor(
id: number,
src: string,
scope: number,
kind: FunctionKind,
name: string,
visibility: FunctionVisibility,
stateMutability: FunctionStateMutability,
parameters: ParameterList,
returnParameters: ParameterList,
rawStringDefinition: string,
) {
super(
id,
src,
scope,
kind,
name,
visibility,
stateMutability,
parameters,
returnParameters,
FunctionStubKind.FunctionDefStub,
rawStringDefinition,
);
}
}
7 changes: 4 additions & 3 deletions src/ast/cairoNodes/cairoFunctionDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import {
import { Implicits } from '../../utils/implicits';

/*
An extension of FunctionDefinition to track which implicit arguments are used
An extension of FunctionDefinition to track which implicit arguments are used.
Additionally we often use function stubs for instances where we want to be able
to insert function during transpilation where it wouldn't make sense to include
their body in the AST. For example, stubs are used for warplib functions, and
those generated to handle memory and storage processing. Marking a CairoFunctionDefintion
as a stub tells the CairoWriter not to print it
those generated to handle memory and storage processing. Marking a CairoFunctionDefinition
as a stub tells the CairoWriter not to print it.
*/

export enum FunctionStubKind {
None,
FunctionDefStub,
StorageDefStub,
StructDefStub,
}

Expand Down
54 changes: 54 additions & 0 deletions src/ast/cairoNodes/cairoGeneratedFunctionDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
FunctionDefinition,
FunctionKind,
FunctionStateMutability,
FunctionVisibility,
ParameterList,
} from 'solc-typed-ast';
import { FunctionStubKind } from './cairoFunctionDefinition';
import { CairoRawStringFunctionDefinition } from './cairoRawStringFunctionDefinition';

export class CairoGeneratedFunctionDefinition extends CairoRawStringFunctionDefinition {
/**
* List of function defintions called by the generated function
*/
public functionsCalled: FunctionDefinition[];

constructor(
id: number,
src: string,
scope: number,
kind: FunctionKind,
name: string,
visibility: FunctionVisibility,
stateMutability: FunctionStateMutability,
parameters: ParameterList,
returnParameters: ParameterList,
functionStubKind: FunctionStubKind,
rawStringDefinition: string,
functionsCalled: FunctionDefinition[],
acceptsRawDArray = false,
acceptsUnpackedStructArray = false,
) {
super(
id,
src,
scope,
kind,
name,
visibility,
stateMutability,
parameters,
returnParameters,
functionStubKind,
rawStringDefinition,
acceptsRawDArray,
acceptsUnpackedStructArray,
);
this.functionsCalled = removeRepeatedFunction(functionsCalled);
}
}

function removeRepeatedFunction(functionsCalled: FunctionDefinition[]) {
return [...new Set(functionsCalled)];
}
45 changes: 45 additions & 0 deletions src/ast/cairoNodes/cairoImportFunctionDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
FunctionKind,
FunctionStateMutability,
FunctionVisibility,
ParameterList,
} from 'solc-typed-ast';
import { CairoFunctionDefinition, FunctionStubKind } from './cairoFunctionDefinition';
import { Implicits } from '../../utils/implicits';

export class CairoImportFunctionDefinition extends CairoFunctionDefinition {
path: string;
constructor(
id: number,
src: string,
scope: number,
name: string,
path: string,
implicits: Set<Implicits>,
parameters: ParameterList,
returnParameters: ParameterList,
stubKind: FunctionStubKind,
acceptsRawDArray = false,
acceptsUnpackedStructArray = false,
) {
super(
id,
src,
scope,
FunctionKind.Function,
name,
false,
FunctionVisibility.Internal,
FunctionStateMutability.NonPayable,
false,
parameters,
returnParameters,
[],
implicits,
stubKind,
acceptsRawDArray,
acceptsUnpackedStructArray,
);
this.path = path;
}
}
51 changes: 51 additions & 0 deletions src/ast/cairoNodes/cairoRawStringFunctionDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
FunctionKind,
FunctionStateMutability,
FunctionVisibility,
ParameterList,
} from 'solc-typed-ast';
import { CairoFunctionDefinition, FunctionStubKind } from './cairoFunctionDefinition';
import { getRawCairoFunctionInfo } from '../../utils/cairoParsing';

export class CairoRawStringFunctionDefinition extends CairoFunctionDefinition {
rawStringDefinition: string;
constructor(
id: number,
src: string,
scope: number,
kind: FunctionKind,
name: string,
visibility: FunctionVisibility,
stateMutability: FunctionStateMutability,
parameters: ParameterList,
returnParameters: ParameterList,
functionSutbKind: FunctionStubKind,
rawStringDefinition: string,
acceptsRawDArray = false,
acceptsUnpackedStructArray = false,
) {
super(
id,
src,
scope,
kind,
name,
false, // Virtual
visibility,
stateMutability,
false, // IsConstructor
parameters,
returnParameters,
[], // Modifier Invocation
functionSutbKind === FunctionStubKind.FunctionDefStub
? new Set(getRawCairoFunctionInfo(rawStringDefinition).implicits)
: functionSutbKind === FunctionStubKind.StorageDefStub
? new Set(['pedersen_ptr'])
: new Set(),
functionSutbKind,
acceptsRawDArray,
acceptsUnpackedStructArray,
);
this.rawStringDefinition = rawStringDefinition;
}
}
2 changes: 2 additions & 0 deletions src/ast/cairoNodes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './cairoAssert';
export * from './cairoContract';
export * from './cairoGeneratedFunctionDefinition';
export * from './cairoFunctionDefinition';
export * from './cairoImportFunctionDefinition';
export * from './cairoTempVarStatement';
Loading