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
2 changes: 2 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 @@ -25,6 +25,8 @@ namespace pl::core::ast {
}
} else if (auto typeNode = dynamic_cast<ASTNodeTypeApplication*>(templateArgument.get()); typeNode != nullptr) {
templateTypeString += fmt::format("{}, ", typeNode->getTypeName());
} else {
templateTypeString += ", ";
}
}

Expand Down
19 changes: 10 additions & 9 deletions lib/source/pl/core/ast/ast_node_type_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ namespace pl::core::ast {
};

std::vector<std::shared_ptr<ptrn::Pattern>> patterns;
if (this->getOperator() == Token::Operator::TypeNameOf) {
if (auto typeApp = dynamic_cast<ASTNodeTypeApplication*>(this->m_expression.get()); typeApp != nullptr) {
auto evaluatedType = typeApp->evaluate(evaluator);
result = dynamic_cast<ASTNodeTypeApplication*>(evaluatedType.get())->getTypeName();
return std::unique_ptr<ASTNode>(new ASTNodeLiteral(result));
}
}

this->m_expression->createPatterns(evaluator, patterns);
if (patterns.empty())
err::E0005.throwError("'auto' can only be used with parameters.", { }, this->getLocation());
Expand All @@ -56,16 +64,9 @@ namespace pl::core::ast {
case Token::Operator::SizeOf:
result = u128(pattern->getSize());
break;
case Token::Operator::TypeNameOf: {
if (auto typeApp = dynamic_cast<ASTNodeTypeApplication*>(this->m_expression.get()); typeApp != nullptr) {
auto evaluatedType = typeApp->evaluate(evaluator);
result = dynamic_cast<ASTNodeTypeApplication*>(evaluatedType.get())->getTypeName();
} else {
result = pattern->getTypeName();
}

case Token::Operator::TypeNameOf:
result = pattern->getTypeName();
break;
}
default:
err::E0001.throwError("Invalid type operation.", {}, this->getLocation());
}
Expand Down
7 changes: 4 additions & 3 deletions lib/source/pl/core/ast/ast_node_variable_decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ namespace pl::core::ast {

auto startOffset = evaluator->getBitwiseReadOffset();

evaluator->createVariable(this->getName(), this->getType().get(), { }, this->m_outVariable, false, false, this->m_constant);
auto evaluatedType = std::unique_ptr<ASTNodeTypeApplication>(dynamic_cast<ast::ASTNodeTypeApplication*>(this->getType()->evaluate(evaluator).release()));
evaluator->createVariable(this->getName(), evaluatedType.get(), { }, this->m_outVariable, false, false, this->m_constant);
auto &variable = evaluator->getScope(0).scope->back();

std::vector<std::shared_ptr<ptrn::Pattern>> initValues;
if (this->m_placementOffset == nullptr) {
evaluator->pushSectionId(ptrn::Pattern::InstantiationSectionId);
this->getType()->createPatterns(evaluator, initValues);
evaluatedType->createPatterns(evaluator, initValues);
evaluator->popSectionId();
} else {
evaluator->pushSectionId(this->m_placementSection == nullptr ? ptrn::Pattern::MainSectionId : evaluator->getSectionId());
Expand All @@ -143,7 +144,7 @@ namespace pl::core::ast {
ON_SCOPE_EXIT { evaluator->setBitwiseReadOffset(currOffset); };

evaluator->setReadOffset(u64(this->evaluatePlacementOffset(evaluator)));
this->getType()->createPatterns(evaluator, initValues);
evaluatedType->createPatterns(evaluator, initValues);
evaluator->popSectionId();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/source/pl/core/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ namespace pl::core {
size_t index = 0;
for (const auto &templateParameter : this->m_currTemplateType.front()->getTemplateParameters()) {
if (const auto templateType = dynamic_cast<ast::ASTNodeTemplateParameter*>(templateParameter.get()); templateType != nullptr){
if (templateType->getName().get() == baseTypeName) {
if (templateType->getName().get() == baseTypeName && templateType->isType()) {
auto type = create<ast::ASTNodeTypeApplication>(nullptr);
type->setTemplateParameterIndex(index);
return type;
Expand Down
23 changes: 23 additions & 0 deletions tests/include/test_patterns/test_pattern_typenameof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,35 @@ namespace pl::test {
};

u32 P = 16;
A q @ 0;
B<u32, q> r;
TypeName<u32, "u32"> a @ 0;
TypeName<A, "A"> b @ 0;
TypeName<B<u32, 2>, "B<u32, 2>"> c @ 0;
TypeName<B<B<u32, P>, 2>, "B<B<u32, 16>, 2>"> d @ 0;
TypeName<TypeName<A, "A"> , "TypeName<A, \"A\">"> e @ 0;
TypeName<B<u32, q>, "B<u32, A{ }>"> f @ 0;
std::assert(typenameof(B<B<u32, P>, 2>) == "B<B<u32, 16>, 2>", "type name should match");
std::assert(typenameof(B<u32, q>) == "B<u32, A{ }>", "type name should match");

B<u32, P> t;
std::assert(typenameof(t) == "B<u32, 16>", "type name should match");

struct S {
u32 e = 16;
B<u32, e> a = t;
str typename = typenameof(a);
};

S s @ 0;
std::assert(s.typename == "B<u32, 16>", "type name should match");

struct U {
u8 size;
u32 value[size];
};

std::assert(typenameof(U) == "U", "type name should match");
)";
}
};
Expand Down
Loading