libqasm
library for handling cQASM files
|
Helper class for analyzing a single AST. More...
Public Member Functions | |
AnalyzerHelper (const Analyzer &analyzer, const ast::Program &ast) | |
Analyzes the given AST using the given analyzer. More... | |
void | analyze_version (const ast::Version &ast) |
Parses the version tag. More... | |
void | analyze_qubits (const ast::Expression &count) |
Checks the qubits statement and updates the scope accordingly. More... | |
tree::Maybe< semantic::Subcircuit > | get_current_subcircuit (const tree::Annotatable &source) |
Returns a reference to the subcircuit that's currently being built. More... | |
Scope & | get_current_scope () |
Returns a reference to the current scope. More... | |
Scope & | get_global_scope () |
Returns a reference to the global scope. More... | |
tree::Maybe< semantic::Block > | get_current_block (const tree::Annotatable &source) |
Returns a reference to the block that's currently being built (1.2+). More... | |
void | add_to_current_block (const tree::Maybe< semantic::Statement > &stmt) |
Adds an analyzed statement to the current block (1.2+). More... | |
void | analyze_statements (const ast::StatementList &statements) |
Analyzes the given statement list, adding the analyzed statements to the current subcircuit (API 1.0/1.1) or block (API 1.2+). More... | |
tree::Maybe< semantic::Block > | analyze_subblock (const ast::StatementList &statements, bool is_loop) |
Analyzes a statement list corresponding to a structured control-flow subblock (1.2+). More... | |
void | analyze_bundle (const ast::Bundle &bundle) |
Analyzes the given bundle and, if valid, adds it to the current subcircuit using API version 1.0/1.1. More... | |
void | analyze_bundle_ext (const ast::Bundle &bundle) |
Analyzes the given bundle and, if valid, adds it to the current scope/block using API version 1.2+. More... | |
tree::Maybe< semantic::Instruction > | analyze_instruction (const ast::Instruction &insn) |
Analyzes the given instruction. More... | |
tree::Maybe< semantic::SetInstruction > | analyze_set_instruction (const ast::Instruction &insn) |
Analyzes the given cQASM 1.2+ set instruction. More... | |
tree::Maybe< semantic::SetInstruction > | analyze_set_instruction_operands (const ast::Expression &lhs_expr, const ast::Expression &rhs_expr) |
Analyzes the given two operands as lhs and rhs of a set instruction. More... | |
tree::Maybe< semantic::GotoInstruction > | analyze_goto_instruction (const ast::Instruction &insn) |
Analyzes the given cQASM 1.2+ goto instruction. More... | |
void | analyze_error_model (const ast::Instruction &insn) |
Analyzes the error model meta-instruction and, if valid, adds it to the analysis result. More... | |
void | analyze_mapping (const ast::Mapping &mapping) |
Analyzes the given mapping and, if valid, adds it to the current scope. More... | |
void | analyze_variables (const ast::Variables &variables) |
Analyzes the given declaration of one or more variables and, if valid, adds them to the current scope. More... | |
void | analyze_subcircuit (const ast::Subcircuit &subcircuit) |
Analyzes the given subcircuit header and, if valid, adds it to the subcircuit list. More... | |
void | analyze_structured (const ast::Structured &structured) |
Analyzes the given structured control-flow statement and, if valid, adds it to the current scope/block using API version 1.2+. More... | |
tree::Maybe< semantic::IfElse > | analyze_if_else (const ast::IfElse &if_else) |
Analyzes the given if-else chain. More... | |
tree::Maybe< semantic::ForLoop > | analyze_for_loop (const ast::ForLoop &for_loop) |
Analyzes the given C-style for loop. More... | |
tree::Maybe< semantic::ForeachLoop > | analyze_foreach_loop (const ast::ForeachLoop &foreach_loop) |
Analyzes the given static for loop. More... | |
tree::Maybe< semantic::WhileLoop > | analyze_while_loop (const ast::WhileLoop &while_loop) |
Analyzes the given while loop. More... | |
tree::Maybe< semantic::RepeatUntilLoop > | analyze_repeat_until_loop (const ast::RepeatUntilLoop &repeat_until_loop) |
Analyzes the given repeat-until loop. More... | |
tree::Any< semantic::AnnotationData > | analyze_annotations (const tree::Any< ast::AnnotationData > &annotations) |
Analyzes the given list of annotations. More... | |
values::Value | analyze_expression (const ast::Expression &expression) |
Parses any kind of expression. More... | |
template<class Type , class... TypeArgs> | |
values::Value | analyze_as (const ast::Expression &expression, TypeArgs... type_args) |
Shorthand for parsing an expression and promoting it to the given type, constructed in-place with the type_args parameter pack. More... | |
primitives::Int | analyze_as_const_int (const ast::Expression &expression) |
Shorthand for parsing an expression to a constant integer. More... | |
values::Value | analyze_matrix (const ast::MatrixLiteral &matrix_lit) |
Parses a matrix. More... | |
template<class ElType , class ElVal , class MatLit , class MatVal > | |
values::Value | analyze_matrix_helper (size_t nrows, size_t ncols, const std::vector< values::Value > &vals) |
Helper for parsing a matrix. More... | |
values::Value | analyze_index (const ast::Index &index) |
Parses an index operator. More... | |
tree::Many< values::ConstInt > | analyze_index_list (const ast::IndexList &index_list, size_t size) |
Parses an index list. More... | |
values::Value | analyze_function (const ast::Identifier &name, const ast::ExpressionList &args) |
Parses a function. More... | |
values::Value | analyze_operator (const std::string &name, const tree::One< ast::Expression > &a, const tree::One< ast::Expression > &b=tree::One< ast::Expression >(), const tree::One< ast::Expression > &c=tree::One< ast::Expression >()) |
Parses an operator. More... | |
Public Attributes | |
const Analyzer & | analyzer |
The analyzer associated with this helper. More... | |
AnalysisResult | result |
The analysis result being constructed. More... | |
std::list< Scope > | scope_stack |
Scope stack. More... | |
std::list< std::pair< tree::Maybe< semantic::GotoInstruction >, std::string > > | gotos |
List of all goto instructions in the program, for name resolution when all other analysis completes. More... | |
Helper class for analyzing a single AST.
This contains the stateful information that Analyzer can't have (to allow Analyzer to be reused).
Definition at line 215 of file cqasm-v1-analyzer.cpp.
cqasm::v1::analyzer::AnalyzerHelper::AnalyzerHelper | ( | const Analyzer & | analyzer, |
const ast::Program & | ast | ||
) |
Analyzes the given AST using the given analyzer.
Definition at line 571 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::analyze_version | ( | const ast::Version & | ast | ) |
Parses the version tag.
Checks the AST version node and puts it into the semantic tree.
Any semantic errors encountered are pushed into the result error vector.
Definition at line 693 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::analyze_qubits | ( | const ast::Expression & | count | ) |
Checks the qubits statement and updates the scope accordingly.
Any semantic errors encountered are pushed into the result error vector.
Definition at line 727 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::Subcircuit > cqasm::v1::analyzer::AnalyzerHelper::get_current_subcircuit | ( | const tree::Annotatable & | source | ) |
Returns a reference to the subcircuit that's currently being built.
If there is no subcircuit yet, a default one is created, using the source location annotation on the source node.
Definition at line 762 of file cqasm-v1-analyzer.cpp.
Scope & cqasm::v1::analyzer::AnalyzerHelper::get_current_scope | ( | ) |
Returns a reference to the current scope.
Definition at line 787 of file cqasm-v1-analyzer.cpp.
Scope & cqasm::v1::analyzer::AnalyzerHelper::get_global_scope | ( | ) |
Returns a reference to the global scope.
Definition at line 794 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::Block > cqasm::v1::analyzer::AnalyzerHelper::get_current_block | ( | const tree::Annotatable & | source | ) |
Returns a reference to the block that's currently being built (1.2+).
Returns a reference to the block that's currently being built.
Definition at line 801 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::add_to_current_block | ( | const tree::Maybe< semantic::Statement > & | stmt | ) |
Adds an analyzed statement to the current block (1.2+).
Definition at line 818 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::analyze_statements | ( | const ast::StatementList & | statements | ) |
Analyzes the given statement list, adding the analyzed statements to the current subcircuit (API 1.0/1.1) or block (API 1.2+).
Definition at line 841 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::Block > cqasm::v1::analyzer::AnalyzerHelper::analyze_subblock | ( | const ast::StatementList & | statements, |
bool | is_loop | ||
) |
Analyzes a statement list corresponding to a structured control-flow subblock (1.2+).
Handles the requisite scoping, then defers to analyze_statements().
Definition at line 876 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::analyze_bundle | ( | const ast::Bundle & | bundle | ) |
Analyzes the given bundle and, if valid, adds it to the current subcircuit using API version 1.0/1.1.
If an error occurs, the message is added to the result error vector, and nothing is added to the subcircuit.
Definition at line 905 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::analyze_bundle_ext | ( | const ast::Bundle & | bundle | ) |
Analyzes the given bundle and, if valid, adds it to the current scope/block using API version 1.2+.
Analyzes the given bundle and, if valid, adds it to the current subcircuit using API version 1.2+.
If an error occurs, the message is added to the result error vector, and nothing is added to the block.
If an error occurs, the message is added to the result error vector, and nothing is added to the subcircuit.
Definition at line 978 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::Instruction > cqasm::v1::analyzer::AnalyzerHelper::analyze_instruction | ( | const ast::Instruction & | insn | ) |
Analyzes the given instruction.
If an error occurs, the message is added to the result error vector, and an empty Maybe is returned.
If an error occurs, the message is added to the result error vector, and an empty Maybe is returned. It's also possible that an empty Maybe is returned without an error, when the condition code statically resolves to false.
Definition at line 1060 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::SetInstruction > cqasm::v1::analyzer::AnalyzerHelper::analyze_set_instruction | ( | const ast::Instruction & | insn | ) |
Analyzes the given cQASM 1.2+ set instruction.
If an error occurs, the message is added to the result error vector, and an empty Maybe is returned.
Definition at line 1172 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::SetInstruction > cqasm::v1::analyzer::AnalyzerHelper::analyze_set_instruction_operands | ( | const ast::Expression & | lhs_expr, |
const ast::Expression & | rhs_expr | ||
) |
Analyzes the given two operands as lhs and rhs of a set instruction.
Used for the actual set instruction as well as the assignments in the header of a C-style for loop.
Definition at line 1224 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::GotoInstruction > cqasm::v1::analyzer::AnalyzerHelper::analyze_goto_instruction | ( | const ast::Instruction & | insn | ) |
Analyzes the given cQASM 1.2+ goto instruction.
If an error occurs, the message is added to the result error vector, and an empty Maybe is returned.
Definition at line 1262 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::analyze_error_model | ( | const ast::Instruction & | insn | ) |
Analyzes the error model meta-instruction and, if valid, adds it to the analysis result.
If an error occurs, the message is added to the result error vector, and nothing is added.
Definition at line 1327 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::analyze_mapping | ( | const ast::Mapping & | mapping | ) |
Analyzes the given mapping and, if valid, adds it to the current scope.
If an error occurs, the message is added to the result error vector, and nothing is added to the scope.
Definition at line 1391 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::analyze_variables | ( | const ast::Variables & | variables | ) |
Analyzes the given declaration of one or more variables and, if valid, adds them to the current scope.
If an error occurs, the message is added to the result error vector, and nothing is added to the scope.
Definition at line 1409 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::analyze_subcircuit | ( | const ast::Subcircuit & | subcircuit | ) |
Analyzes the given subcircuit header and, if valid, adds it to the subcircuit list.
If an error occurs, the message is added to the result error vector, and nothing is added to the result.
Definition at line 1463 of file cqasm-v1-analyzer.cpp.
void cqasm::v1::analyzer::AnalyzerHelper::analyze_structured | ( | const ast::Structured & | structured | ) |
Analyzes the given structured control-flow statement and, if valid, adds it to the current scope/block using API version 1.2+.
If an error occurs, the message is added to the result error vector, and nothing is added to the block.
Definition at line 1500 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::IfElse > cqasm::v1::analyzer::AnalyzerHelper::analyze_if_else | ( | const ast::IfElse & | if_else | ) |
Analyzes the given if-else chain.
Only intended for use as a helper function within analyze_structured().
Definition at line 1561 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::ForLoop > cqasm::v1::analyzer::AnalyzerHelper::analyze_for_loop | ( | const ast::ForLoop & | for_loop | ) |
Analyzes the given C-style for loop.
Only intended for use as a helper function within analyze_structured().
Definition at line 1631 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::ForeachLoop > cqasm::v1::analyzer::AnalyzerHelper::analyze_foreach_loop | ( | const ast::ForeachLoop & | foreach_loop | ) |
Analyzes the given static for loop.
Only intended for use as a helper function within analyze_structured().
Definition at line 1674 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::WhileLoop > cqasm::v1::analyzer::AnalyzerHelper::analyze_while_loop | ( | const ast::WhileLoop & | while_loop | ) |
Analyzes the given while loop.
Only intended for use as a helper function within analyze_structured().
Definition at line 1702 of file cqasm-v1-analyzer.cpp.
tree::Maybe< semantic::RepeatUntilLoop > cqasm::v1::analyzer::AnalyzerHelper::analyze_repeat_until_loop | ( | const ast::RepeatUntilLoop & | repeat_until_loop | ) |
Analyzes the given repeat-until loop.
Only intended for use as a helper function within analyze_structured().
Definition at line 1734 of file cqasm-v1-analyzer.cpp.
tree::Any< semantic::AnnotationData > cqasm::v1::analyzer::AnalyzerHelper::analyze_annotations | ( | const tree::Any< ast::AnnotationData > & | annotations | ) |
Analyzes the given list of annotations.
Any errors found result in the annotation being skipped and an error being appended to the result error vector.
Definition at line 1770 of file cqasm-v1-analyzer.cpp.
values::Value cqasm::v1::analyzer::AnalyzerHelper::analyze_expression | ( | const ast::Expression & | expression | ) |
Parses any kind of expression.
Always returns a filled value or throws an exception.
Definition at line 1801 of file cqasm-v1-analyzer.cpp.
values::Value cqasm::v1::analyzer::AnalyzerHelper::analyze_as | ( | const ast::Expression & | expression, |
TypeArgs... | type_args | ||
) |
Shorthand for parsing an expression and promoting it to the given type, constructed in-place with the type_args parameter pack.
Returns empty when the cast fails.
Definition at line 1898 of file cqasm-v1-analyzer.cpp.
primitives::Int cqasm::v1::analyzer::AnalyzerHelper::analyze_as_const_int | ( | const ast::Expression & | expression | ) |
Shorthand for parsing an expression to a constant integer.
Definition at line 1905 of file cqasm-v1-analyzer.cpp.
values::Value cqasm::v1::analyzer::AnalyzerHelper::analyze_matrix | ( | const ast::MatrixLiteral & | matrix_lit | ) |
Parses a matrix.
Always returns a filled value or throws an exception.
Definition at line 1925 of file cqasm-v1-analyzer.cpp.
values::Value cqasm::v1::analyzer::AnalyzerHelper::analyze_matrix_helper | ( | size_t | nrows, |
size_t | ncols, | ||
const std::vector< values::Value > & | vals | ||
) |
Helper for parsing a matrix.
Highly templated to avoid repeating the same code for different kinds of matrices, but bear in mind that the template parameters are codependent. Returns empty on failure.
Definition at line 1975 of file cqasm-v1-analyzer.cpp.
values::Value cqasm::v1::analyzer::AnalyzerHelper::analyze_index | ( | const ast::Index & | index | ) |
Parses an index operator.
Always returns a filled value or throws an error.
Definition at line 2001 of file cqasm-v1-analyzer.cpp.
tree::Many< values::ConstInt > cqasm::v1::analyzer::AnalyzerHelper::analyze_index_list | ( | const ast::IndexList & | index_list, |
size_t | size | ||
) |
Parses an index list.
Definition at line 2035 of file cqasm-v1-analyzer.cpp.
values::Value cqasm::v1::analyzer::AnalyzerHelper::analyze_function | ( | const ast::Identifier & | name, |
const ast::ExpressionList & | args | ||
) |
Parses a function.
Always returns a filled value or throws an exception.
Definition at line 2088 of file cqasm-v1-analyzer.cpp.
values::Value cqasm::v1::analyzer::AnalyzerHelper::analyze_operator | ( | const std::string & | name, |
const tree::One< ast::Expression > & | a, | ||
const tree::One< ast::Expression > & | b = tree::One<ast::Expression>() , |
||
const tree::One< ast::Expression > & | c = tree::One<ast::Expression>() |
||
) |
Parses an operator.
Always returns a filled value or throws an exception.
Definition at line 2103 of file cqasm-v1-analyzer.cpp.
const Analyzer& cqasm::v1::analyzer::AnalyzerHelper::analyzer |
The analyzer associated with this helper.
Definition at line 221 of file cqasm-v1-analyzer.cpp.
AnalysisResult cqasm::v1::analyzer::AnalyzerHelper::result |
The analysis result being constructed.
Definition at line 226 of file cqasm-v1-analyzer.cpp.
std::list<Scope> cqasm::v1::analyzer::AnalyzerHelper::scope_stack |
Scope stack.
back() is the global scope, front() is the current scope.
Definition at line 231 of file cqasm-v1-analyzer.cpp.
std::list<std::pair<tree::Maybe<semantic::GotoInstruction>, std::string> > cqasm::v1::analyzer::AnalyzerHelper::gotos |
List of all goto instructions in the program, for name resolution when all other analysis completes.
Definition at line 237 of file cqasm-v1-analyzer.cpp.