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
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
// This Source Code Form is subject to the terms of the Mozilla Public
// Lic// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// This Source Code Form is subject to the terms of the Mozilla Public
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2018, Olof Kraigher olof.kraigher@gmail.com

// Allowing this, since box_patterns are feature gated: https://github.com/rust-lang/rfcs/pull/469
// Track here: https://github.com/rust-lang/rust/issues/29641
#![allow(clippy::large_enum_variant)]

mod display;
mod name_util;

#[macro_use]
pub mod search;

pub use self::display::*;
pub use self::name_util::*;

use crate::latin_1::Latin1String;
pub use crate::source::{HasSource, HasSrcPos, Position, Source, SrcPos, WithPos};
pub use crate::symbol_table::Symbol;

/// LRM 15.8 Bit string literals
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum BaseSpecifier {
    B,
    O,
    X,
    UB,
    UO,
    UX,
    SB,
    SO,
    SX,
    D,
}

#[derive(PartialEq, Copy, Clone, Debug)]
pub enum Unary {
    And,
    Or,
    Nand,
    Nor,
    Xor,
    Xnor,
    Abs,
    Not,
    Minus,
    Plus,
    QueQue, // ?? conditional operator
}

#[derive(PartialEq, Copy, Clone, Debug)]
pub enum Binary {
    And,
    Or,
    Nand,
    Nor,
    Xor,
    Xnor,

    EQ,
    NE,
    LT,
    LTE,
    GT,
    GTE,
    QueEQ,
    QueNE,
    QueLT,
    QueLTE,
    QueGT,
    QueGTE,

    SLL,
    SRL,
    SLA,
    SRA,
    ROL,
    ROR,

    Plus,
    Minus,
    Concat,

    Times,
    Div,
    Mod,
    Rem,

    Pow,
}

/// LRM 8.6 Attribute names
#[derive(PartialEq, Debug, Clone)]
pub struct AttributeName {
    pub name: WithPos<Name>,
    pub signature: Option<Signature>,
    pub attr: Ident,
    pub expr: Option<Box<WithPos<Expression>>>,
}

/// LRM 8.7 External names
#[derive(PartialEq, Debug, Clone)]
pub enum ExternalObjectClass {
    Constant,
    Signal,
    Variable,
}

/// LRM 8.7 External names
#[derive(PartialEq, Debug, Clone)]
pub enum ExternalPath {
    Package(WithPos<Name>),
    Absolute(WithPos<Name>),
    Relative(WithPos<Name>),
}

/// LRM 8.7 External names
#[derive(PartialEq, Debug, Clone)]
pub struct ExternalName {
    pub class: ExternalObjectClass,
    pub path: WithPos<ExternalPath>,
    pub subtype: SubtypeIndication,
}

/// LRM 8. Names
#[derive(PartialEq, Debug, Clone)]
pub enum Name {
    Designator(WithRef<Designator>),
    Selected(Box<WithPos<Name>>, WithPos<WithRef<Designator>>),
    SelectedAll(Box<WithPos<Name>>),
    Indexed(Box<WithPos<Name>>, Vec<WithPos<Expression>>),
    Slice(Box<WithPos<Name>>, Box<DiscreteRange>),
    Attribute(Box<AttributeName>),
    FunctionCall(Box<FunctionCall>),
    External(Box<ExternalName>),
}

/// LRM 8. Names
/// A subset of a full name allowing only selected name
#[derive(PartialEq, Debug, Clone)]
pub enum SelectedName {
    Designator(WithRef<Designator>),
    Selected(Box<WithPos<SelectedName>>, WithPos<WithRef<Designator>>),
}

/// LRM 9.3.4 Function calls
#[derive(PartialEq, Debug, Clone)]
pub struct FunctionCall {
    pub name: WithPos<Name>,
    pub parameters: Vec<AssociationElement>,
}

/// LRM 9.3.3 Aggregates
#[derive(PartialEq, Debug, Clone)]
pub enum Choice {
    Expression(WithPos<Expression>),
    DiscreteRange(DiscreteRange),
    Others,
}

/// LRM 9.3.3 Aggregates
#[derive(PartialEq, Debug, Clone)]
pub enum ElementAssociation {
    Positional(WithPos<Expression>),
    Named(Vec<Choice>, WithPos<Expression>),
}

/// LRM 6.5.7 Association Lists
#[derive(PartialEq, Debug, Clone)]
pub enum ActualPart {
    Expression(Expression),
    Open,
}

/// LRM 6.5.7 Association Lists
#[derive(PartialEq, Debug, Clone)]
pub struct AssociationElement {
    pub formal: Option<WithPos<Name>>,
    pub actual: WithPos<ActualPart>,
}

/// LRM 15.5 Abstract literals
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum AbstractLiteral {
    Integer(u64),
    Real(f64),
}

/// LRM 15.8 Bit string literals
#[derive(PartialEq, Debug, Clone)]
pub struct BitString {
    pub length: Option<u32>,
    pub base: BaseSpecifier,
    pub value: Latin1String,
}

/// LRM 9.3.2 Literals
#[derive(PartialEq, Debug, Clone)]
pub enum Literal {
    String(Latin1String),
    BitString(BitString),
    Character(u8),
    AbstractLiteral(AbstractLiteral),
    Physical(AbstractLiteral, Symbol),
    Null,
}

/// LRM 9.3.7 Allocators
#[derive(PartialEq, Debug, Clone)]
pub enum Allocator {
    Qualified(QualifiedExpression),
    Subtype(SubtypeIndication),
}

/// LRM 9.3.5 Qualified expressions
#[derive(PartialEq, Debug, Clone)]
pub struct QualifiedExpression {
    pub name: Box<WithPos<Name>>,
    pub expr: Box<WithPos<Expression>>,
}

/// LRM 9. Expressions
#[derive(PartialEq, Debug, Clone)]
pub enum Expression {
    Binary(Binary, Box<WithPos<Expression>>, Box<WithPos<Expression>>),
    Unary(Unary, Box<WithPos<Expression>>),

    /// LRM 9.3.3 Aggregates
    Aggregate(Vec<ElementAssociation>),

    /// LRM 9.3.5 Qualified expressions
    Qualified(Box<QualifiedExpression>),

    /// LRM 8 Names
    Name(Box<Name>),

    /// LRM 9.3.2 Literals
    Literal(Literal),

    /// LRM 9.3.7 Allocators
    New(Box<WithPos<Allocator>>),
}

pub type Ident = WithPos<Symbol>;

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum Direction {
    Ascending,
    Descending,
}

/// LRM discrete_range
/// discrete_range ::= discrete_subtype_indication | range
/// range ::=
///     range_attribute_name
///   | simple_expression direction simple_expression

#[derive(PartialEq, Debug, Clone)]
pub enum DiscreteRange {
    Discrete(WithPos<SelectedName>, Option<Range>),
    Range(Range),
}

#[derive(PartialEq, Debug, Clone)]
pub struct RangeConstraint {
    pub direction: Direction,
    pub left_expr: Box<WithPos<Expression>>,
    pub right_expr: Box<WithPos<Expression>>,
}

#[derive(PartialEq, Debug, Clone)]
pub enum Range {
    Range(RangeConstraint),
    Attribute(Box<AttributeName>),
}

/// LRM: record_element_constraint
#[derive(PartialEq, Debug, Clone)]
pub struct ElementConstraint {
    pub ident: Ident,
    pub constraint: Box<WithPos<SubtypeConstraint>>,
}

#[derive(PartialEq, Debug, Clone)]
pub enum SubtypeConstraint {
    Range(Range),
    /// Empty Vec means Open
    Array(Vec<DiscreteRange>, Option<Box<WithPos<SubtypeConstraint>>>),
    Record(Vec<ElementConstraint>),
}

/// LRM 6.3 Subtype declarations
#[derive(PartialEq, Debug, Clone)]
pub struct RecordElementResolution {
    pub ident: Ident,
    pub resolution: Box<ResolutionIndication>,
}

/// LRM 6.3 Subtype declarations
#[derive(PartialEq, Debug, Clone)]
pub enum ResolutionIndication {
    FunctionName(WithPos<SelectedName>),
    ArrayElement(WithPos<SelectedName>),
    Record(Vec<RecordElementResolution>),
    Unresolved,
}

/// LRM 6.3 Subtype declarations
#[derive(PartialEq, Debug, Clone)]
pub struct SubtypeIndication {
    pub resolution: ResolutionIndication,
    pub type_mark: WithPos<SelectedName>,
    pub constraint: Option<WithPos<SubtypeConstraint>>,
}

/// LRM 5.3 Array Types
#[derive(PartialEq, Debug, Clone)]
pub enum ArrayIndex {
    /// Unbounded
    /// {identifier} range <>
    IndexSubtypeDefintion(WithPos<SelectedName>),

    /// Constraint
    Discrete(DiscreteRange),
}

/// LRM 5.3.3 Record types
#[derive(PartialEq, Debug, Clone)]
pub struct ElementDeclaration {
    pub ident: Ident,
    pub subtype: SubtypeIndication,
}

/// LRM 5.6.2 Protected type declarations
#[derive(PartialEq, Debug, Clone)]
pub enum ProtectedTypeDeclarativeItem {
    Subprogram(SubprogramDeclaration),
}

#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub enum Designator {
    Identifier(Symbol),
    OperatorSymbol(Latin1String),
    Character(u8),
}

/// An item which has a reference to a declaration
#[derive(PartialEq, Debug, Clone)]
pub struct WithRef<T> {
    pub item: T,
    // @TODO just store the positions of the reference for now
    pub reference: Option<SrcPos>,
}

impl<T> WithRef<T> {
    pub fn new(item: T) -> WithRef<T> {
        WithRef {
            item,
            reference: None,
        }
    }

    pub fn reference_positions(&self) -> impl Iterator<Item = &SrcPos> {
        self.reference.iter()
    }
}

/// LRM 6.6 Alias declarations
#[derive(PartialEq, Debug, Clone)]
pub struct AliasDeclaration {
    pub designator: WithPos<Designator>,
    pub subtype_indication: Option<SubtypeIndication>,
    pub name: WithPos<Name>,
    pub signature: Option<Signature>,
}

/// LRM 6.7 Attribute declarations
#[derive(PartialEq, Debug, Clone)]
pub struct AttributeDeclaration {
    pub ident: Ident,
    pub type_mark: WithPos<SelectedName>,
}

/// LRM 7.2 Attribute specification
#[derive(PartialEq, Debug, Clone)]
pub struct EntityTag {
    pub designator: WithPos<Designator>,
    pub signature: Option<Signature>,
}

/// LRM 7.2 Attribute specification
#[derive(PartialEq, Debug, Clone)]
pub enum EntityName {
    Name(EntityTag),
    All,
    Others,
}

/// LRM 7.2 Attribute specification
// @TODO there are more classes
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum EntityClass {
    Entity,
    Architecture,
    Configuration,
    Package,
    Signal,
    Variable,
    Procedure,
    Function,
    Component,
    Constant,
    Type,
    Label,
}

/// LRM 7.2 Attribute specification
#[derive(PartialEq, Debug, Clone)]
pub struct AttributeSpecification {
    pub ident: Ident,
    pub entity_name: EntityName,
    pub entity_class: EntityClass,
    pub expr: WithPos<Expression>,
}

/// LRM 7.2 Attribute specification
#[derive(PartialEq, Debug, Clone)]
pub enum Attribute {
    Specification(AttributeSpecification),
    Declaration(AttributeDeclaration),
}

/// LRM 5.6.2 Protected type declarations
#[derive(PartialEq, Debug, Clone)]
pub struct ProtectedTypeDeclaration {
    pub items: Vec<ProtectedTypeDeclarativeItem>,
}

/// LRM 5.6.3 Protected type bodies
#[derive(PartialEq, Debug, Clone)]
pub struct ProtectedTypeBody {
    pub decl: Vec<Declaration>,
}

/// LRM 5.4.2 Physical type declaration
#[derive(PartialEq, Debug, Clone)]
pub struct PhysicalTypeDeclaration {
    pub range: Range,
    pub primary_unit: Ident,
    pub secondary_units: Vec<(Ident, Literal)>,
}

/// LRM 5.2.2 Enumeration types
#[derive(PartialEq, Debug, Clone)]
pub enum EnumerationLiteral {
    Identifier(Symbol),
    Character(u8),
}

/// LRM 5 Types
#[derive(PartialEq, Debug, Clone)]
pub enum TypeDefinition {
    /// LRM 5.2 Scalar Types
    /// LRM 5.2.2 Enumeration types
    Enumeration(Vec<WithPos<EnumerationLiteral>>),
    /// LRM 5.2.3 Integer types
    Integer(Range),
    /// LRM 5.2.4 Physical types
    Physical(PhysicalTypeDeclaration),
    // @TODO floating
    /// LRM 5.3 Composite Types
    /// LRM 5.3.2 Array types
    Array(Vec<ArrayIndex>, SubtypeIndication),
    /// LRM 5.3.3 Record types
    Record(Vec<ElementDeclaration>),
    /// LRM 5.4 Access types
    Access(SubtypeIndication),
    /// LRM 5.4.2 Incomplete type declarations
    Incomplete,
    /// LRM 5.5 File types
    File(WithPos<SelectedName>),
    /// LRM 5.6 Protected types
    Protected(ProtectedTypeDeclaration),
    ProtectedBody(ProtectedTypeBody),
    /// LRM 6.3 Subtype declarations
    Subtype(SubtypeIndication),
}

/// LRM 6.2 Type declarations
#[derive(PartialEq, Debug, Clone)]
pub struct TypeDeclaration {
    pub ident: Ident,
    pub def: TypeDefinition,
}

/// LRM 6.4.2 Object Declarations
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum ObjectClass {
    Signal,
    // @TODO signal_kind
    Constant,
    Variable,
    SharedVariable,
}

#[derive(PartialEq, Debug, Clone)]
pub struct ObjectDeclaration {
    pub class: ObjectClass,
    pub ident: Ident,
    pub subtype_indication: SubtypeIndication,
    pub expression: Option<WithPos<Expression>>,
}

#[derive(PartialEq, Debug, Clone)]
pub struct FileDeclaration {
    pub ident: Ident,
    pub subtype_indication: SubtypeIndication,
    pub open_info: Option<WithPos<Expression>>,
    pub file_name: Option<WithPos<Expression>>,
}

#[derive(PartialEq, Debug, Clone)]
pub enum SubprogramDesignator {
    Identifier(Symbol),
    OperatorSymbol(Latin1String),
}

/// LRM 4.2 Subprogram declaration
#[derive(PartialEq, Debug, Clone)]
pub struct ProcedureSpecification {
    pub designator: WithPos<SubprogramDesignator>,
    pub parameter_list: Vec<InterfaceDeclaration>,
}

/// LRM 4.2 Subprogram declaration
#[derive(PartialEq, Debug, Clone)]
pub struct FunctionSpecification {
    pub pure: bool,
    pub designator: WithPos<SubprogramDesignator>,
    pub parameter_list: Vec<InterfaceDeclaration>,
    pub return_type: WithPos<SelectedName>,
}

/// LRM 4.3 Subprogram bodies
#[derive(PartialEq, Debug, Clone)]
pub struct SubprogramBody {
    pub specification: SubprogramDeclaration,
    pub declarations: Vec<Declaration>,
    pub statements: Vec<LabeledSequentialStatement>,
}

/// LRM 4.5.3 Signatures
#[derive(PartialEq, Debug, Clone)]
pub enum Signature {
    Function(Vec<WithPos<SelectedName>>, WithPos<SelectedName>),
    Procedure(Vec<WithPos<SelectedName>>),
}

#[derive(PartialEq, Debug, Clone)]
pub enum SubprogramDeclaration {
    Procedure(ProcedureSpecification),
    Function(FunctionSpecification),
}

#[derive(PartialEq, Debug, Clone)]
pub struct InterfaceFileDeclaration {
    pub ident: Ident,
    pub subtype_indication: SubtypeIndication,
}

/// LRM 6.5.2 Interface object declarations
#[derive(PartialEq, Debug, Clone)]
pub struct InterfaceObjectDeclaration {
    pub class: ObjectClass,
    pub ident: Ident,
    pub mode: Mode,
    pub subtype_indication: SubtypeIndication,
    pub expression: Option<WithPos<Expression>>,
}

#[derive(PartialEq, Debug, Clone)]
pub enum SubprogramDefault {
    Name(WithPos<SelectedName>),
    Box,
}
/// LRM 6.5.5 Interface package declaration
#[derive(PartialEq, Debug, Clone)]
pub enum InterfacePackageGenericMapAspect {
    Map(Vec<AssociationElement>),
    Box,
    Default,
}

/// LRM 6.5.5 Interface package declaration
#[derive(PartialEq, Debug, Clone)]
pub struct InterfacePackageDeclaration {
    pub ident: Ident,
    pub package_name: WithPos<SelectedName>,
    pub generic_map: InterfacePackageGenericMapAspect,
}

#[derive(PartialEq, Debug, Clone)]
pub enum InterfaceDeclaration {
    Object(InterfaceObjectDeclaration),
    File(InterfaceFileDeclaration),
    Type(Ident),
    /// LRM 6.5.4 Interface subprogram declarations
    Subprogram(SubprogramDeclaration, Option<SubprogramDefault>),
    /// LRM 6.5.5 Interface package declaration
    Package(InterfacePackageDeclaration),
}

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum Mode {
    In,
    Out,
    InOut,
    Buffer,
    Linkage,
}

#[derive(PartialEq, Debug, Clone)]
pub struct PortClause {
    pub port_list: Vec<InterfaceDeclaration>,
}

/// LRM 6.8 Component declarations
#[derive(PartialEq, Debug, Clone)]
pub struct ComponentDeclaration {
    pub ident: Ident,
    pub generic_list: Vec<InterfaceDeclaration>,
    pub port_list: Vec<InterfaceDeclaration>,
}

#[derive(PartialEq, Debug, Clone)]
pub enum Declaration {
    Object(ObjectDeclaration),
    File(FileDeclaration),
    Type(TypeDeclaration),
    Component(ComponentDeclaration),
    Attribute(Attribute),
    Alias(AliasDeclaration),
    SubprogramDeclaration(SubprogramDeclaration),
    SubprogramBody(SubprogramBody),
    Use(WithPos<UseClause>),
    Package(PackageInstantiation),
    Configuration(ConfigurationSpecification),
}

/// LRM 10.2 Wait statement
#[derive(PartialEq, Debug, Clone)]
pub struct WaitStatement {
    pub sensitivity_clause: Vec<WithPos<Name>>,
    pub condition_clause: Option<WithPos<Expression>>,
    pub timeout_clause: Option<WithPos<Expression>>,
}

/// LRM 10.3 Assertion statement
#[derive(PartialEq, Debug, Clone)]
pub struct AssertStatement {
    pub condition: WithPos<Expression>,
    pub report: Option<WithPos<Expression>>,
    pub severity: Option<WithPos<Expression>>,
}

/// LRM 10.4 Report statement
#[derive(PartialEq, Debug, Clone)]
pub struct ReportStatement {
    pub report: WithPos<Expression>,
    pub severity: Option<WithPos<Expression>>,
}

/// LRM 10.5 Signal assignment statement
#[derive(PartialEq, Debug, Clone)]
pub enum Target {
    Name(Name),
    Aggregate(Vec<ElementAssociation>),
}

/// LRM 10.5 Signal assignment statement
#[derive(PartialEq, Debug, Clone)]
pub struct WaveformElement {
    pub value: WithPos<Expression>,
    pub after: Option<WithPos<Expression>>,
}

/// LRM 10.5 Signal assignment statement
#[derive(PartialEq, Debug, Clone)]
pub enum Waveform {
    Elements(Vec<WaveformElement>),
    Unaffected,
}

/// LRM 10.5 Signal assignment statement
#[derive(PartialEq, Debug, Clone)]
pub enum DelayMechanism {
    Transport,
    Inertial { reject: Option<WithPos<Expression>> },
}

/// LRM 10.5 Signal assignment statement
#[derive(PartialEq, Debug, Clone)]
pub struct SignalAssignment {
    pub target: WithPos<Target>,
    pub delay_mechanism: Option<DelayMechanism>,
    pub rhs: AssignmentRightHand<Waveform>,
}

#[derive(PartialEq, Debug, Clone)]
pub enum ForceMode {
    In,
    Out,
}

#[derive(PartialEq, Debug, Clone)]
pub struct SignalForceAssignment {
    pub target: WithPos<Target>,
    pub force_mode: Option<ForceMode>,
    pub rhs: AssignmentRightHand<WithPos<Expression>>,
}

#[derive(PartialEq, Debug, Clone)]
pub struct SignalReleaseAssignment {
    pub target: WithPos<Target>,
    pub force_mode: Option<ForceMode>,
}

/// LRM 10.6 Variable assignment statement
#[derive(PartialEq, Debug, Clone)]
pub struct VariableAssignment {
    pub target: WithPos<Target>,
    pub rhs: AssignmentRightHand<WithPos<Expression>>,
}

/// LRM 10.5 Signal assignment statement
/// LRM 10.6 Variable assignment statement
#[derive(PartialEq, Debug, Clone)]
pub enum AssignmentRightHand<T> {
    Simple(T),
    Conditional(Conditionals<T>),
    Selected(Selection<T>),
}

#[derive(PartialEq, Debug, Clone)]
pub struct Conditional<T> {
    pub condition: WithPos<Expression>,
    pub item: T,
}

#[derive(PartialEq, Debug, Clone)]
pub struct Conditionals<T> {
    pub conditionals: Vec<Conditional<T>>,
    pub else_item: Option<T>,
}

pub type ConditionalExpression = Conditional<WithPos<Expression>>;
pub type ConditionalExpressions = Conditionals<WithPos<Expression>>;

/// LRM 10.8 If statement
pub type IfStatement = Conditionals<Vec<LabeledSequentialStatement>>;

#[derive(PartialEq, Debug, Clone)]
pub struct Alternative<T> {
    pub choices: Vec<Choice>,
    pub item: T,
}

#[derive(PartialEq, Debug, Clone)]
pub struct Selection<T> {
    pub expression: WithPos<Expression>,
    pub alternatives: Vec<Alternative<T>>,
}

/// LRM 10.9 Case statement
pub type CaseStatement = Selection<Vec<LabeledSequentialStatement>>;

/// LRM 10.10 Loop statement
#[derive(PartialEq, Debug, Clone)]
pub enum IterationScheme {
    While(WithPos<Expression>),
    For(Ident, DiscreteRange),
}

/// LRM 10.10 Loop statement
#[derive(PartialEq, Debug, Clone)]
pub struct LoopStatement {
    pub iteration_scheme: Option<IterationScheme>,
    pub statements: Vec<LabeledSequentialStatement>,
}

/// LRM 10.11 Next statement
#[derive(PartialEq, Debug, Clone)]
pub struct NextStatement {
    pub loop_label: Option<Ident>,
    pub condition: Option<WithPos<Expression>>,
}

/// LRM 10.12 Exit statement
#[derive(PartialEq, Debug, Clone)]
pub struct ExitStatement {
    pub loop_label: Option<Ident>,
    pub condition: Option<WithPos<Expression>>,
}

/// LRM 10.13 Return statement
#[derive(PartialEq, Debug, Clone)]
pub struct ReturnStatement {
    pub expression: Option<WithPos<Expression>>,
}

/// LRM 10. Sequential statements
#[derive(PartialEq, Debug, Clone)]
pub enum SequentialStatement {
    Wait(WaitStatement),
    Assert(AssertStatement),
    Report(ReportStatement),
    VariableAssignment(VariableAssignment),
    SignalAssignment(SignalAssignment),
    SignalForceAssignment(SignalForceAssignment),
    SignalReleaseAssignment(SignalReleaseAssignment),
    ProcedureCall(FunctionCall),
    If(IfStatement),
    Case(CaseStatement),
    Loop(LoopStatement),
    Next(NextStatement),
    Exit(ExitStatement),
    Return(ReturnStatement),
    Null,
}

/// LRM 10. Sequential statements
#[derive(PartialEq, Debug, Clone)]
pub struct LabeledSequentialStatement {
    pub label: Option<Ident>,
    pub statement: SequentialStatement,
}

/// LRM 11.2 Block statement
#[derive(PartialEq, Debug, Clone)]
pub struct BlockStatement {
    pub guard_condition: Option<WithPos<Expression>>,
    pub decl: Vec<Declaration>,
    pub statements: Vec<LabeledConcurrentStatement>,
}

#[derive(PartialEq, Debug, Clone)]
pub enum SensitivityList {
    Names(Vec<WithPos<Name>>),
    All,
}

/// LRM 11.3 Process statement
#[derive(PartialEq, Debug, Clone)]
pub struct ProcessStatement {
    pub postponed: bool,
    pub sensitivity_list: Option<SensitivityList>,
    pub decl: Vec<Declaration>,
    pub statements: Vec<LabeledSequentialStatement>,
}

/// LRM 11.4 Concurrent procedure call statements
#[derive(PartialEq, Debug, Clone)]
pub struct ConcurrentProcedureCall {
    pub postponed: bool,
    pub call: FunctionCall,
}

/// LRM 11.5 Concurrent assertion statements
#[derive(PartialEq, Debug, Clone)]
pub struct ConcurrentAssertStatement {
    pub postponed: bool,
    pub statement: AssertStatement,
}

/// 11.6 Concurrent signal assignment statements
#[derive(PartialEq, Debug, Clone)]
pub struct ConcurrentSignalAssignment {
    pub postponed: bool,
    pub guarded: bool,
    pub target: WithPos<Target>,
    pub delay_mechanism: Option<DelayMechanism>,
    pub rhs: AssignmentRightHand<Waveform>,
}

/// 11.7 Component instantiation statements
#[derive(PartialEq, Debug, Clone)]
pub enum InstantiatedUnit {
    Component(WithPos<SelectedName>),
    Entity(WithPos<SelectedName>, Option<Ident>),
    Configuration(WithPos<SelectedName>),
}

/// 11.7 Component instantiation statements
#[derive(PartialEq, Debug, Clone)]
pub struct InstantiationStatement {
    pub unit: InstantiatedUnit,
    pub generic_map: Vec<AssociationElement>,
    pub port_map: Vec<AssociationElement>,
}

/// 11.8 Generate statements
#[derive(PartialEq, Debug, Clone)]
pub struct GenerateBody {
    pub alternative_label: Option<Ident>,
    pub decl: Option<Vec<Declaration>>,
    pub statements: Vec<LabeledConcurrentStatement>,
}

/// 11.8 Generate statements
#[derive(PartialEq, Debug, Clone)]
pub struct ForGenerateStatement {
    pub index_name: Ident,
    pub discrete_range: DiscreteRange,
    pub body: GenerateBody,
}

/// 11.8 Generate statements
pub type IfGenerateStatement = Conditionals<GenerateBody>;
pub type CaseGenerateStatement = Selection<GenerateBody>;

/// LRM 11. Concurrent statements
#[derive(PartialEq, Debug, Clone)]
pub enum ConcurrentStatement {
    ProcedureCall(ConcurrentProcedureCall),
    Block(BlockStatement),
    Process(ProcessStatement),
    Assert(ConcurrentAssertStatement),
    Assignment(ConcurrentSignalAssignment),
    Instance(InstantiationStatement),
    ForGenerate(ForGenerateStatement),
    IfGenerate(IfGenerateStatement),
    CaseGenerate(CaseGenerateStatement),
}

/// LRM 11. Concurrent statements
#[derive(PartialEq, Debug, Clone)]
pub struct LabeledConcurrentStatement {
    pub label: Option<Ident>,
    pub statement: ConcurrentStatement,
}

/// LRM 13. Design units and their analysis
#[derive(PartialEq, Debug, Clone)]
pub struct LibraryClause {
    pub name_list: Vec<Ident>,
}

/// LRM 12.4. Use clauses
#[derive(PartialEq, Debug, Clone)]
pub struct UseClause {
    pub name_list: Vec<WithPos<Name>>,
}

/// LRM 13.4 Context clauses
#[derive(PartialEq, Debug, Clone)]
pub struct ContextReference {
    pub name_list: Vec<WithPos<Name>>,
}

/// LRM 13.4 Context clauses
#[derive(PartialEq, Debug, Clone)]
pub enum ContextItem {
    Use(UseClause),
    Library(LibraryClause),
    Context(ContextReference),
}

/// LRM 13.4 Context clauses
#[derive(PartialEq, Debug, Clone)]
pub struct ContextDeclaration {
    pub ident: Ident,
    pub items: Vec<WithPos<ContextItem>>,
}

/// LRM 4.9 Package instatiation declaration
#[derive(PartialEq, Debug, Clone)]
pub struct PackageInstantiation {
    pub ident: Ident,
    pub package_name: WithPos<SelectedName>,
    pub generic_map: Option<Vec<AssociationElement>>,
}

/// LRM 7.3 Configuration specification
#[derive(PartialEq, Debug, Clone)]
pub enum InstantiationList {
    Labels(Vec<Ident>),
    Others,
    All,
}

/// LRM 7.3.2 Binding indication
#[derive(PartialEq, Debug, Clone)]
pub enum EntityAspect {
    Entity(WithPos<SelectedName>, Option<Ident>),
    Configuration(WithPos<SelectedName>),
    Open,
}

/// LRM 7.3.2 Binding indication
#[derive(PartialEq, Debug, Clone)]
pub struct BindingIndication {
    pub entity_aspect: Option<EntityAspect>,
    pub generic_map: Option<Vec<AssociationElement>>,
    pub port_map: Option<Vec<AssociationElement>>,
}

/// LRM 7.3 Configuration specification
#[derive(PartialEq, Debug, Clone)]
pub struct ComponentSpecification {
    pub instantiation_list: InstantiationList,
    pub component_name: WithPos<SelectedName>,
}

/// LRM 7.3.4 Verification unit binding indication
#[derive(PartialEq, Debug, Clone)]
pub struct VUnitBindingIndication {
    pub vunit_list: Vec<WithPos<Name>>,
}

/// LRM 7.3 Configuration specification
#[derive(PartialEq, Debug, Clone)]
pub struct ConfigurationSpecification {
    pub spec: ComponentSpecification,
    pub bind_ind: BindingIndication,
    pub vunit_bind_inds: Vec<VUnitBindingIndication>,
}

/// LRM 3.4 Configuration declarations
#[derive(PartialEq, Debug, Clone)]
pub enum ConfigurationDeclarativeItem {
    Use(WithPos<UseClause>),
    // @TODO attribute
    // @TODO group
}
/// LRM 3.4 Configuration declarations
#[derive(PartialEq, Debug, Clone)]
pub struct ComponentConfiguration {
    pub spec: ComponentSpecification,
    pub bind_ind: Option<BindingIndication>,
    pub vunit_bind_inds: Vec<VUnitBindingIndication>,
    pub block_config: Option<BlockConfiguration>,
}

/// LRM 3.4 Configuration declarations
#[derive(PartialEq, Debug, Clone)]
pub enum ConfigurationItem {
    Block(BlockConfiguration),
    Component(ComponentConfiguration),
}

/// LRM 3.4 Configuration declarations
#[derive(PartialEq, Debug, Clone)]
pub struct BlockConfiguration {
    pub block_spec: WithPos<Name>,
    pub use_clauses: Vec<UseClause>,
    pub items: Vec<ConfigurationItem>,
}

/// LRM 3.4 Configuration declarations
#[derive(PartialEq, Debug, Clone)]
pub struct ConfigurationDeclaration {
    pub ident: Ident,
    pub entity_name: WithPos<SelectedName>,
    pub decl: Vec<ConfigurationDeclarativeItem>,
    pub vunit_bind_inds: Vec<VUnitBindingIndication>,
    pub block_config: BlockConfiguration,
}

/// LRM 3.2 Entity declarations
#[derive(PartialEq, Debug, Clone)]
pub struct EntityDeclaration {
    pub ident: Ident,
    pub generic_clause: Option<Vec<InterfaceDeclaration>>,
    pub port_clause: Option<Vec<InterfaceDeclaration>>,
    pub decl: Vec<Declaration>,
    pub statements: Vec<LabeledConcurrentStatement>,
}
/// LRM 3.3 Architecture bodies
#[derive(PartialEq, Debug, Clone)]
pub struct ArchitectureBody {
    pub ident: Ident,
    pub entity_name: WithRef<Ident>,
    pub decl: Vec<Declaration>,
    pub statements: Vec<LabeledConcurrentStatement>,
}

/// LRM 4.7 Package declarations
#[derive(PartialEq, Debug, Clone)]
pub struct PackageDeclaration {
    pub ident: Ident,
    pub generic_clause: Option<Vec<InterfaceDeclaration>>,
    pub decl: Vec<Declaration>,
}

/// LRM 4.8 Package bodies
#[derive(PartialEq, Debug, Clone)]
pub struct PackageBody {
    pub ident: WithRef<Ident>,
    pub decl: Vec<Declaration>,
}

pub type EntityUnit = DesignUnit<EntityDeclaration>;
pub type ArchitectureUnit = DesignUnit<ArchitectureBody>;
pub type ConfigurationUnit = DesignUnit<ConfigurationDeclaration>;
pub type PackageUnit = DesignUnit<PackageDeclaration>;
pub type PackageBodyUnit = DesignUnit<crate::ast::PackageBody>;
pub type PackageInstanceUnit = DesignUnit<PackageInstantiation>;

/// LRM 13.1 Design units
#[derive(PartialEq, Debug, Clone)]
pub enum AnyPrimaryUnit {
    /// LRM 3.2 Entity declaration
    EntityDeclaration(EntityUnit),

    /// LRM 3.4 Configuration declarations
    Configuration(ConfigurationUnit),

    /// LRM 4.7 Package declarations
    PackageDeclaration(PackageUnit),

    /// LRM 4.9 Package instatiation declaration
    PackageInstance(PackageInstanceUnit),

    /// LRM 13.4 Context clauses
    ContextDeclaration(ContextDeclaration),
}

/// LRM 13.1 Design units
#[derive(PartialEq, Debug, Clone)]
pub enum AnySecondaryUnit {
    /// LRM 3.3 Architecture bodies
    Architecture(ArchitectureUnit),

    /// LRM 4.8 Package bodies
    PackageBody(PackageBodyUnit),
}

pub type ContextClause = Vec<WithPos<ContextItem>>;

/// LRM 13.1 Design units
#[derive(PartialEq, Debug, Clone)]
pub struct DesignUnit<T> {
    pub context_clause: ContextClause,
    pub unit: T,
}

/// LRM 13.1 Design units
#[derive(PartialEq, Debug, Clone)]
pub enum AnyDesignUnit {
    Primary(AnyPrimaryUnit),
    Secondary(AnySecondaryUnit),
}

#[derive(PartialEq, Debug, Clone)]
pub struct DesignFile {
    pub design_units: Vec<AnyDesignUnit>,
}