Skip to content

Commit

Permalink
compiler: fix copy optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 13, 2024
1 parent 884c604 commit 55233eb
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/julec/obj/cxx/scope.jule
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::jule::sema::{
TupleExprModel,
TypeKind,
BuiltinAppendCallExprModel,
SlicingExprModel,
}

const MATCH_EXPR = "_match_expr"
Expand All @@ -53,7 +54,7 @@ impl ScopeCoder {
self.oc.add_indent()
obj += self.oc.indent()
obj += "auto "
if env::OPT_COPY && it.expr.lvalue {
if env::OPT_COPY && is_copy_optimizable(it.expr) {
obj += "&"
}
obj += "expr = "
Expand Down Expand Up @@ -123,7 +124,7 @@ impl ScopeCoder {
self.oc.add_indent()
obj += self.oc.indent()
obj += "auto "
if env::OPT_COPY && it.expr.lvalue {
if env::OPT_COPY && is_copy_optimizable(it.expr) {
obj += "&"
}
obj += "expr = "
Expand Down Expand Up @@ -455,7 +456,7 @@ impl ScopeCoder {

// Constant expressions generated as literals in conditions.
if !generic_type_match && !m.expr.is_const() {
if m.expr.lvalue && env::OPT_COPY {
if env::OPT_COPY && is_copy_optimizable(m.expr) {
obj += "auto &_match_expr{ "
} else {
obj += "auto _match_expr{ "
Expand Down Expand Up @@ -765,6 +766,17 @@ impl ScopeCoder {
}
}

fn is_copy_optimizable(&expr: &Data): bool {
if !expr.lvalue {
ret false
}
match type expr.model {
| &SlicingExprModel:
ret false
}
ret true
}

fn is_iter_copy_optimizable(&expr: &Data, &v: &Var): bool {
if !expr.lvalue && !expr.kind.mutable() {
ret true
Expand Down

0 comments on commit 55233eb

Please sign in to comment.