sun::semantic_analysis::constants::ConstantEvaluator

sun::semantic_analysis::constants::ConstantEvaluator

class · Source (opens in a new tab)

class sun::semantic_analysis::constants::ConstantEvaluator

Evaluates the initializers of a program's file-scope variables.

Public Functions

ConstantEvaluator

public · function · Source (opens in a new tab)

sun::semantic_analysis::constants::ConstantEvaluator::ConstantEvaluator(const DeclarationTable &declarations)

Decisions are stored on the declaring nodes; declarations is used to reach the node of a variable that another initializer reads, and must outlive the evaluator.

Related: DeclarationTable

evaluateExpression

public · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateExpression(const sun::ast::ExprAST &expr)

The value of an analyzed expression, or nothing when it cannot be computed; the obstacle is then recorded with recordBlocker.

Related: ConstantValue, sun::ast::ExprAST

evaluateGlobalInitializer

public · function · Source (opens in a new tab)

const GlobalInitRecord & sun::semantic_analysis::constants::ConstantEvaluator::evaluateGlobalInitializer(const sun::ast::VariableCreationAST &global)

Decides one file-scope variable and stores the decision on its node.

Returns the stored decision.

Related: GlobalInitRecord, sun::ast::VariableCreationAST

evaluateGlobals

public · function · Source (opens in a new tab)

void sun::semantic_analysis::constants::ConstantEvaluator::evaluateGlobals(const sun::ast::BlockExprAST &block)

Decides every file-scope variable in the block, in source order, looking inside modules and bundle scopes.

A variable that already has a decision is left alone. Then checks the order of the variables left to startup (see checkStartupOrder).

Related: sun::ast::BlockExprAST

Private Functions

applyBinaryOperator

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::applyBinaryOperator(sun::parsing::TokenKind op, const ConstantValue &left, const ConstantValue &right, bool unsignedOperation, const sun::types::TypePtr &resultType, const sun::support::Position &position)

Applies an arithmetic, bitwise, shift or comparison operator to two operands of one width, the way generated code does after widening them.

unsignedOperation is the signedness of the left operand's type.

Related: ConstantValue, sun::parsing::TokenKind, sun::types::TypePtr, sun::support::Position

assignLocal

private · function · Source (opens in a new tab)

bool sun::semantic_analysis::constants::ConstantEvaluator::assignLocal(DeclarationId target, const std::string &name, ConstantValue value, const sun::support::Position &position)

Stores a value in a local variable of the innermost frame, converted to the variable's type.

Refuses a name that is not such a variable, which is how a write to a global is kept out.

Related: DeclarationId, ConstantValue, sun::support::Position

checkStartupOrder

private · function · Source (opens in a new tab)

void sun::semantic_analysis::constants::ConstantEvaluator::checkStartupOrder(const GlobalOrder &order)

Startup initializers run in the given order, so one that directly reads a variable initialized later would see it still zeroed.

Reports that as an error. A read hidden inside a called function is not detected.

Related: GlobalOrder

combineOperands

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::combineOperands(sun::parsing::TokenKind op, ConstantValue left, ConstantValue right, const sun::types::TypePtr &leftType, const sun::types::TypePtr &resultType, const sun::support::Position &position)

Brings two operands to one width the way generated code does, then applies the operator.

leftType decides the signedness of the operation.

Related: ConstantValue, sun::parsing::TokenKind, sun::types::TypePtr, sun::support::Position

convertToDeclaredType

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::convertToDeclaredType(ConstantValue value, const sun::types::TypePtr &declaredType, const sun::support::Position &position)

Brings a value to the type it is stored as, the way generated code converts an initializer: a narrower integer widens by its own signedness and a wider one keeps its low bits, and a float is rounded to the other float width.

Any other difference in type is refused.

Related: ConstantValue, sun::types::TypePtr, sun::support::Position

countStep

private · function · Source (opens in a new tab)

bool sun::semantic_analysis::constants::ConstantEvaluator::countStep(const sun::support::Position &position)

Counts one unit of work and reports whether evaluation may continue.

Past the limit the initializer is left to startup.

Related: sun::support::Position

evaluateArrayLiteral

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateArrayLiteral(const sun::ast::ArrayLiteralAST &literal)

Evaluates an array literal, element by element.

Related: ConstantValue, sun::ast::ArrayLiteralAST

evaluateBinary

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateBinary(const sun::ast::BinaryExprAST &binary)

Evaluates an arithmetic, comparison, bitwise or logical operator.

Related: ConstantValue, sun::ast::BinaryExprAST

evaluateCall

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateCall(const sun::ast::CallExprAST &call)

Evaluates a call to a plain function by running its analyzed body.

The function must be pure as far as the evaluator can see: it has a body in this program, takes and returns values, cannot throw, and touches nothing but its own variables and compile-time constants. Anything else is refused, and the global is initialized at startup instead.

Related: ConstantValue, sun::ast::CallExprAST

evaluateCondition

private · function · Source (opens in a new tab)

std::optional< bool > sun::semantic_analysis::constants::ConstantEvaluator::evaluateCondition(const sun::ast::ExprAST &condition)

Evaluates a condition to true or false, or nothing when it cannot.

Related: sun::ast::ExprAST

evaluateConvert

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateConvert(const sun::ast::GenericCallAST &call)

Evaluates the numeric conversion _convert<T>(value).

Related: ConstantValue, sun::ast::GenericCallAST

evaluateIndex

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateIndex(const sun::ast::ExprAST &array, const std::vector< const sun::ast::ExprAST * > &indices, const sun::support::Position &position)

Reads one element of an array value: a[i] or a[i, j].

Related: ConstantValue, sun::ast::ExprAST, sun::support::Position

evaluateLiteral

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateLiteral(const sun::ast::ExprAST &expr)

Evaluates a number, bool, char or string literal.

Related: ConstantValue, sun::ast::ExprAST

evaluateLogical

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateLogical(const sun::ast::BinaryExprAST &binary)

Evaluates and and or, which skip the right side when decided.

Related: ConstantValue, sun::ast::BinaryExprAST

evaluateMemberAccess

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateMemberAccess(const sun::ast::MemberAccessAST &access)

Evaluates m.X on a module and Color.Red on a payload-free enum.

Related: ConstantValue, sun::ast::MemberAccessAST

evaluateTernary

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateTernary(const sun::ast::TernaryExprAST &ternary)

Evaluates cond ?

a : b, computing only the chosen side.

Related: ConstantValue, sun::ast::TernaryExprAST

evaluateUnary

private · function · Source (opens in a new tab)

std::optional< ConstantValue > sun::semantic_analysis::constants::ConstantEvaluator::evaluateUnary(const sun::ast::UnaryExprAST &unary)

Evaluates negation, not and bitwise complement.

Related: ConstantValue, sun::ast::UnaryExprAST

findEvaluableCallee

private · function · Source (opens in a new tab)

const sun::ast::FunctionAST * sun::semantic_analysis::constants::ConstantEvaluator::findEvaluableCallee(const sun::ast::CallExprAST &call)

The function a call runs, when it is one the evaluator can run; otherwise records why not and returns null.

Related: sun::ast::FunctionAST, sun::ast::CallExprAST

findGlobalValue

private · function · Source (opens in a new tab)

const ConstantValue * sun::semantic_analysis::constants::ConstantEvaluator::findGlobalValue(DeclarationId target, const std::string &name, const sun::support::Position &position)

The compile-time value of the const a name refers to, or null with the obstacle recorded.

The value lives on the declaring node, so reading it copies nothing.

Related: ConstantValue, DeclarationId, sun::support::Position

findStoredValue

private · function · Source (opens in a new tab)

const ConstantValue * sun::semantic_analysis::constants::ConstantEvaluator::findStoredValue(const sun::ast::ExprAST &expr)

The value that a read of a variable refers to, without copying it, or null with the obstacle recorded.

expr must be such a read (see readsStoredValue). A local's value may move when a call is evaluated, so the result must be used before the next call.

Related: ConstantValue, sun::ast::ExprAST

recordBlocker

private · function · Source (opens in a new tab)

void sun::semantic_analysis::constants::ConstantEvaluator::recordBlocker(std::string message, const sun::support::Position &position)

Notes why the current initializer cannot be evaluated.

Only the first obstacle is kept, since that is the one a diagnostic should name.

Related: sun::support::Position

runBlock

private · function · Source (opens in a new tab)

ConstantEvaluator::Flow sun::semantic_analysis::constants::ConstantEvaluator::runBlock(const sun::ast::BlockExprAST &block)

Runs the statements of a block in order until one changes the flow.

Related: Flow, sun::ast::BlockExprAST

runLoop

private · function · Source (opens in a new tab)

ConstantEvaluator::Flow sun::semantic_analysis::constants::ConstantEvaluator::runLoop(const sun::ast::ExprAST *condition, const sun::ast::ExprAST *increment, const sun::ast::ExprAST &body, const sun::support::Position &position)

Runs a while loop or a for (init; condition; step) loop.

Related: Flow, sun::ast::ExprAST, sun::support::Position

runStatement

private · function · Source (opens in a new tab)

ConstantEvaluator::Flow sun::semantic_analysis::constants::ConstantEvaluator::runStatement(const sun::ast::ExprAST &statement)

Runs one statement of a function body in the innermost frame.

Related: Flow, sun::ast::ExprAST

Private Fields

blocker_

private · variable · Source (opens in a new tab)

std::optional<StartupReason> sun::semantic_analysis::constants::ConstantEvaluator::blocker_

No documentation comment.

Related: StartupReason

deciding_

private · variable · Source (opens in a new tab)

std::vector<const sun::ast::VariableCreationAST*> sun::semantic_analysis::constants::ConstantEvaluator::deciding_

No documentation comment.

Related: sun::ast::VariableCreationAST

declarations_

private · variable · Source (opens in a new tab)

const DeclarationTable& sun::semantic_analysis::constants::ConstantEvaluator::declarations_

No documentation comment.

Related: DeclarationTable

frames_

private · variable · Source (opens in a new tab)

std::vector<Frame> sun::semantic_analysis::constants::ConstantEvaluator::frames_

No documentation comment.

Related: Frame

returned_

private · variable · Source (opens in a new tab)

std::optional<ConstantValue> sun::semantic_analysis::constants::ConstantEvaluator::returned_

No documentation comment.

Related: ConstantValue

steps_

private · variable · Source (opens in a new tab)

size_t sun::semantic_analysis::constants::ConstantEvaluator::steps_ = 0

No documentation comment.

Private Enums

Flow

private · enum · Source (opens in a new tab)

enum class sun::semantic_analysis::constants::ConstantEvaluator::Flow

What running a statement asks the code around it to do next.

Next

Next

No documentation comment.

Return

Return

No documentation comment.

Break

Break

No documentation comment.

Continue

Continue

No documentation comment.

Failed

Failed

No documentation comment.

Private Type Aliases

Frame

private · typedef · Source (opens in a new tab)

using sun::semantic_analysis::constants::ConstantEvaluator::Frame =  std::unordered_map<DeclarationId, ConstantValue>

The parameters and local variables of one function being evaluated.

Related: DeclarationId, ConstantValue

GlobalOrder

private · typedef · Source (opens in a new tab)

using sun::semantic_analysis::constants::ConstantEvaluator::GlobalOrder =  std::vector<const sun::ast::VariableCreationAST*>

A program's file-scope variables, in the order startup runs them.

Related: sun::ast::VariableCreationAST