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
50 changes: 42 additions & 8 deletions src/parser/cxx/binder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

// cxx
#include <cxx/ast.h>
#include <cxx/ast_interpreter.h>
#include <cxx/control.h>
#include <cxx/decl.h>
#include <cxx/decl_specs.h>
Expand Down Expand Up @@ -551,6 +552,47 @@ void Binder::complete(LambdaExpressionAST* ast) {

auto parentScope = ast->symbol->enclosingScope();
parentScope->addSymbol(ast->symbol);

const Type* returnType = control()->getAutoType();
std::vector<const Type*> parameterTypes;
bool isVariadic = false;

if (auto params = ast->parameterDeclarationClause) {
for (auto it = params->parameterDeclarationList; it; it = it->next) {
auto paramType = it->value->type;

if (control()->is_void(paramType)) {
continue;
}

parameterTypes.push_back(paramType);
}

isVariadic = params->isVariadic;
}

bool isNoexcept = false;

if (auto noexceptSpec =
ast_cast<NoexceptSpecifierAST>(ast->exceptionSpecifier)) {
if (!noexceptSpec->expression) {
isNoexcept = true;
} else {
ASTInterpreter sem{unit_};
auto value = sem.evaluate(noexceptSpec->expression);
if (value.has_value()) {
isNoexcept = sem.toBool(*value).value_or(false);
}
}
}

if (ast->trailingReturnType && ast->trailingReturnType->typeId) {
returnType = ast->trailingReturnType->typeId->type;
}

auto type = control()->getFunctionType(returnType, std::move(parameterTypes),
isVariadic, {}, {}, isNoexcept);
ast->symbol->setType(type);
}

void Binder::bind(ParameterDeclarationClauseAST* ast) {
Expand Down Expand Up @@ -600,14 +642,6 @@ auto Binder::declareFunction(DeclaratorAST* declarator, const Decl& decl)

auto functionSymbol = control()->newFunctionSymbol(scope(), decl.location());

// todo: scope chain fixup should be handled elsewhere, not here.
if (auto proto = getFunctionPrototype(declarator)) {
if (proto->parameterDeclarationClause) {
proto->parameterDeclarationClause->functionParametersSymbol->scope()
->setParent(functionSymbol->scope());
}
}

if (is_parsing_c()) {
functionSymbol->setHasCxxLinkage(false);
}
Expand Down
7 changes: 7 additions & 0 deletions src/parser/cxx/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ void Scope::addSymbol(Symbol* symbol) {
}
}

if (auto params = symbol_cast<FunctionParametersSymbol>(symbol);
params && owner_) {
if (owner_->isFunction() || owner_->isLambda()) {
params->scope()->setParent(this);
}
}

symbol->setEnclosingScope(this);
symbols_.push_back(symbol);

Expand Down
15 changes: 5 additions & 10 deletions src/parser/cxx/type_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2264,18 +2264,13 @@ auto TypeChecker::Visitor::check_pseudo_destructor_access(
void TypeChecker::checkReturnStatement(ReturnStatementAST* ast) {
const Type* targetType = nullptr;
for (auto current = scope_; current; current = current->parent()) {
if (auto function = symbol_cast<FunctionSymbol>(current->owner())) {
if (auto functionType = type_cast<FunctionType>(function->type())) {
targetType = functionType->returnType();
}
break;
}

if (auto lambda = symbol_cast<LambdaSymbol>(current->owner())) {
if (auto functionType = type_cast<FunctionType>(lambda->type())) {
auto owner = current->owner();
if (!owner) continue;
if (owner->isFunction() || owner->isLambda()) {
if (auto functionType = type_cast<FunctionType>(owner->type())) {
targetType = functionType->returnType();
break;
}
break;
}
}

Expand Down