libqasm
library for handling cQASM files
cqasm-v1-resolver.hpp
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <functional>
11 #include <algorithm>
12 #include "cqasm-v1-error-model.hpp"
13 #include "cqasm-v1-instruction.hpp"
14 #include "cqasm-v1-semantic.hpp"
15 #include "cqasm-error.hpp"
16 
17 namespace cqasm {
18 namespace v1 {
19 
23 namespace resolver {
24 
28 CQASM_ANALYSIS_ERROR(NameResolutionFailure);
29 
33 CQASM_ANALYSIS_ERROR(OverloadResolutionFailure);
34 
38 class MappingTable {
39 private:
40  std::unordered_map<std::string, std::pair<const values::Value, tree::Maybe<ast::Mapping>>> table;
41 public:
42 
46  void add(
47  const std::string &name,
48  const values::Value &value,
50  );
51 
56  values::Value resolve(const std::string &name) const;
57 
61  const std::unordered_map<std::string, std::pair<const values::Value, tree::Maybe<ast::Mapping>>> &get_table() const;
62 
63 };
64 
65 // Forward declaration for the name resolver template class. This class is
66 // defined entirely in the C++ file to cut back on compile time.
67 template <class T>
69 
74 using FunctionImpl = std::function<values::Value(const values::Values&)>;
75 
80 private:
81  std::unique_ptr<OverloadedNameResolver<FunctionImpl>> resolver;
82 public:
83 
84  // The following things *are all default*. Unfortunately, the compiler
85  // can't infer them because OverloadedNameResolver is incomplete.
86  FunctionTable();
87  ~FunctionTable();
88  FunctionTable(const FunctionTable& t);
90  FunctionTable& operator=(const FunctionTable& t);
91  FunctionTable& operator=(FunctionTable&& t);
92 
106  void add(const std::string &name, const types::Types &param_types, const FunctionImpl &impl);
107 
114  values::Value call(const std::string &name, const values::Values &args) const;
115 
116 };
117 
122 private:
123  std::unique_ptr<OverloadedNameResolver<error_model::ErrorModel>> resolver;
124 public:
125 
126  // The following things *are all default*. Unfortunately, the compiler
127  // can't infer them because OverloadedNameResolver is incomplete.
128  ErrorModelTable();
129  ~ErrorModelTable();
132  ErrorModelTable& operator=(const ErrorModelTable& t);
133  ErrorModelTable& operator=(ErrorModelTable&& t);
134 
138  void add(const error_model::ErrorModel &type);
139 
148  const std::string &name,
149  const values::Values &args
150  ) const;
151 
152 };
153 
158 private:
159  std::unique_ptr<OverloadedNameResolver<instruction::Instruction>> resolver;
160 public:
161 
162  // The following things *are all default*. Unfortunately, the compiler
163  // can't infer them because OverloadedNameResolver is incomplete.
165  ~InstructionTable();
168  InstructionTable& operator=(const InstructionTable& t);
169  InstructionTable& operator=(InstructionTable&& t);
170 
174  void add(const instruction::Instruction &type);
175 
184  const std::string &name,
185  const values::Values &args
186  ) const;
187 
188 };
189 
190 } // namespace resolver
191 } // namespace v1
192 } // namespace cqasm
void add(const std::string &name, const values::Value &value, const tree::Maybe< ast::Mapping > &node=tree::Maybe< ast::Mapping >())
Adds a mapping.
const std::unordered_map< std::string, std::pair< const values::Value, tree::Maybe< ast::Mapping > > > & get_table() const
Grants read access to the underlying map.
::tree::base::Maybe< T > Maybe
Definition: cqasm-tree.hpp:23
This file contains the Instruction class and support types, each instance representing an instruction...
This file contains the ErrorModel class and support types, each instance representing an error model ...
Toplevel namespace with entry points for the new API.
Table of the supported instructions and their overloads.
tree::Any< Node > Values
Zero or more cQASM values.
Defines the types for the cQASM semantic tree, based on the classes from cqasm::tree.
Representation of an error model.
Representation of an available instruction (also known as gate) in the instruction set...
Table of all overloads of all constant propagation functions.
Table of the supported instructions and their overloads.
Namespace for the "new" cQASM 1.x API.
Contains custom exception objects used by libqasm.
tree::One< Node > Value
A cQASM value, either known at compile-time or an expression for something only known at runtime...
Table of all mappings within a certain scope.
CQASM_ANALYSIS_ERROR(NameResolutionFailure)
Exception for failed name resolutions.
tree::Any< TypeBase > Types
Zero or more cQASM types.
Table of overloaded callables with case-insensitive identifier matching.
values::Value resolve(const std::string &name) const
Resolves a mapping.
::tree::base::One< T > One
Definition: cqasm-tree.hpp:26
std::function< values::Value(const values::Values &)> FunctionImpl
C++ function representing (one of the overloads of) a function usable in cQASM constant expressions...