Loops represent for, while and do-while loop constructs. These all consist of a condition - evaluated to determine if loop iteration should continue, and a body which is the logic to be repeated. For loops also have initial statements which are evaluated prior to loop execution (at loop scope) and commonly used to set up iterators, and iteration expressions which are evaluated between iterations after the body and before the condition. Both conditions and initial statements can be declarations or expressions, so are Statements, and iteration expressions can consist of multiple expressions. The loop body is a Block defining its own scope (encapsulated by initial statement scope for for-loops).
More...
#include <AST.h>
Inherits Statement.
|
using | UniquePtr = std::unique_ptr< Loop > |
|
enum | NodeType {
TreeNode,
StatementListNode,
BlockNode,
ConditionalStatementNode,
CommaOperatorNode,
LoopNode,
KeywordNode,
AssignExpressionNode,
CrementNode,
UnaryOperatorNode,
BinaryOperatorNode,
TernaryOperatorNode,
CastNode,
AttributeNode,
FunctionCallNode,
ExternalVariableNode,
DeclareLocalNode,
ArrayPackNode,
ArrayUnpackNode,
LocalNode,
ValueBoolNode,
ValueInt16Node,
ValueInt32Node,
ValueInt64Node,
ValueFloatNode,
ValueDoubleNode,
ValueStrNode
} |
| An enumerated list of node types for all concrete node types. These can be used for faster evaluation of a given concrete node using the virtual function table via Node::nodetype() rather than performing a dynamic_cast/calling Node::isType. More...
|
|
using | Ptr = std::shared_ptr< Node > |
|
|
| Loop (const tokens::LoopToken loopType, Statement *condition, Block *body, Statement *init=nullptr, Expression *iter=nullptr) |
| Construct a new Loop with the type defined by a tokens::LoopToken, a condition Statement, a Block representing the body and for for-loops an optional initial Statement and iteration Expression. Ownership of all arguments is transferred to the Loop. All arguments have their parent data updated. More...
|
|
| Loop (const Loop &other) |
| Deep copy constructor for an Loop, performing a deep copy on the condition, body and initial Statement/iteration Expression if they exist, ensuring parent information is updated. More...
|
|
| ~Loop () override=default |
|
Loop * | copy () const override final |
| The deep copy method for a Node. More...
|
|
NodeType | nodetype () const override |
| Virtual method for accessing node type information. More...
|
|
const char * | nodename () const override |
| Virtual method for accessing node name information. More...
|
|
const char * | subname () const override |
| Virtual method for accessing node name information. More...
|
|
const Statement * | basetype () const override |
| Virtual method for accessing a node's base class. Note that if this is called explicitly on an instance of ast::Node (the top most base class) a nullptr is returned. This is primarily used by the Visitor to support hierarchical visits. More...
|
|
size_t | children () const override final |
| Virtual method for accessing child information. Returns the number of children a given AST node owns. More...
|
|
const Statement * | child (const size_t i) const override final |
| Virtual method for accessing child information. Returns a const pointer to a child node at the given index. If the index is out of range, a nullptr is returned. More...
|
|
bool | replacechild (const size_t i, Node *node) override final |
| Virtual method that attempted to replace a child at a given index with a provided node type. More...
|
|
tokens::LoopToken | loopType () const |
| Query the type of loop held on this node. More...
|
|
bool | hasInit () const |
| Query if this Loop has a valid initial statement. More...
|
|
bool | hasIter () const |
| Query if this Loop has a valid iteration expression list. More...
|
|
const Statement * | condition () const |
| Access a const pointer to the Loop condition as an abstract statement. More...
|
|
const Block * | body () const |
| Access a const pointer to the Loop body as a Block. More...
|
|
const Statement * | initial () const |
| Access a const pointer to the Loop initial statement as an abstract statement. More...
|
|
const Expression * | iteration () const |
| Access a const pointer to the Loop iteration Expression. More...
|
|
Loops represent for, while and do-while loop constructs. These all consist of a condition - evaluated to determine if loop iteration should continue, and a body which is the logic to be repeated. For loops also have initial statements which are evaluated prior to loop execution (at loop scope) and commonly used to set up iterators, and iteration expressions which are evaluated between iterations after the body and before the condition. Both conditions and initial statements can be declarations or expressions, so are Statements, and iteration expressions can consist of multiple expressions. The loop body is a Block defining its own scope (encapsulated by initial statement scope for for-loops).
- Note
- Only for-loops should have initial statements and/or iteration expressions. Also for-loops allow empty conditions to be given by the user, this is replaced with a 'true' expression in the parser.
◆ Ptr
◆ UniquePtr
◆ NodeType
An enumerated list of node types for all concrete node types. These can be used for faster evaluation of a given concrete node using the virtual function table via Node::nodetype() rather than performing a dynamic_cast/calling Node::isType.
- Note
- This is sometimes referred to as "manual RTTI". We use this technique combine with single dispatch due to opting for CRTP on the main visitor and no templated virtual method support in C++. i.e. no way to double dispatch: visit<template T>(Visitor<T>*)
-
Abstract (pure-virtual) nodes are not listed here. Node::isType should be used to determine if a node is of a given abstract type.
Enumerator |
---|
TreeNode | |
StatementListNode | |
BlockNode | |
ConditionalStatementNode | |
CommaOperatorNode | |
LoopNode | |
KeywordNode | |
AssignExpressionNode | |
CrementNode | |
UnaryOperatorNode | |
BinaryOperatorNode | |
TernaryOperatorNode | |
CastNode | |
AttributeNode | |
FunctionCallNode | |
ExternalVariableNode | |
DeclareLocalNode | |
ArrayPackNode | |
ArrayUnpackNode | |
LocalNode | |
ValueBoolNode | |
ValueInt16Node | |
ValueInt32Node | |
ValueInt64Node | |
ValueFloatNode | |
ValueDoubleNode | |
ValueStrNode | |
◆ Loop() [1/2]
Construct a new Loop with the type defined by a tokens::LoopToken, a condition Statement, a Block representing the body and for for-loops an optional initial Statement and iteration Expression. Ownership of all arguments is transferred to the Loop. All arguments have their parent data updated.
- Parameters
-
loopType | The type of loop - for, while or do-while. |
condition | The condition Statement to determine loop repetition |
body | The Block to be repeated |
init | The (optional) for-loop initial Statement. |
iter | The (optional) for-loop iteration Expression. |
◆ Loop() [2/2]
Deep copy constructor for an Loop, performing a deep copy on the condition, body and initial Statement/iteration Expression if they exist, ensuring parent information is updated.
- Parameters
-
other | A const reference to another Loop to deep copy |
◆ ~Loop()
◆ basetype()
Virtual method for accessing a node's base class. Note that if this is called explicitly on an instance of ast::Node (the top most base class) a nullptr is returned. This is primarily used by the Visitor to support hierarchical visits.
Reimplemented from Statement.
◆ body()
const Block* body |
( |
| ) |
const |
|
inline |
Access a const pointer to the Loop body as a Block.
- Returns
- A const pointer to the body Block
◆ child()
const Statement* child |
( |
const size_t |
i | ) |
const |
|
inlinefinaloverridevirtual |
Virtual method for accessing child information. Returns a const pointer to a child node at the given index. If the index is out of range, a nullptr is returned.
Implements Node.
◆ childidx()
int64_t childidx |
( |
| ) |
const |
|
inlineinherited |
Returns the child index of this node in relation to its parent, or -1 if no valid index is found (usually representing the top most node (i.e. Tree)
- Returns
- The child index of this node
◆ children()
size_t children |
( |
| ) |
const |
|
inlinefinaloverridevirtual |
Virtual method for accessing child information. Returns the number of children a given AST node owns.
Implements Node.
◆ condition()
Access a const pointer to the Loop condition as an abstract statement.
- Returns
- A const pointer to the condition as a statement
◆ copy()
|
inlinefinaloverridevirtual |
◆ hasInit()
Query if this Loop has a valid initial statement.
- Returns
- True if a valid initial statement exists, false otherwise
◆ hasIter()
Query if this Loop has a valid iteration expression list.
- Returns
- True if a valid iteration list exists, false otherwise
◆ initial()
Access a const pointer to the Loop initial statement as an abstract statement.
- Returns
- A const pointer to the initial statement as a statement
◆ isType()
Query whether or not this node is of a specific (derived) type. This method should be used to check if a node is of a particular abstract type. When checking concrete types, it's generally more efficient to check the return value of Node::nodetype()
- Template Parameters
-
NodeT | The node type to query against. |
- Returns
- True if this node is of the given type, false otherwise.
◆ iteration()
◆ loopType()
Query the type of loop held on this node.
- Returns
- The loop type as a tokens::LoopToken
◆ nodename()
const char* nodename |
( |
| ) |
const |
|
inlineoverridevirtual |
Virtual method for accessing node name information.
Implements Node.
◆ nodetype()
Virtual method for accessing node type information.
Implements Node.
◆ parent()
const Node* parent |
( |
| ) |
const |
|
inlineinherited |
Access a const pointer to this nodes parent.
- Note
- Can be a nullptr if this is the top most node in an AST (usually a Tree)
- Returns
- A const pointer to this node's parent node
◆ replace()
bool replace |
( |
Node * |
node | ) |
|
|
inlineinherited |
In place replacement. Attempts to replace this node at its specific location within its Abstract Syntax Tree. On a successful replacement, this node is destroyed, the provided node is inserted in its place and ownership is transferred to the parent node. No further calls to this node can be made on successful replacements.
- Note
- A replacement will fail if this node is the top most node within an AST hierarchy or if the provided node type is not a compatible type for the required abstract storage. For example, if this node is an Attribute being held on a BinaryOperator, only concrete nodes derived from an Expression can be used as a replacement.
-
This method will dynamic_cast the provided node to check to see if it's a compatible type.
- Parameters
-
node | The node to insert on a successful replacement. |
- Returns
- True if the replacement was successful, resulting in destruction of this class and ownership transferal of the provided node. False otherwise, where this and the provided node are unchanged.
◆ replacechild()
bool replacechild |
( |
const size_t |
i, |
|
|
Node * |
node |
|
) |
| |
|
inlinefinaloverridevirtual |
Virtual method that attempted to replace a child at a given index with a provided node type.
Reimplemented from Node.
◆ setParent()
void setParent |
( |
Node * |
parent | ) |
|
|
inlineinherited |
Set this node's parent. This is used during construction of an AST and should not be used.
- Parameters
-
◆ subname()
const char* subname |
( |
| ) |
const |
|
inlineoverridevirtual |
Virtual method for accessing node name information.
Implements Node.
The documentation for this struct was generated from the following file: