Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 Extended: Functions #198

Merged
merged 33 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5306db7
Add function declarations to the grammar.
rturrado Feb 13, 2024
6a0c326
Update res/v3x/parsing files. Because:
rturrado Feb 13, 2024
d88c213
Add return type to function declarations.
rturrado Feb 13, 2024
d69c214
Changes to the v3 lexer:
rturrado Feb 14, 2024
ef52166
Update res/v3x/parsing files:
rturrado Feb 14, 2024
43c8fe7
Changes to the syntactic AST:
rturrado Feb 15, 2024
1378db4
Take v1x/{Scope and AnalyzerHelper} classes out of cqasm-analyzer.
rturrado Feb 15, 2024
b9d3561
Change param_types default parameter to an optional at Instruction co…
rturrado Feb 16, 2024
ab6a8db
Changes to the v3x/parser:
rturrado Feb 20, 2024
6976d4d
Temporarily adding a FunctionTable apart from the FunctionImplTable.
rturrado Feb 21, 2024
1bf49b6
Add assignment statement to the parser and to BuildTreeGenAstVisitor.
rturrado Feb 22, 2024
934f1fd
Changes to the parser:
rturrado Feb 22, 2024
9c9e4e6
Changes to AnalyzeTreeGenAstVisitor:
rturrado Feb 22, 2024
7fcdf51
Update res/v3x/parsing/function files.
rturrado Feb 22, 2024
9442003
Update res/v3x/parsing/function files.
rturrado Feb 23, 2024
3b32a1d
Changes to the parser:
rturrado Feb 23, 2024
d30408b
Fix Python tests.
rturrado Feb 24, 2024
0787e1c
Fix Python tests.
rturrado Feb 24, 2024
2fb83b4
Merge remote-tracking branch 'origin/v3-extended-functions' into v3-e…
rturrado Feb 24, 2024
c368e2e
Fix C++ tests.
rturrado Feb 26, 2024
3f026df
Fix C++ tests.
rturrado Feb 26, 2024
5ba0277
Merge remote-tracking branch 'origin/v3-extended-functions' into v3-e…
rturrado Feb 26, 2024
13f2181
Fix expand_to_include.
rturrado Feb 26, 2024
19e3332
Change filename to file_name.
rturrado Feb 26, 2024
2e74787
Add some Analyzer unit tests.
rturrado Feb 26, 2024
11d53f1
Add some AnalyzeTreeGenAstVisitor::visit_function unit tests.
rturrado Feb 27, 2024
1adbb3b
Added some v3x::values unit tests.
rturrado Feb 27, 2024
c391be0
Addressed some of the comments from Pablo's code review.
rturrado Mar 4, 2024
1fb1e56
Trying to fix clang compilation.
rturrado Mar 5, 2024
6295a85
Trying to fix clang compilation.
rturrado Mar 5, 2024
0c86f00
Merge remote-tracking branch 'origin/v3-extended-functions' into v3-e…
rturrado Mar 5, 2024
20db914
Trying to fix clang compilation.
rturrado Mar 5, 2024
0ae2b1a
Trying to fix clang compilation.
rturrado Mar 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
68 changes: 46 additions & 22 deletions include/cqasm-annotations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,72 @@ namespace cqasm::annotations {
/**
* Source location annotation object, containing source file line numbers etc.
*/
class SourceLocation {
public:
struct SourceLocation {
/**
* The name of the source file.
* An index within a source location.
* An index is defined by a line and a column.
*/
std::optional<std::string> file_name;
struct Index {
std::uint32_t line = 0;
std::uint32_t column = 0;

/**
* The first line of the range, or 0 if unknown.
*/
std::uint32_t first_line;
bool operator==(const Index &other) const = default;
auto operator<=>(const Index &other) const = default;
};

/**
* The first column of the range, or 0 if unknown.
* A range within a source location.
* A range is defined by a first and a last index.
*/
std::uint32_t first_column;
struct Range {
Index first;
Index last;

Range() = default;
Range(const Index &f, const Index &l);
Range(const Range &other) = default;
Range(Range &&other) noexcept = default;
Range& operator=(const Range &other) = default;
Range& operator=(Range &&other) noexcept = default;

bool operator==(const Range &other) const = default;
auto operator<=>(const Range &other) const = default;
};

/**
* The last line of the range, or 0 if unknown.
* The name of the source file.
*/
std::uint32_t last_line;
std::optional<std::string> file_name;

/**
* The last column of the range, or 0 if unknown.
* The source location range.
*/
std::uint32_t last_column;
Range range;

/**
* Constructs a source location object.
*/
explicit SourceLocation(
const std::optional<std::string> &file_name,
std::uint32_t first_line = 0,
std::uint32_t first_column = 0,
std::uint32_t last_line = 0,
std::uint32_t last_column = 0
);
SourceLocation() = default;
explicit SourceLocation(const std::optional<std::string> &file_name, const Range &range);
SourceLocation(const SourceLocation &other) = default;
SourceLocation(SourceLocation &&other) noexcept = default;
SourceLocation& operator=(const SourceLocation &other) = default;
SourceLocation& operator=(SourceLocation &&other) noexcept = default;

bool operator==(const SourceLocation &other) const = default;
// Some versions of clang still do not implement operator<=> for std::optional
// So they will complain if we try to define a default operator<=> on a struct that contains a std::optional
// This implementation just does not check the optional file_name
// Instead, it just assumes that, when comparing source locations, the file_name will be the same
// This will always be the case if we are just parsing or analyzing a file or a string
auto operator<=>(const SourceLocation &other) const {
return range <=> other.range;
}

/**
* Expands the location range to contain the given location in the source file.
*/
void expand_to_include(std::uint32_t line, std::uint32_t column = 1);
void expand_to_include(const Index &last);
};

/**
Expand Down
5 changes: 1 addition & 4 deletions include/cqasm-error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ class Error : public std::runtime_error {
Error(
const std::string &message,
const std::optional<std::string> &file_name,
std::uint32_t first_line,
std::uint32_t first_column,
std::uint32_t last_line,
std::uint32_t last_column);
const annotations::SourceLocation::Range &range);

/**
* Sets the context of this error to the SourceLocation annotation of the given node,
Expand Down
24 changes: 18 additions & 6 deletions include/cqasm-overload.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ namespace cqasm::overload {
struct NameResolutionFailure : public std::exception {};
struct OverloadResolutionFailure : public std::exception {};


//----------//
// Overload //
//----------//

/**
* Represents a possible overload for the parameter types of a function, gate, or error model.
* T is some tag type identifying the overload.
Expand Down Expand Up @@ -60,6 +65,11 @@ class Overload {
}
};


//------------------//
// OverloadResolver //
//------------------//

/**
* Represents a set of possible overloads for the parameter types of a function, gate, or error model.
* T is some tag type identifying the overload.
Expand Down Expand Up @@ -116,6 +126,11 @@ class OverloadResolver {
}
};


//----------------------//
// OverloadNameResolver //
//----------------------//

/**
* Table of overloaded callables with case-sensitive identifier matching.
* T is the tag type of the callable/overload pair.
Expand All @@ -141,8 +156,7 @@ class OverloadedNameResolver {
* so more specific overloads should always be added last.
*/
virtual void add_overload(const std::string &name, const T &tag, const Types &param_types) {
auto entry = table.find(name);
if (entry == table.end()) {
if (auto entry = table.find(name); entry == table.end()) {
auto resolver = OverloadResolver<T, TypeBase, Node>();
resolver.add_overload(tag, param_types);
table.insert(std::pair<std::string, OverloadResolver<T, TypeBase, Node>>(name, std::move(resolver)));
Expand All @@ -159,12 +173,10 @@ class OverloadedNameResolver {
* the appropriately promoted vector of value pointers.
*/
[[nodiscard]] virtual std::pair<T, Values> resolve(const std::string &name, const Values &args) {
auto entry = table.find(name);
if (entry == table.end()) {
throw NameResolutionFailure{};
} else {
if (auto entry = table.find(name); entry != table.end()) {
return entry->second.resolve(args);
}
throw NameResolutionFailure{};
}
};

Expand Down
6 changes: 2 additions & 4 deletions include/cqasm-tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
#include "tree-annotatable.hpp"
#include "tree-base.hpp"

namespace cqasm {

/**
* Namespace for wrapping tree-gen's support library.
*/
namespace tree {
namespace cqasm::tree {

using signed_size_t = ::tree::signed_size_t;

Expand Down Expand Up @@ -45,5 +44,4 @@ One<T> make(Args... args) {
return One<T>(std::make_shared<T>(args...));
}

} // namespace tree
} // namespace cqasm
} // namespace cqasm::tree
4 changes: 2 additions & 2 deletions include/v10/qasm_new_to_old.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ static void handle_parse_result(QasmRepresentation &qasm, cq1x::parser::ParseRes
if (!subcircuit->name.empty()) {
int line_number = 0;
if (auto loc = subcircuit->get_annotation_ptr<cq1x::parser::SourceLocation>()) {
line_number = static_cast<int>(loc->first_line);
line_number = static_cast<int>(loc->range.first.line);
}
auto subcircuit_sp{ std::make_shared<SubCircuit>(
subcircuit->name.c_str(),
Expand Down Expand Up @@ -368,7 +368,7 @@ static void handle_parse_result(QasmRepresentation &qasm, cq1x::parser::ParseRes
if (!opclus) {
int line_number = 0;
if (auto loc = instruction->get_annotation_ptr<cq1x::parser::SourceLocation>()) {
line_number = static_cast<int>(loc->first_line);
line_number = static_cast<int>(loc->range.first.line);
}
opclus = std::make_shared<OperationsCluster>(op, line_number);
} else {
Expand Down
Loading
Loading