Skip to content

Commit 7f70408

Browse files
orpuente-MSidavis
authored andcommitted
Remove dead code (#2301)
1 parent 312cc2a commit 7f70408

File tree

27 files changed

+151
-1060
lines changed

27 files changed

+151
-1060
lines changed

compiler/qsc/src/codegen.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
mod tests;
66

77
pub mod qsharp {
8-
pub use qsc_codegen::qsharp::write_item_string;
98
pub use qsc_codegen::qsharp::write_package_string;
109
pub use qsc_codegen::qsharp::write_stmt_string;
1110
}

compiler/qsc_codegen/src/qsharp.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,6 @@ pub fn write_package_string(package: &Package) -> String {
6161
format_str(&s)
6262
}
6363

64-
#[must_use]
65-
pub fn write_item_string(item: &Item) -> String {
66-
let mut output = Vec::new();
67-
let mut gen = QSharpGen::new(&mut output);
68-
69-
gen.visit_item(item);
70-
71-
let s = match std::str::from_utf8(&output) {
72-
Ok(v) => v.to_owned(),
73-
Err(e) => format!("Invalid UTF-8 sequence: {e}"),
74-
};
75-
76-
output.clear();
77-
format_str(&s)
78-
}
79-
8064
#[must_use]
8165
pub fn write_stmt_string(stmt: &ast::Stmt) -> String {
8266
let mut output = Vec::new();

compiler/qsc_qasm3/src/ast_builder.rs

Lines changed: 2 additions & 239 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ pub(crate) fn build_expr_array_expr(values: Vec<qsc_ast::ast::Expr>, span: Span)
248248
}
249249
}
250250

251+
// This will be used to compile arrays in the near future.
252+
#[allow(dead_code)]
251253
pub(crate) fn build_default_result_array_expr(len: usize, span: Span) -> Expr {
252254
let exprs: Vec<_> = (0..len)
253255
.map(|_| Box::new(build_lit_result_expr(ast::Result::Zero, Span::default())))
@@ -363,35 +365,6 @@ pub(crate) fn build_binary_expr(
363365
}
364366
}
365367

366-
pub(crate) fn is_complex_binop_supported(op: qsc_ast::ast::BinOp) -> bool {
367-
matches!(
368-
op,
369-
ast::BinOp::Add | ast::BinOp::Sub | ast::BinOp::Mul | ast::BinOp::Div | ast::BinOp::Exp
370-
)
371-
}
372-
373-
pub(crate) fn build_complex_binary_expr(
374-
is_assignment: bool,
375-
qsop: ast::BinOp,
376-
lhs: ast::Expr,
377-
rhs: ast::Expr,
378-
span: Span,
379-
) -> ast::Expr {
380-
let name = match qsop {
381-
ast::BinOp::Add => "PlusC",
382-
ast::BinOp::Sub => "MinusC",
383-
ast::BinOp::Mul => "TimesC",
384-
ast::BinOp::Div => "DividedByC",
385-
ast::BinOp::Exp => "PowC",
386-
_ => unreachable!("Unsupported complex binary operation"),
387-
};
388-
389-
if is_assignment {
390-
unreachable!("Unsupported complex binary operation");
391-
}
392-
build_math_call_from_exprs(name, vec![lhs, rhs], span)
393-
}
394-
395368
pub(crate) fn build_math_call_from_exprs(name: &str, exprs: Vec<Expr>, span: Span) -> Expr {
396369
let alloc_ident = Ident {
397370
name: Rc::from(name),
@@ -942,14 +915,6 @@ pub(crate) fn build_wrapped_block_expr(block: Block) -> Expr {
942915
}
943916
}
944917

945-
pub(crate) fn build_stmt_wrapped_block_expr(stmt: Stmt) -> Block {
946-
Block {
947-
id: NodeId::default(),
948-
span: stmt.span,
949-
stmts: Box::new([Box::new(stmt)]),
950-
}
951-
}
952-
953918
pub(crate) fn build_expr_wrapped_block_expr(expr: Expr) -> Block {
954919
Block {
955920
id: NodeId::default(),
@@ -1185,14 +1150,6 @@ pub(crate) fn build_top_level_ns_with_items<S: AsRef<str>>(
11851150
})
11861151
}
11871152

1188-
pub(crate) fn build_top_level_ns_with_item<S: AsRef<str>>(
1189-
whole_span: Span,
1190-
ns: S,
1191-
entry: ast::Item,
1192-
) -> TopLevelNode {
1193-
build_top_level_ns_with_items(whole_span, ns, vec![entry])
1194-
}
1195-
11961153
pub(crate) fn build_operation_with_stmts<S: AsRef<str>>(
11971154
name: S,
11981155
input_pats: Vec<Pat>,
@@ -1459,200 +1416,6 @@ pub(crate) fn build_barrier_call(span: Span) -> Stmt {
14591416
build_stmt_semi_from_expr(expr)
14601417
}
14611418

1462-
pub(crate) fn build_gate_decl(
1463-
name: String,
1464-
cargs: Vec<(String, Ty, Pat)>,
1465-
qargs: Vec<(String, Ty, Pat)>,
1466-
body: Option<Block>,
1467-
name_span: Span,
1468-
body_span: Span,
1469-
gate_span: Span,
1470-
) -> Stmt {
1471-
let args = cargs
1472-
.into_iter()
1473-
.chain(qargs)
1474-
.map(|(_, _, pat)| Box::new(pat))
1475-
.collect::<Vec<_>>();
1476-
1477-
let lo = args
1478-
.iter()
1479-
.min_by_key(|x| x.span.lo)
1480-
.map(|x| x.span.lo)
1481-
.unwrap_or_default();
1482-
1483-
let hi = args
1484-
.iter()
1485-
.max_by_key(|x| x.span.hi)
1486-
.map(|x| x.span.hi)
1487-
.unwrap_or_default();
1488-
1489-
let input_pat_kind = if args.len() > 1 {
1490-
PatKind::Tuple(args.into_boxed_slice())
1491-
} else {
1492-
PatKind::Paren(args[0].clone())
1493-
};
1494-
1495-
let input_pat = Pat {
1496-
kind: Box::new(input_pat_kind),
1497-
span: Span { lo, hi },
1498-
..Default::default()
1499-
};
1500-
let body = CallableBody::Block(Box::new(body.unwrap_or_else(|| Block {
1501-
id: NodeId::default(),
1502-
span: body_span,
1503-
stmts: Box::new([]),
1504-
})));
1505-
let decl = CallableDecl {
1506-
id: NodeId::default(),
1507-
span: name_span,
1508-
kind: CallableKind::Operation,
1509-
name: Box::new(Ident {
1510-
name: name.into(),
1511-
..Default::default()
1512-
}),
1513-
generics: Box::new([]),
1514-
input: Box::new(input_pat),
1515-
output: Box::new(build_path_ident_ty("Unit")),
1516-
functors: None,
1517-
body: Box::new(body),
1518-
};
1519-
let item = Item {
1520-
span: gate_span,
1521-
kind: Box::new(ast::ItemKind::Callable(Box::new(decl))),
1522-
..Default::default()
1523-
};
1524-
1525-
Stmt {
1526-
kind: Box::new(StmtKind::Item(Box::new(item))),
1527-
span: gate_span,
1528-
..Default::default()
1529-
}
1530-
}
1531-
1532-
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
1533-
pub(crate) fn build_lambda<S: AsRef<str>>(
1534-
name: S,
1535-
cargs: Vec<(String, Ty, Pat)>,
1536-
qargs: Vec<(String, Ty, Pat)>,
1537-
body: Option<Block>,
1538-
name_span: Span,
1539-
body_span: Span,
1540-
gate_span: Span,
1541-
return_type: Option<Ty>,
1542-
kind: CallableKind,
1543-
) -> Stmt {
1544-
let args = cargs
1545-
.into_iter()
1546-
.chain(qargs)
1547-
.map(|(name, ty, pat)| (name, ty, pat.span))
1548-
.collect::<Vec<_>>();
1549-
1550-
let lo = args
1551-
.iter()
1552-
.min_by_key(|(_, _, span)| span.lo)
1553-
.map(|(_, _, span)| span.lo)
1554-
.unwrap_or_default();
1555-
1556-
let hi = args
1557-
.iter()
1558-
.max_by_key(|(_, _, span)| span.hi)
1559-
.map(|(_, _, span)| span.hi)
1560-
.unwrap_or_default();
1561-
1562-
let name_args = args
1563-
.iter()
1564-
.map(|(name, _, span)| Pat {
1565-
kind: Box::new(PatKind::Bind(
1566-
Box::new(Ident {
1567-
span: *span,
1568-
name: Rc::from(name.as_ref()),
1569-
..Default::default()
1570-
}),
1571-
None,
1572-
)),
1573-
..Default::default()
1574-
})
1575-
.map(Box::new)
1576-
.collect::<Vec<_>>();
1577-
let input_pat = if args.len() == 1 {
1578-
ast::Pat {
1579-
kind: Box::new(ast::PatKind::Paren(name_args[0].clone())),
1580-
span: Span { lo, hi },
1581-
..Default::default()
1582-
}
1583-
} else {
1584-
ast::Pat {
1585-
kind: Box::new(PatKind::Tuple(name_args.into_boxed_slice())),
1586-
span: Span { lo, hi },
1587-
..Default::default()
1588-
}
1589-
};
1590-
1591-
let block_expr = build_wrapped_block_expr(body.map_or_else(
1592-
|| Block {
1593-
id: NodeId::default(),
1594-
span: body_span,
1595-
stmts: Box::new([]),
1596-
},
1597-
|block| block,
1598-
));
1599-
let lambda_expr = Expr {
1600-
id: NodeId::default(),
1601-
kind: Box::new(ExprKind::Lambda(
1602-
kind,
1603-
Box::new(input_pat),
1604-
Box::new(block_expr),
1605-
)),
1606-
span: gate_span,
1607-
};
1608-
let ty_args = args.iter().map(|(_, ty, _)| ty.clone()).collect::<Vec<_>>();
1609-
let input_ty = if args.len() == 1 {
1610-
ast::Ty {
1611-
kind: Box::new(ast::TyKind::Paren(Box::new(ty_args[0].clone()))),
1612-
..Default::default()
1613-
}
1614-
} else {
1615-
ast::Ty {
1616-
kind: Box::new(ast::TyKind::Tuple(ty_args.into_boxed_slice())),
1617-
..Default::default()
1618-
}
1619-
};
1620-
let return_type = if let Some(ty) = return_type {
1621-
ty
1622-
} else {
1623-
build_path_ident_ty("Unit")
1624-
};
1625-
1626-
let lambda_ty = ast::Ty {
1627-
kind: Box::new(ast::TyKind::Arrow(
1628-
kind,
1629-
Box::new(input_ty),
1630-
Box::new(return_type),
1631-
None,
1632-
)),
1633-
..Default::default()
1634-
};
1635-
Stmt {
1636-
span: gate_span,
1637-
kind: Box::new(StmtKind::Local(
1638-
Mutability::Immutable,
1639-
Box::new(Pat {
1640-
kind: Box::new(PatKind::Bind(
1641-
Box::new(Ident {
1642-
span: name_span,
1643-
name: Rc::from(name.as_ref()),
1644-
..Default::default()
1645-
}),
1646-
Some(Box::new(lambda_ty)),
1647-
)),
1648-
..Default::default()
1649-
}),
1650-
Box::new(lambda_expr),
1651-
)),
1652-
..Default::default()
1653-
}
1654-
}
1655-
16561419
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
16571420
pub(crate) fn build_function_or_operation(
16581421
name: String,

compiler/qsc_qasm3/src/compiler.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ use crate::{
2222
build_gate_call_with_params_and_callee, build_global_call_with_two_params,
2323
build_if_expr_then_block, build_if_expr_then_block_else_block,
2424
build_if_expr_then_block_else_expr, build_if_expr_then_expr_else_expr,
25-
build_implicit_return_stmt, build_indexed_assignment_statement, build_lit_angle_expr,
26-
build_lit_bigint_expr, build_lit_bool_expr, build_lit_complex_expr, build_lit_double_expr,
27-
build_lit_int_expr, build_lit_result_array_expr_from_bitstring, build_lit_result_expr,
28-
build_managed_qubit_alloc, build_math_call_from_exprs, build_math_call_no_params,
29-
build_measure_call, build_operation_with_stmts, build_path_ident_expr, build_path_ident_ty,
30-
build_qasm_import_decl, build_qasm_import_items, build_range_expr, build_reset_call,
31-
build_return_expr, build_return_unit, build_stmt_semi_from_expr,
32-
build_stmt_semi_from_expr_with_span, build_top_level_ns_with_items, build_tuple_expr,
33-
build_unary_op_expr, build_unmanaged_qubit_alloc, build_unmanaged_qubit_alloc_array,
34-
build_while_stmt, build_wrapped_block_expr, managed_qubit_alloc_array,
35-
map_qsharp_type_to_ast_ty, wrap_expr_in_parens,
25+
build_implicit_return_stmt, build_index_expr, build_indexed_assignment_statement,
26+
build_lit_angle_expr, build_lit_bigint_expr, build_lit_bool_expr, build_lit_complex_expr,
27+
build_lit_double_expr, build_lit_int_expr, build_lit_result_array_expr_from_bitstring,
28+
build_lit_result_expr, build_managed_qubit_alloc, build_math_call_from_exprs,
29+
build_math_call_no_params, build_measure_call, build_operation_with_stmts,
30+
build_path_ident_expr, build_path_ident_ty, build_qasm_import_decl,
31+
build_qasm_import_items, build_range_expr, build_reset_call, build_return_expr,
32+
build_return_unit, build_stmt_semi_from_expr, build_stmt_semi_from_expr_with_span,
33+
build_top_level_ns_with_items, build_tuple_expr, build_unary_op_expr,
34+
build_unmanaged_qubit_alloc, build_unmanaged_qubit_alloc_array, build_while_stmt,
35+
build_wrapped_block_expr, managed_qubit_alloc_array, map_qsharp_type_to_ast_ty,
36+
wrap_expr_in_parens,
3637
},
3738
io::SourceResolver,
3839
parser::ast::{list_from_iter, List},
@@ -1221,14 +1222,7 @@ impl QasmCompiler {
12211222

12221223
let ident =
12231224
build_path_ident_expr(&symbol.name, indexed_ident.name_span, indexed_ident.span);
1224-
qsast::Expr {
1225-
id: qsast::NodeId::default(),
1226-
span,
1227-
kind: Box::new(qsast::ExprKind::Index(
1228-
Box::new(ident),
1229-
Box::new(index[0].clone()),
1230-
)),
1231-
}
1225+
build_index_expr(ident, index[0].clone(), span)
12321226
}
12331227

12341228
fn compile_unary_op_expr(&mut self, unary: &UnaryOpExpr) -> qsast::Expr {

0 commit comments

Comments
 (0)