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
use crate::lexer::token::LexToken;
use crate::parser::Parser;
use crate::syntax::identifier::SpannedGlobalIdentifier;
use crate::syntax::NonEmptySyntax;
use crate::syntax::Syntax;
use crate::ParserDatabase;
use lark_collections::Seq;
use lark_debug_derive::DebugWith;
use lark_debug_with::DebugWith;
use lark_entity::Entity;
use lark_entity::EntityTables;
use lark_error::ErrorReported;
use lark_error::ErrorSentinel;
use lark_error::WithError;
use lark_hir as hir;
use lark_span::FileName;
use lark_span::Span;
use lark_span::Spanned;
use lark_string::GlobalIdentifier;
use lark_string::GlobalIdentifierTables;
use lark_string::Text;
use lark_ty as ty;
use lark_ty::declaration::Declaration;
use lark_ty::declaration::DeclarationTables;
use std::sync::Arc;

#[derive(DebugWith)]
pub struct EntitySyntax {
    parent_entity: Entity,
}

impl EntitySyntax {
    pub fn new(parent_entity: Entity) -> Self {
        EntitySyntax { parent_entity }
    }
}

impl Syntax<'parse> for EntitySyntax {
    type Data = ParsedEntity;

    fn test(&mut self, parser: &Parser<'_>) -> bool {
        parser.test(SpannedGlobalIdentifier)
    }

    fn expect(&mut self, parser: &mut Parser<'_>) -> Result<Self::Data, ErrorReported> {
        // Parse the macro keyword, which we must find first. So something like
        //
        // ```
        // struct Foo { ... }
        // ^^^^^^ ----------- parsed by the macro itself
        // |
        // parsed by us
        // ```

        let macro_name = parser.expect(SpannedGlobalIdentifier)?;

        log::debug!(
            "EntitySyntax::parse(macro_name = {:?})",
            macro_name.debug_with(parser),
        );

        let macro_definition = match parser.entity_macro_definitions().get(&macro_name.value) {
            Some(m) => m.clone(),
            None => Err(parser.report_error("no macro with this name", macro_name.span))?,
        };

        Ok(macro_definition.expect(parser, self.parent_entity, macro_name)?)
    }
}

impl NonEmptySyntax<'parse> for EntitySyntax {}

#[derive(Clone, Debug, DebugWith, PartialEq, Eq)]
pub struct ParsedEntity {
    /// The `Entity` identifier by which we are known.
    pub entity: Entity,

    /// The span of the entire entity.
    pub full_span: Span<FileName>,

    /// A (sometimes) shorter span that can be used to highlight this
    /// entity in error messages. For example, for a method, it might
    /// be the method name -- this helps to avoid multi-line error
    /// messages, which are kind of a pain.
    pub characteristic_span: Span<FileName>,

    /// Thunk to extract contents
    pub thunk: ParsedEntityThunk,
}

impl ParsedEntity {
    crate fn new(
        entity: Entity,
        full_span: Span<FileName>,
        characteristic_span: Span<FileName>,
        thunk: ParsedEntityThunk,
    ) -> Self {
        Self {
            entity,
            full_span,
            characteristic_span,
            thunk,
        }
    }
}

/// The "parsed entity thunk" contains methods that will recursively
/// parse the contents of this entity in response to salsa queries
/// (or, if the contents are already parsed, return pre-parsed bits
/// and pieces). These routines are meant to be "purely functional",
/// but the salsa runtime will memoize and ensure they are not
/// reinvoked.
#[derive(Clone)]
pub struct ParsedEntityThunk {
    object: Arc<dyn LazyParsedEntity + Send + Sync>,
}

impl ParsedEntityThunk {
    pub fn new<T: 'static + LazyParsedEntity + Send + Sync>(object: T) -> Self {
        Self {
            object: Arc::new(object),
        }
    }

    /// See [`LazyParsedEntity::parse_children`]
    crate fn parse_children(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Seq<ParsedEntity>> {
        self.object.parse_children(entity, db)
    }

    /// See [`LazyParsedEntity::parse_generic_declarations`]
    crate fn parse_generic_declarations(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Result<Arc<ty::GenericDeclarations>, ErrorReported>> {
        self.object.parse_generic_declarations(entity, db)
    }

    /// See [`LazyParsedEntity::parse_type`]
    crate fn parse_type(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<ty::Ty<Declaration>> {
        self.object.parse_type(entity, db)
    }

    /// See [`LazyParsedEntity::parse_signature`]
    crate fn parse_signature(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Result<ty::Signature<Declaration>, ErrorReported>> {
        self.object.parse_signature(entity, db)
    }

    /// See [`LazyParsedEntity::parse_fn_body`]
    crate fn parse_fn_body(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<hir::FnBody> {
        self.object.parse_fn_body(entity, db)
    }
}

impl std::fmt::Debug for ParsedEntityThunk {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        fmt.debug_struct("LazyParsedEntity").finish()
    }
}

impl std::cmp::PartialEq for ParsedEntityThunk {
    fn eq(&self, other: &Self) -> bool {
        std::sync::Arc::ptr_eq(&self.object, &other.object)
    }
}

impl std::cmp::Eq for ParsedEntityThunk {}

lark_debug_with::debug_fallback_impl!(ParsedEntityThunk);

pub trait LazyParsedEntity {
    /// Parse the children of this entity.
    ///
    /// # Parameters
    ///
    /// - `entity`: the entity id of self
    /// - `db`: the necessary bits/pieces of the parser database
    fn parse_children(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Seq<ParsedEntity>>;

    /// What "generic declarations" are on this entity? e.g., for
    /// `struct Foo<T> { .. }`, this would return the declaration for
    /// `T`.
    fn parse_generic_declarations(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Result<Arc<ty::GenericDeclarations>, ErrorReported>>;

    /// What is the "declared type" of this entity? If the entity has
    /// generic types, this type will include bound types referring to
    /// those generics. e.g., for `struct Foo<T> { .. }`, the result
    /// would be effectively `Foo<T>`.
    fn parse_type(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<ty::Ty<Declaration>>;

    /// What is the "signature" of this entity?
    fn parse_signature(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Result<ty::Signature<Declaration>, ErrorReported>>;

    /// Parses the fn body associated with this entity,
    /// panicking if there is none.
    ///
    /// # Parameters
    ///
    /// - `entity`: the entity id of self
    /// - `db`: the necessary bits/pieces of the parser database
    fn parse_fn_body(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<hir::FnBody>;
}

/// The trait given to the [`LazyParsedEntity`] methods. It is a "dyn
/// capable" variant of `ParserDatabase`.
pub trait LazyParsedEntityDatabase:
    AsRef<GlobalIdentifierTables> + AsRef<EntityTables> + AsRef<DeclarationTables>
{
    /// Looks up a name `name` to see if it matches any entities in the scope of `item_entity`.
    fn resolve_name(&self, item_entity: Entity, name: GlobalIdentifier) -> Option<Entity>;

    /// The `file_text` query
    fn file_text(&self, id: FileName) -> Text;

    /// The `file_tokens` query
    fn file_tokens(&self, id: FileName) -> WithError<Seq<Spanned<LexToken, FileName>>>;

    /// The `generic_declarations` query
    fn generic_declarations(
        &self,
        entity: Entity,
    ) -> WithError<Result<Arc<ty::GenericDeclarations>, ErrorReported>>;

    /// The `ty` query
    fn ty(&self, key: Entity) -> WithError<ty::Ty<Declaration>>;
}

impl<T: ParserDatabase> LazyParsedEntityDatabase for T {
    fn file_text(&self, id: FileName) -> Text {
        ParserDatabase::file_text(self, id)
    }

    fn resolve_name(&self, item_entity: Entity, name: GlobalIdentifier) -> Option<Entity> {
        ParserDatabase::resolve_name(self, item_entity, name)
    }

    fn file_tokens(&self, id: FileName) -> WithError<Seq<Spanned<LexToken, FileName>>> {
        ParserDatabase::file_tokens(self, id)
    }

    fn generic_declarations(
        &self,
        entity: Entity,
    ) -> WithError<Result<Arc<ty::GenericDeclarations>, ErrorReported>> {
        ParserDatabase::generic_declarations(self, entity)
    }

    fn ty(&self, entity: Entity) -> WithError<ty::Ty<Declaration>> {
        ParserDatabase::ty(self, entity)
    }
}

crate struct ErrorParsedEntity {
    crate err: ErrorReported,
}

impl LazyParsedEntity for ErrorParsedEntity {
    fn parse_children(
        &self,
        _entity: Entity,
        _db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Seq<ParsedEntity>> {
        WithError::ok(Seq::default())
    }

    fn parse_generic_declarations(
        &self,
        _entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Result<Arc<ty::GenericDeclarations>, ErrorReported>> {
        WithError::ok(ErrorSentinel::error_sentinel(db, self.err))
    }

    fn parse_type(
        &self,
        _entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<ty::Ty<Declaration>> {
        WithError::ok(ErrorSentinel::error_sentinel(&db, self.err))
    }

    fn parse_signature(
        &self,
        _entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Result<ty::Signature<Declaration>, ErrorReported>> {
        WithError::ok(ErrorSentinel::error_sentinel(&db, self.err))
    }

    fn parse_fn_body(
        &self,
        _entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<hir::FnBody> {
        WithError::ok(ErrorSentinel::error_sentinel(&db, self.err))
    }
}

/// Convenience type: implemnts `LazyParsedEntityDatabase` but just
/// panics.  Use as the impl for methods you don't support on a
/// certain kind of entity.
crate struct InvalidParsedEntity;

impl LazyParsedEntity for InvalidParsedEntity {
    fn parse_children(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Seq<ParsedEntity>> {
        panic!(
            "cannot invoke `parse_children` on {:?}",
            entity.debug_with(db)
        )
    }

    fn parse_generic_declarations(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Result<Arc<ty::GenericDeclarations>, ErrorReported>> {
        panic!(
            "cannot invoke `parse_generic_declarations` on {:?}",
            entity.debug_with(db)
        )
    }

    fn parse_type(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<ty::Ty<Declaration>> {
        panic!("cannot invoke `parse_type` on {:?}", entity.debug_with(db))
    }

    fn parse_signature(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<Result<ty::Signature<Declaration>, ErrorReported>> {
        panic!(
            "cannot invoke `parse_signature` on {:?}",
            entity.debug_with(db)
        )
    }

    fn parse_fn_body(
        &self,
        entity: Entity,
        db: &dyn LazyParsedEntityDatabase,
    ) -> WithError<hir::FnBody> {
        panic!(
            "cannot invoke `parse_fn_body` on {:?}",
            entity.debug_with(db)
        )
    }
}