Skip to content

Commit

Permalink
Prettier pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeshecdom committed Jan 29, 2025
1 parent db5c7e3 commit 9edf51f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 73 deletions.
147 changes: 78 additions & 69 deletions src/optimizer/deprecated/constant-propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export function constantPropagationAnalysis(
// Check that the builtin Functions known by the analyzer are still the ones in StructFunctions and MapFunctions
const knownStructBuiltInFunctions = [
...knownStructBuiltInNonMutationFunctions,
...knownStructBuiltInMutationFunctions
...knownStructBuiltInMutationFunctions,
];

if (
StructFunctions.size !== knownStructBuiltInFunctions.length ||
knownStructBuiltInFunctions.some((name) => !StructFunctions.has(name))
Expand All @@ -81,7 +81,7 @@ export function constantPropagationAnalysis(

const knownMapBuiltInFunctions = [
...knownMapBuiltInNonMutationFunctions,
...knownMapBuiltInMutationFunctions
...knownMapBuiltInMutationFunctions,
];

if (
Expand Down Expand Up @@ -353,16 +353,18 @@ export function constantPropagationAnalysis(
});

if (ast.falseStatements !== null && ast.elseif !== null) {
throwInternalCompilerError("Incorrect AST: 'else' and `else if' cannot occur simultaneously in an AstStatementCondition")
throwInternalCompilerError(
"Incorrect AST: 'else' and `else if' cannot occur simultaneously in an AstStatementCondition",
);
}

if (ast.falseStatements !== null) {
const falseStmts = ast.falseStatements;
simulateBranch(() => {
executeStatements(falseStmts);
});
}
}

if (ast.elseif !== null) {
const elseif = ast.elseif;
simulateBranch(() => {
Expand Down Expand Up @@ -551,7 +553,9 @@ export function constantPropagationAnalysis(
}
}

function interpretExpression(expr: A.AstExpression): A.AstLiteral | undefined {
function interpretExpression(
expr: A.AstExpression,
): A.AstLiteral | undefined {
switch (expr.kind) {
case "address":
case "boolean":
Expand Down Expand Up @@ -642,81 +646,86 @@ export function constantPropagationAnalysis(
}
}

function interpretMethodCall(expr: A.AstMethodCall): A.AstLiteral | undefined {
function interpretMethodCall(
expr: A.AstMethodCall,
): A.AstLiteral | undefined {
const self = tryExpressionEvaluation(expr.self);
const argValues = expr.args.map((e) => tryExpressionEvaluation(e));

const result = typeof self !== "undefined" &&
argValues.every((v) => typeof v !== "undefined") ?
catchNonFatalErrors(() =>
interpreter.interpretMethodCall(
util.makeMethodCall(self, expr.method, argValues, expr.loc),
),
) :
undefined;
const result =
typeof self !== "undefined" &&
argValues.every((v) => typeof v !== "undefined")
? catchNonFatalErrors(() =>
interpreter.interpretMethodCall(
util.makeMethodCall(
self,
expr.method,
argValues,
expr.loc,
),
),
)
: undefined;

// If the method is a mutates function, then the expression acts as an implicit assignment statement
// into expr.self.
// We need to delete the binding for the path expression in expr.self, because
// currently, method calls in the interpreter do not modify self and they do not return the new value for self.

const fullPath = tryExtractPath(expr.self);
if (fullPath === null) {
// ast.self is not a path expression, i.e., it has the form: a.f()...
// then there is nothing to update in the environment stack because a.f()... is not a full path to a variable.

// If the method is a mutates function, then the expression acts as an implicit assignment statement
// into expr.self.
// We need to delete the binding for the path expression in expr.self, because
// currently, method calls in the interpreter do not modify self and they do not return the new value for self.
return result;
}

const fullPath = tryExtractPath(expr.self);
if (fullPath === null) {
// ast.self is not a path expression, i.e., it has the form: a.f()...
// then there is nothing to update in the environment stack because a.f()... is not a full path to a variable.
const baseName = fullPath[0];
if (typeof baseName === "undefined") {
throwInternalCompilerError(
"path expressions must be non-empty",
expr.self.loc,
);
}

return result;
// Check that the method is a mutates function
const selfTypeRef = getExpType(ctx, expr.self);

if (selfTypeRef.kind === "ref") {
const selfT = getType(ctx, selfTypeRef.name);
const f = selfT.functions.get(idText(expr.method));
if (typeof f !== "undefined") {
if (
f.isMutating ||
knownStructBuiltInMutationFunctions.includes(
idText(expr.method),
)
) {
envStack.deactivateBinding(idText(baseName));
}
// Not a mutates function, do nothing
}
// Not a registered function in the reference type. Do nothing
}


const baseName = fullPath[0];
if (typeof baseName === "undefined") {
throwInternalCompilerError(
"path expressions must be non-empty",
expr.self.loc,
);
if (selfTypeRef.kind === "map") {
if (
knownMapBuiltInMutationFunctions.includes(idText(expr.method))
) {
envStack.deactivateBinding(idText(baseName));
}

// Check that the method is a mutates function
const selfTypeRef = getExpType(ctx, expr.self);

if (selfTypeRef.kind === "ref") {
const selfT = getType(ctx, selfTypeRef.name);
const f = selfT.functions.get(idText(expr.method));
if (typeof f !== "undefined") {
if (
f.isMutating ||
knownStructBuiltInMutationFunctions.includes(
idText(expr.method),
)
) {
envStack.deactivateBinding(idText(baseName));
}
// Not a mutates function, do nothing
}
// Not a registered function in the reference type. Do nothing
}

if (selfTypeRef.kind === "map") {
if (
knownMapBuiltInMutationFunctions.includes(
idText(expr.method),
)
) {
envStack.deactivateBinding(idText(baseName));
}
// Not a mutates function, do nothing
}
// Not a mutates function, do nothing
}

// Not a reference or map type, so, it cannot have mutates functions
// Do nothing

// Not a reference or map type, so, it cannot have mutates functions
// Do nothing


return result;
return result;
}

function interpretStaticCall(expr: A.AstStaticCall): A.AstLiteral | undefined {
function interpretStaticCall(
expr: A.AstStaticCall,
): A.AstLiteral | undefined {
const argValues = expr.args.map((e) => tryExpressionEvaluation(e));
if (argValues.every((v) => typeof v !== "undefined")) {
// Pass the call to the interpreter
Expand Down
5 changes: 4 additions & 1 deletion src/optimizer/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,10 @@ export class EnvironmentStack {
newMap.set(name, this.copyValue(val));
}

const newParent = typeof env.parent !== "undefined" ? this.copyEnvironment(env.parent) : undefined;
const newParent =
typeof env.parent !== "undefined"
? this.copyEnvironment(env.parent)
: undefined;

return { values: newMap, parent: newParent };
}
Expand Down
4 changes: 1 addition & 3 deletions src/types/resolveDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2245,9 +2245,7 @@ function initializeConstantsAndDefaultContractAndStructFields(

field.default =
field.type.kind === "ref" && field.type.optional
? util.makeNullLiteral(
field.ast.loc,
)
? util.makeNullLiteral(field.ast.loc)
: undefined;
}
}
Expand Down

0 comments on commit 9edf51f

Please sign in to comment.