rune/indexing/
indexer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
use rust_alloc::rc::Rc;

use core::mem::replace;
use core::num::NonZeroUsize;

use crate::alloc::path::Path;
use crate::alloc::prelude::*;
use crate::alloc::{self, HashMap, VecDeque};
use crate::ast::spanned;
use crate::ast::{self, Span, Spanned};
use crate::compile::attrs;
use crate::compile::{
    self, Doc, DynLocation, Error, ErrorKind, ItemId, ItemMeta, ModId, Visibility, WithSpan,
};
use crate::grammar::{Ignore, Node, Tree};
use crate::macros::MacroCompiler;
use crate::parse::{Parse, Parser, Resolve};
use crate::query::{BuiltInFile, BuiltInFormat, BuiltInLine, BuiltInMacro, BuiltInTemplate, Query};
use crate::runtime::{format, Call};
use crate::worker::{LoadFileKind, Task};
use crate::SourceId;

use super::{Guard, Items, Layer, Scopes};

/// Macros are only allowed to expand recursively into other macros 64 times.
const MAX_MACRO_RECURSION: usize = 64;

pub(crate) struct Indexer<'a, 'arena> {
    /// Query engine.
    pub(crate) q: Query<'a, 'arena>,
    pub(crate) source_id: SourceId,
    pub(crate) items: Items,
    /// Helper to calculate details about an indexed scope.
    pub(crate) scopes: Scopes,
    /// The current item state.
    pub(crate) item: IndexItem,
    /// Indicates if indexer is nested privately inside of another item, and if
    /// so, the descriptive span of its declaration.
    ///
    /// Private items are nested declarations inside of for example fn
    /// declarations:
    ///
    /// ```text
    /// pub fn public() {
    ///     fn private() {
    ///     }
    /// }
    /// ```
    ///
    /// Then, `nested_item` would point to the span of `pub fn public`.
    pub(crate) nested_item: Option<Span>,
    /// Depth of expression macro expansion that we're currently in.
    pub(crate) macro_depth: usize,
    /// The root URL that the indexed file originated from.
    pub(crate) root: Option<&'a Path>,
    /// Imports to process.
    pub(crate) queue: Option<&'a mut VecDeque<Task>>,
    /// Loaded modules.
    pub(crate) loaded: Option<&'a mut HashMap<ModId, (SourceId, Span)>>,
    /// The current tree being processed.
    pub(crate) tree: &'a Rc<Tree>,
}

impl<'a> Ignore<'a> for Indexer<'_, '_> {
    /// Report an error.
    fn error(&mut self, error: Error) -> alloc::Result<()> {
        self.q.diagnostics.error(self.source_id, error)
    }

    fn ignore(&mut self, _: Node<'a>) -> compile::Result<()> {
        Ok(())
    }
}

impl Indexer<'_, '_> {
    /// Push an identifier item.
    pub(super) fn push_id(&mut self) -> alloc::Result<Guard> {
        let id = self.q.pool.next_id(self.item.id);
        self.items.push_id(id)
    }

    /// Insert a new item at the current indexed location.
    pub(crate) fn insert_new_item(
        &mut self,
        span: &dyn Spanned,
        visibility: Visibility,
        docs: &[Doc],
    ) -> compile::Result<ItemMeta> {
        self.q.insert_new_item(
            &self.items,
            self.item.module,
            self.item.impl_item,
            &DynLocation::new(self.source_id, span),
            visibility,
            docs,
        )
    }

    /// Indicate that we've entered an expanded macro context, and ensure that
    /// we don't blow past [`MAX_MACRO_RECURSION`].
    ///
    /// This is used when entering expressions which have been expanded from a
    /// macro - cause those expression might in turn be macros themselves.
    pub(super) fn enter_macro<S>(&mut self, span: &S) -> compile::Result<()>
    where
        S: Spanned,
    {
        self.macro_depth = self.macro_depth.wrapping_add(1);

        if self.macro_depth >= MAX_MACRO_RECURSION {
            return Err(compile::Error::new(
                span,
                ErrorKind::MaxMacroRecursion {
                    depth: self.macro_depth,
                    max: MAX_MACRO_RECURSION,
                },
            ));
        }

        Ok(())
    }

    /// Leave the last macro context.
    pub(super) fn leave_macro(&mut self) {
        self.macro_depth = self.macro_depth.wrapping_sub(1);
    }

    /// Try to expand an internal macro.
    pub(super) fn try_expand_internal_macro(
        &mut self,
        p: &mut attrs::Parser,
        ast: &mut ast::MacroCall,
    ) -> compile::Result<bool> {
        let Some((_, builtin)) =
            p.try_parse::<attrs::BuiltIn>(resolve_context!(self.q), &ast.attributes)?
        else {
            return Ok(false);
        };

        let args = builtin.args(resolve_context!(self.q))?;

        // NB: internal macros are
        let Some(ident) = ast.path.try_as_ident() else {
            return Err(compile::Error::new(
                &ast.path,
                ErrorKind::NoSuchBuiltInMacro {
                    name: ast.path.resolve(resolve_context!(self.q))?,
                },
            ));
        };

        let ident = ident.resolve(resolve_context!(self.q))?;

        let mut internal_macro = match ident {
            "template" => self.expand_template_macro(ast, &args)?,
            "format" => self.expand_format_macro(ast, &args)?,
            "file" => self.expand_file_macro(ast)?,
            "line" => self.expand_line_macro(ast)?,
            _ => {
                return Err(compile::Error::new(
                    &ast.path,
                    ErrorKind::NoSuchBuiltInMacro {
                        name: ast.path.resolve(resolve_context!(self.q))?,
                    },
                ))
            }
        };

        match &mut internal_macro {
            BuiltInMacro::Template(template) => {
                for e in &mut template.exprs {
                    super::index::expr(self, e)?;
                }
            }
            BuiltInMacro::Format(format) => {
                super::index::expr(self, &mut format.value)?;
            }

            BuiltInMacro::Line(_) | BuiltInMacro::File(_) => { /* Nothing to index */ }
        }

        let id = self.q.insert_new_builtin_macro(internal_macro)?;
        ast.id = Some(id);
        Ok(true)
    }

    /// Expand the template macro.
    fn expand_template_macro(
        &mut self,
        ast: &ast::MacroCall,
        args: &attrs::BuiltInArgs,
    ) -> compile::Result<BuiltInMacro> {
        let mut p = Parser::from_token_stream(&ast.input, ast.span());
        let mut exprs = Vec::new();

        while !p.is_eof()? {
            exprs.try_push(p.parse::<ast::Expr>()?)?;

            if p.parse::<Option<T![,]>>()?.is_none() {
                break;
            }
        }

        p.eof()?;

        Ok(BuiltInMacro::Template(BuiltInTemplate {
            span: ast.span(),
            from_literal: args.literal,
            exprs,
        }))
    }

    /// Expand the template macro.
    fn expand_format_macro(
        &mut self,
        ast: &ast::MacroCall,
        _: &attrs::BuiltInArgs,
    ) -> compile::Result<BuiltInMacro> {
        let mut p = Parser::from_token_stream(&ast.input, ast.span());

        let value = p.parse::<ast::Expr>()?;

        // parsed options
        let mut fill = None;
        let mut align = None;
        let mut flags = None;
        let mut width = None;
        let mut precision = None;
        let mut format_type = None;

        while p.try_consume::<T![,]>()? && !p.is_eof()? {
            let key = p.parse::<ast::Ident>()?;
            let _ = p.parse::<T![=]>()?;

            let k = key.resolve(resolve_context!(self.q))?;

            match k {
                "fill" => {
                    if fill.is_some() {
                        return Err(compile::Error::unsupported(
                            key,
                            "Multiple `format!(.., fill = ..)`",
                        ));
                    }

                    let arg = p.parse::<ast::LitChar>()?;
                    let f = arg.resolve(resolve_context!(self.q))?;

                    fill = Some(f);
                }
                "align" => {
                    if align.is_some() {
                        return Err(compile::Error::unsupported(
                            key,
                            "Multiple `format!(.., align = ..)`",
                        ));
                    }

                    let arg = p.parse::<ast::Ident>()?;
                    let a = arg.resolve(resolve_context!(self.q))?;

                    let Ok(a) = str::parse::<format::Alignment>(a) else {
                        return Err(compile::Error::unsupported(
                            key,
                            "`format!(.., align = ..)`",
                        ));
                    };

                    align = Some(a);
                }
                "flags" => {
                    if flags.is_some() {
                        return Err(compile::Error::unsupported(
                            key,
                            "Multiple `format!(.., flags = ..)`",
                        ));
                    }

                    let arg = p.parse::<ast::LitNumber>()?;

                    let Some(f) = arg.resolve(resolve_context!(self.q))?.as_u32(false) else {
                        return Err(compile::Error::msg(arg, "Argument out-of-bounds"));
                    };

                    let f = format::Flags::from(f);
                    flags = Some(f);
                }
                "width" => {
                    if width.is_some() {
                        return Err(compile::Error::unsupported(
                            key,
                            "Multiple `format!(.., width = ..)`",
                        ));
                    }

                    let arg = p.parse::<ast::LitNumber>()?;

                    let Some(f) = arg.resolve(resolve_context!(self.q))?.as_usize(false) else {
                        return Err(compile::Error::msg(arg, "Argument out-of-bounds"));
                    };

                    width = NonZeroUsize::new(f);
                }
                "precision" => {
                    if precision.is_some() {
                        return Err(compile::Error::unsupported(
                            key,
                            "Multiple `format!(.., precision = ..)`",
                        ));
                    }

                    let arg = p.parse::<ast::LitNumber>()?;

                    let Some(f) = arg.resolve(resolve_context!(self.q))?.as_usize(false) else {
                        return Err(compile::Error::msg(arg, "Argument out-of-bounds"));
                    };

                    precision = NonZeroUsize::new(f);
                }
                "type" => {
                    if format_type.is_some() {
                        return Err(compile::Error::unsupported(
                            key,
                            "Multiple `format!(.., type = ..)`",
                        ));
                    }

                    let arg = p.parse::<ast::Ident>()?;
                    let a = arg.resolve(resolve_context!(self.q))?;

                    format_type = Some(match str::parse::<format::Type>(a) {
                        Ok(format_type) => format_type,
                        _ => {
                            return Err(compile::Error::unsupported(
                                key,
                                "`format!(.., type = ..)`",
                            ));
                        }
                    });
                }
                _ => {
                    return Err(compile::Error::unsupported(key, "`format!(.., <key>)`"));
                }
            }
        }

        p.eof()?;

        Ok(BuiltInMacro::Format(BuiltInFormat {
            span: ast.span(),
            fill,
            align,
            width,
            precision,
            flags,
            format_type,
            value,
        }))
    }

    /// Expand a macro returning the current file
    fn expand_file_macro(&mut self, ast: &ast::MacroCall) -> compile::Result<BuiltInMacro> {
        let name = self.q.sources.name(self.source_id).ok_or_else(|| {
            compile::Error::new(
                ast,
                ErrorKind::MissingSourceId {
                    source_id: self.source_id,
                },
            )
        })?;
        let id = self.q.storage.insert_str(name)?;
        let source = ast::StrSource::Synthetic(id);
        let value = ast::Lit::Str(ast::LitStr {
            span: ast.span(),
            source,
        });

        Ok(BuiltInMacro::File(BuiltInFile { value }))
    }

    /// Expand a macro returning the current line for where the macro invocation begins
    fn expand_line_macro(&mut self, ast: &ast::MacroCall) -> compile::Result<BuiltInMacro> {
        let (l, _) = self
            .q
            .sources
            .get(self.source_id)
            .map(|s| s.pos_to_utf8_linecol(ast.open.span.start.into_usize()))
            .unwrap_or_default();

        // 1-indexed as that is what most editors will use
        let id = self.q.storage.insert_number(l + 1)?;
        let source = ast::NumberSource::Synthetic(id);

        Ok(BuiltInMacro::Line(BuiltInLine {
            value: ast::Lit::Number(ast::LitNumber {
                span: ast.span(),
                source,
            }),
        }))
    }

    /// Perform a macro expansion.
    pub(super) fn expand_macro<T>(&mut self, ast: &mut ast::MacroCall) -> compile::Result<T>
    where
        T: Parse,
    {
        ast.path.id = self.item.id;

        let item = self.q.item_for("macro", self.item.id).with_span(&ast)?;

        let mut compiler = MacroCompiler {
            item_meta: item,
            idx: self,
        };

        compiler.eval_macro::<T>(ast)
    }

    /// Perform an attribute macro expansion.
    pub(super) fn expand_attribute_macro<T>(
        &mut self,
        attr: &mut ast::Attribute,
        item: &ast::Item,
    ) -> compile::Result<Option<T>>
    where
        T: Parse,
    {
        attr.path.id = self.item.id;

        let containing = self
            .q
            .item_for("attribute macro", self.item.id)
            .with_span(&*attr)?;

        let mut compiler = MacroCompiler {
            item_meta: containing,
            idx: self,
        };

        compiler.eval_attribute_macro::<T>(attr, item)
    }

    /// Handle a filesystem module.
    pub(super) fn handle_file_mod(
        &mut self,
        ast: &mut ast::ItemMod,
        docs: &[Doc],
    ) -> compile::Result<()> {
        let name = ast.name.resolve(resolve_context!(self.q))?;
        let visibility = ast_to_visibility(&ast.visibility)?;
        let guard = self.items.push_name(name.as_ref())?;

        let (mod_item, mod_item_id) = self.q.insert_mod(
            &self.items,
            &DynLocation::new(self.source_id, spanned::from_fn(|| ast.name_span())),
            self.item.module,
            visibility,
            docs,
        )?;

        self.items.pop(guard).with_span(&*ast)?;

        ast.id = mod_item_id;

        let Some(root) = &self.root else {
            return Err(compile::Error::new(
                &*ast,
                ErrorKind::UnsupportedModuleSource,
            ));
        };

        let source = self
            .q
            .source_loader
            .load(root, self.q.pool.module_item(mod_item), &*ast)?;

        if let Some(loaded) = self.loaded.as_mut() {
            if let Some(_existing) = loaded.try_insert(mod_item, (self.source_id, ast.span()))? {
                return Err(compile::Error::new(
                    &*ast,
                    ErrorKind::ModAlreadyLoaded {
                        item: self.q.pool.module_item(mod_item).try_to_owned()?,
                        #[cfg(feature = "emit")]
                        existing: _existing,
                    },
                ));
            }
        }

        let source_id = self.q.sources.insert(source)?;

        self.q
            .visitor
            .visit_mod(&DynLocation::new(source_id, &*ast))
            .with_span(&*ast)?;

        if let Some(queue) = self.queue.as_mut() {
            queue.try_push_back(Task::LoadFile {
                kind: LoadFileKind::Module {
                    root: self.root.map(|p| p.try_to_owned()).transpose()?,
                },
                source_id,
                mod_item,
                mod_item_id,
            })?;
        }

        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct IndexItem {
    /// The current module being indexed.
    pub(crate) module: ModId,
    /// Whether the item has been inserted or not.
    pub(crate) id: ItemId,
    /// Set if we are inside of an impl self.
    pub(crate) impl_item: Option<ItemId>,
}

impl IndexItem {
    pub(crate) fn new(module: ModId, id: ItemId) -> Self {
        Self {
            module,
            id,
            impl_item: None,
        }
    }

    pub(crate) fn with_impl_item(module: ModId, id: ItemId, impl_item: ItemId) -> Self {
        Self {
            module,
            id,
            impl_item: Some(impl_item),
        }
    }

    /// Replace item we're currently in.
    #[tracing::instrument(skip(self), fields(self.module = ?self.module, self.id = ?self.id, self.impl_item = ?self.impl_item))]
    pub(super) fn replace(&mut self, id: ItemId) -> IndexItem {
        tracing::debug!("replacing item");

        IndexItem {
            module: self.module,
            id: replace(&mut self.id, id),
            impl_item: self.impl_item,
        }
    }

    /// Replace module id.
    pub(super) fn replace_module(&mut self, module: ModId, id: ItemId) -> IndexItem {
        IndexItem {
            module: replace(&mut self.module, module),
            id: replace(&mut self.id, id),
            impl_item: self.impl_item,
        }
    }
}

/// Construct visibility from ast.
pub(super) fn ast_to_visibility(vis: &ast::Visibility) -> compile::Result<Visibility> {
    let span = match vis {
        ast::Visibility::Inherited => return Ok(Visibility::Inherited),
        ast::Visibility::Public(..) => return Ok(Visibility::Public),
        ast::Visibility::Crate(..) => return Ok(Visibility::Crate),
        ast::Visibility::Super(..) => return Ok(Visibility::Super),
        ast::Visibility::SelfValue(..) => return Ok(Visibility::SelfValue),
        ast::Visibility::In(restrict) => restrict.span(),
    };

    Err(compile::Error::new(span, ErrorKind::UnsupportedVisibility))
}

/// Construct the calling convention based on the parameters.
pub(super) fn validate_call(
    is_const: bool,
    is_async: bool,
    layer: &Layer,
) -> compile::Result<Option<Call>> {
    for span in &layer.awaits {
        if is_const {
            return Err(compile::Error::new(span, ErrorKind::AwaitInConst));
        }

        if !is_async {
            return Err(compile::Error::new(span, ErrorKind::AwaitOutsideAsync));
        }
    }

    for span in &layer.yields {
        if is_const {
            return Err(compile::Error::new(span, ErrorKind::YieldInConst));
        }
    }

    if is_const {
        return Ok(None);
    }

    Ok(match (!layer.yields.is_empty(), is_async) {
        (true, false) => Some(Call::Generator),
        (false, false) => Some(Call::Immediate),
        (true, true) => Some(Call::Stream),
        (false, true) => Some(Call::Async),
    })
}