cairo_lang_semantic/items/
functions.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
use std::fmt::Debug;
use std::sync::Arc;

use cairo_lang_debug::DebugWithDb;
use cairo_lang_defs::diagnostic_utils::StableLocation;
use cairo_lang_defs::ids::{
    ExternFunctionId, FreeFunctionId, FunctionTitleId, FunctionWithBodyId, ImplFunctionId,
    LanguageElementId, ModuleFileId, ModuleItemId, NamedLanguageElementId, ParamLongId,
    TopLevelLanguageElementId, TraitFunctionId,
};
use cairo_lang_diagnostics::{Diagnostics, Maybe};
use cairo_lang_filesystem::ids::UnstableSalsaId;
use cairo_lang_proc_macros::{DebugWithDb, SemanticObject};
use cairo_lang_syntax as syntax;
use cairo_lang_syntax::attribute::structured::Attribute;
use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode, ast};
use cairo_lang_utils::{
    Intern, LookupIntern, OptionFrom, define_short_id, require, try_extract_matches,
};
use itertools::{Itertools, chain};
use smol_str::SmolStr;
use syntax::attribute::consts::MUST_USE_ATTR;
use syntax::node::TypedStablePtr;

use super::attribute::SemanticQueryAttrs;
use super::generics::generic_params_to_args;
use super::imp::{ImplId, ImplLongId};
use super::modifiers;
use super::trt::ConcreteTraitGenericFunctionId;
use crate::corelib::{panic_destruct_trait_fn, unit_ty};
use crate::db::SemanticGroup;
use crate::diagnostic::{SemanticDiagnosticKind, SemanticDiagnostics, SemanticDiagnosticsBuilder};
use crate::expr::compute::Environment;
use crate::resolve::{Resolver, ResolverData};
use crate::substitution::{GenericSubstitution, SemanticRewriter, SubstitutionRewriter};
use crate::types::resolve_type;
use crate::{
    ConcreteImplId, ConcreteImplLongId, ConcreteTraitLongId, GenericParam, SemanticDiagnostic,
    TypeId, semantic, semantic_object_for_id,
};

/// A generic function of an impl.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, SemanticObject)]
pub struct ImplGenericFunctionId {
    // TODO(spapini): Consider making these private and enforcing invariants in the ctor.
    /// The impl the function is in.
    pub impl_id: ImplId,
    /// The trait function this impl function implements.
    pub function: TraitFunctionId,
}
impl ImplGenericFunctionId {
    /// Gets the impl function language element, if self.impl_id is of a concrete impl.
    pub fn impl_function(&self, db: &dyn SemanticGroup) -> Maybe<Option<ImplFunctionId>> {
        match self.impl_id.lookup_intern(db) {
            ImplLongId::Concrete(concrete_impl_id) => {
                concrete_impl_id.get_impl_function(db, self.function)
            }
            ImplLongId::GenericParameter(_)
            | ImplLongId::ImplVar(_)
            | ImplLongId::ImplImpl(_)
            | ImplLongId::TraitImpl(_)
            | ImplLongId::GeneratedImpl(_) => Ok(None),
        }
    }
    pub fn format(&self, db: &dyn SemanticGroup) -> SmolStr {
        format!("{}::{}", self.impl_id.name(db.upcast()), self.function.name(db.upcast())).into()
    }
}
impl DebugWithDb<dyn SemanticGroup> for ImplGenericFunctionId {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
        db: &(dyn SemanticGroup + 'static),
    ) -> std::fmt::Result {
        write!(f, "{}", self.format(db))
    }
}

/// The ID of a generic function that can be concretized.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, SemanticObject)]
pub enum GenericFunctionId {
    /// A generic free function.
    Free(FreeFunctionId),
    /// A generic extern function.
    Extern(ExternFunctionId),
    /// A generic function of an impl.
    Impl(ImplGenericFunctionId),
    Trait(ConcreteTraitGenericFunctionId),
}
impl GenericFunctionId {
    pub fn from_generic_with_body(
        db: &dyn SemanticGroup,
        val: GenericFunctionWithBodyId,
    ) -> Maybe<Self> {
        Ok(match val {
            GenericFunctionWithBodyId::Free(id) => GenericFunctionId::Free(id),
            GenericFunctionWithBodyId::Impl(id) => {
                let impl_id = ImplLongId::Concrete(id.concrete_impl_id).intern(db);
                let function = match id.function_body {
                    ImplFunctionBodyId::Impl(body_id) => {
                        db.impl_function_trait_function(body_id)?
                    }
                    ImplFunctionBodyId::Trait(body_id) => body_id,
                };
                GenericFunctionId::Impl(ImplGenericFunctionId { impl_id, function })
            }
            GenericFunctionWithBodyId::Trait(id) => GenericFunctionId::Trait(id),
        })
    }
    pub fn format(&self, db: &dyn SemanticGroup) -> String {
        let defs_db = db.upcast();
        match self {
            GenericFunctionId::Free(id) => id.full_path(defs_db),
            GenericFunctionId::Extern(id) => id.full_path(defs_db),
            GenericFunctionId::Impl(id) => {
                format!("{:?}::{}", id.impl_id.debug(db.elongate()), id.function.name(defs_db))
            }
            GenericFunctionId::Trait(id) => {
                format!(
                    "{}::{}",
                    id.concrete_trait(db).full_path(db),
                    id.trait_function(db).name(defs_db)
                )
            }
        }
    }
    pub fn generic_signature(&self, db: &dyn SemanticGroup) -> Maybe<Signature> {
        match *self {
            GenericFunctionId::Free(id) => db.free_function_signature(id),
            GenericFunctionId::Extern(id) => db.extern_function_signature(id),
            GenericFunctionId::Impl(id) => {
                let concrete_trait_id = id.impl_id.concrete_trait(db)?;
                let signature = db.concrete_trait_function_signature(
                    ConcreteTraitGenericFunctionId::new(db, concrete_trait_id, id.function),
                )?;

                let substitution = &GenericSubstitution::from_impl(id.impl_id);
                SubstitutionRewriter { db, substitution }.rewrite(signature)
            }
            GenericFunctionId::Trait(id) => db.concrete_trait_function_signature(id),
        }
    }
    pub fn generic_params(&self, db: &dyn SemanticGroup) -> Maybe<Vec<GenericParam>> {
        match *self {
            GenericFunctionId::Free(id) => db.free_function_generic_params(id),
            GenericFunctionId::Extern(id) => db.extern_function_declaration_generic_params(id),
            GenericFunctionId::Impl(id) => {
                let concrete_trait_id = db.impl_concrete_trait(id.impl_id)?;
                let id = ConcreteTraitGenericFunctionId::new(db, concrete_trait_id, id.function);
                db.concrete_trait_function_generic_params(id)
            }
            GenericFunctionId::Trait(id) => db.concrete_trait_function_generic_params(id),
        }
    }
    pub fn name(&self, db: &dyn SemanticGroup) -> SmolStr {
        match self {
            GenericFunctionId::Free(free_function) => free_function.name(db.upcast()),
            GenericFunctionId::Extern(extern_function) => extern_function.name(db.upcast()),
            GenericFunctionId::Impl(impl_function) => impl_function.format(db.upcast()),
            GenericFunctionId::Trait(trait_function) => {
                trait_function.trait_function(db).name(db.upcast())
            }
        }
    }
    /// Returns the ModuleFileId of the function's definition if possible.
    pub fn module_file_id(&self, db: &dyn SemanticGroup) -> Option<ModuleFileId> {
        match self {
            GenericFunctionId::Free(free_function) => {
                Some(free_function.module_file_id(db.upcast()))
            }
            GenericFunctionId::Extern(extern_function) => {
                Some(extern_function.module_file_id(db.upcast()))
            }
            GenericFunctionId::Impl(impl_generic_function_id) => {
                // Return the module file of the impl containing the function.
                if let ImplLongId::Concrete(concrete_impl_id) =
                    impl_generic_function_id.impl_id.lookup_intern(db)
                {
                    Some(concrete_impl_id.impl_def_id(db).module_file_id(db.upcast()))
                } else {
                    None
                }
            }
            GenericFunctionId::Trait(trait_function) => Some(
                trait_function.trait_function(db).trait_id(db.upcast()).module_file_id(db.upcast()),
            ),
        }
    }
    /// Returns whether the function has the `#[must_use]` attribute.
    pub fn is_must_use(&self, db: &dyn SemanticGroup) -> Maybe<bool> {
        match self {
            GenericFunctionId::Free(id) => id.has_attr(db, MUST_USE_ATTR),
            GenericFunctionId::Impl(id) => id.function.has_attr(db, MUST_USE_ATTR),
            GenericFunctionId::Trait(id) => id.trait_function(db).has_attr(db, MUST_USE_ATTR),
            GenericFunctionId::Extern(_) => Ok(false),
        }
    }
    /// Returns true if the function does not depend on any generics.
    pub fn is_fully_concrete(&self, db: &dyn SemanticGroup) -> bool {
        match self {
            GenericFunctionId::Free(_) | GenericFunctionId::Extern(_) => true,
            GenericFunctionId::Impl(impl_generic_function) => {
                impl_generic_function.impl_id.is_fully_concrete(db)
            }
            GenericFunctionId::Trait(_) => false,
        }
    }
    /// Returns true if the function does not depend on impl or type variables.
    pub fn is_var_free(&self, db: &dyn SemanticGroup) -> bool {
        match self {
            GenericFunctionId::Free(_) | GenericFunctionId::Extern(_) => true,
            GenericFunctionId::Impl(impl_generic_function) => {
                impl_generic_function.impl_id.is_var_free(db)
            }
            GenericFunctionId::Trait(_) => false,
        }
    }
}
/// Conversion from ModuleItemId to GenericFunctionId.
impl OptionFrom<ModuleItemId> for GenericFunctionId {
    fn option_from(item: ModuleItemId) -> Option<Self> {
        match item {
            ModuleItemId::FreeFunction(id) => Some(GenericFunctionId::Free(id)),
            ModuleItemId::ExternFunction(id) => Some(GenericFunctionId::Extern(id)),
            ModuleItemId::Constant(_)
            | ModuleItemId::Submodule(_)
            | ModuleItemId::Use(_)
            | ModuleItemId::Trait(_)
            | ModuleItemId::Impl(_)
            | ModuleItemId::Struct(_)
            | ModuleItemId::Enum(_)
            | ModuleItemId::TypeAlias(_)
            | ModuleItemId::ImplAlias(_)
            | ModuleItemId::ExternType(_) => None,
        }
    }
}
impl DebugWithDb<dyn SemanticGroup> for GenericFunctionId {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
        db: &(dyn SemanticGroup + 'static),
    ) -> std::fmt::Result {
        match self {
            GenericFunctionId::Free(func) => write!(f, "{:?}", func.debug(db)),
            GenericFunctionId::Extern(func) => write!(f, "{:?}", func.debug(db)),
            GenericFunctionId::Impl(func) => write!(f, "{:?}", func.debug(db)),
            GenericFunctionId::Trait(func) => write!(f, "{:?}", func.debug(db)),
        }
    }
}

/// Function instance.
/// For example: `ImplA::foo<A, B>`, or `bar<A>`.
// TODO(spapini): Make it an enum and add a function pointer variant.
#[derive(Clone, Debug, Hash, PartialEq, Eq, SemanticObject)]
pub struct FunctionLongId {
    pub function: ConcreteFunction,
}
impl DebugWithDb<dyn SemanticGroup> for FunctionLongId {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
        db: &(dyn SemanticGroup + 'static),
    ) -> std::fmt::Result {
        write!(f, "{:?}", self.function.debug(db))
    }
}

define_short_id!(
    FunctionId,
    FunctionLongId,
    SemanticGroup,
    lookup_intern_function,
    intern_function
);
semantic_object_for_id!(FunctionId, lookup_intern_function, intern_function, FunctionLongId);
impl FunctionId {
    pub fn get_concrete(&self, db: &dyn SemanticGroup) -> ConcreteFunction {
        self.lookup_intern(db).function
    }

    /// Returns the ExternFunctionId if this is an extern function. Otherwise returns none.
    pub fn try_get_extern_function_id(&self, db: &dyn SemanticGroup) -> Option<ExternFunctionId> {
        try_extract_matches!(self.get_concrete(db).generic_function, GenericFunctionId::Extern)
    }

    pub fn name(&self, db: &dyn SemanticGroup) -> SmolStr {
        format!("{:?}", self.get_concrete(db).generic_function.name(db)).into()
    }

    pub fn full_name(&self, db: &dyn SemanticGroup) -> String {
        self.get_concrete(db).full_name(db)
    }

    /// Returns true if the function does not depend on any generics.
    pub fn is_fully_concrete(&self, db: &dyn SemanticGroup) -> bool {
        let func = self.get_concrete(db);
        func.generic_function.is_fully_concrete(db)
            && func
                .generic_args
                .iter()
                .all(|generic_argument_id| generic_argument_id.is_fully_concrete(db))
    }
    /// Returns true if the function does not depend on impl or type variables.
    pub fn is_var_free(&self, db: &dyn SemanticGroup) -> bool {
        let func = self.get_concrete(db);
        func.generic_function.is_var_free(db)
            && func
                .generic_args
                .iter()
                .all(|generic_argument_id| generic_argument_id.is_var_free(db))
    }
}
impl FunctionLongId {
    pub fn from_generic(
        db: &dyn SemanticGroup,
        generic_function: GenericFunctionId,
    ) -> Maybe<Self> {
        let generic_params: Vec<_> = generic_function.generic_params(db)?;

        Ok(FunctionLongId {
            function: ConcreteFunction {
                generic_function,
                generic_args: generic_params_to_args(&generic_params, db),
            },
        })
    }
}

/// A generic function of a concrete impl.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, SemanticObject)]
pub struct ImplGenericFunctionWithBodyId {
    pub concrete_impl_id: ConcreteImplId,
    pub function_body: ImplFunctionBodyId,
}

/// The body of an impl function implementation.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, SemanticObject)]
pub enum ImplFunctionBodyId {
    /// A function that was implemented in the impl.
    Impl(ImplFunctionId),
    /// The default implementation of a trait function in the trait.
    Trait(TraitFunctionId),
}
impl ImplFunctionBodyId {
    pub fn name(&self, db: &dyn SemanticGroup) -> SmolStr {
        match self {
            Self::Impl(body_id) => body_id.name(db.upcast()),
            Self::Trait(body_id) => body_id.name(db.upcast()),
        }
    }
    pub fn stable_location(&self, db: &dyn SemanticGroup) -> StableLocation {
        match self {
            Self::Impl(body_id) => body_id.stable_location(db.upcast()),
            Self::Trait(body_id) => body_id.stable_location(db.upcast()),
        }
    }

    pub fn trait_function(&self, db: &dyn SemanticGroup) -> Maybe<TraitFunctionId> {
        match self {
            Self::Impl(impl_function) => db.impl_function_trait_function(*impl_function),
            Self::Trait(trait_function) => Ok(*trait_function),
        }
    }
}

/// The ID of a generic function with body that can be concretized.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, SemanticObject)]
pub enum GenericFunctionWithBodyId {
    Free(FreeFunctionId),
    Impl(ImplGenericFunctionWithBodyId),
    Trait(ConcreteTraitGenericFunctionId),
}
impl GenericFunctionWithBodyId {
    pub fn from_generic(db: &dyn SemanticGroup, other: GenericFunctionId) -> Maybe<Option<Self>> {
        Ok(Some(match other {
            GenericFunctionId::Free(id) => GenericFunctionWithBodyId::Free(id),
            GenericFunctionId::Impl(ImplGenericFunctionId { impl_id, function }) => {
                let ImplLongId::Concrete(concrete_impl_id) = impl_id.lookup_intern(db) else {
                    return Ok(None);
                };
                GenericFunctionWithBodyId::Impl(ImplGenericFunctionWithBodyId {
                    concrete_impl_id,
                    function_body: if let Some(impl_function) =
                        concrete_impl_id.get_impl_function(db, function)?
                    {
                        ImplFunctionBodyId::Impl(impl_function)
                    } else {
                        ImplFunctionBodyId::Trait(function)
                    },
                })
            }
            GenericFunctionId::Trait(id) => GenericFunctionWithBodyId::Trait(id),
            _ => return Ok(None),
        }))
    }
    pub fn name(&self, db: &dyn SemanticGroup) -> SmolStr {
        match self {
            GenericFunctionWithBodyId::Free(free) => free.name(db.upcast()),
            GenericFunctionWithBodyId::Impl(imp) => {
                format!("{}::{}", imp.concrete_impl_id.name(db), imp.function_body.name(db)).into()
            }
            GenericFunctionWithBodyId::Trait(trt) => format!(
                "{}::{}",
                trt.concrete_trait(db).name(db),
                trt.trait_function(db).name(db.upcast())
            )
            .into(),
        }
    }

    pub fn full_path(&self, db: &dyn SemanticGroup) -> String {
        let defs_db = db.upcast();
        match self {
            GenericFunctionWithBodyId::Free(free) => free.full_path(defs_db),
            GenericFunctionWithBodyId::Impl(imp) => {
                format!(
                    "{}::{}",
                    imp.concrete_impl_id.impl_def_id(db).full_path(defs_db),
                    imp.function_body.name(db)
                )
            }
            GenericFunctionWithBodyId::Trait(trt) => format!(
                "{}::{}",
                trt.concrete_trait(db).full_path(db),
                trt.trait_function(db).name(defs_db)
            ),
        }
    }
    pub fn stable_location(&self, db: &dyn SemanticGroup) -> StableLocation {
        match self {
            GenericFunctionWithBodyId::Free(free_function) => {
                free_function.stable_location(db.upcast())
            }
            GenericFunctionWithBodyId::Impl(impl_function) => {
                impl_function.function_body.stable_location(db.upcast())
            }
            GenericFunctionWithBodyId::Trait(trait_function) => {
                trait_function.trait_function(db).stable_location(db.upcast())
            }
        }
    }
}

/// A long Id of a concrete function with body.
#[derive(Clone, Debug, Hash, PartialEq, Eq, SemanticObject)]
pub struct ConcreteFunctionWithBody {
    pub generic_function: GenericFunctionWithBodyId,
    pub generic_args: Vec<semantic::GenericArgumentId>,
}
impl ConcreteFunctionWithBody {
    pub fn function_with_body_id(&self, db: &dyn SemanticGroup) -> FunctionWithBodyId {
        match self.generic_function {
            GenericFunctionWithBodyId::Free(id) => FunctionWithBodyId::Free(id),
            GenericFunctionWithBodyId::Impl(id) => match id.function_body {
                ImplFunctionBodyId::Impl(id) => FunctionWithBodyId::Impl(id),
                ImplFunctionBodyId::Trait(id) => FunctionWithBodyId::Trait(id),
            },
            GenericFunctionWithBodyId::Trait(id) => {
                FunctionWithBodyId::Trait(id.trait_function(db))
            }
        }
    }
    pub fn substitution(&self, db: &dyn SemanticGroup) -> Maybe<GenericSubstitution> {
        Ok(match self.generic_function {
            GenericFunctionWithBodyId::Free(f) => {
                GenericSubstitution::new(&db.free_function_generic_params(f)?, &self.generic_args)
            }
            GenericFunctionWithBodyId::Impl(f) => match f.function_body {
                ImplFunctionBodyId::Impl(body_id) => {
                    let concrete_impl = f.concrete_impl_id.lookup_intern(db);
                    GenericSubstitution::from_impl(
                        ImplLongId::Concrete(f.concrete_impl_id).intern(db),
                    )
                    .concat(GenericSubstitution::new(
                        &chain!(
                            db.impl_function_generic_params(body_id)?,
                            db.impl_def_generic_params(concrete_impl.impl_def_id)?
                        )
                        .collect_vec(),
                        &chain!(
                            self.generic_args.iter().copied(),
                            concrete_impl.generic_args.iter().copied()
                        )
                        .collect_vec(),
                    ))
                }
                ImplFunctionBodyId::Trait(body_id) => {
                    let concrete_impl_id = ImplLongId::Concrete(f.concrete_impl_id).intern(db);
                    let concrete_trait = concrete_impl_id.concrete_trait(db)?.lookup_intern(db);
                    GenericSubstitution::from_impl(concrete_impl_id).concat(
                        GenericSubstitution::new(
                            &chain!(
                                db.trait_function_generic_params(body_id)?,
                                db.trait_generic_params(concrete_trait.trait_id)?
                            )
                            .collect_vec(),
                            &chain!(
                                self.generic_args.iter().copied(),
                                concrete_trait.generic_args.iter().copied()
                            )
                            .collect_vec(),
                        ),
                    )
                }
            },
            GenericFunctionWithBodyId::Trait(f) => {
                let concrete_trait = f.concrete_trait(db).lookup_intern(db);
                GenericSubstitution::new(
                    &chain!(
                        db.trait_function_generic_params(f.trait_function(db))?,
                        db.trait_generic_params(concrete_trait.trait_id)?
                    )
                    .collect_vec(),
                    &chain!(
                        self.generic_args.iter().copied(),
                        concrete_trait.generic_args.iter().copied()
                    )
                    .collect_vec(),
                )
            }
        })
    }
    pub fn from_no_generics_free(
        db: &dyn SemanticGroup,
        free_function_id: FreeFunctionId,
    ) -> Option<Self> {
        require(db.free_function_generic_params(free_function_id).ok()?.is_empty())?;
        Some(ConcreteFunctionWithBody {
            generic_function: GenericFunctionWithBodyId::Free(free_function_id),
            generic_args: vec![],
        })
    }
    pub fn from_generic(db: &dyn SemanticGroup, function_id: FunctionWithBodyId) -> Maybe<Self> {
        Ok(match function_id {
            FunctionWithBodyId::Free(free) => {
                let params = db.free_function_generic_params(free)?;
                let generic_args = generic_params_to_args(&params, db);
                ConcreteFunctionWithBody {
                    generic_function: GenericFunctionWithBodyId::Free(free),
                    generic_args,
                }
            }
            FunctionWithBodyId::Impl(impl_function_id) => {
                let params = db.impl_function_generic_params(impl_function_id)?;
                let generic_args = generic_params_to_args(&params, db);
                let impl_def_id = impl_function_id.impl_def_id(db.upcast());
                let impl_def_params = db.impl_def_generic_params(impl_def_id)?;
                let impl_generic_args = generic_params_to_args(&impl_def_params, db);
                let impl_generic_function = ImplGenericFunctionWithBodyId {
                    concrete_impl_id: ConcreteImplLongId {
                        impl_def_id,
                        generic_args: impl_generic_args,
                    }
                    .intern(db),
                    function_body: ImplFunctionBodyId::Impl(impl_function_id),
                };
                ConcreteFunctionWithBody {
                    generic_function: GenericFunctionWithBodyId::Impl(impl_generic_function),
                    generic_args,
                }
            }
            FunctionWithBodyId::Trait(trait_function_id) => {
                let params = db.trait_function_generic_params(trait_function_id)?;
                let generic_args = generic_params_to_args(&params, db);
                let trait_id = trait_function_id.trait_id(db.upcast());
                let trait_generic_params = db.trait_generic_params(trait_id)?;
                let trait_generic_args = generic_params_to_args(&trait_generic_params, db);
                let concrete_trait_id = ConcreteTraitLongId {
                    generic_args: trait_generic_args,
                    trait_id: trait_function_id.trait_id(db.upcast()),
                }
                .intern(db);
                let trait_generic_function =
                    ConcreteTraitGenericFunctionId::new(db, concrete_trait_id, trait_function_id);
                ConcreteFunctionWithBody {
                    generic_function: GenericFunctionWithBodyId::Trait(trait_generic_function),
                    generic_args,
                }
            }
        })
    }
    pub fn concrete(&self, db: &dyn SemanticGroup) -> Maybe<ConcreteFunction> {
        Ok(ConcreteFunction {
            generic_function: GenericFunctionId::from_generic_with_body(db, self.generic_function)?,
            generic_args: self.generic_args.clone(),
        })
    }
    pub fn function_id(&self, db: &dyn SemanticGroup) -> Maybe<FunctionId> {
        Ok(FunctionLongId { function: self.concrete(db)? }.intern(db))
    }
    pub fn name(&self, db: &dyn SemanticGroup) -> SmolStr {
        self.function_with_body_id(db).name(db.upcast())
    }
    pub fn full_path(&self, db: &dyn SemanticGroup) -> String {
        self.generic_function.full_path(db)
    }
}

impl DebugWithDb<dyn SemanticGroup> for ConcreteFunctionWithBody {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
        db: &(dyn SemanticGroup + 'static),
    ) -> std::fmt::Result {
        write!(f, "{:?}", self.generic_function.name(db.upcast()))?;
        if !self.generic_args.is_empty() {
            write!(f, "::<")?;
            for (i, arg) in self.generic_args.iter().enumerate() {
                if i > 0 {
                    write!(f, ", ")?;
                }
                write!(f, "{:?}", arg.debug(db))?;
            }
            write!(f, ">")?;
        }
        Ok(())
    }
}

define_short_id!(
    ConcreteFunctionWithBodyId,
    ConcreteFunctionWithBody,
    SemanticGroup,
    lookup_intern_concrete_function_with_body,
    intern_concrete_function_with_body
);
semantic_object_for_id!(
    ConcreteFunctionWithBodyId,
    lookup_intern_concrete_function_with_body,
    intern_concrete_function_with_body,
    ConcreteFunctionWithBody
);
impl ConcreteFunctionWithBodyId {
    pub fn function_with_body_id(&self, db: &dyn SemanticGroup) -> FunctionWithBodyId {
        self.lookup_intern(db).function_with_body_id(db)
    }
    pub fn substitution(&self, db: &dyn SemanticGroup) -> Maybe<GenericSubstitution> {
        self.lookup_intern(db).substitution(db)
    }
    pub fn from_no_generics_free(
        db: &dyn SemanticGroup,
        free_function_id: FreeFunctionId,
    ) -> Option<Self> {
        Some(ConcreteFunctionWithBody::from_no_generics_free(db, free_function_id)?.intern(db))
    }
    pub fn from_generic(db: &dyn SemanticGroup, function_id: FunctionWithBodyId) -> Maybe<Self> {
        Ok(ConcreteFunctionWithBody::from_generic(db, function_id)?.intern(db))
    }
    pub fn concrete(&self, db: &dyn SemanticGroup) -> Maybe<ConcreteFunction> {
        self.lookup_intern(db).concrete(db)
    }
    pub fn function_id(&self, db: &dyn SemanticGroup) -> Maybe<FunctionId> {
        self.lookup_intern(db).function_id(db)
    }
    pub fn generic_function(&self, db: &dyn SemanticGroup) -> GenericFunctionWithBodyId {
        self.lookup_intern(db).generic_function
    }
    pub fn name(&self, db: &dyn SemanticGroup) -> SmolStr {
        self.lookup_intern(db).name(db)
    }
    pub fn full_path(&self, db: &dyn SemanticGroup) -> String {
        self.lookup_intern(db).full_path(db)
    }

    pub fn stable_location(&self, db: &dyn SemanticGroup) -> StableLocation {
        self.lookup_intern(db).generic_function.stable_location(db)
    }

    pub fn is_panic_destruct_fn(&self, db: &dyn SemanticGroup) -> Maybe<bool> {
        let trait_function = match self.generic_function(db) {
            GenericFunctionWithBodyId::Free(_) => return Ok(false),
            GenericFunctionWithBodyId::Impl(impl_func) => {
                impl_func.function_body.trait_function(db)?
            }
            GenericFunctionWithBodyId::Trait(trait_func) => trait_func.trait_function(db),
        };
        Ok(trait_function == panic_destruct_trait_fn(db.upcast()))
    }
}

impl UnstableSalsaId for ConcreteFunctionWithBodyId {
    fn get_internal_id(&self) -> &salsa::InternId {
        &self.0
    }
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, SemanticObject)]
pub struct ConcreteFunction {
    pub generic_function: GenericFunctionId,
    pub generic_args: Vec<semantic::GenericArgumentId>,
}
impl ConcreteFunction {
    pub fn body(&self, db: &dyn SemanticGroup) -> Maybe<Option<ConcreteFunctionWithBodyId>> {
        let Some(generic_function) =
            GenericFunctionWithBodyId::from_generic(db, self.generic_function)?
        else {
            return Ok(None);
        };
        Ok(Some(
            ConcreteFunctionWithBody { generic_function, generic_args: self.generic_args.clone() }
                .intern(db),
        ))
    }
    pub fn full_name(&self, db: &dyn SemanticGroup) -> String {
        let maybe_generic_part = if !self.generic_args.is_empty() {
            let mut generics = String::new();
            for (i, arg) in self.generic_args.iter().enumerate() {
                if i > 0 {
                    generics.push_str(", ");
                }
                generics.push_str(&arg.format(db));
            }
            format!("::<{generics}>")
        } else {
            "".to_string()
        };
        format!("{}{maybe_generic_part}", self.generic_function.format(db.upcast()))
    }
}
impl DebugWithDb<dyn SemanticGroup> for ConcreteFunction {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
        db: &(dyn SemanticGroup + 'static),
    ) -> std::fmt::Result {
        write!(f, "{}", self.generic_function.format(db.upcast()))?;
        if !self.generic_args.is_empty() {
            write!(f, "::<")?;
            for (i, arg) in self.generic_args.iter().enumerate() {
                if i > 0 {
                    write!(f, ", ")?;
                }
                write!(f, "{:?}", arg.debug(db))?;
            }
            write!(f, ">")?;
        }
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, Eq, DebugWithDb, SemanticObject)]
#[debug_db(dyn SemanticGroup + 'static)]
pub struct Signature {
    pub params: Vec<semantic::Parameter>,
    pub return_type: semantic::TypeId,
    /// implicit parameters
    pub implicits: Vec<semantic::TypeId>,
    #[dont_rewrite]
    pub panicable: bool,
    #[hide_field_debug_with_db]
    #[dont_rewrite]
    pub stable_ptr: ast::FunctionSignaturePtr,
}

impl Signature {
    pub fn from_ast(
        diagnostics: &mut SemanticDiagnostics,
        db: &dyn SemanticGroup,
        resolver: &mut Resolver<'_>,
        signature_syntax: &ast::FunctionSignature,
        function_title_id: FunctionTitleId,
        environment: &mut Environment,
    ) -> Self {
        let syntax_db = db.upcast();
        let params = function_signature_params(
            diagnostics,
            db,
            resolver,
            &signature_syntax.parameters(syntax_db).elements(syntax_db),
            Some(function_title_id),
            environment,
        );
        let return_type =
            function_signature_return_type(diagnostics, db, resolver, signature_syntax);
        let implicits =
            function_signature_implicit_parameters(diagnostics, db, resolver, signature_syntax);
        let panicable = match signature_syntax.optional_no_panic(db.upcast()) {
            ast::OptionTerminalNoPanic::Empty(_) => true,
            ast::OptionTerminalNoPanic::TerminalNoPanic(_) => false,
        };
        let stable_ptr = signature_syntax.stable_ptr();
        semantic::Signature { params, return_type, implicits, panicable, stable_ptr }
    }
}

pub fn function_signature_return_type(
    diagnostics: &mut SemanticDiagnostics,
    db: &dyn SemanticGroup,
    resolver: &mut Resolver<'_>,
    sig: &ast::FunctionSignature,
) -> semantic::TypeId {
    let ty_syntax = match sig.ret_ty(db.upcast()) {
        ast::OptionReturnTypeClause::Empty(_) => {
            return unit_ty(db);
        }
        ast::OptionReturnTypeClause::ReturnTypeClause(ret_type_clause) => {
            ret_type_clause.ty(db.upcast())
        }
    };
    resolve_type(db, diagnostics, resolver, &ty_syntax)
}

/// Returns the implicit parameters of the given function signature's AST.
pub fn function_signature_implicit_parameters(
    diagnostics: &mut SemanticDiagnostics,
    db: &dyn SemanticGroup,
    resolver: &mut Resolver<'_>,
    sig: &ast::FunctionSignature,
) -> Vec<semantic::TypeId> {
    let syntax_db = db.upcast();

    let ast_implicits = match sig.implicits_clause(syntax_db) {
        ast::OptionImplicitsClause::Empty(_) => Vec::new(),
        ast::OptionImplicitsClause::ImplicitsClause(implicits_clause) => {
            implicits_clause.implicits(syntax_db).elements(syntax_db)
        }
    };

    let mut implicits = Vec::new();
    for implicit in ast_implicits {
        implicits.push(resolve_type(
            db,
            diagnostics,
            resolver,
            &syntax::node::ast::Expr::Path(implicit),
        ));
    }
    implicits
}

/// Returns the parameters of the given function signature's AST.
pub fn function_signature_params(
    diagnostics: &mut SemanticDiagnostics,
    db: &dyn SemanticGroup,
    resolver: &mut Resolver<'_>,
    params: &[ast::Param],
    function_title_id: Option<FunctionTitleId>,
    env: &mut Environment,
) -> Vec<semantic::Parameter> {
    update_env_with_ast_params(diagnostics, db, resolver, params, function_title_id, env)
}

/// Query implementation of [crate::db::SemanticGroup::function_title_signature].
pub fn function_title_signature(
    db: &dyn SemanticGroup,
    function_title_id: FunctionTitleId,
) -> Maybe<Signature> {
    match function_title_id {
        FunctionTitleId::Free(free_function) => db.free_function_signature(free_function),
        FunctionTitleId::Extern(extern_function) => db.extern_function_signature(extern_function),
        FunctionTitleId::Trait(trait_function) => db.trait_function_signature(trait_function),
        FunctionTitleId::Impl(impl_function) => db.impl_function_signature(impl_function),
    }
}
/// Query implementation of [crate::db::SemanticGroup::function_title_generic_params].
pub fn function_title_generic_params(
    db: &dyn SemanticGroup,
    function_title_id: FunctionTitleId,
) -> Maybe<Vec<semantic::GenericParam>> {
    match function_title_id {
        FunctionTitleId::Free(free_function) => db.free_function_generic_params(free_function),
        FunctionTitleId::Extern(extern_function) => {
            db.extern_function_declaration_generic_params(extern_function)
        }
        FunctionTitleId::Trait(trait_function) => db.trait_function_generic_params(trait_function),
        FunctionTitleId::Impl(impl_function) => db.impl_function_generic_params(impl_function),
    }
}

/// Query implementation of [crate::db::SemanticGroup::concrete_function_signature].
pub fn concrete_function_signature(
    db: &dyn SemanticGroup,
    function_id: FunctionId,
) -> Maybe<Signature> {
    let ConcreteFunction { generic_function, generic_args, .. } =
        function_id.lookup_intern(db).function;
    let generic_params = generic_function.generic_params(db)?;
    let generic_signature = generic_function.generic_signature(db)?;
    // TODO(spapini): When trait generics are supported, they need to be substituted
    //   one by one, not together.
    // Panic shouldn't occur since ConcreteFunction is assumed to be constructed correctly.
    let substitution = GenericSubstitution::new(&generic_params, &generic_args);
    SubstitutionRewriter { db, substitution: &substitution }.rewrite(generic_signature)
}

/// For a given list of AST parameters, returns the list of semantic parameters along with the
/// corresponding environment.
fn update_env_with_ast_params(
    diagnostics: &mut SemanticDiagnostics,
    db: &dyn SemanticGroup,
    resolver: &mut Resolver<'_>,
    ast_params: &[ast::Param],
    function_title_id: Option<FunctionTitleId>,
    env: &mut Environment,
) -> Vec<semantic::Parameter> {
    let mut semantic_params = Vec::new();
    for ast_param in ast_params.iter() {
        let semantic_param = ast_param_to_semantic(diagnostics, db, resolver, ast_param);

        if env.add_param(diagnostics, semantic_param.clone(), ast_param, function_title_id).is_ok()
        {
            semantic_params.push(semantic_param);
        }
    }
    semantic_params
}

/// Returns a semantic parameter (and its name) for the given AST parameter.
fn ast_param_to_semantic(
    diagnostics: &mut SemanticDiagnostics,
    db: &dyn SemanticGroup,
    resolver: &mut Resolver<'_>,
    ast_param: &ast::Param,
) -> semantic::Parameter {
    let syntax_db = db.upcast();

    let name = ast_param.name(syntax_db).text(syntax_db);

    let id = ParamLongId(resolver.module_file_id, ast_param.stable_ptr()).intern(db);

    let ty = match ast_param.type_clause(syntax_db) {
        ast::OptionTypeClause::Empty(missing) => {
            resolver.inference().new_type_var(Some(missing.stable_ptr().untyped()))
        }
        ast::OptionTypeClause::TypeClause(ty_syntax) => {
            resolve_type(db, diagnostics, resolver, &ty_syntax.ty(syntax_db))
        }
    };

    let mutability = modifiers::compute_mutability(
        diagnostics,
        syntax_db,
        &ast_param.modifiers(syntax_db).elements(syntax_db),
    );

    semantic::Parameter {
        id,
        name,
        ty,
        mutability,
        stable_ptr: ast_param.name(syntax_db).stable_ptr(),
    }
}

// === Function Declaration ===

#[derive(Clone, Debug, PartialEq, Eq, DebugWithDb)]
#[debug_db(dyn SemanticGroup + 'static)]
pub struct FunctionDeclarationData {
    pub diagnostics: Diagnostics<SemanticDiagnostic>,
    pub signature: semantic::Signature,
    /// The environment induced by the function's signature.
    pub environment: Environment,
    pub generic_params: Vec<semantic::GenericParam>,
    pub attributes: Vec<Attribute>,
    pub resolver_data: Arc<ResolverData>,
    pub inline_config: InlineConfiguration,
    /// Order of implicits to follow by this function.
    ///
    /// For example, this can be used to enforce ABI compatibility with Starknet OS.
    pub implicit_precedence: ImplicitPrecedence,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum InlineConfiguration {
    /// The user did not specify any inlining preferences.
    None,
    Always(Attribute),
    Should(Attribute),
    Never(Attribute),
}

/// If a function with impl generic parameters is marked as '#[inline(always)]', raise a diagnostic.
pub fn forbid_inline_always_with_impl_generic_param(
    diagnostics: &mut SemanticDiagnostics,
    generic_params: &[GenericParam],
    inline_config: &InlineConfiguration,
) {
    let has_impl_generic_param = generic_params.iter().any(|p| matches!(p, GenericParam::Impl(_)));
    match &inline_config {
        InlineConfiguration::Always(attr) if has_impl_generic_param => {
            diagnostics.report(
                attr.stable_ptr.untyped(),
                SemanticDiagnosticKind::InlineAlwaysWithImplGenericArgNotAllowed,
            );
        }
        _ => {}
    }
}

/// Extra information about sorting of implicit arguments of the function.
///
/// In most of the user written code, the implicits are not stated explicitly, but instead are
/// inferred by the compiler. The order on how these implicit arguments are laid out on Sierra level
/// is unspecified though for the users. Currently, the compiler sorts them alphabetically by name
/// for reproducibility, but it can equally just randomize the order on each compilation.
///
/// Some compilation targets tend to expect that particular functions accept particular implicit
/// arguments at fixed positions. For example, the Starknet OS has such assumptions. By reading the
/// implicit precedence information attached to functions, the compiler can now reliably generate
/// compatible code.
///
/// To set, add the `#[implicit_precedence(...)]` attribute to function declaration. Only free or
/// impl functions can have this information defined. For extern functions, the compiler raises an
/// error. It is recommended to always create this attribute from compiler plugins, and not force
/// users to write it manually.
///
/// Use [ImplicitPrecedence::UNSPECIFIED] to represent lack of information.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ImplicitPrecedence(Vec<TypeId>);

impl ImplicitPrecedence {
    /// A precedence that does not actually prefer any implicit.
    ///
    /// When applied to a sequence of implicits, they will just be reordered alphabetically.
    pub const UNSPECIFIED: Self = Self(Vec::new());

    /// Sort implicits according to this precedence: first the ones with precedence
    /// (according to it), then the others by their name.
    pub fn apply(&self, implicits: &mut [TypeId], db: &dyn SemanticGroup) {
        implicits.sort_by_cached_key(|implicit| {
            if let Some(idx) = self.0.iter().position(|item| item == implicit) {
                return (idx, "".to_string());
            }

            (self.0.len(), implicit.format(db))
        });
    }
}

impl FromIterator<TypeId> for ImplicitPrecedence {
    fn from_iter<T: IntoIterator<Item = TypeId>>(iter: T) -> Self {
        Self(Vec::from_iter(iter))
    }
}