microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
iadavis/qiskit2-explore

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/compiler/qsc_frontend/src/incremental.rs

416lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#[cfg(test)]
5mod tests;
6
7use crate::{
8 compile::{
9 self, AstPackage, CompileUnit, Dependencies, Offsetter, PackageStore, SourceMap, preprocess,
10 },
11 error::WithSource,
12 lower::Lowerer,
13 resolve::{self, Resolver},
14 typeck::{self, Checker},
15};
16use qsc_ast::{
17 assigner::Assigner as AstAssigner,
18 ast::{self},
19 mut_visit::MutVisitor,
20 validate::Validator as AstValidator,
21 visit::Visitor as AstVisitor,
22};
23use qsc_data_structures::{language_features::LanguageFeatures, target::TargetCapabilityFlags};
24use qsc_hir::{
25 assigner::Assigner as HirAssigner,
26 hir::{self, PackageId},
27 validate::Validator as HirValidator,
28 visit::Visitor as HirVisitor,
29};
30use std::mem::take;
31
32/// The frontend for an incremental compiler.
33/// It is used to update a single `CompileUnit`
34/// with additional sources.
35pub struct Compiler {
36 ast_assigner: AstAssigner,
37 resolver: Resolver,
38 checker: Checker,
39 lowerer: Lowerer,
40 capabilities: TargetCapabilityFlags,
41 language_features: LanguageFeatures,
42}
43
44pub type Error = WithSource<compile::Error>;
45
46/// The result of an incremental compilation.
47/// These packages can be merged into the original
48/// `CompileUnit` that was used for the incremental compilation.
49#[derive(Debug)]
50pub struct Increment {
51 pub ast: AstPackage,
52 pub hir: hir::Package,
53}
54
55impl Increment {
56 pub fn clear_entry(&mut self) {
57 self.hir.entry = None;
58 }
59}
60
61impl Compiler {
62 /// Creates a new compiler.
63 pub fn new(
64 store: &PackageStore,
65 dependencies: &Dependencies,
66 capabilities: TargetCapabilityFlags,
67 language_features: LanguageFeatures,
68 ) -> Self {
69 let mut resolve_globals = resolve::GlobalTable::new();
70 let mut typeck_globals = typeck::GlobalTable::new();
71 let mut dropped_names = Vec::new();
72 if let Some(unit) = store.get(PackageId::CORE) {
73 resolve_globals.add_external_package(PackageId::CORE, &unit.package, store, None);
74 typeck_globals.add_external_package(PackageId::CORE, &unit.package, store);
75 dropped_names.extend(unit.dropped_names.iter().cloned());
76 }
77
78 for (id, alias) in dependencies {
79 let unit = store
80 .get(*id)
81 .expect("dependency should be added to package store before compilation");
82 resolve_globals.add_external_package(*id, &unit.package, store, alias.as_deref());
83 typeck_globals.add_external_package(*id, &unit.package, store);
84 dropped_names.extend(unit.dropped_names.iter().cloned());
85 }
86
87 Self {
88 ast_assigner: AstAssigner::new(),
89 resolver: Resolver::with_persistent_local_scope(resolve_globals, dropped_names),
90 checker: Checker::new(typeck_globals),
91 lowerer: Lowerer::new(),
92 capabilities,
93 language_features,
94 }
95 }
96
97 /// Compiles Q# fragments.
98 ///
99 /// Uses the assigners and other mutable state from the passed in
100 /// `CompileUnit` to guarantee uniqueness, however does not
101 /// update the `CompileUnit` with the resulting AST and HIR packages.
102 ///
103 /// The caller can use the returned packages to perform passes,
104 /// get information about the newly added items, or do other modifications.
105 /// It is then the caller's responsibility to merge
106 /// these packages into the current `CompileUnit`.
107 ///
108 /// This method calls an accumulator function with any errors returned
109 /// from each of the stages (parsing, lowering), instead of failing.
110 /// If the accumulator succeeds, compilation continues.
111 /// If the accumulator returns an error, compilation stops and the
112 /// error is returned to the caller.
113 pub fn compile_fragments<F, E>(
114 &mut self,
115 unit: &mut CompileUnit,
116 source_name: &str,
117 source_contents: &str,
118 accumulate_errors: F,
119 ) -> Result<Increment, E>
120 where
121 F: FnMut(Vec<Error>) -> Result<(), E>,
122 {
123 let (ast, parse_errors) = Self::parse_fragments(
124 &mut unit.sources,
125 source_name,
126 source_contents,
127 self.language_features,
128 );
129
130 self.compile_fragments_internal(unit, ast, parse_errors, accumulate_errors)
131 }
132
133 /// Compiles Q# AST fragments.
134 ///
135 /// Uses the assigners and other mutable state from the passed in
136 /// `CompileUnit` to guarantee uniqueness, however does not
137 /// update the `CompileUnit` with the resulting AST and HIR packages.
138 ///
139 /// The caller can use the returned packages to perform passes,
140 /// get information about the newly added items, or do other modifications.
141 /// It is then the caller's responsibility to merge
142 /// these packages into the current `CompileUnit`.
143 ///
144 /// This method calls an accumulator function with any errors returned
145 /// from each of the stages instead of failing.
146 /// If the accumulator succeeds, compilation continues.
147 /// If the accumulator returns an error, compilation stops and the
148 /// error is returned to the caller.
149 pub fn compile_ast_fragments<F, E>(
150 &mut self,
151 unit: &mut CompileUnit,
152 source_name: &str,
153 source_contents: &str,
154 package: ast::Package,
155 accumulate_errors: F,
156 ) -> Result<Increment, E>
157 where
158 F: FnMut(Vec<Error>) -> Result<(), E>,
159 {
160 // Update the AST with source information offset from the current source map.
161 let (ast, parse_errors) = Self::offset_ast_fragments(
162 &mut unit.sources,
163 source_name,
164 source_contents,
165 package,
166 vec![],
167 );
168
169 self.compile_fragments_internal(unit, ast, parse_errors, accumulate_errors)
170 }
171
172 fn compile_fragments_internal<F, E>(
173 &mut self,
174 unit: &mut CompileUnit,
175 mut ast: ast::Package,
176 parse_errors: Vec<Error>,
177 mut accumulate_errors: F,
178 ) -> Result<Increment, E>
179 where
180 F: FnMut(Vec<Error>) -> Result<(), E>,
181 {
182 accumulate_errors(parse_errors)?;
183
184 let (hir, errors) = self.resolve_check_lower(unit, &mut ast);
185
186 accumulate_errors(errors)?;
187
188 Ok(Increment {
189 ast: AstPackage {
190 package: ast,
191 names: self.resolver.names().clone(),
192 locals: self.resolver.locals().clone(),
193 globals: self.resolver.globals().clone(),
194 tys: self.checker.table().clone(),
195 },
196 hir,
197 })
198 }
199
200 /// Compiles an entry expression.
201 ///
202 /// Uses the assigners and other mutable state from the passed in
203 /// `CompileUnit` to guarantee uniqueness, however does not
204 /// update the `CompileUnit` with the resulting AST and HIR packages.
205 ///
206 /// The caller can use the returned packages to perform passes,
207 /// get information about the newly added items, or do other modifications.
208 /// It is then the caller's responsibility to merge
209 /// these packages into the current `CompileUnit`.
210 pub fn compile_entry_expr(
211 &mut self,
212 unit: &mut CompileUnit,
213 source_contents: &str,
214 ) -> Result<Increment, Vec<Error>> {
215 let (mut ast, parse_errors) =
216 Self::parse_entry_expr(&mut unit.sources, source_contents, self.language_features);
217
218 if !parse_errors.is_empty() {
219 return Err(parse_errors);
220 }
221
222 let (hir, errors) = self.resolve_check_lower(unit, &mut ast);
223
224 if !errors.is_empty() {
225 return Err(errors);
226 }
227
228 Ok(Increment {
229 ast: AstPackage {
230 package: ast,
231 names: self.resolver.names().clone(),
232 locals: self.resolver.locals().clone(),
233 globals: self.resolver.globals().clone(),
234 tys: self.checker.table().clone(),
235 },
236 hir,
237 })
238 }
239
240 pub fn update(&mut self, unit: &mut CompileUnit, new: Increment) {
241 // Update the AST
242 unit.ast.package = self.concat_ast(take(&mut unit.ast.package), new.ast.package);
243
244 // The new `Increment` will contain the names and tys
245 // from the original package as well, so just
246 // replace the current tables instead of extending.
247 unit.ast.names = new.ast.names;
248 unit.ast.tys = new.ast.tys;
249 unit.ast.locals = new.ast.locals;
250 unit.ast.globals = new.ast.globals;
251
252 // Update the HIR
253 extend_hir(&mut unit.package, new.hir);
254 }
255
256 fn resolve_check_lower(
257 &mut self,
258 unit: &mut CompileUnit,
259 ast: &mut ast::Package,
260 ) -> (hir::Package, Vec<Error>) {
261 let mut cond_compile = preprocess::Conditional::new(self.capabilities);
262 cond_compile.visit_package(ast);
263
264 self.ast_assigner.visit_package(ast);
265
266 self.resolver
267 .extend_dropped_names(cond_compile.into_names());
268 self.resolver.bind_fragments(ast, &mut unit.assigner);
269 self.resolver.resolve(&mut unit.assigner, ast);
270
271 self.checker.check_package(self.resolver.names(), ast);
272 self.checker.solve(self.resolver.names());
273
274 let package = self.lower(&mut unit.assigner, &*ast);
275
276 let errors = self
277 .resolver
278 .drain_errors()
279 .map(|e| compile::Error(e.into()))
280 .chain(
281 self.checker
282 .drain_errors()
283 .map(|e| compile::Error(e.into())),
284 )
285 .chain(
286 self.lowerer
287 .drain_errors()
288 .map(|e| compile::Error(e.into())),
289 )
290 .map(|e| WithSource::from_map(&unit.sources, e))
291 .collect::<Vec<_>>();
292
293 if !errors.is_empty() {
294 self.lowerer.clear_items();
295 }
296
297 (package, errors)
298 }
299
300 /// Creates a new `Package` by combining two packages.
301 /// The two packages should not contain any conflicting `NodeId`s.
302 /// Entry expressions are ignored.
303 #[must_use]
304 fn concat_ast(&mut self, mut left: ast::Package, right: ast::Package) -> ast::Package {
305 let mut nodes = Vec::with_capacity(left.nodes.len() + right.nodes.len());
306 nodes.extend(left.nodes.into_vec());
307 nodes.extend(right.nodes.into_vec());
308 left.id = self.ast_assigner.next_id();
309 left.nodes = nodes.into_boxed_slice();
310
311 AstValidator::default().visit_package(&left);
312 left
313 }
314
315 fn parse_entry_expr(
316 sources: &mut SourceMap,
317 source_contents: &str,
318 language_features: LanguageFeatures,
319 ) -> (ast::Package, Vec<Error>) {
320 let offset = sources.push("<entry>".into(), source_contents.into());
321
322 let (mut expr, errors) = qsc_parse::expr(source_contents, language_features);
323
324 let mut offsetter = Offsetter(offset);
325 offsetter.visit_expr(&mut expr);
326
327 let package = ast::Package {
328 id: ast::NodeId::default(),
329 nodes: Box::default(),
330 entry: Some(expr),
331 };
332
333 (package, with_source(errors, sources, offset))
334 }
335
336 fn parse_fragments(
337 sources: &mut SourceMap,
338 source_name: &str,
339 source_contents: &str,
340 features: LanguageFeatures,
341 ) -> (ast::Package, Vec<Error>) {
342 let offset = sources.push(source_name.into(), source_contents.into());
343 let (mut top_level_nodes, errors) = qsc_parse::top_level_nodes(source_contents, features);
344 let mut offsetter = Offsetter(offset);
345 for node in &mut top_level_nodes {
346 match node {
347 ast::TopLevelNode::Namespace(ns) => offsetter.visit_namespace(ns),
348 ast::TopLevelNode::Stmt(stmt) => offsetter.visit_stmt(stmt),
349 }
350 }
351 let package = ast::Package {
352 id: ast::NodeId::default(),
353 nodes: top_level_nodes.into_boxed_slice(),
354 entry: None,
355 };
356 (package, with_source(errors, sources, offset))
357 }
358
359 /// offset all top level nodes based on the source input
360 /// and return the updated package and errors
361 fn offset_ast_fragments(
362 sources: &mut SourceMap,
363 source_name: &str,
364 source_contents: &str,
365 mut package: ast::Package,
366 errors: Vec<qsc_parse::Error>,
367 ) -> (ast::Package, Vec<Error>) {
368 let offset = sources.push(source_name.into(), source_contents.into());
369
370 let mut offsetter = Offsetter(offset);
371 for node in &mut *package.nodes {
372 match node {
373 ast::TopLevelNode::Namespace(ns) => offsetter.visit_namespace(ns),
374 ast::TopLevelNode::Stmt(stmt) => offsetter.visit_stmt(stmt),
375 }
376 }
377
378 (package, with_source(errors, sources, offset))
379 }
380
381 fn lower(&mut self, hir_assigner: &mut HirAssigner, package: &ast::Package) -> hir::Package {
382 self.lowerer
383 .with(hir_assigner, self.resolver.names(), self.checker.table())
384 .lower_package(package)
385 }
386}
387
388/// Extends the `Package` with the contents of another `Package`.
389/// `other` should not contain any `LocalItemId`s
390/// that conflict with the current `Package`.
391/// The entry expression from `other` will be ignored.
392fn extend_hir(this: &mut hir::Package, mut other: hir::Package) {
393 for (k, v) in other.items.drain() {
394 this.items.insert(k, v);
395 }
396
397 this.stmts.extend(other.stmts);
398
399 HirValidator::default().visit_package(this);
400}
401
402fn with_source(
403 errors: Vec<qsc_parse::Error>,
404 sources: &SourceMap,
405 offset: u32,
406) -> Vec<WithSource<compile::Error>> {
407 errors
408 .into_iter()
409 .map(|e| {
410 WithSource::from_map(
411 sources,
412 compile::Error(compile::ErrorKind::Parse(e.with_offset(offset))),
413 )
414 })
415 .collect()
416}
417