• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nickg / nvc / 14018352298

23 Mar 2025 11:07AM UTC coverage: 92.281% (-0.01%) from 92.295%
14018352298

push

github

nickg
Allow type marks in selected name prefix. Fixes #1173

46 of 47 new or added lines in 2 files covered. (97.87%)

412 existing lines in 11 files now uncovered.

68431 of 74155 relevant lines covered (92.28%)

425171.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.52
/src/parse.c
1
//
2
//  Copyright (C) 2014-2024  Nick Gasson
3
//
4
//  This program is free software: you can redistribute it and/or modify
5
//  it under the terms of the GNU General Public License as published by
6
//  the Free Software Foundation, either version 3 of the License, or
7
//  (at your option) any later version.
8
//
9
//  This program is distributed in the hope that it will be useful,
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
//  GNU General Public License for more details.
13
//
14
//  You should have received a copy of the GNU General Public License
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

18
#include "util.h"
19
#include "array.h"
20
#include "common.h"
21
#include "diag.h"
22
#include "hash.h"
23
#include "inst.h"
24
#include "lib.h"
25
#include "names.h"
26
#include "object.h"
27
#include "option.h"
28
#include "phase.h"
29
#include "psl/psl-node.h"
30
#include "psl/psl-phase.h"
31
#include "scan.h"
32
#include "thread.h"
33
#include "tree.h"
34
#include "type.h"
35

36
#include <string.h>
37
#include <stdarg.h>
38
#include <ctype.h>
39
#include <assert.h>
40
#include <stdbool.h>
41
#include <stdlib.h>
42
#include <limits.h>
43
#include <float.h>
44
#include <inttypes.h>
45

46
typedef struct {
47
   token_t  token;
48
   yylval_t lval;
49
   loc_t    loc;
50
} tokenq_t;
51

52
typedef bool (*look_fn_t)(token_t);
53

54
typedef struct {
55
   token_t   look[4];
56
   token_t   stop[4];
57
   token_t   abort;
58
   token_t   nest_in;
59
   token_t   nest_out;
60
   look_fn_t lookfn;
61
   int       depth;
62
} look_params_t;
63

64
typedef A(tree_t) tree_list_t;
65
typedef A(type_t) type_list_t;
66

67
typedef struct _ident_list ident_list_t;
68

69
struct _ident_list {
70
   ident_list_t *next;
71
   ident_t       ident;
72
   loc_t         loc;
73
};
74

75
#define LOCAL_IDENT_LIST \
76
   __attribute__((cleanup(_ident_list_cleanup))) ident_list_t *
77

78
static loc_t          start_loc;
79
static loc_t          last_loc;
80
static const char    *hint_str = NULL;
81
static int            n_correct = 0;
82
static tokenq_t      *tokenq;
83
static int            tokenq_sz;
84
static int            tokenq_head;
85
static int            tokenq_tail;
86
static yylval_t       last_lval;
87
static token_t        opt_hist[8];
88
static int            nopt_hist = 0;
89
static nametab_t     *nametab = NULL;
90
static bool           bootstrapping = false;
91
static tree_list_t    pragmas = AINIT;
92

93
extern loc_t yylloc;
94

95
#define scan(...) _scan(1, __VA_ARGS__, -1)
96
#define expect(...) _expect(1, __VA_ARGS__, -1)
97
#define one_of(...) _one_of(1, __VA_ARGS__, -1)
98
#define not_at_token(...) ((peek() != tEOF) && !_scan(1, __VA_ARGS__, -1))
99
#define peek() peek_nth(1)
100

101
#define parse_error(loc, ...) do {            \
102
      if (n_correct >= RECOVER_THRESH) {      \
103
         error_at(loc, __VA_ARGS__);          \
104
      }                                       \
105
   } while (0)
106

107
#define RECOVER_THRESH 5
108
#define TRACE_PARSE    0
109
#define WARN_LOOKAHEAD 0
110
#define TRACE_RECOVERY 0
111

112
#define STD(x, y) (standard() >= (STD_##x) ? y : -1)
113

114
typedef void (*add_func_t)(tree_t, tree_t);
115

116
typedef struct {
117
   const char *old_hint;
118
   loc_t       old_start_loc;
119
} state_t;
120

121
#if TRACE_PARSE
122
static int depth = 0;
123
static void _push_state(const state_t *s);
124
#else
125
#define _push_state(s)
126
#endif
127

128
#define EXTEND(s)                                                      \
129
   __attribute__((cleanup(_pop_state), unused))                        \
130
   const state_t _state = { hint_str, start_loc };                     \
131
   hint_str = s;                                                       \
132
   _push_state(&_state);
133

134
#define BEGIN_WITH_HEAD(s, t)                           \
135
   EXTEND(s);                                           \
136
   start_loc = (t) ? *tree_loc(t) : LOC_INVALID;        \
137

138
#define BEGIN(s)  BEGIN_WITH_HEAD(s, NULL)
139

140
#define CURRENT_LOC _diff_loc(&start_loc, &last_loc)
141

142
static tree_t p_expression(void);
143
static tree_t p_sequential_statement(void);
144
static tree_t p_concurrent_statement(void);
145
static tree_t p_package_declaration(tree_t unit);
146
static tree_t p_package_body(tree_t unit);
147
static tree_t p_subprogram_declaration(tree_t spec);
148
static tree_t p_subprogram_body(tree_t spec);
149
static tree_t p_subprogram_specification(void);
150
static tree_t p_name(name_mask_t stop_mask);
151
static tree_t p_block_configuration(tree_t of);
152
static tree_t p_protected_type_body(ident_t id);
153
static type_t p_signature(void);
154
static type_t p_type_mark(void);
155
static tree_t p_function_call(ident_t id, tree_t prefix);
156
static tree_t p_resolution_indication(void);
157
static void p_conditional_waveforms(tree_t stmt, tree_t target, tree_t s0);
158
static void p_generic_map_aspect(tree_t inst, tree_t unit);
159
static ident_t p_designator(void);
160
static void p_interface_list(tree_t parent, tree_kind_t kind, bool ordered);
161
static void p_trailing_label(ident_t label);
162
static tree_t p_condition(void);
163
static type_t p_subtype_indication(void);
164
static tree_t p_record_constraint(type_t base);
165
static tree_t p_qualified_expression(tree_t prefix);
166
static tree_t p_concurrent_procedure_call_statement(ident_t label, tree_t name);
167
static tree_t p_subprogram_instantiation_declaration(void);
168
static tree_t p_record_element_constraint(type_t base);
169
static void p_selected_waveforms(tree_t stmt, tree_t target, tree_t reject);
170
static type_t p_index_subtype_definition(tree_t head);
171
static type_t p_anonymous_type_indication(void);
172
static void p_alias_declaration(tree_t parent);
173
static void p_variable_declaration(tree_t parent);
174
static void p_array_constraint(type_t type, type_t base);
175
static void p_psl_declaration(tree_t parent);
176
static psl_node_t p_psl_sequence(void);
177
static psl_node_t p_psl_property(void);
178
static psl_node_t p_psl_sere(void);
179
static tree_t p_psl_directive(ident_t label);
180
static psl_node_t p_psl_repeated_sere(psl_node_t head);
181
static psl_node_t p_psl_braced_sere(void);
182
static psl_node_t p_hdl_expression(tree_t head, psl_type_t type);
183
static psl_node_t p_psl_builtin_function_call(void);
184

185
static bool consume(token_t tok);
186
static bool optional(token_t tok);
187
static type_t get_element_subtype(tree_t expr);
188

189
static void _pop_state(const state_t *s)
4,427,586✔
190
{
191
#if TRACE_PARSE
192
   printf("%*s<-- %s\n", depth--, "", hint_str);
193
#endif
194
   hint_str = s->old_hint;
4,427,586✔
195
   if (s->old_start_loc.first_line != LINE_INVALID)
4,427,586✔
196
      start_loc = s->old_start_loc;
935,886✔
197
}
4,427,586✔
198

199
#if TRACE_PARSE
200
static void _push_state(const state_t *s)
201
{
202
   printf("%*s--> %s\n", depth++, "", hint_str);
203
}
204
#endif
205

206
static void skip_pragma(pragma_kind_t kind)
53✔
207
{
208
   if (nametab == NULL)
53✔
209
      warn_at(&yylloc, "ignoring pragma outside of design unit");
6✔
210
   else  {
211
      tree_t p = tree_new(T_PRAGMA);
47✔
212
      tree_set_loc(p, &yylloc);
47✔
213
      tree_set_subkind(p, kind);
47✔
214

215
      APUSH(pragmas, p);
47✔
216
   }
217
}
53✔
218

219
static token_t wrapped_yylex(void)
1,698,853✔
220
{
221
   for (;;) {
1,698,906✔
222
      const token_t token = processed_yylex();
1,698,906✔
223
      switch (token) {
1,698,906✔
224
      case tSYNTHON:
3✔
225
         skip_pragma(PRAGMA_SYNTHESIS_ON);
3✔
226
         break;
3✔
227
      case tSYNTHOFF:
4✔
228
         skip_pragma(PRAGMA_SYNTHESIS_OFF);
4✔
229
         break;
4✔
230
      case tCOVERAGEON:
22✔
231
         skip_pragma(PRAGMA_COVERAGE_ON);
22✔
232
         break;
22✔
233
      case tCOVERAGEOFF:
23✔
234
         skip_pragma(PRAGMA_COVERAGE_OFF);
23✔
235
         break;
23✔
236
      case tTRANSLATEON:
×
237
         skip_pragma(PRAGMA_TRANSLATE_ON);
×
UNCOV
238
         break;
×
239
      case tTRANSLATEOFF:
1✔
240
         skip_pragma(PRAGMA_TRANSLATE_OFF);
1✔
241
         break;
1✔
242
      default:
1,698,853✔
243
         return token;
1,698,853✔
244
      }
245
   }
246
}
247

248
static token_t peek_nth(int n)
9,080,014✔
249
{
250
   while (((tokenq_head - tokenq_tail) & (tokenq_sz - 1)) < n) {
10,778,867✔
251
      const token_t token = wrapped_yylex();
1,698,853✔
252

253
      int next = (tokenq_head + 1) & (tokenq_sz - 1);
1,698,853✔
254
      if (unlikely(next == tokenq_tail)) {
1,698,853✔
255
         const int newsz = tokenq_sz * 2;
×
UNCOV
256
         tokenq_t *new = xmalloc_array(newsz, sizeof(tokenq_t));
×
257

258
         tokenq_t *p = new;
×
259
         for (int i = tokenq_tail; i != tokenq_head;
×
260
              i = (i + 1) & (tokenq_sz - 1))
×
UNCOV
261
            *p++ = tokenq[i];
×
262

UNCOV
263
         free(tokenq);
×
264

265
         tokenq      = new;
×
266
         tokenq_sz   = newsz;
×
267
         tokenq_head = p - new;
×
UNCOV
268
         tokenq_tail = 0;
×
269

UNCOV
270
         next = (tokenq_head + 1) & (tokenq_sz - 1);
×
271
      }
272

273
      extern yylval_t yylval;
1,698,853✔
274

275
      tokenq[tokenq_head].token = token;
1,698,853✔
276
      tokenq[tokenq_head].lval  = yylval;
1,698,853✔
277
      tokenq[tokenq_head].loc   = yylloc;
1,698,853✔
278

279
      tokenq_head = next;
1,698,853✔
280
   }
281

282
   const int pos = (tokenq_tail + n - 1) & (tokenq_sz - 1);
9,080,014✔
283
   return tokenq[pos].token;
9,080,014✔
284
}
285

286
static bool look_for(const look_params_t *params)
50,536✔
287
{
288
   bool found = false;
50,536✔
289
   token_t tok = -1;
50,536✔
290
   int n, nest = 0;
50,536✔
291
   for (n = 1; ;) {
50,536✔
292
      tok = peek_nth(n++);
142,682✔
293
      if ((tok == tEOF) || (tok == params->abort))
142,682✔
294
         goto stop_looking;
2✔
295
      else if (tok == params->nest_in)
142,680✔
296
         nest++;
6,501✔
297

298
      if (nest == params->depth) {
142,680✔
299
         for (int i = 0; i < ARRAY_LEN(params->look); i++) {
551,747✔
300
            if (tok == params->look[i]) {
442,763✔
301
               found = true;
6,827✔
302
               goto stop_looking;
6,827✔
303
            }
304
         }
305

306
         if (params->lookfn != NULL && (*params->lookfn)(tok)) {
108,984✔
307
            found = true;
×
UNCOV
308
            goto stop_looking;
×
309
         }
310

311
         for (int i = 0; i < ARRAY_LEN(params->stop); i++) {
391,442✔
312
            if (tok == params->stop[i])
326,165✔
313
               goto stop_looking;
43,707✔
314
         }
315
      }
316

317
      if (tok == params->nest_out)
92,146✔
318
         nest--;
6,501✔
319
   }
320
 stop_looking:
50,536✔
321

322
#if WARN_LOOKAHEAD > 0
323
   if (n >= WARN_LOOKAHEAD)
324
      warn_at(&(tokenq[tokenq_tail].loc), "look ahead depth %d", n);
325
#endif
326

327
   return found;
50,536✔
328
}
329

330
static void drop_token(void)
1,694,601✔
331
{
332
   assert(tokenq_head != tokenq_tail);
1,694,601✔
333

334
   if (start_loc.first_line == LINE_INVALID)
1,694,601✔
335
      start_loc = tokenq[tokenq_tail].loc;
706,856✔
336

337
   last_lval = tokenq[tokenq_tail].lval;
1,694,601✔
338
   last_loc  = tokenq[tokenq_tail].loc;
1,694,601✔
339

340
   tokenq_tail = (tokenq_tail + 1) & (tokenq_sz - 1);
1,694,601✔
341

342
   nopt_hist = 0;
1,694,601✔
343
}
1,694,601✔
344

345
static void drop_tokens_until(token_t tok)
22✔
346
{
347
   token_t next = tEOF;
22✔
348
   do {
73✔
349
      free_token(tok, &last_lval);
73✔
350
      next = peek();
73✔
351
      drop_token();
73✔
352
   } while ((tok != next) && (next != tEOF));
73✔
353

354
#if TRACE_RECOVERY
355
   if (peek() != tEOF)
356
      fmt_loc(stdout, &(tokenq[tokenq_tail].loc));
357
#endif
358
}
22✔
359

360
static void _vexpect(va_list ap)
214✔
361
{
362
   if (n_correct >= RECOVER_THRESH) {
214✔
363
      diag_t *d = diag_new(DIAG_ERROR, &(tokenq[tokenq_tail].loc));
41✔
364
      diag_printf(d, "unexpected $yellow$%s$$ while parsing %s, expecting ",
41✔
365
                  token_str(peek()), hint_str);
366

367
      bool first = true;
41✔
368
      for (int i = 0; i < nopt_hist; i++) {
60✔
369
         diag_printf(d, "%s$yellow$%s$$", i == 0 ? "one of " : ", ",
24✔
370
                     token_str(opt_hist[i]));
371
         first = false;
19✔
372
      }
373

374
      int tok = va_arg(ap, int);
41✔
375
      while (tok != -1) {
140✔
376
         const int tmp = tok;
99✔
377
         tok = va_arg(ap, int);
99✔
378

379
         if (first && (tok != -1))
99✔
380
            diag_printf(d, "one of ");
11✔
381
         else if (!first)
88✔
382
            diag_printf(d, (tok == -1) ? " or " : ", ");
119✔
383

384
         diag_printf(d, "$yellow$%s$$", token_str(tmp));
99✔
385

386
         first = false;
99✔
387
      }
388

389
      diag_hint(d, &(tokenq[tokenq_tail].loc), "this token was unexpected");
41✔
390
      diag_emit(d);
41✔
391
   }
392

393
   n_correct = 0;
214✔
394

395
   drop_token();
214✔
396
   suppress_errors(nametab);
214✔
397
}
214✔
398

399
static void _expect(int dummy, ...)
211✔
400
{
401
   va_list ap;
211✔
402
   va_start(ap, dummy);
211✔
403
   _vexpect(ap);
211✔
404
   va_end(ap);
211✔
405
}
211✔
406

407
static bool consume(token_t tok)
1,694,409✔
408
{
409
   const token_t got = peek();
1,694,409✔
410
   if (tok != got) {
1,694,409✔
411
      expect(tok);
95✔
412
      return false;
95✔
413
   }
414
   else {
415
      n_correct++;
1,694,314✔
416
      drop_token();
1,694,314✔
417
      return true;
1,694,314✔
418
   }
419
}
420

421
static bool optional(token_t tok)
1,349,296✔
422
{
423
   if (peek() == tok) {
1,349,296✔
424
      consume(tok);
194,506✔
425
      return true;
194,506✔
426
   }
427
   else {
428
      if (nopt_hist < ARRAY_LEN(opt_hist))
1,154,790✔
429
         opt_hist[nopt_hist++] = tok;
1,154,790✔
430
      return false;
1,154,790✔
431
   }
432
}
433

434
static bool _scan(int dummy, ...)
2,526,671✔
435
{
436
   va_list ap;
2,526,671✔
437
   va_start(ap, dummy);
2,526,671✔
438

439
   token_t p = peek();
2,526,671✔
440
   bool found = false;
2,526,671✔
441

442
   while (!found) {
2,526,671✔
443
      const int tok = va_arg(ap, token_t);
12,983,165✔
444
      if (tok == -1)
12,983,165✔
445
         break;
446
      else if (p == tok)
10,636,087✔
447
         found = true;
448
   }
449

450
   va_end(ap);
2,526,671✔
451
   return found;
2,526,671✔
452
}
453

454
static int _one_of(int dummy, ...)
195,686✔
455
{
456
   va_list ap;
195,686✔
457
   va_start(ap, dummy);
195,686✔
458

459
   token_t p = peek();
195,686✔
460
   bool found = false;
195,686✔
461

462
   while (!found) {
195,686✔
463
      const int tok = va_arg(ap, token_t);
283,408✔
464
      if (tok == -1)
283,408✔
465
         break;
466
      else if (p == tok)
283,405✔
467
         found = true;
468
   }
469

470
   va_end(ap);
195,686✔
471

472
   if (found) {
195,686✔
473
      consume(p);
195,683✔
474
      return p;
195,683✔
475
   }
476
   else {
477
      va_start(ap, dummy);
3✔
478
      _vexpect(ap);
3✔
479
      va_end(ap);
3✔
480

481
      return -1;
3✔
482
   }
483
}
484

485
static const loc_t *_diff_loc(const loc_t *start, const loc_t *end)
1,339,055✔
486
{
487
   static loc_t result;
1,339,055✔
488

489
   result = get_loc(start->first_line,
1,339,055✔
490
                    start->first_column,
1,339,055✔
491
                    end->first_line + end->line_delta,
1,339,055✔
492
                    end->first_column + end->column_delta,
1,339,055✔
493
                    start->file_ref);
1,339,055✔
494
   return &result;
1,339,055✔
495
}
496

497
static ident_t error_marker(void)
35✔
498
{
499
   return well_known(W_ERROR);
35✔
500
}
501

502
static tree_t error_expr(void)
7✔
503
{
504
   tree_t t = tree_new(T_REF);
7✔
505
   tree_set_ident(t, error_marker());
7✔
506
   tree_set_type(t, type_new(T_NONE));
7✔
507
   return t;
7✔
508
}
509

510
static tree_t find_binding(tree_t inst)
1,692✔
511
{
512
   ident_t name;
1,692✔
513
   tree_t unit = NULL;
1,692✔
514
   if (tree_kind(inst) == T_BINDING) {
1,692✔
515
      name = tree_ident(inst);
206✔
516
      if (tree_has_ident2(inst))
206✔
517
         name = ident_prefix(name, tree_ident2(inst), '-');
94✔
518
   }
519
   else {
520
      name = tree_ident2(inst);
1,486✔
521
      if (tree_has_ref(inst))
1,486✔
522
         unit = tree_ref(inst);
2✔
523
   }
524

525
   if (unit == NULL)
96✔
526
      unit = resolve_name(nametab, tree_loc(inst), name);
1,690✔
527

528
   if (unit == NULL)
1,692✔
529
      return NULL;
530

531
   const char *what = is_design_unit(unit) ? "design unit" : "object";
1,682✔
532
   const tree_kind_t kind = tree_kind(unit);
1,682✔
533
   switch (tree_class(inst)) {
1,682✔
534
   case C_COMPONENT:
419✔
535
      if (kind != T_COMPONENT) {
419✔
536
         parse_error(tree_loc(inst), "%s %s is not a component declaration",
3✔
537
                     what, istr(name));
538
         return NULL;
3✔
539
      }
540
      break;
541
   case C_ENTITY:
1,200✔
542
      if (kind != T_ENTITY && kind != T_ARCH) {
1,200✔
543
         parse_error(tree_loc(inst), "%s %s is not an entity",
2✔
544
                     what, istr(name));
545
         return NULL;
2✔
546
      }
547
      break;
548
   case C_CONFIGURATION:
63✔
549
      if (kind != T_CONFIGURATION) {
63✔
550
         parse_error(tree_loc(inst), "%s %s is not a configuration",
1✔
551
                     what, istr(name));
552
         return NULL;
1✔
553
      }
554
      break;
555
   default:
556
      break;
557
   }
558

559
   return unit;
560
}
561

562
static void set_label_and_loc(tree_t t, ident_t label, const loc_t *loc)
66,516✔
563
{
564
   tree_set_loc(t, loc);
66,516✔
565

566
   if (label == NULL)
66,516✔
567
      label = get_implicit_label(t, nametab);
66,189✔
568

569
   tree_set_ident(t, label);
66,516✔
570
}
66,516✔
571

572
static void require_std(vhdl_standard_t which, const char *feature)
2,228✔
573
{
574
   static bool warned = false;
2,228✔
575

576
   if (standard() < which && !warned) {
2,228✔
577
      warned = true;
1✔
578

579
      if (n_correct >= RECOVER_THRESH) {
1✔
580
         diag_t *d = diag_new(DIAG_ERROR, CURRENT_LOC);
1✔
581
         diag_printf(d, "%s %s not supported in VHDL-%s",
2✔
582
                     feature, feature[strlen(feature)-1] == 's' ? "are" : "is",
1✔
583
                     standard_text(standard()));
584
         diag_hint(d, NULL, "pass $bold$--std=%s$$ to enable this feature",
1✔
585
                   standard_text(which));
586
         diag_emit(d);
1✔
587
      }
588
   }
589
}
2,228✔
590

591
static tree_t bit_string_to_literal(const char *str, const loc_t *loc)
2,283✔
592
{
593
   tree_t t = tree_new(T_STRING);
2,283✔
594
   tree_set_loc(t, loc);
2,283✔
595

596
   const char *p = str;
2,283✔
597
   int length = -1;
2,283✔
598

599
   if (isdigit_iso88591(*p)) {
2,283✔
600
      require_std(STD_08, "bit string literals with length specifier");
85✔
601
      unsigned long slength = strtoul(p, (char **)&p, 10);
85✔
602
      if (slength > INT32_MAX) {
85✔
603
         error_at(loc, "sorry, bit strings longer than %d elements are "
1✔
604
                  "not supported", INT32_MAX);
605
         return t;
1✔
606
      }
607
      length = slength;
84✔
608
   }
609

610
   enum { UNSIGNED, SIGNED } mode = UNSIGNED;
2,282✔
611

612
   switch (*p) {
2,282✔
613
   case 'U': case 'u': mode = UNSIGNED; ++p; break;
17✔
614
   case 'S': case 's': mode = SIGNED; ++p; break;
20✔
615
   }
616

617
   char base_ch = *p++;
2,282✔
618
   int base;
2,282✔
619
   switch (base_ch) {
2,282✔
620
   case 'X': case 'x': base = 16; break;
621
   case 'O': case 'o': base = 8;  break;
85✔
622
   case 'B': case 'b': base = 2;  break;
80✔
623
   case 'D': case 'd': base = 10; break;
53✔
624
   default:
×
UNCOV
625
      parse_error(loc, "invalid base '%c' for bit string", base_ch);
×
626
      return t;
627
   }
628

629
   tree_t one = tree_new(T_REF);
2,282✔
630
   tree_set_ident(one, ident_new("'1'"));
2,282✔
631
   tree_set_loc(one, loc);
2,282✔
632

633
   tree_t zero = tree_new(T_REF);
2,282✔
634
   tree_set_ident(zero, ident_new("'0'"));
2,282✔
635
   tree_set_loc(zero, loc);
2,282✔
636

637
   if (base == 10) {
2,282✔
638
      require_std(STD_08, "decimal bit string literals");
53✔
639

640
      int ddigits = 0;
53✔
641
      uint8_t *decimal LOCAL = xmalloc(strlen(str));
106✔
642

643
      for (++p; *p != '\"'; p++) {
386✔
644
         if (!isdigit_iso88591(*p)) {
334✔
645
            parse_error(loc, "invalid digit '%c' in decimal bit string", *p);
1✔
646
            return t;
1✔
647
         }
648
         else
649
            decimal[ddigits++] = *p - '0';
333✔
650
      }
651

652
      const int maxbits = (length == -1 ? ddigits * 4 : length);
52✔
653
      tree_t *bits LOCAL = xmalloc_array(maxbits, sizeof(tree_t));
104✔
654
      int pos = maxbits - 1;
52✔
655

656
      for (;;) {
1,019✔
657
         bool all_zero = true;
1,019✔
658
         int cout = 0;
1,019✔
659
         for (int i = 0, cin = 0; i < ddigits; i++, cin = cout) {
37,781✔
660
            all_zero &= (decimal[i] == 0);
36,762✔
661
            cout = decimal[i] & 1;
36,762✔
662
            decimal[i] >>= 1;
36,762✔
663
            if (cin) decimal[i] += 5;
36,762✔
664
         }
665

666
         if (all_zero)
1,019✔
667
            break;
668
         else if (pos < 0 && cout) {
968✔
669
            parse_error(CURRENT_LOC, "excess non-zero digits in "
1✔
670
                        "decimal bit string literal");
671
            return t;
1✔
672
         }
673
         else if (pos >= 0)
967✔
674
            bits[pos--] = cout ? one : zero;
1,385✔
675
      }
676

677
      if (length == -1)
51✔
678
         length = maxbits - pos - 1;
10✔
679

680
      for (int i = maxbits - length; i < maxbits; i++)
1,593✔
681
         tree_add_char(t, i <= pos ? zero : bits[i]);
1,542✔
682

683
      return t;
684
   }
685

686
   tree_t *bits LOCAL = NULL;
4,458✔
687
   if (length >= 0)
2,229✔
688
      bits = xmalloc_array(length, sizeof(tree_t));
42✔
689

690
   tree_t pad = mode == UNSIGNED ? zero : NULL;
2,229✔
691
   int nbits = 0;
2,229✔
692
   for (++p; *p != '\"'; p++) {
9,170✔
693
      const bool extended = (isdigit_iso88591(*p) && *p < '0' + base)
12,158✔
694
         || (base > 10 && *p >= 'A' && *p < 'A' + base - 10)
1,733✔
695
         || (base > 10 && *p >= 'a' && *p < 'a' + base - 10);
8,172✔
696

697
      int n = (isdigit_iso88591(*p) ? (*p - '0')
6,945✔
698
               : 10 + (isupper_iso88591(*p) ? (*p - 'A') : (*p - 'a')));
6,945✔
699
      tree_t digit = NULL;
6,945✔
700

701
      if (!extended) {
6,945✔
702
         if (standard() < STD_08 || !isprint_iso88591(*p)) {
129✔
703
            parse_error(loc, "invalid digit '%c' in bit string", *p);
1✔
704
            return t;
1✔
705
         }
706
         else {
707
            const char rune[] = { '\'', *p, '\'', '\0' };
128✔
708
            digit = tree_new(T_REF);
128✔
709
            tree_set_ident(digit, ident_new(rune));
128✔
710
            tree_set_loc(digit, loc);
128✔
711
         }
712
      }
713

714
      for (int d = (base >> 1); d > 0; n = n % d, d >>= 1) {
31,686✔
715
         tree_t bit = extended ? ((n / d) ? one : zero) : digit;
24,745✔
716
         if (pad == NULL) pad = bit;
24,745✔
717
         if (length >= 0) {
24,745✔
718
            tree_t left = nbits == 0 ? bit : bits[0];
380✔
719
            if (nbits < length)
380✔
720
               bits[nbits++] = bit;
245✔
721
            else if (left != pad && tree_ident(left) != tree_ident(pad)) {
135✔
722
               parse_error(CURRENT_LOC, "excess %s digits in bit "
5✔
723
                           "string literal",
724
                           mode == SIGNED ? "significant" : "non-zero");
725
               return t;
3✔
726
            }
727
            else if (length > 0) {
132✔
728
               for (int i = 0; i < length - 1; i++)
1,422✔
729
                  bits[i] = bits[i + 1];
1,295✔
730
               bits[length - 1] = bit;
127✔
731
            }
732
         }
733
         else
734
            tree_add_char(t, bit);
24,365✔
735
      }
736
   }
737

738
   if (pad == NULL && nbits < length)
2,225✔
739
      parse_error(CURRENT_LOC, "signed bit string literal cannot be an "
1✔
740
                  "empty string");
741
   else if (length >= 0) {
2,224✔
742
      // Left-pad with sign bit or zero
743
      int pos = 0;
744
      for (; pos < length - nbits; pos++)
182✔
745
         tree_add_char(t, pad);
144✔
746
      for (int i = 0; pos < length; pos++, i++)
279✔
747
         tree_add_char(t, bits[i]);
241✔
748
   }
749

750
   return t;
751
}
752

753
static tree_t get_time(int64_t fs, const loc_t *loc)
66✔
754
{
755
   tree_t lit = tree_new(T_LITERAL);
66✔
756
   tree_set_subkind(lit, L_INT);
66✔
757
   tree_set_ival(lit, fs);
66✔
758
   tree_set_loc(lit, loc);
66✔
759
   tree_set_type(lit, std_type(NULL, STD_TIME));
66✔
760

761
   return lit;
66✔
762
}
763

764
static void set_delay_mechanism(tree_t t, tree_t reject)
7,279✔
765
{
766
   if (reject == NULL) {
7,279✔
767
      // Inertial delay with same value as waveform
768
      // LRM 93 section 8.4 the rejection limit in this case is
769
      // specified by the time expression of the first waveform
770
      tree_t assign = (tree_kind(t) == T_COND_ASSIGN ? tree_cond(t, 0) : t);
7,201✔
771
      if (tree_waveforms(assign) == 0)
7,201✔
772
         return;
773

774
      tree_t w = tree_waveform(assign, 0);
7,195✔
775
      if (tree_has_delay(w))
7,195✔
776
         tree_set_reject(t, tree_delay(w));
566✔
777
   }
778
   else {
779
      tree_set_reject(t, reject);
78✔
780
      solve_types(nametab, reject, std_type(NULL, STD_TIME));
78✔
781
   }
782
}
783

784
static tree_t add_port(tree_t d, const char *name, type_t type,
64,596✔
785
                       port_mode_t mode, tree_t def)
786
{
787
   type_t ftype = tree_type(d);
64,596✔
788

789
   tree_t port = tree_new(T_PARAM_DECL);
64,596✔
790
   tree_set_ident(port, ident_new(name));
64,596✔
791
   tree_set_loc(port, tree_loc(d));
64,596✔
792
   tree_set_type(port, type);
64,596✔
793
   tree_set_subkind(port, mode);
64,596✔
794
   if (def != NULL)
64,596✔
795
      tree_set_value(port, def);
214✔
796
   if (type_is_file(type))
64,596✔
797
      tree_set_class(port, C_FILE);
693✔
798

799
   tree_add_port(d, port);
64,596✔
800
   type_add_param(ftype, type);
64,596✔
801

802
   return port;
64,596✔
803
}
804

805
static tree_t builtin_proc(ident_t name, subprogram_kind_t kind, ...)
849✔
806
{
807
   type_t f = type_new(T_SIGNATURE);
849✔
808
   type_set_ident(f, name);
849✔
809

810
   tree_t d = tree_new(T_PROC_DECL);
849✔
811
   tree_set_ident(d, name);
849✔
812
   tree_set_type(d, f);
849✔
813
   tree_set_subkind(d, kind);
849✔
814
   tree_set_flag(d, TREE_F_NEVER_WAITS);
849✔
815

816
   tree_set_flag(d, TREE_F_PREDEFINED);
849✔
817
   tree_set_loc(d, CURRENT_LOC);
849✔
818
   return d;
849✔
819
}
820

821
static tree_t builtin_fn(ident_t name, type_t result,
32,635✔
822
                         subprogram_kind_t kind, ...)
823
{
824
   type_t f = type_new(T_SIGNATURE);
32,635✔
825
   type_set_ident(f, name);
32,635✔
826
   type_set_result(f, result);
32,635✔
827

828
   tree_t d = tree_new(T_FUNC_DECL);
32,635✔
829
   tree_set_ident(d, name);
32,635✔
830
   tree_set_type(d, f);
32,635✔
831
   tree_set_subkind(d, kind);
32,635✔
832

833
   va_list ap;
32,635✔
834
   va_start(ap, kind);
32,635✔
835
   char *argname;
32,635✔
836
   while ((argname = va_arg(ap, char*))) {
95,637✔
837
      type_t type = va_arg(ap, type_t);
63,002✔
838
      assert(type != NULL);
63,002✔
839
      add_port(d, argname, type, PORT_IN, NULL);
63,002✔
840
   }
841
   va_end(ap);
32,635✔
842

843
   tree_set_flag(d, TREE_F_PREDEFINED);
32,635✔
844
   tree_set_loc(d, CURRENT_LOC);
32,635✔
845
   return d;
32,635✔
846
}
847

848
static void declare_binary(tree_t container, ident_t name, type_t lhs,
30,334✔
849
                           type_t rhs, type_t result, subprogram_kind_t kind)
850
{
851
   tree_t d = builtin_fn(name, result, kind, "L", lhs, "R", rhs, NULL);
30,334✔
852
   mangle_func(nametab, d);
30,334✔
853
   insert_name(nametab, d, NULL);
30,334✔
854
   tree_add_decl(container, d);
30,334✔
855
}
30,334✔
856

857
static void declare_unary(tree_t container, ident_t name, type_t operand,
2,229✔
858
                          type_t result, subprogram_kind_t kind)
859
{
860
   tree_t d = builtin_fn(name, result, kind, "VALUE", operand, NULL);
2,229✔
861
   mangle_func(nametab, d);
2,229✔
862
   insert_name(nametab, d, NULL);
2,229✔
863
   tree_add_decl(container, d);
2,229✔
864
}
2,229✔
865

866
static bool is_bit_or_std_ulogic(type_t type)
1,671✔
867
{
868
   if (!type_is_enum(type))
1,671✔
869
      return false;
870

871
   ident_t name = type_ident(type);
378✔
872

873
   return name == well_known(W_STD_BIT) || name == well_known(W_IEEE_ULOGIC);
412✔
874
}
875

876
static void declare_predefined_ops(tree_t container, type_t t)
4,763✔
877
{
878
   // Prefined operators are defined in LRM 93 section 7.2
879

880
   ident_t mult   = well_known(W_OP_TIMES);
4,763✔
881
   ident_t div    = well_known(W_OP_DIVIDE);
4,763✔
882
   ident_t plus   = well_known(W_OP_ADD);
4,763✔
883
   ident_t minus  = well_known(W_OP_MINUS);
4,763✔
884
   ident_t cmp_lt = well_known(W_OP_LESS_THAN);
4,763✔
885
   ident_t cmp_le = well_known(W_OP_LESS_EQUAL);
4,763✔
886
   ident_t cmp_gt = well_known(W_OP_GREATER_THAN);
4,763✔
887
   ident_t cmp_ge = well_known(W_OP_GREATER_EQUAL);
4,763✔
888
   ident_t eq     = well_known(W_OP_EQUAL);
4,763✔
889
   ident_t neq    = well_known(W_OP_NOT_EQUAL);
4,763✔
890

891
   ident_t min_i = NULL, max_i = NULL;
4,763✔
892
   if (standard() >= STD_08) {
4,763✔
893
      min_i = ident_new("MINIMUM");
1,671✔
894
      max_i = ident_new("MAXIMUM");
1,671✔
895
   }
896

897
   // Predefined operators
898

899
   tree_t std = find_std(nametab);
4,763✔
900

901
   type_t std_bool = std_type(std, STD_BOOLEAN);
4,763✔
902
   type_t std_int  = NULL;
4,763✔
903
   type_t std_real = NULL;
4,763✔
904
   type_t std_uint = NULL;
4,763✔
905

906
   type_kind_t kind = type_kind(t);
4,763✔
907

908
   switch (kind) {
4,763✔
909
   case T_SUBTYPE:
910
      // Use operators of base type
911
      break;
912

913
   case T_FILE:
914
      // No predefined operators
915
      break;
916

917
   case T_ARRAY:
2,465✔
918
      // Operators on arrays
919
      declare_binary(container, eq, t, t, std_bool, S_ARRAY_EQ);
2,465✔
920
      declare_binary(container, neq, t, t, std_bool, S_ARRAY_NEQ);
2,465✔
921

922
      if (dimension_of(t) == 1) {
2,465✔
923
         type_t elem = type_elem(t);
2,221✔
924
         const bool scalar_elem = type_is_scalar(elem);
2,221✔
925
         const bool ordered =
4,442✔
926
            standard() >= STD_19 ? scalar_elem : type_is_discrete(elem);
2,221✔
927

928
         if (ordered) {
2,221✔
929
            declare_binary(container, cmp_lt, t, t, std_bool, S_ARRAY_LT);
1,004✔
930
            declare_binary(container, cmp_le, t, t, std_bool, S_ARRAY_LE);
1,004✔
931
            declare_binary(container, cmp_gt, t, t, std_bool, S_ARRAY_GT);
1,004✔
932
            declare_binary(container, cmp_ge, t, t, std_bool, S_ARRAY_GE);
1,004✔
933
         }
934

935
         ident_t concat = ident_new("\"&\"");
2,221✔
936
         declare_binary(container, concat, t, t, t, S_CONCAT);
2,221✔
937
         declare_binary(container, concat, t, elem, t, S_CONCAT);
2,221✔
938
         declare_binary(container, concat, elem, t, t, S_CONCAT);
2,221✔
939
         declare_binary(container, concat, elem, elem, t, S_CONCAT);
2,221✔
940

941
         if (standard() >= STD_08) {
2,221✔
942
            if (ordered) {
717✔
943
               declare_binary(container, min_i, t, t, t, S_MINIMUM);
254✔
944
               declare_binary(container, max_i, t, t, t, S_MAXIMUM);
254✔
945
            }
946

947
            if (scalar_elem) {
717✔
948
               declare_unary(container, min_i, t, elem, S_MINIMUM);
278✔
949
               declare_unary(container, max_i, t, elem, S_MAXIMUM);
278✔
950
            }
951
         }
952
      }
953
      break;
954

955
   case T_RECORD:
1,219✔
956
      // Operators on records
957
      declare_binary(container, eq, t, t, std_bool, S_RECORD_EQ);
1,219✔
958
      declare_binary(container, neq, t, t, std_bool, S_RECORD_NEQ);
1,219✔
959
      break;
1,219✔
960

961
   case T_PHYSICAL:
45✔
962
      std_int  = std_type(std, STD_INTEGER);
45✔
963
      std_real = std_type(std, STD_REAL);
45✔
964
      std_uint = std_type(std, STD_UNIVERSAL_INTEGER);
45✔
965

966
      // Multiplication
967
      declare_binary(container, mult, t, std_int, t, S_MUL_PI);
45✔
968
      declare_binary(container, mult, t, std_real, t, S_MUL_PR);
45✔
969
      declare_binary(container, mult, std_int, t, t, S_MUL_IP);
45✔
970
      declare_binary(container, mult, std_real, t, t, S_MUL_RP);
45✔
971

972
      // Division
973
      declare_binary(container, div, t, std_int, t, S_DIV_PI);
45✔
974
      declare_binary(container, div, t, std_real, t, S_DIV_PR);
45✔
975
      declare_binary(container, div, t, t, std_uint, S_DIV_PP);
45✔
976

977
      // Addition
978
      declare_binary(container, plus, t, t, t, S_ADD);
45✔
979

980
      // Subtraction
981
      declare_binary(container, minus, t, t, t, S_SUB);
45✔
982

983
      // Sign operators
984
      declare_unary(container, plus, t, t, S_IDENTITY);
45✔
985
      declare_unary(container, minus, t, t, S_NEGATE);
45✔
986

987
      // Comparison
988
      declare_binary(container, cmp_lt, t, t, std_bool, S_SCALAR_LT);
45✔
989
      declare_binary(container, cmp_le, t, t, std_bool, S_SCALAR_LE);
45✔
990
      declare_binary(container, cmp_gt, t, t, std_bool, S_SCALAR_GT);
45✔
991
      declare_binary(container, cmp_ge, t, t, std_bool, S_SCALAR_GE);
45✔
992

993
      // Equality
994
      declare_binary(container, eq, t, t, std_bool, S_SCALAR_EQ);
45✔
995
      declare_binary(container, neq, t, t, std_bool, S_SCALAR_NEQ);
45✔
996

997
      // Absolute value
998
      declare_unary(container, well_known(W_OP_ABS), t, t, S_ABS);
45✔
999

1000
      if (standard() >= STD_08) {
45✔
1001
         declare_binary(container, min_i, t, t, t, S_MINIMUM);
14✔
1002
         declare_binary(container, max_i, t, t, t, S_MAXIMUM);
14✔
1003

1004
         // Modulus and remainder in 2008 only
1005
         declare_binary(container, well_known(W_OP_MOD), t, t, t, S_MOD);
14✔
1006
         declare_binary(container, well_known(W_OP_REM), t, t, t, S_REM);
14✔
1007
      }
1008

1009
      break;
1010

1011
   case T_INTEGER:
140✔
1012
      // Modulus
1013
      declare_binary(container, well_known(W_OP_MOD), t, t, t, S_MOD);
140✔
1014

1015
      // Remainder
1016
      declare_binary(container, well_known(W_OP_REM), t, t, t, S_REM);
140✔
1017

1018
      // Fall-through
1019
   case T_REAL:
166✔
1020
      // Addition
1021
      declare_binary(container, plus, t, t, t, S_ADD);
166✔
1022

1023
      // Subtraction
1024
      declare_binary(container, minus, t, t, t, S_SUB);
166✔
1025

1026
      // Multiplication
1027
      declare_binary(container, mult, t, t, t, S_MUL);
166✔
1028

1029
      // Division
1030
      declare_binary(container, div, t, t, t, S_DIV);
166✔
1031

1032
      // Sign operators
1033
      declare_unary(container, plus, t, t, S_IDENTITY);
166✔
1034
      declare_unary(container, minus, t, t, S_NEGATE);
166✔
1035

1036
      // Exponentiation
1037
      if (!bootstrapping) {
166✔
1038
         std_int = std_type(std, STD_INTEGER);
154✔
1039
         declare_binary(container, well_known(W_OP_EXPONENT),
154✔
1040
                        t, std_int, t, S_EXP);
1041
      }
1042

1043
      // Absolute value
1044
      declare_unary(container, well_known(W_OP_ABS), t, t, S_ABS);
166✔
1045

1046
      // Fall-through
1047
   case T_ENUM:
619✔
1048
      declare_binary(container, cmp_lt, t, t, std_bool, S_SCALAR_LT);
619✔
1049
      declare_binary(container, cmp_le, t, t, std_bool, S_SCALAR_LE);
619✔
1050
      declare_binary(container, cmp_gt, t, t, std_bool, S_SCALAR_GT);
619✔
1051
      declare_binary(container, cmp_ge, t, t, std_bool, S_SCALAR_GE);
619✔
1052

1053
      if (standard() >= STD_08) {
619✔
1054
         declare_binary(container, min_i, t, t, t, S_MINIMUM);
234✔
1055
         declare_binary(container, max_i, t, t, t, S_MAXIMUM);
234✔
1056
      }
1057

1058
      // Fall-through
1059
   default:
1060
      declare_binary(container, eq, t, t, std_bool, S_SCALAR_EQ);
936✔
1061
      declare_binary(container, neq, t, t, std_bool, S_SCALAR_NEQ);
936✔
1062
      break;
936✔
1063
   }
1064

1065
   if (standard() >= STD_08 && !bootstrapping && type_is_representable(t)) {
4,763✔
1066
      // The TO_STRING operators in STD.STANDARD are declared at
1067
      // the end of the package according to LRM 08 section 5.2.6
1068
      declare_unary(container, ident_new("TO_STRING"), t,
487✔
1069
                    std_type(NULL, STD_STRING), S_TO_STRING);
1070
   }
1071

1072
   // Universal integers and reals have some additional overloaded
1073
   // operators that are not valid for regular integer and real types
1074
   // See LRM 93 section 7.5
1075

1076
   if (bootstrapping && kind == T_REAL
4,763✔
1077
       && t == std_type(std, STD_UNIVERSAL_REAL)) {
6✔
1078
      type_t uint = std_type(std, STD_UNIVERSAL_INTEGER);
3✔
1079

1080
      declare_binary(container, mult, t, uint, t, S_MUL_RI);
3✔
1081
      declare_binary(container, mult, uint, t, t, S_MUL_IR);
3✔
1082
      declare_binary(container, div, t, uint, t, S_DIV_RI);
3✔
1083
   }
1084

1085
   // Matching comparison for BIT and STD_ULOGIC
1086

1087
   if (standard() >= STD_08) {
4,763✔
1088
      if (kind == T_ARRAY) {
1,671✔
1089
         type_t elem = type_elem(t);
795✔
1090
         if (is_bit_or_std_ulogic(elem)) {
795✔
1091
            declare_binary(container, ident_new("\"?=\""),
60✔
1092
                           t, t, elem, S_MATCH_EQ);
1093
            declare_binary(container, ident_new("\"?/=\""),
60✔
1094
                           t, t, elem, S_MATCH_NEQ);
1095
         }
1096
      }
1097
      else if (is_bit_or_std_ulogic(t)) {
876✔
1098
         declare_binary(container, ident_new("\"?=\""), t, t, t, S_MATCH_EQ);
4✔
1099
         declare_binary(container, ident_new("\"?/=\""), t, t, t, S_MATCH_NEQ);
4✔
1100
         declare_binary(container, ident_new("\"?<\""), t, t, t, S_MATCH_LT);
4✔
1101
         declare_binary(container, ident_new("\"?<=\""), t, t, t, S_MATCH_LE);
4✔
1102
         declare_binary(container, ident_new("\"?>\""), t, t, t, S_MATCH_GT);
4✔
1103
         declare_binary(container, ident_new("\"?>=\""), t, t, t, S_MATCH_GE);
4✔
1104
      }
1105
   }
1106

1107
   // Logical operators
1108

1109
   if (bootstrapping && (t == std_bool || t == std_type(std, STD_BIT))) {
4,763✔
1110
      declare_binary(container, ident_new("\"and\""), t, t, t, S_SCALAR_AND);
6✔
1111
      declare_binary(container, ident_new("\"or\""), t, t, t, S_SCALAR_OR);
6✔
1112
      declare_binary(container, ident_new("\"xor\""), t, t, t, S_SCALAR_XOR);
6✔
1113
      declare_binary(container, ident_new("\"nand\""), t, t, t, S_SCALAR_NAND);
6✔
1114
      declare_binary(container, ident_new("\"nor\""), t, t, t, S_SCALAR_NOR);
6✔
1115
      declare_binary(container, ident_new("\"xnor\""), t, t, t, S_SCALAR_XNOR);
6✔
1116
      declare_unary(container, ident_new("\"not\""), t, t, S_SCALAR_NOT);
6✔
1117
   }
1118

1119
   bool vec_logical = false;
4,763✔
1120
   if (kind == T_ARRAY && dimension_of(t) == 1) {
4,763✔
1121
      type_t base = type_elem(t);
2,221✔
1122
      vec_logical = (base == std_bool || base == std_type(NULL, STD_BIT));
2,221✔
1123
   }
1124

1125
   if (vec_logical) {
2,221✔
1126
      std_int = std_type(NULL, STD_INTEGER);
188✔
1127

1128
      ident_t and  = well_known(W_OP_AND);
188✔
1129
      ident_t or   = well_known(W_OP_OR);
188✔
1130
      ident_t xor  = well_known(W_OP_XOR);
188✔
1131
      ident_t nand = well_known(W_OP_NAND);
188✔
1132
      ident_t nor  = well_known(W_OP_NOR);
188✔
1133
      ident_t xnor = well_known(W_OP_XNOR);
188✔
1134

1135
      declare_binary(container, and, t, t, t, S_ARRAY_AND);
188✔
1136
      declare_binary(container, or, t, t, t, S_ARRAY_OR);
188✔
1137
      declare_binary(container, xor, t, t, t, S_ARRAY_XOR);
188✔
1138
      declare_binary(container, nand, t, t, t, S_ARRAY_NAND);
188✔
1139
      declare_binary(container, nor, t, t, t, S_ARRAY_NOR);
188✔
1140
      declare_binary(container, xnor, t, t, t, S_ARRAY_XNOR);
188✔
1141

1142
      declare_unary(container, well_known(W_OP_NOT), t, t, S_ARRAY_NOT);
188✔
1143

1144
      declare_binary(container, ident_new("\"sll\""), t, std_int, t, S_SLL);
188✔
1145
      declare_binary(container, ident_new("\"srl\""), t, std_int, t, S_SRL);
188✔
1146
      declare_binary(container, ident_new("\"sla\""), t, std_int, t, S_SLA);
188✔
1147
      declare_binary(container, ident_new("\"sra\""), t, std_int, t, S_SRA);
188✔
1148
      declare_binary(container, ident_new("\"rol\""), t, std_int, t, S_ROL);
188✔
1149
      declare_binary(container, ident_new("\"ror\""), t, std_int, t, S_ROR);
188✔
1150

1151
      if (standard() >= STD_08) {
188✔
1152
         type_t e = type_elem(t);
38✔
1153

1154
         declare_unary(container, and, t, e, S_REDUCE_AND);
38✔
1155
         declare_unary(container, or, t, e, S_REDUCE_OR);
38✔
1156
         declare_unary(container, xor, t, e, S_REDUCE_XOR);
38✔
1157
         declare_unary(container, nand, t, e, S_REDUCE_NAND);
38✔
1158
         declare_unary(container, nor, t, e, S_REDUCE_NOR);
38✔
1159
         declare_unary(container, xnor, t, e, S_REDUCE_XNOR);
38✔
1160

1161
         declare_binary(container, and, t, e, t, S_MIXED_AND);
38✔
1162
         declare_binary(container, or, t, e, t, S_MIXED_OR);
38✔
1163
         declare_binary(container, xor, t, e, t, S_MIXED_XOR);
38✔
1164
         declare_binary(container, nand, t, e, t, S_MIXED_NAND);
38✔
1165
         declare_binary(container, nor, t, e, t, S_MIXED_NOR);
38✔
1166
         declare_binary(container, xnor, t, e, t, S_MIXED_XNOR);
38✔
1167

1168
         declare_binary(container, and, e, t, t, S_MIXED_AND);
38✔
1169
         declare_binary(container, or, e, t, t, S_MIXED_OR);
38✔
1170
         declare_binary(container, xor, e, t, t, S_MIXED_XOR);
38✔
1171
         declare_binary(container, nand, e, t, t, S_MIXED_NAND);
38✔
1172
         declare_binary(container, nor, e, t, t, S_MIXED_NOR);
38✔
1173
         declare_binary(container, xnor, e, t, t, S_MIXED_XNOR);
38✔
1174
      }
1175
   }
1176

1177
   // Predefined procedures
1178

1179
   switch (kind) {
4,763✔
1180
   case T_FILE:
98✔
1181
      {
1182
         ident_t file_open_i  = ident_new("FILE_OPEN");
98✔
1183
         ident_t file_close_i = ident_new("FILE_CLOSE");
98✔
1184
         ident_t read_i       = ident_new("READ");
98✔
1185
         ident_t write_i      = ident_new("WRITE");
98✔
1186
         ident_t endfile_i    = ident_new("ENDFILE");
98✔
1187
         ident_t read_mode_i  = ident_new("READ_MODE");
98✔
1188

1189
         tree_t read_mode = get_local_decl(nametab, std, read_mode_i, 0);
98✔
1190
         assert(read_mode != NULL);
98✔
1191

1192
         type_t open_kind   = std_type(NULL, STD_FILE_OPEN_KIND);
98✔
1193
         type_t open_status = std_type(NULL, STD_FILE_OPEN_STATUS);
98✔
1194
         type_t std_string  = std_type(NULL, STD_STRING);
98✔
1195

1196
         tree_t file_open1 = builtin_proc(file_open_i, S_FILE_OPEN1);
98✔
1197
         add_port(file_open1, "F", t, PORT_INOUT, NULL);
98✔
1198
         add_port(file_open1, "EXTERNAL_NAME", std_string, PORT_IN, NULL);
98✔
1199
         add_port(file_open1, "OPEN_KIND", open_kind, PORT_IN,
98✔
1200
                  make_ref(read_mode));
1201
         insert_name(nametab, file_open1, file_open_i);
98✔
1202
         tree_add_decl(container, file_open1);
98✔
1203

1204
         tree_t file_open2 = builtin_proc(file_open_i, S_FILE_OPEN2);
98✔
1205
         add_port(file_open2, "STATUS", open_status, PORT_OUT, NULL);
98✔
1206
         add_port(file_open2, "F", t, PORT_INOUT, NULL);
98✔
1207
         add_port(file_open2, "EXTERNAL_NAME", std_string, PORT_IN, NULL);
98✔
1208
         add_port(file_open2, "OPEN_KIND", open_kind, PORT_IN,
98✔
1209
                  make_ref(read_mode));
1210
         insert_name(nametab, file_open2, file_open_i);
98✔
1211
         tree_add_decl(container, file_open2);
98✔
1212

1213
         tree_t file_close = builtin_proc(file_close_i, S_FILE_CLOSE);
98✔
1214
         add_port(file_close, "F", t, PORT_INOUT, NULL);
98✔
1215
         mangle_func(nametab, file_close);
98✔
1216
         insert_name(nametab, file_close, file_close_i);
98✔
1217
         tree_add_decl(container, file_close);
98✔
1218

1219
         if (standard() >= STD_08) {
98✔
1220
            ident_t flush_i = ident_new("FLUSH");
18✔
1221

1222
            tree_t flush = builtin_proc(flush_i, S_FILE_FLUSH);
18✔
1223
            add_port(flush, "F", t, PORT_IN, NULL);
18✔
1224
            mangle_func(nametab, flush);
18✔
1225
            insert_name(nametab, flush, flush_i);
18✔
1226
            tree_add_decl(container, flush);
18✔
1227
         }
1228

1229
         if (standard() >= STD_19) {
98✔
1230
            ident_t rewind_i   = ident_new("FILE_REWIND");
9✔
1231
            ident_t seek_i     = ident_new("FILE_SEEK");
9✔
1232
            ident_t begin_i    = ident_new("FILE_ORIGIN_BEGIN");
9✔
1233
            ident_t truncate_i = ident_new("FILE_TRUNCATE");
9✔
1234
            ident_t state_i    = ident_new("FILE_STATE");
9✔
1235
            ident_t mode_i     = ident_new("FILE_MODE");
9✔
1236
            ident_t position_i = ident_new("FILE_POSITION");
9✔
1237
            ident_t size_i     = ident_new("FILE_SIZE");
9✔
1238
            ident_t canseek_i  = ident_new("FILE_CANSEEK");
9✔
1239

1240
            type_t origin_kind = std_type(NULL, STD_FILE_ORIGIN_KIND);
9✔
1241
            type_t open_state = std_type(NULL, STD_FILE_OPEN_STATE);
9✔
1242

1243
            tree_t origin_begin = get_local_decl(nametab, std, begin_i, 0);
9✔
1244
            assert(origin_begin != NULL);
9✔
1245

1246
            tree_t file_open3 = builtin_fn(file_open_i, open_status,
9✔
1247
                                           S_FILE_OPEN3,
1248
                                           "F", t,
1249
                                           "EXTERNAL_NAME", std_string,
1250
                                           "OPEN_KIND", open_kind,
1251
                                           NULL);
1252
            tree_set_flag(file_open3, TREE_F_IMPURE);
9✔
1253
            tree_set_class(tree_port(file_open3, 0), C_FILE);
9✔
1254
            tree_set_value(tree_port(file_open3, 2), make_ref(read_mode));
9✔
1255
            mangle_func(nametab, file_open3);
9✔
1256
            insert_name(nametab, file_open3, file_open_i);
9✔
1257
            tree_add_decl(container, file_open3);
9✔
1258

1259
            tree_t rewind = builtin_proc(rewind_i, S_FILE_REWIND);
9✔
1260
            add_port(rewind, "F", t, PORT_IN, NULL);
9✔
1261
            mangle_func(nametab, rewind);
9✔
1262
            insert_name(nametab, rewind, rewind_i);
9✔
1263
            tree_add_decl(container, rewind);
9✔
1264

1265
            std_int = std_type(std, STD_INTEGER);
9✔
1266

1267
            tree_t seek = builtin_proc(seek_i, S_FILE_SEEK);
9✔
1268
            add_port(seek, "F", t, PORT_IN, NULL);
9✔
1269
            add_port(seek, "OFFSET", std_int, PORT_IN, NULL);
9✔
1270
            add_port(seek, "ORIGIN", origin_kind, PORT_IN,
9✔
1271
                     make_ref(origin_begin));
1272
            mangle_func(nametab, seek);
9✔
1273
            insert_name(nametab, seek, seek_i);
9✔
1274
            tree_add_decl(container, seek);
9✔
1275

1276
            tree_t truncate = builtin_proc(truncate_i, S_FILE_TRUNCATE);
9✔
1277
            add_port(truncate, "F", t, PORT_IN, NULL);
9✔
1278
            add_port(truncate, "SIZE", std_int, PORT_IN, NULL);
9✔
1279
            add_port(truncate, "ORIGIN", origin_kind, PORT_IN,
9✔
1280
                     make_ref(origin_begin));
1281
            mangle_func(nametab, truncate);
9✔
1282
            insert_name(nametab, truncate, truncate_i);
9✔
1283
            tree_add_decl(container, truncate);
9✔
1284

1285
            tree_t state = builtin_fn(state_i, open_state, S_FILE_STATE,
9✔
1286
                                      "F", t, NULL);
1287
            tree_set_class(tree_port(state, 0), C_FILE);
9✔
1288
            mangle_func(nametab, state);
9✔
1289
            insert_name(nametab, state, state_i);
9✔
1290
            tree_add_decl(container, state);
9✔
1291

1292
            tree_t mode = builtin_fn(mode_i, open_kind, S_FILE_MODE,
9✔
1293
                                     "F", t, NULL);
1294
            tree_set_class(tree_port(mode, 0), C_FILE);
9✔
1295
            mangle_func(nametab, mode);
9✔
1296
            insert_name(nametab, mode, mode_i);
9✔
1297
            tree_add_decl(container, mode);
9✔
1298

1299
            tree_t position = builtin_fn(position_i, std_int, S_FILE_POSITION,
9✔
1300
                                         "F", t, "ORIGIN", origin_kind, NULL);
1301
            tree_set_class(tree_port(position, 0), C_FILE);
9✔
1302
            tree_set_value(tree_port(position, 1), make_ref(origin_begin));
9✔
1303
            mangle_func(nametab, position);
9✔
1304
            insert_name(nametab, position, position_i);
9✔
1305
            tree_add_decl(container, position);
9✔
1306

1307
            tree_t size = builtin_fn(size_i, std_int, S_FILE_SIZE,
9✔
1308
                                     "F", t, NULL);
1309
            tree_set_class(tree_port(size, 0), C_FILE);
9✔
1310
            mangle_func(nametab, size);
9✔
1311
            insert_name(nametab, size, size_i);
9✔
1312
            tree_add_decl(container, size);
9✔
1313

1314
            tree_t canseek = builtin_fn(canseek_i, std_bool, S_FILE_CANSEEK,
9✔
1315
                                        "F", t, NULL);
1316
            tree_set_class(tree_port(canseek, 0), C_FILE);
9✔
1317
            mangle_func(nametab, canseek);
9✔
1318
            insert_name(nametab, canseek, canseek_i);
9✔
1319
            tree_add_decl(container, canseek);
9✔
1320
         }
1321

1322
         type_t of = type_designated(t);
98✔
1323

1324
         tree_t read = builtin_proc(read_i, S_FILE_READ);
98✔
1325
         add_port(read, "F", t, PORT_INOUT, NULL);
98✔
1326
         add_port(read, "VALUE", of, PORT_OUT, NULL);
98✔
1327
         if (type_is_array(of) && type_is_unconstrained(of)) {
98✔
1328
            type_t std_nat = std_type(NULL, STD_NATURAL);
23✔
1329
            add_port(read, "LENGTH", std_nat, PORT_OUT, NULL);
23✔
1330
         }
1331
         insert_name(nametab, read, read_i);
98✔
1332
         tree_add_decl(container, read);
98✔
1333

1334
         tree_t write = builtin_proc(write_i, S_FILE_WRITE);
98✔
1335
         add_port(write, "F", t, PORT_INOUT, NULL);
98✔
1336
         add_port(write, "VALUE", of, PORT_IN, NULL);
98✔
1337
         insert_name(nametab, write, write_i);
98✔
1338
         tree_add_decl(container, write);
98✔
1339

1340
         declare_unary(container, endfile_i, t, std_bool, S_ENDFILE);
98✔
1341
      }
1342
      break;
98✔
1343

1344
   case T_ACCESS:
314✔
1345
      {
1346
         ident_t deallocate_i = ident_new("DEALLOCATE");
314✔
1347

1348
         tree_t deallocate = builtin_proc(deallocate_i, S_DEALLOCATE);
314✔
1349
         add_port(deallocate, "P", t, PORT_INOUT, NULL);
314✔
1350

1351
         mangle_func(nametab, deallocate);
314✔
1352
         insert_name(nametab, deallocate, deallocate_i);
314✔
1353
         tree_add_decl(container, deallocate);
314✔
1354
      }
1355
      break;
314✔
1356

1357
   default:
1358
      break;
1359
   }
1360

1361
   if (bootstrapping && standard() >= STD_08) {
4,763✔
1362
      // Special predefined operators only declared in STANDARD
1363
      type_t std_bit = NULL;
36✔
1364
      if (t == std_bool || t == (std_bit = std_type(NULL, STD_BIT))) {
36✔
1365
         tree_t d1 = builtin_fn(ident_new("RISING_EDGE"), std_bool,
4✔
1366
                                S_RISING_EDGE, "S", t, NULL);
1367
         mangle_func(nametab, d1);
4✔
1368
         tree_set_class(tree_port(d1, 0), C_SIGNAL);
4✔
1369
         tree_add_decl(container, d1);
4✔
1370

1371
         tree_t d2 = builtin_fn(ident_new("FALLING_EDGE"), std_bool,
4✔
1372
                                S_FALLING_EDGE, "S", t, NULL);
1373
         mangle_func(nametab, d2);
4✔
1374
         tree_set_class(tree_port(d2, 0), C_SIGNAL);
4✔
1375
         tree_add_decl(container, d2);
4✔
1376

1377
         if (t == std_bit)
4✔
1378
            declare_unary(container, well_known(W_OP_CCONV), t,
2✔
1379
                          std_bool, S_IDENTITY);
1380
      }
1381
   }
1382
}
4,763✔
1383

1384
static void declare_alias(tree_t container, tree_t to, ident_t name)
8✔
1385
{
1386
   tree_t alias = tree_new(T_ALIAS);
8✔
1387
   tree_set_ident(alias, name);
8✔
1388
   tree_set_value(alias, make_ref(to));
8✔
1389
   tree_set_type(alias, tree_type(to));
8✔
1390
   tree_set_flag(alias, TREE_F_PREDEFINED);
8✔
1391

1392
   tree_add_decl(container, alias);
8✔
1393
}
8✔
1394

1395
static void declare_additional_standard_operators(tree_t unit)
3✔
1396
{
1397
   assert(bootstrapping);
3✔
1398

1399
   // The exponentiation operator must be declared here after INTEGER is
1400
   // declared
1401

1402
   type_t std_uint  = std_type(unit, STD_UNIVERSAL_INTEGER);
3✔
1403
   type_t std_ureal = std_type(unit, STD_UNIVERSAL_REAL);
3✔
1404
   type_t std_int   = std_type(unit, STD_INTEGER);
3✔
1405
   type_t std_real  = std_type(unit, STD_REAL);
3✔
1406

1407
   ident_t exp_i = well_known(W_OP_EXPONENT);
3✔
1408

1409
   declare_binary(unit, exp_i, std_uint, std_int, std_uint, S_EXP);
3✔
1410
   declare_binary(unit, exp_i, std_ureal, std_int, std_ureal, S_EXP);
3✔
1411
   declare_binary(unit, exp_i, std_int, std_int, std_int, S_EXP);
3✔
1412
   declare_binary(unit, exp_i, std_real, std_int, std_real, S_EXP);
3✔
1413

1414
   if (standard() < STD_08)
3✔
1415
      return;
1416

1417
   // LRM 08 5.2.6 says TO_STRING is declared at the end of the STANDARD
1418
   // package
1419

1420
   ident_t to_string   = ident_new("TO_STRING");
2✔
1421
   type_t  std_string  = std_type(unit, STD_STRING);
2✔
1422
   type_t  std_time    = std_type(unit, STD_TIME);
2✔
1423
   type_t  std_natural = std_type(unit, STD_NATURAL);
2✔
1424
   type_t  std_bit_vec = std_type(unit, STD_BIT_VECTOR);
2✔
1425

1426
   const int ndecls = tree_decls(unit);
2✔
1427
   for (int i = 0; i < ndecls; i++) {
682✔
1428
      tree_t d = tree_decl(unit, i);
680✔
1429
      if (tree_kind(d) == T_TYPE_DECL) {
680✔
1430
         type_t type = tree_type(d);
36✔
1431
         if (type_is_representable(type))
36✔
1432
            declare_unary(unit, to_string, type, std_string, S_TO_STRING);
31✔
1433
      }
1434
   }
1435

1436
   // The following special cases are implicitly defined
1437

1438
   tree_t d1 = builtin_fn(to_string, std_string, S_TO_STRING_TIME,
2✔
1439
                          "VALUE", std_time, "UNIT", std_time, NULL);
1440
   mangle_func(nametab, d1);
2✔
1441
   tree_add_decl(unit, d1);
2✔
1442

1443
   tree_t d2 = builtin_fn(to_string, std_string, S_TO_STRING_REAL_DIGITS,
2✔
1444
                          "VALUE", std_real, "DIGITS", std_natural, NULL);
1445
   mangle_func(nametab, d2);
2✔
1446
   tree_add_decl(unit, d2);
2✔
1447

1448
   tree_t d3 = builtin_fn(to_string, std_string, S_TO_STRING_REAL_FORMAT,
2✔
1449
                          "VALUE", std_real, "FORMAT", std_string, NULL);
1450
   mangle_func(nametab, d3);
2✔
1451
   tree_add_decl(unit, d3);
2✔
1452

1453
   tree_t d4 = builtin_fn(ident_new("TO_HSTRING"), std_string,
2✔
1454
                          S_TO_HSTRING_BITVEC, "VALUE", std_bit_vec, NULL);
1455
   mangle_func(nametab, d4);
2✔
1456
   tree_add_decl(unit, d4);
2✔
1457

1458
   declare_alias(unit, d4, ident_new("TO_HEX_STRING"));
2✔
1459

1460
   tree_t d5 = builtin_fn(ident_new("TO_OSTRING"), std_string,
2✔
1461
                          S_TO_OSTRING_BITVEC, "VALUE", std_bit_vec, NULL);
1462
   mangle_func(nametab, d5);
2✔
1463
   tree_add_decl(unit, d5);
2✔
1464

1465
   declare_alias(unit, d5, ident_new("TO_OCTAL_STRING"));
2✔
1466

1467
   tree_t d6;
2✔
1468
   for (int n = 0; (d6 = get_local_decl(nametab, NULL, to_string, n)); n++) {
21✔
1469
      if (type_eq(tree_type(tree_port(d6, 0)), std_bit_vec))
21✔
1470
         break;
1471
   }
1472

1473
   assert(d6 != NULL);
2✔
1474
   declare_alias(unit, d6, ident_new("TO_BSTRING"));
2✔
1475
   declare_alias(unit, d6, ident_new("TO_BINARY_STRING"));
2✔
1476
}
1477

1478
static void unary_op(tree_t expr, tree_t (*arg_fn)(tree_t))
12,093✔
1479
{
1480
   tree_t right = (*arg_fn)(NULL);
12,093✔
1481
   tree_set_loc(expr, CURRENT_LOC);
12,093✔
1482
   add_param(expr, right, P_POS, NULL);
12,093✔
1483
}
12,093✔
1484

1485
static void binary_op(tree_t expr, tree_t left, tree_t (*right_fn)(tree_t))
53,886✔
1486
{
1487
   add_param(expr, left, P_POS, NULL);
53,886✔
1488

1489
   tree_t right = (*right_fn)(NULL);
53,886✔
1490
   tree_set_loc(expr, CURRENT_LOC);
53,886✔
1491
   add_param(expr, right, P_POS, NULL);
53,886✔
1492
}
53,886✔
1493

1494
static tree_t implicit_dereference(tree_t t)
1,287✔
1495
{
1496
   type_t access = type_designated(tree_type(t));
1,287✔
1497

1498
   tree_t all = tree_new(T_ALL);
1,287✔
1499
   tree_set_loc(all, tree_loc(t));
1,287✔
1500
   tree_set_value(all, t);
1,287✔
1501
   tree_set_type(all, access);
1,287✔
1502

1503
   return all;
1,287✔
1504
}
1505

1506
static type_t prefix_type(tree_t prefix, type_t signature)
39,409✔
1507
{
1508
   if (scope_formal_kind(nametab) == F_SUBPROGRAM)
39,409✔
1509
      return NULL;
1510

1511
   // Check we can acutally resolve the base reference at this point
1512
   tree_t ref = prefix;
1513
   tree_kind_t kind;
1514
   while ((kind = tree_kind(ref)) != T_REF) {
41,781✔
1515
      switch (kind) {
2,658✔
1516
      case T_ARRAY_SLICE:
2,389✔
1517
      case T_ARRAY_REF:
1518
      case T_RECORD_REF:
1519
      case T_ALL:
1520
         ref = tree_value(ref);
2,389✔
1521
         break;
2,389✔
1522
      default:
1523
         return NULL;
1524
      }
1525
   }
1526

1527
   if (tree_has_ref(ref) && !class_has_type(class_of(tree_ref(ref))))
39,123✔
1528
      return NULL;
1529

1530
   return solve_types(nametab, prefix, signature);
38,487✔
1531
}
1532

1533
static bool is_range_expr(tree_t t)
32,249✔
1534
{
1535
   switch (tree_kind(t)) {
32,249✔
1536
   case T_REF:
9,384✔
1537
      if (tree_has_ref(t))
9,384✔
1538
         return aliased_type_decl(tree_ref(t)) != NULL;
9,255✔
1539
      else
1540
         return !!(query_name(nametab, tree_ident(t), NULL) & N_TYPE);
129✔
1541

1542
   case T_ATTR_REF:
4,757✔
1543
      {
1544
         const attr_kind_t predef = tree_subkind(t);
4,757✔
1545
         return predef == ATTR_RANGE || predef == ATTR_REVERSE_RANGE;
4,757✔
1546
      }
1547

1548
   default:
1549
      return false;
1550
   }
1551
}
1552

1553
static tree_t ensure_labelled(tree_t t, ident_t label)
20,158✔
1554
{
1555
   tree_set_ident(t, label ?: get_implicit_label(t, nametab));
20,158✔
1556
   return t;
20,158✔
1557
}
1558

1559
static tree_t select_decl(tree_t prefix, ident_t suffix, name_mask_t *mask)
714✔
1560
{
1561
   ident_t qual = ident_prefix(tree_ident(prefix), suffix, '.');
714✔
1562

1563
   tree_t decl = NULL;
714✔
1564
   *mask = query_name(nametab, qual, &decl);
714✔
1565

1566
   tree_t ref = tree_new(T_REF);
714✔
1567
   tree_set_ident(ref, qual);
714✔
1568
   tree_set_loc(ref, CURRENT_LOC);
714✔
1569
   tree_set_ref(ref, decl);
714✔
1570

1571
   if (*mask == 0) {
714✔
1572
      parse_error(CURRENT_LOC, "name %s not found in %s", istr(suffix),
2✔
1573
                  istr(tree_ident(prefix)));
1574
      tree_set_type(ref, type_new(T_NONE));
2✔
1575
   }
1576

1577
   return ref;
714✔
1578
}
1579

1580
static tree_t could_be_slice_name(tree_t fcall)
14,400✔
1581
{
1582
   // The expression F(X) where X is a type name and F is a function
1583
   // should be parsed as an array slice F(X'RANGE) where F is called
1584
   // with no arguments
1585

1586
   if (tree_params(fcall) != 1)
14,400✔
1587
      return fcall;
1588

1589
   tree_t p0 = tree_param(fcall, 0);
5,928✔
1590
   if (tree_subkind(p0) != P_POS)
5,928✔
1591
      return fcall;
1592

1593
   tree_t value = tree_value(p0);
5,904✔
1594
   if (tree_kind(value) != T_REF)
5,904✔
1595
      return fcall;
1596

1597
   if (!tree_has_ref(value))
4,004✔
1598
      return fcall;
1599

1600
   tree_t decl = tree_ref(value);
3,959✔
1601
   if (!is_type_decl(decl))
3,959✔
1602
      return fcall;
1603

1604
   tree_t new = tree_new(T_FCALL);
4✔
1605
   tree_set_ident(new, tree_ident(fcall));
4✔
1606
   tree_set_loc(new, tree_loc(fcall));
4✔
1607

1608
   tree_t aref = tree_new(T_ATTR_REF);
4✔
1609
   tree_set_name(aref, value);
4✔
1610
   tree_set_ident(aref, ident_new("RANGE"));
4✔
1611
   tree_set_loc(aref, tree_loc(value));
4✔
1612
   tree_set_subkind(aref, ATTR_RANGE);
4✔
1613

1614
   tree_t r = tree_new(T_RANGE);
4✔
1615
   tree_set_subkind(r, RANGE_EXPR);
4✔
1616
   tree_set_value(r, aref);
4✔
1617
   tree_set_loc(r, tree_loc(p0));
4✔
1618

1619
   solve_types(nametab, r, NULL);
4✔
1620

1621
   tree_t slice = tree_new(T_ARRAY_SLICE);
4✔
1622
   tree_set_value(slice, new);
4✔
1623
   tree_add_range(slice, r);
4✔
1624
   tree_set_loc(slice, tree_loc(fcall));
4✔
1625

1626
   return slice;
4✔
1627
}
1628

1629
static bool is_implicit_block(tree_t t)
164✔
1630
{
1631
   const tree_kind_t kind = tree_kind(t);
164✔
1632
   return kind == T_ARCH || kind == T_BLOCK || kind == T_IF_GENERATE
164✔
1633
      || kind == T_FOR_GENERATE;
164✔
1634
}
1635

1636
static void make_universal_type(tree_t container, type_kind_t kind,
6✔
1637
                                const char *name, tree_t min, tree_t max)
1638
{
1639
   assert(bootstrapping);
6✔
1640
   ident_t name_i = ident_new(name);
6✔
1641

1642
   type_t type = type_new(kind);
6✔
1643
   type_set_ident(type, name_i);
6✔
1644

1645
   tree_t r = tree_new(T_RANGE);
6✔
1646
   tree_set_subkind(r, RANGE_TO);
6✔
1647
   tree_set_left(r, min);
6✔
1648
   tree_set_right(r, max);
6✔
1649
   tree_set_type(r, type);
6✔
1650

1651
   type_add_dim(type, r);
6✔
1652

1653
   tree_set_type(min, type);
6✔
1654
   tree_set_type(max, type);
6✔
1655

1656
   tree_t decl = tree_new(T_TYPE_DECL);
6✔
1657
   tree_set_loc(decl, CURRENT_LOC);
6✔
1658
   tree_set_ident(decl, name_i);
6✔
1659
   tree_set_type(decl, type);
6✔
1660

1661
   tree_add_decl(container, decl);
6✔
1662

1663
   declare_predefined_ops(container, type);
6✔
1664
}
6✔
1665

1666
static void make_universal_int(tree_t container)
3✔
1667
{
1668
   tree_t min = tree_new(T_LITERAL);
3✔
1669
   tree_set_subkind(min, L_INT);
3✔
1670
   tree_set_ival(min, INT64_MIN);
3✔
1671

1672
   tree_t max = tree_new(T_LITERAL);
3✔
1673
   tree_set_subkind(max, L_INT);
3✔
1674
   tree_set_ival(max, INT64_MAX);
3✔
1675

1676
   make_universal_type(container, T_INTEGER, "universal_integer", min, max);
3✔
1677
}
3✔
1678

1679
static void make_universal_real(tree_t container)
3✔
1680
{
1681
   tree_t min = tree_new(T_LITERAL);
3✔
1682
   tree_set_subkind(min, L_REAL);
3✔
1683
   tree_set_dval(min, -DBL_MAX);
3✔
1684

1685
   tree_t max = tree_new(T_LITERAL);
3✔
1686
   tree_set_subkind(max, L_REAL);
3✔
1687
   tree_set_dval(max, DBL_MAX);
3✔
1688

1689
   make_universal_type(container, T_REAL, "universal_real", min, max);
3✔
1690
}
3✔
1691

1692
static void make_implicit_guard_signal(tree_t block, tree_t expr)
27✔
1693
{
1694
   tree_t guard = tree_new(T_IMPLICIT_SIGNAL);
27✔
1695
   tree_set_subkind(guard, IMPLICIT_GUARD);
27✔
1696
   tree_set_ident(guard, ident_new("GUARD"));
27✔
1697
   tree_set_loc(guard, tree_loc(expr));
27✔
1698
   tree_set_type(guard, std_type(NULL, STD_BOOLEAN));
27✔
1699
   tree_set_value(guard, expr);
27✔
1700

1701
   tree_add_decl(block, guard);
27✔
1702
   insert_name(nametab, guard, NULL);
27✔
1703
   sem_check(guard, nametab);
27✔
1704
}
27✔
1705

1706
static tree_t fcall_to_conv_func(tree_t value)
214✔
1707
{
1708
   assert(tree_kind(value) == T_FCALL);
214✔
1709

1710
   if (!(tree_flags(value) & TREE_F_CONVERSION))
214✔
1711
      return value;
1712

1713
   if (!tree_has_ref(value))
192✔
1714
      return value;
1715

1716
   tree_t decl = tree_ref(value);
191✔
1717
   if (tree_ports(decl) != 1)
191✔
1718
      return value;
1719

1720
   tree_t p0 = tree_value(tree_param(value, 0));
189✔
1721

1722
   tree_t ref = name_to_ref(p0);
189✔
1723
   if (ref == NULL || class_of(ref) != C_SIGNAL)
189✔
1724
      return value;
6✔
1725

1726
   tree_t conv = tree_new(T_CONV_FUNC);
183✔
1727
   tree_set_loc(conv, tree_loc(value));
183✔
1728
   tree_set_value(conv, p0);
183✔
1729
   tree_set_ident(conv, tree_ident(value));
183✔
1730
   tree_set_type(conv, tree_type(value));
183✔
1731
   tree_set_ref(conv, decl);
183✔
1732

1733
   return conv;
183✔
1734
}
1735

1736
static void instantiate_subprogram(tree_t new, tree_t decl, tree_t body)
129✔
1737
{
1738
   tree_t roots[] = { decl, body };
129✔
1739
   ident_t prefixes[] = { tree_ident(decl) };
129✔
1740
   ident_t dotted = ident_prefix(scope_prefix(nametab), tree_ident(new), '.');
129✔
1741
   new_instance(roots, body != NULL ? 2 :1, dotted, prefixes, 1);
138✔
1742

1743
   tree_t decl_copy = roots[0], body_copy = roots[1];
129✔
1744
   tree_t src = body_copy ?: decl_copy;
129✔
1745

1746
   tree_set_type(new, tree_type(src));
129✔
1747
   tree_set_flag(new, tree_flags(decl));
129✔
1748
   tree_set_global_flags(new, tree_global_flags(decl));
129✔
1749

1750
   const int ngenerics = tree_generics(src);
129✔
1751
   for (int i = 0; i < ngenerics; i++)
881✔
1752
      tree_add_generic(new, tree_generic(src, i));
752✔
1753

1754
   const int nports = tree_ports(src);
129✔
1755
   for (int i = 0; i < nports; i++)
308✔
1756
      tree_add_port(new, tree_port(src, i));
179✔
1757

1758
   if (body != NULL) {
129✔
1759
      const int ndecls = tree_decls(body_copy);
120✔
1760
      for (int i = 0; i < ndecls; i++)
218✔
1761
         tree_add_decl(new, tree_decl(body_copy, i));
98✔
1762

1763
      const int nstmts = tree_stmts(body_copy);
120✔
1764
      for (int i = 0; i < nstmts; i++)
315✔
1765
         tree_add_stmt(new, tree_stmt(body_copy, i));
195✔
1766

1767
      tree_set_flag(new, tree_flags(body));
120✔
1768
      tree_set_global_flags(new, tree_global_flags(body));
120✔
1769
   }
1770

1771
   // Allow recursive calls to the uninstantiated subprogram
1772
   map_generic_subprogram(nametab, decl_copy, new);
129✔
1773
   if (body != NULL) map_generic_subprogram(nametab, body_copy, new);
129✔
1774
}
129✔
1775

1776
static void instantiate_package(tree_t new, tree_t pack, tree_t body)
268✔
1777
{
1778
   assert(body == NULL || tree_primary(body) == pack);
268✔
1779

1780
   ident_t prefix = tree_ident(pack);
268✔
1781
   tree_t container = tree_container(pack);
268✔
1782
   if (container != pack)
268✔
1783
      prefix = ident_prefix(tree_ident(container), prefix, '.');
17✔
1784

1785
   tree_t roots[] = { pack, body };
268✔
1786
   ident_t prefixes[] = { prefix };
268✔
1787
   ident_t dotted = ident_prefix(scope_prefix(nametab), tree_ident(new), '.');
268✔
1788
   new_instance(roots, body != NULL ? 2 : 1, dotted, prefixes, 1);
349✔
1789

1790
   tree_t pack_copy = roots[0], body_copy = roots[1];
268✔
1791

1792
   const int ngenerics = tree_generics(pack_copy);
268✔
1793
   for (int i = 0; i < ngenerics; i++)
1,118✔
1794
      tree_add_generic(new, tree_generic(pack_copy, i));
850✔
1795

1796
   const int ndecls = tree_decls(pack_copy);
268✔
1797
   for (int i = 0; i < ndecls; i++) {
5,091✔
1798
      tree_t d = tree_decl(pack_copy, i);
4,823✔
1799
      if (tree_kind(d) == T_CONST_DECL && !tree_has_value(d))
4,823✔
1800
         continue;   // Skip deferred constants
9✔
1801

1802
      tree_add_decl(new, d);
4,814✔
1803
   }
1804

1805
   tree_set_global_flags(new, tree_global_flags(pack));
268✔
1806

1807
   if (body != NULL) {
268✔
1808
      // Copy all the declarations from the body into the package to
1809
      // save keeping track of two separate units. The LRM says the
1810
      // implicit instantiated package body is in the body of an
1811
      // enclosing package if this is in a package declaration. Just
1812
      // ignore that, it doesn't matter.
1813
      const int ndecls = tree_decls(body_copy);
187✔
1814
      for (int i = 0; i < ndecls; i++)
4,050✔
1815
         tree_add_decl(new, tree_decl(body_copy, i));
3,863✔
1816

1817
      tree_set_global_flags(new, tree_global_flags(body));
187✔
1818
   }
1819
}
268✔
1820

1821
static void add_interface(tree_t container, tree_t decl, tree_kind_t kind)
37,865✔
1822
{
1823
   if (kind == T_GENERIC_DECL)
37,865✔
1824
      tree_add_generic(container, decl);
3,245✔
1825
   else
1826
      tree_add_port(container, decl);
34,620✔
1827
}
37,865✔
1828

1829
static void ident_list_push(ident_list_t **list, ident_t i, loc_t loc)
65,795✔
1830
{
1831
   ident_list_t *c = xmalloc(sizeof(ident_list_t));
65,795✔
1832
   c->ident = i;
65,795✔
1833
   c->loc   = loc;
65,795✔
1834
   c->next  = NULL;
65,795✔
1835

1836
   ident_list_t **it;
65,795✔
1837
   for (it = list; *it; it = &((*it)->next));
73,239✔
1838
   *it = c;
65,795✔
1839
}
65,795✔
1840

1841
static void _ident_list_cleanup(ident_list_t **list)
60,282✔
1842
{
1843
   for (ident_list_t *it = *list, *tmp; it; it = tmp) {
126,077✔
1844
      tmp = it->next;
65,795✔
1845
      free(it);
65,795✔
1846
   }
1847
   *list = NULL;
60,282✔
1848
}
60,282✔
1849

1850
static type_t get_subtype_for(tree_t expr)
149✔
1851
{
1852
   type_t type = tree_type(expr);
149✔
1853

1854
   type_t sub = type_new(T_SUBTYPE);
149✔
1855
   type_set_base(sub, type_base_recur(type));
149✔
1856

1857
   const loc_t *loc = tree_loc(expr);
149✔
1858

1859
   tree_t c = tree_new(T_CONSTRAINT);
149✔
1860
   tree_set_loc(c, loc);
149✔
1861

1862
   type_set_constraint(sub, c);
149✔
1863

1864
   if (type_is_record(type)) {
149✔
1865
      tree_set_subkind(c, C_RECORD);
21✔
1866

1867
      const int nfields = type_fields(type);
21✔
1868
      for (int i = 0; i < nfields; i++) {
49✔
1869
         tree_t f = type_field(type, i);
28✔
1870
         type_t ft = tree_type(f);
28✔
1871
         if (type_is_unconstrained(ft)) {
28✔
1872
            tree_t rref = tree_new(T_RECORD_REF);
25✔
1873
            tree_set_ident(rref, tree_ident(f));
25✔
1874
            tree_set_loc(rref, loc);
25✔
1875
            tree_set_ref(rref, f);
25✔
1876
            tree_set_value(rref, expr);
25✔
1877
            tree_set_type(rref, ft);
25✔
1878

1879
            tree_t ec = tree_new(T_ELEM_CONSTRAINT);
25✔
1880
            tree_set_loc(ec, loc);
25✔
1881
            tree_set_ident(ec, tree_ident(f));
25✔
1882
            tree_set_ref(ec, f);
25✔
1883
            tree_set_type(ec, get_subtype_for(rref));
25✔
1884

1885
            tree_add_range(c, ec);
25✔
1886
         }
1887
      }
1888
   }
1889
   else if (type_is_array(type)) {
128✔
1890
      tree_set_subkind(c, C_INDEX);
122✔
1891

1892
      const int ndims = dimension_of(type);
122✔
1893
      for (int i = 0; i < ndims; i++) {
247✔
1894
         tree_t rref = tree_new(T_ATTR_REF);
125✔
1895
         tree_set_name(rref, expr);
125✔
1896
         tree_set_ident(rref, ident_new("RANGE"));
125✔
1897
         tree_set_loc(rref, loc);
125✔
1898
         tree_set_subkind(rref, ATTR_RANGE);
125✔
1899

1900
         if (i > 0) {
125✔
1901
            tree_t p = tree_new(T_LITERAL);
3✔
1902
            tree_set_subkind(p, L_INT);
3✔
1903
            tree_set_ival(p, i + 1);
3✔
1904
            tree_set_loc(p, loc);
3✔
1905
            tree_set_type(p, std_type(NULL, STD_UNIVERSAL_INTEGER));
3✔
1906

1907
            add_param(rref, p, P_POS, NULL);
3✔
1908
         }
1909

1910
         tree_t r = tree_new(T_RANGE);
125✔
1911
         tree_set_subkind(r, RANGE_EXPR);
125✔
1912
         tree_set_value(r, rref);
125✔
1913

1914
         solve_types(nametab, r, NULL);
125✔
1915

1916
         tree_add_range(c, r);
125✔
1917
      }
1918

1919
      type_set_elem(sub, get_element_subtype(expr));
122✔
1920
   }
1921
   else if (type_is_scalar(type))
6✔
1922
      return type;   // TODO: see test/regress/integer3.vhd
1923
   else
1924
      fatal_trace("unhandled type %s in get_subtype_for", type_pp(type));
1925

1926
   return sub;
1927
}
1928

1929
static type_t get_element_subtype(tree_t expr)
162✔
1930
{
1931
   type_t type = tree_type(expr);
162✔
1932
   assert(type_is_array(type));
162✔
1933

1934
   type_t elem = type_elem(type);
162✔
1935
   if (type_const_bounds(elem))
162✔
1936
      return elem;
1937

1938
   tree_t aref = tree_new(T_ARRAY_REF);
43✔
1939
   tree_set_value(aref, expr);
43✔
1940
   tree_set_type(aref, elem);
43✔
1941
   tree_set_loc(aref, tree_loc(expr));
43✔
1942

1943
   const int ndims = dimension_of(type);
43✔
1944
   for (int i = 0; i < ndims; i++) {
89✔
1945
      tree_t left = tree_new(T_ATTR_REF);
46✔
1946
      tree_set_loc(left, tree_loc(expr));
46✔
1947
      tree_set_name(left, expr);
46✔
1948
      tree_set_subkind(left, ATTR_LEFT);
46✔
1949
      tree_set_ident(left, ident_new("LEFT"));
46✔
1950
      tree_set_type(left, index_type_of(type, i));
46✔
1951

1952
      add_param(aref, left, P_POS, NULL);
46✔
1953
   }
1954

1955
   return get_subtype_for(aref);
43✔
1956
}
1957

1958
static type_t apply_subtype_attribute(tree_t aref)
123✔
1959
{
1960
   assert(tree_subkind(aref) == ATTR_SUBTYPE);
123✔
1961

1962
   tree_t name = tree_name(aref);
123✔
1963
   type_t type = get_type_or_null(name);
123✔
1964

1965
   if (type == NULL) {
123✔
1966
      parse_error(tree_loc(aref), "prefix of 'SUBTYPE attribute does not "
1✔
1967
                  "have a type");
1968
      return type_new(T_NONE);
1✔
1969
   }
1970
   else if (type_const_bounds(type))
122✔
1971
      return type;
1972
   else {
1973
      // Construct a new subtype using the constraints from the prefix
1974
      return get_subtype_for(name);
66✔
1975
   }
1976
}
1977

1978
static type_t apply_element_attribute(tree_t aref)
44✔
1979
{
1980
   assert(tree_subkind(aref) == ATTR_ELEMENT);
44✔
1981

1982
   tree_t prefix = tree_name(aref);
44✔
1983
   type_t type = get_type_or_null(prefix);
44✔
1984

1985
   if (type == NULL) {
44✔
1986
      parse_error(tree_loc(aref), "prefix of 'ELEMENT attribute does not "
1✔
1987
                  "have a type");
1988
      return type_new(T_NONE);
1✔
1989
   }
1990
   else if (type_is_none(type))
43✔
1991
      return type;
1992
   else if (!type_is_array(type)) {
41✔
1993
      parse_error(tree_loc(aref), "prefix of 'ELEMENT attribute must be an "
1✔
1994
                  "array type");
1995
      return type_new(T_NONE);
1✔
1996
   }
1997

1998
   return get_element_subtype(prefix);
40✔
1999
}
2000

2001
static type_t apply_designated_subtype_attribute(tree_t aref)
7✔
2002
{
2003
   assert(tree_subkind(aref) == ATTR_DESIGNATED_SUBTYPE);
7✔
2004

2005
   type_t type = get_type_or_null(tree_name(aref));
7✔
2006

2007
   if (type == NULL) {
7✔
2008
      parse_error(tree_loc(aref), "prefix of 'DESIGNATED_SUBTYPE attribute "
1✔
2009
                  "does not have a type");
2010
      return type_new(T_NONE);
1✔
2011
   }
2012
   else if (!type_is_file(type) && !type_is_access(type)) {
6✔
2013
      parse_error(tree_loc(aref), "prefix of 'DESIGNATED_SUBTYPE attribute "
1✔
2014
                  "must be an access or file type");
2015
      return type_new(T_NONE);
1✔
2016
   }
2017

2018
   return type_designated(type);
5✔
2019
}
2020

2021
static type_t apply_base_attribute(tree_t aref)
12✔
2022
{
2023
   assert(tree_subkind(aref) == ATTR_BASE);
12✔
2024

2025
   tree_t name = tree_name(aref);
12✔
2026
   type_t type = NULL;
12✔
2027

2028
   if (tree_kind(name) == T_REF && tree_has_ref(name)) {
12✔
2029
      tree_t decl = aliased_type_decl(tree_ref(name));
12✔
2030
      if (decl != NULL)
12✔
2031
         type = tree_type(decl);
11✔
2032
   }
2033

2034
   if (type == NULL) {
11✔
2035
      parse_error(tree_loc(aref), "prefix of 'BASE attribute must be a type "
1✔
2036
                  "or subtype declaration");
2037
      return type_new(T_NONE);
1✔
2038
   }
2039
   else if (type_kind(type) == T_SUBTYPE)
11✔
2040
      return type_base(type);
6✔
2041
   else
2042
      return type;
2043
}
2044

2045
static type_t apply_index_attribute(tree_t aref)
13✔
2046
{
2047
   assert(tree_subkind(aref) == ATTR_INDEX);
13✔
2048

2049
   type_t type = get_type_or_null(tree_name(aref));
13✔
2050

2051
   if (!type_is_array(type)) {
13✔
2052
      parse_error(tree_loc(aref), "prefix of 'INDEX attribute must be an "
1✔
2053
                  "array type");
2054
      return type_new(T_NONE);
1✔
2055
   }
2056

2057
   const int ndims = dimension_of(type);
12✔
2058
   const int nparams = tree_params(aref);
12✔
2059

2060
   int index = 0;
12✔
2061
   if (nparams == 1) {
12✔
2062
      // The LRM allows any locally static expression here but that is
2063
      // difficult to implement and doesn't seem useful
2064
      tree_t p = tree_value(tree_param(aref, 0));
6✔
2065
      if (tree_kind(p) != T_LITERAL) {
6✔
2066
         parse_error(tree_loc(p), "only integer literals are supported "
1✔
2067
                     "for 'INDEX parameter");
2068
         return type_new(T_NONE);
1✔
2069
      }
2070

2071
      const int64_t ival = tree_ival(p);
5✔
2072
      if (ival < 1 || ival > ndims) {
5✔
2073
         parse_error(tree_loc(p), "'INDEX parameter for type %s must be "
2✔
2074
                     "between 1 and %d", type_pp(type), ndims);
2075
         return type_new(T_NONE);
2✔
2076
      }
2077

2078
      index = ival - 1;
3✔
2079
   }
2080

2081
   return index_type_of(type, index);
9✔
2082
}
2083

2084
static type_t apply_type_attribute(tree_t aref)
199✔
2085
{
2086
   switch (tree_subkind(aref)) {
199✔
2087
   case ATTR_SUBTYPE:
123✔
2088
      return apply_subtype_attribute(aref);
123✔
2089
   case ATTR_ELEMENT:
44✔
2090
      return apply_element_attribute(aref);
44✔
2091
   case ATTR_BASE:
12✔
2092
      return apply_base_attribute(aref);
12✔
2093
   case ATTR_DESIGNATED_SUBTYPE:
7✔
2094
      return apply_designated_subtype_attribute(aref);
7✔
2095
   case ATTR_INDEX:
13✔
2096
      return apply_index_attribute(aref);
13✔
UNCOV
2097
   default:
×
UNCOV
2098
      parse_error(tree_loc(aref), "attribute name is not a valid type mark");
×
UNCOV
2099
      return type_new(T_NONE);
×
2100
   }
2101
}
2102

2103
static void implicit_signal_attribute(tree_t aref)
288✔
2104
{
2105
   if (find_enclosing(nametab, S_SUBPROGRAM) != NULL) {
288✔
2106
      parse_error(tree_loc(aref), "implicit signal %s cannot be used in a "
4✔
2107
                  "subprogram body", istr(tree_ident(aref)));
2108
      return;
149✔
2109
   }
2110

2111
   tree_t b = find_enclosing(nametab, S_CONCURRENT_BLOCK);
284✔
2112
   if (b == NULL) {
284✔
UNCOV
2113
      parse_error(tree_loc(aref), "implicit signal %s cannot be used in "
×
2114
                  "this context", istr(tree_ident(aref)));
UNCOV
2115
      return;
×
2116
   }
2117

2118
   tree_t prefix = tree_name(aref);
284✔
2119
   const attr_kind_t attr = tree_subkind(aref);
284✔
2120

2121
   tree_t delay = NULL;
284✔
2122
   if (attr != ATTR_TRANSACTION && tree_params(aref) > 0)
284✔
2123
      delay = tree_value(tree_param(aref, 0));
103✔
2124

2125
   LOCAL_TEXT_BUF tb = tb_new();
423✔
2126
   tree_t ref = name_to_ref(prefix);
284✔
2127
   if (ref != NULL)
284✔
2128
      tb_istr(tb, tree_ident(ref));
283✔
2129
   tb_append(tb, '$');
284✔
2130
   switch (attr) {
284✔
2131
   case ATTR_DELAYED: tb_cat(tb, "delayed"); break;
152✔
2132
   case ATTR_TRANSACTION: tb_cat(tb, "transaction"); break;
32✔
2133
   case ATTR_STABLE: tb_cat(tb, "stable"); break;
77✔
2134
   case ATTR_QUIET: tb_cat(tb, "quiet"); break;
23✔
2135
   default: break;
2136
   }
2137
   if (delay != NULL && tree_kind(delay) == T_LITERAL) {
284✔
2138
      tb_printf(tb, "_%"PRIi64, tree_ival(delay));
84✔
2139
      if (tree_has_ident(delay))
84✔
2140
         tb_printf(tb, "_%s", istr(tree_ident(delay)));
83✔
2141
   }
2142
   else if (delay != NULL && tree_kind(delay) == T_REF)
200✔
2143
      tb_printf(tb, "_%s", istr(tree_ident(delay)));
18✔
2144

2145
   ident_t id = ident_new(tb_get(tb));
284✔
2146

2147
   implicit_kind_t kind;
284✔
2148
   switch (attr) {
284✔
2149
   case ATTR_DELAYED: kind = IMPLICIT_DELAYED; break;
2150
   case ATTR_STABLE: kind = IMPLICIT_STABLE; break;
2151
   case ATTR_QUIET: kind = IMPLICIT_QUIET; break;
2152
   case ATTR_TRANSACTION: kind = IMPLICIT_TRANSACTION; break;
UNCOV
2153
   default:
×
2154
      fatal_trace("invalid implicit signal attribute");
2155
   }
2156

2157
   const int ndecls = tree_decls(b);
284✔
2158
   for (int i = 0; i < ndecls; i++) {
1,339✔
2159
      tree_t d = tree_decl(b, i);
1,200✔
2160
      if (tree_kind(d) != T_IMPLICIT_SIGNAL)
1,200✔
2161
         continue;
791✔
2162
      else if (tree_ident(d) != id || tree_subkind(d) != kind)
409✔
2163
         continue;
209✔
2164

2165
      bool match = false;
200✔
2166
      tree_t value = tree_value(d);
200✔
2167
      if (attr == ATTR_DELAYED || attr == ATTR_STABLE || attr == ATTR_QUIET) {
200✔
2168
         assert(tree_kind(value) == T_WAVEFORM);
182✔
2169

2170
         match = same_tree(tree_value(value), prefix)
364✔
2171
            && ((delay == NULL && !tree_has_delay(value))
182✔
2172
                || (delay != NULL && tree_has_delay(value)
55✔
2173
                    && same_tree(tree_delay(value), delay)));
54✔
2174
      }
2175
      else if (attr == ATTR_TRANSACTION)
18✔
2176
         match = same_tree(value, prefix);
18✔
2177

2178
      if (match) {
200✔
2179
         tree_set_value(aref, make_ref(d));
145✔
2180
         return;
145✔
2181
      }
2182
      else {
2183
         tb_append(tb, '_');
55✔
2184
         id = ident_new(tb_get(tb));
55✔
2185
      }
2186
   }
2187

2188
   tree_t imp = tree_new(T_IMPLICIT_SIGNAL);
139✔
2189
   tree_set_ident(imp, id);
139✔
2190
   tree_set_loc(imp, tree_loc(aref));
139✔
2191
   tree_set_subkind(imp, kind);
139✔
2192

2193
   if (attr == ATTR_TRANSACTION) {
139✔
2194
      tree_set_type(imp, std_type(NULL, STD_BIT));
14✔
2195
      tree_set_value(imp, prefix);
14✔
2196
   }
2197
   else {
2198
      tree_t w = tree_new(T_WAVEFORM);
125✔
2199
      tree_set_loc(w, CURRENT_LOC);
125✔
2200
      tree_set_value(w, prefix);
125✔
2201
      if (delay != NULL)
125✔
2202
         tree_set_delay(w, delay);
88✔
2203

2204
      tree_set_type(imp, solve_types(nametab, aref, NULL));
125✔
2205
      tree_set_value(imp, w);
125✔
2206
   }
2207

2208
   tree_add_decl(b, imp);
139✔
2209
   tree_set_value(aref, make_ref(imp));
139✔
2210
}
2211

2212
static attr_kind_t parse_predefined_attr(ident_t ident)
20,354✔
2213
{
2214
   static struct {
20,354✔
2215
      const char      *str;
2216
      attr_kind_t      attr;
2217
      vhdl_standard_t  std;
2218
      uint32_t         hash;
2219
      ident_t          ident;
2220
   } predef[] = {
2221
      { "RANGE", ATTR_RANGE },
2222
      { "REVERSE_RANGE", ATTR_REVERSE_RANGE },
2223
      { "LENGTH", ATTR_LENGTH },
2224
      { "LEFT", ATTR_LEFT },
2225
      { "RIGHT", ATTR_RIGHT },
2226
      { "LOW", ATTR_LOW },
2227
      { "HIGH", ATTR_HIGH },
2228
      { "EVENT", ATTR_EVENT },
2229
      { "ACTIVE", ATTR_ACTIVE },
2230
      { "IMAGE", ATTR_IMAGE },
2231
      { "ASCENDING", ATTR_ASCENDING },
2232
      { "LAST_VALUE", ATTR_LAST_VALUE },
2233
      { "LAST_EVENT", ATTR_LAST_EVENT },
2234
      { "LAST_ACTIVE", ATTR_LAST_ACTIVE },
2235
      { "PATH_NAME", ATTR_PATH_NAME },
2236
      { "INSTANCE_NAME", ATTR_INSTANCE_NAME },
2237
      { "SIMPLE_NAME", ATTR_SIMPLE_NAME },
2238
      { "DELAYED", ATTR_DELAYED },
2239
      { "STABLE", ATTR_STABLE },
2240
      { "QUIET", ATTR_QUIET },
2241
      { "TRANSACTION", ATTR_TRANSACTION },
2242
      { "DRIVING_VALUE", ATTR_DRIVING_VALUE },
2243
      { "DRIVING", ATTR_DRIVING },
2244
      { "VALUE", ATTR_VALUE },
2245
      { "SUCC", ATTR_SUCC },
2246
      { "PRED", ATTR_PRED },
2247
      { "LEFTOF", ATTR_LEFTOF },
2248
      { "RIGHTOF", ATTR_RIGHTOF },
2249
      { "POS", ATTR_POS },
2250
      { "VAL", ATTR_VAL },
2251
      { "BASE", ATTR_BASE },
2252
      { "ELEMENT", ATTR_ELEMENT, STD_08 },
2253
      { "CONVERSE", ATTR_CONVERSE, STD_19 },
2254
      { "DESIGNATED_SUBTYPE", ATTR_DESIGNATED_SUBTYPE, STD_19 },
2255
      { "INDEX", ATTR_INDEX, STD_19 },
2256
      { "REFLECT", ATTR_REFLECT, STD_19 },
2257
   };
2258

2259
   INIT_ONCE({
65,462✔
2260
         for (int i = 0; i < ARRAY_LEN(predef); i++) {
2261
            predef[i].ident = ident_new(predef[i].str);
2262
            predef[i].hash  = ident_casehash(predef[i].ident);
2263
         }
2264
      });
2265

2266
   const vhdl_standard_t std = standard();
20,354✔
2267
   const uint32_t hash = ident_casehash(ident);
20,354✔
2268

2269
   for (int i = 0; i < ARRAY_LEN(predef) && predef[i].std <= std; i++) {
135,194✔
2270
      if (predef[i].hash == hash && ident_casecmp(predef[i].ident, ident))
134,971✔
2271
         return predef[i].attr;
20,131✔
2272
   }
2273

2274
   return ATTR_USER;
2275
}
2276

2277
static void add_generic_type_op(tree_t parent, int nargs, type_t type,
972✔
2278
                                type_t result, const char *name)
2279
{
2280
   ident_t id = ident_new(name);
972✔
2281

2282
   type_t ftype = type_new(T_SIGNATURE);
972✔
2283
   type_set_ident(ftype, id);
972✔
2284
   type_set_result(ftype, result);
972✔
2285

2286
   for (int i = 0; i < nargs; i++)
2,817✔
2287
      type_add_param(ftype, type);
1,845✔
2288

2289
   tree_t p = tree_new(T_GENERIC_DECL);
972✔
2290
   tree_set_class(p, C_FUNCTION);
972✔
2291
   tree_set_ident(p, id);
972✔
2292
   tree_set_type(p, ftype);
972✔
2293
   tree_set_subkind(p, PORT_IN);
972✔
2294
   tree_set_loc(p, CURRENT_LOC);
972✔
2295
   tree_set_flag(p, TREE_F_PREDEFINED);
972✔
2296

2297
   // LRM 08 section 6.5.3.1: the *predefined* [..] operators,
2298
   // implicitly declared as formal generic subprograms
2299
   //
2300
   // LCS2016-59 changed the wording here: additional operators are
2301
   // implicitly declared as formal generic subprograms with an
2302
   // interface subprogram default in form of a box (<>)
2303
   //
2304
   // The 2008 LRM seems to be ambiguous as to whether we should map the
2305
   // predefined operator or do a lookup for a matching visible
2306
   // operator.  We always follow the 2019 behaviour here.
2307

2308
   tree_t box = tree_new(T_BOX);
972✔
2309
   tree_set_loc(box, CURRENT_LOC);
972✔
2310

2311
   tree_set_value(p, box);
972✔
2312

2313
   for (int j = 0; j < nargs; j++) {
2,817✔
2314
      tree_t arg = tree_new(T_PARAM_DECL);
1,845✔
2315
      tree_set_ident(arg, ident_new(j == 0 ? "L" : "R"));
2,718✔
2316
      tree_set_type(arg, type);
1,845✔
2317
      tree_set_subkind(arg, PORT_IN);
1,845✔
2318
      tree_set_class(arg, C_CONSTANT);
1,845✔
2319
      tree_set_loc(arg, CURRENT_LOC);
1,845✔
2320

2321
      tree_add_port(p, arg);
1,845✔
2322
   }
2323

2324
   add_interface(parent, p, T_GENERIC_DECL);
972✔
2325
   insert_name(nametab, p, NULL);
972✔
2326
}
972✔
2327

2328
static void declare_generic_ops(tree_t parent, type_t type)
256✔
2329
{
2330
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
256✔
2331
   type_t std_string = std_type(NULL, STD_STRING);
256✔
2332

2333
   const gtype_class_t class = type_subkind(type);
256✔
2334

2335
   switch (class) {
256✔
2336
   case GTYPE_INTEGER:
11✔
2337
      add_generic_type_op(parent, 2, type, type, "\"**\"");
11✔
2338
      // Fall-through
2339
   case GTYPE_PHYSICAL:
15✔
2340
      add_generic_type_op(parent, 2, type, type, "\"mod\"");
15✔
2341
      add_generic_type_op(parent, 2, type, type, "\"rem\"");
15✔
2342
      // Fall-through
2343
   case GTYPE_FLOATING:
19✔
2344
      add_generic_type_op(parent, 2, type, type, "\"+\"");
19✔
2345
      add_generic_type_op(parent, 2, type, type, "\"-\"");
19✔
2346
      add_generic_type_op(parent, 1, type, type, "\"+\"");
19✔
2347
      add_generic_type_op(parent, 1, type, type, "\"-\"");
19✔
2348
      if (class != GTYPE_PHYSICAL) {
19✔
2349
         add_generic_type_op(parent, 2, type, type, "\"*\"");
15✔
2350
         add_generic_type_op(parent, 2, type, type, "\"/\"");
15✔
2351
      }
2352
      add_generic_type_op(parent, 1, type, type, "\"abs\"");
19✔
2353
      // Fall-through
2354
   case GTYPE_DISCRETE:
42✔
2355
   case GTYPE_SCALAR:
2356
      add_generic_type_op(parent, 2, type, std_bool, "\"<\"");
42✔
2357
      add_generic_type_op(parent, 2, type, std_bool, "\">\"");
42✔
2358
      add_generic_type_op(parent, 2, type, std_bool, "\"<=\"");
42✔
2359
      add_generic_type_op(parent, 2, type, std_bool, "\">=\"");
42✔
2360
      add_generic_type_op(parent, 2, type, type, "MINIMUM");
42✔
2361
      add_generic_type_op(parent, 2, type, type, "MAXIMUM");
42✔
2362
      add_generic_type_op(parent, 1, type, std_string, "TO_STRING");
42✔
2363
      // Fall-through
2364
   case GTYPE_ARRAY:
256✔
2365
   case GTYPE_FILE:
2366
   case GTYPE_ACCESS:
2367
   case GTYPE_PRIVATE:
2368
      add_generic_type_op(parent, 2, type, std_bool, "\"=\"");
256✔
2369
      add_generic_type_op(parent, 2, type, std_bool, "\"/=\"");
256✔
2370
      break;
256✔
2371
   }
2372

2373
   if (class == GTYPE_ACCESS) {
256✔
2374
      ident_t id = ident_new("DEALLOCATE");
5✔
2375

2376
      type_t ftype = type_new(T_SIGNATURE);
5✔
2377
      type_set_ident(ftype, id);
5✔
2378
      type_add_param(ftype, type);
5✔
2379

2380
      tree_t p = tree_new(T_GENERIC_DECL);
5✔
2381
      tree_set_class(p, C_PROCEDURE);
5✔
2382
      tree_set_ident(p, id);
5✔
2383
      tree_set_type(p, ftype);
5✔
2384
      tree_set_subkind(p, PORT_IN);
5✔
2385
      tree_set_loc(p, CURRENT_LOC);
5✔
2386
      tree_set_flag(p, TREE_F_PREDEFINED);
5✔
2387

2388
      tree_t box = tree_new(T_BOX);
5✔
2389
      tree_set_loc(box, CURRENT_LOC);
5✔
2390

2391
      tree_set_value(p, box);
5✔
2392

2393
      tree_t arg = tree_new(T_PARAM_DECL);
5✔
2394
      tree_set_ident(arg, ident_new("PTR"));
5✔
2395
      tree_set_type(arg, type);
5✔
2396
      tree_set_subkind(arg, PORT_INOUT);
5✔
2397
      tree_set_class(arg, C_VARIABLE);
5✔
2398
      tree_set_loc(arg, CURRENT_LOC);
5✔
2399

2400
      tree_add_port(p, arg);
5✔
2401

2402
      add_interface(parent, p, T_GENERIC_DECL);
5✔
2403
      insert_name(nametab, p, NULL);
5✔
2404
   }
2405
   else if (class == GTYPE_ARRAY) {
251✔
2406
      // Declare predefined operators for any anonymous element or index
2407
      type_t elem = type_elem(type);
20✔
2408
      if (type_kind(elem) == T_GENERIC && !type_has_ident(elem))
20✔
2409
         declare_generic_ops(parent, elem);
11✔
2410

2411
      const int nindex = type_indexes(type);
20✔
2412
      for (int i = 0; i < nindex; i++) {
40✔
2413
         type_t index = type_index(type, i);
20✔
2414
         if (type_kind(index) == T_GENERIC && !type_has_ident(index))
20✔
2415
            declare_generic_ops(parent, index);
9✔
2416
      }
2417
   }
2418
}
256✔
2419

2420
static bool is_vhdl_infix_op(token_t tok)
84✔
2421
{
2422
   return tok == tEQ || tok == tNEQ || tok == tLT || tok == tGT
84✔
2423
      || tok == tLE || tok == tGE || tok == tNAND || tok == tNOR
81✔
2424
      || tok == tXOR || tok == tXNOR || tok == tMOD || tok == tREM
2425
      || tok == tPLUS || tok == tMINUS || tok == tTIMES || tok == tOVER
2426
      || tok == tPOWER || tok == tMEQ || tok == tMNEQ || tok == tMLT
2427
      || tok == tMLE || tok == tMGT || tok == tMGE;
84✔
2428
}
2429

2430
static void add_predef_alias(tree_t t, void *context)
711✔
2431
{
2432
   tree_t parent = context;
711✔
2433
   assert(is_subprogram(t));
711✔
2434

2435
   tree_t a = tree_new(T_ALIAS);
711✔
2436
   tree_set_loc(a, CURRENT_LOC);
711✔
2437
   tree_set_ident(a, tree_ident(t));
711✔
2438
   tree_set_value(a, make_ref(t));
711✔
2439
   tree_set_type(a, tree_type(t));
711✔
2440
   tree_set_flag(a, TREE_F_PREDEFINED);
711✔
2441

2442
   insert_name(nametab, a, NULL);
711✔
2443
   tree_add_decl(parent, a);
711✔
2444
}
711✔
2445

2446
static void convert_universal_bounds(tree_t r)
5,749✔
2447
{
2448
   // LRM 08 section 5.3.2.2: an implicit conversion to the predefined
2449
   // type INTEGER is assumed if the type of both bounds is the type
2450
   // universal_integer
2451

2452
   assert(tree_kind(r) == T_RANGE);
5,749✔
2453

2454
   const range_kind_t kind = tree_subkind(r);
5,749✔
2455
   if (kind != RANGE_TO && kind != RANGE_DOWNTO)
5,749✔
2456
      return;
2457

2458
   tree_t left = tree_left(r);
3,929✔
2459
   tree_t right = tree_right(r);
3,929✔
2460

2461
   type_t ltype = tree_type(left);
3,929✔
2462
   type_t rtype = tree_type(right);
3,929✔
2463

2464
   type_t uint = std_type(NULL, STD_UNIVERSAL_INTEGER);
3,929✔
2465
   if (!type_eq(ltype, uint) || !type_eq(rtype, uint))
3,929✔
2466
      return;
3,137✔
2467

2468
   type_t std_int = std_type(NULL, STD_INTEGER);
792✔
2469
   tree_set_type(r, std_int);
792✔
2470

2471
   tree_t lconv = tree_new(T_TYPE_CONV);
792✔
2472
   tree_set_loc(lconv, tree_loc(left));
792✔
2473
   tree_set_type(lconv, std_int);
792✔
2474
   tree_set_value(lconv, left);
792✔
2475

2476
   tree_set_left(r, lconv);
792✔
2477

2478
   tree_t rconv = tree_new(T_TYPE_CONV);
792✔
2479
   tree_set_loc(rconv, tree_loc(right));
792✔
2480
   tree_set_type(rconv, std_int);
792✔
2481
   tree_set_value(rconv, right);
792✔
2482

2483
   tree_set_right(r, rconv);
792✔
2484
}
2485

2486
static type_t name_to_type_mark(tree_t name)
82,537✔
2487
{
2488
   type_t type = solve_types(nametab, name, NULL);
82,537✔
2489

2490
   if (type_is_none(type))
82,537✔
2491
      return type;
2492

2493
   const tree_kind_t namek = tree_kind(name);
82,458✔
2494
   if (namek == T_ATTR_REF)
82,458✔
2495
      return type;
2496

2497
   tree_t decl = NULL;
82,330✔
2498
   if (namek == T_REF && tree_has_ref(name))
82,330✔
2499
      decl = aliased_type_decl(tree_ref(name));
82,328✔
2500

2501
   if (decl == NULL) {
82,328✔
2502
      diag_t *d = diag_new(DIAG_ERROR, CURRENT_LOC);
8✔
2503
      const char *id = namek == T_REF ? istr(tree_ident(name)) : NULL;
8✔
2504
      diag_printf(d, "type mark%s%s does not denote a type or a subtype",
10✔
2505
                  id ? " " : "", id ?: "");
2506
      diag_hint(d, CURRENT_LOC, "%s is a %s name", id ?: "this",
10✔
2507
                class_str(class_of(name)));
2508
      diag_emit(d);
8✔
2509
      return type_new(T_NONE);
8✔
2510
   }
2511

2512
   return tree_type(decl);
82,322✔
2513
}
2514

2515
static void find_disconnect_specification(tree_t guard, tree_t target)
22✔
2516
{
2517
   if (tree_kind(target) != T_REF)
22✔
2518
      return;
1✔
2519
   else if (!tree_has_ref(target))
21✔
2520
      return;
2521

2522
   tree_t decl = tree_ref(target);
21✔
2523

2524
   // TODO: use insert_spec for this
2525
   ident_t name = ident_prefix(tree_ident(decl), ident_new("disconnect"), '$');
21✔
2526

2527
   tree_t spec = NULL;
21✔
2528
   query_name(nametab, name, &spec);
21✔
2529

2530
   if (spec != NULL)
21✔
2531
      tree_set_spec(guard, spec);
6✔
2532
}
2533

2534
static tree_t find_subprogram_body(tree_t decl)
130✔
2535
{
2536
   const tree_kind_t decl_kind = tree_kind(decl);
130✔
2537
   if (decl_kind == T_FUNC_BODY || decl_kind == T_PROC_BODY)
130✔
2538
      return decl;
2539

2540
   // Attempt to load the package body if available
2541
   tree_t pack = tree_container(decl);
54✔
2542
   if (tree_kind(pack) != T_PACKAGE)
54✔
2543
      return NULL;
2544

2545
   tree_t du = find_enclosing(nametab, S_DESIGN_UNIT);
53✔
2546
   if (du == pack)
53✔
2547
      return NULL;   // Avoid referencing old version of current package
2548

2549
   tree_t pack_body, d;
45✔
2550
   if (tree_kind(du) == T_PACK_BODY && tree_primary(du) == pack)
45✔
2551
      pack_body = du;
2552
   else if ((pack_body = body_of(pack)) == NULL)
37✔
2553
      return NULL;
2554

2555
   type_t type = tree_type(decl);
45✔
2556
   ident_t id = tree_ident(decl);
45✔
2557
   for (int nth = 0; (d = get_local_decl(nametab, pack_body, id, nth)); nth++) {
109✔
2558
      if (is_subprogram(d) && is_body(d) && type_eq(tree_type(d), type))
108✔
2559
         return d;
44✔
2560
   }
2561

2562
   return NULL;
2563
}
2564

2565
static void package_body_deferred_instantiation(tree_t pack, tree_t container)
4✔
2566
{
2567
   // LRM 08 section 4.4: if the subprogram instantiation declaration
2568
   // occurs immediately within an enclosing package declaration, the
2569
   // generic-mapped subprogram body occurs at the end of the package
2570
   // body corresponding to the enclosing package declaration
2571

2572
   assert(standard() >= STD_08);
4✔
2573

2574
   const int ndecls = tree_decls(pack);
4✔
2575
   for (int i = 0; i < ndecls; i++) {
125✔
2576
      tree_t decl = tree_decl(pack, i);
121✔
2577

2578
      const tree_kind_t dkind = tree_kind(decl);
121✔
2579
      if (dkind != T_FUNC_INST && dkind != T_PROC_INST)
121✔
2580
         continue;
110✔
2581
      else if (!tree_has_ref(decl))
11✔
2582
         continue;
3✔
2583

2584
      tree_t ref = tree_ref(decl);
8✔
2585
      if (is_body(ref))
8✔
UNCOV
2586
         continue;
×
2587

2588
      tree_t body = find_subprogram_body(ref);
8✔
2589
      if (body == NULL) {
8✔
2590
         diag_t *d = diag_new(DIAG_ERROR, CURRENT_LOC);
1✔
2591
         diag_printf(d, "subprogram %s cannot be instantiated until its "
1✔
2592
                     "body has been analysed", type_pp(tree_type(decl)));
2593
         diag_hint(d, tree_loc(decl), "subprogram instantiation in package "
1✔
2594
                   "declarative part");
2595
         diag_hint(d, NULL, "the instantiated subprogram body occurs at "
1✔
2596
                   "the end of the package body corresponding to the "
2597
                   "enclosing package declaration");
2598
         diag_lrm(d, STD_08, "4.4");
1✔
2599
         diag_emit(d);
1✔
2600
      }
2601
      else {
2602
         tree_t inst = tree_new(dkind);
7✔
2603
         tree_set_ident(inst, tree_ident(decl));
7✔
2604
         tree_set_ident2(inst, tree_ident2(decl));
7✔
2605
         tree_set_ref(inst, body);
7✔
2606

2607
         instantiate_subprogram(inst, ref, body);
7✔
2608

2609
         hash_t *gmap = hash_new(16);
7✔
2610
         const int ngenmaps = tree_genmaps(decl);
7✔
2611
         for (int i = 0; i < ngenmaps; i++) {
34✔
2612
            tree_t map = tree_genmap(decl, i);
27✔
2613
            assert(tree_subkind(map) == P_POS);
27✔
2614

2615
            tree_add_genmap(inst, map);
27✔
2616

2617
            tree_t g = tree_generic(inst, tree_pos(map));
27✔
2618
            tree_t value = tree_value(map);
27✔
2619

2620
            switch (tree_class(g)) {
27✔
2621
            case C_TYPE:
7✔
2622
               hash_put(gmap, tree_type(g), tree_type(value));
7✔
2623
               break;
7✔
2624
            case C_FUNCTION:
20✔
2625
            case C_PROCEDURE:
2626
               hash_put(gmap, g, tree_ref(value));
20✔
2627
               break;
20✔
UNCOV
2628
            case C_CONSTANT:
×
UNCOV
2629
               if (is_literal(value))
×
UNCOV
2630
                  hash_put(gmap, g, value);
×
2631
               break;
2632
            default:
×
2633
               should_not_reach_here();
2634
            }
2635
         }
2636

2637
         instance_fixup(inst, gmap);
7✔
2638

2639
         tree_add_decl(container, inst);
7✔
2640
      }
2641
   }
2642
}
4✔
2643

2644
static tree_t find_generic_subprogram_body(tree_t inst, tree_t decl)
122✔
2645
{
2646
   tree_t body = find_subprogram_body(decl);
122✔
2647

2648
   if (body == NULL) {
122✔
2649
      tree_t du = find_enclosing(nametab, S_DESIGN_UNIT);
9✔
2650
      if (tree_kind(du) != T_PACKAGE)
9✔
2651
         parse_error(CURRENT_LOC, "subprogram %s cannot be instantiated "
1✔
2652
                     "until its body has been analysed",
2653
                     istr(tree_ident(decl)));
2654
      else {
2655
         // Will be instantiated at end of package body
2656
         tree_set_ref(inst, decl);
8✔
2657
         tree_set_global_flags(inst, TREE_GF_DEFERRED_INST);
8✔
2658
      }
2659
   }
2660
   else
2661
      tree_set_ref(inst, body);
113✔
2662

2663
   return body;
122✔
2664
}
2665

2666
static psl_node_t with_default_clock(psl_node_t prop)
340✔
2667
{
2668
   if (psl_kind(prop) == P_CLOCKED)
340✔
2669
      return prop;
2670

2671
   psl_node_t def = find_default_clock(nametab);
336✔
2672
   if (def == NULL)
336✔
2673
      return prop;
2674

2675
   psl_node_t p = psl_new(P_CLOCKED);
334✔
2676
   psl_set_value(p, prop);
334✔
2677
   psl_set_ref(p, def);
334✔
2678
   psl_set_loc(p, psl_loc(prop));
334✔
2679

2680
   return p;
334✔
2681
}
2682

2683
////////////////////////////////////////////////////////////////////////////////
2684
// Parser rules
2685

2686
static ident_t p_identifier(void)
483,625✔
2687
{
2688
   // basic_identifier | extended_identifier
2689

2690
   if (consume(tID))
483,625✔
2691
      return last_lval.ident;
483,623✔
2692
   else
2693
      return error_marker();
2✔
2694
}
2695

2696
static ident_t p_selected_identifier(void)
7,140✔
2697
{
2698
   // identifier { . identifier }
2699

2700
   ident_t id = p_identifier();
7,140✔
2701
   while (optional(tDOT))
8,763✔
2702
      id = ident_prefix(id, p_identifier(), '.');
1,623✔
2703

2704
   return id;
7,140✔
2705
}
2706

2707
static ident_list_t *p_identifier_list(void)
60,230✔
2708
{
2709
   // identifier { , identifier }
2710

2711
   ident_list_t *result = NULL;
60,230✔
2712

2713
   ident_list_push(&result, p_identifier(), last_loc);
60,230✔
2714

2715
   while (optional(tCOMMA))
65,750✔
2716
      ident_list_push(&result, p_identifier(), last_loc);
5,520✔
2717

2718
   return result;
60,230✔
2719
}
2720

2721
static ident_t p_operator_symbol(void)
7,543✔
2722
{
2723
   // string_literal
2724

2725
   consume(tSTRING);
7,543✔
2726

2727
   char *s = last_lval.str;
7,543✔
2728
   for (char *p = s; *p != '\0'; p++)
37,916✔
2729
      *p = tolower_iso88591(*p);
30,373✔
2730

2731
   ident_t id = ident_new(s);
7,543✔
2732

2733
   if (!is_operator_symbol(id))
7,543✔
2734
      parse_error(CURRENT_LOC, "%s is not an operator symbol", s);
1✔
2735

2736
   free(s);
7,543✔
2737
   return id;
7,543✔
2738
}
2739

2740
static void p_library_clause(tree_t unit)
1,063✔
2741
{
2742
   // library logical_name_list ;
2743

2744
   BEGIN("library clause");
2,126✔
2745

2746
   consume(tLIBRARY);
1,063✔
2747

2748
   LOCAL_IDENT_LIST ids = p_identifier_list();
2,126✔
2749

2750
   consume(tSEMI);
1,063✔
2751

2752
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
2,126✔
2753
      tree_t l = tree_new(T_LIBRARY);
1,063✔
2754
      tree_set_ident(l, it->ident);
1,063✔
2755
      tree_set_loc(l, &(it->loc));
1,063✔
2756

2757
      tree_add_context(unit, l);
1,063✔
2758

2759
      lib_t lib = lib_find(it->ident);
1,063✔
2760
      if (lib == NULL) {
1,063✔
2761
         LOCAL_TEXT_BUF tb = tb_new();
2✔
2762
         lib_print_search_paths(tb);
1✔
2763

2764
         diag_t *d = diag_new(DIAG_ERROR, CURRENT_LOC);
1✔
2765
         diag_printf(d, "library %s not found", istr(it->ident));
1✔
2766
         lib_search_paths_to_diag(d);
1✔
2767
         diag_emit(d);
1✔
2768
      }
2769
      else
2770
         tree_set_ident2(l, lib_name(lib));
1,062✔
2771

2772
      insert_name(nametab, l, NULL);
1,063✔
2773
   }
2774
}
1,063✔
2775

2776
static void p_use_clause(tree_t unit, add_func_t addf)
2,959✔
2777
{
2778
   // use selected_name { , selected_name } ;
2779

2780
   BEGIN("use clause");
5,918✔
2781

2782
   consume(tUSE);
2,959✔
2783

2784
   do {
2,960✔
2785
      tree_t u = tree_new(T_USE);
2,960✔
2786

2787
      ident_t i1 = p_identifier(), i2 = NULL;
2,960✔
2788
      consume(tDOT);
2,960✔
2789

2790
      do {
5,777✔
2791
         i1 = ident_prefix(i1, i2, '.');
5,777✔
2792

2793
         switch (peek()) {
5,777✔
2794
         case tID:
2,960✔
2795
            i2 = p_identifier();
2,960✔
2796
            break;
2,960✔
2797
         case tSTRING:
7✔
2798
            i2 = p_operator_symbol();
7✔
2799
            break;
7✔
2800
         case tALL:
2,807✔
2801
            consume(tALL);
2,807✔
2802
            i2 = well_known(W_ALL);
2,807✔
2803
            break;
2,807✔
2804
         default:
3✔
2805
            expect(tID, tSTRING, tALL);
3✔
2806
            i2 = NULL;
3✔
2807
            break;
3✔
2808
         }
2809
      } while (optional(tDOT));
5,777✔
2810

2811
      tree_set_ident(u, i1);
2,960✔
2812
      tree_set_ident2(u, i2);
2,960✔
2813

2814
      tree_set_loc(u, CURRENT_LOC);
2,960✔
2815
      (*addf)(unit, u);
2,960✔
2816

2817
      tree_t head = resolve_name(nametab, CURRENT_LOC, i1);
2,960✔
2818
      if (head == NULL)
2,960✔
2819
         continue;
5✔
2820

2821
      const tree_kind_t kind = tree_kind(head);
2,955✔
2822
      if (kind == T_LIBRARY && !tree_has_ident2(head)) {
2,955✔
2823
         // Library declaration had an error
2824
      }
2825
      else if (is_uninstantiated_package(head))
2,955✔
2826
         parse_error(CURRENT_LOC, "cannot use an uninstantiated package");
1✔
2827
      else if (kind == T_LIBRARY || kind == T_PACKAGE
2,954✔
2828
               || kind == T_PACK_INST
153✔
2829
               || (kind == T_GENERIC_DECL
30✔
2830
                   && tree_class(head) == C_PACKAGE)) {
29✔
2831
         tree_set_ref(u, head);
2,953✔
2832
         insert_names_from_use(nametab, u);
2,953✔
2833
      }
2834
      else
2835
         parse_error(CURRENT_LOC, "%s is not a library or %spackage",
1✔
2836
                     istr(i1), standard() >= STD_08 ? "instantiated " : "");
2837
   } while (optional(tCOMMA));
2,960✔
2838

2839
   consume(tSEMI);
2,959✔
2840
}
2,959✔
2841

2842
static void p_context_reference(tree_t unit)
25✔
2843
{
2844
   // context selected_name { , selected_name } ;
2845

2846
   BEGIN("context reference");
50✔
2847

2848
   consume(tCONTEXT);
25✔
2849

2850
   do {
25✔
2851
      ident_t name = p_selected_identifier();
25✔
2852

2853
      tree_t c = tree_new(T_CONTEXT_REF);
25✔
2854
      tree_set_ident(c, name);
25✔
2855
      tree_set_loc(c, CURRENT_LOC);
25✔
2856

2857
      tree_t ctx = resolve_name(nametab, CURRENT_LOC, name);
25✔
2858
      if (ctx != NULL && tree_kind(ctx) == T_CONTEXT) {
25✔
2859
         insert_names_from_context(nametab, ctx);
23✔
2860
         tree_set_ref(c, ctx);
23✔
2861
      }
2862
      else if (ctx != NULL)
2✔
UNCOV
2863
         parse_error(CURRENT_LOC, "%s%s is not a context declaration",
×
2864
                     is_design_unit(ctx) ? "design unit " : "", istr(name));
2865

2866
      tree_add_context(unit, c);
25✔
2867
   } while (optional(tCOMMA));
25✔
2868

2869
   consume(tSEMI);
25✔
2870
}
25✔
2871

2872
static void p_context_item(tree_t unit)
3,939✔
2873
{
2874
   // library_clause | use_clause | 2008: context_reference
2875

2876
   BEGIN("context item");
7,878✔
2877

2878
   switch (peek()) {
3,939✔
2879
   case tLIBRARY:
1,063✔
2880
      p_library_clause(unit);
1,063✔
2881
      break;
1,063✔
2882

2883
   case tUSE:
2,851✔
2884
      p_use_clause(unit, tree_add_context);
2,851✔
2885
      break;
2,851✔
2886

2887
   case tCONTEXT:
25✔
2888
      p_context_reference(unit);
25✔
2889
      break;
25✔
2890

UNCOV
2891
   default:
×
UNCOV
2892
      expect(tLIBRARY, tUSE, tCONTEXT);
×
2893
   }
2894
}
3,939✔
2895

2896
static void p_context_clause(tree_t unit)
12,325✔
2897
{
2898
   // { context_item }
2899

2900
   BEGIN("context clause");
24,650✔
2901

2902
   const int start_errors = error_count();
12,325✔
2903

2904
   while (scan(tLIBRARY, tUSE, tCONTEXT)) {
16,264✔
2905
      if (peek() == tCONTEXT && peek_nth(3) == tIS)
3,958✔
2906
         break;
2907
      else
2908
         p_context_item(unit);
3,939✔
2909
   }
2910

2911
   // Suppress further errors if there are errors in the context
2912
   if (error_count() > start_errors)
12,325✔
2913
      suppress_errors(nametab);
12✔
2914
}
12,325✔
2915

2916
static port_mode_t p_mode(void)
13,524✔
2917
{
2918
   // in | out | inout | buffer | linkage
2919

2920
   switch (one_of(tIN, tOUT, tINOUT, tBUFFER, tLINKAGE)) {
13,524✔
2921
   case tIN:
2922
      return PORT_IN;
2923
   case tOUT:
2924
      return PORT_OUT;
2925
   case tINOUT:
2926
      return PORT_INOUT;
2927
   case tBUFFER:
2928
      return PORT_BUFFER;
2929
   case tLINKAGE:
2930
      return PORT_LINKAGE;
2931
   default:
2932
      return PORT_INVALID;
2933
   }
2934
}
2935

2936
static tree_t p_range(tree_t left)
17,653✔
2937
{
2938
   // attribute_name | simple_expression direction simple_expression
2939

2940
   EXTEND("range");
17,653✔
2941

2942
   tree_t r = tree_new(T_RANGE);
17,653✔
2943
   tree_set_subkind(r, RANGE_ERROR);
17,653✔
2944

2945
   if (is_range_expr(left)) {
17,653✔
2946
      tree_set_subkind(r, RANGE_EXPR);
3,126✔
2947
      tree_set_value(r, left);
3,126✔
2948
      tree_set_loc(r, tree_loc(left));
3,126✔
2949
   }
2950
   else {
2951
      tree_set_left(r, left);
14,527✔
2952

2953
      switch (one_of(tTO, tDOWNTO)) {
14,527✔
2954
      case tTO:
7,327✔
2955
         tree_set_subkind(r, RANGE_TO);
7,327✔
2956
         tree_set_right(r, p_expression());
7,327✔
2957
         break;
7,327✔
2958

2959
      case tDOWNTO:
7,199✔
2960
         tree_set_subkind(r, RANGE_DOWNTO);
7,199✔
2961
         tree_set_right(r, p_expression());
7,199✔
2962
         break;
7,199✔
2963
      }
2964

2965
      tree_set_loc(r, CURRENT_LOC);
14,527✔
2966
   }
2967

2968
   return r;
17,653✔
2969
}
2970

2971
static tree_t p_range_constraint(type_t constraint)
697✔
2972
{
2973
   // range range
2974

2975
   BEGIN("range constraint");
697✔
2976

2977
   consume(tRANGE);
697✔
2978

2979
   tree_t t = tree_new(T_CONSTRAINT);
697✔
2980
   tree_set_subkind(t, C_RANGE);
697✔
2981

2982
   tree_t expr1 = p_expression();
697✔
2983

2984
   switch (peek()) {
697✔
2985
   case tTO:
682✔
2986
   case tDOWNTO:
2987
      {
2988
         tree_t r = p_range(expr1);
682✔
2989
         solve_types(nametab, r, constraint);
682✔
2990
         tree_add_range(t, r);
682✔
2991
      }
2992
      break;
682✔
2993
   default:
15✔
2994
      {
2995
         tree_t r = tree_new(T_RANGE);
15✔
2996
         tree_set_loc(r, tree_loc(expr1));
15✔
2997
         tree_set_subkind(r, RANGE_EXPR);
15✔
2998
         tree_set_value(r, expr1);
15✔
2999

3000
         solve_types(nametab, r, constraint);
15✔
3001

3002
         tree_add_range(t, r);
15✔
3003
      }
3004
   }
3005

3006
   tree_set_loc(t, CURRENT_LOC);
697✔
3007
   return t;
697✔
3008
}
3009

3010
static tree_t p_discrete_range(tree_t head)
16,978✔
3011
{
3012
   // subtype_indication | range
3013

3014
   BEGIN_WITH_HEAD("discrete range", head);
33,956✔
3015

3016
   tree_t expr1 = head ?: p_expression();
16,978✔
3017

3018
   switch (peek()) {
16,978✔
3019
   case tTO:
13,791✔
3020
   case tDOWNTO:
3021
   case tTICK:
3022
      return p_range(expr1);
13,791✔
3023

3024
   case tRANGE:
55✔
3025
      {
3026
         type_t constraint = solve_types(nametab, expr1, NULL);
55✔
3027

3028
         const bool is_type =
110✔
3029
            tree_kind(expr1) == T_REF
55✔
3030
            && tree_has_ref(expr1)
54✔
3031
            && is_type_decl(tree_ref(expr1));
108✔
3032

3033
         if (!is_type && !type_is_none(constraint)) {
55✔
3034
            parse_error(tree_loc(expr1), "expected type mark while parsing "
2✔
3035
                        "discrete range");
3036
            constraint = type_new(T_NONE);
2✔
3037
         }
3038

3039
         consume(tRANGE);
55✔
3040

3041
         tree_t left = p_expression();
55✔
3042
         tree_t r = p_range(left);
55✔
3043
         solve_types(nametab, r, constraint);
55✔
3044
         tree_set_type(r, constraint);
55✔
3045
         return r;
55✔
3046
      }
3047

3048
   default:
3,132✔
3049
      {
3050
         type_t type = solve_types(nametab, expr1, NULL);
3,132✔
3051

3052
         if (tree_kind(expr1) == T_ATTR_REF)
3,132✔
3053
            return p_range(expr1);   // Special attributes such as 'RANGE
2,227✔
3054
         else if (tree_kind(expr1) == T_REF && tree_has_ref(expr1)) {
905✔
3055
            // A type name T may stand in for a discrete range
3056
            // equivalent to T'RANGE
3057
            if (aliased_type_decl(tree_ref(expr1)) != NULL) {
900✔
3058
               tree_t tmp = tree_new(T_ATTR_REF);
898✔
3059
               tree_set_name(tmp, expr1);
898✔
3060
               tree_set_ident(tmp, ident_new("RANGE"));
898✔
3061
               tree_set_loc(tmp, tree_loc(expr1));
898✔
3062
               tree_set_subkind(tmp, ATTR_RANGE);
898✔
3063

3064
               return p_range(tmp);
898✔
3065
            }
3066
            else
3067
               parse_error(CURRENT_LOC, "name %s in discrete range does not "
2✔
3068
                           "refer to a type", istr(tree_ident(expr1)));
3069
         }
3070
         else if (!type_is_none(type))
5✔
3071
            parse_error(CURRENT_LOC, "expecting a discrete range");
2✔
3072

3073
         // Not a valid discrete range
3074
         tree_t r = tree_new(T_RANGE);
7✔
3075
         tree_set_loc(r, CURRENT_LOC);
7✔
3076
         tree_set_subkind(r, RANGE_ERROR);
7✔
3077
         return r;
7✔
3078
      }
3079
   }
3080
}
3081

3082
static tree_t p_slice_name(tree_t prefix, tree_t head)
2,158✔
3083
{
3084
   // prefix ( discrete_range )
3085

3086
   EXTEND("slice name");
2,158✔
3087

3088
   type_t type = prefix_type(prefix, NULL);
2,158✔
3089

3090
   if (type != NULL && type_is_access(type)) {
2,158✔
3091
      prefix = implicit_dereference(prefix);
34✔
3092
      type   = tree_type(prefix);
34✔
3093
   }
3094

3095
   tree_t t = tree_new(T_ARRAY_SLICE);
2,158✔
3096
   tree_set_value(t, prefix);
2,158✔
3097

3098
   type_t index_type = NULL;
2,158✔
3099
   if (type != NULL && type_is_array(type))
2,158✔
3100
      index_type = index_type_of(type, 0);
2,141✔
3101

3102
   tree_t r = p_discrete_range(head);
2,158✔
3103
   solve_types(nametab, r, index_type);
2,158✔
3104
   convert_universal_bounds(r);
2,158✔
3105

3106
   tree_add_range(t, r);
2,158✔
3107
   consume(tRPAREN);
2,158✔
3108

3109
   tree_set_loc(t, CURRENT_LOC);
2,158✔
3110
   return t;
2,158✔
3111
}
3112

3113
static tree_t p_formal_part(type_t *signature)
6,827✔
3114
{
3115
   // formal_designator
3116
   //   | name ( formal_designator )
3117
   //   | type_mark ( formal_designator )
3118

3119
   BEGIN("formal part");
13,654✔
3120

3121
   tree_t name = p_name(0);
6,827✔
3122

3123
   switch (tree_kind(name)) {
6,827✔
3124
   case T_RECORD_REF:
3125
   case T_ARRAY_REF:
3126
   case T_ARRAY_SLICE:
3127
   case T_TYPE_CONV:
3128
      break;
3129

3130
   case T_FCALL:
78✔
3131
      if (tree_params(name) == 1)
78✔
3132
         tree_set_flag(name, TREE_F_CONVERSION);
77✔
3133
      break;
3134

3135
   case T_REF:
6,100✔
3136
      // 2019 allows signature for generic formal designator
3137
      if (peek() == tLSQUARE) {
6,100✔
3138
         require_std(STD_19, "signature in generic formal designator");
5✔
3139
         *signature = p_signature();
5✔
3140
         tree_set_loc(name, CURRENT_LOC);
5✔
3141
      }
3142
      break;
3143

3144
   default:
1✔
3145
      parse_error(CURRENT_LOC, "illegal formal designator");
1✔
3146
      return error_expr();
1✔
3147
   }
3148

3149
   return name;
3150
}
3151

3152
static tree_t p_actual_part(class_t class, formal_kind_t kind)
50,536✔
3153
{
3154
   // actual_designator
3155
   //   | name ( actual_designator )
3156
   //   | type_mark ( actual_designator )
3157

3158
   BEGIN("actual part");
101,072✔
3159

3160
   if (optional(tOPEN)) {
50,536✔
3161
      tree_t t = tree_new(T_OPEN);
87✔
3162
      tree_set_loc(t, CURRENT_LOC);
87✔
3163
      return t;
87✔
3164
   }
3165

3166
   if (class == C_FUNCTION || class == C_PROCEDURE || class == C_PACKAGE)
50,449✔
3167
      return p_name(N_SUBPROGRAM);
142✔
3168
   else if (class == C_TYPE) {
50,307✔
3169
      type_t type = p_subtype_indication();
294✔
3170

3171
      tree_t ref = tree_new(T_TYPE_REF);
294✔
3172
      tree_set_ident(ref, type_ident(type));
294✔
3173
      tree_set_type(ref, type);
294✔
3174
      tree_set_loc(ref, CURRENT_LOC);
294✔
3175
      return ref;
294✔
3176
   }
3177

3178
   if (optional(tINERTIAL)) {
50,013✔
3179
      require_std(STD_08, "inertial in actual designator");
16✔
3180

3181
      tree_t expr = p_expression();
16✔
3182

3183
      if (kind == F_PORT_MAP) {
16✔
3184
         tree_t w = tree_new(T_INERTIAL);
14✔
3185
         tree_set_loc(w, CURRENT_LOC);
14✔
3186
         tree_set_value(w, expr);
14✔
3187

3188
         return w;
14✔
3189
      }
3190
      else {
3191
         parse_error(CURRENT_LOC, "the reserved word INERTIAL can only be "
2✔
3192
                     "used in port map association elements");
3193
         return expr;
2✔
3194
      }
3195
   }
3196

3197
   // If the actual part takes either the second or third form above then the
3198
   // argument to the function call is the actual designator but only if the
3199
   // call is to a named function rather than an operator.
3200
   // This is important for identifying conversion functions later.
3201
   const token_t next = peek();
49,997✔
3202
   const bool had_name = (next == tID || next == tSTRING);
49,997✔
3203

3204
   tree_t designator = p_expression();
49,997✔
3205

3206
   const bool could_be_conversion =
99,994✔
3207
      had_name
3208
      && tree_kind(designator) == T_FCALL
44,017✔
3209
      && tree_params(designator) == 1;
52,531✔
3210

3211
   if (could_be_conversion)
49,997✔
3212
      tree_set_flag(designator, TREE_F_CONVERSION);
833✔
3213

3214
   return designator;
3215
}
3216

3217
static void p_association_element(tree_t map, int pos, tree_t unit,
50,536✔
3218
                                  formal_kind_t kind)
3219
{
3220
   // [ formal_part => ] actual_part
3221

3222
   BEGIN("association element");
101,072✔
3223

3224
   tree_t p = tree_new(T_PARAM);
50,536✔
3225

3226
   const look_params_t lookp = {
50,536✔
3227
      .look     = { tASSOC },
3228
      .stop     = { tCOMMA, tRPAREN },
3229
      .abort    = tSEMI,
3230
      .nest_in  = tLPAREN,
3231
      .nest_out = tRPAREN,
3232
      .depth    = 0
3233
   };
3234

3235
   class_t class = C_DEFAULT;
50,536✔
3236
   type_t type = NULL;
50,536✔
3237
   if (look_for(&lookp)) {
50,536✔
3238
      tree_set_subkind(p, P_NAMED);
6,827✔
3239

3240
      push_scope_for_formals(nametab, kind, unit);
6,827✔
3241

3242
      type_t signature = NULL;
6,827✔
3243
      tree_t name = p_formal_part(&signature);
6,827✔
3244

3245
      tree_t ref = name_to_ref(name);
6,827✔
3246
      if (ref != NULL && tree_has_ref(ref)) {
6,827✔
3247
         tree_t decl = tree_ref(ref);
5,378✔
3248
         const tree_kind_t kind = tree_kind(decl);
5,378✔
3249
         if (kind == T_PORT_DECL || kind == T_PARAM_DECL
5,378✔
3250
             || kind == T_GENERIC_DECL)
1,672✔
3251
            class = tree_class(decl);
4,790✔
3252
      }
3253

3254
      if (signature != NULL && kind != F_GENERIC_MAP) {
6,827✔
3255
         parse_error(tree_loc(name), "a signature is only allowed in a "
1✔
3256
                     "generic formal designator");
3257
         signature = NULL;
1✔
3258
      }
3259

3260
      if (class != C_PACKAGE && (kind == F_GENERIC_MAP || kind == F_PORT_MAP))
6,827✔
3261
         type = solve_types(nametab, name, signature);
3,312✔
3262

3263
      if (kind == F_PORT_MAP && tree_kind(name) == T_FCALL)
6,827✔
3264
         name = fcall_to_conv_func(name);
76✔
3265

3266
      tree_set_name(p, name);
6,827✔
3267

3268
      pop_scope(nametab);
6,827✔
3269

3270
      consume(tASSOC);
6,827✔
3271
   }
3272
   else {
3273
      tree_set_subkind(p, P_POS);
43,709✔
3274
      tree_set_pos(p, pos);
43,709✔
3275

3276
      tree_t formal = NULL;
43,709✔
3277
      switch (kind) {
43,709✔
3278
      case F_GENERIC_MAP:
560✔
3279
         if (unit != NULL && pos < tree_generics(unit))
560✔
3280
            formal = tree_generic(unit, pos);
551✔
3281
         break;
3282
      case F_PORT_MAP:
1,387✔
3283
         if (unit != NULL && pos < tree_ports(unit))
1,387✔
3284
            formal = tree_port(unit, pos);
1,378✔
3285
         break;
3286
      default:
3287
         break;
3288
      }
3289

3290
      if (formal != NULL && (class = tree_class(formal)) != C_PACKAGE)
1,929✔
3291
         type = tree_type(formal);
1,899✔
3292
   }
3293

3294
   tree_t value = p_actual_part(class, kind);
50,536✔
3295

3296
   if (kind == F_PORT_MAP)
50,536✔
3297
      solve_types(nametab, value, type);
3,655✔
3298
   else if (kind == F_GENERIC_MAP && class != C_PACKAGE) {
46,881✔
3299
      type_t value_type = solve_types(nametab, value, type);
1,574✔
3300

3301
      // Make the mapped type available immediately as it may be used in
3302
      // later actuals
3303
      if (class == C_TYPE && type != NULL)
1,574✔
3304
         map_generic_type(nametab, type, value_type);
294✔
3305
   }
3306

3307
   if (kind == F_PORT_MAP && tree_kind(value) == T_FCALL)
50,536✔
3308
      value = fcall_to_conv_func(value);
138✔
3309

3310
   tree_set_value(p, value);
50,536✔
3311
   tree_set_loc(p, CURRENT_LOC);
50,536✔
3312

3313
   switch (kind) {
50,536✔
3314
   case F_GENERIC_MAP:
1,654✔
3315
      tree_add_genmap(map, p);
1,654✔
3316
      break;
1,654✔
3317
   case F_PORT_MAP:
48,882✔
3318
   case F_SUBPROGRAM:
3319
      tree_add_param(map, p);
48,882✔
3320
      break;
48,882✔
UNCOV
3321
   default:
×
3322
      fatal_trace("unexpected formal kind in p_association_element");
3323
   }
3324
}
50,536✔
3325

3326
static void p_association_list(tree_t map, tree_t unit, formal_kind_t kind)
23,645✔
3327
{
3328
   // association_element { , association_element }
3329

3330
   BEGIN("association list");
47,290✔
3331

3332
   int pos = 0;
23,645✔
3333
   do {
50,536✔
3334
      p_association_element(map, pos++, unit, kind);
50,536✔
3335
   } while (optional(tCOMMA));
50,536✔
3336
}
23,645✔
3337

3338
static void p_actual_parameter_part(tree_t call)
21,281✔
3339
{
3340
   // association_list
3341

3342
   BEGIN("actual parameter part");
42,562✔
3343

3344
   p_association_list(call, call, F_SUBPROGRAM);
21,281✔
3345
}
21,281✔
3346

3347
static void p_parameter_map_aspect(tree_t call)
2✔
3348
{
3349
   // [ parameter map ] ( parameter_association_list )
3350

3351
   BEGIN("actual parameter part");
4✔
3352

3353
   consume(tPARAMETER);
2✔
3354
   consume(tMAP);
2✔
3355

3356
   require_std(STD_19, "parameter map aspect");
2✔
3357

3358
   consume(tLPAREN);
2✔
3359

3360
   p_association_list(call, call, F_SUBPROGRAM);
2✔
3361

3362
   consume(tRPAREN);
2✔
3363
}
2✔
3364

3365
static tree_t p_function_call(ident_t id, tree_t prefix)
14,400✔
3366
{
3367
   // name [ ( actual_parameter_part ) ]
3368
   // 2019: name [ generic_map_aspect] [ parameter_map_aspect ]
3369

3370
   EXTEND("function call");
28,800✔
3371

3372
   tree_t call;
14,400✔
3373
   if (prefix != NULL) {
14,400✔
3374
      call = tree_new(T_PROT_FCALL);
774✔
3375
      tree_set_ident(call, id);
774✔
3376
      tree_set_name(call, prefix);
774✔
3377
   }
3378
   else {
3379
      call = tree_new(T_FCALL);
13,626✔
3380
      tree_set_ident(call, id);
13,626✔
3381
   }
3382

3383
   if (peek() == tGENERIC) {
14,400✔
3384
      tree_t inst = tree_new(T_FUNC_INST);
17✔
3385
      tree_set_ident(inst, ident_prefix(id, ident_uniq("inst"), '$'));
17✔
3386

3387
      tree_t decl = resolve_uninstantiated_subprogram(nametab, CURRENT_LOC,
17✔
3388
                                                      id, NULL);
3389
      if (decl != NULL) {
17✔
3390
         tree_t body = find_generic_subprogram_body(inst, decl);
17✔
3391
         instantiate_subprogram(inst, decl, body);
17✔
3392
      }
3393
      else {
3394
         // Create a dummy subprogram type to avoid later errors
UNCOV
3395
         type_t type = type_new(T_SIGNATURE);
×
UNCOV
3396
         type_set_ident(type, id);
×
UNCOV
3397
         type_set_result(type, type_new(T_NONE));
×
3398

3399
         tree_set_type(inst, type);
×
3400
      }
3401

3402
      p_generic_map_aspect(inst, inst);
17✔
3403

3404
      require_std(STD_19, "generic map on function call");
17✔
3405

3406
      tree_set_loc(inst, CURRENT_LOC);
17✔
3407
      sem_check(inst, nametab);
17✔
3408

3409
      hash_t *map = get_generic_map(nametab);
17✔
3410
      if (map != NULL)
17✔
3411
         instance_fixup(inst, map);
17✔
3412

3413
      tree_set_ref(call, inst);
17✔
3414

3415
      mangle_func(nametab, inst);
17✔
3416

3417
      tree_t container = find_enclosing(nametab, S_DECLARATIVE_REGION);
17✔
3418
      tree_add_decl(container, inst);
17✔
3419
   }
3420

3421
   if (peek() == tPARAMETER)
14,400✔
3422
      p_parameter_map_aspect(call);
1✔
3423
   else if (optional(tLPAREN)) {
14,399✔
3424
      p_actual_parameter_part(call);
13,739✔
3425
      consume(tRPAREN);
13,739✔
3426
   }
3427

3428
   tree_set_loc(call, CURRENT_LOC);
14,400✔
3429
   return could_be_slice_name(call);
14,400✔
3430
}
3431

3432
static tree_t p_attribute_name(tree_t prefix)
22,736✔
3433
{
3434
   // prefix [ signature ] ' attribute_designator [ ( expression ) ]
3435

3436
   EXTEND("attribute name");
22,736✔
3437

3438
   type_t signature = NULL;
22,736✔
3439
   if (peek() == tLSQUARE)
22,736✔
3440
      signature = p_signature();
14✔
3441

3442
   consume(tTICK);
22,736✔
3443

3444
   attr_kind_t kind;
22,736✔
3445
   ident_t id;
22,736✔
3446
   switch (peek()) {
22,736✔
3447
   case tRANGE:
2,211✔
3448
      consume(tRANGE);
2,211✔
3449
      id = ident_new("RANGE");
2,211✔
3450
      kind = ATTR_RANGE;
2,211✔
3451
      break;
2,211✔
3452
   case tREVRANGE:
47✔
3453
      consume(tREVRANGE);
47✔
3454
      id = ident_new("REVERSE_RANGE");
47✔
3455
      kind = ATTR_REVERSE_RANGE;
47✔
3456
      break;
47✔
3457
   case tSUBTYPE:
123✔
3458
      consume(tSUBTYPE);
123✔
3459
      require_std(STD_08, "subtype attribute");
123✔
3460
      id = ident_new("SUBTYPE");
123✔
3461
      kind = ATTR_SUBTYPE;
123✔
3462
      break;
123✔
3463
   case tID:
20,354✔
3464
      id = p_identifier();
20,354✔
3465
      kind = parse_predefined_attr(id);
20,354✔
3466
      break;
20,354✔
3467
   default:
1✔
3468
      one_of(tRANGE, tREVRANGE, tID, tSUBTYPE);
1✔
3469
      kind = ATTR_USER;
1✔
3470
      id = error_marker();
1✔
3471
   }
3472

3473
   type_t type = prefix_type(prefix, signature);
22,736✔
3474

3475
   if (signature != NULL) {
22,736✔
3476
      bool valid_signature = false;
14✔
3477
      if (type == NULL)
14✔
3478
         valid_signature = false;
3479
      else if (type_is_subprogram(type))
13✔
3480
         valid_signature = true;
3481
      else if (class_of(prefix) == C_LITERAL && type_is_enum(type))
5✔
3482
         valid_signature = true;
3483
      else if (type_is_none(type))
1✔
3484
         valid_signature = true;   // Prevent cascading errors
3485

3486
      if (!valid_signature)
3487
         parse_error(CURRENT_LOC, "prefix of attribute name with signature "
1✔
3488
                     "does not denote a subprogram or enumeration literal");
3489
   }
3490

3491
   if (type != NULL && type_kind(type) == T_INCOMPLETE) {
22,736✔
3492
      type = resolve_type(nametab, type);
2✔
3493
      tree_set_type(prefix, type);
2✔
3494
   }
3495

3496
   const bool deref_prefix =
45,472✔
3497
      !is_type_attribute(kind) && kind != ATTR_REFLECT
22,736✔
3498
      && type != NULL && type_is_access(type);
22,736✔
3499

3500
   if (deref_prefix) {
22,736✔
3501
      prefix = implicit_dereference(prefix);
168✔
3502
      type   = tree_type(prefix);
168✔
3503
   }
3504

3505
   tree_t t = tree_new(T_ATTR_REF);
22,736✔
3506
   tree_set_name(t, prefix);
22,736✔
3507

3508
   tree_set_ident(t, id);
22,736✔
3509
   tree_set_subkind(t, kind);
22,736✔
3510
   tree_set_loc(t, CURRENT_LOC);
22,736✔
3511

3512
   if (attribute_has_param(kind) && optional(tLPAREN)) {
22,736✔
3513
      add_param(t, p_expression(), P_POS, NULL);
2,987✔
3514
      consume(tRPAREN);
2,987✔
3515
      tree_set_loc(t, CURRENT_LOC);
2,987✔
3516
   }
3517

3518
   if (is_type_attribute(kind))
22,736✔
3519
      tree_set_type(t, apply_type_attribute(t));
199✔
3520
   else if (kind == ATTR_DELAYED || kind == ATTR_TRANSACTION
22,537✔
3521
            || kind == ATTR_STABLE || kind == ATTR_QUIET)
22,351✔
3522
      implicit_signal_attribute(t);
288✔
3523

3524
   return t;
22,736✔
3525
}
3526

3527
static tree_t p_selected_name(tree_t prefix, name_mask_t *mask)
11,885✔
3528
{
3529
   // prefix . suffix
3530

3531
   EXTEND("selected name");
23,770✔
3532

3533
   // If the prefix is a reference to a function then convert it to a
3534
   // call unless it matches the name of the enclosing subprogram
3535
   tree_kind_t prefix_kind = tree_kind(prefix);
11,885✔
3536
   if ((*mask & N_FUNC) && prefix_kind == T_REF) {
11,885✔
3537
      ident_t id = tree_ident(prefix);
17✔
3538
      tree_t sub = find_enclosing(nametab, S_SUBPROGRAM);
17✔
3539
      if (sub != NULL && tree_ident(sub) == id)
17✔
3540
         tree_set_ref(prefix, sub);
2✔
3541
      else {
3542
         prefix = p_function_call(id, NULL);
15✔
3543
         prefix_kind = T_FCALL;
15✔
3544
      }
3545
   }
3546
   else if (prefix_kind == T_PROT_REF) {
11,868✔
3547
      prefix = p_function_call(tree_ident(prefix), tree_value(prefix));
120✔
3548
      prefix_kind = T_FCALL;
120✔
3549
   }
3550

3551
   consume(tDOT);
11,885✔
3552
   *mask = 0;
11,885✔
3553

3554
   ident_t suffix = NULL;
11,885✔
3555
   switch (peek()) {
11,885✔
3556
   case tID:
10,841✔
3557
      suffix = p_identifier();
10,841✔
3558
      break;
10,841✔
3559

3560
   case tSTRING:
5✔
3561
      suffix = p_operator_symbol();
5✔
3562
      break;
5✔
3563

3564
   case tALL:
1,039✔
3565
      {
3566
         consume(tALL);
1,039✔
3567

3568
         tree_t all = tree_new(T_ALL);
1,039✔
3569
         tree_set_loc(all, CURRENT_LOC);
1,039✔
3570
         tree_set_value(all, prefix);
1,039✔
3571
         *mask |= N_OBJECT;
1,039✔
3572
         return all;
1,039✔
3573
      }
3574

UNCOV
3575
   default:
×
UNCOV
3576
      expect(tID, tSTRING, tALL);
×
UNCOV
3577
      *mask |= N_ERROR;
×
UNCOV
3578
      return prefix;
×
3579
   }
3580

3581
   if (prefix_kind == T_REF && tree_has_ref(prefix)) {
10,846✔
3582
      tree_t decl = tree_ref(prefix);
9,063✔
3583
      const tree_kind_t kind = tree_kind(decl);
9,063✔
3584
      if (kind == T_LIBRARY) {
9,063✔
3585
         ident_t unit_name = ident_prefix(tree_ident(prefix), suffix, '.');
316✔
3586
         tree_t unit = resolve_name(nametab, CURRENT_LOC, unit_name);
316✔
3587
         if (unit == NULL) {
316✔
3588
            tree_t dummy = tree_new(T_REF);
3✔
3589
            tree_set_ident(dummy, unit_name);
3✔
3590
            tree_set_type(dummy, type_new(T_NONE));
3✔
3591
            *mask |= N_ERROR;
3✔
3592
            return dummy;
3✔
3593
         }
3594
         else {
3595
            assert(is_design_unit(unit));
313✔
3596

3597
            tree_t ref = tree_new(T_REF);
313✔
3598
            tree_set_loc(ref, CURRENT_LOC);
313✔
3599
            tree_set_ident(ref, unit_name);
313✔
3600
            tree_set_ref(ref, unit);
313✔
3601
            return ref;
313✔
3602
         }
3603
      }
3604
      else if (kind == T_GENERIC_DECL && tree_class(decl) == C_PACKAGE)
8,747✔
3605
         return select_decl(tree_value(decl), suffix, mask);
76✔
3606
      else if (is_container(decl)) {
8,671✔
3607
         tree_t ref = select_decl(prefix, suffix, mask);
638✔
3608
         if (!tree_has_ref(ref))
638✔
3609
            return ref;   // Was error
3610

3611
         // LRM 08 section 8.3 rules for expanded names
3612
         tree_t du = find_enclosing(nametab, S_DESIGN_UNIT);
485✔
3613
         if (du == decl || (kind == T_ENTITY && primary_unit_of(du) == decl))
485✔
3614
            return ref;
2✔
3615
         else if (kind == T_PACKAGE && is_uninstantiated_package(decl))
483✔
3616
            parse_error(CURRENT_LOC, "cannot reference %s in uninstantiated "
1✔
3617
                        "package %s outside of the package itself",
3618
                        istr(suffix), istr(tree_ident(decl)));
3619
         else if (kind != T_PACKAGE && kind != T_PACK_INST
482✔
3620
                  && !is_enclosing(nametab, decl)) {
38✔
3621
            diag_t *d = diag_new(DIAG_ERROR, CURRENT_LOC);
2✔
3622
            diag_printf(d, "expanded name cannot reference %s in %s %s "
2✔
3623
                        "outside of the construct itself", istr(suffix),
3624
                        class_str(class_of(decl)), istr(tree_ident(decl)));
3625
            diag_lrm(d, STD_08, "8.3");
2✔
3626
            diag_emit(d);
2✔
3627
         }
3628

3629
         return ref;
483✔
3630
      }
3631
      else if (is_type_decl(decl)) {
8,033✔
3632
         diag_t *d = pedantic_diag(tree_loc(prefix));
4✔
3633
         if (d != NULL) {
4✔
3634
            diag_printf(d, "type mark cannot be the prefix of a selected name");
1✔
3635
            diag_emit(d);
1✔
3636
         }
3637

3638
         *mask |= N_TYPE;
4✔
3639
      }
3640
   }
3641

3642
   if (scope_formal_kind(nametab) == F_SUBPROGRAM) {
9,816✔
3643
      tree_t rref = tree_new(T_RECORD_REF);
41✔
3644
      tree_set_value(rref, prefix);
41✔
3645
      tree_set_ident(rref, suffix);
41✔
3646
      tree_set_loc(rref, CURRENT_LOC);
41✔
3647
      *mask |= N_OBJECT;
41✔
3648
      return rref;
41✔
3649
   }
3650

3651
   type_t type = solve_types(nametab, prefix, NULL);
9,775✔
3652

3653
   if (type_is_access(type)) {
9,775✔
3654
      prefix = implicit_dereference(prefix);
741✔
3655
      type   = tree_type(prefix);
741✔
3656
      prefix_kind = T_ALL;
741✔
3657
   }
3658

3659
   if (type_kind(type) == T_INCOMPLETE) {
9,775✔
3660
      type = resolve_type(nametab, type);
444✔
3661
      tree_set_type(prefix, type);
444✔
3662
   }
3663

3664
   if (type_is_record(type) || type_is_none(type)) {
9,775✔
3665
      tree_t rref = tree_new(T_RECORD_REF);
8,783✔
3666
      tree_set_value(rref, prefix);
8,783✔
3667
      tree_set_ident(rref, suffix);
8,783✔
3668
      tree_set_loc(rref, CURRENT_LOC);
8,783✔
3669
      *mask |= N_OBJECT;
8,783✔
3670
      return rref;
8,783✔
3671
   }
3672
   else if (type_is_protected(type)) {
992✔
3673
      tree_t pref = tree_new(T_PROT_REF);
990✔
3674
      tree_set_value(pref, prefix);
990✔
3675
      tree_set_ident(pref, suffix);
990✔
3676
      tree_set_loc(pref, CURRENT_LOC);
990✔
3677
      *mask |= N_SUBPROGRAM;
990✔
3678
      return pref;
990✔
3679
   }
3680
   else if (type_kind(type) == T_INCOMPLETE) {
2✔
UNCOV
3681
      parse_error(tree_loc(prefix), "object with incomplete type %s cannot be "
×
3682
                  "selected", type_pp(type));
UNCOV
3683
      *mask |= N_ERROR;
×
UNCOV
3684
      return prefix;
×
3685
   }
3686
   else if (prefix_kind == T_REF) {
2✔
3687
      parse_error(tree_loc(prefix), "object %s with type %s cannot be selected",
1✔
3688
                  istr(tree_ident(prefix)), type_pp(type));
3689
      tree_set_type(prefix, type_new(T_NONE));
1✔
3690
      *mask |= N_ERROR;
1✔
3691
      return prefix;
1✔
3692
   }
3693
   else {
3694
      parse_error(tree_loc(prefix), "object with type %s cannot be selected",
1✔
3695
                  type_pp(type));
3696
      tree_set_type(prefix, type_new(T_NONE));
1✔
3697
      *mask |= N_ERROR;
1✔
3698
      return prefix;
1✔
3699
   }
3700
}
3701

3702
static tree_t p_indexed_name(tree_t prefix, tree_t head)
14,515✔
3703
{
3704
   // prefix ( expression { , expression } )
3705

3706
   EXTEND("indexed name");
14,515✔
3707

3708
   type_t type = prefix_type(prefix, NULL);
14,515✔
3709

3710
   if (type != NULL && type_is_access(type)) {
14,515✔
3711
      prefix = implicit_dereference(prefix);
344✔
3712
      type   = tree_type(prefix);
344✔
3713
   }
3714

3715
   tree_t t = tree_new(T_ARRAY_REF);
14,515✔
3716
   tree_set_value(t, prefix);
14,515✔
3717

3718
   do {
14,515✔
3719
      tree_t index = head ?: p_expression();
16,160✔
3720
      head = NULL;
16,160✔
3721
      add_param(t, index, P_POS, NULL);
16,160✔
3722
   } while (optional(tCOMMA));
16,160✔
3723

3724
   consume(tRPAREN);
14,515✔
3725

3726
   tree_set_loc(t, CURRENT_LOC);
14,515✔
3727
   return t;
14,515✔
3728
}
3729

3730
static tree_t p_type_conversion(tree_t prefix)
2,818✔
3731
{
3732
   // type_conversion ::= type_mark ( expression )
3733

3734
   EXTEND("type conversion");
2,818✔
3735

3736
   consume(tLPAREN);
2,818✔
3737

3738
   type_t type = NULL;
2,818✔
3739
   if (tree_kind(prefix) == T_ATTR_REF)
2,818✔
3740
      type = tree_type(prefix);
9✔
3741
   else {
3742
      assert(tree_kind(prefix) == T_REF);
2,809✔
3743
      tree_t tdecl = resolve_name(nametab, CURRENT_LOC, tree_ident(prefix));
2,809✔
3744
      if (tdecl == NULL)
2,809✔
UNCOV
3745
         type = type_new(T_NONE);
×
3746
      else {
3747
         tdecl = aliased_type_decl(tdecl);
2,809✔
3748
         assert(tdecl);   // Call to this is guarded by N_TYPE mask
2,809✔
3749
         type = tree_type(tdecl);
2,809✔
3750
      }
3751
   }
3752

3753
   tree_t value = p_expression();
2,818✔
3754
   solve_types(nametab, value, NULL);
2,818✔
3755

3756
   tree_t conv = tree_new(T_TYPE_CONV);
2,818✔
3757
   tree_set_type(conv, type);
2,818✔
3758
   tree_set_value(conv, value);
2,818✔
3759

3760
   consume(tRPAREN);
2,818✔
3761

3762
   tree_set_loc(conv, CURRENT_LOC);
2,818✔
3763
   return conv;
2,818✔
3764
}
3765

3766
static void p_partial_pathname(tree_t name)
184✔
3767
{
3768
   // { pathname_element . } object_simple_name
3769

3770
   BEGIN("partial pathname");
368✔
3771

3772
   do {
415✔
3773
      tree_t pe = tree_new(T_PATH_ELT);
415✔
3774
      tree_set_ident(pe, p_identifier());
415✔
3775

3776
      if (optional(tLPAREN)) {
415✔
3777
         tree_t expr = p_expression();
30✔
3778
         solve_types(nametab, expr, NULL);
30✔
3779

3780
         tree_set_subkind(pe, PE_GENERATE);
30✔
3781
         tree_set_value(pe, expr);
30✔
3782
         consume(tRPAREN);
30✔
3783
         tree_set_loc(pe, CURRENT_LOC);
30✔
3784
      }
3785
      else {
3786
         tree_set_subkind(pe, PE_SIMPLE);
385✔
3787
         tree_set_loc(pe, &last_loc);
385✔
3788
      }
3789

3790
      tree_add_part(name, pe);
415✔
3791
   } while (optional(tDOT));
415✔
3792
}
184✔
3793

3794
static void p_package_pathname(tree_t name)
7✔
3795
{
3796
   // @ library_logical_name . package_simple_name . { package_simple_name . }
3797
   //       object_simple_name
3798

3799
   BEGIN("package pathname");
14✔
3800

3801
   consume(tAT);
7✔
3802

3803
   tree_t pe = tree_new(T_PATH_ELT);
7✔
3804
   tree_set_subkind(pe, PE_LIBRARY);
7✔
3805
   tree_set_ident(pe, p_identifier());
7✔
3806
   tree_set_loc(pe, CURRENT_LOC);
7✔
3807

3808
   tree_add_part(name, pe);
7✔
3809

3810
   consume(tDOT);
7✔
3811

3812
   p_partial_pathname(name);
7✔
3813
}
7✔
3814

3815
static void p_absolute_pathname(tree_t name)
43✔
3816
{
3817
   // . partial_pathname
3818

3819
   BEGIN("absolute pathname");
86✔
3820

3821
   consume(tDOT);
43✔
3822

3823
   tree_t pe = tree_new(T_PATH_ELT);
43✔
3824
   tree_set_subkind(pe, PE_ABSOLUTE);
43✔
3825
   tree_set_loc(pe, CURRENT_LOC);
43✔
3826

3827
   tree_add_part(name, pe);
43✔
3828

3829
   p_partial_pathname(name);
43✔
3830
}
43✔
3831

3832
static void p_relative_pathname(tree_t name)
134✔
3833
{
3834
   // { ^ . } partial_pathname
3835

3836
   BEGIN("relative pathname");
268✔
3837

3838
   tree_t pe = tree_new(T_PATH_ELT);
134✔
3839
   tree_set_subkind(pe, PE_RELATIVE);
134✔
3840

3841
   tree_add_part(name, pe);
134✔
3842

3843
   while (peek() == tCARET) {
185✔
3844
      consume(tCARET);
51✔
3845
      consume(tDOT);
51✔
3846

3847
      tree_t pe = tree_new(T_PATH_ELT);
51✔
3848
      tree_set_loc(pe, CURRENT_LOC);
51✔
3849
      tree_set_subkind(pe, PE_CARET);
51✔
3850

3851
      tree_add_part(name, pe);
51✔
3852
   }
3853

3854
   p_partial_pathname(name);
134✔
3855
}
134✔
3856

3857
static void p_external_pathname(tree_t name)
184✔
3858
{
3859
   // package_pathname | absolute_pathname | relative_pathname
3860

3861
   BEGIN("external pathname");
368✔
3862

3863
   switch (peek()) {
184✔
3864
   case tDOT:
43✔
3865
      p_absolute_pathname(name);
43✔
3866
      break;
43✔
3867
   case tCARET:
134✔
3868
   case tID:
3869
      p_relative_pathname(name);
134✔
3870
      break;
134✔
3871
   case tAT:
7✔
3872
      p_package_pathname(name);
7✔
3873
      break;
7✔
UNCOV
3874
   default:
×
UNCOV
3875
      one_of(tDOT, tCARET, tID, tAT);
×
3876
   }
3877
}
184✔
3878

3879
static tree_t p_external_name(void)
184✔
3880
{
3881
   // << constant external_pathname : subtype_indication >>
3882
   //   | << signal external_pathname : subtype_indication >>
3883
   //   | << variable external_pathname : subtype_indication >>
3884

3885
   BEGIN("external name");
184✔
3886

3887
   consume(tLTLT);
184✔
3888

3889
   require_std(STD_08, "external names");
184✔
3890

3891
   tree_t t = tree_new(T_EXTERNAL_NAME);
184✔
3892

3893
   switch (one_of(tCONSTANT, tSIGNAL, tVARIABLE)) {
184✔
3894
   case tSIGNAL:   tree_set_class(t, C_SIGNAL); break;
141✔
3895
   case tCONSTANT: tree_set_class(t, C_CONSTANT); break;
37✔
3896
   case tVARIABLE: tree_set_class(t, C_VARIABLE); break;
6✔
3897
   }
3898

3899
   p_external_pathname(t);
184✔
3900

3901
   consume(tCOLON);
184✔
3902

3903
   tree_set_type(t, p_subtype_indication());
184✔
3904

3905
   consume(tGTGT);
184✔
3906

3907
   tree_set_global_flags(t, TREE_GF_EXTERNAL_NAME);
184✔
3908

3909
   tree_set_loc(t, CURRENT_LOC);
184✔
3910
   return t;
184✔
3911
}
3912

3913
static tree_t p_name(name_mask_t stop_mask)
311,175✔
3914
{
3915
   // simple_name | operator_symbol | selected_name | indexed_name
3916
   //   | slice_name | attribute_name | 2008: external_name
3917

3918
   BEGIN("name");
622,350✔
3919

3920
   ident_t id = NULL;
311,175✔
3921
   switch (peek()) {
311,175✔
3922
   case tSTRING:
99✔
3923
      id = p_operator_symbol();
99✔
3924
      break;
99✔
3925

3926
   case tID:
310,886✔
3927
      id = p_identifier();
310,886✔
3928
      break;
310,886✔
3929

3930
   case tLTLT:
184✔
3931
      return p_external_name();
184✔
3932

3933
   default:
6✔
3934
      {
3935
         expect(tSTRING, tID);
6✔
3936

3937
         tree_t dummy = tree_new(T_REF);
6✔
3938
         tree_set_loc(dummy, CURRENT_LOC);
6✔
3939
         tree_set_ident(dummy, error_marker());
6✔
3940
         tree_set_type(dummy, type_new(T_NONE));
6✔
3941
         return dummy;
6✔
3942
      }
3943
   }
3944

3945
   tree_t decl = NULL;
310,985✔
3946
   name_mask_t mask = query_name(nametab, id, &decl);
310,985✔
3947

3948
   tree_t prefix = tree_new(T_REF);
310,985✔
3949
   tree_set_ident(prefix, id);
310,985✔
3950
   tree_set_loc(prefix, CURRENT_LOC);
310,985✔
3951
   tree_set_ref(prefix, decl);
310,985✔
3952

3953
   for (;;) {
382,534✔
3954
      switch (peek()) {
382,534✔
3955
      case tLPAREN:
3956
         break;
50,728✔
3957

3958
      case tDOT:
11,885✔
3959
         prefix = p_selected_name(prefix, &mask);
11,885✔
3960
         continue;
11,885✔
3961

3962
      case tTICK:
26,686✔
3963
         if (peek_nth(2) == tLPAREN) {
26,686✔
3964
            if (mask & stop_mask)
3,964✔
3965
               return prefix;
253✔
3966
            else {
3967
               prefix = p_qualified_expression(prefix);
3,711✔
3968
               mask = N_OBJECT;
3,711✔
3969
            }
3970
         }
3971
         else {
3972
            prefix = p_attribute_name(prefix);
22,722✔
3973
            mask = is_type_attribute(tree_subkind(prefix)) ? N_TYPE : N_OBJECT;
45,245✔
3974
         }
3975
         continue;
26,433✔
3976

3977
      case tPARAMETER:
102✔
3978
      case tGENERIC:
3979
         if ((mask & stop_mask) || !(mask & N_SUBPROGRAM))
102✔
3980
            return prefix;
85✔
3981
         else {
3982
            prefix = p_function_call(tree_ident(prefix), NULL);
17✔
3983
            mask = N_OBJECT;
17✔
3984
            continue;
17✔
3985
         }
3986

3987
      default:
3988
         return prefix;
3989
      }
3990

3991
      // Prefix could either be an array to be indexed or sliced, a
3992
      // subprogram to be called, or a type conversion.
3993

3994
      if (mask & stop_mask)
50,728✔
3995
         return prefix;
17,510✔
3996
      else if (!(mask & N_FUNC) && (stop_mask == N_TYPE))
33,218✔
3997
         return prefix;   // Better error messages for bad type declaration
4✔
3998

3999
      if (!(mask & N_FUNC) && scope_formal_kind(nametab) == F_SUBPROGRAM) {
33,214✔
4000
         // Assume that A in F(A(N) => ...) is a parameter name
4001
         mask |= N_OBJECT;
18✔
4002
      }
4003

4004
      const tree_kind_t prefix_kind = tree_kind(prefix);
33,214✔
4005

4006
      if (mask & N_TYPE) {
33,214✔
4007
         // Type conversion
4008
         prefix = p_type_conversion(prefix);
2,818✔
4009
         mask = N_OBJECT;
2,818✔
4010
         continue;
2,818✔
4011
      }
4012
      else if (!(mask & N_OBJECT) && prefix_kind == T_REF) {
30,396✔
4013
         // Function call
4014
         prefix = p_function_call(tree_ident(prefix), NULL);
13,594✔
4015
         mask = N_OBJECT;
13,594✔
4016
         continue;
13,594✔
4017
      }
4018
      else if (!(mask & N_OBJECT) && prefix_kind == T_PROT_REF) {
16,802✔
4019
         // Protected function call
4020
         prefix = p_function_call(tree_ident(prefix), tree_value(prefix));
129✔
4021
         mask = N_OBJECT;
129✔
4022
         continue;
129✔
4023
      }
4024

4025
      // Must be a slice or index name: we have to parse up to the first
4026
      // expression to know which
4027

4028
      consume(tLPAREN);
16,673✔
4029

4030
      tree_t head = p_expression();
16,673✔
4031

4032
      if (scan(tDOWNTO, tTO) || is_range_expr(head))
16,673✔
4033
         prefix = p_slice_name(prefix, head);
2,158✔
4034
      else
4035
         prefix = p_indexed_name(prefix, head);
14,515✔
4036
   }
4037
}
4038

4039
static type_t p_type_mark(void)
16,444✔
4040
{
4041
   // name
4042

4043
   BEGIN("type mark");
32,888✔
4044

4045
   tree_t name = p_name(N_TYPE);
16,444✔
4046
   return name_to_type_mark(name);
16,444✔
4047
}
4048

4049
static tree_t p_index_constraint(type_t base)
10,398✔
4050
{
4051
   // ( discrete_range { , discrete_range } )
4052

4053
   BEGIN("index constraint");
10,398✔
4054

4055
   consume(tLPAREN);
10,398✔
4056

4057
   int n = 0;
10,398✔
4058
   tree_t t = tree_new(T_CONSTRAINT);
10,398✔
4059
   tree_set_subkind(t, C_INDEX);
10,398✔
4060
   do {
10,561✔
4061
      type_t index_type = base ? index_type_of(base, n++) : NULL;
10,561✔
4062
      tree_t r = p_discrete_range(NULL);
10,561✔
4063
      solve_types(nametab, r, index_type);
10,561✔
4064
      tree_add_range(t, r);
10,561✔
4065
   } while (optional(tCOMMA));
10,561✔
4066

4067
   consume(tRPAREN);
10,398✔
4068

4069
   tree_set_loc(t, CURRENT_LOC);
10,398✔
4070
   return t;
10,398✔
4071
}
4072

4073
static type_t p_element_constraint(type_t base)
782✔
4074
{
4075
   // array_constraint | record_constraint
4076

4077
   BEGIN("element constraint");
782✔
4078

4079
   type_t sub = type_new(T_SUBTYPE);
782✔
4080
   if (is_anonymous_subtype(base)) {
782✔
4081
      type_set_base(sub, type_base(base));
16✔
4082
      if (type_has_constraint(base))
16✔
4083
         type_set_constraint(sub, type_constraint(base));
11✔
4084
      if (type_has_elem(base))
16✔
4085
         type_set_elem(sub, type_elem(base));
5✔
4086
   }
4087
   else
4088
      type_set_base(sub, base);
766✔
4089

4090
   if (type_is_record(base))
782✔
4091
      type_set_constraint(sub, p_record_constraint(sub));
104✔
4092
   else
4093
      p_array_constraint(sub, base);
678✔
4094

4095
   return sub;
782✔
4096
}
4097

4098
static void p_array_constraint(type_t type, type_t base)
5,246✔
4099
{
4100
   // index_constraint [ array_element_constraint ]
4101
   //   | ( open ) [ array_element_constraint ]
4102

4103
   BEGIN("array constraint");
5,480✔
4104

4105
   if (peek_nth(2) == tOPEN) {
5,246✔
4106
      consume(tLPAREN);
56✔
4107
      consume(tOPEN);
56✔
4108
      consume(tRPAREN);
56✔
4109

4110
      if (!type_is_array(type) && !type_is_none(type))
56✔
4111
         parse_error(CURRENT_LOC, "open array constraint cannot be used "
1✔
4112
                     "with non-array type %s", type_pp(type));
4113
   }
4114
   else {
4115
      tree_t c = p_index_constraint(base);
5,190✔
4116

4117
      if (type_has_constraint(type)) {
5,190✔
4118
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(c));
3✔
4119
         diag_printf(d, "array element is already constrained");
3✔
4120
         diag_hint(d, tree_loc(type_constraint(type)),
3✔
4121
                   "location of existing constraint");
4122
         diag_emit(d);
3✔
4123
      }
4124
      else
4125
         type_set_constraint(type, c);
5,187✔
4126
   }
4127

4128
   if (peek() != tLPAREN)
5,246✔
4129
      return;
5,012✔
4130

4131
   // Base type may not actually be an array due to earlier errors
4132
   type_t elem = base;
234✔
4133
   if (type_is_array(base))
234✔
4134
      elem = type_elem(base);
233✔
4135

4136
   type_set_elem(type, p_element_constraint(elem));
234✔
4137
}
4138

4139
static tree_t p_record_element_constraint(type_t base)
548✔
4140
{
4141
   // simple_name element_constraint
4142

4143
   BEGIN("record element constraint");
548✔
4144

4145
   ident_t id = p_identifier();
548✔
4146

4147
   // Base type may not actually be a record due to earlier errors
4148
   tree_t decl = NULL;
548✔
4149
   if (type_is_record(base))
548✔
4150
      decl = resolve_field_name(nametab, &last_loc, id, base);
548✔
4151

4152
   type_t ftype;
548✔
4153
   if (decl != NULL) {
548✔
4154
      assert(tree_kind(decl) == T_FIELD_DECL);
545✔
4155

4156
      tree_t cons = type_constraint_for_field(base, decl);
545✔
4157
      ftype = cons ? tree_type(cons) : tree_type(decl);
545✔
4158
   }
4159
   else
4160
      ftype = type_new(T_NONE);
3✔
4161

4162
   tree_t elem = tree_new(T_ELEM_CONSTRAINT);
548✔
4163
   tree_set_ident(elem, id);
548✔
4164
   tree_set_ref(elem, decl);
548✔
4165

4166
   type_t sub = p_element_constraint(ftype);
548✔
4167

4168
   tree_set_type(elem, sub);
548✔
4169
   tree_set_loc(elem, CURRENT_LOC);
548✔
4170
   return elem;
548✔
4171
}
4172

4173
static tree_t p_record_constraint(type_t base)
317✔
4174
{
4175
   // ( record_element_constraint { , record_element_constraint } )
4176

4177
   BEGIN("record constraint");
317✔
4178

4179
   consume(tLPAREN);
317✔
4180

4181
   tree_t c = tree_new(T_CONSTRAINT);
317✔
4182
   tree_set_subkind(c, C_RECORD);
317✔
4183

4184
   do {
548✔
4185
      tree_add_range(c, p_record_element_constraint(base));
548✔
4186
   } while (optional(tCOMMA));
548✔
4187

4188
   consume(tRPAREN);
317✔
4189

4190
   tree_set_loc(c, CURRENT_LOC);
317✔
4191
   return c;
317✔
4192
}
4193

4194
static void p_constraint(type_t type)
10,481✔
4195
{
4196
   // range_constraint | index_constraint
4197
   // 2008: range_constraint | array_constraint | record_constraint
4198

4199
   BEGIN("constraint");
20,962✔
4200

4201
   assert(type_kind(type) == T_SUBTYPE);
10,481✔
4202
   type_t base = type_base(type);
10,481✔
4203

4204
   switch (peek()) {
10,481✔
4205
   case tRANGE:
492✔
4206
      type_set_constraint(type, p_range_constraint(base));
492✔
4207
      break;
492✔
4208

4209
   case tLPAREN:
9,989✔
4210
      if (standard() < STD_08)
9,989✔
4211
         type_set_constraint(type, p_index_constraint(base));
5,208✔
4212
      else if (type_is_record(base))
4,781✔
4213
         type_set_constraint(type, p_record_constraint(base));
213✔
4214
      else
4215
         p_array_constraint(type, base);
4,568✔
4216
      break;
4217

UNCOV
4218
   default:
×
UNCOV
4219
      one_of(tRANGE, tLPAREN);
×
4220
   }
4221
}
10,481✔
4222

4223
static tree_t p_element_resolution(void)
30✔
4224
{
4225
   // array_element_resolution | record_resolution
4226

4227
   BEGIN("element resolution");
30✔
4228

4229
   tree_t t = tree_new(T_ELEM_RESOLUTION);
30✔
4230

4231
   do {
31✔
4232
      tree_t a = tree_new(T_ASSOC);
31✔
4233
      tree_set_subkind(a, A_POS);
31✔
4234
      tree_set_value(a, p_resolution_indication());
31✔
4235
      tree_set_loc(a, CURRENT_LOC);
31✔
4236

4237
      tree_add_assoc(t, a);
31✔
4238
   } while (optional(tCOMMA));
31✔
4239

4240
   tree_set_loc(t, CURRENT_LOC);
30✔
4241
   return t;
30✔
4242
}
4243

4244
static tree_t p_resolution_indication(void)
57✔
4245
{
4246
   // resolution_function_name | 2008: ( element_resolution )
4247

4248
   BEGIN("resolution indication");
114✔
4249

4250
   if (peek() == tID || standard() < STD_08)
57✔
4251
      return p_name(N_SUBPROGRAM);
27✔
4252
   else {
4253
      one_of(tLPAREN, tID);
30✔
4254
      tree_t rname = p_element_resolution();
30✔
4255
      consume(tRPAREN);
30✔
4256
      return rname;
30✔
4257
   }
4258
}
4259

4260
static type_t p_subtype_indication(void)
64,698✔
4261
{
4262
   // [ name ] type_mark [ constraint ]
4263

4264
   BEGIN("subtype indication");
64,698✔
4265

4266
   bool made_subtype = false;
64,698✔
4267
   type_t type = NULL;
64,698✔
4268
   tree_t rname = NULL;
64,698✔
4269

4270
   if (peek() == tLPAREN)
64,698✔
4271
      rname = p_resolution_indication();
26✔
4272
   else {
4273
      tree_t name = p_name(N_TYPE);
64,672✔
4274
      if (peek() == tID)
64,672✔
4275
         rname = name;
4276
      else
4277
         type = name_to_type_mark(name);
64,348✔
4278
   }
4279

4280
   if (rname != NULL) {
64,698✔
4281
      type = type_new(T_SUBTYPE);
350✔
4282
      made_subtype = true;
350✔
4283

4284
      type_set_resolution(type, rname);
350✔
4285
      type_set_base(type, p_type_mark());
350✔
4286

4287
      resolve_resolution(nametab, rname, type);
350✔
4288
   }
4289

4290
   if (type == NULL)
64,698✔
UNCOV
4291
      type = p_type_mark();
×
4292

4293
   if (scan(tRANGE, tLPAREN)) {
64,698✔
4294
      if (!made_subtype) {
10,481✔
4295
         type_t sub = type_new(T_SUBTYPE);
10,462✔
4296
         type_set_base(sub, type);
10,462✔
4297

4298
         type = sub;
10,462✔
4299
      }
4300

4301
      p_constraint(type);
10,481✔
4302
   }
4303

4304
   return type;
64,698✔
4305
}
4306

4307
static tree_t p_abstract_literal(void)
87,126✔
4308
{
4309
   // decimal_literal | based_literal
4310

4311
   BEGIN("abstract literal");
87,126✔
4312

4313
   tree_t t = tree_new(T_LITERAL);
87,126✔
4314

4315
   switch (one_of(tINT, tREAL)) {
87,126✔
4316
   case tINT:
69,650✔
4317
      tree_set_subkind(t, L_INT);
69,650✔
4318
      tree_set_ival(t, last_lval.i64);
69,650✔
4319
      break;
69,650✔
4320

4321
   case tREAL:
17,476✔
4322
      tree_set_subkind(t, L_REAL);
17,476✔
4323
      tree_set_dval(t, last_lval.real);
17,476✔
4324
      break;
17,476✔
4325
   }
4326

4327
   tree_set_loc(t, CURRENT_LOC);
87,126✔
4328
   return t;
87,126✔
4329
}
4330

4331
static tree_t p_physical_literal(void)
7,390✔
4332
{
4333
   // [ abstract_literal ] name
4334

4335
   BEGIN("physical literal");
7,390✔
4336

4337
   tree_t mult;
7,390✔
4338
   if (scan(tINT, tREAL))
7,390✔
4339
      mult = p_abstract_literal();
7,389✔
4340
   else {
4341
      mult = tree_new(T_LITERAL);
1✔
4342
      tree_set_ival(mult, 1);
1✔
4343
   }
4344

4345
   ident_t ident = p_identifier();
7,390✔
4346

4347
   tree_set_subkind(mult, L_PHYSICAL);
7,390✔
4348
   tree_set_loc(mult, CURRENT_LOC);
7,390✔
4349
   tree_set_ident(mult, ident);
7,390✔
4350
   tree_set_type(mult, NULL);
7,390✔
4351

4352
   return mult;
7,390✔
4353
}
4354

4355
static tree_t p_numeric_literal(void)
87,014✔
4356
{
4357
   // abstract_literal | physical_literal
4358

4359
   BEGIN("numeric literal");
174,028✔
4360

4361
   if (peek_nth(2) == tID)
87,014✔
4362
      return p_physical_literal();
7,277✔
4363
   else
4364
      return p_abstract_literal();
79,737✔
4365
}
4366

4367
static tree_t p_string_literal(void)
23,037✔
4368
{
4369
   // string_literal
4370

4371
   BEGIN("string literal");
23,037✔
4372

4373
   consume(tSTRING);
23,037✔
4374

4375
   tree_t t = tree_new(T_STRING);
23,037✔
4376
   tree_set_loc(t, CURRENT_LOC);
23,037✔
4377

4378
   for (const char *p = last_lval.str + 1; *(p + 1) != '\0'; p++) {
349,033✔
4379
      const char ch[] = { '\'', *p, '\'', '\0' };
325,996✔
4380
      tree_t ref = tree_new(T_REF);
325,996✔
4381
      tree_set_loc(ref, CURRENT_LOC);
325,996✔
4382
      tree_set_ident(ref, ident_new(ch));
325,996✔
4383
      tree_add_char(t, ref);
325,996✔
4384
   }
4385

4386
   free(last_lval.str);
23,037✔
4387
   return t;
23,037✔
4388
}
4389

4390
static tree_t p_literal(void)
112,515✔
4391
{
4392
   // numeric_literal | enumeration_literal | string_literal
4393
   // | bit_string_literal | null
4394

4395
   BEGIN("literal");
225,030✔
4396

4397
   switch (peek()) {
112,515✔
4398
   case tNULL:
181✔
4399
      {
4400
         consume(tNULL);
181✔
4401

4402
         tree_t t = tree_new(T_LITERAL);
181✔
4403
         tree_set_loc(t, CURRENT_LOC);
181✔
4404
         tree_set_subkind(t, L_NULL);
181✔
4405
         return t;
181✔
4406
      }
4407

4408
   case tINT:
87,014✔
4409
   case tREAL:
4410
      return p_numeric_literal();
87,014✔
4411

4412
   case tSTRING:
23,037✔
4413
      return p_string_literal();
23,037✔
4414

4415
   case tBITSTRING:
2,283✔
4416
      {
4417
         consume(tBITSTRING);
2,283✔
4418

4419
         tree_t t = bit_string_to_literal(last_lval.str, CURRENT_LOC);
2,283✔
4420
         free(last_lval.str);
2,283✔
4421
         return t;
2,283✔
4422
      }
4423

UNCOV
4424
   default:
×
UNCOV
4425
      expect(tNULL, tINT, tREAL);
×
UNCOV
4426
      return error_expr();
×
4427
   }
4428
}
4429

4430
static void p_choice(tree_t parent, tree_t head, type_t constraint)
8,801✔
4431
{
4432
   // simple_expression | discrete_range | simple_name | others
4433

4434
   BEGIN("choice");
17,602✔
4435

4436
   tree_t t = tree_new(T_ASSOC);
8,801✔
4437

4438
   if (head == NULL && optional(tOTHERS))
8,801✔
4439
      tree_set_subkind(t, A_OTHERS);
2,918✔
4440
   else {
4441
      tree_t name = head ?: p_expression();
5,883✔
4442
      const tree_kind_t name_kind = tree_kind(name);
5,883✔
4443

4444
      bool is_range = false;
5,883✔
4445

4446
      if (scan(tDOWNTO, tTO, tRANGE, tREVRANGE))
5,883✔
4447
         is_range = true;
4448
      else if (constraint != NULL && name_kind == T_REF && tree_has_ref(name))
5,340✔
4449
         is_range = is_type_decl(tree_ref(name));
1,060✔
4450
      else if (name_kind == T_ATTR_REF) {
4,280✔
4451
         const attr_kind_t attr = tree_subkind(name);
198✔
4452
         is_range = attr == ATTR_RANGE || attr == ATTR_REVERSE_RANGE;
198✔
4453
      }
4454

4455
      tree_t choice = NULL;
7,141✔
4456
      if (is_range) {
1,258✔
4457
         tree_set_subkind(t, A_RANGE);
658✔
4458
         tree_add_range(t, (choice = p_discrete_range(name)));
658✔
4459
      }
4460
      else {
4461
         tree_set_subkind(t, A_NAMED);
5,225✔
4462
         tree_set_name(t, (choice = name));
5,225✔
4463
      }
4464

4465
      if (constraint != NULL)
5,883✔
4466
         solve_types(nametab, choice, constraint);
2,936✔
4467
   }
4468

4469
   tree_set_loc(t, CURRENT_LOC);
8,801✔
4470
   tree_add_assoc(parent, t);
8,801✔
4471
}
8,801✔
4472

4473
static void p_choices(tree_t parent, tree_t head, type_t constraint)
7,937✔
4474
{
4475
   // choices ::= choice { | choice }
4476

4477
   BEGIN("choices");
15,874✔
4478

4479
   p_choice(parent, head, constraint);
7,937✔
4480

4481
   while (optional(tBAR))
8,801✔
4482
      p_choice(parent, NULL, constraint);
864✔
4483
}
7,937✔
4484

4485
static void p_element_association(tree_t agg, tree_t head)
37,566✔
4486
{
4487
   // [ choices => ] expression
4488

4489
   BEGIN("element association");
75,132✔
4490

4491
   if (head == NULL && peek() != tOTHERS)
37,566✔
4492
      head = p_expression();
28,647✔
4493

4494
   const int nstart = tree_assocs(agg);
37,566✔
4495

4496
   if (scan(tBAR, tASSOC, tTO, tDOWNTO, tOTHERS)) {
37,566✔
4497
      p_choices(agg, head, NULL);
5,181✔
4498

4499
      consume(tASSOC);
5,181✔
4500

4501
      tree_t value = p_expression();
5,181✔
4502
      const int nassocs = tree_assocs(agg);
5,181✔
4503
      for (int i = nstart; i < nassocs; i++) {
10,605✔
4504
         tree_t a = tree_assoc(agg, i);
5,424✔
4505
         tree_set_value(a, value);
5,424✔
4506
         tree_set_loc(a, CURRENT_LOC);
5,424✔
4507
      }
4508
   }
4509
   else {
4510
      tree_t t = tree_new(T_ASSOC);
32,385✔
4511
      tree_set_subkind(t, A_POS);
32,385✔
4512
      tree_set_value(t, head ?: p_expression());
32,385✔
4513
      tree_set_loc(t, CURRENT_LOC);
32,385✔
4514
      tree_set_pos(t, nstart);
32,385✔
4515

4516
      tree_add_assoc(agg, t);
32,385✔
4517
   }
4518
}
37,566✔
4519

4520
static tree_t p_aggregate(void)
2,394✔
4521
{
4522
   // ( element_association { , element_association } )
4523

4524
   BEGIN("aggregate");
2,394✔
4525

4526
   tree_t t = tree_new(T_AGGREGATE);
2,394✔
4527

4528
   consume(tLPAREN);
2,394✔
4529

4530
   do {
2,581✔
4531
      p_element_association(t, NULL);
2,581✔
4532
   } while (optional(tCOMMA));
2,581✔
4533

4534
   consume(tRPAREN);
2,394✔
4535

4536
   tree_set_loc(t, CURRENT_LOC);
2,394✔
4537
   return t;
2,394✔
4538
}
4539

4540
static tree_t p_aggregate_or_expression(void)
26,334✔
4541
{
4542
   // aggregate | expression
4543

4544
   BEGIN("aggregate or expression");
52,668✔
4545

4546
   if (peek_nth(2) == tOTHERS)
26,334✔
4547
      return p_aggregate();
2,262✔
4548

4549
   consume(tLPAREN);
24,072✔
4550

4551
   tree_t head = p_expression();
24,072✔
4552

4553
   switch (peek()) {
24,072✔
4554
   case tRPAREN:
17,630✔
4555
      consume(tRPAREN);
17,630✔
4556
      return head;
17,630✔
4557

4558
   case tASSOC:
6,442✔
4559
   case tCOMMA:
4560
   case tTO:
4561
   case tDOWNTO:
4562
   case tBAR:
4563
      {
4564
         tree_t t = tree_new(T_AGGREGATE);
6,442✔
4565

4566
         p_element_association(t, head);
6,442✔
4567
         while (optional(tCOMMA))
34,985✔
4568
            p_element_association(t, NULL);
28,543✔
4569

4570
         consume(tRPAREN);
6,442✔
4571

4572
         tree_set_loc(t, CURRENT_LOC);
6,442✔
4573
         return t;
6,442✔
4574
      }
4575

UNCOV
4576
   default:
×
UNCOV
4577
      expect(tRPAREN, tASSOC, tCOMMA, tTO, tDOWNTO, tBAR);
×
UNCOV
4578
      drop_tokens_until(tRPAREN);
×
UNCOV
4579
      return head;
×
4580
   }
4581
}
4582

4583
static tree_t p_qualified_expression(tree_t prefix)
3,964✔
4584
{
4585
   // type_mark ' ( expression ) | type_mark ' aggregate
4586

4587
   EXTEND("qualified expression");
3,964✔
4588

4589
   type_t type;
3,964✔
4590
   if (prefix == NULL)
3,964✔
4591
      type = p_type_mark();
253✔
4592
   else {
4593
      tree_t decl = NULL;
3,711✔
4594
      if (tree_kind(prefix) == T_REF && tree_has_ref(prefix))
3,711✔
4595
         decl = aliased_type_decl(tree_ref(prefix));
3,711✔
4596

4597
      if (decl != NULL)
3,711✔
4598
         type = tree_type(decl);
3,710✔
4599
      else {
4600
         parse_error(tree_loc(prefix), "expecting type mark while parsing "
1✔
4601
                     "qualified expression");
4602
         type = type_new(T_NONE);
1✔
4603
      }
4604
   }
4605

4606
   tree_t qual = tree_new(T_QUALIFIED);
3,964✔
4607
   tree_set_type(qual, type);
3,964✔
4608
   tree_set_ident(qual, type_ident(type));
3,964✔
4609

4610
   consume(tTICK);
3,964✔
4611

4612
   tree_t value = p_aggregate_or_expression();
3,964✔
4613

4614
   tree_set_value(qual, value);
3,964✔
4615
   solve_types(nametab, value, type);
3,964✔
4616

4617
   tree_set_loc(qual, CURRENT_LOC);
3,964✔
4618
   return qual;
3,964✔
4619
}
4620

4621
static tree_t p_allocator(void)
541✔
4622
{
4623
   // new subtype_indication | new qualified_expression
4624

4625
   BEGIN("allocator");
541✔
4626

4627
   consume(tNEW);
541✔
4628

4629
   tree_t new = tree_new(T_NEW);
541✔
4630

4631
   tree_t value;
541✔
4632
   if (peek_nth(2) == tTICK && peek_nth(3) == tLPAREN)
541✔
4633
      value = p_qualified_expression(NULL);
253✔
4634
   else {
4635
      type_t type = p_subtype_indication();
288✔
4636

4637
      value = tree_new(T_QUALIFIED);
288✔
4638
      tree_set_type(value, type);
288✔
4639
      tree_set_loc(value, CURRENT_LOC);
288✔
4640
   }
4641

4642
   tree_set_value(new, value);
541✔
4643
   tree_set_loc(new, CURRENT_LOC);
541✔
4644

4645
   return new;
541✔
4646
}
4647

4648
static tree_t p_primary(tree_t head)
323,742✔
4649
{
4650
   // name | literal | aggregate | function_call | qualified_expression
4651
   //   | type_conversion | allocator | ( expression ) |
4652
   //   | PSL: Built_In_Function_Call
4653

4654
   BEGIN("primary");
647,484✔
4655

4656
   assert(head == NULL);
323,742✔
4657

4658
   switch (peek()) {
323,742✔
4659
   case tLPAREN:
22,370✔
4660
      return p_aggregate_or_expression();
22,370✔
4661

4662
   case tINT:
89,478✔
4663
   case tREAL:
4664
   case tNULL:
4665
   case tBITSTRING:
4666
      return p_literal();
89,478✔
4667

4668
   case tSTRING:
23,114✔
4669
      if (peek_nth(2) != tLPAREN && peek_nth(2) != tDOT)
23,114✔
4670
         return p_literal();
23,037✔
4671
      // Fall-through
4672
   case tID:
4673
      {
4674
         tree_t expr = p_name(0);
188,151✔
4675
         if (peek() == tLSQUARE)
188,151✔
4676
            return p_attribute_name(expr);
14✔
4677
         else if (tree_kind(expr) == T_PROT_REF)
188,137✔
4678
            return p_function_call(tree_ident(expr), tree_value(expr));
525✔
4679
         else
4680
            return expr;
4681
      }
4682

4683
   case tLTLT:
116✔
4684
      return p_name(N_SUBPROGRAM);
116✔
4685

4686
   case tNEW:
541✔
4687
      return p_allocator();
541✔
4688

4689
   case tPREV:
43✔
4690
   case tPSLNEXT:
4691
   case tROSE:
4692
   case tFELL:
4693
   case tENDED:
4694
   case tSTABLE:
4695
      {
4696
         psl_node_t p = p_psl_builtin_function_call();
43✔
4697
         psl_check(p, nametab);
43✔
4698

4699
         tree_t t = tree_new(T_PSL_FCALL);
43✔
4700
         tree_set_psl(t, p);
43✔
4701
         tree_set_loc(t, CURRENT_LOC);
43✔
4702
         return t;
43✔
4703
      }
4704

4705
   default:
6✔
4706
      expect(tLPAREN, tINT, tREAL, tNULL, tID, tSTRING, tBITSTRING, tNEW);
6✔
4707
      return error_expr();
6✔
4708
   }
4709
}
4710

4711
static ident_t p_logical_operator(void)
8,417✔
4712
{
4713
   switch (one_of(tAND, tOR, tNAND, tNOR, tXOR, tXNOR)) {
8,417✔
4714
   case tAND:
5,812✔
4715
      return well_known(W_OP_AND);
5,812✔
4716
   case tOR:
1,822✔
4717
      return well_known(W_OP_OR);
1,822✔
4718
   case tNAND:
147✔
4719
      return well_known(W_OP_NAND);
147✔
4720
   case tNOR:
151✔
4721
      return well_known(W_OP_NOR);
151✔
4722
   case tXOR:
344✔
4723
      return well_known(W_OP_XOR);
344✔
4724
   case tXNOR:
141✔
4725
      return well_known(W_OP_XNOR);
141✔
UNCOV
4726
   default:
×
UNCOV
4727
      return error_marker();
×
4728
   }
4729
}
4730

4731
static tree_t p_unary_expression(tree_t head)
323,861✔
4732
{
4733
   // primary | abs primary | not primary | unary_logical_operator primary
4734

4735
   BEGIN("unary expression");
647,722✔
4736

4737
   if (head != NULL)
323,861✔
4738
      return head;    // Injected from mis-parsed PSL property
4739

4740
   ident_t op = NULL;
323,453✔
4741
   switch (peek()) {
323,453✔
4742
   case tNOT:
3,072✔
4743
      consume(tNOT);
3,072✔
4744
      op = well_known(W_OP_NOT);
3,072✔
4745
      break;
3,072✔
4746

4747
   case tABS:
248✔
4748
      consume(tABS);
248✔
4749
      op = well_known(W_OP_ABS);
248✔
4750
      break;
248✔
4751

4752
   case tAND:
276✔
4753
   case tOR:
4754
   case tNAND:
4755
   case tNOR:
4756
   case tXOR:
4757
   case tXNOR:
4758
      require_std(STD_08, "unary logical operators");
276✔
4759
      op = p_logical_operator();
276✔
4760
      break;
276✔
4761

4762
   default:
4763
      break;
4764
   }
4765

4766
   if (op != NULL) {
3,596✔
4767
      tree_t t = tree_new(T_FCALL);
3,596✔
4768
      tree_set_ident(t, op);
3,596✔
4769
      unary_op(t, p_primary);
3,596✔
4770
      tree_set_loc(t, CURRENT_LOC);
3,596✔
4771

4772
      return t;
3,596✔
4773
   }
4774
   else
4775
      return p_primary(NULL);
319,857✔
4776
}
4777

4778
static tree_t p_factor(tree_t head)
323,861✔
4779
{
4780
   // unary_expression [ ** unary_expression ]
4781

4782
   BEGIN("factor");
647,722✔
4783

4784
   tree_t operand = p_unary_expression(head);
323,861✔
4785

4786
   if (optional(tPOWER)) {
323,861✔
4787
      tree_t second = p_primary(NULL);
287✔
4788

4789
      tree_t t = tree_new(T_FCALL);
287✔
4790
      tree_set_loc(t, CURRENT_LOC);
287✔
4791
      tree_set_ident(t, well_known(W_OP_EXPONENT));
287✔
4792
      add_param(t, operand, P_POS, NULL);
287✔
4793
      add_param(t, second, P_POS, NULL);
287✔
4794

4795
      return t;
287✔
4796
   }
4797

4798
   return operand;
4799
}
4800

4801
static ident_t p_multiplying_operator(void)
3,156✔
4802
{
4803
   switch (one_of(tTIMES, tOVER, tMOD, tREM)) {
3,156✔
4804
   case tTIMES:
1,825✔
4805
      return well_known(W_OP_TIMES);
1,825✔
4806
   case tOVER:
890✔
4807
      return well_known(W_OP_DIVIDE);
890✔
4808
   case tMOD:
290✔
4809
      return well_known(W_OP_MOD);
290✔
4810
   case tREM:
151✔
4811
      return well_known(W_OP_REM);
151✔
UNCOV
4812
   default:
×
UNCOV
4813
      return error_marker();
×
4814
   }
4815
}
4816

4817
static tree_t p_term(tree_t head)
320,705✔
4818
{
4819
   // factor { multiplying_operator factor }
4820

4821
   BEGIN("term");
320,705✔
4822

4823
   tree_t term = p_factor(head);
320,705✔
4824

4825
   while (scan(tTIMES, tOVER, tMOD, tREM)) {
323,861✔
4826
      ident_t op  = p_multiplying_operator();
3,156✔
4827
      tree_t left = term;
3,156✔
4828

4829
      term = tree_new(T_FCALL);
3,156✔
4830
      tree_set_ident(term, op);
3,156✔
4831
      binary_op(term, left, p_factor);
3,156✔
4832
   }
4833

4834
   return term;
320,705✔
4835
}
4836

4837
static ident_t p_adding_operator(void)
12,141✔
4838
{
4839
   switch (one_of(tPLUS, tMINUS, tAMP)) {
12,141✔
4840
   case tPLUS:
3,859✔
4841
      return well_known(W_OP_ADD);
3,859✔
4842
   case tMINUS:
5,726✔
4843
      return well_known(W_OP_MINUS);
5,726✔
4844
   case tAMP:
2,556✔
4845
      return well_known(W_OP_CONCAT);
2,556✔
UNCOV
4846
   default:
×
UNCOV
4847
      return error_marker();
×
4848
   }
4849
}
4850

4851
static ident_t p_sign(void)
8,495✔
4852
{
4853
   switch (one_of(tPLUS, tMINUS)) {
8,495✔
4854
   case tPLUS:
38✔
4855
      return well_known(W_OP_ADD);
38✔
4856
   case tMINUS:
8,457✔
4857
      return well_known(W_OP_MINUS);
8,457✔
UNCOV
4858
   default:
×
UNCOV
4859
      return error_marker();
×
4860
   }
4861
}
4862

4863
static tree_t p_simple_expression(tree_t head)
308,564✔
4864
{
4865
   // [ sign ] term { adding_operator term }
4866

4867
   BEGIN("simple expression");
308,564✔
4868

4869
   tree_t expr = NULL;
308,564✔
4870
   if (head == NULL && scan(tPLUS, tMINUS)) {
317,059✔
4871
      ident_t sign = p_sign();
8,495✔
4872
      expr = tree_new(T_FCALL);
8,495✔
4873
      tree_set_ident(expr, sign);
8,495✔
4874
      unary_op(expr, p_term);
8,495✔
4875
      tree_set_loc(expr, CURRENT_LOC);
8,495✔
4876
   }
4877
   else
4878
      expr = p_term(head);
300,069✔
4879

4880
   while (scan(tPLUS, tMINUS, tAMP)) {
320,705✔
4881
      tree_t left = expr;
12,141✔
4882
      expr = tree_new(T_FCALL);
12,141✔
4883
      tree_set_ident(expr, p_adding_operator());
12,141✔
4884
      binary_op(expr, left, p_term);
12,141✔
4885
   }
4886

4887
   return expr;
308,564✔
4888
}
4889

4890
static ident_t p_shift_operator(void)
312✔
4891
{
4892
   switch (one_of(tSLL, tSRL, tSLA, tSRA, tROL, tROR)) {
312✔
4893
   case tSLL:
36✔
4894
      return well_known(W_OP_SLL);
36✔
4895
   case tSRL:
32✔
4896
      return well_known(W_OP_SRL);
32✔
4897
   case tSLA:
26✔
4898
      return well_known(W_OP_SLA);
26✔
4899
   case tSRA:
32✔
4900
      return well_known(W_OP_SRA);
32✔
4901
   case tROL:
96✔
4902
      return well_known(W_OP_ROL);
96✔
4903
   case tROR:
90✔
4904
      return well_known(W_OP_ROR);
90✔
UNCOV
4905
   default:
×
UNCOV
4906
      return error_marker();
×
4907
   }
4908
}
4909

4910
static tree_t p_shift_expression(tree_t head)
308,252✔
4911
{
4912
   // simple_expression [ shift_operator simple_expression ]
4913

4914
   BEGIN("shift expression");
308,252✔
4915

4916
   tree_t shift = p_simple_expression(head);
308,252✔
4917

4918
   while (scan(tSLL, tSRL, tSLA, tSRA, tROL, tROR)) {
308,564✔
4919
      ident_t op   = p_shift_operator();
312✔
4920
      tree_t left  = shift;
312✔
4921
      tree_t right = p_simple_expression(NULL);
312✔
4922

4923
      shift = tree_new(T_FCALL);
312✔
4924
      tree_set_ident(shift, op);
312✔
4925
      tree_set_loc(shift, CURRENT_LOC);
312✔
4926

4927
      add_param(shift, left, P_POS, NULL);
312✔
4928
      add_param(shift, right, P_POS, NULL);
312✔
4929
   }
4930

4931
   return shift;
308,252✔
4932
}
4933

4934
static ident_t p_relational_operator(void)
30,448✔
4935
{
4936
   switch (one_of(tEQ, tNEQ, tLT, tLE, tGT, tGE,
30,448✔
4937
                  tMEQ, tMNEQ, tMLT, tMLE, tMGT, tMGE)) {
4938
   case tEQ:
23,325✔
4939
      return well_known(W_OP_EQUAL);
23,325✔
4940
   case tNEQ:
1,100✔
4941
      return well_known(W_OP_NOT_EQUAL);
1,100✔
4942
   case tLT:
2,438✔
4943
      return well_known(W_OP_LESS_THAN);
2,438✔
4944
   case tLE:
848✔
4945
      return well_known(W_OP_LESS_EQUAL);
848✔
4946
   case tGT:
1,508✔
4947
      return well_known(W_OP_GREATER_THAN);
1,508✔
4948
   case tGE:
840✔
4949
      return well_known(W_OP_GREATER_EQUAL);
840✔
4950
   case tMEQ:
97✔
4951
      return well_known(W_OP_MATCH_EQUAL);
97✔
4952
   case tMNEQ:
64✔
4953
      return well_known(W_OP_MATCH_NOT_EQUAL);
64✔
4954
   case tMLT:
58✔
4955
      return well_known(W_OP_MATCH_LESS_THAN);
58✔
4956
   case tMLE:
59✔
4957
      return well_known(W_OP_MATCH_LESS_EQUAL);
59✔
4958
   case tMGT:
56✔
4959
      return well_known(W_OP_MATCH_GREATER_THAN);
56✔
4960
   case tMGE:
55✔
4961
      return well_known(W_OP_MATCH_GREATER_EQUAL);
55✔
UNCOV
4962
   default:
×
UNCOV
4963
      return error_marker();
×
4964
   }
4965
}
4966

4967
static tree_t p_relation(tree_t head)
277,804✔
4968
{
4969
   // shift_expression [ relational_operator shift_expression ]
4970

4971
   BEGIN("relation");
277,804✔
4972

4973
   tree_t rel = p_shift_expression(head);
277,804✔
4974

4975
   while (scan(tEQ, tNEQ, tLT, tLE, tGT, tGE,
490,047✔
4976
               tMEQ, tMNEQ, STD(08, tMLT), tMLE, tMGT, tMGE)) {
4977
      ident_t op  = p_relational_operator();
30,448✔
4978
      tree_t left = rel;
30,448✔
4979

4980
      rel = tree_new(T_FCALL);
30,448✔
4981
      tree_set_ident(rel, op);
30,448✔
4982
      binary_op(rel, left, p_shift_expression);
30,448✔
4983
   }
4984

4985
   return rel;
277,804✔
4986
}
4987

4988
static tree_t p_expression_with_head(tree_t head)
268,578✔
4989
{
4990
   // relation { and relation } | relation { or relation }
4991
   //   | relation { xor relation } | relation [ nand relation ]
4992
   //   | relation [ nor relation ] | relation { xnor relation }
4993
   //   | 2008: condition_operator primary
4994

4995
   BEGIN("expression");
268,578✔
4996

4997
   tree_t expr = p_relation(head);
268,578✔
4998

4999
   int loop_limit = (scan(tNOR, tNAND) ? 1 : INT_MAX);
268,578✔
5000

5001
   while (loop_limit-- && scan(tAND, tOR, tXOR, tNAND, tNOR, tXNOR)) {
276,717✔
5002
      tree_t new = tree_new(T_FCALL);
8,139✔
5003
      tree_set_ident(new, p_logical_operator());
8,139✔
5004
      binary_op(new, expr, p_relation);
8,139✔
5005
      expr = new;
8,139✔
5006
   }
5007

5008
   return expr;
268,578✔
5009
}
5010

5011
static inline tree_t p_expression(void)
268,566✔
5012
{
5013
   // expression | 2008: condition_operator primary
5014

5015
   if (optional(tCCONV)) {
268,566✔
5016
      require_std(STD_08, "condition conversion");
2✔
5017

5018
      tree_t expr = tree_new(T_FCALL);
2✔
5019
      tree_set_ident(expr, well_known(W_OP_CCONV));
2✔
5020
      unary_op(expr, p_primary);
2✔
5021
      return expr;
2✔
5022
   }
5023
   else
5024
      return p_expression_with_head(NULL);
268,564✔
5025
}
5026

5027
static type_t p_interface_type_indication(tree_t parent)
33,084✔
5028
{
5029
   // subtype_indication | anonymous_type_indication
5030

5031
   BEGIN("interface type indication");
66,168✔
5032

5033
   if (peek() == tTYPE) {
33,084✔
5034
      require_std(STD_19, "anonymous type indication");
3✔
5035

5036
      type_t type = p_anonymous_type_indication();
3✔
5037

5038
      tree_t g = tree_new(T_GENERIC_DECL);
3✔
5039
      tree_set_loc(g, CURRENT_LOC);
3✔
5040
      tree_set_ident(g, ident_uniq("anonymous"));
3✔
5041
      tree_set_subkind(g, PORT_IN);
3✔
5042
      tree_set_class(g, C_TYPE);
3✔
5043
      tree_set_type(g, type);
3✔
5044

5045
      tree_add_generic(parent, g);
3✔
5046

5047
      return type;
3✔
5048
   }
5049
   else
5050
      return p_subtype_indication();
33,081✔
5051
}
5052

5053
static void p_interface_constant_declaration(tree_t parent, tree_kind_t kind,
27,725✔
5054
                                             bool ordered)
5055
{
5056
   // [ constant ] identifier_list : [ in ] subtype_indication [ := expression ]
5057

5058
   BEGIN("interface constant declaration");
55,450✔
5059

5060
   const bool explicit_constant = optional(tCONSTANT);
27,725✔
5061
   tree_flags_t flags = (explicit_constant) ? TREE_F_EXPLICIT_CLASS : 0;
27,725✔
5062

5063
   LOCAL_IDENT_LIST ids = p_identifier_list();
55,450✔
5064

5065
   consume(tCOLON);
27,725✔
5066

5067
   // The grammar only allows IN here but we are more leniant to avoid
5068
   // having disambiguate constant and variable interface declarations
5069
   // See LRM 93 section 2.1.1 for default class
5070
   port_mode_t mode = PORT_IN;
27,725✔
5071
   if (scan(tIN, tOUT, tINOUT, tBUFFER, tLINKAGE)) {
27,725✔
5072
      flags |= TREE_F_EXPLICIT_MODE;
8,413✔
5073
      mode = p_mode();
8,413✔
5074
   }
5075

5076
   class_t class = C_CONSTANT;
27,725✔
5077
   if ((mode == PORT_OUT || mode == PORT_INOUT) && !explicit_constant)
27,725✔
5078
      class = C_VARIABLE;
1,781✔
5079

5080
   if (kind == T_GENERIC_DECL) {
27,725✔
5081
      // In the 2008 standard generics in packages or subprograms are
5082
      // locally static if they have a locally static subtype
5083
      switch (tree_kind(parent)) {
1,709✔
5084
      case T_PACKAGE:
253✔
5085
      case T_FUNC_DECL:
5086
      case T_PROC_DECL:
5087
         flags |= TREE_F_LOCALLY_STATIC;
253✔
5088
         break;
253✔
5089
      default:
5090
         break;
5091
      }
5092
   }
5093

5094
   type_t type = p_interface_type_indication(parent);
27,725✔
5095

5096
   tree_t init = NULL;
27,725✔
5097
   if (optional(tWALRUS)) {
27,725✔
5098
      init = p_expression();
5,028✔
5099
      solve_types(nametab, init, type);
5,028✔
5100
   }
5101

5102
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
58,354✔
5103
      tree_t d = tree_new(kind);
30,629✔
5104
      tree_set_ident(d, it->ident);
30,629✔
5105
      tree_set_loc(d, &(it->loc));
30,629✔
5106
      tree_set_subkind(d, mode);
30,629✔
5107
      tree_set_type(d, type);
30,629✔
5108
      tree_set_class(d, class);
30,629✔
5109
      tree_set_flag(d, flags);
30,629✔
5110

5111
      if (init != NULL)
30,629✔
5112
         tree_set_value(d, init);
5,143✔
5113

5114
      add_interface(parent, d, kind);
30,629✔
5115
      sem_check(d, nametab);
30,629✔
5116

5117
      if (ordered)
30,629✔
5118
         insert_name(nametab, d, NULL);
7,583✔
5119

5120
      flags |= TREE_F_CONTINUATION;
30,629✔
5121
   }
5122
}
27,725✔
5123

5124
static void p_array_mode_view_indication(type_t *type, tree_t *name)
17✔
5125
{
5126
   // view ( name ) of subtype_indication
5127

5128
   BEGIN("array mode view indication");
17✔
5129

5130
   consume(tVIEW);
17✔
5131
   consume(tLPAREN);
17✔
5132

5133
   *name = p_name(0);
17✔
5134
   solve_types(nametab, *name, NULL);
17✔
5135

5136
   consume(tRPAREN);
17✔
5137
   consume(tOF);
17✔
5138

5139
   *type = p_subtype_indication();
17✔
5140
}
17✔
5141

5142
static void p_record_mode_view_indication(type_t *type, tree_t *name)
75✔
5143
{
5144
   // view name [ of subtype_indication ]
5145

5146
   BEGIN("record mode view indication");
150✔
5147

5148
   consume(tVIEW);
75✔
5149

5150
   *name = p_name(0);
75✔
5151
   type_t name_type = solve_types(nametab, *name, NULL);
75✔
5152

5153
   if (optional(tOF))
75✔
5154
      *type = p_subtype_indication();
11✔
5155
   else if (type_kind(name_type) != T_VIEW) {
64✔
5156
      parse_error(tree_loc(*name), "name in mode view indication does not "
1✔
5157
                  "denote a mode view");
5158
      *type = type_new(T_NONE);
1✔
5159
   }
5160
   else
5161
      *type = type_designated(name_type);
63✔
5162
}
75✔
5163

5164
static port_mode_t p_mode_view_indication(type_t *type, tree_t *name)
92✔
5165
{
5166
   // record_mode_view_indication | array_mode_view_indication
5167

5168
   BEGIN("mode view indication");
184✔
5169

5170
   if (peek_nth(2) == tLPAREN) {
92✔
5171
      p_array_mode_view_indication(type, name);
17✔
5172
      return PORT_ARRAY_VIEW;
17✔
5173
   }
5174
   else {
5175
      p_record_mode_view_indication(type, name);
75✔
5176
      return PORT_RECORD_VIEW;
75✔
5177
   }
5178
}
5179

5180
static void p_interface_signal_declaration(tree_t parent, tree_kind_t kind,
4,646✔
5181
                                           bool ordered)
5182
{
5183
   // [signal] identifier_list : [ mode ] subtype_indication [ bus ]
5184
   //    [ := expression ]
5185
   // 2019: [ signal ] identifier_list : mode_indication
5186

5187
   BEGIN("interface signal declaration");
9,292✔
5188

5189
   tree_flags_t flags = optional(tSIGNAL) ? TREE_F_EXPLICIT_CLASS : 0;
4,646✔
5190

5191
   LOCAL_IDENT_LIST ids = p_identifier_list();
9,292✔
5192
   consume(tCOLON);
4,646✔
5193

5194
   type_t type = NULL;
4,646✔
5195
   tree_t init = NULL;
4,646✔
5196
   port_mode_t mode = PORT_IN;
4,646✔
5197

5198
   if (peek() == tVIEW) {
4,646✔
5199
      require_std(STD_19, "mode view indication");
92✔
5200
      mode = p_mode_view_indication(&type, &init);
92✔
5201
   }
5202
   else {
5203
      if (scan(tIN, tOUT, tINOUT, tBUFFER, tLINKAGE)) {
4,554✔
5204
         mode = p_mode();
4,208✔
5205
         flags |= TREE_F_EXPLICIT_MODE;
4,208✔
5206
      }
5207

5208
      type = p_interface_type_indication(parent);
4,554✔
5209

5210
      if (optional(tBUS))
4,554✔
5211
         flags |= TREE_F_BUS;
5✔
5212

5213
      if (optional(tWALRUS)) {
4,554✔
5214
         init = p_expression();
337✔
5215
         solve_types(nametab, init, type);
337✔
5216
      }
5217
   }
5218

5219
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
9,564✔
5220
      tree_t d = tree_new(kind);
4,918✔
5221
      tree_set_ident(d, it->ident);
4,918✔
5222
      tree_set_loc(d, &(it->loc));
4,918✔
5223
      tree_set_subkind(d, mode);
4,918✔
5224
      tree_set_type(d, type);
4,918✔
5225
      tree_set_class(d, C_SIGNAL);
4,918✔
5226
      tree_set_flag(d, flags);
4,918✔
5227

5228
      if (init != NULL)
4,918✔
5229
         tree_set_value(d, init);
431✔
5230

5231
      add_interface(parent, d, kind);
4,918✔
5232
      sem_check(d, nametab);
4,918✔
5233

5234
      if (ordered)
4,918✔
5235
         insert_name(nametab, d, NULL);
126✔
5236

5237
      flags |= TREE_F_CONTINUATION;
4,918✔
5238
   }
5239
}
4,646✔
5240

5241
static void p_interface_variable_declaration(tree_t parent, tree_kind_t kind)
805✔
5242
{
5243
   // [variable] identifier_list : [ mode ] subtype_indication [ := expression ]
5244

5245
   BEGIN("interface variable declaration");
1,610✔
5246

5247
   tree_flags_t flags = optional(tVARIABLE) ? TREE_F_EXPLICIT_CLASS : 0;
805✔
5248

5249
   LOCAL_IDENT_LIST ids = p_identifier_list();
1,610✔
5250
   consume(tCOLON);
805✔
5251

5252
   port_mode_t mode = PORT_IN;
805✔
5253
   if (scan(tIN, tOUT, tINOUT, tBUFFER, tLINKAGE)) {
805✔
5254
      mode = p_mode();
789✔
5255
      flags |= TREE_F_EXPLICIT_MODE;
789✔
5256
   }
5257

5258
   type_t type = p_interface_type_indication(parent);
805✔
5259

5260
   tree_t init = NULL;
805✔
5261
   if (optional(tWALRUS)) {
805✔
5262
      init = p_expression();
6✔
5263
      solve_types(nametab, init, type);
6✔
5264
   }
5265

5266
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
1,616✔
5267
      tree_t d = tree_new(kind);
811✔
5268
      tree_set_ident(d, it->ident);
811✔
5269
      tree_set_loc(d, &(it->loc));
811✔
5270
      tree_set_type(d, type);
811✔
5271
      tree_set_class(d, C_VARIABLE);
811✔
5272
      tree_set_subkind(d, mode);
811✔
5273
      tree_set_flag(d, flags);
811✔
5274

5275
      if (init != NULL)
811✔
5276
         tree_set_value(d, init);
6✔
5277

5278
      add_interface(parent, d, kind);
811✔
5279
      sem_check(d, nametab);
811✔
5280

5281
      if (standard() >= STD_19)
811✔
5282
         insert_name(nametab, d, NULL);
28✔
5283

5284
      flags |= TREE_F_CONTINUATION;
811✔
5285
   }
5286
}
805✔
5287

5288
static void p_interface_file_declaration(tree_t parent, tree_kind_t kind)
29✔
5289
{
5290
   // file identifier_list : subtype_indication
5291

5292
   BEGIN("interface file declaration");
58✔
5293

5294
   consume(tFILE);
29✔
5295

5296
   LOCAL_IDENT_LIST ids = p_identifier_list();
58✔
5297

5298
   consume(tCOLON);
29✔
5299

5300
   type_t type = p_subtype_indication();
29✔
5301

5302
   tree_flags_t flags = 0;
29✔
5303
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
62✔
5304
      tree_t d = tree_new(kind);
33✔
5305
      tree_set_ident(d, it->ident);
33✔
5306
      tree_set_loc(d, &(it->loc));
33✔
5307
      tree_set_subkind(d, PORT_IN);
33✔
5308
      tree_set_flag(d, flags);
33✔
5309
      tree_set_type(d, type);
33✔
5310
      tree_set_class(d, C_FILE);
33✔
5311

5312
      add_interface(parent, d, kind);
33✔
5313
      sem_check(d, nametab);
33✔
5314

5315
      if (standard() >= STD_19)
33✔
5316
         insert_name(nametab, d, NULL);
15✔
5317

5318
      flags |= TREE_F_CONTINUATION;
33✔
5319
   }
5320
}
29✔
5321

5322
static void p_private_incomplete_type_definition(type_t type)
24✔
5323
{
5324
   // 2019: private
5325

5326
   BEGIN("private incomplete type definition");
48✔
5327

5328
   consume(tPRIVATE);
24✔
5329

5330
   type_set_subkind(type, GTYPE_PRIVATE);
24✔
5331
}
24✔
5332

5333
static void p_scalar_incomplete_type_definition(type_t type)
5✔
5334
{
5335
   // 2019: <>
5336

5337
   BEGIN("scalar incomplete type definition");
10✔
5338

5339
   consume(tBOX);
5✔
5340

5341
   type_set_subkind(type, GTYPE_SCALAR);
5✔
5342
}
5✔
5343

5344
static void p_discrete_incomplete_type_definition(type_t type)
19✔
5345
{
5346
   // 2019: ( <> )
5347

5348
   BEGIN("discrete incomplete type definition");
38✔
5349

5350
   consume(tLPAREN);
19✔
5351
   consume(tBOX);
19✔
5352
   consume(tRPAREN);
19✔
5353

5354
   type_set_subkind(type, GTYPE_DISCRETE);
19✔
5355
}
19✔
5356

5357
static void p_integer_incomplete_type_definition(type_t type)
11✔
5358
{
5359
   // 2019: range <>
5360

5361
   BEGIN("integer incomplete type definition");
22✔
5362

5363
   consume(tRANGE);
11✔
5364
   consume(tBOX);
11✔
5365

5366
   type_set_subkind(type, GTYPE_INTEGER);
11✔
5367
}
11✔
5368

5369
static void p_physical_incomplete_type_definition(type_t type)
4✔
5370
{
5371
   // 2019: units <>
5372

5373
   BEGIN("physical incomplete type definition");
8✔
5374

5375
   consume(tUNITS);
4✔
5376
   consume(tBOX);
4✔
5377

5378
   type_set_subkind(type, GTYPE_PHYSICAL);
4✔
5379
}
4✔
5380

5381
static void p_floating_incomplete_type_definition(type_t type)
4✔
5382
{
5383
   // 2019: range <> . <>
5384

5385
   BEGIN("floating incomplete type definition");
8✔
5386

5387
   consume(tRANGE);
4✔
5388
   consume(tBOX);
4✔
5389
   consume(tDOT);
4✔
5390
   consume(tBOX);
4✔
5391

5392
   type_set_subkind(type, GTYPE_FLOATING);
4✔
5393
}
4✔
5394

5395
static type_t p_array_index_incomplete_type(void)
20✔
5396
{
5397
   // index_subtype_definition | index_constraint | anonymous_type_indication
5398

5399
   BEGIN("array index incomplete type");
40✔
5400

5401
   if (peek() == tTYPE)
20✔
5402
      return p_anonymous_type_indication();
9✔
5403
   else
5404
      return p_index_subtype_definition(NULL);
11✔
5405
}
5406

5407
static void p_array_index_incomplete_type_list(type_t type)
20✔
5408
{
5409
   // array_index_incomplete_type { , array_index_incomplete_type }
5410

5411
   BEGIN("array index incomplete type list");
40✔
5412

5413
   do {
20✔
5414
      type_add_index(type, p_array_index_incomplete_type());
20✔
5415
   } while (optional(tCOMMA));
20✔
5416
}
20✔
5417

5418
static type_t p_incomplete_subtype_indication(void)
31✔
5419
{
5420
   // subtype_indication | anonymous_type_indication
5421

5422
   BEGIN("incomplete subtype indication");
62✔
5423

5424
   if (peek() == tTYPE)
31✔
5425
      return p_anonymous_type_indication();
18✔
5426
   else
5427
      return p_subtype_indication();
13✔
5428
}
5429

5430
static void p_array_incomplete_type_definition(type_t type)
20✔
5431
{
5432
   // 2019: array ( array_index_incomplete_type_list )
5433
   //   of element_incomplete_subtype_indication
5434

5435
   BEGIN("array incomplete type definition");
40✔
5436

5437
   consume(tARRAY);
20✔
5438
   consume(tLPAREN);
20✔
5439

5440
   p_array_index_incomplete_type_list(type);
20✔
5441

5442
   consume(tRPAREN);
20✔
5443
   consume(tOF);
20✔
5444

5445
   type_set_elem(type, p_incomplete_subtype_indication());
20✔
5446

5447
   type_set_subkind(type, GTYPE_ARRAY);
20✔
5448
}
20✔
5449

5450
static void p_access_incomplete_type_definition(type_t type)
5✔
5451
{
5452
   // 2019: access incomplete_subtype_indication
5453

5454
   BEGIN("access incomplete type definition");
10✔
5455

5456
   consume(tACCESS);
5✔
5457

5458
   type_set_designated(type, p_incomplete_subtype_indication());
5✔
5459

5460
   type_set_subkind(type, GTYPE_ACCESS);
5✔
5461
}
5✔
5462

5463
static void p_file_incomplete_type_definition(type_t type)
6✔
5464
{
5465
   // 2019: file of incomplete_subtype_indication
5466

5467
   BEGIN("file incomplete type definition");
12✔
5468

5469
   consume(tFILE);
6✔
5470
   consume(tOF);
6✔
5471

5472
   type_set_designated(type, p_incomplete_subtype_indication());
6✔
5473

5474
   type_set_subkind(type, GTYPE_FILE);
6✔
5475
}
6✔
5476

5477
static void p_incomplete_type_definition(type_t type)
98✔
5478
{
5479
   // private_incomplete_type_definition
5480
   //   | scalar_incomplete_type_definition
5481
   //   | discrete_incomplete_type_definition
5482
   //   | integer_incomplete_type_definition
5483
   //   | physical_incomplete_type_definition
5484
   //   | floating_incomplete_type_definition
5485
   //   | array_incomplete_type_definition
5486
   //   | access_incomplete_type_definition
5487
   //   | file_incomplete_type_definition
5488

5489
   BEGIN("incomplete type definition");
196✔
5490

5491
   switch (peek()) {
98✔
5492
   case tPRIVATE:
24✔
5493
      p_private_incomplete_type_definition(type);
24✔
5494
      break;
24✔
5495
   case tBOX:
5✔
5496
      p_scalar_incomplete_type_definition(type);
5✔
5497
      break;
5✔
5498
   case tLPAREN:
19✔
5499
      p_discrete_incomplete_type_definition(type);
19✔
5500
      break;
19✔
5501
   case tRANGE:
15✔
5502
      if (peek_nth(3) == tDOT)
15✔
5503
         p_floating_incomplete_type_definition(type);
4✔
5504
      else
5505
         p_integer_incomplete_type_definition(type);
11✔
5506
      break;
5507
   case tUNITS:
4✔
5508
      p_physical_incomplete_type_definition(type);
4✔
5509
      break;
4✔
5510
   case tARRAY:
20✔
5511
      p_array_incomplete_type_definition(type);
20✔
5512
      break;
20✔
5513
   case tACCESS:
5✔
5514
      p_access_incomplete_type_definition(type);
5✔
5515
      break;
5✔
5516
   case tFILE:
6✔
5517
      p_file_incomplete_type_definition(type);
6✔
5518
      break;
6✔
UNCOV
5519
   default:
×
UNCOV
5520
      one_of(tPRIVATE, tBOX, tLPAREN, tRANGE, tUNITS, tARRAY, tACCESS, tFILE);
×
5521
   }
5522

5523
   require_std(STD_19, "incomplete type definition");
98✔
5524
}
98✔
5525

5526
static type_t p_anonymous_type_indication(void)
30✔
5527
{
5528
   // type is incomplete_type_definition
5529

5530
   BEGIN("anonymous type indication");
30✔
5531

5532
   consume(tTYPE);
30✔
5533
   consume(tIS);
30✔
5534

5535
   type_t type = type_new(T_GENERIC);
30✔
5536
   p_incomplete_type_definition(type);
30✔
5537

5538
   return type;
30✔
5539
}
5540

5541
static void p_interface_type_declaration(tree_t parent, tree_kind_t kind)
238✔
5542
{
5543
   // 2008: type identifier
5544
   // 2019: type identifier [ is incomplete_type_definition ]
5545

5546
   BEGIN("interface type declaration");
476✔
5547

5548
   consume(tTYPE);
238✔
5549

5550
   ident_t id = p_identifier();
238✔
5551

5552
   require_std(STD_08, "interface type declarations");
238✔
5553

5554
   type_t type = type_new(T_GENERIC);
238✔
5555
   type_set_ident(type, id);
238✔
5556

5557
   if (optional(tIS))
238✔
5558
      p_incomplete_type_definition(type);
68✔
5559
   else
5560
      type_set_subkind(type, GTYPE_PRIVATE);
170✔
5561

5562
   tree_t d = tree_new(kind);
238✔
5563
   tree_set_ident(d, id);
238✔
5564
   tree_set_loc(d, CURRENT_LOC);
238✔
5565
   tree_set_type(d, type);
238✔
5566
   tree_set_class(d, C_TYPE);
238✔
5567
   tree_set_subkind(d, PORT_IN);
238✔
5568

5569
   add_interface(parent, d, kind);
238✔
5570
   sem_check(d, nametab);
238✔
5571

5572
   // Type generics are immediately visible
5573
   insert_name(nametab, d, NULL);
238✔
5574

5575
   // LRM 08 section 6.5.3: the predefined equality and inequality
5576
   // operators are implicitly declared as formal generic subprograms
5577
   // immediately following the interface type declaration in the
5578
   // enclosing interface list
5579

5580
   if (kind == T_GENERIC_DECL)
238✔
5581
      declare_generic_ops(parent, type);
236✔
5582
}
238✔
5583

5584
static void p_formal_parameter_list(tree_t decl, type_t type)
15,282✔
5585
{
5586
   // interface_list
5587

5588
   BEGIN("formal parameter list");
30,563✔
5589

5590
   p_interface_list(decl, T_PARAM_DECL, standard() >= STD_19);
15,282✔
5591

5592
   const int nports = tree_ports(decl);
15,282✔
5593
   if (nports == 0)
15,282✔
5594
      return;   // Was parse error
1✔
5595

5596
   for (int i = 0; i < nports; i++) {
46,211✔
5597
      tree_t p = tree_port(decl, i);
30,930✔
5598
      if (i == 0 && tree_has_value(p))
30,930✔
5599
         tree_set_flag(decl, TREE_F_CALL_NO_ARGS);
252✔
5600
      if (tree_has_type(p))
30,930✔
5601
         type_add_param(type, tree_type(p));
30,929✔
5602
      else
5603
         type_add_param(type, type_new(T_NONE));   // Will raise error later
1✔
5604
   }
5605
}
5606

5607
static tree_t p_interface_function_specification(void)
178✔
5608
{
5609
   // [ pure | impure ] function designator
5610
   //    [ [ parameter ] ( formal_parameter_list ) ] return type_mark
5611

5612
   // 2019:
5613
   // [ pure | impure ] function designator
5614
   //    [ [ parameter ] ( formal_parameter_list ) ]
5615
   //    return [ return_identifier of ] type_mark
5616

5617
   BEGIN("interface function specification");
178✔
5618

5619
   bool impure = false;
178✔
5620
   switch (peek()) {
178✔
5621
   case tPURE: consume(tPURE); break;
3✔
5622
   case tIMPURE: consume(tIMPURE); impure = true; break;
2✔
5623
   default: break;
5624
   }
5625

5626
   consume(tFUNCTION);
178✔
5627

5628
   ident_t id = p_designator();
178✔
5629

5630
   type_t type = type_new(T_SIGNATURE);
178✔
5631
   type_set_ident(type, id);
178✔
5632

5633
   tree_t d = tree_new(T_GENERIC_DECL);
178✔
5634
   tree_set_class(d, C_FUNCTION);
178✔
5635
   tree_set_ident(d, id);
178✔
5636
   tree_set_type(d, type);
178✔
5637
   tree_set_subkind(d, PORT_IN);
178✔
5638

5639
   if (impure)
178✔
5640
      tree_set_flag(d, TREE_F_IMPURE);
2✔
5641

5642
   if (optional(tLPAREN)) {
178✔
5643
      push_scope(nametab);
173✔
5644
      p_formal_parameter_list(d, type);
173✔
5645
      consume(tRPAREN);
173✔
5646
      pop_scope(nametab);
173✔
5647
   }
5648
   else
5649
      tree_set_flag(d, TREE_F_CALL_NO_ARGS);
5✔
5650

5651
   consume(tRETURN);
178✔
5652

5653
   if (peek_nth(2) != tOF)
178✔
5654
      type_set_result(type, p_type_mark());
178✔
5655
   else {
UNCOV
5656
      require_std(STD_19, "function knows return type");
×
UNCOV
5657
      ident_t id = p_identifier();
×
5658

UNCOV
5659
      consume(tOF);
×
5660

5661
      type_t sub = type_new(T_SUBTYPE);
×
UNCOV
5662
      type_set_ident(sub, id);
×
5663
      type_set_base(sub, p_type_mark());
×
5664

5665
      type_set_result(type, sub);
×
5666
   }
5667

5668
   tree_set_loc(d, CURRENT_LOC);
178✔
5669
   return d;
178✔
5670
}
5671

5672
static tree_t p_interface_procedure_specification(void)
9✔
5673
{
5674
   // procedure designator [ [ parameter ] ( formal_parameter_list ) ]
5675

5676
   BEGIN("interface procedure specification");
9✔
5677

5678
   consume(tPROCEDURE);
9✔
5679

5680
   ident_t id = p_designator();
9✔
5681

5682
   type_t type = type_new(T_SIGNATURE);
9✔
5683
   type_set_ident(type, id);
9✔
5684

5685
   tree_t d = tree_new(T_GENERIC_DECL);
9✔
5686
   tree_set_class(d, C_PROCEDURE);
9✔
5687
   tree_set_ident(d, id);
9✔
5688
   tree_set_type(d, type);
9✔
5689
   tree_set_subkind(d, PORT_IN);
9✔
5690

5691
   if (optional(tLPAREN)) {
9✔
5692
      push_scope(nametab);
9✔
5693
      p_formal_parameter_list(d, type);
9✔
5694
      consume(tRPAREN);
9✔
5695
      pop_scope(nametab);
9✔
5696
   }
5697
   else
UNCOV
5698
      tree_set_flag(d, TREE_F_CALL_NO_ARGS);
×
5699

5700
   tree_set_loc(d, CURRENT_LOC);
9✔
5701
   return d;
9✔
5702
}
5703

5704
static void p_interface_subprogram_declaration(tree_t parent, tree_kind_t kind)
187✔
5705
{
5706
   // interface_subprogram_specification [ is interface_subprogram_default ]
5707

5708
   BEGIN("interface subprogram declaration");
374✔
5709

5710
   tree_t d = NULL;
187✔
5711

5712
   require_std(STD_08, "interface subprogram declarations");
187✔
5713

5714
   switch (peek()) {
187✔
5715
   case tFUNCTION:
178✔
5716
   case tPURE:
5717
   case tIMPURE:
5718
      d = p_interface_function_specification();
178✔
5719
      break;
178✔
5720

5721
   case tPROCEDURE:
9✔
5722
      d = p_interface_procedure_specification();
9✔
5723
      break;
9✔
5724

UNCOV
5725
   default:
×
UNCOV
5726
      one_of(tFUNCTION, tPROCEDURE, tPURE, tIMPURE);
×
UNCOV
5727
      return;
×
5728
   }
5729

5730
   if (optional(tIS)) {
187✔
5731
      switch (peek()) {
139✔
5732
      case tID:
4✔
5733
         {
5734
            ident_t id = p_identifier();
4✔
5735
            type_t constraint = tree_type(d);
4✔
5736

5737
            tree_t decl = resolve_subprogram_name(nametab, &last_loc,
4✔
5738
                                                  id, constraint);
5739

5740
            tree_t box = tree_new(T_BOX);
4✔
5741
            tree_set_loc(box, &last_loc);
4✔
5742
            tree_set_type(box, constraint);
4✔
5743
            tree_set_ident(box, id);
4✔
5744
            tree_set_ref(box, decl);
4✔
5745

5746
            tree_set_value(d, box);
4✔
5747
         }
5748
         break;
4✔
5749
      case tBOX:
135✔
5750
         {
5751
            consume(tBOX);
135✔
5752

5753
            tree_t box = tree_new(T_BOX);
135✔
5754
            tree_set_loc(box, CURRENT_LOC);
135✔
5755
            tree_set_type(box, tree_type(d));
135✔
5756

5757
            tree_set_value(d, box);
135✔
5758
         }
5759
         break;
135✔
UNCOV
5760
      default:
×
UNCOV
5761
         expect(tID, tBOX);
×
5762
      }
5763
   }
5764

5765
   add_interface(parent, d, kind);
187✔
5766
   sem_check(d, nametab);
187✔
5767

5768
   insert_name(nametab, d, NULL);
187✔
5769
}
5770

5771
static void p_interface_package_generic_map_aspect(tree_t map, tree_t pack)
72✔
5772
{
5773
   // generic_map_aspect | generic map ( <> ) | generic map ( default )
5774

5775
   BEGIN("interface package generic map aspect");
144✔
5776

5777
   consume(tGENERIC);
72✔
5778
   consume(tMAP);
72✔
5779
   consume(tLPAREN);
72✔
5780

5781
   switch (peek()) {
72✔
5782
   case tBOX:
56✔
5783
      consume(tBOX);
56✔
5784
      tree_set_subkind(map, PACKAGE_MAP_BOX);
56✔
5785
      break;
56✔
5786

5787
   case tDEFAULT:
6✔
5788
      consume(tDEFAULT);
6✔
5789
      tree_set_subkind(map, PACKAGE_MAP_DEFAULT);
6✔
5790
      break;
6✔
5791

5792
   default:
10✔
5793
      tree_set_subkind(map, PACKAGE_MAP_MATCHING);
10✔
5794
      p_association_list(map, pack, F_GENERIC_MAP);
10✔
5795
      break;
10✔
5796
   }
5797

5798
   consume(tRPAREN);
72✔
5799

5800
   tree_set_loc(map, CURRENT_LOC);
72✔
5801
}
72✔
5802

5803
static void p_interface_package_declaration(tree_t parent, tree_kind_t kind)
72✔
5804
{
5805
   // package identifier is new uninstantiated_package_name
5806
   //    interface_package_generic_map_aspect
5807

5808
   BEGIN("interface package declaration");
144✔
5809

5810
   consume(tPACKAGE);
72✔
5811

5812
   require_std(STD_08, "interface package declarations");
72✔
5813

5814
   tree_t d = tree_new(T_GENERIC_DECL);
72✔
5815
   tree_set_class(d, C_PACKAGE);
72✔
5816
   tree_set_ident(d, p_identifier());
72✔
5817
   tree_set_subkind(d, PORT_IN);
72✔
5818

5819
   consume(tIS);
72✔
5820
   consume(tNEW);
72✔
5821

5822
   ident_t unit_name = p_selected_identifier();
72✔
5823

5824
   tree_t pack = resolve_name(nametab, CURRENT_LOC, unit_name);
72✔
5825
   if (pack != NULL && !is_uninstantiated_package(pack)) {
72✔
5826
      parse_error(CURRENT_LOC, "unit %s is not an uninstantiated package",
1✔
5827
                  istr(unit_name));
5828
      pack = NULL;
5829
   }
5830

5831
   tree_t map = tree_new(T_PACKAGE_MAP);
72✔
5832
   tree_set_ident(map, unit_name);
72✔
5833
   tree_set_loc(map, CURRENT_LOC);
72✔
5834
   tree_set_ref(map, pack);
72✔
5835

5836
   tree_set_value(d, map);
72✔
5837

5838
   p_interface_package_generic_map_aspect(map, pack);
72✔
5839

5840
   tree_set_loc(d, CURRENT_LOC);
72✔
5841

5842
   add_interface(parent, d, kind);
72✔
5843
   sem_check(d, nametab);
72✔
5844

5845
   insert_name(nametab, d, NULL);
72✔
5846
}
72✔
5847

5848
static void p_interface_declaration(tree_t parent, tree_kind_t kind,
33,702✔
5849
                                    bool ordered)
5850
{
5851
   // interface_constant_declaration | interface_signal_declaration
5852
   //   | interface_variable_declaration | interface_file_declaration
5853
   //   | 2008: interface_type_declaration
5854
   //   | 2008: interface_subprogram_declaration
5855
   //   | 2008: interface_package_declaration
5856

5857
   BEGIN("interface declaration");
67,404✔
5858

5859
   switch (peek()) {
33,702✔
5860
   case tCONSTANT:
5,198✔
5861
      p_interface_constant_declaration(parent, kind, ordered);
5,198✔
5862
      break;
5,198✔
5863

5864
   case tSIGNAL:
1,128✔
5865
      p_interface_signal_declaration(parent, kind, ordered);
1,128✔
5866
      break;
1,128✔
5867

5868
   case tVARIABLE:
805✔
5869
      p_interface_variable_declaration(parent, kind);
805✔
5870
      break;
805✔
5871

5872
   case tFILE:
29✔
5873
      p_interface_file_declaration(parent, kind);
29✔
5874
      break;
29✔
5875

5876
   case tTYPE:
238✔
5877
      p_interface_type_declaration(parent, kind);
238✔
5878
      break;
238✔
5879

5880
   case tFUNCTION:
187✔
5881
   case tPROCEDURE:
5882
   case tPURE:
5883
   case tIMPURE:
5884
      p_interface_subprogram_declaration(parent, kind);
187✔
5885
      break;
187✔
5886

5887
   case tPACKAGE:
72✔
5888
      p_interface_package_declaration(parent, kind);
72✔
5889
      break;
72✔
5890

5891
   case tID:
26,045✔
5892
      if (kind == T_PORT_DECL)
26,045✔
5893
         p_interface_signal_declaration(parent, kind, ordered);
3,518✔
5894
      else
5895
         p_interface_constant_declaration(parent, kind, ordered);
22,527✔
5896
      break;
5897

UNCOV
5898
   default:
×
UNCOV
5899
      expect(tCONSTANT, tSIGNAL, tVARIABLE, tFILE, tID, tTYPE,
×
5900
             STD(08, tFUNCTION), tPROCEDURE, tPURE, tIMPURE, tPACKAGE);
5901
   }
5902
}
33,702✔
5903

5904
static void p_interface_element(tree_t parent, tree_kind_t kind, bool ordered)
33,702✔
5905
{
5906
   // interface_declaration
5907

5908
   BEGIN("interface element");
67,404✔
5909

5910
   p_interface_declaration(parent, kind, ordered);
33,702✔
5911
}
33,702✔
5912

5913
static void p_interface_list(tree_t parent, tree_kind_t kind, bool ordered)
17,530✔
5914
{
5915
   // interface_element { ; interface_element }
5916

5917
   BEGIN("interface list");
35,059✔
5918

5919
   if (peek() == tRPAREN) {
17,530✔
5920
      parse_error(&last_loc, "interface list cannot be empty");
1✔
5921
      return;
1✔
5922
   }
5923

5924
   if (ordered)
17,529✔
5925
      push_scope(nametab);
4,106✔
5926

5927
   p_interface_element(parent, kind, ordered);
17,529✔
5928

5929
   while (optional(tSEMI)) {
33,702✔
5930
      if (peek() == tRPAREN) {
16,180✔
5931
         require_std(STD_19, "optional trailing semicolons on interface lists");
7✔
5932
         break;
7✔
5933
      }
5934
      p_interface_element(parent, kind, ordered);
16,173✔
5935
   }
5936

5937
   if (ordered)
17,529✔
5938
      pop_scope(nametab);
4,106✔
5939
}
5940

5941
static void p_port_list(tree_t parent)
1,236✔
5942
{
5943
   // port_list ::= interface_list
5944

5945
   BEGIN("port list");
2,472✔
5946

5947
   p_interface_list(parent, T_PORT_DECL, standard() >= STD_19);
1,236✔
5948
}
1,236✔
5949

5950
static void p_port_clause(tree_t parent)
1,236✔
5951
{
5952
   // port ( port_list ) ;
5953

5954
   BEGIN("port clause");
2,472✔
5955

5956
   consume(tPORT);
1,236✔
5957
   consume(tLPAREN);
1,236✔
5958

5959
   p_port_list(parent);
1,236✔
5960

5961
   consume(tRPAREN);
1,236✔
5962
   consume(tSEMI);
1,236✔
5963
}
1,236✔
5964

5965
static void p_generic_list(tree_t parent)
1,012✔
5966
{
5967
   // generic_list ::= interface_list
5968

5969
   BEGIN("generic list");
2,024✔
5970

5971
   p_interface_list(parent, T_GENERIC_DECL, standard() >= STD_08);
1,012✔
5972
}
1,012✔
5973

5974
static void p_generic_clause(tree_t parent)
921✔
5975
{
5976
   // generic ( generic_list ) ;
5977

5978
   BEGIN("generic clause");
1,842✔
5979

5980
   consume(tGENERIC);
921✔
5981
   consume(tLPAREN);
921✔
5982

5983
   p_generic_list(parent);
921✔
5984

5985
   consume(tRPAREN);
921✔
5986
   consume(tSEMI);
921✔
5987
}
921✔
5988

5989
static void p_entity_header(tree_t entity)
4,952✔
5990
{
5991
   // [ generic_clause ] [ port_clause ]
5992

5993
   BEGIN("entity header");
9,904✔
5994

5995
   if (scan(tGENERIC)) {
4,952✔
5996
      p_generic_clause(entity);
540✔
5997
      insert_generics(nametab, entity);
540✔
5998
   }
5999

6000
   if (scan(tPORT)) {
4,952✔
6001
      p_port_clause(entity);
870✔
6002
      insert_ports(nametab, entity);
870✔
6003
   }
6004
}
4,952✔
6005

6006
static tree_t p_attribute_declaration(void)
118✔
6007
{
6008
   // attribute identifier : type_mark ;
6009

6010
   BEGIN("attribute declaration");
118✔
6011

6012
   tree_t t = tree_new(T_ATTR_DECL);
118✔
6013

6014
   consume(tATTRIBUTE);
118✔
6015
   tree_set_ident(t, p_identifier());
118✔
6016
   consume(tCOLON);
118✔
6017
   tree_set_type(t, p_type_mark());
118✔
6018
   consume(tSEMI);
118✔
6019

6020
   tree_set_loc(t, CURRENT_LOC);
118✔
6021
   insert_name(nametab, t, NULL);
118✔
6022
   sem_check(t, nametab);
118✔
6023
   return t;
118✔
6024
}
6025

6026
static class_t p_entity_class(void)
341✔
6027
{
6028
   // entity | procedure | type | signal | label | group | architecture
6029
   //   | function | subtype | variable | literal | file | configuration
6030
   //   | package | constant | component | units
6031

6032
   BEGIN("entity class");
682✔
6033

6034
   switch (one_of(tENTITY, tPROCEDURE, tTYPE, tSIGNAL, tLABEL, tGROUP,
341✔
6035
                  tARCHITECTURE, tFUNCTION, tSUBTYPE, tVARIABLE, tLITERAL,
6036
                  tFILE, tCONFIGURATION, tPACKAGE, tCONSTANT, tCOMPONENT,
6037
                  tUNITS)) {
6038
   case tENTITY:
6039
      return C_ENTITY;
6040
   case tPROCEDURE:
6041
      return C_PROCEDURE;
6042
   case tTYPE:
6043
      return C_TYPE;
6044
   case tSIGNAL:
6045
      return C_SIGNAL;
6046
   case tLABEL:
6047
      return C_LABEL;
6048
   case tGROUP:
6049
      return C_DEFAULT;
6050
   case tARCHITECTURE:
6051
      return C_ARCHITECTURE;
6052
   case tFUNCTION:
6053
      return C_FUNCTION;
6054
   case tSUBTYPE:
6055
      return C_SUBTYPE;
6056
   case tVARIABLE:
6057
      return C_VARIABLE;
6058
   case tLITERAL:
6059
      return C_LITERAL;
6060
   case tFILE:
6061
      return C_FILE;
6062
   case tCONFIGURATION:
6063
      return C_CONFIGURATION;
6064
   case tPACKAGE:
6065
      return C_PACKAGE;
6066
   case tCONSTANT:
6067
      return C_CONSTANT;
6068
   case tCOMPONENT:
6069
      return C_COMPONENT;
6070
   case tUNITS:
6071
      return C_UNITS;
6072
   default:
6073
      return C_DEFAULT;
6074
   }
6075
}
6076

6077
static tree_t p_entity_designator(void)
336✔
6078
{
6079
   // entity_tag [ signature ]
6080

6081
   BEGIN("entity designator");
336✔
6082

6083
   ident_t id = p_designator();
336✔
6084

6085
   tree_t t = tree_new(T_ATTR_SPEC);
336✔
6086
   tree_set_loc(t, CURRENT_LOC);
336✔
6087
   tree_set_subkind(t, SPEC_EXACT);
336✔
6088
   tree_set_ident2(t, id);
336✔
6089

6090
   if (peek() == tLSQUARE) {
336✔
6091
      type_t signature = p_signature();
148✔
6092

6093
      tree_t d = resolve_subprogram_name(nametab, tree_loc(t), id, signature);
148✔
6094
      tree_set_ref(t, d);
148✔
6095
   }
6096

6097
   return t;
336✔
6098
}
6099

6100
static void p_entity_name_list(tree_list_t *list)
339✔
6101
{
6102
   // entity_designator { , entity_designator } | others | all
6103

6104
   BEGIN("entity name list");
678✔
6105

6106
   switch (peek()) {
339✔
6107
   case tOTHERS:
1✔
6108
      {
6109
         consume(tOTHERS);
1✔
6110

6111
         tree_t t = tree_new(T_ATTR_SPEC);
1✔
6112
         tree_set_loc(t, CURRENT_LOC);
1✔
6113
         tree_set_subkind(t, SPEC_OTHERS);
1✔
6114

6115
         APUSH(*list, t);
1✔
6116
      }
6117
      break;
1✔
6118
   case tALL:
2✔
6119
      {
6120
         consume(tALL);
2✔
6121

6122
         tree_t t = tree_new(T_ATTR_SPEC);
2✔
6123
         tree_set_loc(t, CURRENT_LOC);
2✔
6124
         tree_set_subkind(t, SPEC_ALL);
2✔
6125

6126
         APUSH(*list, t);
2✔
6127
      }
6128
      break;
2✔
UNCOV
6129
   default:
×
6130
      do {
336✔
6131
         APUSH(*list, p_entity_designator());
336✔
6132
      } while (optional(tCOMMA));
336✔
6133
   }
6134
}
339✔
6135

6136
static class_t p_entity_specification(tree_list_t *list)
339✔
6137
{
6138
   // entity_name_list : entity_class
6139

6140
   BEGIN("entity specification");
678✔
6141

6142
   p_entity_name_list(list);
339✔
6143

6144
   consume(tCOLON);
339✔
6145

6146
   return p_entity_class();
339✔
6147
}
6148

6149
static void p_attribute_specification(tree_t parent)
339✔
6150
{
6151
   // attribute attribute_designator of entity_specification is expression ;
6152

6153
   BEGIN("attribute specification");
339✔
6154

6155
   consume(tATTRIBUTE);
339✔
6156
   ident_t head = p_identifier();
339✔
6157

6158
   type_t type;
339✔
6159
   tree_t attr_decl = resolve_name(nametab, CURRENT_LOC, head);
339✔
6160
   if (attr_decl == NULL)
339✔
6161
      type = type_new(T_NONE);
2✔
6162
   else if (tree_kind(attr_decl) != T_ATTR_DECL) {
337✔
UNCOV
6163
      parse_error(CURRENT_LOC, "name %s is not an attribute declaration",
×
6164
                  istr(head));
UNCOV
6165
      type = type_new(T_NONE);
×
6166
   }
6167
   else
6168
      type = tree_type(attr_decl);
337✔
6169

6170
   consume(tOF);
339✔
6171

6172
   tree_list_t specs = AINIT;
339✔
6173
   class_t class = p_entity_specification(&specs);
339✔
6174

6175
   consume(tIS);
339✔
6176

6177
   tree_t value = p_expression();
339✔
6178
   solve_types(nametab, value, type);
339✔
6179

6180
   consume(tSEMI);
339✔
6181

6182
   const loc_t *loc = CURRENT_LOC;
339✔
6183

6184
   for (int i = 0; i < specs.count; i++) {
678✔
6185
      tree_t t = specs.items[i];
339✔
6186
      assert(tree_kind(t) == T_ATTR_SPEC);
339✔
6187

6188
      if (class == C_LITERAL || class == C_LABEL)
339✔
6189
         ;   // Cannot check name now
6190
      else if (tree_subkind(t) == SPEC_EXACT && !tree_has_ref(t))
323✔
6191
         tree_set_ref(t, resolve_name(nametab, loc, tree_ident2(t)));
177✔
6192

6193
      tree_set_class(t, class);
339✔
6194
      tree_set_ident(t, head);
339✔
6195
      tree_set_value(t, value);
339✔
6196
      tree_set_type(t, type);
339✔
6197

6198
      insert_name(nametab, t, NULL);
339✔
6199
      tree_add_decl(parent, t);
339✔
6200
      sem_check(t, nametab);
339✔
6201
   }
6202

6203
   ACLEAR(specs);
339✔
6204
}
339✔
6205

6206
static type_t p_integer_type_definition(tree_t r, ident_t id)
137✔
6207
{
6208
   // range_constraint
6209

6210
   EXTEND("integer type definition");
137✔
6211

6212
   type_t t = type_new(T_INTEGER);
137✔
6213
   type_add_dim(t, r);
137✔
6214
   type_set_ident(t, id);
137✔
6215
   mangle_type(nametab, t);
137✔
6216

6217
   return t;
137✔
6218
}
6219

6220
static type_t p_real_type_definition(tree_t r, ident_t id)
23✔
6221
{
6222
   // range_constraint
6223

6224
   EXTEND("real type definition");
23✔
6225

6226
   type_t t = type_new(T_REAL);
23✔
6227
   type_add_dim(t, r);
23✔
6228
   type_set_ident(t, id);
23✔
6229
   mangle_type(nametab, t);
23✔
6230

6231
   return t;
23✔
6232
}
6233

6234
static tree_t p_base_unit_declaration(void)
45✔
6235
{
6236
   // identifier ;
6237

6238
   BEGIN("base unit declaration");
45✔
6239

6240
   ident_t id = p_identifier();
45✔
6241
   consume(tSEMI);
45✔
6242

6243
   tree_t value = tree_new(T_LITERAL);
45✔
6244
   tree_set_loc(value, CURRENT_LOC);
45✔
6245
   tree_set_subkind(value, L_PHYSICAL);
45✔
6246
   tree_set_ident(value, id);
45✔
6247
   tree_set_ival(value, 1);
45✔
6248

6249
   tree_t t = tree_new(T_UNIT_DECL);
45✔
6250
   tree_set_loc(t, CURRENT_LOC);
45✔
6251
   tree_set_value(t, value);
45✔
6252
   tree_set_ident(t, id);
45✔
6253

6254
   return t;
45✔
6255
}
6256

6257
static tree_t p_secondary_unit_declaration(type_t type)
113✔
6258
{
6259
   // identifier = physical_literal ;
6260

6261
   BEGIN("secondary unit declaration");
113✔
6262

6263
   ident_t id = p_identifier();
113✔
6264
   consume(tEQ);
113✔
6265

6266
   tree_t value = p_physical_literal();
113✔
6267

6268
   consume(tSEMI);
113✔
6269

6270
   tree_t u = tree_new(T_UNIT_DECL);
113✔
6271
   tree_set_ident(u, id);
113✔
6272
   tree_set_value(u, value);
113✔
6273
   tree_set_loc(u, CURRENT_LOC);
113✔
6274
   tree_set_type(u, type);
113✔
6275

6276
   return u;
113✔
6277
}
6278

6279
static type_t p_physical_type_definition(tree_t range, ident_t id)
45✔
6280
{
6281
   // range_constraint units base_unit_declaration
6282
   //   { secondary_unit_declaration } end units [ name ]
6283

6284
   EXTEND("physical type definition");
45✔
6285

6286
   type_t t = type_new(T_PHYSICAL);
45✔
6287
   type_set_ident(t, id);
45✔
6288
   mangle_type(nametab, t);
45✔
6289

6290
   consume(tUNITS);
45✔
6291

6292
   tree_t base = p_base_unit_declaration();
45✔
6293
   tree_set_type(base, t);
45✔
6294
   tree_set_type(tree_value(base), t);
45✔
6295
   type_add_unit(t, base);
45✔
6296
   type_add_dim(t, range);
45✔
6297

6298
   push_scope(nametab);
45✔
6299

6300
   insert_name(nametab, base, NULL);
45✔
6301

6302
   while (scan(tINT, tREAL, tID)) {
158✔
6303
      tree_t unit = p_secondary_unit_declaration(t);
113✔
6304
      type_add_unit(t, unit);
113✔
6305
      insert_name(nametab, unit, NULL);
113✔
6306
   }
6307

6308
   pop_scope(nametab);
45✔
6309

6310
   consume(tEND);
45✔
6311
   consume(tUNITS);
45✔
6312

6313
   if (peek() == tID) {
45✔
6314
      ident_t trailing = p_identifier();
1✔
6315
      if (trailing != id)
1✔
6316
         parse_error(&yylloc, "expected physical type definition trailing "
1✔
6317
                     "identifier to match %s", istr(id));
6318
   }
6319

6320
   return t;
45✔
6321
}
6322

6323
static tree_t p_enumeration_literal(void)
4,608✔
6324
{
6325
   // identifier | character_literal
6326

6327
   BEGIN("enumeration literal");
4,608✔
6328

6329
   tree_t t = tree_new(T_ENUM_LIT);
4,608✔
6330
   tree_set_ident(t, p_identifier());
4,608✔
6331
   tree_set_loc(t, CURRENT_LOC);
4,608✔
6332

6333
   return t;
4,608✔
6334
}
6335

6336
static type_t p_enumeration_type_definition(ident_t id)
453✔
6337
{
6338
   // ( enumeration_literal { , enumeration_literal } )
6339

6340
   BEGIN("enumeration type definition");
453✔
6341

6342
   type_t t = type_new(T_ENUM);
453✔
6343
   type_set_ident(t, id);
453✔
6344
   mangle_type(nametab, t);
453✔
6345

6346
   consume(tLPAREN);
453✔
6347

6348
   unsigned pos = 0;
453✔
6349
   do {
4,608✔
6350
      tree_t lit = p_enumeration_literal();
4,608✔
6351
      tree_set_pos(lit, pos++);
4,608✔
6352
      tree_set_type(lit, t);
4,608✔
6353
      type_enum_add_literal(t, lit);
4,608✔
6354
   } while (optional(tCOMMA));
4,608✔
6355

6356
   tree_t r = tree_new(T_RANGE);
453✔
6357
   tree_set_subkind(r, RANGE_TO);
453✔
6358
   tree_set_left(r, make_ref(type_enum_literal(t, 0)));
453✔
6359
   tree_set_right(r, make_ref(type_enum_literal(t, pos - 1)));
453✔
6360
   tree_set_loc(r, CURRENT_LOC);
453✔
6361
   tree_set_type(r, t);
453✔
6362

6363
   type_add_dim(t, r);
453✔
6364

6365
   consume(tRPAREN);
453✔
6366

6367
   return t;
453✔
6368
}
6369

6370
static type_t p_scalar_type_definition(ident_t id)
658✔
6371
{
6372
   // enumeration_type_definition | integer_type_definition
6373
   //   | floating_type_definition | physical_type_definition
6374

6375
   BEGIN("scalar type definition");
1,316✔
6376

6377
   switch (peek()) {
658✔
6378
   case tRANGE:
205✔
6379
      {
6380
         tree_t r = tree_range(p_range_constraint(NULL), 0);
205✔
6381

6382
         if (peek() == tUNITS)
205✔
6383
            return p_physical_type_definition(r, id);
45✔
6384
         else if (type_is_real(tree_type(r)))
160✔
6385
            return p_real_type_definition(r, id);
23✔
6386
         else
6387
            return p_integer_type_definition(r, id);
137✔
6388
      }
6389

6390
   case tLPAREN:
453✔
6391
      return p_enumeration_type_definition(id);
453✔
6392

UNCOV
6393
   default:
×
UNCOV
6394
      one_of(tRANGE, tLPAREN);
×
UNCOV
6395
      return type_new(T_NONE);
×
6396
   }
6397
}
6398

6399
static type_t p_access_type_definition(ident_t id)
314✔
6400
{
6401
   // access subtype_indication
6402

6403
   BEGIN("access type definition");
314✔
6404

6405
   consume(tACCESS);
314✔
6406

6407
   type_t t = type_new(T_ACCESS);
314✔
6408
   type_set_ident(t, id);
314✔
6409
   type_set_designated(t, p_subtype_indication());
314✔
6410
   mangle_type(nametab, t);
314✔
6411

6412
   return t;
314✔
6413
}
6414

6415
static type_t p_file_type_definition(ident_t id)
98✔
6416
{
6417
   // file of type_mark
6418

6419
   BEGIN("file type definition");
98✔
6420

6421
   consume(tFILE);
98✔
6422
   consume(tOF);
98✔
6423

6424
   type_t t = type_new(T_FILE);
98✔
6425
   type_set_ident(t, id);
98✔
6426
   type_set_designated(t, p_type_mark());
98✔
6427
   mangle_type(nametab, t);
98✔
6428

6429
   return t;
98✔
6430
}
6431

6432
static void p_element_declaration(type_t rec)
3,403✔
6433
{
6434
   // identifier_list : element_subtype_definition ;
6435

6436
   BEGIN("element declaration");
6,806✔
6437

6438
   LOCAL_IDENT_LIST ids = p_identifier_list();
6,806✔
6439

6440
   consume(tCOLON);
3,403✔
6441

6442
   type_t type = p_subtype_indication();
3,403✔
6443

6444
   consume(tSEMI);
3,403✔
6445

6446
   int pos = type_fields(rec);
3,403✔
6447
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
7,088✔
6448
      tree_t f = tree_new(T_FIELD_DECL);
3,685✔
6449
      tree_set_ident(f, it->ident);
3,685✔
6450
      tree_set_type(f, type);
3,685✔
6451
      tree_set_pos(f, pos++);
3,685✔
6452
      tree_set_loc(f, &(it->loc));
3,685✔
6453

6454
      type_add_field(rec, f);
3,685✔
6455
   }
6456
}
3,403✔
6457

6458
static type_t p_record_type_definition(ident_t id)
1,219✔
6459
{
6460
   // record element_declaration { element_declaration } end record
6461
   //   [ simple_name ]
6462

6463
   // 2019: record { element_declaration } end record [ simple_name ]
6464
   BEGIN("record type definition");
1,219✔
6465

6466
   consume(tRECORD);
1,219✔
6467

6468
   type_t r = type_new(T_RECORD);
1,219✔
6469
   type_set_ident(r, id);
1,219✔
6470
   mangle_type(nametab, r);
1,219✔
6471

6472
   if (peek() == tEND)
1,219✔
6473
      require_std(STD_19, "empty record");
3✔
6474
   else {
6475
      do {
3,403✔
6476
         p_element_declaration(r);
3,403✔
6477
      } while (peek() == tID);
3,403✔
6478
   }
6479

6480
   consume(tEND);
1,219✔
6481
   consume(tRECORD);
1,219✔
6482

6483
   if (peek() == tID) {
1,219✔
6484
      ident_t trailing = p_identifier();
37✔
6485
      if (trailing != id)
37✔
6486
         parse_error(&yylloc, "expected record type definition trailing "
1✔
6487
                     "identifier to match %s", istr(id));
6488
   }
6489

6490
   return r;
1,219✔
6491
}
6492

6493
static type_t p_index_subtype_definition(tree_t head)
1,912✔
6494
{
6495
   // type_mark range <>
6496

6497
   BEGIN_WITH_HEAD("index subtype definition", head);
1,912✔
6498

6499
   type_t type;
1,912✔
6500
   if (head != NULL)
1,912✔
6501
      type = name_to_type_mark(head);
1,745✔
6502
   else
6503
      type = p_type_mark();
167✔
6504

6505
   consume(tRANGE);
1,912✔
6506
   consume(tBOX);
1,912✔
6507

6508
   return type;
1,912✔
6509
}
6510

6511
static type_t p_unconstrained_array_definition(type_t base, tree_t head)
1,745✔
6512
{
6513
   // array ( index_subtype_definition { , index_subtype_definition } )
6514
   //   of subtype_indication
6515

6516
   EXTEND("unconstrained array definition");
1,745✔
6517

6518
   type_add_index(base, p_index_subtype_definition(head));
1,745✔
6519

6520
   while (optional(tCOMMA))
1,901✔
6521
      type_add_index(base, p_index_subtype_definition(NULL));
156✔
6522

6523
   mangle_type(nametab, base);
1,745✔
6524

6525
   consume(tRPAREN);
1,745✔
6526
   consume(tOF);
1,745✔
6527

6528
   type_set_elem(base, p_subtype_indication());
1,745✔
6529
   return base;
1,745✔
6530
}
6531

6532
static type_t p_constrained_array_definition(type_t base, tree_t head)
720✔
6533
{
6534
   // array index_constraint of element_subtype_indication
6535

6536
   EXTEND("constrained array definition");
720✔
6537

6538
   tree_t constraint = tree_new(T_CONSTRAINT);
720✔
6539
   tree_set_subkind(constraint, C_INDEX);
720✔
6540

6541
   type_t sub = type_new(T_SUBTYPE);
720✔
6542
   type_set_base(sub, base);
720✔
6543
   type_set_constraint(sub, constraint);
720✔
6544
   type_set_ident(sub, type_ident(base));
720✔
6545

6546
   mangle_type(nametab, sub);
720✔
6547

6548
   do {
878✔
6549
      tree_t r = p_discrete_range(head);
878✔
6550
      solve_types(nametab, r, NULL);
878✔
6551
      convert_universal_bounds(r);
878✔
6552

6553
      tree_add_range(constraint, r);
878✔
6554
      type_add_index(base, tree_type(r));
878✔
6555

6556
      head = NULL;
878✔
6557
   } while (optional(tCOMMA));
878✔
6558

6559
   consume(tRPAREN);
720✔
6560

6561
   tree_set_loc(constraint, CURRENT_LOC);
720✔
6562

6563
   consume(tOF);
720✔
6564

6565
   type_set_elem(base, p_subtype_indication());
720✔
6566
   return sub;
720✔
6567
}
6568

6569
static type_t p_array_type_definition(ident_t id)
2,465✔
6570
{
6571
   // unconstrained_array_definition | constrained_array_definition
6572

6573
   BEGIN("array type definition");
4,930✔
6574

6575
   type_t base = type_new(T_ARRAY);
2,465✔
6576
   type_set_ident(base, id);
2,465✔
6577

6578
   consume(tARRAY);
2,465✔
6579
   consume(tLPAREN);
2,465✔
6580

6581
   tree_t head = p_expression();
2,465✔
6582

6583
   if (peek_nth(2) == tBOX)
2,465✔
6584
      return p_unconstrained_array_definition(base, head);
1,745✔
6585
   else
6586
      return p_constrained_array_definition(base, head);
720✔
6587
}
6588

6589
static type_t p_composite_type_definition(ident_t id)
3,684✔
6590
{
6591
   // array_type_definition | record_type_definition
6592

6593
   BEGIN("composite type definition");
7,368✔
6594

6595
   switch (peek()) {
3,684✔
6596
   case tRECORD:
1,219✔
6597
      return p_record_type_definition(id);
1,219✔
6598

6599
   case tARRAY:
2,465✔
6600
      return p_array_type_definition(id);
2,465✔
6601

UNCOV
6602
   default:
×
UNCOV
6603
      expect(tRECORD, tARRAY);
×
UNCOV
6604
      return type_new(T_NONE);
×
6605
   }
6606
}
6607

6608
static void p_private_variable_declaration(tree_t decl)
9✔
6609
{
6610
   // 2019: private variable_declaration
6611

6612
   BEGIN("private variable declaration");
18✔
6613

6614
   consume(tPRIVATE);
9✔
6615

6616
   require_std(STD_19, "private variable declarations");
9✔
6617

6618
   p_variable_declaration(decl);
9✔
6619
}
9✔
6620

6621
static void p_protected_type_declarative_item(tree_t decl)
473✔
6622
{
6623
   // subprogram_declaration | 2008: subprogram_instantiation_declaration
6624
   //   | attribute_specification | use_clause
6625
   //   | 2019: private_variable_declaration | alias_declaration
6626

6627
   BEGIN("protected type declarative item");
946✔
6628

6629
   switch (peek()) {
473✔
UNCOV
6630
   case tATTRIBUTE:
×
UNCOV
6631
      p_attribute_specification(decl);
×
UNCOV
6632
      break;
×
6633

6634
   case tUSE:
×
6635
      p_use_clause(decl, tree_add_decl);
×
6636
      break;
×
6637

6638
   case tFUNCTION:
449✔
6639
   case tPROCEDURE:
6640
   case tIMPURE:
6641
   case tPURE:
6642
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
449✔
UNCOV
6643
         tree_add_decl(decl, p_subprogram_instantiation_declaration());
×
6644
      else {
6645
         tree_t spec = p_subprogram_specification();
449✔
6646
         tree_set_flag(spec, TREE_F_PROTECTED);
449✔
6647
         tree_add_decl(decl, p_subprogram_declaration(spec));
449✔
6648
      }
6649
      break;
6650

6651
   case tPRIVATE:
9✔
6652
      p_private_variable_declaration(decl);
9✔
6653
      break;
9✔
6654

6655
   case tALIAS:
15✔
6656
      p_alias_declaration(decl);
15✔
6657
      break;
15✔
6658

UNCOV
6659
   default:
×
UNCOV
6660
      expect(tATTRIBUTE, tUSE, STD(08, tFUNCTION), tPROCEDURE, tIMPURE, tPURE,
×
6661
             STD(19, tPRIVATE), tALIAS);
6662
   }
6663
}
473✔
6664

6665
static void p_protected_type_declarative_part(tree_t decl)
208✔
6666
{
6667
   // { protected_type_declarative_item }
6668

6669
   BEGIN("protected type declarative part");
416✔
6670

6671
   while (not_at_token(tEND))
681✔
6672
      p_protected_type_declarative_item(decl);
473✔
6673
}
208✔
6674

6675
static tree_t p_protected_type_declaration(ident_t id)
208✔
6676
{
6677
   // protected protected_type_declarative_part end protected [ simple_name ]
6678

6679
   BEGIN("protected type declaration");
208✔
6680

6681
   consume(tPROTECTED);
208✔
6682

6683
   type_t type = type_new(T_PROTECTED);
208✔
6684
   type_set_ident(type, id);
208✔
6685

6686
   mangle_type(nametab, type);
208✔
6687

6688
   tree_t t = tree_new(T_PROT_DECL);
208✔
6689
   tree_set_ident(t, id);
208✔
6690
   tree_set_type(t, type);
208✔
6691
   tree_set_loc(t, CURRENT_LOC);
208✔
6692

6693
   insert_name(nametab, t, NULL);
208✔
6694

6695
   push_scope(nametab);
208✔
6696
   scope_set_prefix(nametab, id);
208✔
6697

6698
   p_protected_type_declarative_part(t);
208✔
6699

6700
   const int ndecls = tree_decls(t);
208✔
6701
   for (int i = 0; i < ndecls; i++) {
681✔
6702
      tree_t d = tree_decl(t, i);
473✔
6703
      if (is_subprogram(d) || tree_kind(d) == T_ALIAS)
473✔
6704
         type_add_field(type, d);
464✔
6705
   }
6706

6707
   pop_scope(nametab);
208✔
6708

6709
   consume(tEND);
208✔
6710
   consume(tPROTECTED);
208✔
6711

6712
   p_trailing_label(id);
208✔
6713

6714
   tree_set_loc(t, CURRENT_LOC);
208✔
6715
   sem_check(t, nametab);
208✔
6716
   return t;
208✔
6717
}
6718

6719
static tree_t p_protected_type_definition(ident_t id)
412✔
6720
{
6721
   // protected_type_declaration | protected_type_body
6722

6723
   BEGIN("protected type definition");
824✔
6724

6725
   if (peek_nth(2) == tBODY)
412✔
6726
      return p_protected_type_body(id);
204✔
6727
   else
6728
      return p_protected_type_declaration(id);
208✔
6729
}
6730

6731
static type_t p_type_definition(tree_t tdecl)
4,757✔
6732
{
6733
   // scalar_type_definition | composite_type_definition
6734
   //   | access_type_definition | file_type_definition
6735
   //   | 2000: protected_type_definition
6736

6737
   BEGIN("type definition");
9,514✔
6738

6739
   ident_t id = tree_ident(tdecl);
4,757✔
6740

6741
   switch (peek()) {
4,757✔
6742
   case tRANGE:
658✔
6743
   case tLPAREN:
6744
      return p_scalar_type_definition(id);
658✔
6745

6746
   case tACCESS:
314✔
6747
      return p_access_type_definition(id);
314✔
6748

6749
   case tFILE:
98✔
6750
      return p_file_type_definition(id);
98✔
6751

6752
   case tRECORD:
3,684✔
6753
   case tARRAY:
6754
      return p_composite_type_definition(id);
3,684✔
6755

6756
   default:
3✔
6757
      expect(tRANGE, tACCESS, tFILE, tRECORD);
3✔
6758
      return type_new(T_NONE);
3✔
6759
   }
6760
}
6761

6762
static type_t p_full_type_declaration(tree_t tdecl)
4,757✔
6763
{
6764
   // type identifier is type_definition ;
6765

6766
   EXTEND("full type declaration");
4,757✔
6767

6768
   consume(tIS);
4,757✔
6769

6770
   type_t t = p_type_definition(tdecl);
4,757✔
6771

6772
   consume(tSEMI);
4,757✔
6773

6774
   return t;
4,757✔
6775
}
6776

6777
static type_t p_incomplete_type_declaration(ident_t id)
45✔
6778
{
6779
   // type identifier ;
6780

6781
   EXTEND("incomplete type declaration");
45✔
6782

6783
   consume(tSEMI);
45✔
6784

6785
   type_t t = type_new(T_INCOMPLETE);
45✔
6786
   type_set_ident(t, id);
45✔
6787
   mangle_type(nametab, t);
45✔
6788

6789
   return t;
45✔
6790
}
6791

6792
static void p_type_declaration(tree_t container)
5,214✔
6793
{
6794
   // full_type_declaration | incomplete_type_declaration
6795

6796
   BEGIN("type declaration");
10,428✔
6797

6798
   consume(tTYPE);
5,214✔
6799

6800
   ident_t id = p_identifier();
5,214✔
6801
   hide_name(nametab, id);
5,214✔
6802

6803
   // Protected type definitions are trees rather than types
6804
   if (peek_nth(1) == tIS && peek_nth(2) == tPROTECTED) {
5,214✔
6805
      consume(tIS);
412✔
6806
      tree_add_decl(container, p_protected_type_definition(id));
412✔
6807
      consume(tSEMI);
412✔
6808
   }
6809
   else {
6810
      // Insert univeral_integer and universal_real predefined functions
6811
      // before STD.INTEGER and STD.REAL respectively
6812
      if (bootstrapping) {
4,802✔
6813
         if (id == ident_new("INTEGER"))
43✔
6814
            make_universal_int(container);
3✔
6815
         else if (id == ident_new("REAL"))
40✔
6816
            make_universal_real(container);
3✔
6817
      }
6818

6819
      tree_t t = tree_new(T_TYPE_DECL);
4,802✔
6820
      tree_set_ident(t, id);
4,802✔
6821
      tree_set_loc(t, CURRENT_LOC);
4,802✔
6822

6823
      type_t type;
4,802✔
6824
      if (peek() == tSEMI)
4,802✔
6825
         type = p_incomplete_type_declaration(id);
45✔
6826
      else
6827
         type = p_full_type_declaration(t);
4,757✔
6828

6829
      tree_set_type(t, type);
4,802✔
6830
      tree_set_loc(t, CURRENT_LOC);
4,802✔
6831

6832
      insert_name(nametab, t, id);
4,802✔
6833
      tree_add_decl(container, t);
4,802✔
6834

6835
      const type_kind_t kind = type_kind(type);
4,802✔
6836

6837
      type_t base = type;
4,802✔
6838
      if (kind == T_SUBTYPE) {
4,802✔
6839
         base = type_base(type);
720✔
6840
         assert(type_kind(base) == T_ARRAY);
720✔
6841
         mangle_type(nametab, base);
720✔
6842
      }
6843

6844
      if (kind != T_INCOMPLETE)
4,802✔
6845
         declare_predefined_ops(container, base);
4,757✔
6846

6847
      if (kind == T_PHYSICAL) {
4,802✔
6848
         const int nunits = type_units(type);
45✔
6849
         for (int i = 0; i < nunits; i++)
203✔
6850
            solve_types(nametab, tree_value(type_unit(type, i)), type);
158✔
6851
      }
6852

6853
      sem_check(t, nametab);
4,802✔
6854
   }
6855
}
5,214✔
6856

6857
static tree_t p_subtype_declaration(void)
1,442✔
6858
{
6859
   // subtype identifier is subtype_indication ;
6860

6861
   BEGIN("subtype declaration");
1,442✔
6862

6863
   consume(tSUBTYPE);
1,442✔
6864

6865
   ident_t id = p_identifier();
1,442✔
6866
   hide_name(nametab, id);
1,442✔
6867

6868
   consume(tIS);
1,442✔
6869

6870
   type_t sub = p_subtype_indication();
1,442✔
6871

6872
   consume(tSEMI);
1,442✔
6873

6874
   if (type_kind(sub) != T_SUBTYPE || type_has_ident(sub)) {
1,442✔
6875
      // Case where subtype_indication did not impose any
6876
      // constraint so we must create the subtype object here
6877
      type_t new = type_new(T_SUBTYPE);
138✔
6878
      type_set_base(new, sub);
138✔
6879

6880
      sub = new;
138✔
6881
   }
6882
   type_set_ident(sub, id);
1,442✔
6883

6884
   mangle_type(nametab, sub);
1,442✔
6885

6886
   tree_t t = tree_new(T_SUBTYPE_DECL);
1,442✔
6887
   tree_set_ident(t, id);
1,442✔
6888
   tree_set_type(t, sub);
1,442✔
6889
   tree_set_loc(t, CURRENT_LOC);
1,442✔
6890

6891
   insert_name(nametab, t, id);
1,442✔
6892
   sem_check(t, nametab);
1,442✔
6893
   return t;
1,442✔
6894
}
6895

6896
static tree_t p_conditional_expression(void)
9,899✔
6897
{
6898
   // expression { when condition else expression }
6899

6900
   BEGIN("conditional expression");
19,798✔
6901

6902
   tree_t expr0 = p_expression();
9,899✔
6903

6904
   if (optional(tWHEN)) {
9,899✔
6905
      require_std(STD_19, "conditional expressions");
15✔
6906

6907
      tree_t value = tree_new(T_COND_VALUE);
15✔
6908

6909
      do {
18✔
6910
         tree_t cond = tree_new(T_COND_EXPR);
18✔
6911
         tree_set_result(cond, expr0);
18✔
6912
         tree_set_value(cond, p_condition());
18✔
6913

6914
         tree_set_loc(cond, CURRENT_LOC);
18✔
6915

6916
         tree_add_cond(value, cond);
18✔
6917

6918
         consume(tELSE);
18✔
6919

6920
         expr0 = p_expression();
18✔
6921
      } while (optional(tWHEN));
18✔
6922

6923
      tree_t last = tree_new(T_COND_EXPR);
15✔
6924
      tree_set_result(last, expr0);
15✔
6925
      tree_set_loc(last, CURRENT_LOC);
15✔
6926

6927
      tree_add_cond(value, last);
15✔
6928

6929
      tree_set_loc(value, CURRENT_LOC);
15✔
6930
      return value;
15✔
6931
   }
6932
   else
6933
      return expr0;
6934
}
6935

6936
static tree_t p_expression_or_unaffected(void)
29,150✔
6937
{
6938
   // expression | unaffected
6939

6940
   BEGIN("expression or unaffected");
58,300✔
6941

6942
   if (optional(tUNAFFECTED)) {
29,150✔
6943
      require_std(STD_19, "unaffected in conditional expression");
10✔
6944
      return NULL;
10✔
6945
   }
6946
   else
6947
      return p_expression();
29,140✔
6948
}
6949

6950
static tree_t p_conditional_or_unaffected_expression(vhdl_standard_t minstd)
29,109✔
6951
{
6952
   // expression_or_unaffected { when condition else expression_or_unaffected }
6953
   //   [ when condition ]
6954

6955
   BEGIN("conditional expression");
58,218✔
6956

6957
   tree_t expr0 = p_expression_or_unaffected();
29,109✔
6958

6959
   if (optional(tWHEN)) {
29,109✔
6960
      require_std(minstd, "conditional expressions");
30✔
6961

6962
      tree_t value = tree_new(T_COND_VALUE);
30✔
6963

6964
      tree_t c0 = tree_new(T_COND_EXPR);
30✔
6965
      tree_set_result(c0, expr0);
30✔
6966
      tree_set_value(c0, p_condition());
30✔
6967
      tree_set_loc(c0, CURRENT_LOC);
30✔
6968

6969
      tree_add_cond(value, c0);
30✔
6970

6971
      while (optional(tELSE)) {
46✔
6972
         tree_t cond = tree_new(T_COND_EXPR);
41✔
6973
         tree_set_result(cond, p_expression_or_unaffected());
41✔
6974
         tree_set_loc(cond, CURRENT_LOC);
41✔
6975

6976
         tree_add_cond(value, cond);
41✔
6977

6978
         if (!optional(tWHEN))
41✔
6979
            break;
6980

6981
         tree_set_value(cond, p_condition());
16✔
6982
         tree_set_loc(cond, CURRENT_LOC);
16✔
6983
      }
6984

6985
      tree_set_loc(value, CURRENT_LOC);
30✔
6986
      return value;
30✔
6987
   }
6988
   else
6989
      return expr0;
6990
}
6991

6992
static void p_constant_declaration(tree_t parent)
6,665✔
6993
{
6994
   // constant identifier_list : subtype_indication [ := expression ] ;
6995

6996
   BEGIN("constant declaration");
13,330✔
6997

6998
   consume(tCONSTANT);
6,665✔
6999

7000
   LOCAL_IDENT_LIST ids = p_identifier_list();
13,330✔
7001

7002
   for (ident_list_t *it = ids; it != NULL; it = it->next)
13,334✔
7003
      hide_name(nametab, it->ident);
6,669✔
7004

7005
   consume(tCOLON);
6,665✔
7006

7007
   type_t type = p_subtype_indication();
6,665✔
7008

7009
   tree_t init = NULL;
6,665✔
7010
   if (optional(tWALRUS)) {
6,665✔
7011
      init = p_conditional_expression();
6,247✔
7012

7013
      if (standard() < STD_19 || type_is_unconstrained(type))
6,247✔
7014
         solve_types(nametab, init, type);
5,609✔
7015
      else
7016
         solve_known_subtype(nametab, init, type);
638✔
7017
   }
7018

7019
   // According to the LRM all constants are globally static but that
7020
   // leads to a number of logical inconsistencies so we retrict to only
7021
   // constants in certain regions where the value can be computed
7022
   // during elaboration
7023
   tree_flags_t flags = 0;
6,665✔
7024
   switch (tree_kind(parent)) {
6,665✔
7025
   case T_ENTITY:
4,127✔
7026
   case T_ARCH:
7027
   case T_PACKAGE:
7028
   case T_PACK_BODY:
7029
   case T_BLOCK:
7030
   case T_FOR_GENERATE:
7031
   case T_COND_STMT:     // If-generate
7032
   case T_ALTERNATIVE:   // For-generate
7033
      flags |= TREE_F_GLOBALLY_STATIC;
4,127✔
7034
      break;
4,127✔
7035
   default:
7036
      break;
7037
   }
7038

7039
   consume(tSEMI);
6,665✔
7040

7041
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
13,334✔
7042
      tree_t t = tree_new(T_CONST_DECL);
6,669✔
7043
      tree_set_ident(t, it->ident);
6,669✔
7044
      tree_set_type(t, type);
6,669✔
7045
      tree_set_flag(t, flags);
6,669✔
7046
      tree_set_loc(t, &(it->loc));
6,669✔
7047
      if (init != NULL)
6,669✔
7048
         tree_set_value(t, init);
6,251✔
7049

7050
      tree_add_decl(parent, t);
6,669✔
7051

7052
      insert_name(nametab, t, it->ident);
6,669✔
7053
      sem_check(t, nametab);
6,669✔
7054
   }
7055
}
6,665✔
7056

7057
static tree_t p_condition(void)
27,960✔
7058
{
7059
   BEGIN("condition");
27,960✔
7060

7061
   tree_t value = p_expression();
27,960✔
7062
   solve_condition(nametab, &value);
27,960✔
7063

7064
   return value;
27,960✔
7065
}
7066

7067
static tree_t p_assertion(void)
17,249✔
7068
{
7069
   // assert condition [ report expression ] [ severity expression ]
7070

7071
   BEGIN("assertion");
17,249✔
7072

7073
   tree_t s = tree_new(T_ASSERT);
17,249✔
7074

7075
   consume(tASSERT);
17,249✔
7076

7077
   tree_set_value(s, p_condition());
17,249✔
7078

7079
   if (optional(tREPORT)) {
17,249✔
7080
      tree_t message = p_expression();
6,302✔
7081
      solve_types(nametab, message, std_type(NULL, STD_STRING));
6,302✔
7082
      tree_set_message(s, message);
6,302✔
7083
   }
7084

7085
   if (optional(tSEVERITY)) {
17,249✔
7086
      tree_t severity = p_expression();
5,964✔
7087
      solve_types(nametab, severity, std_type(NULL, STD_SEVERITY_LEVEL));
5,964✔
7088
      tree_set_severity(s, severity);
5,964✔
7089
   }
7090

7091
   tree_set_loc(s, CURRENT_LOC);
17,249✔
7092
   return s;
17,249✔
7093
}
7094

7095
static tree_t p_concurrent_assertion_statement(ident_t label)
962✔
7096
{
7097
   // [ label : ] [ postponed ] assertion ;
7098

7099
   BEGIN("concurrent assertion statement");
962✔
7100

7101
   tree_t conc = tree_new(T_CONCURRENT);
962✔
7102

7103
   if (optional(tPOSTPONED))
962✔
7104
      tree_set_flag(conc, TREE_F_POSTPONED);
22✔
7105

7106
   tree_t s = p_assertion();
962✔
7107
   tree_add_stmt(conc, s);
962✔
7108

7109
   consume(tSEMI);
962✔
7110

7111
   tree_set_loc(s, CURRENT_LOC);
962✔
7112
   tree_set_loc(conc, CURRENT_LOC);
962✔
7113

7114
   ensure_labelled(conc, label);
962✔
7115

7116
   if (label) insert_name(nametab, conc, NULL);
962✔
7117
   sem_check(conc, nametab);
962✔
7118
   return conc;
962✔
7119
}
7120

7121
static ident_t p_designator(void)
25,533✔
7122
{
7123
   // identifier | operator_symbol
7124

7125
   BEGIN("designator");
51,066✔
7126

7127
   switch (peek()) {
25,533✔
7128
   case tID:
18,100✔
7129
      return p_identifier();
18,100✔
7130
   case tSTRING:
7,432✔
7131
      return p_operator_symbol();
7,432✔
7132
   default:
1✔
7133
      expect(tID, tSTRING);
1✔
7134
      return error_marker();
1✔
7135
   }
7136
}
7137

7138
static void p_subprogram_header(tree_t spec)
16,517✔
7139
{
7140
   // 2008: [ generic ( generic_list ) [ generic_map_aspect ] ]
7141

7142
   if (optional(tGENERIC)) {
16,517✔
7143
      require_std(STD_08, "generic subprograms");
91✔
7144

7145
      consume(tLPAREN);
91✔
7146
      p_generic_list(spec);
91✔
7147
      consume(tRPAREN);
91✔
7148

7149
      insert_generics(nametab, spec);
91✔
7150
   }
7151
}
16,517✔
7152

7153
static tree_t p_subprogram_specification(void)
16,517✔
7154
{
7155
   // procedure designator subprogram_header
7156
   //       [ [parameter] ( formal_parameter_list ) ]
7157
   //   | [ pure | impure ] function designator subprogram_header
7158
   //       [ [parameter] ( formal_parameter_list ) ] return type_mark
7159

7160
   // 2019:
7161
   //  [ pure | impure ] function designator subprogram_header
7162
   //       [ [parameter] ( formal_parameter_list ) ]
7163
   //       return [ return_identifier of ] type_mark
7164

7165
   BEGIN("subprogram specification");
33,034✔
7166

7167
   tree_t t = NULL;
16,517✔
7168
   type_t type = NULL;
16,517✔
7169

7170
   bool impure = false;
16,517✔
7171
   switch (one_of(tFUNCTION, tPROCEDURE, tPURE, tIMPURE)) {
16,517✔
7172
   case tIMPURE:
847✔
7173
      impure = true;
847✔
7174
      // Fall-through
7175
   case tPURE:
880✔
7176
      consume(tFUNCTION);
880✔
7177
      // Fall-through
7178
   case tFUNCTION:
13,299✔
7179
      t = tree_new(T_FUNC_DECL);
13,299✔
7180
      break;
13,299✔
7181

7182
   case tPROCEDURE:
3,218✔
7183
      t = tree_new(T_PROC_DECL);
3,218✔
7184
      break;
3,218✔
7185

UNCOV
7186
   default:
×
UNCOV
7187
      return tree_new(T_FUNC_DECL);
×
7188
   }
7189

7190
   type = type_new(T_SIGNATURE);
16,517✔
7191
   tree_set_type(t, type);
16,517✔
7192
   tree_set_ident(t, p_designator());
16,517✔
7193
   tree_set_subkind(t, S_USER);
16,517✔
7194

7195
   type_set_ident(type, tree_ident(t));
16,517✔
7196

7197
   if (impure)
16,517✔
7198
      tree_set_flag(t, TREE_F_IMPURE);
847✔
7199

7200
   push_scope(nametab);
16,517✔
7201

7202
   p_subprogram_header(t);
16,517✔
7203

7204
   bool has_param_list = false;
16,517✔
7205
   if (optional(tLPAREN))
16,517✔
7206
      has_param_list = true;
7207
   else if (standard() >= STD_08 && optional(tPARAMETER)) {
1,464✔
7208
      consume(tLPAREN);
47✔
7209
      has_param_list = true;
47✔
7210
   }
7211

7212
   if (has_param_list) {
47✔
7213
      p_formal_parameter_list(t, type);
15,100✔
7214
      consume(tRPAREN);
15,100✔
7215
   }
7216
   else
7217
      tree_set_flag(t, TREE_F_CALL_NO_ARGS);
1,417✔
7218

7219
   if (tree_kind(t) == T_FUNC_DECL) {
16,517✔
7220
      consume(tRETURN);
13,299✔
7221

7222
      if (peek_nth(2) != tOF)
13,299✔
7223
         type_set_result(type, p_type_mark());
13,282✔
7224
      else {
7225
         require_std(STD_19, "function knows return type");
17✔
7226

7227
         ident_t id = p_identifier();
17✔
7228

7229
         consume(tOF);
17✔
7230

7231
         type_t of = p_type_mark(), sub;
17✔
7232

7233
         if (type_is_unconstrained(of)) {
17✔
7234
            tree_t rref = tree_new(T_REF);
15✔
7235
            tree_set_loc(rref, CURRENT_LOC);
15✔
7236
            tree_set_ident(rref, ident_new("result"));
15✔
7237
            tree_set_ref(rref, t);
15✔
7238
            tree_set_type(rref, of);
15✔
7239

7240
            sub = get_subtype_for(rref);
15✔
7241
         }
7242
         else {
7243
            parse_error(CURRENT_LOC, "type mark of return identifier must "
2✔
7244
                        "denote an unconstrained type");
7245

7246
            sub = type_new(T_SUBTYPE);
2✔
7247
            type_set_base(sub, of);
2✔
7248
         }
7249

7250
         type_set_ident(sub, id);
17✔
7251
         type_set_result(type, sub);
17✔
7252

7253
         tree_set_flag(t, TREE_F_KNOWS_SUBTYPE);
17✔
7254
      }
7255
   }
7256

7257
   pop_scope(nametab);
16,517✔
7258

7259
   mangle_func(nametab, t);
16,517✔
7260

7261
   tree_set_loc(t, CURRENT_LOC);
16,517✔
7262
   return t;
16,517✔
7263
}
7264

7265
static tree_t p_subprogram_instantiation_declaration(void)
93✔
7266
{
7267
   // subprogram_kind designator is new uninstantiated_subprogram_name
7268
   //   [ signature ] [ generic_map_aspect ] ;
7269

7270
   tree_kind_t kind;
93✔
7271
   switch (one_of(tFUNCTION, tPROCEDURE)) {
93✔
7272
   case tFUNCTION: kind = T_FUNC_INST; break;
7273
   case tPROCEDURE: kind = T_PROC_INST; break;
35✔
7274
   default: return NULL;
7275
   }
7276

7277
   tree_t inst = tree_new(kind);
93✔
7278
   tree_set_ident(inst, p_designator());
93✔
7279

7280
   consume(tIS);
93✔
7281
   consume(tNEW);
93✔
7282

7283
   require_std(STD_08, "subprogram instantiation declarations");
93✔
7284

7285
   tree_t name = p_name(N_SUBPROGRAM | N_TYPE);
93✔
7286

7287
   type_t constraint = NULL;
93✔
7288
   if (peek() == tLSQUARE)
93✔
7289
      constraint = p_signature();
25✔
7290

7291
   tree_t decl = NULL;
93✔
7292
   if (tree_kind(name) != T_REF)
93✔
7293
      parse_error(CURRENT_LOC, "expecting uninstantiated subprogram name");
1✔
7294
   else {
7295
      decl = resolve_uninstantiated_subprogram(nametab, tree_loc(name),
92✔
7296
                                               tree_ident(name), constraint);
7297
   }
7298

7299
   if (decl != NULL) {
93✔
7300
      tree_t body = find_generic_subprogram_body(inst, decl);
86✔
7301
      instantiate_subprogram(inst, decl, body);
86✔
7302
   }
7303
   else {
7304
      // Create a dummy subprogram type to avoid later errors
7305
      type_t type = type_new(T_SIGNATURE);
7✔
7306
      type_set_ident(type, error_marker());
7✔
7307
      if (kind == T_FUNC_INST)
7✔
7308
         type_set_result(type, type_new(T_NONE));
3✔
7309

7310
      tree_set_type(inst, type);
7✔
7311
   }
7312

7313
   if (peek() == tGENERIC)
93✔
7314
      p_generic_map_aspect(inst, inst);
89✔
7315

7316
   consume(tSEMI);
93✔
7317

7318
   tree_set_loc(inst, CURRENT_LOC);
93✔
7319
   sem_check(inst, nametab);
93✔
7320

7321
   hash_t *map = get_generic_map(nametab);
93✔
7322
   if (map != NULL)
93✔
7323
      instance_fixup(inst, map);
89✔
7324

7325
   mangle_func(nametab, inst);
93✔
7326
   insert_name(nametab, inst, NULL);
93✔
7327
   return inst;
93✔
7328
}
7329

7330
static void p_variable_declaration(tree_t parent)
10,116✔
7331
{
7332
   // [ shared ] variable identifier_list : subtype_indication
7333
   //   [ := expression ] ;
7334

7335
   BEGIN("variable declaration");
20,232✔
7336

7337
   const bool shared = optional(tSHARED);
10,116✔
7338

7339
   consume(tVARIABLE);
10,116✔
7340

7341
   LOCAL_IDENT_LIST ids = p_identifier_list();
20,232✔
7342

7343
   for (ident_list_t *it = ids; it != NULL; it = it->next)
21,399✔
7344
      hide_name(nametab, it->ident);
11,283✔
7345

7346
   consume(tCOLON);
10,116✔
7347

7348
   type_t type = p_subtype_indication();
10,116✔
7349

7350
   tree_t init = NULL;
10,116✔
7351
   if (optional(tWALRUS)) {
10,116✔
7352
      init = p_conditional_expression();
2,011✔
7353
      solve_types(nametab, init, type);
2,011✔
7354
   }
7355

7356
   consume(tSEMI);
10,116✔
7357

7358
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
21,399✔
7359
      tree_t t = tree_new(T_VAR_DECL);
11,283✔
7360
      tree_set_loc(t, &(it->loc));
11,283✔
7361
      tree_set_ident(t, it->ident);
11,283✔
7362
      tree_set_type(t, type);
11,283✔
7363

7364
      if (init != NULL)
11,283✔
7365
         tree_set_value(t, init);
2,038✔
7366

7367
      if (shared)
11,283✔
7368
         tree_set_flag(t, TREE_F_SHARED);
179✔
7369

7370
      tree_add_decl(parent, t);
11,283✔
7371
      insert_name(nametab, t, it->ident);
11,283✔
7372
      sem_check(t, nametab);
11,283✔
7373
   }
7374
}
10,116✔
7375

7376
static tree_flags_t p_signal_kind(void)
5,332✔
7377
{
7378
   // register | bus
7379

7380
   switch (peek()) {
5,332✔
7381
   case tBUS:
24✔
7382
      consume(tBUS);
24✔
7383
      return TREE_F_BUS;
24✔
7384
   case tREGISTER:
3✔
7385
      consume(tREGISTER);
3✔
7386
      return TREE_F_REGISTER;
3✔
7387
   default:
7388
      return 0;
7389
   }
7390
}
7391

7392
static void p_signal_declaration(tree_t parent)
5,332✔
7393
{
7394
   // signal identifier_list : subtype_indication [ signal_kind ]
7395
   //   [ := expression ] ;
7396

7397
   BEGIN("signal declaration");
10,664✔
7398

7399
   consume(tSIGNAL);
5,332✔
7400

7401
   LOCAL_IDENT_LIST ids = p_identifier_list();
10,664✔
7402

7403
   for (ident_list_t *it = ids; it != NULL; it = it->next)
11,527✔
7404
      hide_name(nametab, it->ident);
6,195✔
7405

7406
   consume(tCOLON);
5,332✔
7407

7408
   type_t type = p_subtype_indication();
5,332✔
7409
   tree_flags_t flags = p_signal_kind();
5,332✔
7410

7411
   tree_t init = NULL;
5,332✔
7412
   if (optional(tWALRUS)) {
5,332✔
7413
      init = p_conditional_expression();
1,641✔
7414
      solve_known_subtype(nametab, init, type);
1,641✔
7415
   }
7416

7417
   consume(tSEMI);
5,332✔
7418

7419
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
11,527✔
7420
      tree_t t = tree_new(T_SIGNAL_DECL);
6,195✔
7421
      tree_set_loc(t, &(it->loc));
6,195✔
7422
      tree_set_ident(t, it->ident);
6,195✔
7423
      tree_set_type(t, type);
6,195✔
7424
      tree_set_value(t, init);
6,195✔
7425
      tree_set_flag(t, flags);
6,195✔
7426

7427
      tree_add_decl(parent, t);
6,195✔
7428

7429
      insert_name(nametab, t, it->ident);
6,195✔
7430
      sem_check(t, nametab);
6,195✔
7431
   }
7432
}
5,332✔
7433

7434
static type_t p_signature(void)
723✔
7435
{
7436
   // [ [ type_mark { , type_mark } ] [ return type_mark ] ]
7437

7438
   BEGIN("signature");
1,446✔
7439

7440
   type_t type = type_new(T_SIGNATURE);
723✔
7441

7442
   consume(tLSQUARE);
723✔
7443

7444
   bool error = false;
723✔
7445
   if (not_at_token(tRETURN, tRSQUARE)) {
723✔
7446
      do {
1,636✔
7447
         type_t param = p_type_mark();
1,636✔
7448
         type_add_param(type, param);
1,636✔
7449
         error = error || type_is_none(param);
1,636✔
7450
      } while (optional(tCOMMA));
1,636✔
7451
   }
7452

7453
   if (optional(tRETURN)) {
723✔
7454
      type_t ret = p_type_mark();
325✔
7455
      type_set_result(type, ret);
325✔
7456
      error = error || type_is_none(ret);
327✔
7457
   }
7458

7459
   consume(tRSQUARE);
723✔
7460

7461
   return error ? type_new(T_NONE) : type;
723✔
7462
}
7463

7464
static void p_alias_declaration(tree_t parent)
1,560✔
7465
{
7466
   // alias alias_designator [ : subtype_indication ] is name [ signature ] ;
7467

7468
   BEGIN("alias declaration");
3,120✔
7469

7470
   tree_t t = tree_new(T_ALIAS);
1,560✔
7471

7472
   consume(tALIAS);
1,560✔
7473

7474
   ident_t id = p_designator();
1,560✔
7475
   tree_set_ident(t, id);
1,560✔
7476

7477
   hide_name(nametab, id);
1,560✔
7478

7479
   bool has_subtype_indication = false;
1,560✔
7480
   if (optional(tCOLON)) {
1,560✔
7481
      tree_set_type(t, p_subtype_indication());
838✔
7482
      has_subtype_indication = true;
838✔
7483
   }
7484
   consume(tIS);
1,560✔
7485

7486
   tree_t value = p_name(N_SUBPROGRAM | N_TYPE);
1,560✔
7487
   tree_set_value(t, value);
1,560✔
7488

7489
   if (peek() == tLPAREN) {
1,560✔
7490
      parse_error(tree_loc(value), "name cannot be indexed or sliced");
1✔
7491
      tree_set_type(t, type_new(T_NONE));
1✔
7492
      drop_tokens_until(tRPAREN);
1✔
7493
   }
7494

7495
   const tree_kind_t value_kind = tree_kind(value);
1,560✔
7496
   bool nonobject_alias = false, type_alias = false;
1,560✔
7497

7498
   if (peek() == tLSQUARE) {
1,560✔
7499
      type_t type = p_signature();
531✔
7500
      if (has_subtype_indication) {
531✔
UNCOV
7501
         parse_error(CURRENT_LOC, "alias declaration may not contain both a "
×
7502
                     "signature and a subtype indication");
UNCOV
7503
         type = type_new(T_NONE);
×
7504
      }
7505
      else if (value_kind != T_REF && value_kind != T_PROT_REF) {
531✔
7506
         if (!type_is_none((type = solve_types(nametab, value, NULL)))) {
1✔
7507
            parse_error(tree_loc(value), "invalid name in subprogram alias");
×
UNCOV
7508
            type = type_new(T_NONE);
×
7509
         }
7510
      }
7511
      else {
7512
         ident_t id = tree_ident(value);
530✔
7513
         type_set_ident(type, ident_rfrom(id, '.') ?: id);
530✔
7514
         solve_types(nametab, value, type);
530✔
7515
      }
7516
      tree_set_type(t, type);
531✔
7517
   }
7518
   else if (value_kind == T_REF && tree_has_ref(value)) {
1,029✔
7519
      // A nonobject alias may be a design unit which does not have a type
7520
      tree_t decl = tree_ref(value);
868✔
7521
      if (is_design_unit(decl)) {
868✔
7522
         tree_set_flag(t, TREE_F_NONOBJECT_ALIAS);
1✔
7523
         nonobject_alias = true;
1✔
7524
      }
7525
      else
7526
         solve_types(nametab, value, NULL);
867✔
7527
   }
7528
   else
7529
      solve_types(nametab, value, NULL);
161✔
7530

7531
   if (value_kind == T_REF && tree_has_ref(value)) {
1,560✔
7532
      tree_t decl = tree_ref(value);
1,370✔
7533
      if (is_type_decl(decl))
1,370✔
7534
         type_alias = true;
65✔
7535
   }
7536

7537
   if ((type_alias || nonobject_alias) && has_subtype_indication)
1,560✔
7538
      parse_error(CURRENT_LOC, "nonobject alias may not have a "
1✔
7539
                  "subtype indication");
7540

7541
   consume(tSEMI);
1,560✔
7542

7543
   tree_set_loc(t, CURRENT_LOC);
1,560✔
7544

7545
   insert_name(nametab, t, NULL);
1,560✔
7546
   sem_check(t, nametab);
1,560✔
7547

7548
   tree_add_decl(parent, t);
1,560✔
7549

7550
   if (type_alias) {
1,560✔
7551
      // LRM 08 section 6.6.3 an implicit alias declaration exists for
7552
      // each enumeration literal, unit, and predefined operators
7553

7554
      type_t type = tree_type(value);
65✔
7555

7556
      switch (type_kind(type)) {
65✔
7557
      case T_ENUM:
27✔
7558
         {
7559
            const int nlits = type_enum_literals(type);
27✔
7560
            for (int i = 0; i < nlits; i++) {
641✔
7561
               tree_t lit = type_enum_literal(type, i);
614✔
7562

7563
               tree_t a = tree_new(T_ALIAS);
614✔
7564
               tree_set_loc(a, CURRENT_LOC);
614✔
7565
               tree_set_ident(a, tree_ident(lit));
614✔
7566
               tree_set_value(a, make_ref(lit));
614✔
7567
               tree_set_type(a, type);
614✔
7568
               tree_set_flag(a, TREE_F_PREDEFINED);
614✔
7569

7570
               insert_name(nametab, a, NULL);
614✔
7571
               tree_add_decl(parent, a);
614✔
7572
            }
7573
         }
7574
         break;
7575

7576
      case T_PHYSICAL:
1✔
7577
         {
7578
            const int nunits = type_units(type);
1✔
7579
            for (int i = 0; i < nunits; i++) {
3✔
7580
               tree_t unit = type_unit(type, i);
2✔
7581

7582
               tree_t a = tree_new(T_ALIAS);
2✔
7583
               tree_set_loc(a, CURRENT_LOC);
2✔
7584
               tree_set_ident(a, tree_ident(unit));
2✔
7585
               tree_set_value(a, make_ref(unit));
2✔
7586
               tree_set_type(a, type);
2✔
7587
               tree_set_flag(a, TREE_F_PREDEFINED);
2✔
7588

7589
               insert_name(nametab, a, NULL);
2✔
7590
               tree_add_decl(parent, a);
2✔
7591
            }
7592
         }
7593
      default:
7594
         break;
7595
      }
7596

7597
      walk_predefs(nametab, tree_ident(value), add_predef_alias, parent);
65✔
7598
   }
7599
}
1,560✔
7600

7601
static void p_file_open_information(tree_t *mode, tree_t *name)
148✔
7602
{
7603
   // [ open expression ] is file_logical_name
7604

7605
   BEGIN("file open information");
296✔
7606

7607
   if (optional(tOPEN)) {
148✔
7608
      *mode = p_expression();
38✔
7609
      solve_types(nametab, *mode, std_type(NULL, STD_FILE_OPEN_KIND));
38✔
7610
   }
7611
   else
7612
      *mode = NULL;
110✔
7613

7614
   if (optional(tIS)) {
148✔
7615
      ident_t mode_name = ident_new("READ_MODE");
47✔
7616
      if ((*mode == NULL) && scan(tIN, tOUT)) {
47✔
7617
         // VHDL-87 compatibility
7618
         switch (one_of(tIN, tOUT)) {
2✔
7619
         case tIN: break;
7620
         case tOUT: mode_name = ident_new("WRITE_MODE"); break;
1✔
7621
         }
7622
      }
7623

7624
      *name = p_expression();
47✔
7625
      solve_types(nametab, *name, std_type(NULL, STD_STRING));
47✔
7626

7627
      if (*mode == NULL) {
47✔
7628
         tree_t decl = get_local_decl(nametab, find_std(nametab), mode_name, 0);
9✔
7629
         *mode = make_ref(decl);
9✔
7630
      }
7631
   }
7632
   else
7633
      *name = NULL;
101✔
7634
}
148✔
7635

7636
static void p_file_declaration(tree_t parent)
148✔
7637
{
7638
   // file identifier_list : subtype_indication [ file_open_information ] ;
7639

7640
   BEGIN("file declaration");
296✔
7641

7642
   consume(tFILE);
148✔
7643

7644
   LOCAL_IDENT_LIST ids = p_identifier_list();
296✔
7645

7646
   for (ident_list_t *it = ids; it != NULL; it = it->next)
302✔
7647
      hide_name(nametab, it->ident);
154✔
7648

7649
   consume(tCOLON);
148✔
7650

7651
   type_t type = p_subtype_indication();
148✔
7652

7653
   tree_t mode, name;
148✔
7654
   p_file_open_information(&mode, &name);
148✔
7655

7656
   consume(tSEMI);
148✔
7657

7658
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
302✔
7659
      tree_t t = tree_new(T_FILE_DECL);
154✔
7660
      tree_set_ident(t, it->ident);
154✔
7661
      tree_set_type(t, type);
154✔
7662
      if (name != NULL) {
154✔
7663
         tree_set_file_mode(t, mode);
47✔
7664
         tree_set_value(t, name);
47✔
7665
      }
7666
      tree_set_loc(t, &(it->loc));
154✔
7667

7668
      tree_add_decl(parent, t);
154✔
7669

7670
      insert_name(nametab, t, it->ident);
154✔
7671
      sem_check(t, nametab);
154✔
7672
   }
7673
}
148✔
7674

7675
static void p_disconnection_specification(tree_t container)
14✔
7676
{
7677
   // disconnect guarded_signal_specification after time_expression ;
7678

7679
   BEGIN("disconnection specification");
28✔
7680

7681
   consume(tDISCONNECT);
14✔
7682

7683
   LOCAL_IDENT_LIST ids = p_identifier_list();
28✔
7684

7685
   consume(tCOLON);
14✔
7686

7687
   type_t type = p_type_mark();
14✔
7688

7689
   consume(tAFTER);
14✔
7690

7691
   tree_t delay = p_expression();
14✔
7692
   solve_types(nametab, delay, std_type(NULL, STD_TIME));
14✔
7693

7694
   consume(tSEMI);
14✔
7695

7696
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
28✔
7697
      tree_t d = tree_new(T_DISCONNECT);
14✔
7698
      tree_set_loc(d, &(it->loc));
14✔
7699
      tree_set_ident(d, it->ident);
14✔
7700
      tree_set_type(d, type);
14✔
7701
      tree_set_delay(d, delay);
14✔
7702
      tree_set_ref(d, resolve_name(nametab, CURRENT_LOC, it->ident));
14✔
7703

7704
      // TODO: we should use insert_spec for this
7705
      ident_t name = ident_prefix(it->ident, ident_new("disconnect"), '$');
14✔
7706
      insert_name(nametab, d, name);
14✔
7707

7708
      tree_add_decl(container, d);
14✔
7709
      sem_check(d, nametab);
14✔
7710
   }
7711
}
14✔
7712

7713
static void p_entity_class_entry_list(tree_t group)
2✔
7714
{
7715
   // entity_class [ <> ] { , entity_class [ <> ] }
7716

7717
   BEGIN("entity class entry list");
4✔
7718

7719
   do {
2✔
7720
      p_entity_class();
2✔
7721
      optional(tBOX);
2✔
7722
   } while (optional(tCOMMA));
2✔
7723
}
2✔
7724

7725
static tree_t p_group_template_declaration(void)
2✔
7726
{
7727
   // group identifier is ( entity_class_entry_list ) ;
7728

7729
   BEGIN("group template declaration");
2✔
7730

7731
   consume(tGROUP);
2✔
7732

7733
   tree_t g = tree_new(T_GROUP_TEMPLATE);
2✔
7734
   tree_set_ident(g, p_identifier());
2✔
7735

7736
   consume(tIS);
2✔
7737
   consume(tLPAREN);
2✔
7738
   p_entity_class_entry_list(g);
2✔
7739
   consume(tRPAREN);
2✔
7740
   consume(tSEMI);
2✔
7741

7742
   tree_set_loc(g, CURRENT_LOC);
2✔
7743
   insert_name(nametab, g, NULL);
2✔
7744

7745
   return g;
2✔
7746
}
7747

7748
static void p_group_constituent_list(tree_t group)
2✔
7749
{
7750
   // group_constituent_list ::= group_constituent { , group_constituent }
7751

7752
   BEGIN("group constituent list");
4✔
7753

7754
   do {
2✔
7755
      (void)p_name(0);   // Do nothing with groups currently
2✔
7756
   } while (optional(tCOMMA));
2✔
7757
}
2✔
7758

7759
static tree_t p_group_declaration(void)
2✔
7760
{
7761
   // group identifier : group_template_name ( group_constituent_list ) ;
7762

7763
   BEGIN("group declaration");
2✔
7764

7765
   consume(tGROUP);
2✔
7766

7767
   tree_t g = tree_new(T_GROUP);
2✔
7768
   tree_set_ident(g, p_identifier());
2✔
7769

7770
   consume(tCOLON);
2✔
7771

7772
   ident_t template_name = p_identifier();
2✔
7773
   tree_t template = resolve_name(nametab, CURRENT_LOC, template_name);
2✔
7774
   if (template != NULL && tree_kind(template) != T_GROUP_TEMPLATE) {
2✔
UNCOV
7775
      parse_error(CURRENT_LOC, "%s does not name a group template",
×
7776
                  istr(template_name));
7777
      template = NULL;
7778
   }
7779

7780
   tree_set_ref(g, template);
2✔
7781

7782
   consume(tLPAREN);
2✔
7783
   p_group_constituent_list(g);
2✔
7784
   consume(tRPAREN);
2✔
7785
   consume(tSEMI);
2✔
7786

7787
   tree_set_loc(g, CURRENT_LOC);
2✔
7788
   insert_name(nametab, g, NULL);
2✔
7789

7790
   return g;
2✔
7791
}
7792

7793
static void p_protected_type_body_declarative_item(tree_t body)
750✔
7794
{
7795
   // subprogram_declaration | subprogram_body | type_declaration
7796
   //   | subtype_declaration | constant_declaration | variable_declaration
7797
   //   | file_declaration | alias_declaration | attribute_declaration
7798
   //   | attribute_specification | use_clause | group_template_declaration
7799
   //   | group_declaration | 2008: subprogram_instantiation_declaration
7800

7801
   BEGIN("protected type body declarative item");
1,500✔
7802

7803
   switch (peek()) {
750✔
UNCOV
7804
   case tATTRIBUTE:
×
UNCOV
7805
      if (peek_nth(3) == tOF)
×
UNCOV
7806
         p_attribute_specification(body);
×
7807
      else
7808
         tree_add_decl(body, p_attribute_declaration());
×
7809
      break;
7810

7811
   case tTYPE:
39✔
7812
      p_type_declaration(body);
39✔
7813
      break;
39✔
7814

UNCOV
7815
   case tSUBTYPE:
×
UNCOV
7816
      tree_add_decl(body, p_subtype_declaration());
×
UNCOV
7817
      break;
×
7818

7819
   case tCONSTANT:
7✔
7820
      p_constant_declaration(body);
7✔
7821
      break;
7✔
7822

UNCOV
7823
   case tALIAS:
×
UNCOV
7824
      p_alias_declaration(body);
×
UNCOV
7825
      break;
×
7826

7827
   case tFUNCTION:
457✔
7828
   case tPROCEDURE:
7829
   case tIMPURE:
7830
   case tPURE:
7831
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
457✔
UNCOV
7832
         tree_add_decl(body, p_subprogram_instantiation_declaration());
×
7833
      else {
7834
         tree_t spec = p_subprogram_specification();
457✔
7835
         tree_set_flag(spec, TREE_F_PROTECTED);
457✔
7836
         if (peek() == tSEMI)
457✔
7837
            tree_add_decl(body, p_subprogram_declaration(spec));
5✔
7838
         else
7839
            tree_add_decl(body, p_subprogram_body(spec));
452✔
7840
      }
7841
      break;
7842

7843
   case tVARIABLE:
238✔
7844
      p_variable_declaration(body);
238✔
7845
      break;
238✔
7846

UNCOV
7847
   case tUSE:
×
UNCOV
7848
      p_use_clause(body, tree_add_decl);
×
UNCOV
7849
      break;
×
7850

7851
   case tFILE:
9✔
7852
      p_file_declaration(body);
9✔
7853
      break;
9✔
7854

UNCOV
7855
   case tGROUP:
×
UNCOV
7856
      if (peek_nth(3) == tIS)
×
UNCOV
7857
         tree_add_decl(body, p_group_template_declaration());
×
7858
      else
7859
         tree_add_decl(body, p_group_declaration());
×
7860
      break;
7861

UNCOV
7862
   default:
×
7863
      expect(tATTRIBUTE, tTYPE, tSUBTYPE, tCONSTANT, tFUNCTION, tPROCEDURE,
×
7864
             tIMPURE, tPURE, tALIAS, tVARIABLE, tUSE, tFILE, tGROUP);
7865
   }
7866
}
750✔
7867

7868
static void p_protected_type_body_declarative_part(tree_t body)
204✔
7869
{
7870
   // { protected_type_body_declarative_item }
7871

7872
   BEGIN("protected type body declarative part");
408✔
7873

7874
   while (not_at_token(tEND))
954✔
7875
      p_protected_type_body_declarative_item(body);
750✔
7876
}
204✔
7877

7878
static tree_t p_protected_type_body(ident_t id)
204✔
7879
{
7880
   // protected body protected_type_body_declarative_part end protected body
7881
   //   [ simple name ]
7882

7883
   BEGIN("protected type body");
204✔
7884

7885
   consume(tPROTECTED);
204✔
7886
   consume(tBODY);
204✔
7887

7888
   tree_t body = tree_new(T_PROT_BODY);
204✔
7889
   tree_set_ident(body, id);
204✔
7890
   tree_set_loc(body, CURRENT_LOC);
204✔
7891

7892
   tree_t decl = resolve_name(nametab, CURRENT_LOC, id);
204✔
7893
   if (decl != NULL) {
204✔
7894
      switch (tree_kind(decl)) {
201✔
7895
      case T_PROT_BODY:   // Duplicate body will trigger an error later
2✔
7896
         if (!tree_has_primary(decl)) {
2✔
7897
            assert(error_count() > 0); // Fallout from previous error, ignore
1✔
7898
            decl = NULL;
7899
            break;
7900
         }
7901
         decl = tree_primary(decl);
1✔
7902
         // Fall-through
7903
      case T_PROT_DECL:
198✔
7904
         tree_set_primary(body, decl);
198✔
7905
         break;
198✔
7906
      default:
2✔
7907
         parse_error(CURRENT_LOC, "object %s is not a protected type "
2✔
7908
                     "declaration", istr(id));
7909
         decl = NULL;
7910
      }
7911
   }
7912

7913
   if (decl == NULL)
201✔
7914
      tree_set_type(body, type_new(T_NONE));
6✔
7915
   else {
7916
      type_t type = tree_type(decl);
198✔
7917
      assert(type_is_protected(type));
198✔
7918
      tree_set_type(body, type);
198✔
7919
   }
7920

7921
   insert_name(nametab, body, NULL);
204✔
7922

7923
   push_scope(nametab);
204✔
7924

7925
   if (decl != NULL) insert_decls(nametab, decl);
204✔
7926

7927
   scope_set_prefix(nametab, id);
204✔
7928
   scope_set_container(nametab, body);
204✔
7929

7930
   p_protected_type_body_declarative_part(body);
204✔
7931

7932
   consume(tEND);
204✔
7933
   consume(tPROTECTED);
204✔
7934
   consume(tBODY);
204✔
7935

7936
   p_trailing_label(id);
204✔
7937

7938
   tree_set_loc(body, CURRENT_LOC);
204✔
7939
   sem_check(body, nametab);
204✔
7940

7941
   pop_scope(nametab);
204✔
7942
   return body;
204✔
7943
}
7944

7945
static tree_t p_package_instantiation_declaration(tree_t unit)
271✔
7946
{
7947
   // 2008: package identifier is new name [ generic_map_aspect ] ;
7948

7949
   consume(tPACKAGE);
271✔
7950

7951
   ident_t id = p_identifier();
271✔
7952

7953
   consume(tIS);
271✔
7954
   consume(tNEW);
271✔
7955

7956
   require_std(STD_08, "package instantiation declarations");
271✔
7957

7958
   ident_t unit_name = p_selected_identifier();
271✔
7959
   tree_t pack = resolve_name(nametab, CURRENT_LOC, unit_name);
271✔
7960

7961
   if (pack != NULL && !is_uninstantiated_package(pack)) {
271✔
7962
      parse_error(CURRENT_LOC, "unit %s is not an uninstantiated package",
2✔
7963
                  istr(unit_name));
7964
      pack = NULL;
7965
   }
7966

7967
   tree_t new;
271✔
7968
   if (unit != NULL) {
271✔
7969
      // Package instantiation declaration as primary unit
7970
      assert(tree_kind(unit) == T_DESIGN_UNIT);
70✔
7971
      tree_change_kind(unit, T_PACK_INST);
70✔
7972
      new = unit;
70✔
7973

7974
      ident_t qual = ident_prefix(lib_name(lib_work()), id, '.');
70✔
7975
      tree_set_ident(new, qual);
70✔
7976
   }
7977
   else {
7978
      new = tree_new(T_PACK_INST);
201✔
7979
      tree_set_ident(new, id);
201✔
7980
   }
7981

7982
   tree_t body = NULL;
271✔
7983
   if (pack != NULL) {
271✔
7984
      if (package_needs_body(pack) && (body = body_of(pack)) == NULL)
268✔
UNCOV
7985
         parse_error(CURRENT_LOC, "package %s cannot be instantiated until "
×
7986
                     "its body has been analysed", istr(unit_name));
7987

7988
      instantiate_package(new, pack, body);
268✔
7989
   }
7990

7991
   tree_set_ref(new, pack);
271✔
7992

7993
   if (peek() == tGENERIC)
271✔
7994
      p_generic_map_aspect(new, new);
255✔
7995

7996
   consume(tSEMI);
271✔
7997

7998
   tree_set_loc(new, CURRENT_LOC);
271✔
7999
   insert_name(nametab, new, NULL);
271✔
8000

8001
   sem_check(new, nametab);
271✔
8002

8003
   hash_t *map = get_generic_map(nametab);
271✔
8004
   if (map != NULL)
271✔
8005
      instance_fixup(new, map);
240✔
8006

8007
   return new;
271✔
8008
}
8009

8010
static tree_t p_element_array_mode_view_indication(void)
2✔
8011
{
8012
   // view ( name )
8013

8014
   BEGIN("element array mode view indication");
2✔
8015

8016
   consume(tVIEW);
2✔
8017
   consume(tLPAREN);
2✔
8018

8019
   tree_t name = p_name(0);
2✔
8020
   solve_types(nametab, name, NULL);
2✔
8021

8022
   consume(tRPAREN);
2✔
8023

8024
   return name;
2✔
8025
}
8026

8027
static tree_t p_element_record_mode_view_indication(void)
6✔
8028
{
8029
   // view name
8030

8031
   BEGIN("element record mode view indication");
6✔
8032

8033
   consume(tVIEW);
6✔
8034

8035
   tree_t name = p_name(0);
6✔
8036
   solve_types(nametab, name, NULL);
6✔
8037

8038
   return name;
6✔
8039
}
8040

8041
static port_mode_t p_element_mode_view_indication(tree_t *name)
8✔
8042
{
8043
   // element_record_mode_view_indication | element_array_mode_view_indication
8044

8045
   BEGIN("element mode view indication");
16✔
8046

8047
   if (peek_nth(2) == tLPAREN) {
8✔
8048
      *name = p_element_array_mode_view_indication();
2✔
8049
      return PORT_ARRAY_VIEW;
2✔
8050
   }
8051
   else {
8052
      *name = p_element_record_mode_view_indication();
6✔
8053
      return PORT_RECORD_VIEW;
6✔
8054
   }
8055
}
8056

8057
static void p_mode_view_element_declaration(type_t view, type_t of)
122✔
8058
{
8059
   // record_element_list : element_mode_indication ;
8060

8061
   BEGIN("mode view element declaration");
244✔
8062

8063
   LOCAL_IDENT_LIST ids = p_identifier_list();
244✔
8064

8065
   consume(tCOLON);
122✔
8066

8067
   tree_t name = NULL;
122✔
8068

8069
   port_mode_t mode;
122✔
8070
   if (peek() == tVIEW)
122✔
8071
      mode = p_element_mode_view_indication(&name);
8✔
8072
   else
8073
      mode = p_mode();
114✔
8074

8075
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
255✔
8076
      tree_t elt = tree_new(T_VIEW_ELEMENT);
133✔
8077
      tree_set_ident(elt, it->ident);
133✔
8078
      tree_set_loc(elt, &(it->loc));
133✔
8079
      tree_set_subkind(elt, mode);
133✔
8080
      tree_set_value(elt, name);
133✔
8081

8082
      type_add_field(view, elt);
133✔
8083

8084
      tree_t f = resolve_field_name(nametab, &(it->loc), it->ident, of);
133✔
8085
      if (f == NULL)
133✔
8086
         tree_set_type(elt, type_new(T_NONE));
2✔
8087
      else {
8088
         tree_set_ref(elt, f);
131✔
8089
         tree_set_type(elt, tree_type(f));
131✔
8090
      }
8091
   }
8092

8093
   consume(tSEMI);
122✔
8094
}
122✔
8095

8096
static tree_t p_mode_view_declaration(void)
57✔
8097
{
8098
   // view identifier of subtype_indication is
8099
   //   { mode_view_element_definition } end view [ mode_view_simple_name ] ;
8100

8101
   BEGIN("mode view declaration");
57✔
8102

8103
   consume(tVIEW);
57✔
8104

8105
   tree_t view = tree_new(T_VIEW_DECL);
57✔
8106

8107
   ident_t id = p_identifier();
57✔
8108
   tree_set_ident(view, id);
57✔
8109

8110
   consume(tOF);
57✔
8111

8112
   type_t of = p_subtype_indication();
57✔
8113

8114
   type_t type = type_new(T_VIEW);
57✔
8115
   type_set_ident(type, id);
57✔
8116
   type_set_designated(type, of);
57✔
8117

8118
   tree_set_type(view, type);
57✔
8119

8120
   consume(tIS);
57✔
8121

8122
   if (type_is_record(of)) {
57✔
8123
      while (not_at_token(tEND))
178✔
8124
         p_mode_view_element_declaration(type, of);
122✔
8125

8126
      consume(tEND);
56✔
8127
   }
8128
   else {
8129
      parse_error(CURRENT_LOC, "subtype indication of a mode view declaration "
1✔
8130
                  "must denote a record type");
8131
      drop_tokens_until(tEND);
1✔
8132
   }
8133

8134
   consume(tVIEW);
57✔
8135

8136
   p_trailing_label(id);
57✔
8137

8138
   consume(tSEMI);
57✔
8139

8140
   tree_set_loc(view, CURRENT_LOC);
57✔
8141
   insert_name(nametab, view, NULL);
57✔
8142
   sem_check(view, nametab);
57✔
8143
   return view;
57✔
8144
}
8145

8146
static void p_entity_declarative_item(tree_t entity)
84✔
8147
{
8148
   // subprogram_declaration | subprogram_body | type_declaration
8149
   //   | subtype_declaration | constant_declaration | signal_declaration
8150
   //   | shared_variable_declaration | file_declaration | alias_declaration
8151
   //   | attribute_declaration | attribute_specification
8152
   //   | disconnection_specification | use_clause | group_template_declaration
8153
   //   | group_declaration | 2008: subprogram_instantiation_declaration
8154
   //   | 2019: mode_view_declaration
8155

8156
   BEGIN("entity declarative item");
168✔
8157

8158
   switch (peek()) {
84✔
8159
   case tATTRIBUTE:
19✔
8160
      if (peek_nth(3) == tOF)
19✔
8161
         p_attribute_specification(entity);
10✔
8162
      else
8163
         tree_add_decl(entity, p_attribute_declaration());
9✔
8164
      break;
8165

8166
   case tTYPE:
1✔
8167
      p_type_declaration(entity);
1✔
8168
      break;
1✔
8169

8170
   case tSUBTYPE:
7✔
8171
      tree_add_decl(entity, p_subtype_declaration());
7✔
8172
      break;
7✔
8173

8174
   case tCONSTANT:
10✔
8175
      p_constant_declaration(entity);
10✔
8176
      break;
10✔
8177

8178
   case tALIAS:
1✔
8179
      p_alias_declaration(entity);
1✔
8180
      break;
1✔
8181

8182
   case tFUNCTION:
31✔
8183
   case tPROCEDURE:
8184
   case tIMPURE:
8185
   case tPURE:
8186
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
31✔
UNCOV
8187
         tree_add_decl(entity, p_subprogram_instantiation_declaration());
×
8188
      else {
8189
         tree_t spec = p_subprogram_specification();
31✔
8190
         if (peek() == tSEMI)
31✔
8191
            tree_add_decl(entity, p_subprogram_declaration(spec));
9✔
8192
         else
8193
            tree_add_decl(entity, p_subprogram_body(spec));
22✔
8194
      }
8195
      break;
8196

8197
   case tUSE:
5✔
8198
      p_use_clause(entity, tree_add_decl);
5✔
8199
      break;
5✔
8200

8201
   case tDISCONNECT:
1✔
8202
      p_disconnection_specification(entity);
1✔
8203
      break;
1✔
8204

UNCOV
8205
   case tGROUP:
×
UNCOV
8206
      if (peek_nth(3) == tIS)
×
UNCOV
8207
         tree_add_decl(entity, p_group_template_declaration());
×
8208
      else
8209
         tree_add_decl(entity, p_group_declaration());
×
8210
      break;
8211

8212
   case tSHARED:
3✔
8213
      p_variable_declaration(entity);
3✔
8214
      break;
3✔
8215

8216
   case tSIGNAL:
6✔
8217
      p_signal_declaration(entity);
6✔
8218
      break;
6✔
8219

UNCOV
8220
   case tVIEW:
×
UNCOV
8221
      tree_add_decl(entity, p_mode_view_declaration());
×
UNCOV
8222
      break;
×
8223

8224
   default:
×
8225
      expect(tATTRIBUTE, tTYPE, tSUBTYPE, tCONSTANT, tFUNCTION, tPROCEDURE,
×
8226
             tIMPURE, tPURE, tALIAS, tUSE, tDISCONNECT, tGROUP, tSHARED,
8227
             tSIGNAL, STD(19, tVIEW));
8228
   }
8229
}
84✔
8230

8231
static void p_entity_declarative_part(tree_t entity)
4,952✔
8232
{
8233
   // { entity_declarative_item }
8234

8235
   BEGIN("entity declarative part");
9,904✔
8236

8237
   while (not_at_token(tEND, tBEGIN))
5,036✔
8238
      p_entity_declarative_item(entity);
84✔
8239
}
4,952✔
8240

8241
static void p_subprogram_declarative_item(tree_t sub)
9,448✔
8242
{
8243
   // subprogram_declaration | subprogram_body | type_declaration
8244
   //   | subtype_declaration | constant_declaration | variable_declaration
8245
   //   | file_declaration | alias_declaration | attribute_declaration
8246
   //   | attribute_specification | use_clause | group_template_declaration
8247
   //   | group_declaration | 2008: subprogram_instantiation_declaration
8248

8249
   BEGIN("subprogram declarative item");
18,896✔
8250

8251
   switch (peek()) {
9,448✔
8252
   case tVARIABLE:
6,567✔
8253
      p_variable_declaration(sub);
6,567✔
8254
      break;
6,567✔
8255

8256
   case tTYPE:
83✔
8257
      p_type_declaration(sub);
83✔
8258
      break;
83✔
8259

8260
   case tALIAS:
756✔
8261
      p_alias_declaration(sub);
756✔
8262
      break;
756✔
8263

8264
   case tCONSTANT:
1,827✔
8265
      p_constant_declaration(sub);
1,827✔
8266
      break;
1,827✔
8267

8268
   case tFUNCTION:
107✔
8269
   case tPROCEDURE:
8270
   case tIMPURE:
8271
   case tPURE:
8272
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
107✔
8273
         tree_add_decl(sub, p_subprogram_instantiation_declaration());
1✔
8274
      else {
8275
         tree_t spec = p_subprogram_specification();
106✔
8276
         tree_set_flag(spec, tree_flags(sub) & TREE_F_PROTECTED);
106✔
8277
         if (peek() == tSEMI)
106✔
UNCOV
8278
            tree_add_decl(sub, p_subprogram_declaration(spec));
×
8279
         else
8280
            tree_add_decl(sub, p_subprogram_body(spec));
106✔
8281
      }
8282
      break;
8283

8284
   case tATTRIBUTE:
10✔
8285
      if (peek_nth(3) == tOF)
10✔
8286
         p_attribute_specification(sub);
6✔
8287
      else
8288
         tree_add_decl(sub, p_attribute_declaration());
4✔
8289
      break;
8290

8291
   case tSUBTYPE:
56✔
8292
      tree_add_decl(sub, p_subtype_declaration());
56✔
8293
      break;
56✔
8294

8295
   case tUSE:
9✔
8296
      p_use_clause(sub, tree_add_decl);
9✔
8297
      break;
9✔
8298

8299
   case tFILE:
25✔
8300
      p_file_declaration(sub);
25✔
8301
      break;
25✔
8302

UNCOV
8303
   case tGROUP:
×
UNCOV
8304
      if (peek_nth(3) == tIS)
×
UNCOV
8305
         tree_add_decl(sub, p_group_template_declaration());
×
8306
      else
8307
         tree_add_decl(sub, p_group_declaration());
×
8308
      break;
8309

8310
   default:
8✔
8311
      expect(tVARIABLE, tTYPE, tALIAS, tCONSTANT, tFUNCTION, tPROCEDURE,
8✔
8312
             tIMPURE, tPURE, tATTRIBUTE, tSUBTYPE, tUSE, tFILE, tGROUP);
8313
   }
8314
}
9,448✔
8315

8316
static void p_subprogram_declarative_part(tree_t sub)
9,453✔
8317
{
8318
   // { subprogram_declarative_item }
8319

8320
   BEGIN("subprogram declarative part");
18,906✔
8321

8322
   while (not_at_token(tBEGIN))
18,901✔
8323
      p_subprogram_declarative_item(sub);
9,448✔
8324
}
9,453✔
8325

8326
static void p_sequence_of_statements(tree_t parent)
31,625✔
8327
{
8328
   // { sequential_statement }
8329

8330
   BEGIN("sequence of statements");
63,247✔
8331

8332
   while (not_at_token(tEND, tELSE, tELSIF, tWHEN))
114,613✔
8333
      tree_add_stmt(parent, p_sequential_statement());
82,991✔
8334
}
31,622✔
8335

8336
static void p_trailing_label(ident_t label)
40,233✔
8337
{
8338
   // [ label ]
8339

8340
   if ((peek() == tID) || (peek() == tSTRING)) {
40,233✔
8341
      ident_t trailing = p_designator();
6,840✔
8342
      if (label == NULL)
6,840✔
8343
         parse_error(&last_loc, "unexpected trailing label for %s without "
3✔
8344
                     "label", hint_str);
8345
      else if (trailing != label)
6,837✔
8346
         parse_error(&last_loc, "expected trailing %s label to match %s",
7✔
8347
                     hint_str, istr(label));
8348
   }
8349
}
40,233✔
8350

8351
static tree_t p_subprogram_body(tree_t spec)
9,453✔
8352
{
8353
   // subprogram_specification is subprogram_declarative_part begin
8354
   //   subprogram_statement_part end [ subprogram_kind ] [ designator ] ;
8355

8356
   EXTEND("subprogram body");
9,453✔
8357

8358
   consume(tIS);
9,453✔
8359

8360
   const tree_kind_t kind =
18,906✔
8361
      (tree_kind(spec) == T_FUNC_DECL) ? T_FUNC_BODY : T_PROC_BODY;
9,453✔
8362
   tree_change_kind(spec, kind);
9,453✔
8363

8364
   insert_name(nametab, spec, NULL);
9,453✔
8365

8366
   push_scope(nametab);
9,453✔
8367
   scope_set_subprogram(nametab, spec);
9,453✔
8368

8369
   insert_generics(nametab, spec);
9,453✔
8370
   insert_ports(nametab, spec);
9,453✔
8371

8372
   sem_check(spec, nametab);
9,453✔
8373

8374
   if (tree_flags(spec) & TREE_F_KNOWS_SUBTYPE) {
9,453✔
8375
      // LRM 19 section 4.2.1: an implicit subtype declaration is
8376
      // created as the first declarative item when the function
8377
      // includes a return identifier
8378

8379
      type_t sub = type_result(tree_type(spec));
15✔
8380
      assert(type_kind(sub) == T_SUBTYPE);
15✔
8381

8382
      tree_t d = tree_new(T_SUBTYPE_DECL);
15✔
8383
      tree_set_ident(d, type_ident(sub));
15✔
8384
      tree_set_type(d, sub);
15✔
8385
      tree_set_loc(d, CURRENT_LOC);
15✔
8386

8387
      insert_name(nametab, d, NULL);
15✔
8388

8389
      tree_add_decl(spec, d);
15✔
8390
   }
8391

8392
   p_subprogram_declarative_part(spec);
9,453✔
8393

8394
   consume(tBEGIN);
9,453✔
8395

8396
   p_sequence_of_statements(spec);
9,453✔
8397

8398
   consume(tEND);
9,453✔
8399

8400
   pop_scope(nametab);
9,453✔
8401

8402
   if (scan(tFUNCTION, tPROCEDURE))
9,453✔
8403
      consume(kind == T_FUNC_BODY ? tFUNCTION : tPROCEDURE);
8,393✔
8404

8405
   p_trailing_label(tree_ident(spec));
9,453✔
8406
   consume(tSEMI);
9,453✔
8407

8408
   tree_set_loc(spec, CURRENT_LOC);
9,453✔
8409
   return spec;
9,453✔
8410
}
8411

8412
static tree_t p_subprogram_declaration(tree_t spec)
7,064✔
8413
{
8414
   // subprogram_specification ;
8415

8416
   EXTEND("subprogram declaration");
7,064✔
8417

8418
   insert_name(nametab, spec, NULL);
7,064✔
8419

8420
   consume(tSEMI);
7,064✔
8421

8422
   tree_set_loc(spec, CURRENT_LOC);
7,064✔
8423
   sem_check(spec, nametab);
7,064✔
8424
   return spec;
7,064✔
8425
}
8426

8427
static void p_sensitivity_list(tree_t proc)
746✔
8428
{
8429
   // name { , name }
8430

8431
   BEGIN("sensitivity list");
1,492✔
8432

8433
   do {
1,105✔
8434
      tree_t name = p_name(0);
1,105✔
8435
      tree_add_trigger(proc, name);
1,105✔
8436
      solve_types(nametab, name, NULL);
1,105✔
8437
   } while (optional(tCOMMA));
1,105✔
8438
}
746✔
8439

8440
static void p_process_declarative_item(tree_t proc)
4,820✔
8441
{
8442
   // subprogram_declaration | subprogram_body | type_declaration
8443
   //   | subtype_declaration | constant_declaration | variable_declaration
8444
   //   | file_declaration | alias_declaration | attribute_declaration
8445
   //   | attribute_specification | use_clause | group_template_declaration
8446
   //   | group_declaration | 2008: subprogram_instantiation_declaration
8447

8448
   BEGIN("process declarative item");
9,640✔
8449

8450
   switch (peek()) {
4,820✔
8451
   case tVARIABLE:
3,126✔
8452
      p_variable_declaration(proc);
3,126✔
8453
      break;
3,126✔
8454

8455
   case tTYPE:
275✔
8456
      p_type_declaration(proc);
275✔
8457
      break;
275✔
8458

8459
   case tSUBTYPE:
103✔
8460
      tree_add_decl(proc, p_subtype_declaration());
103✔
8461
      break;
103✔
8462

8463
   case tCONSTANT:
704✔
8464
      p_constant_declaration(proc);
704✔
8465
      break;
704✔
8466

8467
   case tFUNCTION:
397✔
8468
   case tPROCEDURE:
8469
   case tIMPURE:
8470
   case tPURE:
8471
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
397✔
UNCOV
8472
         tree_add_decl(proc, p_subprogram_instantiation_declaration());
×
8473
      else {
8474
         tree_t spec = p_subprogram_specification();
397✔
8475
         if (peek() == tSEMI)
397✔
8476
            tree_add_decl(proc, p_subprogram_declaration(spec));
91✔
8477
         else
8478
            tree_add_decl(proc, p_subprogram_body(spec));
306✔
8479
      }
8480
      break;
8481

8482
   case tATTRIBUTE:
38✔
8483
      if (peek_nth(3) == tOF)
38✔
8484
         p_attribute_specification(proc);
19✔
8485
      else
8486
         tree_add_decl(proc, p_attribute_declaration());
19✔
8487
      break;
8488

8489
   case tUSE:
5✔
8490
      p_use_clause(proc, tree_add_decl);
5✔
8491
      break;
5✔
8492

8493
   case tALIAS:
92✔
8494
      p_alias_declaration(proc);
92✔
8495
      break;
92✔
8496

8497
   case tFILE:
61✔
8498
      p_file_declaration(proc);
61✔
8499
      break;
61✔
8500

8501
   case tGROUP:
2✔
8502
      if (peek_nth(3) == tIS)
2✔
8503
         tree_add_decl(proc, p_group_template_declaration());
1✔
8504
      else
8505
         tree_add_decl(proc, p_group_declaration());
1✔
8506
      break;
8507

8508
   default:
17✔
8509
      expect(tVARIABLE, tTYPE, tSUBTYPE, tCONSTANT, tFUNCTION, tPROCEDURE,
17✔
8510
             tIMPURE, tPURE, tATTRIBUTE, tUSE, tALIAS, tFILE, tGROUP);
8511
   }
8512
}
4,820✔
8513

8514
static void p_process_declarative_part(tree_t proc)
4,780✔
8515
{
8516
   // { process_declarative_item }
8517

8518
   BEGIN("process declarative part");
9,560✔
8519

8520
   while (not_at_token(tBEGIN))
9,587✔
8521
      p_process_declarative_item(proc);
4,807✔
8522
}
4,780✔
8523

8524
static void p_process_statement_part(tree_t proc)
4,780✔
8525
{
8526
   // { sequential_statement }
8527

8528
   BEGIN("process statement part");
9,557✔
8529

8530
   p_sequence_of_statements(proc);
4,780✔
8531
}
4,777✔
8532

8533
static void p_process_sensitivity_list(tree_t proc)
203✔
8534
{
8535
   // 2008: all | sensitivity_list
8536

8537
   BEGIN("process sensitivity list");
406✔
8538

8539
   if (peek() == tALL) {
203✔
8540
      consume(tALL);
48✔
8541

8542
      tree_t all = tree_new(T_ALL);
48✔
8543
      tree_set_loc(all, CURRENT_LOC);
48✔
8544
      tree_add_trigger(proc, all);
48✔
8545
   }
8546
   else
8547
      p_sensitivity_list(proc);
155✔
8548
}
203✔
8549

8550
static tree_t p_process_statement(ident_t label)
4,780✔
8551
{
8552
   // [ process_label : ] [ postponed ] process [ ( sensitivity_list ) ] [ is ]
8553
   //   process_declarative_part begin process_statement_part end [ postponed ]
8554
   //   process [ label ] ;
8555

8556
   EXTEND("process statement");
4,780✔
8557

8558
   tree_t t = tree_new(T_PROCESS);
4,780✔
8559

8560
   const bool postponed = optional(tPOSTPONED);
4,780✔
8561

8562
   consume(tPROCESS);
4,780✔
8563

8564
   if (optional(tLPAREN)) {
4,780✔
8565
      if (standard() < STD_08)
457✔
8566
         p_sensitivity_list(t);
254✔
8567
      else
8568
         p_process_sensitivity_list(t);
203✔
8569
      consume(tRPAREN);
457✔
8570
   }
8571

8572
   optional(tIS);
4,780✔
8573

8574
   if (label == NULL) {
4,780✔
8575
      tree_set_ident(t, get_implicit_label(t, nametab));
1,980✔
8576
      tree_set_flag(t, TREE_F_SYNTHETIC_NAME);
1,980✔
8577
   }
8578
   else {
8579
      tree_set_loc(t, CURRENT_LOC);
2,800✔
8580
      tree_set_ident(t, label);
2,800✔
8581
      insert_name(nametab, t, label);
2,800✔
8582
   }
8583

8584
   push_scope(nametab);
4,780✔
8585
   scope_set_container(nametab, t);
4,780✔
8586
   scope_set_prefix(nametab, tree_ident(t));
4,780✔
8587

8588
   p_process_declarative_part(t);
4,780✔
8589

8590
   consume(tBEGIN);
4,780✔
8591

8592
   p_process_statement_part(t);
4,780✔
8593

8594
   consume(tEND);
4,777✔
8595
   if (postponed)
4,777✔
8596
      optional(tPOSTPONED);
16✔
8597
   consume(tPROCESS);
4,777✔
8598

8599
   p_trailing_label(label);
4,777✔
8600

8601
   consume(tSEMI);
4,777✔
8602

8603
   pop_scope(nametab);
4,777✔
8604

8605
   tree_set_loc(t, CURRENT_LOC);
4,777✔
8606

8607
   if (postponed)
4,777✔
8608
      tree_set_flag(t, TREE_F_POSTPONED);
16✔
8609

8610
   sem_check(t, nametab);
4,777✔
8611
   return t;
4,777✔
8612
}
8613

8614
static tree_t p_entity_statement(void)
37✔
8615
{
8616
   // concurrent_assertion_statement | concurrent_procedure_call_statement
8617
   //   | process_statement
8618

8619
   BEGIN("entity statement");
74✔
8620

8621
   ident_t label = NULL;
37✔
8622
   if ((peek() == tID) && (peek_nth(2) == tCOLON)) {
37✔
8623
      label = p_identifier();
4✔
8624
      consume(tCOLON);
4✔
8625
   }
8626

8627
   switch (peek()) {
37✔
8628
   case tASSERT:
29✔
8629
      return p_concurrent_assertion_statement(label);
29✔
8630

8631
   case tPROCESS:
2✔
8632
      return p_process_statement(label);
2✔
8633

8634
   case tID:
3✔
8635
      return p_concurrent_procedure_call_statement(label, NULL);
3✔
8636

8637
   case tPOSTPONED:
3✔
8638
      if (peek_nth(2) == tASSERT)
3✔
UNCOV
8639
         return p_concurrent_assertion_statement(label);
×
8640
      else if (peek_nth(2) == tPROCESS)
3✔
UNCOV
8641
         return p_process_statement(label);
×
8642
      else
8643
         return p_concurrent_procedure_call_statement(label, NULL);
3✔
8644

8645
   default:
×
UNCOV
8646
      expect(tASSERT, tPROCESS, tPOSTPONED);
×
UNCOV
8647
      return tree_new(T_NULL);
×
8648
   }
8649
}
8650

8651
static void p_entity_statement_part(tree_t entity)
52✔
8652
{
8653
   // { entity_statement }
8654

8655
   BEGIN("entity statement part");
104✔
8656

8657
   while (not_at_token(tEND))
89✔
8658
      tree_add_stmt(entity, p_entity_statement());
37✔
8659
}
52✔
8660

8661
static void p_entity_declaration(tree_t unit)
4,952✔
8662
{
8663
   // entity identifier is entity_header entity_declarative_part
8664
   //   [ begin entity_statement_part ] end [ entity ] [ entity_simple_name ] ;
8665

8666
   BEGIN("entity declaration");
9,904✔
8667

8668
   tree_change_kind(unit, T_ENTITY);
4,952✔
8669

8670
   consume(tENTITY);
4,952✔
8671

8672
   ident_t id = p_identifier();
4,952✔
8673
   tree_set_ident(unit, id);
4,952✔
8674

8675
   consume(tIS);
4,952✔
8676

8677
   push_scope(nametab);
4,952✔
8678

8679
   tree_set_loc(unit, CURRENT_LOC);
4,952✔
8680
   insert_name(nametab, unit, id);
4,952✔
8681

8682
   push_scope(nametab);
4,952✔
8683

8684
   ident_t qual = ident_prefix(lib_name(lib_work()), id, '.');
4,952✔
8685
   scope_set_prefix(nametab, qual);
4,952✔
8686

8687
   p_entity_header(unit);
4,952✔
8688
   p_entity_declarative_part(unit);
4,952✔
8689

8690
   if (optional(tBEGIN))
4,952✔
8691
      p_entity_statement_part(unit);
52✔
8692

8693
   consume(tEND);
4,952✔
8694
   optional(tENTITY);
4,952✔
8695
   p_trailing_label(id);
4,952✔
8696
   consume(tSEMI);
4,952✔
8697

8698
   tree_set_ident(unit, qual);
4,952✔
8699
   tree_set_loc(unit, CURRENT_LOC);
4,952✔
8700

8701
   sem_check(unit, nametab);
4,952✔
8702

8703
   pop_scope(nametab);
4,952✔
8704
   pop_scope(nametab);
4,952✔
8705
}
4,952✔
8706

8707
static tree_t p_component_declaration(void)
313✔
8708
{
8709
   // component identifier [ is ] [ generic_clause ] [ port_clause ]
8710
   //   end component [ simple_name ] ;
8711

8712
   // 2019:
8713
   // component identifier [ is ] [ generic_clause ] [ port_clause ]
8714
   //   end [ component ] [ simple_name ] ;
8715

8716
   BEGIN("component declaration");
313✔
8717

8718
   tree_t c = tree_new(T_COMPONENT);
313✔
8719

8720
   consume(tCOMPONENT);
313✔
8721
   tree_set_ident(c, p_identifier());
313✔
8722
   optional(tIS);
313✔
8723

8724
   push_scope(nametab);
313✔
8725

8726
   if (peek() == tGENERIC) {
313✔
8727
      p_generic_clause(c);
117✔
8728
      insert_generics(nametab, c);
117✔
8729
   }
8730

8731
   if (peek() == tPORT) {
313✔
8732
      p_port_clause(c);
210✔
8733
      insert_ports(nametab, c);
210✔
8734
   }
8735

8736
   pop_scope(nametab);
313✔
8737

8738
   consume(tEND);
313✔
8739
   if (peek() != tCOMPONENT)
313✔
8740
      require_std(STD_19, "optional end component");
1✔
8741
   else
8742
      consume(tCOMPONENT);
312✔
8743
   p_trailing_label(tree_ident(c));
313✔
8744
   consume(tSEMI);
313✔
8745

8746
   tree_set_loc(c, CURRENT_LOC);
313✔
8747
   insert_name(nametab, c, NULL);
313✔
8748
   sem_check(c, nametab);
313✔
8749
   return c;
313✔
8750
}
8751

8752
static void p_package_declarative_item(tree_t pack)
12,413✔
8753
{
8754
   // subprogram_declaration | type_declaration | subtype_declaration
8755
   //   | constant_declaration | signal_declaration
8756
   //   | shared_variable_declaration | file_declaration | alias_declaration
8757
   //   | component_declaration | attribute_declaration
8758
   //   | attribute_specification | disconnection_specification | use_clause
8759
   //   | group_template_declaration | group_declaration
8760
   //   | 2008: package_instantiation_declaration
8761
   //   | 2008: package_declaration | 2019: mode_view_declaration
8762
   //
8763

8764
   BEGIN("package declarative item");
24,826✔
8765

8766
   switch (peek()) {
12,413✔
8767
   case tTYPE:
2,122✔
8768
      p_type_declaration(pack);
2,122✔
8769
      break;
2,122✔
8770

8771
   case tFUNCTION:
6,361✔
8772
   case tPROCEDURE:
8773
   case tIMPURE:
8774
   case tPURE:
8775
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
6,361✔
8776
         tree_add_decl(pack, p_subprogram_instantiation_declaration());
42✔
8777
      else {
8778
         tree_t spec = p_subprogram_specification();
6,319✔
8779
         if (peek() == tSEMI)
6,319✔
8780
            tree_add_decl(pack, p_subprogram_declaration(spec));
6,317✔
8781
         else
8782
            tree_add_decl(pack, p_subprogram_body(spec));
2✔
8783
      }
8784
      break;
8785

8786
   case tSUBTYPE:
803✔
8787
      tree_add_decl(pack, p_subtype_declaration());
803✔
8788
      break;
803✔
8789

8790
   case tSIGNAL:
184✔
8791
      p_signal_declaration(pack);
184✔
8792
      break;
184✔
8793

8794
   case tATTRIBUTE:
260✔
8795
      if (peek_nth(3) == tOF)
260✔
8796
         p_attribute_specification(pack);
188✔
8797
      else
8798
         tree_add_decl(pack, p_attribute_declaration());
72✔
8799
      break;
8800

8801
   case tCONSTANT:
1,916✔
8802
      p_constant_declaration(pack);
1,916✔
8803
      break;
1,916✔
8804

8805
   case tCOMPONENT:
40✔
8806
      tree_add_decl(pack, p_component_declaration());
40✔
8807
      break;
40✔
8808

8809
   case tFILE:
14✔
8810
      p_file_declaration(pack);
14✔
8811
      break;
14✔
8812

8813
   case tSHARED:
29✔
8814
      p_variable_declaration(pack);
29✔
8815
      break;
29✔
8816

8817
   case tALIAS:
554✔
8818
      p_alias_declaration(pack);
554✔
8819
      break;
554✔
8820

8821
   case tUSE:
11✔
8822
      p_use_clause(pack, tree_add_decl);
11✔
8823
      break;
11✔
8824

UNCOV
8825
   case tDISCONNECT:
×
UNCOV
8826
      p_disconnection_specification(pack);
×
UNCOV
8827
      break;
×
8828

8829
   case tGROUP:
×
8830
      if (peek_nth(3) == tIS)
×
8831
         tree_add_decl(pack, p_group_template_declaration());
×
8832
      else
8833
         tree_add_decl(pack, p_group_declaration());
×
8834
      break;
8835

8836
   case tPACKAGE:
24✔
8837
      if (peek_nth(4) == tNEW)
24✔
8838
         tree_add_decl(pack, p_package_instantiation_declaration(NULL));
14✔
8839
      else {
8840
         require_std(STD_08, "nested package declarations");
10✔
8841
         tree_add_decl(pack, p_package_declaration(NULL));
10✔
8842
      }
8843
      break;
8844

8845
   case tVIEW:
46✔
8846
      tree_add_decl(pack, p_mode_view_declaration());
46✔
8847
      break;
46✔
8848

8849
   default:
49✔
8850
      expect(tTYPE, tFUNCTION, tPROCEDURE, tIMPURE, tPURE, tSUBTYPE, tSIGNAL,
97✔
8851
             tATTRIBUTE, tCONSTANT, tCOMPONENT, tFILE, tSHARED, tALIAS, tUSE,
8852
             tDISCONNECT, tGROUP, tPACKAGE, STD(19, tVIEW));
8853
   }
8854
}
12,413✔
8855

8856
static void p_package_declarative_part(tree_t pack)
1,439✔
8857
{
8858
   // { package_declarative_item }
8859

8860
   BEGIN("package declarative part");
2,878✔
8861

8862
   while (not_at_token(tEND))
13,852✔
8863
      p_package_declarative_item(pack);
12,413✔
8864
}
1,439✔
8865

8866
static void p_package_header(tree_t unit)
650✔
8867
{
8868
   // 2008: [ generic_clause [ generic_map_aspect ; ] ]
8869

8870
   BEGIN("package header");
1,300✔
8871

8872
   if (peek() == tGENERIC) {
650✔
8873
      p_generic_clause(unit);
209✔
8874

8875
      if (peek() == tGENERIC) {
209✔
8876
         p_generic_map_aspect(unit, unit);
1✔
8877
         consume(tSEMI);
1✔
8878
      }
8879

8880
      insert_generics(nametab, unit);
209✔
8881
   }
8882
}
650✔
8883

8884
static tree_t p_package_declaration(tree_t unit)
1,439✔
8885
{
8886
   // package identifier is package_declarative_part end [ package ]
8887
   //   [ simple_name ] ;
8888
   //
8889
   // 2008: package identifier is package_header package_declarative_part
8890
   //   end [ package ] [ simple_name ] ;
8891

8892
   BEGIN("package declaration");
1,439✔
8893

8894
   consume(tPACKAGE);
1,439✔
8895

8896
   ident_t name = p_identifier(), qual = name;
1,439✔
8897

8898
   tree_t pack;
1,439✔
8899
   if (unit != NULL) {
1,439✔
8900
      // Package declaration as primary unit
8901
      assert(tree_kind(unit) == T_DESIGN_UNIT);
1,424✔
8902
      tree_change_kind(unit, T_PACKAGE);
1,424✔
8903
      pack = unit;
1,424✔
8904

8905

8906
      qual = ident_prefix(lib_name(lib_work()), name, '.');
1,424✔
8907
      scope_set_prefix(nametab, qual);
1,424✔
8908
   }
8909
   else {
8910
      pack = tree_new(T_PACKAGE);
15✔
8911
      scope_set_prefix(nametab, name);
15✔
8912
   }
8913

8914
   tree_set_ident(pack, name);
1,439✔
8915
   tree_set_loc(pack, CURRENT_LOC);
1,439✔
8916

8917
   insert_name(nametab, pack, NULL);
1,439✔
8918

8919
   push_scope(nametab);
1,439✔
8920

8921
   consume(tIS);
1,439✔
8922

8923
   push_scope(nametab);
1,439✔
8924
   scope_set_container(nametab, pack);
1,439✔
8925

8926
   if (standard() >= STD_08)
1,439✔
8927
      p_package_header(pack);
650✔
8928

8929
   p_package_declarative_part(pack);
1,439✔
8930

8931
   if (bootstrapping)
1,439✔
8932
      declare_additional_standard_operators(pack);
3✔
8933

8934
   pop_scope(nametab);
1,439✔
8935

8936
   consume(tEND);
1,439✔
8937
   optional(tPACKAGE);
1,439✔
8938
   p_trailing_label(name);
1,439✔
8939
   consume(tSEMI);
1,439✔
8940

8941
   tree_set_ident(pack, qual);
1,439✔
8942
   tree_set_loc(pack, CURRENT_LOC);
1,439✔
8943

8944
   sem_check(pack, nametab);
1,439✔
8945

8946
   pop_scope(nametab);
1,439✔
8947
   return pack;
1,439✔
8948
}
8949

8950
static ident_list_t *p_instantiation_list(void)
214✔
8951
{
8952
   // label { , label } | others | all
8953

8954
   switch (peek()) {
214✔
8955
   case tID:
162✔
8956
      return p_identifier_list();
162✔
8957

8958
   case tOTHERS:
7✔
8959
      consume(tOTHERS);
7✔
8960
      return NULL;
7✔
8961

8962
   case tALL:
45✔
8963
      {
8964
         consume(tALL);
45✔
8965

8966
         ident_list_t *result = NULL;
45✔
8967
         ident_list_push(&result, well_known(W_ALL), last_loc);
45✔
8968
         return result;
45✔
8969
      }
8970

UNCOV
8971
   default:
×
UNCOV
8972
      expect(tID, tOTHERS, tALL);
×
UNCOV
8973
      return NULL;
×
8974
   }
8975
}
8976

8977
static ident_list_t *p_component_specification(ident_t *comp_name)
214✔
8978
{
8979
   // instantiation_list : name
8980

8981
   BEGIN("component specification");
214✔
8982

8983
   ident_list_t *ids = p_instantiation_list();
214✔
8984
   consume(tCOLON);
214✔
8985
   *comp_name = p_identifier();
214✔
8986

8987
   return ids;
214✔
8988
}
8989

8990
static void p_port_map_aspect(tree_t inst, tree_t unit)
1,290✔
8991
{
8992
   // port map ( association_list )
8993

8994
   BEGIN("port map aspect");
2,580✔
8995

8996
   consume(tPORT);
1,290✔
8997
   consume(tMAP);
1,290✔
8998
   consume(tLPAREN);
1,290✔
8999

9000
   p_association_list(inst, unit, F_PORT_MAP);
1,290✔
9001

9002
   consume(tRPAREN);
1,290✔
9003
}
1,290✔
9004

9005
static void p_generic_map_aspect(tree_t inst, tree_t unit)
1,062✔
9006
{
9007
   // generic map ( association_list )
9008

9009
   BEGIN("generic map aspect");
2,124✔
9010

9011
   consume(tGENERIC);
1,062✔
9012
   consume(tMAP);
1,062✔
9013
   consume(tLPAREN);
1,062✔
9014

9015
   p_association_list(inst, unit, F_GENERIC_MAP);
1,062✔
9016

9017
   consume(tRPAREN);
1,062✔
9018
}
1,062✔
9019

9020
static tree_t p_entity_aspect(void)
212✔
9021
{
9022
   // entity name [ ( identifier) ] | configuration name | open
9023

9024
   switch (one_of(tENTITY, tCONFIGURATION, tOPEN)) {
212✔
9025
   case tENTITY:
157✔
9026
      {
9027
         tree_t bind = tree_new(T_BINDING);
157✔
9028
         tree_set_class(bind, C_ENTITY);
157✔
9029
         tree_set_ident(bind, p_selected_identifier());
157✔
9030
         if (optional(tLPAREN)) {
157✔
9031
            tree_set_ident2(bind, p_identifier());
94✔
9032
            consume(tRPAREN);
94✔
9033
         }
9034
         tree_set_loc(bind, CURRENT_LOC);
157✔
9035

9036
         return bind;
157✔
9037
      }
9038

9039
   case tCONFIGURATION:
49✔
9040
      {
9041
         tree_t bind = tree_new(T_BINDING);
49✔
9042
         tree_set_class(bind, C_CONFIGURATION);
49✔
9043
         tree_set_ident(bind, p_selected_identifier());
49✔
9044
         tree_set_loc(bind, CURRENT_LOC);
49✔
9045

9046
         return bind;
49✔
9047
      }
9048

9049
   case tOPEN:
9050
   default:
9051
      return NULL;
9052
   }
9053
}
9054

9055
static tree_t p_binding_indication(tree_t comp)
214✔
9056
{
9057
   // [ use entity_aspect ] [ generic_map_aspect ] [ port_map_aspect ]
9058

9059
   BEGIN("binding indication");
214✔
9060

9061
   tree_t bind = NULL, unit = NULL;
214✔
9062
   if (optional(tUSE)) {
214✔
9063
      if ((bind = p_entity_aspect()) && (unit = find_binding(bind))) {
212✔
9064
         tree_set_ref(bind, unit);
202✔
9065
         unit = primary_unit_of(unit);
202✔
9066
      }
9067
   }
9068

9069
   if (comp) {
214✔
9070
      insert_generics(nametab, comp);
210✔
9071
      insert_ports(nametab, comp);
210✔
9072
   }
9073

9074
   if (peek() == tGENERIC) {
214✔
9075
      if (bind == NULL) {
55✔
9076
         consume(tGENERIC);
1✔
9077
         parse_error(CURRENT_LOC, "sorry, binding indication with generic map "
1✔
9078
                     "aspect and OPEN entity aspect is not yet supported");
9079
         drop_tokens_until(tRPAREN);
1✔
9080
      }
9081
      else
9082
         p_generic_map_aspect(bind, unit);
54✔
9083
   }
9084

9085
   if (peek() == tPORT) {
214✔
9086
      if (bind == NULL) {
61✔
UNCOV
9087
         consume(tPORT);
×
UNCOV
9088
         parse_error(CURRENT_LOC, "sorry, binding indication with port map "
×
9089
                     "aspect and OPEN entity aspect is not yet supported");
UNCOV
9090
         drop_tokens_until(tRPAREN);
×
9091
      }
9092
      else
9093
         p_port_map_aspect(bind, unit);
61✔
9094
   }
9095

9096
   if (bind != NULL)
214✔
9097
      tree_set_loc(bind, CURRENT_LOC);
206✔
9098

9099
   return bind;
214✔
9100
}
9101

9102
static void p_configuration_specification(tree_t parent)
98✔
9103
{
9104
   // for component_specification binding_indication ;
9105

9106
   BEGIN("configuration specification");
196✔
9107

9108
   consume(tFOR);
98✔
9109

9110
   ident_t comp_name;
98✔
9111
   LOCAL_IDENT_LIST ids = p_component_specification(&comp_name);
196✔
9112

9113
   tree_t comp = resolve_name(nametab, CURRENT_LOC, comp_name);
98✔
9114
   if (comp != NULL && tree_kind(comp) != T_COMPONENT) {
98✔
9115
      parse_error(CURRENT_LOC, "%s does not name a component", istr(comp_name));
1✔
9116
      comp = NULL;
9117
   }
9118

9119
   push_scope(nametab);
98✔
9120

9121
   bool is_open = (peek() == tUSE && peek_nth(2) == tOPEN);
98✔
9122
   tree_t bind = p_binding_indication(comp);
98✔
9123
   if (!is_open && bind == NULL)
98✔
9124
      parse_error(CURRENT_LOC, "a binding indication in an explicit "
1✔
9125
                  "configuration specification must contain an entity aspect");
9126
   consume(tSEMI);
98✔
9127

9128
   if (ids != NULL) {
98✔
9129
      for (ident_list_t *it = ids; it != NULL; it = it->next) {
191✔
9130
         tree_t t = tree_new(T_SPEC);
96✔
9131
         tree_set_loc(t, &(it->loc));
96✔
9132
         tree_set_ident(t, it->ident);
96✔
9133
         tree_set_ident2(t, comp_name);
96✔
9134
         tree_set_value(t, bind);
96✔
9135
         tree_set_ref(t, comp);
96✔
9136

9137
         const spec_kind_t kind =
192✔
9138
            it->ident == well_known(W_ALL) ? SPEC_ALL : SPEC_EXACT;
96✔
9139

9140
         tree_add_decl(parent, t);
96✔
9141
         insert_spec(nametab, t, kind, it->ident, 1);
96✔
9142
         sem_check(t, nametab);
96✔
9143
      }
9144
   }
9145
   else {
9146
      // Instantiation list was "others"
9147
      tree_t t = tree_new(T_SPEC);
3✔
9148
      tree_set_loc(t, CURRENT_LOC);
3✔
9149
      tree_set_ident2(t, comp_name);
3✔
9150
      tree_set_value(t, bind);
3✔
9151
      tree_set_ref(t, comp);
3✔
9152

9153
      tree_add_decl(parent, t);
3✔
9154
      insert_spec(nametab, t, SPEC_OTHERS, NULL, 1);
3✔
9155
      sem_check(t, nametab);
3✔
9156
   }
9157

9158
   pop_scope(nametab);
98✔
9159
}
98✔
9160

9161
static void p_configuration_declarative_part(tree_t unit)
6✔
9162
{
9163
   // use_clause | attribute_specification | group_declaration
9164

9165
   BEGIN("configuration declarative part");
12✔
9166

9167
   switch (peek()) {
6✔
9168
   case tUSE:
3✔
9169
      p_use_clause(unit, tree_add_decl);
3✔
9170
      break;
3✔
9171

9172
   case tATTRIBUTE:
3✔
9173
      p_attribute_specification(unit);
3✔
9174
      break;
3✔
9175

UNCOV
9176
   case tGROUP:
×
UNCOV
9177
      if (peek_nth(3) == tIS)
×
UNCOV
9178
         tree_add_decl(unit, p_group_template_declaration());
×
9179
      else
9180
         tree_add_decl(unit, p_group_declaration());
×
9181
      break;
9182

UNCOV
9183
   default:
×
9184
      expect(tUSE, tATTRIBUTE, tGROUP);
×
9185
   }
9186
}
6✔
9187

9188
static void p_component_configuration(tree_t unit)
116✔
9189
{
9190
   // for component_specification [ binding_indication ; ]
9191
   //   [ block_configuration ] end for ;
9192

9193
   BEGIN("component configuration");
232✔
9194

9195
   consume(tFOR);
116✔
9196

9197
   ident_t comp_name;
116✔
9198
   LOCAL_IDENT_LIST ids = p_component_specification(&comp_name);
232✔
9199

9200
   tree_t comp = resolve_name(nametab, CURRENT_LOC, comp_name);
116✔
9201
   if (comp != NULL && tree_kind(comp) != T_COMPONENT) {
116✔
UNCOV
9202
      parse_error(CURRENT_LOC, "%s does not name a component", istr(comp_name));
×
9203
      comp = NULL;
9204
   }
9205

9206
   push_scope(nametab);
116✔
9207

9208
   // TODO: should be optional
9209
   tree_t bind = p_binding_indication(comp);
116✔
9210
   consume(tSEMI);
116✔
9211

9212
   tree_t bcfg = NULL;
116✔
9213
   if (peek() == tFOR) {
116✔
9214
      tree_t of = tree_has_ref(bind) ? tree_ref(bind) : NULL;
2✔
9215
      bcfg = p_block_configuration(of);
2✔
9216
   }
9217

9218
   if (ids != NULL) {
116✔
9219
      for (ident_list_t *it = ids; it != NULL; it = it->next) {
224✔
9220
         tree_t t = tree_new(T_SPEC);
112✔
9221
         tree_set_loc(t, &(it->loc));
112✔
9222
         tree_set_ident(t, it->ident);
112✔
9223
         tree_set_ident2(t, comp_name);
112✔
9224
         tree_set_value(t, bind);
112✔
9225
         tree_set_ref(t, comp);
112✔
9226
         if (bcfg != NULL) tree_add_decl(t, bcfg);
112✔
9227

9228
         const spec_kind_t kind =
224✔
9229
            it->ident == well_known(W_ALL) ? SPEC_ALL : SPEC_EXACT;
112✔
9230

9231
         tree_add_decl(unit, t);
112✔
9232
         sem_check(t, nametab);
112✔
9233
         insert_spec(nametab, t, kind, it->ident, 1);
112✔
9234
      }
9235
   }
9236
   else {
9237
      // Instantiation list was "others"
9238
      tree_t t = tree_new(T_SPEC);
4✔
9239
      tree_set_loc(t, CURRENT_LOC);
4✔
9240
      tree_set_ident2(t, comp_name);
4✔
9241
      tree_set_value(t, bind);
4✔
9242
      tree_set_ref(t, comp);
4✔
9243
      if (bcfg != NULL) tree_add_decl(t, bcfg);
4✔
9244

9245
      tree_add_decl(unit, t);
4✔
9246
      sem_check(t, nametab);
4✔
9247
      insert_spec(nametab, t, SPEC_OTHERS, NULL, 1);
4✔
9248
   }
9249

9250
   pop_scope(nametab);
116✔
9251

9252
   consume(tEND);
116✔
9253
   consume(tFOR);
116✔
9254
   consume(tSEMI);
116✔
9255
}
116✔
9256

9257
static void p_configuration_item(tree_t unit)
165✔
9258
{
9259
   // block_configuration | component_configuration
9260

9261
   BEGIN("configuration item");
330✔
9262

9263
   const token_t third = peek_nth(3);
165✔
9264
   if ((third == tCOLON) || (third == tCOMMA))
165✔
9265
      p_component_configuration(unit);
116✔
9266
   else
9267
      tree_add_decl(unit, p_block_configuration(NULL));
49✔
9268
}
165✔
9269

9270
static tree_t p_index_specification(void)
26✔
9271
{
9272
   // discrete_range | expression
9273

9274
   tree_t head = p_expression();
26✔
9275

9276
   if (scan(tTO, tDOWNTO))
26✔
9277
      return p_discrete_range(head);
10✔
9278
   else
9279
      return head;
9280
}
9281

9282
static void p_block_specification(tree_t b)
166✔
9283
{
9284
   // label | label [ ( index_specification ) ]
9285

9286
   BEGIN("block specification");
332✔
9287

9288
   ident_t id = p_identifier();
166✔
9289
   tree_set_ident(b, id);
166✔
9290

9291
   if (optional(tLPAREN)) {
166✔
9292
      (void)p_index_specification();    // XXX: not used
26✔
9293
      consume(tRPAREN);
26✔
9294
   }
9295
}
166✔
9296

9297
static tree_t p_block_configuration(tree_t of)
166✔
9298
{
9299
   // for block_specification { use_clause } { configuration_item } end for ;
9300

9301
   BEGIN("block configuration");
166✔
9302

9303
   consume(tFOR);
166✔
9304

9305
   tree_t b = tree_new(T_BLOCK_CONFIG);
166✔
9306
   p_block_specification(b);
166✔
9307

9308
   push_scope(nametab);
166✔
9309

9310
   tree_t sub = NULL;
166✔
9311
   if (of != NULL) {
166✔
9312
      switch (tree_kind(of)) {
116✔
9313
      case T_ENTITY:
114✔
9314
         {
9315
            ident_t qual = ident_prefix(tree_ident(of), tree_ident(b), '-');
114✔
9316
            if ((sub = lib_get_qualified(qual)) == NULL)
114✔
9317
               parse_error(CURRENT_LOC, "cannot find architecture %s of "
1✔
9318
                           "entity %s", istr(tree_ident(b)),
9319
                           istr(tree_ident(of)));
9320
         }
9321
         break;
9322
      case T_ARCH:
2✔
9323
         {
9324
            ident_t expect = ident_rfrom(tree_ident(of), '-');
2✔
9325
            if (tree_ident(b) != expect)
2✔
UNCOV
9326
               parse_error(CURRENT_LOC, "block specification label %s does not "
×
9327
                           "match architecture name %s", istr(tree_ident(b)),
9328
                           istr(expect));
9329
            else
9330
               sub = of;
9331
         }
9332
         break;
UNCOV
9333
      default:
×
9334
         fatal_trace("unexpected unit type %s in block configuration",
9335
                     tree_kind_str(tree_kind(of)));
9336
         break;
9337
      }
9338
   }
9339
   else
9340
      sub = resolve_name(nametab, CURRENT_LOC, tree_ident(b));
50✔
9341

9342
   if (sub != NULL && !is_implicit_block(sub)) {
166✔
9343
      parse_error(CURRENT_LOC, "%s is not a block that can be configured",
1✔
9344
                  istr(tree_ident(b)));
9345
      sub = NULL;
9346
   }
9347

9348
   if (sub != NULL) {
165✔
9349
      tree_set_ref(b, sub);
163✔
9350

9351
      if (tree_kind(sub) == T_IF_GENERATE)
163✔
9352
         sub = tree_cond(sub, 0);
6✔
9353

9354
      insert_names_for_config(nametab, sub);
163✔
9355
   }
9356
   else
9357
      suppress_errors(nametab);
3✔
9358

9359
   while (not_at_token(tEND))
331✔
9360
      p_configuration_item(b);
165✔
9361

9362
   if (sub != NULL) {
166✔
9363
      const int nstmts = tree_stmts(sub);
163✔
9364
      for (int i = 0; i < nstmts; i++) {
681✔
9365
         tree_t s = tree_stmt(sub, i);
518✔
9366
         if (tree_kind(s) == T_INSTANCE)
518✔
9367
            query_spec(nametab, s);
143✔
9368
      }
9369

9370
   }
9371

9372
   pop_scope(nametab);
166✔
9373

9374
   consume(tEND);
166✔
9375
   consume(tFOR);
166✔
9376
   consume(tSEMI);
166✔
9377

9378
   tree_set_loc(b, CURRENT_LOC);
166✔
9379
   sem_check(b, nametab);
166✔
9380
   return b;
166✔
9381
}
9382

9383
static ident_t p_entity_name(tree_t *entity)
5,083✔
9384
{
9385
   // name
9386

9387
   BEGIN("entity name");
10,166✔
9388

9389
   ident_t ename = p_selected_identifier();
5,083✔
9390

9391
   ident_t qual = ename;
5,083✔
9392
   if (ident_runtil(ename, '.') == ename)
5,083✔
9393
      qual = ident_prefix(lib_name(lib_work()), ename, '.');
5,081✔
9394

9395
   *entity = resolve_name(nametab, CURRENT_LOC, qual);
5,083✔
9396

9397
   if (*entity != NULL && tree_kind(*entity) != T_ENTITY) {
5,083✔
9398
      diag_t *d = diag_new(DIAG_ERROR, CURRENT_LOC);
2✔
9399
      diag_printf(d, "%s%s is not an entity",
4✔
9400
                  is_design_unit(*entity) ? "design unit " : "", istr(ename));
2✔
9401
      diag_hint(d, tree_loc(*entity), "%s is a %s", istr(ename),
2✔
9402
                class_str(class_of(*entity)));
9403
      diag_emit(d);
2✔
9404
      *entity = NULL;
2✔
9405
      return ename;
2✔
9406
   }
9407

9408
   return ename;
9409
}
9410

9411
static void p_configuration_declaration(tree_t unit)
115✔
9412
{
9413
   // configuration identifier of name is configuration_declarative_part
9414
   //   block_configuration end [ configuration ] [ simple_name ] ;
9415

9416
   BEGIN("configuration declaration");
230✔
9417

9418
   consume(tCONFIGURATION);
115✔
9419

9420
   tree_change_kind(unit, T_CONFIGURATION);
115✔
9421

9422
   ident_t id = p_identifier();
115✔
9423
   tree_set_ident(unit, id);
115✔
9424

9425
   push_scope(nametab);
115✔
9426

9427
   consume(tOF);
115✔
9428

9429
   tree_t of = NULL;
115✔
9430
   ident_t ename = p_entity_name(&of);
115✔
9431
   tree_set_ident2(unit, ename);
115✔
9432

9433
   if (of != NULL) {
115✔
9434
      tree_set_primary(unit, of);
114✔
9435
      insert_name(nametab, of, ename);
114✔
9436
      insert_decls(nametab, of);
114✔
9437
   }
9438

9439
   consume(tIS);
115✔
9440

9441
   while (not_at_token(tFOR))
121✔
9442
      p_configuration_declarative_part(unit);
6✔
9443

9444
   tree_add_decl(unit, p_block_configuration(of));
115✔
9445

9446
   consume(tEND);
115✔
9447
   optional(tCONFIGURATION);
115✔
9448
   p_trailing_label(id);
115✔
9449
   consume(tSEMI);
115✔
9450

9451
   pop_scope(nametab);
115✔
9452

9453
   tree_set_loc(unit, CURRENT_LOC);
115✔
9454
   sem_check(unit, nametab);
115✔
9455

9456
   tree_set_ident(unit, ident_prefix(lib_name(lib_work()), id, '.'));
115✔
9457
}
115✔
9458

9459
static void p_context_declaration(tree_t unit)
19✔
9460
{
9461
   // 2008: context identifier is context_clause end [ context ]
9462
   //       [ context_simple_name ] ;
9463

9464
   BEGIN("context declaration");
38✔
9465

9466
   consume(tCONTEXT);
19✔
9467

9468
   tree_change_kind(unit, T_CONTEXT);
19✔
9469

9470
   ident_t id = p_identifier();
19✔
9471
   tree_set_ident(unit, ident_prefix(lib_name(lib_work()), id, '.'));
19✔
9472

9473
   consume(tIS);
19✔
9474

9475
   push_scope(nametab);
19✔
9476

9477
   // LRM 08 section 13.1 forbids preceeding context clause
9478
   if (tree_contexts(unit) != 3)     // Implicit WORK and STD
19✔
9479
      parse_error(tree_loc(tree_context(unit, 3)), "context clause preceeding "
1✔
9480
                  "context declaration must be empty");
9481

9482
   p_context_clause(unit);
19✔
9483

9484
   consume(tEND);
19✔
9485
   optional(tCONTEXT);
19✔
9486
   p_trailing_label(id);
19✔
9487
   consume(tSEMI);
19✔
9488

9489
   tree_set_loc(unit, CURRENT_LOC);
19✔
9490
   sem_check(unit, nametab);
19✔
9491

9492
   pop_scope(nametab);
19✔
9493
}
19✔
9494

9495
static void p_primary_unit(tree_t unit)
6,580✔
9496
{
9497
   // entity_declaration | configuration_declaration | package_declaration
9498
   //   | 2008: package_instantiation_declaration
9499

9500
   BEGIN("primary unit");
13,160✔
9501

9502
   switch (peek()) {
6,580✔
9503
   case tENTITY:
4,952✔
9504
      p_entity_declaration(unit);
4,952✔
9505
      break;
4,952✔
9506

9507
   case tPACKAGE:
1,494✔
9508
      if (standard() >= STD_08 && peek_nth(4) == tNEW)
1,494✔
9509
         p_package_instantiation_declaration(unit);
70✔
9510
      else
9511
         p_package_declaration(unit);
1,424✔
9512
      break;
9513

9514
   case tCONFIGURATION:
115✔
9515
      p_configuration_declaration(unit);
115✔
9516
      break;
115✔
9517

9518
   case tCONTEXT:
19✔
9519
      p_context_declaration(unit);
19✔
9520
      break;
19✔
9521

UNCOV
9522
   default:
×
UNCOV
9523
      expect(tENTITY, tPACKAGE, tCONFIGURATION);
×
9524
   }
9525
}
6,580✔
9526

9527
static void p_block_declarative_item(tree_t parent)
12,162✔
9528
{
9529
   // subprogram_declaration | subprogram_body
9530
   //   | 2008: package_instantiation_declaration | type_declaration
9531
   //   | subtype_declaration | constant_declaration | signal_declaration
9532
   //   | shared_variable_declaration | file_declaration | alias_declaration
9533
   //   | component_declaration | attribute_declaration
9534
   //   | attribute_specification | configuration_specification
9535
   //   | disconnection_specification | use_clause | group_template_declaration
9536
   //   | group_declaration | 2008: subprogram_instantiation_declaration
9537
   //   | 2008: psl_clock_declaration | 2008: package_declaration
9538
   //   | 2019: mode_view_declaration
9539

9540
   BEGIN("block declarative item");
24,324✔
9541

9542
   switch (peek()) {
12,162✔
9543
   case tSIGNAL:
5,142✔
9544
      p_signal_declaration(parent);
5,142✔
9545
      break;
5,142✔
9546

9547
   case tTYPE:
2,336✔
9548
      p_type_declaration(parent);
2,336✔
9549
      break;
2,336✔
9550

9551
   case tSUBTYPE:
425✔
9552
      tree_add_decl(parent, p_subtype_declaration());
425✔
9553
      break;
425✔
9554

9555
   case tFILE:
32✔
9556
      p_file_declaration(parent);
32✔
9557
      break;
32✔
9558

9559
   case tCONSTANT:
1,409✔
9560
      p_constant_declaration(parent);
1,409✔
9561
      break;
1,409✔
9562

9563
   case tFUNCTION:
1,793✔
9564
   case tPROCEDURE:
9565
   case tIMPURE:
9566
   case tPURE:
9567
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
1,793✔
9568
         tree_add_decl(parent, p_subprogram_instantiation_declaration());
49✔
9569
      else {
9570
         tree_t spec = p_subprogram_specification();
1,744✔
9571
         if (peek() == tSEMI)
1,744✔
9572
            tree_add_decl(parent, p_subprogram_declaration(spec));
161✔
9573
         else
9574
            tree_add_decl(parent, p_subprogram_body(spec));
1,583✔
9575
      }
9576
      break;
9577

9578
   case tALIAS:
137✔
9579
      p_alias_declaration(parent);
137✔
9580
      break;
137✔
9581

9582
   case tATTRIBUTE:
92✔
9583
      if (peek_nth(3) == tOF)
92✔
9584
         p_attribute_specification(parent);
78✔
9585
      else
9586
         tree_add_decl(parent, p_attribute_declaration());
14✔
9587
      break;
9588

9589
   case tFOR:
98✔
9590
      p_configuration_specification(parent);
98✔
9591
      break;
98✔
9592

9593
   case tCOMPONENT:
273✔
9594
      tree_add_decl(parent, p_component_declaration());
273✔
9595
      break;
273✔
9596

9597
   case tUSE:
41✔
9598
      p_use_clause(parent, tree_add_decl);
41✔
9599
      break;
41✔
9600

9601
   case tSHARED:
119✔
9602
      p_variable_declaration(parent);
119✔
9603
      break;
119✔
9604

9605
   case tDISCONNECT:
13✔
9606
      p_disconnection_specification(parent);
13✔
9607
      break;
13✔
9608

9609
   case tGROUP:
2✔
9610
      if (peek_nth(3) == tIS)
2✔
9611
         tree_add_decl(parent, p_group_template_declaration());
1✔
9612
      else
9613
         tree_add_decl(parent, p_group_declaration());
1✔
9614
      break;
9615

9616
   case tPACKAGE:
175✔
9617
      if (peek_nth(4) == tNEW)
175✔
9618
         tree_add_decl(parent, p_package_instantiation_declaration(NULL));
171✔
9619
      else if (peek_nth(2) == tBODY) {
4✔
UNCOV
9620
         require_std(STD_08, "nested package declarations");
×
UNCOV
9621
         tree_add_decl(parent, p_package_body(NULL));
×
9622
      }
9623
      else {
9624
         require_std(STD_08, "nested package declarations");
4✔
9625
         tree_add_decl(parent, p_package_declaration(NULL));
4✔
9626
      }
9627
      break;
9628

9629
   case tSTARTPSL:
35✔
9630
      consume(tSTARTPSL);
35✔
9631
      p_psl_declaration(parent);
35✔
9632
      break;
35✔
9633

9634
   case tDEFAULT:
22✔
9635
   case tSEQUENCE:
9636
   case tPROPERTY:
9637
   case tENDPOINT:
9638
      p_psl_declaration(parent);
22✔
9639
      break;
22✔
9640

9641
   case tVIEW:
11✔
9642
      tree_add_decl(parent, p_mode_view_declaration());
11✔
9643
      break;
11✔
9644

9645
   default:
7✔
9646
      expect(tSIGNAL, tTYPE, tSUBTYPE, tFILE, tCONSTANT, tFUNCTION, tIMPURE,
21✔
9647
             tPURE, tPROCEDURE, tALIAS, tATTRIBUTE, tFOR, tCOMPONENT, tUSE,
9648
             tSHARED, tDISCONNECT, tGROUP, STD(08, tPACKAGE), STD(19, tVIEW));
9649
   }
9650
}
12,162✔
9651

9652
static tree_t p_target(tree_t name)
23,747✔
9653
{
9654
   // name | aggregate
9655

9656
   BEGIN("target");
47,494✔
9657

9658
   if (name == NULL) {
23,747✔
9659
      if (peek() == tLPAREN)
77✔
9660
         return p_aggregate();
23✔
9661
      else
9662
         return p_name(0);
54✔
9663
   }
9664
   else
9665
      return name;
9666
}
9667

9668
static tree_t p_simple_variable_assignment(ident_t label, tree_t name)
16,462✔
9669
{
9670
   // [ label : ] target := expression ;
9671
   // 2019: [ label : ] target := conditional_or_unaffected_expression ;
9672

9673
   EXTEND("simple variable assignment");
16,462✔
9674

9675
   tree_t target = p_target(name);
16,462✔
9676

9677
   consume(tWALRUS);
16,462✔
9678

9679
   tree_t value = p_conditional_or_unaffected_expression(STD_08);
16,462✔
9680

9681
   type_t target_type = solve_target(nametab, target, value);
16,462✔
9682
   solve_known_subtype(nametab, value, target_type);
16,462✔
9683

9684
   tree_t t = tree_new(T_VAR_ASSIGN);
16,462✔
9685
   tree_set_target(t, target);
16,462✔
9686
   tree_set_value(t, value);
16,462✔
9687

9688
   consume(tSEMI);
16,462✔
9689

9690
   tree_set_loc(t, CURRENT_LOC);
16,462✔
9691
   ensure_labelled(t, label);
16,462✔
9692

9693
   sem_check(t, nametab);
16,462✔
9694
   return t;
16,462✔
9695
}
9696

9697
static void p_selected_expressions(tree_t stmt, tree_t target)
9✔
9698
{
9699
   // { expression when choices , } expression when choices
9700

9701
   BEGIN("selected waveforms");
18✔
9702

9703
   type_t with_type = tree_type(tree_value(stmt));
9✔
9704

9705
   do {
24✔
9706
      tree_t expr = p_expression();
24✔
9707

9708
      type_t constraint;
24✔
9709
      if (tree_has_type(target))
24✔
9710
         constraint = tree_type(target);
15✔
9711
      else
9712
         constraint = solve_target(nametab, target, expr);
9✔
9713

9714
      solve_known_subtype(nametab, expr, constraint);
24✔
9715

9716
      tree_t a = tree_new(T_VAR_ASSIGN);
24✔
9717
      tree_set_target(a, target);
24✔
9718
      tree_set_value(a, expr);
24✔
9719

9720
      sem_check(a, nametab);
24✔
9721

9722
      consume(tWHEN);
24✔
9723

9724
      tree_t alt = tree_new(T_ALTERNATIVE);
24✔
9725
      tree_add_stmt(alt, a);
24✔
9726

9727
      p_choices(alt, NULL, with_type);
24✔
9728

9729
      tree_set_loc(alt, CURRENT_LOC);
24✔
9730
      tree_add_stmt(stmt, alt);
24✔
9731
   } while (optional(tCOMMA));
24✔
9732
}
9✔
9733

9734
static tree_t p_selected_variable_assignment(ident_t label)
12✔
9735
{
9736
   // with expression select [ ? ] target := selected_expressions ;
9737

9738
   BEGIN("selected variable assignment");
12✔
9739

9740
   require_std(STD_08, "selected variable assignment");
12✔
9741

9742
   consume(tWITH);
12✔
9743

9744
   tree_t value = p_expression();
12✔
9745
   solve_types(nametab, value, NULL);
12✔
9746

9747
   consume(tSELECT);
12✔
9748

9749
   tree_kind_t kind = T_SELECT;
12✔
9750
   if (optional(tQUESTION))
12✔
9751
      kind = T_MATCH_SELECT;
6✔
9752

9753
   tree_t t = tree_new(kind);
12✔
9754
   tree_set_value(t, value);
12✔
9755

9756
   tree_t target = p_target(NULL);
12✔
9757

9758
   // This is the easiest place to disambiguate variable and signal
9759
   // assignment without a deep lookahead
9760
   switch (one_of(tWALRUS, tLE)) {
12✔
9761
   case tLE:
3✔
9762
      p_selected_waveforms(t, target, NULL);
3✔
9763
      break;
3✔
9764

9765
   case tWALRUS:
9✔
9766
   default:
9767
      p_selected_expressions(t, target);
9✔
9768
      break;
9✔
9769
   }
9770

9771
   consume(tSEMI);
12✔
9772

9773
   tree_set_loc(t, CURRENT_LOC);
12✔
9774
   ensure_labelled(t, label);
12✔
9775

9776
   sem_check(t, nametab);
12✔
9777
   return t;
12✔
9778
}
9779

9780
static tree_t p_variable_assignment_statement(ident_t label, tree_t name)
16,474✔
9781
{
9782
   // [ label : ] simple_variable_assignment
9783
   //   | 2008: [ label : ] conditional_variable_assignment
9784
   //   | 2008: [ label : ] selected_variable_assignment
9785

9786
   EXTEND("variable assignment statement");
32,948✔
9787

9788
   if (name == NULL && peek() == tWITH)
16,474✔
9789
      return p_selected_variable_assignment(label);
12✔
9790
   else
9791
      return p_simple_variable_assignment(label, name);
16,462✔
9792
}
9793

9794
static tree_t p_waveform_element(tree_t target)
7,738✔
9795
{
9796
   // expression [ after expression ] | null [ after expression ]
9797

9798
   BEGIN("waveform element");
7,738✔
9799

9800
   tree_t w = tree_new(T_WAVEFORM);
7,738✔
9801

9802
   if (!optional(tNULL)) {
7,738✔
9803
      tree_t value = p_expression();
7,721✔
9804
      tree_set_value(w, value);
7,721✔
9805

9806
      type_t constraint;
7,721✔
9807
      if (tree_has_type(target))
7,721✔
9808
         constraint = tree_type(target);
573✔
9809
      else
9810
         constraint = solve_target(nametab, target, value);
7,148✔
9811

9812
      solve_known_subtype(nametab, value, constraint);
7,721✔
9813
   }
9814
   else if (!tree_has_type(target))
17✔
9815
      solve_types(nametab, target, NULL);
16✔
9816

9817
   if (optional(tAFTER)) {
7,738✔
9818
      tree_t delay = p_expression();
869✔
9819
      tree_set_delay(w, delay);
869✔
9820
      solve_types(nametab, delay, std_type(NULL, STD_TIME));
869✔
9821
   }
9822

9823
   tree_set_loc(w, CURRENT_LOC);
7,738✔
9824

9825
   return w;
7,738✔
9826
}
9827

9828
static void p_waveform(tree_t stmt, tree_t target)
7,513✔
9829
{
9830
   // waveform_element { , waveform_element } | unaffected
9831

9832
   BEGIN("waveform");
15,020✔
9833

9834
   if (optional(tUNAFFECTED)) {
7,513✔
9835
      solve_types(nametab, target, NULL);
6✔
9836
      return;
6✔
9837
   }
9838

9839
   do {
7,738✔
9840
      tree_add_waveform(stmt, p_waveform_element(target));
7,738✔
9841
   } while (optional(tCOMMA));
7,738✔
9842

9843
   tree_set_loc(stmt, CURRENT_LOC);
7,507✔
9844
}
9845

9846
static tree_t p_delay_mechanism(void)
7,176✔
9847
{
9848
   // transport | [ reject expression ] inertial
9849

9850
   BEGIN("delay mechanism");
14,352✔
9851

9852
   switch (peek()) {
7,176✔
9853
   case tTRANSPORT:
66✔
9854
      consume(tTRANSPORT);
66✔
9855
      return get_time(0, CURRENT_LOC);
66✔
9856

9857
   case tREJECT:
12✔
9858
      {
9859
         consume(tREJECT);
12✔
9860
         tree_t t = p_expression();
12✔
9861
         consume(tINERTIAL);
12✔
9862
         return t;
12✔
9863
      }
9864

9865
   case tINERTIAL:
1✔
9866
      consume(tINERTIAL);
1✔
9867
      return NULL;
1✔
9868

9869
   default:
9870
      return NULL;
9871
   }
9872
}
9873

9874
static port_mode_t p_force_mode(void)
97✔
9875
{
9876
   // in | out
9877

9878
   BEGIN("force mode");
194✔
9879

9880
   switch (peek()) {
97✔
9881
   case tIN: consume(tIN); return PORT_IN;
4✔
9882
   case tOUT: consume(tOUT); return PORT_OUT;
7✔
9883
   default: return PORT_INVALID;
9884
   }
9885
}
9886

9887
static tree_t p_simple_force_assignment(ident_t label, tree_t target)
61✔
9888
{
9889
   // target <= force [ force_mode ] expression ;
9890

9891
   EXTEND("simple force assignment");
61✔
9892

9893
   consume(tFORCE);
61✔
9894

9895
   require_std(STD_08, "simple force assignments");
61✔
9896

9897
   type_t target_type;
61✔
9898
   if (tree_kind(target) == T_AGGREGATE) {
61✔
9899
      parse_error(CURRENT_LOC, "target of a simple force assignment may "
1✔
9900
                  "not be an aggregate");
9901
      target_type = type_new(T_NONE);
1✔
9902
   }
9903
   else
9904
      target_type = solve_types(nametab, target, NULL);
60✔
9905

9906
   tree_set_type(target, target_type);
61✔
9907

9908
   tree_t t = tree_new(T_FORCE);
61✔
9909
   tree_set_target(t, target);
61✔
9910
   tree_set_subkind(t, p_force_mode());
61✔
9911

9912
   tree_t expr = p_expression();
61✔
9913
   solve_types(nametab, expr, target_type);
61✔
9914

9915
   tree_set_value(t, expr);
61✔
9916

9917
   consume(tSEMI);
61✔
9918

9919
   set_label_and_loc(t, label, CURRENT_LOC);
61✔
9920
   sem_check(t, nametab);
61✔
9921
   return t;
61✔
9922
}
9923

9924
static tree_t p_simple_release_assignment(ident_t label, tree_t target)
36✔
9925
{
9926
   // target <= release [ force_mode ] ;
9927

9928
   EXTEND("simple force assignment");
36✔
9929

9930
   consume(tRELEASE);
36✔
9931

9932
   require_std(STD_08, "simple release assignments");
36✔
9933

9934
   type_t target_type;
36✔
9935
   if (tree_kind(target) == T_AGGREGATE) {
36✔
9936
      parse_error(CURRENT_LOC, "target of a simple release assignment may "
1✔
9937
                  "not be an aggregate");
9938
      target_type = type_new(T_NONE);
1✔
9939
   }
9940
   else
9941
      target_type = solve_types(nametab, target, NULL);
35✔
9942

9943
   tree_set_type(target, target_type);
36✔
9944

9945
   tree_t t = tree_new(T_RELEASE);
36✔
9946
   tree_set_target(t, target);
36✔
9947
   tree_set_subkind(t, p_force_mode());
36✔
9948

9949
   consume(tSEMI);
36✔
9950

9951
   set_label_and_loc(t, label, CURRENT_LOC);
36✔
9952
   sem_check(t, nametab);
36✔
9953
   return t;
36✔
9954
}
9955

9956
static tree_t p_signal_assignment_statement(ident_t label, tree_t name)
5,079✔
9957
{
9958
   // [ label : ] target <= [ delay_mechanism ] waveform ;
9959

9960
   EXTEND("signal assignment statement");
10,158✔
9961

9962
   tree_t target = p_target(name);
5,079✔
9963

9964
   consume(tLE);
5,079✔
9965

9966
   switch (peek()) {
5,079✔
9967
   case tFORCE: return p_simple_force_assignment(label, target);
61✔
9968
   case tRELEASE: return p_simple_release_assignment(label, target);
36✔
9969
   default: break;
4,982✔
9970
   }
9971

9972

9973
   tree_t t = tree_new(T_SIGNAL_ASSIGN);
4,982✔
9974
   tree_set_target(t, target);
4,982✔
9975

9976
   tree_t reject = p_delay_mechanism();
4,982✔
9977

9978
   p_waveform(t, target);
4,982✔
9979

9980
   if (peek() == tWHEN) {
4,982✔
9981
      require_std(STD_08, "conditional signal assignment statements");
5✔
9982

9983
      tree_t stmt = tree_new(T_COND_ASSIGN);
5✔
9984
      tree_set_target(stmt, target);
5✔
9985

9986
      p_conditional_waveforms(stmt, target, t);
5✔
9987

9988
      const int nconds = tree_conds(stmt);
5✔
9989
      for (int i = 0; i < nconds; i++) {
15✔
9990
         tree_t c = tree_cond(stmt, i);
10✔
9991
         assert(tree_stmts(c) == 1);
10✔
9992
         set_delay_mechanism(tree_stmt(c, 0), reject);
10✔
9993
      }
9994

9995
      t = stmt;
9996
   }
9997
   else
9998
      set_delay_mechanism(t, reject);
4,977✔
9999

10000
   consume(tSEMI);
4,982✔
10001

10002
   set_label_and_loc(t, label, CURRENT_LOC);
4,982✔
10003
   sem_check(t, nametab);
4,982✔
10004
   return t;
4,982✔
10005
}
10006

10007
static void p_sensitivity_clause(tree_t wait)
337✔
10008
{
10009
   // on sensitivity_list
10010

10011
   BEGIN("sensitivity clause");
674✔
10012

10013
   consume(tON);
337✔
10014
   p_sensitivity_list(wait);
337✔
10015
}
337✔
10016

10017
static void p_condition_clause(tree_t wait)
275✔
10018
{
10019
   // until condition
10020

10021
   BEGIN("condition clause");
550✔
10022

10023
   consume(tUNTIL);
275✔
10024

10025
   tree_set_value(wait, p_condition());
275✔
10026
}
275✔
10027

10028
static void p_timeout_clause(tree_t wait)
4,995✔
10029
{
10030
   // for expression
10031

10032
   BEGIN("timeout clause");
9,990✔
10033

10034
   consume(tFOR);
4,995✔
10035

10036
   tree_t delay = p_expression();
4,995✔
10037
   tree_set_delay(wait, delay);
4,995✔
10038
   solve_types(nametab, delay, std_type(NULL, STD_TIME));
4,995✔
10039
}
4,995✔
10040

10041
static tree_t p_wait_statement(ident_t label)
9,348✔
10042
{
10043
   // [ label : ] wait [ sensitivity_clause ] [ condition_clause ]
10044
   //   [ timeout_clause ] ;
10045

10046
   EXTEND("wait statement");
9,348✔
10047

10048
   tree_t t = tree_new(T_WAIT);
9,348✔
10049

10050
   consume(tWAIT);
9,348✔
10051

10052
   if (peek() == tON)
9,348✔
10053
      p_sensitivity_clause(t);
337✔
10054

10055
   if (peek() == tUNTIL)
9,348✔
10056
      p_condition_clause(t);
275✔
10057

10058
   if (peek() == tFOR)
9,348✔
10059
      p_timeout_clause(t);
4,995✔
10060

10061
   consume(tSEMI);
9,348✔
10062

10063
   set_label_and_loc(t, label, CURRENT_LOC);
9,348✔
10064
   sem_check(t, nametab);
9,348✔
10065
   return t;
9,348✔
10066
}
10067

10068
static tree_t p_assertion_statement(ident_t label)
16,287✔
10069
{
10070
   // [ label : ] assertion ;
10071

10072
   EXTEND("assertion statement");
16,287✔
10073

10074
   tree_t t = p_assertion();
16,287✔
10075
   consume(tSEMI);
16,287✔
10076

10077
   set_label_and_loc(t, label, CURRENT_LOC);
16,287✔
10078
   sem_check(t, nametab);
16,287✔
10079
   return t;
16,287✔
10080
}
10081

10082
static tree_t p_report_statement(ident_t label)
2,014✔
10083
{
10084
   // [ label : ] report expression [ severity expression ] ;
10085

10086
   EXTEND("report statement");
2,014✔
10087

10088
   tree_t t = tree_new(T_REPORT);
2,014✔
10089

10090
   consume(tREPORT);
2,014✔
10091

10092
   tree_t m = p_expression();
2,014✔
10093
   tree_set_message(t, m);
2,014✔
10094
   solve_types(nametab, m, std_type(NULL, STD_STRING));
2,014✔
10095

10096
   if (optional(tSEVERITY)) {
2,014✔
10097
      tree_t s = p_expression();
437✔
10098
      solve_types(nametab, s, std_type(NULL, STD_SEVERITY_LEVEL));
437✔
10099
      tree_set_severity(t, s);
437✔
10100
   }
10101

10102
   consume(tSEMI);
2,014✔
10103

10104
   set_label_and_loc(t, label, CURRENT_LOC);
2,014✔
10105
   sem_check(t, nametab);
2,014✔
10106
   return t;
2,014✔
10107
}
10108

10109
static tree_t p_if_statement(ident_t label)
8,682✔
10110
{
10111
   // [ label : ] if condition then sequence_of_statements
10112
   //   { elsif condition then sequence_of_statements }
10113
   //   [ else sequence_of_statements ] end if [ label ] ;
10114

10115
   EXTEND("if statement");
8,682✔
10116

10117
   tree_t t = tree_new(T_IF);
8,682✔
10118
   consume(tIF);
8,682✔
10119

10120
   tree_t c0 = tree_new(T_COND_STMT);
8,682✔
10121
   tree_set_value(c0, p_condition());
8,682✔
10122
   tree_add_cond(t, c0);
8,682✔
10123

10124
   consume(tTHEN);
8,682✔
10125

10126
   p_sequence_of_statements(c0);
8,682✔
10127

10128
   tree_set_loc(c0, CURRENT_LOC);
8,682✔
10129

10130
   while (optional(tELSIF)) {
9,575✔
10131
      tree_t c = tree_new(T_COND_STMT);
893✔
10132
      tree_set_value(c, p_condition());
893✔
10133
      tree_add_cond(t, c);
893✔
10134

10135
      consume(tTHEN);
893✔
10136

10137
      p_sequence_of_statements(c);
893✔
10138

10139
      tree_set_loc(c, CURRENT_LOC);
893✔
10140
   }
10141

10142
   if (optional(tELSE)) {
8,682✔
10143
      tree_t c = tree_new(T_COND_STMT);
2,329✔
10144
      tree_add_cond(t, c);
2,329✔
10145

10146
      p_sequence_of_statements(c);
2,329✔
10147

10148
      tree_set_loc(c, CURRENT_LOC);
2,329✔
10149
   }
10150

10151
   consume(tEND);
8,682✔
10152
   consume(tIF);
8,682✔
10153
   p_trailing_label(label);
8,682✔
10154
   consume(tSEMI);
8,682✔
10155

10156
   set_label_and_loc(t, label, CURRENT_LOC);
8,682✔
10157
   sem_check(t, nametab);
8,682✔
10158
   return t;
8,682✔
10159
}
10160

10161
static tree_t p_null_statement(ident_t label)
227✔
10162
{
10163
   // [ label : ] null ;
10164

10165
   EXTEND("null statement");
227✔
10166

10167
   consume(tNULL);
227✔
10168
   consume(tSEMI);
227✔
10169

10170
   tree_t t = tree_new(T_NULL);
227✔
10171
   set_label_and_loc(t, label, CURRENT_LOC);
227✔
10172
   return t;
227✔
10173
}
10174

10175
static void p_parameter_specification(tree_t loop, tree_kind_t pkind)
2,713✔
10176
{
10177
   // identifier in discrete_range
10178

10179
   BEGIN("paremeter specification");
5,426✔
10180

10181
   ident_t id = p_identifier();
2,713✔
10182
   const loc_t id_loc = last_loc;
2,713✔
10183

10184
   consume(tIN);
2,713✔
10185

10186
   tree_t r = p_discrete_range(NULL);
2,713✔
10187
   solve_types(nametab, r, NULL);
2,713✔
10188
   convert_universal_bounds(r);
2,713✔
10189
   tree_add_range(loop, r);
2,713✔
10190

10191
   // LRM 08 section 10.10: the loop parameter is an object whose type
10192
   // is the base type of the discrete range
10193
   type_t base = type_base_recur(tree_type(r));
2,713✔
10194

10195
   tree_t constraint = tree_new(T_CONSTRAINT);
2,713✔
10196
   tree_set_subkind(constraint, C_RANGE);
2,713✔
10197
   tree_add_range(constraint, r);
2,713✔
10198

10199
   type_t sub = type_new(T_SUBTYPE);
2,713✔
10200
   type_set_base(sub, base);
2,713✔
10201
   type_set_constraint(sub, constraint);
2,713✔
10202

10203
   tree_t param = tree_new(pkind);
2,713✔
10204
   tree_set_ident(param, id);
2,713✔
10205
   tree_set_type(param, sub);
2,713✔
10206
   tree_set_loc(param, &id_loc);
2,713✔
10207
   tree_set_class(param, C_CONSTANT);
2,713✔
10208
   tree_set_subkind(param, PORT_IN);
2,713✔
10209

10210
   tree_add_decl(loop, param);
2,713✔
10211

10212
   insert_name(nametab, param, NULL);
2,713✔
10213
}
2,713✔
10214

10215
static tree_t p_iteration_scheme(void)
3,013✔
10216
{
10217
   // while condition | for parameter_specification
10218

10219
   BEGIN("iteration scheme");
6,026✔
10220

10221
   if (optional(tWHILE)) {
3,013✔
10222
      tree_t t = tree_new(T_WHILE);
201✔
10223
      tree_set_value(t, p_condition());
201✔
10224
      return t;
201✔
10225
   }
10226
   else if (optional(tFOR)) {
2,812✔
10227
      tree_t t = tree_new(T_FOR);
2,549✔
10228
      scope_set_container(nametab, t);
2,549✔
10229
      p_parameter_specification(t, T_PARAM_DECL);
2,549✔
10230
      return t;
2,549✔
10231
   }
10232
   else
10233
      return tree_new(T_LOOP);
263✔
10234
}
10235

10236
static tree_t p_loop_statement(ident_t label)
3,013✔
10237
{
10238
   // [ loop_label : ] [ iteration_scheme ] loop sequence_of_statements
10239
   //   end loop [ loop_label ] ;
10240

10241
   BEGIN("loop statement");
3,013✔
10242

10243
   push_scope(nametab);
3,013✔
10244

10245
   tree_t t = p_iteration_scheme();
3,013✔
10246

10247
   consume(tLOOP);
3,013✔
10248

10249
   scope_set_container(nametab, t);
3,013✔
10250
   set_label_and_loc(t, label, CURRENT_LOC);
3,013✔
10251

10252
   if (label != NULL)
3,013✔
10253
      insert_name(nametab, t, NULL);
104✔
10254

10255
   sem_check(t, nametab);
3,013✔
10256

10257
   p_sequence_of_statements(t);
3,013✔
10258

10259
   consume(tEND);
3,013✔
10260
   consume(tLOOP);
3,013✔
10261
   p_trailing_label(label);
3,013✔
10262
   consume(tSEMI);
3,013✔
10263

10264
   pop_scope(nametab);
3,013✔
10265

10266
   tree_set_loc(t, CURRENT_LOC);
3,013✔
10267
   return t;
3,013✔
10268
}
10269

10270
static tree_t p_return_statement(ident_t label)
12,999✔
10271
{
10272
   // [ label : ] return [ expression ] ;
10273
   // 2019: [ label : ] return [ when condition ] ;
10274
   // 2019: [ label : ] return conditional_or_unaffected_expression ;
10275

10276
   EXTEND("return statement");
12,999✔
10277

10278
   consume(tRETURN);
12,999✔
10279

10280
   tree_t stmt = NULL;
12,999✔
10281

10282
   if (optional(tWHEN)) {
12,999✔
10283
      require_std(STD_19, "conditional return statement");
7✔
10284

10285
      stmt = tree_new(T_COND_RETURN);
7✔
10286
      tree_set_value(stmt, p_condition());
7✔
10287
   }
10288
   else {
10289
      stmt = tree_new(T_RETURN);
12,992✔
10290

10291
      if (peek() != tSEMI) {
12,992✔
10292
         type_t return_type = NULL;
12,647✔
10293
         tree_t subprog = find_enclosing(nametab, S_SUBPROGRAM);
12,647✔
10294
         if (subprog != NULL && tree_kind(subprog) == T_FUNC_BODY)
12,647✔
10295
            return_type = type_result(tree_type(subprog));
12,643✔
10296

10297
         tree_t value = p_conditional_or_unaffected_expression(STD_19);
12,647✔
10298
         solve_types(nametab, value, return_type);
12,647✔
10299
         tree_set_value(stmt, value);
12,647✔
10300
      }
10301
   }
10302

10303
   consume(tSEMI);
12,999✔
10304

10305
   set_label_and_loc(stmt, label, CURRENT_LOC);
12,999✔
10306
   sem_check(stmt, nametab);
12,999✔
10307
   return stmt;
12,999✔
10308
}
10309

10310
static tree_t p_exit_statement(ident_t label)
316✔
10311
{
10312
   // [ label : ] exit [ label ] [ when condition ] ;
10313

10314
   EXTEND("exit statement");
316✔
10315

10316
   consume(tEXIT);
316✔
10317

10318
   tree_t t = tree_new(T_EXIT);
316✔
10319

10320
   if (peek() == tID) {
316✔
10321
      ident_t id = p_identifier();
38✔
10322
      tree_set_ident2(t, id);
38✔
10323

10324
      tree_t loop = resolve_name(nametab, CURRENT_LOC, id);
38✔
10325
      if (loop != NULL && !is_loop_stmt(loop))
38✔
UNCOV
10326
         parse_error(CURRENT_LOC, "%s is not a loop statement", istr(id));
×
10327
   }
10328
   else {
10329
      tree_t loop = find_enclosing(nametab, S_LOOP);
278✔
10330
      if (loop == NULL)
278✔
10331
         parse_error(CURRENT_LOC, "cannot use exit statement outside loop");
2✔
10332
      else
10333
         tree_set_ident2(t, tree_ident(loop));
276✔
10334
   }
10335

10336
   if (optional(tWHEN))
316✔
10337
      tree_set_value(t, p_condition());
129✔
10338

10339
   consume(tSEMI);
316✔
10340

10341
   set_label_and_loc(t, label, CURRENT_LOC);
316✔
10342
   sem_check(t, nametab);
316✔
10343
   return t;
316✔
10344
}
10345

10346
static tree_t p_next_statement(ident_t label)
96✔
10347
{
10348
   // [ label : ] next [ label ] [ when condition ] ;
10349

10350
   EXTEND("next statement");
96✔
10351

10352
   consume(tNEXT);
96✔
10353

10354
   tree_t t = tree_new(T_NEXT);
96✔
10355

10356
   if (peek() == tID) {
96✔
10357
      ident_t id = p_identifier();
2✔
10358
      tree_set_ident2(t, id);
2✔
10359

10360
      tree_t loop = resolve_name(nametab, CURRENT_LOC, id);
2✔
10361
      if (loop != NULL && !is_loop_stmt(loop))
2✔
UNCOV
10362
         parse_error(CURRENT_LOC, "%s is not a loop statement", istr(id));
×
10363
   }
10364
   else {
10365
      tree_t loop = find_enclosing(nametab, S_LOOP);
94✔
10366
      if (loop == NULL)
94✔
10367
         parse_error(CURRENT_LOC, "cannot use next statement outside loop");
3✔
10368
      else
10369
         tree_set_ident2(t, tree_ident(loop));
91✔
10370
   }
10371

10372
   if (optional(tWHEN))
96✔
10373
      tree_set_value(t, p_condition());
66✔
10374

10375
   consume(tSEMI);
96✔
10376

10377
   set_label_and_loc(t, label, CURRENT_LOC);
96✔
10378
   sem_check(t, nametab);
96✔
10379
   return t;
96✔
10380
}
10381

10382
static tree_t p_procedure_call_statement(ident_t label, tree_t name)
7,905✔
10383
{
10384
   // [ label : ] procedure_call ;
10385
   //
10386
   // name [ ( actual_parameter_part ) ]
10387
   // 2019: name [ generic_map_aspect ] [ parameter_map_aspect ]
10388

10389
   EXTEND("procedure call statement");
15,807✔
10390

10391
   tree_t call = NULL;
7,905✔
10392

10393
   switch (tree_kind(name)) {
7,905✔
10394
   case T_REF:
7,708✔
10395
      call = tree_new(T_PCALL);
7,708✔
10396
      tree_set_ident2(call, tree_ident(name));
7,708✔
10397
      break;
7,708✔
10398

10399
   case T_PROT_REF:
187✔
10400
      call = tree_new(T_PROT_PCALL);
187✔
10401
      tree_set_ident2(call, tree_ident(name));
187✔
10402
      tree_set_name(call, tree_value(name));
187✔
10403
      break;
187✔
10404

10405
   default:
10✔
10406
      // Only print an error if name is a valid expression
10407
      if (!type_is_none(solve_types(nametab, name, NULL)))
10✔
10408
         parse_error(CURRENT_LOC, "expected procedure name");
5✔
10409

10410
      call = tree_new(T_PCALL);
7✔
10411
      tree_set_ident2(call, error_marker());
7✔
10412
      set_label_and_loc(call, label, CURRENT_LOC);
7✔
10413
      drop_tokens_until(tSEMI);
7✔
10414
      return call;
7✔
10415
   }
10416

10417
   if (peek() == tGENERIC) {
7,895✔
10418
      ident_t id = tree_ident(name);
19✔
10419
      tree_t inst = tree_new(T_PROC_INST);
19✔
10420
      tree_set_ident(inst, ident_prefix(id, ident_uniq("inst"), '$'));
19✔
10421

10422
      tree_t decl = resolve_uninstantiated_subprogram(nametab, CURRENT_LOC,
19✔
10423
                                                      id, NULL);
10424
      if (decl != NULL) {
19✔
10425
         tree_t body = find_generic_subprogram_body(inst, decl);
19✔
10426
         instantiate_subprogram(inst, decl, body);
19✔
10427
      }
10428
      else {
10429
         // Create a dummy subprogram type to avoid later errors
UNCOV
10430
         type_t type = type_new(T_SIGNATURE);
×
UNCOV
10431
         type_set_ident(type, id);
×
10432

UNCOV
10433
         tree_set_type(inst, type);
×
10434
      }
10435

10436
      p_generic_map_aspect(inst, inst);
19✔
10437

10438
      require_std(STD_19, "generic map on procedure call");
19✔
10439

10440
      tree_set_loc(inst, CURRENT_LOC);
19✔
10441
      sem_check(inst, nametab);
19✔
10442

10443
      hash_t *map = get_generic_map(nametab);
19✔
10444
      if (map != NULL)
19✔
10445
         instance_fixup(inst, map);
19✔
10446

10447
      tree_set_ref(call, inst);
19✔
10448

10449
      mangle_func(nametab, inst);
19✔
10450

10451
      tree_t container = find_enclosing(nametab, S_DECLARATIVE_REGION);
19✔
10452
      tree_add_decl(container, inst);
19✔
10453
   }
10454

10455
   if (peek() == tPARAMETER)
7,895✔
10456
      p_parameter_map_aspect(call);
1✔
10457
   else if (optional(tLPAREN)) {
7,894✔
10458
      p_actual_parameter_part(call);
7,492✔
10459
      consume(tRPAREN);
7,492✔
10460
   }
10461

10462
   consume(tSEMI);
7,895✔
10463

10464
   set_label_and_loc(call, label, CURRENT_LOC);
7,895✔
10465

10466
   solve_types(nametab, call, NULL);
7,895✔
10467
   sem_check(call, nametab);
7,895✔
10468
   return call;
7,895✔
10469
}
10470

10471
static tree_t p_case_statement_alternative(type_t type)
2,467✔
10472
{
10473
   // when choices => sequence_of_statements
10474

10475
   BEGIN("case statement alternative");
2,467✔
10476

10477
   consume(tWHEN);
2,467✔
10478

10479
   tree_t alt = tree_new(T_ALTERNATIVE);
2,467✔
10480

10481
   p_choices(alt, NULL, type);
2,467✔
10482

10483
   consume(tASSOC);
2,467✔
10484

10485
   p_sequence_of_statements(alt);
2,467✔
10486

10487
   tree_set_loc(alt, CURRENT_LOC);
2,467✔
10488
   return alt;
2,467✔
10489
}
10490

10491
static tree_t p_case_statement(ident_t label)
545✔
10492
{
10493
   // [ label : ] case [?] expression is case_statement_alternative
10494
   //   { case_statement_alternative } end case [?] [ label ] ;
10495

10496
   EXTEND("case statement");
545✔
10497

10498
   consume(tCASE);
545✔
10499

10500
   tree_kind_t kind = T_CASE;
545✔
10501
   if (optional(tQUESTION)) {
545✔
10502
      require_std(STD_08, "matching case statements");
31✔
10503
      kind = T_MATCH_CASE;
31✔
10504
   }
10505

10506
   tree_t t = tree_new(kind);
545✔
10507

10508
   tree_t value = p_expression();
545✔
10509
   tree_set_value(t, value);
545✔
10510

10511
   type_t type = solve_types(nametab, value, NULL);
545✔
10512

10513
   consume(tIS);
545✔
10514

10515
   do {
2,467✔
10516
      tree_add_stmt(t, p_case_statement_alternative(type));
2,467✔
10517
   } while (peek() == tWHEN);
2,467✔
10518

10519
   consume(tEND);
545✔
10520
   consume(tCASE);
545✔
10521

10522
   if (kind == T_MATCH_CASE)
545✔
10523
      consume(tQUESTION);
31✔
10524

10525
   p_trailing_label(label);
545✔
10526
   consume(tSEMI);
545✔
10527

10528
   set_label_and_loc(t, label, CURRENT_LOC);
545✔
10529
   sem_check(t, nametab);
545✔
10530
   return t;
545✔
10531
}
10532

10533
static void p_sequential_block_declarative_part(tree_t block)
8✔
10534
{
10535
   // { process_declarative_item }
10536

10537
   BEGIN("sequential block declarative part");
16✔
10538

10539
   while (not_at_token(tBEGIN))
21✔
10540
      p_process_declarative_item(block);
13✔
10541
}
8✔
10542

10543
static void p_sequential_block_statement_part(tree_t block)
8✔
10544
{
10545
   // { sequential_statement }
10546

10547
   BEGIN("sequential block statement part");
16✔
10548

10549
   p_sequence_of_statements(block);
8✔
10550
}
8✔
10551

10552
static tree_t p_sequential_block_statement(ident_t label)
8✔
10553
{
10554
   // [ label : ] block  [ is ] sequential_block_declarative_part
10555
   //   begin sequential_block_statement_part end [ block ] [ label ] ;
10556

10557
   BEGIN("sequential block statement");
8✔
10558

10559
   consume(tBLOCK);
8✔
10560
   optional(tIS);
8✔
10561

10562
   require_std(STD_19, "sequential block statements");
8✔
10563

10564
   push_scope(nametab);
8✔
10565

10566
   tree_t t = tree_new(T_SEQUENCE);
8✔
10567

10568
   scope_set_container(nametab, t);
8✔
10569
   set_label_and_loc(t, label, CURRENT_LOC);
8✔
10570

10571
   p_sequential_block_declarative_part(t);
8✔
10572

10573
   consume(tBEGIN);
8✔
10574

10575
   p_sequential_block_statement_part(t);
8✔
10576

10577
   consume(tEND);
8✔
10578
   optional(tBLOCK);
8✔
10579

10580
   p_trailing_label(label);
8✔
10581
   consume(tSEMI);
8✔
10582

10583
   tree_set_loc(t, CURRENT_LOC);
8✔
10584
   sem_check(t, nametab);
8✔
10585
   pop_scope(nametab);
8✔
10586

10587
   return t;
8✔
10588
}
10589

10590
static tree_t p_sequential_statement(void)
82,995✔
10591
{
10592
   // wait_statement | assertion_statement | report_statement
10593
   //   | signal_assignment_statement | variable_assignment_statement
10594
   //   | procedure_call_statement | if_statement | case_statement
10595
   //   | loop_statement | next_statement | exit_statement | return_statement
10596
   //   | null_statement | 2019: sequential_block_statement
10597

10598
   BEGIN("sequential statement");
165,987✔
10599

10600
   ident_t label = NULL;
82,995✔
10601
   if ((peek() == tID) && (peek_nth(2) == tCOLON)) {
82,995✔
10602
      label = p_identifier();
330✔
10603
      consume(tCOLON);
330✔
10604
   }
10605

10606
   switch (peek()) {
82,995✔
10607
   case tWAIT:
9,348✔
10608
      return p_wait_statement(label);
9,348✔
10609

10610
   case tASSERT:
16,287✔
10611
      return p_assertion_statement(label);
16,287✔
10612

10613
   case tREPORT:
2,014✔
10614
      return p_report_statement(label);
2,014✔
10615

10616
   case tIF:
8,682✔
10617
      return p_if_statement(label);
8,682✔
10618

10619
   case tNULL:
227✔
10620
      return p_null_statement(label);
227✔
10621

10622
   case tRETURN:
12,999✔
10623
      return p_return_statement(label);
12,999✔
10624

10625
   case tCASE:
545✔
10626
      return p_case_statement(label);
545✔
10627

10628
   case tWHILE:
3,013✔
10629
   case tLOOP:
10630
   case tFOR:
10631
      return p_loop_statement(label);
3,013✔
10632

10633
   case tEXIT:
316✔
10634
      return p_exit_statement(label);
316✔
10635

10636
   case tNEXT:
96✔
10637
      return p_next_statement(label);
96✔
10638

10639
   case tWITH:
12✔
10640
      return p_variable_assignment_statement(label, NULL);
12✔
10641

10642
   case tID:
10643
   case tLTLT:
10644
      break;
29,338✔
10645

10646
   case tLPAREN:
109✔
10647
      {
10648
         tree_t agg = p_aggregate();
109✔
10649

10650
         switch (peek()) {
109✔
10651
         case tWALRUS:
56✔
10652
            return p_variable_assignment_statement(label, agg);
56✔
10653

10654
         case tLE:
53✔
10655
            return p_signal_assignment_statement(label, agg);
53✔
10656

UNCOV
10657
         default:
×
UNCOV
10658
            expect(tWALRUS, tLE);
×
UNCOV
10659
            return tree_new(T_NULL);
×
10660
         }
10661
      }
10662

10663
   case tBLOCK:
8✔
10664
      return p_sequential_block_statement(label);
8✔
10665

10666
   default:
1✔
10667
      expect(tWAIT, tID, tASSERT, tREPORT, tIF, tNULL, tRETURN, tCASE, tWHILE,
1✔
10668
             tFOR, tLOOP, tEXIT, tNEXT, tWITH, tLTLT, tLPAREN, tBLOCK);
10669
      drop_tokens_until(tSEMI);
1✔
10670
      return tree_new(T_NULL);
1✔
10671
   }
10672

10673
   tree_t name = p_name(N_SUBPROGRAM);
29,338✔
10674

10675
   switch (peek()) {
29,338✔
10676
   case tWALRUS:
16,406✔
10677
      return p_variable_assignment_statement(label, name);
16,406✔
10678

10679
   case tLE:
5,026✔
10680
      return p_signal_assignment_statement(label, name);
5,026✔
10681

10682
   case tSEMI:
7,905✔
10683
   case tLPAREN:
10684
   case tGENERIC:
10685
   case tPARAMETER:
10686
      return p_procedure_call_statement(label, name);
7,905✔
10687

10688
   default:
1✔
10689
      expect(tWALRUS, tLE, tSEMI);
1✔
10690
      drop_tokens_until(tSEMI);
1✔
10691
      return tree_new(T_NULL);
1✔
10692
   }
10693
}
10694

10695
static tree_t p_instantiated_unit(tree_t name)
1,486✔
10696
{
10697
   // [ component ] name
10698
   //   | entity name [ ( identifier ) ]
10699
   //   | configuration name
10700

10701
   BEGIN("instantiated unit");
1,486✔
10702

10703
   tree_t t = tree_new(T_INSTANCE);
1,486✔
10704

10705
   switch (peek()) {
1,486✔
10706
   case tENTITY:
1,049✔
10707
      consume(tENTITY);
1,049✔
10708
      tree_set_class(t, C_ENTITY);
1,049✔
10709
      break;
1,049✔
10710

10711
   case tCONFIGURATION:
14✔
10712
      consume(tCONFIGURATION);
14✔
10713
      tree_set_class(t, C_CONFIGURATION);
14✔
10714
      break;
14✔
10715

10716
   case tCOMPONENT:
201✔
10717
      consume(tCOMPONENT);
201✔
10718
      // Fall-through
10719

10720
   default:
423✔
10721
      tree_set_class(t, C_COMPONENT);
423✔
10722
   }
10723

10724
   if (name != NULL) {
1,486✔
10725
      if (tree_kind(name) == T_REF) {
3✔
10726
         tree_set_ident2(t, tree_ident(name));
2✔
10727
         if (tree_has_ref(name))
2✔
10728
            tree_set_ref(t, tree_ref(name));
2✔
10729
      }
10730
      else {
10731
         parse_error(tree_loc(name), "invalid instantiated unit name");
1✔
10732
         tree_set_ident2(t, error_marker());
1✔
10733
      }
10734
   }
10735
   else
10736
      tree_set_ident2(t, p_selected_identifier());
1,483✔
10737

10738
   if ((tree_class(t) == C_ENTITY) && optional(tLPAREN)) {
1,486✔
10739
      tree_set_ident2(t, ident_prefix(tree_ident2(t), p_identifier(), '-'));
63✔
10740
      consume(tRPAREN);
63✔
10741
   }
10742

10743
   tree_set_loc(t, CURRENT_LOC);
1,486✔
10744
   return t;
1,486✔
10745
}
10746

10747
static tree_t p_component_instantiation_statement(ident_t label, tree_t name)
1,486✔
10748
{
10749
   // label : instantiated_unit [ generic_map_aspect ] [ port_map_aspect ] ;
10750

10751
   EXTEND("component instantiation statement");
1,486✔
10752

10753
   tree_t t = p_instantiated_unit(name);
1,486✔
10754
   tree_set_ident(t, label);
1,486✔
10755
   tree_set_loc(t, CURRENT_LOC);
1,486✔
10756

10757
   tree_t ref = find_binding(t);
1,486✔
10758
   tree_set_ref(t, ref);
1,486✔
10759

10760
   tree_t entity = ref ? primary_unit_of(ref) : NULL;
1,486✔
10761

10762
   tree_t spec = query_spec(nametab, t);
1,486✔
10763
   if (spec != NULL)
1,486✔
10764
      tree_set_spec(t, spec);
95✔
10765

10766
   if (label != NULL)
1,486✔
10767
      insert_name(nametab, t, NULL);
1,484✔
10768

10769
   push_scope(nametab);
1,486✔
10770

10771
   if (ref == NULL) suppress_errors(nametab);
1,486✔
10772

10773
   if (peek() == tGENERIC)
1,486✔
10774
      p_generic_map_aspect(t, entity);
579✔
10775

10776
   if (peek() == tPORT)
1,486✔
10777
      p_port_map_aspect(t, entity);
1,075✔
10778

10779
   consume(tSEMI);
1,486✔
10780

10781
   tree_set_loc(t, CURRENT_LOC);
1,486✔
10782

10783
   if (label == NULL){
1,486✔
10784
      parse_error(CURRENT_LOC, "component instantiation statement must "
2✔
10785
                  "have a label");
10786
      tree_set_ident(t, error_marker());
2✔
10787
   }
10788

10789
   sem_check(t, nametab);
1,486✔
10790
   pop_scope(nametab);
1,486✔
10791

10792
   return t;
1,486✔
10793
}
10794

10795
static void p_options(tree_t *reject, tree_t *guard)
2,194✔
10796
{
10797
   // [ guarded ] [ delay_mechanism ]
10798

10799
   BEGIN("options");
2,194✔
10800

10801
   if (optional(tGUARDED)) {
2,194✔
10802
      tree_t decl = NULL;
24✔
10803
      name_mask_t mask = query_name(nametab, ident_new("GUARD"), &decl);
24✔
10804
      if ((mask & N_OBJECT) && decl != NULL) {
24✔
10805
         tree_t g = tree_new(T_GUARD);
22✔
10806
         tree_set_loc(g, CURRENT_LOC);
22✔
10807
         tree_set_ref(g, decl);
22✔
10808
         tree_set_type(g, tree_type(decl));
22✔
10809

10810
         *guard = g;
22✔
10811
      }
10812
      else
10813
         parse_error(CURRENT_LOC, "guarded assignment has no visible "
24✔
10814
                     "guard signal");
10815
   }
10816

10817
   *reject = p_delay_mechanism();
2,194✔
10818
}
2,194✔
10819

10820
static void p_conditional_waveforms(tree_t stmt, tree_t target, tree_t s0)
2,152✔
10821
{
10822
   // { waveform when condition else } waveform [ when condition ]
10823

10824
   BEGIN("conditional waveforms");
4,304✔
10825

10826
   for (;;) {
2,302✔
10827
      tree_t c = tree_new(T_COND_STMT);
2,302✔
10828

10829
      tree_t a = s0;
2,302✔
10830
      if (a == NULL) {
2,302✔
10831
         a = tree_new(T_SIGNAL_ASSIGN);
2,297✔
10832
         tree_set_target(a, target);
2,297✔
10833
         p_waveform(a, target);
2,297✔
10834
      }
10835
      else {
10836
         s0 = NULL;
5✔
10837
         tree_set_loc(a, CURRENT_LOC);
5✔
10838
      }
10839
      tree_add_stmt(c, a);
2,302✔
10840
      tree_add_cond(stmt, c);
2,302✔
10841

10842
      if (optional(tWHEN)) {
2,302✔
10843
         tree_t when = p_condition();
226✔
10844
         tree_set_value(c, when);
226✔
10845
         tree_set_loc(c, tree_loc(when));
226✔
10846
         solve_types(nametab, when, std_type(NULL, STD_BOOLEAN));
226✔
10847

10848
         if (!optional(tELSE))
226✔
10849
            break;
10850
      }
10851
      else
10852
         break;
10853
   }
10854
}
2,152✔
10855

10856
static tree_t p_conditional_signal_assignment(tree_t name)
2,147✔
10857
{
10858
   // target <= options conditional_waveforms ;
10859

10860
   BEGIN("conditional signal assignment");
2,147✔
10861

10862
   tree_t conc = tree_new(T_CONCURRENT);
2,147✔
10863
   tree_t stmt = tree_new(T_COND_ASSIGN);
2,147✔
10864
   tree_add_stmt(conc, stmt);
2,147✔
10865

10866
   tree_t target = p_target(name);
2,147✔
10867
   tree_set_target(stmt, target);
2,147✔
10868

10869
   consume(tLE);
2,147✔
10870

10871
   tree_t reject = NULL, guard = NULL;
2,147✔
10872
   p_options(&reject, &guard);
2,147✔
10873

10874
   if (guard != NULL) {
2,147✔
10875
      tree_set_guard(stmt, guard);
15✔
10876
      find_disconnect_specification(guard, target);
15✔
10877
   }
10878

10879
   p_conditional_waveforms(stmt, target, NULL);
2,147✔
10880

10881
   const int nconds = tree_conds(stmt);
2,147✔
10882
   for (int i = 0; i < nconds; i++) {
4,439✔
10883
      tree_t c = tree_cond(stmt, i);
2,292✔
10884
      assert(tree_stmts(c) == 1);
2,292✔
10885
      set_delay_mechanism(tree_stmt(c, 0), reject);
2,292✔
10886
   }
10887

10888
   consume(tSEMI);
2,147✔
10889

10890
   tree_set_loc(stmt, CURRENT_LOC);
2,147✔
10891
   tree_set_loc(conc, CURRENT_LOC);
2,147✔
10892
   return conc;
2,147✔
10893
}
10894

10895
static void p_selected_waveforms(tree_t stmt, tree_t target, tree_t reject)
50✔
10896
{
10897
   // { waveform when choices , } waveform when choices
10898

10899
   BEGIN("selected waveforms");
100✔
10900

10901
   type_t with_type = tree_type(tree_value(stmt));
50✔
10902

10903
   do {
234✔
10904
      tree_t a = tree_new(T_SIGNAL_ASSIGN);
234✔
10905
      tree_set_target(a, target);
234✔
10906
      if (reject != NULL)
234✔
UNCOV
10907
         tree_set_reject(a, reject);
×
10908

10909
      p_waveform(a, target);
234✔
10910

10911
      sem_check(a, nametab);
234✔
10912

10913
      consume(tWHEN);
234✔
10914

10915
      tree_t alt = tree_new(T_ALTERNATIVE);
234✔
10916
      tree_add_stmt(alt, a);
234✔
10917

10918
      p_choices(alt, NULL, with_type);
234✔
10919

10920
      tree_set_loc(alt, CURRENT_LOC);
234✔
10921
      tree_add_stmt(stmt, alt);
234✔
10922
   } while (optional(tCOMMA));
234✔
10923
}
50✔
10924

10925
static tree_t p_selected_signal_assignment(void)
47✔
10926
{
10927
   // with expression select target <= options selected_waveforms ;
10928

10929
   BEGIN("selected signal assignment");
47✔
10930

10931
   consume(tWITH);
47✔
10932

10933
   tree_t value = p_expression();
47✔
10934
   solve_types(nametab, value, NULL);
47✔
10935

10936
   consume(tSELECT);
47✔
10937

10938
   tree_t stmt;
47✔
10939
   if (optional(tQUESTION)) {
47✔
10940
      require_std(STD_08, "matching select statements");
9✔
10941
      stmt = tree_new(T_MATCH_SELECT);
9✔
10942
   }
10943
   else
10944
      stmt = tree_new(T_SELECT);
38✔
10945

10946
   tree_set_value(stmt, value);
47✔
10947

10948
   tree_t conc = tree_new(T_CONCURRENT);
47✔
10949
   tree_add_stmt(conc, stmt);
47✔
10950

10951
   tree_t target = p_target(NULL);
47✔
10952

10953
   consume(tLE);
47✔
10954

10955
   tree_t reject = NULL, guard = NULL;
47✔
10956
   p_options(&reject, &guard);
47✔
10957

10958
   if (guard != NULL) {
47✔
10959
      tree_set_guard(stmt, guard);
7✔
10960
      find_disconnect_specification(guard, target);
7✔
10961
   }
10962

10963
   p_selected_waveforms(stmt, target, reject);
47✔
10964
   consume(tSEMI);
47✔
10965

10966
   tree_set_loc(stmt, CURRENT_LOC);
47✔
10967
   tree_set_loc(conc, CURRENT_LOC);
47✔
10968
   return conc;
47✔
10969
}
10970

10971
static tree_t p_concurrent_signal_assignment_statement(ident_t label,
2,194✔
10972
                                                       tree_t name)
10973
{
10974
   // [ label : ] [ postponed ] conditional_signal_assignment
10975
   //   | [ label : ] [ postponed ] selected_signal_assignment
10976

10977
   EXTEND("concurrent signal assignment statement");
2,194✔
10978

10979
   const bool postponed = name == NULL && optional(tPOSTPONED);
2,194✔
10980

10981
   tree_t t;
2,194✔
10982
   if (peek() == tWITH) {
2,194✔
10983
      assert(name == NULL);
47✔
10984
      t = p_selected_signal_assignment();
47✔
10985
   }
10986
   else
10987
      t = p_conditional_signal_assignment(name);
2,147✔
10988

10989
   tree_set_loc(t, CURRENT_LOC);
2,194✔
10990
   ensure_labelled(t, label);
2,194✔
10991

10992
   if (postponed)
2,194✔
UNCOV
10993
      tree_set_flag(t, TREE_F_POSTPONED);
×
10994

10995
   if (label) insert_name(nametab, t, NULL);
2,194✔
10996
   sem_check(t, nametab);
2,194✔
10997
   return t;
2,194✔
10998
}
10999

11000
static tree_t p_concurrent_procedure_call_statement(ident_t label, tree_t name)
64✔
11001
{
11002
   // [ label : ] [ postponed ] procedure_call ;
11003

11004
   EXTEND("concurrent procedure call statement");
64✔
11005

11006
   const bool postponed = name == NULL && optional(tPOSTPONED);
64✔
11007

11008
   tree_t call = NULL;
64✔
11009
   if (name == NULL) {
64✔
11010
      call = tree_new(T_PCALL);
6✔
11011
      tree_set_ident2(call, p_identifier());
6✔
11012
   }
11013
   else if (tree_kind(name) == T_PROT_REF) {
58✔
11014
      call = tree_new(T_PROT_PCALL);
3✔
11015
      tree_set_ident2(call, tree_ident(name));
3✔
11016
      tree_set_name(call, tree_value(name));
3✔
11017
   }
11018
   else {
11019
      call = tree_new(T_PCALL);
55✔
11020
      tree_set_ident2(call, tree_ident(name));
55✔
11021
   }
11022

11023
   if (optional(tLPAREN)) {
64✔
11024
      p_actual_parameter_part(call);
50✔
11025
      consume(tRPAREN);
50✔
11026
   }
11027

11028
   consume(tSEMI);
64✔
11029

11030
   tree_set_loc(call, CURRENT_LOC);
64✔
11031

11032
   solve_types(nametab, call, NULL);
64✔
11033

11034
   tree_t conc = tree_new(T_CONCURRENT);
64✔
11035
   tree_add_stmt(conc, call);
64✔
11036

11037
   if (postponed)
64✔
11038
      tree_set_flag(conc, TREE_F_POSTPONED);
3✔
11039

11040
   tree_set_loc(conc, CURRENT_LOC);
64✔
11041
   ensure_labelled(conc, label);
64✔
11042
   tree_set_ident(call, tree_ident(conc));
64✔
11043

11044
   if (label) insert_name(nametab, conc, NULL);
64✔
11045
   sem_check(conc, nametab);
64✔
11046
   return conc;
64✔
11047
}
11048

11049
static void p_concurrent_statement_or_psl(tree_t parent)
10,685✔
11050
{
11051
   // Allow PSL declarations in concurrent statement part when using
11052
   // "--psl" comments
11053

11054
   if (peek() == tSTARTPSL) {
10,685✔
11055
      consume(tSTARTPSL);
351✔
11056

11057
      if (peek() == tID) {
351✔
11058
         ident_t label = p_identifier();
172✔
11059
         consume(tCOLON);
172✔
11060
         tree_add_stmt(parent, p_psl_directive(label));
172✔
11061
      }
11062
      else if (scan(tDEFAULT, tSEQUENCE, tPROPERTY, tENDPOINT))
179✔
11063
         p_psl_declaration(parent);
54✔
11064
      else
11065
         tree_add_stmt(parent, p_psl_directive(NULL));
125✔
11066
   }
11067
   else
11068
      tree_add_stmt(parent, p_concurrent_statement());
10,334✔
11069
}
10,682✔
11070

11071
static void p_block_statement_part(tree_t arch)
377✔
11072
{
11073
   // { concurrent_statement }
11074

11075
   BEGIN("block statement part");
754✔
11076

11077
   while (not_at_token(tEND))
775✔
11078
      p_concurrent_statement_or_psl(arch);
398✔
11079
}
377✔
11080

11081
static void p_block_declarative_part(tree_t arch)
377✔
11082
{
11083
   // { block_declarative_item }
11084

11085
   BEGIN("block declarative part");
754✔
11086

11087
   while (not_at_token(tBEGIN))
734✔
11088
      p_block_declarative_item(arch);
357✔
11089
}
377✔
11090

11091
static void p_block_header(tree_t block)
377✔
11092
{
11093
   // [ generic_clause [ generic_map_aspect ; ] ]
11094
   //   [ port_clause [ port_map_aspect ; ] ]
11095

11096
   if (peek() == tGENERIC) {
377✔
11097
      p_generic_clause(block);
55✔
11098

11099
      if (peek() == tGENERIC) {
55✔
11100
         p_generic_map_aspect(block, block);
48✔
11101
         consume(tSEMI);
48✔
11102
      }
11103

11104
      insert_generics(nametab, block);
55✔
11105
   }
11106

11107
   if (peek() == tPORT) {
377✔
11108
      p_port_clause(block);
156✔
11109

11110
      if (peek() == tPORT) {
156✔
11111
         p_port_map_aspect(block, block);
154✔
11112
         consume(tSEMI);
154✔
11113
      }
11114

11115
      insert_ports(nametab, block);
156✔
11116
   }
11117
}
377✔
11118

11119
static tree_t p_block_statement(ident_t label)
377✔
11120
{
11121
   // label : block [ ( expression ) ] [ is ] block_header
11122
   //   block_declarative_part begin block_statement_part end block [ label ] ;
11123

11124
   EXTEND("block statement");
377✔
11125

11126
   tree_t b = tree_new(T_BLOCK);
377✔
11127
   tree_set_ident(b, label);
377✔
11128

11129
   consume(tBLOCK);
377✔
11130

11131
   if (label == NULL)
377✔
11132
      parse_error(CURRENT_LOC, "block statement must have a label");
1✔
11133
   else {
11134
      tree_set_loc(b, CURRENT_LOC);
376✔
11135
      insert_name(nametab, b, NULL);
376✔
11136
   }
11137

11138
   push_scope(nametab);
377✔
11139
   scope_set_prefix(nametab, label ?: error_marker());
377✔
11140
   scope_set_container(nametab, b);
377✔
11141

11142
   if (peek() == tLPAREN) {
377✔
11143
      consume(tLPAREN);
27✔
11144

11145
      tree_t expr = p_expression();
27✔
11146
      solve_condition(nametab, &expr);
27✔
11147

11148
      make_implicit_guard_signal(b, expr);
27✔
11149

11150
      consume(tRPAREN);
27✔
11151
   }
11152

11153
   optional(tIS);
377✔
11154
   p_block_header(b);
377✔
11155
   p_block_declarative_part(b);
377✔
11156
   consume(tBEGIN);
377✔
11157
   p_block_statement_part(b);
377✔
11158
   consume(tEND);
377✔
11159
   consume(tBLOCK);
377✔
11160
   p_trailing_label(label);
377✔
11161
   consume(tSEMI);
377✔
11162

11163
   tree_set_loc(b, CURRENT_LOC);
377✔
11164
   sem_check(b, nametab);
377✔
11165

11166
   hash_t *map = get_generic_map(nametab);
377✔
11167
   if (map != NULL)
377✔
11168
      instance_fixup(b, map);
43✔
11169

11170
   pop_scope(nametab);
377✔
11171
   return b;
377✔
11172
}
11173

11174
static void p_generate_statement_body(tree_t container, ident_t alt_label)
372✔
11175
{
11176
   // [ block_declarative_part begin ] { concurrent_statement }
11177
   //   [ end [ alternative_label ] ; ]
11178

11179
   BEGIN("generate statement body");
744✔
11180

11181
   if (scan(tSIGNAL, tTYPE, tSUBTYPE, tFILE, tCONSTANT, tFUNCTION, tIMPURE,
372✔
11182
            tPURE, tALIAS, tATTRIBUTE, tBEGIN, tPROCEDURE, tFOR, tCOMPONENT,
11183
            tUSE, tSHARED)) {
11184
      while (not_at_token(tBEGIN))
191✔
11185
         p_block_declarative_item(container);
79✔
11186
      consume(tBEGIN);
112✔
11187
   }
11188

11189
   while (not_at_token(tEND, tELSIF, tELSE, tWHEN))
790✔
11190
      p_concurrent_statement_or_psl(container);
418✔
11191

11192
   if (peek() == tEND && (peek_nth(2) == tID || peek_nth(2) == tSEMI)) {
372✔
11193
      consume(tEND);
9✔
11194
      p_trailing_label(alt_label);
9✔
11195
      consume(tSEMI);
9✔
11196
   }
11197
}
372✔
11198

11199
static tree_t p_for_generate_statement(ident_t label)
164✔
11200
{
11201
   // for generate_parameter_specification generate generate_statement_body
11202
   //   end generate [ generate_label ] ;
11203

11204
   EXTEND("for generate statement");
164✔
11205

11206
   consume(tFOR);
164✔
11207

11208
   tree_t g = tree_new(T_FOR_GENERATE);
164✔
11209
   tree_set_ident(g, label);
164✔
11210

11211
   if (label != NULL)
164✔
11212
      insert_name(nametab, g, NULL);
163✔
11213

11214
   push_scope(nametab);
164✔
11215
   scope_set_prefix(nametab, label);
164✔
11216
   scope_set_container(nametab, g);
164✔
11217

11218
   p_parameter_specification(g, T_GENERIC_DECL);
164✔
11219

11220
   consume(tGENERATE);
164✔
11221

11222
   p_generate_statement_body(g, NULL);
164✔
11223

11224
   consume(tEND);
164✔
11225
   consume(tGENERATE);
164✔
11226
   p_trailing_label(label);
164✔
11227
   consume(tSEMI);
164✔
11228

11229
   pop_scope(nametab);
164✔
11230

11231
   if (label == NULL)
164✔
11232
      parse_error(CURRENT_LOC, "generate statement must have a label");
1✔
11233

11234
   tree_set_loc(g, CURRENT_LOC);
164✔
11235
   sem_check(g, nametab);
164✔
11236
   return g;
164✔
11237
}
11238

11239
static tree_t p_if_generate_statement(ident_t label)
167✔
11240
{
11241
   // if [ alternative_label : ] condition generate generate_statement_body
11242
   //   { elsif [ alternative_label : ] condition generate
11243
   //     generate_statement_body }
11244
   //   [ else [ alternative_label : ] generate generate_statement_body ]
11245
   //   end generate [ generate_label ] ;
11246

11247
   EXTEND("if generate statement");
167✔
11248

11249
   consume(tIF);
167✔
11250

11251
   tree_t g = tree_new(T_IF_GENERATE);
167✔
11252
   tree_set_ident(g, label);
167✔
11253

11254
   if (label != NULL)
167✔
11255
      insert_name(nametab, g, NULL);
166✔
11256

11257
   ident_t alt_label = NULL;
167✔
11258
   if (peek() == tID && peek_nth(2) == tCOLON) {
167✔
11259
      require_std(STD_08, "alternative labels");
4✔
11260

11261
      alt_label = p_identifier();
4✔
11262
      consume(tCOLON);
4✔
11263
   }
11264

11265
   push_scope(nametab);
167✔
11266
   scope_set_container(nametab, g);
167✔
11267
   scope_set_prefix(nametab, alt_label ?: label);
330✔
11268

11269
   tree_t c0 = tree_new(T_COND_STMT);
167✔
11270
   tree_set_ident(c0, alt_label ?: label);
167✔
11271
   tree_set_value(c0, p_condition());
167✔
11272

11273
   tree_add_cond(g, c0);
167✔
11274

11275
   consume(tGENERATE);
167✔
11276

11277
   p_generate_statement_body(c0, alt_label);
167✔
11278

11279
   pop_scope(nametab);
167✔
11280

11281
   tree_set_loc(c0, CURRENT_LOC);
167✔
11282

11283
   while (optional(tELSIF)) {
168✔
11284
      require_std(STD_08, "elsif in generate statements");
1✔
11285

11286
      ident_t alt_label = NULL;
1✔
11287
      if (peek() == tID && peek_nth(2) == tCOLON) {
1✔
11288
         alt_label = p_identifier();
1✔
11289
         consume(tCOLON);
1✔
11290
      }
11291

11292
      push_scope(nametab);
1✔
11293
      scope_set_prefix(nametab, alt_label ?: label);
1✔
11294

11295
      tree_t c = tree_new(T_COND_STMT);
1✔
11296
      tree_set_ident(c, alt_label ?: label);
1✔
11297
      tree_set_value(c, p_condition());
1✔
11298

11299
      consume(tGENERATE);
1✔
11300

11301
      p_generate_statement_body(c, alt_label);
1✔
11302

11303
      pop_scope(nametab);
1✔
11304

11305
      tree_set_loc(c, CURRENT_LOC);
1✔
11306
      tree_add_cond(g, c);
1✔
11307
   }
11308

11309
   if (optional(tELSE)) {
167✔
11310
      require_std(STD_08, "else in generate statements");
9✔
11311

11312
      ident_t alt_label = label;
9✔
11313
      if (peek() == tID && peek_nth(2) == tCOLON) {
9✔
11314
         alt_label = p_identifier();
7✔
11315
         consume(tCOLON);
7✔
11316
      }
11317

11318
      push_scope(nametab);
9✔
11319
      scope_set_prefix(nametab, alt_label ?: label);
9✔
11320

11321
      tree_t c = tree_new(T_COND_STMT);
9✔
11322
      tree_set_ident(c, alt_label ?: label);
9✔
11323

11324
      consume(tGENERATE);
9✔
11325

11326
      p_generate_statement_body(c, alt_label);
9✔
11327

11328
      pop_scope(nametab);
9✔
11329

11330
      tree_set_loc(c, CURRENT_LOC);
9✔
11331
      tree_add_cond(g, c);
9✔
11332
   }
11333

11334
   consume(tEND);
167✔
11335
   consume(tGENERATE);
167✔
11336
   p_trailing_label(label);
167✔
11337
   consume(tSEMI);
167✔
11338

11339
   if (label == NULL)
167✔
11340
      parse_error(CURRENT_LOC, "generate statement must have a label");
1✔
11341

11342
   tree_set_loc(g, CURRENT_LOC);
167✔
11343
   sem_check(g, nametab);
167✔
11344
   return g;
167✔
11345
}
11346

11347
static tree_t p_case_generate_alternative(type_t type)
31✔
11348
{
11349
   // when [ alternative_label : ] choices => generate_statement_body
11350

11351
   BEGIN("case generate alternative");
31✔
11352

11353
   consume(tWHEN);
31✔
11354

11355
   ident_t alt_label = NULL;
31✔
11356
   if (peek() == tID && peek_nth(2) == tCOLON) {
31✔
11357
      alt_label = p_identifier();
3✔
11358
      consume(tCOLON);
3✔
11359
   }
11360

11361
   tree_t alt = tree_new(T_ALTERNATIVE);
31✔
11362
   tree_set_ident(alt, alt_label);
31✔
11363
   p_choices(alt, NULL, type);
31✔
11364

11365
   consume(tASSOC);
31✔
11366

11367
   push_scope(nametab);
31✔
11368
   scope_set_prefix(nametab, alt_label);
31✔
11369

11370
   p_generate_statement_body(alt, alt_label);
31✔
11371

11372
   tree_set_loc(alt, CURRENT_LOC);
31✔
11373
   pop_scope(nametab);
31✔
11374

11375
   return alt;
31✔
11376
}
11377

11378
static tree_t p_case_generate_statement(ident_t label)
10✔
11379
{
11380
   // case expression generate case_generate_alternative
11381
   //   { case_generate_alternative } end generate [ generate_label ] ;
11382

11383
   EXTEND("case generate statement");
10✔
11384

11385
   consume(tCASE);
10✔
11386

11387
   require_std(STD_08, "case generate statements");
10✔
11388

11389
   tree_t g = tree_new(T_CASE_GENERATE);
10✔
11390
   tree_set_ident(g, label);
10✔
11391

11392
   if (label != NULL)
10✔
11393
      insert_name(nametab, g, NULL);
10✔
11394

11395
   tree_t value = p_expression();
10✔
11396
   tree_set_value(g, value);
10✔
11397

11398
   type_t type = solve_types(nametab, value, NULL);
10✔
11399

11400
   consume(tGENERATE);
10✔
11401

11402
   do {
31✔
11403
      tree_add_stmt(g, p_case_generate_alternative(type));
31✔
11404
   } while (peek() == tWHEN);
31✔
11405

11406
   consume(tEND);
10✔
11407
   consume(tGENERATE);
10✔
11408

11409
   p_trailing_label(label);
10✔
11410
   consume(tSEMI);
10✔
11411

11412
   if (label == NULL)
10✔
UNCOV
11413
      parse_error(CURRENT_LOC, "generate statement must have a label");
×
11414

11415
   tree_set_loc(g, CURRENT_LOC);
10✔
11416
   sem_check(g, nametab);
10✔
11417
   return g;
10✔
11418
}
11419

11420
static tree_t p_generate_statement(ident_t label)
341✔
11421
{
11422
   // for_generate_statement | if_generate_statement | case_generate_statement
11423

11424
   EXTEND("generate statement");
682✔
11425

11426
   switch (peek()) {
341✔
11427
   case tFOR:
164✔
11428
      return p_for_generate_statement(label);
164✔
11429
   case tIF:
167✔
11430
      return p_if_generate_statement(label);
167✔
11431
   case tCASE:
10✔
11432
      return p_case_generate_statement(label);
10✔
UNCOV
11433
   default:
×
UNCOV
11434
      expect(tFOR, tIF, tCASE);
×
UNCOV
11435
      drop_tokens_until(tSEMI);
×
UNCOV
11436
      return ensure_labelled(tree_new(T_BLOCK), label);
×
11437
   }
11438
}
11439

11440
static psl_node_t p_psl_low_bound(tree_t head)
19✔
11441
{
11442
   // Number | MIN_VAL
11443

11444
   BEGIN_WITH_HEAD("PSL Low Bound", head);
38✔
11445

11446
   return p_hdl_expression(head, PSL_TYPE_NUMERIC);
19✔
11447
}
11448

11449
static psl_node_t p_psl_high_bound(void)
19✔
11450
{
11451
   // Number | MAX_VAL
11452

11453
   BEGIN("PSL Low Bound");
38✔
11454

11455
   if (optional(tINF)) {
19✔
11456
      tree_t inf = tree_new(T_LITERAL);
6✔
11457
      tree_set_loc(inf, CURRENT_LOC);
6✔
11458
      tree_set_subkind(inf, L_INT);
6✔
11459
      tree_set_ival(inf, INT32_MAX);
6✔
11460

11461
      psl_node_t p = psl_new(P_HDL_EXPR);
6✔
11462
      psl_set_type(p, PSL_TYPE_NUMERIC);
6✔
11463
      psl_set_tree(p, inf);
6✔
11464
      psl_set_loc(p, CURRENT_LOC);
6✔
11465
      return p;
6✔
11466
   }
11467
   else
11468
      return p_hdl_expression(NULL, PSL_TYPE_NUMERIC);
13✔
11469
}
11470

11471
static psl_node_t p_psl_range(tree_t head)
19✔
11472
{
11473
   // Low_Bound RANGE_SYM High_Bound
11474

11475
   BEGIN_WITH_HEAD("PSL Range", head);
19✔
11476

11477
   psl_node_t low = p_psl_low_bound(head);
19✔
11478

11479
   consume(tTO);
19✔
11480

11481
   psl_node_t high = p_psl_high_bound();
19✔
11482

11483
   psl_node_t p = psl_new(P_RANGE);
19✔
11484
   psl_set_left(p, low);
19✔
11485
   psl_set_right(p, high);
19✔
11486

11487
   psl_set_loc(p, CURRENT_LOC);
19✔
11488
   return p;
19✔
11489
}
11490

11491
static psl_node_t p_psl_value_range(void)
1✔
11492
{
11493
   // Value | Range
11494

11495
   BEGIN("PSL Value Range");
2✔
11496

11497
   tree_t tree = p_expression();
1✔
11498

11499
   if (peek() == tTO)
1✔
11500
      return p_psl_range(tree);
1✔
11501

UNCOV
11502
   psl_node_t p = psl_new(P_HDL_EXPR);
×
UNCOV
11503
   psl_set_tree(p, tree);
×
11504

UNCOV
11505
   psl_set_loc(p, CURRENT_LOC);
×
11506
   return p;
×
11507
}
11508

11509
static psl_node_t p_psl_value_set(void)
1✔
11510
{
11511
   // { Value_Range { , Value_Range } } | boolean
11512

11513
   BEGIN("PSL Value Set");
1✔
11514

11515
   psl_node_t p = psl_new(P_VALUE_SET);
1✔
11516

11517
   switch (one_of(tBOOLEAN, tLBRACE)) {
1✔
UNCOV
11518
   case tBOOLEAN:
×
UNCOV
11519
      psl_set_subkind(p, PSL_VALUE_SET_BOOLEAN);
×
UNCOV
11520
      break;
×
11521
   case tLBRACE:
1✔
11522
      {
11523
         psl_set_subkind(p, PSL_VALUE_SET_EXPLICIT);
1✔
11524

11525
         do {
1✔
11526
            psl_add_operand(p, p_psl_value_range());
1✔
11527
         } while (optional(tCOMMA));
1✔
11528

11529
         consume(tRBRACE);
1✔
11530
      }
11531
      break;
1✔
11532
   }
11533

11534
   psl_set_loc(p, CURRENT_LOC);
1✔
11535
   return p;
1✔
11536
}
11537

11538
static psl_node_t p_hdl_expression(tree_t head, psl_type_t type)
1,085✔
11539
{
11540
   BEGIN("PSL HDL expression");
1,085✔
11541

11542
   tree_t expr = p_relation(head);
1,085✔
11543

11544
   int loop_limit = (scan(tNOR, tNAND) ? 1 : INT_MAX);
1,085✔
11545

11546
   // AND and OR must be parsed as PSL operators
11547
   while (loop_limit-- && scan(tXOR, tNAND, tNOR, tXNOR)) {
1,087✔
11548
      tree_t new = tree_new(T_FCALL);
2✔
11549
      tree_set_ident(new, p_logical_operator());
2✔
11550
      binary_op(new, expr, p_relation);
2✔
11551
      expr = new;
2✔
11552
   }
11553

11554
   psl_node_t p = psl_new(P_HDL_EXPR);
1,085✔
11555
   psl_set_tree(p, expr);
1,085✔
11556
   psl_set_loc(p, tree_loc(expr));
1,085✔
11557
   psl_set_type(p, type);
1,085✔
11558

11559
   psl_set_loc(p, CURRENT_LOC);
1,085✔
11560
   return p;
1,085✔
11561
}
11562

11563
static psl_node_t p_psl_or_hdl_expression(void)
497✔
11564
{
11565
   // HDL_Expression | PSL_Expression | Built_In_Function_Call
11566
   //    | Union_Expression
11567

11568
   BEGIN("PSL or HDL expression");
994✔
11569

11570
   psl_node_t head = p_hdl_expression(NULL, PSL_TYPE_BOOLEAN);
497✔
11571

11572
   if (optional(tUNION)) {
497✔
UNCOV
11573
      psl_node_t new = psl_new(P_UNION);
×
UNCOV
11574
      psl_add_operand(new, head);
×
UNCOV
11575
      psl_add_operand(new, p_psl_or_hdl_expression());
×
UNCOV
11576
      psl_set_loc(new, CURRENT_LOC);
×
11577
      return new;
×
11578
   }
11579

11580
   return head;
11581
}
11582

11583
static psl_node_t p_psl_boolean(tree_t head)
436✔
11584
{
11585
   // HDL_or_PSL_Expression
11586

11587
   BEGIN_WITH_HEAD("PSL Boolean", head);
872✔
11588

11589
   psl_node_t p = p_hdl_expression(head, PSL_TYPE_BOOLEAN);
436✔
11590

11591
   const token_t infix = peek();
436✔
11592
   switch (infix) {
436✔
11593
   case tAND:
54✔
11594
   case tOR:
11595
      {
11596
         consume(infix);
54✔
11597

11598
         psl_node_t right = p_psl_boolean(NULL);
54✔
11599

11600
         tree_t fcall = tree_new(T_FCALL);
54✔
11601
         tree_set_ident(fcall, well_known(infix == tOR ? W_OP_OR : W_OP_AND));
108✔
11602
         tree_set_loc(fcall, CURRENT_LOC);
54✔
11603
         add_param(fcall, psl_tree(p), P_POS, NULL);
54✔
11604
         add_param(fcall, psl_tree(right), P_POS, NULL);
54✔
11605

11606
         psl_set_tree(p, fcall);
54✔
11607
         psl_set_loc(p, CURRENT_LOC);
54✔
11608
         return p;
54✔
11609
      }
11610

11611
   default:
11612
      return p;
11613
   }
11614
}
11615

11616
static psl_node_t p_psl_clock_expression(void)
84✔
11617
{
11618
   tree_t expr = p_expression();
84✔
11619
   solve_types(nametab, expr, std_type(NULL, STD_BOOLEAN));
84✔
11620

11621
   psl_node_t p = psl_new(P_CLOCK_DECL);
84✔
11622
   psl_set_tree(p, expr);
84✔
11623

11624
   return p;
84✔
11625
}
11626

11627
static tree_t p_psl_clock_declaration(void)
84✔
11628
{
11629
   // default clock is Clock_Expression ;
11630

11631
   BEGIN("PSL clock declaration");
84✔
11632

11633
   scan_as_psl();
84✔
11634

11635
   consume(tDEFAULT);
84✔
11636
   consume(tCLOCK);
84✔
11637

11638
   consume(tIS);
84✔
11639

11640
   scan_as_vhdl();
84✔
11641

11642
   psl_node_t p = p_psl_clock_expression();
84✔
11643

11644
   tree_t t = tree_new(T_PSL_DECL);
84✔
11645
   tree_set_psl(t, p);
84✔
11646
   tree_set_ident(t, well_known(W_DEFAULT_CLOCK));
84✔
11647

11648
   consume(tSEMI);
84✔
11649

11650
   psl_set_loc(p, CURRENT_LOC);
84✔
11651
   psl_check(p, nametab);
84✔
11652

11653
   insert_name(nametab, t, NULL);
84✔
11654

11655
   tree_set_loc(t, CURRENT_LOC);
84✔
11656
   return t;
84✔
11657
}
11658

11659
static psl_node_t p_psl_builtin_function_call(void)
43✔
11660
{
11661
   // prev (Any_Type [ , Number [ , Clock_Expression ]] )
11662
   //  | next ( Any_Type )
11663
   //  | stable ( Any_Type [ , Clock_Expression ] )
11664
   //  | rose ( Bit [ , Clock_Expression ] )
11665
   //  | fell ( Bit [ , Clock_Expression ] )
11666
   //  | ended ( Sequence [ , Clock_Expression ])
11667
   //  | nondet ( Value_Set )
11668
   //  | nondet_vector ( Number, Value_Set)
11669

11670
   BEGIN("PSL Built-in Function call");
43✔
11671

11672
   psl_builtin_kind_t kind;
43✔
11673
   switch (one_of(tPSLNEXT, tPREV, tSTABLE, tROSE, tFELL, tENDED,
43✔
11674
                  tNONDET, tNONDETV)) {
11675
   case tPSLNEXT: kind = PSL_BUILTIN_NEXT; break;
11676
   case tPREV:    kind = PSL_BUILTIN_PREV; break;
31✔
11677
   case tSTABLE:  kind = PSL_BUILTIN_STABLE; break;
3✔
11678
   case tROSE:    kind = PSL_BUILTIN_ROSE; break;
5✔
11679
   case tFELL:    kind = PSL_BUILTIN_FELL; break;
3✔
11680
   case tENDED:   kind = PSL_BUILTIN_ENDED; break;
1✔
UNCOV
11681
   case tNONDET:  kind = PSL_BUILTIN_NONDET; break;
×
UNCOV
11682
   case tNONDETV: kind = PSL_BUILTIN_NONDET_VECTOR; break;
×
11683
   default: should_not_reach_here();
11684
   }
11685

11686
   psl_node_t p = psl_new(P_BUILTIN_FCALL);
43✔
11687
   psl_set_subkind(p, kind);
43✔
11688

11689
   consume(tLPAREN);
43✔
11690

11691
   switch (kind) {
43✔
11692
   case PSL_BUILTIN_PREV:
31✔
11693
      {
11694
         psl_node_t p1 = p_hdl_expression(NULL, PSL_TYPE_ANY);
31✔
11695
         psl_add_operand(p, p1);
31✔
11696

11697
         if (optional(tCOMMA)) {
31✔
11698
            psl_node_t p2 = p_hdl_expression(NULL, PSL_TYPE_NUMERIC);
16✔
11699
            psl_add_operand(p, p2);
16✔
11700

11701
            if (optional(tCOMMA))
16✔
UNCOV
11702
               (void)p_psl_clock_expression();
×
11703
         }
11704
      }
11705
      break;
11706

UNCOV
11707
   case PSL_BUILTIN_NEXT:
×
11708
      {
UNCOV
11709
         psl_node_t p1 = p_hdl_expression(NULL, PSL_TYPE_ANY);
×
UNCOV
11710
         psl_add_operand(p, p1);
×
11711
      }
UNCOV
11712
      break;
×
11713

11714
   case PSL_BUILTIN_FELL:
11✔
11715
   case PSL_BUILTIN_ROSE:
11716
   case PSL_BUILTIN_STABLE:
11717
      {
11718
         psl_node_t p1 = p_hdl_expression(NULL, PSL_TYPE_BIT);
11✔
11719
         psl_add_operand(p, p1);
11✔
11720

11721
         if (optional(tCOMMA))
11✔
UNCOV
11722
            (void)p_psl_clock_expression();
×
11723
      }
11724
      break;
11725

11726
   case PSL_BUILTIN_ENDED:
1✔
11727
      {
11728
         psl_node_t p1 = p_psl_sequence();
1✔
11729
         psl_add_operand(p, p1);
1✔
11730

11731
         if (optional(tCOMMA))
1✔
UNCOV
11732
            (void)p_psl_clock_expression();
×
11733
      }
11734
      break;
11735

11736
   case PSL_BUILTIN_NONDET:
×
11737
      {
UNCOV
11738
         psl_node_t p1 = p_psl_value_set();
×
UNCOV
11739
         psl_add_operand(p, p1);
×
11740
      }
UNCOV
11741
      break;
×
11742

11743
   case PSL_BUILTIN_NONDET_VECTOR:
×
11744
      {
11745
         psl_node_t p1 = p_hdl_expression(NULL, PSL_TYPE_NUMERIC);
×
UNCOV
11746
         psl_add_operand(p, p1);
×
11747

UNCOV
11748
         consume(tCOMMA);
×
11749

11750
         psl_node_t p2 = p_psl_value_set();
×
UNCOV
11751
         psl_add_operand(p, p2);
×
11752
      }
UNCOV
11753
      break;
×
11754
   }
11755

11756
   consume(tRPAREN);
43✔
11757

11758
   psl_set_loc(p, CURRENT_LOC);
43✔
11759
   return p;
43✔
11760
}
11761

UNCOV
11762
static psl_node_t p_psl_index_range(void)
×
11763
{
11764
   // ( Range )
11765

11766
   BEGIN("PSL Index Range");
×
11767

UNCOV
11768
   consume(tLPAREN);
×
11769

11770
   psl_node_t p = p_psl_range(NULL);
×
11771

11772
   consume(tRPAREN);
×
11773

11774
   return p;
×
11775
}
11776

11777
static psl_node_t p_psl_parameter_definition(void)
1✔
11778
{
11779
   // Identifier [ Index_Range ] in Value_Set
11780

11781
   BEGIN("PSL Parameter Definition");
1✔
11782

11783
   tree_t decl = tree_new(T_PARAM_DECL);
1✔
11784

11785
   tree_set_ident(decl, p_identifier());
1✔
11786
   insert_name(nametab, decl, NULL);
1✔
11787

11788
   psl_node_t p = psl_new(P_PARAM_DECL);
1✔
11789
   psl_set_tree(p, decl);
1✔
11790

11791
   if (peek() == tLPAREN)
1✔
UNCOV
11792
      (void)p_psl_index_range();
×
11793

11794
   consume(tIN);
1✔
11795

11796
   psl_set_value(p, p_psl_value_set());
1✔
11797

11798
   psl_set_loc(p, CURRENT_LOC);
1✔
11799
   return p;
1✔
11800
}
11801

11802
static void p_psl_parameters_definition(psl_node_t p)
1✔
11803
{
11804
   // Parameter_Definition {, Parameter_Definition }
11805

11806
   BEGIN("PSL Parameters Definition");
2✔
11807

11808
   do {
1✔
11809
      psl_add_operand(p, p_psl_parameter_definition());
1✔
11810
   } while (optional(tCOMMA));
1✔
11811
}
1✔
11812

11813
static psl_node_t p_psl_parametrized_sere(void)
1✔
11814
{
11815
   // for Parameters_Definition : And_Or_SERE_OP { SERE }
11816

11817
   BEGIN("PSL Parametrized SERE");
1✔
11818

11819
   consume(tFOR);
1✔
11820

11821
   psl_node_t p = psl_new(P_PARAM_SERE);
1✔
11822
   p_psl_parameters_definition(p);
1✔
11823

11824
   consume(tCOLON);
1✔
11825

11826
   switch (one_of(tAMP, tDBLAMP, tBAR)) {
1✔
UNCOV
11827
   case tAMP:
×
UNCOV
11828
      psl_set_subkind(p, PSL_SERE_NEQ_AND);
×
UNCOV
11829
      break;
×
11830
   case tDBLAMP:
1✔
11831
      psl_set_subkind(p, PSL_SERE_EQU_AND);
1✔
11832
      break;
1✔
11833
   case tBAR:
×
UNCOV
11834
      psl_set_subkind(p, PSL_SERE_OR);
×
UNCOV
11835
      break;
×
11836
   }
11837

11838
   consume(tLBRACE);
1✔
11839

11840
   psl_set_value(p, p_psl_sere());
1✔
11841

11842
   consume(tRBRACE);
1✔
11843

11844
   psl_set_loc(p, CURRENT_LOC);
1✔
11845
   return p;
1✔
11846
}
11847

11848
static psl_node_t p_psl_count(void)
48✔
11849
{
11850
   // Number | Range
11851

11852
   BEGIN("PSL Count");
96✔
11853

11854
   tree_t count = p_expression();
48✔
11855

11856
   if (peek() == tTO)
48✔
11857
      return p_psl_range(count);
15✔
11858
   else
11859
      return p_hdl_expression(count, PSL_TYPE_NUMERIC);
33✔
11860
}
11861

11862
static psl_node_t p_psl_proc_block(psl_node_t value)
4✔
11863
{
11864
   // [[ Proc_Block_Item { Proc_Block_Item }]]
11865

11866
   BEGIN("PSL Procedural Block");
4✔
11867

11868
   consume(t2LSQUARE);
4✔
11869

11870
   tree_t b = tree_new(T_SEQUENCE);
4✔
11871

11872
   psl_node_t p = psl_new(P_PROC_BLOCK);
4✔
11873
   psl_set_value(p, value);
4✔
11874
   psl_set_tree(p, b);
4✔
11875

11876
   scan_as_vhdl();
4✔
11877

11878
   while (not_at_token(t2RSQUARE)) {
11✔
11879
      if (scan(tSIGNAL, tTYPE, tSUBTYPE, tFILE, tCONSTANT, tFUNCTION, tIMPURE,
7✔
11880
               tPURE, tPROCEDURE, tALIAS, tATTRIBUTE, tFOR, tCOMPONENT, tUSE,
11881
               tSHARED, tDISCONNECT, tGROUP, tPACKAGE))
11882
         p_block_declarative_item(b);
3✔
11883
      else
11884
         tree_add_stmt(b, p_sequential_statement());
4✔
11885
   }
11886

11887
   scan_as_psl();
4✔
11888

11889
   consume(t2RSQUARE);
4✔
11890

11891
   tree_set_loc(b, CURRENT_LOC);
4✔
11892
   psl_set_loc(p, CURRENT_LOC);
4✔
11893
   return p;
4✔
11894
}
11895

11896
static type_t p_psl_subtype_indication(void)
7✔
11897
{
11898
   BEGIN("PSL subtype indication");
14✔
11899

11900
   // Handle ambiguity in "p_subtype_indication". Two consecutive IDs
11901
   // may indicate resolution function, or only "type_mark" followed by
11902
   // actual parameter name.
11903
   token_t third = peek_nth(3);
7✔
11904
   if (peek() == tID && peek_nth(1) == tID &&
7✔
11905
       (third == tSEMI || third == tCOMMA || third == tRPAREN))
7✔
11906
      return p_type_mark();
6✔
11907
   else
11908
      return p_subtype_indication();
1✔
11909
}
11910

11911
static type_t p_psl_param_spec(psl_node_t node, psl_type_t *psl_type, class_t *class)
22✔
11912
{
11913
   //    const
11914
   //    | [const | mutable] Value_Parameter
11915
   //    | sequence
11916
   //    | property
11917
   //
11918
   //    Value_Parameter ::=
11919
   //       HDL_Type
11920
   //     | PSL_Type_Class
11921
   //
11922
   //    HDL_Type ::=
11923
   //       hdltype HDL_VARIABLE_TYPE
11924
   //
11925
   //    PSL_Type_Class ::=
11926
   //       boolean | bit | bitvector | numeric | string
11927

11928
   BEGIN("PSL Parameter specification");
44✔
11929

11930
   *class = C_SIGNAL;
22✔
11931

11932
   switch (peek()) {
22✔
11933
   case tPROPERTY:
1✔
11934
      consume(tPROPERTY);
1✔
11935
      *psl_type = PSL_TYPE_PROPERTY;
1✔
11936
      break;
1✔
11937

11938
   case tSEQUENCE:
1✔
11939
      consume(tSEQUENCE);
1✔
11940
      *psl_type = PSL_TYPE_SEQUENCE;
1✔
11941
      break;
1✔
11942

11943
   case tCONST:
10✔
11944
      *class = C_CONSTANT;
10✔
11945
      // Handle PSL 1.1 "const" only
11946
      if (peek_nth(2) == tID) {
10✔
11947
         consume(tCONST);
2✔
11948
         *psl_type = PSL_TYPE_NUMERIC;
2✔
11949
         return std_type(NULL, STD_INTEGER);
2✔
11950
      }
11951
   case tMUTABLE:
11952
      one_of(tCONST, tMUTABLE);
14✔
11953
   case tHDLTYPE:
18✔
11954
   case tBOOLEAN:
11955
   case tBIT:
11956
   case tBITVECTOR:
11957
   case tNUMERIC:
11958
   case tSTRINGK:
11959
      switch (one_of(tHDLTYPE, tBOOLEAN, tBIT, tBITVECTOR, tNUMERIC, tSTRINGK)) {
18✔
11960
      case tHDLTYPE:
7✔
11961
         *psl_type = PSL_TYPE_HDLTYPE;
7✔
11962
         scan_as_vhdl();
7✔
11963
         type_t t = p_psl_subtype_indication();
7✔
11964
         scan_as_psl();
7✔
11965
         return t;
7✔
11966
      case tBOOLEAN:
2✔
11967
         *psl_type = PSL_TYPE_BOOLEAN;
2✔
11968
         return std_type(NULL, STD_BOOLEAN);
2✔
11969
      case tBIT:
2✔
11970
         *psl_type = PSL_TYPE_BIT;
2✔
11971
         return std_type(NULL, STD_BIT);
2✔
11972
         break;
2✔
11973
      case tBITVECTOR:
2✔
11974
         *psl_type = PSL_TYPE_BITVECTOR;
2✔
11975
         return std_type(NULL, STD_BIT_VECTOR);
2✔
11976
         break;
3✔
11977
      case tNUMERIC:
3✔
11978
         *psl_type = PSL_TYPE_NUMERIC;
3✔
11979
         return std_type(NULL, STD_INTEGER);
3✔
11980
         break;
2✔
11981
      case tSTRINGK:
2✔
11982
         *psl_type = PSL_TYPE_STRING;
2✔
11983
         return std_type(NULL, STD_STRING);
2✔
11984
      }
11985
   }
11986

11987
  return type_new(T_NONE);
2✔
11988
}
11989

11990
static void p_psl_formal_parameter(psl_node_t node)
22✔
11991
{
11992
   // Param_Spec PSL_Identifier { , PSL_Identifier }
11993

11994
   BEGIN("PSL Formal parameter");
44✔
11995

11996
   psl_type_t psl_type = PSL_TYPE_NUMERIC;
22✔
11997
   class_t class;
22✔
11998
   type_t type = p_psl_param_spec(node, &psl_type, &class);
22✔
11999

12000
   do {
22✔
12001
      tree_t p = tree_new(T_PARAM_DECL);
22✔
12002
      tree_set_ident(p, p_identifier());
22✔
12003
      tree_set_loc(p, CURRENT_LOC);
22✔
12004
      tree_set_class(p, class);
22✔
12005
      tree_set_subkind(p, psl_type);
22✔
12006
      tree_set_type(p, type);
22✔
12007
      psl_add_port(node, p);
22✔
12008
      insert_name(nametab, p, NULL);
22✔
12009
   } while (optional(tCOMMA));
22✔
12010
}
22✔
12011

12012
static void p_psl_formal_parameter_list(psl_node_t node)
20✔
12013
{
12014
   // Formal_Parameter { ; Formal_Parameter }
12015

12016
   BEGIN("PSL Formal parameter list");
40✔
12017

12018
   p_psl_formal_parameter(node);
20✔
12019

12020
   while (optional(tSEMI))
22✔
12021
      p_psl_formal_parameter(node);
2✔
12022
}
20✔
12023

12024
static void p_psl_actual_parameter(psl_node_t node, bool seq)
7✔
12025
{
12026
   // Actual_Parameter ::=
12027
   //    AnyType | Sequence | Property
12028
   // sequence_Actual_Parameter ::=
12029
   //    AnyType | Sequence
12030

12031
   BEGIN("PSL Actual parameter");
14✔
12032

12033
   // "Sequence" includes "Any_Type" parsing (HDL or PSL expression)
12034
   // "Property" includes "Sequence" parsing
12035
   psl_add_operand(node, (seq) ? p_psl_sequence() : p_psl_property());
7✔
12036
}
7✔
12037

12038
static void p_psl_actual_parameter_list(psl_node_t node, bool seq)
6✔
12039
{
12040

12041
   // sequence_Actual_Parameter { , sequence_Actual_Parameter }
12042
   // Actual_Parameter { , Actual_Parameter }
12043

12044
   BEGIN("PSL Actual parameter list");
12✔
12045

12046
   p_psl_actual_parameter(node, seq);
6✔
12047

12048
   while (optional(tCOMMA))
7✔
12049
      p_psl_actual_parameter(node, seq);
1✔
12050
}
6✔
12051

12052
static psl_node_t p_psl_clocked_sere(psl_node_t head)
1✔
12053
{
12054
   // Braced_SERE @ Clock_Expression
12055

12056
   BEGIN("PSL Clocked SERE");
1✔
12057

12058
   consume(tAT);
1✔
12059

12060
   tree_t expr = p_expression();
1✔
12061
   solve_types(nametab, expr, std_type(NULL, STD_BOOLEAN));
1✔
12062

12063
   psl_node_t p = psl_new(P_CLOCKED);
1✔
12064
   psl_set_value(p, head);
1✔
12065
   psl_set_tree(p, expr);
1✔
12066

12067
   psl_set_loc(p, CURRENT_LOC);
1✔
12068
   return p;
1✔
12069
}
12070

12071
static psl_node_t p_psl_compound_sere(psl_node_t head)
36✔
12072
{
12073
   // Repeated_SERE | Braced_SERE | Clocked_SERE
12074
   //   | Compound_SERE '|' Compound_SERE
12075
   //   | Compound_SERE & Compound_SERE
12076
   //   | Compound_SERE && Compound_SERE
12077
   //   | Compound_SERE within Compound_SERE
12078
   //   | Parameterized_SERE
12079

12080
   BEGIN("PSL Compound SERE");
36✔
12081

12082
   if (head == NULL) {
36✔
12083
      switch (peek()) {
18✔
12084
      case tLBRACE:
16✔
12085
         head = p_psl_braced_sere();
16✔
12086
         break;
16✔
12087
      case tFOR:
1✔
12088
         head = p_psl_parametrized_sere();
1✔
12089
         break;
1✔
12090
      default:
1✔
12091
         head = p_psl_boolean(NULL);
1✔
12092
         break;
1✔
12093
      }
12094
   }
12095

12096
   for (;;) {
58✔
12097
      switch (peek()) {
58✔
12098
      case tBAR:
5✔
12099
         {
12100
            consume(tBAR);
5✔
12101

12102
            psl_node_t p = psl_new(P_SERE);
5✔
12103
            psl_add_operand(p, head);
5✔
12104
            psl_set_subkind(p, PSL_SERE_OR);
5✔
12105
            psl_add_operand(p, p_psl_compound_sere(NULL));
5✔
12106
            psl_set_loc(p, CURRENT_LOC);
5✔
12107

12108
            head = p;
5✔
12109
         }
12110
         break;
5✔
12111
      case tAMP:
2✔
12112
         {
12113
            consume(tAMP);
2✔
12114

12115
            psl_node_t p = psl_new(P_SERE);
2✔
12116
            psl_add_operand(p, head);
2✔
12117
            psl_set_subkind(p, PSL_SERE_NEQ_AND);
2✔
12118
            psl_add_operand(p, p_psl_compound_sere(NULL));
2✔
12119
            psl_set_loc(p, CURRENT_LOC);
2✔
12120

12121
            head = p;
2✔
12122
         }
12123
         break;
2✔
12124
      case tDBLAMP:
9✔
12125
         {
12126
            consume(tDBLAMP);
9✔
12127

12128
            psl_node_t p = psl_new(P_SERE);
9✔
12129
            psl_add_operand(p, head);
9✔
12130
            psl_set_subkind(p, PSL_SERE_EQU_AND);
9✔
12131
            psl_add_operand(p, p_psl_compound_sere(NULL));
9✔
12132
            psl_set_loc(p, CURRENT_LOC);
9✔
12133

12134
            head = p;
9✔
12135
         }
12136
         break;
9✔
12137
      case tWITHIN:
1✔
12138
         {
12139
            consume(tWITHIN);
1✔
12140

12141
            psl_node_t p = psl_new(P_SERE);
1✔
12142
            psl_add_operand(p, head);
1✔
12143
            psl_set_subkind(p, PSL_SERE_WITHIN);
1✔
12144
            psl_add_operand(p, p_psl_compound_sere(NULL));
1✔
12145
            psl_set_loc(p, CURRENT_LOC);
1✔
12146

12147
            head = p;
1✔
12148
         }
12149
         break;
1✔
12150
      case tPLUSRPT:
5✔
12151
      case tTIMESRPT:
12152
      case tGOTORPT:
12153
      case tEQRPT:
12154
      case t2LSQUARE:
12155
         head = p_psl_repeated_sere(head);
5✔
12156
         break;
5✔
UNCOV
12157
      case tAT:
×
UNCOV
12158
         head = p_psl_clocked_sere(head);
×
UNCOV
12159
         break;
×
12160
      default:
36✔
12161
         return head;
36✔
12162
      }
12163
   }
12164
}
12165

12166
static psl_node_t p_psl_sere(void)
394✔
12167
{
12168
   // Boolean | Boolean Proc_Block | Sequence | SERE ; SERE
12169
   //   | SERE : SERE | Compound_SERE
12170

12171
   BEGIN("PSL SERE");
394✔
12172

12173
   psl_node_t head;
394✔
12174
   switch (peek()) {
394✔
12175
   case tLBRACE:
362✔
12176
   case tPLUSRPT:
12177
   case tTIMESRPT:
12178
   case tGOTORPT:
12179
   case tEQRPT:
12180
   case tID:
12181
      head = p_psl_sequence();
362✔
12182
      break;
362✔
12183
   case tFOR:
1✔
12184
      head = p_psl_compound_sere(NULL);
1✔
12185
      break;
1✔
12186
   default:
31✔
12187
      head = p_psl_boolean(NULL);
31✔
12188
      break;
31✔
12189
   }
12190

12191
   for (;;) {
594✔
12192
      const token_t tok = peek();
594✔
12193

12194
      switch (tok) {
594✔
12195
      case tCOLON:
182✔
12196
      case tSEMI:
12197
         {
12198
            consume(tok);
182✔
12199

12200
            const psl_sere_kind_t kind =
182✔
12201
               tok == tCOLON ? PSL_SERE_FUSION : PSL_SERE_CONCAT;
182✔
12202

12203
            psl_node_t rhs = p_psl_sere();
182✔
12204

12205
            psl_node_t p = psl_new(P_SERE);
182✔
12206
            psl_set_subkind(p, kind);
182✔
12207
            psl_add_operand(p, head);
182✔
12208
            psl_set_loc(p, CURRENT_LOC);
182✔
12209

12210
            if (psl_kind(rhs) == P_SERE && psl_subkind(rhs) == kind) {
182✔
12211
               const int nops = psl_operands(rhs);
55✔
12212
               for (int i = 0; i < nops; i++)
168✔
12213
                  psl_add_operand(p, psl_operand(rhs, i));
113✔
12214
            }
12215
            else
12216
               psl_add_operand(p, rhs);
127✔
12217

12218
            head = p;
12219
         }
12220
         break;
12221
      case tBAR:
18✔
12222
      case tAMP:
12223
      case tDBLAMP:
12224
      case tWITHIN:
12225
      case tLSQUARE:
12226
      case tPLUSRPT:
12227
      case tTIMESRPT:
12228
      case tEQRPT:
12229
      case tGOTORPT:
12230
         head = p_psl_compound_sere(head);
18✔
12231
         continue;
18✔
12232
      default:
394✔
12233
         return head;
394✔
12234
      }
12235
   }
12236
}
12237

12238
static psl_node_t p_psl_braced_sere(void)
211✔
12239
{
12240
   // { [ [[ HDL DECL {HDL DECL} ]] ] SERE }
12241
   //   | { [free HDL Identifier {HDL Identifier} ] SERE }
12242

12243
   BEGIN("PSL Braced SERE");
211✔
12244

12245
   consume(tLBRACE);
211✔
12246

12247
   psl_node_t sere = p_psl_sere();
211✔
12248

12249
   consume(tRBRACE);
211✔
12250

12251
   psl_set_loc(sere, CURRENT_LOC);
211✔
12252
   return sere;
211✔
12253
}
12254

12255
static psl_node_t p_psl_repeated_sere(psl_node_t head)
70✔
12256
{
12257
   // Boolean [* [ Count ] ] | Sequence [* [ Count ] ] | [* [ Count ] ]
12258
   //   | Boolean [+] | Sequence [+] | [+] | Boolean [= Count ]
12259
   //   | Boolean [-> [ positive_Count ] ] | Boolean Proc_Block
12260
   //   | Sequence Proc_Block
12261

12262
   EXTEND("PSL Repeated SERE");
140✔
12263

12264
   assert(head != NULL);
70✔
12265

12266
   if (peek() == t2LSQUARE)
70✔
12267
      return p_psl_proc_block(head);
4✔
12268

12269
   psl_node_t p = psl_new(P_REPEAT);
66✔
12270
   psl_set_value(p, head);
66✔
12271

12272
   switch (one_of(tPLUSRPT, tTIMESRPT, tGOTORPT, tEQRPT, t2LSQUARE)) {
66✔
12273
   case tPLUSRPT:
9✔
12274
      psl_set_subkind(p, PSL_PLUS_REPEAT);
9✔
12275
      break;
9✔
12276

12277
   case tTIMESRPT:
32✔
12278
      psl_set_subkind(p, PSL_TIMES_REPEAT);
32✔
12279
      if (peek() != tRSQUARE)
32✔
12280
         psl_set_delay(p, p_psl_count());
23✔
12281
      consume(tRSQUARE);
32✔
12282
      break;
32✔
12283

12284
   case tGOTORPT:
9✔
12285
      psl_set_subkind(p, PSL_GOTO_REPEAT);
9✔
12286
      if (peek() != tRSQUARE)
9✔
12287
         psl_set_delay(p, p_psl_count());
9✔
12288
      consume(tRSQUARE);
9✔
12289
      break;
9✔
12290

12291
   case tEQRPT:
16✔
12292
      psl_set_subkind(p, PSL_EQUAL_REPEAT);
16✔
12293
      if (peek() != tRSQUARE)
16✔
12294
         psl_set_delay(p, p_psl_count());
16✔
12295
      consume(tRSQUARE);
16✔
12296
      break;
16✔
12297

12298
   default:
12299
      return head;
12300
   }
12301

12302
   psl_set_loc(p, CURRENT_LOC);
66✔
12303
   return p;
66✔
12304
}
12305

12306
static psl_node_t p_sequence_instance(tree_t decl)
7✔
12307
{
12308
   // Name [ ( Actual_Parameter_List ) ]
12309

12310
   EXTEND("PSL Sequence Instance");
7✔
12311

12312
   psl_node_t s_decl = tree_psl(decl);
7✔
12313

12314
   if (psl_kind(s_decl) != P_SEQUENCE_DECL)
7✔
12315
      parse_error(CURRENT_LOC, "%s is not a PSL sequence",
1✔
12316
                  istr(tree_ident(decl)));
12317

12318
   psl_node_t p = psl_new(P_SEQUENCE_INST);
7✔
12319
   psl_set_ref(p, s_decl);
7✔
12320

12321
   if (optional(tLPAREN)) {
7✔
12322
      p_psl_actual_parameter_list(p, true);
6✔
12323
      consume(tRPAREN);
6✔
12324
   }
12325

12326
   psl_set_loc(p, CURRENT_LOC);
7✔
12327
   return p;
7✔
12328
}
12329

12330
static psl_node_t p_psl_sequence(void)
562✔
12331
{
12332
   // Sequence_Instance | Repeated_SERE | Braced_SERE | Clocked_SERE
12333

12334
   BEGIN("PSL Sequence");
562✔
12335

12336
   psl_node_t head = NULL;
562✔
12337
   switch (peek()) {
562✔
12338
   case tLBRACE:
195✔
12339
      head = p_psl_braced_sere();
195✔
12340
      break;
195✔
12341
   case tID:
352✔
12342
      {
12343
         tree_t name = p_name(N_PSL);
352✔
12344

12345
         // Check for sequence instance
12346
         if (tree_kind(name) == T_REF && tree_has_ref(name)) {
352✔
12347
            tree_t decl = tree_ref(name);
349✔
12348
            if (tree_kind(decl) == T_PSL_DECL)
349✔
12349
               head = p_sequence_instance(decl);
7✔
12350
         }
12351

12352
         if (head == NULL)
7✔
12353
            head = p_psl_boolean(name);
345✔
12354
      }
12355
      break;
12356
   case tTIMESRPT:
10✔
12357
   case tPLUSRPT:
12358
      {
12359
         // Empty SERE is equivalent to TRUE
12360
         type_t std_bool = std_type(NULL, STD_BOOLEAN);
10✔
12361
         tree_t true_ref = make_ref(type_enum_literal(std_bool, 1));
10✔
12362

12363
         head = psl_new(P_HDL_EXPR);
10✔
12364
         psl_set_tree(head, true_ref);
10✔
12365
         psl_set_type(head, PSL_TYPE_BOOLEAN);
10✔
12366
      }
12367
      break;
10✔
12368
   default:
5✔
12369
      head = p_psl_boolean(NULL);
5✔
12370
      break;
5✔
12371
   }
12372

12373
   for (;;) {
628✔
12374
      switch (peek()) {
628✔
12375
      case tPLUSRPT:
65✔
12376
      case tTIMESRPT:
12377
      case tGOTORPT:
12378
      case tEQRPT:
12379
      case t2LSQUARE:
12380
         head = p_psl_repeated_sere(head);
65✔
12381
         break;
65✔
12382
      case tAT:
1✔
12383
         head = p_psl_clocked_sere(head);
1✔
12384
         break;
1✔
12385
      default:
562✔
12386
         return head;
562✔
12387
      }
12388
   }
12389
}
12390

12391
static psl_node_t p_psl_fl_property(void)
1,005✔
12392
{
12393
   // Boolean | ( FL_Property ) | FL_Property @ Clock_Expression
12394
   //   | always FL_Property
12395
   //   | never FL_Property
12396
   //   | eventually! FL_Property
12397
   //   | next FL_Property | next [ Number ] FL_Property
12398
   //   | next! FL_Property | next! [ Number ] FL_Property
12399
   //   | next_event ( Boolean ) ( FL_Property )
12400
   //   | next_event ( Boolean ) ( FL_Property )
12401
   //   | next_event! ( Boolean ) [ Number ] ( FL_Property )
12402
   //   | next_event! ( Boolean ) [ Number ] ( FL_Property )
12403
   //   | FL_Property -> FL_Property
12404
   //   | FL_Property <-> FL_Property
12405
   //   | FL_Property or FL_Property
12406
   //   | FL_Property and FL_Property
12407
   //   | FL_Property until! FL_Property
12408
   //   | FL_Property until!_ FL_Property
12409
   //   | FL_Property until FL_Property
12410
   //   | FL_Property until_ FL_Property
12411
   //   | FL_Property sync_abort Boolean
12412
   //   | FL_Property async_abort Boolean
12413
   //   | FL_Property abort Boolean
12414
   //   | Sequence [ ! ]
12415
   //   | Sequence |-> FL_Property
12416
   //   | Sequence |=> FL_Property
12417

12418
   BEGIN("FL property");
2,010✔
12419

12420
   psl_node_t p = NULL;
1,005✔
12421
   const token_t tok = peek();
1,005✔
12422
   switch (tok) {
1,005✔
12423
   case tALWAYS:
140✔
12424
      {
12425
         consume(tALWAYS);
140✔
12426

12427
         p = psl_new(P_ALWAYS);
140✔
12428
         psl_set_value(p, p_psl_fl_property());
140✔
12429
         psl_set_loc(p, CURRENT_LOC);
140✔
12430
      }
12431
      break;
140✔
12432

12433
   case tNEVER:
10✔
12434
      {
12435
         consume(tNEVER);
10✔
12436

12437
         p = psl_new(P_NEVER);
10✔
12438
         psl_set_value(p, p_psl_fl_property());
10✔
12439
         psl_set_loc(p, CURRENT_LOC);
10✔
12440
      }
12441
      break;
10✔
12442

12443
   case tEVENTUALLY:
8✔
12444
      {
12445
         consume(tEVENTUALLY);
8✔
12446

12447
         p = psl_new(P_EVENTUALLY);
8✔
12448
         psl_set_value(p, p_psl_fl_property());
8✔
12449
         psl_set_loc(p, CURRENT_LOC);
8✔
12450
      }
12451
      break;
8✔
12452

12453
   case tPSLNEXT:
84✔
12454
   case tNEXT1:
12455
      {
12456
         consume(tok);
84✔
12457

12458
         p = psl_new(P_NEXT);
84✔
12459

12460
         if (tok == tNEXT1)
84✔
12461
            psl_set_flag(p, PSL_F_STRONG);
1✔
12462

12463
         if (optional(tLSQUARE)) {
84✔
12464
            psl_node_t count = p_hdl_expression(NULL, PSL_TYPE_NUMERIC);
28✔
12465
            psl_set_delay(p, count);
28✔
12466

12467
            consume(tRSQUARE);
28✔
12468
         }
12469

12470
         psl_set_value(p, p_psl_fl_property());
84✔
12471
         psl_set_loc(p, CURRENT_LOC);
84✔
12472
      }
12473
      break;
84✔
12474

12475
   case tNEXTA:
2✔
12476
   case tNEXTA1:
12477
      {
12478
         consume(tok);
2✔
12479

12480
         p = psl_new(P_NEXT_A);
2✔
12481

12482
         if (tok == tNEXTA1)
2✔
UNCOV
12483
            psl_set_flag(p, PSL_F_STRONG);
×
12484

12485
         consume(tLSQUARE);
2✔
12486

12487
         psl_set_delay(p, p_psl_range(NULL));
2✔
12488

12489
         consume(tRSQUARE);
2✔
12490

12491
         psl_set_value(p, p_psl_fl_property());
2✔
12492
         psl_set_loc(p, CURRENT_LOC);
2✔
12493
      }
12494
      break;
2✔
12495

12496
   case tNEXTE:
1✔
12497
   case tNEXTE1:
12498
      {
12499
         consume(tok);
1✔
12500

12501
         p = psl_new(P_NEXT_E);
1✔
12502

12503
         if (tok == tNEXTE1)
1✔
UNCOV
12504
            psl_set_flag(p, PSL_F_STRONG);
×
12505

12506
         consume(tLSQUARE);
1✔
12507

12508
         psl_set_delay(p, p_psl_range(NULL));
1✔
12509

12510
         consume(tRSQUARE);
1✔
12511

12512
         psl_set_value(p, p_psl_fl_property());
1✔
12513
         psl_set_loc(p, CURRENT_LOC);
1✔
12514
      }
12515
      break;
1✔
12516

12517
   case tNEXTEVENT:
1✔
12518
   case tNEXTEVENT1:
12519
      {
12520
         consume(tok);
1✔
12521

12522
         p = psl_new(P_NEXT_EVENT);
1✔
12523

12524
         if (tok == tNEXTEVENT1)
1✔
UNCOV
12525
            psl_set_flag(p, PSL_F_STRONG);
×
12526

12527
         consume(tLPAREN);
1✔
12528
         (void)p_psl_fl_property();
1✔
12529
         consume(tRPAREN);
1✔
12530

12531
         if (optional(tLSQUARE)) {
1✔
12532
            psl_node_t count = p_hdl_expression(NULL, PSL_TYPE_NUMERIC);
1✔
12533
            psl_set_delay(p, count);
1✔
12534

12535
            consume(tRSQUARE);
1✔
12536
         }
12537

12538
         consume(tLPAREN);
1✔
12539
         psl_set_value(p, p_psl_fl_property());
1✔
12540
         consume(tRPAREN);
1✔
12541

12542
         psl_set_loc(p, CURRENT_LOC);
1✔
12543
      }
12544
      break;
1✔
12545

12546
   case tLPAREN:
210✔
12547
      {
12548
         consume(tLPAREN);
210✔
12549
         p = p_psl_fl_property();
210✔
12550
         consume(tRPAREN);
210✔
12551

12552
         if (psl_kind(p) == P_HDL_EXPR && is_vhdl_infix_op(peek())) {
210✔
12553
            // Handle awkward cases like "always x -> (x or y) = '1'"
12554
            // where we cannot determine ahead-of-time whether (x or y)
12555
            // is a VHDL expression or PSL property
12556
            tree_t expr = p_expression_with_head(psl_tree(p));
14✔
12557
            psl_set_tree(p, expr);
14✔
12558
         }
12559

12560
         psl_set_loc(p, CURRENT_LOC);
210✔
12561
      }
12562
      break;
210✔
12563

12564
   case tLBRACE:
61✔
12565
   case tPLUSRPT:
12566
   case tTIMESRPT:
12567
   case tGOTORPT:
12568
   case tEQRPT:
12569
      p = p_psl_sequence();
61✔
12570
      break;
61✔
12571

12572
   default:
488✔
12573
      p = p_psl_or_hdl_expression();
488✔
12574
      break;
488✔
12575
   }
12576

12577
   const token_t infix = peek();
1,005✔
12578
   switch (infix) {
1,005✔
12579
   case tIFIMPL:
156✔
12580
   case tIFFIMPL:
12581
   case tAND:
12582
   case tOR:
12583
      {
12584
         consume(infix);
156✔
12585

12586
         psl_logic_t kind;
156✔
12587
         switch (infix) {
156✔
12588
         case tAND:     kind = PSL_LOGIC_AND; break;
12589
         case tOR:      kind = PSL_LOGIC_OR; break;
30✔
12590
         case tIFIMPL:  kind = PSL_LOGIC_IF; break;
111✔
12591
         case tIFFIMPL: kind = PSL_LOGIC_IFF; break;
6✔
12592
         default: should_not_reach_here();
12593
         }
12594

12595
         psl_node_t right = p_psl_fl_property();
156✔
12596

12597
         // A logical operation where the two operands are HDL
12598
         // expressions should be parsed as a single HDL expression
12599
         const bool prefer_hdl =
312✔
12600
            (kind == PSL_LOGIC_OR || kind == PSL_LOGIC_AND)
156✔
12601
            && psl_kind(p) == P_HDL_EXPR && psl_kind(right) == P_HDL_EXPR;
156✔
12602

12603
         if (prefer_hdl) {
156✔
12604
            ident_t op = well_known(kind == PSL_LOGIC_OR ? W_OP_OR : W_OP_AND);
38✔
12605

12606
            tree_t fcall = tree_new(T_FCALL);
29✔
12607
            tree_set_ident(fcall, op);
29✔
12608
            tree_set_loc(fcall, CURRENT_LOC);
29✔
12609
            add_param(fcall, psl_tree(p), P_POS, NULL);
29✔
12610
            add_param(fcall, psl_tree(right), P_POS, NULL);
29✔
12611

12612
            psl_set_tree(p, fcall);
29✔
12613
            psl_set_loc(p, CURRENT_LOC);
29✔
12614
            return p;
29✔
12615
         }
12616
         else {
12617
            psl_node_t log = psl_new(P_LOGICAL);
127✔
12618
            psl_set_subkind(log, kind);
127✔
12619
            psl_add_operand(log, p);
127✔
12620
            psl_add_operand(log, right);
127✔
12621
            psl_set_loc(log, CURRENT_LOC);
127✔
12622

12623
            return log;
127✔
12624
         }
12625
      }
12626

12627
   case tSUFFIXOVR:
11✔
12628
   case tSUFFIXNON:
12629
      {
12630
         consume(infix);
11✔
12631

12632
         const psl_suffix_impl_t kind =
11✔
12633
            infix == tSUFFIXOVR ? PSL_SUFFIX_OVERLAP : PSL_SUFFIX_NON;
11✔
12634

12635
         psl_node_t suff = psl_new(P_SUFFIX_IMPL);
11✔
12636
         psl_set_subkind(suff, kind);
11✔
12637
         psl_add_operand(suff, p);
11✔
12638
         psl_add_operand(suff, p_psl_fl_property());
11✔
12639
         psl_set_loc(suff, CURRENT_LOC);
11✔
12640

12641
         return suff;
11✔
12642
      }
12643

12644
   case tUNTIL:
26✔
12645
   case tUNTIL_:
12646
   case tUNTIL1:
12647
   case tUNTIL1_:
12648
      {
12649
         consume(infix);
26✔
12650

12651
         psl_flags_t flags = 0;
26✔
12652
         if (infix == tUNTIL1 || infix == tUNTIL1_)
26✔
12653
            flags |= PSL_F_STRONG;
3✔
12654
         if (infix == tUNTIL_ || infix == tUNTIL1_)
26✔
12655
            flags |= PSL_F_INCLUSIVE;
13✔
12656

12657
         psl_node_t until = psl_new(P_UNTIL);
26✔
12658
         psl_set_flag(until, flags);
26✔
12659
         psl_add_operand(until, p);
26✔
12660
         psl_add_operand(until, p_psl_fl_property());
26✔
12661
         psl_set_loc(until, CURRENT_LOC);
26✔
12662

12663
         return until;
26✔
12664
      }
12665

12666
   case tBEFORE:
9✔
12667
   case tBEFORE_:
12668
   case tBEFORE1:
12669
   case tBEFORE1_:
12670
      {
12671
         consume(infix);
9✔
12672

12673
         psl_flags_t flags = 0;
9✔
12674
         if (infix == tBEFORE1 || infix == tBEFORE1_)
9✔
12675
            flags |= PSL_F_STRONG;
1✔
12676
         if (infix == tBEFORE_ || infix == tBEFORE1_)
9✔
12677
            flags |= PSL_F_INCLUSIVE;
3✔
12678

12679
         psl_node_t until = psl_new(P_BEFORE);
9✔
12680
         psl_set_flag(until, flags);
9✔
12681
         psl_add_operand(until, p);
9✔
12682
         psl_add_operand(until, p_psl_fl_property());
9✔
12683
         psl_set_loc(until, CURRENT_LOC);
9✔
12684

12685
         return until;
9✔
12686
      }
12687

12688
   case tABORT:
9✔
12689
   case tASYNC_ABORT:
12690
   case tSYNC_ABORT:
12691
      {
12692
         consume(infix);
9✔
12693

12694
         const psl_abort_t kind =
9✔
12695
            infix == tSYNC_ABORT ? PSL_ABORT_SYNC : PSL_ABORT_ASYNC;
9✔
12696

12697
         psl_node_t abort = psl_new(P_ABORT);
9✔
12698
         psl_set_subkind(abort, kind);
9✔
12699
         psl_add_operand(abort, p);
9✔
12700
         psl_add_operand(abort, p_psl_or_hdl_expression());
9✔
12701
         psl_set_loc(abort, CURRENT_LOC);
9✔
12702

12703
         return abort;
9✔
12704
      }
12705

12706
   case tAT:
4✔
12707
      {
12708
         consume(tAT);
4✔
12709

12710
         tree_t expr = p_expression();
4✔
12711
         solve_types(nametab, expr, std_type(NULL, STD_BOOLEAN));
4✔
12712

12713
         psl_node_t clk = psl_new(P_CLOCKED);
4✔
12714
         psl_set_value(clk, p);
4✔
12715
         psl_set_tree(clk, expr);
4✔
12716

12717
         psl_set_loc(clk, CURRENT_LOC);
4✔
12718
         return clk;
4✔
12719
      }
12720

12721
   default:
12722
      return p;
12723
   }
12724
}
12725

12726
static void p_psl_report(psl_node_t p)
76✔
12727
{
12728
   consume(tREPORT);
76✔
12729

12730
   tree_t m = p_expression();
76✔
12731
   psl_set_message(p, m);
76✔
12732
   solve_types(nametab, m, std_type(NULL, STD_STRING));
76✔
12733
}
76✔
12734

12735
static psl_node_t p_psl_property(void)
346✔
12736
{
12737
   // Replicator Property | FL_Property | OBE_Property
12738

12739
   BEGIN("property");
692✔
12740

12741
   return p_psl_fl_property();
346✔
12742
}
12743

12744
static psl_node_t p_psl_assert_directive(void)
166✔
12745
{
12746
   // assert Property [ report String ] ;
12747

12748
   BEGIN("assert directive");
166✔
12749

12750
   consume(tASSERT);
166✔
12751

12752
   psl_node_t p = with_default_clock(p_psl_property());
166✔
12753

12754
   psl_node_t a = psl_new(P_ASSERT);
166✔
12755
   psl_set_value(a, p);
166✔
12756

12757
   if (peek() == tREPORT)
166✔
12758
      p_psl_report(a);
3✔
12759

12760
   consume(tSEMI);
166✔
12761

12762
   psl_set_loc(a, CURRENT_LOC);
166✔
12763
   return a;
166✔
12764
}
12765

12766
static psl_node_t p_psl_assume_directive(void)
4✔
12767
{
12768
   // assume Property ;
12769
   // assume_guarantee Property [ report String ] ;
12770

12771
   BEGIN("assume directive");
4✔
12772

12773
   token_t tok = one_of(tASSUME, tASSUMEG);
4✔
12774

12775
   psl_node_t a = psl_new(P_ASSUME);
4✔
12776
   if (tok == tASSUME)
4✔
12777
      psl_set_subkind(a, PSL_NO_GUARANTEE);
3✔
12778
   else
12779
      psl_set_subkind(a, PSL_GUARANTEE);
1✔
12780

12781
   psl_node_t p = with_default_clock(p_psl_property());
4✔
12782
   psl_set_value(a, p);
4✔
12783

12784
   if (peek() == tREPORT && tok == tASSUMEG)
4✔
12785
      p_psl_report(a);
1✔
12786

12787
   consume(tSEMI);
4✔
12788

12789
   psl_set_loc(a, CURRENT_LOC);
4✔
12790
   return a;
4✔
12791
}
12792

12793
static psl_node_t p_psl_restrict_directive(void)
4✔
12794
{
12795
   // restrict Sequence  ;
12796
   // restrict_guarantee Sequence [ report String ] ;
12797

12798
   BEGIN("restrict directive");
4✔
12799

12800
   token_t tok = peek();
4✔
12801
   assert(tok == tRESTRICT || tok == tRESTRICTG);
4✔
12802
   consume(tok);
4✔
12803

12804
   psl_node_t a = psl_new(P_RESTRICT);
4✔
12805
   if (tok == tRESTRICT)
4✔
12806
      psl_set_subkind(a, PSL_NO_GUARANTEE);
3✔
12807
   else
12808
      psl_set_subkind(a, PSL_GUARANTEE);
1✔
12809

12810
   psl_node_t p = with_default_clock(p_psl_sequence());
4✔
12811
   psl_set_value(a, p);
4✔
12812

12813
   if (peek() == tREPORT && tok == tRESTRICTG)
4✔
12814
      p_psl_report(a);
1✔
12815

12816
   consume(tSEMI);
4✔
12817

12818
   psl_set_loc(a, CURRENT_LOC);
4✔
12819
   return a;
4✔
12820
}
12821

12822
static psl_node_t p_psl_fairness(void)
3✔
12823
{
12824
   // fairness Boolean ;
12825
   // strong fairness Boolean , Boolean ;
12826

12827
   BEGIN("fairness statement");
3✔
12828

12829
   psl_flags_t flags = 0;
3✔
12830
   if (optional(tSTRONG))
3✔
12831
      flags |= PSL_F_STRONG;
1✔
12832

12833
   consume(tFAIRNESS);
3✔
12834

12835
   psl_node_t a = psl_new(P_FAIRNESS);
3✔
12836
   psl_set_flag(a, flags);
3✔
12837

12838
   tree_t e1 = p_expression();
3✔
12839
   solve_psl_condition(nametab, &e1);
3✔
12840

12841
   psl_node_t p1 = psl_new(P_HDL_EXPR);
3✔
12842
   psl_set_tree(p1, e1);
3✔
12843

12844
   psl_add_operand(a, p1);
3✔
12845

12846
   if (flags & PSL_F_STRONG) {
3✔
12847
      consume(tCOMMA);
1✔
12848

12849
      tree_t e2 = p_expression();
1✔
12850
      solve_psl_condition(nametab, &e2);
1✔
12851

12852
      psl_node_t p2 = psl_new(P_HDL_EXPR);
1✔
12853
      psl_set_tree(p2, e2);
1✔
12854

12855
      psl_add_operand(a, p2);
1✔
12856
   }
12857

12858
   consume(tSEMI);
3✔
12859

12860
   psl_set_loc(a, CURRENT_LOC);
3✔
12861
   return a;
3✔
12862
}
12863

12864
static psl_node_t p_psl_cover_directive(void)
121✔
12865
{
12866
   // cover Sequence [ report String ] ;
12867

12868
   BEGIN("cover directive");
121✔
12869

12870
   consume(tCOVER);
121✔
12871

12872
   psl_node_t p = with_default_clock(p_psl_sequence());
121✔
12873

12874
   psl_node_t a = psl_new(P_COVER);
121✔
12875
   psl_set_value(a, p);
121✔
12876

12877
   if (peek() == tREPORT)
121✔
12878
      p_psl_report(a);
71✔
12879

12880
   consume(tSEMI);
121✔
12881

12882
   psl_set_loc(a, CURRENT_LOC);
121✔
12883
   return a;
121✔
12884
}
12885

12886
static psl_node_t p_psl_verification_directive(void)
299✔
12887
{
12888
   // Assert_Directive | Assume_Directive | Restrict_Directive
12889
   //   | Restrict!_Directive | Cover_Directive | Fairness_Statement
12890

12891
   BEGIN("verification directive");
598✔
12892

12893
   switch (peek()) {
299✔
12894
   case tASSERT:
166✔
12895
      return p_psl_assert_directive();
166✔
12896
   case tASSUME:
4✔
12897
   case tASSUMEG:
12898
      return p_psl_assume_directive();
4✔
12899
   case tRESTRICT:
4✔
12900
   case tRESTRICTG:
12901
      return p_psl_restrict_directive();
4✔
12902
   case tFAIRNESS:
3✔
12903
   case tSTRONG:
12904
      return p_psl_fairness();
3✔
12905
   case tCOVER:
121✔
12906
      return p_psl_cover_directive();
121✔
12907
   default:
1✔
12908
      one_of(tASSERT, tASSUME, tASSUMEG, tRESTRICT, tRESTRICTG, tFAIRNESS,
1✔
12909
             tSTRONG, tCOVER);
12910
      return NULL;
1✔
12911
   }
12912
}
12913

12914
static tree_t p_psl_directive(ident_t label)
299✔
12915
{
12916
   // [ Label : ] Verification_Directive
12917

12918
   BEGIN("PSL directive");
299✔
12919

12920
   tree_t t = tree_new(T_PSL_DIRECT);
299✔
12921

12922
   scan_as_psl();
299✔
12923

12924
   // Verification directive can contain Proc_Block with
12925
   // local declarations -> Push scope
12926
   push_scope(nametab);
299✔
12927
   insert_names_for_psl(nametab);
299✔
12928

12929
   psl_node_t p = p_psl_verification_directive();
299✔
12930

12931
   scan_as_vhdl();
299✔
12932

12933
   if (p != NULL) {
299✔
12934
      tree_set_psl(t, p);
298✔
12935
      psl_check(p, nametab);
298✔
12936
   }
12937

12938
   pop_scope(nametab);
299✔
12939

12940
   tree_set_loc(t, CURRENT_LOC);
299✔
12941
   ensure_labelled(t, label);
299✔
12942

12943
   if (label) insert_name(nametab, t, NULL);
299✔
12944

12945
   return t;
299✔
12946
}
12947

12948
static tree_t p_psl_property_declaration(void)
21✔
12949
{
12950
   // property PSL_Identifier [ ( Formal_Parameter_List ) ] is Property ;
12951

12952
   BEGIN("PSL property declaration");
21✔
12953

12954
   scan_as_psl();
21✔
12955

12956
   psl_node_t decl = psl_new(P_PROPERTY_DECL);
21✔
12957

12958
   consume(tPROPERTY);
21✔
12959

12960
   ident_t ident = p_identifier();
21✔
12961
   psl_set_ident(decl, ident);
21✔
12962

12963
   tree_t t = tree_new(T_PSL_DECL);
21✔
12964
   tree_set_psl(t, decl);
21✔
12965
   tree_set_ident(t, ident);
21✔
12966
   insert_name(nametab, t, NULL);
21✔
12967

12968
   push_scope(nametab);
21✔
12969
   scope_set_container(nametab, t);
21✔
12970
   insert_names_for_psl(nametab);
21✔
12971

12972
   if (optional(tLPAREN)) {
21✔
12973
      p_psl_formal_parameter_list(decl);
19✔
12974
      consume(tRPAREN);
19✔
12975
   }
12976

12977
   consume(tIS);
21✔
12978

12979
   psl_node_t prop = p_psl_property();
21✔
12980
   psl_set_value(decl, prop);
21✔
12981

12982
   consume(tSEMI);
21✔
12983

12984
   psl_set_loc(decl, CURRENT_LOC);
21✔
12985
   psl_check(decl, nametab);
21✔
12986

12987
   pop_scope(nametab);
21✔
12988

12989
   scan_as_vhdl();
21✔
12990

12991
   tree_set_loc(t, CURRENT_LOC);
21✔
12992
   return t;
21✔
12993
}
12994

12995
static tree_t p_psl_sequence_declaration(void)
5✔
12996
{
12997
   // sequence PSL_Identifier [ ( Formal_Parameter_List ) ] is Sequence ;
12998

12999
   BEGIN("PSL sequence declaration");
5✔
13000

13001
   scan_as_psl();
5✔
13002

13003
   psl_node_t decl = psl_new(P_SEQUENCE_DECL);
5✔
13004

13005
   consume(tSEQUENCE);
5✔
13006

13007
   ident_t ident = p_identifier();
5✔
13008
   psl_set_ident(decl, ident);
5✔
13009

13010
   tree_t t = tree_new(T_PSL_DECL);
5✔
13011
   tree_set_psl(t, decl);
5✔
13012
   tree_set_ident(t, ident);
5✔
13013
   insert_name(nametab, t, NULL);
5✔
13014

13015
   push_scope(nametab);
5✔
13016
   scope_set_container(nametab, t);
5✔
13017
   insert_names_for_psl(nametab);
5✔
13018

13019
   if (optional(tLPAREN)) {
5✔
13020
      p_psl_formal_parameter_list(decl);
1✔
13021
      consume(tRPAREN);
1✔
13022
   }
13023

13024
   consume(tIS);
5✔
13025

13026
   psl_node_t prop = p_psl_sequence();
5✔
13027
   psl_set_value(decl, prop);
5✔
13028

13029
   consume(tSEMI);
5✔
13030

13031
   psl_set_loc(decl, CURRENT_LOC);
5✔
13032
   psl_check(decl, nametab);
5✔
13033

13034
   pop_scope(nametab);
5✔
13035

13036
   scan_as_vhdl();
5✔
13037

13038
   tree_set_loc(t, CURRENT_LOC);
5✔
13039
   return t;
5✔
13040
}
13041

13042
static tree_t p_psl_endpoint_declaration(void)
1✔
13043
{
13044
   // sequence PSL_Identifier is Sequence ;
13045

13046
   BEGIN("PSL endpoint declaration");
1✔
13047

13048
   scan_as_psl();
1✔
13049

13050
   psl_node_t decl = psl_new(P_ENDPOINT_DECL);
1✔
13051

13052
   consume(tENDPOINT);
1✔
13053

13054
   ident_t ident = p_identifier();
1✔
13055
   psl_set_ident(decl, ident);
1✔
13056

13057
   tree_t t = tree_new(T_PSL_DECL);
1✔
13058
   tree_set_psl(t, decl);
1✔
13059
   tree_set_ident(t, ident);
1✔
13060
   insert_name(nametab, t, NULL);
1✔
13061

13062
   push_scope(nametab);
1✔
13063
   insert_names_for_psl(nametab);
1✔
13064

13065
   consume(tIS);
1✔
13066

13067
   psl_node_t prop = p_psl_sequence();
1✔
13068
   psl_set_value(decl, prop);
1✔
13069

13070
   consume(tSEMI);
1✔
13071

13072
   psl_set_loc(decl, CURRENT_LOC);
1✔
13073
   psl_check(decl, nametab);
1✔
13074

13075
   pop_scope(nametab);
1✔
13076

13077
   scan_as_vhdl();
1✔
13078

13079
   tree_set_loc(t, CURRENT_LOC);
1✔
13080
   return t;
1✔
13081
}
13082

13083
static void p_psl_declaration(tree_t parent)
111✔
13084
{
13085
   // Property_Declaration | Sequence_Declaration | Clock_Declaration
13086

13087
   BEGIN("PSL declaration");
222✔
13088

13089
   switch (peek()) {
111✔
13090
   case tPROPERTY:
21✔
13091
      tree_add_decl(parent, p_psl_property_declaration());
21✔
13092
      break;
21✔
13093
   case tSEQUENCE:
5✔
13094
      tree_add_decl(parent, p_psl_sequence_declaration());
5✔
13095
      break;
5✔
13096
   case tDEFAULT:
84✔
13097
      tree_add_decl(parent, p_psl_clock_declaration());
84✔
13098
      break;
84✔
13099
   case tENDPOINT:
1✔
13100
      tree_add_decl(parent, p_psl_endpoint_declaration());
1✔
13101
      break;
1✔
UNCOV
13102
   default:
×
UNCOV
13103
      one_of(tPROPERTY, tSEQUENCE, tDEFAULT, tENDPOINT);
×
UNCOV
13104
      break;
×
13105
   }
13106
}
111✔
13107

13108
static tree_t p_psl_or_concurrent_assert(ident_t label)
155✔
13109
{
13110
   // Handle the ambiguity between a PSL assertion and a normal VHDL
13111
   // concurrent assertion statement.
13112
   //
13113
   //  assert condition [ report expression ] [ severity expression ] ;
13114
   //   | assert Property [ report String ] ;
13115

13116
   BEGIN("PSL or concurrent assertion statement");
310✔
13117

13118
   if (peek() == tPOSTPONED)
155✔
UNCOV
13119
      return p_concurrent_assertion_statement(label);   // Cannot be PSL
×
13120

13121
   consume(tASSERT);
155✔
13122

13123
   push_scope(nametab);
155✔
13124
   insert_names_for_psl(nametab);
155✔
13125

13126
   scan_as_psl();
155✔
13127

13128
   psl_node_t p = p_psl_property();
155✔
13129

13130
   scan_as_vhdl();
155✔
13131

13132
   tree_t conc;
155✔
13133
   if (psl_kind(p) == P_HDL_EXPR) {
155✔
13134
      tree_t value = psl_tree(p);
110✔
13135
      solve_condition(nametab, &value);
110✔
13136

13137
      tree_t s = tree_new(T_ASSERT);
110✔
13138
      tree_set_value(s, value);
110✔
13139

13140
      if (optional(tREPORT)) {
110✔
13141
         tree_t message = p_expression();
18✔
13142
         solve_types(nametab, message, std_type(NULL, STD_STRING));
18✔
13143
         tree_set_message(s, message);
18✔
13144
      }
13145

13146
      if (optional(tSEVERITY)) {
110✔
13147
         tree_t severity = p_expression();
18✔
13148
         solve_types(nametab, severity, std_type(NULL, STD_SEVERITY_LEVEL));
18✔
13149
         tree_set_severity(s, severity);
18✔
13150
      }
13151

13152
      consume(tSEMI);
110✔
13153

13154
      tree_set_loc(s, CURRENT_LOC);
110✔
13155

13156
      conc = tree_new(T_CONCURRENT);
110✔
13157
      tree_add_stmt(conc, s);
110✔
13158
   }
13159
   else {
13160
      psl_node_t a = psl_new(P_ASSERT);
45✔
13161
      psl_set_value(a, with_default_clock(p));
45✔
13162

13163
      if (peek() == tREPORT)
45✔
UNCOV
13164
         p_psl_report(a);
×
13165

13166
      consume(tSEMI);
45✔
13167

13168
      psl_set_loc(a, CURRENT_LOC);
45✔
13169
      psl_check(a, nametab);
45✔
13170

13171
      conc = tree_new(T_PSL_DIRECT);
45✔
13172
      tree_set_psl(conc, a);
45✔
13173
   }
13174

13175
   pop_scope(nametab);
155✔
13176

13177
   tree_set_loc(conc, CURRENT_LOC);
155✔
13178
   ensure_labelled(conc, label);
155✔
13179

13180
   if (label) insert_name(nametab, conc, NULL);
155✔
13181
   sem_check(conc, nametab);
155✔
13182
   return conc;
155✔
13183
}
13184

13185
static tree_t p_concurrent_statement(void)
10,334✔
13186
{
13187
   // block_statement | process_statement | concurrent_procedure_call_statement
13188
   //   | concurrent_assertion_statement
13189
   //   | concurrent_signal_assignment_statement
13190
   //   | component_instantiation_statement | generate_statement
13191
   //   | 2008: psl_directive
13192

13193
   BEGIN("concurrent statement");
20,665✔
13194

13195
   ident_t label = NULL;
10,334✔
13196
   if ((peek() == tID) && (peek_nth(2) == tCOLON)) {
10,334✔
13197
      label = p_identifier();
5,640✔
13198
      consume(tCOLON);
5,640✔
13199
   }
13200

13201
   switch (peek()) {
10,334✔
13202
   case tPROCESS:
4,762✔
13203
      return p_process_statement(label);
4,762✔
13204

13205
   case tCOMPONENT:
1,264✔
13206
   case tENTITY:
13207
   case tCONFIGURATION:
13208
      return p_component_instantiation_statement(label, NULL);
1,264✔
13209

13210
   case tWITH:
47✔
13211
      return p_concurrent_signal_assignment_statement(label, NULL);
47✔
13212

13213
   case tASSERT:
1,066✔
13214
      if (standard() >= STD_08)
1,066✔
13215
         return p_psl_or_concurrent_assert(label);
155✔
13216
      else
13217
         return p_concurrent_assertion_statement(label);
911✔
13218

13219
   case tCOVER:
2✔
13220
      return p_psl_directive(label);
2✔
13221

13222
   case tBLOCK:
377✔
13223
      return p_block_statement(label);
377✔
13224

13225
   case tIF:
341✔
13226
   case tFOR:
13227
   case tCASE:
13228
      return p_generate_statement(label);
341✔
13229

13230
   case tLPAREN:
18✔
13231
   case tLTLT:
13232
      return p_concurrent_signal_assignment_statement(label, NULL);
18✔
13233

13234
   case tID:
2,409✔
13235
      {
13236
         const token_t p2 = peek_nth(2);
2,409✔
13237
         if ((label != NULL && p2 == tSEMI) || p2 == tGENERIC || p2 == tPORT)
2,409✔
13238
            return p_component_instantiation_statement(label, NULL);
219✔
13239
         else {
13240
            tree_t name = p_name(N_SUBPROGRAM), conc;
2,190✔
13241
            if (peek() == tLE)
2,190✔
13242
               return p_concurrent_signal_assignment_statement(label, name);
2,127✔
13243
            else if (scan(tGENERIC, tPORT))
63✔
13244
               return p_component_instantiation_statement(label, name);
3✔
13245
            else {
13246
               switch (tree_kind(name)) {
60✔
13247
               case T_REF:
55✔
13248
                  if (tree_has_ref(name)) {
55✔
13249
                     tree_t decl = tree_ref(name);
49✔
13250
                     if (tree_kind(decl) == T_COMPONENT)
49✔
UNCOV
13251
                        return p_component_instantiation_statement(label, name);
×
13252
                  }
13253
                  // Fall-through
13254
               case T_PROT_REF:
13255
                  return p_concurrent_procedure_call_statement(label, name);
58✔
13256
               default:
2✔
13257
                  parse_error(CURRENT_LOC, "expected concurrent statement");
2✔
13258
                  drop_tokens_until(tSEMI);
2✔
13259
                  return ensure_labelled(tree_new(T_CONCURRENT), label);
2✔
13260
               }
13261
            }
13262

13263
            return conc;
13264
         }
13265
      }
13266

13267
   case tPOSTPONED:
40✔
13268
      {
13269
         const token_t tok2 = peek_nth(2);
40✔
13270
         if (tok2 == tASSERT)
40✔
13271
            return p_concurrent_assertion_statement(label);
22✔
13272
         else if (tok2 == tID || tok2 == tLPAREN) {
18✔
13273
            consume(tPOSTPONED);
2✔
13274

13275
            tree_t name = p_name(N_SUBPROGRAM), conc;
2✔
13276
            if (peek() == tLE)
2✔
13277
               conc = p_concurrent_signal_assignment_statement(label, name);
2✔
13278
            else
UNCOV
13279
               conc = p_concurrent_procedure_call_statement(label, name);
×
13280

13281
            tree_set_flag(conc, TREE_F_POSTPONED);
2✔
13282
            return conc;
2✔
13283
         }
13284
         else
13285
            return p_process_statement(label);
16✔
13286
      }
13287
   default:
8✔
13288
      expect(tPROCESS, tPOSTPONED, tCOMPONENT, tENTITY, tCONFIGURATION,
15✔
13289
             tWITH, tASSERT, tBLOCK, tIF, tFOR, tCASE, tLPAREN, tID,
13290
             STD(08, tLTLT));
13291
      drop_tokens_until(tSEMI);
8✔
13292
      return ensure_labelled(tree_new(T_BLOCK), label);
8✔
13293
   }
13294
}
13295

13296
static void p_architecture_declarative_part(tree_t arch)
4,968✔
13297
{
13298
   // { block_declarative_item }
13299

13300
   BEGIN("architecture declarative part");
9,936✔
13301

13302
   while (not_at_token(tBEGIN))
16,691✔
13303
      p_block_declarative_item(arch);
11,723✔
13304
}
4,968✔
13305

13306
static void p_architecture_statement_part(tree_t arch)
4,968✔
13307
{
13308
   // { concurrent_statement }
13309

13310
   BEGIN("architecture statement part");
9,933✔
13311

13312
   while (not_at_token(tEND))
14,834✔
13313
      p_concurrent_statement_or_psl(arch);
9,869✔
13314
}
4,965✔
13315

13316
static void p_architecture_body(tree_t unit)
4,968✔
13317
{
13318
   // architecture identifier of entity_name is architecture_declarative_part
13319
   //   begin architecture_statement_part end [ architecture ]
13320
   //   [ architecture_simple_name ] ;
13321

13322
   BEGIN("architecture body");
9,933✔
13323

13324
   tree_change_kind(unit, T_ARCH);
4,968✔
13325

13326
   consume(tARCHITECTURE);
4,968✔
13327

13328
   ident_t arch_name = p_identifier();
4,968✔
13329
   tree_set_ident(unit, arch_name);
4,968✔
13330

13331
   consume(tOF);
4,968✔
13332

13333
   tree_t e = NULL;
4,968✔
13334
   ident_t entity_name = p_entity_name(&e);
4,968✔
13335
   tree_set_ident2(unit, entity_name);
4,968✔
13336

13337
   consume(tIS);
4,968✔
13338

13339
   if (e != NULL) {
4,968✔
13340
      tree_set_primary(unit, e);
4,965✔
13341
      insert_names_from_context(nametab, e);
4,965✔
13342
   }
13343

13344
   push_scope(nametab);
4,968✔
13345

13346
   if (entity_name != arch_name && e != NULL)
4,968✔
13347
      insert_name(nametab, e, entity_name);
4,918✔
13348

13349
   ident_t ename = ident_prefix(lib_name(lib_work()), entity_name, '.');
4,968✔
13350
   ident_t qual = ident_prefix(ename, arch_name, '-');
4,968✔
13351
   scope_set_prefix(nametab, qual);
4,968✔
13352

13353
   tree_set_loc(unit, CURRENT_LOC);
4,968✔
13354
   insert_name(nametab, unit, NULL);
4,968✔
13355

13356
   push_scope(nametab);
4,968✔
13357
   scope_set_container(nametab, unit);
4,968✔
13358

13359
   if (e != NULL) {
4,968✔
13360
      insert_generics(nametab, e);
4,965✔
13361
      insert_ports(nametab, e);
4,965✔
13362
      insert_decls(nametab, e);
4,965✔
13363
   }
13364

13365
   continue_proc_labelling_from(e, nametab);
4,968✔
13366

13367
   p_architecture_declarative_part(unit);
4,968✔
13368

13369
   consume(tBEGIN);
4,968✔
13370

13371
   p_architecture_statement_part(unit);
4,968✔
13372

13373
   consume(tEND);
4,965✔
13374
   optional(tARCHITECTURE);
4,965✔
13375
   p_trailing_label(arch_name);
4,965✔
13376
   consume(tSEMI);
4,965✔
13377

13378
   tree_set_ident(unit, qual);
4,965✔
13379
   tree_set_loc(unit, CURRENT_LOC);
4,965✔
13380

13381
   sem_check(unit, nametab);
4,965✔
13382

13383
   pop_scope(nametab);
4,965✔
13384
   pop_scope(nametab);
4,965✔
13385
}
4,965✔
13386

13387
static void p_package_body_declarative_item(tree_t parent)
8,340✔
13388
{
13389
   // subprogram_declaration | subprogram_body | type_declaration
13390
   //   | subtype_declaration | constant_declaration
13391
   //   | shared_variable_declaration | file_declaration | alias_declaration
13392
   //   | use_clause | group_template_declaration | group_declaration
13393
   //
13394
   // 2008: subprogram_instantiation_declaration | attribute_declaration
13395
   //         | attribute_specification | package_instantiation_declaration
13396
   //         | package_declaration
13397

13398
   BEGIN("package body declarative item");
16,680✔
13399

13400
   switch (peek()) {
8,340✔
13401
   case tFUNCTION:
7,015✔
13402
   case tPROCEDURE:
13403
   case tIMPURE:
13404
   case tPURE:
13405
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
7,015✔
13406
         tree_add_decl(parent, p_subprogram_instantiation_declaration());
1✔
13407
      else {
13408
         tree_t spec = p_subprogram_specification();
7,014✔
13409
         if (peek() == tSEMI)
7,014✔
13410
            tree_add_decl(parent, p_subprogram_declaration(spec));
32✔
13411
         else
13412
            tree_add_decl(parent, p_subprogram_body(spec));
6,982✔
13413
      }
13414
      break;
13415

13416
   case tSHARED:
25✔
13417
      p_variable_declaration(parent);
25✔
13418
      break;
25✔
13419

13420
   case tFILE:
7✔
13421
      p_file_declaration(parent);
7✔
13422
      break;
7✔
13423

13424
   case tATTRIBUTE:
35✔
13425
      if (peek_nth(3) == tOF)
35✔
13426
         p_attribute_specification(parent);
35✔
13427
      else
UNCOV
13428
         tree_add_decl(parent, p_attribute_declaration());
×
13429

13430
      if (standard() < STD_08)
35✔
13431
         parse_error(tree_loc(tree_decl(parent, tree_decls(parent) - 1)),
1✔
13432
                     "package body may not contain attribute declarations or"
13433
                     " specifications in VHDL-%s", standard_text(standard()));
13434
      break;
13435

13436
   case tTYPE:
358✔
13437
      p_type_declaration(parent);
358✔
13438
      break;
358✔
13439

13440
   case tCONSTANT:
792✔
13441
      p_constant_declaration(parent);
792✔
13442
      break;
792✔
13443

13444
   case tSUBTYPE:
48✔
13445
      tree_add_decl(parent, p_subtype_declaration());
48✔
13446
      break;
48✔
13447

13448
   case tALIAS:
5✔
13449
      p_alias_declaration(parent);
5✔
13450
      break;
5✔
13451

13452
   case tUSE:
34✔
13453
      p_use_clause(parent, tree_add_decl);
34✔
13454
      break;
34✔
13455

UNCOV
13456
   case tGROUP:
×
UNCOV
13457
      if (peek_nth(3) == tIS)
×
UNCOV
13458
         tree_add_decl(parent, p_group_template_declaration());
×
13459
      else
13460
         tree_add_decl(parent, p_group_declaration());
×
13461
      break;
13462

13463
   case tPACKAGE:
18✔
13464
      if (peek_nth(4) == tNEW)
18✔
13465
         tree_add_decl(parent, p_package_instantiation_declaration(NULL));
16✔
13466
      else if (peek_nth(2) == tBODY) {
2✔
13467
         require_std(STD_08, "nested package declarations");
1✔
13468
         tree_add_decl(parent, p_package_body(NULL));
1✔
13469
      }
13470
      else {
13471
         require_std(STD_08, "nested package declarations");
1✔
13472
         tree_add_decl(parent, p_package_declaration(NULL));
1✔
13473
      }
13474
      break;
13475

13476
   default:
3✔
13477
      expect(tFUNCTION, tPROCEDURE, tSHARED, tIMPURE, tPURE, tATTRIBUTE, tTYPE,
6✔
13478
             tCONSTANT, tSUBTYPE, tFILE, tALIAS, tUSE, tGROUP,
13479
             STD(08, tPACKAGE));
13480
   }
13481
}
8,340✔
13482

13483
static void p_package_body_declarative_part(tree_t unit)
756✔
13484
{
13485
   // { package_body_declarative_item }
13486

13487
   BEGIN("package body declarative part");
1,512✔
13488

13489
   while (not_at_token(tEND))
9,096✔
13490
      p_package_body_declarative_item(unit);
8,340✔
13491
}
756✔
13492

13493
static tree_t p_package_body(tree_t unit)
756✔
13494
{
13495
   // package body simple_name is package_body_declarative_part
13496
   //   end [ package body ] [ simple_name ] ;
13497

13498
   BEGIN("package body");
756✔
13499

13500
   consume(tPACKAGE);
756✔
13501
   consume(tBODY);
756✔
13502

13503
   ident_t name = p_identifier(), qual = name;
756✔
13504

13505
   tree_t body;
756✔
13506
   if (unit != NULL) {
756✔
13507
      // Package body as primary unit
13508
      assert(tree_kind(unit) == T_DESIGN_UNIT);
755✔
13509
      tree_change_kind(unit, T_PACK_BODY);
755✔
13510
      body = unit;
755✔
13511

13512
      qual = ident_prefix(lib_name(lib_work()), name, '.');
755✔
13513
   }
13514
   else
13515
      body = tree_new(T_PACK_BODY);
1✔
13516

13517
   tree_t pack = resolve_name(nametab, CURRENT_LOC, qual);
756✔
13518
   if (pack != NULL && tree_kind(pack) != T_PACKAGE) {
756✔
UNCOV
13519
      parse_error(CURRENT_LOC, "unit %s is not a package", istr(qual));
×
13520
      pack = NULL;
13521
   }
13522
   else if (pack != NULL) {
756✔
13523
      tree_set_primary(body, pack);
751✔
13524
      insert_names_from_context(nametab, pack);
751✔
13525
   }
13526

13527
   push_scope(nametab);
756✔
13528

13529
   tree_set_ident(body, ident_prefix(qual, ident_new("body"), '-'));
756✔
13530
   tree_set_loc(body, CURRENT_LOC);
756✔
13531

13532
   scope_set_prefix(nametab, qual);
756✔
13533
   insert_name(nametab, body, name);
756✔
13534

13535
   consume(tIS);
756✔
13536

13537
   push_scope(nametab);
756✔
13538
   scope_set_container(nametab, body);
756✔
13539

13540
   if (pack != NULL) {
756✔
13541
      insert_generics(nametab, pack);
751✔
13542
      insert_decls(nametab, pack);
751✔
13543
   }
13544

13545
   p_package_body_declarative_part(body);
756✔
13546

13547
   if (pack != NULL && (tree_global_flags(pack) & TREE_GF_DEFERRED_INST))
756✔
13548
      package_body_deferred_instantiation(pack, body);
4✔
13549

13550
   consume(tEND);
756✔
13551

13552
   if (optional(tPACKAGE))
756✔
13553
      consume(tBODY);
664✔
13554

13555
   p_trailing_label(name);
756✔
13556
   consume(tSEMI);
756✔
13557

13558
   tree_set_loc(body, CURRENT_LOC);
756✔
13559
   sem_check(body, nametab);
756✔
13560

13561
   pop_scope(nametab);
756✔
13562
   pop_scope(nametab);
756✔
13563

13564
   return body;
756✔
13565
}
13566

13567
static void p_secondary_unit(tree_t unit)
5,723✔
13568
{
13569
   // architecture_body | package_body
13570

13571
   BEGIN("secondary unit");
11,443✔
13572

13573
   switch (peek()) {
5,723✔
13574
   case tARCHITECTURE:
4,968✔
13575
      p_architecture_body(unit);
4,968✔
13576
      break;
4,968✔
13577

13578
   case tPACKAGE:
755✔
13579
      p_package_body(unit);
755✔
13580
      break;
755✔
13581

UNCOV
13582
   default:
×
UNCOV
13583
      expect(tARCHITECTURE, tPACKAGE);
×
13584
   }
13585
}
5,720✔
13586

13587
static void p_library_unit(tree_t unit)
12,306✔
13588
{
13589
   // primary_unit | secondary_unit
13590

13591
   BEGIN("library unit");
12,306✔
13592

13593
   switch (peek()) {
12,306✔
13594
   case tENTITY:
5,086✔
13595
   case tCONFIGURATION:
13596
   case tCONTEXT:
13597
      p_primary_unit(unit);
5,086✔
13598
      break;
5,086✔
13599

13600
   case tARCHITECTURE:
4,968✔
13601
      p_secondary_unit(unit);
4,968✔
13602
      break;
4,968✔
13603

13604
   case tPACKAGE:
2,249✔
13605
      if (peek_nth(2) == tBODY)
2,249✔
13606
         p_secondary_unit(unit);
755✔
13607
      else
13608
         p_primary_unit(unit);
1,494✔
13609
      break;
13610

13611
   default:
3✔
13612
      expect(tENTITY, tCONFIGURATION, tARCHITECTURE, tPACKAGE,
6✔
13613
             STD(08, tCONTEXT));
13614
   }
13615

13616
   if (bootstrapping && unit != find_std(nametab))
12,303✔
13617
      parse_error(tree_loc(unit), "--bootstrap must only be used with "
12,303✔
13618
                  "STANDARD package");
13619
}
12,303✔
13620

13621
static tree_t p_design_unit(void)
12,306✔
13622
{
13623
   BEGIN("design unit");
12,306✔
13624

13625
   push_scope(nametab);
12,306✔
13626

13627
   tree_t unit = tree_new(T_DESIGN_UNIT);
12,306✔
13628
   scope_set_container(nametab, unit);
12,306✔
13629

13630
   ident_t std_i = well_known(W_STD);
12,306✔
13631

13632
   tree_t std = tree_new(T_LIBRARY);
12,306✔
13633
   tree_set_ident(std, std_i);
12,306✔
13634
   tree_set_ident2(std, std_i);
12,306✔
13635
   tree_add_context(unit, std);
12,306✔
13636
   insert_name(nametab, std, std_i);
12,306✔
13637

13638
   ident_t work_name = lib_name(lib_work());
12,306✔
13639
   tree_t work = tree_new(T_LIBRARY);
12,306✔
13640
   tree_set_ident(work, work_name);
12,306✔
13641
   tree_set_ident2(work, work_name);
12,306✔
13642
   tree_add_context(unit, work);
12,306✔
13643
   insert_name(nametab, work, well_known(W_WORK));
12,306✔
13644
   insert_name(nametab, work, NULL);
12,306✔
13645

13646
   // The std.standard package is implicit unless we are bootstrapping
13647
   if (!bootstrapping) {
12,306✔
13648
      lib_t lstd = lib_require(std_i);
12,303✔
13649
      ident_t standard_i = well_known(W_STD_STANDARD);
12,303✔
13650
      tree_t std_pkg = lib_get(lstd, standard_i);
12,303✔
13651
      if (std_pkg == NULL)
12,303✔
UNCOV
13652
         fatal("cannot find %s package", istr(standard_i));
×
13653

13654
      tree_t u = tree_new(T_USE);
12,303✔
13655
      tree_set_ident(u, standard_i);
12,303✔
13656
      tree_set_ident2(u, well_known(W_ALL));
12,303✔
13657
      tree_set_ref(u, std_pkg);
12,303✔
13658

13659
      tree_add_context(unit, u);
12,303✔
13660
      insert_names_from_use(nametab, u);
12,303✔
13661
   }
13662

13663
   p_context_clause(unit);
12,306✔
13664
   p_library_unit(unit);
12,306✔
13665

13666
   pop_scope(nametab);
12,303✔
13667

13668
   return unit;
12,303✔
13669
}
13670

13671
static void flush_pragmas(tree_t unit)
12,303✔
13672
{
13673
   // Make sure pragmas always appear in off/on pairs
13674
   tree_t coverage_off = NULL, synthesis_off = NULL, translate_off = NULL;
12,303✔
13675

13676
   for (int i = 0; i < pragmas.count; i++) {
12,350✔
13677
      tree_t p = pragmas.items[i];
47✔
13678
      tree_add_pragma(unit, p);
47✔
13679

13680
      switch (tree_subkind(p)) {
47✔
13681
      case PRAGMA_SYNTHESIS_ON: synthesis_off = NULL; break;
3✔
13682
      case PRAGMA_SYNTHESIS_OFF: synthesis_off = p; break;
4✔
13683
      case PRAGMA_COVERAGE_ON: coverage_off = NULL; break;
19✔
13684
      case PRAGMA_COVERAGE_OFF: coverage_off = p; break;
20✔
UNCOV
13685
      case PRAGMA_TRANSLATE_ON: translate_off = NULL; break;
×
13686
      case PRAGMA_TRANSLATE_OFF: translate_off = p; break;
1✔
13687
      }
13688
   }
13689

13690
   if (coverage_off != NULL)
12,303✔
13691
      warn_at(tree_loc(coverage_off), "no matching $bold$coverage on$$ "
1✔
13692
              "directive seen before end of design unit");
13693

13694
   if (synthesis_off != NULL)
12,303✔
13695
      warn_at(tree_loc(synthesis_off), "no matching $bold$synthesis "
1✔
13696
              "translate_on$$ directive seen before end of design unit");
13697

13698
   if (translate_off != NULL)
12,303✔
13699
      warn_at(tree_loc(translate_off), "no matching $bold$pragma "
1✔
13700
              "translate_on$$ directive seen before end of design unit");
13701

13702
   ACLEAR(pragmas);
12,303✔
13703
}
12,303✔
13704

13705
tree_t parse(void)
16,582✔
13706
{
13707
   n_correct = RECOVER_THRESH;
16,582✔
13708

13709
   scan_as_vhdl();
16,582✔
13710

13711
   if (peek() == tEOF)
16,582✔
13712
      return NULL;
13713

13714
   make_new_arena();
12,306✔
13715
   nametab = nametab_new();
12,306✔
13716

13717
   tree_t unit = p_design_unit();
12,306✔
13718

13719
   nametab_finish(nametab);
12,303✔
13720
   nametab = NULL;
12,303✔
13721

13722
   flush_pragmas(unit);
12,303✔
13723

13724
   if (tree_kind(unit) == T_DESIGN_UNIT)
12,303✔
13725
      return NULL;
3✔
13726

13727
   return unit;
13728
}
13729

13730
void reset_vhdl_parser(void)
4,275✔
13731
{
13732
   bootstrapping = opt_get_int(OPT_BOOTSTRAP);
4,275✔
13733

13734
   if (tokenq == NULL) {
4,275✔
13735
      tokenq_sz = 128;
4,260✔
13736
      tokenq = xmalloc_array(tokenq_sz, sizeof(tokenq_t));
4,260✔
13737
   }
13738

13739
   tokenq_head = tokenq_tail = 0;
4,275✔
13740
}
4,275✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc