Skip to content

Commit c8afce2

Browse files
idavisminestarks
andauthored
Setting up initial language service configuration. (#2355)
Co-authored-by: Mine Starks <[email protected]>
1 parent 102e444 commit c8afce2

Some content is hidden

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

77 files changed

+3495
-1773
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/qsc/src/bin/qsc.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use qsc_frontend::{
1818
use qsc_hir::hir::Package;
1919
use qsc_partial_eval::ProgramEntry;
2020
use qsc_passes::PackageType;
21-
use qsc_project::{FileSystem, StdFs};
21+
use qsc_project::{FileSystem, ProjectType, StdFs};
2222
use std::sync::Arc;
2323
use std::{
2424
concat, fs,
@@ -300,8 +300,11 @@ fn load_project(
300300
}
301301

302302
// This builds all the dependencies
303+
let ProjectType::QSharp(package_graph_sources) = project.project_type else {
304+
panic!("project should be a Q# project");
305+
};
303306
let buildable_program =
304-
BuildableProgram::new(TargetCapabilityFlags::all(), project.package_graph_sources);
307+
BuildableProgram::new(TargetCapabilityFlags::all(), package_graph_sources);
305308

306309
if !buildable_program.dependency_errors.is_empty() {
307310
for e in buildable_program.dependency_errors {

compiler/qsc/src/bin/qsi.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use qsc_eval::{
2121
};
2222
use qsc_frontend::compile::{SourceContents, SourceMap, SourceName};
2323
use qsc_passes::PackageType;
24-
use qsc_project::{FileSystem, StdFs};
24+
use qsc_project::{FileSystem, ProjectType, StdFs};
2525
use std::{
2626
fs,
2727
io::{self, prelude::BufRead, Write},
@@ -299,8 +299,11 @@ fn load_project(
299299
}
300300

301301
// This builds all the dependencies
302+
let ProjectType::QSharp(package_graph_sources) = project.project_type else {
303+
panic!("project should be a Q# project");
304+
};
302305
let buildable_program =
303-
BuildableProgram::new(TargetCapabilityFlags::all(), project.package_graph_sources);
306+
BuildableProgram::new(TargetCapabilityFlags::all(), package_graph_sources);
304307

305308
if !buildable_program.dependency_errors.is_empty() {
306309
for e in buildable_program.dependency_errors {

compiler/qsc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub mod ast {
3333
pub mod project {
3434
pub use qsc_project::{
3535
DirEntry, EntryType, Error, FileSystem, Manifest, ManifestDescriptor, PackageCache,
36-
PackageGraphSources,
36+
PackageGraphSources, ProjectType,
3737
};
3838
}
3939

compiler/qsc/src/packages/tests.rs

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,55 @@ use crate::{compile, LanguageFeatures, TargetCapabilityFlags};
44
use expect_test::expect;
55
use qsc_frontend::compile::{CompileUnit, SourceMap};
66
use qsc_passes::PackageType;
7-
use qsc_project::{PackageInfo, Project};
7+
use qsc_project::{PackageInfo, Project, ProjectType};
88
use rustc_hash::FxHashMap;
99
use std::sync::Arc;
1010

1111
fn mock_program() -> Project {
12-
Project {
13-
// Mock data for the ProgramConfig
14-
package_graph_sources: qsc_project::PackageGraphSources {
15-
root: qsc_project::PackageInfo {
12+
// Mock data for the ProgramConfig
13+
let package_graph_sources = qsc_project::PackageGraphSources {
14+
root: qsc_project::PackageInfo {
15+
sources: vec![(
16+
Arc::from("test"),
17+
Arc::from("@EntryPoint() operation Main() : Unit {}"),
18+
)],
19+
language_features: LanguageFeatures::default(),
20+
dependencies: FxHashMap::from_iter(vec![(
21+
Arc::from("SomeLibraryAlias"),
22+
Arc::from("SomeLibraryKey"),
23+
)]),
24+
package_type: Some(qsc_project::PackageType::Exe),
25+
},
26+
packages: FxHashMap::from_iter(vec![(
27+
Arc::from("SomeLibraryKey"),
28+
PackageInfo {
1629
sources: vec![(
17-
Arc::from("test"),
18-
Arc::from("@EntryPoint() operation Main() : Unit {}"),
30+
Arc::from("librarymain"),
31+
Arc::from("operation LibraryMain() : Unit {} export LibraryMain;"),
1932
)],
2033
language_features: LanguageFeatures::default(),
21-
dependencies: FxHashMap::from_iter(vec![(
22-
Arc::from("SomeLibraryAlias"),
23-
Arc::from("SomeLibraryKey"),
24-
)]),
25-
package_type: Some(qsc_project::PackageType::Exe),
34+
dependencies: FxHashMap::default(),
35+
package_type: Some(qsc_project::PackageType::Lib),
2636
},
27-
packages: FxHashMap::from_iter(vec![(
28-
Arc::from("SomeLibraryKey"),
29-
PackageInfo {
30-
sources: vec![(
31-
Arc::from("librarymain"),
32-
Arc::from("operation LibraryMain() : Unit {} export LibraryMain;"),
33-
)],
34-
language_features: LanguageFeatures::default(),
35-
dependencies: FxHashMap::default(),
36-
package_type: Some(qsc_project::PackageType::Lib),
37-
},
38-
)]),
39-
},
37+
)]),
38+
};
39+
Project {
4040
lints: vec![],
4141
errors: vec![],
4242
path: "project/qsharp.json".into(),
4343
name: "project".into(),
44+
project_type: qsc_project::ProjectType::QSharp(package_graph_sources),
4445
}
4546
}
4647

4748
#[test]
4849
fn test_prepare_package_store() {
4950
let program = mock_program();
50-
let buildable_program = super::prepare_package_store(
51-
TargetCapabilityFlags::default(),
52-
program.package_graph_sources,
53-
);
51+
let ProjectType::QSharp(package_graph_sources) = program.project_type else {
52+
panic!("project should be a Q# project");
53+
};
54+
let buildable_program =
55+
super::prepare_package_store(TargetCapabilityFlags::default(), package_graph_sources);
5456

5557
expect![[r"
5658
[]
@@ -118,17 +120,17 @@ fn test_prepare_package_store() {
118120

119121
#[test]
120122
fn missing_dependency_doesnt_force_failure() {
121-
let mut program = mock_program();
122-
program
123-
.package_graph_sources
123+
let program = mock_program();
124+
let ProjectType::QSharp(mut package_graph_sources) = program.project_type else {
125+
panic!("project should be a Q# project");
126+
};
127+
package_graph_sources
124128
.root
125129
.dependencies
126130
.insert("NonExistent".into(), "nonexistent-dep-key".into());
127131

128-
let buildable_program = super::prepare_package_store(
129-
TargetCapabilityFlags::default(),
130-
program.package_graph_sources,
131-
);
132+
let buildable_program =
133+
super::prepare_package_store(TargetCapabilityFlags::default(), package_graph_sources);
132134

133135
expect![[r"
134136
[]
@@ -194,21 +196,21 @@ fn missing_dependency_doesnt_force_failure() {
194196
#[allow(clippy::too_many_lines)]
195197
#[test]
196198
fn dependency_error() {
197-
let mut program = mock_program();
199+
let program = mock_program();
198200
// Inject a syntax error into one of the dependencies
199-
program
200-
.package_graph_sources
201+
let ProjectType::QSharp(mut package_graph_sources) = program.project_type else {
202+
panic!("project should be a Q# project");
203+
};
204+
package_graph_sources
201205
.packages
202206
.values_mut()
203207
.next()
204208
.expect("expected at least one dependency in the mock program")
205209
.sources[0]
206210
.1 = "broken_syntax".into();
207211

208-
let buildable_program = super::prepare_package_store(
209-
TargetCapabilityFlags::default(),
210-
program.package_graph_sources,
211-
);
212+
let buildable_program =
213+
super::prepare_package_store(TargetCapabilityFlags::default(), package_graph_sources);
212214

213215
expect![[r#"
214216
[

compiler/qsc/src/qasm.rs

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use std::path::Path;
4+
use std::sync::Arc;
55

6+
use qsc_data_structures::target::TargetCapabilityFlags;
7+
use qsc_frontend::compile::PackageStore;
8+
use qsc_frontend::error::WithSource;
9+
use qsc_hir::hir::PackageId;
10+
use qsc_passes::PackageType;
611
use qsc_qasm::io::SourceResolver;
712
pub use qsc_qasm::{
813
CompilerConfig, OperationSignature, OutputSemantics, ProgramType, QasmCompileUnit,
@@ -24,17 +29,81 @@ pub mod completion {
2429
pub use qsc_qasm::compile_to_qsharp_ast_with_config;
2530
pub use qsc_qasm::package_store_with_qasm;
2631

32+
pub struct CompileRawQasmResult(
33+
pub PackageStore,
34+
pub PackageId,
35+
pub Vec<(PackageId, Option<std::sync::Arc<str>>)>,
36+
pub Option<OperationSignature>,
37+
pub Vec<crate::compile::Error>,
38+
);
39+
2740
#[must_use]
28-
pub fn parse_raw_qasm_as_fragments<S, P, R>(
41+
pub fn compile_raw_qasm<R: SourceResolver, S: Into<Arc<str>>>(
2942
source: S,
30-
path: P,
43+
path: S,
3144
resolver: Option<&mut R>,
32-
) -> QasmCompileUnit
33-
where
34-
S: AsRef<str>,
35-
P: AsRef<Path>,
36-
R: SourceResolver,
37-
{
45+
package_type: PackageType,
46+
capabilities: TargetCapabilityFlags,
47+
) -> CompileRawQasmResult {
48+
let config = CompilerConfig::new(
49+
QubitSemantics::Qiskit,
50+
OutputSemantics::OpenQasm,
51+
ProgramType::File,
52+
Some("program".into()),
53+
None,
54+
);
55+
compile_with_config(source, path, resolver, config, package_type, capabilities)
56+
}
57+
58+
#[must_use]
59+
pub fn compile_with_config<R: SourceResolver, S: Into<Arc<str>>>(
60+
source: S,
61+
path: S,
62+
resolver: Option<&mut R>,
63+
config: CompilerConfig,
64+
package_type: PackageType,
65+
capabilities: TargetCapabilityFlags,
66+
) -> CompileRawQasmResult {
67+
let unit = compile_to_qsharp_ast_with_config(source, path, resolver, config);
68+
69+
let (source_map, errors, package, sig) = unit.into_tuple();
70+
71+
let (stdid, qasmid, mut store) = package_store_with_qasm(capabilities);
72+
let dependencies = vec![
73+
(PackageId::CORE, None),
74+
(stdid, None),
75+
(qasmid, Some("QasmStd".into())),
76+
];
77+
78+
let (mut unit, compile_errors) = crate::compile::compile_ast(
79+
&store,
80+
&dependencies,
81+
package,
82+
source_map.clone(),
83+
package_type,
84+
capabilities,
85+
);
86+
unit.expose();
87+
let source_package_id = store.insert(unit);
88+
89+
let mut compile_errors = compile_errors;
90+
for error in errors {
91+
let err = WithSource::from_map(
92+
&source_map,
93+
crate::compile::ErrorKind::OpenQasm(error.into_error()),
94+
);
95+
compile_errors.push(err);
96+
}
97+
98+
CompileRawQasmResult(store, source_package_id, dependencies, sig, compile_errors)
99+
}
100+
101+
#[must_use]
102+
pub fn parse_raw_qasm_as_fragments<R: SourceResolver, S: Into<Arc<str>>>(
103+
source: S,
104+
path: S,
105+
resolver: Option<&mut R>,
106+
) -> QasmCompileUnit {
38107
let config = CompilerConfig::new(
39108
QubitSemantics::Qiskit,
40109
OutputSemantics::OpenQasm,
@@ -46,22 +115,22 @@ where
46115
}
47116

48117
#[must_use]
49-
pub fn parse_raw_qasm_as_operation<S, P, R>(
118+
pub fn parse_raw_qasm_as_operation<
119+
R: SourceResolver,
120+
S: Into<Arc<str>>,
121+
N: Into<Arc<str>>,
122+
P: Into<Arc<str>>,
123+
>(
50124
source: S,
51-
name: S,
125+
name: N,
52126
path: P,
53127
resolver: Option<&mut R>,
54-
) -> QasmCompileUnit
55-
where
56-
S: AsRef<str>,
57-
P: AsRef<Path>,
58-
R: SourceResolver,
59-
{
128+
) -> QasmCompileUnit {
60129
let config = CompilerConfig::new(
61130
QubitSemantics::Qiskit,
62131
OutputSemantics::OpenQasm,
63132
ProgramType::Operation,
64-
Some(name.as_ref().into()),
133+
Some(name.into()),
65134
None,
66135
);
67136
compile_to_qsharp_ast_with_config(source, path, resolver, config)

compiler/qsc_project/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ async-trait = { workspace = true }
1919
qsc_linter = { path = "../qsc_linter" }
2020
qsc_circuit = { path = "../qsc_circuit" }
2121
qsc_data_structures = { path = "../qsc_data_structures" }
22+
qsc_qasm = { path = "../qsc_qasm" }
2223
rustc-hash = { workspace = true }
2324
futures = { workspace = true }
2425
log = { workspace = true }

compiler/qsc_project/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod error;
1313
mod fs;
1414
mod js;
1515
mod manifest;
16+
pub mod openqasm;
1617
mod project;
1718

1819
pub use error::StdFsError;
@@ -23,5 +24,6 @@ pub use manifest::{Manifest, ManifestDescriptor, PackageRef, PackageType, MANIFE
2324
pub use project::FileSystemAsync;
2425
pub use project::{
2526
key_for_package_ref, package_ref_from_key, DependencyCycle, DirEntry, EntryType, Error,
26-
FileSystem, PackageCache, PackageGraphSources, PackageInfo, Project, GITHUB_SCHEME,
27+
FileSystem, PackageCache, PackageGraphSources, PackageInfo, Project, ProjectType,
28+
GITHUB_SCHEME,
2729
};

0 commit comments

Comments
 (0)