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
1 change: 1 addition & 0 deletions src/mlir/cxx/mlir/cxx_dialect_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ auto cxx::lowerToMLIR(mlir::ModuleOp module) -> mlir::LogicalResult {
pm.addPass(cxx::createLowerToLLVMPass());

if (failed(pm.run(module))) {
module.print(llvm::errs());
return mlir::failure();
}

Expand Down
8 changes: 8 additions & 0 deletions src/parser/cxx/binder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,14 @@ 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
38 changes: 38 additions & 0 deletions src/parser/cxx/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3534,6 +3534,7 @@ auto Parser::parse_if_statement(StatementAST*& yyast) -> bool {
parse_init_statement(ast->initializer);

parse_condition(ast->condition, ExprContext{});
check_bool_condition(ast->condition);

expect(TokenKind::T_RPAREN, ast->rparenLoc);

Expand Down Expand Up @@ -3567,6 +3568,7 @@ auto Parser::parse_switch_statement(StatementAST*& yyast) -> bool {
parse_init_statement(ast->initializer);

parse_condition(ast->condition, ExprContext{});
check_integral_condition(ast->condition);

expect(TokenKind::T_RPAREN, ast->rparenLoc);

Expand Down Expand Up @@ -3594,6 +3596,7 @@ auto Parser::parse_while_statement(StatementAST*& yyast) -> bool {
expect(TokenKind::T_LPAREN, ast->lparenLoc);

parse_condition(ast->condition, ExprContext{});
check_bool_condition(ast->condition);

expect(TokenKind::T_RPAREN, ast->rparenLoc);

Expand Down Expand Up @@ -3623,6 +3626,7 @@ auto Parser::parse_do_statement(StatementAST*& yyast) -> bool {
expect(TokenKind::T_LPAREN, ast->lparenLoc);

parse_expression(ast->expression, ExprContext{});
check_bool_condition(ast->expression);

expect(TokenKind::T_RPAREN, ast->rparenLoc);

Expand Down Expand Up @@ -3694,6 +3698,7 @@ auto Parser::parse_for_statement(StatementAST*& yyast) -> bool {

if (!match(TokenKind::T_SEMICOLON, ast->semicolonLoc)) {
parse_condition(ast->condition, ExprContext{});
check_bool_condition(ast->condition);
expect(TokenKind::T_SEMICOLON, ast->semicolonLoc);
}

Expand Down Expand Up @@ -4047,6 +4052,7 @@ auto Parser::parse_empty_or_attribute_declaration(
auto Parser::parse_notypespec_function_definition(
DeclarationAST*& yyast, List<AttributeSpecifierAST*>* atributes,
BindingContext ctx) -> bool {
if (!is_parsing_cxx()) return false;
if (!context_allows_function_definition(ctx)) return false;

LookaheadParser lookahead{this};
Expand Down Expand Up @@ -4110,6 +4116,8 @@ auto Parser::parse_structured_binding(DeclarationAST*& yyast,
List<SpecifierAST*>* declSpecifierList,
const DeclSpecs& specs,
BindingContext ctx) -> bool {
if (!is_parsing_cxx()) return false;

LookaheadParser lookahead{this};

if (!context_allows_structured_bindings(ctx)) {
Expand Down Expand Up @@ -5507,6 +5515,11 @@ auto Parser::parse_init_declarator(InitDeclaratorAST*& yyast,
}
}

if (ast->initializer && !control()->is_reference(symbol->type())) {
(void)implicit_conversion(ast->initializer,
control()->remove_cv(symbol->type()));
}

return true;
}

Expand Down Expand Up @@ -9821,6 +9834,31 @@ void Parser::check(StatementAST* ast) {
check.checkReturnStatement(returnStatement);
}

void Parser::check_bool_condition(ExpressionAST*& ast) {
if (binder_.inTemplate()) return;
TypeChecker check{unit};
check.setScope(scope());
check.setReportErrors(config().checkTypes);
check.check_bool_condition(ast);
}

void Parser::check_integral_condition(ExpressionAST*& ast) {
if (binder_.inTemplate()) return;
TypeChecker check{unit};
check.setScope(scope());
check.setReportErrors(config().checkTypes);
check.check_integral_condition(ast);
}

auto Parser::implicit_conversion(ExpressionAST*& yyast, const Type* targetType)
-> bool {
if (binder_.inTemplate()) return true;
TypeChecker check{unit};
check.setScope(scope());
check.setReportErrors(config().checkTypes);
return check.implicit_conversion(yyast, targetType);
}

auto Parser::getFunction(Scope* scope, const Name* name, const Type* type)
-> FunctionSymbol* {
auto parentScope = scope;
Expand Down
6 changes: 6 additions & 0 deletions src/parser/cxx/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,12 @@ class Parser final {
void check(ExpressionAST* ast);
void check(StatementAST* ast);

void check_bool_condition(ExpressionAST*& ast);
void check_integral_condition(ExpressionAST*& ast);

[[nodiscard]] auto implicit_conversion(ExpressionAST*& yyast,
const Type* targetType) -> bool;

// lookup

[[nodiscard]] auto getFunction(Scope* scope, const Name* name,
Expand Down
23 changes: 21 additions & 2 deletions src/parser/cxx/type_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1726,14 +1726,13 @@ auto TypeChecker::Visitor::implicit_conversion(ExpressionAST*& expr,
if (!expr || !expr->type) return false;
if (!destinationType) return false;

if (control()->is_same(expr->type, destinationType)) return true;

auto savedValueCategory = expr->valueCategory;
auto savedExpr = expr;
auto didConvert = ensure_prvalue(expr);

adjust_cv(expr);

if (control()->is_same(expr->type, destinationType)) return true;
if (integral_promotion(expr)) return true;
if (floating_point_promotion(expr)) return true;
if (integral_conversion(expr, destinationType)) return true;
Expand Down Expand Up @@ -2286,4 +2285,24 @@ void TypeChecker::checkReturnStatement(ReturnStatementAST* ast) {
(void)visitor.implicit_conversion(ast->expression, targetType);
}

auto TypeChecker::implicit_conversion(ExpressionAST*& yyast,
const Type* targetType) -> bool {
Visitor visitor{*this};
return visitor.implicit_conversion(yyast, targetType);
}

void TypeChecker::check_bool_condition(ExpressionAST*& expr) {
Visitor visitor{*this};
(void)visitor.implicit_conversion(expr, unit_->control()->getBoolType());
}

void TypeChecker::check_integral_condition(ExpressionAST*& expr) {
auto control = unit_->control();
if (!control->is_integral(expr->type) && !control->is_enum(expr->type))
return;
Visitor visitor{*this};
(void)visitor.lvalue_to_rvalue_conversion(expr);
visitor.adjust_cv(expr);
}

} // namespace cxx
7 changes: 7 additions & 0 deletions src/parser/cxx/type_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@ class TypeChecker {

void check(ExpressionAST* ast);

// todo: remove
void checkReturnStatement(ReturnStatementAST* ast);

void check_bool_condition(ExpressionAST*& ast);
void check_integral_condition(ExpressionAST*& ast);

[[nodiscard]] auto implicit_conversion(ExpressionAST*& expr,
const Type* targetType) -> bool;

private:
struct Visitor;

Expand Down
8 changes: 5 additions & 3 deletions tests/unit_tests/ast/asm_declaration_01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ end:;
// CHECK-NEXT: core-declarator: id-declarator
// CHECK-NEXT: unqualified-id: name-id
// CHECK-NEXT: identifier: p
// CHECK-NEXT: initializer: equal-initializer [prvalue decltype(nullptr)]
// CHECK-NEXT: expression: nullptr-literal-expression [prvalue decltype(nullptr)]
// CHECK-NEXT: literal: nullptr
// CHECK-NEXT: initializer: implicit-cast-expression [prvalue char*]
// CHECK-NEXT: cast-kind: pointer-conversion
// CHECK-NEXT: expression: equal-initializer [prvalue decltype(nullptr)]
// CHECK-NEXT: expression: nullptr-literal-expression [prvalue decltype(nullptr)]
// CHECK-NEXT: literal: nullptr
// CHECK-NEXT: declaration-statement
// CHECK-NEXT: declaration: asm-declaration
// CHECK-NEXT: literal: "nop"
Expand Down
8 changes: 5 additions & 3 deletions tests/unit_tests/ast/deduce_this_01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct object {
// CHECK-NEXT: statement: compound-statement
// CHECK-NEXT: statement-list
// CHECK-NEXT: return-statement
// CHECK-NEXT: expression: id-expression [lvalue auto]
// CHECK-NEXT: unqualified-id: name-id
// CHECK-NEXT: identifier: self
// CHECK-NEXT: expression: implicit-cast-expression [prvalue auto]
// CHECK-NEXT: cast-kind: lvalue-to-rvalue-conversion
// CHECK-NEXT: expression: id-expression [lvalue auto]
// CHECK-NEXT: unqualified-id: name-id
// CHECK-NEXT: identifier: self
10 changes: 4 additions & 6 deletions tests/unit_tests/ast/function_to_pointer_conv_01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ auto main() -> int {
// CHECK-NEXT: unqualified-id: name-id
// CHECK-NEXT: identifier: p
// CHECK-NEXT: right-expression: implicit-cast-expression [prvalue void (*)(int, int)]
// CHECK-NEXT: cast-kind: function-pointer-conversion
// CHECK-NEXT: expression: implicit-cast-expression [prvalue void (*)(int, int)]
// CHECK-NEXT: cast-kind: function-to-pointer-conversion
// CHECK-NEXT: expression: id-expression [lvalue void (int, int)]
// CHECK-NEXT: unqualified-id: name-id
// CHECK-NEXT: identifier: f
// CHECK-NEXT: cast-kind: function-to-pointer-conversion
// CHECK-NEXT: expression: id-expression [lvalue void (int, int)]
// CHECK-NEXT: unqualified-id: name-id
// CHECK-NEXT: identifier: f