Skip to content

Commit

Permalink
fixe IndexAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
kKahina committed Aug 30, 2024
1 parent 54878a0 commit 2c5a92c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 72 deletions.
67 changes: 12 additions & 55 deletions src/transformers/visitors/toOrchestrationVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ if (name.endsWith('_')) {
expression: InnerNode,
interactsWithSecret: true,
});

if (location.trueIndex !== -1){ fnDefNode.node._newASTPointer.body.preStatements[ifPreIndex].trueBody.push(newNode1); }
else if (location.falseIndex !== -1){ fnDefNode.node._newASTPointer.body.preStatements[ifPreIndex].falseBody.push(newNode1); }
else {fnDefNode.node._newASTPointer.body.preStatements.push(newNode1);}
Expand Down Expand Up @@ -343,6 +343,7 @@ if (name.endsWith('_')) {
}
}
}



// After the non-secret variables have been modified we need to reset the original variable name to its initial value.
Expand Down Expand Up @@ -1393,6 +1394,16 @@ const visitor = {
leftHandSide,
rightHandSide: binOpNode,
});
if (path.node.leftHandSide.nodeType === 'IndexAccess') {
binOpNode.leftExpression.name = path.node.leftHandSide.baseExpression.name+'_'+ path.node.leftHandSide.indexExpression.name;

} else {
binOpNode.leftExpression.name = path.node.leftHandSide.name;
}




return assNode;
};

Expand All @@ -1419,60 +1430,6 @@ const visitor = {
// node._newASTPointer = newNode; // no need to ascribe the node._newASTPointer, because we're exiting.
},
},
// Assignment: {
// enter(path: NodePath) {
// const { node, parent } = path;
// const newNode = buildNode(node.nodeType, { operator: node.operator });
// node._newASTPointer = newNode;

// if (parent._newASTPointer.nodeType === 'VariableDeclarationStatement') {
// parent._newASTPointer.initialValue = newNode;
// } else {
// parent._newASTPointer.expression = newNode;
// }
// },

// exit(path: NodePath) {
// // Convert 'a += b' into 'a = a + b' for all operators
// const expandAssignment = (node: any) => {
// const { operator, leftHandSide, rightHandSide } = node;
// const expandableOps = ['+=', '-=', '*=', '/=', '%=', '|=', '&=', '^='];
// if (!expandableOps.includes(operator)) return node;
// const op = operator.charAt(0);
// const binOpNode = buildNode('BinaryOperation', {
// operator: op,
// leftExpression: cloneDeep(leftHandSide),
// rightExpression: rightHandSide,
// });
// const assNode = buildNode('Assignment', {
// operator: '=',
// leftHandSide,
// rightHandSide: binOpNode,
// });
// return assNode;
// };

// const { parent } = path;
// const binding = path.getReferencedBinding(path.node.leftHandSide);
// if (binding instanceof VariableBinding && !binding.isSecret && binding.stateVariable) {
// if (parent._newASTPointer.nodeType === 'VariableDeclarationStatement') {
// const circuitNode = parent._newASTPointer.initialValue;
// const newNode = expandAssignment(circuitNode);
// parent._newASTPointer.initialValue = newNode;
// } else {
// const circuitNode = parent._newASTPointer.expression;
// const newNode = expandAssignment(circuitNode);
// parent._newASTPointer.expression = newNode;
// }
// }
// if (path.getAncestorContainedWithin('initializationExpression') && parent._newASTPointer.nodeType === 'VariableDeclarationStatement') {
// parent._newASTPointer.initialValue.isInitializationAssignment = true;
// } else if (path.getAncestorContainedWithin('initializationExpression')) {
// parent._newASTPointer.expression.isInitializationAssignment = true;
// }
// },
// },


TupleExpression: {
enter(path: NodePath, state: any) {
Expand Down
11 changes: 0 additions & 11 deletions src/traverse/NodePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -984,21 +984,10 @@ export default class NodePath {
isConstantArray(node: any = this.node): boolean {
if (!this.isArray(node)) return false;
let arrLen;
console.log('vvvvvvvvvvvvvvvvvNodenode', node);
switch (node.nodeType) {
case 'IndexAccess':
arrLen = node.baseExpression.typeDescriptions.typeString.match(/(?<=\[)(\d+)(?=\])/);
break;

// case 'VariableDeclaration':

// if (node.typeDescriptions && node.typeDescriptions.typeString) {
// arrLen = node.typeDescriptions.typeString.match(/(?<=\[)(\d+)(?=\])/);
// } else if (node.typeName && typeof node.typeName.name === 'string' && !isNaN(node.typeName.name)) {
// return false;
// }
// break;

case 'Identifier':
default:
arrLen = node.typeDescriptions.typeString.match(/(?<=\[)(\d+)(?=\])/);
Expand Down
6 changes: 1 addition & 5 deletions src/traverse/Scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,10 @@ export class Scope {
if (!referencingNode)
return null;

//if (referencingNode.nodeType === 'VariableDeclaration') return null ;


const indicator = this.getIndicatorById(
path.getReferencedDeclarationId(referencingNode) || referencingNode?.id
);



if (!path.isMapping(referencingNode) && !path.isArray(referencingNode) && !path.isStruct(referencingNode)) return indicator;

if (path.isStruct(referencingNode) && NodePath.getPath(referencingNode).getAncestorOfType('MemberAccess') && path.isMapping(referencingNode)) {
Expand Down
2 changes: 1 addition & 1 deletion test/contracts/Arrays-input.zol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract Assign {

function add(secret uint256[5] calldata value, uint256[5] calldata publicValue) public {
b = publicValue;
for (uint256 index = 0; index < 2; index++) {
for (uint256 index = 0; index < 5; index++) {
known a += value[index];
}
}
Expand Down

0 comments on commit 2c5a92c

Please sign in to comment.