Skip to content

Commit

Permalink
codegen: Add version condition on special function traits
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Nov 25, 2020
1 parent b16d610 commit 5ce31f2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/codegen/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub fn generate(

trait_impls::generate(
w,
env,
&analysis.name,
&analysis.functions,
&analysis.specials,
Expand Down
1 change: 1 addition & 0 deletions src/codegen/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub fn generate(w: &mut dyn Write, env: &Env, analysis: &analysis::record::Info)

trait_impls::generate(
w,
env,
&analysis.name,
&analysis.functions,
&analysis.specials,
Expand Down
53 changes: 39 additions & 14 deletions src/codegen/trait_impls.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use crate::analysis::{
functions::Info,
special_functions::{Infos, Type},
use super::general::version_condition;
use crate::{
analysis::{
functions::Info,
special_functions::{Infos, Type},
},
Env,
};
use std::io::{Result, Write};

pub fn generate(
w: &mut dyn Write,
env: &Env,
type_name: &str,
functions: &[Info],
specials: &Infos,
Expand All @@ -15,16 +20,16 @@ pub fn generate(
if let Some(info) = lookup(functions, name) {
match *type_ {
Type::Compare => {
if specials.get(&Type::Equal).is_none() {
generate_eq_compare(w, type_name, info, trait_name)?;
if !specials.contains_key(&Type::Equal) {
generate_eq_compare(w, env, type_name, info, trait_name)?;
}
generate_ord(w, type_name, info, trait_name)?;
generate_ord(w, env, type_name, info, trait_name)?;
}
Type::Equal => {
generate_eq(w, type_name, info, trait_name)?;
generate_eq(w, env, type_name, info, trait_name)?;
}
Type::ToString => generate_display(w, type_name, info, trait_name)?,
Type::Hash => generate_hash(w, type_name, info, trait_name)?,
Type::ToString => generate_display(w, env, type_name, info, trait_name)?,
Type::Hash => generate_hash(w, env, type_name, info, trait_name)?,
_ => {}
}
}
Expand Down Expand Up @@ -71,10 +76,14 @@ fn generate_call(func_name: &str, args: &[&str], trait_name: Option<&str>) -> St

fn generate_display(
w: &mut dyn Write,
env: &Env,
type_name: &str,
func: &Info,
trait_name: Option<&str>,
) -> Result<()> {
writeln!(w)?;
version_condition(w, env, func.version, false, 0)?;

use crate::analysis::out_parameters::Mode;

let call = generate_call(&func.name, &[], trait_name);
Expand All @@ -93,7 +102,7 @@ fn generate_display(

writeln!(
w,
"
"\
impl fmt::Display for {type_name} {{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{
Expand All @@ -107,15 +116,19 @@ impl fmt::Display for {type_name} {{

fn generate_hash(
w: &mut dyn Write,
env: &Env,
type_name: &str,
func: &Info,
trait_name: Option<&str>,
) -> Result<()> {
writeln!(w)?;
version_condition(w, env, func.version, false, 0)?;

let call = generate_call(&func.name, &[], trait_name);

writeln!(
w,
"
"\
impl hash::Hash for {type_name} {{
#[inline]
fn hash<H>(&self, state: &mut H) where H: hash::Hasher {{
Expand All @@ -129,15 +142,19 @@ impl hash::Hash for {type_name} {{

fn generate_eq(
w: &mut dyn Write,
env: &Env,
type_name: &str,
func: &Info,
trait_name: Option<&str>,
) -> Result<()> {
writeln!(w)?;
version_condition(w, env, func.version, false, 0)?;

let call = generate_call(&func.name, &["other"], trait_name);

writeln!(
w,
"
"\
impl PartialEq for {type_name} {{
#[inline]
fn eq(&self, other: &Self) -> bool {{
Expand All @@ -153,15 +170,19 @@ impl Eq for {type_name} {{}}",

fn generate_eq_compare(
w: &mut dyn Write,
env: &Env,
type_name: &str,
func: &Info,
trait_name: Option<&str>,
) -> Result<()> {
writeln!(w)?;
version_condition(w, env, func.version, false, 0)?;

let call = generate_call(&func.name, &["other"], trait_name);

writeln!(
w,
"
"\
impl PartialEq for {type_name} {{
#[inline]
fn eq(&self, other: &Self) -> bool {{
Expand All @@ -177,15 +198,19 @@ impl Eq for {type_name} {{}}",

fn generate_ord(
w: &mut dyn Write,
env: &Env,
type_name: &str,
func: &Info,
trait_name: Option<&str>,
) -> Result<()> {
writeln!(w)?;
version_condition(w, env, func.version, false, 0)?;

let call = generate_call(&func.name, &["other"], trait_name);

writeln!(
w,
"
"\
impl PartialOrd for {type_name} {{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {{
Expand Down

0 comments on commit 5ce31f2

Please sign in to comment.