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

nickg / nvc / 19989699650

06 Dec 2025 02:01PM UTC coverage: 92.607% (+0.04%) from 92.565%
19989699650

push

github

nickg
Refactor representation of case alternative choices

224 of 233 new or added lines in 8 files covered. (96.14%)

3 existing lines in 2 files now uncovered.

75371 of 81388 relevant lines covered (92.61%)

413814.18 hits per line

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

96.72
/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
static psl_node_t p_psl_union(tree_t head);
185

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

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

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

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

216
      APUSH(pragmas, p);
59✔
217
   }
218
}
65✔
219

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

249
static token_t peek_nth(int n)
8,933,175✔
250
{
251
   while (((tokenq_head - tokenq_tail) & (tokenq_sz - 1)) < n) {
10,661,174✔
252
      const token_t token = wrapped_yylex();
1,727,999✔
253

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

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

264
         free(tokenq);
×
265

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

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

274
      extern yylval_t yylval;
1,727,999✔
275

276
      tokenq[tokenq_head].token = token;
1,727,999✔
277
      tokenq[tokenq_head].lval  = yylval;
1,727,999✔
278
      tokenq[tokenq_head].loc   = yylloc;
1,727,999✔
279

280
      tokenq_head = next;
1,727,999✔
281
   }
282

283
   const int pos = (tokenq_tail + n - 1) & (tokenq_sz - 1);
8,933,175✔
284
   return tokenq[pos].token;
8,933,175✔
285
}
286

287
static bool look_for(const look_params_t *params)
51,002✔
288
{
289
   bool found = false;
51,002✔
290
   token_t tok = -1;
51,002✔
291
   int n, nest = 0;
51,002✔
292
   for (n = 1; ;) {
51,002✔
293
      tok = peek_nth(n++);
143,752✔
294
      if ((tok == tEOF) || (tok == params->abort))
143,752✔
295
         goto stop_looking;
2✔
296
      else if (tok == params->nest_in)
143,750✔
297
         nest++;
6,521✔
298

299
      if (nest == params->depth) {
143,750✔
300
         for (int i = 0; i < ARRAY_LEN(params->look); i++) {
555,854✔
301
            if (tok == params->look[i]) {
446,086✔
302
               found = true;
7,014✔
303
               goto stop_looking;
7,014✔
304
            }
305
         }
306

307
         if (params->lookfn != NULL && (*params->lookfn)(tok)) {
109,768✔
308
            found = true;
×
309
            goto stop_looking;
×
310
         }
311

312
         for (int i = 0; i < ARRAY_LEN(params->stop); i++) {
394,516✔
313
            if (tok == params->stop[i])
328,734✔
314
               goto stop_looking;
43,986✔
315
         }
316
      }
317

318
      if (tok == params->nest_out)
92,750✔
319
         nest--;
6,521✔
320
   }
321
 stop_looking:
51,002✔
322

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

328
   return found;
51,002✔
329
}
330

331
static void drop_token(void)
1,723,510✔
332
{
333
   assert(tokenq_head != tokenq_tail);
1,723,510✔
334

335
   if (start_loc.first_line == LINE_INVALID)
1,723,510✔
336
      start_loc = tokenq[tokenq_tail].loc;
717,461✔
337

338
   last_lval = tokenq[tokenq_tail].lval;
1,723,510✔
339
   last_loc  = tokenq[tokenq_tail].loc;
1,723,510✔
340

341
   tokenq_tail = (tokenq_tail + 1) & (tokenq_sz - 1);
1,723,510✔
342

343
   nopt_hist = 0;
1,723,510✔
344
}
1,723,510✔
345

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

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

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

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

375
      int tok = va_arg(ap, int);
44✔
376
      while (tok != -1) {
157✔
377
         const int tmp = tok;
113✔
378
         tok = va_arg(ap, int);
113✔
379

380
         if (first && (tok != -1))
113✔
381
            diag_printf(d, "one of ");
12✔
382
         else if (!first)
101✔
383
            diag_printf(d, (tok == -1) ? " or " : ", ");
142✔
384

385
         diag_printf(d, "$yellow$%s$$", token_str(tmp));
113✔
386

387
         first = false;
113✔
388
      }
389

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

394
   n_correct = 0;
228✔
395

396
   drop_token();
228✔
397
   suppress_errors(nametab);
228✔
398
}
228✔
399

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

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

422
static bool optional(token_t tok)
1,366,785✔
423
{
424
   if (peek() == tok) {
1,366,785✔
425
      consume(tok);
196,074✔
426
      return true;
196,074✔
427
   }
428
   else {
429
      if (nopt_hist < ARRAY_LEN(opt_hist))
1,170,711✔
430
         opt_hist[nopt_hist++] = tok;
1,170,711✔
431
      return false;
1,170,711✔
432
   }
433
}
434

435
static bool _scan(int dummy, ...)
2,003,115✔
436
{
437
   va_list ap;
2,003,115✔
438
   va_start(ap, dummy);
2,003,115✔
439

440
   token_t p = peek();
2,003,115✔
441
   bool found = false;
2,003,115✔
442

443
   while (!found) {
2,003,115✔
444
      const int tok = va_arg(ap, token_t);
10,397,336✔
445
      if (tok == -1)
10,397,336✔
446
         break;
447
      else if (p == tok)
8,569,062✔
448
         found = true;
449
   }
450

451
   va_end(ap);
2,003,115✔
452
   return found;
2,003,115✔
453
}
454

455
static int _one_of(int dummy, ...)
198,957✔
456
{
457
   va_list ap;
198,957✔
458
   va_start(ap, dummy);
198,957✔
459

460
   token_t p = peek();
198,957✔
461
   bool found = false;
198,957✔
462

463
   while (!found) {
198,957✔
464
      const int tok = va_arg(ap, token_t);
286,344✔
465
      if (tok == -1)
286,344✔
466
         break;
467
      else if (p == tok)
286,341✔
468
         found = true;
469
   }
470

471
   va_end(ap);
198,957✔
472

473
   if (found) {
198,957✔
474
      consume(p);
198,954✔
475
      return p;
198,954✔
476
   }
477
   else {
478
      va_start(ap, dummy);
3✔
479
      _vexpect(ap);
3✔
480
      va_end(ap);
3✔
481

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

486
static const loc_t *_diff_loc(const loc_t *start, const loc_t *end)
1,348,547✔
487
{
488
   static loc_t result;
1,348,547✔
489

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

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

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

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

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

529
   if (unit == NULL)
1,776✔
530
      return NULL;
531

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

560
   return unit;
561
}
562

563
static void set_label_and_loc(tree_t t, ident_t label, const loc_t *loc)
67,887✔
564
{
565
   tree_set_loc(t, loc);
67,887✔
566

567
   if (label == NULL)
67,887✔
568
      label = get_implicit_label(t, nametab);
67,512✔
569

570
   tree_set_ident(t, label);
67,887✔
571
}
67,887✔
572

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

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

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

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

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

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

611
   enum { UNSIGNED, SIGNED } mode = UNSIGNED;
2,578✔
612

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

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

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

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

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

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

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

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

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

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

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

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

684
      return t;
685
   }
686

687
   tree_t *bits LOCAL = NULL;
5,050✔
688
   if (length >= 0)
2,525✔
689
      bits = xmalloc_array(length, sizeof(tree_t));
42✔
690

691
   tree_t pad = mode == UNSIGNED ? zero : NULL;
2,525✔
692
   int nbits = 0;
2,525✔
693
   for (++p; *p != '\"'; p++) {
10,227✔
694
      const bool extended = (isdigit_iso88591(*p) && *p < '0' + base)
13,381✔
695
         || (base > 10 && *p >= 'A' && *p < 'A' + base - 10)
2,032✔
696
         || (base > 10 && *p >= 'a' && *p < 'a' + base - 10);
9,052✔
697

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

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

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

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

751
   return t;
752
}
753

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

762
   return lit;
66✔
763
}
764

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

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

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

790
   tree_t port = tree_new(T_PARAM_DECL);
67,027✔
791
   tree_set_ident(port, ident_new(name));
67,027✔
792
   tree_set_loc(port, tree_loc(d));
67,027✔
793
   tree_set_type(port, type);
67,027✔
794
   tree_set_subkind(port, mode);
67,027✔
795
   if (def != NULL)
67,027✔
796
      tree_set_value(port, def);
220✔
797
   if (type_is_file(type))
67,027✔
798
      tree_set_class(port, C_FILE);
711✔
799

800
   tree_add_port(d, port);
67,027✔
801
   type_add_param(ftype, type);
67,027✔
802

803
   return port;
67,027✔
804
}
805

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

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

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

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

829
   tree_t d = tree_new(T_FUNC_DECL);
33,901✔
830
   tree_set_ident(d, name);
33,901✔
831
   tree_set_type(d, f);
33,901✔
832
   tree_set_subkind(d, kind);
33,901✔
833

834
   va_list ap;
33,901✔
835
   va_start(ap, kind);
33,901✔
836
   char *argname;
33,901✔
837
   while ((argname = va_arg(ap, char*))) {
99,288✔
838
      type_t type = va_arg(ap, type_t);
65,387✔
839
      assert(type != NULL);
65,387✔
840
      add_port(d, argname, type, PORT_IN, NULL);
65,387✔
841
   }
842
   va_end(ap);
33,901✔
843

844
   tree_set_flag(d, TREE_F_PREDEFINED);
33,901✔
845
   tree_set_loc(d, CURRENT_LOC);
33,901✔
846
   return d;
33,901✔
847
}
848

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

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

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

872
   ident_t name = type_ident(type);
386✔
873

874
   return name == well_known(W_STD_BIT) || name == well_known(W_IEEE_ULOGIC);
418✔
875
}
876

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

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

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

898
   // Predefined operators
899

900
   tree_t std = find_std(nametab);
4,945✔
901

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

907
   type_kind_t kind = type_kind(t);
4,945✔
908

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

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

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

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

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

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

942
         if (standard() >= STD_08) {
2,304✔
943
            if (ordered) {
787✔
944
               declare_binary(container, min_i, t, t, t, S_MINIMUM);
265✔
945
               declare_binary(container, max_i, t, t, t, S_MAXIMUM);
265✔
946
            }
947

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

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

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

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

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

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

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

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

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

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

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

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

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

1010
      break;
1011

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

1016
      // Remainder
1017
      declare_binary(container, well_known(W_OP_REM), t, t, t, S_REM);
149✔
1018

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

1024
      // Subtraction
1025
      declare_binary(container, minus, t, t, t, S_SUB);
175✔
1026

1027
      // Multiplication
1028
      declare_binary(container, mult, t, t, t, S_MUL);
175✔
1029

1030
      // Division
1031
      declare_binary(container, div, t, t, t, S_DIV);
175✔
1032

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

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

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

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

1054
      if (standard() >= STD_08) {
631✔
1055
         declare_binary(container, min_i, t, t, t, S_MINIMUM);
245✔
1056
         declare_binary(container, max_i, t, t, t, S_MAXIMUM);
245✔
1057
      }
1058

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

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

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

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

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

1086
   // Matching comparison for BIT and STD_ULOGIC
1087

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

1108
   // Logical operators
1109

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

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

1126
   if (vec_logical) {
2,304✔
1127
      std_int = std_type(NULL, STD_INTEGER);
198✔
1128

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

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

1143
      declare_unary(container, well_known(W_OP_NOT), t, t, S_ARRAY_NOT);
198✔
1144

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

1152
      if (standard() >= STD_08) {
198✔
1153
         type_t e = type_elem(t);
45✔
1154

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

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

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

1178
   // Predefined procedures
1179

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1323
         type_t of = type_designated(t);
101✔
1324

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

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

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

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

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

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

1358
   default:
1359
      break;
1360
   }
1361

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1437
   // The following special cases are implicitly defined
1438

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

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

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

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

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

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

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

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

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

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

1486
static void binary_op(tree_t expr, tree_t left, tree_t (*right_fn)(tree_t))
54,026✔
1487
{
1488
   add_param(expr, left, P_POS, NULL);
54,026✔
1489

1490
   tree_t right = (*right_fn)(NULL);
54,026✔
1491
   tree_set_loc(expr, CURRENT_LOC);
54,026✔
1492
   add_param(expr, right, P_POS, NULL);
54,026✔
1493
}
54,026✔
1494

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

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

1504
   return all;
1,297✔
1505
}
1506

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

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

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

1531
   return solve_types(nametab, prefix, signature);
39,028✔
1532
}
1533

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

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

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

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

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

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

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

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

1578
   return ref;
748✔
1579
}
1580

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

1587
   if (tree_params(fcall) != 1)
14,521✔
1588
      return fcall;
1589

1590
   tree_t p0 = tree_param(fcall, 0);
6,115✔
1591
   if (tree_subkind(p0) != P_POS)
6,115✔
1592
      return fcall;
1593

1594
   tree_t value = tree_value(p0);
6,090✔
1595
   if (tree_kind(value) != T_REF)
6,090✔
1596
      return fcall;
1597

1598
   if (!tree_has_ref(value))
4,140✔
1599
      return fcall;
1600

1601
   tree_t decl = tree_ref(value);
4,095✔
1602
   if (!is_type_decl(decl))
4,095✔
1603
      return fcall;
1604

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

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

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

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

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

1627
   return slice;
4✔
1628
}
1629

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

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

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

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

1652
   type_add_dim(type, r);
6✔
1653

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

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

1662
   tree_add_decl(container, decl);
6✔
1663

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

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

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

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

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

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

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

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

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

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

1711
   if (!(tree_flags(value) & TREE_F_CONVERSION))
230✔
1712
      return value;
1713

1714
   if (!tree_has_ref(value))
209✔
1715
      return value;
1716

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

1721
   tree_t p0 = tree_value(tree_param(value, 0));
207✔
1722

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

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

1734
   return conv;
201✔
1735
}
1736

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

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

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

1751
   tree_copy_generics(new, src);
129✔
1752
   tree_copy_ports(new, src);
129✔
1753

1754
   if (body != NULL) {
129✔
1755
      tree_copy_decls(new, body_copy);
120✔
1756
      tree_copy_stmts(new, body_copy);
120✔
1757

1758
      tree_set_flag(new, tree_flags(body));
120✔
1759
      tree_set_global_flags(new, tree_global_flags(body));
120✔
1760
   }
1761

1762
   // Allow recursive calls to the uninstantiated subprogram
1763
   map_generic_subprogram(nametab, decl_copy, new);
129✔
1764
   if (body != NULL) map_generic_subprogram(nametab, body_copy, new);
129✔
1765
}
129✔
1766

1767
static void instantiate_package(tree_t new, tree_t pack, tree_t body)
281✔
1768
{
1769
   assert(body == NULL || tree_primary(body) == pack);
281✔
1770

1771
   ident_t prefix = tree_ident(pack);
281✔
1772
   tree_t container = tree_container(pack);
281✔
1773
   if (container != pack)
281✔
1774
      prefix = ident_prefix(tree_ident(container), prefix, '.');
17✔
1775

1776
   tree_t roots[] = { pack, body };
281✔
1777
   ident_t prefixes[] = { prefix };
281✔
1778
   ident_t dotted = ident_prefix(scope_prefix(nametab), tree_ident(new), '.');
281✔
1779
   new_instance(roots, body != NULL ? 2 : 1, dotted, prefixes, 1);
365✔
1780

1781
   tree_t pack_copy = roots[0], body_copy = roots[1];
281✔
1782

1783
   tree_copy_generics(new, pack_copy);
281✔
1784

1785
   const int ndecls = tree_decls(pack_copy);
281✔
1786
   for (int i = 0; i < ndecls; i++) {
5,150✔
1787
      tree_t d = tree_decl(pack_copy, i);
4,869✔
1788
      if (tree_kind(d) == T_CONST_DECL && !tree_has_value(d))
4,869✔
1789
         continue;   // Skip deferred constants
9✔
1790

1791
      tree_add_decl(new, d);
4,860✔
1792
   }
1793

1794
   tree_set_global_flags(new, tree_global_flags(pack));
281✔
1795

1796
   if (body != NULL) {
281✔
1797
      // Copy all the declarations from the body into the package to
1798
      // save keeping track of two separate units. The LRM says the
1799
      // implicit instantiated package body is in the body of an
1800
      // enclosing package if this is in a package declaration. Just
1801
      // ignore that, it doesn't matter.
1802
      const int ndecls = tree_decls(body_copy);
197✔
1803
      for (int i = 0; i < ndecls; i++)
4,070✔
1804
         tree_add_decl(new, tree_decl(body_copy, i));
3,873✔
1805

1806
      tree_set_global_flags(new, tree_global_flags(body));
197✔
1807
   }
1808
}
281✔
1809

1810
static void add_interface(tree_t container, tree_t decl, tree_kind_t kind)
38,137✔
1811
{
1812
   if (kind == T_GENERIC_DECL)
38,137✔
1813
      tree_add_generic(container, decl);
3,353✔
1814
   else
1815
      tree_add_port(container, decl);
34,784✔
1816
}
38,137✔
1817

1818
static void ident_list_push(ident_list_t **list, ident_t i, loc_t loc)
66,849✔
1819
{
1820
   ident_list_t *c = xmalloc(sizeof(ident_list_t));
66,849✔
1821
   c->ident = i;
66,849✔
1822
   c->loc   = loc;
66,849✔
1823
   c->next  = NULL;
66,849✔
1824

1825
   ident_list_t **it;
66,849✔
1826
   for (it = list; *it; it = &((*it)->next));
74,381✔
1827
   *it = c;
66,849✔
1828
}
66,849✔
1829

1830
static void _ident_list_cleanup(ident_list_t **list)
61,304✔
1831
{
1832
   for (ident_list_t *it = *list, *tmp; it; it = tmp) {
128,153✔
1833
      tmp = it->next;
66,849✔
1834
      free(it);
66,849✔
1835
   }
1836
   *list = NULL;
61,304✔
1837
}
61,304✔
1838

1839
static type_t get_subtype_for(tree_t expr)
215✔
1840
{
1841
   type_t type = tree_type(expr);
215✔
1842

1843
   type_t sub = type_new(T_SUBTYPE);
215✔
1844
   type_set_base(sub, type_base_recur(type));
215✔
1845

1846
   const loc_t *loc = tree_loc(expr);
215✔
1847

1848
   tree_t c = tree_new(T_CONSTRAINT);
215✔
1849
   tree_set_loc(c, loc);
215✔
1850

1851
   type_set_constraint(sub, c);
215✔
1852

1853
   if (type_is_record(type)) {
215✔
1854
      tree_set_subkind(c, C_RECORD);
23✔
1855

1856
      const int nfields = type_fields(type);
23✔
1857
      for (int i = 0; i < nfields; i++) {
55✔
1858
         tree_t f = type_field(type, i);
32✔
1859
         type_t ft = tree_type(f);
32✔
1860

1861
         if (!type_is_unconstrained(ft))
32✔
1862
            continue;
5✔
1863

1864
         tree_t rref = tree_new(T_RECORD_REF);
27✔
1865
         tree_set_ident(rref, tree_ident(f));
27✔
1866
         tree_set_loc(rref, loc);
27✔
1867
         tree_set_ref(rref, f);
27✔
1868
         tree_set_value(rref, expr);
27✔
1869

1870
         tree_t cons = type_constraint_for_field(type, f);
27✔
1871
         if (cons != NULL)
27✔
1872
            tree_set_type(rref, tree_type(cons));
13✔
1873
         else
1874
            tree_set_type(rref, ft);
14✔
1875

1876
         tree_t ec = tree_new(T_ELEM_CONSTRAINT);
27✔
1877
         tree_set_loc(ec, loc);
27✔
1878
         tree_set_ident(ec, tree_ident(f));
27✔
1879
         tree_set_ref(ec, f);
27✔
1880
         tree_set_type(ec, get_subtype_for(rref));
27✔
1881

1882
         tree_add_range(c, ec);
27✔
1883
      }
1884
   }
1885
   else if (type_is_array(type)) {
192✔
1886
      tree_set_subkind(c, C_INDEX);
176✔
1887

1888
      const int ndims = dimension_of(type);
176✔
1889
      for (int i = 0; i < ndims; i++) {
358✔
1890
         tree_t rref = tree_new(T_ATTR_REF);
182✔
1891
         tree_set_name(rref, expr);
182✔
1892
         tree_set_ident(rref, ident_new("RANGE"));
182✔
1893
         tree_set_loc(rref, loc);
182✔
1894
         tree_set_subkind(rref, ATTR_RANGE);
182✔
1895

1896
         if (i > 0) {
182✔
1897
            tree_t p = tree_new(T_LITERAL);
6✔
1898
            tree_set_subkind(p, L_INT);
6✔
1899
            tree_set_ival(p, i + 1);
6✔
1900
            tree_set_loc(p, loc);
6✔
1901
            tree_set_type(p, std_type(NULL, STD_UNIVERSAL_INTEGER));
6✔
1902

1903
            add_param(rref, p, P_POS, NULL);
6✔
1904
         }
1905

1906
         tree_t r = tree_new(T_RANGE);
182✔
1907
         tree_set_subkind(r, RANGE_EXPR);
182✔
1908
         tree_set_value(r, rref);
182✔
1909
         tree_set_loc(r, loc);
182✔
1910

1911
         solve_types(nametab, r, NULL);
182✔
1912

1913
         tree_add_range(c, r);
182✔
1914
      }
1915

1916
      type_set_elem(sub, get_element_subtype(expr));
176✔
1917
   }
1918
   else if (!is_anonymous_subtype(type))
16✔
1919
      return type;
1920
   else if (type_is_scalar(type)) {
7✔
1921
      // TODO: wrong, see test/regress/integer3.vhd
1922
      tree_set_subkind(c, C_RANGE);
7✔
1923
      tree_add_range(c, range_of(type, 0));
7✔
1924
   }
1925
   else
1926
      should_not_reach_here();
1927

1928
   return sub;
1929
}
1930

1931
static type_t get_element_subtype(tree_t expr)
239✔
1932
{
1933
   type_t type = tree_type(expr);
239✔
1934
   assert(type_is_array(type));
239✔
1935

1936
   type_t elem = type_elem(type);
239✔
1937
   if (type_const_bounds(elem) || class_of(expr) == C_TYPE)
239✔
1938
      return elem;
194✔
1939

1940
   tree_t aref = tree_new(T_ARRAY_REF);
45✔
1941
   tree_set_value(aref, expr);
45✔
1942
   tree_set_type(aref, elem);
45✔
1943
   tree_set_loc(aref, tree_loc(expr));
45✔
1944

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

1954
      add_param(aref, left, P_POS, NULL);
48✔
1955
   }
1956

1957
   return get_subtype_for(aref);
45✔
1958
}
1959

1960
static type_t apply_subtype_attribute(tree_t aref)
129✔
1961
{
1962
   assert(tree_subkind(aref) == ATTR_SUBTYPE);
129✔
1963

1964
   tree_t name = tree_name(aref);
129✔
1965
   type_t type = get_type_or_null(name);
129✔
1966

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

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

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

1985
   if (type == NULL) {
67✔
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))
66✔
1991
      return type;
1992
   else if (!type_is_array(type)) {
64✔
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);
63✔
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)
228✔
2085
{
2086
   switch (tree_subkind(aref)) {
228✔
2087
   case ATTR_SUBTYPE:
129✔
2088
      return apply_subtype_attribute(aref);
129✔
2089
   case ATTR_ELEMENT:
67✔
2090
      return apply_element_attribute(aref);
67✔
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✔
2097
   default:
×
2098
      parse_error(tree_loc(aref), "attribute name is not a valid type mark");
×
2099
      return type_new(T_NONE);
×
2100
   }
2101
}
2102

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

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

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

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

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

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

2147
   implicit_kind_t kind;
306✔
2148
   switch (attr) {
306✔
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;
2153
   default:
×
2154
      fatal_trace("invalid implicit signal attribute");
2155
   }
2156

2157
   const int ndecls = tree_decls(b);
306✔
2158
   for (int i = 0; i < ndecls; i++) {
1,444✔
2159
      tree_t d = tree_decl(b, i);
1,310✔
2160
      if (tree_kind(d) != T_IMPLICIT_SIGNAL)
1,310✔
2161
         continue;
919✔
2162
      else if (tree_ident(d) != id || tree_subkind(d) != kind)
391✔
2163
         continue;
203✔
2164

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

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

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

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

2193
   if (attr == ATTR_TRANSACTION) {
134✔
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);
120✔
2199
      tree_set_loc(w, CURRENT_LOC);
120✔
2200
      tree_set_value(w, prefix);
120✔
2201
      if (delay != NULL)
120✔
2202
         tree_set_delay(w, delay);
80✔
2203

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

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

2212
static attr_kind_t parse_predefined_attr(ident_t ident)
20,785✔
2213
{
2214
   static struct {
20,785✔
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({
68,917✔
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,785✔
2267
   const uint32_t hash = ident_casehash(ident);
20,785✔
2268

2269
   for (int i = 0; i < ARRAY_LEN(predef) && predef[i].std <= std; i++) {
139,606✔
2270
      if (predef[i].hash == hash && ident_casecmp(predef[i].ident, ident))
139,383✔
2271
         return predef[i].attr;
20,562✔
2272
   }
2273

2274
   return ATTR_USER;
2275
}
2276

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

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

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

2289
   tree_t p = tree_new(T_GENERIC_DECL);
984✔
2290
   tree_set_class(p, C_FUNCTION);
984✔
2291
   tree_set_ident(p, id);
984✔
2292
   tree_set_type(p, ftype);
984✔
2293
   tree_set_subkind(p, PORT_IN);
984✔
2294
   tree_set_loc(p, CURRENT_LOC);
984✔
2295
   tree_set_flag(p, TREE_F_PREDEFINED);
984✔
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);
984✔
2309
   tree_set_loc(box, CURRENT_LOC);
984✔
2310

2311
   tree_set_value(p, box);
984✔
2312

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

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

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

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

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

2335
   switch (class) {
262✔
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:
262✔
2365
   case GTYPE_FILE:
2366
   case GTYPE_ACCESS:
2367
   case GTYPE_PRIVATE:
2368
      add_generic_type_op(parent, 2, type, std_bool, "\"=\"");
262✔
2369
      add_generic_type_op(parent, 2, type, std_bool, "\"/=\"");
262✔
2370
      break;
262✔
2371
   }
2372

2373
   if (class == GTYPE_ACCESS) {
262✔
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) {
257✔
2406
      // Declare predefined operators for any anonymous element or index
2407
      type_t elem = type_elem(type);
21✔
2408
      if (type_kind(elem) == T_GENERIC && !type_has_ident(elem))
21✔
2409
         declare_generic_ops(parent, elem);
11✔
2410

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

2420
static bool is_vhdl_infix_op(token_t tok)
97✔
2421
{
2422
   return tok == tEQ || tok == tNEQ || tok == tLT || tok == tGT
97✔
2423
      || tok == tLE || tok == tGE || tok == tNAND || tok == tNOR
94✔
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;
97✔
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,829✔
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,829✔
2453

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

2458
   tree_t left = tree_left(r);
4,030✔
2459
   tree_t right = tree_right(r);
4,030✔
2460

2461
   type_t ltype = tree_type(left);
4,030✔
2462
   type_t rtype = tree_type(right);
4,030✔
2463

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

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

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

2476
   tree_set_left(r, lconv);
837✔
2477

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

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

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

2490
   if (type_is_none(type))
83,672✔
2491
      return type;
2492

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

2497
   tree_t decl = NULL;
83,458✔
2498
   if (namek == T_REF && tree_has_ref(name))
83,458✔
2499
      decl = aliased_type_decl(tree_ref(name));
83,455✔
2500

2501
   if (decl == NULL) {
83,455✔
2502
      diag_t *d = diag_new(DIAG_ERROR, CURRENT_LOC);
7✔
2503
      const char *id = namek == T_REF ? istr(tree_ident(name)) : NULL;
7✔
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);
7✔
2509
      return type_new(T_NONE);
7✔
2510
   }
2511

2512
   return tree_type(decl);
83,451✔
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✔
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✔
2628
            case C_CONSTANT:
×
2629
               if (is_literal(value))
×
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)
377✔
2667
{
2668
   if (psl_kind(prop) == P_CLOCKED)
377✔
2669
      return prop;
2670

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

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

2680
   return p;
371✔
2681
}
2682

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

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

2690
   if (consume(tID))
490,626✔
2691
      return last_lval.ident;
490,623✔
2692
   else
2693
      return error_marker();
3✔
2694
}
2695

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

2700
   ident_t id = p_identifier();
7,488✔
2701
   while (optional(tDOT))
9,141✔
2702
      id = ident_prefix(id, p_identifier(), '.');
1,653✔
2703

2704
   return id;
7,488✔
2705
}
2706

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

2711
   ident_list_t *result = NULL;
61,251✔
2712

2713
   ident_list_push(&result, p_identifier(), last_loc);
61,251✔
2714

2715
   while (optional(tCOMMA))
66,803✔
2716
      ident_list_push(&result, p_identifier(), last_loc);
5,552✔
2717

2718
   return result;
61,251✔
2719
}
2720

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

2725
   consume(tSTRING);
7,423✔
2726

2727
   char *s = last_lval.str;
7,423✔
2728
   for (char *p = s; *p != '\0'; p++)
37,292✔
2729
      *p = tolower_iso88591(*p);
29,869✔
2730

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

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

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

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

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

2746
   consume(tLIBRARY);
1,115✔
2747

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

2750
   consume(tSEMI);
1,115✔
2751

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

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

2759
      lib_t lib = lib_find(it->ident);
1,115✔
2760
      if (lib == NULL) {
1,115✔
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,114✔
2771

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

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

2780
   BEGIN("use clause");
6,134✔
2781

2782
   consume(tUSE);
3,067✔
2783

2784
   do {
3,068✔
2785
      tree_t u = tree_new(T_USE);
3,068✔
2786

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

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

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

2811
      tree_set_ident(u, i1);
3,068✔
2812
      tree_set_ident2(u, i2);
3,068✔
2813

2814
      tree_set_loc(u, CURRENT_LOC);
3,068✔
2815
      (*addf)(unit, u);
3,068✔
2816

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

2821
      const tree_kind_t kind = tree_kind(head);
3,063✔
2822
      if (kind == T_LIBRARY && !tree_has_ident2(head)) {
3,063✔
2823
         // Library declaration had an error
2824
      }
2825
      else if (is_uninstantiated_package(head))
3,063✔
2826
         parse_error(CURRENT_LOC, "cannot use an uninstantiated package");
1✔
2827
      else if (kind == T_LIBRARY || kind == T_PACKAGE
3,062✔
2828
               || kind == T_PACK_INST
163✔
2829
               || (kind == T_GENERIC_DECL
31✔
2830
                   && tree_class(head) == C_PACKAGE)) {
30✔
2831
         tree_set_ref(u, head);
3,061✔
2832
         insert_names_from_use(nametab, u);
3,061✔
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));
3,068✔
2838

2839
   consume(tSEMI);
3,067✔
2840
}
3,067✔
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✔
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)
4,095✔
2873
{
2874
   // library_clause | use_clause | 2008: context_reference
2875

2876
   BEGIN("context item");
8,190✔
2877

2878
   switch (peek()) {
4,095✔
2879
   case tLIBRARY:
1,115✔
2880
      p_library_clause(unit);
1,115✔
2881
      break;
1,115✔
2882

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

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

2891
   default:
×
2892
      expect(tLIBRARY, tUSE, tCONTEXT);
×
2893
   }
2894
}
4,095✔
2895

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

2900
   BEGIN("context clause");
25,866✔
2901

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

2904
   while (scan(tLIBRARY, tUSE, tCONTEXT)) {
17,028✔
2905
      if (peek() == tCONTEXT && peek_nth(3) == tIS)
4,114✔
2906
         break;
2907
      else
2908
         p_context_item(unit);
4,095✔
2909
   }
2910

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

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

2920
   switch (one_of(tIN, tOUT, tINOUT, tBUFFER, tLINKAGE)) {
13,800✔
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)
18,153✔
2937
{
2938
   // attribute_name | simple_expression direction simple_expression
2939

2940
   EXTEND("range");
18,153✔
2941

2942
   tree_t r = tree_new(T_RANGE);
18,153✔
2943
   tree_set_subkind(r, RANGE_ERROR);
18,153✔
2944

2945
   if (is_range_expr(left)) {
18,153✔
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);
15,027✔
2952

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

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

2965
      tree_set_loc(r, CURRENT_LOC);
15,027✔
2966
   }
2967

2968
   return r;
18,153✔
2969
}
2970

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

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

2977
   consume(tRANGE);
714✔
2978

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

2982
   tree_t expr1 = p_expression();
714✔
2983

2984
   switch (peek()) {
714✔
2985
   case tTO:
699✔
2986
   case tDOWNTO:
2987
      {
2988
         tree_t r = p_range(expr1);
699✔
2989
         solve_types(nametab, r, constraint);
699✔
2990
         tree_add_range(t, r);
699✔
2991
      }
2992
      break;
699✔
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);
714✔
3007
   return t;
714✔
3008
}
3009

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

3014
   BEGIN_WITH_HEAD("discrete range", head);
34,918✔
3015

3016
   tree_t expr1 = head ?: p_expression();
17,459✔
3017

3018
   switch (peek()) {
17,459✔
3019
   case tTO:
14,273✔
3020
   case tDOWNTO:
3021
   case tTICK:
3022
      return p_range(expr1);
14,273✔
3023

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

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

3033
         if (!is_type && !type_is_none(constraint)) {
56✔
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);
56✔
3040

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

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

3052
         if (tree_kind(expr1) == T_ATTR_REF)
3,130✔
3053
            return p_range(expr1);   // Special attributes such as 'RANGE
2,225✔
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) {
902✔
3058
               tree_t tmp = tree_new(T_ATTR_REF);
900✔
3059
               tree_set_name(tmp, expr1);
900✔
3060
               tree_set_ident(tmp, ident_new("RANGE"));
900✔
3061
               tree_set_loc(tmp, tree_loc(expr1));
900✔
3062
               tree_set_subkind(tmp, ATTR_RANGE);
900✔
3063

3064
               return p_range(tmp);
900✔
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))
3✔
3071
            parse_error(CURRENT_LOC, "expecting a discrete range");
1✔
3072

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

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

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

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

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

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

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

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

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

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

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

3119
   BEGIN("formal part");
14,028✔
3120

3121
   tree_t name = p_name(0);
7,014✔
3122

3123
   switch (tree_kind(name)) {
7,014✔
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:
86✔
3131
      if (tree_params(name) == 1)
86✔
3132
         tree_set_flag(name, TREE_F_CONVERSION);
85✔
3133
      break;
3134

3135
   case T_REF:
6,251✔
3136
      // 2019 allows signature for generic formal designator
3137
      if (peek() == tLSQUARE) {
6,251✔
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)
51,002✔
3153
{
3154
   // actual_designator
3155
   //   | name ( actual_designator )
3156
   //   | type_mark ( actual_designator )
3157

3158
   BEGIN("actual part");
102,004✔
3159

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

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

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

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

3181
      tree_t expr = p_expression();
20✔
3182

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

3188
         return w;
18✔
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();
50,445✔
3202
   const bool had_name = (next == tID || next == tSTRING);
50,445✔
3203

3204
   tree_t designator = p_expression();
50,445✔
3205

3206
   const bool could_be_conversion =
100,890✔
3207
      had_name
3208
      && tree_kind(designator) == T_FCALL
44,429✔
3209
      && tree_params(designator) == 1;
53,016✔
3210

3211
   if (could_be_conversion)
50,445✔
3212
      tree_set_flag(designator, TREE_F_CONVERSION);
854✔
3213

3214
   return designator;
3215
}
3216

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

3222
   BEGIN("association element");
102,004✔
3223

3224
   tree_t p = tree_new(T_PARAM);
51,002✔
3225

3226
   const look_params_t lookp = {
51,002✔
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;
51,002✔
3236
   type_t type = NULL;
51,002✔
3237
   if (look_for(&lookp)) {
51,002✔
3238
      tree_set_subkind(p, P_NAMED);
7,014✔
3239

3240
      push_scope_for_formals(nametab, kind, unit);
7,014✔
3241

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

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

3254
      if (signature != NULL && kind != F_GENERIC_MAP) {
7,014✔
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))
7,014✔
3261
         type = solve_types(nametab, name, signature);
3,493✔
3262

3263
      if (kind == F_PORT_MAP && tree_kind(name) == T_FCALL)
7,014✔
3264
         name = fcall_to_conv_func(name);
84✔
3265

3266
      tree_set_name(p, name);
7,014✔
3267

3268
      pop_scope(nametab);
7,014✔
3269

3270
      consume(tASSOC);
7,014✔
3271
   }
3272
   else {
3273
      tree_set_subkind(p, P_POS);
43,988✔
3274
      tree_set_pos(p, pos);
43,988✔
3275

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

3290
      if (formal != NULL && (class = tree_class(formal)) != C_PACKAGE)
2,077✔
3291
         type = tree_type(formal);
2,047✔
3292
   }
3293

3294
   tree_t value = p_actual_part(class, kind);
51,002✔
3295

3296
   if (kind == F_PORT_MAP)
51,002✔
3297
      solve_types(nametab, value, type);
3,902✔
3298
   else if (kind == F_GENERIC_MAP && class != C_PACKAGE) {
47,100✔
3299
      type_t value_type = solve_types(nametab, value, type);
1,657✔
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,657✔
3304
         map_generic_type(nametab, type, value_type);
301✔
3305
   }
3306

3307
   if (kind == F_PORT_MAP && tree_kind(value) == T_FCALL)
51,002✔
3308
      value = fcall_to_conv_func(value);
146✔
3309

3310
   tree_set_value(p, value);
51,002✔
3311
   tree_set_loc(p, CURRENT_LOC);
51,002✔
3312

3313
   switch (kind) {
51,002✔
3314
   case F_GENERIC_MAP:
1,737✔
3315
      tree_add_genmap(map, p);
1,737✔
3316
      break;
1,737✔
3317
   case F_PORT_MAP:
49,265✔
3318
   case F_SUBPROGRAM:
3319
      tree_add_param(map, p);
49,265✔
3320
      break;
49,265✔
3321
   default:
×
3322
      fatal_trace("unexpected formal kind in p_association_element");
3323
   }
3324
}
51,002✔
3325

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

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

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

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

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

3344
   p_association_list(call, call, F_SUBPROGRAM);
21,449✔
3345
}
21,449✔
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,521✔
3366
{
3367
   // name [ ( actual_parameter_part ) ]
3368
   // 2019: name [ generic_map_aspect] [ parameter_map_aspect ]
3369

3370
   EXTEND("function call");
29,042✔
3371

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

3383
   if (peek() == tGENERIC) {
14,521✔
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
3395
         type_t type = type_new(T_SIGNATURE);
×
3396
         type_set_ident(type, id);
×
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,521✔
3422
      p_parameter_map_aspect(call);
1✔
3423
   else if (optional(tLPAREN)) {
14,520✔
3424
      p_actual_parameter_part(call);
13,850✔
3425
      consume(tRPAREN);
13,850✔
3426
   }
3427

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

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

3436
   EXTEND("attribute name");
23,171✔
3437

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

3442
   consume(tTICK);
23,171✔
3443

3444
   attr_kind_t kind;
23,171✔
3445
   ident_t id;
23,171✔
3446
   switch (peek()) {
23,171✔
3447
   case tRANGE:
2,203✔
3448
      consume(tRANGE);
2,203✔
3449
      id = ident_new("RANGE");
2,203✔
3450
      kind = ATTR_RANGE;
2,203✔
3451
      break;
2,203✔
3452
   case tREVRANGE:
53✔
3453
      consume(tREVRANGE);
53✔
3454
      id = ident_new("REVERSE_RANGE");
53✔
3455
      kind = ATTR_REVERSE_RANGE;
53✔
3456
      break;
53✔
3457
   case tSUBTYPE:
129✔
3458
      consume(tSUBTYPE);
129✔
3459
      require_std(STD_08, "subtype attribute");
129✔
3460
      id = ident_new("SUBTYPE");
129✔
3461
      kind = ATTR_SUBTYPE;
129✔
3462
      break;
129✔
3463
   case tID:
20,785✔
3464
      id = p_identifier();
20,785✔
3465
      kind = parse_predefined_attr(id);
20,785✔
3466
      break;
20,785✔
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);
23,171✔
3474

3475
   if (signature != NULL) {
23,171✔
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) {
23,171✔
3492
      type = resolve_type(nametab, type);
2✔
3493
      tree_set_type(prefix, type);
2✔
3494
   }
3495

3496
   const bool deref_prefix =
46,342✔
3497
      !is_type_attribute(kind) && kind != ATTR_REFLECT
23,171✔
3498
      && kind != ATTR_PATH_NAME && kind != ATTR_INSTANCE_NAME
22,943✔
3499
      && kind != ATTR_SIMPLE_NAME
22,624✔
3500
      && type != NULL && type_is_access(type);
45,222✔
3501

3502
   if (deref_prefix) {
23,171✔
3503
      prefix = implicit_dereference(prefix);
168✔
3504
      type   = tree_type(prefix);
168✔
3505
   }
3506

3507
   tree_t t = tree_new(T_ATTR_REF);
23,171✔
3508
   tree_set_name(t, prefix);
23,171✔
3509

3510
   tree_set_ident(t, id);
23,171✔
3511
   tree_set_subkind(t, kind);
23,171✔
3512
   tree_set_loc(t, CURRENT_LOC);
23,171✔
3513

3514
   if (attribute_has_param(kind) && optional(tLPAREN)) {
23,171✔
3515
      add_param(t, p_expression(), P_POS, NULL);
3,099✔
3516
      consume(tRPAREN);
3,099✔
3517
      tree_set_loc(t, CURRENT_LOC);
3,099✔
3518
   }
3519

3520
   if (is_type_attribute(kind))
23,171✔
3521
      tree_set_type(t, apply_type_attribute(t));
228✔
3522
   else if (kind == ATTR_DELAYED || kind == ATTR_TRANSACTION
22,943✔
3523
            || kind == ATTR_STABLE || kind == ATTR_QUIET)
22,747✔
3524
      implicit_signal_attribute(t);
310✔
3525

3526
   return t;
23,171✔
3527
}
3528

3529
static tree_t p_selected_name(tree_t prefix, name_mask_t *mask)
12,192✔
3530
{
3531
   // prefix . suffix
3532

3533
   EXTEND("selected name");
24,384✔
3534

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

3553
   consume(tDOT);
12,192✔
3554
   *mask = 0;
12,192✔
3555

3556
   ident_t suffix = NULL;
12,192✔
3557
   switch (peek()) {
12,192✔
3558
   case tID:
11,148✔
3559
      suffix = p_identifier();
11,148✔
3560
      break;
11,148✔
3561

3562
   case tSTRING:
5✔
3563
      suffix = p_operator_symbol();
5✔
3564
      break;
5✔
3565

3566
   case tALL:
1,039✔
3567
      {
3568
         consume(tALL);
1,039✔
3569

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

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

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

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

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

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

3640
         *mask |= N_TYPE;
4✔
3641
      }
3642
   }
3643

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

3653
   type_t type = solve_types(nametab, prefix, NULL);
10,018✔
3654

3655
   if (type_is_access(type)) {
10,018✔
3656
      prefix = implicit_dereference(prefix);
750✔
3657
      type   = tree_type(prefix);
750✔
3658
      prefix_kind = T_ALL;
750✔
3659
   }
3660

3661
   if (type_kind(type) == T_INCOMPLETE) {
10,018✔
3662
      type = resolve_type(nametab, type);
447✔
3663
      tree_set_type(prefix, type);
447✔
3664
   }
3665

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

3704
static tree_t p_indexed_name(tree_t prefix, tree_t head)
14,559✔
3705
{
3706
   // prefix ( expression { , expression } )
3707

3708
   EXTEND("indexed name");
14,559✔
3709

3710
   type_t type = prefix_type(prefix, NULL);
14,559✔
3711

3712
   if (type != NULL && type_is_access(type)) {
14,559✔
3713
      prefix = implicit_dereference(prefix);
344✔
3714
      type   = tree_type(prefix);
344✔
3715
   }
3716

3717
   tree_t t = tree_new(T_ARRAY_REF);
14,559✔
3718
   tree_set_value(t, prefix);
14,559✔
3719

3720
   do {
14,559✔
3721
      tree_t index = head ?: p_expression();
16,200✔
3722
      head = NULL;
16,200✔
3723
      add_param(t, index, P_POS, NULL);
16,200✔
3724
   } while (optional(tCOMMA));
16,200✔
3725

3726
   consume(tRPAREN);
14,559✔
3727

3728
   tree_set_loc(t, CURRENT_LOC);
14,559✔
3729
   return t;
14,559✔
3730
}
3731

3732
static tree_t p_type_conversion(tree_t prefix)
2,853✔
3733
{
3734
   // type_conversion ::= type_mark ( expression )
3735

3736
   EXTEND("type conversion");
2,853✔
3737

3738
   consume(tLPAREN);
2,853✔
3739

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

3755
   tree_t value = p_expression();
2,853✔
3756
   solve_types(nametab, value, NULL);
2,853✔
3757

3758
   tree_t conv = tree_new(T_TYPE_CONV);
2,853✔
3759
   tree_set_type(conv, type);
2,853✔
3760
   tree_set_value(conv, value);
2,853✔
3761

3762
   consume(tRPAREN);
2,853✔
3763

3764
   tree_set_loc(conv, CURRENT_LOC);
2,853✔
3765
   return conv;
2,853✔
3766
}
3767

3768
static void p_partial_pathname(tree_t name)
185✔
3769
{
3770
   // { pathname_element . } object_simple_name
3771

3772
   BEGIN("partial pathname");
370✔
3773

3774
   do {
417✔
3775
      tree_t pe = tree_new(T_PATH_ELT);
417✔
3776
      tree_set_ident(pe, p_identifier());
417✔
3777

3778
      if (optional(tLPAREN)) {
417✔
3779
         tree_t expr = p_expression();
30✔
3780
         solve_types(nametab, expr, NULL);
30✔
3781

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

3792
      tree_add_part(name, pe);
417✔
3793
   } while (optional(tDOT));
417✔
3794
}
185✔
3795

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

3801
   BEGIN("package pathname");
14✔
3802

3803
   consume(tAT);
7✔
3804

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

3810
   tree_add_part(name, pe);
7✔
3811

3812
   consume(tDOT);
7✔
3813

3814
   p_partial_pathname(name);
7✔
3815
}
7✔
3816

3817
static void p_absolute_pathname(tree_t name)
44✔
3818
{
3819
   // . partial_pathname
3820

3821
   BEGIN("absolute pathname");
88✔
3822

3823
   consume(tDOT);
44✔
3824

3825
   tree_t pe = tree_new(T_PATH_ELT);
44✔
3826
   tree_set_subkind(pe, PE_ABSOLUTE);
44✔
3827
   tree_set_loc(pe, CURRENT_LOC);
44✔
3828

3829
   tree_add_part(name, pe);
44✔
3830

3831
   p_partial_pathname(name);
44✔
3832
}
44✔
3833

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

3838
   BEGIN("relative pathname");
268✔
3839

3840
   tree_t pe = tree_new(T_PATH_ELT);
134✔
3841
   tree_set_subkind(pe, PE_RELATIVE);
134✔
3842

3843
   tree_add_part(name, pe);
134✔
3844

3845
   while (peek() == tCARET) {
185✔
3846
      consume(tCARET);
51✔
3847
      consume(tDOT);
51✔
3848

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

3853
      tree_add_part(name, pe);
51✔
3854
   }
3855

3856
   p_partial_pathname(name);
134✔
3857
}
134✔
3858

3859
static void p_external_pathname(tree_t name)
185✔
3860
{
3861
   // package_pathname | absolute_pathname | relative_pathname
3862

3863
   BEGIN("external pathname");
370✔
3864

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

3881
static tree_t p_external_name(void)
185✔
3882
{
3883
   // << constant external_pathname : subtype_indication >>
3884
   //   | << signal external_pathname : subtype_indication >>
3885
   //   | << variable external_pathname : subtype_indication >>
3886

3887
   BEGIN("external name");
185✔
3888

3889
   consume(tLTLT);
185✔
3890

3891
   require_std(STD_08, "external names");
185✔
3892

3893
   tree_t t = tree_new(T_EXTERNAL_NAME);
185✔
3894

3895
   switch (one_of(tCONSTANT, tSIGNAL, tVARIABLE)) {
185✔
3896
   case tSIGNAL:   tree_set_class(t, C_SIGNAL); break;
142✔
3897
   case tCONSTANT: tree_set_class(t, C_CONSTANT); break;
37✔
3898
   case tVARIABLE: tree_set_class(t, C_VARIABLE); break;
6✔
3899
   }
3900

3901
   p_external_pathname(t);
185✔
3902

3903
   consume(tCOLON);
185✔
3904

3905
   tree_set_type(t, p_subtype_indication());
185✔
3906

3907
   consume(tGTGT);
185✔
3908

3909
   tree_set_global_flags(t, TREE_GF_EXTERNAL_NAME);
185✔
3910

3911
   tree_set_loc(t, CURRENT_LOC);
185✔
3912
   return t;
185✔
3913
}
3914

3915
static tree_t p_name(name_mask_t stop_mask)
313,699✔
3916
{
3917
   // simple_name | operator_symbol | selected_name | indexed_name
3918
   //   | slice_name | attribute_name | 2008: external_name
3919

3920
   BEGIN("name");
627,398✔
3921

3922
   ident_t id = NULL;
313,699✔
3923
   switch (peek()) {
313,699✔
3924
   case tSTRING:
99✔
3925
      id = p_operator_symbol();
99✔
3926
      break;
99✔
3927

3928
   case tID:
313,409✔
3929
      id = p_identifier();
313,409✔
3930
      break;
313,409✔
3931

3932
   case tLTLT:
185✔
3933
      return p_external_name();
185✔
3934

3935
   default:
6✔
3936
      {
3937
         expect(tSTRING, tID);
6✔
3938

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

3947
   tree_t decl = NULL;
313,508✔
3948
   name_mask_t mask = query_name(nametab, id, &decl);
313,508✔
3949

3950
   tree_t prefix = tree_new(T_REF);
313,508✔
3951
   tree_set_ident(prefix, id);
313,508✔
3952
   tree_set_loc(prefix, CURRENT_LOC);
313,508✔
3953
   tree_set_ref(prefix, decl);
313,508✔
3954

3955
   for (;;) {
385,750✔
3956
      switch (peek()) {
385,750✔
3957
      case tLPAREN:
3958
         break;
51,364✔
3959

3960
      case tDOT:
12,192✔
3961
         prefix = p_selected_name(prefix, &mask);
12,192✔
3962
         continue;
12,192✔
3963

3964
      case tTICK:
26,799✔
3965
         if (peek_nth(2) == tLPAREN) {
26,799✔
3966
            if (mask & stop_mask)
3,642✔
3967
               return prefix;
256✔
3968
            else {
3969
               prefix = p_qualified_expression(prefix);
3,386✔
3970
               mask = N_OBJECT;
3,386✔
3971
            }
3972
         }
3973
         else {
3974
            prefix = p_attribute_name(prefix);
23,157✔
3975
            mask = is_type_attribute(tree_subkind(prefix)) ? N_TYPE : N_OBJECT;
46,086✔
3976
         }
3977
         continue;
26,543✔
3978

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

3989
      default:
3990
         return prefix;
3991
      }
3992

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

3996
      if (mask & stop_mask)
51,364✔
3997
         return prefix;
17,874✔
3998

3999
      if (!(mask & N_FUNC) && scope_formal_kind(nametab) == F_SUBPROGRAM) {
33,490✔
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,490✔
4005

4006
      if (mask & N_TYPE) {
33,490✔
4007
         // Type conversion
4008
         prefix = p_type_conversion(prefix);
2,853✔
4009
         mask = N_OBJECT;
2,853✔
4010
         continue;
2,853✔
4011
      }
4012
      else if ((mask & N_SUBPROGRAM) && prefix_kind == T_REF) {
30,637✔
4013
         // Function call
4014
         prefix = p_function_call(tree_ident(prefix), NULL);
13,705✔
4015
         mask = N_OBJECT;
13,705✔
4016
         continue;
13,705✔
4017
      }
4018
      else if (!(mask & N_OBJECT) && prefix_kind == T_PROT_REF) {
16,932✔
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,803✔
4029

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

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

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

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

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

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

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

4055
   consume(tLPAREN);
10,750✔
4056

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

4067
   consume(tRPAREN);
10,750✔
4068

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

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

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

4079
   type_t sub = type_new(T_SUBTYPE);
867✔
4080
   if (is_anonymous_subtype(base)) {
867✔
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);
851✔
4089

4090
   if (type_is_record(base))
867✔
4091
      type_set_constraint(sub, p_record_constraint(sub));
122✔
4092
   else
4093
      p_array_constraint(sub, base);
745✔
4094

4095
   return sub;
867✔
4096
}
4097

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

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

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

4110
      if (!type_is_array(type) && !type_is_none(type))
62✔
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,487✔
4116

4117
      if (type_has_constraint(type)) {
5,487✔
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,484✔
4126
   }
4127

4128
   if (peek() != tLPAREN)
5,549✔
4129
      return;
5,266✔
4130

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

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

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

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

4145
   ident_t id = p_identifier();
584✔
4146

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

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

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

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

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

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

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

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

4179
   consume(tLPAREN);
347✔
4180

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

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

4188
   consume(tRPAREN);
347✔
4189

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

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

4199
   BEGIN("constraint");
21,584✔
4200

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

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

4209
   case tLPAREN:
10,292✔
4210
      if (standard() < STD_08)
10,292✔
4211
         type_set_constraint(type, p_index_constraint(base));
5,263✔
4212
      else if (type_is_record(base))
5,029✔
4213
         type_set_constraint(type, p_record_constraint(base));
225✔
4214
      else
4215
         p_array_constraint(type, base);
4,804✔
4216
      break;
4217

4218
   default:
×
4219
      one_of(tRANGE, tLPAREN);
×
4220
   }
4221
}
10,792✔
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)
65,743✔
4261
{
4262
   // [ name ] type_mark [ constraint ]
4263

4264
   BEGIN("subtype indication");
65,743✔
4265

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

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

4280
   if (rname != NULL) {
65,743✔
4281
      type = type_new(T_SUBTYPE);
354✔
4282
      made_subtype = true;
354✔
4283

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

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

4290
   if (type == NULL)
65,743✔
4291
      type = p_type_mark();
×
4292

4293
   if (scan(tRANGE, tLPAREN)) {
65,743✔
4294
      if (!made_subtype) {
10,792✔
4295
         type_t sub = type_new(T_SUBTYPE);
10,773✔
4296
         type_set_base(sub, type);
10,773✔
4297

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

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

4304
   return type;
65,743✔
4305
}
4306

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

4311
   BEGIN("abstract literal");
89,429✔
4312

4313
   tree_t t = tree_new(T_LITERAL);
89,429✔
4314

4315
   switch (one_of(tINT, tREAL)) {
89,429✔
4316
   case tINT:
71,880✔
4317
      tree_set_subkind(t, L_INT);
71,880✔
4318
      tree_set_ival(t, last_lval.i64);
71,880✔
4319
      break;
71,880✔
4320

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

4327
   tree_set_loc(t, CURRENT_LOC);
89,429✔
4328
   return t;
89,429✔
4329
}
4330

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

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

4337
   tree_t mult;
7,813✔
4338
   if (scan(tINT, tREAL))
7,813✔
4339
      mult = p_abstract_literal();
7,812✔
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,813✔
4346

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

4352
   return mult;
7,813✔
4353
}
4354

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

4359
   BEGIN("numeric literal");
178,634✔
4360

4361
   if (peek_nth(2) == tID)
89,317✔
4362
      return p_physical_literal();
7,700✔
4363
   else
4364
      return p_abstract_literal();
81,617✔
4365
}
4366

4367
static tree_t p_string_literal(void)
22,681✔
4368
{
4369
   // string_literal
4370

4371
   BEGIN("string literal");
22,681✔
4372

4373
   consume(tSTRING);
22,681✔
4374

4375
   tree_t t = tree_new(T_STRING);
22,681✔
4376
   tree_set_loc(t, CURRENT_LOC);
22,681✔
4377

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

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

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

4395
   BEGIN("literal");
229,516✔
4396

4397
   switch (peek()) {
114,758✔
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:
89,317✔
4409
   case tREAL:
4410
      return p_numeric_literal();
89,317✔
4411

4412
   case tSTRING:
22,681✔
4413
      return p_string_literal();
22,681✔
4414

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

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

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

4430
static void p_agg_choice(tree_t parent, tree_t head, type_t constraint)
5,698✔
4431
{
4432
   // simple_expression | discrete_range | simple_name | others
4433

4434
   BEGIN("choice");
11,396✔
4435

4436
   // TODO: replace with p_choice
4437

4438
   tree_t t = tree_new(T_ASSOC);
5,698✔
4439

4440
   if (head == NULL && optional(tOTHERS))
5,698✔
4441
      tree_set_subkind(t, A_OTHERS);
2,601✔
4442
   else {
4443
      tree_t name = head ?: p_expression();
3,097✔
4444
      const tree_kind_t name_kind = tree_kind(name);
3,097✔
4445

4446
      bool is_range = false;
3,097✔
4447

4448
      if (scan(tDOWNTO, tTO, tRANGE, tREVRANGE))
3,097✔
4449
         is_range = true;
4450
      else if (constraint != NULL && name_kind == T_REF && tree_has_ref(name))
2,548✔
UNCOV
4451
         is_range = is_type_decl(tree_ref(name));
×
4452
      else if (name_kind == T_ATTR_REF) {
2,548✔
4453
         const attr_kind_t attr = tree_subkind(name);
185✔
4454
         is_range = attr == ATTR_RANGE || attr == ATTR_REVERSE_RANGE;
185✔
4455
      }
4456

4457
      tree_t choice = NULL;
3,282✔
4458
      if (is_range) {
185✔
4459
         tree_set_subkind(t, A_RANGE);
656✔
4460
         tree_add_range(t, (choice = p_discrete_range(name)));
656✔
4461
      }
4462
      else {
4463
         tree_set_subkind(t, A_NAMED);
2,441✔
4464
         tree_set_name(t, (choice = name));
2,441✔
4465
      }
4466

4467
      if (constraint != NULL)
3,097✔
UNCOV
4468
         solve_types(nametab, choice, constraint);
×
4469
   }
4470

4471
   tree_set_loc(t, CURRENT_LOC);
5,698✔
4472
   tree_add_assoc(parent, t);
5,698✔
4473
}
5,698✔
4474

4475
static void p_agg_choices(tree_t parent, tree_t head, type_t constraint)
5,455✔
4476
{
4477
   // choices ::= choice { | choice }
4478

4479
   BEGIN("choices");
10,910✔
4480

4481
   // TODO: replace with p_choice
4482

4483
   p_agg_choice(parent, head, constraint);
5,455✔
4484

4485
   while (optional(tBAR))
5,698✔
4486
      p_agg_choice(parent, NULL, constraint);
243✔
4487
}
5,455✔
4488

4489
static tree_t p_choice(tree_t head, type_t constraint)
3,499✔
4490
{
4491
   // simple_expression | discrete_range | simple_name | others
4492

4493
   BEGIN("choice");
6,998✔
4494

4495
   tree_t t = tree_new(T_CHOICE);
3,499✔
4496

4497
   if (head == NULL && optional(tOTHERS)) {
3,499✔
4498
      tree_set_loc(t, CURRENT_LOC);
455✔
4499
      return t;
455✔
4500
   }
4501

4502
   tree_t name = head ?: p_expression();
3,044✔
4503
   const tree_kind_t name_kind = tree_kind(name);
3,044✔
4504

4505
   bool is_range = false;
3,044✔
4506

4507
   if (scan(tDOWNTO, tTO, tRANGE, tREVRANGE))
3,044✔
4508
      is_range = true;
4509
   else if (constraint != NULL && name_kind == T_REF && tree_has_ref(name))
3,008✔
4510
      is_range = is_type_decl(tree_ref(name));
1,060✔
4511
   else if (name_kind == T_ATTR_REF) {
1,948✔
4512
      const attr_kind_t attr = tree_subkind(name);
13✔
4513
      is_range = attr == ATTR_RANGE || attr == ATTR_REVERSE_RANGE;
13✔
4514
   }
4515

4516
   tree_t choice = NULL;
4,117✔
4517
   if (is_range)
1,073✔
4518
      tree_add_range(t, (choice = p_discrete_range(name)));
44✔
4519
   else
4520
      tree_set_name(t, (choice = name));
3,000✔
4521

4522
   if (constraint != NULL)
3,044✔
4523
      solve_types(nametab, choice, constraint);
3,044✔
4524

4525
   tree_set_loc(t, CURRENT_LOC);
3,044✔
4526
   return t;
3,044✔
4527
}
4528

4529
static void p_choices(tree_t parent, tree_t head, type_t constraint)
2,876✔
4530
{
4531
   // choices ::= choice { | choice }
4532

4533
   BEGIN("choices");
5,752✔
4534

4535
   do {
3,499✔
4536
      tree_add_choice(parent, p_choice(head, constraint));
3,499✔
4537
      head = NULL;
3,499✔
4538
   } while (optional(tBAR));
3,499✔
4539
}
2,876✔
4540

4541
static void p_element_association(tree_t agg, tree_t head)
37,738✔
4542
{
4543
   // [ choices => ] expression
4544

4545
   BEGIN("element association");
75,476✔
4546

4547
   if (head == NULL && peek() != tOTHERS)
37,738✔
4548
      head = p_expression();
28,535✔
4549

4550
   const int nstart = tree_assocs(agg);
37,738✔
4551

4552
   if (scan(tBAR, tASSOC, tTO, tDOWNTO, tOTHERS)) {
37,738✔
4553
      p_agg_choices(agg, head, NULL);
5,455✔
4554

4555
      consume(tASSOC);
5,455✔
4556

4557
      tree_t value = p_expression();
5,455✔
4558
      const int nassocs = tree_assocs(agg);
5,455✔
4559
      for (int i = nstart; i < nassocs; i++) {
11,153✔
4560
         tree_t a = tree_assoc(agg, i);
5,698✔
4561
         tree_set_value(a, value);
5,698✔
4562
         tree_set_loc(a, CURRENT_LOC);
5,698✔
4563
      }
4564
   }
4565
   else {
4566
      tree_t t = tree_new(T_ASSOC);
32,283✔
4567
      tree_set_subkind(t, A_POS);
32,283✔
4568
      tree_set_value(t, head ?: p_expression());
32,283✔
4569
      tree_set_loc(t, CURRENT_LOC);
32,283✔
4570
      tree_set_pos(t, nstart);
32,283✔
4571

4572
      tree_add_assoc(agg, t);
32,283✔
4573
   }
4574
}
37,738✔
4575

4576
static tree_t p_aggregate(void)
2,512✔
4577
{
4578
   // ( element_association { , element_association } )
4579

4580
   BEGIN("aggregate");
2,512✔
4581

4582
   tree_t t = tree_new(T_AGGREGATE);
2,512✔
4583

4584
   consume(tLPAREN);
2,512✔
4585

4586
   do {
2,699✔
4587
      p_element_association(t, NULL);
2,699✔
4588
   } while (optional(tCOMMA));
2,699✔
4589

4590
   consume(tRPAREN);
2,512✔
4591

4592
   tree_set_loc(t, CURRENT_LOC);
2,512✔
4593
   return t;
2,512✔
4594
}
4595

4596
static tree_t p_aggregate_or_expression(void)
26,182✔
4597
{
4598
   // aggregate | expression
4599

4600
   BEGIN("aggregate or expression");
52,364✔
4601

4602
   if (peek_nth(2) == tOTHERS)
26,182✔
4603
      return p_aggregate();
2,380✔
4604

4605
   consume(tLPAREN);
23,802✔
4606

4607
   tree_t head = p_expression();
23,802✔
4608

4609
   switch (peek()) {
23,802✔
4610
   case tRPAREN:
17,200✔
4611
      consume(tRPAREN);
17,200✔
4612
      return head;
17,200✔
4613

4614
   case tASSOC:
6,602✔
4615
   case tCOMMA:
4616
   case tTO:
4617
   case tDOWNTO:
4618
   case tBAR:
4619
      {
4620
         tree_t t = tree_new(T_AGGREGATE);
6,602✔
4621

4622
         p_element_association(t, head);
6,602✔
4623
         while (optional(tCOMMA))
35,039✔
4624
            p_element_association(t, NULL);
28,437✔
4625

4626
         consume(tRPAREN);
6,602✔
4627

4628
         tree_set_loc(t, CURRENT_LOC);
6,602✔
4629
         return t;
6,602✔
4630
      }
4631

4632
   default:
×
4633
      expect(tRPAREN, tASSOC, tCOMMA, tTO, tDOWNTO, tBAR);
×
4634
      drop_tokens_until(tRPAREN);
×
4635
      return head;
×
4636
   }
4637
}
4638

4639
static tree_t p_qualified_expression(tree_t prefix)
3,642✔
4640
{
4641
   // type_mark ' ( expression ) | type_mark ' aggregate
4642

4643
   EXTEND("qualified expression");
3,642✔
4644

4645
   type_t type = NULL;
3,642✔
4646
   if (prefix == NULL)
3,642✔
4647
      type = p_type_mark();
256✔
4648
   else {
4649
      switch (tree_kind(prefix)) {
3,386✔
4650
      case T_ATTR_REF:
1✔
4651
         if (is_type_attribute(tree_subkind(prefix)))
1✔
4652
            type = tree_type(prefix);
1✔
4653
         break;
4654
      case T_REF:
3,385✔
4655
         if (tree_has_ref(prefix)) {
3,385✔
4656
            tree_t decl = aliased_type_decl(tree_ref(prefix));
3,385✔
4657
            if (decl != NULL)
3,385✔
4658
               type = tree_type(decl);
3,384✔
4659
         }
4660
         break;
4661
      default:
4662
         break;
4663
      }
4664
   }
4665

4666
   if (type == NULL) {
3,641✔
4667
      parse_error(tree_loc(prefix), "expecting type mark while parsing "
1✔
4668
                  "qualified expression");
4669
      type = type_new(T_NONE);
1✔
4670
   }
4671

4672
   tree_t qual = tree_new(T_QUALIFIED);
3,642✔
4673
   tree_set_type(qual, type);
3,642✔
4674
   tree_set_ident(qual, type_ident(type));
3,642✔
4675

4676
   consume(tTICK);
3,642✔
4677

4678
   tree_t value = p_aggregate_or_expression();
3,642✔
4679

4680
   tree_set_value(qual, value);
3,642✔
4681
   solve_types(nametab, value, type);
3,642✔
4682

4683
   tree_set_loc(qual, CURRENT_LOC);
3,642✔
4684
   return qual;
3,642✔
4685
}
4686

4687
static tree_t p_allocator(void)
545✔
4688
{
4689
   // new subtype_indication | new qualified_expression
4690

4691
   BEGIN("allocator");
545✔
4692

4693
   consume(tNEW);
545✔
4694

4695
   tree_t new = tree_new(T_NEW);
545✔
4696

4697
   tree_t value;
545✔
4698
   if (peek_nth(2) == tTICK && peek_nth(3) == tLPAREN)
545✔
4699
      value = p_qualified_expression(NULL);
256✔
4700
   else {
4701
      type_t type = p_subtype_indication();
289✔
4702

4703
      value = tree_new(T_QUALIFIED);
289✔
4704
      tree_set_type(value, type);
289✔
4705
      tree_set_loc(value, CURRENT_LOC);
289✔
4706
   }
4707

4708
   tree_set_value(new, value);
545✔
4709
   tree_set_loc(new, CURRENT_LOC);
545✔
4710

4711
   return new;
545✔
4712
}
4713

4714
static tree_t p_primary(tree_t head)
326,861✔
4715
{
4716
   // name | literal | aggregate | function_call | qualified_expression
4717
   //   | type_conversion | allocator | ( expression ) |
4718
   //   | PSL: Built_In_Function_Call
4719

4720
   BEGIN("primary");
653,722✔
4721

4722
   assert(head == NULL);
326,861✔
4723

4724
   switch (peek()) {
326,861✔
4725
   case tLPAREN:
22,540✔
4726
      return p_aggregate_or_expression();
22,540✔
4727

4728
   case tINT:
92,077✔
4729
   case tREAL:
4730
   case tNULL:
4731
   case tBITSTRING:
4732
      return p_literal();
92,077✔
4733

4734
   case tSTRING:
22,758✔
4735
      if (peek_nth(2) != tLPAREN && peek_nth(2) != tDOT)
22,758✔
4736
         return p_literal();
22,681✔
4737
      // Fall-through
4738
   case tID:
4739
      {
4740
         tree_t expr = p_name(0);
188,844✔
4741
         if (peek() == tLSQUARE)
188,844✔
4742
            return p_attribute_name(expr);
14✔
4743
         else if (tree_kind(expr) == T_PROT_REF)
188,830✔
4744
            return p_function_call(tree_ident(expr), tree_value(expr));
535✔
4745
         else
4746
            return expr;
4747
      }
4748

4749
   case tLTLT:
117✔
4750
      return p_name(N_SUBPROGRAM);
117✔
4751

4752
   case tNEW:
545✔
4753
      return p_allocator();
545✔
4754

4755
   case tPREV:
49✔
4756
   case tPSLNEXT:
4757
   case tROSE:
4758
   case tFELL:
4759
   case tENDED:
4760
   case tSTABLE:
4761
   case tNONDET:
4762
      {
4763
         psl_node_t p = p_psl_builtin_function_call();
49✔
4764
         psl_check(p, nametab);
49✔
4765

4766
         tree_t t = tree_new(T_PSL_FCALL);
49✔
4767
         tree_set_psl(t, p);
49✔
4768
         tree_set_loc(t, CURRENT_LOC);
49✔
4769
         return t;
49✔
4770
      }
4771

4772
   default:
8✔
4773
      expect(tLPAREN, tINT, tREAL, tNULL, tID, tSTRING, tBITSTRING, tNEW);
8✔
4774
      return error_expr();
8✔
4775
   }
4776
}
4777

4778
static ident_t p_logical_operator(void)
8,247✔
4779
{
4780
   switch (one_of(tAND, tOR, tNAND, tNOR, tXOR, tXNOR)) {
8,247✔
4781
   case tAND:
5,727✔
4782
      return well_known(W_OP_AND);
5,727✔
4783
   case tOR:
1,749✔
4784
      return well_known(W_OP_OR);
1,749✔
4785
   case tNAND:
147✔
4786
      return well_known(W_OP_NAND);
147✔
4787
   case tNOR:
151✔
4788
      return well_known(W_OP_NOR);
151✔
4789
   case tXOR:
332✔
4790
      return well_known(W_OP_XOR);
332✔
4791
   case tXNOR:
141✔
4792
      return well_known(W_OP_XNOR);
141✔
4793
   default:
×
4794
      return error_marker();
×
4795
   }
4796
}
4797

4798
static tree_t p_unary_expression(tree_t head)
326,990✔
4799
{
4800
   // primary | abs primary | not primary | unary_logical_operator primary
4801

4802
   BEGIN("unary expression");
653,980✔
4803

4804
   if (head != NULL)
326,990✔
4805
      return head;    // Injected from mis-parsed PSL property
4806

4807
   ident_t op = NULL;
326,568✔
4808
   switch (peek()) {
326,568✔
4809
   case tNOT:
2,942✔
4810
      consume(tNOT);
2,942✔
4811
      op = well_known(W_OP_NOT);
2,942✔
4812
      break;
2,942✔
4813

4814
   case tABS:
252✔
4815
      consume(tABS);
252✔
4816
      op = well_known(W_OP_ABS);
252✔
4817
      break;
252✔
4818

4819
   case tAND:
276✔
4820
   case tOR:
4821
   case tNAND:
4822
   case tNOR:
4823
   case tXOR:
4824
   case tXNOR:
4825
      require_std(STD_08, "unary logical operators");
276✔
4826
      op = p_logical_operator();
276✔
4827
      break;
276✔
4828

4829
   default:
4830
      break;
4831
   }
4832

4833
   if (op != NULL) {
3,470✔
4834
      tree_t t = tree_new(T_FCALL);
3,470✔
4835
      tree_set_ident(t, op);
3,470✔
4836
      unary_op(t, p_primary);
3,470✔
4837
      tree_set_loc(t, CURRENT_LOC);
3,470✔
4838

4839
      return t;
3,470✔
4840
   }
4841
   else
4842
      return p_primary(NULL);
323,098✔
4843
}
4844

4845
static tree_t p_factor(tree_t head)
326,990✔
4846
{
4847
   // unary_expression [ ** unary_expression ]
4848

4849
   BEGIN("factor");
653,980✔
4850

4851
   tree_t operand = p_unary_expression(head);
326,990✔
4852

4853
   if (optional(tPOWER)) {
326,990✔
4854
      tree_t second = p_primary(NULL);
291✔
4855

4856
      tree_t t = tree_new(T_FCALL);
291✔
4857
      tree_set_loc(t, CURRENT_LOC);
291✔
4858
      tree_set_ident(t, well_known(W_OP_EXPONENT));
291✔
4859
      add_param(t, operand, P_POS, NULL);
291✔
4860
      add_param(t, second, P_POS, NULL);
291✔
4861

4862
      return t;
291✔
4863
   }
4864

4865
   return operand;
4866
}
4867

4868
static ident_t p_multiplying_operator(void)
3,162✔
4869
{
4870
   switch (one_of(tTIMES, tOVER, tMOD, tREM)) {
3,162✔
4871
   case tTIMES:
1,828✔
4872
      return well_known(W_OP_TIMES);
1,828✔
4873
   case tOVER:
899✔
4874
      return well_known(W_OP_DIVIDE);
899✔
4875
   case tMOD:
288✔
4876
      return well_known(W_OP_MOD);
288✔
4877
   case tREM:
147✔
4878
      return well_known(W_OP_REM);
147✔
4879
   default:
×
4880
      return error_marker();
×
4881
   }
4882
}
4883

4884
static tree_t p_term(tree_t head)
323,828✔
4885
{
4886
   // factor { multiplying_operator factor }
4887

4888
   BEGIN("term");
323,828✔
4889

4890
   tree_t term = p_factor(head);
323,828✔
4891

4892
   while (scan(tTIMES, tOVER, tMOD, tREM)) {
326,990✔
4893
      ident_t op  = p_multiplying_operator();
3,162✔
4894
      tree_t left = term;
3,162✔
4895

4896
      term = tree_new(T_FCALL);
3,162✔
4897
      tree_set_ident(term, op);
3,162✔
4898
      binary_op(term, left, p_factor);
3,162✔
4899
   }
4900

4901
   return term;
323,828✔
4902
}
4903

4904
static ident_t p_adding_operator(void)
12,247✔
4905
{
4906
   switch (one_of(tPLUS, tMINUS, tAMP)) {
12,247✔
4907
   case tPLUS:
3,909✔
4908
      return well_known(W_OP_ADD);
3,909✔
4909
   case tMINUS:
5,741✔
4910
      return well_known(W_OP_MINUS);
5,741✔
4911
   case tAMP:
2,597✔
4912
      return well_known(W_OP_CONCAT);
2,597✔
4913
   default:
×
4914
      return error_marker();
×
4915
   }
4916
}
4917

4918
static ident_t p_sign(void)
8,448✔
4919
{
4920
   switch (one_of(tPLUS, tMINUS)) {
8,448✔
4921
   case tPLUS:
36✔
4922
      return well_known(W_OP_ADD);
36✔
4923
   case tMINUS:
8,412✔
4924
      return well_known(W_OP_MINUS);
8,412✔
4925
   default:
×
4926
      return error_marker();
×
4927
   }
4928
}
4929

4930
static tree_t p_simple_expression(tree_t head)
311,581✔
4931
{
4932
   // [ sign ] term { adding_operator term }
4933

4934
   BEGIN("simple expression");
311,581✔
4935

4936
   tree_t expr = NULL;
311,581✔
4937
   if (head == NULL && scan(tPLUS, tMINUS)) {
320,029✔
4938
      ident_t sign = p_sign();
8,448✔
4939
      expr = tree_new(T_FCALL);
8,448✔
4940
      tree_set_ident(expr, sign);
8,448✔
4941
      unary_op(expr, p_term);
8,448✔
4942
      tree_set_loc(expr, CURRENT_LOC);
8,448✔
4943
   }
4944
   else
4945
      expr = p_term(head);
303,133✔
4946

4947
   while (scan(tPLUS, tMINUS, tAMP)) {
323,828✔
4948
      tree_t left = expr;
12,247✔
4949
      expr = tree_new(T_FCALL);
12,247✔
4950
      tree_set_ident(expr, p_adding_operator());
12,247✔
4951
      binary_op(expr, left, p_term);
12,247✔
4952
   }
4953

4954
   return expr;
311,581✔
4955
}
4956

4957
static ident_t p_shift_operator(void)
312✔
4958
{
4959
   switch (one_of(tSLL, tSRL, tSLA, tSRA, tROL, tROR)) {
312✔
4960
   case tSLL:
36✔
4961
      return well_known(W_OP_SLL);
36✔
4962
   case tSRL:
32✔
4963
      return well_known(W_OP_SRL);
32✔
4964
   case tSLA:
26✔
4965
      return well_known(W_OP_SLA);
26✔
4966
   case tSRA:
32✔
4967
      return well_known(W_OP_SRA);
32✔
4968
   case tROL:
96✔
4969
      return well_known(W_OP_ROL);
96✔
4970
   case tROR:
90✔
4971
      return well_known(W_OP_ROR);
90✔
4972
   default:
×
4973
      return error_marker();
×
4974
   }
4975
}
4976

4977
static tree_t p_shift_expression(tree_t head)
311,269✔
4978
{
4979
   // simple_expression [ shift_operator simple_expression ]
4980

4981
   BEGIN("shift expression");
311,269✔
4982

4983
   tree_t shift = p_simple_expression(head);
311,269✔
4984

4985
   while (scan(tSLL, tSRL, tSLA, tSRA, tROL, tROR)) {
311,581✔
4986
      ident_t op   = p_shift_operator();
312✔
4987
      tree_t left  = shift;
312✔
4988
      tree_t right = p_simple_expression(NULL);
312✔
4989

4990
      shift = tree_new(T_FCALL);
312✔
4991
      tree_set_ident(shift, op);
312✔
4992
      tree_set_loc(shift, CURRENT_LOC);
312✔
4993

4994
      add_param(shift, left, P_POS, NULL);
312✔
4995
      add_param(shift, right, P_POS, NULL);
312✔
4996
   }
4997

4998
   return shift;
311,269✔
4999
}
5000

5001
static ident_t p_relational_operator(void)
30,646✔
5002
{
5003
   switch (one_of(tEQ, tNEQ, tLT, tLE, tGT, tGE,
30,646✔
5004
                  tMEQ, tMNEQ, tMLT, tMLE, tMGT, tMGE)) {
5005
   case tEQ:
23,813✔
5006
      return well_known(W_OP_EQUAL);
23,813✔
5007
   case tNEQ:
1,037✔
5008
      return well_known(W_OP_NOT_EQUAL);
1,037✔
5009
   case tLT:
2,440✔
5010
      return well_known(W_OP_LESS_THAN);
2,440✔
5011
   case tLE:
738✔
5012
      return well_known(W_OP_LESS_EQUAL);
738✔
5013
   case tGT:
1,490✔
5014
      return well_known(W_OP_GREATER_THAN);
1,490✔
5015
   case tGE:
739✔
5016
      return well_known(W_OP_GREATER_EQUAL);
739✔
5017
   case tMEQ:
97✔
5018
      return well_known(W_OP_MATCH_EQUAL);
97✔
5019
   case tMNEQ:
64✔
5020
      return well_known(W_OP_MATCH_NOT_EQUAL);
64✔
5021
   case tMLT:
58✔
5022
      return well_known(W_OP_MATCH_LESS_THAN);
58✔
5023
   case tMLE:
59✔
5024
      return well_known(W_OP_MATCH_LESS_EQUAL);
59✔
5025
   case tMGT:
56✔
5026
      return well_known(W_OP_MATCH_GREATER_THAN);
56✔
5027
   case tMGE:
55✔
5028
      return well_known(W_OP_MATCH_GREATER_EQUAL);
55✔
5029
   default:
×
5030
      return error_marker();
×
5031
   }
5032
}
5033

5034
static tree_t p_relation(tree_t head)
280,623✔
5035
{
5036
   // shift_expression [ relational_operator shift_expression ]
5037

5038
   BEGIN("relation");
280,623✔
5039

5040
   tree_t rel = p_shift_expression(head);
280,623✔
5041

5042
   while (scan(tEQ, tNEQ, tLT, tLE, tGT, tGE,
492,442✔
5043
               tMEQ, tMNEQ, STD(08, tMLT), tMLE, tMGT, tMGE)) {
5044
      ident_t op  = p_relational_operator();
30,646✔
5045
      tree_t left = rel;
30,646✔
5046

5047
      rel = tree_new(T_FCALL);
30,646✔
5048
      tree_set_ident(rel, op);
30,646✔
5049
      binary_op(rel, left, p_shift_expression);
30,646✔
5050
   }
5051

5052
   return rel;
280,623✔
5053
}
5054

5055
static tree_t p_expression_with_head(tree_t head)
271,462✔
5056
{
5057
   // relation { and relation } | relation { or relation }
5058
   //   | relation { xor relation } | relation [ nand relation ]
5059
   //   | relation [ nor relation ] | relation { xnor relation }
5060
   //   | 2008: condition_operator primary
5061
   //   | PSL: relation { union relation }
5062

5063
   BEGIN("expression");
271,462✔
5064

5065
   tree_t expr = p_relation(head);
271,462✔
5066

5067
   for (;;) {
279,187✔
5068
      const token_t tok = peek();
279,187✔
5069
      if (tok == tUNION) {
279,187✔
5070
         psl_node_t un = p_psl_union(expr);
12✔
5071
         tree_t new = tree_new(T_PSL_UNION);
12✔
5072
         tree_set_psl(new, un);
12✔
5073
         tree_set_loc(new, CURRENT_LOC);
12✔
5074
         expr = new;
12✔
5075
      }
5076
      else if (tok == tNOR || tok == tNAND) {
279,175✔
5077
         tree_t new = tree_new(T_FCALL);
256✔
5078
         tree_set_ident(new, p_logical_operator());
256✔
5079
         binary_op(new, expr, p_relation);
256✔
5080
         expr = new;
256✔
5081
         break;
256✔
5082
      }
5083
      else if (tok == tAND || tok == tOR || tok == tXOR || tok == tXNOR) {
278,919✔
5084
         tree_t new = tree_new(T_FCALL);
7,713✔
5085
         tree_set_ident(new, p_logical_operator());
7,713✔
5086
         binary_op(new, expr, p_relation);
7,713✔
5087
         expr = new;
7,713✔
5088
      }
5089
      else
5090
         break;
5091
   }
5092

5093
   return expr;
271,462✔
5094
}
5095

5096
static inline tree_t p_expression(void)
271,450✔
5097
{
5098
   // expression | 2008: condition_operator primary
5099

5100
   if (optional(tCCONV)) {
271,450✔
5101
      require_std(STD_08, "condition conversion");
2✔
5102

5103
      tree_t expr = tree_new(T_FCALL);
2✔
5104
      tree_set_ident(expr, well_known(W_OP_CCONV));
2✔
5105
      unary_op(expr, p_primary);
2✔
5106
      return expr;
2✔
5107
   }
5108
   else
5109
      return p_expression_with_head(NULL);
271,448✔
5110
}
5111

5112
static type_t p_interface_type_indication(tree_t parent)
33,404✔
5113
{
5114
   // subtype_indication | anonymous_type_indication
5115

5116
   BEGIN("interface type indication");
66,808✔
5117

5118
   if (peek() == tTYPE) {
33,404✔
5119
      require_std(STD_19, "anonymous type indication");
3✔
5120

5121
      type_t type = p_anonymous_type_indication();
3✔
5122

5123
      tree_t g = tree_new(T_GENERIC_DECL);
3✔
5124
      tree_set_loc(g, CURRENT_LOC);
3✔
5125
      tree_set_ident(g, ident_uniq("anonymous"));
3✔
5126
      tree_set_subkind(g, PORT_IN);
3✔
5127
      tree_set_class(g, C_TYPE);
3✔
5128
      tree_set_type(g, type);
3✔
5129

5130
      tree_add_generic(parent, g);
3✔
5131

5132
      return type;
3✔
5133
   }
5134
   else
5135
      return p_subtype_indication();
33,401✔
5136
}
5137

5138
static void p_interface_constant_declaration(tree_t parent, tree_kind_t kind,
27,802✔
5139
                                             bool ordered)
5140
{
5141
   // [ constant ] identifier_list : [ in ] subtype_indication [ := expression ]
5142

5143
   BEGIN("interface constant declaration");
55,604✔
5144

5145
   const bool explicit_constant = optional(tCONSTANT);
27,802✔
5146
   tree_flags_t flags = (explicit_constant) ? TREE_F_EXPLICIT_CLASS : 0;
27,802✔
5147

5148
   LOCAL_IDENT_LIST ids = p_identifier_list();
55,604✔
5149

5150
   consume(tCOLON);
27,802✔
5151

5152
   // The grammar only allows IN here but we are more leniant to avoid
5153
   // having disambiguate constant and variable interface declarations
5154
   // See LRM 93 section 2.1.1 for default class
5155
   port_mode_t mode = PORT_IN;
27,802✔
5156
   if (scan(tIN, tOUT, tINOUT, tBUFFER, tLINKAGE)) {
27,802✔
5157
      flags |= TREE_F_EXPLICIT_MODE;
8,428✔
5158
      mode = p_mode();
8,428✔
5159
   }
5160

5161
   class_t class = C_CONSTANT;
27,802✔
5162
   if ((mode == PORT_OUT || mode == PORT_INOUT) && !explicit_constant)
27,802✔
5163
      class = C_VARIABLE;
1,787✔
5164

5165
   if (kind == T_GENERIC_DECL) {
27,802✔
5166
      // In the 2008 standard generics in packages or subprograms are
5167
      // locally static if they have a locally static subtype
5168
      switch (tree_kind(parent)) {
1,792✔
5169
      case T_PACKAGE:
264✔
5170
      case T_FUNC_DECL:
5171
      case T_PROC_DECL:
5172
         flags |= TREE_F_LOCALLY_STATIC;
264✔
5173
         break;
264✔
5174
      default:
5175
         break;
5176
      }
5177
   }
5178

5179
   type_t type = p_interface_type_indication(parent);
27,802✔
5180

5181
   tree_t init = NULL;
27,802✔
5182
   if (optional(tWALRUS)) {
27,802✔
5183
      init = p_expression();
5,082✔
5184
      solve_types(nametab, init, type);
5,082✔
5185
   }
5186

5187
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
58,415✔
5188
      tree_t d = tree_new(kind);
30,613✔
5189
      tree_set_ident(d, it->ident);
30,613✔
5190
      tree_set_loc(d, &(it->loc));
30,613✔
5191
      tree_set_subkind(d, mode);
30,613✔
5192
      tree_set_type(d, type);
30,613✔
5193
      tree_set_class(d, class);
30,613✔
5194
      tree_set_flag(d, flags);
30,613✔
5195

5196
      if (init != NULL)
30,613✔
5197
         tree_set_value(d, init);
5,198✔
5198

5199
      add_interface(parent, d, kind);
30,613✔
5200
      sem_check(d, nametab);
30,613✔
5201

5202
      if (ordered)
30,613✔
5203
         insert_name(nametab, d, NULL);
7,532✔
5204

5205
      flags |= TREE_F_CONTINUATION;
30,613✔
5206
   }
5207
}
27,802✔
5208

5209
static void p_array_mode_view_indication(type_t *type, tree_t *name)
17✔
5210
{
5211
   // view ( name ) of subtype_indication
5212

5213
   BEGIN("array mode view indication");
17✔
5214

5215
   consume(tVIEW);
17✔
5216
   consume(tLPAREN);
17✔
5217

5218
   *name = p_name(0);
17✔
5219
   solve_types(nametab, *name, NULL);
17✔
5220

5221
   consume(tRPAREN);
17✔
5222
   consume(tOF);
17✔
5223

5224
   *type = p_subtype_indication();
17✔
5225
}
17✔
5226

5227
static void p_record_mode_view_indication(type_t *type, tree_t *name)
85✔
5228
{
5229
   // view name [ of subtype_indication ]
5230

5231
   BEGIN("record mode view indication");
170✔
5232

5233
   consume(tVIEW);
85✔
5234

5235
   *name = p_name(0);
85✔
5236
   type_t name_type = solve_types(nametab, *name, NULL);
85✔
5237

5238
   if (optional(tOF))
85✔
5239
      *type = p_subtype_indication();
12✔
5240
   else if (type_kind(name_type) != T_VIEW) {
73✔
5241
      parse_error(tree_loc(*name), "name in mode view indication does not "
1✔
5242
                  "denote a mode view");
5243
      *type = type_new(T_NONE);
1✔
5244
   }
5245
   else
5246
      *type = type_designated(name_type);
72✔
5247
}
85✔
5248

5249
static port_mode_t p_mode_view_indication(type_t *type, tree_t *name)
102✔
5250
{
5251
   // record_mode_view_indication | array_mode_view_indication
5252

5253
   BEGIN("mode view indication");
204✔
5254

5255
   if (peek_nth(2) == tLPAREN) {
102✔
5256
      p_array_mode_view_indication(type, name);
17✔
5257
      return PORT_ARRAY_VIEW;
17✔
5258
   }
5259
   else {
5260
      p_record_mode_view_indication(type, name);
85✔
5261
      return PORT_RECORD_VIEW;
85✔
5262
   }
5263
}
5264

5265
static void p_interface_signal_declaration(tree_t parent, tree_kind_t kind,
4,885✔
5266
                                           bool ordered)
5267
{
5268
   // [signal] identifier_list : [ mode ] subtype_indication [ bus ]
5269
   //    [ := expression ]
5270
   // 2019: [ signal ] identifier_list : mode_indication
5271

5272
   BEGIN("interface signal declaration");
9,770✔
5273

5274
   tree_flags_t flags = optional(tSIGNAL) ? TREE_F_EXPLICIT_CLASS : 0;
4,885✔
5275

5276
   LOCAL_IDENT_LIST ids = p_identifier_list();
9,770✔
5277
   consume(tCOLON);
4,885✔
5278

5279
   type_t type = NULL;
4,885✔
5280
   tree_t init = NULL;
4,885✔
5281
   port_mode_t mode = PORT_IN;
4,885✔
5282

5283
   if (peek() == tVIEW) {
4,885✔
5284
      require_std(STD_19, "mode view indication");
102✔
5285
      mode = p_mode_view_indication(&type, &init);
102✔
5286
   }
5287
   else {
5288
      if (scan(tIN, tOUT, tINOUT, tBUFFER, tLINKAGE)) {
4,783✔
5289
         mode = p_mode();
4,434✔
5290
         flags |= TREE_F_EXPLICIT_MODE;
4,434✔
5291
      }
5292

5293
      type = p_interface_type_indication(parent);
4,783✔
5294

5295
      if (optional(tBUS))
4,783✔
5296
         flags |= TREE_F_BUS;
5✔
5297

5298
      if (optional(tWALRUS)) {
4,783✔
5299
         init = p_expression();
338✔
5300
         solve_types(nametab, init, type);
338✔
5301
      }
5302
   }
5303

5304
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
10,049✔
5305
      tree_t d = tree_new(kind);
5,164✔
5306
      tree_set_ident(d, it->ident);
5,164✔
5307
      tree_set_loc(d, &(it->loc));
5,164✔
5308
      tree_set_subkind(d, mode);
5,164✔
5309
      tree_set_type(d, type);
5,164✔
5310
      tree_set_class(d, C_SIGNAL);
5,164✔
5311
      tree_set_flag(d, flags);
5,164✔
5312

5313
      if (init != NULL)
5,164✔
5314
         tree_set_value(d, init);
442✔
5315

5316
      add_interface(parent, d, kind);
5,164✔
5317
      sem_check(d, nametab);
5,164✔
5318

5319
      if (ordered)
5,164✔
5320
         insert_name(nametab, d, NULL);
159✔
5321

5322
      flags |= TREE_F_CONTINUATION;
5,164✔
5323
   }
5324
}
4,885✔
5325

5326
static void p_interface_variable_declaration(tree_t parent, tree_kind_t kind)
819✔
5327
{
5328
   // [variable] identifier_list : [ mode ] subtype_indication [ := expression ]
5329

5330
   BEGIN("interface variable declaration");
1,638✔
5331

5332
   tree_flags_t flags = optional(tVARIABLE) ? TREE_F_EXPLICIT_CLASS : 0;
819✔
5333

5334
   LOCAL_IDENT_LIST ids = p_identifier_list();
1,638✔
5335
   consume(tCOLON);
819✔
5336

5337
   port_mode_t mode = PORT_IN;
819✔
5338
   if (scan(tIN, tOUT, tINOUT, tBUFFER, tLINKAGE)) {
819✔
5339
      mode = p_mode();
802✔
5340
      flags |= TREE_F_EXPLICIT_MODE;
802✔
5341
   }
5342

5343
   type_t type = p_interface_type_indication(parent);
819✔
5344

5345
   tree_t init = NULL;
819✔
5346
   if (optional(tWALRUS)) {
819✔
5347
      init = p_expression();
6✔
5348
      solve_types(nametab, init, type);
6✔
5349
   }
5350

5351
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
1,644✔
5352
      tree_t d = tree_new(kind);
825✔
5353
      tree_set_ident(d, it->ident);
825✔
5354
      tree_set_loc(d, &(it->loc));
825✔
5355
      tree_set_type(d, type);
825✔
5356
      tree_set_class(d, C_VARIABLE);
825✔
5357
      tree_set_subkind(d, mode);
825✔
5358
      tree_set_flag(d, flags);
825✔
5359

5360
      if (init != NULL)
825✔
5361
         tree_set_value(d, init);
6✔
5362

5363
      add_interface(parent, d, kind);
825✔
5364
      sem_check(d, nametab);
825✔
5365

5366
      if (standard() >= STD_19)
825✔
5367
         insert_name(nametab, d, NULL);
28✔
5368

5369
      flags |= TREE_F_CONTINUATION;
825✔
5370
   }
5371
}
819✔
5372

5373
static void p_interface_file_declaration(tree_t parent, tree_kind_t kind)
35✔
5374
{
5375
   // file identifier_list : subtype_indication
5376

5377
   BEGIN("interface file declaration");
70✔
5378

5379
   consume(tFILE);
35✔
5380

5381
   LOCAL_IDENT_LIST ids = p_identifier_list();
70✔
5382

5383
   consume(tCOLON);
35✔
5384

5385
   type_t type = p_subtype_indication();
35✔
5386

5387
   tree_flags_t flags = 0;
35✔
5388
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
74✔
5389
      tree_t d = tree_new(kind);
39✔
5390
      tree_set_ident(d, it->ident);
39✔
5391
      tree_set_loc(d, &(it->loc));
39✔
5392
      tree_set_subkind(d, PORT_IN);
39✔
5393
      tree_set_flag(d, flags);
39✔
5394
      tree_set_type(d, type);
39✔
5395
      tree_set_class(d, C_FILE);
39✔
5396

5397
      add_interface(parent, d, kind);
39✔
5398
      sem_check(d, nametab);
39✔
5399

5400
      if (standard() >= STD_19)
39✔
5401
         insert_name(nametab, d, NULL);
15✔
5402

5403
      flags |= TREE_F_CONTINUATION;
39✔
5404
   }
5405
}
35✔
5406

5407
static void p_private_incomplete_type_definition(type_t type)
24✔
5408
{
5409
   // 2019: private
5410

5411
   BEGIN("private incomplete type definition");
48✔
5412

5413
   consume(tPRIVATE);
24✔
5414

5415
   type_set_subkind(type, GTYPE_PRIVATE);
24✔
5416
}
24✔
5417

5418
static void p_scalar_incomplete_type_definition(type_t type)
5✔
5419
{
5420
   // 2019: <>
5421

5422
   BEGIN("scalar incomplete type definition");
10✔
5423

5424
   consume(tBOX);
5✔
5425

5426
   type_set_subkind(type, GTYPE_SCALAR);
5✔
5427
}
5✔
5428

5429
static void p_discrete_incomplete_type_definition(type_t type)
19✔
5430
{
5431
   // 2019: ( <> )
5432

5433
   BEGIN("discrete incomplete type definition");
38✔
5434

5435
   consume(tLPAREN);
19✔
5436
   consume(tBOX);
19✔
5437
   consume(tRPAREN);
19✔
5438

5439
   type_set_subkind(type, GTYPE_DISCRETE);
19✔
5440
}
19✔
5441

5442
static void p_integer_incomplete_type_definition(type_t type)
11✔
5443
{
5444
   // 2019: range <>
5445

5446
   BEGIN("integer incomplete type definition");
22✔
5447

5448
   consume(tRANGE);
11✔
5449
   consume(tBOX);
11✔
5450

5451
   type_set_subkind(type, GTYPE_INTEGER);
11✔
5452
}
11✔
5453

5454
static void p_physical_incomplete_type_definition(type_t type)
4✔
5455
{
5456
   // 2019: units <>
5457

5458
   BEGIN("physical incomplete type definition");
8✔
5459

5460
   consume(tUNITS);
4✔
5461
   consume(tBOX);
4✔
5462

5463
   type_set_subkind(type, GTYPE_PHYSICAL);
4✔
5464
}
4✔
5465

5466
static void p_floating_incomplete_type_definition(type_t type)
4✔
5467
{
5468
   // 2019: range <> . <>
5469

5470
   BEGIN("floating incomplete type definition");
8✔
5471

5472
   consume(tRANGE);
4✔
5473
   consume(tBOX);
4✔
5474
   consume(tDOT);
4✔
5475
   consume(tBOX);
4✔
5476

5477
   type_set_subkind(type, GTYPE_FLOATING);
4✔
5478
}
4✔
5479

5480
static type_t p_array_index_incomplete_type(void)
21✔
5481
{
5482
   // index_subtype_definition | index_constraint | anonymous_type_indication
5483

5484
   BEGIN("array index incomplete type");
42✔
5485

5486
   if (peek() == tTYPE)
21✔
5487
      return p_anonymous_type_indication();
9✔
5488
   else
5489
      return p_index_subtype_definition(NULL);
12✔
5490
}
5491

5492
static void p_array_index_incomplete_type_list(type_t type)
21✔
5493
{
5494
   // array_index_incomplete_type { , array_index_incomplete_type }
5495

5496
   BEGIN("array index incomplete type list");
42✔
5497

5498
   do {
21✔
5499
      type_add_index(type, p_array_index_incomplete_type());
21✔
5500
   } while (optional(tCOMMA));
21✔
5501
}
21✔
5502

5503
static type_t p_incomplete_subtype_indication(void)
32✔
5504
{
5505
   // subtype_indication | anonymous_type_indication
5506

5507
   BEGIN("incomplete subtype indication");
64✔
5508

5509
   if (peek() == tTYPE)
32✔
5510
      return p_anonymous_type_indication();
18✔
5511
   else
5512
      return p_subtype_indication();
14✔
5513
}
5514

5515
static void p_array_incomplete_type_definition(type_t type)
21✔
5516
{
5517
   // 2019: array ( array_index_incomplete_type_list )
5518
   //   of element_incomplete_subtype_indication
5519

5520
   BEGIN("array incomplete type definition");
42✔
5521

5522
   consume(tARRAY);
21✔
5523
   consume(tLPAREN);
21✔
5524

5525
   p_array_index_incomplete_type_list(type);
21✔
5526

5527
   consume(tRPAREN);
21✔
5528
   consume(tOF);
21✔
5529

5530
   type_set_elem(type, p_incomplete_subtype_indication());
21✔
5531

5532
   type_set_subkind(type, GTYPE_ARRAY);
21✔
5533
}
21✔
5534

5535
static void p_access_incomplete_type_definition(type_t type)
5✔
5536
{
5537
   // 2019: access incomplete_subtype_indication
5538

5539
   BEGIN("access incomplete type definition");
10✔
5540

5541
   consume(tACCESS);
5✔
5542

5543
   type_set_designated(type, p_incomplete_subtype_indication());
5✔
5544

5545
   type_set_subkind(type, GTYPE_ACCESS);
5✔
5546
}
5✔
5547

5548
static void p_file_incomplete_type_definition(type_t type)
6✔
5549
{
5550
   // 2019: file of incomplete_subtype_indication
5551

5552
   BEGIN("file incomplete type definition");
12✔
5553

5554
   consume(tFILE);
6✔
5555
   consume(tOF);
6✔
5556

5557
   type_set_designated(type, p_incomplete_subtype_indication());
6✔
5558

5559
   type_set_subkind(type, GTYPE_FILE);
6✔
5560
}
6✔
5561

5562
static void p_incomplete_type_definition(type_t type)
99✔
5563
{
5564
   // private_incomplete_type_definition
5565
   //   | scalar_incomplete_type_definition
5566
   //   | discrete_incomplete_type_definition
5567
   //   | integer_incomplete_type_definition
5568
   //   | physical_incomplete_type_definition
5569
   //   | floating_incomplete_type_definition
5570
   //   | array_incomplete_type_definition
5571
   //   | access_incomplete_type_definition
5572
   //   | file_incomplete_type_definition
5573

5574
   BEGIN("incomplete type definition");
198✔
5575

5576
   switch (peek()) {
99✔
5577
   case tPRIVATE:
24✔
5578
      p_private_incomplete_type_definition(type);
24✔
5579
      break;
24✔
5580
   case tBOX:
5✔
5581
      p_scalar_incomplete_type_definition(type);
5✔
5582
      break;
5✔
5583
   case tLPAREN:
19✔
5584
      p_discrete_incomplete_type_definition(type);
19✔
5585
      break;
19✔
5586
   case tRANGE:
15✔
5587
      if (peek_nth(3) == tDOT)
15✔
5588
         p_floating_incomplete_type_definition(type);
4✔
5589
      else
5590
         p_integer_incomplete_type_definition(type);
11✔
5591
      break;
5592
   case tUNITS:
4✔
5593
      p_physical_incomplete_type_definition(type);
4✔
5594
      break;
4✔
5595
   case tARRAY:
21✔
5596
      p_array_incomplete_type_definition(type);
21✔
5597
      break;
21✔
5598
   case tACCESS:
5✔
5599
      p_access_incomplete_type_definition(type);
5✔
5600
      break;
5✔
5601
   case tFILE:
6✔
5602
      p_file_incomplete_type_definition(type);
6✔
5603
      break;
6✔
5604
   default:
×
5605
      one_of(tPRIVATE, tBOX, tLPAREN, tRANGE, tUNITS, tARRAY, tACCESS, tFILE);
×
5606
   }
5607

5608
   require_std(STD_19, "incomplete type definition");
99✔
5609
}
99✔
5610

5611
static type_t p_anonymous_type_indication(void)
30✔
5612
{
5613
   // type is incomplete_type_definition
5614

5615
   BEGIN("anonymous type indication");
30✔
5616

5617
   consume(tTYPE);
30✔
5618
   consume(tIS);
30✔
5619

5620
   type_t type = type_new(T_GENERIC);
30✔
5621
   p_incomplete_type_definition(type);
30✔
5622

5623
   return type;
30✔
5624
}
5625

5626
static void p_interface_type_declaration(tree_t parent, tree_kind_t kind)
244✔
5627
{
5628
   // 2008: type identifier
5629
   // 2019: type identifier [ is incomplete_type_definition ]
5630

5631
   BEGIN("interface type declaration");
488✔
5632

5633
   consume(tTYPE);
244✔
5634

5635
   ident_t id = p_identifier();
244✔
5636

5637
   require_std(STD_08, "interface type declarations");
244✔
5638

5639
   type_t type = type_new(T_GENERIC);
244✔
5640
   type_set_ident(type, id);
244✔
5641

5642
   if (optional(tIS))
244✔
5643
      p_incomplete_type_definition(type);
69✔
5644
   else
5645
      type_set_subkind(type, GTYPE_PRIVATE);
175✔
5646

5647
   tree_t d = tree_new(kind);
244✔
5648
   tree_set_ident(d, id);
244✔
5649
   tree_set_loc(d, CURRENT_LOC);
244✔
5650
   tree_set_type(d, type);
244✔
5651
   tree_set_class(d, C_TYPE);
244✔
5652
   tree_set_subkind(d, PORT_IN);
244✔
5653

5654
   add_interface(parent, d, kind);
244✔
5655
   sem_check(d, nametab);
244✔
5656

5657
   // Type generics are immediately visible
5658
   insert_name(nametab, d, NULL);
244✔
5659

5660
   // LRM 08 section 6.5.3: the predefined equality and inequality
5661
   // operators are implicitly declared as formal generic subprograms
5662
   // immediately following the interface type declaration in the
5663
   // enclosing interface list
5664

5665
   if (kind == T_GENERIC_DECL)
244✔
5666
      declare_generic_ops(parent, type);
242✔
5667
}
244✔
5668

5669
static void p_formal_parameter_list(tree_t decl, type_t type)
15,278✔
5670
{
5671
   // interface_list
5672

5673
   BEGIN("formal parameter list");
30,555✔
5674

5675
   p_interface_list(decl, T_PARAM_DECL, standard() >= STD_19);
15,278✔
5676

5677
   const int nports = tree_ports(decl);
15,278✔
5678
   if (nports == 0)
15,278✔
5679
      return;   // Was parse error
1✔
5680

5681
   for (int i = 0; i < nports; i++) {
46,137✔
5682
      tree_t p = tree_port(decl, i);
30,860✔
5683
      if (i == 0 && tree_has_value(p))
30,860✔
5684
         tree_set_flag(decl, TREE_F_CALL_NO_ARGS);
281✔
5685
      if (tree_has_type(p))
30,860✔
5686
         type_add_param(type, tree_type(p));
30,859✔
5687
      else
5688
         type_add_param(type, type_new(T_NONE));   // Will raise error later
1✔
5689
   }
5690
}
5691

5692
static tree_t p_interface_function_specification(void)
181✔
5693
{
5694
   // [ pure | impure ] function designator
5695
   //    [ [ parameter ] ( formal_parameter_list ) ] return type_mark
5696

5697
   // 2019:
5698
   // [ pure | impure ] function designator
5699
   //    [ [ parameter ] ( formal_parameter_list ) ]
5700
   //    return [ return_identifier of ] type_mark
5701

5702
   BEGIN("interface function specification");
181✔
5703

5704
   bool impure = false;
181✔
5705
   switch (peek()) {
181✔
5706
   case tPURE: consume(tPURE); break;
3✔
5707
   case tIMPURE: consume(tIMPURE); impure = true; break;
2✔
5708
   default: break;
5709
   }
5710

5711
   consume(tFUNCTION);
181✔
5712

5713
   ident_t id = p_designator();
181✔
5714

5715
   type_t type = type_new(T_SIGNATURE);
181✔
5716
   type_set_ident(type, id);
181✔
5717

5718
   tree_t d = tree_new(T_GENERIC_DECL);
181✔
5719
   tree_set_class(d, C_FUNCTION);
181✔
5720
   tree_set_ident(d, id);
181✔
5721
   tree_set_type(d, type);
181✔
5722
   tree_set_subkind(d, PORT_IN);
181✔
5723

5724
   if (impure)
181✔
5725
      tree_set_flag(d, TREE_F_IMPURE);
2✔
5726

5727
   if (optional(tLPAREN)) {
181✔
5728
      push_scope(nametab);
176✔
5729
      p_formal_parameter_list(d, type);
176✔
5730
      consume(tRPAREN);
176✔
5731
      pop_scope(nametab);
176✔
5732
   }
5733
   else
5734
      tree_set_flag(d, TREE_F_CALL_NO_ARGS);
5✔
5735

5736
   consume(tRETURN);
181✔
5737

5738
   if (peek_nth(2) != tOF)
181✔
5739
      type_set_result(type, p_type_mark());
181✔
5740
   else {
5741
      require_std(STD_19, "function knows return type");
×
5742
      ident_t id = p_identifier();
×
5743

5744
      consume(tOF);
×
5745

5746
      type_t sub = type_new(T_SUBTYPE);
×
5747
      type_set_ident(sub, id);
×
5748
      type_set_base(sub, p_type_mark());
×
5749

5750
      type_set_result(type, sub);
×
5751
   }
5752

5753
   tree_set_loc(d, CURRENT_LOC);
181✔
5754
   return d;
181✔
5755
}
5756

5757
static tree_t p_interface_procedure_specification(void)
9✔
5758
{
5759
   // procedure designator [ [ parameter ] ( formal_parameter_list ) ]
5760

5761
   BEGIN("interface procedure specification");
9✔
5762

5763
   consume(tPROCEDURE);
9✔
5764

5765
   ident_t id = p_designator();
9✔
5766

5767
   type_t type = type_new(T_SIGNATURE);
9✔
5768
   type_set_ident(type, id);
9✔
5769

5770
   tree_t d = tree_new(T_GENERIC_DECL);
9✔
5771
   tree_set_class(d, C_PROCEDURE);
9✔
5772
   tree_set_ident(d, id);
9✔
5773
   tree_set_type(d, type);
9✔
5774
   tree_set_subkind(d, PORT_IN);
9✔
5775

5776
   if (optional(tLPAREN)) {
9✔
5777
      push_scope(nametab);
9✔
5778
      p_formal_parameter_list(d, type);
9✔
5779
      consume(tRPAREN);
9✔
5780
      pop_scope(nametab);
9✔
5781
   }
5782
   else
5783
      tree_set_flag(d, TREE_F_CALL_NO_ARGS);
×
5784

5785
   tree_set_loc(d, CURRENT_LOC);
9✔
5786
   return d;
9✔
5787
}
5788

5789
static void p_interface_subprogram_declaration(tree_t parent, tree_kind_t kind)
190✔
5790
{
5791
   // interface_subprogram_specification [ is interface_subprogram_default ]
5792

5793
   BEGIN("interface subprogram declaration");
380✔
5794

5795
   tree_t d = NULL;
190✔
5796

5797
   require_std(STD_08, "interface subprogram declarations");
190✔
5798

5799
   switch (peek()) {
190✔
5800
   case tFUNCTION:
181✔
5801
   case tPURE:
5802
   case tIMPURE:
5803
      d = p_interface_function_specification();
181✔
5804
      break;
181✔
5805

5806
   case tPROCEDURE:
9✔
5807
      d = p_interface_procedure_specification();
9✔
5808
      break;
9✔
5809

5810
   default:
×
5811
      one_of(tFUNCTION, tPROCEDURE, tPURE, tIMPURE);
×
5812
      return;
×
5813
   }
5814

5815
   if (optional(tIS)) {
190✔
5816
      switch (peek()) {
142✔
5817
      case tID:
4✔
5818
         {
5819
            ident_t id = p_identifier();
4✔
5820
            type_t constraint = tree_type(d);
4✔
5821

5822
            tree_t decl = resolve_subprogram_name(nametab, &last_loc,
4✔
5823
                                                  id, constraint);
5824

5825
            tree_t box = tree_new(T_BOX);
4✔
5826
            tree_set_loc(box, &last_loc);
4✔
5827
            tree_set_type(box, constraint);
4✔
5828
            tree_set_ident(box, id);
4✔
5829
            tree_set_ref(box, decl);
4✔
5830

5831
            tree_set_value(d, box);
4✔
5832
         }
5833
         break;
4✔
5834
      case tBOX:
138✔
5835
         {
5836
            consume(tBOX);
138✔
5837

5838
            tree_t box = tree_new(T_BOX);
138✔
5839
            tree_set_loc(box, CURRENT_LOC);
138✔
5840
            tree_set_type(box, tree_type(d));
138✔
5841

5842
            tree_set_value(d, box);
138✔
5843
         }
5844
         break;
138✔
5845
      default:
×
5846
         expect(tID, tBOX);
×
5847
      }
5848
   }
5849

5850
   add_interface(parent, d, kind);
190✔
5851
   sem_check(d, nametab);
190✔
5852

5853
   insert_name(nametab, d, NULL);
190✔
5854
}
5855

5856
static void p_interface_package_generic_map_aspect(tree_t map, tree_t pack)
73✔
5857
{
5858
   // generic_map_aspect | generic map ( <> ) | generic map ( default )
5859

5860
   BEGIN("interface package generic map aspect");
146✔
5861

5862
   consume(tGENERIC);
73✔
5863
   consume(tMAP);
73✔
5864
   consume(tLPAREN);
73✔
5865

5866
   switch (peek()) {
73✔
5867
   case tBOX:
56✔
5868
      consume(tBOX);
56✔
5869
      tree_set_subkind(map, PACKAGE_MAP_BOX);
56✔
5870
      break;
56✔
5871

5872
   case tDEFAULT:
6✔
5873
      consume(tDEFAULT);
6✔
5874
      tree_set_subkind(map, PACKAGE_MAP_DEFAULT);
6✔
5875
      break;
6✔
5876

5877
   default:
11✔
5878
      tree_set_subkind(map, PACKAGE_MAP_MATCHING);
11✔
5879
      p_association_list(map, pack, F_GENERIC_MAP);
11✔
5880
      break;
11✔
5881
   }
5882

5883
   consume(tRPAREN);
73✔
5884

5885
   tree_set_loc(map, CURRENT_LOC);
73✔
5886
}
73✔
5887

5888
static void p_interface_package_declaration(tree_t parent, tree_kind_t kind)
73✔
5889
{
5890
   // package identifier is new uninstantiated_package_name
5891
   //    interface_package_generic_map_aspect
5892

5893
   BEGIN("interface package declaration");
146✔
5894

5895
   consume(tPACKAGE);
73✔
5896

5897
   require_std(STD_08, "interface package declarations");
73✔
5898

5899
   tree_t d = tree_new(T_GENERIC_DECL);
73✔
5900
   tree_set_class(d, C_PACKAGE);
73✔
5901
   tree_set_ident(d, p_identifier());
73✔
5902
   tree_set_subkind(d, PORT_IN);
73✔
5903

5904
   consume(tIS);
73✔
5905
   consume(tNEW);
73✔
5906

5907
   ident_t unit_name = p_selected_identifier();
73✔
5908

5909
   tree_t pack = resolve_name(nametab, CURRENT_LOC, unit_name);
73✔
5910
   if (pack != NULL && !is_uninstantiated_package(pack)) {
73✔
5911
      parse_error(CURRENT_LOC, "unit %s is not an uninstantiated package",
1✔
5912
                  istr(unit_name));
5913
      pack = NULL;
5914
   }
5915

5916
   tree_t map = tree_new(T_PACKAGE_MAP);
73✔
5917
   tree_set_ident(map, unit_name);
73✔
5918
   tree_set_loc(map, CURRENT_LOC);
73✔
5919
   tree_set_ref(map, pack);
73✔
5920

5921
   tree_set_value(d, map);
73✔
5922

5923
   p_interface_package_generic_map_aspect(map, pack);
73✔
5924

5925
   tree_set_loc(d, CURRENT_LOC);
73✔
5926

5927
   add_interface(parent, d, kind);
73✔
5928
   sem_check(d, nametab);
73✔
5929

5930
   insert_name(nametab, d, NULL);
73✔
5931
}
73✔
5932

5933
static void p_interface_declaration(tree_t parent, tree_kind_t kind,
34,049✔
5934
                                    bool ordered)
5935
{
5936
   // interface_constant_declaration | interface_signal_declaration
5937
   //   | interface_variable_declaration | interface_file_declaration
5938
   //   | 2008: interface_type_declaration
5939
   //   | 2008: interface_subprogram_declaration
5940
   //   | 2008: interface_package_declaration
5941

5942
   BEGIN("interface declaration");
68,098✔
5943

5944
   switch (peek()) {
34,049✔
5945
   case tCONSTANT:
5,212✔
5946
      p_interface_constant_declaration(parent, kind, ordered);
5,212✔
5947
      break;
5,212✔
5948

5949
   case tSIGNAL:
1,140✔
5950
      p_interface_signal_declaration(parent, kind, ordered);
1,140✔
5951
      break;
1,140✔
5952

5953
   case tVARIABLE:
819✔
5954
      p_interface_variable_declaration(parent, kind);
819✔
5955
      break;
819✔
5956

5957
   case tFILE:
35✔
5958
      p_interface_file_declaration(parent, kind);
35✔
5959
      break;
35✔
5960

5961
   case tTYPE:
244✔
5962
      p_interface_type_declaration(parent, kind);
244✔
5963
      break;
244✔
5964

5965
   case tFUNCTION:
190✔
5966
   case tPROCEDURE:
5967
   case tPURE:
5968
   case tIMPURE:
5969
      p_interface_subprogram_declaration(parent, kind);
190✔
5970
      break;
190✔
5971

5972
   case tPACKAGE:
73✔
5973
      p_interface_package_declaration(parent, kind);
73✔
5974
      break;
73✔
5975

5976
   case tID:
26,335✔
5977
      if (kind == T_PORT_DECL)
26,335✔
5978
         p_interface_signal_declaration(parent, kind, ordered);
3,745✔
5979
      else
5980
         p_interface_constant_declaration(parent, kind, ordered);
22,590✔
5981
      break;
5982

5983
   default:
1✔
5984
      expect(tCONSTANT, tSIGNAL, tVARIABLE, tFILE, tID, tTYPE,
1✔
5985
             STD(08, tFUNCTION), tPROCEDURE, tPURE, tIMPURE, tPACKAGE);
5986
   }
5987
}
34,049✔
5988

5989
static void p_interface_element(tree_t parent, tree_kind_t kind, bool ordered)
34,049✔
5990
{
5991
   // interface_declaration
5992

5993
   BEGIN("interface element");
68,098✔
5994

5995
   p_interface_declaration(parent, kind, ordered);
34,049✔
5996
}
34,049✔
5997

5998
static void p_interface_list(tree_t parent, tree_kind_t kind, bool ordered)
17,708✔
5999
{
6000
   // interface_element { ; interface_element }
6001

6002
   BEGIN("interface list");
35,415✔
6003

6004
   if (peek() == tRPAREN) {
17,708✔
6005
      parse_error(&last_loc, "interface list cannot be empty");
1✔
6006
      return;
1✔
6007
   }
6008

6009
   if (ordered)
17,707✔
6010
      push_scope(nametab);
4,107✔
6011

6012
   p_interface_element(parent, kind, ordered);
17,707✔
6013

6014
   while (optional(tSEMI)) {
34,049✔
6015
      if (peek() == tRPAREN) {
16,357✔
6016
         require_std(STD_19, "optional trailing semicolons on interface lists");
15✔
6017
         break;
15✔
6018
      }
6019
      p_interface_element(parent, kind, ordered);
16,342✔
6020
   }
6021

6022
   if (ordered)
17,707✔
6023
      pop_scope(nametab);
4,107✔
6024
}
6025

6026
static void p_port_list(tree_t parent)
1,347✔
6027
{
6028
   // port_list ::= interface_list
6029

6030
   BEGIN("port list");
2,694✔
6031

6032
   p_interface_list(parent, T_PORT_DECL, standard() >= STD_19);
1,347✔
6033
}
1,347✔
6034

6035
static void p_port_clause(tree_t parent)
1,347✔
6036
{
6037
   // port ( port_list ) ;
6038

6039
   BEGIN("port clause");
2,694✔
6040

6041
   consume(tPORT);
1,347✔
6042
   consume(tLPAREN);
1,347✔
6043

6044
   p_port_list(parent);
1,347✔
6045

6046
   consume(tRPAREN);
1,347✔
6047
   consume(tSEMI);
1,347✔
6048
}
1,347✔
6049

6050
static void p_generic_list(tree_t parent)
1,083✔
6051
{
6052
   // generic_list ::= interface_list
6053

6054
   BEGIN("generic list");
2,166✔
6055

6056
   p_interface_list(parent, T_GENERIC_DECL, standard() >= STD_08);
1,083✔
6057
}
1,083✔
6058

6059
static void p_generic_clause(tree_t parent)
992✔
6060
{
6061
   // generic ( generic_list ) ;
6062

6063
   BEGIN("generic clause");
1,984✔
6064

6065
   consume(tGENERIC);
992✔
6066
   consume(tLPAREN);
992✔
6067

6068
   p_generic_list(parent);
992✔
6069

6070
   consume(tRPAREN);
992✔
6071
   consume(tSEMI);
992✔
6072
}
992✔
6073

6074
static void p_entity_header(tree_t entity)
5,201✔
6075
{
6076
   // [ generic_clause ] [ port_clause ]
6077

6078
   BEGIN("entity header");
10,402✔
6079

6080
   if (scan(tGENERIC)) {
5,201✔
6081
      p_generic_clause(entity);
561✔
6082
      insert_generics(nametab, entity);
561✔
6083
   }
6084

6085
   if (scan(tPORT)) {
5,201✔
6086
      p_port_clause(entity);
900✔
6087
      insert_ports(nametab, entity);
900✔
6088
   }
6089
}
5,201✔
6090

6091
static tree_t p_attribute_declaration(void)
118✔
6092
{
6093
   // attribute identifier : type_mark ;
6094

6095
   BEGIN("attribute declaration");
118✔
6096

6097
   tree_t t = tree_new(T_ATTR_DECL);
118✔
6098

6099
   consume(tATTRIBUTE);
118✔
6100
   tree_set_ident(t, p_identifier());
118✔
6101
   consume(tCOLON);
118✔
6102
   tree_set_type(t, p_type_mark());
118✔
6103
   consume(tSEMI);
118✔
6104

6105
   tree_set_loc(t, CURRENT_LOC);
118✔
6106
   insert_name(nametab, t, NULL);
118✔
6107
   sem_check(t, nametab);
118✔
6108
   return t;
118✔
6109
}
6110

6111
static class_t p_entity_class(void)
353✔
6112
{
6113
   // entity | procedure | type | signal | label | group | architecture
6114
   //   | function | subtype | variable | literal | file | configuration
6115
   //   | package | constant | component | units
6116

6117
   BEGIN("entity class");
706✔
6118

6119
   switch (one_of(tENTITY, tPROCEDURE, tTYPE, tSIGNAL, tLABEL, tGROUP,
353✔
6120
                  tARCHITECTURE, tFUNCTION, tSUBTYPE, tVARIABLE, tLITERAL,
6121
                  tFILE, tCONFIGURATION, tPACKAGE, tCONSTANT, tCOMPONENT,
6122
                  tUNITS)) {
6123
   case tENTITY:
6124
      return C_ENTITY;
6125
   case tPROCEDURE:
6126
      return C_PROCEDURE;
6127
   case tTYPE:
6128
      return C_TYPE;
6129
   case tSIGNAL:
6130
      return C_SIGNAL;
6131
   case tLABEL:
6132
      return C_LABEL;
6133
   case tGROUP:
6134
      return C_DEFAULT;
6135
   case tARCHITECTURE:
6136
      return C_ARCHITECTURE;
6137
   case tFUNCTION:
6138
      return C_FUNCTION;
6139
   case tSUBTYPE:
6140
      return C_SUBTYPE;
6141
   case tVARIABLE:
6142
      return C_VARIABLE;
6143
   case tLITERAL:
6144
      return C_LITERAL;
6145
   case tFILE:
6146
      return C_FILE;
6147
   case tCONFIGURATION:
6148
      return C_CONFIGURATION;
6149
   case tPACKAGE:
6150
      return C_PACKAGE;
6151
   case tCONSTANT:
6152
      return C_CONSTANT;
6153
   case tCOMPONENT:
6154
      return C_COMPONENT;
6155
   case tUNITS:
6156
      return C_UNITS;
6157
   default:
6158
      return C_DEFAULT;
6159
   }
6160
}
6161

6162
static tree_t p_entity_designator(void)
348✔
6163
{
6164
   // entity_tag [ signature ]
6165

6166
   BEGIN("entity designator");
348✔
6167

6168
   ident_t id = p_designator();
348✔
6169

6170
   tree_t t = tree_new(T_ATTR_SPEC);
348✔
6171
   tree_set_loc(t, CURRENT_LOC);
348✔
6172
   tree_set_subkind(t, SPEC_EXACT);
348✔
6173
   tree_set_ident2(t, id);
348✔
6174

6175
   if (peek() == tLSQUARE) {
348✔
6176
      type_t signature = p_signature();
151✔
6177

6178
      tree_t d = resolve_subprogram_name(nametab, tree_loc(t), id, signature);
151✔
6179
      tree_set_ref(t, d);
151✔
6180
   }
6181

6182
   return t;
348✔
6183
}
6184

6185
static void p_entity_name_list(tree_list_t *list)
351✔
6186
{
6187
   // entity_designator { , entity_designator } | others | all
6188

6189
   BEGIN("entity name list");
702✔
6190

6191
   switch (peek()) {
351✔
6192
   case tOTHERS:
1✔
6193
      {
6194
         consume(tOTHERS);
1✔
6195

6196
         tree_t t = tree_new(T_ATTR_SPEC);
1✔
6197
         tree_set_loc(t, CURRENT_LOC);
1✔
6198
         tree_set_subkind(t, SPEC_OTHERS);
1✔
6199

6200
         APUSH(*list, t);
1✔
6201
      }
6202
      break;
1✔
6203
   case tALL:
2✔
6204
      {
6205
         consume(tALL);
2✔
6206

6207
         tree_t t = tree_new(T_ATTR_SPEC);
2✔
6208
         tree_set_loc(t, CURRENT_LOC);
2✔
6209
         tree_set_subkind(t, SPEC_ALL);
2✔
6210

6211
         APUSH(*list, t);
2✔
6212
      }
6213
      break;
2✔
6214
   default:
×
6215
      do {
348✔
6216
         APUSH(*list, p_entity_designator());
348✔
6217
      } while (optional(tCOMMA));
348✔
6218
   }
6219
}
351✔
6220

6221
static class_t p_entity_specification(tree_list_t *list)
351✔
6222
{
6223
   // entity_name_list : entity_class
6224

6225
   BEGIN("entity specification");
702✔
6226

6227
   p_entity_name_list(list);
351✔
6228

6229
   consume(tCOLON);
351✔
6230

6231
   return p_entity_class();
351✔
6232
}
6233

6234
static void p_attribute_specification(tree_t parent)
351✔
6235
{
6236
   // attribute attribute_designator of entity_specification is expression ;
6237

6238
   BEGIN("attribute specification");
351✔
6239

6240
   consume(tATTRIBUTE);
351✔
6241
   ident_t head = p_identifier();
351✔
6242

6243
   type_t type;
351✔
6244
   tree_t attr_decl = resolve_name(nametab, CURRENT_LOC, head);
351✔
6245
   if (attr_decl == NULL)
351✔
6246
      type = type_new(T_NONE);
2✔
6247
   else if (tree_kind(attr_decl) != T_ATTR_DECL) {
349✔
6248
      parse_error(CURRENT_LOC, "name %s is not an attribute declaration",
×
6249
                  istr(head));
6250
      type = type_new(T_NONE);
×
6251
   }
6252
   else
6253
      type = tree_type(attr_decl);
349✔
6254

6255
   consume(tOF);
351✔
6256

6257
   tree_list_t specs = AINIT;
351✔
6258
   class_t class = p_entity_specification(&specs);
351✔
6259

6260
   consume(tIS);
351✔
6261

6262
   tree_t value = p_expression();
351✔
6263
   solve_types(nametab, value, type);
351✔
6264

6265
   consume(tSEMI);
351✔
6266

6267
   const loc_t *loc = CURRENT_LOC;
351✔
6268

6269
   for (int i = 0; i < specs.count; i++) {
702✔
6270
      tree_t t = specs.items[i];
351✔
6271
      assert(tree_kind(t) == T_ATTR_SPEC);
351✔
6272

6273
      if (class == C_LITERAL || class == C_LABEL)
351✔
6274
         ;   // Cannot check name now
6275
      else if (tree_subkind(t) == SPEC_EXACT && !tree_has_ref(t))
335✔
6276
         tree_set_ref(t, resolve_name(nametab, loc, tree_ident2(t)));
186✔
6277

6278
      tree_set_class(t, class);
351✔
6279
      tree_set_ident(t, head);
351✔
6280
      tree_set_value(t, value);
351✔
6281
      tree_set_type(t, type);
351✔
6282

6283
      insert_name(nametab, t, NULL);
351✔
6284
      tree_add_decl(parent, t);
351✔
6285
      sem_check(t, nametab);
351✔
6286
   }
6287

6288
   ACLEAR(specs);
351✔
6289
}
351✔
6290

6291
static type_t p_integer_type_definition(tree_t r, ident_t id)
146✔
6292
{
6293
   // range_constraint
6294

6295
   EXTEND("integer type definition");
146✔
6296

6297
   type_t t = type_new(T_INTEGER);
146✔
6298
   type_add_dim(t, r);
146✔
6299
   type_set_ident(t, id);
146✔
6300
   mangle_type(nametab, t);
146✔
6301

6302
   return t;
146✔
6303
}
6304

6305
static type_t p_real_type_definition(tree_t r, ident_t id)
23✔
6306
{
6307
   // range_constraint
6308

6309
   EXTEND("real type definition");
23✔
6310

6311
   type_t t = type_new(T_REAL);
23✔
6312
   type_add_dim(t, r);
23✔
6313
   type_set_ident(t, id);
23✔
6314
   mangle_type(nametab, t);
23✔
6315

6316
   return t;
23✔
6317
}
6318

6319
static tree_t p_base_unit_declaration(void)
45✔
6320
{
6321
   // identifier ;
6322

6323
   BEGIN("base unit declaration");
45✔
6324

6325
   ident_t id = p_identifier();
45✔
6326
   consume(tSEMI);
45✔
6327

6328
   tree_t value = tree_new(T_LITERAL);
45✔
6329
   tree_set_loc(value, CURRENT_LOC);
45✔
6330
   tree_set_subkind(value, L_PHYSICAL);
45✔
6331
   tree_set_ident(value, id);
45✔
6332
   tree_set_ival(value, 1);
45✔
6333

6334
   tree_t t = tree_new(T_UNIT_DECL);
45✔
6335
   tree_set_loc(t, CURRENT_LOC);
45✔
6336
   tree_set_value(t, value);
45✔
6337
   tree_set_ident(t, id);
45✔
6338

6339
   return t;
45✔
6340
}
6341

6342
static tree_t p_secondary_unit_declaration(type_t type)
113✔
6343
{
6344
   // identifier = physical_literal ;
6345

6346
   BEGIN("secondary unit declaration");
113✔
6347

6348
   ident_t id = p_identifier();
113✔
6349
   consume(tEQ);
113✔
6350

6351
   tree_t value = p_physical_literal();
113✔
6352

6353
   consume(tSEMI);
113✔
6354

6355
   tree_t u = tree_new(T_UNIT_DECL);
113✔
6356
   tree_set_ident(u, id);
113✔
6357
   tree_set_value(u, value);
113✔
6358
   tree_set_loc(u, CURRENT_LOC);
113✔
6359
   tree_set_type(u, type);
113✔
6360

6361
   return u;
113✔
6362
}
6363

6364
static type_t p_physical_type_definition(tree_t range, ident_t id)
45✔
6365
{
6366
   // range_constraint units base_unit_declaration
6367
   //   { secondary_unit_declaration } end units [ name ]
6368

6369
   EXTEND("physical type definition");
45✔
6370

6371
   type_t t = type_new(T_PHYSICAL);
45✔
6372
   type_set_ident(t, id);
45✔
6373
   mangle_type(nametab, t);
45✔
6374

6375
   consume(tUNITS);
45✔
6376

6377
   tree_t base = p_base_unit_declaration();
45✔
6378
   tree_set_type(base, t);
45✔
6379
   tree_set_type(tree_value(base), t);
45✔
6380
   type_add_unit(t, base);
45✔
6381
   type_add_dim(t, range);
45✔
6382

6383
   push_scope(nametab);
45✔
6384

6385
   insert_name(nametab, base, NULL);
45✔
6386

6387
   while (scan(tINT, tREAL, tID)) {
158✔
6388
      tree_t unit = p_secondary_unit_declaration(t);
113✔
6389
      type_add_unit(t, unit);
113✔
6390
      insert_name(nametab, unit, NULL);
113✔
6391
   }
6392

6393
   pop_scope(nametab);
45✔
6394

6395
   consume(tEND);
45✔
6396
   consume(tUNITS);
45✔
6397

6398
   if (peek() == tID) {
45✔
6399
      ident_t trailing = p_identifier();
1✔
6400
      if (trailing != id)
1✔
6401
         parse_error(&yylloc, "expected physical type definition trailing "
1✔
6402
                     "identifier to match %s", istr(id));
6403
   }
6404

6405
   return t;
45✔
6406
}
6407

6408
static tree_t p_enumeration_literal(void)
4,626✔
6409
{
6410
   // identifier | character_literal
6411

6412
   BEGIN("enumeration literal");
4,626✔
6413

6414
   tree_t t = tree_new(T_ENUM_LIT);
4,626✔
6415
   tree_set_ident(t, p_identifier());
4,626✔
6416
   tree_set_loc(t, CURRENT_LOC);
4,626✔
6417

6418
   return t;
4,626✔
6419
}
6420

6421
static type_t p_enumeration_type_definition(ident_t id)
456✔
6422
{
6423
   // ( enumeration_literal { , enumeration_literal } )
6424

6425
   BEGIN("enumeration type definition");
456✔
6426

6427
   type_t t = type_new(T_ENUM);
456✔
6428
   type_set_ident(t, id);
456✔
6429
   mangle_type(nametab, t);
456✔
6430

6431
   consume(tLPAREN);
456✔
6432

6433
   unsigned pos = 0;
456✔
6434
   do {
4,626✔
6435
      tree_t lit = p_enumeration_literal();
4,626✔
6436
      tree_set_pos(lit, pos++);
4,626✔
6437
      tree_set_type(lit, t);
4,626✔
6438
      type_enum_add_literal(t, lit);
4,626✔
6439
   } while (optional(tCOMMA));
4,626✔
6440

6441
   tree_t r = tree_new(T_RANGE);
456✔
6442
   tree_set_subkind(r, RANGE_TO);
456✔
6443
   tree_set_left(r, make_ref(type_enum_literal(t, 0)));
456✔
6444
   tree_set_right(r, make_ref(type_enum_literal(t, pos - 1)));
456✔
6445
   tree_set_loc(r, CURRENT_LOC);
456✔
6446
   tree_set_type(r, t);
456✔
6447

6448
   type_add_dim(t, r);
456✔
6449

6450
   consume(tRPAREN);
456✔
6451

6452
   return t;
456✔
6453
}
6454

6455
static type_t p_scalar_type_definition(ident_t id)
670✔
6456
{
6457
   // enumeration_type_definition | integer_type_definition
6458
   //   | floating_type_definition | physical_type_definition
6459

6460
   BEGIN("scalar type definition");
1,340✔
6461

6462
   switch (peek()) {
670✔
6463
   case tRANGE:
214✔
6464
      {
6465
         tree_t r = tree_range(p_range_constraint(NULL), 0);
214✔
6466

6467
         if (peek() == tUNITS)
214✔
6468
            return p_physical_type_definition(r, id);
45✔
6469
         else if (type_is_real(tree_type(r)))
169✔
6470
            return p_real_type_definition(r, id);
23✔
6471
         else
6472
            return p_integer_type_definition(r, id);
146✔
6473
      }
6474

6475
   case tLPAREN:
456✔
6476
      return p_enumeration_type_definition(id);
456✔
6477

6478
   default:
×
6479
      one_of(tRANGE, tLPAREN);
×
6480
      return type_new(T_NONE);
×
6481
   }
6482
}
6483

6484
static type_t p_access_type_definition(ident_t id)
324✔
6485
{
6486
   // access subtype_indication
6487

6488
   BEGIN("access type definition");
324✔
6489

6490
   consume(tACCESS);
324✔
6491

6492
   type_t t = type_new(T_ACCESS);
324✔
6493
   type_set_ident(t, id);
324✔
6494
   type_set_designated(t, p_subtype_indication());
324✔
6495
   mangle_type(nametab, t);
324✔
6496

6497
   return t;
324✔
6498
}
6499

6500
static type_t p_file_type_definition(ident_t id)
101✔
6501
{
6502
   // file of type_mark
6503

6504
   BEGIN("file type definition");
101✔
6505

6506
   consume(tFILE);
101✔
6507
   consume(tOF);
101✔
6508

6509
   type_t t = type_new(T_FILE);
101✔
6510
   type_set_ident(t, id);
101✔
6511
   type_set_designated(t, p_type_mark());
101✔
6512
   mangle_type(nametab, t);
101✔
6513

6514
   return t;
101✔
6515
}
6516

6517
static void p_element_declaration(type_t rec)
3,519✔
6518
{
6519
   // identifier_list : element_subtype_definition ;
6520

6521
   BEGIN("element declaration");
7,038✔
6522

6523
   LOCAL_IDENT_LIST ids = p_identifier_list();
7,038✔
6524

6525
   consume(tCOLON);
3,519✔
6526

6527
   type_t type = p_subtype_indication();
3,519✔
6528

6529
   consume(tSEMI);
3,519✔
6530

6531
   int pos = type_fields(rec);
3,519✔
6532
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
7,336✔
6533
      tree_t f = tree_new(T_FIELD_DECL);
3,817✔
6534
      tree_set_ident(f, it->ident);
3,817✔
6535
      tree_set_type(f, type);
3,817✔
6536
      tree_set_pos(f, pos++);
3,817✔
6537
      tree_set_loc(f, &(it->loc));
3,817✔
6538

6539
      type_add_field(rec, f);
3,817✔
6540
   }
6541
}
3,519✔
6542

6543
static type_t p_record_type_definition(ident_t id)
1,291✔
6544
{
6545
   // record element_declaration { element_declaration } end record
6546
   //   [ simple_name ]
6547

6548
   // 2019: record { element_declaration } end record [ simple_name ]
6549
   BEGIN("record type definition");
1,291✔
6550

6551
   consume(tRECORD);
1,291✔
6552

6553
   type_t r = type_new(T_RECORD);
1,291✔
6554
   type_set_ident(r, id);
1,291✔
6555
   mangle_type(nametab, r);
1,291✔
6556

6557
   if (peek() == tEND)
1,291✔
6558
      require_std(STD_19, "empty record");
3✔
6559
   else {
6560
      do {
3,519✔
6561
         p_element_declaration(r);
3,519✔
6562
      } while (peek() == tID);
3,519✔
6563
   }
6564

6565
   consume(tEND);
1,291✔
6566
   consume(tRECORD);
1,291✔
6567

6568
   if (peek() == tID) {
1,291✔
6569
      ident_t trailing = p_identifier();
41✔
6570
      if (trailing != id)
41✔
6571
         parse_error(&yylloc, "expected record type definition trailing "
1✔
6572
                     "identifier to match %s", istr(id));
6573
   }
6574

6575
   return r;
1,291✔
6576
}
6577

6578
static type_t p_index_subtype_definition(tree_t head)
1,978✔
6579
{
6580
   // type_mark range <>
6581

6582
   BEGIN_WITH_HEAD("index subtype definition", head);
1,978✔
6583

6584
   type_t type;
1,978✔
6585
   if (head != NULL)
1,978✔
6586
      type = name_to_type_mark(head);
1,806✔
6587
   else
6588
      type = p_type_mark();
172✔
6589

6590
   consume(tRANGE);
1,978✔
6591
   consume(tBOX);
1,978✔
6592

6593
   return type;
1,978✔
6594
}
6595

6596
static type_t p_unconstrained_array_definition(type_t base, tree_t head)
1,806✔
6597
{
6598
   // array ( index_subtype_definition { , index_subtype_definition } )
6599
   //   of subtype_indication
6600

6601
   EXTEND("unconstrained array definition");
1,806✔
6602

6603
   type_add_index(base, p_index_subtype_definition(head));
1,806✔
6604

6605
   while (optional(tCOMMA))
1,966✔
6606
      type_add_index(base, p_index_subtype_definition(NULL));
160✔
6607

6608
   mangle_type(nametab, base);
1,806✔
6609

6610
   consume(tRPAREN);
1,806✔
6611
   consume(tOF);
1,806✔
6612

6613
   type_set_elem(base, p_subtype_indication());
1,806✔
6614
   return base;
1,806✔
6615
}
6616

6617
static type_t p_constrained_array_definition(type_t base, tree_t head)
744✔
6618
{
6619
   // array index_constraint of element_subtype_indication
6620

6621
   EXTEND("constrained array definition");
744✔
6622

6623
   tree_t constraint = tree_new(T_CONSTRAINT);
744✔
6624
   tree_set_subkind(constraint, C_INDEX);
744✔
6625

6626
   type_t sub = type_new(T_SUBTYPE);
744✔
6627
   type_set_base(sub, base);
744✔
6628
   type_set_constraint(sub, constraint);
744✔
6629
   type_set_ident(sub, type_ident(base));
744✔
6630

6631
   mangle_type(nametab, sub);
744✔
6632

6633
   do {
900✔
6634
      tree_t r = p_discrete_range(head);
900✔
6635
      solve_types(nametab, r, NULL);
900✔
6636
      convert_universal_bounds(r);
900✔
6637

6638
      tree_add_range(constraint, r);
900✔
6639
      type_add_index(base, tree_type(r));
900✔
6640

6641
      head = NULL;
900✔
6642
   } while (optional(tCOMMA));
900✔
6643

6644
   consume(tRPAREN);
744✔
6645

6646
   tree_set_loc(constraint, CURRENT_LOC);
744✔
6647

6648
   consume(tOF);
744✔
6649

6650
   type_set_elem(base, p_subtype_indication());
744✔
6651
   return sub;
744✔
6652
}
6653

6654
static type_t p_array_type_definition(ident_t id)
2,550✔
6655
{
6656
   // unconstrained_array_definition | constrained_array_definition
6657

6658
   BEGIN("array type definition");
5,100✔
6659

6660
   type_t base = type_new(T_ARRAY);
2,550✔
6661
   type_set_ident(base, id);
2,550✔
6662

6663
   consume(tARRAY);
2,550✔
6664
   consume(tLPAREN);
2,550✔
6665

6666
   tree_t head = p_expression();
2,550✔
6667

6668
   if (peek_nth(2) == tBOX)
2,550✔
6669
      return p_unconstrained_array_definition(base, head);
1,806✔
6670
   else
6671
      return p_constrained_array_definition(base, head);
744✔
6672
}
6673

6674
static type_t p_composite_type_definition(ident_t id)
3,841✔
6675
{
6676
   // array_type_definition | record_type_definition
6677

6678
   BEGIN("composite type definition");
7,682✔
6679

6680
   switch (peek()) {
3,841✔
6681
   case tRECORD:
1,291✔
6682
      return p_record_type_definition(id);
1,291✔
6683

6684
   case tARRAY:
2,550✔
6685
      return p_array_type_definition(id);
2,550✔
6686

6687
   default:
×
6688
      expect(tRECORD, tARRAY);
×
6689
      return type_new(T_NONE);
×
6690
   }
6691
}
6692

6693
static void p_private_variable_declaration(tree_t decl)
9✔
6694
{
6695
   // 2019: private variable_declaration
6696

6697
   BEGIN("private variable declaration");
18✔
6698

6699
   consume(tPRIVATE);
9✔
6700

6701
   require_std(STD_19, "private variable declarations");
9✔
6702

6703
   p_variable_declaration(decl);
9✔
6704
}
9✔
6705

6706
static void p_protected_type_declarative_item(tree_t decl)
477✔
6707
{
6708
   // subprogram_declaration | 2008: subprogram_instantiation_declaration
6709
   //   | attribute_specification | use_clause
6710
   //   | 2019: private_variable_declaration | alias_declaration
6711

6712
   BEGIN("protected type declarative item");
954✔
6713

6714
   switch (peek()) {
477✔
6715
   case tATTRIBUTE:
×
6716
      p_attribute_specification(decl);
×
6717
      break;
×
6718

6719
   case tUSE:
×
6720
      p_use_clause(decl, tree_add_decl);
×
6721
      break;
×
6722

6723
   case tFUNCTION:
453✔
6724
   case tPROCEDURE:
6725
   case tIMPURE:
6726
   case tPURE:
6727
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
453✔
6728
         tree_add_decl(decl, p_subprogram_instantiation_declaration());
×
6729
      else {
6730
         tree_t spec = p_subprogram_specification();
453✔
6731
         tree_set_flag(spec, TREE_F_PROTECTED);
453✔
6732
         tree_add_decl(decl, p_subprogram_declaration(spec));
453✔
6733
      }
6734
      break;
6735

6736
   case tPRIVATE:
9✔
6737
      p_private_variable_declaration(decl);
9✔
6738
      break;
9✔
6739

6740
   case tALIAS:
15✔
6741
      p_alias_declaration(decl);
15✔
6742
      break;
15✔
6743

6744
   default:
×
6745
      expect(tATTRIBUTE, tUSE, STD(08, tFUNCTION), tPROCEDURE, tIMPURE, tPURE,
×
6746
             STD(19, tPRIVATE), tALIAS);
6747
   }
6748
}
477✔
6749

6750
static void p_protected_type_declarative_part(tree_t decl)
213✔
6751
{
6752
   // { protected_type_declarative_item }
6753

6754
   BEGIN("protected type declarative part");
426✔
6755

6756
   while (not_at_token(tEND))
690✔
6757
      p_protected_type_declarative_item(decl);
477✔
6758
}
213✔
6759

6760
static tree_t p_protected_type_declaration(ident_t id)
213✔
6761
{
6762
   // protected protected_type_declarative_part end protected [ simple_name ]
6763

6764
   BEGIN("protected type declaration");
213✔
6765

6766
   consume(tPROTECTED);
213✔
6767

6768
   type_t type = type_new(T_PROTECTED);
213✔
6769
   type_set_ident(type, id);
213✔
6770

6771
   mangle_type(nametab, type);
213✔
6772

6773
   tree_t t = tree_new(T_PROT_DECL);
213✔
6774
   tree_set_ident(t, id);
213✔
6775
   tree_set_type(t, type);
213✔
6776
   tree_set_loc(t, CURRENT_LOC);
213✔
6777

6778
   insert_name(nametab, t, NULL);
213✔
6779

6780
   push_scope(nametab);
213✔
6781
   scope_set_prefix(nametab, id);
213✔
6782

6783
   p_protected_type_declarative_part(t);
213✔
6784

6785
   const int ndecls = tree_decls(t);
213✔
6786
   for (int i = 0; i < ndecls; i++) {
690✔
6787
      tree_t d = tree_decl(t, i);
477✔
6788
      if (is_subprogram(d) || tree_kind(d) == T_ALIAS)
477✔
6789
         type_add_field(type, d);
468✔
6790
   }
6791

6792
   pop_scope(nametab);
213✔
6793

6794
   consume(tEND);
213✔
6795
   consume(tPROTECTED);
213✔
6796

6797
   p_trailing_label(id);
213✔
6798

6799
   tree_set_loc(t, CURRENT_LOC);
213✔
6800
   sem_check(t, nametab);
213✔
6801
   return t;
213✔
6802
}
6803

6804
static tree_t p_protected_type_definition(ident_t id)
422✔
6805
{
6806
   // protected_type_declaration | protected_type_body
6807

6808
   BEGIN("protected type definition");
844✔
6809

6810
   if (peek_nth(2) == tBODY)
422✔
6811
      return p_protected_type_body(id);
209✔
6812
   else
6813
      return p_protected_type_declaration(id);
213✔
6814
}
6815

6816
static type_t p_type_definition(tree_t tdecl)
4,939✔
6817
{
6818
   // scalar_type_definition | composite_type_definition
6819
   //   | access_type_definition | file_type_definition
6820
   //   | 2000: protected_type_definition
6821

6822
   BEGIN("type definition");
9,878✔
6823

6824
   ident_t id = tree_ident(tdecl);
4,939✔
6825

6826
   switch (peek()) {
4,939✔
6827
   case tRANGE:
670✔
6828
   case tLPAREN:
6829
      return p_scalar_type_definition(id);
670✔
6830

6831
   case tACCESS:
324✔
6832
      return p_access_type_definition(id);
324✔
6833

6834
   case tFILE:
101✔
6835
      return p_file_type_definition(id);
101✔
6836

6837
   case tRECORD:
3,841✔
6838
   case tARRAY:
6839
      return p_composite_type_definition(id);
3,841✔
6840

6841
   default:
3✔
6842
      expect(tRANGE, tACCESS, tFILE, tRECORD);
3✔
6843
      return type_new(T_NONE);
3✔
6844
   }
6845
}
6846

6847
static type_t p_full_type_declaration(tree_t tdecl)
4,939✔
6848
{
6849
   // type identifier is type_definition ;
6850

6851
   EXTEND("full type declaration");
4,939✔
6852

6853
   consume(tIS);
4,939✔
6854

6855
   type_t t = p_type_definition(tdecl);
4,939✔
6856

6857
   consume(tSEMI);
4,939✔
6858

6859
   return t;
4,939✔
6860
}
6861

6862
static type_t p_incomplete_type_declaration(ident_t id)
48✔
6863
{
6864
   // type identifier ;
6865

6866
   EXTEND("incomplete type declaration");
48✔
6867

6868
   consume(tSEMI);
48✔
6869

6870
   type_t t = type_new(T_INCOMPLETE);
48✔
6871
   type_set_ident(t, id);
48✔
6872
   mangle_type(nametab, t);
48✔
6873

6874
   return t;
48✔
6875
}
6876

6877
static void p_type_declaration(tree_t container)
5,409✔
6878
{
6879
   // full_type_declaration | incomplete_type_declaration
6880

6881
   BEGIN("type declaration");
10,818✔
6882

6883
   consume(tTYPE);
5,409✔
6884

6885
   ident_t id = p_identifier();
5,409✔
6886
   hide_name(nametab, id);
5,409✔
6887

6888
   // Protected type definitions are trees rather than types
6889
   if (peek_nth(1) == tIS && peek_nth(2) == tPROTECTED) {
5,409✔
6890
      consume(tIS);
422✔
6891
      tree_add_decl(container, p_protected_type_definition(id));
422✔
6892
      consume(tSEMI);
422✔
6893
   }
6894
   else {
6895
      // Insert univeral_integer and universal_real predefined functions
6896
      // before STD.INTEGER and STD.REAL respectively
6897
      if (bootstrapping) {
4,987✔
6898
         if (id == ident_new("INTEGER"))
43✔
6899
            make_universal_int(container);
3✔
6900
         else if (id == ident_new("REAL"))
40✔
6901
            make_universal_real(container);
3✔
6902
      }
6903

6904
      tree_t t = tree_new(T_TYPE_DECL);
4,987✔
6905
      tree_set_ident(t, id);
4,987✔
6906
      tree_set_loc(t, CURRENT_LOC);
4,987✔
6907

6908
      type_t type;
4,987✔
6909
      if (peek() == tSEMI)
4,987✔
6910
         type = p_incomplete_type_declaration(id);
48✔
6911
      else
6912
         type = p_full_type_declaration(t);
4,939✔
6913

6914
      tree_set_type(t, type);
4,987✔
6915
      tree_set_loc(t, CURRENT_LOC);
4,987✔
6916

6917
      insert_name(nametab, t, id);
4,987✔
6918
      tree_add_decl(container, t);
4,987✔
6919

6920
      const type_kind_t kind = type_kind(type);
4,987✔
6921

6922
      type_t base = type;
4,987✔
6923
      if (kind == T_SUBTYPE) {
4,987✔
6924
         base = type_base(type);
744✔
6925
         assert(type_kind(base) == T_ARRAY);
744✔
6926
         mangle_type(nametab, base);
744✔
6927
      }
6928

6929
      if (kind != T_INCOMPLETE)
4,987✔
6930
         declare_predefined_ops(container, base);
4,939✔
6931

6932
      if (kind == T_PHYSICAL) {
4,987✔
6933
         const int nunits = type_units(type);
45✔
6934
         for (int i = 0; i < nunits; i++)
203✔
6935
            solve_types(nametab, tree_value(type_unit(type, i)), type);
158✔
6936
      }
6937

6938
      sem_check(t, nametab);
4,987✔
6939
   }
6940
}
5,409✔
6941

6942
static tree_t p_subtype_declaration(void)
1,472✔
6943
{
6944
   // subtype identifier is subtype_indication ;
6945

6946
   BEGIN("subtype declaration");
1,472✔
6947

6948
   consume(tSUBTYPE);
1,472✔
6949

6950
   ident_t id = p_identifier();
1,472✔
6951
   hide_name(nametab, id);
1,472✔
6952

6953
   consume(tIS);
1,472✔
6954

6955
   type_t sub = p_subtype_indication();
1,472✔
6956

6957
   consume(tSEMI);
1,472✔
6958

6959
   if (type_kind(sub) != T_SUBTYPE || type_has_ident(sub)) {
1,472✔
6960
      // Case where subtype_indication did not impose any
6961
      // constraint so we must create the subtype object here
6962
      type_t new = type_new(T_SUBTYPE);
139✔
6963
      type_set_base(new, sub);
139✔
6964

6965
      sub = new;
139✔
6966
   }
6967
   type_set_ident(sub, id);
1,472✔
6968

6969
   mangle_type(nametab, sub);
1,472✔
6970

6971
   tree_t t = tree_new(T_SUBTYPE_DECL);
1,472✔
6972
   tree_set_ident(t, id);
1,472✔
6973
   tree_set_type(t, sub);
1,472✔
6974
   tree_set_loc(t, CURRENT_LOC);
1,472✔
6975

6976
   insert_name(nametab, t, id);
1,472✔
6977
   sem_check(t, nametab);
1,472✔
6978
   return t;
1,472✔
6979
}
6980

6981
static tree_t p_conditional_expression(void)
10,095✔
6982
{
6983
   // expression { when condition else expression }
6984

6985
   BEGIN("conditional expression");
20,190✔
6986

6987
   tree_t expr0 = p_expression();
10,095✔
6988

6989
   if (optional(tWHEN)) {
10,095✔
6990
      require_std(STD_19, "conditional expressions");
15✔
6991

6992
      tree_t value = tree_new(T_COND_VALUE);
15✔
6993

6994
      do {
18✔
6995
         tree_t cond = tree_new(T_COND_EXPR);
18✔
6996
         tree_set_result(cond, expr0);
18✔
6997
         tree_set_value(cond, p_condition());
18✔
6998

6999
         tree_set_loc(cond, CURRENT_LOC);
18✔
7000

7001
         tree_add_cond(value, cond);
18✔
7002

7003
         consume(tELSE);
18✔
7004

7005
         expr0 = p_expression();
18✔
7006
      } while (optional(tWHEN));
18✔
7007

7008
      tree_t last = tree_new(T_COND_EXPR);
15✔
7009
      tree_set_result(last, expr0);
15✔
7010
      tree_set_loc(last, CURRENT_LOC);
15✔
7011

7012
      tree_add_cond(value, last);
15✔
7013

7014
      tree_set_loc(value, CURRENT_LOC);
15✔
7015
      return value;
15✔
7016
   }
7017
   else
7018
      return expr0;
7019
}
7020

7021
static tree_t p_expression_or_unaffected(void)
29,370✔
7022
{
7023
   // expression | unaffected
7024

7025
   BEGIN("expression or unaffected");
58,740✔
7026

7027
   if (optional(tUNAFFECTED)) {
29,370✔
7028
      require_std(STD_19, "unaffected in conditional expression");
10✔
7029
      return NULL;
10✔
7030
   }
7031
   else
7032
      return p_expression();
29,360✔
7033
}
7034

7035
static tree_t p_conditional_or_unaffected_expression(vhdl_standard_t minstd)
29,321✔
7036
{
7037
   // expression_or_unaffected { when condition else expression_or_unaffected }
7038
   //   [ when condition ]
7039

7040
   BEGIN("conditional expression");
58,642✔
7041

7042
   tree_t expr0 = p_expression_or_unaffected();
29,321✔
7043

7044
   if (optional(tWHEN)) {
29,321✔
7045
      require_std(minstd, "conditional expressions");
35✔
7046

7047
      tree_t value = tree_new(T_COND_VALUE);
35✔
7048

7049
      tree_t c0 = tree_new(T_COND_EXPR);
35✔
7050
      tree_set_result(c0, expr0);
35✔
7051
      tree_set_value(c0, p_condition());
35✔
7052
      tree_set_loc(c0, CURRENT_LOC);
35✔
7053

7054
      tree_add_cond(value, c0);
35✔
7055

7056
      while (optional(tELSE)) {
54✔
7057
         tree_t cond = tree_new(T_COND_EXPR);
49✔
7058
         tree_set_result(cond, p_expression_or_unaffected());
49✔
7059
         tree_set_loc(cond, CURRENT_LOC);
49✔
7060

7061
         tree_add_cond(value, cond);
49✔
7062

7063
         if (!optional(tWHEN))
49✔
7064
            break;
7065

7066
         tree_set_value(cond, p_condition());
19✔
7067
         tree_set_loc(cond, CURRENT_LOC);
19✔
7068
      }
7069

7070
      tree_set_loc(value, CURRENT_LOC);
35✔
7071
      return value;
35✔
7072
   }
7073
   else
7074
      return expr0;
7075
}
7076

7077
static void p_constant_declaration(tree_t parent)
6,715✔
7078
{
7079
   // constant identifier_list : subtype_indication [ := expression ] ;
7080

7081
   BEGIN("constant declaration");
13,430✔
7082

7083
   consume(tCONSTANT);
6,715✔
7084

7085
   LOCAL_IDENT_LIST ids = p_identifier_list();
13,430✔
7086

7087
   for (ident_list_t *it = ids; it != NULL; it = it->next)
13,434✔
7088
      hide_name(nametab, it->ident);
6,719✔
7089

7090
   consume(tCOLON);
6,715✔
7091

7092
   type_t type = p_subtype_indication();
6,715✔
7093

7094
   tree_t init = NULL;
6,715✔
7095
   if (optional(tWALRUS))
6,715✔
7096
      init = p_conditional_expression();
6,290✔
7097

7098
   // According to the LRM all constants are globally static but that
7099
   // leads to a number of logical inconsistencies so we retrict to only
7100
   // constants in certain regions where the value can be computed
7101
   // during elaboration
7102
   tree_flags_t flags = 0;
6,715✔
7103
   switch (tree_kind(parent)) {
6,715✔
7104
   case T_ENTITY:
4,232✔
7105
   case T_ARCH:
7106
   case T_PACKAGE:
7107
   case T_PACK_BODY:
7108
   case T_BLOCK:
7109
   case T_FOR_GENERATE:
7110
   case T_COND_STMT:     // If-generate
7111
   case T_ALTERNATIVE:   // For-generate
7112
      flags |= TREE_F_GLOBALLY_STATIC;
4,232✔
7113
      break;
4,232✔
7114
   default:
7115
      break;
7116
   }
7117

7118
   consume(tSEMI);
6,715✔
7119

7120
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
13,434✔
7121
      tree_t t = tree_new(T_CONST_DECL);
6,719✔
7122
      tree_set_ident(t, it->ident);
6,719✔
7123
      tree_set_type(t, type);
6,719✔
7124
      tree_set_flag(t, flags);
6,719✔
7125
      tree_set_loc(t, &(it->loc));
6,719✔
7126

7127
      if (init != NULL) {
6,719✔
7128
         tree_set_value(t, init);
6,294✔
7129

7130
         if (standard() < STD_19 || type_is_unconstrained(type))
6,294✔
7131
            solve_types(nametab, init, type);
5,673✔
7132
         else
7133
            solve_known_subtype(nametab, init, t);
621✔
7134
      }
7135

7136
      tree_add_decl(parent, t);
6,719✔
7137

7138
      insert_name(nametab, t, it->ident);
6,719✔
7139
      sem_check(t, nametab);
6,719✔
7140
   }
7141
}
6,715✔
7142

7143
static tree_t p_condition(void)
28,325✔
7144
{
7145
   BEGIN("condition");
28,325✔
7146

7147
   tree_t value = p_expression();
28,325✔
7148
   solve_condition(nametab, &value);
28,325✔
7149

7150
   return value;
28,325✔
7151
}
7152

7153
static tree_t p_assertion(void)
17,586✔
7154
{
7155
   // assert condition [ report expression ] [ severity expression ]
7156

7157
   BEGIN("assertion");
17,586✔
7158

7159
   tree_t s = tree_new(T_ASSERT);
17,586✔
7160

7161
   consume(tASSERT);
17,586✔
7162

7163
   tree_set_value(s, p_condition());
17,586✔
7164

7165
   if (optional(tREPORT)) {
17,586✔
7166
      tree_t message = p_expression();
6,092✔
7167
      solve_types(nametab, message, std_type(NULL, STD_STRING));
6,092✔
7168
      tree_set_message(s, message);
6,092✔
7169
   }
7170

7171
   if (optional(tSEVERITY)) {
17,586✔
7172
      tree_t severity = p_expression();
5,646✔
7173
      solve_types(nametab, severity, std_type(NULL, STD_SEVERITY_LEVEL));
5,646✔
7174
      tree_set_severity(s, severity);
5,646✔
7175
   }
7176

7177
   tree_set_loc(s, CURRENT_LOC);
17,586✔
7178
   return s;
17,586✔
7179
}
7180

7181
static tree_t p_concurrent_assertion_statement(ident_t label)
963✔
7182
{
7183
   // [ label : ] [ postponed ] assertion ;
7184

7185
   BEGIN("concurrent assertion statement");
963✔
7186

7187
   tree_t conc = tree_new(T_CONCURRENT);
963✔
7188

7189
   if (optional(tPOSTPONED))
963✔
7190
      tree_set_flag(conc, TREE_F_POSTPONED);
22✔
7191

7192
   tree_t s = p_assertion();
963✔
7193
   tree_add_stmt(conc, s);
963✔
7194

7195
   consume(tSEMI);
963✔
7196

7197
   tree_set_loc(s, CURRENT_LOC);
963✔
7198
   tree_set_loc(conc, CURRENT_LOC);
963✔
7199

7200
   ensure_labelled(conc, label);
963✔
7201

7202
   if (label) insert_name(nametab, conc, NULL);
963✔
7203
   sem_check(conc, nametab);
963✔
7204
   return conc;
963✔
7205
}
7206

7207
static ident_t p_designator(void)
25,728✔
7208
{
7209
   // identifier | operator_symbol
7210

7211
   BEGIN("designator");
51,456✔
7212

7213
   switch (peek()) {
25,728✔
7214
   case tID:
18,415✔
7215
      return p_identifier();
18,415✔
7216
   case tSTRING:
7,312✔
7217
      return p_operator_symbol();
7,312✔
7218
   default:
1✔
7219
      expect(tID, tSTRING);
1✔
7220
      return error_marker();
1✔
7221
   }
7222
}
7223

7224
static void p_subprogram_header(tree_t spec)
16,587✔
7225
{
7226
   // 2008: [ generic ( generic_list ) [ generic_map_aspect ] ]
7227

7228
   if (optional(tGENERIC)) {
16,587✔
7229
      require_std(STD_08, "generic subprograms");
91✔
7230

7231
      consume(tLPAREN);
91✔
7232
      p_generic_list(spec);
91✔
7233
      consume(tRPAREN);
91✔
7234

7235
      insert_generics(nametab, spec);
91✔
7236
   }
7237
}
16,587✔
7238

7239
static tree_t p_subprogram_specification(void)
16,587✔
7240
{
7241
   // procedure designator subprogram_header
7242
   //       [ [parameter] ( formal_parameter_list ) ]
7243
   //   | [ pure | impure ] function designator subprogram_header
7244
   //       [ [parameter] ( formal_parameter_list ) ] return type_mark
7245

7246
   // 2019:
7247
   //  [ pure | impure ] function designator subprogram_header
7248
   //       [ [parameter] ( formal_parameter_list ) ]
7249
   //       return [ return_identifier of ] type_mark
7250

7251
   BEGIN("subprogram specification");
33,174✔
7252

7253
   tree_t t = NULL;
16,587✔
7254
   type_t type = NULL;
16,587✔
7255

7256
   bool impure = false;
16,587✔
7257
   switch (one_of(tFUNCTION, tPROCEDURE, tPURE, tIMPURE)) {
16,587✔
7258
   case tIMPURE:
888✔
7259
      impure = true;
888✔
7260
      // Fall-through
7261
   case tPURE:
921✔
7262
      consume(tFUNCTION);
921✔
7263
      // Fall-through
7264
   case tFUNCTION:
13,311✔
7265
      t = tree_new(T_FUNC_DECL);
13,311✔
7266
      break;
13,311✔
7267

7268
   case tPROCEDURE:
3,276✔
7269
      t = tree_new(T_PROC_DECL);
3,276✔
7270
      break;
3,276✔
7271

7272
   default:
×
7273
      return tree_new(T_FUNC_DECL);
×
7274
   }
7275

7276
   type = type_new(T_SIGNATURE);
16,587✔
7277
   tree_set_type(t, type);
16,587✔
7278
   tree_set_ident(t, p_designator());
16,587✔
7279
   tree_set_subkind(t, S_USER);
16,587✔
7280

7281
   type_set_ident(type, tree_ident(t));
16,587✔
7282

7283
   if (impure)
16,587✔
7284
      tree_set_flag(t, TREE_F_IMPURE);
888✔
7285

7286
   push_scope(nametab);
16,587✔
7287

7288
   p_subprogram_header(t);
16,587✔
7289

7290
   bool has_param_list = false;
16,587✔
7291
   if (optional(tLPAREN))
16,587✔
7292
      has_param_list = true;
7293
   else if (standard() >= STD_08 && optional(tPARAMETER)) {
1,541✔
7294
      consume(tLPAREN);
47✔
7295
      has_param_list = true;
47✔
7296
   }
7297

7298
   if (has_param_list) {
47✔
7299
      p_formal_parameter_list(t, type);
15,093✔
7300
      consume(tRPAREN);
15,093✔
7301
   }
7302
   else
7303
      tree_set_flag(t, TREE_F_CALL_NO_ARGS);
1,494✔
7304

7305
   if (tree_kind(t) == T_FUNC_DECL) {
16,587✔
7306
      consume(tRETURN);
13,311✔
7307

7308
      if (peek_nth(2) != tOF)
13,311✔
7309
         type_set_result(type, p_type_mark());
13,294✔
7310
      else {
7311
         require_std(STD_19, "function knows return type");
17✔
7312

7313
         ident_t id = p_identifier();
17✔
7314

7315
         consume(tOF);
17✔
7316

7317
         type_t of = p_type_mark(), sub;
17✔
7318

7319
         if (type_is_unconstrained(of)) {
17✔
7320
            tree_t rref = tree_new(T_REF);
15✔
7321
            tree_set_loc(rref, CURRENT_LOC);
15✔
7322
            tree_set_ident(rref, ident_new("result"));
15✔
7323
            tree_set_ref(rref, t);
15✔
7324
            tree_set_type(rref, of);
15✔
7325

7326
            sub = get_subtype_for(rref);
15✔
7327
         }
7328
         else {
7329
            parse_error(CURRENT_LOC, "type mark of return identifier must "
2✔
7330
                        "denote an unconstrained type");
7331

7332
            sub = type_new(T_SUBTYPE);
2✔
7333
            type_set_base(sub, of);
2✔
7334
         }
7335

7336
         type_set_ident(sub, id);
17✔
7337
         type_set_result(type, sub);
17✔
7338

7339
         tree_set_flag(t, TREE_F_KNOWS_SUBTYPE);
17✔
7340
      }
7341
   }
7342

7343
   pop_scope(nametab);
16,587✔
7344

7345
   mangle_func(nametab, t);
16,587✔
7346

7347
   tree_set_loc(t, CURRENT_LOC);
16,587✔
7348
   return t;
16,587✔
7349
}
7350

7351
static tree_t p_subprogram_instantiation_declaration(void)
93✔
7352
{
7353
   // subprogram_kind designator is new uninstantiated_subprogram_name
7354
   //   [ signature ] [ generic_map_aspect ] ;
7355

7356
   tree_kind_t kind;
93✔
7357
   switch (one_of(tFUNCTION, tPROCEDURE)) {
93✔
7358
   case tFUNCTION: kind = T_FUNC_INST; break;
7359
   case tPROCEDURE: kind = T_PROC_INST; break;
35✔
7360
   default: return NULL;
7361
   }
7362

7363
   tree_t inst = tree_new(kind);
93✔
7364
   tree_set_ident(inst, p_designator());
93✔
7365

7366
   consume(tIS);
93✔
7367
   consume(tNEW);
93✔
7368

7369
   require_std(STD_08, "subprogram instantiation declarations");
93✔
7370

7371
   tree_t name = p_name(N_SUBPROGRAM | N_TYPE);
93✔
7372

7373
   type_t constraint = NULL;
93✔
7374
   if (peek() == tLSQUARE)
93✔
7375
      constraint = p_signature();
25✔
7376

7377
   tree_t decl = NULL;
93✔
7378
   if (tree_kind(name) != T_REF)
93✔
7379
      parse_error(CURRENT_LOC, "expecting uninstantiated subprogram name");
1✔
7380
   else {
7381
      decl = resolve_uninstantiated_subprogram(nametab, tree_loc(name),
92✔
7382
                                               tree_ident(name), constraint);
7383
   }
7384

7385
   if (decl != NULL) {
93✔
7386
      tree_t body = find_generic_subprogram_body(inst, decl);
86✔
7387
      instantiate_subprogram(inst, decl, body);
86✔
7388
   }
7389
   else {
7390
      // Create a dummy subprogram type to avoid later errors
7391
      type_t type = type_new(T_SIGNATURE);
7✔
7392
      type_set_ident(type, error_marker());
7✔
7393
      if (kind == T_FUNC_INST)
7✔
7394
         type_set_result(type, type_new(T_NONE));
3✔
7395

7396
      tree_set_type(inst, type);
7✔
7397
   }
7398

7399
   if (peek() == tGENERIC)
93✔
7400
      p_generic_map_aspect(inst, inst);
89✔
7401

7402
   consume(tSEMI);
93✔
7403

7404
   tree_set_loc(inst, CURRENT_LOC);
93✔
7405
   sem_check(inst, nametab);
93✔
7406

7407
   hash_t *map = get_generic_map(nametab);
93✔
7408
   if (map != NULL)
93✔
7409
      instance_fixup(inst, map);
89✔
7410

7411
   mangle_func(nametab, inst);
93✔
7412
   insert_name(nametab, inst, NULL);
93✔
7413
   return inst;
93✔
7414
}
7415

7416
static void p_variable_declaration(tree_t parent)
10,240✔
7417
{
7418
   // [ shared ] variable identifier_list : subtype_indication
7419
   //   [ := expression ] ;
7420

7421
   BEGIN("variable declaration");
20,480✔
7422

7423
   const bool shared = optional(tSHARED);
10,240✔
7424

7425
   consume(tVARIABLE);
10,240✔
7426

7427
   LOCAL_IDENT_LIST ids = p_identifier_list();
20,480✔
7428

7429
   for (ident_list_t *it = ids; it != NULL; it = it->next)
21,669✔
7430
      hide_name(nametab, it->ident);
11,429✔
7431

7432
   consume(tCOLON);
10,240✔
7433

7434
   type_t type = p_subtype_indication();
10,240✔
7435

7436
   tree_t init = NULL;
10,240✔
7437
   if (optional(tWALRUS)) {
10,240✔
7438
      init = p_conditional_expression();
2,028✔
7439
      solve_types(nametab, init, type);
2,028✔
7440
   }
7441

7442
   consume(tSEMI);
10,240✔
7443

7444
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
21,669✔
7445
      tree_t t = tree_new(T_VAR_DECL);
11,429✔
7446
      tree_set_loc(t, &(it->loc));
11,429✔
7447
      tree_set_ident(t, it->ident);
11,429✔
7448
      tree_set_type(t, type);
11,429✔
7449

7450
      if (init != NULL)
11,429✔
7451
         tree_set_value(t, init);
2,055✔
7452

7453
      if (shared)
11,429✔
7454
         tree_set_flag(t, TREE_F_SHARED);
183✔
7455

7456
      tree_add_decl(parent, t);
11,429✔
7457
      insert_name(nametab, t, it->ident);
11,429✔
7458
      sem_check(t, nametab);
11,429✔
7459
   }
7460
}
10,240✔
7461

7462
static tree_flags_t p_signal_kind(void)
5,633✔
7463
{
7464
   // register | bus
7465

7466
   switch (peek()) {
5,633✔
7467
   case tBUS:
24✔
7468
      consume(tBUS);
24✔
7469
      return TREE_F_BUS;
24✔
7470
   case tREGISTER:
3✔
7471
      consume(tREGISTER);
3✔
7472
      return TREE_F_REGISTER;
3✔
7473
   default:
7474
      return 0;
7475
   }
7476
}
7477

7478
static void p_signal_declaration(tree_t parent)
5,633✔
7479
{
7480
   // signal identifier_list : subtype_indication [ signal_kind ]
7481
   //   [ := expression ] ;
7482

7483
   BEGIN("signal declaration");
11,266✔
7484

7485
   consume(tSIGNAL);
5,633✔
7486

7487
   LOCAL_IDENT_LIST ids = p_identifier_list();
11,266✔
7488

7489
   for (ident_list_t *it = ids; it != NULL; it = it->next)
12,203✔
7490
      hide_name(nametab, it->ident);
6,570✔
7491

7492
   consume(tCOLON);
5,633✔
7493

7494
   type_t type = p_subtype_indication();
5,633✔
7495
   tree_flags_t flags = p_signal_kind();
5,633✔
7496

7497
   tree_t init = NULL;
5,633✔
7498
   if (optional(tWALRUS))
5,633✔
7499
      init = p_conditional_expression();
1,777✔
7500

7501
   consume(tSEMI);
5,633✔
7502

7503
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
12,203✔
7504
      tree_t t = tree_new(T_SIGNAL_DECL);
6,570✔
7505
      tree_set_loc(t, &(it->loc));
6,570✔
7506
      tree_set_ident(t, it->ident);
6,570✔
7507
      tree_set_type(t, type);
6,570✔
7508
      tree_set_flag(t, flags);
6,570✔
7509

7510
      if (init != NULL) {
6,570✔
7511
         solve_known_subtype(nametab, init, t);
1,966✔
7512
         tree_set_value(t, init);
1,966✔
7513
      }
7514

7515
      tree_add_decl(parent, t);
6,570✔
7516

7517
      insert_name(nametab, t, it->ident);
6,570✔
7518
      sem_check(t, nametab);
6,570✔
7519
   }
7520
}
5,633✔
7521

7522
static type_t p_signature(void)
726✔
7523
{
7524
   // [ [ type_mark { , type_mark } ] [ return type_mark ] ]
7525

7526
   BEGIN("signature");
1,452✔
7527

7528
   type_t type = type_new(T_SIGNATURE);
726✔
7529

7530
   consume(tLSQUARE);
726✔
7531

7532
   bool error = false;
726✔
7533
   if (not_at_token(tRETURN, tRSQUARE)) {
726✔
7534
      do {
1,636✔
7535
         type_t param = p_type_mark();
1,636✔
7536
         type_add_param(type, param);
1,636✔
7537
         error = error || type_is_none(param);
1,636✔
7538
      } while (optional(tCOMMA));
1,636✔
7539
   }
7540

7541
   if (optional(tRETURN)) {
726✔
7542
      type_t ret = p_type_mark();
328✔
7543
      type_set_result(type, ret);
328✔
7544
      error = error || type_is_none(ret);
330✔
7545
   }
7546

7547
   consume(tRSQUARE);
726✔
7548

7549
   return error ? type_new(T_NONE) : type;
726✔
7550
}
7551

7552
static void p_alias_declaration(tree_t parent)
1,552✔
7553
{
7554
   // alias alias_designator [ : subtype_indication ] is name [ signature ] ;
7555

7556
   BEGIN("alias declaration");
3,104✔
7557

7558
   tree_t t = tree_new(T_ALIAS);
1,552✔
7559

7560
   consume(tALIAS);
1,552✔
7561

7562
   ident_t id = p_designator();
1,552✔
7563
   tree_set_ident(t, id);
1,552✔
7564

7565
   hide_name(nametab, id);
1,552✔
7566

7567
   bool has_subtype_indication = false;
1,552✔
7568
   if (optional(tCOLON)) {
1,552✔
7569
      tree_set_type(t, p_subtype_indication());
808✔
7570
      has_subtype_indication = true;
808✔
7571
   }
7572
   consume(tIS);
1,552✔
7573

7574
   tree_t value = p_name(N_SUBPROGRAM | N_TYPE);
1,552✔
7575
   tree_set_value(t, value);
1,552✔
7576

7577
   if (peek() == tLPAREN) {
1,552✔
7578
      parse_error(tree_loc(value), "name cannot be indexed or sliced");
1✔
7579
      tree_set_type(t, type_new(T_NONE));
1✔
7580
      drop_tokens_until(tRPAREN);
1✔
7581
   }
7582

7583
   const tree_kind_t value_kind = tree_kind(value);
1,552✔
7584
   bool nonobject_alias = false, type_alias = false;
1,552✔
7585

7586
   if (peek() == tLSQUARE) {
1,552✔
7587
      type_t type = p_signature();
531✔
7588
      if (has_subtype_indication) {
531✔
7589
         parse_error(CURRENT_LOC, "alias declaration may not contain both a "
×
7590
                     "signature and a subtype indication");
7591
         type = type_new(T_NONE);
×
7592
      }
7593
      else if (value_kind != T_REF && value_kind != T_PROT_REF) {
531✔
7594
         if (!type_is_none((type = solve_types(nametab, value, NULL)))) {
1✔
7595
            parse_error(tree_loc(value), "invalid name in subprogram alias");
×
7596
            type = type_new(T_NONE);
×
7597
         }
7598
      }
7599
      else {
7600
         ident_t id = tree_ident(value);
530✔
7601
         type_set_ident(type, ident_rfrom(id, '.') ?: id);
530✔
7602
         solve_types(nametab, value, type);
530✔
7603
      }
7604
      tree_set_type(t, type);
531✔
7605
   }
7606
   else if (value_kind == T_REF && tree_has_ref(value)) {
1,021✔
7607
      // A nonobject alias may be a design unit which does not have a type
7608
      tree_t decl = tree_ref(value);
848✔
7609
      if (is_design_unit(decl) || tree_kind(decl) == T_COMPONENT) {
848✔
7610
         tree_set_flag(t, TREE_F_NONOBJECT_ALIAS);
3✔
7611
         nonobject_alias = true;
3✔
7612
      }
7613
      else
7614
         solve_types(nametab, value, NULL);
845✔
7615
   }
7616
   else
7617
      solve_types(nametab, value, NULL);
173✔
7618

7619
   if (value_kind == T_REF && tree_has_ref(value)) {
1,552✔
7620
      tree_t decl = tree_ref(value);
1,350✔
7621
      if (is_type_decl(decl))
1,350✔
7622
         type_alias = true;
65✔
7623
   }
7624

7625
   if ((type_alias || nonobject_alias) && has_subtype_indication)
1,552✔
7626
      parse_error(CURRENT_LOC, "nonobject alias may not have a "
1✔
7627
                  "subtype indication");
7628

7629
   consume(tSEMI);
1,552✔
7630

7631
   tree_set_loc(t, CURRENT_LOC);
1,552✔
7632

7633
   insert_name(nametab, t, NULL);
1,552✔
7634
   sem_check(t, nametab);
1,552✔
7635

7636
   tree_add_decl(parent, t);
1,552✔
7637

7638
   if (type_alias) {
1,552✔
7639
      // LRM 08 section 6.6.3 an implicit alias declaration exists for
7640
      // each enumeration literal, unit, and predefined operators
7641

7642
      type_t type = tree_type(value);
65✔
7643

7644
      switch (type_kind(type)) {
65✔
7645
      case T_ENUM:
27✔
7646
         {
7647
            const int nlits = type_enum_literals(type);
27✔
7648
            for (int i = 0; i < nlits; i++) {
641✔
7649
               tree_t lit = type_enum_literal(type, i);
614✔
7650

7651
               tree_t a = tree_new(T_ALIAS);
614✔
7652
               tree_set_loc(a, CURRENT_LOC);
614✔
7653
               tree_set_ident(a, tree_ident(lit));
614✔
7654
               tree_set_value(a, make_ref(lit));
614✔
7655
               tree_set_type(a, type);
614✔
7656
               tree_set_flag(a, TREE_F_PREDEFINED);
614✔
7657

7658
               insert_name(nametab, a, NULL);
614✔
7659
               tree_add_decl(parent, a);
614✔
7660
            }
7661
         }
7662
         break;
7663

7664
      case T_PHYSICAL:
1✔
7665
         {
7666
            const int nunits = type_units(type);
1✔
7667
            for (int i = 0; i < nunits; i++) {
3✔
7668
               tree_t unit = type_unit(type, i);
2✔
7669

7670
               tree_t a = tree_new(T_ALIAS);
2✔
7671
               tree_set_loc(a, CURRENT_LOC);
2✔
7672
               tree_set_ident(a, tree_ident(unit));
2✔
7673
               tree_set_value(a, make_ref(unit));
2✔
7674
               tree_set_type(a, type);
2✔
7675
               tree_set_flag(a, TREE_F_PREDEFINED);
2✔
7676

7677
               insert_name(nametab, a, NULL);
2✔
7678
               tree_add_decl(parent, a);
2✔
7679
            }
7680
         }
7681
      default:
7682
         break;
7683
      }
7684

7685
      walk_predefs(nametab, tree_ident(value), add_predef_alias, parent);
65✔
7686
   }
7687
}
1,552✔
7688

7689
static void p_file_open_information(tree_t *mode, tree_t *name)
154✔
7690
{
7691
   // [ open expression ] is file_logical_name
7692

7693
   BEGIN("file open information");
308✔
7694

7695
   if (optional(tOPEN)) {
154✔
7696
      *mode = p_expression();
41✔
7697
      solve_types(nametab, *mode, std_type(NULL, STD_FILE_OPEN_KIND));
41✔
7698
   }
7699
   else
7700
      *mode = NULL;
113✔
7701

7702
   if (optional(tIS)) {
154✔
7703
      ident_t mode_name = ident_new("READ_MODE");
50✔
7704
      if ((*mode == NULL) && scan(tIN, tOUT)) {
50✔
7705
         // VHDL-87 compatibility
7706
         switch (one_of(tIN, tOUT)) {
2✔
7707
         case tIN: break;
7708
         case tOUT: mode_name = ident_new("WRITE_MODE"); break;
1✔
7709
         }
7710
      }
7711

7712
      *name = p_expression();
50✔
7713
      solve_types(nametab, *name, std_type(NULL, STD_STRING));
50✔
7714

7715
      if (*mode == NULL) {
50✔
7716
         tree_t decl = get_local_decl(nametab, find_std(nametab), mode_name, 0);
9✔
7717
         *mode = make_ref(decl);
9✔
7718
      }
7719
   }
7720
   else
7721
      *name = NULL;
104✔
7722
}
154✔
7723

7724
static void p_file_declaration(tree_t parent)
154✔
7725
{
7726
   // file identifier_list : subtype_indication [ file_open_information ] ;
7727

7728
   BEGIN("file declaration");
308✔
7729

7730
   consume(tFILE);
154✔
7731

7732
   LOCAL_IDENT_LIST ids = p_identifier_list();
308✔
7733

7734
   for (ident_list_t *it = ids; it != NULL; it = it->next)
314✔
7735
      hide_name(nametab, it->ident);
160✔
7736

7737
   consume(tCOLON);
154✔
7738

7739
   type_t type = p_subtype_indication();
154✔
7740

7741
   tree_t mode, name;
154✔
7742
   p_file_open_information(&mode, &name);
154✔
7743

7744
   consume(tSEMI);
154✔
7745

7746
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
314✔
7747
      tree_t t = tree_new(T_FILE_DECL);
160✔
7748
      tree_set_ident(t, it->ident);
160✔
7749
      tree_set_type(t, type);
160✔
7750
      if (name != NULL) {
160✔
7751
         tree_set_file_mode(t, mode);
50✔
7752
         tree_set_value(t, name);
50✔
7753
      }
7754
      tree_set_loc(t, &(it->loc));
160✔
7755

7756
      tree_add_decl(parent, t);
160✔
7757

7758
      insert_name(nametab, t, it->ident);
160✔
7759
      sem_check(t, nametab);
160✔
7760
   }
7761
}
154✔
7762

7763
static void p_disconnection_specification(tree_t container)
14✔
7764
{
7765
   // disconnect guarded_signal_specification after time_expression ;
7766

7767
   BEGIN("disconnection specification");
28✔
7768

7769
   consume(tDISCONNECT);
14✔
7770

7771
   LOCAL_IDENT_LIST ids = p_identifier_list();
28✔
7772

7773
   consume(tCOLON);
14✔
7774

7775
   type_t type = p_type_mark();
14✔
7776

7777
   consume(tAFTER);
14✔
7778

7779
   tree_t delay = p_expression();
14✔
7780
   solve_types(nametab, delay, std_type(NULL, STD_TIME));
14✔
7781

7782
   consume(tSEMI);
14✔
7783

7784
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
28✔
7785
      tree_t d = tree_new(T_DISCONNECT);
14✔
7786
      tree_set_loc(d, &(it->loc));
14✔
7787
      tree_set_ident(d, it->ident);
14✔
7788
      tree_set_type(d, type);
14✔
7789
      tree_set_delay(d, delay);
14✔
7790
      tree_set_ref(d, resolve_name(nametab, CURRENT_LOC, it->ident));
14✔
7791

7792
      // TODO: we should use insert_spec for this
7793
      ident_t name = ident_prefix(it->ident, ident_new("disconnect"), '$');
14✔
7794
      insert_name(nametab, d, name);
14✔
7795

7796
      tree_add_decl(container, d);
14✔
7797
      sem_check(d, nametab);
14✔
7798
   }
7799
}
14✔
7800

7801
static void p_entity_class_entry_list(tree_t group)
2✔
7802
{
7803
   // entity_class [ <> ] { , entity_class [ <> ] }
7804

7805
   BEGIN("entity class entry list");
4✔
7806

7807
   do {
2✔
7808
      p_entity_class();
2✔
7809
      optional(tBOX);
2✔
7810
   } while (optional(tCOMMA));
2✔
7811
}
2✔
7812

7813
static tree_t p_group_template_declaration(void)
2✔
7814
{
7815
   // group identifier is ( entity_class_entry_list ) ;
7816

7817
   BEGIN("group template declaration");
2✔
7818

7819
   consume(tGROUP);
2✔
7820

7821
   tree_t g = tree_new(T_GROUP_TEMPLATE);
2✔
7822
   tree_set_ident(g, p_identifier());
2✔
7823

7824
   consume(tIS);
2✔
7825
   consume(tLPAREN);
2✔
7826
   p_entity_class_entry_list(g);
2✔
7827
   consume(tRPAREN);
2✔
7828
   consume(tSEMI);
2✔
7829

7830
   tree_set_loc(g, CURRENT_LOC);
2✔
7831
   insert_name(nametab, g, NULL);
2✔
7832

7833
   return g;
2✔
7834
}
7835

7836
static void p_group_constituent_list(tree_t group)
2✔
7837
{
7838
   // group_constituent_list ::= group_constituent { , group_constituent }
7839

7840
   BEGIN("group constituent list");
4✔
7841

7842
   do {
2✔
7843
      (void)p_name(0);   // Do nothing with groups currently
2✔
7844
   } while (optional(tCOMMA));
2✔
7845
}
2✔
7846

7847
static tree_t p_group_declaration(void)
2✔
7848
{
7849
   // group identifier : group_template_name ( group_constituent_list ) ;
7850

7851
   BEGIN("group declaration");
2✔
7852

7853
   consume(tGROUP);
2✔
7854

7855
   tree_t g = tree_new(T_GROUP);
2✔
7856
   tree_set_ident(g, p_identifier());
2✔
7857

7858
   consume(tCOLON);
2✔
7859

7860
   ident_t template_name = p_identifier();
2✔
7861
   tree_t template = resolve_name(nametab, CURRENT_LOC, template_name);
2✔
7862
   if (template != NULL && tree_kind(template) != T_GROUP_TEMPLATE) {
2✔
7863
      parse_error(CURRENT_LOC, "%s does not name a group template",
×
7864
                  istr(template_name));
7865
      template = NULL;
7866
   }
7867

7868
   tree_set_ref(g, template);
2✔
7869

7870
   consume(tLPAREN);
2✔
7871
   p_group_constituent_list(g);
2✔
7872
   consume(tRPAREN);
2✔
7873
   consume(tSEMI);
2✔
7874

7875
   tree_set_loc(g, CURRENT_LOC);
2✔
7876
   insert_name(nametab, g, NULL);
2✔
7877

7878
   return g;
2✔
7879
}
7880

7881
static void p_protected_type_body_declarative_item(tree_t body)
757✔
7882
{
7883
   // subprogram_declaration | subprogram_body | type_declaration
7884
   //   | subtype_declaration | constant_declaration | variable_declaration
7885
   //   | file_declaration | alias_declaration | attribute_declaration
7886
   //   | attribute_specification | use_clause | group_template_declaration
7887
   //   | group_declaration | 2008: subprogram_instantiation_declaration
7888

7889
   BEGIN("protected type body declarative item");
1,514✔
7890

7891
   switch (peek()) {
757✔
7892
   case tATTRIBUTE:
×
7893
      if (peek_nth(3) == tOF)
×
7894
         p_attribute_specification(body);
×
7895
      else
7896
         tree_add_decl(body, p_attribute_declaration());
×
7897
      break;
7898

7899
   case tTYPE:
40✔
7900
      p_type_declaration(body);
40✔
7901
      break;
40✔
7902

7903
   case tSUBTYPE:
×
7904
      tree_add_decl(body, p_subtype_declaration());
×
7905
      break;
×
7906

7907
   case tCONSTANT:
7✔
7908
      p_constant_declaration(body);
7✔
7909
      break;
7✔
7910

7911
   case tALIAS:
×
7912
      p_alias_declaration(body);
×
7913
      break;
×
7914

7915
   case tFUNCTION:
461✔
7916
   case tPROCEDURE:
7917
   case tIMPURE:
7918
   case tPURE:
7919
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
461✔
7920
         tree_add_decl(body, p_subprogram_instantiation_declaration());
×
7921
      else {
7922
         tree_t spec = p_subprogram_specification();
461✔
7923
         tree_set_flag(spec, TREE_F_PROTECTED);
461✔
7924
         if (peek() == tSEMI)
461✔
7925
            tree_add_decl(body, p_subprogram_declaration(spec));
5✔
7926
         else
7927
            tree_add_decl(body, p_subprogram_body(spec));
456✔
7928
      }
7929
      break;
7930

7931
   case tVARIABLE:
240✔
7932
      p_variable_declaration(body);
240✔
7933
      break;
240✔
7934

7935
   case tUSE:
×
7936
      p_use_clause(body, tree_add_decl);
×
7937
      break;
×
7938

7939
   case tFILE:
9✔
7940
      p_file_declaration(body);
9✔
7941
      break;
9✔
7942

7943
   case tGROUP:
×
7944
      if (peek_nth(3) == tIS)
×
7945
         tree_add_decl(body, p_group_template_declaration());
×
7946
      else
7947
         tree_add_decl(body, p_group_declaration());
×
7948
      break;
7949

7950
   default:
×
7951
      expect(tATTRIBUTE, tTYPE, tSUBTYPE, tCONSTANT, tFUNCTION, tPROCEDURE,
×
7952
             tIMPURE, tPURE, tALIAS, tVARIABLE, tUSE, tFILE, tGROUP);
7953
   }
7954
}
757✔
7955

7956
static void p_protected_type_body_declarative_part(tree_t body)
209✔
7957
{
7958
   // { protected_type_body_declarative_item }
7959

7960
   BEGIN("protected type body declarative part");
418✔
7961

7962
   while (not_at_token(tEND))
966✔
7963
      p_protected_type_body_declarative_item(body);
757✔
7964
}
209✔
7965

7966
static tree_t p_protected_type_body(ident_t id)
209✔
7967
{
7968
   // protected body protected_type_body_declarative_part end protected body
7969
   //   [ simple name ]
7970

7971
   BEGIN("protected type body");
209✔
7972

7973
   consume(tPROTECTED);
209✔
7974
   consume(tBODY);
209✔
7975

7976
   tree_t body = tree_new(T_PROT_BODY);
209✔
7977
   tree_set_ident(body, id);
209✔
7978
   tree_set_loc(body, CURRENT_LOC);
209✔
7979

7980
   tree_t decl = resolve_name(nametab, CURRENT_LOC, id);
209✔
7981
   if (decl != NULL) {
209✔
7982
      switch (tree_kind(decl)) {
206✔
7983
      case T_PROT_BODY:   // Duplicate body will trigger an error later
2✔
7984
         if (!tree_has_primary(decl)) {
2✔
7985
            assert(error_count() > 0); // Fallout from previous error, ignore
1✔
7986
            decl = NULL;
7987
            break;
7988
         }
7989
         decl = tree_primary(decl);
1✔
7990
         // Fall-through
7991
      case T_PROT_DECL:
203✔
7992
         tree_set_primary(body, decl);
203✔
7993
         break;
203✔
7994
      default:
2✔
7995
         parse_error(CURRENT_LOC, "object %s is not a protected type "
2✔
7996
                     "declaration", istr(id));
7997
         decl = NULL;
7998
      }
7999
   }
8000

8001
   if (decl == NULL)
206✔
8002
      tree_set_type(body, type_new(T_NONE));
6✔
8003
   else {
8004
      type_t type = tree_type(decl);
203✔
8005
      assert(type_is_protected(type));
203✔
8006
      tree_set_type(body, type);
203✔
8007
   }
8008

8009
   insert_name(nametab, body, NULL);
209✔
8010

8011
   push_scope(nametab);
209✔
8012

8013
   if (decl != NULL) insert_decls(nametab, decl);
209✔
8014

8015
   scope_set_prefix(nametab, id);
209✔
8016
   scope_set_container(nametab, body);
209✔
8017

8018
   p_protected_type_body_declarative_part(body);
209✔
8019

8020
   consume(tEND);
209✔
8021
   consume(tPROTECTED);
209✔
8022
   consume(tBODY);
209✔
8023

8024
   p_trailing_label(id);
209✔
8025

8026
   tree_set_loc(body, CURRENT_LOC);
209✔
8027
   sem_check(body, nametab);
209✔
8028

8029
   pop_scope(nametab);
209✔
8030
   return body;
209✔
8031
}
8032

8033
static tree_t p_package_instantiation_declaration(tree_t unit)
284✔
8034
{
8035
   // 2008: package identifier is new name [ generic_map_aspect ] ;
8036

8037
   consume(tPACKAGE);
284✔
8038

8039
   ident_t id = p_identifier();
284✔
8040

8041
   consume(tIS);
284✔
8042
   consume(tNEW);
284✔
8043

8044
   require_std(STD_08, "package instantiation declarations");
284✔
8045

8046
   ident_t unit_name = p_selected_identifier();
284✔
8047
   tree_t pack = resolve_name(nametab, CURRENT_LOC, unit_name);
284✔
8048

8049
   if (pack != NULL && !is_uninstantiated_package(pack)) {
284✔
8050
      parse_error(CURRENT_LOC, "unit %s is not an uninstantiated package",
2✔
8051
                  istr(unit_name));
8052
      pack = NULL;
8053
   }
8054

8055
   tree_t new;
284✔
8056
   if (unit != NULL) {
284✔
8057
      // Package instantiation declaration as primary unit
8058
      assert(tree_kind(unit) == T_DESIGN_UNIT);
76✔
8059
      tree_change_kind(unit, T_PACK_INST);
76✔
8060
      new = unit;
76✔
8061

8062
      ident_t qual = ident_prefix(lib_name(lib_work()), id, '.');
76✔
8063
      tree_set_ident(new, qual);
76✔
8064
   }
8065
   else {
8066
      new = tree_new(T_PACK_INST);
208✔
8067
      tree_set_ident(new, id);
208✔
8068
   }
8069

8070
   tree_t body = NULL;
284✔
8071
   if (pack != NULL) {
284✔
8072
      if (package_needs_body(pack) && (body = body_of(pack)) == NULL)
281✔
8073
         parse_error(CURRENT_LOC, "package %s cannot be instantiated until "
×
8074
                     "its body has been analysed", istr(unit_name));
8075

8076
      instantiate_package(new, pack, body);
281✔
8077
   }
8078

8079
   tree_set_ref(new, pack);
284✔
8080

8081
   if (peek() == tGENERIC)
284✔
8082
      p_generic_map_aspect(new, new);
267✔
8083

8084
   consume(tSEMI);
284✔
8085

8086
   tree_set_loc(new, CURRENT_LOC);
284✔
8087
   insert_name(nametab, new, NULL);
284✔
8088

8089
   sem_check(new, nametab);
284✔
8090

8091
   hash_t *map = get_generic_map(nametab);
284✔
8092
   if (map != NULL)
284✔
8093
      instance_fixup(new, map);
252✔
8094

8095
   return new;
284✔
8096
}
8097

8098
static tree_t p_element_array_mode_view_indication(void)
2✔
8099
{
8100
   // view ( name )
8101

8102
   BEGIN("element array mode view indication");
2✔
8103

8104
   consume(tVIEW);
2✔
8105
   consume(tLPAREN);
2✔
8106

8107
   tree_t name = p_name(0);
2✔
8108
   solve_types(nametab, name, NULL);
2✔
8109

8110
   consume(tRPAREN);
2✔
8111

8112
   return name;
2✔
8113
}
8114

8115
static tree_t p_element_record_mode_view_indication(void)
16✔
8116
{
8117
   // view name
8118

8119
   BEGIN("element record mode view indication");
16✔
8120

8121
   consume(tVIEW);
16✔
8122

8123
   tree_t name = p_name(0);
16✔
8124
   solve_types(nametab, name, NULL);
16✔
8125

8126
   return name;
16✔
8127
}
8128

8129
static port_mode_t p_element_mode_view_indication(tree_t *name)
18✔
8130
{
8131
   // element_record_mode_view_indication | element_array_mode_view_indication
8132

8133
   BEGIN("element mode view indication");
36✔
8134

8135
   if (peek_nth(2) == tLPAREN) {
18✔
8136
      *name = p_element_array_mode_view_indication();
2✔
8137
      return PORT_ARRAY_VIEW;
2✔
8138
   }
8139
   else {
8140
      *name = p_element_record_mode_view_indication();
16✔
8141
      return PORT_RECORD_VIEW;
16✔
8142
   }
8143
}
8144

8145
static void p_mode_view_element_declaration(type_t view, type_t of)
154✔
8146
{
8147
   // record_element_list : element_mode_indication ;
8148

8149
   BEGIN("mode view element declaration");
308✔
8150

8151
   LOCAL_IDENT_LIST ids = p_identifier_list();
308✔
8152

8153
   consume(tCOLON);
154✔
8154

8155
   tree_t name = NULL;
154✔
8156

8157
   port_mode_t mode;
154✔
8158
   if (peek() == tVIEW)
154✔
8159
      mode = p_element_mode_view_indication(&name);
18✔
8160
   else
8161
      mode = p_mode();
136✔
8162

8163
   for (ident_list_t *it = ids; it != NULL; it = it->next) {
322✔
8164
      tree_t elt = tree_new(T_VIEW_ELEMENT);
168✔
8165
      tree_set_ident(elt, it->ident);
168✔
8166
      tree_set_loc(elt, &(it->loc));
168✔
8167
      tree_set_subkind(elt, mode);
168✔
8168
      tree_set_value(elt, name);
168✔
8169

8170
      type_add_field(view, elt);
168✔
8171

8172
      tree_t f = resolve_field_name(nametab, &(it->loc), it->ident, of);
168✔
8173
      if (f == NULL)
168✔
8174
         tree_set_type(elt, type_new(T_NONE));
2✔
8175
      else {
8176
         tree_set_ref(elt, f);
166✔
8177
         tree_set_type(elt, tree_type(f));
166✔
8178
      }
8179
   }
8180

8181
   consume(tSEMI);
154✔
8182
}
154✔
8183

8184
static tree_t p_mode_view_declaration(void)
73✔
8185
{
8186
   // view identifier of subtype_indication is
8187
   //   { mode_view_element_definition } end view [ mode_view_simple_name ] ;
8188

8189
   BEGIN("mode view declaration");
73✔
8190

8191
   consume(tVIEW);
73✔
8192

8193
   tree_t view = tree_new(T_VIEW_DECL);
73✔
8194

8195
   ident_t id = p_identifier();
73✔
8196
   tree_set_ident(view, id);
73✔
8197

8198
   consume(tOF);
73✔
8199

8200
   type_t of = p_subtype_indication();
73✔
8201

8202
   type_t type = type_new(T_VIEW);
73✔
8203
   type_set_ident(type, id);
73✔
8204
   type_set_designated(type, of);
73✔
8205

8206
   tree_set_type(view, type);
73✔
8207

8208
   consume(tIS);
73✔
8209

8210
   if (type_is_record(of)) {
73✔
8211
      while (not_at_token(tEND))
226✔
8212
         p_mode_view_element_declaration(type, of);
154✔
8213

8214
      consume(tEND);
72✔
8215
   }
8216
   else {
8217
      parse_error(CURRENT_LOC, "subtype indication of a mode view declaration "
1✔
8218
                  "must denote a record type");
8219
      drop_tokens_until(tEND);
1✔
8220
   }
8221

8222
   consume(tVIEW);
73✔
8223

8224
   p_trailing_label(id);
73✔
8225

8226
   consume(tSEMI);
73✔
8227

8228
   tree_set_loc(view, CURRENT_LOC);
73✔
8229
   insert_name(nametab, view, NULL);
73✔
8230
   sem_check(view, nametab);
73✔
8231
   return view;
73✔
8232
}
8233

8234
static void p_entity_declarative_item(tree_t entity)
93✔
8235
{
8236
   // subprogram_declaration | subprogram_body | type_declaration
8237
   //   | subtype_declaration | constant_declaration | signal_declaration
8238
   //   | shared_variable_declaration | file_declaration | alias_declaration
8239
   //   | attribute_declaration | attribute_specification
8240
   //   | disconnection_specification | use_clause | group_template_declaration
8241
   //   | group_declaration | 2008: subprogram_instantiation_declaration
8242
   //   | 2008: package_instantiation_declaration | 2019: mode_view_declaration
8243

8244
   BEGIN("entity declarative item");
186✔
8245

8246
   switch (peek()) {
93✔
8247
   case tATTRIBUTE:
19✔
8248
      if (peek_nth(3) == tOF)
19✔
8249
         p_attribute_specification(entity);
10✔
8250
      else
8251
         tree_add_decl(entity, p_attribute_declaration());
9✔
8252
      break;
8253

8254
   case tTYPE:
1✔
8255
      p_type_declaration(entity);
1✔
8256
      break;
1✔
8257

8258
   case tSUBTYPE:
7✔
8259
      tree_add_decl(entity, p_subtype_declaration());
7✔
8260
      break;
7✔
8261

8262
   case tCONSTANT:
10✔
8263
      p_constant_declaration(entity);
10✔
8264
      break;
10✔
8265

8266
   case tALIAS:
1✔
8267
      p_alias_declaration(entity);
1✔
8268
      break;
1✔
8269

8270
   case tFUNCTION:
31✔
8271
   case tPROCEDURE:
8272
   case tIMPURE:
8273
   case tPURE:
8274
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
31✔
8275
         tree_add_decl(entity, p_subprogram_instantiation_declaration());
×
8276
      else {
8277
         tree_t spec = p_subprogram_specification();
31✔
8278
         if (peek() == tSEMI)
31✔
8279
            tree_add_decl(entity, p_subprogram_declaration(spec));
9✔
8280
         else
8281
            tree_add_decl(entity, p_subprogram_body(spec));
22✔
8282
      }
8283
      break;
8284

8285
   case tUSE:
6✔
8286
      p_use_clause(entity, tree_add_decl);
6✔
8287
      break;
6✔
8288

8289
   case tDISCONNECT:
1✔
8290
      p_disconnection_specification(entity);
1✔
8291
      break;
1✔
8292

8293
   case tGROUP:
×
8294
      if (peek_nth(3) == tIS)
×
8295
         tree_add_decl(entity, p_group_template_declaration());
×
8296
      else
8297
         tree_add_decl(entity, p_group_declaration());
×
8298
      break;
8299

8300
   case tSHARED:
3✔
8301
      p_variable_declaration(entity);
3✔
8302
      break;
3✔
8303

8304
   case tSIGNAL:
6✔
8305
      p_signal_declaration(entity);
6✔
8306
      break;
6✔
8307

8308
   case tPACKAGE:
3✔
8309
      if (peek_nth(4) == tNEW)
3✔
8310
         tree_add_decl(entity, p_package_instantiation_declaration(NULL));
3✔
8311
      else {
8312
         require_std(STD_08, "nested package declarations");
×
8313
         tree_add_decl(entity, p_package_declaration(NULL));
×
8314
      }
8315
      break;
8316

8317
   case tVIEW:
×
8318
      tree_add_decl(entity, p_mode_view_declaration());
×
8319
      break;
×
8320

8321
   default:
5✔
8322
      expect(tATTRIBUTE, tTYPE, tSUBTYPE, tCONSTANT, tFUNCTION, tPROCEDURE,
10✔
8323
             tIMPURE, tPURE, tALIAS, tUSE, tDISCONNECT, tGROUP, tSHARED,
8324
             tSIGNAL, STD(08, tPACKAGE), STD(19, tVIEW));
8325
   }
8326
}
93✔
8327

8328
static void p_entity_declarative_part(tree_t entity)
5,201✔
8329
{
8330
   // { entity_declarative_item }
8331

8332
   BEGIN("entity declarative part");
10,402✔
8333

8334
   while (not_at_token(tEND, tBEGIN))
5,294✔
8335
      p_entity_declarative_item(entity);
93✔
8336
}
5,201✔
8337

8338
static void p_subprogram_declarative_item(tree_t sub)
9,397✔
8339
{
8340
   // subprogram_declaration | subprogram_body | type_declaration
8341
   //   | subtype_declaration | constant_declaration | variable_declaration
8342
   //   | file_declaration | alias_declaration | attribute_declaration
8343
   //   | attribute_specification | use_clause | group_template_declaration
8344
   //   | group_declaration | 2008: subprogram_instantiation_declaration
8345

8346
   BEGIN("subprogram declarative item");
18,794✔
8347

8348
   switch (peek()) {
9,397✔
8349
   case tVARIABLE:
6,594✔
8350
      p_variable_declaration(sub);
6,594✔
8351
      break;
6,594✔
8352

8353
   case tTYPE:
83✔
8354
      p_type_declaration(sub);
83✔
8355
      break;
83✔
8356

8357
   case tALIAS:
726✔
8358
      p_alias_declaration(sub);
726✔
8359
      break;
726✔
8360

8361
   case tCONSTANT:
1,770✔
8362
      p_constant_declaration(sub);
1,770✔
8363
      break;
1,770✔
8364

8365
   case tFUNCTION:
110✔
8366
   case tPROCEDURE:
8367
   case tIMPURE:
8368
   case tPURE:
8369
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
110✔
8370
         tree_add_decl(sub, p_subprogram_instantiation_declaration());
1✔
8371
      else {
8372
         tree_t spec = p_subprogram_specification();
109✔
8373
         tree_set_flag(spec, tree_flags(sub) & TREE_F_PROTECTED);
109✔
8374
         if (peek() == tSEMI)
109✔
8375
            tree_add_decl(sub, p_subprogram_declaration(spec));
×
8376
         else
8377
            tree_add_decl(sub, p_subprogram_body(spec));
109✔
8378
      }
8379
      break;
8380

8381
   case tATTRIBUTE:
13✔
8382
      if (peek_nth(3) == tOF)
13✔
8383
         p_attribute_specification(sub);
9✔
8384
      else
8385
         tree_add_decl(sub, p_attribute_declaration());
4✔
8386
      break;
8387

8388
   case tSUBTYPE:
56✔
8389
      tree_add_decl(sub, p_subtype_declaration());
56✔
8390
      break;
56✔
8391

8392
   case tUSE:
9✔
8393
      p_use_clause(sub, tree_add_decl);
9✔
8394
      break;
9✔
8395

8396
   case tFILE:
28✔
8397
      p_file_declaration(sub);
28✔
8398
      break;
28✔
8399

8400
   case tGROUP:
×
8401
      if (peek_nth(3) == tIS)
×
8402
         tree_add_decl(sub, p_group_template_declaration());
×
8403
      else
8404
         tree_add_decl(sub, p_group_declaration());
×
8405
      break;
8406

8407
   default:
8✔
8408
      expect(tVARIABLE, tTYPE, tALIAS, tCONSTANT, tFUNCTION, tPROCEDURE,
8✔
8409
             tIMPURE, tPURE, tATTRIBUTE, tSUBTYPE, tUSE, tFILE, tGROUP);
8410
   }
8411
}
9,397✔
8412

8413
static void p_subprogram_declarative_part(tree_t sub)
9,544✔
8414
{
8415
   // { subprogram_declarative_item }
8416

8417
   BEGIN("subprogram declarative part");
19,088✔
8418

8419
   while (not_at_token(tBEGIN))
18,941✔
8420
      p_subprogram_declarative_item(sub);
9,397✔
8421
}
9,544✔
8422

8423
static void p_sequence_of_statements(tree_t parent)
31,965✔
8424
{
8425
   // { sequential_statement }
8426

8427
   BEGIN("sequence of statements");
63,927✔
8428

8429
   while (not_at_token(tEND, tELSE, tELSIF, tWHEN))
116,385✔
8430
      tree_add_stmt(parent, p_sequential_statement());
84,423✔
8431
}
31,962✔
8432

8433
static void p_trailing_label(ident_t label)
41,236✔
8434
{
8435
   // [ label ]
8436

8437
   if ((peek() == tID) || (peek() == tSTRING)) {
41,236✔
8438
      ident_t trailing = p_designator();
6,958✔
8439
      if (label == NULL)
6,958✔
8440
         parse_error(&last_loc, "unexpected trailing label for %s without "
3✔
8441
                     "label", hint_str);
8442
      else if (trailing != label)
6,955✔
8443
         parse_error(&last_loc, "expected trailing %s label to match %s",
7✔
8444
                     hint_str, istr(label));
8445
   }
8446
}
41,236✔
8447

8448
static tree_t p_subprogram_body(tree_t spec)
9,544✔
8449
{
8450
   // subprogram_specification is subprogram_declarative_part begin
8451
   //   subprogram_statement_part end [ subprogram_kind ] [ designator ] ;
8452

8453
   EXTEND("subprogram body");
9,544✔
8454

8455
   consume(tIS);
9,544✔
8456

8457
   const tree_kind_t kind =
19,088✔
8458
      (tree_kind(spec) == T_FUNC_DECL) ? T_FUNC_BODY : T_PROC_BODY;
9,544✔
8459
   tree_change_kind(spec, kind);
9,544✔
8460

8461
   insert_name(nametab, spec, NULL);
9,544✔
8462

8463
   push_scope(nametab);
9,544✔
8464
   scope_set_subprogram(nametab, spec);
9,544✔
8465

8466
   insert_generics(nametab, spec);
9,544✔
8467
   insert_ports(nametab, spec);
9,544✔
8468

8469
   sem_check(spec, nametab);
9,544✔
8470

8471
   if (tree_flags(spec) & TREE_F_KNOWS_SUBTYPE) {
9,544✔
8472
      // LRM 19 section 4.2.1: an implicit subtype declaration is
8473
      // created as the first declarative item when the function
8474
      // includes a return identifier
8475

8476
      type_t sub = type_result(tree_type(spec));
15✔
8477
      assert(type_kind(sub) == T_SUBTYPE);
15✔
8478

8479
      tree_t d = tree_new(T_SUBTYPE_DECL);
15✔
8480
      tree_set_ident(d, type_ident(sub));
15✔
8481
      tree_set_type(d, sub);
15✔
8482
      tree_set_loc(d, CURRENT_LOC);
15✔
8483

8484
      insert_name(nametab, d, NULL);
15✔
8485

8486
      tree_add_decl(spec, d);
15✔
8487
   }
8488

8489
   p_subprogram_declarative_part(spec);
9,544✔
8490

8491
   consume(tBEGIN);
9,544✔
8492

8493
   p_sequence_of_statements(spec);
9,544✔
8494

8495
   consume(tEND);
9,544✔
8496

8497
   pop_scope(nametab);
9,544✔
8498

8499
   if (scan(tFUNCTION, tPROCEDURE))
9,544✔
8500
      consume(kind == T_FUNC_BODY ? tFUNCTION : tPROCEDURE);
8,506✔
8501

8502
   p_trailing_label(tree_ident(spec));
9,544✔
8503
   consume(tSEMI);
9,544✔
8504

8505
   tree_set_loc(spec, CURRENT_LOC);
9,544✔
8506
   return spec;
9,544✔
8507
}
8508

8509
static tree_t p_subprogram_declaration(tree_t spec)
7,043✔
8510
{
8511
   // subprogram_specification ;
8512

8513
   EXTEND("subprogram declaration");
7,043✔
8514

8515
   insert_name(nametab, spec, NULL);
7,043✔
8516

8517
   consume(tSEMI);
7,043✔
8518

8519
   tree_set_loc(spec, CURRENT_LOC);
7,043✔
8520
   sem_check(spec, nametab);
7,043✔
8521
   return spec;
7,043✔
8522
}
8523

8524
static void p_sensitivity_list(tree_t proc)
783✔
8525
{
8526
   // name { , name }
8527

8528
   BEGIN("sensitivity list");
1,566✔
8529

8530
   do {
1,143✔
8531
      tree_t name = p_name(0);
1,143✔
8532
      tree_add_trigger(proc, name);
1,143✔
8533
      solve_types(nametab, name, NULL);
1,143✔
8534
   } while (optional(tCOMMA));
1,143✔
8535
}
783✔
8536

8537
static void p_process_declarative_item(tree_t proc)
4,934✔
8538
{
8539
   // subprogram_declaration | subprogram_body | type_declaration
8540
   //   | subtype_declaration | constant_declaration | variable_declaration
8541
   //   | file_declaration | alias_declaration | attribute_declaration
8542
   //   | attribute_specification | use_clause | group_template_declaration
8543
   //   | group_declaration | 2008: subprogram_instantiation_declaration
8544

8545
   BEGIN("process declarative item");
9,868✔
8546

8547
   switch (peek()) {
4,934✔
8548
   case tVARIABLE:
3,217✔
8549
      p_variable_declaration(proc);
3,217✔
8550
      break;
3,217✔
8551

8552
   case tTYPE:
277✔
8553
      p_type_declaration(proc);
277✔
8554
      break;
277✔
8555

8556
   case tSUBTYPE:
103✔
8557
      tree_add_decl(proc, p_subtype_declaration());
103✔
8558
      break;
103✔
8559

8560
   case tCONSTANT:
706✔
8561
      p_constant_declaration(proc);
706✔
8562
      break;
706✔
8563

8564
   case tFUNCTION:
413✔
8565
   case tPROCEDURE:
8566
   case tIMPURE:
8567
   case tPURE:
8568
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
413✔
8569
         tree_add_decl(proc, p_subprogram_instantiation_declaration());
×
8570
      else {
8571
         tree_t spec = p_subprogram_specification();
413✔
8572
         if (peek() == tSEMI)
413✔
8573
            tree_add_decl(proc, p_subprogram_declaration(spec));
91✔
8574
         else
8575
            tree_add_decl(proc, p_subprogram_body(spec));
322✔
8576
      }
8577
      break;
8578

8579
   case tATTRIBUTE:
38✔
8580
      if (peek_nth(3) == tOF)
38✔
8581
         p_attribute_specification(proc);
19✔
8582
      else
8583
         tree_add_decl(proc, p_attribute_declaration());
19✔
8584
      break;
8585

8586
   case tUSE:
5✔
8587
      p_use_clause(proc, tree_add_decl);
5✔
8588
      break;
5✔
8589

8590
   case tALIAS:
92✔
8591
      p_alias_declaration(proc);
92✔
8592
      break;
92✔
8593

8594
   case tFILE:
64✔
8595
      p_file_declaration(proc);
64✔
8596
      break;
64✔
8597

8598
   case tGROUP:
2✔
8599
      if (peek_nth(3) == tIS)
2✔
8600
         tree_add_decl(proc, p_group_template_declaration());
1✔
8601
      else
8602
         tree_add_decl(proc, p_group_declaration());
1✔
8603
      break;
8604

8605
   default:
17✔
8606
      expect(tVARIABLE, tTYPE, tSUBTYPE, tCONSTANT, tFUNCTION, tPROCEDURE,
17✔
8607
             tIMPURE, tPURE, tATTRIBUTE, tUSE, tALIAS, tFILE, tGROUP);
8608
   }
8609
}
4,934✔
8610

8611
static void p_process_declarative_part(tree_t proc)
5,004✔
8612
{
8613
   // { process_declarative_item }
8614

8615
   BEGIN("process declarative part");
10,008✔
8616

8617
   while (not_at_token(tBEGIN))
9,925✔
8618
      p_process_declarative_item(proc);
4,921✔
8619
}
5,004✔
8620

8621
static void p_process_statement_part(tree_t proc)
5,004✔
8622
{
8623
   // { sequential_statement }
8624

8625
   BEGIN("process statement part");
10,005✔
8626

8627
   p_sequence_of_statements(proc);
5,004✔
8628
}
5,001✔
8629

8630
static void p_process_sensitivity_list(tree_t proc)
213✔
8631
{
8632
   // 2008: all | sensitivity_list
8633

8634
   BEGIN("process sensitivity list");
426✔
8635

8636
   if (peek() == tALL) {
213✔
8637
      consume(tALL);
50✔
8638

8639
      tree_t all = tree_new(T_ALL);
50✔
8640
      tree_set_loc(all, CURRENT_LOC);
50✔
8641
      tree_add_trigger(proc, all);
50✔
8642
   }
8643
   else
8644
      p_sensitivity_list(proc);
163✔
8645
}
213✔
8646

8647
static tree_t p_process_statement(ident_t label)
5,004✔
8648
{
8649
   // [ process_label : ] [ postponed ] process [ ( sensitivity_list ) ] [ is ]
8650
   //   process_declarative_part begin process_statement_part end [ postponed ]
8651
   //   process [ label ] ;
8652

8653
   EXTEND("process statement");
5,004✔
8654

8655
   tree_t t = tree_new(T_PROCESS);
5,004✔
8656

8657
   const bool postponed = optional(tPOSTPONED);
5,004✔
8658

8659
   consume(tPROCESS);
5,004✔
8660

8661
   if (optional(tLPAREN)) {
5,004✔
8662
      if (standard() < STD_08)
472✔
8663
         p_sensitivity_list(t);
259✔
8664
      else
8665
         p_process_sensitivity_list(t);
213✔
8666
      consume(tRPAREN);
472✔
8667
   }
8668

8669
   optional(tIS);
5,004✔
8670

8671
   if (label == NULL) {
5,004✔
8672
      tree_set_ident(t, get_implicit_label(t, nametab));
2,104✔
8673
      tree_set_flag(t, TREE_F_SYNTHETIC_NAME);
2,104✔
8674
   }
8675
   else {
8676
      tree_set_loc(t, CURRENT_LOC);
2,900✔
8677
      tree_set_ident(t, label);
2,900✔
8678
      insert_name(nametab, t, label);
2,900✔
8679
   }
8680

8681
   push_scope(nametab);
5,004✔
8682
   scope_set_container(nametab, t);
5,004✔
8683
   scope_set_prefix(nametab, tree_ident(t));
5,004✔
8684

8685
   p_process_declarative_part(t);
5,004✔
8686

8687
   consume(tBEGIN);
5,004✔
8688

8689
   p_process_statement_part(t);
5,004✔
8690

8691
   consume(tEND);
5,001✔
8692
   if (postponed)
5,001✔
8693
      optional(tPOSTPONED);
25✔
8694
   consume(tPROCESS);
5,001✔
8695

8696
   p_trailing_label(label);
5,001✔
8697

8698
   consume(tSEMI);
5,001✔
8699

8700
   pop_scope(nametab);
5,001✔
8701

8702
   tree_set_loc(t, CURRENT_LOC);
5,001✔
8703

8704
   if (postponed)
5,001✔
8705
      tree_set_flag(t, TREE_F_POSTPONED);
25✔
8706

8707
   sem_check(t, nametab);
5,001✔
8708
   return t;
5,001✔
8709
}
8710

8711
static tree_t p_entity_statement(void)
37✔
8712
{
8713
   // concurrent_assertion_statement | concurrent_procedure_call_statement
8714
   //   | process_statement
8715

8716
   BEGIN("entity statement");
74✔
8717

8718
   ident_t label = NULL;
37✔
8719
   if ((peek() == tID) && (peek_nth(2) == tCOLON)) {
37✔
8720
      label = p_identifier();
4✔
8721
      consume(tCOLON);
4✔
8722
   }
8723

8724
   switch (peek()) {
37✔
8725
   case tASSERT:
29✔
8726
      return p_concurrent_assertion_statement(label);
29✔
8727

8728
   case tPROCESS:
2✔
8729
      return p_process_statement(label);
2✔
8730

8731
   case tID:
3✔
8732
      return p_concurrent_procedure_call_statement(label, NULL);
3✔
8733

8734
   case tPOSTPONED:
3✔
8735
      if (peek_nth(2) == tASSERT)
3✔
8736
         return p_concurrent_assertion_statement(label);
×
8737
      else if (peek_nth(2) == tPROCESS)
3✔
8738
         return p_process_statement(label);
×
8739
      else
8740
         return p_concurrent_procedure_call_statement(label, NULL);
3✔
8741

8742
   default:
×
8743
      expect(tASSERT, tPROCESS, tPOSTPONED);
×
8744
      return tree_new(T_NULL);
×
8745
   }
8746
}
8747

8748
static void p_entity_statement_part(tree_t entity)
64✔
8749
{
8750
   // { entity_statement }
8751

8752
   BEGIN("entity statement part");
128✔
8753

8754
   while (not_at_token(tEND))
101✔
8755
      tree_add_stmt(entity, p_entity_statement());
37✔
8756
}
64✔
8757

8758
static void p_entity_declaration(tree_t unit)
5,201✔
8759
{
8760
   // entity identifier is entity_header entity_declarative_part
8761
   //   [ begin entity_statement_part ] end [ entity ] [ entity_simple_name ] ;
8762

8763
   BEGIN("entity declaration");
10,402✔
8764

8765
   tree_change_kind(unit, T_ENTITY);
5,201✔
8766

8767
   consume(tENTITY);
5,201✔
8768

8769
   ident_t id = p_identifier();
5,201✔
8770
   tree_set_ident(unit, id);
5,201✔
8771

8772
   consume(tIS);
5,201✔
8773

8774
   push_scope(nametab);
5,201✔
8775

8776
   tree_set_loc(unit, CURRENT_LOC);
5,201✔
8777
   insert_name(nametab, unit, id);
5,201✔
8778

8779
   push_scope(nametab);
5,201✔
8780

8781
   ident_t qual = ident_prefix(lib_name(lib_work()), id, '.');
5,201✔
8782
   scope_set_prefix(nametab, qual);
5,201✔
8783

8784
   p_entity_header(unit);
5,201✔
8785
   p_entity_declarative_part(unit);
5,201✔
8786

8787
   if (optional(tBEGIN))
5,201✔
8788
      p_entity_statement_part(unit);
64✔
8789

8790
   consume(tEND);
5,201✔
8791
   optional(tENTITY);
5,201✔
8792
   p_trailing_label(id);
5,201✔
8793
   consume(tSEMI);
5,201✔
8794

8795
   tree_set_ident(unit, qual);
5,201✔
8796
   tree_set_loc(unit, CURRENT_LOC);
5,201✔
8797

8798
   sem_check(unit, nametab);
5,201✔
8799

8800
   pop_scope(nametab);
5,201✔
8801
   pop_scope(nametab);
5,201✔
8802
}
5,201✔
8803

8804
static tree_t p_component_declaration(void)
361✔
8805
{
8806
   // component identifier [ is ] [ generic_clause ] [ port_clause ]
8807
   //   end component [ simple_name ] ;
8808

8809
   // 2019:
8810
   // component identifier [ is ] [ generic_clause ] [ port_clause ]
8811
   //   end [ component ] [ simple_name ] ;
8812

8813
   BEGIN("component declaration");
361✔
8814

8815
   tree_t c = tree_new(T_COMPONENT);
361✔
8816

8817
   consume(tCOMPONENT);
361✔
8818
   tree_set_ident(c, p_identifier());
361✔
8819
   optional(tIS);
361✔
8820

8821
   push_scope(nametab);
361✔
8822

8823
   if (peek() == tGENERIC) {
361✔
8824
      p_generic_clause(c);
151✔
8825
      insert_generics(nametab, c);
151✔
8826
   }
8827

8828
   if (peek() == tPORT) {
361✔
8829
      p_port_clause(c);
256✔
8830
      insert_ports(nametab, c);
256✔
8831
   }
8832

8833
   pop_scope(nametab);
361✔
8834

8835
   consume(tEND);
361✔
8836
   if (peek() != tCOMPONENT)
361✔
8837
      require_std(STD_19, "optional end component");
1✔
8838
   else
8839
      consume(tCOMPONENT);
360✔
8840
   p_trailing_label(tree_ident(c));
361✔
8841
   consume(tSEMI);
361✔
8842

8843
   tree_set_loc(c, CURRENT_LOC);
361✔
8844
   insert_name(nametab, c, NULL);
361✔
8845
   sem_check(c, nametab);
361✔
8846
   return c;
361✔
8847
}
8848

8849
static void p_package_declarative_item(tree_t pack)
12,484✔
8850
{
8851
   // subprogram_declaration | type_declaration | subtype_declaration
8852
   //   | constant_declaration | signal_declaration
8853
   //   | shared_variable_declaration | file_declaration | alias_declaration
8854
   //   | component_declaration | attribute_declaration
8855
   //   | attribute_specification | disconnection_specification | use_clause
8856
   //   | group_template_declaration | group_declaration
8857
   //   | 2008: package_instantiation_declaration
8858
   //   | 2008: package_declaration | 2008: package_body
8859
   //   | 2008: package_instantiation_declaration | 2019: mode_view_declaration
8860
   //
8861

8862
   BEGIN("package declarative item");
24,968✔
8863

8864
   switch (peek()) {
12,484✔
8865
   case tTYPE:
2,166✔
8866
      p_type_declaration(pack);
2,166✔
8867
      break;
2,166✔
8868

8869
   case tFUNCTION:
6,334✔
8870
   case tPROCEDURE:
8871
   case tIMPURE:
8872
   case tPURE:
8873
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
6,334✔
8874
         tree_add_decl(pack, p_subprogram_instantiation_declaration());
42✔
8875
      else {
8876
         tree_t spec = p_subprogram_specification();
6,292✔
8877
         if (peek() == tSEMI)
6,292✔
8878
            tree_add_decl(pack, p_subprogram_declaration(spec));
6,290✔
8879
         else
8880
            tree_add_decl(pack, p_subprogram_body(spec));
2✔
8881
      }
8882
      break;
8883

8884
   case tSUBTYPE:
809✔
8885
      tree_add_decl(pack, p_subtype_declaration());
809✔
8886
      break;
809✔
8887

8888
   case tSIGNAL:
185✔
8889
      p_signal_declaration(pack);
185✔
8890
      break;
185✔
8891

8892
   case tATTRIBUTE:
260✔
8893
      if (peek_nth(3) == tOF)
260✔
8894
         p_attribute_specification(pack);
188✔
8895
      else
8896
         tree_add_decl(pack, p_attribute_declaration());
72✔
8897
      break;
8898

8899
   case tCONSTANT:
1,944✔
8900
      p_constant_declaration(pack);
1,944✔
8901
      break;
1,944✔
8902

8903
   case tCOMPONENT:
41✔
8904
      tree_add_decl(pack, p_component_declaration());
41✔
8905
      break;
41✔
8906

8907
   case tFILE:
14✔
8908
      p_file_declaration(pack);
14✔
8909
      break;
14✔
8910

8911
   case tSHARED:
29✔
8912
      p_variable_declaration(pack);
29✔
8913
      break;
29✔
8914

8915
   case tALIAS:
563✔
8916
      p_alias_declaration(pack);
563✔
8917
      break;
563✔
8918

8919
   case tUSE:
11✔
8920
      p_use_clause(pack, tree_add_decl);
11✔
8921
      break;
11✔
8922

8923
   case tDISCONNECT:
×
8924
      p_disconnection_specification(pack);
×
8925
      break;
×
8926

8927
   case tGROUP:
×
8928
      if (peek_nth(3) == tIS)
×
8929
         tree_add_decl(pack, p_group_template_declaration());
×
8930
      else
8931
         tree_add_decl(pack, p_group_declaration());
×
8932
      break;
8933

8934
   case tPACKAGE:
24✔
8935
      if (peek_nth(4) == tNEW)
24✔
8936
         tree_add_decl(pack, p_package_instantiation_declaration(NULL));
14✔
8937
      else {
8938
         require_std(STD_08, "nested package declarations");
10✔
8939
         tree_add_decl(pack, p_package_declaration(NULL));
10✔
8940
      }
8941
      break;
8942

8943
   case tVIEW:
56✔
8944
      tree_add_decl(pack, p_mode_view_declaration());
56✔
8945
      break;
56✔
8946

8947
   default:
48✔
8948
      expect(tTYPE, tFUNCTION, tPROCEDURE, tIMPURE, tPURE, tSUBTYPE, tSIGNAL,
95✔
8949
             tATTRIBUTE, tCONSTANT, tCOMPONENT, tFILE, tSHARED, tALIAS, tUSE,
8950
             tDISCONNECT, tGROUP, tPACKAGE, STD(19, tVIEW));
8951
   }
8952
}
12,484✔
8953

8954
static void p_package_declarative_part(tree_t pack)
1,499✔
8955
{
8956
   // { package_declarative_item }
8957

8958
   BEGIN("package declarative part");
2,998✔
8959

8960
   while (not_at_token(tEND))
13,983✔
8961
      p_package_declarative_item(pack);
12,484✔
8962
}
1,499✔
8963

8964
static void p_package_header(tree_t unit)
693✔
8965
{
8966
   // 2008: [ generic_clause [ generic_map_aspect ; ] ]
8967

8968
   BEGIN("package header");
1,386✔
8969

8970
   if (peek() == tGENERIC) {
693✔
8971
      p_generic_clause(unit);
223✔
8972

8973
      if (peek() == tGENERIC) {
223✔
8974
         p_generic_map_aspect(unit, unit);
1✔
8975
         consume(tSEMI);
1✔
8976
      }
8977

8978
      insert_generics(nametab, unit);
223✔
8979
   }
8980
}
693✔
8981

8982
static tree_t p_package_declaration(tree_t unit)
1,499✔
8983
{
8984
   // package identifier is package_declarative_part end [ package ]
8985
   //   [ simple_name ] ;
8986
   //
8987
   // 2008: package identifier is package_header package_declarative_part
8988
   //   end [ package ] [ simple_name ] ;
8989

8990
   BEGIN("package declaration");
1,499✔
8991

8992
   consume(tPACKAGE);
1,499✔
8993

8994
   ident_t name = p_identifier(), qual = name;
1,499✔
8995

8996
   tree_t pack;
1,499✔
8997
   if (unit != NULL) {
1,499✔
8998
      // Package declaration as primary unit
8999
      assert(tree_kind(unit) == T_DESIGN_UNIT);
1,484✔
9000
      tree_change_kind(unit, T_PACKAGE);
1,484✔
9001
      pack = unit;
1,484✔
9002

9003

9004
      qual = ident_prefix(lib_name(lib_work()), name, '.');
1,484✔
9005
      scope_set_prefix(nametab, qual);
1,484✔
9006
   }
9007
   else {
9008
      pack = tree_new(T_PACKAGE);
15✔
9009
      scope_set_prefix(nametab, name);
15✔
9010
   }
9011

9012
   tree_set_ident(pack, name);
1,499✔
9013
   tree_set_loc(pack, CURRENT_LOC);
1,499✔
9014

9015
   insert_name(nametab, pack, NULL);
1,499✔
9016

9017
   push_scope(nametab);
1,499✔
9018

9019
   consume(tIS);
1,499✔
9020

9021
   push_scope(nametab);
1,499✔
9022
   scope_set_container(nametab, pack);
1,499✔
9023

9024
   if (standard() >= STD_08)
1,499✔
9025
      p_package_header(pack);
693✔
9026

9027
   p_package_declarative_part(pack);
1,499✔
9028

9029
   if (bootstrapping)
1,499✔
9030
      declare_additional_standard_operators(pack);
3✔
9031

9032
   pop_scope(nametab);
1,499✔
9033

9034
   consume(tEND);
1,499✔
9035
   optional(tPACKAGE);
1,499✔
9036
   p_trailing_label(name);
1,499✔
9037
   consume(tSEMI);
1,499✔
9038

9039
   tree_set_ident(pack, qual);
1,499✔
9040
   tree_set_loc(pack, CURRENT_LOC);
1,499✔
9041

9042
   sem_check(pack, nametab);
1,499✔
9043

9044
   pop_scope(nametab);
1,499✔
9045
   return pack;
1,499✔
9046
}
9047

9048
static ident_list_t *p_instantiation_list(void)
219✔
9049
{
9050
   // label { , label } | others | all
9051

9052
   switch (peek()) {
219✔
9053
   case tID:
166✔
9054
      return p_identifier_list();
166✔
9055

9056
   case tOTHERS:
7✔
9057
      consume(tOTHERS);
7✔
9058
      return NULL;
7✔
9059

9060
   case tALL:
46✔
9061
      {
9062
         consume(tALL);
46✔
9063

9064
         ident_list_t *result = NULL;
46✔
9065
         ident_list_push(&result, well_known(W_ALL), last_loc);
46✔
9066
         return result;
46✔
9067
      }
9068

9069
   default:
×
9070
      expect(tID, tOTHERS, tALL);
×
9071
      return NULL;
×
9072
   }
9073
}
9074

9075
static ident_list_t *p_component_specification(ident_t *comp_name)
219✔
9076
{
9077
   // instantiation_list : name
9078

9079
   BEGIN("component specification");
219✔
9080

9081
   ident_list_t *ids = p_instantiation_list();
219✔
9082
   consume(tCOLON);
219✔
9083
   *comp_name = p_identifier();
219✔
9084

9085
   return ids;
219✔
9086
}
9087

9088
static void p_port_map_aspect(tree_t inst, tree_t unit)
1,405✔
9089
{
9090
   // port map ( association_list )
9091

9092
   BEGIN("port map aspect");
2,810✔
9093

9094
   consume(tPORT);
1,405✔
9095
   consume(tMAP);
1,405✔
9096
   consume(tLPAREN);
1,405✔
9097

9098
   p_association_list(inst, unit, F_PORT_MAP);
1,405✔
9099

9100
   consume(tRPAREN);
1,405✔
9101
}
1,405✔
9102

9103
static void p_generic_map_aspect(tree_t inst, tree_t unit)
1,124✔
9104
{
9105
   // generic map ( association_list )
9106

9107
   BEGIN("generic map aspect");
2,248✔
9108

9109
   consume(tGENERIC);
1,124✔
9110
   consume(tMAP);
1,124✔
9111
   consume(tLPAREN);
1,124✔
9112

9113
   p_association_list(inst, unit, F_GENERIC_MAP);
1,124✔
9114

9115
   consume(tRPAREN);
1,124✔
9116
}
1,124✔
9117

9118
static tree_t p_entity_aspect(void)
216✔
9119
{
9120
   // entity name [ ( identifier) ] | configuration name | open
9121

9122
   switch (one_of(tENTITY, tCONFIGURATION, tOPEN)) {
216✔
9123
   case tENTITY:
158✔
9124
      {
9125
         tree_t bind = tree_new(T_BINDING);
158✔
9126
         tree_set_class(bind, C_ENTITY);
158✔
9127
         tree_set_ident(bind, p_selected_identifier());
158✔
9128
         if (optional(tLPAREN)) {
158✔
9129
            tree_set_ident2(bind, p_identifier());
94✔
9130
            consume(tRPAREN);
94✔
9131
         }
9132
         tree_set_loc(bind, CURRENT_LOC);
158✔
9133

9134
         return bind;
158✔
9135
      }
9136

9137
   case tCONFIGURATION:
49✔
9138
      {
9139
         tree_t bind = tree_new(T_BINDING);
49✔
9140
         tree_set_class(bind, C_CONFIGURATION);
49✔
9141
         tree_set_ident(bind, p_selected_identifier());
49✔
9142
         tree_set_loc(bind, CURRENT_LOC);
49✔
9143

9144
         return bind;
49✔
9145
      }
9146

9147
   case tOPEN:
9148
   default:
9149
      return NULL;
9150
   }
9151
}
9152

9153
static tree_t p_binding_indication(tree_t comp)
218✔
9154
{
9155
   // [ use entity_aspect ] [ generic_map_aspect ] [ port_map_aspect ]
9156

9157
   BEGIN("binding indication");
218✔
9158

9159
   tree_t bind = NULL, unit = NULL;
218✔
9160
   if (optional(tUSE)) {
218✔
9161
      if ((bind = p_entity_aspect()) && (unit = find_binding(bind))) {
216✔
9162
         tree_set_ref(bind, unit);
203✔
9163
         unit = primary_unit_of(unit);
203✔
9164
      }
9165
   }
9166

9167
   if (comp) {
218✔
9168
      insert_generics(nametab, comp);
214✔
9169
      insert_ports(nametab, comp);
214✔
9170
   }
9171

9172
   if (peek() == tGENERIC) {
218✔
9173
      if (bind == NULL) {
55✔
9174
         consume(tGENERIC);
1✔
9175
         parse_error(CURRENT_LOC, "sorry, binding indication with generic map "
1✔
9176
                     "aspect and OPEN entity aspect is not yet supported");
9177
         drop_tokens_until(tRPAREN);
1✔
9178
      }
9179
      else
9180
         p_generic_map_aspect(bind, unit);
54✔
9181
   }
9182

9183
   if (peek() == tPORT) {
218✔
9184
      if (bind == NULL) {
61✔
9185
         consume(tPORT);
×
9186
         parse_error(CURRENT_LOC, "sorry, binding indication with port map "
×
9187
                     "aspect and OPEN entity aspect is not yet supported");
9188
         drop_tokens_until(tRPAREN);
×
9189
      }
9190
      else
9191
         p_port_map_aspect(bind, unit);
61✔
9192
   }
9193

9194
   if (bind != NULL)
218✔
9195
      tree_set_loc(bind, CURRENT_LOC);
207✔
9196

9197
   return bind;
218✔
9198
}
9199

9200
static void p_configuration_specification(tree_t parent)
102✔
9201
{
9202
   // for component_specification binding_indication ;
9203

9204
   BEGIN("configuration specification");
204✔
9205

9206
   consume(tFOR);
102✔
9207

9208
   ident_t comp_name;
102✔
9209
   LOCAL_IDENT_LIST ids = p_component_specification(&comp_name);
204✔
9210

9211
   tree_t comp = resolve_name(nametab, CURRENT_LOC, comp_name);
102✔
9212
   if (comp != NULL && tree_kind(comp) != T_COMPONENT) {
102✔
9213
      parse_error(CURRENT_LOC, "%s does not name a component", istr(comp_name));
1✔
9214
      comp = NULL;
9215
   }
9216

9217
   push_scope(nametab);
102✔
9218

9219
   bool is_open = (peek() == tUSE && peek_nth(2) == tOPEN);
102✔
9220
   tree_t bind = p_binding_indication(comp);
102✔
9221
   if (!is_open && bind == NULL)
102✔
9222
      parse_error(CURRENT_LOC, "a binding indication in an explicit "
1✔
9223
                  "configuration specification must contain an entity aspect");
9224
   consume(tSEMI);
102✔
9225

9226
   if (ids != NULL) {
102✔
9227
      for (ident_list_t *it = ids; it != NULL; it = it->next) {
202✔
9228
         tree_t t = tree_new(T_SPEC);
103✔
9229
         tree_set_loc(t, &(it->loc));
103✔
9230
         tree_set_ident(t, it->ident);
103✔
9231
         tree_set_ident2(t, comp_name);
103✔
9232
         tree_set_value(t, bind);
103✔
9233
         tree_set_ref(t, comp);
103✔
9234

9235
         const spec_kind_t kind =
206✔
9236
            it->ident == well_known(W_ALL) ? SPEC_ALL : SPEC_EXACT;
103✔
9237

9238
         tree_add_decl(parent, t);
103✔
9239
         insert_spec(nametab, t, kind, it->ident, 1);
103✔
9240
         sem_check(t, nametab);
103✔
9241
      }
9242
   }
9243
   else {
9244
      // Instantiation list was "others"
9245
      tree_t t = tree_new(T_SPEC);
3✔
9246
      tree_set_loc(t, CURRENT_LOC);
3✔
9247
      tree_set_ident2(t, comp_name);
3✔
9248
      tree_set_value(t, bind);
3✔
9249
      tree_set_ref(t, comp);
3✔
9250

9251
      tree_add_decl(parent, t);
3✔
9252
      insert_spec(nametab, t, SPEC_OTHERS, NULL, 1);
3✔
9253
      sem_check(t, nametab);
3✔
9254
   }
9255

9256
   pop_scope(nametab);
102✔
9257
}
102✔
9258

9259
static void p_configuration_declarative_part(tree_t unit)
6✔
9260
{
9261
   // use_clause | attribute_specification | group_declaration
9262

9263
   BEGIN("configuration declarative part");
12✔
9264

9265
   switch (peek()) {
6✔
9266
   case tUSE:
3✔
9267
      p_use_clause(unit, tree_add_decl);
3✔
9268
      break;
3✔
9269

9270
   case tATTRIBUTE:
3✔
9271
      p_attribute_specification(unit);
3✔
9272
      break;
3✔
9273

9274
   case tGROUP:
×
9275
      if (peek_nth(3) == tIS)
×
9276
         tree_add_decl(unit, p_group_template_declaration());
×
9277
      else
9278
         tree_add_decl(unit, p_group_declaration());
×
9279
      break;
9280

9281
   default:
×
9282
      expect(tUSE, tATTRIBUTE, tGROUP);
×
9283
   }
9284
}
6✔
9285

9286
static void p_component_configuration(tree_t unit)
117✔
9287
{
9288
   // for component_specification [ binding_indication ; ]
9289
   //   [ block_configuration ] end for ;
9290

9291
   BEGIN("component configuration");
234✔
9292

9293
   consume(tFOR);
117✔
9294

9295
   ident_t comp_name;
117✔
9296
   LOCAL_IDENT_LIST ids = p_component_specification(&comp_name);
234✔
9297

9298
   tree_t comp = resolve_name(nametab, CURRENT_LOC, comp_name);
117✔
9299
   if (comp != NULL && tree_kind(comp) != T_COMPONENT) {
117✔
9300
      parse_error(CURRENT_LOC, "%s does not name a component", istr(comp_name));
×
9301
      comp = NULL;
9302
   }
9303

9304
   push_scope(nametab);
117✔
9305

9306
   tree_t bind = NULL;
117✔
9307
   if (peek() != tEND && peek() != tFOR) {
117✔
9308
      bind = p_binding_indication(comp);
116✔
9309
      consume(tSEMI);
116✔
9310
   }
9311

9312
   tree_t bcfg = NULL;
117✔
9313
   if (peek() == tFOR) {
117✔
9314
      tree_t of = bind != NULL && tree_has_ref(bind) ? tree_ref(bind) : NULL;
2✔
9315
      bcfg = p_block_configuration(of);
2✔
9316
   }
9317

9318
   if (ids != NULL) {
117✔
9319
      for (ident_list_t *it = ids; it != NULL; it = it->next) {
226✔
9320
         tree_t t = tree_new(T_SPEC);
113✔
9321
         tree_set_loc(t, &(it->loc));
113✔
9322
         tree_set_ident(t, it->ident);
113✔
9323
         tree_set_ident2(t, comp_name);
113✔
9324
         tree_set_value(t, bind);
113✔
9325
         tree_set_ref(t, comp);
113✔
9326
         if (bcfg != NULL) tree_add_decl(t, bcfg);
113✔
9327

9328
         const spec_kind_t kind =
226✔
9329
            it->ident == well_known(W_ALL) ? SPEC_ALL : SPEC_EXACT;
113✔
9330

9331
         tree_add_decl(unit, t);
113✔
9332
         sem_check(t, nametab);
113✔
9333
         insert_spec(nametab, t, kind, it->ident, 1);
113✔
9334
      }
9335
   }
9336
   else {
9337
      // Instantiation list was "others"
9338
      tree_t t = tree_new(T_SPEC);
4✔
9339
      tree_set_loc(t, CURRENT_LOC);
4✔
9340
      tree_set_ident2(t, comp_name);
4✔
9341
      tree_set_value(t, bind);
4✔
9342
      tree_set_ref(t, comp);
4✔
9343
      if (bcfg != NULL) tree_add_decl(t, bcfg);
4✔
9344

9345
      tree_add_decl(unit, t);
4✔
9346
      sem_check(t, nametab);
4✔
9347
      insert_spec(nametab, t, SPEC_OTHERS, NULL, 1);
4✔
9348
   }
9349

9350
   pop_scope(nametab);
117✔
9351

9352
   consume(tEND);
117✔
9353
   consume(tFOR);
117✔
9354
   consume(tSEMI);
117✔
9355
}
117✔
9356

9357
static void p_configuration_item(tree_t unit)
167✔
9358
{
9359
   // block_configuration | component_configuration
9360

9361
   BEGIN("configuration item");
334✔
9362

9363
   const token_t third = peek_nth(3);
167✔
9364
   if ((third == tCOLON) || (third == tCOMMA))
167✔
9365
      p_component_configuration(unit);
117✔
9366
   else
9367
      tree_add_decl(unit, p_block_configuration(NULL));
50✔
9368
}
167✔
9369

9370
static tree_t p_index_specification(void)
26✔
9371
{
9372
   // discrete_range | expression
9373

9374
   tree_t head = p_expression();
26✔
9375

9376
   if (scan(tTO, tDOWNTO))
26✔
9377
      return p_discrete_range(head);
10✔
9378
   else
9379
      return head;
9380
}
9381

9382
static void p_block_specification(tree_t b)
168✔
9383
{
9384
   // label | label [ ( index_specification ) ]
9385

9386
   BEGIN("block specification");
336✔
9387

9388
   ident_t id = p_identifier();
168✔
9389
   tree_set_ident(b, id);
168✔
9390

9391
   if (optional(tLPAREN)) {
168✔
9392
      (void)p_index_specification();    // XXX: not used
26✔
9393
      consume(tRPAREN);
26✔
9394
   }
9395
}
168✔
9396

9397
static tree_t p_block_configuration(tree_t of)
168✔
9398
{
9399
   // for block_specification { use_clause } { configuration_item } end for ;
9400

9401
   BEGIN("block configuration");
168✔
9402

9403
   consume(tFOR);
168✔
9404

9405
   tree_t b = tree_new(T_BLOCK_CONFIG);
168✔
9406
   p_block_specification(b);
168✔
9407

9408
   push_scope(nametab);
168✔
9409

9410
   tree_t sub = NULL;
168✔
9411
   if (of != NULL) {
168✔
9412
      switch (tree_kind(of)) {
117✔
9413
      case T_ENTITY:
115✔
9414
         {
9415
            ident_t qual = ident_prefix(tree_ident(of), tree_ident(b), '-');
115✔
9416
            if ((sub = lib_get_qualified(qual)) == NULL)
115✔
9417
               parse_error(CURRENT_LOC, "cannot find architecture %s of "
1✔
9418
                           "entity %s", istr(tree_ident(b)),
9419
                           istr(tree_ident(of)));
9420
         }
9421
         break;
9422
      case T_ARCH:
2✔
9423
         {
9424
            ident_t expect = ident_rfrom(tree_ident(of), '-');
2✔
9425
            if (tree_ident(b) != expect)
2✔
9426
               parse_error(CURRENT_LOC, "block specification label %s does not "
×
9427
                           "match architecture name %s", istr(tree_ident(b)),
9428
                           istr(expect));
9429
            else
9430
               sub = of;
9431
         }
9432
         break;
9433
      default:
×
9434
         fatal_trace("unexpected unit type %s in block configuration",
9435
                     tree_kind_str(tree_kind(of)));
9436
         break;
9437
      }
9438
   }
9439
   else
9440
      sub = resolve_name(nametab, CURRENT_LOC, tree_ident(b));
51✔
9441

9442
   if (sub != NULL && !is_implicit_block(sub)) {
168✔
9443
      parse_error(CURRENT_LOC, "%s is not a block that can be configured",
1✔
9444
                  istr(tree_ident(b)));
9445
      sub = NULL;
9446
   }
9447

9448
   if (sub != NULL) {
167✔
9449
      tree_set_ref(b, sub);
165✔
9450

9451
      if (tree_kind(sub) == T_IF_GENERATE)
165✔
9452
         sub = tree_cond(sub, 0);
6✔
9453

9454
      insert_names_for_config(nametab, sub);
165✔
9455
   }
9456
   else
9457
      suppress_errors(nametab);
3✔
9458

9459
   while (not_at_token(tEND))
335✔
9460
      p_configuration_item(b);
167✔
9461

9462
   if (sub != NULL) {
168✔
9463
      const int nstmts = tree_stmts(sub);
165✔
9464
      for (int i = 0; i < nstmts; i++) {
685✔
9465
         tree_t s = tree_stmt(sub, i);
520✔
9466
         if (tree_kind(s) == T_INSTANCE)
520✔
9467
            query_spec(nametab, s);
144✔
9468
      }
9469

9470
   }
9471

9472
   pop_scope(nametab);
168✔
9473

9474
   consume(tEND);
168✔
9475
   consume(tFOR);
168✔
9476
   consume(tSEMI);
168✔
9477

9478
   tree_set_loc(b, CURRENT_LOC);
168✔
9479
   sem_check(b, nametab);
168✔
9480
   return b;
168✔
9481
}
9482

9483
static ident_t p_entity_name(tree_t *entity)
5,333✔
9484
{
9485
   // name
9486

9487
   BEGIN("entity name");
10,666✔
9488

9489
   ident_t ename = p_selected_identifier();
5,333✔
9490

9491
   ident_t qual = ename;
5,333✔
9492
   if (ident_runtil(ename, '.') == ename)
5,333✔
9493
      qual = ident_prefix(lib_name(lib_work()), ename, '.');
5,331✔
9494

9495
   *entity = resolve_name(nametab, CURRENT_LOC, qual);
5,333✔
9496

9497
   if (*entity != NULL && tree_kind(*entity) != T_ENTITY) {
5,333✔
9498
      diag_t *d = diag_new(DIAG_ERROR, CURRENT_LOC);
2✔
9499
      diag_printf(d, "%s%s is not an entity",
4✔
9500
                  is_design_unit(*entity) ? "design unit " : "", istr(ename));
2✔
9501
      diag_hint(d, tree_loc(*entity), "%s is a %s", istr(ename),
2✔
9502
                class_str(class_of(*entity)));
9503
      diag_emit(d);
2✔
9504
      *entity = NULL;
2✔
9505
      return ename;
2✔
9506
   }
9507

9508
   return ename;
9509
}
9510

9511
static void p_configuration_declaration(tree_t unit)
116✔
9512
{
9513
   // configuration identifier of name is configuration_declarative_part
9514
   //   block_configuration end [ configuration ] [ simple_name ] ;
9515

9516
   BEGIN("configuration declaration");
232✔
9517

9518
   consume(tCONFIGURATION);
116✔
9519

9520
   tree_change_kind(unit, T_CONFIGURATION);
116✔
9521

9522
   ident_t id = p_identifier();
116✔
9523
   tree_set_ident(unit, id);
116✔
9524

9525
   push_scope(nametab);
116✔
9526

9527
   consume(tOF);
116✔
9528

9529
   tree_t of = NULL;
116✔
9530
   ident_t ename = p_entity_name(&of);
116✔
9531
   tree_set_ident2(unit, ename);
116✔
9532

9533
   if (of != NULL) {
116✔
9534
      tree_set_primary(unit, of);
115✔
9535
      insert_name(nametab, of, ename);
115✔
9536
      insert_decls(nametab, of);
115✔
9537
   }
9538

9539
   consume(tIS);
116✔
9540

9541
   while (not_at_token(tFOR))
122✔
9542
      p_configuration_declarative_part(unit);
6✔
9543

9544
   tree_add_decl(unit, p_block_configuration(of));
116✔
9545

9546
   consume(tEND);
116✔
9547
   optional(tCONFIGURATION);
116✔
9548
   p_trailing_label(id);
116✔
9549
   consume(tSEMI);
116✔
9550

9551
   pop_scope(nametab);
116✔
9552

9553
   tree_set_loc(unit, CURRENT_LOC);
116✔
9554
   sem_check(unit, nametab);
116✔
9555

9556
   tree_set_ident(unit, ident_prefix(lib_name(lib_work()), id, '.'));
116✔
9557
}
116✔
9558

9559
static void p_context_declaration(tree_t unit)
19✔
9560
{
9561
   // 2008: context identifier is context_clause end [ context ]
9562
   //       [ context_simple_name ] ;
9563

9564
   BEGIN("context declaration");
38✔
9565

9566
   consume(tCONTEXT);
19✔
9567

9568
   tree_change_kind(unit, T_CONTEXT);
19✔
9569

9570
   ident_t id = p_identifier();
19✔
9571
   tree_set_ident(unit, ident_prefix(lib_name(lib_work()), id, '.'));
19✔
9572

9573
   consume(tIS);
19✔
9574

9575
   push_scope(nametab);
19✔
9576

9577
   // LRM 08 section 13.1 forbids preceeding context clause
9578
   if (tree_contexts(unit) != 3)     // Implicit WORK and STD
19✔
9579
      parse_error(tree_loc(tree_context(unit, 3)), "context clause preceeding "
1✔
9580
                  "context declaration must be empty");
9581

9582
   p_context_clause(unit);
19✔
9583

9584
   consume(tEND);
19✔
9585
   optional(tCONTEXT);
19✔
9586
   p_trailing_label(id);
19✔
9587
   consume(tSEMI);
19✔
9588

9589
   tree_set_loc(unit, CURRENT_LOC);
19✔
9590
   sem_check(unit, nametab);
19✔
9591

9592
   pop_scope(nametab);
19✔
9593
}
19✔
9594

9595
static void p_primary_unit(tree_t unit)
6,896✔
9596
{
9597
   // entity_declaration | configuration_declaration | package_declaration
9598
   //   | 2008: package_instantiation_declaration
9599

9600
   BEGIN("primary unit");
13,792✔
9601

9602
   switch (peek()) {
6,896✔
9603
   case tENTITY:
5,201✔
9604
      p_entity_declaration(unit);
5,201✔
9605
      break;
5,201✔
9606

9607
   case tPACKAGE:
1,560✔
9608
      if (standard() >= STD_08 && peek_nth(4) == tNEW)
1,560✔
9609
         p_package_instantiation_declaration(unit);
76✔
9610
      else
9611
         p_package_declaration(unit);
1,484✔
9612
      break;
9613

9614
   case tCONFIGURATION:
116✔
9615
      p_configuration_declaration(unit);
116✔
9616
      break;
116✔
9617

9618
   case tCONTEXT:
19✔
9619
      p_context_declaration(unit);
19✔
9620
      break;
19✔
9621

9622
   default:
×
9623
      expect(tENTITY, tPACKAGE, tCONFIGURATION);
×
9624
   }
9625
}
6,896✔
9626

9627
static void p_block_declarative_item(tree_t parent)
12,881✔
9628
{
9629
   // subprogram_declaration | subprogram_body
9630
   //   | 2008: package_instantiation_declaration | type_declaration
9631
   //   | subtype_declaration | constant_declaration | signal_declaration
9632
   //   | shared_variable_declaration | file_declaration | alias_declaration
9633
   //   | component_declaration | attribute_declaration
9634
   //   | attribute_specification | configuration_specification
9635
   //   | disconnection_specification | use_clause | group_template_declaration
9636
   //   | group_declaration | 2008: subprogram_instantiation_declaration
9637
   //   | 2008: psl_clock_declaration | 2008: package_declaration
9638
   //   | 2019: mode_view_declaration
9639

9640
   BEGIN("block declarative item");
25,762✔
9641

9642
   switch (peek()) {
12,881✔
9643
   case tSIGNAL:
5,442✔
9644
      p_signal_declaration(parent);
5,442✔
9645
      break;
5,442✔
9646

9647
   case tTYPE:
2,482✔
9648
      p_type_declaration(parent);
2,482✔
9649
      break;
2,482✔
9650

9651
   case tSUBTYPE:
449✔
9652
      tree_add_decl(parent, p_subtype_declaration());
449✔
9653
      break;
449✔
9654

9655
   case tFILE:
32✔
9656
      p_file_declaration(parent);
32✔
9657
      break;
32✔
9658

9659
   case tCONSTANT:
1,484✔
9660
      p_constant_declaration(parent);
1,484✔
9661
      break;
1,484✔
9662

9663
   case tFUNCTION:
1,885✔
9664
   case tPROCEDURE:
9665
   case tIMPURE:
9666
   case tPURE:
9667
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
1,885✔
9668
         tree_add_decl(parent, p_subprogram_instantiation_declaration());
49✔
9669
      else {
9670
         tree_t spec = p_subprogram_specification();
1,836✔
9671
         if (peek() == tSEMI)
1,836✔
9672
            tree_add_decl(parent, p_subprogram_declaration(spec));
161✔
9673
         else
9674
            tree_add_decl(parent, p_subprogram_body(spec));
1,675✔
9675
      }
9676
      break;
9677

9678
   case tALIAS:
142✔
9679
      p_alias_declaration(parent);
142✔
9680
      break;
142✔
9681

9682
   case tATTRIBUTE:
101✔
9683
      if (peek_nth(3) == tOF)
101✔
9684
         p_attribute_specification(parent);
87✔
9685
      else
9686
         tree_add_decl(parent, p_attribute_declaration());
14✔
9687
      break;
9688

9689
   case tFOR:
102✔
9690
      p_configuration_specification(parent);
102✔
9691
      break;
102✔
9692

9693
   case tCOMPONENT:
320✔
9694
      tree_add_decl(parent, p_component_declaration());
320✔
9695
      break;
320✔
9696

9697
   case tUSE:
44✔
9698
      p_use_clause(parent, tree_add_decl);
44✔
9699
      break;
44✔
9700

9701
   case tSHARED:
123✔
9702
      p_variable_declaration(parent);
123✔
9703
      break;
123✔
9704

9705
   case tDISCONNECT:
13✔
9706
      p_disconnection_specification(parent);
13✔
9707
      break;
13✔
9708

9709
   case tGROUP:
2✔
9710
      if (peek_nth(3) == tIS)
2✔
9711
         tree_add_decl(parent, p_group_template_declaration());
1✔
9712
      else
9713
         tree_add_decl(parent, p_group_declaration());
1✔
9714
      break;
9715

9716
   case tPACKAGE:
179✔
9717
      if (peek_nth(4) == tNEW)
179✔
9718
         tree_add_decl(parent, p_package_instantiation_declaration(NULL));
175✔
9719
      else if (peek_nth(2) == tBODY) {
4✔
9720
         require_std(STD_08, "nested package declarations");
×
9721
         tree_add_decl(parent, p_package_body(NULL));
×
9722
      }
9723
      else {
9724
         require_std(STD_08, "nested package declarations");
4✔
9725
         tree_add_decl(parent, p_package_declaration(NULL));
4✔
9726
      }
9727
      break;
9728

9729
   case tSTARTPSL:
35✔
9730
      consume(tSTARTPSL);
35✔
9731
      p_psl_declaration(parent);
35✔
9732
      break;
35✔
9733

9734
   case tDEFAULT:
22✔
9735
   case tSEQUENCE:
9736
   case tPROPERTY:
9737
   case tENDPOINT:
9738
      p_psl_declaration(parent);
22✔
9739
      break;
22✔
9740

9741
   case tVIEW:
17✔
9742
      tree_add_decl(parent, p_mode_view_declaration());
17✔
9743
      break;
17✔
9744

9745
   default:
7✔
9746
      expect(tSIGNAL, tTYPE, tSUBTYPE, tFILE, tCONSTANT, tFUNCTION, tIMPURE,
21✔
9747
             tPURE, tPROCEDURE, tALIAS, tATTRIBUTE, tFOR, tCOMPONENT, tUSE,
9748
             tSHARED, tDISCONNECT, tGROUP, STD(08, tPACKAGE), STD(19, tVIEW));
9749
   }
9750
}
12,881✔
9751

9752
static tree_t p_target(tree_t name)
24,164✔
9753
{
9754
   // name | aggregate
9755

9756
   BEGIN("target");
48,328✔
9757

9758
   if (name == NULL) {
24,164✔
9759
      if (peek() == tLPAREN)
77✔
9760
         return p_aggregate();
23✔
9761
      else
9762
         return p_name(0);
54✔
9763
   }
9764
   else
9765
      return name;
9766
}
9767

9768
static tree_t p_simple_variable_assignment(ident_t label, tree_t name)
16,523✔
9769
{
9770
   // [ label : ] target := expression ;
9771
   // 2019: [ label : ] target := conditional_or_unaffected_expression ;
9772

9773
   EXTEND("simple variable assignment");
16,523✔
9774

9775
   tree_t target = p_target(name);
16,523✔
9776

9777
   consume(tWALRUS);
16,523✔
9778

9779
   tree_t value = p_conditional_or_unaffected_expression(STD_08);
16,523✔
9780

9781
   solve_target(nametab, target, value);
16,523✔
9782
   solve_known_subtype(nametab, value, target);
16,523✔
9783

9784
   tree_t t = tree_new(T_VAR_ASSIGN);
16,523✔
9785
   tree_set_target(t, target);
16,523✔
9786
   tree_set_value(t, value);
16,523✔
9787

9788
   consume(tSEMI);
16,523✔
9789

9790
   tree_set_loc(t, CURRENT_LOC);
16,523✔
9791
   ensure_labelled(t, label);
16,523✔
9792

9793
   sem_check(t, nametab);
16,523✔
9794
   return t;
16,523✔
9795
}
9796

9797
static void p_selected_expressions(tree_t stmt, tree_t target)
9✔
9798
{
9799
   // { expression when choices , } expression when choices
9800

9801
   BEGIN("selected waveforms");
18✔
9802

9803
   type_t with_type = tree_type(tree_value(stmt));
9✔
9804

9805
   do {
24✔
9806
      tree_t expr = p_expression();
24✔
9807

9808
      if (!tree_has_type(target))
24✔
9809
         solve_target(nametab, target, expr);
9✔
9810

9811
      solve_known_subtype(nametab, expr, target);
24✔
9812

9813
      tree_t a = tree_new(T_VAR_ASSIGN);
24✔
9814
      tree_set_target(a, target);
24✔
9815
      tree_set_value(a, expr);
24✔
9816

9817
      sem_check(a, nametab);
24✔
9818

9819
      consume(tWHEN);
24✔
9820

9821
      tree_t alt = tree_new(T_ALTERNATIVE);
24✔
9822
      tree_add_stmt(alt, a);
24✔
9823

9824
      p_choices(alt, NULL, with_type);
24✔
9825

9826
      tree_set_loc(alt, CURRENT_LOC);
24✔
9827
      tree_add_stmt(stmt, alt);
24✔
9828
   } while (optional(tCOMMA));
24✔
9829
}
9✔
9830

9831
static tree_t p_selected_variable_assignment(ident_t label)
12✔
9832
{
9833
   // with expression select [ ? ] target := selected_expressions ;
9834

9835
   BEGIN("selected variable assignment");
12✔
9836

9837
   require_std(STD_08, "selected variable assignment");
12✔
9838

9839
   consume(tWITH);
12✔
9840

9841
   tree_t value = p_expression();
12✔
9842
   solve_types(nametab, value, NULL);
12✔
9843

9844
   consume(tSELECT);
12✔
9845

9846
   tree_kind_t kind = T_SELECT;
12✔
9847
   if (optional(tQUESTION))
12✔
9848
      kind = T_MATCH_SELECT;
6✔
9849

9850
   tree_t t = tree_new(kind);
12✔
9851
   tree_set_value(t, value);
12✔
9852

9853
   tree_t target = p_target(NULL);
12✔
9854

9855
   // This is the easiest place to disambiguate variable and signal
9856
   // assignment without a deep lookahead
9857
   switch (one_of(tWALRUS, tLE)) {
12✔
9858
   case tLE:
3✔
9859
      p_selected_waveforms(t, target, NULL);
3✔
9860
      break;
3✔
9861

9862
   case tWALRUS:
9✔
9863
   default:
9864
      p_selected_expressions(t, target);
9✔
9865
      break;
9✔
9866
   }
9867

9868
   consume(tSEMI);
12✔
9869

9870
   tree_set_loc(t, CURRENT_LOC);
12✔
9871
   ensure_labelled(t, label);
12✔
9872

9873
   sem_check(t, nametab);
12✔
9874
   return t;
12✔
9875
}
9876

9877
static tree_t p_variable_assignment_statement(ident_t label, tree_t name)
16,535✔
9878
{
9879
   // [ label : ] simple_variable_assignment
9880
   //   | 2008: [ label : ] conditional_variable_assignment
9881
   //   | 2008: [ label : ] selected_variable_assignment
9882

9883
   EXTEND("variable assignment statement");
33,070✔
9884

9885
   if (name == NULL && peek() == tWITH)
16,535✔
9886
      return p_selected_variable_assignment(label);
12✔
9887
   else
9888
      return p_simple_variable_assignment(label, name);
16,523✔
9889
}
9890

9891
static tree_t p_waveform_element(tree_t target)
8,113✔
9892
{
9893
   // expression [ after expression ] | null [ after expression ]
9894

9895
   BEGIN("waveform element");
8,113✔
9896

9897
   tree_t w = tree_new(T_WAVEFORM);
8,113✔
9898

9899
   if (!optional(tNULL)) {
8,113✔
9900
      tree_t value = p_expression();
8,096✔
9901
      tree_set_value(w, value);
8,096✔
9902

9903
      if (!tree_has_type(target))
8,096✔
9904
         solve_target(nametab, target, value);
7,494✔
9905

9906
      solve_known_subtype(nametab, value, target);
8,096✔
9907
   }
9908
   else if (!tree_has_type(target))
17✔
9909
      solve_types(nametab, target, NULL);
16✔
9910

9911
   if (optional(tAFTER)) {
8,113✔
9912
      tree_t delay = p_expression();
909✔
9913
      tree_set_delay(w, delay);
909✔
9914
      solve_types(nametab, delay, std_type(NULL, STD_TIME));
909✔
9915
   }
9916

9917
   tree_set_loc(w, CURRENT_LOC);
8,113✔
9918

9919
   return w;
8,113✔
9920
}
9921

9922
static void p_waveform(tree_t stmt, tree_t target)
7,877✔
9923
{
9924
   // waveform_element { , waveform_element } | unaffected
9925

9926
   BEGIN("waveform");
15,748✔
9927

9928
   if (optional(tUNAFFECTED)) {
7,877✔
9929
      solve_types(nametab, target, NULL);
6✔
9930
      return;
6✔
9931
   }
9932

9933
   do {
8,113✔
9934
      tree_add_waveform(stmt, p_waveform_element(target));
8,113✔
9935
   } while (optional(tCOMMA));
8,113✔
9936

9937
   tree_set_loc(stmt, CURRENT_LOC);
7,871✔
9938
}
9939

9940
static tree_t p_delay_mechanism(void)
7,522✔
9941
{
9942
   // transport | [ reject expression ] inertial
9943

9944
   BEGIN("delay mechanism");
15,044✔
9945

9946
   switch (peek()) {
7,522✔
9947
   case tTRANSPORT:
66✔
9948
      consume(tTRANSPORT);
66✔
9949
      return get_time(0, CURRENT_LOC);
66✔
9950

9951
   case tREJECT:
12✔
9952
      {
9953
         consume(tREJECT);
12✔
9954
         tree_t t = p_expression();
12✔
9955
         consume(tINERTIAL);
12✔
9956
         return t;
12✔
9957
      }
9958

9959
   case tINERTIAL:
1✔
9960
      consume(tINERTIAL);
1✔
9961
      return NULL;
1✔
9962

9963
   default:
9964
      return NULL;
9965
   }
9966
}
9967

9968
static port_mode_t p_force_mode(void)
107✔
9969
{
9970
   // in | out
9971

9972
   BEGIN("force mode");
214✔
9973

9974
   switch (peek()) {
107✔
9975
   case tIN: consume(tIN); return PORT_IN;
4✔
9976
   case tOUT: consume(tOUT); return PORT_OUT;
7✔
9977
   default: return PORT_INVALID;
9978
   }
9979
}
9980

9981
static tree_t p_simple_force_assignment(ident_t label, tree_t target)
68✔
9982
{
9983
   // target <= force [ force_mode ] conditional_or_unaffected_expression ;
9984

9985
   EXTEND("simple force assignment");
68✔
9986

9987
   consume(tFORCE);
68✔
9988

9989
   require_std(STD_08, "simple force assignments");
68✔
9990

9991
   type_t target_type;
68✔
9992
   if (tree_kind(target) == T_AGGREGATE) {
68✔
9993
      parse_error(CURRENT_LOC, "target of a simple force assignment may "
1✔
9994
                  "not be an aggregate");
9995
      target_type = type_new(T_NONE);
1✔
9996
   }
9997
   else
9998
      target_type = solve_types(nametab, target, NULL);
67✔
9999

10000
   tree_set_type(target, target_type);
68✔
10001

10002
   tree_t t = tree_new(T_FORCE);
68✔
10003
   tree_set_target(t, target);
68✔
10004
   tree_set_subkind(t, p_force_mode());
68✔
10005

10006
   tree_t expr = p_conditional_or_unaffected_expression(STD_08);
68✔
10007
   solve_types(nametab, expr, target_type);
68✔
10008

10009
   tree_set_value(t, expr);
68✔
10010

10011
   consume(tSEMI);
68✔
10012

10013
   set_label_and_loc(t, label, CURRENT_LOC);
68✔
10014
   sem_check(t, nametab);
68✔
10015
   return t;
68✔
10016
}
10017

10018
static tree_t p_simple_release_assignment(ident_t label, tree_t target)
39✔
10019
{
10020
   // target <= release [ force_mode ] ;
10021

10022
   EXTEND("simple force assignment");
39✔
10023

10024
   consume(tRELEASE);
39✔
10025

10026
   require_std(STD_08, "simple release assignments");
39✔
10027

10028
   type_t target_type;
39✔
10029
   if (tree_kind(target) == T_AGGREGATE) {
39✔
10030
      parse_error(CURRENT_LOC, "target of a simple release assignment may "
1✔
10031
                  "not be an aggregate");
10032
      target_type = type_new(T_NONE);
1✔
10033
   }
10034
   else
10035
      target_type = solve_types(nametab, target, NULL);
38✔
10036

10037
   tree_set_type(target, target_type);
39✔
10038

10039
   tree_t t = tree_new(T_RELEASE);
39✔
10040
   tree_set_target(t, target);
39✔
10041
   tree_set_subkind(t, p_force_mode());
39✔
10042

10043
   consume(tSEMI);
39✔
10044

10045
   set_label_and_loc(t, label, CURRENT_LOC);
39✔
10046
   sem_check(t, nametab);
39✔
10047
   return t;
39✔
10048
}
10049

10050
static tree_t p_signal_assignment_statement(ident_t label, tree_t name)
5,324✔
10051
{
10052
   // [ label : ] target <= [ delay_mechanism ] waveform ;
10053

10054
   EXTEND("signal assignment statement");
10,648✔
10055

10056
   tree_t target = p_target(name);
5,324✔
10057

10058
   consume(tLE);
5,324✔
10059

10060
   switch (peek()) {
5,324✔
10061
   case tFORCE: return p_simple_force_assignment(label, target);
68✔
10062
   case tRELEASE: return p_simple_release_assignment(label, target);
39✔
10063
   default: break;
5,217✔
10064
   }
10065

10066

10067
   tree_t t = tree_new(T_SIGNAL_ASSIGN);
5,217✔
10068
   tree_set_target(t, target);
5,217✔
10069

10070
   tree_t reject = p_delay_mechanism();
5,217✔
10071

10072
   p_waveform(t, target);
5,217✔
10073

10074
   if (peek() == tWHEN) {
5,217✔
10075
      require_std(STD_08, "conditional signal assignment statements");
5✔
10076

10077
      tree_t stmt = tree_new(T_COND_ASSIGN);
5✔
10078
      tree_set_target(stmt, target);
5✔
10079

10080
      p_conditional_waveforms(stmt, target, t);
5✔
10081

10082
      const int nconds = tree_conds(stmt);
5✔
10083
      for (int i = 0; i < nconds; i++) {
15✔
10084
         tree_t c = tree_cond(stmt, i);
10✔
10085
         assert(tree_stmts(c) == 1);
10✔
10086
         set_delay_mechanism(tree_stmt(c, 0), reject);
10✔
10087
      }
10088

10089
      t = stmt;
10090
   }
10091
   else
10092
      set_delay_mechanism(t, reject);
5,212✔
10093

10094
   consume(tSEMI);
5,217✔
10095

10096
   set_label_and_loc(t, label, CURRENT_LOC);
5,217✔
10097
   sem_check(t, nametab);
5,217✔
10098
   return t;
5,217✔
10099
}
10100

10101
static void p_sensitivity_clause(tree_t wait)
361✔
10102
{
10103
   // on sensitivity_list
10104

10105
   BEGIN("sensitivity clause");
722✔
10106

10107
   consume(tON);
361✔
10108
   p_sensitivity_list(wait);
361✔
10109
}
361✔
10110

10111
static void p_condition_clause(tree_t wait)
306✔
10112
{
10113
   // until condition
10114

10115
   BEGIN("condition clause");
612✔
10116

10117
   consume(tUNTIL);
306✔
10118

10119
   tree_set_value(wait, p_condition());
306✔
10120
}
306✔
10121

10122
static void p_timeout_clause(tree_t wait)
5,317✔
10123
{
10124
   // for expression
10125

10126
   BEGIN("timeout clause");
10,634✔
10127

10128
   consume(tFOR);
5,317✔
10129

10130
   tree_t delay = p_expression();
5,317✔
10131
   tree_set_delay(wait, delay);
5,317✔
10132
   solve_types(nametab, delay, std_type(NULL, STD_TIME));
5,317✔
10133
}
5,317✔
10134

10135
static tree_t p_wait_statement(ident_t label)
9,917✔
10136
{
10137
   // [ label : ] wait [ sensitivity_clause ] [ condition_clause ]
10138
   //   [ timeout_clause ] ;
10139

10140
   EXTEND("wait statement");
9,917✔
10141

10142
   tree_t t = tree_new(T_WAIT);
9,917✔
10143

10144
   consume(tWAIT);
9,917✔
10145

10146
   if (peek() == tON)
9,917✔
10147
      p_sensitivity_clause(t);
361✔
10148

10149
   if (peek() == tUNTIL)
9,917✔
10150
      p_condition_clause(t);
306✔
10151

10152
   if (peek() == tFOR)
9,917✔
10153
      p_timeout_clause(t);
5,317✔
10154

10155
   consume(tSEMI);
9,917✔
10156

10157
   set_label_and_loc(t, label, CURRENT_LOC);
9,917✔
10158
   sem_check(t, nametab);
9,917✔
10159
   return t;
9,917✔
10160
}
10161

10162
static tree_t p_assertion_statement(ident_t label)
16,623✔
10163
{
10164
   // [ label : ] assertion ;
10165

10166
   EXTEND("assertion statement");
16,623✔
10167

10168
   tree_t t = p_assertion();
16,623✔
10169
   consume(tSEMI);
16,623✔
10170

10171
   set_label_and_loc(t, label, CURRENT_LOC);
16,623✔
10172
   sem_check(t, nametab);
16,623✔
10173
   return t;
16,623✔
10174
}
10175

10176
static tree_t p_report_statement(ident_t label)
2,135✔
10177
{
10178
   // [ label : ] report expression [ severity expression ] ;
10179

10180
   EXTEND("report statement");
2,135✔
10181

10182
   tree_t t = tree_new(T_REPORT);
2,135✔
10183

10184
   consume(tREPORT);
2,135✔
10185

10186
   tree_t m = p_expression();
2,135✔
10187
   tree_set_message(t, m);
2,135✔
10188
   solve_types(nametab, m, std_type(NULL, STD_STRING));
2,135✔
10189

10190
   if (optional(tSEVERITY)) {
2,135✔
10191
      tree_t s = p_expression();
458✔
10192
      solve_types(nametab, s, std_type(NULL, STD_SEVERITY_LEVEL));
458✔
10193
      tree_set_severity(t, s);
458✔
10194
   }
10195

10196
   consume(tSEMI);
2,135✔
10197

10198
   set_label_and_loc(t, label, CURRENT_LOC);
2,135✔
10199
   sem_check(t, nametab);
2,135✔
10200
   return t;
2,135✔
10201
}
10202

10203
static tree_t p_if_statement(ident_t label)
8,638✔
10204
{
10205
   // [ label : ] if condition then sequence_of_statements
10206
   //   { elsif condition then sequence_of_statements }
10207
   //   [ else sequence_of_statements ] end if [ label ] ;
10208

10209
   EXTEND("if statement");
8,638✔
10210

10211
   tree_t t = tree_new(T_IF);
8,638✔
10212
   consume(tIF);
8,638✔
10213

10214
   tree_t c0 = tree_new(T_COND_STMT);
8,638✔
10215
   tree_set_value(c0, p_condition());
8,638✔
10216
   tree_add_cond(t, c0);
8,638✔
10217

10218
   consume(tTHEN);
8,638✔
10219

10220
   p_sequence_of_statements(c0);
8,638✔
10221

10222
   tree_set_loc(c0, CURRENT_LOC);
8,638✔
10223

10224
   while (optional(tELSIF)) {
9,519✔
10225
      tree_t c = tree_new(T_COND_STMT);
881✔
10226
      tree_set_value(c, p_condition());
881✔
10227
      tree_add_cond(t, c);
881✔
10228

10229
      consume(tTHEN);
881✔
10230

10231
      p_sequence_of_statements(c);
881✔
10232

10233
      tree_set_loc(c, CURRENT_LOC);
881✔
10234
   }
10235

10236
   if (optional(tELSE)) {
8,638✔
10237
      tree_t c = tree_new(T_COND_STMT);
2,343✔
10238
      tree_add_cond(t, c);
2,343✔
10239

10240
      p_sequence_of_statements(c);
2,343✔
10241

10242
      tree_set_loc(c, CURRENT_LOC);
2,343✔
10243
   }
10244

10245
   consume(tEND);
8,638✔
10246
   consume(tIF);
8,638✔
10247
   p_trailing_label(label);
8,638✔
10248
   consume(tSEMI);
8,638✔
10249

10250
   set_label_and_loc(t, label, CURRENT_LOC);
8,638✔
10251
   sem_check(t, nametab);
8,638✔
10252
   return t;
8,638✔
10253
}
10254

10255
static tree_t p_null_statement(ident_t label)
226✔
10256
{
10257
   // [ label : ] null ;
10258

10259
   EXTEND("null statement");
226✔
10260

10261
   consume(tNULL);
226✔
10262
   consume(tSEMI);
226✔
10263

10264
   tree_t t = tree_new(T_NULL);
226✔
10265
   set_label_and_loc(t, label, CURRENT_LOC);
226✔
10266
   return t;
226✔
10267
}
10268

10269
static void p_parameter_specification(tree_t loop, tree_kind_t pkind)
2,685✔
10270
{
10271
   // identifier in discrete_range
10272

10273
   BEGIN("paremeter specification");
5,370✔
10274

10275
   ident_t id = p_identifier();
2,685✔
10276
   const loc_t id_loc = last_loc;
2,685✔
10277

10278
   consume(tIN);
2,685✔
10279

10280
   tree_t r = p_discrete_range(NULL);
2,685✔
10281
   solve_types(nametab, r, NULL);
2,685✔
10282
   convert_universal_bounds(r);
2,685✔
10283
   tree_add_range(loop, r);
2,685✔
10284

10285
   // LRM 08 section 10.10: the loop parameter is an object whose type
10286
   // is the base type of the discrete range
10287
   type_t base = type_base_recur(tree_type(r));
2,685✔
10288

10289
   tree_t constraint = tree_new(T_CONSTRAINT);
2,685✔
10290
   tree_set_subkind(constraint, C_RANGE);
2,685✔
10291
   tree_add_range(constraint, r);
2,685✔
10292

10293
   type_t sub = type_new(T_SUBTYPE);
2,685✔
10294
   type_set_base(sub, base);
2,685✔
10295
   type_set_constraint(sub, constraint);
2,685✔
10296

10297
   tree_t param = tree_new(pkind);
2,685✔
10298
   tree_set_ident(param, id);
2,685✔
10299
   tree_set_type(param, sub);
2,685✔
10300
   tree_set_loc(param, &id_loc);
2,685✔
10301
   tree_set_class(param, C_CONSTANT);
2,685✔
10302
   tree_set_subkind(param, PORT_IN);
2,685✔
10303

10304
   tree_add_decl(loop, param);
2,685✔
10305

10306
   insert_name(nametab, param, NULL);
2,685✔
10307
}
2,685✔
10308

10309
static tree_t p_iteration_scheme(void)
2,978✔
10310
{
10311
   // while condition | for parameter_specification
10312

10313
   BEGIN("iteration scheme");
5,956✔
10314

10315
   if (optional(tWHILE)) {
2,978✔
10316
      tree_t t = tree_new(T_WHILE);
204✔
10317
      tree_set_value(t, p_condition());
204✔
10318
      return t;
204✔
10319
   }
10320
   else if (optional(tFOR)) {
2,774✔
10321
      tree_t t = tree_new(T_FOR);
2,507✔
10322
      scope_set_container(nametab, t);
2,507✔
10323
      p_parameter_specification(t, T_PARAM_DECL);
2,507✔
10324
      return t;
2,507✔
10325
   }
10326
   else
10327
      return tree_new(T_LOOP);
267✔
10328
}
10329

10330
static tree_t p_loop_statement(ident_t label)
2,978✔
10331
{
10332
   // [ loop_label : ] [ iteration_scheme ] loop sequence_of_statements
10333
   //   end loop [ loop_label ] ;
10334

10335
   BEGIN("loop statement");
2,978✔
10336

10337
   push_scope(nametab);
2,978✔
10338

10339
   tree_t t = p_iteration_scheme();
2,978✔
10340

10341
   consume(tLOOP);
2,978✔
10342

10343
   scope_set_container(nametab, t);
2,978✔
10344
   set_label_and_loc(t, label, CURRENT_LOC);
2,978✔
10345

10346
   if (label != NULL)
2,978✔
10347
      insert_name(nametab, t, NULL);
110✔
10348

10349
   sem_check(t, nametab);
2,978✔
10350

10351
   p_sequence_of_statements(t);
2,978✔
10352

10353
   consume(tEND);
2,978✔
10354
   consume(tLOOP);
2,978✔
10355
   p_trailing_label(label);
2,978✔
10356
   consume(tSEMI);
2,978✔
10357

10358
   pop_scope(nametab);
2,978✔
10359

10360
   tree_set_loc(t, CURRENT_LOC);
2,978✔
10361
   return t;
2,978✔
10362
}
10363

10364
static tree_t p_return_statement(ident_t label)
13,082✔
10365
{
10366
   // [ label : ] return [ expression ] ;
10367
   // 2019: [ label : ] return [ when condition ] ;
10368
   // 2019: [ label : ] return conditional_or_unaffected_expression ;
10369

10370
   EXTEND("return statement");
13,082✔
10371

10372
   consume(tRETURN);
13,082✔
10373

10374
   tree_t stmt = NULL;
13,082✔
10375

10376
   if (optional(tWHEN)) {
13,082✔
10377
      require_std(STD_19, "conditional return statement");
7✔
10378

10379
      stmt = tree_new(T_COND_RETURN);
7✔
10380
      tree_set_value(stmt, p_condition());
7✔
10381
   }
10382
   else {
10383
      stmt = tree_new(T_RETURN);
13,075✔
10384

10385
      if (peek() != tSEMI) {
13,075✔
10386
         type_t return_type = NULL;
12,730✔
10387
         tree_t subprog = find_enclosing(nametab, S_SUBPROGRAM);
12,730✔
10388
         if (subprog != NULL && tree_kind(subprog) == T_FUNC_BODY) {
12,730✔
10389
            return_type = type_result(tree_type(subprog));
12,726✔
10390
            tree_set_type(stmt, return_type);
12,726✔
10391
         }
10392

10393
         tree_t value = p_conditional_or_unaffected_expression(STD_19);
12,730✔
10394
         solve_types(nametab, value, return_type);
12,730✔
10395
         tree_set_value(stmt, value);
12,730✔
10396
      }
10397
   }
10398

10399
   consume(tSEMI);
13,082✔
10400

10401
   set_label_and_loc(stmt, label, CURRENT_LOC);
13,082✔
10402
   sem_check(stmt, nametab);
13,082✔
10403
   return stmt;
13,082✔
10404
}
10405

10406
static tree_t p_exit_statement(ident_t label)
319✔
10407
{
10408
   // [ label : ] exit [ label ] [ when condition ] ;
10409

10410
   EXTEND("exit statement");
319✔
10411

10412
   consume(tEXIT);
319✔
10413

10414
   tree_t t = tree_new(T_EXIT);
319✔
10415

10416
   if (peek() == tID) {
319✔
10417
      ident_t id = p_identifier();
38✔
10418
      tree_set_ident2(t, id);
38✔
10419

10420
      tree_t loop = resolve_name(nametab, CURRENT_LOC, id);
38✔
10421
      if (loop != NULL && !is_loop_stmt(loop))
38✔
10422
         parse_error(CURRENT_LOC, "%s is not a loop statement", istr(id));
×
10423
   }
10424
   else {
10425
      tree_t loop = find_enclosing(nametab, S_LOOP);
281✔
10426
      if (loop == NULL)
281✔
10427
         parse_error(CURRENT_LOC, "cannot use exit statement outside loop");
2✔
10428
      else
10429
         tree_set_ident2(t, tree_ident(loop));
279✔
10430
   }
10431

10432
   if (optional(tWHEN))
319✔
10433
      tree_set_value(t, p_condition());
129✔
10434

10435
   consume(tSEMI);
319✔
10436

10437
   set_label_and_loc(t, label, CURRENT_LOC);
319✔
10438
   sem_check(t, nametab);
319✔
10439
   return t;
319✔
10440
}
10441

10442
static tree_t p_next_statement(ident_t label)
96✔
10443
{
10444
   // [ label : ] next [ label ] [ when condition ] ;
10445

10446
   EXTEND("next statement");
96✔
10447

10448
   consume(tNEXT);
96✔
10449

10450
   tree_t t = tree_new(T_NEXT);
96✔
10451

10452
   if (peek() == tID) {
96✔
10453
      ident_t id = p_identifier();
2✔
10454
      tree_set_ident2(t, id);
2✔
10455

10456
      tree_t loop = resolve_name(nametab, CURRENT_LOC, id);
2✔
10457
      if (loop != NULL && !is_loop_stmt(loop))
2✔
10458
         parse_error(CURRENT_LOC, "%s is not a loop statement", istr(id));
×
10459
   }
10460
   else {
10461
      tree_t loop = find_enclosing(nametab, S_LOOP);
94✔
10462
      if (loop == NULL)
94✔
10463
         parse_error(CURRENT_LOC, "cannot use next statement outside loop");
3✔
10464
      else
10465
         tree_set_ident2(t, tree_ident(loop));
91✔
10466
   }
10467

10468
   if (optional(tWHEN))
96✔
10469
      tree_set_value(t, p_condition());
63✔
10470

10471
   consume(tSEMI);
96✔
10472

10473
   set_label_and_loc(t, label, CURRENT_LOC);
96✔
10474
   sem_check(t, nametab);
96✔
10475
   return t;
96✔
10476
}
10477

10478
static tree_t p_procedure_call_statement(ident_t label, tree_t name)
7,988✔
10479
{
10480
   // [ label : ] procedure_call ;
10481
   //
10482
   // name [ ( actual_parameter_part ) ]
10483
   // 2019: name [ generic_map_aspect ] [ parameter_map_aspect ]
10484

10485
   EXTEND("procedure call statement");
15,973✔
10486

10487
   tree_t call = NULL;
7,988✔
10488

10489
   switch (tree_kind(name)) {
7,988✔
10490
   case T_REF:
7,788✔
10491
      call = tree_new(T_PCALL);
7,788✔
10492
      tree_set_ident2(call, tree_ident(name));
7,788✔
10493
      break;
7,788✔
10494

10495
   case T_PROT_REF:
190✔
10496
      call = tree_new(T_PROT_PCALL);
190✔
10497
      tree_set_ident2(call, tree_ident(name));
190✔
10498
      tree_set_name(call, tree_value(name));
190✔
10499
      break;
190✔
10500

10501
   default:
10✔
10502
      // Only print an error if name is a valid expression
10503
      if (!type_is_none(solve_types(nametab, name, NULL)))
10✔
10504
         parse_error(CURRENT_LOC, "expected procedure name");
5✔
10505

10506
      call = tree_new(T_PCALL);
7✔
10507
      tree_set_ident2(call, error_marker());
7✔
10508
      set_label_and_loc(call, label, CURRENT_LOC);
7✔
10509
      drop_tokens_until(tSEMI);
7✔
10510
      return call;
7✔
10511
   }
10512

10513
   if (peek() == tGENERIC) {
7,978✔
10514
      ident_t id = tree_ident(name);
19✔
10515
      tree_t inst = tree_new(T_PROC_INST);
19✔
10516
      tree_set_ident(inst, ident_prefix(id, ident_uniq("inst"), '$'));
19✔
10517

10518
      tree_t decl = resolve_uninstantiated_subprogram(nametab, CURRENT_LOC,
19✔
10519
                                                      id, NULL);
10520
      if (decl != NULL) {
19✔
10521
         tree_t body = find_generic_subprogram_body(inst, decl);
19✔
10522
         instantiate_subprogram(inst, decl, body);
19✔
10523
      }
10524
      else {
10525
         // Create a dummy subprogram type to avoid later errors
10526
         type_t type = type_new(T_SIGNATURE);
×
10527
         type_set_ident(type, id);
×
10528

10529
         tree_set_type(inst, type);
×
10530
      }
10531

10532
      p_generic_map_aspect(inst, inst);
19✔
10533

10534
      require_std(STD_19, "generic map on procedure call");
19✔
10535

10536
      tree_set_loc(inst, CURRENT_LOC);
19✔
10537
      sem_check(inst, nametab);
19✔
10538

10539
      hash_t *map = get_generic_map(nametab);
19✔
10540
      if (map != NULL)
19✔
10541
         instance_fixup(inst, map);
19✔
10542

10543
      tree_set_ref(call, inst);
19✔
10544

10545
      mangle_func(nametab, inst);
19✔
10546

10547
      tree_t container = find_enclosing(nametab, S_DECLARATIVE_REGION);
19✔
10548
      tree_add_decl(container, inst);
19✔
10549
   }
10550

10551
   if (peek() == tPARAMETER)
7,978✔
10552
      p_parameter_map_aspect(call);
1✔
10553
   else if (optional(tLPAREN)) {
7,977✔
10554
      p_actual_parameter_part(call);
7,545✔
10555
      consume(tRPAREN);
7,545✔
10556
   }
10557

10558
   consume(tSEMI);
7,978✔
10559

10560
   set_label_and_loc(call, label, CURRENT_LOC);
7,978✔
10561

10562
   solve_types(nametab, call, NULL);
7,978✔
10563
   sem_check(call, nametab);
7,978✔
10564
   return call;
7,978✔
10565
}
10566

10567
static tree_t p_case_statement_alternative(type_t type)
2,569✔
10568
{
10569
   // when choices => sequence_of_statements
10570

10571
   BEGIN("case statement alternative");
2,569✔
10572

10573
   consume(tWHEN);
2,569✔
10574

10575
   tree_t alt = tree_new(T_ALTERNATIVE);
2,569✔
10576

10577
   p_choices(alt, NULL, type);
2,569✔
10578

10579
   consume(tASSOC);
2,569✔
10580

10581
   p_sequence_of_statements(alt);
2,569✔
10582

10583
   tree_set_loc(alt, CURRENT_LOC);
2,569✔
10584
   return alt;
2,569✔
10585
}
10586

10587
static tree_t p_case_statement(ident_t label)
556✔
10588
{
10589
   // [ label : ] case [?] expression is case_statement_alternative
10590
   //   { case_statement_alternative } end case [?] [ label ] ;
10591

10592
   EXTEND("case statement");
556✔
10593

10594
   consume(tCASE);
556✔
10595

10596
   tree_kind_t kind = T_CASE;
556✔
10597
   if (optional(tQUESTION)) {
556✔
10598
      require_std(STD_08, "matching case statements");
28✔
10599
      kind = T_MATCH_CASE;
28✔
10600
   }
10601

10602
   tree_t t = tree_new(kind);
556✔
10603

10604
   tree_t value = p_expression();
556✔
10605
   tree_set_value(t, value);
556✔
10606

10607
   type_t type = solve_types(nametab, value, NULL);
556✔
10608

10609
   consume(tIS);
556✔
10610

10611
   do {
2,569✔
10612
      tree_add_stmt(t, p_case_statement_alternative(type));
2,569✔
10613
   } while (peek() == tWHEN);
2,569✔
10614

10615
   consume(tEND);
556✔
10616
   consume(tCASE);
556✔
10617

10618
   if (kind == T_MATCH_CASE)
556✔
10619
      consume(tQUESTION);
28✔
10620

10621
   p_trailing_label(label);
556✔
10622
   consume(tSEMI);
556✔
10623

10624
   set_label_and_loc(t, label, CURRENT_LOC);
556✔
10625
   sem_check(t, nametab);
556✔
10626
   return t;
556✔
10627
}
10628

10629
static void p_sequential_block_declarative_part(tree_t block)
8✔
10630
{
10631
   // { process_declarative_item }
10632

10633
   BEGIN("sequential block declarative part");
16✔
10634

10635
   while (not_at_token(tBEGIN))
21✔
10636
      p_process_declarative_item(block);
13✔
10637
}
8✔
10638

10639
static void p_sequential_block_statement_part(tree_t block)
8✔
10640
{
10641
   // { sequential_statement }
10642

10643
   BEGIN("sequential block statement part");
16✔
10644

10645
   p_sequence_of_statements(block);
8✔
10646
}
8✔
10647

10648
static tree_t p_sequential_block_statement(ident_t label)
8✔
10649
{
10650
   // [ label : ] block  [ is ] sequential_block_declarative_part
10651
   //   begin sequential_block_statement_part end [ block ] [ label ] ;
10652

10653
   BEGIN("sequential block statement");
8✔
10654

10655
   consume(tBLOCK);
8✔
10656
   optional(tIS);
8✔
10657

10658
   require_std(STD_19, "sequential block statements");
8✔
10659

10660
   push_scope(nametab);
8✔
10661

10662
   tree_t t = tree_new(T_SEQUENCE);
8✔
10663

10664
   scope_set_container(nametab, t);
8✔
10665
   set_label_and_loc(t, label, CURRENT_LOC);
8✔
10666

10667
   p_sequential_block_declarative_part(t);
8✔
10668

10669
   consume(tBEGIN);
8✔
10670

10671
   p_sequential_block_statement_part(t);
8✔
10672

10673
   consume(tEND);
8✔
10674
   optional(tBLOCK);
8✔
10675

10676
   p_trailing_label(label);
8✔
10677
   consume(tSEMI);
8✔
10678

10679
   tree_set_loc(t, CURRENT_LOC);
8✔
10680
   sem_check(t, nametab);
8✔
10681
   pop_scope(nametab);
8✔
10682

10683
   return t;
8✔
10684
}
10685

10686
static tree_t p_sequential_statement(void)
84,427✔
10687
{
10688
   // wait_statement | assertion_statement | report_statement
10689
   //   | signal_assignment_statement | variable_assignment_statement
10690
   //   | procedure_call_statement | if_statement | case_statement
10691
   //   | loop_statement | next_statement | exit_statement | return_statement
10692
   //   | null_statement | 2019: sequential_block_statement
10693

10694
   BEGIN("sequential statement");
168,851✔
10695

10696
   ident_t label = NULL;
84,427✔
10697
   if ((peek() == tID) && (peek_nth(2) == tCOLON)) {
84,427✔
10698
      label = p_identifier();
378✔
10699
      consume(tCOLON);
378✔
10700
   }
10701

10702
   switch (peek()) {
84,427✔
10703
   case tWAIT:
9,917✔
10704
      return p_wait_statement(label);
9,917✔
10705

10706
   case tASSERT:
16,623✔
10707
      return p_assertion_statement(label);
16,623✔
10708

10709
   case tREPORT:
2,135✔
10710
      return p_report_statement(label);
2,135✔
10711

10712
   case tIF:
8,638✔
10713
      return p_if_statement(label);
8,638✔
10714

10715
   case tNULL:
226✔
10716
      return p_null_statement(label);
226✔
10717

10718
   case tRETURN:
13,082✔
10719
      return p_return_statement(label);
13,082✔
10720

10721
   case tCASE:
556✔
10722
      return p_case_statement(label);
556✔
10723

10724
   case tWHILE:
2,978✔
10725
   case tLOOP:
10726
   case tFOR:
10727
      return p_loop_statement(label);
2,978✔
10728

10729
   case tEXIT:
319✔
10730
      return p_exit_statement(label);
319✔
10731

10732
   case tNEXT:
96✔
10733
      return p_next_statement(label);
96✔
10734

10735
   case tWITH:
12✔
10736
      return p_variable_assignment_statement(label, NULL);
12✔
10737

10738
   case tID:
10739
   case tLTLT:
10740
      break;
29,727✔
10741

10742
   case tLPAREN:
109✔
10743
      {
10744
         tree_t agg = p_aggregate();
109✔
10745

10746
         switch (peek()) {
109✔
10747
         case tWALRUS:
56✔
10748
            return p_variable_assignment_statement(label, agg);
56✔
10749

10750
         case tLE:
53✔
10751
            return p_signal_assignment_statement(label, agg);
53✔
10752

10753
         default:
×
10754
            expect(tWALRUS, tLE);
×
10755
            return tree_new(T_NULL);
×
10756
         }
10757
      }
10758

10759
   case tBLOCK:
8✔
10760
      return p_sequential_block_statement(label);
8✔
10761

10762
   default:
1✔
10763
      expect(tWAIT, tID, tASSERT, tREPORT, tIF, tNULL, tRETURN, tCASE, tWHILE,
1✔
10764
             tFOR, tLOOP, tEXIT, tNEXT, tWITH, tLTLT, tLPAREN, tBLOCK);
10765
      drop_tokens_until(tSEMI);
1✔
10766
      return tree_new(T_NULL);
1✔
10767
   }
10768

10769
   tree_t name = p_name(N_SUBPROGRAM);
29,727✔
10770

10771
   switch (peek()) {
29,727✔
10772
   case tWALRUS:
16,467✔
10773
      return p_variable_assignment_statement(label, name);
16,467✔
10774

10775
   case tLE:
5,271✔
10776
      return p_signal_assignment_statement(label, name);
5,271✔
10777

10778
   case tSEMI:
7,988✔
10779
   case tLPAREN:
10780
   case tGENERIC:
10781
   case tPARAMETER:
10782
      return p_procedure_call_statement(label, name);
7,988✔
10783

10784
   default:
1✔
10785
      expect(tWALRUS, tLE, tSEMI);
1✔
10786
      drop_tokens_until(tSEMI);
1✔
10787
      return tree_new(T_NULL);
1✔
10788
   }
10789
}
10790

10791
static tree_t p_instantiated_unit(tree_t name)
1,569✔
10792
{
10793
   // [ component ] name
10794
   //   | entity name [ ( identifier ) ]
10795
   //   | configuration name
10796

10797
   BEGIN("instantiated unit");
1,569✔
10798

10799
   tree_t t = tree_new(T_INSTANCE);
1,569✔
10800

10801
   switch (peek()) {
1,569✔
10802
   case tENTITY:
1,071✔
10803
      consume(tENTITY);
1,071✔
10804
      tree_set_class(t, C_ENTITY);
1,071✔
10805
      break;
1,071✔
10806

10807
   case tCONFIGURATION:
14✔
10808
      consume(tCONFIGURATION);
14✔
10809
      tree_set_class(t, C_CONFIGURATION);
14✔
10810
      break;
14✔
10811

10812
   case tCOMPONENT:
253✔
10813
      consume(tCOMPONENT);
253✔
10814
      // Fall-through
10815

10816
   default:
484✔
10817
      tree_set_class(t, C_COMPONENT);
484✔
10818
   }
10819

10820
   if (name != NULL) {
1,569✔
10821
      if (tree_kind(name) == T_REF) {
3✔
10822
         tree_set_ident2(t, tree_ident(name));
2✔
10823
         if (tree_has_ref(name))
2✔
10824
            tree_set_ref(t, tree_ref(name));
2✔
10825
      }
10826
      else {
10827
         parse_error(tree_loc(name), "invalid instantiated unit name");
1✔
10828
         tree_set_ident2(t, error_marker());
1✔
10829
      }
10830
   }
10831
   else
10832
      tree_set_ident2(t, p_selected_identifier());
1,566✔
10833

10834
   if ((tree_class(t) == C_ENTITY) && optional(tLPAREN)) {
1,569✔
10835
      tree_set_ident2(t, ident_prefix(tree_ident2(t), p_identifier(), '-'));
68✔
10836
      consume(tRPAREN);
68✔
10837
   }
10838

10839
   tree_set_loc(t, CURRENT_LOC);
1,569✔
10840
   return t;
1,569✔
10841
}
10842

10843
static tree_t p_component_instantiation_statement(ident_t label, tree_t name)
1,569✔
10844
{
10845
   // label : instantiated_unit [ generic_map_aspect ] [ port_map_aspect ] ;
10846

10847
   EXTEND("component instantiation statement");
1,569✔
10848

10849
   tree_t t = p_instantiated_unit(name);
1,569✔
10850
   tree_set_ident(t, label);
1,569✔
10851
   tree_set_loc(t, CURRENT_LOC);
1,569✔
10852

10853
   tree_t ref = find_binding(t);
1,569✔
10854
   tree_set_ref(t, ref);
1,569✔
10855

10856
   tree_t entity = ref ? primary_unit_of(ref) : NULL;
1,569✔
10857

10858
   tree_t spec = query_spec(nametab, t);
1,569✔
10859
   if (spec != NULL)
1,569✔
10860
      tree_set_spec(t, spec);
102✔
10861

10862
   if (label != NULL)
1,569✔
10863
      insert_name(nametab, t, NULL);
1,567✔
10864

10865
   push_scope(nametab);
1,569✔
10866

10867
   if (ref == NULL) suppress_errors(nametab);
1,569✔
10868

10869
   if (peek() == tGENERIC)
1,569✔
10870
      p_generic_map_aspect(t, entity);
627✔
10871

10872
   if (peek() == tPORT)
1,569✔
10873
      p_port_map_aspect(t, entity);
1,155✔
10874

10875
   consume(tSEMI);
1,569✔
10876

10877
   tree_set_loc(t, CURRENT_LOC);
1,569✔
10878

10879
   if (label == NULL){
1,569✔
10880
      parse_error(CURRENT_LOC, "component instantiation statement must "
2✔
10881
                  "have a label");
10882
      tree_set_ident(t, error_marker());
2✔
10883
   }
10884

10885
   sem_check(t, nametab);
1,569✔
10886
   pop_scope(nametab);
1,569✔
10887

10888
   return t;
1,569✔
10889
}
10890

10891
static void p_options(tree_t *reject, tree_t *guard)
2,305✔
10892
{
10893
   // [ guarded ] [ delay_mechanism ]
10894

10895
   BEGIN("options");
2,305✔
10896

10897
   if (optional(tGUARDED)) {
2,305✔
10898
      tree_t decl = NULL;
24✔
10899
      name_mask_t mask = query_name(nametab, ident_new("GUARD"), &decl);
24✔
10900
      if ((mask & N_OBJECT) && decl != NULL) {
24✔
10901
         tree_t g = tree_new(T_GUARD);
22✔
10902
         tree_set_loc(g, CURRENT_LOC);
22✔
10903
         tree_set_ref(g, decl);
22✔
10904
         tree_set_type(g, tree_type(decl));
22✔
10905

10906
         *guard = g;
22✔
10907
      }
10908
      else
10909
         parse_error(CURRENT_LOC, "guarded assignment has no visible "
24✔
10910
                     "guard signal");
10911
   }
10912

10913
   *reject = p_delay_mechanism();
2,305✔
10914
}
2,305✔
10915

10916
static void p_conditional_waveforms(tree_t stmt, tree_t target, tree_t s0)
2,263✔
10917
{
10918
   // { waveform when condition else } waveform [ when condition ]
10919

10920
   BEGIN("conditional waveforms");
4,526✔
10921

10922
   for (;;) {
2,431✔
10923
      tree_t c = tree_new(T_COND_STMT);
2,431✔
10924

10925
      tree_t a = s0;
2,431✔
10926
      if (a == NULL) {
2,431✔
10927
         a = tree_new(T_SIGNAL_ASSIGN);
2,426✔
10928
         tree_set_target(a, target);
2,426✔
10929
         p_waveform(a, target);
2,426✔
10930
      }
10931
      else {
10932
         s0 = NULL;
5✔
10933
         tree_set_loc(a, CURRENT_LOC);
5✔
10934
      }
10935
      tree_add_stmt(c, a);
2,431✔
10936
      tree_add_cond(stmt, c);
2,431✔
10937

10938
      if (optional(tWHEN)) {
2,431✔
10939
         tree_t when = p_condition();
256✔
10940
         tree_set_value(c, when);
256✔
10941
         tree_set_loc(c, tree_loc(when));
256✔
10942
         solve_types(nametab, when, std_type(NULL, STD_BOOLEAN));
256✔
10943

10944
         if (!optional(tELSE))
256✔
10945
            break;
10946
      }
10947
      else
10948
         break;
10949
   }
10950
}
2,263✔
10951

10952
static tree_t p_conditional_signal_assignment(tree_t name)
2,258✔
10953
{
10954
   // target <= options conditional_waveforms ;
10955

10956
   BEGIN("conditional signal assignment");
2,258✔
10957

10958
   tree_t conc = tree_new(T_CONCURRENT);
2,258✔
10959
   tree_t stmt = tree_new(T_COND_ASSIGN);
2,258✔
10960
   tree_add_stmt(conc, stmt);
2,258✔
10961

10962
   tree_t target = p_target(name);
2,258✔
10963
   tree_set_target(stmt, target);
2,258✔
10964

10965
   consume(tLE);
2,258✔
10966

10967
   tree_t reject = NULL, guard = NULL;
2,258✔
10968
   p_options(&reject, &guard);
2,258✔
10969

10970
   if (guard != NULL) {
2,258✔
10971
      tree_set_guard(stmt, guard);
15✔
10972
      find_disconnect_specification(guard, target);
15✔
10973
   }
10974

10975
   p_conditional_waveforms(stmt, target, NULL);
2,258✔
10976

10977
   const int nconds = tree_conds(stmt);
2,258✔
10978
   for (int i = 0; i < nconds; i++) {
4,679✔
10979
      tree_t c = tree_cond(stmt, i);
2,421✔
10980
      assert(tree_stmts(c) == 1);
2,421✔
10981
      set_delay_mechanism(tree_stmt(c, 0), reject);
2,421✔
10982
   }
10983

10984
   consume(tSEMI);
2,258✔
10985

10986
   tree_set_loc(stmt, CURRENT_LOC);
2,258✔
10987
   tree_set_loc(conc, CURRENT_LOC);
2,258✔
10988
   return conc;
2,258✔
10989
}
10990

10991
static void p_selected_waveforms(tree_t stmt, tree_t target, tree_t reject)
50✔
10992
{
10993
   // { waveform when choices , } waveform when choices
10994

10995
   BEGIN("selected waveforms");
100✔
10996

10997
   type_t with_type = tree_type(tree_value(stmt));
50✔
10998

10999
   do {
234✔
11000
      tree_t a = tree_new(T_SIGNAL_ASSIGN);
234✔
11001
      tree_set_target(a, target);
234✔
11002
      if (reject != NULL)
234✔
11003
         tree_set_reject(a, reject);
×
11004

11005
      p_waveform(a, target);
234✔
11006

11007
      sem_check(a, nametab);
234✔
11008

11009
      consume(tWHEN);
234✔
11010

11011
      tree_t alt = tree_new(T_ALTERNATIVE);
234✔
11012
      tree_add_stmt(alt, a);
234✔
11013

11014
      p_choices(alt, NULL, with_type);
234✔
11015

11016
      tree_set_loc(alt, CURRENT_LOC);
234✔
11017
      tree_add_stmt(stmt, alt);
234✔
11018
   } while (optional(tCOMMA));
234✔
11019
}
50✔
11020

11021
static tree_t p_selected_signal_assignment(void)
47✔
11022
{
11023
   // with expression select target <= options selected_waveforms ;
11024

11025
   BEGIN("selected signal assignment");
47✔
11026

11027
   consume(tWITH);
47✔
11028

11029
   tree_t value = p_expression();
47✔
11030
   solve_types(nametab, value, NULL);
47✔
11031

11032
   consume(tSELECT);
47✔
11033

11034
   tree_t stmt;
47✔
11035
   if (optional(tQUESTION)) {
47✔
11036
      require_std(STD_08, "matching select statements");
9✔
11037
      stmt = tree_new(T_MATCH_SELECT);
9✔
11038
   }
11039
   else
11040
      stmt = tree_new(T_SELECT);
38✔
11041

11042
   tree_set_value(stmt, value);
47✔
11043

11044
   tree_t conc = tree_new(T_CONCURRENT);
47✔
11045
   tree_add_stmt(conc, stmt);
47✔
11046

11047
   tree_t target = p_target(NULL);
47✔
11048

11049
   consume(tLE);
47✔
11050

11051
   tree_t reject = NULL, guard = NULL;
47✔
11052
   p_options(&reject, &guard);
47✔
11053

11054
   if (guard != NULL) {
47✔
11055
      tree_set_guard(stmt, guard);
7✔
11056
      find_disconnect_specification(guard, target);
7✔
11057
   }
11058

11059
   p_selected_waveforms(stmt, target, reject);
47✔
11060
   consume(tSEMI);
47✔
11061

11062
   tree_set_loc(stmt, CURRENT_LOC);
47✔
11063
   tree_set_loc(conc, CURRENT_LOC);
47✔
11064
   return conc;
47✔
11065
}
11066

11067
static tree_t p_concurrent_signal_assignment_statement(ident_t label,
2,305✔
11068
                                                       tree_t name)
11069
{
11070
   // [ label : ] [ postponed ] conditional_signal_assignment
11071
   //   | [ label : ] [ postponed ] selected_signal_assignment
11072

11073
   EXTEND("concurrent signal assignment statement");
2,305✔
11074

11075
   const bool postponed = name == NULL && optional(tPOSTPONED);
2,305✔
11076

11077
   tree_t t;
2,305✔
11078
   if (peek() == tWITH) {
2,305✔
11079
      assert(name == NULL);
47✔
11080
      t = p_selected_signal_assignment();
47✔
11081
   }
11082
   else
11083
      t = p_conditional_signal_assignment(name);
2,258✔
11084

11085
   tree_set_loc(t, CURRENT_LOC);
2,305✔
11086
   ensure_labelled(t, label);
2,305✔
11087

11088
   if (postponed)
2,305✔
11089
      tree_set_flag(t, TREE_F_POSTPONED);
×
11090

11091
   if (label) insert_name(nametab, t, NULL);
2,305✔
11092
   sem_check(t, nametab);
2,305✔
11093
   return t;
2,305✔
11094
}
11095

11096
static tree_t p_concurrent_procedure_call_statement(ident_t label, tree_t name)
68✔
11097
{
11098
   // [ label : ] [ postponed ] procedure_call ;
11099

11100
   EXTEND("concurrent procedure call statement");
68✔
11101

11102
   const bool postponed = name == NULL && optional(tPOSTPONED);
68✔
11103

11104
   tree_t call = NULL;
68✔
11105
   if (name == NULL) {
68✔
11106
      call = tree_new(T_PCALL);
6✔
11107
      tree_set_ident2(call, p_identifier());
6✔
11108
   }
11109
   else if (tree_kind(name) == T_PROT_REF) {
62✔
11110
      call = tree_new(T_PROT_PCALL);
3✔
11111
      tree_set_ident2(call, tree_ident(name));
3✔
11112
      tree_set_name(call, tree_value(name));
3✔
11113
   }
11114
   else {
11115
      call = tree_new(T_PCALL);
59✔
11116
      tree_set_ident2(call, tree_ident(name));
59✔
11117
   }
11118

11119
   if (optional(tLPAREN)) {
68✔
11120
      p_actual_parameter_part(call);
54✔
11121
      consume(tRPAREN);
54✔
11122
   }
11123

11124
   consume(tSEMI);
68✔
11125

11126
   tree_set_loc(call, CURRENT_LOC);
68✔
11127

11128
   solve_types(nametab, call, NULL);
68✔
11129

11130
   tree_t conc = tree_new(T_CONCURRENT);
68✔
11131
   tree_add_stmt(conc, call);
68✔
11132

11133
   if (postponed)
68✔
11134
      tree_set_flag(conc, TREE_F_POSTPONED);
3✔
11135

11136
   tree_set_loc(conc, CURRENT_LOC);
68✔
11137
   ensure_labelled(conc, label);
68✔
11138
   tree_set_ident(call, tree_ident(conc));
68✔
11139

11140
   if (label) insert_name(nametab, conc, NULL);
68✔
11141
   sem_check(conc, nametab);
68✔
11142
   return conc;
68✔
11143
}
11144

11145
static void p_concurrent_statement_or_psl(tree_t parent)
11,246✔
11146
{
11147
   // Allow PSL declarations in concurrent statement part when using
11148
   // "--psl" comments
11149

11150
   if (peek() == tSTARTPSL) {
11,246✔
11151
      consume(tSTARTPSL);
400✔
11152

11153
      if (peek() == tID) {
400✔
11154
         ident_t label = p_identifier();
199✔
11155
         consume(tCOLON);
199✔
11156
         tree_add_stmt(parent, p_psl_directive(label));
199✔
11157
      }
11158
      else if (scan(tDEFAULT, tSEQUENCE, tPROPERTY, tENDPOINT))
201✔
11159
         p_psl_declaration(parent);
66✔
11160
      else
11161
         tree_add_stmt(parent, p_psl_directive(NULL));
135✔
11162
   }
11163
   else
11164
      tree_add_stmt(parent, p_concurrent_statement());
10,846✔
11165
}
11,243✔
11166

11167
static void p_block_statement_part(tree_t arch)
426✔
11168
{
11169
   // { concurrent_statement }
11170

11171
   BEGIN("block statement part");
852✔
11172

11173
   while (not_at_token(tEND))
881✔
11174
      p_concurrent_statement_or_psl(arch);
455✔
11175
}
426✔
11176

11177
static void p_block_declarative_part(tree_t arch)
426✔
11178
{
11179
   // { block_declarative_item }
11180

11181
   BEGIN("block declarative part");
852✔
11182

11183
   while (not_at_token(tBEGIN))
810✔
11184
      p_block_declarative_item(arch);
384✔
11185
}
426✔
11186

11187
static void p_block_header(tree_t block)
426✔
11188
{
11189
   // [ generic_clause [ generic_map_aspect ; ] ]
11190
   //   [ port_clause [ port_map_aspect ; ] ]
11191

11192
   if (peek() == tGENERIC) {
426✔
11193
      p_generic_clause(block);
57✔
11194

11195
      if (peek() == tGENERIC) {
57✔
11196
         p_generic_map_aspect(block, block);
50✔
11197
         consume(tSEMI);
50✔
11198
      }
11199

11200
      insert_generics(nametab, block);
57✔
11201
   }
11202

11203
   if (peek() == tPORT) {
426✔
11204
      p_port_clause(block);
191✔
11205

11206
      if (peek() == tPORT) {
191✔
11207
         p_port_map_aspect(block, block);
189✔
11208
         consume(tSEMI);
189✔
11209
      }
11210

11211
      insert_ports(nametab, block);
191✔
11212
   }
11213
}
426✔
11214

11215
static tree_t p_block_statement(ident_t label)
426✔
11216
{
11217
   // label : block [ ( expression ) ] [ is ] block_header
11218
   //   block_declarative_part begin block_statement_part end block [ label ] ;
11219

11220
   EXTEND("block statement");
426✔
11221

11222
   tree_t b = tree_new(T_BLOCK);
426✔
11223
   tree_set_ident(b, label);
426✔
11224

11225
   consume(tBLOCK);
426✔
11226

11227
   if (label == NULL)
426✔
11228
      parse_error(CURRENT_LOC, "block statement must have a label");
1✔
11229
   else {
11230
      tree_set_loc(b, CURRENT_LOC);
425✔
11231
      insert_name(nametab, b, NULL);
425✔
11232
   }
11233

11234
   push_scope(nametab);
426✔
11235
   scope_set_prefix(nametab, label ?: error_marker());
426✔
11236
   scope_set_container(nametab, b);
426✔
11237

11238
   if (peek() == tLPAREN) {
426✔
11239
      consume(tLPAREN);
27✔
11240

11241
      tree_t expr = p_expression();
27✔
11242
      solve_condition(nametab, &expr);
27✔
11243

11244
      make_implicit_guard_signal(b, expr);
27✔
11245

11246
      consume(tRPAREN);
27✔
11247
   }
11248

11249
   optional(tIS);
426✔
11250
   p_block_header(b);
426✔
11251
   p_block_declarative_part(b);
426✔
11252
   consume(tBEGIN);
426✔
11253
   p_block_statement_part(b);
426✔
11254
   consume(tEND);
426✔
11255
   consume(tBLOCK);
426✔
11256
   p_trailing_label(label);
426✔
11257
   consume(tSEMI);
426✔
11258

11259
   tree_set_loc(b, CURRENT_LOC);
426✔
11260
   sem_check(b, nametab);
426✔
11261

11262
   hash_t *map = get_generic_map(nametab);
426✔
11263
   if (map != NULL)
426✔
11264
      instance_fixup(b, map);
44✔
11265

11266
   pop_scope(nametab);
426✔
11267
   return b;
426✔
11268
}
11269

11270
static void p_generate_statement_body(tree_t container, ident_t alt_label)
422✔
11271
{
11272
   // [ block_declarative_part begin ] { concurrent_statement }
11273
   //   [ end [ alternative_label ] ; ]
11274

11275
   BEGIN("generate statement body");
844✔
11276

11277
   if (scan(tSIGNAL, tTYPE, tSUBTYPE, tFILE, tCONSTANT, tFUNCTION, tIMPURE,
422✔
11278
            tPURE, tALIAS, tATTRIBUTE, tBEGIN, tPROCEDURE, tFOR, tCOMPONENT,
11279
            tUSE, tSHARED)) {
11280
      while (not_at_token(tBEGIN))
194✔
11281
         p_block_declarative_item(container);
79✔
11282
      consume(tBEGIN);
115✔
11283
   }
11284

11285
   while (not_at_token(tEND, tELSIF, tELSE, tWHEN))
887✔
11286
      p_concurrent_statement_or_psl(container);
465✔
11287

11288
   if (peek() == tEND && (peek_nth(2) == tID || peek_nth(2) == tSEMI)) {
422✔
11289
      consume(tEND);
9✔
11290
      p_trailing_label(alt_label);
9✔
11291
      consume(tSEMI);
9✔
11292
   }
11293
}
422✔
11294

11295
static tree_t p_for_generate_statement(ident_t label)
178✔
11296
{
11297
   // for generate_parameter_specification generate generate_statement_body
11298
   //   end generate [ generate_label ] ;
11299

11300
   EXTEND("for generate statement");
178✔
11301

11302
   consume(tFOR);
178✔
11303

11304
   tree_t g = tree_new(T_FOR_GENERATE);
178✔
11305
   tree_set_ident(g, label);
178✔
11306

11307
   if (label != NULL)
178✔
11308
      insert_name(nametab, g, NULL);
177✔
11309

11310
   push_scope(nametab);
178✔
11311
   scope_set_prefix(nametab, label);
178✔
11312
   scope_set_container(nametab, g);
178✔
11313

11314
   p_parameter_specification(g, T_GENERIC_DECL);
178✔
11315

11316
   consume(tGENERATE);
178✔
11317

11318
   p_generate_statement_body(g, NULL);
178✔
11319

11320
   consume(tEND);
178✔
11321
   consume(tGENERATE);
178✔
11322
   p_trailing_label(label);
178✔
11323
   consume(tSEMI);
178✔
11324

11325
   pop_scope(nametab);
178✔
11326

11327
   if (label == NULL)
178✔
11328
      parse_error(CURRENT_LOC, "generate statement must have a label");
1✔
11329

11330
   tree_set_loc(g, CURRENT_LOC);
178✔
11331
   sem_check(g, nametab);
178✔
11332
   return g;
178✔
11333
}
11334

11335
static tree_t p_if_generate_statement(ident_t label)
176✔
11336
{
11337
   // if [ alternative_label : ] condition generate generate_statement_body
11338
   //   { elsif [ alternative_label : ] condition generate
11339
   //     generate_statement_body }
11340
   //   [ else [ alternative_label : ] generate generate_statement_body ]
11341
   //   end generate [ generate_label ] ;
11342

11343
   EXTEND("if generate statement");
176✔
11344

11345
   consume(tIF);
176✔
11346

11347
   tree_t g = tree_new(T_IF_GENERATE);
176✔
11348
   tree_set_ident(g, label);
176✔
11349

11350
   if (label != NULL)
176✔
11351
      insert_name(nametab, g, NULL);
175✔
11352

11353
   ident_t alt_label = NULL;
176✔
11354
   if (peek() == tID && peek_nth(2) == tCOLON) {
176✔
11355
      require_std(STD_08, "alternative labels");
4✔
11356

11357
      alt_label = p_identifier();
4✔
11358
      consume(tCOLON);
4✔
11359
   }
11360

11361
   push_scope(nametab);
176✔
11362
   scope_set_container(nametab, g);
176✔
11363
   scope_set_prefix(nametab, alt_label ?: label);
348✔
11364

11365
   tree_t c0 = tree_new(T_COND_STMT);
176✔
11366
   tree_set_ident(c0, alt_label ?: label);
176✔
11367
   tree_set_value(c0, p_condition());
176✔
11368

11369
   tree_add_cond(g, c0);
176✔
11370

11371
   consume(tGENERATE);
176✔
11372

11373
   p_generate_statement_body(c0, alt_label);
176✔
11374

11375
   pop_scope(nametab);
176✔
11376

11377
   tree_set_loc(c0, CURRENT_LOC);
176✔
11378

11379
   while (optional(tELSIF)) {
183✔
11380
      require_std(STD_08, "elsif in generate statements");
7✔
11381

11382
      ident_t alt_label = NULL;
7✔
11383
      if (peek() == tID && peek_nth(2) == tCOLON) {
7✔
11384
         alt_label = p_identifier();
7✔
11385
         consume(tCOLON);
7✔
11386
      }
11387

11388
      push_scope(nametab);
7✔
11389
      scope_set_prefix(nametab, alt_label ?: label);
7✔
11390

11391
      tree_t c = tree_new(T_COND_STMT);
7✔
11392
      tree_set_ident(c, alt_label ?: label);
7✔
11393
      tree_set_value(c, p_condition());
7✔
11394

11395
      consume(tGENERATE);
7✔
11396

11397
      p_generate_statement_body(c, alt_label);
7✔
11398

11399
      pop_scope(nametab);
7✔
11400

11401
      tree_set_loc(c, CURRENT_LOC);
7✔
11402
      tree_add_cond(g, c);
7✔
11403
   }
11404

11405
   if (optional(tELSE)) {
176✔
11406
      require_std(STD_08, "else in generate statements");
12✔
11407

11408
      ident_t alt_label = label;
12✔
11409
      if (peek() == tID && peek_nth(2) == tCOLON) {
12✔
11410
         alt_label = p_identifier();
7✔
11411
         consume(tCOLON);
7✔
11412
      }
11413

11414
      push_scope(nametab);
12✔
11415
      scope_set_prefix(nametab, alt_label ?: label);
12✔
11416

11417
      tree_t c = tree_new(T_COND_STMT);
12✔
11418
      tree_set_ident(c, alt_label ?: label);
12✔
11419

11420
      consume(tGENERATE);
12✔
11421

11422
      p_generate_statement_body(c, alt_label);
12✔
11423

11424
      pop_scope(nametab);
12✔
11425

11426
      tree_set_loc(c, CURRENT_LOC);
12✔
11427
      tree_add_cond(g, c);
12✔
11428
   }
11429

11430
   consume(tEND);
176✔
11431
   consume(tGENERATE);
176✔
11432
   p_trailing_label(label);
176✔
11433
   consume(tSEMI);
176✔
11434

11435
   if (label == NULL)
176✔
11436
      parse_error(CURRENT_LOC, "generate statement must have a label");
1✔
11437

11438
   tree_set_loc(g, CURRENT_LOC);
176✔
11439
   sem_check(g, nametab);
176✔
11440
   return g;
176✔
11441
}
11442

11443
static tree_t p_case_generate_alternative(type_t type)
49✔
11444
{
11445
   // when [ alternative_label : ] choices => generate_statement_body
11446

11447
   BEGIN("case generate alternative");
49✔
11448

11449
   consume(tWHEN);
49✔
11450

11451
   ident_t alt_label = NULL;
49✔
11452
   if (peek() == tID && peek_nth(2) == tCOLON) {
49✔
11453
      alt_label = p_identifier();
15✔
11454
      consume(tCOLON);
15✔
11455
   }
11456

11457
   tree_t alt = tree_new(T_ALTERNATIVE);
49✔
11458
   tree_set_ident(alt, alt_label);
49✔
11459
   p_choices(alt, NULL, type);
49✔
11460

11461
   consume(tASSOC);
49✔
11462

11463
   push_scope(nametab);
49✔
11464
   scope_set_prefix(nametab, alt_label);
49✔
11465

11466
   p_generate_statement_body(alt, alt_label);
49✔
11467

11468
   tree_set_loc(alt, CURRENT_LOC);
49✔
11469
   pop_scope(nametab);
49✔
11470

11471
   return alt;
49✔
11472
}
11473

11474
static tree_t p_case_generate_statement(ident_t label)
19✔
11475
{
11476
   // case expression generate case_generate_alternative
11477
   //   { case_generate_alternative } end generate [ generate_label ] ;
11478

11479
   EXTEND("case generate statement");
19✔
11480

11481
   consume(tCASE);
19✔
11482

11483
   require_std(STD_08, "case generate statements");
19✔
11484

11485
   tree_t g = tree_new(T_CASE_GENERATE);
19✔
11486
   tree_set_ident(g, label);
19✔
11487

11488
   if (label != NULL)
19✔
11489
      insert_name(nametab, g, NULL);
19✔
11490

11491
   tree_t value = p_expression();
19✔
11492
   tree_set_value(g, value);
19✔
11493

11494
   type_t type = solve_types(nametab, value, NULL);
19✔
11495

11496
   consume(tGENERATE);
19✔
11497

11498
   do {
49✔
11499
      tree_add_stmt(g, p_case_generate_alternative(type));
49✔
11500
   } while (peek() == tWHEN);
49✔
11501

11502
   consume(tEND);
19✔
11503
   consume(tGENERATE);
19✔
11504

11505
   p_trailing_label(label);
19✔
11506
   consume(tSEMI);
19✔
11507

11508
   if (label == NULL)
19✔
11509
      parse_error(CURRENT_LOC, "generate statement must have a label");
×
11510

11511
   tree_set_loc(g, CURRENT_LOC);
19✔
11512
   sem_check(g, nametab);
19✔
11513
   return g;
19✔
11514
}
11515

11516
static tree_t p_generate_statement(ident_t label)
373✔
11517
{
11518
   // for_generate_statement | if_generate_statement | case_generate_statement
11519

11520
   EXTEND("generate statement");
746✔
11521

11522
   switch (peek()) {
373✔
11523
   case tFOR:
178✔
11524
      return p_for_generate_statement(label);
178✔
11525
   case tIF:
176✔
11526
      return p_if_generate_statement(label);
176✔
11527
   case tCASE:
19✔
11528
      return p_case_generate_statement(label);
19✔
11529
   default:
×
11530
      expect(tFOR, tIF, tCASE);
×
11531
      drop_tokens_until(tSEMI);
×
11532
      return ensure_labelled(tree_new(T_BLOCK), label);
×
11533
   }
11534
}
11535

11536
static psl_node_t p_psl_low_bound(tree_t head)
32✔
11537
{
11538
   // Number | MIN_VAL
11539

11540
   BEGIN_WITH_HEAD("PSL Low Bound", head);
64✔
11541

11542
   return p_hdl_expression(head, PSL_TYPE_NUMERIC);
32✔
11543
}
11544

11545
static psl_node_t p_psl_high_bound(void)
32✔
11546
{
11547
   // Number | MAX_VAL
11548

11549
   BEGIN("PSL Low Bound");
64✔
11550

11551
   if (optional(tINF)) {
32✔
11552
      tree_t inf = tree_new(T_LITERAL);
6✔
11553
      tree_set_loc(inf, CURRENT_LOC);
6✔
11554
      tree_set_subkind(inf, L_INT);
6✔
11555
      tree_set_ival(inf, INT32_MAX);
6✔
11556

11557
      psl_node_t p = psl_new(P_HDL_EXPR);
6✔
11558
      psl_set_type(p, PSL_TYPE_NUMERIC);
6✔
11559
      psl_set_tree(p, inf);
6✔
11560
      psl_set_loc(p, CURRENT_LOC);
6✔
11561
      return p;
6✔
11562
   }
11563
   else
11564
      return p_hdl_expression(NULL, PSL_TYPE_NUMERIC);
26✔
11565
}
11566

11567
static psl_node_t p_psl_range(tree_t head)
32✔
11568
{
11569
   // Low_Bound RANGE_SYM High_Bound
11570

11571
   BEGIN_WITH_HEAD("PSL Range", head);
32✔
11572

11573
   psl_node_t low = p_psl_low_bound(head);
32✔
11574

11575
   consume(tTO);
32✔
11576

11577
   psl_node_t high = p_psl_high_bound();
32✔
11578

11579
   psl_node_t p = psl_new(P_RANGE);
32✔
11580
   psl_set_left(p, low);
32✔
11581
   psl_set_right(p, high);
32✔
11582

11583
   psl_set_loc(p, CURRENT_LOC);
32✔
11584
   return p;
32✔
11585
}
11586

11587
static psl_node_t p_psl_value_range(void)
10✔
11588
{
11589
   // Value | Range
11590

11591
   BEGIN("PSL Value Range");
20✔
11592

11593
   tree_t tree = p_expression();
10✔
11594

11595
   if (peek() == tTO)
10✔
11596
      return p_psl_range(tree);
4✔
11597

11598
   psl_node_t p = psl_new(P_HDL_EXPR);
6✔
11599
   psl_set_tree(p, tree);
6✔
11600

11601
   psl_set_loc(p, CURRENT_LOC);
6✔
11602
   return p;
6✔
11603
}
11604

11605
static psl_node_t p_psl_value_set(void)
7✔
11606
{
11607
   // { Value_Range { , Value_Range } } | boolean
11608

11609
   BEGIN("PSL Value Set");
7✔
11610

11611
   psl_node_t p = psl_new(P_VALUE_SET);
7✔
11612

11613
   switch (one_of(tBOOLEAN, tLBRACE)) {
7✔
11614
   case tBOOLEAN:
3✔
11615
      psl_set_subkind(p, PSL_VALUE_SET_BOOLEAN);
3✔
11616
      break;
3✔
11617
   case tLBRACE:
4✔
11618
      {
11619
         psl_set_subkind(p, PSL_VALUE_SET_EXPLICIT);
4✔
11620

11621
         do {
10✔
11622
            psl_add_operand(p, p_psl_value_range());
10✔
11623
         } while (optional(tCOMMA));
10✔
11624

11625
         consume(tRBRACE);
4✔
11626
      }
11627
      break;
4✔
11628
   }
11629

11630
   psl_set_loc(p, CURRENT_LOC);
7✔
11631
   return p;
7✔
11632
}
11633

11634
static psl_node_t p_psl_union(tree_t head)
25✔
11635
{
11636
   consume(tUNION);
25✔
11637

11638
   psl_node_t lhs = psl_new(P_HDL_EXPR);
25✔
11639
   psl_set_tree(lhs, head);
25✔
11640
   psl_set_loc(lhs, tree_loc(head));
25✔
11641
   psl_set_type(lhs, PSL_TYPE_ANY);
25✔
11642
   psl_set_loc(lhs, CURRENT_LOC);
25✔
11643

11644
   psl_node_t un = psl_new(P_UNION);
25✔
11645
   psl_add_operand(un, lhs);
25✔
11646
   psl_add_operand(un, p_hdl_expression(NULL, PSL_TYPE_ANY));
25✔
11647
   psl_set_loc(un, CURRENT_LOC);
25✔
11648

11649
   return un;
25✔
11650
}
11651

11652
static psl_node_t p_hdl_expression(tree_t head, psl_type_t type)
1,190✔
11653
{
11654
   BEGIN("PSL HDL expression");
1,190✔
11655

11656
   tree_t expr = p_relation(head);
1,190✔
11657

11658
   // AND and OR must be parsed as PSL operators
11659
   for (;;) {
1,205✔
11660
      const token_t tok = peek();
1,205✔
11661
      if (tok == tUNION) {
1,205✔
11662
         psl_node_t un = p_psl_union(expr);
13✔
11663
         tree_t new = tree_new(T_PSL_UNION);
13✔
11664
         tree_set_psl(new, un);
13✔
11665
         tree_set_loc(new, CURRENT_LOC);
13✔
11666
         expr = new;
13✔
11667
      }
11668
      else if (tok == tNOR || tok == tNAND) {
1,192✔
11669
         tree_t new = tree_new(T_FCALL);
×
11670
         tree_set_ident(new, p_logical_operator());
×
11671
         binary_op(new, expr, p_relation);
×
11672
         expr = new;
×
11673
         break;
×
11674
      }
11675
      else if (tok == tXOR || tok == tXNOR) {
1,192✔
11676
         tree_t new = tree_new(T_FCALL);
2✔
11677
         tree_set_ident(new, p_logical_operator());
2✔
11678
         binary_op(new, expr, p_relation);
2✔
11679
         expr = new;
2✔
11680
      }
11681
      else
11682
         break;
11683
   }
11684

11685
   psl_node_t p = psl_new(P_HDL_EXPR);
1,190✔
11686
   psl_set_tree(p, expr);
1,190✔
11687
   psl_set_loc(p, tree_loc(expr));
1,190✔
11688
   psl_set_type(p, type);
1,190✔
11689
   psl_set_loc(p, CURRENT_LOC);
1,190✔
11690

11691
   return p;
1,190✔
11692
}
11693

11694
static psl_node_t p_psl_boolean(tree_t head)
456✔
11695
{
11696
   // HDL_Expression | PSL_Expression | Built_In_Function_Call
11697
   //    | Union_Expression
11698

11699
   BEGIN_WITH_HEAD("PSL Boolean", head);
912✔
11700

11701
   psl_node_t p = p_hdl_expression(head, PSL_TYPE_BOOLEAN);
456✔
11702

11703
   const token_t infix = peek();
456✔
11704
   switch (infix) {
456✔
11705
   case tAND:
54✔
11706
   case tOR:
11707
      {
11708
         consume(infix);
54✔
11709

11710
         psl_node_t right = p_psl_boolean(NULL);
54✔
11711

11712
         tree_t fcall = tree_new(T_FCALL);
54✔
11713
         tree_set_ident(fcall, well_known(infix == tOR ? W_OP_OR : W_OP_AND));
108✔
11714
         tree_set_loc(fcall, CURRENT_LOC);
54✔
11715
         add_param(fcall, psl_tree(p), P_POS, NULL);
54✔
11716
         add_param(fcall, psl_tree(right), P_POS, NULL);
54✔
11717

11718
         psl_set_tree(p, fcall);
54✔
11719
         psl_set_loc(p, CURRENT_LOC);
54✔
11720
         return p;
54✔
11721
      }
11722

11723
   default:
11724
      return p;
11725
   }
11726
}
11727

11728
static psl_node_t p_psl_clock_expression(void)
96✔
11729
{
11730
   tree_t expr = p_expression();
96✔
11731
   solve_types(nametab, expr, std_type(NULL, STD_BOOLEAN));
96✔
11732

11733
   psl_node_t p = psl_new(P_CLOCK_DECL);
96✔
11734
   psl_set_tree(p, expr);
96✔
11735

11736
   return p;
96✔
11737
}
11738

11739
static tree_t p_psl_clock_declaration(void)
96✔
11740
{
11741
   // default clock is Clock_Expression ;
11742

11743
   BEGIN("PSL clock declaration");
96✔
11744

11745
   scan_as_psl();
96✔
11746

11747
   consume(tDEFAULT);
96✔
11748
   consume(tCLOCK);
96✔
11749

11750
   consume(tIS);
96✔
11751

11752
   scan_as_vhdl();
96✔
11753

11754
   psl_node_t p = p_psl_clock_expression();
96✔
11755

11756
   tree_t t = tree_new(T_PSL_DECL);
96✔
11757
   tree_set_psl(t, p);
96✔
11758
   tree_set_ident(t, well_known(W_DEFAULT_CLOCK));
96✔
11759

11760
   consume(tSEMI);
96✔
11761

11762
   psl_set_loc(p, CURRENT_LOC);
96✔
11763
   psl_check(p, nametab);
96✔
11764

11765
   insert_name(nametab, t, NULL);
96✔
11766

11767
   tree_set_loc(t, CURRENT_LOC);
96✔
11768
   return t;
96✔
11769
}
11770

11771
static psl_node_t p_psl_builtin_function_call(void)
49✔
11772
{
11773
   // prev (Any_Type [ , Number [ , Clock_Expression ]] )
11774
   //  | next ( Any_Type )
11775
   //  | stable ( Any_Type [ , Clock_Expression ] )
11776
   //  | rose ( Bit [ , Clock_Expression ] )
11777
   //  | fell ( Bit [ , Clock_Expression ] )
11778
   //  | ended ( Sequence [ , Clock_Expression ])
11779
   //  | nondet ( Value_Set )
11780
   //  | nondet_vector ( Number, Value_Set)
11781

11782
   BEGIN("PSL Built-in Function call");
49✔
11783

11784
   psl_builtin_kind_t kind;
49✔
11785
   switch (one_of(tPSLNEXT, tPREV, tSTABLE, tROSE, tFELL, tENDED,
49✔
11786
                  tNONDET, tNONDETV)) {
11787
   case tPSLNEXT: kind = PSL_BUILTIN_NEXT; break;
11788
   case tPREV:    kind = PSL_BUILTIN_PREV; break;
31✔
11789
   case tSTABLE:  kind = PSL_BUILTIN_STABLE; break;
3✔
11790
   case tROSE:    kind = PSL_BUILTIN_ROSE; break;
5✔
11791
   case tFELL:    kind = PSL_BUILTIN_FELL; break;
3✔
11792
   case tENDED:   kind = PSL_BUILTIN_ENDED; break;
1✔
11793
   case tNONDET:  kind = PSL_BUILTIN_NONDET; break;
6✔
11794
   case tNONDETV: kind = PSL_BUILTIN_NONDET_VECTOR; break;
×
11795
   default: should_not_reach_here();
11796
   }
11797

11798
   psl_node_t p = psl_new(P_BUILTIN_FCALL);
49✔
11799
   psl_set_subkind(p, kind);
49✔
11800

11801
   consume(tLPAREN);
49✔
11802

11803
   switch (kind) {
49✔
11804
   case PSL_BUILTIN_PREV:
31✔
11805
      {
11806
         psl_node_t p1 = p_hdl_expression(NULL, PSL_TYPE_ANY);
31✔
11807
         psl_add_operand(p, p1);
31✔
11808

11809
         if (optional(tCOMMA)) {
31✔
11810
            psl_node_t p2 = p_hdl_expression(NULL, PSL_TYPE_NUMERIC);
16✔
11811
            psl_add_operand(p, p2);
16✔
11812

11813
            if (optional(tCOMMA))
16✔
11814
               (void)p_psl_clock_expression();
×
11815
         }
11816
      }
11817
      break;
11818

11819
   case PSL_BUILTIN_NEXT:
×
11820
      {
11821
         psl_node_t p1 = p_hdl_expression(NULL, PSL_TYPE_ANY);
×
11822
         psl_add_operand(p, p1);
×
11823
      }
11824
      break;
×
11825

11826
   case PSL_BUILTIN_FELL:
11✔
11827
   case PSL_BUILTIN_ROSE:
11828
   case PSL_BUILTIN_STABLE:
11829
      {
11830
         psl_node_t p1 = p_hdl_expression(NULL, PSL_TYPE_BIT);
11✔
11831
         psl_add_operand(p, p1);
11✔
11832

11833
         if (optional(tCOMMA))
11✔
11834
            (void)p_psl_clock_expression();
×
11835
      }
11836
      break;
11837

11838
   case PSL_BUILTIN_ENDED:
1✔
11839
      {
11840
         psl_node_t p1 = p_psl_sequence();
1✔
11841
         psl_add_operand(p, p1);
1✔
11842

11843
         if (optional(tCOMMA))
1✔
11844
            (void)p_psl_clock_expression();
×
11845
      }
11846
      break;
11847

11848
   case PSL_BUILTIN_NONDET:
6✔
11849
      {
11850
         psl_node_t p1 = p_psl_value_set();
6✔
11851
         psl_add_operand(p, p1);
6✔
11852
      }
11853
      break;
6✔
11854

11855
   case PSL_BUILTIN_NONDET_VECTOR:
×
11856
      {
11857
         psl_node_t p1 = p_hdl_expression(NULL, PSL_TYPE_NUMERIC);
×
11858
         psl_add_operand(p, p1);
×
11859

11860
         consume(tCOMMA);
×
11861

11862
         psl_node_t p2 = p_psl_value_set();
×
11863
         psl_add_operand(p, p2);
×
11864
      }
11865
      break;
×
11866
   }
11867

11868
   consume(tRPAREN);
49✔
11869

11870
   psl_set_loc(p, CURRENT_LOC);
49✔
11871
   return p;
49✔
11872
}
11873

11874
static psl_node_t p_psl_index_range(void)
×
11875
{
11876
   // ( Range )
11877

11878
   BEGIN("PSL Index Range");
×
11879

11880
   consume(tLPAREN);
×
11881

11882
   psl_node_t p = p_psl_range(NULL);
×
11883

11884
   consume(tRPAREN);
×
11885

11886
   return p;
×
11887
}
11888

11889
static psl_node_t p_psl_parameter_definition(void)
1✔
11890
{
11891
   // Identifier [ Index_Range ] in Value_Set
11892

11893
   BEGIN("PSL Parameter Definition");
1✔
11894

11895
   tree_t decl = tree_new(T_PARAM_DECL);
1✔
11896

11897
   tree_set_ident(decl, p_identifier());
1✔
11898
   insert_name(nametab, decl, NULL);
1✔
11899

11900
   psl_node_t p = psl_new(P_PARAM_DECL);
1✔
11901
   psl_set_tree(p, decl);
1✔
11902

11903
   if (peek() == tLPAREN)
1✔
11904
      (void)p_psl_index_range();
×
11905

11906
   consume(tIN);
1✔
11907

11908
   psl_set_value(p, p_psl_value_set());
1✔
11909

11910
   psl_set_loc(p, CURRENT_LOC);
1✔
11911
   return p;
1✔
11912
}
11913

11914
static void p_psl_parameters_definition(psl_node_t p)
1✔
11915
{
11916
   // Parameter_Definition {, Parameter_Definition }
11917

11918
   BEGIN("PSL Parameters Definition");
2✔
11919

11920
   do {
1✔
11921
      psl_add_operand(p, p_psl_parameter_definition());
1✔
11922
   } while (optional(tCOMMA));
1✔
11923
}
1✔
11924

11925
static psl_node_t p_psl_parametrized_sere(void)
1✔
11926
{
11927
   // for Parameters_Definition : And_Or_SERE_OP { SERE }
11928

11929
   BEGIN("PSL Parametrized SERE");
1✔
11930

11931
   consume(tFOR);
1✔
11932

11933
   psl_node_t p = psl_new(P_PARAM_SERE);
1✔
11934
   p_psl_parameters_definition(p);
1✔
11935

11936
   consume(tCOLON);
1✔
11937

11938
   switch (one_of(tAMP, tDBLAMP, tBAR)) {
1✔
11939
   case tAMP:
×
11940
      psl_set_subkind(p, PSL_SERE_NEQ_AND);
×
11941
      break;
×
11942
   case tDBLAMP:
1✔
11943
      psl_set_subkind(p, PSL_SERE_EQU_AND);
1✔
11944
      break;
1✔
11945
   case tBAR:
×
11946
      psl_set_subkind(p, PSL_SERE_OR);
×
11947
      break;
×
11948
   }
11949

11950
   consume(tLBRACE);
1✔
11951

11952
   psl_set_value(p, p_psl_sere());
1✔
11953

11954
   consume(tRBRACE);
1✔
11955

11956
   psl_set_loc(p, CURRENT_LOC);
1✔
11957
   return p;
1✔
11958
}
11959

11960
static psl_node_t p_psl_count(void)
48✔
11961
{
11962
   // Number | Range
11963

11964
   BEGIN("PSL Count");
96✔
11965

11966
   tree_t count = p_expression();
48✔
11967

11968
   if (peek() == tTO)
48✔
11969
      return p_psl_range(count);
15✔
11970
   else
11971
      return p_hdl_expression(count, PSL_TYPE_NUMERIC);
33✔
11972
}
11973

11974
static psl_node_t p_psl_proc_block(psl_node_t value)
4✔
11975
{
11976
   // [[ Proc_Block_Item { Proc_Block_Item }]]
11977

11978
   BEGIN("PSL Procedural Block");
4✔
11979

11980
   consume(t2LSQUARE);
4✔
11981

11982
   tree_t b = tree_new(T_SEQUENCE);
4✔
11983

11984
   psl_node_t p = psl_new(P_PROC_BLOCK);
4✔
11985
   psl_set_value(p, value);
4✔
11986
   psl_set_tree(p, b);
4✔
11987

11988
   scan_as_vhdl();
4✔
11989

11990
   while (not_at_token(t2RSQUARE)) {
11✔
11991
      if (scan(tSIGNAL, tTYPE, tSUBTYPE, tFILE, tCONSTANT, tFUNCTION, tIMPURE,
7✔
11992
               tPURE, tPROCEDURE, tALIAS, tATTRIBUTE, tFOR, tCOMPONENT, tUSE,
11993
               tSHARED, tDISCONNECT, tGROUP, tPACKAGE))
11994
         p_block_declarative_item(b);
3✔
11995
      else
11996
         tree_add_stmt(b, p_sequential_statement());
4✔
11997
   }
11998

11999
   scan_as_psl();
4✔
12000

12001
   consume(t2RSQUARE);
4✔
12002

12003
   tree_set_loc(b, CURRENT_LOC);
4✔
12004
   psl_set_loc(p, CURRENT_LOC);
4✔
12005
   return p;
4✔
12006
}
12007

12008
static type_t p_psl_subtype_indication(void)
7✔
12009
{
12010
   BEGIN("PSL subtype indication");
14✔
12011

12012
   // Handle ambiguity in "p_subtype_indication". Two consecutive IDs
12013
   // may indicate resolution function, or only "type_mark" followed by
12014
   // actual parameter name.
12015
   token_t third = peek_nth(3);
7✔
12016
   if (peek() == tID && peek_nth(1) == tID &&
7✔
12017
       (third == tSEMI || third == tCOMMA || third == tRPAREN))
7✔
12018
      return p_type_mark();
6✔
12019
   else
12020
      return p_subtype_indication();
1✔
12021
}
12022

12023
static type_t p_psl_param_spec(psl_node_t node, psl_type_t *psl_type, class_t *class)
22✔
12024
{
12025
   //    const
12026
   //    | [const | mutable] Value_Parameter
12027
   //    | sequence
12028
   //    | property
12029
   //
12030
   //    Value_Parameter ::=
12031
   //       HDL_Type
12032
   //     | PSL_Type_Class
12033
   //
12034
   //    HDL_Type ::=
12035
   //       hdltype HDL_VARIABLE_TYPE
12036
   //
12037
   //    PSL_Type_Class ::=
12038
   //       boolean | bit | bitvector | numeric | string
12039

12040
   BEGIN("PSL Parameter specification");
44✔
12041

12042
   *class = C_SIGNAL;
22✔
12043

12044
   switch (peek()) {
22✔
12045
   case tPROPERTY:
1✔
12046
      consume(tPROPERTY);
1✔
12047
      *psl_type = PSL_TYPE_PROPERTY;
1✔
12048
      break;
1✔
12049

12050
   case tSEQUENCE:
1✔
12051
      consume(tSEQUENCE);
1✔
12052
      *psl_type = PSL_TYPE_SEQUENCE;
1✔
12053
      break;
1✔
12054

12055
   case tCONST:
10✔
12056
      *class = C_CONSTANT;
10✔
12057
      // Handle PSL 1.1 "const" only
12058
      if (peek_nth(2) == tID) {
10✔
12059
         consume(tCONST);
2✔
12060
         *psl_type = PSL_TYPE_NUMERIC;
2✔
12061
         return std_type(NULL, STD_INTEGER);
2✔
12062
      }
12063
   case tMUTABLE:
12064
      one_of(tCONST, tMUTABLE);
14✔
12065
   case tHDLTYPE:
18✔
12066
   case tBOOLEAN:
12067
   case tBIT:
12068
   case tBITVECTOR:
12069
   case tNUMERIC:
12070
   case tSTRINGK:
12071
      switch (one_of(tHDLTYPE, tBOOLEAN, tBIT, tBITVECTOR, tNUMERIC, tSTRINGK)) {
18✔
12072
      case tHDLTYPE:
7✔
12073
         *psl_type = PSL_TYPE_HDLTYPE;
7✔
12074
         scan_as_vhdl();
7✔
12075
         type_t t = p_psl_subtype_indication();
7✔
12076
         scan_as_psl();
7✔
12077
         return t;
7✔
12078
      case tBOOLEAN:
2✔
12079
         *psl_type = PSL_TYPE_BOOLEAN;
2✔
12080
         return std_type(NULL, STD_BOOLEAN);
2✔
12081
      case tBIT:
2✔
12082
         *psl_type = PSL_TYPE_BIT;
2✔
12083
         return std_type(NULL, STD_BIT);
2✔
12084
         break;
2✔
12085
      case tBITVECTOR:
2✔
12086
         *psl_type = PSL_TYPE_BITVECTOR;
2✔
12087
         return std_type(NULL, STD_BIT_VECTOR);
2✔
12088
         break;
3✔
12089
      case tNUMERIC:
3✔
12090
         *psl_type = PSL_TYPE_NUMERIC;
3✔
12091
         return std_type(NULL, STD_INTEGER);
3✔
12092
         break;
2✔
12093
      case tSTRINGK:
2✔
12094
         *psl_type = PSL_TYPE_STRING;
2✔
12095
         return std_type(NULL, STD_STRING);
2✔
12096
      }
12097
   }
12098

12099
  return type_new(T_NONE);
2✔
12100
}
12101

12102
static void p_psl_formal_parameter(psl_node_t node)
22✔
12103
{
12104
   // Param_Spec PSL_Identifier { , PSL_Identifier }
12105

12106
   BEGIN("PSL Formal parameter");
44✔
12107

12108
   psl_type_t psl_type = PSL_TYPE_NUMERIC;
22✔
12109
   class_t class;
22✔
12110
   type_t type = p_psl_param_spec(node, &psl_type, &class);
22✔
12111

12112
   do {
22✔
12113
      tree_t p = tree_new(T_PARAM_DECL);
22✔
12114
      tree_set_ident(p, p_identifier());
22✔
12115
      tree_set_loc(p, CURRENT_LOC);
22✔
12116
      tree_set_class(p, class);
22✔
12117
      tree_set_subkind(p, psl_type);
22✔
12118
      tree_set_type(p, type);
22✔
12119
      psl_add_port(node, p);
22✔
12120
      insert_name(nametab, p, NULL);
22✔
12121
   } while (optional(tCOMMA));
22✔
12122
}
22✔
12123

12124
static void p_psl_formal_parameter_list(psl_node_t node)
20✔
12125
{
12126
   // Formal_Parameter { ; Formal_Parameter }
12127

12128
   BEGIN("PSL Formal parameter list");
40✔
12129

12130
   p_psl_formal_parameter(node);
20✔
12131

12132
   while (optional(tSEMI))
22✔
12133
      p_psl_formal_parameter(node);
2✔
12134
}
20✔
12135

12136
static void p_psl_actual_parameter(psl_node_t node, bool seq)
7✔
12137
{
12138
   // Actual_Parameter ::=
12139
   //    AnyType | Sequence | Property
12140
   // sequence_Actual_Parameter ::=
12141
   //    AnyType | Sequence
12142

12143
   BEGIN("PSL Actual parameter");
14✔
12144

12145
   // "Sequence" includes "Any_Type" parsing (HDL or PSL expression)
12146
   // "Property" includes "Sequence" parsing
12147
   psl_add_operand(node, (seq) ? p_psl_sequence() : p_psl_property());
7✔
12148
}
7✔
12149

12150
static void p_psl_actual_parameter_list(psl_node_t node, bool seq)
6✔
12151
{
12152

12153
   // sequence_Actual_Parameter { , sequence_Actual_Parameter }
12154
   // Actual_Parameter { , Actual_Parameter }
12155

12156
   BEGIN("PSL Actual parameter list");
12✔
12157

12158
   p_psl_actual_parameter(node, seq);
6✔
12159

12160
   while (optional(tCOMMA))
7✔
12161
      p_psl_actual_parameter(node, seq);
1✔
12162
}
6✔
12163

12164
static psl_node_t p_psl_clocked_sere(psl_node_t head)
1✔
12165
{
12166
   // Braced_SERE @ Clock_Expression
12167

12168
   BEGIN("PSL Clocked SERE");
1✔
12169

12170
   consume(tAT);
1✔
12171

12172
   tree_t expr = p_expression();
1✔
12173
   solve_types(nametab, expr, std_type(NULL, STD_BOOLEAN));
1✔
12174

12175
   psl_node_t p = psl_new(P_CLOCKED);
1✔
12176
   psl_set_value(p, head);
1✔
12177
   psl_set_tree(p, expr);
1✔
12178

12179
   psl_set_loc(p, CURRENT_LOC);
1✔
12180
   return p;
1✔
12181
}
12182

12183
static psl_node_t p_psl_compound_sere(psl_node_t head)
36✔
12184
{
12185
   // Repeated_SERE | Braced_SERE | Clocked_SERE
12186
   //   | Compound_SERE '|' Compound_SERE
12187
   //   | Compound_SERE & Compound_SERE
12188
   //   | Compound_SERE && Compound_SERE
12189
   //   | Compound_SERE within Compound_SERE
12190
   //   | Parameterized_SERE
12191

12192
   BEGIN("PSL Compound SERE");
36✔
12193

12194
   if (head == NULL) {
36✔
12195
      switch (peek()) {
18✔
12196
      case tLBRACE:
16✔
12197
         head = p_psl_braced_sere();
16✔
12198
         break;
16✔
12199
      case tFOR:
1✔
12200
         head = p_psl_parametrized_sere();
1✔
12201
         break;
1✔
12202
      default:
1✔
12203
         head = p_psl_boolean(NULL);
1✔
12204
         break;
1✔
12205
      }
12206
   }
12207

12208
   for (;;) {
58✔
12209
      switch (peek()) {
58✔
12210
      case tBAR:
5✔
12211
         {
12212
            consume(tBAR);
5✔
12213

12214
            psl_node_t p = psl_new(P_SERE);
5✔
12215
            psl_add_operand(p, head);
5✔
12216
            psl_set_subkind(p, PSL_SERE_OR);
5✔
12217
            psl_add_operand(p, p_psl_compound_sere(NULL));
5✔
12218
            psl_set_loc(p, CURRENT_LOC);
5✔
12219

12220
            head = p;
5✔
12221
         }
12222
         break;
5✔
12223
      case tAMP:
2✔
12224
         {
12225
            consume(tAMP);
2✔
12226

12227
            psl_node_t p = psl_new(P_SERE);
2✔
12228
            psl_add_operand(p, head);
2✔
12229
            psl_set_subkind(p, PSL_SERE_NEQ_AND);
2✔
12230
            psl_add_operand(p, p_psl_compound_sere(NULL));
2✔
12231
            psl_set_loc(p, CURRENT_LOC);
2✔
12232

12233
            head = p;
2✔
12234
         }
12235
         break;
2✔
12236
      case tDBLAMP:
9✔
12237
         {
12238
            consume(tDBLAMP);
9✔
12239

12240
            psl_node_t p = psl_new(P_SERE);
9✔
12241
            psl_add_operand(p, head);
9✔
12242
            psl_set_subkind(p, PSL_SERE_EQU_AND);
9✔
12243
            psl_add_operand(p, p_psl_compound_sere(NULL));
9✔
12244
            psl_set_loc(p, CURRENT_LOC);
9✔
12245

12246
            head = p;
9✔
12247
         }
12248
         break;
9✔
12249
      case tWITHIN:
1✔
12250
         {
12251
            consume(tWITHIN);
1✔
12252

12253
            psl_node_t p = psl_new(P_SERE);
1✔
12254
            psl_add_operand(p, head);
1✔
12255
            psl_set_subkind(p, PSL_SERE_WITHIN);
1✔
12256
            psl_add_operand(p, p_psl_compound_sere(NULL));
1✔
12257
            psl_set_loc(p, CURRENT_LOC);
1✔
12258

12259
            head = p;
1✔
12260
         }
12261
         break;
1✔
12262
      case tPLUSRPT:
5✔
12263
      case tTIMESRPT:
12264
      case tGOTORPT:
12265
      case tEQRPT:
12266
      case t2LSQUARE:
12267
         head = p_psl_repeated_sere(head);
5✔
12268
         break;
5✔
12269
      case tAT:
×
12270
         head = p_psl_clocked_sere(head);
×
12271
         break;
×
12272
      default:
36✔
12273
         return head;
36✔
12274
      }
12275
   }
12276
}
12277

12278
static psl_node_t p_psl_sere(void)
414✔
12279
{
12280
   // Boolean | Boolean Proc_Block | Sequence | SERE ; SERE
12281
   //   | SERE : SERE | Compound_SERE
12282

12283
   BEGIN("PSL SERE");
414✔
12284

12285
   psl_node_t head;
414✔
12286
   switch (peek()) {
414✔
12287
   case tLBRACE:
373✔
12288
   case tPLUSRPT:
12289
   case tTIMESRPT:
12290
   case tGOTORPT:
12291
   case tEQRPT:
12292
   case tID:
12293
      head = p_psl_sequence();
373✔
12294
      break;
373✔
12295
   case tFOR:
1✔
12296
      head = p_psl_compound_sere(NULL);
1✔
12297
      break;
1✔
12298
   default:
40✔
12299
      head = p_psl_boolean(NULL);
40✔
12300
      break;
40✔
12301
   }
12302

12303
   for (;;) {
614✔
12304
      const token_t tok = peek();
614✔
12305

12306
      switch (tok) {
614✔
12307
      case tCOLON:
182✔
12308
      case tSEMI:
12309
         {
12310
            consume(tok);
182✔
12311

12312
            const psl_sere_kind_t kind =
182✔
12313
               tok == tCOLON ? PSL_SERE_FUSION : PSL_SERE_CONCAT;
182✔
12314

12315
            psl_node_t rhs = p_psl_sere();
182✔
12316

12317
            psl_node_t p = psl_new(P_SERE);
182✔
12318
            psl_set_subkind(p, kind);
182✔
12319
            psl_add_operand(p, head);
182✔
12320
            psl_set_loc(p, CURRENT_LOC);
182✔
12321

12322
            if (psl_kind(rhs) == P_SERE && psl_subkind(rhs) == kind) {
182✔
12323
               const int nops = psl_operands(rhs);
55✔
12324
               for (int i = 0; i < nops; i++)
168✔
12325
                  psl_add_operand(p, psl_operand(rhs, i));
113✔
12326
            }
12327
            else
12328
               psl_add_operand(p, rhs);
127✔
12329

12330
            head = p;
12331
         }
12332
         break;
12333
      case tBAR:
18✔
12334
      case tAMP:
12335
      case tDBLAMP:
12336
      case tWITHIN:
12337
      case tLSQUARE:
12338
      case tPLUSRPT:
12339
      case tTIMESRPT:
12340
      case tEQRPT:
12341
      case tGOTORPT:
12342
         head = p_psl_compound_sere(head);
18✔
12343
         continue;
18✔
12344
      default:
414✔
12345
         return head;
414✔
12346
      }
12347
   }
12348
}
12349

12350
static psl_node_t p_psl_braced_sere(void)
231✔
12351
{
12352
   // { [ [[ HDL DECL {HDL DECL} ]] ] SERE }
12353
   //   | { [free HDL Identifier {HDL Identifier} ] SERE }
12354

12355
   BEGIN("PSL Braced SERE");
231✔
12356

12357
   consume(tLBRACE);
231✔
12358

12359
   psl_node_t sere = p_psl_sere();
231✔
12360

12361
   consume(tRBRACE);
231✔
12362

12363
   psl_set_loc(sere, CURRENT_LOC);
231✔
12364
   return sere;
231✔
12365
}
12366

12367
static psl_node_t p_psl_repeated_sere(psl_node_t head)
70✔
12368
{
12369
   // Boolean [* [ Count ] ] | Sequence [* [ Count ] ] | [* [ Count ] ]
12370
   //   | Boolean [+] | Sequence [+] | [+] | Boolean [= Count ]
12371
   //   | Boolean [-> [ positive_Count ] ] | Boolean Proc_Block
12372
   //   | Sequence Proc_Block
12373

12374
   EXTEND("PSL Repeated SERE");
140✔
12375

12376
   assert(head != NULL);
70✔
12377

12378
   if (peek() == t2LSQUARE)
70✔
12379
      return p_psl_proc_block(head);
4✔
12380

12381
   psl_node_t p = psl_new(P_REPEAT);
66✔
12382
   psl_set_value(p, head);
66✔
12383

12384
   switch (one_of(tPLUSRPT, tTIMESRPT, tGOTORPT, tEQRPT, t2LSQUARE)) {
66✔
12385
   case tPLUSRPT:
9✔
12386
      psl_set_subkind(p, PSL_PLUS_REPEAT);
9✔
12387
      break;
9✔
12388

12389
   case tTIMESRPT:
32✔
12390
      psl_set_subkind(p, PSL_TIMES_REPEAT);
32✔
12391
      if (peek() != tRSQUARE)
32✔
12392
         psl_set_delay(p, p_psl_count());
23✔
12393
      consume(tRSQUARE);
32✔
12394
      break;
32✔
12395

12396
   case tGOTORPT:
9✔
12397
      psl_set_subkind(p, PSL_GOTO_REPEAT);
9✔
12398
      if (peek() != tRSQUARE)
9✔
12399
         psl_set_delay(p, p_psl_count());
9✔
12400
      consume(tRSQUARE);
9✔
12401
      break;
9✔
12402

12403
   case tEQRPT:
16✔
12404
      psl_set_subkind(p, PSL_EQUAL_REPEAT);
16✔
12405
      if (peek() != tRSQUARE)
16✔
12406
         psl_set_delay(p, p_psl_count());
16✔
12407
      consume(tRSQUARE);
16✔
12408
      break;
16✔
12409

12410
   default:
12411
      return head;
12412
   }
12413

12414
   psl_set_loc(p, CURRENT_LOC);
66✔
12415
   return p;
66✔
12416
}
12417

12418
static psl_node_t p_sequence_instance(tree_t decl)
7✔
12419
{
12420
   // Name [ ( Actual_Parameter_List ) ]
12421

12422
   EXTEND("PSL Sequence Instance");
7✔
12423

12424
   psl_node_t s_decl = tree_psl(decl);
7✔
12425

12426
   if (psl_kind(s_decl) != P_SEQUENCE_DECL)
7✔
12427
      parse_error(CURRENT_LOC, "%s is not a PSL sequence",
1✔
12428
                  istr(tree_ident(decl)));
12429

12430
   psl_node_t p = psl_new(P_SEQUENCE_INST);
7✔
12431
   psl_set_ref(p, s_decl);
7✔
12432

12433
   if (optional(tLPAREN)) {
7✔
12434
      p_psl_actual_parameter_list(p, true);
6✔
12435
      consume(tRPAREN);
6✔
12436
   }
12437

12438
   psl_set_loc(p, CURRENT_LOC);
7✔
12439
   return p;
7✔
12440
}
12441

12442
static psl_node_t p_psl_sequence(void)
593✔
12443
{
12444
   // Sequence_Instance | Repeated_SERE | Braced_SERE | Clocked_SERE
12445

12446
   BEGIN("PSL Sequence");
593✔
12447

12448
   psl_node_t head = NULL;
593✔
12449
   switch (peek()) {
593✔
12450
   case tLBRACE:
215✔
12451
      head = p_psl_braced_sere();
215✔
12452
      break;
215✔
12453
   case tID:
363✔
12454
      {
12455
         tree_t name = p_name(N_PSL);
363✔
12456

12457
         // Check for sequence instance
12458
         if (tree_kind(name) == T_REF && tree_has_ref(name)) {
363✔
12459
            tree_t decl = tree_ref(name);
354✔
12460
            if (tree_kind(decl) == T_PSL_DECL)
354✔
12461
               head = p_sequence_instance(decl);
7✔
12462
         }
12463

12464
         if (head == NULL)
7✔
12465
            head = p_psl_boolean(name);
356✔
12466
      }
12467
      break;
12468
   case tTIMESRPT:
10✔
12469
   case tPLUSRPT:
12470
      {
12471
         // Empty SERE is equivalent to TRUE
12472
         type_t std_bool = std_type(NULL, STD_BOOLEAN);
10✔
12473
         tree_t true_ref = make_ref(type_enum_literal(std_bool, 1));
10✔
12474

12475
         head = psl_new(P_HDL_EXPR);
10✔
12476
         psl_set_tree(head, true_ref);
10✔
12477
         psl_set_type(head, PSL_TYPE_BOOLEAN);
10✔
12478
      }
12479
      break;
10✔
12480
   default:
5✔
12481
      head = p_psl_boolean(NULL);
5✔
12482
      break;
5✔
12483
   }
12484

12485
   for (;;) {
659✔
12486
      switch (peek()) {
659✔
12487
      case tPLUSRPT:
65✔
12488
      case tTIMESRPT:
12489
      case tGOTORPT:
12490
      case tEQRPT:
12491
      case t2LSQUARE:
12492
         head = p_psl_repeated_sere(head);
65✔
12493
         break;
65✔
12494
      case tAT:
1✔
12495
         head = p_psl_clocked_sere(head);
1✔
12496
         break;
1✔
12497
      default:
593✔
12498
         return head;
593✔
12499
      }
12500
   }
12501
}
12502

12503
static psl_node_t p_psl_fl_property(void)
1,084✔
12504
{
12505
   // Boolean | ( FL_Property ) | FL_Property @ Clock_Expression
12506
   //   | always FL_Property
12507
   //   | never FL_Property
12508
   //   | eventually! FL_Property
12509
   //   | next FL_Property | next [ Number ] FL_Property
12510
   //   | next! FL_Property | next! [ Number ] FL_Property
12511
   //   | next_event ( Boolean ) ( FL_Property )
12512
   //   | next_event ( Boolean ) ( FL_Property )
12513
   //   | next_event! ( Boolean ) [ Number ] ( FL_Property )
12514
   //   | next_event! ( Boolean ) [ Number ] ( FL_Property )
12515
   //   | FL_Property -> FL_Property
12516
   //   | FL_Property <-> FL_Property
12517
   //   | FL_Property or FL_Property
12518
   //   | FL_Property and FL_Property
12519
   //   | FL_Property until! FL_Property
12520
   //   | FL_Property until!_ FL_Property
12521
   //   | FL_Property until FL_Property
12522
   //   | FL_Property until_ FL_Property
12523
   //   | FL_Property sync_abort Boolean
12524
   //   | FL_Property async_abort Boolean
12525
   //   | FL_Property abort Boolean
12526
   //   | Sequence [ ! ]
12527
   //   | Sequence |-> FL_Property
12528
   //   | Sequence |=> FL_Property
12529

12530
   BEGIN("FL property");
2,168✔
12531

12532
   psl_node_t p = NULL;
1,084✔
12533
   const token_t tok = peek();
1,084✔
12534
   switch (tok) {
1,084✔
12535
   case tALWAYS:
149✔
12536
      {
12537
         consume(tALWAYS);
149✔
12538

12539
         p = psl_new(P_ALWAYS);
149✔
12540
         psl_set_value(p, p_psl_fl_property());
149✔
12541
         psl_set_loc(p, CURRENT_LOC);
149✔
12542
      }
12543
      break;
149✔
12544

12545
   case tNEVER:
13✔
12546
      {
12547
         consume(tNEVER);
13✔
12548

12549
         p = psl_new(P_NEVER);
13✔
12550
         psl_set_value(p, p_psl_fl_property());
13✔
12551
         psl_set_loc(p, CURRENT_LOC);
13✔
12552
      }
12553
      break;
13✔
12554

12555
   case tEVENTUALLY:
8✔
12556
      {
12557
         consume(tEVENTUALLY);
8✔
12558

12559
         p = psl_new(P_EVENTUALLY);
8✔
12560
         psl_set_value(p, p_psl_fl_property());
8✔
12561
         psl_set_loc(p, CURRENT_LOC);
8✔
12562
      }
12563
      break;
8✔
12564

12565
   case tPSLNEXT:
84✔
12566
   case tNEXT1:
12567
      {
12568
         consume(tok);
84✔
12569

12570
         p = psl_new(P_NEXT);
84✔
12571

12572
         if (tok == tNEXT1)
84✔
12573
            psl_set_flag(p, PSL_F_STRONG);
1✔
12574

12575
         if (optional(tLSQUARE)) {
84✔
12576
            psl_node_t count = p_hdl_expression(NULL, PSL_TYPE_NUMERIC);
28✔
12577
            psl_set_delay(p, count);
28✔
12578

12579
            consume(tRSQUARE);
28✔
12580
         }
12581

12582
         psl_set_value(p, p_psl_fl_property());
84✔
12583
         psl_set_loc(p, CURRENT_LOC);
84✔
12584
      }
12585
      break;
84✔
12586

12587
   case tNEXTA:
2✔
12588
   case tNEXTA1:
12589
      {
12590
         consume(tok);
2✔
12591

12592
         p = psl_new(P_NEXT_A);
2✔
12593

12594
         if (tok == tNEXTA1)
2✔
12595
            psl_set_flag(p, PSL_F_STRONG);
×
12596

12597
         consume(tLSQUARE);
2✔
12598

12599
         psl_set_delay(p, p_psl_range(NULL));
2✔
12600

12601
         consume(tRSQUARE);
2✔
12602

12603
         psl_set_value(p, p_psl_fl_property());
2✔
12604
         psl_set_loc(p, CURRENT_LOC);
2✔
12605
      }
12606
      break;
2✔
12607

12608
   case tNEXTE:
11✔
12609
   case tNEXTE1:
12610
      {
12611
         consume(tok);
11✔
12612

12613
         p = psl_new(P_NEXT_E);
11✔
12614

12615
         if (tok == tNEXTE1)
11✔
12616
            psl_set_flag(p, PSL_F_STRONG);
3✔
12617

12618
         consume(tLSQUARE);
11✔
12619

12620
         psl_set_delay(p, p_psl_range(NULL));
11✔
12621

12622
         consume(tRSQUARE);
11✔
12623

12624
         psl_set_value(p, p_psl_fl_property());
11✔
12625
         psl_set_loc(p, CURRENT_LOC);
11✔
12626
      }
12627
      break;
11✔
12628

12629
   case tNEXTEVENT:
1✔
12630
   case tNEXTEVENT1:
12631
      {
12632
         consume(tok);
1✔
12633

12634
         p = psl_new(P_NEXT_EVENT);
1✔
12635

12636
         if (tok == tNEXTEVENT1)
1✔
12637
            psl_set_flag(p, PSL_F_STRONG);
×
12638

12639
         consume(tLPAREN);
1✔
12640
         (void)p_psl_fl_property();
1✔
12641
         consume(tRPAREN);
1✔
12642

12643
         if (optional(tLSQUARE)) {
1✔
12644
            psl_node_t count = p_hdl_expression(NULL, PSL_TYPE_NUMERIC);
1✔
12645
            psl_set_delay(p, count);
1✔
12646

12647
            consume(tRSQUARE);
1✔
12648
         }
12649

12650
         consume(tLPAREN);
1✔
12651
         psl_set_value(p, p_psl_fl_property());
1✔
12652
         consume(tRPAREN);
1✔
12653

12654
         psl_set_loc(p, CURRENT_LOC);
1✔
12655
      }
12656
      break;
1✔
12657

12658
   case tLPAREN:
233✔
12659
      {
12660
         consume(tLPAREN);
233✔
12661
         p = p_psl_fl_property();
233✔
12662
         consume(tRPAREN);
233✔
12663

12664
         if (psl_kind(p) == P_HDL_EXPR && is_vhdl_infix_op(peek())) {
233✔
12665
            // Handle awkward cases like "always x -> (x or y) = '1'"
12666
            // where we cannot determine ahead-of-time whether (x or y)
12667
            // is a VHDL expression or PSL property
12668
            tree_t expr = p_expression_with_head(psl_tree(p));
14✔
12669
            psl_set_tree(p, expr);
14✔
12670
         }
12671

12672
         psl_set_loc(p, CURRENT_LOC);
233✔
12673
      }
12674
      break;
233✔
12675

12676
   case tLBRACE:
61✔
12677
   case tPLUSRPT:
12678
   case tTIMESRPT:
12679
   case tGOTORPT:
12680
   case tEQRPT:
12681
      p = p_psl_sequence();
61✔
12682
      break;
61✔
12683

12684
   default:
522✔
12685
      p = p_hdl_expression(NULL, PSL_TYPE_BOOLEAN);
522✔
12686
      break;
522✔
12687
   }
12688

12689
   const token_t infix = peek();
1,084✔
12690
   switch (infix) {
1,084✔
12691
   case tIFIMPL:
165✔
12692
   case tIFFIMPL:
12693
   case tAND:
12694
   case tOR:
12695
      {
12696
         consume(infix);
165✔
12697

12698
         psl_logic_t kind;
165✔
12699
         switch (infix) {
165✔
12700
         case tAND:     kind = PSL_LOGIC_AND; break;
12701
         case tOR:      kind = PSL_LOGIC_OR; break;
30✔
12702
         case tIFIMPL:  kind = PSL_LOGIC_IF; break;
120✔
12703
         case tIFFIMPL: kind = PSL_LOGIC_IFF; break;
6✔
12704
         default: should_not_reach_here();
12705
         }
12706

12707
         psl_node_t right = p_psl_fl_property();
165✔
12708

12709
         // A logical operation where the two operands are HDL
12710
         // expressions should be parsed as a single HDL expression
12711
         const bool prefer_hdl =
330✔
12712
            (kind == PSL_LOGIC_OR || kind == PSL_LOGIC_AND)
165✔
12713
            && psl_kind(p) == P_HDL_EXPR && psl_kind(right) == P_HDL_EXPR;
165✔
12714

12715
         if (prefer_hdl) {
165✔
12716
            ident_t op = well_known(kind == PSL_LOGIC_OR ? W_OP_OR : W_OP_AND);
38✔
12717

12718
            tree_t fcall = tree_new(T_FCALL);
29✔
12719
            tree_set_ident(fcall, op);
29✔
12720
            tree_set_loc(fcall, CURRENT_LOC);
29✔
12721
            add_param(fcall, psl_tree(p), P_POS, NULL);
29✔
12722
            add_param(fcall, psl_tree(right), P_POS, NULL);
29✔
12723

12724
            psl_set_tree(p, fcall);
29✔
12725
            psl_set_loc(p, CURRENT_LOC);
29✔
12726
            return p;
29✔
12727
         }
12728
         else {
12729
            psl_node_t log = psl_new(P_LOGICAL);
136✔
12730
            psl_set_subkind(log, kind);
136✔
12731
            psl_add_operand(log, p);
136✔
12732
            psl_add_operand(log, right);
136✔
12733
            psl_set_loc(log, CURRENT_LOC);
136✔
12734

12735
            return log;
136✔
12736
         }
12737
      }
12738

12739
   case tSUFFIXOVR:
11✔
12740
   case tSUFFIXNON:
12741
      {
12742
         consume(infix);
11✔
12743

12744
         const psl_suffix_impl_t kind =
11✔
12745
            infix == tSUFFIXOVR ? PSL_SUFFIX_OVERLAP : PSL_SUFFIX_NON;
11✔
12746

12747
         psl_node_t suff = psl_new(P_SUFFIX_IMPL);
11✔
12748
         psl_set_subkind(suff, kind);
11✔
12749
         psl_add_operand(suff, p);
11✔
12750
         psl_add_operand(suff, p_psl_fl_property());
11✔
12751
         psl_set_loc(suff, CURRENT_LOC);
11✔
12752

12753
         return suff;
11✔
12754
      }
12755

12756
   case tUNTIL:
26✔
12757
   case tUNTIL_:
12758
   case tUNTIL1:
12759
   case tUNTIL1_:
12760
      {
12761
         consume(infix);
26✔
12762

12763
         psl_flags_t flags = 0;
26✔
12764
         if (infix == tUNTIL1 || infix == tUNTIL1_)
26✔
12765
            flags |= PSL_F_STRONG;
3✔
12766
         if (infix == tUNTIL_ || infix == tUNTIL1_)
26✔
12767
            flags |= PSL_F_INCLUSIVE;
13✔
12768

12769
         psl_node_t until = psl_new(P_UNTIL);
26✔
12770
         psl_set_flag(until, flags);
26✔
12771
         psl_add_operand(until, p);
26✔
12772
         psl_add_operand(until, p_psl_fl_property());
26✔
12773
         psl_set_loc(until, CURRENT_LOC);
26✔
12774

12775
         return until;
26✔
12776
      }
12777

12778
   case tBEFORE:
9✔
12779
   case tBEFORE_:
12780
   case tBEFORE1:
12781
   case tBEFORE1_:
12782
      {
12783
         consume(infix);
9✔
12784

12785
         psl_flags_t flags = 0;
9✔
12786
         if (infix == tBEFORE1 || infix == tBEFORE1_)
9✔
12787
            flags |= PSL_F_STRONG;
1✔
12788
         if (infix == tBEFORE_ || infix == tBEFORE1_)
9✔
12789
            flags |= PSL_F_INCLUSIVE;
3✔
12790

12791
         psl_node_t until = psl_new(P_BEFORE);
9✔
12792
         psl_set_flag(until, flags);
9✔
12793
         psl_add_operand(until, p);
9✔
12794
         psl_add_operand(until, p_psl_fl_property());
9✔
12795
         psl_set_loc(until, CURRENT_LOC);
9✔
12796

12797
         return until;
9✔
12798
      }
12799

12800
   case tABORT:
9✔
12801
   case tASYNC_ABORT:
12802
   case tSYNC_ABORT:
12803
      {
12804
         consume(infix);
9✔
12805

12806
         const psl_abort_t kind =
9✔
12807
            infix == tSYNC_ABORT ? PSL_ABORT_SYNC : PSL_ABORT_ASYNC;
9✔
12808

12809
         psl_node_t abort = psl_new(P_ABORT);
9✔
12810
         psl_set_subkind(abort, kind);
9✔
12811
         psl_add_operand(abort, p);
9✔
12812
         psl_add_operand(abort, p_hdl_expression(NULL, PSL_TYPE_BOOLEAN));
9✔
12813
         psl_set_loc(abort, CURRENT_LOC);
9✔
12814

12815
         return abort;
9✔
12816
      }
12817

12818
   case tAT:
4✔
12819
      {
12820
         consume(tAT);
4✔
12821

12822
         tree_t expr = p_expression();
4✔
12823
         solve_types(nametab, expr, std_type(NULL, STD_BOOLEAN));
4✔
12824

12825
         psl_node_t clk = psl_new(P_CLOCKED);
4✔
12826
         psl_set_value(clk, p);
4✔
12827
         psl_set_tree(clk, expr);
4✔
12828

12829
         psl_set_loc(clk, CURRENT_LOC);
4✔
12830
         return clk;
4✔
12831
      }
12832

12833
   default:
12834
      return p;
12835
   }
12836
}
12837

12838
static void p_psl_report(psl_node_t p)
106✔
12839
{
12840
   consume(tREPORT);
106✔
12841

12842
   tree_t m = p_expression();
106✔
12843
   psl_set_message(p, m);
106✔
12844
   solve_types(nametab, m, std_type(NULL, STD_STRING));
106✔
12845
}
106✔
12846

12847
static psl_node_t p_psl_property(void)
371✔
12848
{
12849
   // Replicator Property | FL_Property | OBE_Property
12850

12851
   BEGIN("property");
742✔
12852

12853
   return p_psl_fl_property();
371✔
12854
}
12855

12856
static psl_node_t p_psl_assert_directive(void)
183✔
12857
{
12858
   // assert Property [ report String ] ;
12859

12860
   BEGIN("assert directive");
183✔
12861

12862
   consume(tASSERT);
183✔
12863

12864
   psl_node_t p = with_default_clock(p_psl_property());
183✔
12865

12866
   psl_node_t a = psl_new(P_ASSERT);
183✔
12867
   psl_set_value(a, p);
183✔
12868

12869
   if (peek() == tREPORT)
183✔
12870
      p_psl_report(a);
15✔
12871

12872
   consume(tSEMI);
183✔
12873

12874
   psl_set_loc(a, CURRENT_LOC);
183✔
12875
   return a;
183✔
12876
}
12877

12878
static psl_node_t p_psl_assume_directive(void)
4✔
12879
{
12880
   // assume Property ;
12881
   // assume_guarantee Property [ report String ] ;
12882

12883
   BEGIN("assume directive");
4✔
12884

12885
   token_t tok = one_of(tASSUME, tASSUMEG);
4✔
12886

12887
   psl_node_t a = psl_new(P_ASSUME);
4✔
12888
   if (tok == tASSUME)
4✔
12889
      psl_set_subkind(a, PSL_NO_GUARANTEE);
3✔
12890
   else
12891
      psl_set_subkind(a, PSL_GUARANTEE);
1✔
12892

12893
   psl_node_t p = with_default_clock(p_psl_property());
4✔
12894
   psl_set_value(a, p);
4✔
12895

12896
   if (peek() == tREPORT && tok == tASSUMEG)
4✔
12897
      p_psl_report(a);
1✔
12898

12899
   consume(tSEMI);
4✔
12900

12901
   psl_set_loc(a, CURRENT_LOC);
4✔
12902
   return a;
4✔
12903
}
12904

12905
static psl_node_t p_psl_restrict_directive(void)
4✔
12906
{
12907
   // restrict Sequence  ;
12908
   // restrict_guarantee Sequence [ report String ] ;
12909

12910
   BEGIN("restrict directive");
4✔
12911

12912
   token_t tok = peek();
4✔
12913
   assert(tok == tRESTRICT || tok == tRESTRICTG);
4✔
12914
   consume(tok);
4✔
12915

12916
   psl_node_t a = psl_new(P_RESTRICT);
4✔
12917
   if (tok == tRESTRICT)
4✔
12918
      psl_set_subkind(a, PSL_NO_GUARANTEE);
3✔
12919
   else
12920
      psl_set_subkind(a, PSL_GUARANTEE);
1✔
12921

12922
   psl_node_t p = with_default_clock(p_psl_sequence());
4✔
12923
   psl_set_value(a, p);
4✔
12924

12925
   if (peek() == tREPORT && tok == tRESTRICTG)
4✔
12926
      p_psl_report(a);
1✔
12927

12928
   consume(tSEMI);
4✔
12929

12930
   psl_set_loc(a, CURRENT_LOC);
4✔
12931
   return a;
4✔
12932
}
12933

12934
static psl_node_t p_psl_fairness(void)
3✔
12935
{
12936
   // fairness Boolean ;
12937
   // strong fairness Boolean , Boolean ;
12938

12939
   BEGIN("fairness statement");
3✔
12940

12941
   psl_flags_t flags = 0;
3✔
12942
   if (optional(tSTRONG))
3✔
12943
      flags |= PSL_F_STRONG;
1✔
12944

12945
   consume(tFAIRNESS);
3✔
12946

12947
   psl_node_t a = psl_new(P_FAIRNESS);
3✔
12948
   psl_set_flag(a, flags);
3✔
12949

12950
   tree_t e1 = p_expression();
3✔
12951
   solve_psl_condition(nametab, &e1);
3✔
12952

12953
   psl_node_t p1 = psl_new(P_HDL_EXPR);
3✔
12954
   psl_set_tree(p1, e1);
3✔
12955

12956
   psl_add_operand(a, p1);
3✔
12957

12958
   if (flags & PSL_F_STRONG) {
3✔
12959
      consume(tCOMMA);
1✔
12960

12961
      tree_t e2 = p_expression();
1✔
12962
      solve_psl_condition(nametab, &e2);
1✔
12963

12964
      psl_node_t p2 = psl_new(P_HDL_EXPR);
1✔
12965
      psl_set_tree(p2, e2);
1✔
12966

12967
      psl_add_operand(a, p2);
1✔
12968
   }
12969

12970
   consume(tSEMI);
3✔
12971

12972
   psl_set_loc(a, CURRENT_LOC);
3✔
12973
   return a;
3✔
12974
}
12975

12976
static psl_node_t p_psl_cover_directive(void)
141✔
12977
{
12978
   // cover Sequence [ report String ] ;
12979

12980
   BEGIN("cover directive");
141✔
12981

12982
   consume(tCOVER);
141✔
12983

12984
   psl_node_t p = with_default_clock(p_psl_sequence());
141✔
12985

12986
   psl_node_t a = psl_new(P_COVER);
141✔
12987
   psl_set_value(a, p);
141✔
12988

12989
   if (peek() == tREPORT)
141✔
12990
      p_psl_report(a);
89✔
12991

12992
   consume(tSEMI);
141✔
12993

12994
   psl_set_loc(a, CURRENT_LOC);
141✔
12995
   return a;
141✔
12996
}
12997

12998
static psl_node_t p_psl_verification_directive(void)
336✔
12999
{
13000
   // Assert_Directive | Assume_Directive | Restrict_Directive
13001
   //   | Restrict!_Directive | Cover_Directive | Fairness_Statement
13002

13003
   BEGIN("verification directive");
672✔
13004

13005
   switch (peek()) {
336✔
13006
   case tASSERT:
183✔
13007
      return p_psl_assert_directive();
183✔
13008
   case tASSUME:
4✔
13009
   case tASSUMEG:
13010
      return p_psl_assume_directive();
4✔
13011
   case tRESTRICT:
4✔
13012
   case tRESTRICTG:
13013
      return p_psl_restrict_directive();
4✔
13014
   case tFAIRNESS:
3✔
13015
   case tSTRONG:
13016
      return p_psl_fairness();
3✔
13017
   case tCOVER:
141✔
13018
      return p_psl_cover_directive();
141✔
13019
   default:
1✔
13020
      one_of(tASSERT, tASSUME, tASSUMEG, tRESTRICT, tRESTRICTG, tFAIRNESS,
1✔
13021
             tSTRONG, tCOVER);
13022
      return NULL;
1✔
13023
   }
13024
}
13025

13026
static tree_t p_psl_directive(ident_t label)
336✔
13027
{
13028
   // [ Label : ] Verification_Directive
13029

13030
   BEGIN("PSL directive");
336✔
13031

13032
   tree_t t = tree_new(T_PSL_DIRECT);
336✔
13033

13034
   scan_as_psl();
336✔
13035

13036
   // Verification directive can contain Proc_Block with
13037
   // local declarations -> Push scope
13038
   push_scope(nametab);
336✔
13039
   insert_names_for_psl(nametab);
336✔
13040

13041
   psl_node_t p = p_psl_verification_directive();
336✔
13042

13043
   scan_as_vhdl();
336✔
13044

13045
   if (p != NULL) {
336✔
13046
      tree_set_psl(t, p);
335✔
13047
      psl_check(p, nametab);
335✔
13048
   }
13049

13050
   pop_scope(nametab);
336✔
13051

13052
   tree_set_loc(t, CURRENT_LOC);
336✔
13053
   ensure_labelled(t, label);
336✔
13054

13055
   if (label) insert_name(nametab, t, NULL);
336✔
13056

13057
   return t;
336✔
13058
}
13059

13060
static tree_t p_psl_property_declaration(void)
21✔
13061
{
13062
   // property PSL_Identifier [ ( Formal_Parameter_List ) ] is Property ;
13063

13064
   BEGIN("PSL property declaration");
21✔
13065

13066
   scan_as_psl();
21✔
13067

13068
   psl_node_t decl = psl_new(P_PROPERTY_DECL);
21✔
13069

13070
   consume(tPROPERTY);
21✔
13071

13072
   ident_t ident = p_identifier();
21✔
13073
   psl_set_ident(decl, ident);
21✔
13074

13075
   tree_t t = tree_new(T_PSL_DECL);
21✔
13076
   tree_set_psl(t, decl);
21✔
13077
   tree_set_ident(t, ident);
21✔
13078
   insert_name(nametab, t, NULL);
21✔
13079

13080
   push_scope(nametab);
21✔
13081
   scope_set_container(nametab, t);
21✔
13082
   insert_names_for_psl(nametab);
21✔
13083

13084
   if (optional(tLPAREN)) {
21✔
13085
      p_psl_formal_parameter_list(decl);
19✔
13086
      consume(tRPAREN);
19✔
13087
   }
13088

13089
   consume(tIS);
21✔
13090

13091
   psl_node_t prop = p_psl_property();
21✔
13092
   psl_set_value(decl, prop);
21✔
13093

13094
   consume(tSEMI);
21✔
13095

13096
   psl_set_loc(decl, CURRENT_LOC);
21✔
13097
   psl_check(decl, nametab);
21✔
13098

13099
   pop_scope(nametab);
21✔
13100

13101
   scan_as_vhdl();
21✔
13102

13103
   tree_set_loc(t, CURRENT_LOC);
21✔
13104
   return t;
21✔
13105
}
13106

13107
static tree_t p_psl_sequence_declaration(void)
5✔
13108
{
13109
   // sequence PSL_Identifier [ ( Formal_Parameter_List ) ] is Sequence ;
13110

13111
   BEGIN("PSL sequence declaration");
5✔
13112

13113
   scan_as_psl();
5✔
13114

13115
   psl_node_t decl = psl_new(P_SEQUENCE_DECL);
5✔
13116

13117
   consume(tSEQUENCE);
5✔
13118

13119
   ident_t ident = p_identifier();
5✔
13120
   psl_set_ident(decl, ident);
5✔
13121

13122
   tree_t t = tree_new(T_PSL_DECL);
5✔
13123
   tree_set_psl(t, decl);
5✔
13124
   tree_set_ident(t, ident);
5✔
13125
   insert_name(nametab, t, NULL);
5✔
13126

13127
   push_scope(nametab);
5✔
13128
   scope_set_container(nametab, t);
5✔
13129
   insert_names_for_psl(nametab);
5✔
13130

13131
   if (optional(tLPAREN)) {
5✔
13132
      p_psl_formal_parameter_list(decl);
1✔
13133
      consume(tRPAREN);
1✔
13134
   }
13135

13136
   consume(tIS);
5✔
13137

13138
   psl_node_t prop = p_psl_sequence();
5✔
13139
   psl_set_value(decl, prop);
5✔
13140

13141
   consume(tSEMI);
5✔
13142

13143
   psl_set_loc(decl, CURRENT_LOC);
5✔
13144
   psl_check(decl, nametab);
5✔
13145

13146
   pop_scope(nametab);
5✔
13147

13148
   scan_as_vhdl();
5✔
13149

13150
   tree_set_loc(t, CURRENT_LOC);
5✔
13151
   return t;
5✔
13152
}
13153

13154
static tree_t p_psl_endpoint_declaration(void)
1✔
13155
{
13156
   // sequence PSL_Identifier is Sequence ;
13157

13158
   BEGIN("PSL endpoint declaration");
1✔
13159

13160
   scan_as_psl();
1✔
13161

13162
   psl_node_t decl = psl_new(P_ENDPOINT_DECL);
1✔
13163

13164
   consume(tENDPOINT);
1✔
13165

13166
   ident_t ident = p_identifier();
1✔
13167
   psl_set_ident(decl, ident);
1✔
13168

13169
   tree_t t = tree_new(T_PSL_DECL);
1✔
13170
   tree_set_psl(t, decl);
1✔
13171
   tree_set_ident(t, ident);
1✔
13172
   insert_name(nametab, t, NULL);
1✔
13173

13174
   push_scope(nametab);
1✔
13175
   insert_names_for_psl(nametab);
1✔
13176

13177
   consume(tIS);
1✔
13178

13179
   psl_node_t prop = p_psl_sequence();
1✔
13180
   psl_set_value(decl, prop);
1✔
13181

13182
   consume(tSEMI);
1✔
13183

13184
   psl_set_loc(decl, CURRENT_LOC);
1✔
13185
   psl_check(decl, nametab);
1✔
13186

13187
   pop_scope(nametab);
1✔
13188

13189
   scan_as_vhdl();
1✔
13190

13191
   tree_set_loc(t, CURRENT_LOC);
1✔
13192
   return t;
1✔
13193
}
13194

13195
static void p_psl_declaration(tree_t parent)
123✔
13196
{
13197
   // Property_Declaration | Sequence_Declaration | Clock_Declaration
13198

13199
   BEGIN("PSL declaration");
246✔
13200

13201
   switch (peek()) {
123✔
13202
   case tPROPERTY:
21✔
13203
      tree_add_decl(parent, p_psl_property_declaration());
21✔
13204
      break;
21✔
13205
   case tSEQUENCE:
5✔
13206
      tree_add_decl(parent, p_psl_sequence_declaration());
5✔
13207
      break;
5✔
13208
   case tDEFAULT:
96✔
13209
      tree_add_decl(parent, p_psl_clock_declaration());
96✔
13210
      break;
96✔
13211
   case tENDPOINT:
1✔
13212
      tree_add_decl(parent, p_psl_endpoint_declaration());
1✔
13213
      break;
1✔
13214
   default:
×
13215
      one_of(tPROPERTY, tSEQUENCE, tDEFAULT, tENDPOINT);
×
13216
      break;
×
13217
   }
13218
}
123✔
13219

13220
static tree_t p_psl_or_concurrent_assert(ident_t label)
163✔
13221
{
13222
   // Handle the ambiguity between a PSL assertion and a normal VHDL
13223
   // concurrent assertion statement.
13224
   //
13225
   //  assert condition [ report expression ] [ severity expression ] ;
13226
   //   | assert Property [ report String ] ;
13227

13228
   BEGIN("PSL or concurrent assertion statement");
326✔
13229

13230
   if (peek() == tPOSTPONED)
163✔
13231
      return p_concurrent_assertion_statement(label);   // Cannot be PSL
×
13232

13233
   consume(tASSERT);
163✔
13234

13235
   push_scope(nametab);
163✔
13236
   insert_names_for_psl(nametab);
163✔
13237

13238
   scan_as_psl();
163✔
13239

13240
   psl_node_t p = p_psl_property();
163✔
13241

13242
   scan_as_vhdl();
163✔
13243

13244
   tree_t conc;
163✔
13245
   if (psl_kind(p) == P_HDL_EXPR) {
163✔
13246
      tree_t value = psl_tree(p);
118✔
13247
      solve_condition(nametab, &value);
118✔
13248

13249
      tree_t s = tree_new(T_ASSERT);
118✔
13250
      tree_set_value(s, value);
118✔
13251

13252
      if (optional(tREPORT)) {
118✔
13253
         tree_t message = p_expression();
24✔
13254
         solve_types(nametab, message, std_type(NULL, STD_STRING));
24✔
13255
         tree_set_message(s, message);
24✔
13256
      }
13257

13258
      if (optional(tSEVERITY)) {
118✔
13259
         tree_t severity = p_expression();
24✔
13260
         solve_types(nametab, severity, std_type(NULL, STD_SEVERITY_LEVEL));
24✔
13261
         tree_set_severity(s, severity);
24✔
13262
      }
13263

13264
      consume(tSEMI);
118✔
13265

13266
      tree_set_loc(s, CURRENT_LOC);
118✔
13267

13268
      conc = tree_new(T_CONCURRENT);
118✔
13269
      tree_add_stmt(conc, s);
118✔
13270
   }
13271
   else {
13272
      psl_node_t a = psl_new(P_ASSERT);
45✔
13273
      psl_set_value(a, with_default_clock(p));
45✔
13274

13275
      if (peek() == tREPORT)
45✔
13276
         p_psl_report(a);
×
13277

13278
      consume(tSEMI);
45✔
13279

13280
      psl_set_loc(a, CURRENT_LOC);
45✔
13281
      psl_check(a, nametab);
45✔
13282

13283
      conc = tree_new(T_PSL_DIRECT);
45✔
13284
      tree_set_psl(conc, a);
45✔
13285
   }
13286

13287
   pop_scope(nametab);
163✔
13288

13289
   tree_set_loc(conc, CURRENT_LOC);
163✔
13290
   ensure_labelled(conc, label);
163✔
13291

13292
   if (label) insert_name(nametab, conc, NULL);
163✔
13293
   sem_check(conc, nametab);
163✔
13294
   return conc;
163✔
13295
}
13296

13297
static tree_t p_concurrent_statement(void)
10,846✔
13298
{
13299
   // block_statement | process_statement | concurrent_procedure_call_statement
13300
   //   | concurrent_assertion_statement
13301
   //   | concurrent_signal_assignment_statement
13302
   //   | component_instantiation_statement | generate_statement
13303
   //   | 2008: psl_directive
13304

13305
   BEGIN("concurrent statement");
21,689✔
13306

13307
   ident_t label = NULL;
10,846✔
13308
   if ((peek() == tID) && (peek_nth(2) == tCOLON)) {
10,846✔
13309
      label = p_identifier();
5,949✔
13310
      consume(tCOLON);
5,949✔
13311
   }
13312

13313
   switch (peek()) {
10,846✔
13314
   case tPROCESS:
4,977✔
13315
      return p_process_statement(label);
4,977✔
13316

13317
   case tCOMPONENT:
1,338✔
13318
   case tENTITY:
13319
   case tCONFIGURATION:
13320
      return p_component_instantiation_statement(label, NULL);
1,338✔
13321

13322
   case tWITH:
47✔
13323
      return p_concurrent_signal_assignment_statement(label, NULL);
47✔
13324

13325
   case tASSERT:
1,075✔
13326
      if (standard() >= STD_08)
1,075✔
13327
         return p_psl_or_concurrent_assert(label);
163✔
13328
      else
13329
         return p_concurrent_assertion_statement(label);
912✔
13330

13331
   case tCOVER:
2✔
13332
      return p_psl_directive(label);
2✔
13333

13334
   case tBLOCK:
426✔
13335
      return p_block_statement(label);
426✔
13336

13337
   case tIF:
373✔
13338
   case tFOR:
13339
   case tCASE:
13340
      return p_generate_statement(label);
373✔
13341

13342
   case tLPAREN:
18✔
13343
   case tLTLT:
13344
      return p_concurrent_signal_assignment_statement(label, NULL);
18✔
13345

13346
   case tID:
2,533✔
13347
      {
13348
         const token_t p2 = peek_nth(2);
2,533✔
13349
         if ((label != NULL && p2 == tSEMI) || p2 == tGENERIC || p2 == tPORT)
2,533✔
13350
            return p_component_instantiation_statement(label, NULL);
228✔
13351
         else {
13352
            tree_t name = p_name(N_SUBPROGRAM), conc;
2,305✔
13353
            if (peek() == tLE)
2,305✔
13354
               return p_concurrent_signal_assignment_statement(label, name);
2,238✔
13355
            else if (scan(tGENERIC, tPORT))
67✔
13356
               return p_component_instantiation_statement(label, name);
3✔
13357
            else {
13358
               switch (tree_kind(name)) {
64✔
13359
               case T_REF:
59✔
13360
                  if (tree_has_ref(name)) {
59✔
13361
                     tree_t decl = tree_ref(name);
53✔
13362
                     if (tree_kind(decl) == T_COMPONENT)
53✔
13363
                        return p_component_instantiation_statement(label, name);
×
13364
                  }
13365
                  // Fall-through
13366
               case T_PROT_REF:
13367
                  return p_concurrent_procedure_call_statement(label, name);
62✔
13368
               default:
2✔
13369
                  parse_error(CURRENT_LOC, "expected concurrent statement");
2✔
13370
                  drop_tokens_until(tSEMI);
2✔
13371
                  return ensure_labelled(tree_new(T_CONCURRENT), label);
2✔
13372
               }
13373
            }
13374

13375
            return conc;
13376
         }
13377
      }
13378

13379
   case tPOSTPONED:
49✔
13380
      {
13381
         const token_t tok2 = peek_nth(2);
49✔
13382
         if (tok2 == tASSERT)
49✔
13383
            return p_concurrent_assertion_statement(label);
22✔
13384
         else if (tok2 == tID || tok2 == tLPAREN) {
27✔
13385
            consume(tPOSTPONED);
2✔
13386

13387
            tree_t name = p_name(N_SUBPROGRAM), conc;
2✔
13388
            if (peek() == tLE)
2✔
13389
               conc = p_concurrent_signal_assignment_statement(label, name);
2✔
13390
            else
13391
               conc = p_concurrent_procedure_call_statement(label, name);
×
13392

13393
            tree_set_flag(conc, TREE_F_POSTPONED);
2✔
13394
            return conc;
2✔
13395
         }
13396
         else
13397
            return p_process_statement(label);
25✔
13398
      }
13399
   default:
8✔
13400
      expect(tPROCESS, tPOSTPONED, tCOMPONENT, tENTITY, tCONFIGURATION,
15✔
13401
             tWITH, tASSERT, tBLOCK, tIF, tFOR, tCASE, tLPAREN, tID,
13402
             STD(08, tLTLT));
13403
      drop_tokens_until(tSEMI);
8✔
13404
      return ensure_labelled(tree_new(T_BLOCK), label);
8✔
13405
   }
13406
}
13407

13408
static void p_architecture_declarative_part(tree_t arch)
5,217✔
13409
{
13410
   // { block_declarative_item }
13411

13412
   BEGIN("architecture declarative part");
10,434✔
13413

13414
   while (not_at_token(tBEGIN))
17,632✔
13415
      p_block_declarative_item(arch);
12,415✔
13416
}
5,217✔
13417

13418
static void p_architecture_statement_part(tree_t arch)
5,217✔
13419
{
13420
   // { concurrent_statement }
13421

13422
   BEGIN("architecture statement part");
10,431✔
13423

13424
   while (not_at_token(tEND))
15,540✔
13425
      p_concurrent_statement_or_psl(arch);
10,326✔
13426
}
5,214✔
13427

13428
static void p_architecture_body(tree_t unit)
5,217✔
13429
{
13430
   // architecture identifier of entity_name is architecture_declarative_part
13431
   //   begin architecture_statement_part end [ architecture ]
13432
   //   [ architecture_simple_name ] ;
13433

13434
   BEGIN("architecture body");
10,431✔
13435

13436
   tree_change_kind(unit, T_ARCH);
5,217✔
13437

13438
   consume(tARCHITECTURE);
5,217✔
13439

13440
   ident_t arch_name = p_identifier();
5,217✔
13441
   tree_set_ident(unit, arch_name);
5,217✔
13442

13443
   consume(tOF);
5,217✔
13444

13445
   tree_t e = NULL;
5,217✔
13446
   ident_t entity_name = p_entity_name(&e);
5,217✔
13447
   tree_set_ident2(unit, entity_name);
5,217✔
13448

13449
   consume(tIS);
5,217✔
13450

13451
   if (e != NULL) {
5,217✔
13452
      tree_set_primary(unit, e);
5,214✔
13453
      insert_names_from_context(nametab, e);
5,214✔
13454
   }
13455

13456
   push_scope(nametab);
5,217✔
13457

13458
   if (entity_name != arch_name && e != NULL)
5,217✔
13459
      insert_name(nametab, e, entity_name);
5,164✔
13460

13461
   ident_t ename = ident_prefix(lib_name(lib_work()), entity_name, '.');
5,217✔
13462
   ident_t qual = ident_prefix(ename, arch_name, '-');
5,217✔
13463
   scope_set_prefix(nametab, qual);
5,217✔
13464

13465
   tree_set_loc(unit, CURRENT_LOC);
5,217✔
13466
   insert_name(nametab, unit, NULL);
5,217✔
13467

13468
   push_scope(nametab);
5,217✔
13469
   scope_set_container(nametab, unit);
5,217✔
13470

13471
   if (e != NULL) {
5,217✔
13472
      insert_generics(nametab, e);
5,214✔
13473
      insert_ports(nametab, e);
5,214✔
13474
      insert_decls(nametab, e);
5,214✔
13475
   }
13476

13477
   continue_proc_labelling_from(e, nametab);
5,217✔
13478

13479
   p_architecture_declarative_part(unit);
5,217✔
13480

13481
   consume(tBEGIN);
5,217✔
13482

13483
   p_architecture_statement_part(unit);
5,217✔
13484

13485
   consume(tEND);
5,214✔
13486
   optional(tARCHITECTURE);
5,214✔
13487
   p_trailing_label(arch_name);
5,214✔
13488
   consume(tSEMI);
5,214✔
13489

13490
   tree_set_ident(unit, qual);
5,214✔
13491
   tree_set_loc(unit, CURRENT_LOC);
5,214✔
13492

13493
   sem_check(unit, nametab);
5,214✔
13494

13495
   pop_scope(nametab);
5,214✔
13496
   pop_scope(nametab);
5,214✔
13497
}
5,214✔
13498

13499
static void p_package_body_declarative_item(tree_t parent)
8,330✔
13500
{
13501
   // subprogram_declaration | subprogram_body | type_declaration
13502
   //   | subtype_declaration | constant_declaration
13503
   //   | shared_variable_declaration | file_declaration | alias_declaration
13504
   //   | use_clause | group_template_declaration | group_declaration
13505
   //
13506
   // 2008: subprogram_instantiation_declaration | attribute_declaration
13507
   //         | attribute_specification | package_instantiation_declaration
13508
   //         | package_declaration
13509

13510
   BEGIN("package body declarative item");
16,660✔
13511

13512
   switch (peek()) {
8,330✔
13513
   case tFUNCTION:
6,993✔
13514
   case tPROCEDURE:
13515
   case tIMPURE:
13516
   case tPURE:
13517
      if (peek_nth(3) == tIS && peek_nth(4) == tNEW)
6,993✔
13518
         tree_add_decl(parent, p_subprogram_instantiation_declaration());
1✔
13519
      else {
13520
         tree_t spec = p_subprogram_specification();
6,992✔
13521
         if (peek() == tSEMI)
6,992✔
13522
            tree_add_decl(parent, p_subprogram_declaration(spec));
34✔
13523
         else
13524
            tree_add_decl(parent, p_subprogram_body(spec));
6,958✔
13525
      }
13526
      break;
13527

13528
   case tSHARED:
25✔
13529
      p_variable_declaration(parent);
25✔
13530
      break;
25✔
13531

13532
   case tFILE:
7✔
13533
      p_file_declaration(parent);
7✔
13534
      break;
7✔
13535

13536
   case tATTRIBUTE:
35✔
13537
      if (peek_nth(3) == tOF)
35✔
13538
         p_attribute_specification(parent);
35✔
13539
      else
13540
         tree_add_decl(parent, p_attribute_declaration());
×
13541

13542
      if (standard() < STD_08)
35✔
13543
         parse_error(tree_loc(tree_decl(parent, tree_decls(parent) - 1)),
1✔
13544
                     "package body may not contain attribute declarations or"
13545
                     " specifications in VHDL-%s", standard_text(standard()));
13546
      break;
13547

13548
   case tTYPE:
360✔
13549
      p_type_declaration(parent);
360✔
13550
      break;
360✔
13551

13552
   case tCONSTANT:
794✔
13553
      p_constant_declaration(parent);
794✔
13554
      break;
794✔
13555

13556
   case tSUBTYPE:
48✔
13557
      tree_add_decl(parent, p_subtype_declaration());
48✔
13558
      break;
48✔
13559

13560
   case tALIAS:
13✔
13561
      p_alias_declaration(parent);
13✔
13562
      break;
13✔
13563

13564
   case tUSE:
34✔
13565
      p_use_clause(parent, tree_add_decl);
34✔
13566
      break;
34✔
13567

13568
   case tGROUP:
×
13569
      if (peek_nth(3) == tIS)
×
13570
         tree_add_decl(parent, p_group_template_declaration());
×
13571
      else
13572
         tree_add_decl(parent, p_group_declaration());
×
13573
      break;
13574

13575
   case tPACKAGE:
18✔
13576
      if (peek_nth(4) == tNEW)
18✔
13577
         tree_add_decl(parent, p_package_instantiation_declaration(NULL));
16✔
13578
      else if (peek_nth(2) == tBODY) {
2✔
13579
         require_std(STD_08, "nested package declarations");
1✔
13580
         tree_add_decl(parent, p_package_body(NULL));
1✔
13581
      }
13582
      else {
13583
         require_std(STD_08, "nested package declarations");
1✔
13584
         tree_add_decl(parent, p_package_declaration(NULL));
1✔
13585
      }
13586
      break;
13587

13588
   default:
3✔
13589
      expect(tFUNCTION, tPROCEDURE, tSHARED, tIMPURE, tPURE, tATTRIBUTE, tTYPE,
6✔
13590
             tCONSTANT, tSUBTYPE, tFILE, tALIAS, tUSE, tGROUP,
13591
             STD(08, tPACKAGE));
13592
   }
13593
}
8,330✔
13594

13595
static void p_package_body_declarative_part(tree_t unit)
798✔
13596
{
13597
   // { package_body_declarative_item }
13598

13599
   BEGIN("package body declarative part");
1,596✔
13600

13601
   while (not_at_token(tEND))
9,128✔
13602
      p_package_body_declarative_item(unit);
8,330✔
13603
}
798✔
13604

13605
static tree_t p_package_body(tree_t unit)
798✔
13606
{
13607
   // package body simple_name is package_body_declarative_part
13608
   //   end [ package body ] [ simple_name ] ;
13609

13610
   BEGIN("package body");
798✔
13611

13612
   consume(tPACKAGE);
798✔
13613
   consume(tBODY);
798✔
13614

13615
   ident_t name = p_identifier(), qual = name;
798✔
13616

13617
   tree_t body;
798✔
13618
   if (unit != NULL) {
798✔
13619
      // Package body as primary unit
13620
      assert(tree_kind(unit) == T_DESIGN_UNIT);
797✔
13621
      tree_change_kind(unit, T_PACK_BODY);
797✔
13622
      body = unit;
797✔
13623

13624
      qual = ident_prefix(lib_name(lib_work()), name, '.');
797✔
13625
   }
13626
   else
13627
      body = tree_new(T_PACK_BODY);
1✔
13628

13629
   tree_t pack = resolve_name(nametab, CURRENT_LOC, qual);
798✔
13630
   if (pack != NULL && tree_kind(pack) != T_PACKAGE) {
798✔
13631
      parse_error(CURRENT_LOC, "unit %s is not a package", istr(qual));
×
13632
      pack = NULL;
13633
   }
13634
   else if (pack != NULL) {
798✔
13635
      tree_set_primary(body, pack);
793✔
13636
      insert_names_from_context(nametab, pack);
793✔
13637
   }
13638

13639
   push_scope(nametab);
798✔
13640

13641
   tree_set_ident(body, ident_prefix(qual, ident_new("body"), '-'));
798✔
13642
   tree_set_loc(body, CURRENT_LOC);
798✔
13643

13644
   scope_set_prefix(nametab, qual);
798✔
13645
   insert_name(nametab, body, name);
798✔
13646

13647
   consume(tIS);
798✔
13648

13649
   push_scope(nametab);
798✔
13650
   scope_set_container(nametab, body);
798✔
13651

13652
   if (pack != NULL) {
798✔
13653
      insert_generics(nametab, pack);
793✔
13654
      insert_decls(nametab, pack);
793✔
13655
   }
13656

13657
   p_package_body_declarative_part(body);
798✔
13658

13659
   if (pack != NULL && (tree_global_flags(pack) & TREE_GF_DEFERRED_INST))
798✔
13660
      package_body_deferred_instantiation(pack, body);
4✔
13661

13662
   consume(tEND);
798✔
13663

13664
   if (optional(tPACKAGE))
798✔
13665
      consume(tBODY);
706✔
13666

13667
   p_trailing_label(name);
798✔
13668
   consume(tSEMI);
798✔
13669

13670
   tree_set_loc(body, CURRENT_LOC);
798✔
13671
   sem_check(body, nametab);
798✔
13672

13673
   pop_scope(nametab);
798✔
13674
   pop_scope(nametab);
798✔
13675

13676
   return body;
798✔
13677
}
13678

13679
static void p_secondary_unit(tree_t unit)
6,014✔
13680
{
13681
   // architecture_body | package_body
13682

13683
   BEGIN("secondary unit");
12,025✔
13684

13685
   switch (peek()) {
6,014✔
13686
   case tARCHITECTURE:
5,217✔
13687
      p_architecture_body(unit);
5,217✔
13688
      break;
5,217✔
13689

13690
   case tPACKAGE:
797✔
13691
      p_package_body(unit);
797✔
13692
      break;
797✔
13693

13694
   default:
×
13695
      expect(tARCHITECTURE, tPACKAGE);
×
13696
   }
13697
}
6,011✔
13698

13699
static void p_library_unit(tree_t unit)
12,914✔
13700
{
13701
   // primary_unit | secondary_unit
13702

13703
   BEGIN("library unit");
12,914✔
13704

13705
   switch (peek()) {
12,914✔
13706
   case tENTITY:
5,336✔
13707
   case tCONFIGURATION:
13708
   case tCONTEXT:
13709
      p_primary_unit(unit);
5,336✔
13710
      break;
5,336✔
13711

13712
   case tARCHITECTURE:
5,217✔
13713
      p_secondary_unit(unit);
5,217✔
13714
      break;
5,217✔
13715

13716
   case tPACKAGE:
2,357✔
13717
      if (peek_nth(2) == tBODY)
2,357✔
13718
         p_secondary_unit(unit);
797✔
13719
      else
13720
         p_primary_unit(unit);
1,560✔
13721
      break;
13722

13723
   default:
4✔
13724
      expect(tENTITY, tCONFIGURATION, tARCHITECTURE, tPACKAGE,
7✔
13725
             STD(08, tCONTEXT));
13726
   }
13727

13728
   if (bootstrapping && unit != find_std(nametab))
12,911✔
13729
      parse_error(tree_loc(unit), "--bootstrap must only be used with "
12,911✔
13730
                  "STANDARD package");
13731
}
12,911✔
13732

13733
static tree_t p_design_unit(void)
12,914✔
13734
{
13735
   BEGIN("design unit");
12,914✔
13736

13737
   push_scope(nametab);
12,914✔
13738

13739
   tree_t unit = tree_new(T_DESIGN_UNIT);
12,914✔
13740
   scope_set_container(nametab, unit);
12,914✔
13741

13742
   ident_t std_i = well_known(W_STD);
12,914✔
13743

13744
   tree_t std = tree_new(T_LIBRARY);
12,914✔
13745
   tree_set_ident(std, std_i);
12,914✔
13746
   tree_set_ident2(std, std_i);
12,914✔
13747
   tree_add_context(unit, std);
12,914✔
13748
   insert_name(nametab, std, std_i);
12,914✔
13749

13750
   ident_t work_name = lib_name(lib_work());
12,914✔
13751
   tree_t work = tree_new(T_LIBRARY);
12,914✔
13752
   tree_set_ident(work, work_name);
12,914✔
13753
   tree_set_ident2(work, work_name);
12,914✔
13754
   tree_add_context(unit, work);
12,914✔
13755
   insert_name(nametab, work, well_known(W_WORK));
12,914✔
13756
   insert_name(nametab, work, NULL);
12,914✔
13757

13758
   // The std.standard package is implicit unless we are bootstrapping
13759
   if (!bootstrapping) {
12,914✔
13760
      lib_t lstd = lib_require(std_i);
12,911✔
13761
      ident_t standard_i = well_known(W_STD_STANDARD);
12,911✔
13762
      tree_t std_pkg = lib_get(lstd, standard_i);
12,911✔
13763
      if (std_pkg == NULL)
12,911✔
13764
         fatal("cannot find %s package", istr(standard_i));
×
13765

13766
      tree_t u = tree_new(T_USE);
12,911✔
13767
      tree_set_ident(u, standard_i);
12,911✔
13768
      tree_set_ident2(u, well_known(W_ALL));
12,911✔
13769
      tree_set_ref(u, std_pkg);
12,911✔
13770

13771
      tree_add_context(unit, u);
12,911✔
13772
      insert_names_from_use(nametab, u);
12,911✔
13773
   }
13774

13775
   p_context_clause(unit);
12,914✔
13776
   p_library_unit(unit);
12,914✔
13777

13778
   pop_scope(nametab);
12,911✔
13779

13780
   return unit;
12,911✔
13781
}
13782

13783
static void flush_pragmas(tree_t unit)
12,911✔
13784
{
13785
   // Make sure pragmas always appear in off/on pairs
13786
   tree_t coverage_off = NULL, synthesis_off = NULL, translate_off = NULL;
12,911✔
13787

13788
   for (int i = 0; i < pragmas.count; i++) {
12,970✔
13789
      tree_t p = pragmas.items[i];
59✔
13790
      tree_add_pragma(unit, p);
59✔
13791

13792
      switch (tree_subkind(p)) {
59✔
13793
      case PRAGMA_SYNTHESIS_ON: synthesis_off = NULL; break;
3✔
13794
      case PRAGMA_SYNTHESIS_OFF: synthesis_off = p; break;
4✔
13795
      case PRAGMA_COVERAGE_ON: coverage_off = NULL; break;
25✔
13796
      case PRAGMA_COVERAGE_OFF: coverage_off = p; break;
26✔
13797
      case PRAGMA_TRANSLATE_ON: translate_off = NULL; break;
×
13798
      case PRAGMA_TRANSLATE_OFF: translate_off = p; break;
1✔
13799
      }
13800
   }
13801

13802
   if (coverage_off != NULL)
12,911✔
13803
      warn_at(tree_loc(coverage_off), "no matching $bold$coverage on$$ "
1✔
13804
              "directive seen before end of design unit");
13805

13806
   if (synthesis_off != NULL)
12,911✔
13807
      warn_at(tree_loc(synthesis_off), "no matching $bold$synthesis "
1✔
13808
              "translate_on$$ directive seen before end of design unit");
13809

13810
   if (translate_off != NULL)
12,911✔
13811
      warn_at(tree_loc(translate_off), "no matching $bold$pragma "
1✔
13812
              "translate_on$$ directive seen before end of design unit");
13813

13814
   ACLEAR(pragmas);
12,911✔
13815
}
12,911✔
13816

13817
tree_t parse(void)
17,436✔
13818
{
13819
   n_correct = RECOVER_THRESH;
17,436✔
13820

13821
   scan_as_vhdl();
17,436✔
13822

13823
   if (peek() == tEOF)
17,436✔
13824
      return NULL;
13825

13826
   make_new_arena();
12,914✔
13827
   nametab = nametab_new();
12,914✔
13828

13829
   tree_t unit = p_design_unit();
12,914✔
13830

13831
   nametab_finish(nametab);
12,911✔
13832
   nametab = NULL;
12,911✔
13833

13834
   flush_pragmas(unit);
12,911✔
13835

13836
   if (tree_kind(unit) == T_DESIGN_UNIT)
12,911✔
13837
      return NULL;
4✔
13838

13839
   return unit;
13840
}
13841

13842
void reset_vhdl_parser(void)
4,512✔
13843
{
13844
   bootstrapping = opt_get_int(OPT_BOOTSTRAP);
4,512✔
13845

13846
   if (tokenq == NULL) {
4,512✔
13847
      tokenq_sz = 128;
4,497✔
13848
      tokenq = xmalloc_array(tokenq_sz, sizeof(tokenq_t));
4,497✔
13849
   }
13850

13851
   tokenq_head = tokenq_tail = 0;
4,512✔
13852
}
4,512✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc