Skip to content

Commit

Permalink
frontend: Prepare for emitting self calls as generic calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Feb 4, 2025
1 parent bd4f88d commit 8769a55
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 26 deletions.
2 changes: 2 additions & 0 deletions dora-bytecode/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ pub enum ConstPoolOpcode {
Fct,
TraitObjectMethod,
Generic,
GenericSelf,
Enum,
EnumVariant,
EnumElement,
Expand All @@ -705,6 +706,7 @@ pub enum ConstPoolEntry {
Fct(FunctionId, BytecodeTypeArray),
TraitObjectMethod(BytecodeType, FunctionId),
Generic(u32, FunctionId, BytecodeTypeArray),
GenericSelf(FunctionId, BytecodeTypeArray),
Enum(EnumId, BytecodeTypeArray),
EnumVariant(EnumId, BytecodeTypeArray, u32),
EnumElement(EnumId, BytecodeTypeArray, u32, u32),
Expand Down
9 changes: 9 additions & 0 deletions dora-bytecode/src/dumper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ pub fn dump(w: &mut dyn io::Write, prog: &Program, bc: &BytecodeFunction) -> std
fmt_name(prog, &display_fct(prog, *fct_id), &type_params)
)?;
}
ConstPoolEntry::GenericSelf(fct_id, type_params) => {
writeln!(
w,
"{}{} => Self::Method {}",
align,
idx,
fmt_name(prog, &display_fct(prog, *fct_id), &type_params)
)?;
}
ConstPoolEntry::TraitObject {
trait_ty,
actual_object_ty,
Expand Down
19 changes: 16 additions & 3 deletions dora-frontend/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,8 @@ impl<'a> AstBytecodeGen<'a> {
| CallType::Method(..)
| CallType::GenericMethod(..)
| CallType::GenericStaticMethod(..)
| CallType::GenericMethodSelf(..)
| CallType::GenericStaticMethodSelf(..)
| CallType::TraitObjectMethod(..)
| CallType::Fct(..) => {}

Expand Down Expand Up @@ -1893,7 +1895,9 @@ impl<'a> AstBytecodeGen<'a> {
| CallType::NewStruct(..)
| CallType::NewEnum(..)
| CallType::Intrinsic(..)
| CallType::Lambda(..) => unreachable!(),
| CallType::Lambda(..)
| CallType::GenericMethodSelf(..)
| CallType::GenericStaticMethodSelf(..) => unreachable!(),
}
}

Expand Down Expand Up @@ -3191,7 +3195,7 @@ impl<'a> AstBytecodeGen<'a> {
fct: &FctDefinition,
call_type: &CallType,
) -> ConstPoolIdx {
match *call_type {
match call_type {
CallType::GenericStaticMethod(id, .., ref type_params)
| CallType::GenericMethod(id, .., ref type_params) => {
assert_eq!(
Expand All @@ -3204,6 +3208,13 @@ impl<'a> AstBytecodeGen<'a> {
bty_array_from_ty(&type_params),
)
}
CallType::GenericMethodSelf(_, fct_id, type_params)
| CallType::GenericStaticMethodSelf(_, fct_id, type_params) => {
self.builder.add_const(ConstPoolEntry::GenericSelf(
FunctionId(fct_id.index().try_into().expect("overflow")),
bty_array_from_ty(&type_params),
))
}
CallType::TraitObjectMethod(ref trait_object_ty, _) => {
self.builder.add_const(ConstPoolEntry::TraitObjectMethod(
bty_from_ty(trait_object_ty.clone()),
Expand Down Expand Up @@ -3257,7 +3268,9 @@ impl<'a> AstBytecodeGen<'a> {
| CallType::NewClass(..)
| CallType::NewStruct(..)
| CallType::NewEnum(..)
| CallType::Intrinsic(..) => {
| CallType::Intrinsic(..)
| CallType::GenericMethodSelf(..)
| CallType::GenericStaticMethodSelf(..) => {
unreachable!()
}
}
Expand Down
26 changes: 17 additions & 9 deletions dora-frontend/src/sema/src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ pub enum CallType {
SourceTypeArray,
),

// Invoke trait method from a default trait method, e.g. self.method().
GenericMethodSelf(TraitDefinitionId, FctDefinitionId, SourceTypeArray),

// Invoke static trait method on type param, e.g. T::method()
GenericStaticMethod(
TypeParamId,
Expand All @@ -363,19 +366,22 @@ pub enum CallType {
SourceTypeArray,
),

// Invoke static trait method from a default trait method, e.g. Self::method().
GenericStaticMethodSelf(TraitDefinitionId, FctDefinitionId, SourceTypeArray),

// Class constructor of new class syntax, i.e. ClassName(<args>).
NewClass(ClassDefinitionId, SourceTypeArray),

// Construct enum value
// Construct enum value.
NewEnum(SourceType, u32),

// Struct constructor call Struct(<args>)
// Struct constructor call Struct(<args>).
NewStruct(StructDefinitionId, SourceTypeArray),

// Used for internal functions (those are not exposed to Dora as Fct). Used for enum comparisons.
Intrinsic(Intrinsic),

// Call to lambda,
// Invoke lambda function.
Lambda(SourceTypeArray, SourceType),
}

Expand Down Expand Up @@ -410,12 +416,14 @@ impl CallType {

pub fn fct_id(&self) -> Option<FctDefinitionId> {
match *self {
CallType::Fct(fctid, _) => Some(fctid),
CallType::Method(_, fctid, _) => Some(fctid),
CallType::Expr(_, fctid, _) => Some(fctid),
CallType::TraitObjectMethod(_, fctid) => Some(fctid),
CallType::GenericMethod(_, _, fctid, _) => Some(fctid),
CallType::GenericStaticMethod(_, _, fctid, _) => Some(fctid),
CallType::Fct(fct_id, _)
| CallType::Method(_, fct_id, _)
| CallType::Expr(_, fct_id, _)
| CallType::TraitObjectMethod(_, fct_id)
| CallType::GenericMethod(_, _, fct_id, _)
| CallType::GenericStaticMethod(_, _, fct_id, _)
| CallType::GenericMethodSelf(_, fct_id, _)
| CallType::GenericStaticMethodSelf(_, fct_id, _) => Some(fct_id),

CallType::NewClass(..)
| CallType::NewStruct(..)
Expand Down
14 changes: 10 additions & 4 deletions dora-frontend/src/typeck/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,16 @@ fn check_expr_call_self(

ck.analysis.set_ty(e.id, return_type.clone());

// This should likely become a generic call in the future, once
// the default trait method isn't copied into the impl method anymore.
let call_type =
CallType::Method(SourceType::This, trait_method_id, trait_type_params.clone());
let call_type = if ck.sa.flags.new_default_impl {
CallType::GenericMethodSelf(
trait_method.trait_id(),
trait_method_id,
trait_type_params.clone(),
)
} else {
CallType::Method(SourceType::This, trait_method_id, trait_type_params.clone())
};

ck.analysis.map_calls.insert(e.id, Arc::new(call_type));

check_args_compatible_fct(
Expand Down
5 changes: 5 additions & 0 deletions dora-runtime/src/boots/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ fn encode_constpool_entry(vm: &VM, const_entry: &ConstPoolEntry, buffer: &mut By
buffer.emit_id(fct_id.0 as usize);
encode_bytecode_type_array(vm, source_type_array, buffer);
}
&ConstPoolEntry::GenericSelf(fct_id, ref source_type_array) => {
buffer.emit_u8(ConstPoolOpcode::GenericSelf.into());
buffer.emit_id(fct_id.0 as usize);
encode_bytecode_type_array(vm, source_type_array, buffer);
}
&ConstPoolEntry::Class(cls_id, ref source_type_array) => {
buffer.emit_u8(ConstPoolOpcode::Class.into());
buffer.emit_id(cls_id.0 as usize);
Expand Down
21 changes: 11 additions & 10 deletions pkgs/boots/bytecode/opcode.dora
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ pub const CONSTPOOL_OPCODE_FIELD: Int32 = 7;
pub const CONSTPOOL_OPCODE_FCT: Int32 = 8;
pub const CONSTPOOL_OPCODE_TRAIT_OBJECT_METHOD: Int32 = 9;
pub const CONSTPOOL_OPCODE_GENERIC: Int32 = 10;
pub const CONSTPOOL_OPCODE_ENUM: Int32 = 11;
pub const CONSTPOOL_OPCODE_ENUM_VARIANT: Int32 = 12;
pub const CONSTPOOL_OPCODE_ENUM_ELEMENT: Int32 = 13;
pub const CONSTPOOL_OPCODE_STRUCT: Int32 = 14;
pub const CONSTPOOL_OPCODE_STRUCT_FIELD: Int32 = 15;
pub const CONSTPOOL_OPCODE_TRAIT_OBJECT: Int32 = 16;
pub const CONSTPOOL_OPCODE_TUPLE_ELEMENT: Int32 = 17;
pub const CONSTPOOL_OPCODE_TUPLE: Int32 = 18;
pub const CONSTPOOL_OPCODE_LAMBDA: Int32 = 19;
pub const CONSTPOOL_OPCODE_JUMP_TABLE: Int32 = 20;
pub const CONSTPOOL_OPCODE_GENERIC_SELF: Int32 = 11;
pub const CONSTPOOL_OPCODE_ENUM: Int32 = 12;
pub const CONSTPOOL_OPCODE_ENUM_VARIANT: Int32 = 13;
pub const CONSTPOOL_OPCODE_ENUM_ELEMENT: Int32 = 14;
pub const CONSTPOOL_OPCODE_STRUCT: Int32 = 15;
pub const CONSTPOOL_OPCODE_STRUCT_FIELD: Int32 = 16;
pub const CONSTPOOL_OPCODE_TRAIT_OBJECT: Int32 = 17;
pub const CONSTPOOL_OPCODE_TUPLE_ELEMENT: Int32 = 18;
pub const CONSTPOOL_OPCODE_TUPLE: Int32 = 19;
pub const CONSTPOOL_OPCODE_LAMBDA: Int32 = 20;
pub const CONSTPOOL_OPCODE_JUMP_TABLE: Int32 = 21;

pub const LAZY_COMPILATION_SITE_DIRECT: Int32 = 0;
pub const LAZY_COMPILATION_SITE_VIRTUAL: Int32 = 1;
Expand Down

0 comments on commit 8769a55

Please sign in to comment.