Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact(rivetc.codegen): don't alloc tagged enum's struct values #55

Merged
merged 8 commits into from
Dec 16, 2023
Merged
Changes from 1 commit
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
Next Next commit
change
StunxFS committed Dec 15, 2023
commit 16b06cce18cff38a94fc13bcd55bd5b38e1b103c
10 changes: 5 additions & 5 deletions lib/rivet/src/ast/Expr.ri
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ pub enum Expr < traits.Stringable {
pos: token.Pos;
mut type: Type;

pub func clean_paren(self) -> Expr {
pub func clean_paren(&self) -> Expr {
mut res := self.expr;
while res is .Paren(paren) {
res = paren.expr;
@@ -184,7 +184,7 @@ pub enum Expr < traits.Stringable {
mut is_enum_variant: bool;
mut enum_variant_sym: TypeSym;

func has_named_args(self) -> bool {
func has_named_args(&self) -> bool {
for arg in self.args {
if arg.is_named {
return true;
@@ -193,7 +193,7 @@ pub enum Expr < traits.Stringable {
return false;
}

func get_named_arg(self, name: string) -> ?CallArg {
func get_named_arg(&self, name: string) -> ?CallArg {
for arg in self.args {
if arg.is_named && arg.name == name {
return arg;
@@ -204,7 +204,7 @@ pub enum Expr < traits.Stringable {

/// Returns the number of pure arguments, that is, not named, that
/// this call has.
func pure_args_count(self) -> uint {
func pure_args_count(&self) -> uint {
mut l: uint := 0;
for arg in self.args {
if !arg.is_named {
@@ -218,7 +218,7 @@ pub enum Expr < traits.Stringable {
}

#[inline]
func has_err_handler(self) -> bool {
func has_err_handler(&self) -> bool {
return self.err_handler.has_expr || self.err_handler.is_propagate;
}
},
20 changes: 11 additions & 9 deletions lib/rivet/src/ast/Type.ri
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import ../token;
#[default_value(.Void)]
pub enum Type < traits.Stringable {
Basic {
mut sym: TypeSym;
mut sym: ?TypeSym;
mut is_unresolved: bool;
mut expr: Expr;
pos: token.Pos;
@@ -31,7 +31,7 @@ pub enum Type < traits.Stringable {
mut sym: TypeSym;
pos: token.Pos;

func ==(self, other: Self) -> bool {
func ==(&self, other: &Self) -> bool {
if self.inners.len != other.inners.len {
return false;
}
@@ -140,7 +140,7 @@ pub enum Type < traits.Stringable {
...self
)
},
.Basic(basic) if !basic.is_unresolved -> if basic.sym.info is .Alias(alias_info) {
.Basic(basic) if !basic.is_unresolved -> if basic.sym?.info is .Alias(alias_info) {
alias_info.parent.unalias() ?? alias_info.parent
} else {
.Basic(basic.sym)
@@ -299,7 +299,7 @@ pub enum Type < traits.Stringable {
},
.Basic(basic_lhs) if rhs is .Basic(basic_rhs) ->
!(basic_lhs.is_unresolved && basic_rhs.is_unresolved)
&& basic_lhs.sym == basic_rhs.sym,
&& basic_lhs.sym? == basic_rhs.sym?,
else -> false
};
}
@@ -391,12 +391,14 @@ pub enum Type < traits.Stringable {
}
sb.to_string()
},
.Basic(basic) -> if basic.is_unresolved {
basic.expr.to_string()
} else if qualstr {
basic.sym.qualname()
.Basic(basic) -> if type_sym := basic.sym {
if qualstr {
type_sym.qualname()
} else {
type_sym.name
}
} else {
basic.sym.name
basic.expr.to_string()
}
};
}
12 changes: 6 additions & 6 deletions lib/rivet/src/ast/TypeInfo.ri
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ pub enum TypeInfo < traits.Stringable {
mut implements: []TypeSym;
mut has_objects: bool;

func index_of(self, type_sym: TypeSym) -> ?uint {
func index_of(&self, type_sym: TypeSym) -> ?uint {
for i, ts in self.implements {
if ts.id == type_sym.id {
return i;
@@ -81,11 +81,11 @@ pub enum TypeInfo < traits.Stringable {
self.variants.push(variant);
}

func has_variant(self, name: string) -> bool {
func has_variant(&self, name: string) -> bool {
return self.get_variant(name) != none;
}

func get_variant(self, name: string) -> ?EnumVariant {
func get_variant(&self, name: string) -> ?EnumVariant {
for v in self.variants {
if v.name == name {
return v;
@@ -94,7 +94,7 @@ pub enum TypeInfo < traits.Stringable {
return none;
}

func get_variant_by_type(self, type: Type) -> ?EnumVariant {
func get_variant_by_type(&self, type: Type) -> ?EnumVariant {
for v in self.variants {
if v.has_type && v.type == type {
return v;
@@ -103,7 +103,7 @@ pub enum TypeInfo < traits.Stringable {
return none;
}

func contains_trait(self, type_sym: TypeSym) -> bool {
func contains_trait(&self, type_sym: TypeSym) -> bool {
for trait_ in self.traits {
if trait_.id == type_sym.id {
return true;
@@ -120,7 +120,7 @@ pub enum TypeInfo < traits.Stringable {
mut bases: []mut TypeSym;
mut traits: []mut TypeSym;

func contains_trait(self, type_sym: TypeSym) -> bool {
func contains_trait(&self, type_sym: TypeSym) -> bool {
for trait_ in self.traits {
if trait_.id == type_sym.id {
return true;
17 changes: 7 additions & 10 deletions lib/rivet/src/resolver/Register.ri
Original file line number Diff line number Diff line change
@@ -198,7 +198,13 @@ pub struct Register {
}
},
.Extend(extend_decl) -> if extend_decl.type is .Basic(basic_type) {
if basic_type.is_unresolved {
if type_sym := basic_type.sym {
self.sym = type_sym;
if type_sym.is_primitive()
&& !self.source_file.mod.is_core() {
report.error("cannot extend primitive types", extend_decl.pos);
}
} else {
if basic_type.expr is .Ident(ident) {
self.sym = if type_sym := self.sym.scope.find(ident.name) {
type_sym
@@ -220,15 +226,6 @@ pub struct Register {
);
continue;
}
} else {
self.sym = basic_type.sym;
if basic_type.sym.is_primitive()
&& !self.source_file.mod.is_core() {
report.error(
"cannot extend primitive types",
extend_decl.pos
);
}
}
self.walk_decls(extend_decl.decls);
} else {
11 changes: 6 additions & 5 deletions rivetc/src/codegen/__init__.py
Original file line number Diff line number Diff line change
@@ -981,10 +981,10 @@ def gen_expr(self, expr, custom_tmp = None):
)
elif typ_sym.kind == TypeKind.Enum:
if expr.is_enum_variant:
tmp = self.boxed_instance(
cg_utils.mangle_symbol(expr.enum_variant_sym),
expr.enum_variant_sym.id
tmp = ir.Ident(
ir.Type(cg_utils.mangle_symbol(expr.enum_variant_sym)), self.cur_func.local_name()
)
self.cur_func.alloca(tmp)
initted_fields = []
type_fields = expr.enum_variant_sym.full_fields()
for i, f in enumerate(expr.args):
@@ -1860,8 +1860,9 @@ def gen_expr(self, expr, custom_tmp = None):
val = ir.Selector(
ir.Type(self.ir_type(expr.typ)), obj_val, ir.Name(f"v{expr.right.variant_info.value}")
)
if expr.var.is_mut and not isinstance(var_t2, ir.Pointer):
if expr.var.is_mut and not isinstance(var_t, ir.Pointer):
val = ir.Inst(ir.InstKind.GetPtr, [val], var_t2)
var_t = var_t.ptr()
else:
val = ir.Inst(
ir.InstKind.Cast, [
@@ -3165,7 +3166,7 @@ def sort_type_symbols(self, tss):
variant_sym = variant.typ.symbol()
if variant_sym.kind != TypeKind.Struct:
continue
if variant_sym.is_boxed() or isinstance(variant.typ, type.Option):
if variant_sym.is_boxed() or isinstance(variant.typ, type.Option) or not variant.has_fields:
continue
dep = cg_utils.mangle_symbol(variant_sym)
if dep not in typ_names or dep in field_deps:
2 changes: 1 addition & 1 deletion rivetc/src/register.py
Original file line number Diff line number Diff line change
@@ -156,7 +156,7 @@ def walk_decls(self, decls):
sym.Type(
decl.is_public, variant.name,
TypeKind.Struct,
info = sym.StructInfo(False, True, True)
info = sym.StructInfo(False, False, True)
)
)
old_v_sym = self.sym