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

nickg / nvc / 20582222397

29 Dec 2025 08:17PM UTC coverage: 92.573% (+0.008%) from 92.565%
20582222397

push

github

nickg
Add a table ref opcode for table lookups

76 of 77 new or added lines in 5 files covered. (98.7%)

510 existing lines in 6 files now uncovered.

75818 of 81901 relevant lines covered (92.57%)

454870.08 hits per line

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

94.03
/src/common.c
1
//
2
//  Copyright (C) 2013-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 "common.h"
20
#include "diag.h"
21
#include "hash.h"
22
#include "ident.h"
23
#include "lib.h"
24
#include "lower.h"
25
#include "option.h"
26
#include "phase.h"
27
#include "printf.h"
28
#include "scan.h"
29
#include "thread.h"
30
#include "type.h"
31
#include "vlog/vlog-phase.h"
32
#include "sdf/sdf-phase.h"
33
#include "sdf/sdf-util.h"
34

35
#include <assert.h>
36
#include <ctype.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include <inttypes.h>
40

41
static vhdl_standard_t  current_std  = STD_08;
42
static bool             have_set_std = false;
43
static ident_t          id_cache[NUM_WELL_KNOWN];
44
static text_buf_t      *syntax_buf = NULL;
45

46
int64_t assume_int(tree_t t)
602,589✔
47
{
48
   int64_t value;
602,589✔
49
   if (folded_int(t, &value))
602,589✔
50
      return value;
602,589✔
51

52
   fatal_at(tree_loc(t), "expression cannot be folded to "
×
53
            "an integer constant");
54
}
55

56
void range_bounds(tree_t r, int64_t *low, int64_t *high)
95,698✔
57
{
58
   assert(tree_kind(r) == T_RANGE);
95,698✔
59

60
   const int64_t left = assume_int(tree_left(r));
95,698✔
61
   const int64_t right = assume_int(tree_right(r));
95,698✔
62

63
   *low  = tree_subkind(r) == RANGE_TO ? left : right;
95,698✔
64
   *high = tree_subkind(r) == RANGE_TO ? right : left;
95,698✔
65
}
95,698✔
66

67
bool folded_int(tree_t t, int64_t *l)
4,827,383✔
68
{
69
   switch (tree_kind(t)) {
4,871,548✔
70
   case T_LITERAL:
3,029,513✔
71
      switch (tree_subkind(t)) {
3,029,513✔
72
      case L_PHYSICAL:
18,127✔
73
         if (tree_has_ref(t))
18,127✔
74
            return false;
75
         // Fall-through
76
      case L_INT:
77
         *l = tree_ival(t);
2,955,970✔
78
         return true;
2,955,970✔
79
      default:
80
         return false;
81
      }
82
   case T_QUALIFIED:
352✔
83
      return folded_int(tree_value(t), l);
352✔
84
   case T_REF:
1,605,253✔
85
      if (tree_has_ref(t)) {
1,605,253✔
86
         tree_t decl = tree_ref(t);
1,605,252✔
87
         switch (tree_kind(decl)) {
1,605,252✔
88
         case T_CONST_DECL:
51,487✔
89
            if (tree_has_value(decl))
51,487✔
90
               return folded_int(tree_value(decl), l);
43,336✔
91
            else
92
               return false;
93
         case T_ENUM_LIT:
1,474,839✔
94
            *l = tree_pos(decl);
1,474,839✔
95
            return true;
1,474,839✔
96
         case T_ALIAS:
477✔
97
            return folded_int(tree_value(decl), l);
477✔
98
         default:
99
            return false;
100
         }
101
      }
102
      // Fall-through
103
   default:
104
      return false;
105
   }
106
}
107

108
bool folded_real(tree_t t, double *l)
256,754✔
109
{
110
   switch (tree_kind(t)) {
256,763✔
111
   case T_LITERAL:
247,827✔
112
      if (tree_subkind(t) == L_REAL) {
247,827✔
113
         *l = tree_dval(t);
247,750✔
114
         return true;
247,750✔
115
      }
116
      else
117
         return false;
118
   case T_QUALIFIED:
9✔
119
      return folded_real(tree_value(t), l);
9✔
120
   default:
121
      return false;
122
   }
123
}
124

125
bool folded_length(tree_t r, int64_t *l)
69,339✔
126
{
127
   int64_t low, high;
69,339✔
128
   if (folded_bounds(r, &low, &high)) {
69,339✔
129
      *l = MAX(high - low + 1, 0);
65,378✔
130
      return true;
65,378✔
131
   }
132
   else
133
      return false;
134
}
135

136
bool folded_bounds(tree_t r, int64_t *low, int64_t *high)
1,958,753✔
137
{
138
   assert(tree_kind(r) == T_RANGE);
1,958,753✔
139

140
   const range_kind_t rkind = tree_subkind(r);
1,958,753✔
141

142
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
1,958,753✔
143
      return false;
144

145
   int64_t left, right;
1,951,189✔
146
   if (!folded_int(tree_left(r), &left))
1,951,189✔
147
      return false;
148
   else if (!folded_int(tree_right(r), &right))
1,824,024✔
149
      return false;
150

151
   switch (rkind) {
1,781,197✔
152
   case RANGE_TO:
1,650,845✔
153
      *low  = left;
1,650,845✔
154
      *high = right;
1,650,845✔
155
      return true;
1,650,845✔
156
   case RANGE_DOWNTO:
130,352✔
157
      *low  = right;
130,352✔
158
      *high = left;
130,352✔
159
      return true;
130,352✔
160
   default:
161
      return false;
162
   }
163
}
164

165
bool folded_bounds_real(tree_t r, double *low, double *high)
95,184✔
166
{
167
   assert(tree_kind(r) == T_RANGE);
95,184✔
168

169
   const range_kind_t rkind = tree_subkind(r);
95,184✔
170

171
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
95,184✔
172
      return false;
173

174
   double left, right;
95,181✔
175
   if (folded_real(tree_left(r), &left) && folded_real(tree_right(r), &right)) {
95,181✔
176
      switch (rkind) {
95,086✔
177
      case RANGE_TO:
95,086✔
178
         *low  = left;
95,086✔
179
         *high = right;
95,086✔
180
         return true;
95,086✔
181
      case RANGE_DOWNTO:
×
182
         *low  = right;
×
183
         *high = left;
×
184
         return true;
×
185
      default:
186
         return false;
187
      }
188
   }
189
   else
190
      return false;
95✔
191
}
192

193
bool folded_bool(tree_t t, bool *b)
39,425✔
194
{
195
   if (tree_kind(t) == T_REF) {
39,425✔
196
      tree_t decl = tree_ref(t);
9,349✔
197
      if (tree_kind(decl) == T_ENUM_LIT
9,349✔
198
          && type_ident(tree_type(decl)) == well_known(W_STD_BOOL)) {
6,807✔
199
         *b = (tree_pos(decl) == 1);
6,807✔
200
         return true;
6,807✔
201
      }
202
   }
203

204
   return false;
205
}
206

207
tree_t get_enum_lit(tree_t t, type_t type, int pos)
7,184✔
208
{
209
   type_t enum_type = type_base_recur(type ?: tree_type(t));
7,184✔
210
   tree_t lit = type_enum_literal(enum_type, pos);
7,184✔
211

212
   tree_t b = tree_new(T_REF);
7,184✔
213
   tree_set_loc(b, tree_loc(t));
7,184✔
214
   tree_set_ref(b, lit);
7,184✔
215
   tree_set_type(b, enum_type);
7,184✔
216
   tree_set_ident(b, tree_ident(lit));
7,184✔
217

218
   return b;
7,184✔
219
}
220

221
tree_t get_int_lit(tree_t t, type_t type, int64_t i)
39,365✔
222
{
223
   tree_t f = tree_new(T_LITERAL);
39,365✔
224
   tree_set_subkind(f, L_INT);
39,365✔
225
   tree_set_ival(f, i);
39,365✔
226
   tree_set_loc(f, tree_loc(t));
39,365✔
227
   tree_set_type(f, type ?: tree_type(t));
39,365✔
228

229
   return f;
39,365✔
230
}
231

232
tree_t get_discrete_lit(tree_t t, type_t type, int64_t i)
4,490✔
233
{
234
   if (type_is_enum(type)) {
4,490✔
235
      type_t base = type_base_recur(type);
80✔
236
      const int maxlit = type_enum_literals(base);
80✔
237
      if (i >= maxlit)
80✔
238
         return NULL;
239
      else
240
         return get_enum_lit(t, type, i);
80✔
241
   }
242
   else
243
      return get_int_lit(t, type, i);
4,410✔
244
}
245

246
tree_t get_real_lit(tree_t t, type_t type, double r)
×
247
{
248
   tree_t f = tree_new(T_LITERAL);
×
249
   tree_set_loc(f, tree_loc(t));
×
250
   tree_set_subkind(f, L_REAL);
×
251
   tree_set_dval(f, r);
×
252
   tree_set_type(f, type ?: tree_type(t));
×
253

254
   return f;
×
255
}
256

257
bool parse_value(type_t type, const char *str, parsed_value_t *value)
102✔
258
{
259
   type_t base = type_base_recur(type);
102✔
260
   const type_kind_t basek = type_kind(base);
102✔
261

262
   if (basek == T_ARRAY && type_is_character_array(base)) {
102✔
263
      value->enums = NULL;
21✔
264

265
      int map[256];
21✔
266
      for (int i = 0; i < ARRAY_LEN(map); i++)
5,397✔
267
         map[i] = INT_MAX;
5,376✔
268

269
      type_t elem = type_elem(base);
21✔
270

271
      const int nlits = type_enum_literals(elem);
21✔
272
      for (int i = 0; i < nlits; i++) {
2,603✔
273
         ident_t id = tree_ident(type_enum_literal(elem, i));
2,582✔
274
         if (ident_char(id, 0) == '\'')
2,582✔
275
            map[(uint8_t)ident_char(id, 1)] = i;
1,932✔
276
      }
277

278
      while (map[(uint8_t)*str] == INT_MAX && isspace_iso88591(*str))
28✔
279
         ++str;
7✔
280

281
      int base = 0, dbits = 1;
21✔
282
      bool quoted = false;
21✔
283
      if (map['\"'] == INT_MAX) {
21✔
284
         if (str[0] == '\"') {
11✔
285
            quoted = true;
2✔
286
            str++;
2✔
287
         }
288
         else if (str[1] == '\"' && toupper_iso88591(str[0]) == 'X') {
9✔
289
            quoted = true;
3✔
290
            base = 16;
3✔
291
            dbits = 4;
3✔
292
            str += 2;
3✔
293
         }
294
      }
295

296
      const size_t max = strlen(str) * dbits;
21✔
297
      enum_array_t *array = xmalloc_flex(sizeof(enum_array_t), max, 1);
21✔
298

299
      int n = 0, m;
21✔
300
      for (; *str != '\0'; str++, n += dbits) {
116✔
301
         if (base > 0) {
82✔
302
            const bool extended = (isdigit_iso88591(*str) && *str < '0' + base)
20✔
303
               || (base > 10 && *str >= 'A' && *str < 'A' + base - 10)
4✔
304
               || (base > 10 && *str >= 'a' && *str < 'a' + base - 10);
15✔
305

306
            if (!extended)
12✔
307
               break;
308

309
            int digit = isdigit_iso88591(*str)
9✔
310
               ? (*str - '0')
8✔
311
               : 10 + (toupper_iso88591(*str) - 'A');
9✔
312

313
            bool valid = true;
9✔
314
            for (int i = dbits - 1; i >= 0; i--) {
45✔
315
               const uint8_t bit = (digit >> i) & 1 ? '1' : '0';
36✔
316
               valid &= (m = map[bit]) != INT_MAX;
36✔
317
               array->values[n + dbits - i - 1] = m;
36✔
318
            }
319

320
            if (!valid)
9✔
321
               break;
322
         }
323
         else if ((m = map[(uint8_t)*str]) == INT_MAX)
70✔
324
            break;
325
         else
326
            array->values[n] = m;
65✔
327
      }
328

329
      assert(n <= max);
21✔
330
      array->count = n;
21✔
331

332
      if (quoted && *str++ != '\"') {
21✔
333
         free(array);
2✔
334
         return false;
2✔
335
      }
336

337
      for (; *str; str++) {
27✔
338
         if (!isspace_iso88591(*str)) {
10✔
339
            free(array);
2✔
340
            return false;
2✔
341
         }
342
      }
343

344
      value->enums = array;
17✔
345
      return true;
17✔
346
   }
347

348
   while (isspace_iso88591(*str))
91✔
349
      ++str;
10✔
350

351
   switch (basek) {
81✔
352
   case T_INTEGER:
56✔
353
      {
354
         const bool is_negative = *str == '-';
56✔
355
         int num_digits = 0;
56✔
356

357
         if (is_negative) ++str;
56✔
358

359
         int64_t sum = 0;
360
         for (; isdigit_iso88591(*str) || (*str == '_'); str++) {
156✔
361
            if (*str != '_') {
100✔
362
               sum *= 10;
98✔
363
               sum += (*str - '0');
98✔
364
               num_digits++;
98✔
365
            }
366
         }
367

368
         value->integer = is_negative ? -sum : sum;
56✔
369

370
         if (num_digits == 0)
56✔
371
            return false;
372
      }
373
      break;
374

375
   case T_ENUM:
14✔
376
      {
377
         bool upcase = true;
14✔
378
         char *copy LOCAL = xstrdup(str), *p;
27✔
379
         for (p = copy; (*p != '\0') && !isspace_iso88591(*p); p++, str++) {
55✔
380
            if (*p == '\'')
41✔
381
               upcase = false;
382
            if (upcase)
23✔
383
               *p = toupper_iso88591(*p);
14✔
384
         }
385
         *p = '\0';
14✔
386

387
         ident_t id = ident_new(copy);
14✔
388

389
         value->integer = -1;
14✔
390

391
         const int nlits = type_enum_literals(base);
14✔
392
         for (int i = 0; i < nlits; i++) {
38✔
393
            if (tree_ident(type_enum_literal(base, i)) == id) {
37✔
394
               value->integer = i;
13✔
395
               break;
13✔
396
            }
397
         }
398

399
         if (value->integer == -1)
14✔
400
            return false;
1✔
401
      }
402
      break;
403

404
   case T_REAL:
6✔
405
      {
406
         char *eptr = NULL;
6✔
407
         value->real = strtod(str, &eptr);
6✔
408
         str = eptr;
6✔
409
      }
410
      break;
6✔
411

412
   case T_PHYSICAL:
5✔
413
      {
414
         char *eptr = NULL;
5✔
415
         double scale = strtod(str, &eptr);
5✔
416
         str = eptr;
5✔
417

418
         while (isspace_iso88591(*str)) ++str;
10✔
419

420
         char *copy LOCAL = xstrdup(str), *p;
10✔
421
         for (p = copy; *p && !isspace_iso88591(*p); p++, str++)
13✔
422
            *p = toupper_iso88591(*p);
8✔
423
         *p = '\0';
5✔
424

425
         if (p == copy)
5✔
426
            return false;
427

428
         ident_t id = ident_new(copy);
4✔
429

430
         value->integer = -1;
4✔
431

432
         const int nunits = type_units(base);
4✔
433
         for (int i = 0; i < nunits; i++) {
11✔
434
            tree_t u = type_unit(base, i);
11✔
435
            if (tree_ident(u) == id) {
11✔
436
               value->integer = scale * assume_int(tree_value(u));
4✔
437
               break;
4✔
438
            }
439
         }
440

441
         if (value->integer == -1)
4✔
442
            return false;
443
      }
444
      break;
445

446
   default:
447
      return false;
448
   }
449

450
   for (; *str; str++) {
90✔
451
      if (!isspace_iso88591(*str))
11✔
452
         return false;
453
   }
454

455
   return true;
456
}
457

458
tree_t make_ref(tree_t to)
6,874✔
459
{
460
   tree_t t = tree_new(T_REF);
6,874✔
461
   tree_set_ident(t, tree_ident(to));
6,874✔
462
   tree_set_ref(t, to);
6,874✔
463
   tree_set_type(t, tree_type(to));
6,874✔
464
   tree_set_loc(t, tree_loc(to));
6,874✔
465
   return t;
6,874✔
466
}
467

468
vhdl_standard_t standard(void)
2,714,670✔
469
{
470
   return current_std;
2,714,670✔
471
}
472

473
void set_standard(vhdl_standard_t s)
5,530✔
474
{
475
   current_std = s;
5,530✔
476
   have_set_std = true;
5,530✔
477
}
5,530✔
478

479
void set_default_standard(vhdl_standard_t s)
484✔
480
{
481
   if (!have_set_std)
484✔
482
      set_standard(s);
102✔
483
}
484✔
484

485
const char *standard_text(vhdl_standard_t s)
5,031✔
486
{
487
   static const char *text[] = {
5,031✔
488
      "1987", "1993", "2000", "2002", "2008", "2019"
489
   };
490

491
   if ((unsigned)s < ARRAY_LEN(text))
5,031✔
492
      return text[s];
5,031✔
493
   else
494
      return "????";
495
}
496

497
tree_t find_element_mode_indication(tree_t view, tree_t field, bool *converse)
518✔
498
{
499
   switch (tree_kind(view)) {
1,608✔
500
   case T_REF:
697✔
501
      return find_element_mode_indication(tree_ref(view), field, converse);
697✔
502

503
   case T_ALIAS:
179✔
504
      return find_element_mode_indication(tree_value(view), field, converse);
179✔
505

506
   case T_ATTR_REF:
214✔
507
      assert(tree_subkind(view) == ATTR_CONVERSE);
214✔
508
      *converse = !*converse;
214✔
509
      return find_element_mode_indication(tree_name(view), field, converse);
214✔
510

511
   case T_VIEW_DECL:
518✔
512
      {
513
         type_t view_type = tree_type(view);
518✔
514
         assert(type_kind(view_type) == T_VIEW);
518✔
515

516
         const int nelems = type_fields(view_type);
518✔
517
         for (int i = 0; i < nelems; i++) {
853✔
518
            tree_t e = type_field(view_type, i);
853✔
519
            if (tree_ref(e) == field)
853✔
520
               return e;
518✔
521
         }
522

523
         return NULL;
524
      }
525

526
   default:
×
527
      fatal_trace("unhandled tree kind %s in find_element_mode_indication",
528
                  tree_kind_str(tree_kind(view)));
529
   }
530
}
531

532
port_mode_t converse_mode(tree_t port, bool converse)
342✔
533
{
534
   const port_mode_t mode = tree_subkind(port);
342✔
535
   switch (mode) {
342✔
536
   case PORT_IN: return converse ? PORT_OUT : PORT_IN;
161✔
537
   case PORT_OUT: return converse ? PORT_IN : PORT_OUT;
153✔
538
   default: return mode;
539
   }
540
}
541

542
class_t class_of(tree_t t)
1,056,636✔
543
{
544
   switch (tree_kind(t)) {
1,111,154✔
545
   case T_VAR_DECL:
546
      return C_VARIABLE;
547
   case T_SIGNAL_DECL:
48,143✔
548
   case T_IMPLICIT_SIGNAL:
549
      return C_SIGNAL;
48,143✔
550
   case T_CONST_DECL:
38,432✔
551
      return C_CONSTANT;
38,432✔
552
   case T_PORT_DECL:
163,073✔
553
   case T_GENERIC_DECL:
554
   case T_PARAM_DECL:
555
   case T_EXTERNAL_NAME:
556
      return tree_class(t);
163,073✔
557
   case T_ENUM_LIT:
600,858✔
558
   case T_LITERAL:
559
   case T_STRING:
560
      return C_LITERAL;
600,858✔
561
   case T_FIELD_DECL:
1,125✔
562
   case T_ATTR_DECL:
563
      return C_DEFAULT;
1,125✔
564
   case T_VIEW_DECL:
166✔
565
      return C_VIEW;
166✔
566
   case T_UNIT_DECL:
8,224✔
567
      return C_UNITS;
8,224✔
568
   case T_ARCH:
47✔
569
      return C_ARCHITECTURE;
47✔
570
   case T_FUNC_DECL:
2,686✔
571
   case T_FUNC_BODY:
572
   case T_FUNC_INST:
573
   case T_FCALL:
574
   case T_PROT_FCALL:
575
      return C_FUNCTION;
2,686✔
576
   case T_PROC_DECL:
9,360✔
577
   case T_PROC_BODY:
578
   case T_PROC_INST:
579
   case T_PCALL:
580
   case T_PROT_PCALL:
581
      return C_PROCEDURE;
9,360✔
582
   case T_ENTITY:
135✔
583
      return C_ENTITY;
135✔
584
   case T_SUBTYPE_DECL:
20,381✔
585
      return C_SUBTYPE;
20,381✔
586
   case T_TYPE_DECL:
78,140✔
587
   case T_PROT_DECL:
588
   case T_PROT_BODY:
589
      return C_TYPE;
78,140✔
590
   case T_FILE_DECL:
1,186✔
591
      return C_FILE;
1,186✔
592
   case T_PROCESS:
138✔
593
   case T_BLOCK:
594
   case T_FOR:
595
   case T_INSTANCE:
596
   case T_CONCURRENT:
597
   case T_ELAB:
598
   case T_PSL_DECL:
599
   case T_PSL_DIRECT:
600
   case T_FOR_GENERATE:
601
   case T_IF_GENERATE:
602
   case T_CASE_GENERATE:
603
      return C_LABEL;
138✔
604
   case T_COMPONENT:
1✔
605
      return C_COMPONENT;
1✔
606
   case T_REF:
43,351✔
607
   case T_PROT_REF:
608
      return tree_has_ref(t) ? class_of(tree_ref(t)) : C_DEFAULT;
43,351✔
609
   case T_ARRAY_REF:
11,171✔
610
   case T_ARRAY_SLICE:
611
   case T_RECORD_REF:
612
   case T_ALL:
613
   case T_ALIAS:
614
   case T_QUALIFIED:
615
      return class_of(tree_value(t));
11,171✔
616
   case T_PACKAGE:
1,227✔
617
   case T_PACK_BODY:
618
   case T_PACK_INST:
619
      return C_PACKAGE;
1,227✔
620
   case T_CONFIGURATION:
1✔
621
      return C_CONFIGURATION;
1✔
622
   case T_LIBRARY:
1✔
623
      return C_LIBRARY;
1✔
624
   case T_ATTR_REF:
70✔
625
      switch (tree_subkind(t)) {
70✔
626
      case ATTR_DELAYED:
627
      case ATTR_STABLE:
628
      case ATTR_QUIET:
629
      case ATTR_TRANSACTION:
630
         return C_SIGNAL;
631
      default:
64✔
632
         return C_DEFAULT;
64✔
633
      }
634
   case T_CONTEXT:
×
635
      return C_CONTEXT;
×
636
   default:
×
637
      fatal_trace("missing class_of for %s", tree_kind_str(tree_kind(t)));
638
   }
639
}
640

641
bool class_has_type(class_t c)
899,863✔
642
{
643
   switch (c) {
899,863✔
644
   case C_LABEL:
645
   case C_ENTITY:
646
   case C_ARCHITECTURE:
647
   case C_COMPONENT:
648
   case C_CONFIGURATION:
649
   case C_PACKAGE:
650
   case C_LIBRARY:
651
      return false;
652
   default:
898,585✔
653
      return true;
898,585✔
654
   }
655
}
656

657
const char *class_str(class_t c)
337✔
658
{
659
   static const char *strs[] = {
337✔
660
      "default", "signal", "variable", "constant", "file", "entity",
661
      "component", "configuration", "architecture", "function", "package",
662
      "type", "subtype", "label", "procedure", "literal", "units", "library",
663
      "context", "view",
664
   };
665
   assert(c < ARRAY_LEN(strs));
337✔
666
   return strs[c];
337✔
667
}
668

669
const char *assoc_kind_str(assoc_kind_t akind)
9✔
670
{
671
   switch (akind) {
9✔
672
   case A_NAMED:  return "named";
673
   case A_CONCAT:
2✔
674
   case A_POS:    return "positional";
2✔
675
   case A_OTHERS: return "others";
3✔
676
   case A_SLICE:
1✔
677
   case A_RANGE:  return "range";
1✔
678
   default:       return "??";
×
679
   }
680
}
681

682
bool is_subprogram(tree_t t)
800,072✔
683
{
684
   switch (tree_kind(t)) {
800,072✔
685
   case T_FUNC_DECL:
686
   case T_FUNC_BODY:
687
   case T_FUNC_INST:
688
   case T_PROC_DECL:
689
   case T_PROC_BODY:
690
   case T_PROC_INST:
691
      return true;
692
   case T_GENERIC_DECL:
4,442✔
693
      {
694
         const class_t class = tree_class(t);
4,442✔
695
         return class == C_FUNCTION || class == C_PROCEDURE;
4,442✔
696
      }
697
   default:
579,983✔
698
      return false;
579,983✔
699
   }
700
}
701

702
bool is_loop_stmt(tree_t t)
489✔
703
{
704
   const tree_kind_t kind = tree_kind(t);
489✔
705
   return kind == T_WHILE || kind == T_FOR || kind == T_LOOP;
489✔
706
}
707

708
bool is_container(tree_t t)
11,732✔
709
{
710
   switch (tree_kind(t)) {
11,732✔
711
   case T_FUNC_BODY:
712
   case T_PROC_BODY:
713
   case T_ENTITY:
714
   case T_ARCH:
715
   case T_PACKAGE:
716
   case T_PACK_BODY:
717
   case T_CONFIGURATION:
718
   case T_BLOCK:
719
   case T_PROT_BODY:
720
   case T_ELAB:
721
   case T_FOR:
722
   case T_PROCESS:
723
   case T_PACK_INST:
724
      return true;
725
   default:
9,328✔
726
      return false;
9,328✔
727
   }
728
}
729

730
bool is_concurrent_block(tree_t t)
1,167✔
731
{
732
   switch (tree_kind(t)) {
1,167✔
733
   case T_ARCH:
734
   case T_ENTITY:
735
   case T_BLOCK:
736
   case T_IF_GENERATE:
737
   case T_FOR_GENERATE:
738
   case T_CASE_GENERATE:
739
      return true;
740
   default:
392✔
741
      return false;
392✔
742
   }
743
}
744

745
bool is_package(tree_t t)
37,503✔
746
{
747
   switch (tree_kind(t)) {
37,503✔
748
   case T_PACKAGE:
749
   case T_PACK_BODY:
750
   case T_PACK_INST:
751
      return true;
752
   default:
1,002✔
753
      return false;
1,002✔
754
   }
755
}
756

757
bool is_design_unit(tree_t t)
41,916✔
758
{
759
   switch (tree_kind(t)) {
41,916✔
760
   case T_ENTITY:
761
   case T_ARCH:
762
   case T_PACKAGE:
763
   case T_PACK_BODY:
764
   case T_CONFIGURATION:
765
   case T_CONTEXT:
766
   case T_PACK_INST:
767
      return true;
768
   default:
9,356✔
769
      return false;
9,356✔
770
   }
771
}
772

773
bool is_literal(tree_t t)
53,346✔
774
{
775
   switch (tree_kind(t)) {
53,346✔
776
   case T_REF:
15,933✔
777
      return tree_has_ref(t) && tree_kind(tree_ref(t)) == T_ENUM_LIT;
17,810✔
778
   case T_LITERAL:
779
      return true;
780
   case T_STRING:
23,341✔
781
   default:
782
      return false;
23,341✔
783
   }
784
}
785

786
bool is_body(tree_t t)
10,846✔
787
{
788
   switch (tree_kind(t)) {
10,846✔
789
   case T_FUNC_BODY:
790
   case T_PROC_BODY:
791
   case T_PACK_BODY:
792
   case T_PROT_BODY:
793
      return true;
794
   default:
3,129✔
795
      return false;
3,129✔
796
   }
797
}
798

799
bool is_guarded_signal(tree_t decl)
14,705✔
800
{
801
   switch (tree_kind(decl)) {
14,705✔
802
   case T_PORT_DECL:
14,226✔
803
   case T_SIGNAL_DECL:
804
      return !!(tree_flags(decl) & (TREE_F_BUS | TREE_F_REGISTER));
14,226✔
805
   default:
806
      return false;
807
   }
808
}
809

810
bool is_type_decl(tree_t t)
896,472✔
811
{
812
   switch (tree_kind(t)) {
896,472✔
813
   case T_TYPE_DECL:
814
   case T_SUBTYPE_DECL:
815
   case T_PROT_DECL:
816
   case T_PROT_BODY:
817
      return true;
818
   default:
704,991✔
819
      return false;
704,991✔
820
   }
821
}
822

823
tree_t aliased_type_decl(tree_t decl)
135,419✔
824
{
825
   switch (tree_kind(decl)) {
135,689✔
826
   case T_ALIAS:
272✔
827
      {
828
         tree_t value = tree_value(decl);
272✔
829
         const tree_kind_t kind = tree_kind(value);
272✔
830
         if (kind == T_REF && tree_has_ref(value))
272✔
831
            return aliased_type_decl(tree_ref(value));
270✔
832
         else if (kind == T_ATTR_REF && is_type_attribute(tree_subkind(value)))
2✔
833
             return value;
834
         else
835
            return NULL;
×
836
      }
837
   case T_TYPE_DECL:
838
   case T_SUBTYPE_DECL:
839
   case T_PROT_DECL:
840
   case T_PROT_BODY:
841
      return decl;
842
   case T_GENERIC_DECL:
1,554✔
843
      if (tree_class(decl) == C_TYPE)
1,554✔
844
         return decl;
845
      else
846
         return NULL;
751✔
847
   default:
32,923✔
848
      return NULL;
32,923✔
849
   }
850
}
851

852
tree_t add_param(tree_t call, tree_t value, param_kind_t kind, tree_t name)
166,637✔
853
{
854
   tree_t p = tree_new(T_PARAM);
166,637✔
855
   tree_set_loc(p, tree_loc(value));
166,637✔
856
   tree_set_subkind(p, kind);
166,637✔
857
   tree_set_value(p, value);
166,637✔
858

859
   switch (kind) {
166,637✔
860
   case P_NAMED:
137✔
861
      assert(name != NULL);
137✔
862
      tree_set_name(p, name);
137✔
863
      break;
137✔
864
   case P_POS:
166,500✔
865
      tree_set_pos(p, tree_params(call));
166,500✔
866
      break;
166,500✔
867
   }
868

869
   tree_add_param(call, p);
166,637✔
870
   return p;
166,637✔
871
}
872

873
type_t array_aggregate_type(type_t array, int from_dim)
354✔
874
{
875
   if (type_is_none(array))
354✔
876
      return type_new(T_NONE);
3✔
877

878
   if (type_is_unconstrained(array)) {
351✔
879
      const int nindex = type_indexes(array);
45✔
880
      assert(from_dim < nindex);
45✔
881

882
      type_t type = type_new(T_ARRAY);
45✔
883
      type_set_ident(type, type_ident(array));
45✔
884
      type_set_elem(type, type_elem(array));
45✔
885

886
      for (int i = from_dim; i < nindex; i++)
90✔
887
         type_add_index(type, type_index(array, i));
45✔
888

889
      return type;
890
   }
891
   else {
892
      const int ndims = dimension_of(array);
306✔
893
      assert(from_dim < ndims);
306✔
894

895
      type_t base = type_new(T_ARRAY);
306✔
896
      type_set_ident(base, type_ident(array));
306✔
897
      type_set_elem(base, type_elem(array));
306✔
898

899
      tree_t constraint = tree_new(T_CONSTRAINT);
306✔
900
      tree_set_subkind(constraint, C_INDEX);
306✔
901

902
      type_t sub = type_new(T_SUBTYPE);
306✔
903
      type_set_base(sub, base);
306✔
904
      type_set_constraint(sub, constraint);
306✔
905

906
      for (int i = from_dim; i < ndims; i++) {
642✔
907
         tree_t r = range_of(array, i);
336✔
908
         if (r == NULL) {
336✔
909
            // Not enough constraints were provided for the type
910
            r = tree_new(T_RANGE);
1✔
911
            tree_set_subkind(r, RANGE_ERROR);
1✔
912
            tree_set_type(r, type_new(T_NONE));
1✔
913
         }
914

915
         type_add_index(base, tree_type(r));
336✔
916
         tree_add_range(constraint, r);
336✔
917
      }
918

919
      return sub;
920
   }
921
}
922

923
unsigned bits_for_range(int64_t low, int64_t high)
2,445,688✔
924
{
925
   if (low > high)
2,445,688✔
926
      return 0;   // Null range
927
   else if (low < 0) {
2,445,687✔
928
      // Signed integers
929
      if (low >= INT8_MIN && high <= INT8_MAX)
578,331✔
930
         return 8;
931
      else if (low >= INT16_MIN && high <= INT16_MAX)
572,491✔
932
         return 16;
933
      else if (low >= INT32_MIN && high <= INT32_MAX)
572,195✔
934
         return 32;
935
      else
936
         return 64;
92,574✔
937
   }
938
   else {
939
      // Unsigned integers
940
      if (high <= 1)
1,867,356✔
941
         return 1;
942
      else if (high <= UINT8_MAX)
1,170,145✔
943
         return 8;
944
      else if (high <= UINT16_MAX)
225,414✔
945
         return 16;
946
      else if (high <= UINT32_MAX)
221,345✔
947
         return 32;
948
      else
949
         return 64;
47,233✔
950
   }
951
}
952

953
unsigned dimension_of(type_t type)
1,459,542✔
954
{
955
   switch (type_kind(type)) {
2,748,030✔
956
   case T_SUBTYPE:
1,288,486✔
957
      return dimension_of(type_base(type));
1,288,486✔
958
   case T_GENERIC:
145✔
959
      switch (type_subkind(type)) {
145✔
960
      case GTYPE_ARRAY:
133✔
961
         return type_indexes(type);
133✔
962
      case GTYPE_ACCESS:
×
963
         return dimension_of(type_designated(type));
×
964
      case GTYPE_FILE:
965
      case GTYPE_PRIVATE:
966
         return 0;
967
      default:
12✔
968
         return 1;
12✔
969
      }
970
   case T_ARRAY:
1,451,789✔
971
      return type_indexes(type);
1,451,789✔
972
   case T_NONE:
973
   case T_RECORD:
974
   case T_INCOMPLETE:
975
   case T_FILE:
976
   case T_SIGNATURE:
977
      return 0;
978
   case T_INTEGER:
7,536✔
979
   case T_REAL:
980
   case T_PHYSICAL:
981
   case T_ENUM:
982
      return type_dims(type);
7,536✔
983
   case T_ACCESS:
2✔
984
      return dimension_of(type_designated(type));
2✔
985
   default:
×
986
      fatal_trace("invalid type kind %s in dimension_of",
987
                  type_kind_str(type_kind(type)));
988
   }
989
}
990

991
tree_t range_of(type_t type, unsigned dim)
2,142,913✔
992
{
993
   switch (type_kind(type)) {
2,178,308✔
994
   case T_SUBTYPE:
1,676,197✔
995
      if (type_has_constraint(type)) {
1,676,197✔
996
         tree_t c = type_constraint(type);
1,640,802✔
997
         switch (tree_subkind(c)) {
1,640,802✔
998
         case C_INDEX:
1,640,802✔
999
         case C_RANGE:
1000
            if (dim < tree_ranges(c))
1,640,802✔
1001
               return tree_range(c, dim);
1,640,801✔
1002
            else
1003
               return NULL;   // Must be an error
1004
         default:
×
1005
            should_not_reach_here();
1006
         }
1007
      }
1008
      else
1009
         return range_of(type_base(type), dim);
35,395✔
1010
   case T_INTEGER:
502,111✔
1011
   case T_REAL:
1012
   case T_PHYSICAL:
1013
   case T_ENUM:
1014
      return type_dim(type, dim);
502,111✔
1015
   default:
×
1016
      fatal_trace("invalid type kind %s for %s in range_of",
1017
                  type_kind_str(type_kind(type)), type_pp(type));
1018
   }
1019
}
1020

1021
range_kind_t direction_of(type_t type, unsigned dim)
25,187✔
1022
{
1023
   switch (type_kind(type)) {
25,267✔
1024
   case T_ENUM:
1025
      return RANGE_TO;
1026
   case T_NONE:
×
1027
      return RANGE_ERROR;
×
1028
   case T_INTEGER:
25,146✔
1029
   case T_REAL:
1030
   case T_PHYSICAL:
1031
   case T_SUBTYPE:
1032
      {
1033
         tree_t r = range_of(type, dim);
25,146✔
1034
         const range_kind_t rkind = tree_subkind(r);
25,146✔
1035
         if (rkind == RANGE_EXPR) {
25,146✔
1036
            // Return a fixed direction if possible
1037
            tree_t value = tree_value(r);
168✔
1038
            assert(tree_kind(value) == T_ATTR_REF);
168✔
1039

1040
            DEBUG_ONLY({
168✔
1041
                  const attr_kind_t attr = tree_subkind(value);
1042
                  assert(attr == ATTR_RANGE || attr == ATTR_REVERSE_RANGE);
1043
               });
168✔
1044

1045
            tree_t name = tree_name(value);
168✔
1046
            if (tree_kind(name) == T_REF && tree_has_ref(name)) {
168✔
1047
               tree_t decl = tree_ref(name);
167✔
1048
               if (is_type_decl(decl))
167✔
1049
                  return direction_of(tree_type(decl), 0);
80✔
1050
            }
1051
         }
1052

1053
         return rkind;
1054
      }
1055
   default:
×
1056
      fatal_trace("invalid type kind %s in direction_of",
1057
                  type_kind_str(type_kind(type)));
1058
   }
1059
}
1060

1061
type_t index_type_of(type_t type, unsigned dim)
239,894✔
1062
{
1063
   if (dim >= dimension_of(type))
239,896✔
1064
      return NULL;
1065

1066
   type_t base = type_base_recur(type);
239,856✔
1067
   type_kind_t base_kind = type_kind(base);
239,856✔
1068
   if (base_kind == T_ARRAY)
239,856✔
1069
      return type_index(base, dim);
236,307✔
1070
   else if (base_kind == T_ENUM || base_kind == T_NONE)
3,549✔
1071
      return type;
1072
   else if (base_kind == T_GENERIC && type_subkind(base) == GTYPE_ARRAY)
3,391✔
1073
      return type_index(base, dim);
82✔
1074
   else if (base_kind == T_RECORD || base_kind == T_GENERIC)
3,309✔
1075
      return NULL;
1076
   else if (base_kind == T_ACCESS)
3,303✔
1077
      return index_type_of(type_designated(type), dim);
2✔
1078
   else
1079
      return tree_type(range_of(base, dim));
3,301✔
1080
}
1081

1082
int64_t rebase_index(type_t array_type, int dim, int64_t value)
240✔
1083
{
1084
   // Convert value which is in the range of array_type to a zero-based index
1085
   tree_t r = range_of(array_type, dim);
240✔
1086
   const int64_t left = assume_int(tree_left(r));
240✔
1087
   return (tree_subkind(r) == RANGE_TO) ? value - left : left - value;
240✔
1088
}
1089

1090
ident_t well_known(well_known_t id)
377,637✔
1091
{
1092
   assert(id < NUM_WELL_KNOWN);
377,637✔
1093
   return id_cache[id];
377,637✔
1094
}
1095

1096
well_known_t is_well_known(ident_t ident)
263,409✔
1097
{
1098
   return ident_key(ident);
263,409✔
1099
}
1100

1101
void intern_strings(void)
5,696✔
1102
{
1103
   static const char *tab[] = {
5,696✔
1104
      [W_STD_STANDARD]   = "STD.STANDARD",
1105
      [W_ALL]            = "all",
1106
      [W_STD_BIT]        = "STD.STANDARD.BIT",
1107
      [W_STD_BOOL]       = "STD.STANDARD.BOOLEAN",
1108
      [W_STD_CHAR]       = "STD.STANDARD.CHARACTER",
1109
      [W_STD_NATURAL]    = "STD.STANDARD.NATURAL",
1110
      [W_STD_POSITIVE]   = "STD.STANDARD.POSITIVE",
1111
      [W_STD_INTEGER]    = "STD.STANDARD.INTEGER",
1112
      [W_STD_STRING]     = "STD.STANDARD.STRING",
1113
      [W_STD_REAL]       = "STD.STANDARD.REAL",
1114
      [W_STD_TIME]       = "STD.STANDARD.TIME",
1115
      [W_STD_BIT_VECTOR] = "STD.STANDARD.BIT_VECTOR",
1116

1117
      [W_IEEE_SIGNED]   = "IEEE.NUMERIC_STD.SIGNED",
1118
      [W_IEEE_UNSIGNED] = "IEEE.NUMERIC_STD.UNSIGNED",
1119
      [W_IEEE_LOGIC]    = "IEEE.STD_LOGIC_1164.STD_LOGIC",
1120
      [W_IEEE_ULOGIC]   = "IEEE.STD_LOGIC_1164.STD_ULOGIC",
1121

1122
      [W_IEEE_1164_AND]  = "IEEE.STD_LOGIC_1164.\"and\"",
1123
      [W_IEEE_1164_NAND] = "IEEE.STD_LOGIC_1164.\"nand\"",
1124
      [W_IEEE_1164_OR]   = "IEEE.STD_LOGIC_1164.\"or\"",
1125
      [W_IEEE_1164_NOR]  = "IEEE.STD_LOGIC_1164.\"nor\"",
1126
      [W_IEEE_1164_XOR]  = "IEEE.STD_LOGIC_1164.\"xor\"",
1127
      [W_IEEE_1164_XNOR] = "IEEE.STD_LOGIC_1164.\"xnor\"",
1128

1129
      [W_FOREIGN]         = "FOREIGN",
1130
      [W_WORK]            = "WORK",
1131
      [W_STD]             = "STD",
1132
      [W_THUNK]           = "thunk",
1133
      [W_BODY]            = "body",
1134
      [W_CARET]           = "^",
1135
      [W_IEEE]            = "IEEE",
1136
      [W_IEEE_1164]       = "IEEE.STD_LOGIC_1164",
1137
      [W_ERROR]           = "$error",
1138
      [W_ELAB]            = "elab",
1139
      [W_NUMERIC_STD]     = "IEEE.NUMERIC_STD",
1140
      [W_NUMERIC_BIT]     = "IEEE.NUMERIC_BIT",
1141
      [W_NVC]             = "NVC",
1142
      [W_DEFAULT_CLOCK]   = "default clock",
1143
      [W_STD_REFLECTION]  = "STD.REFLECTION",
1144
      [W_NEVER_WAITS]     = "NEVER_WAITS",
1145
      [W_NVC_VERILOG]     = "NVC.VERILOG",
1146
      [W_NVC_PSL_SUPPORT] = "NVC.PSL_SUPPORT",
1147
      [W_INSTANCE_NAME]   = "instance_name",
1148
      [W_PATH_NAME]       = "path_name",
1149
      [W_VITAL]           = "VITAL",
1150
      [W_RESOLUTION]      = "resolution",
1151
      [W_TEXT_UTIL]       = "NVC.TEXT_UTIL",
1152
      [W_VERILOG_LOGIC]   = "NVC.VERILOG.T_LOGIC",
1153
      [W_DLR_SIGNED]      = "$signed",
1154
      [W_DLR_CLOG2]       = "$clog2",
1155

1156
      [W_IEEE_LOGIC_VECTOR]      = "IEEE.STD_LOGIC_1164.STD_LOGIC_VECTOR",
1157
      [W_IEEE_ULOGIC_VECTOR]     = "IEEE.STD_LOGIC_1164.STD_ULOGIC_VECTOR",
1158
      [W_IEEE_1164_RISING_EDGE]  = "IEEE.STD_LOGIC_1164.RISING_EDGE(sU)B",
1159
      [W_IEEE_1164_FALLING_EDGE] = "IEEE.STD_LOGIC_1164.FALLING_EDGE(sU)B",
1160

1161
      [W_NUMERIC_STD_UNSIGNED] = "IEEE.NUMERIC_STD_UNSIGNED",
1162
      [W_NUMERIC_BIT_UNSIGNED] = "IEEE.NUMERIC_BIT_UNSIGNED",
1163
      [W_VERILOG_NET_VALUE]    = "NVC.VERILOG.T_NET_VALUE",
1164
      [W_VERILOG_WIRE_ARRAY]   = "NVC.VERILOG.T_WIRE_ARRAY",
1165

1166
      [W_OP_CCONV]               = "\"??\"",
1167
      [W_OP_AND]                 = "\"and\"",
1168
      [W_OP_OR]                  = "\"or\"",
1169
      [W_OP_NAND]                = "\"nand\"",
1170
      [W_OP_NOR]                 = "\"nor\"",
1171
      [W_OP_XOR]                 = "\"xor\"",
1172
      [W_OP_XNOR]                = "\"xnor\"",
1173
      [W_OP_EQUAL]               = "\"=\"",
1174
      [W_OP_NOT_EQUAL]           = "\"/=\"",
1175
      [W_OP_LESS_THAN]           = "\"<\"",
1176
      [W_OP_LESS_EQUAL]          = "\"<=\"",
1177
      [W_OP_GREATER_THAN]        = "\">\"",
1178
      [W_OP_GREATER_EQUAL]       = "\">=\"",
1179
      [W_OP_MATCH_EQUAL]         = "\"?=\"",
1180
      [W_OP_MATCH_NOT_EQUAL]     = "\"?/=\"",
1181
      [W_OP_MATCH_LESS_THAN]     = "\"?<\"",
1182
      [W_OP_MATCH_LESS_EQUAL]    = "\"?<=\"",
1183
      [W_OP_MATCH_GREATER_THAN]  = "\"?>\"",
1184
      [W_OP_MATCH_GREATER_EQUAL] = "\"?>=\"",
1185
      [W_OP_SLL]                 = "\"sll\"",
1186
      [W_OP_SRL]                 = "\"srl\"",
1187
      [W_OP_SLA]                 = "\"sla\"",
1188
      [W_OP_SRA]                 = "\"sra\"",
1189
      [W_OP_ROL]                 = "\"rol\"",
1190
      [W_OP_ROR]                 = "\"ror\"",
1191
      [W_OP_ADD]                 = "\"+\"",
1192
      [W_OP_MINUS]               = "\"-\"",
1193
      [W_OP_CONCAT]              = "\"&\"",
1194
      [W_OP_TIMES]               = "\"*\"",
1195
      [W_OP_DIVIDE]              = "\"/\"",
1196
      [W_OP_MOD]                 = "\"mod\"",
1197
      [W_OP_REM]                 = "\"rem\"",
1198
      [W_OP_EXPONENT]            = "\"**\"",
1199
      [W_OP_ABS]                 = "\"abs\"",
1200
      [W_OP_NOT]                 = "\"not\"",
1201
   };
1202

1203
   for (int i = 0; i < ARRAY_LEN(tab); i++)
524,032✔
1204
      id_cache[i] = ident_intern(i, tab[i]);
518,336✔
1205
}
5,696✔
1206

1207
bool is_uninstantiated_package(tree_t pack)
33,973✔
1208
{
1209
   return tree_kind(pack) == T_PACKAGE
33,973✔
1210
      && tree_generics(pack) > 0
32,545✔
1211
      && tree_genmaps(pack) == 0;
35,739✔
1212
}
1213

1214
bool is_uninstantiated_subprogram(tree_t decl)
104,670✔
1215
{
1216
   switch (tree_kind(decl)) {
104,670✔
1217
   case T_FUNC_DECL:
104,133✔
1218
   case T_FUNC_BODY:
1219
   case T_PROC_DECL:
1220
   case T_PROC_BODY:
1221
      return tree_generics(decl) > 0;
104,133✔
1222
   default:
1223
      return false;
1224
   }
1225
}
1226

1227
bool is_anonymous_subtype(type_t type)
214,273✔
1228
{
1229
   return type_kind(type) == T_SUBTYPE && !type_has_ident(type);
214,273✔
1230
}
1231

1232
bool unit_needs_cgen(tree_t unit)
202✔
1233
{
1234
   switch (tree_kind(unit)) {
202✔
1235
   case T_PACK_INST:
1236
      return true;
1237
   case T_PACK_BODY:
×
1238
      {
1239
         tree_t pack = tree_primary(unit);
×
1240
         return package_needs_body(pack) && !is_uninstantiated_package(pack);
×
1241
      }
1242
   case T_PACKAGE:
12✔
1243
      return !package_needs_body(unit) && !is_uninstantiated_package(unit);
24✔
1244
   default:
×
1245
      return false;
×
1246
   }
1247
}
1248

1249
bool package_needs_body(tree_t pack)
9,815✔
1250
{
1251
   assert(tree_kind(pack) == T_PACKAGE);
9,815✔
1252

1253
   const int ndecls = tree_decls(pack);
9,815✔
1254
   for (int i = 0; i < ndecls; i++) {
673,965✔
1255
      tree_t d = tree_decl(pack, i);
672,883✔
1256
      const tree_kind_t dkind = tree_kind(d);
672,883✔
1257
      if ((dkind == T_FUNC_DECL || dkind == T_PROC_DECL)
672,883✔
1258
          && !(tree_flags(d) & TREE_F_PREDEFINED))
602,639✔
1259
         return true;
1260
      else if (dkind == T_CONST_DECL && !tree_has_value(d))
664,619✔
1261
         return true;
1262
      else if (dkind == T_PROT_DECL)
664,256✔
1263
         return true;
1264
   }
1265

1266
   return false;
1267
}
1268

1269
static tree_t cached_unit(tree_t hint, tree_t *cache, well_known_t lib_name,
15,203✔
1270
                          well_known_t unit_name)
1271
{
1272
   const vhdl_standard_t curr = standard();
15,203✔
1273

1274
   if (cache[curr] == NULL) {
15,203✔
1275
      if (hint != NULL)
4,633✔
1276
         cache[curr] = hint;
1,475✔
1277
      else {
1278
         lib_t std = lib_require(well_known(lib_name));
3,158✔
1279
         cache[curr] = lib_get(std, well_known(unit_name));
3,158✔
1280
         assert(cache[curr] != NULL);
3,158✔
1281
      }
1282
   }
1283

1284
   assert(hint == NULL || hint == cache[curr]);
15,203✔
1285
   return cache[curr];
15,203✔
1286
}
1287

1288
static tree_t cached_std(tree_t hint)
14,277✔
1289
{
1290
   static tree_t standard_cache[STD_19 + 1] = {};
14,277✔
1291
   return cached_unit(hint, standard_cache, W_STD, W_STD_STANDARD);
14,277✔
1292
}
1293

1294
static tree_t search_type_decls(tree_t container, ident_t name)
14,823✔
1295
{
1296
   const int ndecls = tree_decls(container);
14,823✔
1297

1298
   for (int i = 0; i < ndecls; i++) {
771,417✔
1299
      tree_t d = tree_decl(container, i);
771,417✔
1300
      if (is_type_decl(d) && tree_ident(d) == name)
771,417✔
1301
         return d;
14,823✔
1302
   }
1303

1304
   return NULL;
1305
}
1306

1307
type_t std_type(tree_t std, std_type_t which)
958,294✔
1308
{
1309
   static type_t cache[STD_FILE_OPEN_STATE + 1] = {};
958,294✔
1310
   assert(which < ARRAY_LEN(cache));
958,294✔
1311

1312
   if (cache[which] == NULL) {
958,294✔
1313
      const char *names[] = {
14,277✔
1314
         "universal_integer",
1315
         "universal_real",
1316
         "INTEGER",
1317
         "REAL",
1318
         "BOOLEAN",
1319
         "STRING",
1320
         "TIME",
1321
         "BIT",
1322
         "FILE_OPEN_KIND",
1323
         "FILE_OPEN_STATUS",
1324
         "NATURAL",
1325
         "BIT_VECTOR",
1326
         "SEVERITY_LEVEL",
1327
         "FILE_ORIGIN_KIND",
1328
         "FILE_OPEN_STATE",
1329
      };
1330

1331
      tree_t d = search_type_decls(cached_std(std), ident_new(names[which]));
14,277✔
1332
      if (d == NULL)
14,277✔
1333
         fatal_trace("cannot find standard type %s", names[which]);
1334

1335
      // Do not cache standard types while bootstrapping as the GC will
1336
      // move the objects after parsing
1337
      static int can_cache = -1;
14,277✔
1338
      if (can_cache == -1) can_cache = !opt_get_int(OPT_BOOTSTRAP);
14,277✔
1339

1340
      if (can_cache)
14,277✔
1341
         return (cache[which] = tree_type(d));
14,170✔
1342
      else
1343
         return tree_type(d);
107✔
1344
   }
1345
   else
1346
      return cache[which];
1347
}
1348

1349
type_t ieee_type(ieee_type_t which)
1,922✔
1350
{
1351
   static type_t cache[IEEE_STD_LOGIC_VECTOR + 1] = {};
1,922✔
1352
   assert(which < ARRAY_LEN(cache));
1,922✔
1353

1354
   if (cache[which] == NULL) {
1,922✔
1355
      static const char *const names[] = {
254✔
1356
         [IEEE_STD_ULOGIC] = "STD_ULOGIC",
1357
         [IEEE_STD_LOGIC] = "STD_LOGIC",
1358
         [IEEE_STD_ULOGIC_VECTOR] = "STD_ULOGIC_VECTOR",
1359
         [IEEE_STD_LOGIC_VECTOR] = "STD_LOGIC_VECTOR",
1360
      };
1361

1362
      static tree_t ieee_cache[STD_19 + 1] = {};
254✔
1363
      tree_t unit = cached_unit(NULL, ieee_cache, W_IEEE, W_IEEE_1164);
254✔
1364

1365
      tree_t d = search_type_decls(unit, ident_new(names[which]));
254✔
1366
      if (d == NULL)
254✔
1367
         fatal_trace("cannot find IEEE type %s", names[which]);
1368

1369
      // STD.STANDARD cannot depend on IEEE
1370
      assert(!opt_get_int(OPT_BOOTSTRAP));
254✔
1371

1372
      return (cache[which] = tree_type(d));
254✔
1373
   }
1374
   else
1375
      return cache[which];
1376
}
1377

1378
static tree_t cached_verilog(void)
640✔
1379
{
1380
   static tree_t verilog_cache[STD_19 + 1] = {};
640✔
1381
   return cached_unit(NULL, verilog_cache, W_NVC, W_NVC_VERILOG);
640✔
1382
}
1383

1384
type_t verilog_type(verilog_type_t which)
871✔
1385
{
1386
   static type_t cache[VERILOG_WIRE_ARRAY + 1] = {};
871✔
1387
   assert(which < ARRAY_LEN(cache));
871✔
1388

1389
   if (cache[which] == NULL) {
871✔
1390
      static const char *const names[] = {
260✔
1391
         [VERILOG_LOGIC] = "T_LOGIC",
1392
         [VERILOG_LOGIC_ARRAY] = "T_LOGIC_ARRAY",
1393
         [VERILOG_INT64] = "T_INT64",
1394
         [VERILOG_NET_VALUE] = "T_NET_VALUE",
1395
         [VERILOG_NET_ARRAY] = "T_NET_ARRAY",
1396
         [VERILOG_WIRE] = "T_WIRE",
1397
         [VERILOG_WIRE_ARRAY] = "T_WIRE_ARRAY",
1398
      };
1399

1400
      tree_t d = search_type_decls(cached_verilog(), ident_new(names[which]));
260✔
1401
      if (d == NULL)
260✔
1402
         fatal_trace("cannot find NVC.VERILOG type %s", names[which]);
1403

1404
      // STD.STANDARD cannot depend on NVC.VERILOG
1405
      assert(!opt_get_int(OPT_BOOTSTRAP));
260✔
1406

1407
      return (cache[which] = tree_type(d));
260✔
1408
   }
1409
   else
1410
      return cache[which];
1411
}
1412

1413
type_t reflection_type(reflect_type_t which)
189✔
1414
{
1415
   static type_t cache[REFLECT_SUBTYPE_MIRROR + 1] = {};
189✔
1416
   assert(which < ARRAY_LEN(cache));
189✔
1417

1418
   if (cache[which] == NULL) {
189✔
1419
      static const char *const names[] = {
32✔
1420
         [REFLECT_VALUE_MIRROR] = "VALUE_MIRROR",
1421
         [REFLECT_SUBTYPE_MIRROR] = "SUBTYPE_MIRROR",
1422
      };
1423

1424
      static tree_t reflect_cache[STD_19 + 1] = {};
32✔
1425
      tree_t unit = cached_unit(NULL, reflect_cache, W_STD, W_STD_REFLECTION);
32✔
1426

1427
      tree_t d = search_type_decls(unit, ident_new(names[which]));
32✔
1428
      if (d == NULL)
32✔
1429
         fatal_trace("cannot find REFLECTION type %s", names[which]);
1430

1431
      // STD.STANDARD cannot depend on REFLECTION
1432
      assert(!opt_get_int(OPT_BOOTSTRAP));
32✔
1433

1434
      return (cache[which] = tree_type(d));
32✔
1435
   }
1436
   else
1437
      return cache[which];
1438
}
1439

1440
bool is_open_coded_builtin(subprogram_kind_t kind)
1,234,929✔
1441
{
1442
   switch (kind) {
1,234,929✔
1443
   case S_ADD:
1444
   case S_SUB:
1445
   case S_DIV:
1446
   case S_MUL:
1447
   case S_MUL_PR:
1448
   case S_MUL_RP:
1449
   case S_MUL_PI:
1450
   case S_MUL_IP:
1451
   case S_DIV_PR:
1452
   case S_DIV_PP:
1453
   case S_DIV_PI:
1454
   case S_IDENTITY:
1455
   case S_NEGATE:
1456
   case S_SCALAR_LT:
1457
   case S_SCALAR_LE:
1458
   case S_SCALAR_GT:
1459
   case S_SCALAR_GE:
1460
   case S_SCALAR_EQ:
1461
   case S_SCALAR_NEQ:
1462
   case S_ABS:
1463
   case S_MOD:
1464
   case S_REM:
1465
   case S_EXP:
1466
   case S_MUL_RI:
1467
   case S_MUL_IR:
1468
   case S_DIV_RI:
1469
   case S_CONCAT:
1470
   case S_SCALAR_AND:
1471
   case S_SCALAR_OR:
1472
   case S_SCALAR_NOT:
1473
   case S_SCALAR_NAND:
1474
   case S_SCALAR_NOR:
1475
   case S_SCALAR_XOR:
1476
   case S_SCALAR_XNOR:
1477
   case S_FILE_OPEN1:
1478
   case S_FILE_OPEN2:
1479
   case S_FILE_READ:
1480
   case S_FILE_WRITE:
1481
   case S_DEALLOCATE:
1482
   case S_IEEE_AND:
1483
   case S_IEEE_OR:
1484
   case S_IEEE_XOR:
1485
   case S_IEEE_NAND:
1486
   case S_IEEE_NOR:
1487
   case S_IEEE_XNOR:
1488
   case S_IEEE_NOT:
1489
      return true;
1490
   default:
502,041✔
1491
      return false;
502,041✔
1492
   }
1493
}
1494

1495
tree_t std_func(ident_t mangled)
×
1496
{
UNCOV
1497
   tree_t std = cached_std(NULL);
×
1498

UNCOV
1499
   const int ndecls = tree_decls(std);
×
UNCOV
1500
   for (int i = 0; i < ndecls; i++) {
×
UNCOV
1501
      tree_t d = tree_decl(std, i);
×
UNCOV
1502
      if (is_subprogram(d) && tree_has_ident2(d) && tree_ident2(d) == mangled)
×
UNCOV
1503
         return d;
×
1504
   }
1505

1506
   return NULL;
1507
}
1508

1509
tree_t verilog_func(ident_t mangled)
380✔
1510
{
1511
   tree_t pack = cached_verilog();
380✔
1512

1513
   const int ndecls = tree_decls(pack);
380✔
1514
   for (int i = 0; i < ndecls; i++) {
40,642✔
1515
      tree_t d = tree_decl(pack, i);
40,642✔
1516
      if (is_subprogram(d) && tree_ident2(d) == mangled)
40,642✔
1517
         return d;
380✔
1518
   }
1519

1520
   fatal_trace("missing Verilog helper function %s", istr(mangled));
1521
}
1522

1523
tree_t name_to_ref(tree_t name)
95,798✔
1524
{
1525
   tree_kind_t kind;
95,798✔
1526
   while ((kind = tree_kind(name)) != T_REF) {
107,244✔
1527
      switch (kind) {
12,803✔
1528
      case T_ARRAY_REF:
11,446✔
1529
      case T_ARRAY_SLICE:
1530
      case T_RECORD_REF:
1531
      case T_ALL:
1532
         name = tree_value(name);
11,446✔
1533
         break;
11,446✔
1534
      default:
1535
         return NULL;
1536
      }
1537
   }
1538

1539
   return name;
1540
}
1541

1542
const char *port_mode_str(port_mode_t mode)
47✔
1543
{
1544
   const char *mode_str[] = {
47✔
1545
      "INVALID", "IN", "OUT", "INOUT", "BUFFER", "LINKAGE", "VIEW", "VIEW"
1546
   };
1547
   assert(mode < ARRAY_LEN(mode_str));
47✔
1548
   return mode_str[mode];
47✔
1549
}
1550

1551
void mangle_one_type(text_buf_t *buf, type_t type)
181,841✔
1552
{
1553
   ident_t ident = type_ident(type);
181,841✔
1554

1555
   char code = 0;
181,841✔
1556
   switch (is_well_known(ident)) {
181,841✔
1557
   case W_STD_INTEGER:        code = 'I'; break;
1558
   case W_STD_STRING:         code = 'S'; break;
3,803✔
1559
   case W_STD_REAL:           code = 'R'; break;
3,555✔
1560
   case W_STD_BOOL:           code = 'B'; break;
24,703✔
1561
   case W_STD_CHAR:           code = 'C'; break;
678✔
1562
   case W_STD_TIME:           code = 'T'; break;
1,116✔
1563
   case W_STD_NATURAL:        code = 'N'; break;
4,282✔
1564
   case W_STD_POSITIVE:       code = 'P'; break;
375✔
1565
   case W_STD_BIT:            code = 'J'; break;
2,694✔
1566
   case W_STD_BIT_VECTOR:     code = 'Q'; break;
2,996✔
1567
   case W_IEEE_LOGIC:         code = 'L'; break;
381✔
1568
   case W_IEEE_ULOGIC:        code = 'U'; break;
5,289✔
1569
   case W_IEEE_LOGIC_VECTOR:  code = 'V'; break;
2,194✔
1570
   case W_IEEE_ULOGIC_VECTOR: code = 'Y'; break;
1,452✔
1571
   default: break;
1572
   }
1573

1574
   if (code)
53,518✔
1575
      tb_append(buf, code);
66,541✔
1576
   else {
1577
      tb_printf(buf, "%zu", ident_len(ident));
115,300✔
1578
      tb_istr(buf, ident);
115,300✔
1579
   }
1580
}
181,841✔
1581

1582
ident_t get_call_context(ident_t mangled)
2,627✔
1583
{
1584
   const char *str = istr(mangled), *p = str, *end = NULL;
2,627✔
1585
   for (; *p; p++) {
95,103✔
1586
      if (*p == '(') break;
90,686✔
1587
      if (*p == '.') end = p;
89,849✔
1588
   }
1589
   assert(end != NULL);
2,627✔
1590

1591
   return ident_new_n(str, end - str);
2,627✔
1592
}
1593

1594
tree_t primary_unit_of(tree_t unit)
30,087✔
1595
{
1596
   switch (tree_kind(unit)) {
30,087✔
1597
   case T_ENTITY:
1598
   case T_COMPONENT:
1599
   case T_PACKAGE:
1600
   case T_BLOCK:
1601
   case T_ELAB:
1602
   case T_PACK_INST:
1603
      return unit;
1604
   case T_ARCH:
18,339✔
1605
   case T_CONFIGURATION:
1606
   case T_PACK_BODY:
1607
      return tree_primary(unit);
18,339✔
UNCOV
1608
   default:
×
1609
      fatal_trace("invalid kind %s in primary_unit_of",
1610
                  tree_kind_str(tree_kind(unit)));
1611
   }
1612
}
1613

1614
unsigned get_case_choice_char(tree_t value, int depth)
11,914✔
1615
{
1616
   switch (tree_kind(value)) {
12,533✔
1617
   case T_STRING:
11,892✔
1618
      if (depth < tree_chars(value))
11,892✔
1619
         return assume_int(tree_char(value, depth));
11,471✔
1620
      else
1621
         return ~0;   // Out of bounds
1622

1623
   case T_AGGREGATE:
22✔
1624
      {
1625
         const int nassocs = tree_assocs(value);
22✔
1626
         type_t type = tree_type(value);
22✔
1627

1628
         for (int i = 0, pos = 0; i < nassocs; i++) {
36✔
1629
            tree_t a = tree_assoc(value, i);
35✔
1630
            switch (tree_subkind(a)) {
35✔
UNCOV
1631
            case A_NAMED:
×
UNCOV
1632
               if (rebase_index(type, 0, assume_int(tree_name(a))) == depth)
×
UNCOV
1633
                  return assume_int(tree_value(a));
×
1634
               break;
1635

1636
            case A_POS:
11✔
1637
               if (pos++ == (unsigned)depth)
11✔
1638
                  return assume_int(tree_value(a));
5✔
1639
               break;
1640

1641
            case A_CONCAT:
24✔
1642
               {
1643
                  tree_t left = tree_value(a);
24✔
1644

1645
                  type_t left_type = tree_type(left);
24✔
1646
                  if (type_is_unconstrained(left_type))
24✔
UNCOV
1647
                     fatal_at(tree_loc(left), "sorry, this expression is not "
×
1648
                              "currently supported in a case choice");
1649

1650
                  tree_t lr = range_of(tree_type(left), 0);
24✔
1651
                  int64_t left_len;
24✔
1652
                  if (!folded_length(lr, &left_len))
24✔
UNCOV
1653
                     fatal_at(tree_loc(left), "cannot determine length of "
×
1654
                              "aggregate element");
1655

1656
                  if (depth < pos + left_len)
24✔
1657
                     return get_case_choice_char(left, depth - pos);
16✔
1658

1659
                  pos += left_len;
8✔
1660
               }
1661
               break;
8✔
1662

UNCOV
1663
            case A_OTHERS:
×
1664
               return assume_int(tree_value(a));
×
1665

UNCOV
1666
            case A_SLICE:
×
1667
               {
1668
                  tree_t base = tree_value(a);
×
UNCOV
1669
                  tree_t r = tree_range(a, 0);
×
1670

1671
                  const int64_t rleft = assume_int(tree_left(r));
×
UNCOV
1672
                  const int64_t rright = assume_int(tree_right(r));
×
1673

UNCOV
1674
                  const int64_t loffset = rebase_index(type, 0, rleft);
×
UNCOV
1675
                  const int64_t roffset = rebase_index(type, 0, rright);
×
1676

UNCOV
1677
                  if (depth >= loffset && depth <= roffset)
×
UNCOV
1678
                     return get_case_choice_char(base, depth - loffset);
×
1679
               }
1680
            }
1681
         }
1682

1683
         // This will produce an error during bounds checking
1684
         return ~0;
1685
      }
1686

1687
   case T_REF:
379✔
1688
      {
1689
         tree_t decl = tree_ref(value);
379✔
1690
         assert(tree_kind(decl) == T_CONST_DECL || tree_kind(decl) == T_ALIAS);
379✔
1691
         assert(tree_has_value(decl));
379✔
1692
         return get_case_choice_char(tree_value(decl), depth);
379✔
1693
      }
1694

1695
   case T_ARRAY_SLICE:
240✔
1696
      {
1697
         tree_t base = tree_value(value);
240✔
1698
         tree_t r = tree_range(value, 0);
240✔
1699
         const int64_t rleft = assume_int(tree_left(r));
240✔
1700
         const int64_t offset = rebase_index(tree_type(base), 0, rleft);
240✔
1701
         return get_case_choice_char(base, depth + offset);
240✔
1702
      }
1703

UNCOV
1704
   default:
×
UNCOV
1705
      fatal_at(tree_loc(value), "unsupported tree type %s in case choice",
×
1706
               tree_kind_str(tree_kind(value)));
1707
   }
1708
}
1709

1710
int64_t encode_case_choice(tree_t value, int length, int bits)
1,630✔
1711
{
1712
   uint64_t enc = 0;
1,630✔
1713
   for (int i = 0; i < length; i++) {
13,506✔
1714
      if (bits > 0) {
11,876✔
1715
         enc <<= bits;
4,343✔
1716
         enc |= get_case_choice_char(value, i);
4,343✔
1717
      }
1718
      else {
1719
         enc *= 0x27d4eb2d;
7,533✔
1720
         enc += get_case_choice_char(value, i);
7,533✔
1721
      }
1722
   }
1723

1724
   return enc;
1,630✔
1725
}
1726

1727
void to_string(text_buf_t *tb, type_t type, int64_t value)
587✔
1728
{
1729
   if (type_is_integer(type))
587✔
1730
      tb_printf(tb, "%"PRIi64, value);
444✔
1731
   else if (type_is_enum(type)) {
143✔
1732
      type_t base = type_base_recur(type);
62✔
1733
      if (value < 0 || value >= type_enum_literals(base))
62✔
1734
         tb_printf(tb, "%"PRIi64, value);
3✔
1735
      else
1736
         tb_cat(tb, istr(tree_ident(type_enum_literal(base, value))));
59✔
1737
   }
1738
   else if (type_is_physical(type)) {
81✔
1739
      type_t base = type_base_recur(type);
24✔
1740
      const unsigned nunits = type_units(base);
24✔
1741
      tree_t max_unit = NULL;
24✔
1742
      int64_t max_unit_value = 0;
24✔
1743

1744
      // Find the largest unit that evenly divides the given value
1745
      for (unsigned u = 0; u < nunits; u++) {
216✔
1746
         tree_t unit = type_unit(base, u);
192✔
1747
         const int64_t unit_value = assume_int(tree_value(unit));
192✔
1748
         if ((unit_value > max_unit_value) && (value % unit_value == 0)) {
192✔
1749
            max_unit = unit;
90✔
1750
            max_unit_value = unit_value;
90✔
1751
         }
1752
      }
1753
      assert(max_unit);
24✔
1754

1755
      tb_printf(tb, "%"PRIi64" %s", value / max_unit_value,
24✔
1756
                istr(tree_ident(max_unit)));
1757
   }
1758
   else if (type_is_real(type)) {
57✔
1759
      union { int64_t i; double r; } u = { .i = value };
51✔
1760
      tb_printf(tb, "%.17g", u.r);
51✔
1761
   }
1762
   else if (type_is_access(type)) {
6✔
1763
      if (value == 0)
6✔
1764
         tb_cat(tb, "NULL");
3✔
1765
      else
1766
         tb_printf(tb, "%p", (void *)value);
3✔
1767
   }
1768
}
587✔
1769

1770
static bool is_static(tree_t expr)
3,779✔
1771
{
1772
   switch (tree_kind(expr)) {
3,883✔
1773
   case T_REF:
1,114✔
1774
      {
1775
         tree_t decl = tree_ref(expr);
1,114✔
1776
         switch (tree_kind(decl)) {
1,114✔
1777
         case T_CONST_DECL:
143✔
1778
            return !!(tree_flags(decl) & TREE_F_GLOBALLY_STATIC);
143✔
1779
         case T_UNIT_DECL:
1780
         case T_ENUM_LIT:
1781
         case T_GENERIC_DECL:
1782
            return true;
UNCOV
1783
         case T_ALIAS:
×
UNCOV
1784
            return is_static(tree_value(decl));
×
1785
         default:
197✔
1786
            return false;
197✔
1787
         }
1788
      }
1789

1790
   case T_LITERAL:
1791
   case T_STRING:
1792
      return true;
1793

1794
   case T_FCALL:
650✔
1795
      return !!(tree_flags(expr) & (TREE_F_LOCALLY_STATIC
650✔
1796
                                    | TREE_F_GLOBALLY_STATIC));
1797

1798
   case T_RECORD_REF:
101✔
1799
      return is_static(tree_value(expr));
101✔
1800

1801
   case T_ARRAY_REF:
24✔
1802
      {
1803
         if (!is_static(tree_value(expr)))
24✔
1804
            return false;
1805

1806
         const int nparams = tree_params(expr);
24✔
1807
         for (int i = 0; i < nparams; i++) {
48✔
1808
            if (!is_static(tree_value(tree_param(expr, i))))
24✔
1809
               return false;
1810
         }
1811

1812
         return true;
1813
      }
1814

1815
   case T_ARRAY_SLICE:
12✔
1816
      {
1817
         if (!is_static(tree_value(expr)))
12✔
1818
            return false;
1819

1820
         assert(tree_ranges(expr) == 1);
12✔
1821

1822
         tree_t r = tree_range(expr, 0);
12✔
1823
         if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
12✔
UNCOV
1824
            return false;
×
1825

1826
         return true;
1827
      }
1828

1829
   case T_ATTR_REF:
19✔
1830
      {
1831
         switch (tree_subkind(expr)) {
19✔
1832
         case ATTR_EVENT:
1833
         case ATTR_ACTIVE:
1834
         case ATTR_LAST_EVENT:
1835
         case ATTR_LAST_ACTIVE:
1836
         case ATTR_LAST_VALUE:
1837
         case ATTR_DRIVING:
1838
         case ATTR_DRIVING_VALUE:
1839
         case ATTR_STABLE:
1840
         case ATTR_QUIET:
1841
            return false;
1842
         case ATTR_POS:
3✔
1843
         case ATTR_VAL:
1844
         case ATTR_LEFTOF:
1845
         case ATTR_RIGHTOF:
1846
         case ATTR_SUCC:
1847
         case ATTR_PRED:
1848
         case ATTR_VALUE:
1849
         case ATTR_IMAGE:
1850
            assert(tree_params(expr) == 1);
3✔
1851
            return is_static(tree_value(tree_param(expr, 0)));
3✔
1852
         case ATTR_LENGTH:
16✔
1853
         case ATTR_LEFT:
1854
         case ATTR_RIGHT:
1855
         case ATTR_LOW:
1856
         case ATTR_HIGH:
1857
            {
1858
               tree_t ref = name_to_ref(tree_name(expr));
16✔
1859
               if (ref == NULL)
16✔
1860
                  return false;
1861

1862
               switch (tree_kind(tree_ref(ref))) {
16✔
1863
               case T_GENERIC_DECL:
1864
               case T_PORT_DECL:
1865
               case T_SIGNAL_DECL:
1866
                  return true;
1867
               default:
3✔
1868
                  return false;
3✔
1869
               }
1870
            }
UNCOV
1871
         default:
×
UNCOV
1872
            return true;
×
1873
         }
1874
      }
1875

UNCOV
1876
   default:
×
UNCOV
1877
      return false;
×
1878
   }
1879
}
1880

1881
tree_t longest_static_prefix(tree_t expr)
18,468✔
1882
{
1883
   switch (tree_kind(expr)) {
18,468✔
1884
   case T_ARRAY_REF:
2,623✔
1885
      {
1886
         tree_t value = tree_value(expr);
2,623✔
1887
         tree_t prefix = longest_static_prefix(value);
2,623✔
1888

1889
         if (prefix != value)
2,623✔
1890
            return prefix;
1891

1892
         const int nparams = tree_params(expr);
2,595✔
1893
         for (int i = 0; i < nparams; i++) {
5,179✔
1894
            if (!is_static(tree_value(tree_param(expr, i))))
2,832✔
1895
               return prefix;
1896
         }
1897

1898
         return expr;
1899
      }
1900

1901
   case T_ARRAY_SLICE:
451✔
1902
      {
1903
         tree_t value = tree_value(expr);
451✔
1904
         tree_t prefix = longest_static_prefix(value);
451✔
1905

1906
         if (prefix != value)
451✔
1907
            return prefix;
1908

1909
         assert(tree_ranges(expr) == 1);
444✔
1910

1911
         tree_t r = tree_range(expr, 0);
444✔
1912
         if (tree_subkind(r) == RANGE_EXPR)
444✔
1913
            return prefix;
1914
         else if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
438✔
1915
            return prefix;
16✔
1916

1917
         return expr;
1918
      }
1919

1920
   case T_RECORD_REF:
1,038✔
1921
      {
1922
         tree_t value = tree_value(expr);
1,038✔
1923
         tree_t prefix = longest_static_prefix(value);
1,038✔
1924

1925
         if (prefix != value)
1,038✔
1926
            return prefix;
21✔
1927

1928
         return expr;
1929
      }
1930

1931
   default:
1932
      return expr;
1933
   }
1934
}
1935

1936
tree_t body_of(tree_t pack)
8,959✔
1937
{
1938
   const tree_kind_t kind = tree_kind(pack);
8,959✔
1939
   if (kind == T_PACK_INST)
8,959✔
1940
      return NULL;
1941

1942
   assert(tree_kind(pack) == T_PACKAGE);
8,959✔
1943

1944
   ident_t body_i = well_known(W_BODY);
8,959✔
1945
   ident_t body_name = ident_prefix(tree_ident(pack), body_i, '-');
8,959✔
1946
   return lib_get_qualified(body_name);
8,959✔
1947
}
1948

1949
tree_t find_generic_map(tree_t unit, int pos, tree_t g)
651✔
1950
{
1951
   const int ngenmaps = tree_genmaps(unit);
651✔
1952

1953
   if (pos < ngenmaps) {
651✔
1954
      tree_t m = tree_genmap(unit, pos);
513✔
1955
      if (tree_subkind(m) == P_POS && tree_pos(m) == pos)
513✔
1956
         return tree_value(m);
367✔
1957
   }
1958

1959
   for (int j = 0; j < ngenmaps; j++) {
998✔
1960
      tree_t m = tree_genmap(unit, j);
860✔
1961
      switch (tree_subkind(m)) {
860✔
1962
      case P_NAMED:
440✔
1963
         {
1964
            tree_t name = tree_name(m);
440✔
1965
            assert(tree_kind(name) == T_REF);
440✔
1966

1967
            if (tree_has_ref(name) && tree_ref(name) == g)
440✔
1968
               return tree_value(m);
15✔
1969
         }
1970
         break;
1971

1972
      case P_POS:
420✔
1973
         if (tree_pos(m) == pos)
420✔
1974
            return tree_value(m);
131✔
1975
         break;
1976

1977
      default:
1978
         break;
1979
      }
1980
   }
1981

1982
   return NULL;
1983
}
1984

1985
bool relaxed_rules(void)
544,573✔
1986
{
1987
   return opt_get_int(OPT_RELAXED);
544,573✔
1988
}
1989

1990
bool is_type_attribute(attr_kind_t kind)
72,578✔
1991
{
1992
   switch (kind) {
72,578✔
1993
   case ATTR_SUBTYPE:
1994
   case ATTR_BASE:
1995
   case ATTR_ELEMENT:
1996
   case ATTR_DESIGNATED_SUBTYPE:
1997
   case ATTR_INDEX:
1998
      return true;
1999
   default:
71,822✔
2000
      return false;
71,822✔
2001
   }
2002
}
2003

2004
bool attribute_has_param(attr_kind_t attr)
24,144✔
2005
{
2006
   switch (attr) {
24,144✔
2007
   case ATTR_IMAGE:
2008
   case ATTR_SUCC:
2009
   case ATTR_PRED:
2010
   case ATTR_DELAYED:
2011
   case ATTR_LEFTOF:
2012
   case ATTR_RIGHTOF:
2013
   case ATTR_VALUE:
2014
   case ATTR_POS:
2015
   case ATTR_LOW:
2016
   case ATTR_HIGH:
2017
   case ATTR_LEFT:
2018
   case ATTR_RIGHT:
2019
   case ATTR_LENGTH:
2020
   case ATTR_RANGE:
2021
   case ATTR_REVERSE_RANGE:
2022
   case ATTR_VAL:
2023
   case ATTR_QUIET:
2024
   case ATTR_STABLE:
2025
   case ATTR_INDEX:
2026
   case ATTR_ASCENDING:
2027
      return true;
2028
   default:
2,327✔
2029
      return false;
2,327✔
2030
   }
2031
}
2032

2033
type_t get_type_or_null(tree_t t)
2,287,823✔
2034
{
2035
   switch (tree_kind(t)) {
2,287,823✔
2036
   case T_LIBRARY:
2037
   case T_ATTR_SPEC:
2038
   case T_PACKAGE:
2039
   case T_PACK_INST:
2040
   case T_PACK_BODY:
2041
   case T_ENTITY:
2042
   case T_ARCH:
2043
   case T_PROCESS:
2044
   case T_COMPONENT:
2045
   case T_INSTANCE:
2046
   case T_CONCURRENT:
2047
   case T_BLOCK:
2048
   case T_WHILE:
2049
   case T_FOR:
2050
   case T_LOOP:
2051
   case T_GROUP_TEMPLATE:
2052
   case T_CONFIGURATION:
2053
   case T_GROUP:
2054
   case T_FOR_GENERATE:
2055
   case T_IF_GENERATE:
2056
   case T_CASE_GENERATE:
2057
   case T_USE:
2058
   case T_CONTEXT:
2059
   case T_PSL_DECL:
2060
   case T_PSL_DIRECT:
2061
   case T_WAVEFORM:
2062
      return NULL;
2063
   default:
2,194,813✔
2064
      if (tree_has_type(t))
2,194,813✔
2065
         return tree_type(t);
2,193,339✔
2066
      else
2067
         return NULL;
2068
   }
2069
}
2070

2071
type_t subtype_for_string(tree_t str, type_t base)
26,142✔
2072
{
2073
   if (type_const_bounds(base))
26,142✔
2074
      return base;    // Can be checked statically
2075
   else if (!type_is_unconstrained(base))
22,886✔
2076
      base = type_base_recur(base);
272✔
2077

2078
   // Construct a new constrained array subtype: the direction and
2079
   // bounds are the same as those for a positional array aggregate
2080
   type_t sub = type_new(T_SUBTYPE);
22,886✔
2081
   type_set_base(sub, base);
22,886✔
2082

2083
   type_t index_type = index_type_of(base, 0);
22,886✔
2084
   const bool is_enum = type_is_enum(index_type);
22,886✔
2085

2086
   // The direction is determined by the index type
2087
   range_kind_t dir = direction_of(index_type, 0);
22,886✔
2088
   tree_t index_r = range_of(index_type, 0);
22,886✔
2089

2090
   // The left bound is the left of the index type and the right bound
2091
   // is determined by the number of elements
2092

2093
   tree_t left = NULL, right = NULL;
22,886✔
2094
   const int nchars = tree_chars(str);
22,886✔
2095

2096
   if (is_enum) {
22,886✔
2097
      const int nlits = type_enum_literals(type_base_recur(index_type));
16✔
2098
      int64_t index_left = assume_int(tree_left(index_r));
16✔
2099

2100
      int64_t iright, ileft;
16✔
2101
      if (nchars == 0) {
16✔
2102
         iright = index_left;
1✔
2103
         ileft = assume_int(tree_right(index_r));
1✔
2104
      }
2105
      else if (dir == RANGE_DOWNTO) {
15✔
UNCOV
2106
         ileft = index_left;
×
UNCOV
2107
         iright = MIN(nlits - 1, MAX(0, index_left - nchars + 1));
×
2108
      }
2109
      else {
2110
         ileft = index_left;
15✔
2111
         iright = MIN(nlits - 1, MAX(0, index_left + nchars - 1));
15✔
2112
      }
2113

2114
      left = get_enum_lit(str, index_type, ileft);
16✔
2115
      right = get_enum_lit(str, index_type, iright);
16✔
2116
   }
2117
   else {
2118
      left = tree_left(index_r);
22,870✔
2119

2120
      int64_t iright;
22,870✔
2121
      if (dir == RANGE_DOWNTO)
22,870✔
UNCOV
2122
         iright = assume_int(left) - nchars + 1;
×
2123
      else
2124
         iright = assume_int(left) + nchars - 1;
22,870✔
2125

2126
      right = get_int_lit(str, index_type, iright);
22,870✔
2127
   }
2128

2129
   tree_t r = tree_new(T_RANGE);
22,886✔
2130
   tree_set_subkind(r, dir);
22,886✔
2131
   tree_set_left(r, left);
22,886✔
2132
   tree_set_right(r, right);
22,886✔
2133
   tree_set_loc(r, tree_loc(str));
22,886✔
2134
   tree_set_type(r, index_type);
22,886✔
2135

2136
   tree_t c = tree_new(T_CONSTRAINT);
22,886✔
2137
   tree_set_subkind(c, C_INDEX);
22,886✔
2138
   tree_add_range(c, r);
22,886✔
2139
   tree_set_loc(c, tree_loc(str));
22,886✔
2140

2141
   type_set_constraint(sub, c);
22,886✔
2142

2143
   return sub;
22,886✔
2144
}
2145

2146
tree_t change_ref(tree_t name, tree_t new)
2,338✔
2147
{
2148
   switch (tree_kind(name)) {
2,338✔
2149
   case T_REF:
1,616✔
2150
      {
2151
         tree_t ref = make_ref(new);
1,616✔
2152
         tree_set_loc(ref, tree_loc(name));
1,616✔
2153
         return ref;
1,616✔
2154
      }
2155

2156
   case T_ARRAY_REF:
112✔
2157
      {
2158
         tree_t value = change_ref(tree_value(name), new);
112✔
2159

2160
         tree_t t = tree_new(T_ARRAY_REF);
112✔
2161
         tree_set_loc(t, tree_loc(name));
112✔
2162
         tree_set_value(t, value);
112✔
2163
         tree_set_type(t, type_elem(tree_type(value)));
112✔
2164

2165
         const int nparams = tree_params(name);
112✔
2166
         for (int i = 0; i < nparams; i++)
224✔
2167
            tree_add_param(t, tree_param(name, i));
112✔
2168

2169
         return t;
2170
      }
2171

2172
   case T_ARRAY_SLICE:
47✔
2173
      {
2174
         tree_t value = change_ref(tree_value(name), new);
47✔
2175
         tree_t r = tree_range(name, 0);
47✔
2176

2177
         tree_t constraint = tree_new(T_CONSTRAINT);
47✔
2178
         tree_set_subkind(constraint, C_INDEX);
47✔
2179
         tree_add_range(constraint, r);
47✔
2180

2181
         type_t slice_type = type_new(T_SUBTYPE);
47✔
2182
         type_set_constraint(slice_type, constraint);
47✔
2183
         type_set_base(slice_type, tree_type(value));
47✔
2184

2185
         tree_t t = tree_new(T_ARRAY_SLICE);
47✔
2186
         tree_set_loc(t, tree_loc(name));
47✔
2187
         tree_set_value(t, value);
47✔
2188
         tree_set_type(t, slice_type);
47✔
2189
         tree_add_range(t, r);
47✔
2190

2191
         return t;
47✔
2192
      }
2193

2194
   case T_RECORD_REF:
404✔
2195
      {
2196
         tree_t t = tree_new(T_RECORD_REF);
404✔
2197
         tree_set_loc(t, tree_loc(name));
404✔
2198
         tree_set_value(t, change_ref(tree_value(name), new));
404✔
2199
         tree_set_type(t, tree_type(name));
404✔
2200
         tree_set_ident(t, tree_ident(name));
404✔
2201
         tree_set_ref(t, tree_ref(name));
404✔
2202

2203
         return t;
404✔
2204
      }
2205

2206
   case T_CONV_FUNC:
150✔
2207
      {
2208
         tree_t t = tree_new(T_CONV_FUNC);
150✔
2209
         tree_set_loc(t, tree_loc(name));
150✔
2210
         tree_set_value(t, change_ref(tree_value(name), new));
150✔
2211
         tree_set_ident(t, tree_ident(name));
150✔
2212
         tree_set_type(t, tree_type(name));
150✔
2213
         tree_set_ref(t, tree_ref(name));
150✔
2214

2215
         return t;
150✔
2216
      }
2217

2218
   case T_TYPE_CONV:
9✔
2219
      {
2220
         tree_t t = tree_new(T_TYPE_CONV);
9✔
2221
         tree_set_loc(t, tree_loc(name));
9✔
2222
         tree_set_type(t, tree_type(name));
9✔
2223
         tree_set_value(t, change_ref(tree_value(name), new));
9✔
2224

2225
         return t;
9✔
2226
      }
2227

UNCOV
2228
   default:
×
2229
      fatal_trace("cannot handle tree kind %s in elab_change_ref",
2230
                  tree_kind_str(tree_kind(name)));
2231
   }
2232
}
2233

2234
static void build_wait_for_target(tree_t expr, build_wait_fn_t fn, void *ctx)
3,020✔
2235
{
2236
   switch (tree_kind(expr)) {
3,020✔
2237
   case T_ARRAY_SLICE:
81✔
2238
      build_wait(tree_range(expr, 0), fn, ctx);
81✔
2239
      break;
81✔
2240

2241
   case T_ARRAY_REF:
576✔
2242
      {
2243
         const int nparams = tree_params(expr);
576✔
2244
         for (int i = 0; i < nparams; i++)
1,152✔
2245
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
576✔
2246
      }
2247
      break;
2248

2249
   default:
2250
      break;
2251
   }
2252
}
3,020✔
2253

2254
void build_wait(tree_t expr, build_wait_fn_t fn, void *ctx)
14,105✔
2255
{
2256
   // LRM 08 section 10.2 has rules for building a wait statement from a
2257
   // sensitivity list. LRM 08 section 11.3 extends these rules to
2258
   // all-sensitised processes.
2259

2260
   switch (tree_kind(expr)) {
17,510✔
2261
   case T_REF:
4,470✔
2262
      if (class_of(tree_ref(expr)) == C_SIGNAL)
4,470✔
2263
         (*fn)(expr, ctx);
2,612✔
2264
      break;
2265

2266
   case T_EXTERNAL_NAME:
7✔
2267
      if (tree_class(expr) == C_SIGNAL)
7✔
2268
         (*fn)(expr, ctx);
7✔
2269
      break;
2270

2271
   case T_WAVEFORM:
2,953✔
2272
   case T_QUALIFIED:
2273
   case T_TYPE_CONV:
2274
   case T_INERTIAL:
2275
      if (tree_has_value(expr))
2,953✔
2276
         build_wait(tree_value(expr), fn, ctx);
2,944✔
2277
      break;
2278

2279
   case T_ASSERT:
405✔
2280
      build_wait(tree_value(expr), fn, ctx);
405✔
2281
      // Fall-through
2282
   case T_REPORT:
409✔
2283
      if (tree_has_message(expr))
409✔
2284
         build_wait(tree_message(expr), fn, ctx);
280✔
2285
      break;
2286

2287
   case T_ARRAY_REF:
828✔
2288
   case T_ARRAY_SLICE:
2289
   case T_RECORD_REF:
2290
      {
2291
         tree_t ref = name_to_ref(expr);
828✔
2292
         if (ref != NULL && class_of(ref) == C_SIGNAL
828✔
2293
             && longest_static_prefix(expr) == expr)
529✔
2294
            (*fn)(expr, ctx);
452✔
2295
         else {
2296
            build_wait(tree_value(expr), fn, ctx);
376✔
2297
            build_wait_for_target(expr, fn, ctx);
376✔
2298
         }
2299
      }
2300
      break;
2301

2302
   case T_FCALL:
2,025✔
2303
   case T_PCALL:
2304
   case T_PROT_FCALL:
2305
   case T_PROT_PCALL:
2306
      {
2307
         tree_t decl = tree_ref(expr);
2,025✔
2308
         const int nparams = tree_params(expr);
2,025✔
2309
         for (int i = 0; i < nparams; i++) {
5,504✔
2310
            tree_t p = tree_param(expr, i), port;
3,479✔
2311
            switch (tree_subkind(p)) {
3,479✔
2312
            case P_POS:
3,479✔
2313
               port = tree_port(decl, tree_pos(p));
3,479✔
2314
               break;
3,479✔
UNCOV
2315
            case P_NAMED:
×
UNCOV
2316
               port = tree_ref(name_to_ref(tree_name(p)));
×
UNCOV
2317
               break;
×
UNCOV
2318
            default:
×
2319
               should_not_reach_here();
2320
            }
2321
            assert(tree_kind(port) == T_PARAM_DECL);
3,479✔
2322

2323
            switch (tree_subkind(port)) {
3,479✔
2324
            case PORT_IN:
3,472✔
2325
            case PORT_INOUT:
2326
            case PORT_ARRAY_VIEW:
2327
            case PORT_RECORD_VIEW:
2328
               build_wait(tree_value(p), fn, ctx);
3,472✔
2329
               break;
3,472✔
2330
            default:
2331
               break;
2332
            }
2333
         }
2334
      }
2335
      break;
2336

2337
   case T_AGGREGATE:
380✔
2338
      {
2339
         const int nassocs = tree_assocs(expr);
380✔
2340
         for (int i = 0; i < nassocs; i++) {
1,384✔
2341
            tree_t a = tree_assoc(expr, i);
1,004✔
2342
            build_wait(tree_value(a), fn, ctx);
1,004✔
2343

2344
            switch (tree_subkind(a)) {
1,004✔
2345
            case A_RANGE:
53✔
2346
            case A_SLICE:
2347
               build_wait(tree_range(a, 0), fn, ctx);
53✔
2348
               break;
53✔
2349
            case A_NAMED:
93✔
2350
               build_wait(tree_name(a), fn, ctx);
93✔
2351
               break;
93✔
2352
            }
2353
         }
2354
      }
2355
      break;
2356

2357
   case T_ATTR_REF:
362✔
2358
      {
2359
         const attr_kind_t predef = tree_subkind(expr);
362✔
2360
         if (predef == ATTR_EVENT || predef == ATTR_ACTIVE)
362✔
2361
            build_wait(tree_name(expr), fn, ctx);
169✔
2362

2363
         const int nparams = tree_params(expr);
362✔
2364
         for (int i = 0; i < nparams; i++)
379✔
2365
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
17✔
2366
      }
2367
      break;
2368

2369
   case T_LITERAL:
2370
   case T_STRING:
2371
   case T_DUMMY_DRIVER:
2372
      break;
2373

2374
   case T_IF:
235✔
2375
      {
2376
         const int nconds = tree_conds(expr);
235✔
2377
         for (int i = 0; i < nconds; i++)
641✔
2378
            build_wait(tree_cond(expr, i), fn, ctx);
406✔
2379
      }
2380
      break;
2381

2382
   case T_COND_STMT:
410✔
2383
      {
2384
         if (tree_has_value(expr))
410✔
2385
            build_wait(tree_value(expr), fn, ctx);
275✔
2386

2387
         const int nstmts = tree_stmts(expr);
410✔
2388
         for (int i = 0; i < nstmts; i++)
822✔
2389
            build_wait(tree_stmt(expr, i), fn, ctx);
412✔
2390
      }
2391
      break;
2392

2393
   case T_COND_VALUE:
3✔
2394
      {
2395
         const int nconds = tree_conds(expr);
3✔
2396
         for (int i = 0; i < nconds; i++)
12✔
2397
            build_wait(tree_cond(expr, i), fn, ctx);
9✔
2398
         break;
2399
      }
2400

2401
   case T_COND_EXPR:
9✔
2402
      {
2403
         if (tree_has_value(expr))
9✔
2404
            build_wait(tree_value(expr), fn, ctx);
6✔
2405

2406
         build_wait(tree_result(expr), fn, ctx);
9✔
2407
         break;
9✔
2408
      }
2409

2410
   case T_PROCESS:
53✔
2411
   case T_SEQUENCE:
2412
   case T_PROC_BODY:
2413
      {
2414
         const int ndecls = tree_decls(expr);
53✔
2415
         for (int i = 0; i < ndecls; i++) {
71✔
2416
            tree_t d = tree_decl(expr, i);
18✔
2417
            if (tree_kind(d) == T_PROC_BODY)
18✔
2418
               build_wait(d, fn, ctx);
2✔
2419
         }
2420

2421
         const int nstmts = tree_stmts(expr);
53✔
2422
         for (int i = 0; i < nstmts; i++)
121✔
2423
            build_wait(tree_stmt(expr, i), fn, ctx);
68✔
2424
      }
2425
      break;
2426

2427
   case T_SIGNAL_ASSIGN:
2,625✔
2428
      {
2429
         build_wait_for_target(tree_target(expr), fn, ctx);
2,625✔
2430

2431
         const int nwaves = tree_waveforms(expr);
2,625✔
2432
         for (int i = 0; i < nwaves; i++)
5,424✔
2433
            build_wait(tree_waveform(expr, i), fn, ctx);
2,799✔
2434
      }
2435
      break;
2436

2437
   case T_VAR_ASSIGN:
16✔
2438
   case T_FORCE:
2439
      build_wait_for_target(tree_target(expr), fn, ctx);
16✔
2440
      build_wait(tree_value(expr), fn, ctx);
16✔
2441
      break;
16✔
2442

2443
   case T_RELEASE:
3✔
2444
      build_wait_for_target(tree_target(expr), fn, ctx);
3✔
2445
      break;
3✔
2446

2447
   case T_CASE:
47✔
2448
   case T_MATCH_CASE:
2449
      {
2450
         build_wait(tree_value(expr), fn, ctx);
47✔
2451

2452
         const int nstmts = tree_stmts(expr);
47✔
2453
         for (int i = 0; i < nstmts; i++) {
285✔
2454
            tree_t alt = tree_stmt(expr, i);
238✔
2455

2456
            const int nstmts = tree_stmts(alt);
238✔
2457
            for (int j = 0; j < nstmts; j++)
476✔
2458
               build_wait(tree_stmt(alt, j), fn, ctx);
238✔
2459
         }
2460
      }
2461
      break;
2462

2463
   case T_FOR:
16✔
2464
      {
2465
         build_wait(tree_range(expr, 0), fn, ctx);
16✔
2466

2467
         const int nstmts = tree_stmts(expr);
16✔
2468
         for (int i = 0; i < nstmts; i++)
35✔
2469
            build_wait(tree_stmt(expr, i), fn, ctx);
19✔
2470
      }
2471
      break;
2472

2473
   case T_WHILE:
1✔
2474
      build_wait(tree_value(expr), fn, ctx);
1✔
2475
      // Fall-through
2476
   case T_LOOP:
4✔
2477
      {
2478
         const int nstmts = tree_stmts(expr);
4✔
2479
         for (int i = 0; i < nstmts; i++)
20✔
2480
            build_wait(tree_stmt(expr, i), fn, ctx);
16✔
2481
      }
2482
      break;
2483

2484
   case T_NEXT:
9✔
2485
   case T_EXIT:
2486
      if (tree_has_value(expr))
9✔
2487
         build_wait(tree_value(expr), fn, ctx);
6✔
2488
      break;
2489

2490
   case T_RANGE:
150✔
2491
      if (tree_subkind(expr) == RANGE_EXPR)
150✔
2492
         build_wait(tree_value(expr), fn, ctx);
11✔
2493
      else {
2494
         build_wait(tree_left(expr), fn, ctx);
139✔
2495
         build_wait(tree_right(expr), fn, ctx);
139✔
2496
      }
2497
      break;
2498

2499
   case T_OPEN:
2500
      break;
2501

UNCOV
2502
   default:
×
2503
      fatal_trace("Cannot handle tree kind %s in wait expression",
2504
                  tree_kind_str(tree_kind(expr)));
2505
   }
2506
}
14,105✔
2507

2508
void print_syntax(const char *fmt, ...)
2,128✔
2509
{
2510
   LOCAL_TEXT_BUF tb = tb_new();
2,128✔
2511
   bool highlighting = false;
2,128✔
2512
   static bool comment = false, last_was_newline = false;
2,128✔
2513
   for (const char *p = fmt; *p != '\0'; p++) {
11,125✔
2514
      if (comment) {
8,997✔
2515
         if (*p == '\n' || *p == '\r') {
3,244✔
2516
            comment = false;
94✔
2517
            last_was_newline = true;
94✔
2518
            tb_cat(tb, "$$\n");
94✔
2519
         }
2520
         else if (*p != '~' && *p != '#') {
3,150✔
2521
            tb_append(tb, *p);
2,990✔
2522
            last_was_newline = false;
2,990✔
2523
         }
2524
         if (p > fmt && *p == '/' && *(p - 1) == '*') {
3,244✔
2525
            tb_cat(tb, "$$");
16✔
2526
            comment = false;
16✔
2527
            last_was_newline = false;
16✔
2528
         }
2529
      }
2530
      else if (*p == '\r') {
5,753✔
2531
         if (!last_was_newline) {
22✔
UNCOV
2532
            tb_append(tb, '\n');
×
UNCOV
2533
            last_was_newline = true;
×
2534
         }
2535
      }
2536
      else if (*p == '#' && *(p + 1) != '#') {
5,731✔
2537
         tb_cat(tb, "$bold$$cyan$");
380✔
2538
         last_was_newline = false;
380✔
2539
         highlighting = true;
380✔
2540
      }
2541
      else if (*p == '~' && *(p + 1) != '~') {
5,351✔
2542
         tb_cat(tb, "$yellow$");
1✔
2543
         last_was_newline = false;
1✔
2544
         highlighting = true;
1✔
2545
      }
2546
      else if ((*p == '-' && *(p + 1) == '-')
5,350✔
2547
               || (*p == '/' && *(p + 1) == '/')
5,260✔
2548
               || (*p == '/' && *(p + 1) == '*')) {
5,256✔
2549
         tb_cat(tb, "$red$");
110✔
2550
         tb_append(tb, *p);
110✔
2551
         last_was_newline = false;
110✔
2552
         comment = true;
110✔
2553
      }
2554
      else if (!isalnum_iso88591(*p) && *p != '_'
5,240✔
2555
               && *p != '%' && highlighting) {
2,698✔
2556
         tb_cat(tb, "$$");
325✔
2557
         tb_append(tb, *p);
325✔
2558
         last_was_newline = false;
325✔
2559
         highlighting = false;
325✔
2560
      }
2561
      else {
2562
         tb_append(tb, *p);
4,915✔
2563
         last_was_newline = (*p == '\n');
4,915✔
2564
      }
2565
   }
2566

2567
   if (highlighting)
2,128✔
2568
      tb_cat(tb, "$$");
56✔
2569

2570
   va_list ap;
2,128✔
2571
   va_start(ap, fmt);
2,128✔
2572

2573
   if (syntax_buf != NULL) {
2,128✔
2574
      ostream_t os = { tb_ostream_write, syntax_buf, CHARSET_ISO88591, false };
2,128✔
2575
      nvc_vfprintf(&os, tb_get(tb), ap);
2,128✔
2576
   }
2577
   else
UNCOV
2578
      nvc_vprintf(tb_get(tb), ap);
×
2579

2580
   va_end(ap);
2,128✔
2581
}
2,128✔
2582

2583
void capture_syntax(text_buf_t *tb)
9✔
2584
{
2585
   assert(tb == NULL || syntax_buf == NULL);
9✔
2586
   syntax_buf = tb;
9✔
2587
}
9✔
2588

2589
void analyse_file(const char *file, jit_t *jit, unit_registry_t *ur,
4,027✔
2590
                  mir_context_t *mc)
2591
{
2592
   input_from_file(file);
4,027✔
2593

2594
   switch (source_kind()) {
4,027✔
2595
   case SOURCE_VHDL:
3,723✔
2596
      {
2597
         lib_t work = lib_work();
3,723✔
2598
         int base_errors = 0;
3,723✔
2599
         tree_t unit;
3,723✔
2600
         while (base_errors = error_count(), (unit = parse())) {
14,315✔
2601
            if (error_count() == base_errors) {
10,592✔
2602
               lib_put(work, unit);
10,592✔
2603
               unit_registry_purge(ur, tree_ident(unit));
10,592✔
2604

2605
               simplify_local(unit, jit, ur, mc);
10,592✔
2606
               bounds_check(unit);
10,592✔
2607
            }
2608
            else
UNCOV
2609
               lib_put_error(work, unit);
×
2610
         }
2611
      }
2612
      break;
2613

2614
   case SOURCE_VERILOG:
304✔
2615
      {
2616
         LOCAL_TEXT_BUF tb = tb_new();
608✔
2617
         vlog_preprocess(tb, true);
304✔
2618

2619
         file_ref_t file_ref = loc_file_ref(file, NULL);
304✔
2620
         input_from_buffer(tb_get(tb), tb_len(tb), file_ref, SOURCE_VERILOG);
304✔
2621

2622
         lib_t work = lib_work();
304✔
2623
         vlog_node_t module;
304✔
2624
         while ((module = vlog_parse())) {
961✔
2625
            if (error_count() == 0) {
353✔
2626
               vlog_check(module);
353✔
2627

2628
               if (error_count() == 0) {
353✔
2629
                  vlog_simp(module);
353✔
2630
                  lib_put_vlog(work, module);
353✔
2631
               }
2632
            }
2633
         }
2634
      }
2635
      break;
304✔
2636

2637
   case SOURCE_SDF:
×
2638
      {
UNCOV
2639
         sdf_file_t *sdf_file = sdf_parse(file, 0);
×
UNCOV
2640
         progress("analysed SDF file: %s", file);
×
2641

UNCOV
2642
         if (sdf_file != NULL) {
×
UNCOV
2643
            warnf("SDF is not yet supported");
×
UNCOV
2644
            sdf_file_free(sdf_file);
×
2645
         }
2646
      }
2647
      break;
2648
   }
2649
}
4,024✔
2650

2651
bool all_character_literals(type_t type)
15,415✔
2652
{
2653
   assert(type_is_enum(type));
15,415✔
2654

2655
   type_t base = type_base_recur(type);
15,415✔
2656
   const int nlits = type_enum_literals(base);
15,415✔
2657
   for (int i = 0; i < nlits; i++) {
75,582✔
2658
      if (ident_char(tree_ident(type_enum_literal(base, i)), 0) != '\'')
66,081✔
2659
         return false;
2660
   }
2661

2662
   return true;
2663
}
2664

2665
bool is_operator_symbol(ident_t ident)
28,732✔
2666
{
2667
   const well_known_t wk = is_well_known(ident);
28,732✔
2668
   if (wk >= NUM_WELL_KNOWN)
28,732✔
2669
      return false;
2670
   else if (standard() < STD_08)
16,428✔
2671
      return wk >= W_OP_AND && wk <= W_OP_NOT;
3,266✔
2672
   else
2673
      return wk >= W_OP_AND && wk <= W_OP_MATCH_GREATER_EQUAL;
13,162✔
2674
}
2675

2676
bool same_tree(tree_t a, tree_t b)
7,267✔
2677
{
2678
   const tree_kind_t akind = tree_kind(a);
7,267✔
2679
   if (akind != tree_kind(b))
7,267✔
2680
      return false;
2681

2682
   switch (akind) {
7,145✔
2683
   case T_REF:
2,995✔
2684
      return tree_ref(a) == tree_ref(b);
2,995✔
2685
   case T_ARRAY_REF:
1,091✔
2686
      {
2687
         if (!same_tree(tree_value(a), tree_value(b)))
1,091✔
2688
            return false;
2689

2690
         const int nparams = tree_params(a);
1,069✔
2691
         assert(nparams == tree_params(b));
1,069✔
2692

2693
         for (int i = 0; i < nparams; i++) {
1,948✔
2694
            tree_t pa = tree_value(tree_param(a, i));
1,744✔
2695
            tree_t pb = tree_value(tree_param(b, i));
1,744✔
2696
            if (!same_tree(pa, pb))
1,744✔
2697
               return false;
2698
         }
2699

2700
         return true;
2701
      }
2702
   case T_ARRAY_SLICE:
41✔
2703
      {
2704
         if (!same_tree(tree_value(a), tree_value(b)))
41✔
2705
            return false;
2706

2707
         tree_t ra = tree_range(a, 0);
41✔
2708
         tree_t rb = tree_range(b, 0);
41✔
2709

2710
         const range_kind_t rakind = tree_subkind(ra);
41✔
2711
         if (rakind != tree_subkind(rb) || rakind == RANGE_EXPR)
41✔
2712
            return false;
2713

2714
         return same_tree(tree_left(ra), tree_left(rb))
41✔
2715
            && same_tree(tree_right(ra), tree_right(rb));
44✔
2716
      }
2717
   case T_RECORD_REF:
765✔
2718
      return ident_casecmp(tree_ident(a), tree_ident(b))
765✔
2719
         && same_tree(tree_value(a), tree_value(b));
771✔
2720
   case T_LITERAL:
1,975✔
2721
      {
2722
         const literal_kind_t lkind = tree_subkind(a);
1,975✔
2723
         if (lkind != tree_subkind(b))
1,975✔
2724
            return false;
2725

2726
         switch (lkind) {
1,975✔
2727
         case L_PHYSICAL:
24✔
2728
            {
2729
               ident_t aid = tree_has_ident(a) ? tree_ident(a) : NULL;
24✔
2730
               ident_t bid = tree_has_ident(b) ? tree_ident(b) : NULL;
24✔
2731
               if (aid == bid)
24✔
2732
                  return tree_ival(a) == tree_ival(b);
24✔
2733
               else
2734
                  return false;
2735
            }
2736
         case L_INT:
1,926✔
2737
            return tree_ival(a) == tree_ival(b);
1,926✔
2738
         case L_REAL:
25✔
2739
            return tree_dval(a) == tree_dval(b);
25✔
2740
         case L_NULL:
2741
            return true;
UNCOV
2742
         default:
×
UNCOV
2743
            return false;
×
2744
         }
2745
      }
2746
   case T_STRING:
253✔
2747
      {
2748
         const int nchars = tree_chars(a);
253✔
2749
         if (tree_chars(b) != nchars)
253✔
2750
            return false;
2751

2752
         for (int i = 0; i < nchars; i++) {
308✔
2753
            if (!same_tree(tree_char(a, i), tree_char(b, i)))
56✔
2754
               return false;
2755
         }
2756

2757
         return true;
2758
      }
2759
   case T_OPEN:
2760
      return true;
2761
   default:
25✔
2762
      return false;
25✔
2763
   }
2764
}
2765

2766
static range_kind_t get_range_direction(tree_t r)
317✔
2767
{
2768
   assert(tree_kind(r) == T_RANGE);
317✔
2769

2770
   const range_kind_t dir = tree_subkind(r);
317✔
2771
   if (dir != RANGE_EXPR)
317✔
2772
      return dir;
2773

2774
   // Handle ranges like X'RANGE where X has known direction
2775

2776
   tree_t aref = tree_value(r);
49✔
2777
   assert(tree_kind(aref) == T_ATTR_REF);
49✔
2778

2779
   const attr_kind_t kind = tree_subkind(aref);
49✔
2780
   assert(kind == ATTR_RANGE || kind == ATTR_REVERSE_RANGE);
49✔
2781

2782
   type_t prefix_type = tree_type(tree_name(aref));
49✔
2783
   if (type_is_unconstrained(prefix_type))
49✔
2784
      return RANGE_EXPR;
2785

2786
   tree_t prefix_r = range_of(prefix_type, 0);
46✔
2787

2788
   const range_kind_t prefix_dir = get_range_direction(prefix_r);
46✔
2789
   if (prefix_dir != RANGE_TO && prefix_dir != RANGE_DOWNTO)
46✔
2790
      return prefix_dir;
2791
   else if (kind == ATTR_REVERSE_RANGE)
46✔
2792
      return prefix_dir == RANGE_TO ? RANGE_DOWNTO : RANGE_TO;
3✔
2793
   else
2794
      return prefix_dir;
2795
}
2796

2797
bool calculate_aggregate_bounds(tree_t expr, range_kind_t *kind,
7,058✔
2798
                                int64_t *left, int64_t *right)
2799
{
2800
   // Calculate the direction and bounds of an array aggregate using the
2801
   // rules in LRM 93 7.3.2.2
2802

2803
   type_t type = tree_type(expr);
7,058✔
2804
   type_t index_type = index_type_of(type, 0);
7,058✔
2805
   if (index_type == NULL || type_is_none(index_type))
7,058✔
2806
      return false;
1✔
2807

2808
   tree_t index_r = range_of(index_type, 0), base_r = index_r;
7,057✔
2809

2810
   int64_t low, high;
7,057✔
2811
   if (!folded_bounds(index_r, &low, &high))
7,057✔
2812
      return false;
2813

2814
   int64_t clow = INT64_MAX, chigh = INT64_MIN;  // Actual bounds computed below
7,042✔
2815

2816
   range_kind_t dir;
7,042✔
2817
   if (type_is_unconstrained(type))
7,042✔
2818
      dir = tree_subkind(index_r);
6,771✔
2819
   else {
2820
      base_r = range_of(type, 0);
271✔
2821
      dir = get_range_direction(base_r);
271✔
2822
   }
2823

2824
   const int nassocs = tree_assocs(expr);
7,042✔
2825

2826
   if (standard() >= STD_08) {
7,042✔
2827
      // VHDL-2008 range association determines index direction for
2828
      // unconstrained aggregate when the expression type matches the
2829
      // array type
2830
      for (int i = 0; i < nassocs; i++) {
14,887✔
2831
         tree_t a = tree_assoc(expr, i);
10,768✔
2832
         if (tree_subkind(a) == A_SLICE)
10,768✔
2833
            dir = tree_subkind(tree_range(a, 0));
56✔
2834
      }
2835
   }
2836

2837
   if (dir != RANGE_TO && dir != RANGE_DOWNTO)
7,042✔
2838
      return false;
2839

2840
   int64_t pos = 0;
2841
   for (int i = 0; i < nassocs; i++) {
19,653✔
2842
      tree_t a = tree_assoc(expr, i);
17,385✔
2843
      int64_t ilow = 0, ihigh = 0;
17,385✔
2844
      const assoc_kind_t akind = tree_subkind(a);
17,385✔
2845

2846
      switch (akind) {
17,385✔
2847
      case A_NAMED:
611✔
2848
         {
2849
            tree_t name = tree_name(a);
611✔
2850
            if (folded_int(name, &ilow))
611✔
2851
               ihigh = ilow;
601✔
2852
            else
2853
               return false;
4,762✔
2854
         }
2855
         break;
601✔
2856

2857
      case A_RANGE:
1,169✔
2858
      case A_SLICE:
2859
         {
2860
            tree_t r = tree_range(a, 0);
1,169✔
2861
            const range_kind_t rkind = tree_subkind(r);
1,169✔
2862
            if (rkind == RANGE_TO || rkind == RANGE_DOWNTO) {
1,169✔
2863
               tree_t left = tree_left(r), right = tree_right(r);
858✔
2864

2865
               int64_t ileft, iright;
858✔
2866
               if (folded_int(left, &ileft) && folded_int(right, &iright)) {
858✔
2867
                  ilow = (rkind == RANGE_TO ? ileft : iright);
437✔
2868
                  ihigh = (rkind == RANGE_TO ? iright : ileft);
437✔
2869
               }
2870
               else
2871
                  return false;
421✔
2872
            }
2873
            else
2874
               return false;
2875
         }
2876
         break;
2877

2878
      case A_OTHERS:
2879
         return false;
2880

2881
      case A_POS:
9,834✔
2882
         if (i == 0) {
9,834✔
2883
            int64_t ileft;
1,568✔
2884
            if (folded_int(tree_left(base_r), &ileft))
1,568✔
2885
               ilow = ihigh = ileft;
1,568✔
2886
            else
UNCOV
2887
               return false;
×
2888
         }
2889
         else if (dir == RANGE_TO)
8,266✔
2890
            ilow = ihigh = clow + pos;
8,264✔
2891
         else
2892
            ilow = ihigh = chigh - pos;
2✔
2893
         pos++;
9,834✔
2894
         break;
9,834✔
2895

2896
      case A_CONCAT:
5,760✔
2897
         {
2898
            type_t value_type = tree_type(tree_value(a));
5,760✔
2899

2900
            int64_t length;
5,760✔
2901
            if (type_is_unconstrained(value_type))
5,760✔
2902
               return false;
4,009✔
2903
            else if (folded_length(range_of(value_type, 0), &length)) {
2,073✔
2904
               if (i == 0) {
1,751✔
2905
                  int64_t ileft;
1,454✔
2906
                  if (folded_int(tree_left(base_r), &ileft))
1,454✔
2907
                     ilow = ihigh = ileft;
1,454✔
2908
                  else
UNCOV
2909
                     return false;
×
2910
               }
2911
               else if (dir == RANGE_TO) {
297✔
2912
                  ilow = clow + pos;
279✔
2913
                  ihigh = ilow + length - 1;
279✔
2914
               }
2915
               else {
2916
                  ihigh = chigh - pos;
18✔
2917
                  ilow = ihigh - length + 1;
18✔
2918
               }
2919
               pos += length;
1,751✔
2920
            }
2921
            else
2922
               return false;
2923
         }
2924
         break;
2925
      }
2926

2927
      clow = MIN(clow, ilow);
12,623✔
2928
      chigh = MAX(chigh, ihigh);
12,623✔
2929
   }
2930

2931
   if (clow < low || chigh > high)
2,268✔
2932
      return false;   // Will raise a bounds check error later
2933

2934
   *kind = dir;
2,263✔
2935
   *left = dir == RANGE_TO ? clow : chigh;
2,263✔
2936
   *right = dir == RANGE_TO ? chigh : clow;
2,263✔
2937

2938
   return true;
2,263✔
2939
}
2940

2941
type_t calculate_aggregate_subtype(tree_t expr)
5,943✔
2942
{
2943
   range_kind_t dir;
5,943✔
2944
   int64_t ileft, iright;
5,943✔
2945
   if (!calculate_aggregate_bounds(expr, &dir, &ileft, &iright))
5,943✔
2946
      return NULL;
2947

2948
   type_t type = tree_type(expr);
2,245✔
2949

2950
   const int ndims = dimension_of(type);
2,245✔
2951
   type_t a0_type = NULL;
2,245✔
2952
   if (ndims > 1) {
2,245✔
2953
      a0_type = tree_type(tree_value(tree_assoc(expr, 0)));
72✔
2954
      if (type_is_unconstrained(a0_type))
72✔
2955
         return NULL;
2956

2957
      assert(dimension_of(a0_type) == ndims - 1);
72✔
2958
   }
2959

2960
   type_t index_type = index_type_of(type, 0);
2,245✔
2961

2962
   tree_t left = get_discrete_lit(expr, index_type, ileft);
2,245✔
2963
   tree_t right = get_discrete_lit(expr, index_type, iright);
2,245✔
2964
   assert(left != NULL && right != NULL);
2,245✔
2965

2966
   type_t sub = type_new(T_SUBTYPE);
2,245✔
2967
   type_set_base(sub, type_base_recur(type));
2,245✔
2968

2969
   type_t elem = type_elem(type);
2,245✔
2970
   if (type_is_unconstrained(elem)) {
2,245✔
2971
      tree_t a0 = tree_assoc(expr, 0);
106✔
2972
      switch (tree_subkind(a0)) {
106✔
2973
      case A_CONCAT:
6✔
2974
      case A_SLICE:
2975
         a0_type = type_elem(tree_type(tree_value(a0)));
6✔
2976
         break;
6✔
2977
      default:
100✔
2978
         a0_type = tree_type(tree_value(a0));
100✔
2979
         break;
100✔
2980
      }
2981

2982
      if (!type_is_unconstrained(a0_type))
106✔
2983
         elem = a0_type;
62✔
2984
   }
2985

2986
   type_set_elem(sub, elem);
2,245✔
2987

2988
   tree_t cons = tree_new(T_CONSTRAINT);
2,245✔
2989
   tree_set_subkind(cons, C_INDEX);
2,245✔
2990

2991
   tree_t r = tree_new(T_RANGE);
2,245✔
2992
   tree_set_subkind(r, dir);
2,245✔
2993
   tree_set_type(r, index_type);
2,245✔
2994
   tree_set_left(r, left);
2,245✔
2995
   tree_set_right(r, right);
2,245✔
2996

2997
   tree_add_range(cons, r);
2,245✔
2998

2999
   for (int i = 1; i < ndims; i++)
2,317✔
3000
      tree_add_range(cons, range_of(a0_type, i - 1));
72✔
3001

3002
   type_set_constraint(sub, cons);
2,245✔
3003

3004
   return sub;
2,245✔
3005
}
3006

3007
bool can_be_signal(type_t type)
15,990✔
3008
{
3009
   switch (type_kind(type)) {
28,120✔
3010
   case T_RECORD:
3,319✔
3011
      {
3012
         const int nfields = type_fields(type);
3,319✔
3013
         for (int i = 0; i < nfields; i++) {
13,057✔
3014
            if (!can_be_signal(tree_type(type_field(type, i))))
10,530✔
3015
               return false;
3016
         }
3017

3018
         return true;
3019
      }
3020
   case T_ARRAY:
4,691✔
3021
      return can_be_signal(type_elem(type));
4,691✔
3022
   case T_SUBTYPE:
7,439✔
3023
      return can_be_signal(type_base(type));
7,439✔
3024
   case T_ACCESS:
3025
   case T_FILE:
3026
   case T_PROTECTED:
3027
   case T_INCOMPLETE:
3028
      return false;
3029
   default:
9,204✔
3030
      return true;
9,204✔
3031
   }
3032
}
3033

3034
type_t merge_constraints(type_t to, type_t from)
1,073✔
3035
{
3036
   assert(type_is_unconstrained(to));
1,073✔
3037
   assert(type_eq(to, from));
1,073✔
3038

3039
   tree_t cto = NULL;
1,073✔
3040
   if (type_kind(to) == T_SUBTYPE && type_has_constraint(to))
1,073✔
3041
      cto = type_constraint(to);
23✔
3042

3043
   tree_t cfrom = NULL;
1,073✔
3044
   if (type_kind(from) == T_SUBTYPE && type_has_constraint(from))
1,073✔
3045
      cfrom = type_constraint(from);
860✔
3046

3047
   if (cfrom == NULL)
860✔
3048
      return to;
213✔
3049

3050
   type_t sub = type_new(T_SUBTYPE);
860✔
3051
   type_set_base(sub, type_base_recur(to));
860✔
3052

3053
   if (type_is_array(to)) {
860✔
3054
      type_set_constraint(sub, cto ?: cfrom);
1,700✔
3055

3056
      type_t elem = type_elem(to);
855✔
3057
      if (type_is_unconstrained(elem))
855✔
3058
         type_set_elem(sub, merge_constraints(elem, type_elem(from)));
14✔
3059
      else
3060
         type_set_elem(sub, elem);
841✔
3061
   }
3062
   else {
3063
      tree_t cnew = tree_new(T_CONSTRAINT);
5✔
3064
      tree_set_subkind(cnew, C_RECORD);
5✔
3065
      tree_set_loc(cnew, tree_loc(cto ?: cfrom));
6✔
3066

3067
      type_set_constraint(sub, cnew);
5✔
3068

3069
      if (cto != NULL) {
5✔
3070
         const int nto = tree_ranges(cto);
4✔
3071
         for (int i = 0; i < nto; i++)
8✔
3072
            tree_add_range(cnew, tree_range(cto, i));
4✔
3073
      }
3074

3075
      const int nfrom = tree_ranges(cfrom), base = tree_ranges(cnew);
5✔
3076
      for (int i = 0; i < nfrom; i++) {
13✔
3077
         tree_t ec = tree_range(cfrom, i);
8✔
3078
         assert(tree_kind(ec) == T_ELEM_CONSTRAINT);
8✔
3079

3080
         ident_t id = tree_ident(ec);
8✔
3081
         bool found = false;
8✔
3082
         for (int j = 0; j < base && !found; j++)
15✔
3083
            found |= tree_ident(tree_range(cnew, j)) == id;
7✔
3084

3085
         if (!found)
8✔
3086
            tree_add_range(cnew, ec);
4✔
3087
      }
3088
   }
3089

3090
   return sub;
3091
}
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