libqasm
library for handling cQASM files
Public Member Functions | Public Attributes | List of all members
cqasm::v1::analyzer::AnalyzerHelper Class Reference

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::Subcircuitget_current_subcircuit (const tree::Annotatable &source)
 Returns a reference to the subcircuit that's currently being built. More...
 
Scopeget_current_scope ()
 Returns a reference to the current scope. More...
 
Scopeget_global_scope ()
 Returns a reference to the global scope. More...
 
tree::Maybe< semantic::Blockget_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::Blockanalyze_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::Instructionanalyze_instruction (const ast::Instruction &insn)
 Analyzes the given instruction. More...
 
tree::Maybe< semantic::SetInstructionanalyze_set_instruction (const ast::Instruction &insn)
 Analyzes the given cQASM 1.2+ set instruction. More...
 
tree::Maybe< semantic::SetInstructionanalyze_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::GotoInstructionanalyze_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::IfElseanalyze_if_else (const ast::IfElse &if_else)
 Analyzes the given if-else chain. More...
 
tree::Maybe< semantic::ForLoopanalyze_for_loop (const ast::ForLoop &for_loop)
 Analyzes the given C-style for loop. More...
 
tree::Maybe< semantic::ForeachLoopanalyze_foreach_loop (const ast::ForeachLoop &foreach_loop)
 Analyzes the given static for loop. More...
 
tree::Maybe< semantic::WhileLoopanalyze_while_loop (const ast::WhileLoop &while_loop)
 Analyzes the given while loop. More...
 
tree::Maybe< semantic::RepeatUntilLoopanalyze_repeat_until_loop (const ast::RepeatUntilLoop &repeat_until_loop)
 Analyzes the given repeat-until loop. More...
 
tree::Any< semantic::AnnotationDataanalyze_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::ConstIntanalyze_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 Analyzeranalyzer
 The analyzer associated with this helper. More...
 
AnalysisResult result
 The analysis result being constructed. More...
 
std::list< Scopescope_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...
 

Detailed Description

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.

Collaboration diagram for cqasm::v1::analyzer::AnalyzerHelper:
Collaboration graph
[legend]

Constructor & Destructor Documentation

◆ AnalyzerHelper()

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.

Member Function Documentation

◆ analyze_version()

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.

◆ analyze_qubits()

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.

◆ get_current_subcircuit()

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.

◆ get_current_scope()

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.

◆ get_global_scope()

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.

◆ get_current_block()

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.

◆ add_to_current_block()

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.

◆ analyze_statements()

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.

◆ analyze_subblock()

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.

◆ analyze_bundle()

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.

◆ analyze_bundle_ext()

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.

◆ analyze_instruction()

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.

◆ analyze_set_instruction()

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.

◆ analyze_set_instruction_operands()

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.

◆ analyze_goto_instruction()

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.

◆ analyze_error_model()

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.

◆ analyze_mapping()

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.

◆ analyze_variables()

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.

◆ analyze_subcircuit()

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.

◆ analyze_structured()

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.

◆ analyze_if_else()

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.

◆ analyze_for_loop()

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.

◆ analyze_foreach_loop()

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.

◆ analyze_while_loop()

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.

◆ analyze_repeat_until_loop()

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.

◆ analyze_annotations()

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.

◆ analyze_expression()

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.

◆ analyze_as()

template<class Type , class... TypeArgs>
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.

◆ analyze_as_const_int()

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.

◆ analyze_matrix()

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.

◆ analyze_matrix_helper()

template<class ElType , class ElVal , class MatLit , class MatVal >
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.

◆ analyze_index()

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.

◆ analyze_index_list()

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.

◆ analyze_function()

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.

◆ analyze_operator()

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.

Member Data Documentation

◆ analyzer

const Analyzer& cqasm::v1::analyzer::AnalyzerHelper::analyzer

The analyzer associated with this helper.

Definition at line 221 of file cqasm-v1-analyzer.cpp.

◆ result

AnalysisResult cqasm::v1::analyzer::AnalyzerHelper::result

The analysis result being constructed.

Definition at line 226 of file cqasm-v1-analyzer.cpp.

◆ scope_stack

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.

◆ gotos

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.


The documentation for this class was generated from the following file: