Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/source/pl/core/ast/ast_node_type_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,20 @@ namespace pl::core::ast {

ast::ASTNode* type = this->getType().get();
if (auto typDecl = dynamic_cast<ast::ASTNodeTypeDecl*>(type); typDecl != nullptr) {
std::vector<std::unique_ptr<ASTNode>> templateArgs(this->m_templateArguments.size());
for (size_t i = 0; i < this->m_templateArguments.size(); i++) {
auto &templateArgument = this->m_templateArguments[i];
if (auto typeApp = dynamic_cast<ast::ASTNodeTypeApplication*>(templateArgument.get()); typeApp != nullptr) {
templateArgs[i] = templateArgument->evaluate(evaluator);
}
}

evaluator->setCurrentTemplateArguments(std::move(templateArgs));
return typDecl->getTypeDefinition(evaluator);
} else if(auto builtinType = dynamic_cast<ast::ASTNodeBuiltinType*>(type); builtinType != nullptr) {
return builtinType;
} else if(auto typeApp = dynamic_cast<ast::ASTNodeTypeApplication*>(type); typeApp != nullptr) {
return typeApp->getTypeDefinition(evaluator);
}

return nullptr;
Expand Down
25 changes: 23 additions & 2 deletions lib/source/pl/core/ast/ast_node_type_decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,31 @@ namespace pl::core::ast {
const ASTNode* ASTNodeTypeDecl::getTypeDefinition(Evaluator *evaluator) const {
if (m_type == nullptr)
err::E0004.throwError(fmt::format("Cannot use incomplete type '{}' before it has been defined.", this->m_name), "Try defining this type further up in your code before trying to instantiate it.", this->getLocation());
else if (auto typeApp = dynamic_cast<ASTNodeTypeApplication*>(m_type.get()); typeApp != nullptr)
else if (auto typeApp = dynamic_cast<ASTNodeTypeApplication*>(m_type.get()); typeApp != nullptr) {
evaluator->pushTypeTemplateParameters();
ON_SCOPE_EXIT {
evaluator->popTypeTemplateParameters();
};

auto& templateArguments = evaluator->getCurrentTemplateArguments();
std::vector<std::shared_ptr<ptrn::Pattern>> templatePatterns;
for (size_t i = 0; i < this->m_templateParameters.size(); i++) {
auto &templateParameter = this->m_templateParameters[i];

if (i >= templateArguments.size()) {
break;
}

if (templateParameter->isType()) {
auto& argument = templateArguments[i];
evaluator->getTypeTemplateParameters().emplace_back(std::move(argument));
}
}

return typeApp->getTypeDefinition(evaluator);
else
} else {
return m_type.get();
}
}

[[nodiscard]] const std::string ASTNodeTypeDecl::getTypeName() const {
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(AVAILABLE_TESTS
TemplateParametersScope
TypeNameOf
CustomBuiltInType
Using
)


Expand Down
51 changes: 51 additions & 0 deletions tests/include/test_patterns/test_pattern_using.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include "test_pattern.hpp"

namespace pl::test {

class TestPatternUsing : public TestPattern {
public:
TestPatternUsing(core::Evaluator *evaluator) : TestPattern(evaluator, "Using") {
}
~TestPatternUsing() override = default;

[[nodiscard]] std::string getSourceCode() const override {
return R"(
using US<T> = T;
US<u32> u;
US<u32> v = 64;
u = 64;
std::assert(u == 64 && v == 64, "u,v should be 64");

using UST<T> = US<T>;
UST<u32> ust;
UST<u32> vst = 16;
ust = 16;
std::assert(ust == 16 && vst == 16, "ust,vst should be 16");

struct USS<T> {
US<T> us = 16;
};
USS<u32> uss;
std::assert(uss.us == 16, "us should be 16");

USS<US<u32>> ussus;
std::assert(ussus.us == 16, "ussus should be 16");

US<u8> us2[2];
US<u8> us3[2] @ 0;
std::assert(us3[0] == 137, "us3[0] should be 137");

namespace A {
using US<T> = T;
}
A::US<u8> us4;
A::US<u8> us5 @ 0;
us4 = us5;
std::assert(us5 == 137 && us4 == 137, "us4, us5 should be 137");
)";
}
};

}
2 changes: 2 additions & 0 deletions tests/source/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "test_patterns/test_pattern_template_parameters_scope.hpp"
#include "test_patterns/test_pattern_typenameof.hpp"
#include "test_patterns/test_pattern_custom_builtin_type.hpp"
#include "test_patterns/test_pattern_using.hpp"

static pl::core::Evaluator s_evaluator;

Expand Down Expand Up @@ -65,4 +66,5 @@ std::array Tests = {
TEST(TemplateParametersScope),
TEST(TypeNameOf),
TEST(CustomBuiltinType),
TEST(Using),
};
Loading