lunir/pipelines/compile/
mod.rs

1// MIT License
2
3// Copyright (c) 2023 lunir-project
4
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23use super::OptimizationLevel;
24use crate::{ast::tree::*, il::IlChunk};
25use std::sync::{Arc, Weak};
26
27#[doc(hidden)]
28#[derive(Clone, Debug)]
29pub struct NoSerializer;
30#[doc(hidden)]
31#[derive(Clone, Debug)]
32pub struct WithSerializer<S: Fn(IlChunk) -> Vec<u8>>(S);
33
34#[doc(hidden)]
35#[derive(Clone, Debug)]
36pub struct NoTree;
37#[doc(hidden)]
38#[derive(Clone, Debug)]
39pub struct WithTree<'n>(&'n Node);
40
41/// The interface of LUNIR's compilation pipeline. `CompilationJob` allows you to pass in parameters to the LUNIR compilation pipeline and invoke it, even across threads.
42#[derive(Clone, Debug)]
43pub struct CompilationJob<T, F> {
44    optimization_level: OptimizationLevel,
45    _reference: Weak<()>,
46    serializer: F,
47    tree: T,
48}
49
50impl<T, F> CompilationJob<T, F> {
51    /// Sets the optimization level of this `CompilationJob`.
52    fn optimization_level(mut self, level: OptimizationLevel) -> Self {
53        self.optimization_level = level;
54
55        self
56    }
57}
58impl<'a, T> CompilationJob<T, NoSerializer> {
59    /// Adds a target format serializer function to this `CompilationJob` to allow it to produce a final bytecode.
60    pub fn serializer<S: Fn(IlChunk) -> Vec<u8>>(
61        self,
62        serializer: S,
63    ) -> CompilationJob<T, WithSerializer<S>> {
64        CompilationJob {
65            optimization_level: self.optimization_level,
66            _reference: self._reference,
67            serializer: WithSerializer(serializer),
68            tree: self.tree,
69        }
70    }
71}
72
73impl<'a, F> CompilationJob<NoTree, F> {
74    /// Adds a source abstract syntax tree to this `CompilationJob`.
75    pub fn tree(self, tree: &'a Node) -> CompilationJob<WithTree<'a>, F> {
76        CompilationJob {
77            optimization_level: self.optimization_level,
78            _reference: self._reference,
79            serializer: self.serializer,
80            tree: WithTree(tree),
81        }
82    }
83}
84
85impl<'a, S: Fn(IlChunk) -> Vec<u8>> CompilationJob<WithTree<'a>, WithSerializer<S>> {
86    /// Invokes LUNIR's compilation pipeline with the parameters passed through the `CompilationJob`. This will consume the job.
87    #[must_use = "The result of compilation should be used."]
88    pub fn run(self) -> Vec<u8> {
89        todo!()
90    }
91}
92
93/// A factory for `CompilationJob`s.
94pub struct Compiler {
95    handle: Arc<()>,
96}
97
98impl Compiler {
99    /// Creates a new `Compiler`.
100    pub fn new() -> Self {
101        Self {
102            handle: Arc::new(()),
103        }
104    }
105}
106
107impl Compiler {
108    /// Constructs a `CompilationJob`.
109    pub fn create_job(&self) -> CompilationJob<NoTree, NoSerializer> {
110        CompilationJob {
111            _reference: Arc::downgrade(&self.handle),
112            optimization_level: OptimizationLevel::default(),
113            serializer: NoSerializer,
114            tree: NoTree,
115        }
116    }
117
118    /// Returns the number of currently living `CompilationJob`s created by this compiler or by cloning `CompilationJob`s created by this compiler.
119    pub fn job_count(&self) -> usize {
120        Arc::weak_count(&self.handle)
121    }
122}