[meta] Move the doc() default values in the Operand/OperandKind;

This commit is contained in:
Benjamin Bouvier
2019-10-29 11:50:34 +01:00
parent 0b8a579943
commit 2bebc40c16
2 changed files with 28 additions and 26 deletions

View File

@@ -19,11 +19,21 @@ use crate::cdsl::typevar::TypeVar;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(crate) struct Operand { pub(crate) struct Operand {
pub name: &'static str, pub name: &'static str,
pub doc: Option<String>, doc: Option<String>,
pub kind: OperandKind, pub kind: OperandKind,
} }
impl Operand { impl Operand {
pub fn doc(&self) -> Option<&str> {
match &self.doc {
Some(doc) => Some(doc),
None => match &self.kind.fields {
OperandKindFields::TypeVar(tvar) => Some(&tvar.doc),
_ => self.kind.doc(),
},
}
}
pub fn is_value(&self) -> bool { pub fn is_value(&self) -> bool {
match self.kind.fields { match self.kind.fields {
OperandKindFields::TypeVar(_) => true, OperandKindFields::TypeVar(_) => true,
@@ -95,17 +105,9 @@ impl OperandBuilder {
self self
} }
pub fn build(self) -> Operand { pub fn build(self) -> Operand {
let doc = match self.doc {
Some(doc) => Some(doc),
None => match &self.kind.fields {
OperandKindFields::TypeVar(tvar) => Some(tvar.doc.clone()),
_ => self.kind.doc.clone(),
},
};
Operand { Operand {
name: self.name, name: self.name,
doc, doc: self.doc,
kind: self.kind, kind: self.kind,
} }
} }
@@ -137,6 +139,19 @@ pub(crate) struct OperandKind {
} }
impl OperandKind { impl OperandKind {
fn doc(&self) -> Option<&str> {
match &self.doc {
Some(doc) => Some(&doc),
None => match &self.fields {
OperandKindFields::TypeVar(type_var) => Some(&type_var.doc),
OperandKindFields::ImmEnum(_)
| OperandKindFields::ImmValue
| OperandKindFields::EntityRef
| OperandKindFields::VariableArgs => None,
},
}
}
pub fn imm_name(&self) -> Option<&str> { pub fn imm_name(&self) -> Option<&str> {
match self.fields { match self.fields {
OperandKindFields::ImmEnum(_) OperandKindFields::ImmEnum(_)
@@ -230,20 +245,9 @@ impl OperandKindBuilder {
}, },
}; };
let doc = match self.doc {
Some(doc) => Some(doc),
None => match &self.fields {
OperandKindFields::TypeVar(type_var) => Some(type_var.doc.clone()),
OperandKindFields::ImmEnum(_)
| OperandKindFields::ImmValue
| OperandKindFields::EntityRef
| OperandKindFields::VariableArgs => None,
},
};
OperandKind { OperandKind {
name: self.name, name: self.name,
doc, doc: self.doc,
default_member, default_member,
rust_type, rust_type,
fields: self.fields, fields: self.fields,

View File

@@ -925,8 +925,7 @@ fn gen_inst_builder(inst: &Instruction, format: &InstructionFormat, fmt: &mut Fo
args_doc.push(format!( args_doc.push(format!(
"- {}: {}", "- {}: {}",
op.name, op.name,
op.doc op.doc()
.as_ref()
.expect("every instruction's input operand must be documented") .expect("every instruction's input operand must be documented")
)); ));
} }
@@ -935,8 +934,7 @@ fn gen_inst_builder(inst: &Instruction, format: &InstructionFormat, fmt: &mut Fo
rets_doc.push(format!( rets_doc.push(format!(
"- {}: {}", "- {}: {}",
op.name, op.name,
op.doc op.doc()
.as_ref()
.expect("every instruction's output operand must be documented") .expect("every instruction's output operand must be documented")
)); ));
} }