Skip to content

Commit 3ae9ebe

Browse files
authored
Better symbol resolution for OpenQASM LS support (#2679)
This PR adds missing reference support for the OpenQASM language service. Symbols used in defs (captured), designator expressions, type width expresssions, array dim expressions.
1 parent 4a9ac7a commit 3ae9ebe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+4837
-1266
lines changed

source/compiler/qsc_qasm/src/compiler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ impl QasmCompiler {
701701
ty: Type::BitArray(width, false),
702702
expr: stmt.lhs.clone(),
703703
kind: semast::CastKind::Implicit,
704+
ty_exprs: list_from_iter([]),
704705
})),
705706
const_value: None,
706707
ty: Type::BitArray(width, false),
@@ -886,7 +887,7 @@ impl QasmCompiler {
886887
.params
887888
.iter()
888889
.map(|arg| {
889-
let symbol = self.symbols[*arg].clone();
890+
let symbol = self.symbols[arg.symbol_id].clone();
890891
let name = symbol.name.clone();
891892
let semantic_type = symbol.ty.clone();
892893
let qsharp_ty = self.map_semantic_type_to_qsharp_type(&symbol.ty, symbol.ty_span);

source/compiler/qsc_qasm/src/semantic/ast.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ pub struct ClassicalDeclarationStmt {
328328
pub span: Span,
329329
pub ty_span: Span,
330330
pub symbol_id: SymbolId,
331+
pub ty_exprs: List<Expr>,
331332
pub init_expr: Box<Expr>,
332333
}
333334

@@ -336,6 +337,7 @@ impl Display for ClassicalDeclarationStmt {
336337
writeln_header(f, "ClassicalDeclarationStmt", self.span)?;
337338
writeln_field(f, "symbol_id", &self.symbol_id)?;
338339
writeln_field(f, "ty_span", &self.ty_span)?;
340+
writeln_list_field(f, "ty_exprs", &self.ty_exprs)?;
339341
write_field(f, "init_expr", self.init_expr.as_ref())
340342
}
341343
}
@@ -351,14 +353,30 @@ impl Display for ContinueStmt {
351353
}
352354
}
353355

356+
#[derive(Clone, Debug)]
357+
pub struct DefParameter {
358+
pub span: Span,
359+
pub symbol_id: SymbolId,
360+
pub ty_exprs: List<Expr>,
361+
}
362+
363+
impl Display for DefParameter {
364+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
365+
writeln_header(f, "DefParameter", self.span)?;
366+
writeln_field(f, "symbol_id", &self.symbol_id)?;
367+
write_list_field(f, "ty_exprs", &self.ty_exprs)
368+
}
369+
}
370+
354371
#[derive(Clone, Debug)]
355372
pub struct DefStmt {
356373
pub span: Span,
357374
pub symbol_id: SymbolId,
358375
pub has_qubit_params: bool,
359-
pub params: Box<[SymbolId]>,
376+
pub params: List<DefParameter>,
360377
pub body: Block,
361378
pub return_type_span: Span,
379+
pub return_ty_exprs: List<Expr>,
362380
}
363381

364382
impl Display for DefStmt {
@@ -368,6 +386,7 @@ impl Display for DefStmt {
368386
writeln_field(f, "has_qubit_params", &self.has_qubit_params)?;
369387
writeln_list_field(f, "parameters", &self.params)?;
370388
writeln_field(f, "return_type_span", &self.return_type_span)?;
389+
writeln_list_field(f, "return_ty_exprs", &self.return_ty_exprs)?;
371390
write_field(f, "body", &self.body)
372391
}
373392
}
@@ -428,19 +447,24 @@ impl Display for ExprStmt {
428447
pub struct ExternDecl {
429448
pub span: Span,
430449
pub symbol_id: SymbolId,
450+
pub ty_exprs: List<Expr>,
451+
pub return_ty_exprs: List<Expr>,
431452
}
432453

433454
impl Display for ExternDecl {
434455
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
435456
writeln_header(f, "ExternDecl", self.span)?;
436-
write_field(f, "symbol_id", &self.symbol_id)
457+
writeln_field(f, "symbol_id", &self.symbol_id)?;
458+
writeln_list_field(f, "ty_exprs", &self.ty_exprs)?;
459+
write_list_field(f, "return_ty_exprs", &self.return_ty_exprs)
437460
}
438461
}
439462

440463
#[derive(Clone, Debug)]
441464
pub struct ForStmt {
442465
pub span: Span,
443466
pub loop_variable: SymbolId,
467+
pub ty_exprs: List<Expr>,
444468
pub set_declaration: Box<EnumerableSet>,
445469
pub body: Stmt,
446470
}
@@ -449,6 +473,7 @@ impl Display for ForStmt {
449473
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
450474
writeln_header(f, "ForStmt", self.span)?;
451475
writeln_field(f, "loop_variable", &self.loop_variable)?;
476+
writeln_list_field(f, "ty_exprs", &self.ty_exprs)?;
452477
writeln_field(f, "iterable", &self.set_declaration)?;
453478
write_field(f, "body", &self.body)
454479
}
@@ -534,19 +559,22 @@ pub struct InputDeclaration {
534559
// We don't have a type span here, because input decls are in
535560
// the symbol table which tracks the ty span separately.
536561
pub symbol_id: SymbolId,
562+
pub ty_exprs: List<Expr>,
537563
}
538564

539565
impl Display for InputDeclaration {
540566
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
541567
writeln_header(f, "InputDeclaration", self.span)?;
542-
write_field(f, "symbol_id", &self.symbol_id)
568+
writeln_field(f, "symbol_id", &self.symbol_id)?;
569+
write_list_field(f, "ty_exprs", &self.ty_exprs)
543570
}
544571
}
545572

546573
#[derive(Clone, Debug)]
547574
pub struct OutputDeclaration {
548575
pub span: Span,
549576
pub ty_span: Span,
577+
pub ty_exprs: List<Expr>,
550578
pub symbol_id: SymbolId,
551579
pub init_expr: Box<Expr>,
552580
}
@@ -555,6 +583,7 @@ impl Display for OutputDeclaration {
555583
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
556584
writeln_header(f, "OutputDeclaration", self.span)?;
557585
writeln_field(f, "symbol_id", &self.symbol_id)?;
586+
writeln_list_field(f, "ty_exprs", &self.ty_exprs)?;
558587
writeln_field(f, "ty_span", &self.ty_span)?;
559588
write_field(f, "init_expr", &self.init_expr)
560589
}
@@ -1194,6 +1223,7 @@ impl Display for CastKind {
11941223
pub struct Cast {
11951224
pub span: Span,
11961225
pub ty: Type,
1226+
pub ty_exprs: List<Expr>,
11971227
pub expr: Expr,
11981228
pub kind: CastKind,
11991229
}
@@ -1202,6 +1232,7 @@ impl Display for Cast {
12021232
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
12031233
writeln_header(f, "Cast", self.span)?;
12041234
writeln_field(f, "ty", &self.ty)?;
1235+
writeln_list_field(f, "ty_exprs", &self.ty_exprs)?;
12051236
writeln_field(f, "expr", &self.expr)?;
12061237
write_field(f, "kind", &self.kind)
12071238
}

0 commit comments

Comments
 (0)