diff --git a/src/spider/CMakeLists.txt b/src/spider/CMakeLists.txt index 4119bbc55..dd9cd4824 100644 --- a/src/spider/CMakeLists.txt +++ b/src/spider/CMakeLists.txt @@ -233,6 +233,7 @@ set(SPIDER_TDL_SHARED_HEADERS tdl/parser/ast/node_impl/type_impl/primitive_impl/Float.hpp tdl/parser/ast/node_impl/type_impl/primitive_impl/Int.hpp tdl/parser/ast/node_impl/type_impl/Struct.hpp + tdl/parser/ast/SourceLocation.hpp tdl/parser/ast/utils.hpp CACHE INTERNAL "spider task definition language shared header files" diff --git a/src/spider/tdl/parser/ast/Node.hpp b/src/spider/tdl/parser/ast/Node.hpp index 3fa105a17..4992d15ec 100644 --- a/src/spider/tdl/parser/ast/Node.hpp +++ b/src/spider/tdl/parser/ast/Node.hpp @@ -11,6 +11,8 @@ #include #include +#include + namespace spider::tdl::parser::ast { /** * Abstracted base class for all AST nodes in the TDL. @@ -90,9 +92,13 @@ class Node { -> ystdlib::error_handling::Result = 0; + [[nodiscard]] auto get_source_location() const noexcept -> SourceLocation { + return m_source_location; + } + protected: // Constructor - Node() = default; + explicit Node(SourceLocation source_location) : m_source_location{source_location} {} /** * Adds a child node to this AST node. @@ -118,6 +124,7 @@ class Node { // Variables std::vector> m_children; Node const* m_parent = nullptr; + SourceLocation m_source_location; }; } // namespace spider::tdl::parser::ast diff --git a/src/spider/tdl/parser/ast/SourceLocation.hpp b/src/spider/tdl/parser/ast/SourceLocation.hpp new file mode 100644 index 000000000..63ff05add --- /dev/null +++ b/src/spider/tdl/parser/ast/SourceLocation.hpp @@ -0,0 +1,24 @@ +#ifndef SPIDER_TDL_AST_SOURCELOCATION_HPP +#define SPIDER_TDL_AST_SOURCELOCATION_HPP + +#include + +namespace spider::tdl::parser::ast { +class SourceLocation { +public: + // Constructor + SourceLocation(size_t line, size_t column) : m_line{line}, m_column{column} {} + + // Methods + [[nodiscard]] auto get_line() const noexcept -> size_t { return m_line; } + + [[nodiscard]] auto get_column() const noexcept -> size_t { return m_column; } + +private: + // Variables + size_t m_line; + size_t m_column; +}; +} // namespace spider::tdl::parser::ast + +#endif // SPIDER_TDL_AST_SOURCELOCATION_HPP diff --git a/src/spider/tdl/parser/ast/node_impl/Function.cpp b/src/spider/tdl/parser/ast/node_impl/Function.cpp index a2987cf77..8af8233f9 100644 --- a/src/spider/tdl/parser/ast/node_impl/Function.cpp +++ b/src/spider/tdl/parser/ast/node_impl/Function.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include using spider::tdl::parser::ast::node_impl::Function; @@ -41,7 +42,8 @@ namespace spider::tdl::parser::ast::node_impl { auto Function::create( std::unique_ptr name, std::unique_ptr return_type, - std::vector> params + std::vector> params, + SourceLocation source_location ) -> ystdlib::error_handling::Result> { YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type(name.get())); @@ -61,7 +63,7 @@ auto Function::create( param_names.emplace(param_name); } - auto function{std::make_unique(Function{has_return})}; + auto function{std::make_unique(Function{has_return, source_location})}; YSTDLIB_ERROR_HANDLING_TRYV(function->add_child(std::move(name))); if (has_return) { YSTDLIB_ERROR_HANDLING_TRYV(function->add_child(std::move(return_type))); diff --git a/src/spider/tdl/parser/ast/node_impl/Function.hpp b/src/spider/tdl/parser/ast/node_impl/Function.hpp index fed05b48a..2d41dd6ff 100644 --- a/src/spider/tdl/parser/ast/node_impl/Function.hpp +++ b/src/spider/tdl/parser/ast/node_impl/Function.hpp @@ -16,6 +16,7 @@ #include #include #include +#include namespace spider::tdl::parser::ast::node_impl { class Function : public Node { @@ -32,6 +33,7 @@ class Function : public Node { * @param name * @param return_type * @param params + * @param source_location * @return A result containing a unique pointer to a new `Function` instance with the given * name, return type, and parameters on success, or an error code indicating the failure: * - ErrorCodeEnum::DuplicatedParamName if `params` contains duplicated parameter names. @@ -40,7 +42,8 @@ class Function : public Node { [[nodiscard]] static auto create( std::unique_ptr name, std::unique_ptr return_type, - std::vector> params + std::vector> params, + SourceLocation source_location ) -> ystdlib::error_handling::Result>; // Methods implementing `Node` @@ -101,7 +104,9 @@ class Function : public Node { private: // Constructor - explicit Function(bool has_return) : m_has_return{has_return} {} + Function(bool has_return, SourceLocation source_location) + : Node{source_location}, + m_has_return{has_return} {} // Methods [[nodiscard]] auto get_num_non_param_children() const noexcept -> size_t { diff --git a/src/spider/tdl/parser/ast/node_impl/Identifier.hpp b/src/spider/tdl/parser/ast/node_impl/Identifier.hpp index a78b14a61..225010ddf 100644 --- a/src/spider/tdl/parser/ast/node_impl/Identifier.hpp +++ b/src/spider/tdl/parser/ast/node_impl/Identifier.hpp @@ -10,6 +10,7 @@ #include #include +#include namespace spider::tdl::parser::ast::node_impl { class Identifier : public Node { @@ -17,10 +18,11 @@ class Identifier : public Node { // Factory function /** * @param name + * @param source_location * @return A unique pointer to a new `Identifier` instance with the given name. */ - static auto create(std::string name) -> std::unique_ptr { - return std::make_unique(Identifier{std::move(name)}); + static auto create(std::string name, SourceLocation source_location) -> std::unique_ptr { + return std::make_unique(Identifier{std::move(name), source_location}); } // Methods implementing `Node` @@ -32,7 +34,9 @@ class Identifier : public Node { private: // Constructor - explicit Identifier(std::string name) noexcept : m_name{std::move(name)} {} + Identifier(std::string name, SourceLocation source_location) noexcept + : Node{source_location}, + m_name{std::move(name)} {} // Variables std::string m_name; diff --git a/src/spider/tdl/parser/ast/node_impl/NamedVar.cpp b/src/spider/tdl/parser/ast/node_impl/NamedVar.cpp index c988b0f14..83e55d651 100644 --- a/src/spider/tdl/parser/ast/node_impl/NamedVar.cpp +++ b/src/spider/tdl/parser/ast/node_impl/NamedVar.cpp @@ -11,15 +11,19 @@ #include #include #include +#include #include namespace spider::tdl::parser::ast::node_impl { -auto NamedVar::create(std::unique_ptr id, std::unique_ptr type) - -> ystdlib::error_handling::Result> { +auto NamedVar::create( + std::unique_ptr id, + std::unique_ptr type, + SourceLocation source_location +) -> ystdlib::error_handling::Result> { YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type(id.get())); YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type(type.get())); - auto named_var{std::make_unique(NamedVar{})}; + auto named_var{std::make_unique(NamedVar{source_location})}; YSTDLIB_ERROR_HANDLING_TRYV(named_var->add_child(std::move(id))); YSTDLIB_ERROR_HANDLING_TRYV(named_var->add_child(std::move(type))); return named_var; diff --git a/src/spider/tdl/parser/ast/node_impl/NamedVar.hpp b/src/spider/tdl/parser/ast/node_impl/NamedVar.hpp index deed577a2..6e92b7d60 100644 --- a/src/spider/tdl/parser/ast/node_impl/NamedVar.hpp +++ b/src/spider/tdl/parser/ast/node_impl/NamedVar.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace spider::tdl::parser::ast::node_impl { /** @@ -21,11 +22,13 @@ class NamedVar : public Node { /** * @param id * @param type + * @param source_location * @return A result containing a unique pointer to a new `NamedVar` instance with the given name * on success, or an error code indicating the failure: * - Forwards `validate_child_node_type`'s return values. */ - [[nodiscard]] static auto create(std::unique_ptr id, std::unique_ptr type) + [[nodiscard]] static auto + create(std::unique_ptr id, std::unique_ptr type, SourceLocation source_location) -> ystdlib::error_handling::Result>; // Methods implementing `Node` @@ -47,7 +50,7 @@ class NamedVar : public Node { private: // Constructor - NamedVar() = default; + explicit NamedVar(SourceLocation source_location) : Node{source_location} {} }; } // namespace spider::tdl::parser::ast::node_impl diff --git a/src/spider/tdl/parser/ast/node_impl/Namespace.cpp b/src/spider/tdl/parser/ast/node_impl/Namespace.cpp index ee8612f7d..a82300e64 100644 --- a/src/spider/tdl/parser/ast/node_impl/Namespace.cpp +++ b/src/spider/tdl/parser/ast/node_impl/Namespace.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include using spider::tdl::parser::ast::node_impl::Namespace; @@ -40,8 +41,11 @@ auto NamespaceErrorCodeCategory::message(Namespace::ErrorCodeEnum error_enum) co } namespace spider::tdl::parser::ast::node_impl { -auto Namespace::create(std::unique_ptr name, std::vector> functions) - -> ystdlib::error_handling::Result> { +auto Namespace::create( + std::unique_ptr name, + std::vector> functions, + SourceLocation source_location +) -> ystdlib::error_handling::Result> { YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type(name.get())); if (functions.empty()) { @@ -59,7 +63,7 @@ auto Namespace::create(std::unique_ptr name, std::vector(Namespace{})}; + auto function{std::make_unique(Namespace{source_location})}; YSTDLIB_ERROR_HANDLING_TRYV(function->add_child(std::move(name))); for (auto& func : functions) { YSTDLIB_ERROR_HANDLING_TRYV(function->add_child(std::move(func))); diff --git a/src/spider/tdl/parser/ast/node_impl/Namespace.hpp b/src/spider/tdl/parser/ast/node_impl/Namespace.hpp index e9056fc68..2500f8e22 100644 --- a/src/spider/tdl/parser/ast/node_impl/Namespace.hpp +++ b/src/spider/tdl/parser/ast/node_impl/Namespace.hpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace spider::tdl::parser::ast::node_impl { /** @@ -34,6 +35,7 @@ class Namespace : public Node { /** * @param name * @param functions + * @param source_location * @return A result containing a unique pointer to a new `Namespace` instance with the given * name and functions on success, or an error code indicating the failure: * - ErrorCodeEnum::DuplicatedFunctionName if the `functions` contains duplicated function @@ -41,9 +43,11 @@ class Namespace : public Node { * - ErrorCodeEnum::EmptyNamespace if the `functions` is empty. * - Forwards `validate_child_node_type`'s return values. */ - [[nodiscard]] static auto - create(std::unique_ptr name, std::vector> functions) - -> ystdlib::error_handling::Result>; + [[nodiscard]] static auto create( + std::unique_ptr name, + std::vector> functions, + SourceLocation source_location + ) -> ystdlib::error_handling::Result>; // Methods implementing `Node` [[nodiscard]] auto serialize_to_str(size_t indentation_level) const @@ -85,7 +89,7 @@ class Namespace : public Node { private: // Constructor - Namespace() = default; + explicit Namespace(SourceLocation source_location) : Node{source_location} {} }; } // namespace spider::tdl::parser::ast::node_impl diff --git a/src/spider/tdl/parser/ast/node_impl/StructSpec.cpp b/src/spider/tdl/parser/ast/node_impl/StructSpec.cpp index e5b15bffb..f38b380a3 100644 --- a/src/spider/tdl/parser/ast/node_impl/StructSpec.cpp +++ b/src/spider/tdl/parser/ast/node_impl/StructSpec.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include using spider::tdl::parser::ast::node_impl::StructSpec; @@ -41,8 +42,11 @@ auto StructSpecErrorCodeCategory::message(StructSpec::ErrorCodeEnum error_enum) } namespace spider::tdl::parser::ast::node_impl { -auto StructSpec::create(std::unique_ptr name, std::vector> fields) - -> ystdlib::error_handling::Result> { +auto StructSpec::create( + std::unique_ptr name, + std::vector> fields, + SourceLocation source_location +) -> ystdlib::error_handling::Result> { YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type(name.get())); if (fields.empty()) { @@ -60,7 +64,7 @@ auto StructSpec::create(std::unique_ptr name, std::vector(StructSpec{})}; + auto struct_spec{std::make_shared(StructSpec{source_location})}; YSTDLIB_ERROR_HANDLING_TRYV(struct_spec->add_child(std::move(name))); for (auto& field : fields) { YSTDLIB_ERROR_HANDLING_TRYV(struct_spec->add_child(std::move(field))); diff --git a/src/spider/tdl/parser/ast/node_impl/StructSpec.hpp b/src/spider/tdl/parser/ast/node_impl/StructSpec.hpp index 0bd3f8e5b..bdd354be1 100644 --- a/src/spider/tdl/parser/ast/node_impl/StructSpec.hpp +++ b/src/spider/tdl/parser/ast/node_impl/StructSpec.hpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace spider::tdl::parser::ast::node_impl { /** @@ -34,6 +35,7 @@ class StructSpec : public Node { /** * @param name * @param fields + * @param source_location * @return A result containing a shared pointer to a new `StructSpec` instance with the name and * fields on success, or an error code indicating the failure: * - StructSpec::ErrorCodeEnum::DuplicatedFieldName if the `fields` contains duplicated field @@ -41,9 +43,11 @@ class StructSpec : public Node { * - StructSpec::ErrorCodeEnum::EmptyStruct if the `fields` is empty. * - Forwards `validate_child_node_type`'s return values. */ - [[nodiscard]] static auto - create(std::unique_ptr name, std::vector> fields) - -> ystdlib::error_handling::Result>; + [[nodiscard]] static auto create( + std::unique_ptr name, + std::vector> fields, + SourceLocation source_location + ) -> ystdlib::error_handling::Result>; // Methods implementing `Node` [[nodiscard]] auto serialize_to_str(size_t indentation_level) const @@ -85,7 +89,7 @@ class StructSpec : public Node { private: // Constructor - StructSpec() = default; + explicit StructSpec(SourceLocation source_location) : Node{source_location} {} }; } // namespace spider::tdl::parser::ast::node_impl diff --git a/src/spider/tdl/parser/ast/node_impl/Type.hpp b/src/spider/tdl/parser/ast/node_impl/Type.hpp index cd4dbb1fe..e7ff47ca1 100644 --- a/src/spider/tdl/parser/ast/node_impl/Type.hpp +++ b/src/spider/tdl/parser/ast/node_impl/Type.hpp @@ -2,10 +2,15 @@ #define SPIDER_TDL_PARSER_AST_NODE_IMPL_TYPE_HPP #include +#include namespace spider::tdl::parser::ast::node_impl { // Abstract base class for all type nodes in the AST. -class Type : public Node {}; +class Type : public Node { +protected: + // Constructor + explicit Type(SourceLocation source_location) : Node{source_location} {} +}; } // namespace spider::tdl::parser::ast::node_impl #endif // SPIDER_TDL_PARSER_AST_NODE_IMPL_TYPE_HPP diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/Container.hpp b/src/spider/tdl/parser/ast/node_impl/type_impl/Container.hpp index 0b361cde3..33b70dbb5 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/Container.hpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/Container.hpp @@ -2,10 +2,15 @@ #define SPIDER_TDL_PARSER_AST_NODE_IMPL_TYPE_IMPL_CONTAINER_HPP #include +#include namespace spider::tdl::parser::ast::node_impl::type_impl { // Abstract base class for all container type nodes in the AST. -class Container : public Type {}; +class Container : public Type { +protected: + // Constructor + explicit Container(SourceLocation source_location) : Type{source_location} {} +}; } // namespace spider::tdl::parser::ast::node_impl::type_impl #endif // SPIDER_TDL_PARSER_AST_NODE_IMPL_TYPE_IMPL_CONTAINER_HPP diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/Primitive.hpp b/src/spider/tdl/parser/ast/node_impl/type_impl/Primitive.hpp index 76edee4ef..144639c93 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/Primitive.hpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/Primitive.hpp @@ -2,10 +2,15 @@ #define SPIDER_TDL_PARSER_AST_NODE_IMPL_TYPE_IMPL_PRIMITIVE_HPP #include +#include namespace spider::tdl::parser::ast::node_impl::type_impl { // Abstract base class for all primitive type nodes in the AST. -class Primitive : public Type {}; +class Primitive : public Type { +protected: + // Constructor + explicit Primitive(SourceLocation source_location) : Type{source_location} {} +}; } // namespace spider::tdl::parser::ast::node_impl::type_impl #endif // SPIDER_TDL_PARSER_AST_NODE_IMPL_TYPE_IMPL_PRIMITIVE_HPP diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.cpp index ad6b4fa27..a3af9be3f 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include using spider::tdl::parser::ast::node_impl::type_impl::Struct; @@ -37,11 +38,11 @@ auto StructErrorCodeCategory::message(Struct::ErrorCodeEnum error_enum) const -> } namespace spider::tdl::parser::ast::node_impl::type_impl { -auto Struct::create(std::unique_ptr name) +auto Struct::create(std::unique_ptr name, SourceLocation source_location) -> ystdlib::error_handling::Result> { YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type(name.get())); - auto struct_node{std::make_unique(Struct{})}; + auto struct_node{std::make_unique(Struct{source_location})}; YSTDLIB_ERROR_HANDLING_TRYV(struct_node->add_child(std::move(name))); return struct_node; } diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.hpp b/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.hpp index 811d6fa05..6f26e77a4 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.hpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.hpp @@ -14,6 +14,7 @@ #include #include #include +#include namespace spider::tdl::parser::ast::node_impl::type_impl { class Struct : public Type { @@ -30,11 +31,12 @@ class Struct : public Type { // Factory function /** * @param name + * @param source_location * @return A result containing a unique pointer to a new `Struct` instance with the given name * on success, or an error code indicating the failure: * - Forwards `validate_child_node_type`'s return values. */ - [[nodiscard]] static auto create(std::unique_ptr name) + [[nodiscard]] static auto create(std::unique_ptr name, SourceLocation source_location) -> ystdlib::error_handling::Result>; // Methods implementing `Node` @@ -63,7 +65,7 @@ class Struct : public Type { private: // Constructor - Struct() = default; + explicit Struct(SourceLocation source_location) : Type{source_location} {} // Variables std::shared_ptr m_spec; diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.cpp index f26a730f1..ab3e11aab 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.cpp @@ -10,14 +10,15 @@ #include #include +#include #include namespace spider::tdl::parser::ast::node_impl::type_impl::container_impl { -auto List::create(std::unique_ptr element_type) +auto List::create(std::unique_ptr element_type, SourceLocation source_location) -> ystdlib::error_handling::Result> { YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type(element_type.get())); - auto list{std::make_unique(List{})}; + auto list{std::make_unique(List{source_location})}; YSTDLIB_ERROR_HANDLING_TRYV(list->add_child(std::move(element_type))); return list; } diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.hpp b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.hpp index 94d49d4cc..964b3b54e 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.hpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace spider::tdl::parser::ast::node_impl::type_impl::container_impl { class List : public Container { @@ -17,11 +18,13 @@ class List : public Container { // Factory function /** * @param element_type The type of elements in the list. + * @param source_location * @return A result containing a unique pointer to a new `List` instance with the given element * type on success, or an error code indicating the failure: * - Forwards `validate_child_node_type`'s return values. */ - [[nodiscard]] static auto create(std::unique_ptr element_type) + [[nodiscard]] static auto + create(std::unique_ptr element_type, SourceLocation source_location) -> ystdlib::error_handling::Result>; // Methods implementing `Node` @@ -37,7 +40,7 @@ class List : public Container { private: // Constructor - List() = default; + explicit List(SourceLocation source_location) : Container{source_location} {} }; } // namespace spider::tdl::parser::ast::node_impl::type_impl::container_impl diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.cpp index 22bac7482..f518d329c 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include using spider::tdl::parser::ast::node_impl::type_impl::container_impl::Map; @@ -66,8 +67,11 @@ auto is_supported_key_type(Type const* key_type) -> bool { } } // namespace -auto Map::create(std::unique_ptr key_type, std::unique_ptr value_type) - -> ystdlib::error_handling::Result> { +auto Map::create( + std::unique_ptr key_type, + std::unique_ptr value_type, + SourceLocation source_location +) -> ystdlib::error_handling::Result> { YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type(key_type.get())); YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type(value_type.get())); @@ -77,7 +81,7 @@ auto Map::create(std::unique_ptr key_type, std::unique_ptr value_typ return ErrorCode{ErrorCodeEnum::UnsupportedKeyType}; } - auto map{std::make_unique(Map{})}; + auto map{std::make_unique(Map{source_location})}; YSTDLIB_ERROR_HANDLING_TRYV(map->add_child(std::move(key_type))); YSTDLIB_ERROR_HANDLING_TRYV(map->add_child(std::move(value_type))); return map; diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.hpp b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.hpp index 25040c552..c9d0c6459 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.hpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.hpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace spider::tdl::parser::ast::node_impl::type_impl::container_impl { class Map : public Container { @@ -27,14 +28,17 @@ class Map : public Container { /** * @param key_type * @param value_type + * @param source_location * @return A result containing a unique pointer to a new `Map` instance with the given key and * value types on success, or an error code indicating the failure: * - Map::ErrorCodeEnum::UnsupportedKeyType if the `key_type` is not supported. * - Forwards `validate_child_node_type`'s return values. */ - [[nodiscard]] static auto - create(std::unique_ptr key_type, std::unique_ptr value_type) - -> ystdlib::error_handling::Result>; + [[nodiscard]] static auto create( + std::unique_ptr key_type, + std::unique_ptr value_type, + SourceLocation source_location + ) -> ystdlib::error_handling::Result>; // Methods implementing `Node` [[nodiscard]] auto serialize_to_str(size_t indentation_level) const @@ -55,7 +59,7 @@ class Map : public Container { private: // Constructor - Map() = default; + explicit Map(SourceLocation source_location) : Container{source_location} {} }; } // namespace spider::tdl::parser::ast::node_impl::type_impl::container_impl diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp index 2a02bb856..e1423e854 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp @@ -13,16 +13,17 @@ #include #include +#include #include namespace spider::tdl::parser::ast::node_impl::type_impl::container_impl { -auto Tuple::create(std::vector> elements) +auto Tuple::create(std::vector> elements, SourceLocation source_location) -> ystdlib::error_handling::Result> { for (auto const& type : elements) { YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type(type.get())); } - auto tuple{std::make_unique(Tuple{})}; + auto tuple{std::make_unique(Tuple{source_location})}; for (auto& type : elements) { YSTDLIB_ERROR_HANDLING_TRYV(tuple->add_child(std::move(type))); } diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.hpp b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.hpp index 0ed86dc33..1981bf51a 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.hpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.hpp @@ -10,6 +10,7 @@ #include #include +#include namespace spider::tdl::parser::ast::node_impl::type_impl::container_impl { class Tuple : public Container { @@ -17,11 +18,13 @@ class Tuple : public Container { // Factory function /** * @param elements + * @param source_location * @return A result containing a unique pointer to a new `Tuple` instance as a collection of the * given element types, or an error code indicating the failure: * - Forwards `validate_child_node_type`'s return values. */ - [[nodiscard]] static auto create(std::vector> elements) + [[nodiscard]] static auto + create(std::vector> elements, SourceLocation source_location) -> ystdlib::error_handling::Result>; // Methods implementing `Node` @@ -33,7 +36,7 @@ class Tuple : public Container { private: // Constructor - Tuple() = default; + explicit Tuple(SourceLocation source_location) : Container{source_location} {} }; } // namespace spider::tdl::parser::ast::node_impl::type_impl::container_impl diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Bool.hpp b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Bool.hpp index 3afda1814..1599c6e48 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Bool.hpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Bool.hpp @@ -9,16 +9,18 @@ #include #include +#include namespace spider::tdl::parser::ast::node_impl::type_impl::primitive_impl { class Bool : public Primitive { public: // Factory function /** + * @param source_location * @return A unique pointer to a new `Bool` instance. */ - [[nodiscard]] static auto create() -> std::unique_ptr { - return std::make_unique(Bool{}); + [[nodiscard]] static auto create(SourceLocation source_location) -> std::unique_ptr { + return std::make_unique(Bool{source_location}); } // Methods implementing `Node` @@ -27,7 +29,7 @@ class Bool : public Primitive { private: // Constructor - explicit Bool() = default; + explicit Bool(SourceLocation source_location) : Primitive{source_location} {} }; } // namespace spider::tdl::parser::ast::node_impl::type_impl::primitive_impl diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Float.hpp b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Float.hpp index 561708628..77309f07a 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Float.hpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Float.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace spider::tdl::parser::ast::node_impl::type_impl::primitive_impl { class Float : public Primitive { @@ -17,10 +18,12 @@ class Float : public Primitive { // Factory function /** * @param spec + * @param source_location * @return A unique pointer to a new `Float` instance with the given type spec. */ - [[nodiscard]] static auto create(FloatSpec spec) -> std::unique_ptr { - return std::make_unique(Float{spec}); + [[nodiscard]] static auto create(FloatSpec spec, SourceLocation source_location) + -> std::unique_ptr { + return std::make_unique(Float{spec, source_location}); } // Methods implementing `Node` @@ -32,7 +35,9 @@ class Float : public Primitive { private: // Constructor - explicit Float(FloatSpec spec) : m_spec{spec} {} + Float(FloatSpec spec, SourceLocation source_location) + : Primitive{source_location}, + m_spec{spec} {} // Variables FloatSpec m_spec; diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Int.hpp b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Int.hpp index dcc629803..c0a159f7b 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Int.hpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Int.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace spider::tdl::parser::ast::node_impl::type_impl::primitive_impl { class Int : public Primitive { @@ -17,10 +18,12 @@ class Int : public Primitive { // Factory function /** * @param spec + * @param source_location * @return A unique pointer to a new `Int` instance with the given type spec. */ - [[nodiscard]] static auto create(IntSpec spec) -> std::unique_ptr { - return std::make_unique(Int{spec}); + [[nodiscard]] static auto create(IntSpec spec, SourceLocation source_location) + -> std::unique_ptr { + return std::make_unique(Int{spec, source_location}); } // Methods implementing `Node` @@ -32,7 +35,7 @@ class Int : public Primitive { private: // Constructor - explicit Int(IntSpec spec) : m_spec{spec} {} + Int(IntSpec spec, SourceLocation source_location) : Primitive{source_location}, m_spec{spec} {} // Variables IntSpec m_spec; diff --git a/src/spider/tdl/parser/ast/utils.hpp b/src/spider/tdl/parser/ast/utils.hpp index 610be08e5..ea040ed9e 100644 --- a/src/spider/tdl/parser/ast/utils.hpp +++ b/src/spider/tdl/parser/ast/utils.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include diff --git a/tests/tdl/test-parser-ast.cpp b/tests/tdl/test-parser-ast.cpp index 8350ee2bc..82a1dd822 100644 --- a/tests/tdl/test-parser-ast.cpp +++ b/tests/tdl/test-parser-ast.cpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace { /** @@ -51,11 +52,19 @@ create_named_var(std::string_view name, std::unique_ptr std::unique_ptr; +/** + * @return A source location for testing. + */ +[[nodiscard]] auto create_source_location() -> spider::tdl::parser::ast::SourceLocation; + auto create_struct_node(std::string_view name) -> std::unique_ptr { using spider::tdl::parser::ast::node_impl::Identifier; using spider::tdl::parser::ast::node_impl::type_impl::Struct; - auto struct_node_result{Struct::create(Identifier::create(std::string{name}))}; + auto struct_node_result{Struct::create( + Identifier::create(std::string{name}, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(struct_node_result.has_error()); return std::move(struct_node_result.value()); } @@ -66,7 +75,11 @@ auto create_named_var(std::string_view name, std::unique_ptr std::unique_ptr spider::tdl::parser::ast::SourceLocation { + return spider::tdl::parser::ast::SourceLocation{0, 0}; +} + TEST_CASE("test-ast-node", "[tdl][ast][Node]") { using spider::tdl::parser::ast::FloatSpec; using spider::tdl::parser::ast::IntSpec; @@ -109,7 +127,7 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { constexpr std::string_view cTestName{"test_name"}; constexpr std::string_view cSerializedIdentifier{"[Identifier]:test_name"}; - auto const node{Identifier::create(std::string{cTestName})}; + auto const node{Identifier::create(std::string{cTestName}, create_source_location())}; auto const* identifier{dynamic_cast(node.get())}; REQUIRE(nullptr != identifier); @@ -129,7 +147,7 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { std::make_pair(IntSpec::Int64, std::string_view{"[Type[Primitive[Int]]]:int64"}) ); - auto const node{Int::create(int_spec)}; + auto const node{Int::create(int_spec, create_source_location())}; auto const* int_node{dynamic_cast(node.get())}; REQUIRE(nullptr != int_node); @@ -153,7 +171,7 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { ) ); - auto const node{Float::create(float_spec)}; + auto const node{Float::create(float_spec, create_source_location())}; auto const* float_node{dynamic_cast(node.get())}; REQUIRE(nullptr != float_node); @@ -166,7 +184,7 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { } SECTION("Type Bool") { - auto const node{Bool::create()}; + auto const node{Bool::create(create_source_location())}; auto const* bool_node{dynamic_cast(node.get())}; REQUIRE(nullptr != bool_node); @@ -179,9 +197,13 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { } SECTION("List of Map") { - auto map_result{Map::create(Int::create(IntSpec::Int64), Float::create(FloatSpec::Double))}; + auto map_result{Map::create( + Int::create(IntSpec::Int64, create_source_location()), + Float::create(FloatSpec::Double, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(map_result.has_error()); - auto list_result{List::create(std::move(map_result.value()))}; + auto list_result{List::create(std::move(map_result.value()), create_source_location())}; REQUIRE_FALSE(list_result.has_error()); auto const* list_node{dynamic_cast(list_result.value().get())}; REQUIRE(nullptr != list_node); @@ -203,13 +225,20 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { } SECTION("Map of List") { - auto key_list_result{List::create(Int::create(IntSpec::Int8))}; + auto key_list_result{List::create( + Int::create(IntSpec::Int8, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(key_list_result.has_error()); - auto value_list_result{List::create(Float::create(FloatSpec::Float))}; + auto value_list_result{List::create( + Float::create(FloatSpec::Float, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(value_list_result.has_error()); auto map_result{Map::create( std::move(key_list_result.value()), - std::move(value_list_result.value()) + std::move(value_list_result.value()), + create_source_location() )}; REQUIRE_FALSE(map_result.has_error()); auto const* map_node{dynamic_cast(map_result.value().get())}; @@ -235,21 +264,28 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { SECTION("Invalid inputs for container type creation") { constexpr std::string_view cTestName{"test_name"}; - auto list_result{List::create(Identifier::create(std::string{cTestName}))}; + auto list_result{List::create( + Identifier::create(std::string{cTestName}, create_source_location()), + create_source_location() + )}; REQUIRE(list_result.has_error()); REQUIRE(list_result.error() == Node::ErrorCode{Node::ErrorCodeEnum::UnexpectedChildNodeType}); - auto invalid_key_type_map_result{ - Map::create(Identifier::create(std::string{cTestName}), Int::create(IntSpec::Int64)) - }; + auto invalid_key_type_map_result{Map::create( + Identifier::create(std::string{cTestName}, create_source_location()), + Int::create(IntSpec::Int64, create_source_location()), + create_source_location() + )}; REQUIRE(invalid_key_type_map_result.has_error()); REQUIRE(invalid_key_type_map_result.error() == Node::ErrorCode{Node::ErrorCodeEnum::UnexpectedChildNodeType}); - auto invalid_value_type_map_result{ - Map::create(Int::create(IntSpec::Int64), Identifier::create(std::string{cTestName})) - }; + auto invalid_value_type_map_result{Map::create( + Int::create(IntSpec::Int64, create_source_location()), + Identifier::create(std::string{cTestName}, create_source_location()), + create_source_location() + )}; REQUIRE(invalid_value_type_map_result.has_error()); REQUIRE(invalid_value_type_map_result.error() == Node::ErrorCode{Node::ErrorCodeEnum::UnexpectedChildNodeType}); @@ -258,30 +294,43 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { SECTION("Unsupported key types in Map") { // We can't enum all types. Just asserting two types to ensure that the error is propagated // correctly. - auto unsupported_primitive_key_type_map_result{ - Map::create(Float::create(FloatSpec::Float), Int::create(IntSpec::Int64)) - }; + auto unsupported_primitive_key_type_map_result{Map::create( + Float::create(FloatSpec::Float, create_source_location()), + Int::create(IntSpec::Int64, create_source_location()), + create_source_location() + )}; REQUIRE(unsupported_primitive_key_type_map_result.has_error()); REQUIRE(unsupported_primitive_key_type_map_result.error() == Map::ErrorCode{Map::ErrorCodeEnum::UnsupportedKeyType}); - auto list_result{List::create(Int::create(IntSpec::Int64))}; + auto list_result{List::create( + Int::create(IntSpec::Int64, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(list_result.has_error()); - auto unsupported_list_key_type_map_result{ - Map::create(std::move(list_result.value()), Int::create(IntSpec::Int64)) - }; + auto unsupported_list_key_type_map_result{Map::create( + std::move(list_result.value()), + Int::create(IntSpec::Int64, create_source_location()), + create_source_location() + )}; REQUIRE(unsupported_list_key_type_map_result.has_error()); REQUIRE(unsupported_list_key_type_map_result.error() == Map::ErrorCode{Map::ErrorCodeEnum::UnsupportedKeyType}); } SECTION("NamedVar") { - auto id_result{Identifier::create("TestId")}; - auto map_result{Map::create(Int::create(IntSpec::Int64), Float::create(FloatSpec::Double))}; + auto id_result{Identifier::create("TestId", create_source_location())}; + auto map_result{Map::create( + Int::create(IntSpec::Int64, create_source_location()), + Float::create(FloatSpec::Double, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(map_result.has_error()); - auto named_var_result{ - NamedVar::create(std::move(id_result), std::move(map_result.value())) - }; + auto named_var_result{NamedVar::create( + std::move(id_result), + std::move(map_result.value()), + create_source_location() + )}; REQUIRE_FALSE(named_var_result.has_error()); auto const* named_var_node{dynamic_cast(named_var_result.value().get())}; REQUIRE(nullptr != named_var_node); @@ -306,7 +355,7 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { SECTION("Tuple") { SECTION("Empty") { - auto empty_tuple_result{Tuple::create({})}; + auto empty_tuple_result{Tuple::create({}, create_source_location())}; REQUIRE_FALSE(empty_tuple_result.has_error()); auto const* tuple_node{dynamic_cast(empty_tuple_result.value().get())}; REQUIRE(nullptr != tuple_node); @@ -320,17 +369,19 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { } SECTION("Tuple with elements") { - auto int_node{Int::create(IntSpec::Int64)}; - auto float_node{Float::create(FloatSpec::Double)}; - auto map_result{ - Map::create(Int::create(IntSpec::Int64), Float::create(FloatSpec::Double)) - }; + auto int_node{Int::create(IntSpec::Int64, create_source_location())}; + auto float_node{Float::create(FloatSpec::Double, create_source_location())}; + auto map_result{Map::create( + Int::create(IntSpec::Int64, create_source_location()), + Float::create(FloatSpec::Double, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(map_result.has_error()); std::vector> elements; elements.emplace_back(std::move(int_node)); elements.emplace_back(std::move(float_node)); elements.emplace_back(std::move(map_result.value())); - auto tuple_result{Tuple::create(std::move(elements))}; + auto tuple_result{Tuple::create(std::move(elements), create_source_location())}; REQUIRE_FALSE(tuple_result.has_error()); auto const* tuple_node{dynamic_cast(tuple_result.value().get())}; REQUIRE(nullptr != tuple_node); @@ -359,19 +410,29 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { SECTION("StructSpec") { constexpr std::string_view cTestStructName{"TestStruct"}; - auto int_field_result{ - NamedVar::create(Identifier::create("m_int"), Int::create(IntSpec::Int64)) - }; + auto int_field_result{NamedVar::create( + Identifier::create("m_int", create_source_location()), + Int::create(IntSpec::Int64, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(int_field_result.has_error()); - auto float_field_result{ - NamedVar::create(Identifier::create("m_float"), Float::create(FloatSpec::Double)) - }; + auto float_field_result{NamedVar::create( + Identifier::create("m_float", create_source_location()), + Float::create(FloatSpec::Double, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(float_field_result.has_error()); - auto map_result{Map::create(Int::create(IntSpec::Int64), Float::create(FloatSpec::Double))}; + auto map_result{Map::create( + Int::create(IntSpec::Int64, create_source_location()), + Float::create(FloatSpec::Double, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(map_result.has_error()); - auto map_field_result{ - NamedVar::create(Identifier::create("m_map"), std::move(map_result.value())) - }; + auto map_field_result{NamedVar::create( + Identifier::create("m_map", create_source_location()), + std::move(map_result.value()), + create_source_location() + )}; REQUIRE_FALSE(map_field_result.has_error()); std::vector> fields; fields.emplace_back(std::move(int_field_result.value())); @@ -380,8 +441,9 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { SECTION("Basic") { auto struct_spec_result{StructSpec::create( - Identifier::create(std::string{cTestStructName}), - std::move(fields) + Identifier::create(std::string{cTestStructName}, create_source_location()), + std::move(fields), + create_source_location() )}; REQUIRE_FALSE(struct_spec_result.has_error()); auto const* struct_spec_node{ @@ -424,9 +486,11 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { } SECTION("Fields with duplicated name") { - auto duplicated_int_field_result{ - NamedVar::create(Identifier::create("m_int"), Int::create(IntSpec::Int64)) - }; + auto duplicated_int_field_result{NamedVar::create( + Identifier::create("m_int", create_source_location()), + Int::create(IntSpec::Int64, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(duplicated_int_field_result.has_error()); // The `SECTION` execution model ensures that objects are not reused across parallel // `SECTION`s. Variables in different `SECTION`s are independent. Suppress warnings @@ -434,8 +498,9 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { // NOLINTNEXTLINE(bugprone-use-after-move) fields.emplace_back(std::move(duplicated_int_field_result.value())); auto struct_spec_result{StructSpec::create( - Identifier::create(std::string{cTestStructName}), - std::move(fields) + Identifier::create(std::string{cTestStructName}, create_source_location()), + std::move(fields), + create_source_location() )}; REQUIRE(struct_spec_result.has_error()); REQUIRE(struct_spec_result.error() @@ -443,9 +508,11 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { } SECTION("Empty") { - auto struct_spec_result{ - StructSpec::create(Identifier::create(std::string{cTestStructName}), {}) - }; + auto struct_spec_result{StructSpec::create( + Identifier::create(std::string{cTestStructName}, create_source_location()), + {}, + create_source_location() + )}; REQUIRE(struct_spec_result.has_error()); REQUIRE(struct_spec_result.error() == StructSpec::ErrorCode{StructSpec::ErrorCodeEnum::EmptyStruct}); @@ -456,22 +523,28 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { constexpr std::string_view cTestStructName{"TestStruct"}; // Create a `StructSpec` - auto int_field_result{ - NamedVar::create(Identifier::create("m_int"), Int::create(IntSpec::Int64)) - }; + auto int_field_result{NamedVar::create( + Identifier::create("m_int", create_source_location()), + Int::create(IntSpec::Int64, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(int_field_result.has_error()); std::vector> fields; fields.emplace_back(std::move(int_field_result.value())); auto struct_spec_result{StructSpec::create( - Identifier::create(std::string{cTestStructName}), - std::move(fields) + Identifier::create(std::string{cTestStructName}, create_source_location()), + std::move(fields), + create_source_location() )}; REQUIRE_FALSE(struct_spec_result.has_error()); REQUIRE(nullptr != dynamic_cast(struct_spec_result.value().get())); SECTION("Struct with StructSpec") { - auto struct_result{Struct::create(Identifier::create(std::string{cTestStructName}))}; + auto struct_result{Struct::create( + Identifier::create(std::string{cTestStructName}, create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(struct_result.has_error()); auto* struct_node{dynamic_cast(struct_result.value().get())}; REQUIRE(nullptr != struct_node); @@ -506,7 +579,10 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { } SECTION("Set spec to a wrong Struct") { - auto struct_result{Struct::create(Identifier::create("WrongStruct"))}; + auto struct_result{Struct::create( + Identifier::create("WrongStruct", create_source_location()), + create_source_location() + )}; REQUIRE_FALSE(struct_result.has_error()); auto* struct_node{dynamic_cast(struct_result.value().get())}; REQUIRE(nullptr != struct_node); @@ -521,25 +597,32 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { constexpr std::string_view cTestFuncName{"test_function"}; constexpr std::string_view cTestStructName{"TestStruct"}; - auto function_name{Identifier::create(std::string{cTestFuncName})}; + auto function_name{ + Identifier::create(std::string{cTestFuncName}, create_source_location()) + }; std::vector> tuple_elements; - tuple_elements.emplace_back(Int::create(IntSpec::Int64)); + tuple_elements.emplace_back(Int::create(IntSpec::Int64, create_source_location())); tuple_elements.emplace_back(create_struct_node(cTestStructName)); - tuple_elements.emplace_back(Bool::create()); + tuple_elements.emplace_back(Bool::create(create_source_location())); - auto return_tuple_result{Tuple::create(std::move(tuple_elements))}; + auto return_tuple_result{ + Tuple::create(std::move(tuple_elements), create_source_location()) + }; REQUIRE_FALSE(return_tuple_result.has_error()); std::vector> parameters; - parameters.emplace_back(create_named_var("param_0", Int::create(IntSpec::Int64))); + parameters.emplace_back( + create_named_var("param_0", Int::create(IntSpec::Int64, create_source_location())) + ); parameters.emplace_back(create_named_var("param_1", create_struct_node(cTestStructName))); SECTION("Basic") { auto func_result{Function::create( std::move(function_name), std::move(return_tuple_result.value()), - std::move(parameters) + std::move(parameters), + create_source_location() )}; REQUIRE_FALSE(func_result.has_error()); auto const* func_node{dynamic_cast(func_result.value().get())}; @@ -587,14 +670,21 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { // The `SECTION` execution model ensures that objects are not reused across parallel // `SECTION`s. Variables in different `SECTION`s are independent. Suppress warnings // about potential use-after-move, as this is intentional. - // NOLINTNEXTLINE(bugprone-use-after-move) - auto func_result{Function::create(std::move(function_name), {}, std::move(parameters))}; + // NOLINTBEGIN(bugprone-use-after-move) + auto func_result{Function::create( + std::move(function_name), + {}, + std::move(parameters), + create_source_location() + )}; + // NOLINTEND(bugprone-use-after-move) REQUIRE_FALSE(func_result.has_error()); auto const* func_node{dynamic_cast(func_result.value().get())}; REQUIRE(nullptr != func_node); REQUIRE(func_node->get_num_children() == 3); REQUIRE(func_node->get_num_params() == 2); + REQUIRE(func_node->get_name() == cTestFuncName); REQUIRE(nullptr == func_node->get_return_type()); @@ -631,7 +721,8 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { auto func_result{Function::create( std::move(function_name), std::move(return_tuple_result.value()), - {} + {}, + create_source_location() )}; // NOLINTEND(bugprone-use-after-move) REQUIRE_FALSE(func_result.has_error()); @@ -668,7 +759,9 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { // `SECTION`s. Variables in different `SECTION`s are independent. Suppress warnings // about potential use-after-move, as this is intentional. // NOLINTNEXTLINE(bugprone-use-after-move) - auto func_result{Function::create(std::move(function_name), {}, {})}; + auto func_result{ + Function::create(std::move(function_name), {}, {}, create_source_location()) + }; REQUIRE_FALSE(func_result.has_error()); auto const* func_node{dynamic_cast(func_result.value().get())}; REQUIRE(nullptr != func_node); @@ -693,8 +786,16 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { // `SECTION`s. Variables in different `SECTION`s are independent. Suppress warnings // about potential use-after-move, as this is intentional. // NOLINTNEXTLINE(bugprone-use-after-move) - parameters.emplace_back(create_named_var("param_0", Int::create(IntSpec::Int64))); - auto func_result{Function::create(std::move(function_name), {}, std::move(parameters))}; + parameters.emplace_back(create_named_var( + "param_0", + Int::create(IntSpec::Int64, create_source_location()) + )); + auto func_result{Function::create( + std::move(function_name), + {}, + std::move(parameters), + create_source_location() + )}; REQUIRE(func_result.has_error()); REQUIRE(func_result.error() == Function::ErrorCode{Function::ErrorCodeEnum::DuplicatedParamName}); @@ -710,8 +811,9 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { SECTION("Basic") { auto namespace_result{Namespace::create( - Identifier::create(std::string{cTestNamespaceName}), - std::move(functions) + Identifier::create(std::string{cTestNamespaceName}, create_source_location()), + std::move(functions), + create_source_location() )}; REQUIRE_FALSE(namespace_result.has_error()); auto const* namespace_node{ @@ -744,9 +846,11 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { } SECTION("Empty") { - auto namespace_result{ - Namespace::create(Identifier::create(std::string{cTestNamespaceName}), {}) - }; + auto namespace_result{Namespace::create( + Identifier::create(std::string{cTestNamespaceName}, create_source_location()), + {}, + create_source_location() + )}; REQUIRE(namespace_result.has_error()); REQUIRE(namespace_result.error() == Namespace::ErrorCode{Namespace::ErrorCodeEnum::EmptyNamespace}); @@ -759,8 +863,9 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { // NOLINTBEGIN(bugprone-use-after-move) functions.emplace_back(create_func("func_0")); auto namespace_result{Namespace::create( - Identifier::create(std::string{cTestNamespaceName}), - std::move(functions) + Identifier::create(std::string{cTestNamespaceName}, create_source_location()), + std::move(functions), + create_source_location() )}; // NOLINTEND(bugprone-use-after-move) REQUIRE(namespace_result.has_error());