dawn-cmake/docs/compound_statements.md
Ben Clayton 6e459fecb7 sem: Add CompoundStatement
This change introduces sem::CompoundStatement, a new base class for
statements that can hold other statements.

sem::BlockStatements now derives from sem::CompoundStatement, and
this change introduces the following new CompoundStatements:
* `sem::IfStatement`
* `sem::ElseStatement`
* `sem::ForLoopStatement`
* `sem::LoopStatement`
* `sem::SwitchStatement`.
These new CompoundStatements are now inserted into the semantic
tree as now documented in `docs/compound_statements.md`.

The `sem::BlockStatement::FindFirstParent()` methods have been
moved down to `sem::Statement`.

The `Resolver::BlockScope()` method has been replaced with
`Resolver::Scope()` which now maintains the `current_statement_`,
`current_compound_statement_ ` and `current_block_`. This
simplifies statement nesting.

The most significant change in behavior is that statements now
always have a parent, so calling Block() on the initializer or
continuing of a for-loop statement will now return the
BlockStatement that holds the for-loop. Before this would
return nullptr.

Fixed: tint:979
Change-Id: I90e38fd719da2a281ed9210e975ab96171cb6842
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57707
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
2021-07-14 09:44:41 +00:00

1.6 KiB

Compound Statements

Compound statements are statements that can hold other statements.

This document maps the WGSL compound statements to their semantic tree representations.

if statement

WGSL:

if (condition_a) {
    statement_a;
} else if (condition_b) {
    statement_b;
} else {
    statement_c;
}

Semantic tree:

sem::IfStatement {
    condition_a
    sem::BlockStatement {
        statement_a
    }
    sem::ElseStatement {
        condition_b
        sem::BlockStatement {
            statement_b
        }
    }
    sem::ElseStatement {
        sem::BlockStatement {
            statement_c
        }
    }
}

for loop

WGSL:

for (initializer; condition; continuing) {
    statement;
}

Semantic tree:

sem::ForLoopStatement {
    sem::Statement  initializer
    sem::Expression condition
    sem::Statement  continuing

    sem::LoopBlockStatement {
        sem::Statement statement
    }
}

loop

WGSL:

loop (condition) {
    statement_a;
    continuing {
        statement_b;
    }
}

Semantic tree:

sem::LoopStatement {
    sem::Expression condition

    sem::LoopBlockStatement {
        sem::Statement statement_a
        sem::LoopContinuingBlockStatement {
            sem::Statement statement_b
        }
    }
}

switch statement

WGSL:

switch (condition) {
    case literal_a, literal_b: {
        statement_a;
    }
    default {
        statement_b;
    }
}

Semantic tree:

sem::SwitchStatement {
    sem::Expression condition
    sem::SwitchCaseBlockStatement {
        sem::Statement statement_a
    }
    sem::SwitchCaseBlockStatement {
        sem::Statement statement_b
    }
}