Skip to content

BaseExpression

INCLUDE FILE
#include "Expression/BaseExpression.h"

class BaseExpression

Abstract base class for all expressions.

DEFINITION
class BaseExpression;

constructor BaseExpression

Initialize a BaseExpression with default values.

DEFINITION
BaseExpression ( );

constructor BaseExpression

Initialize a BaseExpression with a specific location.

DEFINITION
BaseExpression ( const Location & location );
PARAMETERDESCRIPTION
location

The location of the expression.

destructor ~BaseExpressionvirtual

Destroy the BaseExpression object.

DEFINITION
virtual ~BaseExpression ( );

method cloneconst virtual

Create a deep copy of the expression.

A subclass must override this method if it can occur in a macro or function definition, .repeat body, or instruction encoding.

DEFINITION
virtual Expression  clone ( const CloneContext & context ) const;
PARAMETERDESCRIPTION
context
RETURNSDESCRIPTION
Expression

A shared pointer to the cloned expression.

method evaluatevirtual

Evaluate the expression in the given context.

The default implementation calls evaluate() on all sub-expressions using traverse(), and then calls evaluate_process().

Errors during evaluation should either be reported via DiagnosticOutput and result in the expression being marked as invalid, or an exception should be thrown.

DEFINITION
virtual std::optional< Expression >  evaluate ( const EvaluationContext & context );
PARAMETERDESCRIPTION
context

The evaluation context.

RETURNSDESCRIPTION
std::optional< Expression >

The Expression to replace this expression with, {} if no replacement is needed.

method evaluate_processvirtual protected

Process the evaluation of the expression.

This method is called by the default implementation of evaluate() after calling evaluate() on all sub-expressions.

DEFINITION
virtual std::optional< Expression >  evaluate_process ( const EvaluationContext & context );
PARAMETERDESCRIPTION
context

The evaluation context.

RETURNSDESCRIPTION
std::optional< Expression >

If the expression changed, returns the new expression, otherwise {}.

method expand_callsvirtual

Expand function calls in the expression.

The default implementation calls expand_calls() on all sub-expressions using traverse() and returns {}.

If a function call cannot be expanded, either an error should be reported via DiagnosticOutput and the expression marked as invalid, or an exception should be thrown.

DEFINITION
virtual std::optional< Expression >  expand_calls ( );
RETURNSDESCRIPTION
std::optional< Expression >

The Expression to replace this Expression with, {} if no replacement is needed.

method has_valueconst virtual

Check if the value of the expression is known.

The default implementation returns true if value() returns a value, false otherwise. If the value is expensive to compute, subclasses may override this method to provide a more efficient implementation.

DEFINITION
virtual bool  has_value ( ) const;
RETURNSDESCRIPTION
bool

true if the value is known, false otherwise.

method maximum_valueconst virtual

Get the minimum possible value of the expression.

The default implementation returns the value of the expression if it is known, or {} otherwise. Subclasses should override this method if they can determine a minimum value without knowing the exact value of the expression.

DEFINITION
virtual std::optional< Value >  maximum_value ( ) const;
RETURNSDESCRIPTION
std::optional< Value >

The minimum possible value of the expression. if it can be determined, {} otherwise.

method minimum_valueconst virtual

Get the maximum possible value of the expression.

The default implementation returns the value of the expression if it is known, or {} otherwise. Subclasses should override this method if they can determine a maximum value without knowing the exact value of the expression.

DEFINITION
virtual std::optional< Value >  minimum_value ( ) const;
RETURNSDESCRIPTION
std::optional< Value >

The maximum possible value of the expression if it can be determined, {} otherwise.

method resolvevirtual

Resolve all names in the expression.

The default implementation calls resolve() on all sub-expressions using traverse().

If a name cannot be resolved, either an error should be reported via DiagnosticOutput and the expression marked as invalid, or an exception should be thrown.

DEFINITION
virtual void  resolve ( Scope * scope,
Entity * containing_entity );
PARAMETERDESCRIPTION
scope

The scope to resolve names in.

containing_entity

The entity that contains the expression.

method serializeconst virtual

Serialize the expression to a stream.

DEFINITION
virtual void  serialize ( std::ostream & stream ) const;
PARAMETERDESCRIPTION
stream

The stream to serialize to.

method serialize_subconst virtual protected

Serialize the expression to a stream.

This method must be implemented by derived classes.

DEFINITION
virtual void  serialize_sub ( std::ostream & stream ) const = 0;
PARAMETERDESCRIPTION
stream

The stream to serialize to.

method traversevirtual protected

Call a method on all sub-expressions of this expression.

It is used by the default implementations of resolve() and expand_calls(). Unless a subclass has no sub-expressions or overrides both of these methods, it should override traverse().

DEFINITION
virtual void  traverse ( std::function< void(Expression &)>  callable );
PARAMETERDESCRIPTION
callable

A callable to apply to each sub-expression.

method typeconst virtual

Get the type of the expression's value.

The default implementation returns the type of the value of the expression if it is known, or {} otherwise. Subclasses should override this method if they can determine the type of the expression without knowing the exact value of the expression.

DEFINITION
virtual std::optional< Value::Type >  type ( ) const;
RETURNSDESCRIPTION
std::optional< Value::Type >

The type of the expression's value if it can be determined, {} otherwise.

method valueconst virtual

Get the value of the expression if it is known.

Subclasses should override this method. The default implementation returns {}.

DEFINITION
virtual std::optional< Value >  value ( ) const;
RETURNSDESCRIPTION
std::optional< Value >

The value of the expression, if it is known.

member location

The location of the expression in the source code.

DEFINITION
Location  location;

member resolved

Indicates whether the expression has been resolved.

DEFINITION
bool  resolved;

member valid

Indicates whether the expression is valid.

If an error occurs during evaluation, this flag should be set to false. This allows the expression to be used in further evaluations without causing additional errors.

DEFINITION
bool  valid;