5 #include <unordered_set> 24 const std::string &name,
29 auto it = table.find(lname);
30 if (it != table.end()) {
47 if (entry == table.end()) {
48 throw NameResolutionFailure(
"failed to resolve " + name);
50 return Value(entry->second.first->clone());
57 const std::unordered_map<std::string, std::pair<const values::Value, tree::Maybe<ast::Mapping>>> &
MappingTable::get_table()
const {
74 template <
class... Ts>
76 : tag(tag), param_types(param_types)
90 return param_types.size();
98 return param_types.at(index);
113 std::vector<Overload<T>> overloads;
123 overloads.emplace_back(tag, param_types);
133 for (
auto overload = overloads.rbegin(); overload != overloads.rend(); ++overload) {
134 if (overload->num_params() != args.size()) {
139 for (
size_t i = 0; i < args.size(); i++) {
140 auto promoted_arg =
promote(args.at(i), overload->param_type_at(i));
141 if (promoted_arg.empty()) {
145 promoted_args.add(promoted_arg);
148 return std::pair<T, Values>(overload->get_tag(), promoted_args);
151 throw OverloadResolutionFailure(
"failed to resolve overload");
163 std::unordered_map<std::string, OverloadResolver<T>> table;
177 auto entry = table.find(name_lower);
178 if (entry == table.end()) {
181 table.insert(std::pair<std::string,
OverloadResolver<T>>(name_lower, std::move(resolver)));
183 entry->second.add_overload(tag, param_types);
197 auto entry = table.find(name_lower);
198 if (entry == table.end()) {
199 throw NameResolutionFailure(
"failed to resolve " + name);
202 return entry->second.resolve(args);
203 }
catch (OverloadResolutionFailure &e) {
205 e.message <<
"failed to resolve overload for " << name;
225 resolver = std::move(t.resolver);
243 resolver->add_overload(name, impl, param_types);
255 auto resolution = resolver->resolve(name, args);
259 return resolution.first(resolution.second);
274 resolver = std::move(t.resolver);
293 auto resolved = resolver->resolve(name, args);
294 return tree::make<semantic::ErrorModel>(
295 tree::make<error_model::ErrorModel>(resolved.first),
312 resolver = std::move(t.resolver);
331 const std::string &name,
334 auto resolved = resolver->resolve(name, args);
335 return tree::make<semantic::Instruction>(
336 tree::make<instruction::Instruction>(resolved.first),
void add(const error_model::ErrorModel &type)
Registers an error model.
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
types::Types types_of(const Values &values)
Returns the types of the given values.
std::string name
The name of the error model.
types::Types param_types
The vector of parameter types that this error model expects.
Toplevel namespace with entry points for the new API.
Defines various utility functions.
ErrorModelTable & operator=(const ErrorModelTable &t)
InstructionTable & operator=(const InstructionTable &t)
void add_overload(const T &tag, const Types ¶m_types)
Adds a possible overload to the resolver.
Table of the supported instructions and their overloads.
FunctionTable & operator=(const FunctionTable &t)
tree::Any< Node > Values
Zero or more cQASM values.
Representation of an error model.
Overload(const T &tag, const Types ¶m_types)
Construct a possible overload.
Representation of an available instruction (also known as gate) in the instruction set...
Value promote(const Value &value, const types::Type &type)
Type-checks and (if necessary) promotes the given value to the given type.
Table of all overloads of all constant propagation functions.
Table of the supported instructions and their overloads.
types::Types param_types
The vector of parameter types that this instruction expects.
tree::One< semantic::Instruction > resolve(const std::string &name, const values::Values &args) const
Resolves an instruction.
Namespace for the "new" cQASM 1.x API.
void add_overload(const std::string &name, const T &tag, const Types ¶m_types)
Registers a callable.
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...
tree::One< TypeBase > Type
A cQASM type.
::tree::base::Any< T > Any
tree::Any< TypeBase > Types
Zero or more cQASM types.
std::pair< T, Values > resolve(const Values &args)
Tries to resolve which overload belongs to the given argument list, if any.
tree::One< semantic::ErrorModel > resolve(const std::string &name, const values::Values &args) const
Resolves an error model.
void add(const instruction::Instruction &type)
Registers an instruction type.
Contains MappingTable, FunctionTable, and ErrorModelTable, representing the various cQASM namespaces ...
std::pair< T, Values > resolve(const std::string &name, const Values &args)
Resolves the particular overload for the callable with the given case-insensitively matched name...
Represents a set of possible overloads for the parameter types of a function, gate, or error model.
std::string lowercase(const std::string &name)
Makes a string lowercase.
Table of overloaded callables with case-insensitive identifier matching.
values::Value resolve(const std::string &name) const
Resolves a mapping.
const T & get_tag() const
Returns the tag for this overload.
values::Value call(const std::string &name, const values::Values &args) const
Calls a function.
::tree::base::One< T > One
void add(const std::string &name, const types::Types ¶m_types, const FunctionImpl &impl)
Registers a function.
Represents a possible overload for the parameter types of a function, gate, or error model...
std::function< values::Value(const values::Values &)> FunctionImpl
C++ function representing (one of the overloads of) a function usable in cQASM constant expressions...
size_t num_params() const
Returns the number of parameters for this overload.
const Type & param_type_at(size_t index) const
Returns the parameter type object for the parameter at the specified index.
std::string name
The name of the instruction.