Skip to content

Commit

Permalink
compiler: fix code generation for tuple-type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jul 29, 2024
1 parent 4e91197 commit 588c9c7
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/julec/obj/cxx/object.jule
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use env
use opt
use obj::{IR}
use conv for std::conv
use comptime for std::comptime
use jule for std::jule
use build for std::jule::build::{
Directive,
Expand Down Expand Up @@ -34,6 +35,7 @@ use std::jule::sema::{
Prim,
Sptr,
TypeSymbol,
Operators,
}
use path for std::fs::path
use strings for std::strings
Expand Down Expand Up @@ -743,13 +745,12 @@ impl ObjectCoder {
}

fn structureInsDecl(mut &self, mut &s: &StructIns) {
if len(s.Methods) > 0 {
for (_, mut m) in s.Methods {
self.pushResult(m)
for (_, mut m) in s.Methods {
// Operator methods are declared by [structureDecl].
if isOpMethod(m) {
self.funcDecl(m, false)
}
}

self.write("struct ")
let outIdent = identCoder.structureIns(s)

Expand Down Expand Up @@ -792,6 +793,22 @@ impl ObjectCoder {
}
}

fn structureMethodDecls(mut &self) {
for (_, mut s) in self.ir.Ordered.Structs {
if s.Token != nil {
for (_, mut ins) in s.Instances {
for (_, mut m) in ins.Methods {
// Operator methods are declared by [structureDecl].
if !isOpMethod(m) {
self.pushResult(m)
self.funcDecl(m, false)
}
}
}
}
}
}

fn paramsDecls(mut &self, mut &params: []&ParamIns) {
if len(params) == 0 {
self.write("(void)")
Expand Down Expand Up @@ -1149,8 +1166,9 @@ impl ObjectCoder {
fn decls(mut &self) {
self.traitDecls()
self.structurePlainDecls()
self.headPos = len(self.Obj)
self.structureDecls()
self.headPos = len(self.Obj)
self.structureMethodDecls()
self.funcDecls()
self.write("\n\n")
self.traitDataTypes()
Expand Down Expand Up @@ -1415,4 +1433,20 @@ fn pushMethodsFromInherits(mut &dest: &Trait, mut &src: &Trait) {
let mut t2 = inh.Kind.Trait()
pushMethodsFromInherit(dest, t2)
}
}

// Reports whtehr the m is operator overloading function.
fn isOpMethod(&m: &Fn): bool {
if m == nil || len(m.Generics) > 0 || len(m.Instances) == 0 {
ret false
}
let mi = m.Instances[0]
const tableT = comptime::TypeOf(Operators)
const tableV = comptime::ValueOf(mi.Owner.Operators)
for _, field in comptime::Range(tableT.Fields()) {
if mi == tableV.Field(field.Name()).Unwrap() {
ret true
}
}
ret false
}

0 comments on commit 588c9c7

Please sign in to comment.