moore-vhdl 0.1.0

The VHDL implementation of the moore compiler framework.
Documentation
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
// Copyright (c) 2017 Fabian Schuiki

//! The High-level Intermediate Representation of a VHDL design.

use moore_common::source::*;
use moore_common::name::*;
use moore_common::util::HasSpan;
use score::*;
use typed_arena::Arena;
use syntax::ast;
use konst::*;
pub use syntax::ast::Dir;

/// A collection of arenas where HIR nodes may be allocated.
make_arenas!(
	pub struct Arenas {
		lib:                 Lib,
		entity:              Entity,
		arch:                Arch,
		intf_sig:            IntfSignal,
		subtype_ind:         SubtypeInd,
		package:             Package,
		package_body:        PackageBody,
		package_inst:        PackageInst,
		type_decl:           TypeDecl,
		subtype_decl:        SubtypeDecl,
		expr:                Expr,
		aggregate:           Aggregate,
		const_decl:          Decl<ConstDecl>,
		signal_decl:         Decl<SignalDecl>,
		variable_decl:       Decl<VarDecl>,
		file_decl:           Decl<FileDecl>,
		process_stmt:        ProcessStmt,
		sig_assign_stmt:     SigAssignStmt,
		array_type_index:    Spanned<ArrayTypeIndex>,
		subprog:             Subprog,
		subprog_body:        SubprogBody,
		subprog_inst:        SubprogInst,
		type_mark:           TypeMarkRef,
		wait_stmt:           Stmt<WaitStmt>,
		assert_stmt:         Stmt<AssertStmt>,
		report_stmt:         Stmt<ReportStmt>,
		// sig_assign_stmt:     Stmt<SigAssignStmt>,
		var_assign_stmt:     Stmt<VarAssignStmt>,
		call_stmt:           Stmt<CallStmt>,
		if_stmt:             Stmt<IfStmt>,
		case_stmt:           Stmt<CaseStmt>,
		loop_stmt:           Stmt<LoopStmt>,
		nexit_stmt:          Stmt<NexitStmt>,
		return_stmt:         Stmt<ReturnStmt>,
		null_stmt:           Stmt<NullStmt>,
	}
);

impl Arenas {
	/// Create a new set of arenas.
	pub fn new() -> Arenas {
		Default::default()
	}
}


#[derive(Debug)]
pub struct Lib {
	pub entities: Vec<EntityRef>,
	pub cfgs: Vec<CfgRef>,
	pub pkg_decls: Vec<PkgDeclRef>,
	pub pkg_insts: Vec<PkgInstRef>,
	pub ctxs: Vec<CtxRef>,
	pub archs: Vec<ArchRef>,
	pub pkg_bodies: Vec<PkgBodyRef>,
}

impl Lib {
	pub fn new() -> Lib {
		Lib {
			entities: Vec::new(),
			cfgs: Vec::new(),
			pkg_decls: Vec::new(),
			pkg_insts: Vec::new(),
			ctxs: Vec::new(),
			archs: Vec::new(),
			pkg_bodies: Vec::new(),
		}
	}
}


#[derive(Debug)]
pub struct Entity {
	/// The context items associated with the entity.
	pub ctx_items: CtxItemsRef,
	/// The library in which the entity is defined.
	pub lib: LibRef,
	/// The entity name.
	pub name: Spanned<Name>,
	/// The list of generics that the entity declares.
	pub generics: Vec<GenericRef>,
	/// The list of ports that the entity declares.
	pub ports: Vec<IntfSignalRef>,
}


#[derive(Debug)]
pub struct Arch {
	/// The context items associated with the entity.
	pub ctx_items: CtxItemsRef,
	/// The entity of the architecture.
	pub entity: EntityRef,
	/// The architecture name.
	pub name: Spanned<Name>,
	/// The list of declarations in the architecture.
	pub decls: Vec<DeclInBlockRef>,
	/// The list of statements in the architecture.
	pub stmts: Vec<ConcStmtRef>,
}


#[derive(Debug)]
pub struct IntfSignal {
	/// The name of this signal.
	pub name: Spanned<Name>,
	/// The mode of this signal.
	pub mode: IntfSignalMode,
	/// The type of this signal.
	pub ty: SubtypeIndRef,
	/// Whether this signal was declared with the `bus` keyword.
	pub bus: bool,
	/// The expression determining the initial value of this signals.
	pub init: Option<ExprRef>,
}


#[derive(Debug, Clone, Copy)]
pub enum IntfSignalMode {
	In,
	Out,
	Inout,
	Buffer,
	Linkage,
}


#[derive(Debug)]
pub struct SubtypeInd {
	/// The location within the source code.
	pub span: Span,
	/// The type mark.
	pub type_mark: Spanned<TypeMarkRef>,
	/// The optional constraint.
	pub constraint: Option<Spanned<Constraint>>,
}


/// A constraint.
///
/// See IEEE 1076-2008 section 6.3.
///
/// ```ignore
/// constraint := range_constraint | array_constraint | record_constraint
/// ```
#[derive(Debug)]
pub enum Constraint {
	/// A range constraint.
	Range(Range),
	/// An array constraint.
	Array(ArrayConstraint),
	/// A record constraint.
	Record(RecordConstraint),
}

impl From<ArrayConstraint> for Constraint {
	fn from(value: ArrayConstraint) -> Constraint {
		Constraint::Array(value)
	}
}

impl From<RecordConstraint> for Constraint {
	fn from(value: RecordConstraint) -> Constraint {
		Constraint::Record(value)
	}
}

/// An element constraint.
///
/// See IEEE 1076-2008 section 6.3.
///
/// ```ignore
/// element_constraint := array_constraint | record_constraint
/// ```
#[derive(Debug)]
pub enum ElementConstraint {
	Array(ArrayConstraint),
	Record(RecordConstraint),
}

impl HasSpan for ElementConstraint {
	fn span(&self) -> Span {
		match *self {
			ElementConstraint::Array(ref n) => n.span(),
			ElementConstraint::Record(ref n) => n.span(),
		}
	}
}

impl From<ArrayConstraint> for ElementConstraint {
	fn from(value: ArrayConstraint) -> ElementConstraint {
		ElementConstraint::Array(value)
	}
}

impl From<RecordConstraint> for ElementConstraint {
	fn from(value: RecordConstraint) -> ElementConstraint {
		ElementConstraint::Record(value)
	}
}

/// An array constraint.
///
/// See IEEE 1076-2008 section 5.3.2.
///
/// ```ignore
/// array_constraint :=
///     index_constraint [array.element_constraint] |
///     "(" "open" ")" [array.element_constraint]
/// ```
#[derive(Debug)]
pub struct ArrayConstraint {
	/// The span this constraint covers.
	pub span: Span,
	/// The index constraint. An empty vector corresponds to the `open`
	/// constraint.
	pub index: Vec<Spanned<DiscreteRange>>,
	/// The optional element constraint.
	pub elem: Option<Box<Spanned<ElementConstraint>>>,
}

impl HasSpan for ArrayConstraint {
	fn span(&self) -> Span {
		self.span
	}
}

/// A discrete range.
///
/// See IEEE 1076-2008 section 5.3.2.1.
///
/// ```ignore
/// discrete_range := discrete.subtype_indication | range
/// ```
#[derive(Debug)]
pub enum DiscreteRange {
	/// A discrete range specified by a discrete subtype.
	Subtype(SubtypeIndRef),
	/// A discrete range specified by a range.
	Range(Range),
}

impl From<SubtypeIndRef> for DiscreteRange {
	fn from(value: SubtypeIndRef) -> DiscreteRange {
		DiscreteRange::Subtype(value)
	}
}

impl From<Range> for DiscreteRange {
	fn from(value: Range) -> DiscreteRange {
		DiscreteRange::Range(value)
	}
}

/// A range.
///
/// See IEEE 1076-2008 section 5.2.1.
///
/// ```ignore
/// range := range.attribute_name | simple_expression direction simple_expression
/// ```
#[derive(Debug)]
pub enum Range {
	// Attr(AttrRef),
	Immediate(Dir, ExprRef, ExprRef),
}


/// A record constraint as per IEEE 1076-2008 section 5.3.3.
#[derive(Debug)]
pub struct RecordConstraint {
	/// The span this constraint covers.
	pub span: Span,
	/// Constraints for individual elements.
	pub elems: Vec<(Spanned<Name>, Box<Spanned<ElementConstraint>>)>,
}

impl HasSpan for RecordConstraint {
	fn span(&self) -> Span {
		self.span
	}
}


/// A package declaration.
///
/// See IEEE 1076-2008 section 4.7.
#[derive(Debug)]
pub struct Package {
	/// The parent scope.
	pub parent: ScopeRef,
	/// The package name.
	pub name: Spanned<Name>,
	/// The list of generics.
	pub generics: Vec<GenericRef>,
	/// The list of declarations in the package.
	pub decls: Vec<DeclInPkgRef>,
}

/// A package body.
///
/// See IEEE 1076-2008 section 4.8.
#[derive(Debug)]
pub struct PackageBody {
	/// The parent scope.
	pub parent: ScopeRef,
	/// The package name.
	pub name: Spanned<Name>,
	/// The package which this body targets.
	pub pkg: Spanned<LatentPkgRef>,
	/// The declarations.
	pub decls: Vec<DeclInPkgBodyRef>,
}

/// A package instantiation.
///
/// See IEEE 1076-2008 section 4.9.
#[derive(Debug)]
pub struct PackageInst {
	/// The parent scope.
	pub parent: ScopeRef,
	/// The name of the package.
	pub name: Spanned<Name>,
	/// The package to be instantiated.
	pub pkg: Spanned<LatentPkgRef>,
	/// The generic map.
	pub generic_map: Vec<()>,
}


#[derive(Debug)]
pub struct TypeDecl {
	/// The parent scope.
	pub parent: ScopeRef,
	/// The type name.
	pub name: Spanned<Name>,
	/// The type data.
	pub data: Option<Spanned<TypeData>>,
}

/// The meat of a type declaration.
#[derive(Debug)]
pub enum TypeData {
	/// An enumeration type.
	Enum(Vec<EnumLit>),
	/// An integer, float, or physical type with optional units.
	Range(Dir, ExprRef, ExprRef),
	/// An access type.
	Access(SubtypeIndRef),
	/// An array type.
	Array(Vec<ArrayTypeIndexRef>, SubtypeIndRef),
	/// A file type.
	File(TypeMarkRef),
	/// A record type.
	Record(Vec<(Spanned<Name>, SubtypeIndRef)>),
}

/// An enumeration literal as listed in a type declaration.
#[derive(Debug)]
pub enum EnumLit {
	Ident(Spanned<Name>),
	Char(Spanned<char>),
}

/// An index of an array type.
#[derive(Debug)]
pub enum ArrayTypeIndex {
	/// An unbounded array index of the form `... range <>`.
	Unbounded(Spanned<TypeMarkRef>),
	/// A constrained array index of the form of a subtype indication.
	Subtype(SubtypeIndRef),
	/// A constrained array index of the form `... to/downto ...`.
	Range(Dir, ExprRef, ExprRef),
}


/// A subtype declaration as per IEEE 1076-2008 section 6.3.
#[derive(Debug)]
pub struct SubtypeDecl {
	/// The parent scope.
	pub parent: ScopeRef,
	/// The subtype name.
	pub name: Spanned<Name>,
	/// The actualy subtype.
	pub subty: SubtypeIndRef,
}


/// An expression.
///
/// See IEEE 1076-2008 section 9.
#[derive(Debug)]
pub struct Expr {
	/// The parent scope.
	pub parent: ScopeRef,
	/// The range in the source file that this expression covers.
	pub span: Span,
	/// The expression data.
	pub data: ExprData,
}

/// An expression variant.
#[derive(Debug)]
pub enum ExprData {
	/// A resolved name. Consists of the definition and the definition's span.
	// #[deprecated]
	Name(Def, Span),
	/// A resolved constant name.
	ConstName(ConstDeclRef),
	/// A resolved signal name.
	SignalName(SignalRef),
	/// A resolved variable name.
	VarName(VarDeclRef),
	/// A resolved file name.
	FileName(FileDeclRef),
	/// An overloaded enum name.
	EnumName(Vec<Spanned<EnumRef>>),
	/// An overloaded resolved name.
	#[deprecated]
	OverloadedName(Vec<Spanned<Def>>),
	/// A selection, e.g. `a.b`.
	Select(ExprRef, Spanned<ResolvableName>),
	/// An attribute selection, e.g. `a'b`.
	Attr(ExprRef, Spanned<ResolvableName>),
	/// A bit string literal.
	StringLiteral(Name),
	/// An integer literal.
	IntegerLiteral(ConstInt),
	/// A float literal.
	FloatLiteral(ConstFloat),
	/// A unary operator expression.
	Unary(Spanned<UnaryOp>, ExprRef),
	/// A binary operator expression.
	Binary(Operator, ExprRef, ExprRef),
	/// A range expression.
	Range(Dir, ExprRef, ExprRef),
	/// An aggregate expression.
	Aggregate(AggregateRef),
	/// A qualified expression.
	Qualified(Spanned<TypeMarkRef>, ExprRef),
	/// An allocator expression, i.e. `new`.
	Allocator(Spanned<TypeMarkRef>, Option<ExprRef>),
	/// A cast expression.
	Cast(Spanned<TypeMarkRef>, ExprRef),
	/// A function call expression.
	Call(ExprRef, Spanned<AssocList>),
}

/// A unary operator.
///
/// See IEEE 1076-2008 section 9.2.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum UnaryOp {
	/// The `not` operator.
	Not,
	/// The `abs` operator.
	Abs,
	/// The `+` sign operator.
	Pos,
	/// The `-` sign operator.
	Neg,
	/// A logical operator.
	Logical(ast::LogicalOp),
}

/// A binary operator.
///
/// See IEEE 1076-2008 section 9.2.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum BinaryOp {
	/// A logical operator.
	Logical(ast::LogicalOp),
	/// A relational operator.
	Rel(ast::RelationalOp),
	/// A matching relational operator. These are the relational operators
	/// prefixed with a `?`.
	Match(ast::RelationalOp),
	/// A shift operator.
	Shift(ast::ShiftOp),
	/// The `+` operator.
	Add,
	/// The `-` operator.
	Sub,
	/// The `&` operator.
	Concat,
	/// The `*` operator.
	Mul,
	/// The `/` operator.
	Div,
	/// The `mod` operator.
	Mod,
	/// The `rem` operator.
	Rem,
	/// The `**` operator.
	Pow,
}

/// An object declaration.
///
/// See IEEE 1076-2008 section 6.4.2.1/
#[derive(Debug)]
pub struct Decl<T> {
	/// The scope within which the declaration is made.
	pub parent: ScopeRef,
	/// The span this declaration covers.
	pub span: Span,
	/// The name of the declared object.
	pub name: Spanned<Name>,
	/// The actual declaration.
	pub decl: T,
}

/// A constant declaration.
///
/// See IEEE 1076-2008 section 6.4.2.2.
#[derive(Debug)]
pub struct ConstDecl {
	/// The type of the constant.
	pub ty: SubtypeIndRef,
	/// The optional initial value for the constant.
	pub init: Option<ExprRef>,
}

/// A signal declaration.
///
/// See IEEE 1076-2008 section 6.4.2.3.
#[derive(Debug)]
pub struct SignalDecl {
	/// The subtype of the signal.
	pub ty: SubtypeIndRef,
	/// The signal kind.
	pub kind: SignalKind,
	/// The optional initial value for the signals.
	pub init: Option<ExprRef>,
}

/// A signal kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SignalKind {
	Normal,
	Register,
	Bus,
}

/// A variable declaration.
///
/// See IEEE 1076-2008 section 6.4.2.4.
#[derive(Debug)]
pub struct VarDecl {
	/// Whether the variable was declared as shared or not.
	pub shared: bool,
	/// The subtype of the variable.
	pub ty: SubtypeIndRef,
	/// The optional initial value for the variable.
	pub init: Option<ExprRef>,
}

/// A file declaration.
///
/// See IEEE 1076-2008 section 6.4.2.5.
#[derive(Debug)]
pub struct FileDecl {
	/// The subtype of the file.
	pub ty: SubtypeIndRef,
	/// The expression evaluating to the file name.
	pub filename: Option<ExprRef>,
	/// The expression evaluating to the opening mode.
	pub mode: Option<ExprRef>,
}

/// A process statement.
///
/// See IEEE 1076-2008 section 11.3.
#[derive(Debug)]
pub struct ProcessStmt {
	/// The scope within which the process is declared.
	pub parent: ScopeRef,
	/// The optional process label.
	pub label: Option<Spanned<Name>>,
	/// Whether this is a postponed process. See language reference.
	pub postponed: bool,
	/// The sensitivity list.
	pub sensitivity: ProcessSensitivity,
	/// The declarations made before the `begin` keyword.
	pub decls: Vec<DeclInProcRef>,
	/// The statements inside the process.
	pub stmts: Vec<SeqStmtRef>,
}

/// A process sensitivity specification.
///
/// See IEEE 1076-2008 section 11.3.
#[derive(Debug)]
pub enum ProcessSensitivity {
	/// No sensitivity list provided.
	None,
	/// The `all` sensitivity list.
	All,
	/// Explicitly enumerated signals.
	List(Vec<Def>),
}

/// A sequential signal assignment.
///
/// See IEEE 1076-2008 section 10.5.
#[derive(Debug)]
pub struct SigAssignStmt {
	/// The scope within which the statement has been made.
	pub parent: ScopeRef,
	/// The location of the entire statement in the source file.
	pub span: Span,
	/// The optional statement label.
	pub label: Option<Spanned<Name>>,
	/// The target of the assignment.
	pub target: SigAssignTarget,
	/// The location of the right hand side in the source file.
	pub target_span: Span,
	/// The kind of the assignment.
	pub kind: SigAssignKind,
	/// The location of the right hand side in the source file.
	pub kind_span: Span,
}

/// A signal assignment target.
#[derive(Debug)]
pub enum SigAssignTarget {
	Name(SignalRef),
	Aggregate,
}

/// A signal assignment kind.
#[derive(Debug)]
pub enum SigAssignKind {
	/// A simple waveform assignment.
	SimpleWave(DelayMechanism, Waveform),
	/// A simple force assignment.
	SimpleForce(ForceMode, ExprRef),
	/// A simple release assignment.
	SimpleRelease(ForceMode),
	/// A conditional waveform assignment.
	CondWave(DelayMechanism, Cond<Waveform>),
	/// A conditional force assignment.
	CondForce(ForceMode, Cond<ExprRef>),
	/// A selected waveform assignment.
	SelWave(DelayMechanism, Sel<Waveform>),
	/// A selected force assignment.
	SelForce(ForceMode, Sel<ExprRef>),
}

/// A conditional waveform or expression.
///
/// See IEEE 1076-2008 section 10.5.3.
#[derive(Debug)]
pub struct Cond<T> {
	/// The conditional values, represented as (value, cond) tuples.
	pub when: Vec<(T, ExprRef)>,
	/// The optional `else` value.
	pub other: Option<T>,
}

/// A selected waveform or expression.
///
/// See IEEE 1076-2008 section 10.5.4.
#[derive(Debug)]
pub struct Sel<T> {
	/// Whether matching comparisons are to be used.
	pub matching: bool,
	/// The discriminant expression that is used to select among the choices.
	pub disc: ExprRef,
	/// The selected values, represented as (value, choices) tuples.
	pub when: Vec<(T, Spanned<Choices>)>,
}

/// The mode of a signal force/release statement.
///
/// See IEEE 1076-2008 section 10.5.2.1.
#[derive(Copy, Clone, Debug)]
pub enum ForceMode {
	/// Specifies an effective-value force/release. This is the default if the
	/// assignment target is a in port/signal, or no port/signal at all.
	In,
	/// Specifies a driving-value force/release. This is the default if the
	/// assignment target is a out/inout/buffer port/signal.
	Out,
}

/// The delay mechanism of a normal signal assignment.
#[derive(Copy, Clone, Debug)]
pub enum DelayMechanism {
	/// A `transport` delay mechanism.
	Transport,
	/// A `inertial` delay mechanism.
	Inertial,
	/// A `reject <time_expr> inertial` delay mechanism.
	RejectInertial(ExprRef),
}

/// A signal assignment waveform.
///
/// An empty vector corresponds to the `unaffected` waveform.
pub type Waveform = Vec<WaveElem>;

/// An element of a signal assignment waveform.
#[derive(Debug)]
pub struct WaveElem {
	/// The value expression of the element. Corresponds to `null` if `None`.
	pub value: Option<ExprRef>,
	/// The optional `after` time expression.
	pub after: Option<ExprRef>,
}

/// A subprogram.
///
/// See IEEE 1076-2008 section 4.2.
#[derive(Clone, Debug)]
pub struct Subprog {
	/// The parent scope.
	pub parent: ScopeRef,
	/// The specification, aka the signature.
	pub spec: SubprogSpec,
}

/// A subprogram body.
///
/// See IEEE 1076-2008 section 4.3.
#[derive(Clone, Debug)]
pub struct SubprogBody {
	/// The parent scope.
	pub parent: ScopeRef,
	/// The specification, aka the signature.
	pub spec: SubprogSpec,
	/// The subprogram this body targets.
	pub subprog: Spanned<LatentSubprogRef>,
	/// The declarations in the subprogram.
	pub decls: Vec<DeclInSubprogRef>,
	/// The statements in the subprogram.
	pub stmts: Vec<SeqStmtRef>,
}

/// A subprogram instantiation.
///
/// See IEEE 1076-2008 section 4.4.
#[derive(Clone, Debug)]
pub struct SubprogInst {
	/// The parent scope.
	pub parent: ScopeRef,
	/// Whether this is a procedure, pure function, or impure function.
	pub kind: SubprogKind,
	/// The name of the subprogram.
	pub name: Spanned<ResolvableName>,
	/// The subprogram to be instantiated.
	pub subprog: Spanned<LatentSubprogRef>,
	/// The generic map.
	pub generic_map: Vec<()>,
}

/// A subprogram specification.
///
/// This can be thought of as the signature of a subprogram. It is shared by the
/// subprogram declaration and body, and must match.
#[derive(Clone, Debug)]
pub struct SubprogSpec {
	/// The name of the subprogram. For procedures this must be an identifier.
	pub name: Spanned<ResolvableName>,
	/// Whether this is a procedure, pure function, or impure function.
	pub kind: SubprogKind,
	/// The list of generics.
	pub generics: Vec<GenericRef>,
	/// The generic map.
	pub generic_map: Vec<()>,
	/// The subprogram parameters.
	pub params: Vec<IntfObjRef>,
	/// The return type.
	pub return_type: Option<Spanned<LatentTypeMarkRef>>,
}

/// A subprogram kind.
///
/// Identifies a subprogram as procedure, pure function, or impure function.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum SubprogKind {
	/// A procedure.
	Proc,
	/// A pure function.
	PureFunc,
	/// An impure function.
	ImpureFunc,
}

/// A statement.
///
/// See IEEE 1076-2008 section 10.1.
#[derive(Debug)]
pub struct Stmt<T> {
	/// The parent scope.
	pub parent: ScopeRef,
	/// The span this statement covers.
	pub span: Span,
	/// The optional label.
	pub label: Option<Spanned<Name>>,
	/// The inner statement.
	pub stmt: T,
}

/// A wait statement.
///
/// See IEEE 1076-2008 section 10.2.
#[derive(Debug)]
pub struct WaitStmt {
	/// The sensitivity clause.
	pub sens: Option<Spanned<SensitivityList>>,
	/// The condition clause.
	pub cond: Option<ExprRef>,
	/// The timeout clause.
	pub timeout: Option<ExprRef>,
}

/// An assertion statement.
///
/// See IEEE 1076-2008 section 10.3.
#[derive(Debug)]
pub struct AssertStmt {
	/// The condition to be asserted.
	pub cond: ExprRef,
	/// The report message.
	pub report: Option<ExprRef>,
	/// The severity level.
	pub severity: Option<ExprRef>,
}

/// A report statement.
///
/// See IEEE 1076-2008 section 10.4.
#[derive(Debug)]
pub struct ReportStmt {
	/// The report message.
	pub report: ExprRef,
	/// The severity level.
	pub severity: Option<ExprRef>,
}

// pub struct SigAssignStmt;

/// A variable assignment statement.
///
/// See IEEE 1076-2008 section 10.6.
#[derive(Debug)]
pub struct VarAssignStmt {
	/// The target variable.
	pub target: Spanned<Target>,
	/// The assignment kind.
	pub kind: VarAssignKind,
}

/// A variable assignment kind.
#[derive(Debug)]
pub enum VarAssignKind {
	/// A simple assignment.
	Simple(ExprRef),
	/// A conditional assignment.
	Cond(Cond<ExprRef>),
	/// A selected assignment.
	Sel(Sel<ExprRef>),
}

/// A procedure call statement.
///
/// See IEEE 1076-2008 section 10.7.
#[derive(Debug)]
pub struct CallStmt {
	/// The target subprogram.
	pub subprog: SubprogRef,
	/// The optional call parameters.
	pub params: (),
}

/// An if statement.
///
/// See IEEE 1076-2008 section 10.8.
#[derive(Debug)]
pub struct IfStmt {
	/// The condition and statements of each branch.
	pub branches: Vec<(ExprRef, Vec<SeqStmtRef>)>,
	/// The optional else branch.
	pub otherwise: Option<Vec<SeqStmtRef>>,
}

/// A case statement.
///
/// See IEEE 1076-2008 section 10.9.
#[derive(Debug)]
pub struct CaseStmt {
	/// Whether this is a matching case statement (indicated by `?`).
	pub matching: bool,
	/// The expression being switched over.
	pub switch: ExprRef,
	/// The cases.
	pub cases: Vec<(Spanned<Choices>, Vec<SeqStmtRef>)>,
}

/// A loop statement.
///
/// See IEEE 1076-2008 section 10.10.
#[derive(Debug)]
pub struct LoopStmt {
	/// The loop scheme.
	pub scheme: LoopScheme,
	/// The loop statements.
	pub stmts: Vec<SeqStmtRef>,
}

/// A loop scheme.
///
/// See IEEE 1076-2008 section 10.10.
#[derive(Debug)]
pub enum LoopScheme {
	/// An infinite loop.
	Loop,
	/// A while loop.
	While(ExprRef),
	/// A for loop.
	For(Spanned<Name>, Spanned<DiscreteRange>),
}

/// A next or exit statement.
///
/// See IEEE 1076-2008 section 10.11 and 10.12.
#[derive(Debug)]
pub struct NexitStmt {
	/// Whether this is a next or exit statement.
	pub mode: NexitMode,
	/// The optional loop the statement operates on. If omitted the statement
	/// applies to the innermost loop.
	pub target: Option<Spanned<LoopStmtRef>>,
	/// The optional condition.
	pub cond: Option<ExprRef>,
}

/// A discriminant for next/exit statements.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum NexitMode {
	/// A next statement.
	Next,
	/// An exit statement.
	Exit,
}

/// A return statement.
///
/// See IEEE 1076-2008 section 10.13.
#[derive(Debug)]
pub struct ReturnStmt {
	/// The optional return value.
	pub expr: Option<ExprRef>,
}

/// A null statement.
///
/// See IEEE 1076-2008 section 10.14.
#[derive(Debug)]
pub struct NullStmt;

/// A sensitivity list.
///
/// See IEEE 1076-2008 section 10.2.
pub type SensitivityList = Vec<Spanned<SignalRef>>;

/// A target.
///
/// See IEEE 1076-2008 section 10.5.2.1.
#[derive(Debug)]
pub enum Target {
	Name(ExprRef),
	Aggregate(AggregateRef),
}

/// An aggregate.
///
/// See IEEE 1076-2008 section 9.3.3.1.
#[derive(Debug)]
pub struct Aggregate {
	/// The parent scope.
	pub parent: ScopeRef,
	/// The span the aggregate covers in the source file.
	pub span: Span,
	/// The positional fields of the aggregate.
	pub positional: Vec<Spanned<ExprRef>>,
	/// The named fields of the aggregate.
	pub named: AggregateKind,
	/// The `others` field of the aggregate.
	pub others: Option<Spanned<ExprRef>>,
}

/// A list of choices used in aggregates, selected assignments, and case
/// statements.
///
/// See IEEE 1076-2008 section 9.3.3.1.
pub type Choices = Vec<Spanned<Choice>>;

/// A choice in an aggregate.
///
/// See IEEE 1076-2008 section 9.3.3.1.
#[derive(Debug)]
pub enum Choice {
	/// An expression.
	Expr(ExprRef),
	/// A discrete range.
	DiscreteRange(DiscreteRange),
	/// A record element.
	Element(Name),
	/// The keyword `others`.
	Others
}

impl Choice {
	/// Check if the choice is `others`.
	pub fn is_others(&self) -> bool {
		match *self {
			Choice::Others => true,
			_ => false,
		}
	}

	/// Check if the choice is a record element.
	pub fn is_element(&self) -> bool {
		match *self {
			Choice::Element(_) => true,
			_ => false,
		}
	}
}

/// A list of choices used in record aggregates.
pub type RecordChoices = Vec<Spanned<Name>>;

/// A list of choices used in array aggregates.
pub type ArrayChoices = Vec<Spanned<ArrayChoice>>;

/// A choice in an array aggregate.
#[derive(Debug)]
pub enum ArrayChoice {
	/// An expression.
	Expr(ExprRef),
	/// A discrete range.
	DiscreteRange(DiscreteRange),
}

/// An aggregate kind.
///
/// This determines whether the named elements make the aggregate a record or an
/// array aggregate.
#[derive(Debug)]
pub enum AggregateKind {
	/// The aggregate has no named elements and can be both.
	Both,
	/// A record aggregate.
	Record(Vec<Spanned<(RecordChoices, Spanned<ExprRef>)>>),
	/// An array aggregate.
	Array(Vec<Spanned<(ArrayChoices, Spanned<ExprRef>)>>),
}

impl AggregateKind {
	/// Get a reference to a named field.
	///
	/// Panics if called on `Both`.
	pub fn get(&self, index: usize) -> Spanned<ExprRef> {
		match *self {
			AggregateKind::Both => panic!("get() called on `AggregateKind::Both`"),
			AggregateKind::Record(ref fields) => fields[index].value.1,
			AggregateKind::Array(ref fields) => fields[index].value.1,
		}
	}
}

/// An association list.
///
/// See IEEE 1076-2008 section 6.5.7.
pub type AssocList = Vec<AssocElement>;

/// An association element.
#[derive(Debug)]
pub struct AssocElement {
	/// The span the element covers in the source file.
	pub span: Span,
	/// The optional formal part.
	pub formal: Option<Spanned<ExprRef>>,
	/// The actual part.
	pub actual: Spanned<AssocActual>,
}

/// An actual part of an association element.
#[derive(Debug)]
pub enum AssocActual {
	/// An expression or name.
	Expr(ExprRef),
	/// An expression with leading `inertial` keyword.
	InertialExpr(ExprRef),
	/// A subtype indication.
	Subtype(SubtypeIndRef),
	/// An open association.
	Open,
}