Skip to content

Commit

Permalink
Cleaning up dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
idavis committed Sep 6, 2024
1 parent 29d425d commit 750b622
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 185 deletions.
1 change: 1 addition & 0 deletions compiler/qsc_qasm3/src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct Angle {
size: u32,
}

#[allow(dead_code)]
impl Angle {
fn new(value: u64, size: u32) -> Self {
Angle { value, size }
Expand Down
63 changes: 1 addition & 62 deletions compiler/qsc_qasm3/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,38 +746,6 @@ pub(crate) fn build_gate_call_with_params_and_callee(
}
}

pub(crate) fn build_gate_call_expr(
args: Vec<ast::Expr>,
name_span: Span,
gate_name: &str,
call_span: Span,
expr_span: Span,
) -> ast::Expr {
let param_expr = build_gate_call_param_expr(args, 0);

let ident = ast::Ident {
id: NodeId::default(),
span: name_span,
name: Rc::from(gate_name),
};
let callee_expr = ast::Expr {
id: NodeId::default(),
span: call_span,
kind: Box::new(ast::ExprKind::Path(Box::new(ast::Path {
id: NodeId::default(),
span: Span::default(),
segments: None,
name: Box::new(ident),
}))),
};
let call_kind = ast::ExprKind::Call(Box::new(callee_expr), Box::new(param_expr));
ast::Expr {
kind: Box::new(call_kind),
span: expr_span,
..Default::default()
}
}

pub fn build_gate_call_param_expr(args: Vec<Expr>, remaining: usize) -> Expr {
if args.len() == 1 && remaining > 0 {
return args[0].clone();
Expand Down Expand Up @@ -832,14 +800,6 @@ pub(crate) fn build_lit_double_expr(value: f64, span: Span) -> Expr {
}
}

pub(crate) fn build_lit_string_expr(value: f64, span: Span) -> Expr {
Expr {
id: NodeId::default(),
span,
kind: Box::new(ExprKind::Lit(Box::new(Lit::Double(value)))),
}
}

pub(crate) fn build_stmt_semi_from_expr(expr: Expr) -> Stmt {
Stmt {
id: NodeId::default(),
Expand All @@ -848,22 +808,14 @@ pub(crate) fn build_stmt_semi_from_expr(expr: Expr) -> Stmt {
}
}

pub(crate) fn build_stmt_semi_from_block(block: Block) -> Stmt {
let expr = build_wrapped_block_expr(block);
Stmt {
id: NodeId::default(),
span: expr.span,
kind: Box::new(StmtKind::Semi(Box::new(expr))),
}
}

pub(crate) fn build_wrapped_block_expr(block: Block) -> Expr {
Expr {
id: NodeId::default(),
span: block.span,
kind: Box::new(ast::ExprKind::Block(Box::new(block))),
}
}

pub(crate) fn build_stmt_wrapped_block_expr(stmt: Stmt) -> Block {
Block {
id: NodeId::default(),
Expand Down Expand Up @@ -957,19 +909,6 @@ pub(crate) fn build_implicit_return_stmt(output_expr: Expr) -> Stmt {
}
}

pub(crate) fn build_explicit_return_stmt(output_expr: Expr) -> Stmt {
let span = output_expr.span;
Stmt {
kind: Box::new(StmtKind::Semi(Box::new(Expr {
kind: Box::new(ExprKind::Return(Box::new(output_expr))),
span,
..Default::default()
}))),
span,
..Default::default()
}
}

pub(crate) fn build_path_ident_ty<S: AsRef<str>>(name: S) -> Ty {
let ident = ast::Ident {
name: Rc::from(name.as_ref()),
Expand Down
44 changes: 1 addition & 43 deletions compiler/qsc_qasm3/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,7 @@ impl QasmCompiler {
}
}

#[allow(dead_code)]
fn compile_gphase_call_expr(
&mut self,
gphase_call_expr: &oq3_syntax::ast::GPhaseCallExpr,
Expand Down Expand Up @@ -1904,28 +1905,6 @@ impl QasmCompiler {
}
}

fn compile_int_literal_to_complex(
&mut self,
value: &oq3_syntax::ast::IntNumber,
span: Span,
) -> Option<ast::Expr> {
let value = value.value().expect("FloatNumber must have a value");
if let Some(value) = safe_u128_to_f64(value) {
Some(build_lit_complex_expr(
crate::types::Complex::new(value, 0.0),
span,
))
} else {
let kind = SemanticErrorKind::InvalidCastValueRange(
"Integer".to_string(),
"Complex real".to_string(),
span,
);
self.push_semantic_error(kind);
None
}
}

fn compile_float_literal(value: &oq3_syntax::ast::FloatNumber, span: Span) -> ast::Expr {
build_lit_double_expr(value.value().expect("FloatNumber must have a value"), span)
}
Expand Down Expand Up @@ -3983,15 +3962,6 @@ impl QasmCompiler {
self.errors.push(error);
}

/// Pushes an error for a pulse control not being supported.
pub fn push_pulse_control_error(&mut self, node: &SyntaxNode) {
let span = span_for_syntax_node(node);
let text = node.text().to_string();
let kind = crate::ErrorKind::PulseControlNotSupported(text, span);
let error = self.create_err(kind);
self.errors.push(error);
}

/// Creates an error from the given kind with the current source mapping.
fn create_err(&self, kind: crate::ErrorKind) -> WithSource<crate::Error> {
let error = crate::Error(kind);
Expand Down Expand Up @@ -4087,18 +4057,6 @@ fn binop_requires_int_conversion_for_type(op: BinaryOp, ty_1: &Type, ty_2: &Type
}
}

fn binop_requires_int_magic(op: BinaryOp, ty_1: &Type, ty_2: &Type) -> bool {
match op {
BinaryOp::CmpOp(_) => match (ty_1, ty_2) {
(Type::BitArray(ArrayDims::D1(d1), _), Type::BitArray(ArrayDims::D1(d2), _)) => {
d1 == d2
}
_ => false,
},
_ => false,
}
}

fn binop_requires_bool_conversion_for_type(op: BinaryOp) -> bool {
matches!(op, BinaryOp::LogicOp(..))
}
Expand Down
7 changes: 0 additions & 7 deletions compiler/qsc_qasm3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#![allow(dead_code)]

mod angle;
mod ast_builder;
mod compile;
Expand Down Expand Up @@ -57,8 +55,6 @@ impl Error {
pub enum ErrorKind {
#[error("this statement is not yet handled during OpenQASM 3 import: {0}")]
Unimplemented(String, #[label] Span),
#[error("pulse control statements are not supported: {0}")]
PulseControlNotSupported(String, #[label] Span),
#[error("calibration statements are not supported: {0}")]
CalibrationsNotSupported(String, #[label] Span),
#[error("{0} are not supported.")]
Expand All @@ -78,9 +74,6 @@ impl ErrorKind {
fn with_offset(self, offset: u32) -> Self {
match self {
ErrorKind::Unimplemented(error, span) => Self::Unimplemented(error, span + offset),
ErrorKind::PulseControlNotSupported(error, span) => {
Self::PulseControlNotSupported(error, span + offset)
}
ErrorKind::CalibrationsNotSupported(error, span) => {
Self::CalibrationsNotSupported(error, span + offset)
}
Expand Down
22 changes: 0 additions & 22 deletions compiler/qsc_qasm3/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ impl Default for Symbol {

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SymbolError {
/// The symbol was not found in the symbol table.
NotFound,
/// The symbol already exists in the symbol table, at the current scope.
AlreadyExists,
}
Expand Down Expand Up @@ -182,10 +180,6 @@ impl Scope {
.and_then(|id| self.id_to_symbol.get(id))
}

pub fn get_symbol_by_id(&self, id: SymbolId) -> Option<&Symbol> {
self.id_to_symbol.get(&id)
}

fn get_ordered_symbols(&self) -> Vec<Symbol> {
self.order
.iter()
Expand Down Expand Up @@ -307,15 +301,6 @@ impl SymbolTable {
None
}

pub fn get_symbol_by_id(&self, id: SymbolId) -> Option<&Symbol> {
for scope in self.scopes.iter().rev() {
if let Some(symbol) = scope.get_symbol_by_id(id) {
return Some(symbol);
}
}
None
}

pub fn is_current_scope_global(&self) -> bool {
matches!(self.scopes.last(), Some(scope) if scope.kind == ScopeKind::Global)
}
Expand All @@ -327,13 +312,6 @@ impl SymbolTable {
.any(|scope| scope.kind == ScopeKind::Function)
}

pub fn is_scope_rooted_in_gate(&self) -> bool {
self.scopes
.iter()
.rev()
.any(|scope| scope.kind == ScopeKind::Gate)
}

pub fn is_scope_rooted_in_global(&self) -> bool {
for scope in self.scopes.iter().rev() {
if scope.kind == ScopeKind::Function {
Expand Down
4 changes: 4 additions & 0 deletions compiler/qsc_qasm3/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub(crate) fn gen_qsharp_stmt(stmt: &Stmt) -> String {
qsc::codegen::qsharp::write_stmt_string(stmt)
}

#[allow(dead_code)]
pub(crate) fn compare_compilation_to_qsharp(unit: &QasmCompileUnit, expected: &str) {
let package = unit.package.as_ref().expect("package must exist");
let despanned_ast = AstDespanner.despan(package);
Expand Down Expand Up @@ -280,6 +281,7 @@ fn get_first_statement_as_qsharp(package: &Package) -> String {

pub struct AstDespanner;
impl AstDespanner {
#[allow(dead_code)] // false positive lint
pub fn despan(&mut self, package: &Package) -> Package {
let mut p = package.clone();
self.visit_package(&mut p);
Expand All @@ -294,8 +296,10 @@ impl MutVisitor for AstDespanner {
}
}

#[allow(dead_code)]
struct HirDespanner;
impl HirDespanner {
#[allow(dead_code)]
fn despan(&mut self, package: &qsc::hir::Package) -> qsc::hir::Package {
let mut p = package.clone();
qsc::hir::mut_visit::MutVisitor::visit_package(self, &mut p);
Expand Down
53 changes: 2 additions & 51 deletions compiler/qsc_qasm3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ pub enum GateModifier {
NegCtrl(Option<usize>, Span),
}

impl QasmTypedExpr {
pub fn new(ty: oq3_semantics::types::Type, expr: qsc::ast::Expr) -> Self {
Self { ty, expr }
}
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Complex {
pub real: f64,
Expand All @@ -103,6 +97,7 @@ impl Complex {
}
}

#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Type {
Bool(bool),
Expand All @@ -128,6 +123,7 @@ pub(crate) enum Type {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CallableKind {
/// A function.
#[allow(dead_code)]
Function,
/// An operation.
Operation,
Expand Down Expand Up @@ -214,51 +210,6 @@ impl Display for ArrayDimensions {
}
}

fn can_cast_type_in_assignment(lhs: &Type, rhs: &Type) -> bool {
if types_equal_ignore_const(lhs, rhs) {
return true;
}
matches!(
(lhs, rhs),
(Type::Double(_) | Type::Complex(_), Type::Int(_)) | (Type::Complex(_), Type::Double(_))
)
}

fn types_equal_ignore_const(lhs: &Type, rhs: &Type) -> bool {
match (lhs, rhs) {
(Type::Bool(_), Type::Bool(_))
| (Type::Int(_), Type::Int(_))
| (Type::Double(_), Type::Double(_))
| (Type::Complex(_), Type::Complex(_))
| (Type::Result(_), Type::Result(_))
| (Type::BigInt(_), Type::BigInt(_))
| (Type::Qubit, Type::Qubit) => true,
(Type::Tuple(lhs), Type::Tuple(rhs)) => {
if lhs.len() != rhs.len() {
return false;
}
lhs.iter()
.zip(rhs.iter())
.all(|(lhs, rhs)| types_equal_ignore_const(lhs, rhs))
}
(Type::ResultArray(lhs_dims, _), Type::ResultArray(rhs_dims, _))
| (Type::BigIntArray(lhs_dims, _), Type::BigIntArray(rhs_dims, _))
| (Type::BoolArray(lhs_dims, _), Type::BoolArray(rhs_dims, _))
| (Type::IntArray(lhs_dims, _), Type::IntArray(rhs_dims, _))
| (Type::DoubleArray(lhs_dims), Type::DoubleArray(rhs_dims))
| (Type::QubitArray(lhs_dims), Type::QubitArray(rhs_dims)) => lhs_dims == rhs_dims,
(Type::TupleArray(lhs_dims, lhs), Type::TupleArray(rhs_dims, rhs)) => {
lhs_dims == rhs_dims
&& lhs.len() == rhs.len()
&& lhs
.iter()
.zip(rhs.iter())
.all(|(lhs, rhs)| types_equal_ignore_const(lhs, rhs))
}
_ => false,
}
}

/// Get the indexed type of a given type.
/// For example, if the type is `Int[2][3]`, the indexed type is `Int[2]`.
/// If the type is `Int[2]`, the indexed type is `Int`.
Expand Down

0 comments on commit 750b622

Please sign in to comment.