microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billt/revert-mimalloc

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_frontend/src/incremental.rs

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