Skip to content

Commit

Permalink
refactor: Remove dead code and hide unnecessarily public properties
Browse files Browse the repository at this point in the history
This makes it easier to analyze which logic is actually used and makes
the porting easier.
  • Loading branch information
Xanewok committed Jul 22, 2024
1 parent 6569b50 commit 5739893
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ function processContractAstNode(
contractNode.linearizedBaseContracts
);

file.addContract(contract);

for (const node of contractNode.nodes) {
if (node.nodeType === "FunctionDefinition") {
const functionAbis = contractAbi?.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,27 @@ export enum ContractFunctionVisibility {
}

export class SourceFile {
public readonly contracts: Contract[] = [];
public readonly functions: ContractFunction[] = [];
private readonly _functions: ContractFunction[] = [];

constructor(
public readonly sourceName: string,
public readonly content: string
) {}

public addContract(contract: Contract) {
if (contract.location.file !== this) {
throw new Error("Trying to add a contract from another file");
}

this.contracts.push(contract);
}

public addFunction(func: ContractFunction) {
if (func.location.file !== this) {
throw new Error("Trying to add a function from another file");
}

this.functions.push(func);
this._functions.push(func);
}

public getContainingFunction(
location: SourceLocation
): ContractFunction | undefined {
// TODO: Optimize this with a binary search or an internal tree

for (const func of this.functions) {
for (const func of this._functions) {
if (func.location.contains(location)) {
return func;
}
Expand Down Expand Up @@ -124,12 +115,12 @@ export class SourceLocation {
}

export class Contract {
public readonly localFunctions: ContractFunction[] = [];
public readonly customErrors: CustomError[] = [];

private _constructor: ContractFunction | undefined;
private _fallback: ContractFunction | undefined;
private _receive: ContractFunction | undefined;
private readonly _localFunctions: ContractFunction[] = [];
private readonly _selectorHexToFunction: Map<string, ContractFunction> =
new Map();

Expand Down Expand Up @@ -174,7 +165,7 @@ export class Contract {
}
}

this.localFunctions.push(func);
this._localFunctions.push(func);
}

public addCustomError(customError: CustomError) {
Expand All @@ -189,7 +180,7 @@ export class Contract {
this._receive = baseContract._receive;
}

for (const baseContractFunction of baseContract.localFunctions) {
for (const baseContractFunction of baseContract._localFunctions) {
if (
baseContractFunction.type !== ContractFunctionType.GETTER &&
baseContractFunction.type !== ContractFunctionType.FUNCTION
Expand Down Expand Up @@ -305,52 +296,10 @@ export class Instruction {
public readonly pc: number,
public readonly opcode: Opcode,
public readonly jumpType: JumpType,
// Used only for debugging
public readonly pushData?: Buffer,
public readonly location?: SourceLocation
) {}

/**
* Checks equality with another Instruction.
*/
public equals(other: Instruction): boolean {
if (this.pc !== other.pc) {
return false;
}

if (this.opcode !== other.opcode) {
return false;
}

if (this.jumpType !== other.jumpType) {
return false;
}

if (this.pushData !== undefined) {
if (other.pushData === undefined) {
return false;
}

if (!this.pushData.equals(other.pushData)) {
return false;
}
} else if (other.pushData !== undefined) {
return false;
}

if (this.location !== undefined) {
if (other.location === undefined) {
return false;
}

if (!this.location.equals(other.location)) {
return false;
}
} else if (other.location !== undefined) {
return false;
}

return true;
}
}

interface ImmutableReference {
Expand All @@ -365,7 +314,7 @@ export class Bytecode {
public readonly contract: Contract,
public readonly isDeployment: boolean,
public readonly normalizedCode: Buffer,
public readonly instructions: Instruction[],
instructions: Instruction[],
public readonly libraryAddressPositions: number[],
public readonly immutableReferences: ImmutableReference[],
public readonly compilerVersion: string
Expand All @@ -388,22 +337,4 @@ export class Bytecode {
public hasInstruction(pc: number): boolean {
return this._pcToInstruction.has(pc);
}

/**
* Checks equality with another Bytecode.
*/
public equals(other: Bytecode): boolean {
if (this._pcToInstruction.size !== other._pcToInstruction.size) {
return false;
}

for (const [key, val] of this._pcToInstruction) {
const otherVal = other._pcToInstruction.get(key);
if (otherVal === undefined || !val.equals(otherVal)) {
return false;
}
}

return true;
}
}

0 comments on commit 5739893

Please sign in to comment.