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

nickg / nvc / 24238351848

10 Apr 2026 10:22AM UTC coverage: 92.348% (+0.01%) from 92.338%
24238351848

Pull #1488

github

web-flow
Merge 198926fb5 into e00449bb3
Pull Request #1488: Verilog: implement $info, $warning, $error and fix $fatal format strings

1156 of 1216 new or added lines in 23 files covered. (95.07%)

1443 existing lines in 24 files now uncovered.

76369 of 82697 relevant lines covered (92.35%)

609386.16 hits per line

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

94.19
/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)
969,150✔
47
{
48
   int64_t value;
969,150✔
49
   if (folded_int(t, &value))
969,150✔
50
      return value;
969,150✔
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)
186,824✔
57
{
58
   assert(tree_kind(r) == T_RANGE);
186,824✔
59

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

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

67
bool folded_int(tree_t t, int64_t *l)
6,889,930✔
68
{
69
   switch (tree_kind(t)) {
6,941,509✔
70
   case T_LITERAL:
4,059,414✔
71
      switch (tree_subkind(t)) {
4,059,414✔
72
      case L_PHYSICAL:
24,175✔
73
         if (tree_has_ref(t))
24,175✔
74
            return false;
75
         // Fall-through
76
      case L_INT:
77
         *l = tree_ival(t);
3,965,493✔
78
         return true;
3,965,493✔
79
      default:
80
         return false;
81
      }
82
   case T_QUALIFIED:
470✔
83
      return folded_int(tree_value(t), l);
470✔
84
   case T_REF:
2,599,092✔
85
      if (tree_has_ref(t)) {
2,599,092✔
86
         tree_t decl = tree_ref(t);
2,599,092✔
87
         switch (tree_kind(decl)) {
2,599,092✔
88
         case T_CONST_DECL:
62,852✔
89
            if (tree_has_value(decl))
62,852✔
90
               return folded_int(tree_value(decl), l);
50,523✔
91
            else
92
               return false;
93
         case T_ENUM_LIT:
2,442,178✔
94
            *l = tree_pos(decl);
2,442,178✔
95
            return true;
2,442,178✔
96
         case T_ALIAS:
586✔
97
            return folded_int(tree_value(decl), l);
586✔
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)
317,791✔
109
{
110
   switch (tree_kind(t)) {
317,800✔
111
   case T_LITERAL:
307,648✔
112
      if (tree_subkind(t) == L_REAL) {
307,648✔
113
         *l = tree_dval(t);
307,403✔
114
         return true;
307,403✔
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)
87,143✔
126
{
127
   int64_t low, high;
87,143✔
128
   if (folded_bounds(r, &low, &high)) {
87,143✔
129
      *l = MAX(high - low + 1, 0);
82,575✔
130
      return true;
82,575✔
131
   }
132
   else
133
      return false;
134
}
135

136
bool folded_bounds(tree_t r, int64_t *low, int64_t *high)
2,764,989✔
137
{
138
   assert(tree_kind(r) == T_RANGE);
2,764,989✔
139

140
   const range_kind_t rkind = tree_subkind(r);
2,764,989✔
141

142
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
2,764,989✔
143
      return false;
144

145
   int64_t left, right;
2,754,301✔
146
   if (!folded_int(tree_left(r), &left))
2,754,301✔
147
      return false;
148
   else if (!folded_int(tree_right(r), &right))
2,594,708✔
149
      return false;
150

151
   switch (rkind) {
2,539,371✔
152
   case RANGE_TO:
2,348,061✔
153
      *low  = left;
2,348,061✔
154
      *high = right;
2,348,061✔
155
      return true;
2,348,061✔
156
   case RANGE_DOWNTO:
191,310✔
157
      *low  = right;
191,310✔
158
      *high = left;
191,310✔
159
      return true;
191,310✔
160
   default:
161
      return false;
162
   }
163
}
164

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

169
   const range_kind_t rkind = tree_subkind(r);
116,068✔
170

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

174
   double left, right;
116,040✔
175
   if (folded_real(tree_left(r), &left) && folded_real(tree_right(r), &right)) {
116,040✔
176
      switch (rkind) {
115,944✔
177
      case RANGE_TO:
115,944✔
178
         *low  = left;
115,944✔
179
         *high = right;
115,944✔
180
         return true;
115,944✔
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;
96✔
191
}
192

193
bool folded_bool(tree_t t, bool *b)
49,282✔
194
{
195
   if (tree_kind(t) == T_REF) {
49,282✔
196
      tree_t decl = tree_ref(t);
11,535✔
197
      if (tree_kind(decl) == T_ENUM_LIT
11,535✔
198
          && type_ident(tree_type(decl)) == well_known(W_STD_BOOL)) {
8,733✔
199
         *b = (tree_pos(decl) == 1);
8,733✔
200
         return true;
8,733✔
201
      }
202
   }
203

204
   return false;
205
}
206

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

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

218
   return b;
9,473✔
219
}
220

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

229
   return f;
50,633✔
230
}
231

232
tree_t get_discrete_lit(tree_t t, type_t type, int64_t i)
5,846✔
233
{
234
   if (type_is_enum(type)) {
5,846✔
235
      type_t base = type_base_recur(type);
90✔
236
      const int maxlit = type_enum_literals(base);
90✔
237
      if (i >= maxlit)
90✔
238
         return NULL;
239
      else
240
         return get_enum_lit(t, type, i);
90✔
241
   }
242
   else
243
      return get_int_lit(t, type, i);
5,756✔
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)
125✔
258
{
259
   type_t base = type_base_recur(type);
125✔
260
   const type_kind_t basek = type_kind(base);
125✔
261

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

265
      int map[256];
23✔
266
      for (int i = 0; i < ARRAY_LEN(map); i++)
5,911✔
267
         map[i] = INT_MAX;
5,888✔
268

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

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

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

281
      int base = 0, dbits = 1;
23✔
282
      bool quoted = false;
23✔
283
      if (map['\"'] == INT_MAX) {
23✔
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;
23✔
297
      enum_array_t *array = xmalloc_flex(sizeof(enum_array_t), max, 1);
23✔
298

299
      int n = 0, m;
23✔
300
      for (; *str != '\0'; str++, n += dbits) {
122✔
301
         if (base > 0) {
84✔
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)
72✔
324
            break;
325
         else
326
            array->values[n] = m;
67✔
327
      }
328

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

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

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

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

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

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

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

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

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

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

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

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

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

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

399
         if (value->integer == -1)
16✔
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++) {
111✔
451
      if (!isspace_iso88591(*str))
11✔
452
         return false;
453
   }
454

455
   return true;
456
}
457

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

468
vhdl_standard_t standard(void)
4,283,916✔
469
{
470
   return current_std;
4,283,916✔
471
}
472

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

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

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

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

497
tree_t find_element_mode_indication(tree_t view, tree_t field, bool *converse)
785✔
498
{
499
   switch (tree_kind(view)) {
2,400✔
500
   case T_REF:
1,045✔
501
      return find_element_mode_indication(tree_ref(view), field, converse);
1,045✔
502

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

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

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

516
         const int nelems = type_fields(view_type);
785✔
517
         for (int i = 0; i < nelems; i++) {
1,288✔
518
            tree_t e = type_field(view_type, i);
1,288✔
519
            if (tree_ref(e) == field)
1,288✔
520
               return e;
785✔
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)
495✔
533
{
534
   const port_mode_t mode = tree_subkind(port);
495✔
535
   switch (mode) {
495✔
536
   case PORT_IN: return converse ? PORT_OUT : PORT_IN;
230✔
537
   case PORT_OUT: return converse ? PORT_IN : PORT_OUT;
224✔
538
   default: return mode;
539
   }
540
}
541

542
class_t class_of(tree_t t)
476,449✔
543
{
544
   switch (tree_kind(t)) {
536,026✔
545
   case T_VAR_DECL:
546
      return C_VARIABLE;
547
   case T_SIGNAL_DECL:
32,213✔
548
   case T_IMPLICIT_SIGNAL:
549
      return C_SIGNAL;
32,213✔
550
   case T_CONST_DECL:
15,591✔
551
      return C_CONSTANT;
15,591✔
552
   case T_PORT_DECL:
92,873✔
553
   case T_GENERIC_DECL:
554
   case T_PARAM_DECL:
555
   case T_EXTERNAL_NAME:
556
      return tree_class(t);
92,873✔
557
   case T_ENUM_LIT:
282,772✔
558
   case T_LITERAL:
559
   case T_STRING:
560
      return C_LITERAL;
282,772✔
561
   case T_FIELD_DECL:
36✔
562
   case T_ATTR_DECL:
563
      return C_DEFAULT;
36✔
564
   case T_VIEW_DECL:
2✔
565
      return C_VIEW;
2✔
566
   case T_UNIT_DECL:
10,811✔
567
      return C_UNITS;
10,811✔
568
   case T_ARCH:
30✔
569
      return C_ARCHITECTURE;
30✔
570
   case T_FUNC_DECL:
785✔
571
   case T_FUNC_BODY:
572
   case T_FUNC_INST:
573
   case T_FCALL:
574
   case T_PROT_FCALL:
575
      return C_FUNCTION;
785✔
576
   case T_PROC_DECL:
10,865✔
577
   case T_PROC_BODY:
578
   case T_PROC_INST:
579
   case T_PCALL:
580
   case T_PROT_PCALL:
581
      return C_PROCEDURE;
10,865✔
582
   case T_ENTITY:
87✔
583
      return C_ENTITY;
87✔
584
   case T_SUBTYPE_DECL:
1,425✔
585
      return C_SUBTYPE;
1,425✔
586
   case T_TYPE_DECL:
659✔
587
   case T_PROT_DECL:
588
   case T_PROT_BODY:
589
      return C_TYPE;
659✔
590
   case T_FILE_DECL:
256✔
591
      return C_FILE;
256✔
592
   case T_PROCESS:
545✔
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;
545✔
604
   case T_COMPONENT:
1✔
605
      return C_COMPONENT;
1✔
606
   case T_REF:
50,993✔
607
   case T_PROT_REF:
608
      return tree_has_ref(t) ? class_of(tree_ref(t)) : C_DEFAULT;
50,993✔
609
   case T_ARRAY_REF:
8,586✔
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));
8,586✔
616
   case T_PACKAGE:
848✔
617
   case T_PACK_BODY:
618
   case T_PACK_INST:
619
      return C_PACKAGE;
848✔
620
   case T_CONFIGURATION:
1✔
621
      return C_CONFIGURATION;
1✔
622
   case T_LIBRARY:
3✔
623
      return C_LIBRARY;
3✔
624
   case T_ATTR_REF:
43✔
625
      switch (tree_subkind(t)) {
43✔
626
      case ATTR_DELAYED:
627
      case ATTR_STABLE:
628
      case ATTR_QUIET:
629
      case ATTR_TRANSACTION:
630
         return C_SIGNAL;
631
      default:
34✔
632
         return C_DEFAULT;
34✔
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)
272,690✔
642
{
643
   switch (c) {
272,690✔
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:
271,985✔
653
      return true;
271,985✔
654
   }
655
}
656

657
const char *class_str(class_t c)
828✔
658
{
659
   static const char *strs[] = {
828✔
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));
828✔
666
   return strs[c];
828✔
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)
999,487✔
683
{
684
   switch (tree_kind(t)) {
999,487✔
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:
6,157✔
693
      {
694
         const class_t class = tree_class(t);
6,157✔
695
         return class == C_FUNCTION || class == C_PROCEDURE;
6,157✔
696
      }
697
   default:
735,408✔
698
      return false;
735,408✔
699
   }
700
}
701

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

708
bool is_container(tree_t t)
15,284✔
709
{
710
   switch (tree_kind(t)) {
15,284✔
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:
13,437✔
726
      return false;
13,437✔
727
   }
728
}
729

730
bool is_concurrent_block(tree_t t)
1,764✔
731
{
732
   switch (tree_kind(t)) {
1,764✔
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:
544✔
741
      return false;
544✔
742
   }
743
}
744

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

757
bool is_design_unit(tree_t t)
66,437✔
758
{
759
   switch (tree_kind(t)) {
66,437✔
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:
19,431✔
769
      return false;
19,431✔
770
   }
771
}
772

773
bool is_literal(tree_t t)
75,805✔
774
{
775
   switch (tree_kind(t)) {
75,805✔
776
   case T_REF:
26,425✔
777
      return tree_has_ref(t) && tree_kind(tree_ref(t)) == T_ENUM_LIT;
29,000✔
778
   case T_LITERAL:
779
      return true;
780
   case T_STRING:
27,606✔
781
   default:
782
      return false;
27,606✔
783
   }
784
}
785

786
bool is_body(tree_t t)
13,440✔
787
{
788
   switch (tree_kind(t)) {
13,440✔
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:
4,841✔
795
      return false;
4,841✔
796
   }
797
}
798

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

810
bool is_type_decl(tree_t t)
1,147,243✔
811
{
812
   switch (tree_kind(t)) {
1,147,243✔
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:
899,319✔
819
      return false;
899,319✔
820
   }
821
}
822

823
tree_t aliased_type_decl(tree_t decl)
181,533✔
824
{
825
   switch (tree_kind(decl)) {
182,803✔
826
   case T_ALIAS:
1,302✔
827
      {
828
         tree_t value = tree_value(decl);
1,302✔
829
         const tree_kind_t kind = tree_kind(value);
1,302✔
830
         if (kind == T_REF && tree_has_ref(value))
1,302✔
831
            return aliased_type_decl(tree_ref(value));
1,270✔
832
         else if (kind == T_ATTR_REF && is_type_attribute(tree_subkind(value)))
32✔
833
             return value;
834
         else
835
            return NULL;
30✔
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:
2,275✔
843
      if (tree_class(decl) == C_TYPE)
2,275✔
844
         return decl;
845
      else
846
         return NULL;
1,181✔
847
   default:
63,104✔
848
      return NULL;
63,104✔
849
   }
850
}
851

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

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

869
   tree_add_param(call, p);
199,784✔
870
   return p;
199,784✔
871
}
872

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

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

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

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

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

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

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

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

906
      for (int i = from_dim; i < ndims; i++) {
710✔
907
         tree_t r = range_of(array, i);
371✔
908
         if (r == NULL) {
371✔
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));
371✔
916
         tree_add_range(constraint, r);
371✔
917
      }
918

919
      return sub;
920
   }
921
}
922

923
unsigned bits_for_range(int64_t low, int64_t high)
2,464,016✔
924
{
925
   if (low > high)
2,464,016✔
926
      return 0;   // Null range
927
   else if (low < 0) {
2,464,016✔
928
      // Signed integers
929
      if (low >= INT8_MIN && high <= INT8_MAX)
642,485✔
930
         return 8;
931
      else if (low >= INT16_MIN && high <= INT16_MAX)
642,425✔
932
         return 16;
933
      else if (low >= INT32_MIN && high <= INT32_MAX)
642,304✔
934
         return 32;
935
      else
936
         return 64;
95,162✔
937
   }
938
   else {
939
      // Unsigned integers
940
      if (high <= 1)
1,821,531✔
941
         return 1;
942
      else if (high <= UINT8_MAX)
1,133,436✔
943
         return 8;
944
      else if (high <= UINT16_MAX)
3,298✔
945
         return 16;
946
      else if (high <= UINT32_MAX)
3,093✔
947
         return 32;
948
      else
949
         return 64;
164✔
950
   }
951
}
952

953
unsigned dimension_of(type_t type)
2,127,794✔
954
{
955
   switch (type_kind(type)) {
4,036,778✔
956
   case T_SUBTYPE:
1,908,982✔
957
      return dimension_of(type_base(type));
1,908,982✔
958
   case T_GENERIC:
428✔
959
      switch (type_subkind(type)) {
428✔
960
      case GTYPE_ARRAY:
378✔
961
         return type_indexes(type);
378✔
962
      case GTYPE_ACCESS:
×
963
         return dimension_of(type_designated(type));
×
964
      case GTYPE_FILE:
965
      case GTYPE_PRIVATE:
966
         return 0;
967
      default:
50✔
968
         return 1;
50✔
969
      }
970
   case T_ARRAY:
2,118,320✔
971
      return type_indexes(type);
2,118,320✔
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:
8,957✔
979
   case T_REAL:
980
   case T_PHYSICAL:
981
   case T_ENUM:
982
      return type_dims(type);
8,957✔
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)
3,211,512✔
992
{
993
   switch (type_kind(type)) {
3,253,674✔
994
   case T_SUBTYPE:
2,592,139✔
995
      if (type_has_constraint(type)) {
2,592,139✔
996
         tree_t c = type_constraint(type);
2,549,977✔
997
         switch (tree_subkind(c)) {
2,549,977✔
998
         case C_INDEX:
2,549,977✔
999
         case C_RANGE:
1000
            if (dim < tree_ranges(c))
2,549,977✔
1001
               return tree_range(c, dim);
2,549,976✔
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);
42,162✔
1010
   case T_INTEGER:
661,535✔
1011
   case T_REAL:
1012
   case T_PHYSICAL:
1013
   case T_ENUM:
1014
      return type_dim(type, dim);
661,535✔
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)
31,543✔
1022
{
1023
   switch (type_kind(type)) {
31,647✔
1024
   case T_ENUM:
1025
      return RANGE_TO;
1026
   case T_NONE:
×
1027
      return RANGE_ERROR;
×
1028
   case T_INTEGER:
31,521✔
1029
   case T_REAL:
1030
   case T_PHYSICAL:
1031
   case T_SUBTYPE:
1032
      {
1033
         tree_t r = range_of(type, dim);
31,521✔
1034
         const range_kind_t rkind = tree_subkind(r);
31,521✔
1035
         if (rkind == RANGE_EXPR) {
31,521✔
1036
            // Return a fixed direction if possible
1037
            tree_t value = tree_value(r);
196✔
1038
            assert(tree_kind(value) == T_ATTR_REF);
196✔
1039

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

1045
            tree_t name = tree_name(value);
196✔
1046
            if (tree_kind(name) == T_REF && tree_has_ref(name)) {
196✔
1047
               tree_t decl = tree_ref(name);
195✔
1048
               if (is_type_decl(decl))
195✔
1049
                  return direction_of(tree_type(decl), 0);
104✔
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)
306,436✔
1062
{
1063
   if (dim >= dimension_of(type))
306,438✔
1064
      return NULL;
1065

1066
   type_t base = type_base_recur(type);
306,395✔
1067
   type_kind_t base_kind = type_kind(base);
306,395✔
1068
   if (base_kind == T_ARRAY)
306,395✔
1069
      return type_index(base, dim);
302,005✔
1070
   else if (base_kind == T_ENUM || base_kind == T_NONE)
4,390✔
1071
      return type;
1072
   else if (base_kind == T_GENERIC && type_subkind(base) == GTYPE_ARRAY)
4,195✔
1073
      return type_index(base, dim);
228✔
1074
   else if (base_kind == T_RECORD || base_kind == T_GENERIC)
3,967✔
1075
      return NULL;
1076
   else if (base_kind == T_ACCESS)
3,942✔
1077
      return index_type_of(type_designated(type), dim);
2✔
1078
   else
1079
      return tree_type(range_of(base, dim));
3,940✔
1080
}
1081

1082
int64_t rebase_index(type_t array_type, int dim, int64_t value)
320✔
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);
320✔
1086
   const int64_t left = assume_int(tree_left(r));
320✔
1087
   return (tree_subkind(r) == RANGE_TO) ? value - left : left - value;
320✔
1088
}
1089

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

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

1101
void intern_strings(void)
8,562✔
1102
{
1103
   static const char *tab[] = {
8,562✔
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_UNSIGNED]    = "$unsigned",
1155
      [W_DLR_CLOG2]       = "$clog2",
1156
      [W_COUNTERS]        = "#counters",
1157

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

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

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

1205
   for (int i = 0; i < ARRAY_LEN(tab); i++)
804,828✔
1206
      id_cache[i] = ident_intern(i, tab[i]);
796,266✔
1207
}
8,562✔
1208

1209
bool is_uninstantiated_package(tree_t pack)
55,515✔
1210
{
1211
   return tree_kind(pack) == T_PACKAGE
55,515✔
1212
      && tree_generics(pack) > 0
53,608✔
1213
      && tree_genmaps(pack) == 0;
58,102✔
1214
}
1215

1216
bool is_uninstantiated_subprogram(tree_t decl)
126,455✔
1217
{
1218
   switch (tree_kind(decl)) {
126,455✔
1219
   case T_FUNC_DECL:
125,697✔
1220
   case T_FUNC_BODY:
1221
   case T_PROC_DECL:
1222
   case T_PROC_BODY:
1223
      return tree_generics(decl) > 0;
125,697✔
1224
   default:
1225
      return false;
1226
   }
1227
}
1228

1229
bool is_anonymous_subtype(type_t type)
273,843✔
1230
{
1231
   return type_kind(type) == T_SUBTYPE && !type_has_ident(type);
273,843✔
1232
}
1233

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

1251
bool package_needs_body(tree_t pack)
17,688✔
1252
{
1253
   assert(tree_kind(pack) == T_PACKAGE);
17,688✔
1254

1255
   const int ndecls = tree_decls(pack);
17,688✔
1256
   for (int i = 0; i < ndecls; i++) {
1,197,380✔
1257
      tree_t d = tree_decl(pack, i);
1,195,670✔
1258
      const tree_kind_t dkind = tree_kind(d);
1,195,670✔
1259
      if ((dkind == T_FUNC_DECL || dkind == T_PROC_DECL)
1,195,670✔
1260
          && !(tree_flags(d) & TREE_F_PREDEFINED))
1,072,820✔
1261
         return true;
1262
      else if (dkind == T_CONST_DECL && !tree_has_value(d))
1,180,466✔
1263
         return true;
1264
      else if (dkind == T_PROT_DECL)
1,179,862✔
1265
         return true;
1266
   }
1267

1268
   return false;
1269
}
1270

1271
static tree_t cached_unit(tree_t hint, tree_t *cache, well_known_t lib_name,
19,850✔
1272
                          well_known_t unit_name)
1273
{
1274
   const vhdl_standard_t curr = standard();
19,850✔
1275

1276
   if (cache[curr] == NULL) {
19,850✔
1277
      if (hint != NULL)
6,181✔
1278
         cache[curr] = hint;
1,891✔
1279
      else {
1280
         lib_t std = lib_require(well_known(lib_name));
4,290✔
1281
         cache[curr] = lib_get(std, well_known(unit_name));
4,290✔
1282
         assert(cache[curr] != NULL);
4,290✔
1283
      }
1284
   }
1285

1286
   assert(hint == NULL || hint == cache[curr]);
19,850✔
1287
   return cache[curr];
19,850✔
1288
}
1289

1290
static tree_t cached_std(tree_t hint)
18,350✔
1291
{
1292
   static tree_t standard_cache[STD_19 + 1] = {};
18,350✔
1293
   return cached_unit(hint, standard_cache, W_STD, W_STD_STANDARD);
18,350✔
1294
}
1295

1296
static tree_t search_type_decls(tree_t container, ident_t name)
19,232✔
1297
{
1298
   const int ndecls = tree_decls(container);
19,232✔
1299

1300
   for (int i = 0; i < ndecls; i++) {
998,460✔
1301
      tree_t d = tree_decl(container, i);
998,460✔
1302
      if (is_type_decl(d) && tree_ident(d) == name)
998,460✔
1303
         return d;
19,232✔
1304
   }
1305

1306
   return NULL;
1307
}
1308

1309
type_t std_type(tree_t std, std_type_t which)
1,230,744✔
1310
{
1311
   static type_t cache[STD_FILE_OPEN_STATE + 1] = {};
1,230,744✔
1312
   assert(which < ARRAY_LEN(cache));
1,230,744✔
1313

1314
   if (cache[which] == NULL) {
1,230,744✔
1315
      const char *names[] = {
18,350✔
1316
         "universal_integer",
1317
         "universal_real",
1318
         "INTEGER",
1319
         "REAL",
1320
         "BOOLEAN",
1321
         "STRING",
1322
         "TIME",
1323
         "BIT",
1324
         "FILE_OPEN_KIND",
1325
         "FILE_OPEN_STATUS",
1326
         "NATURAL",
1327
         "BIT_VECTOR",
1328
         "SEVERITY_LEVEL",
1329
         "FILE_ORIGIN_KIND",
1330
         "FILE_OPEN_STATE",
1331
      };
1332

1333
      tree_t d = search_type_decls(cached_std(std), ident_new(names[which]));
18,350✔
1334
      if (d == NULL)
18,350✔
1335
         fatal_trace("cannot find standard type %s", names[which]);
1336

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

1342
      if (can_cache)
18,350✔
1343
         return (cache[which] = tree_type(d));
18,243✔
1344
      else
1345
         return tree_type(d);
107✔
1346
   }
1347
   else
1348
      return cache[which];
1349
}
1350

1351
type_t ieee_type(ieee_type_t which)
2,608✔
1352
{
1353
   static type_t cache[IEEE_STD_LOGIC_VECTOR + 1] = {};
2,608✔
1354
   assert(which < ARRAY_LEN(cache));
2,608✔
1355

1356
   if (cache[which] == NULL) {
2,608✔
1357
      static const char *const names[] = {
396✔
1358
         [IEEE_STD_ULOGIC] = "STD_ULOGIC",
1359
         [IEEE_STD_LOGIC] = "STD_LOGIC",
1360
         [IEEE_STD_ULOGIC_VECTOR] = "STD_ULOGIC_VECTOR",
1361
         [IEEE_STD_LOGIC_VECTOR] = "STD_LOGIC_VECTOR",
1362
      };
1363

1364
      static tree_t ieee_cache[STD_19 + 1] = {};
396✔
1365
      tree_t unit = cached_unit(NULL, ieee_cache, W_IEEE, W_IEEE_1164);
396✔
1366

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

1371
      // STD.STANDARD cannot depend on IEEE
1372
      assert(!opt_get_int(OPT_BOOTSTRAP));
396✔
1373

1374
      return (cache[which] = tree_type(d));
396✔
1375
   }
1376
   else
1377
      return cache[which];
1378
}
1379

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

1386
type_t verilog_type(verilog_type_t which)
1,417✔
1387
{
1388
   static type_t cache[VERILOG_WIRE_ARRAY + 1] = {};
1,417✔
1389
   assert(which < ARRAY_LEN(cache));
1,417✔
1390

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

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

1406
      // STD.STANDARD cannot depend on NVC.VERILOG
1407
      assert(!opt_get_int(OPT_BOOTSTRAP));
431✔
1408

1409
      return (cache[which] = tree_type(d));
431✔
1410
   }
1411
   else
1412
      return cache[which];
1413
}
1414

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

1420
   if (cache[which] == NULL) {
281✔
1421
      static const char *const names[] = {
55✔
1422
         [REFLECT_VALUE_MIRROR] = "VALUE_MIRROR",
1423
         [REFLECT_SUBTYPE_MIRROR] = "SUBTYPE_MIRROR",
1424
      };
1425

1426
      static tree_t reflect_cache[STD_19 + 1] = {};
55✔
1427
      tree_t unit = cached_unit(NULL, reflect_cache, W_STD, W_STD_REFLECTION);
55✔
1428

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

1433
      // STD.STANDARD cannot depend on REFLECTION
1434
      assert(!opt_get_int(OPT_BOOTSTRAP));
55✔
1435

1436
      return (cache[which] = tree_type(d));
55✔
1437
   }
1438
   else
1439
      return cache[which];
1440
}
1441

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

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

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

1508
   return NULL;
1509
}
1510

1511
tree_t verilog_func(ident_t mangled)
618✔
1512
{
1513
   tree_t pack = cached_verilog();
618✔
1514

1515
   const int ndecls = tree_decls(pack);
618✔
1516
   for (int i = 0; i < ndecls; i++) {
66,114✔
1517
      tree_t d = tree_decl(pack, i);
66,114✔
1518
      if (is_subprogram(d) && tree_ident2(d) == mangled)
66,114✔
1519
         return d;
618✔
1520
   }
1521

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

1525
tree_t name_to_ref(tree_t name)
104,241✔
1526
{
1527
   tree_kind_t kind;
104,241✔
1528
   while ((kind = tree_kind(name)) != T_REF) {
117,435✔
1529
      switch (kind) {
14,676✔
1530
      case T_ARRAY_REF:
13,194✔
1531
      case T_ARRAY_SLICE:
1532
      case T_RECORD_REF:
1533
      case T_ALL:
1534
         name = tree_value(name);
13,194✔
1535
         break;
13,194✔
1536
      default:
1537
         return NULL;
1538
      }
1539
   }
1540

1541
   return name;
1542
}
1543

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

1553
void mangle_one_type(text_buf_t *buf, type_t type)
218,159✔
1554
{
1555
   ident_t ident = type_ident(type);
218,159✔
1556

1557
   char code = 0;
218,159✔
1558
   switch (is_well_known(ident)) {
218,159✔
1559
   case W_STD_INTEGER:        code = 'I'; break;
1560
   case W_STD_STRING:         code = 'S'; break;
4,433✔
1561
   case W_STD_REAL:           code = 'R'; break;
4,057✔
1562
   case W_STD_BOOL:           code = 'B'; break;
29,710✔
1563
   case W_STD_CHAR:           code = 'C'; break;
730✔
1564
   case W_STD_TIME:           code = 'T'; break;
1,218✔
1565
   case W_STD_NATURAL:        code = 'N'; break;
4,846✔
1566
   case W_STD_POSITIVE:       code = 'P'; break;
475✔
1567
   case W_STD_BIT:            code = 'J'; break;
3,355✔
1568
   case W_STD_BIT_VECTOR:     code = 'Q'; break;
3,570✔
1569
   case W_IEEE_LOGIC:         code = 'L'; break;
421✔
1570
   case W_IEEE_ULOGIC:        code = 'U'; break;
5,518✔
1571
   case W_IEEE_LOGIC_VECTOR:  code = 'V'; break;
2,331✔
1572
   case W_IEEE_ULOGIC_VECTOR: code = 'Y'; break;
1,469✔
1573
   default: break;
1574
   }
1575

1576
   if (code)
62,133✔
1577
      tb_append(buf, code);
77,962✔
1578
   else {
1579
      tb_printf(buf, "%zu", ident_len(ident));
140,197✔
1580
      tb_istr(buf, ident);
140,197✔
1581
   }
1582
}
218,159✔
1583

1584
ident_t get_call_context(ident_t mangled)
4,448✔
1585
{
1586
   const char *str = istr(mangled), *p = str, *end = NULL;
4,448✔
1587
   for (; *p; p++) {
163,042✔
1588
      if (*p == '(') break;
155,467✔
1589
      if (*p == '.') end = p;
154,146✔
1590
   }
1591
   assert(end != NULL);
4,448✔
1592

1593
   return ident_new_n(str, end - str);
4,448✔
1594
}
1595

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

1616
unsigned get_case_choice_char(tree_t value, int depth)
14,945✔
1617
{
1618
   switch (tree_kind(value)) {
15,765✔
1619
   case T_STRING:
14,923✔
1620
      if (depth < tree_chars(value))
14,923✔
1621
         return assume_int(tree_char(value, depth));
14,362✔
1622
      else
1623
         return ~0;   // Out of bounds
1624

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

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

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

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

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

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

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

1661
                  pos += left_len;
8✔
1662
               }
1663
               break;
8✔
1664

UNCOV
1665
            case A_OTHERS:
×
1666
               return assume_int(tree_value(a));
×
1667

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

UNCOV
1673
                  const int64_t rleft = assume_int(tree_left(r));
×
1674
                  const int64_t rright = assume_int(tree_right(r));
×
1675

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

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

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

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

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

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

1712
int64_t encode_case_choice(tree_t value, int length, int bits)
1,925✔
1713
{
1714
   uint64_t enc = 0;
1,925✔
1715
   for (int i = 0; i < length; i++) {
16,832✔
1716
      if (bits > 0) {
14,907✔
1717
         enc <<= bits;
5,124✔
1718
         enc |= get_case_choice_char(value, i);
5,124✔
1719
      }
1720
      else {
1721
         enc *= 0x27d4eb2d;
9,783✔
1722
         enc += get_case_choice_char(value, i);
9,783✔
1723
      }
1724
   }
1725

1726
   return enc;
1,925✔
1727
}
1728

1729
void to_string(text_buf_t *tb, type_t type, int64_t value)
723✔
1730
{
1731
   if (type_is_integer(type))
723✔
1732
      tb_printf(tb, "%"PRIi64, value);
556✔
1733
   else if (type_is_enum(type)) {
167✔
1734
      type_t base = type_base_recur(type);
70✔
1735
      if (value < 0 || value >= type_enum_literals(base))
70✔
1736
         tb_printf(tb, "%"PRIi64, value);
4✔
1737
      else
1738
         tb_cat(tb, istr(tree_ident(type_enum_literal(base, value))));
66✔
1739
   }
1740
   else if (type_is_physical(type)) {
97✔
1741
      type_t base = type_base_recur(type);
27✔
1742
      const unsigned nunits = type_units(base);
27✔
1743
      tree_t max_unit = NULL;
27✔
1744
      int64_t max_unit_value = 0;
27✔
1745

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

1757
      tb_printf(tb, "%"PRIi64" %s", value / max_unit_value,
27✔
1758
                istr(tree_ident(max_unit)));
1759
   }
1760
   else if (type_is_real(type)) {
70✔
1761
      union { int64_t i; double r; } u = { .i = value };
62✔
1762
      tb_printf(tb, "%.17g", u.r);
62✔
1763
   }
1764
   else if (type_is_access(type)) {
8✔
1765
      if (value == 0)
8✔
1766
         tb_cat(tb, "NULL");
4✔
1767
      else
1768
         tb_printf(tb, "%p", (void *)value);
4✔
1769
   }
1770
}
723✔
1771

1772
static bool is_static(tree_t expr)
3,448✔
1773
{
1774
   switch (tree_kind(expr)) {
3,511✔
1775
   case T_REF:
882✔
1776
      {
1777
         tree_t decl = tree_ref(expr);
882✔
1778
         switch (tree_kind(decl)) {
882✔
1779
         case T_CONST_DECL:
178✔
1780
            return !!(tree_flags(decl) & TREE_F_GLOBALLY_STATIC);
178✔
1781
         case T_UNIT_DECL:
1782
         case T_ENUM_LIT:
1783
         case T_GENERIC_DECL:
1784
            return true;
UNCOV
1785
         case T_ALIAS:
×
1786
            return is_static(tree_value(decl));
×
1787
         default:
257✔
1788
            return false;
257✔
1789
         }
1790
      }
1791

1792
   case T_LITERAL:
1793
   case T_STRING:
1794
      return true;
1795

1796
   case T_FCALL:
228✔
1797
      return !!(tree_flags(expr) & (TREE_F_LOCALLY_STATIC
228✔
1798
                                    | TREE_F_GLOBALLY_STATIC));
1799

1800
   case T_RECORD_REF:
59✔
1801
      return is_static(tree_value(expr));
59✔
1802

1803
   case T_ARRAY_REF:
64✔
1804
      {
1805
         if (!is_static(tree_value(expr)))
64✔
1806
            return false;
1807

1808
         const int nparams = tree_params(expr);
64✔
1809
         for (int i = 0; i < nparams; i++) {
128✔
1810
            if (!is_static(tree_value(tree_param(expr, i))))
64✔
1811
               return false;
1812
         }
1813

1814
         return true;
1815
      }
1816

1817
   case T_ARRAY_SLICE:
32✔
1818
      {
1819
         if (!is_static(tree_value(expr)))
32✔
1820
            return false;
1821

1822
         assert(tree_ranges(expr) == 1);
32✔
1823

1824
         tree_t r = tree_range(expr, 0);
32✔
1825
         if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
32✔
UNCOV
1826
            return false;
×
1827

1828
         return true;
1829
      }
1830

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

1866
               switch (tree_kind(tree_ref(ref))) {
35✔
1867
               case T_GENERIC_DECL:
1868
               case T_PORT_DECL:
1869
               case T_SIGNAL_DECL:
1870
               case T_SUBTYPE_DECL:
1871
               case T_TYPE_DECL:
1872
                  return true;
1873
               default:
9✔
1874
                  return false;
9✔
1875
               }
1876
            }
UNCOV
1877
         default:
×
1878
            return true;
×
1879
         }
1880
      }
1881

UNCOV
1882
   default:
×
1883
      return false;
×
1884
   }
1885
}
1886

1887
tree_t longest_static_prefix(tree_t expr)
16,156✔
1888
{
1889
   switch (tree_kind(expr)) {
16,156✔
1890
   case T_ARRAY_REF:
2,337✔
1891
      {
1892
         tree_t value = tree_value(expr);
2,337✔
1893
         tree_t prefix = longest_static_prefix(value);
2,337✔
1894

1895
         if (prefix != value)
2,337✔
1896
            return prefix;
1897

1898
         const int nparams = tree_params(expr);
2,300✔
1899
         for (int i = 0; i < nparams; i++) {
4,550✔
1900
            if (!is_static(tree_value(tree_param(expr, i))))
2,576✔
1901
               return prefix;
1902
         }
1903

1904
         return expr;
1905
      }
1906

1907
   case T_ARRAY_SLICE:
349✔
1908
      {
1909
         tree_t value = tree_value(expr);
349✔
1910
         tree_t prefix = longest_static_prefix(value);
349✔
1911

1912
         if (prefix != value)
349✔
1913
            return prefix;
1914

1915
         assert(tree_ranges(expr) == 1);
340✔
1916

1917
         tree_t r = tree_range(expr, 0);
340✔
1918
         if (tree_subkind(r) == RANGE_EXPR) {
340✔
1919
            if (!is_static(tree_value(r)))
15✔
1920
               return prefix;
6✔
1921
         }
1922
         else if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
325✔
1923
            return prefix;
20✔
1924

1925
         return expr;
1926
      }
1927

1928
   case T_RECORD_REF:
1,021✔
1929
      {
1930
         tree_t value = tree_value(expr);
1,021✔
1931
         tree_t prefix = longest_static_prefix(value);
1,021✔
1932

1933
         if (prefix != value)
1,021✔
1934
            return prefix;
28✔
1935

1936
         return expr;
1937
      }
1938

1939
   default:
1940
      return expr;
1941
   }
1942
}
1943

1944
tree_t body_of(tree_t pack)
16,336✔
1945
{
1946
   const tree_kind_t kind = tree_kind(pack);
16,336✔
1947
   if (kind == T_PACK_INST)
16,336✔
1948
      return NULL;
1949

1950
   assert(tree_kind(pack) == T_PACKAGE);
16,336✔
1951

1952
   ident_t body_i = well_known(W_BODY);
16,336✔
1953
   ident_t body_name = ident_prefix(tree_ident(pack), body_i, '-');
16,336✔
1954
   return lib_get_qualified(body_name);
16,336✔
1955
}
1956

1957
tree_t find_generic_map(tree_t unit, int pos, tree_t g)
932✔
1958
{
1959
   const int ngenmaps = tree_genmaps(unit);
932✔
1960

1961
   if (pos < ngenmaps) {
932✔
1962
      tree_t m = tree_genmap(unit, pos);
754✔
1963
      if (tree_subkind(m) == P_POS && tree_pos(m) == pos)
754✔
1964
         return tree_value(m);
567✔
1965
   }
1966

1967
   for (int j = 0; j < ngenmaps; j++) {
1,297✔
1968
      tree_t m = tree_genmap(unit, j);
1,119✔
1969
      switch (tree_subkind(m)) {
1,119✔
1970
      case P_NAMED:
569✔
1971
         {
1972
            tree_t name = tree_name(m);
569✔
1973
            assert(tree_kind(name) == T_REF);
569✔
1974

1975
            if (tree_has_ref(name) && tree_ref(name) == g)
569✔
1976
               return tree_value(m);
19✔
1977
         }
1978
         break;
1979

1980
      case P_POS:
550✔
1981
         if (tree_pos(m) == pos)
550✔
1982
            return tree_value(m);
168✔
1983
         break;
1984

1985
      default:
1986
         break;
1987
      }
1988
   }
1989

1990
   return NULL;
1991
}
1992

1993
bool relaxed_rules(void)
713,002✔
1994
{
1995
   return opt_get_int(OPT_RELAXED);
713,002✔
1996
}
1997

1998
bool is_type_attribute(attr_kind_t kind)
56,584✔
1999
{
2000
   switch (kind) {
56,584✔
2001
   case ATTR_SUBTYPE:
2002
   case ATTR_BASE:
2003
   case ATTR_ELEMENT:
2004
   case ATTR_DESIGNATED_SUBTYPE:
2005
   case ATTR_INDEX:
2006
      return true;
2007
   default:
55,816✔
2008
      return false;
55,816✔
2009
   }
2010
}
2011

2012
bool attribute_has_param(attr_kind_t attr)
28,187✔
2013
{
2014
   switch (attr) {
28,187✔
2015
   case ATTR_IMAGE:
2016
   case ATTR_SUCC:
2017
   case ATTR_PRED:
2018
   case ATTR_DELAYED:
2019
   case ATTR_LEFTOF:
2020
   case ATTR_RIGHTOF:
2021
   case ATTR_VALUE:
2022
   case ATTR_POS:
2023
   case ATTR_LOW:
2024
   case ATTR_HIGH:
2025
   case ATTR_LEFT:
2026
   case ATTR_RIGHT:
2027
   case ATTR_LENGTH:
2028
   case ATTR_RANGE:
2029
   case ATTR_REVERSE_RANGE:
2030
   case ATTR_VAL:
2031
   case ATTR_QUIET:
2032
   case ATTR_STABLE:
2033
   case ATTR_INDEX:
2034
   case ATTR_ASCENDING:
2035
      return true;
2036
   default:
3,053✔
2037
      return false;
3,053✔
2038
   }
2039
}
2040

2041
type_t get_type_or_null(tree_t t)
4,630,412✔
2042
{
2043
   switch (tree_kind(t)) {
4,630,412✔
2044
   case T_LIBRARY:
2045
   case T_ATTR_SPEC:
2046
   case T_PACKAGE:
2047
   case T_PACK_INST:
2048
   case T_PACK_BODY:
2049
   case T_ENTITY:
2050
   case T_ARCH:
2051
   case T_PROCESS:
2052
   case T_COMPONENT:
2053
   case T_INSTANCE:
2054
   case T_CONCURRENT:
2055
   case T_BLOCK:
2056
   case T_WHILE:
2057
   case T_FOR:
2058
   case T_LOOP:
2059
   case T_GROUP_TEMPLATE:
2060
   case T_CONFIGURATION:
2061
   case T_GROUP:
2062
   case T_FOR_GENERATE:
2063
   case T_IF_GENERATE:
2064
   case T_CASE_GENERATE:
2065
   case T_USE:
2066
   case T_CONTEXT:
2067
   case T_PSL_DECL:
2068
   case T_PSL_DIRECT:
2069
   case T_WAVEFORM:
2070
      return NULL;
2071
   default:
4,508,710✔
2072
      if (tree_has_type(t))
4,508,710✔
2073
         return tree_type(t);
4,500,318✔
2074
      else
2075
         return NULL;
2076
   }
2077
}
2078

2079
type_t subtype_for_string(tree_t str, type_t base)
33,061✔
2080
{
2081
   if (type_const_bounds(base))
33,061✔
2082
      return base;    // Can be checked statically
2083
   else if (!type_is_unconstrained(base))
28,923✔
2084
      base = type_base_recur(base);
355✔
2085

2086
   // Construct a new constrained array subtype: the direction and
2087
   // bounds are the same as those for a positional array aggregate
2088
   type_t sub = type_new(T_SUBTYPE);
28,923✔
2089
   type_set_base(sub, base);
28,923✔
2090

2091
   type_t index_type = index_type_of(base, 0);
28,923✔
2092
   const bool is_enum = type_is_enum(index_type);
28,923✔
2093

2094
   // The direction is determined by the index type
2095
   range_kind_t dir = direction_of(index_type, 0);
28,923✔
2096
   tree_t index_r = range_of(index_type, 0);
28,923✔
2097

2098
   // The left bound is the left of the index type and the right bound
2099
   // is determined by the number of elements
2100

2101
   tree_t left = NULL, right = NULL;
28,923✔
2102
   const int nchars = tree_chars(str);
28,923✔
2103

2104
   if (is_enum) {
28,923✔
2105
      const int nlits = type_enum_literals(type_base_recur(index_type));
16✔
2106
      int64_t index_left = assume_int(tree_left(index_r));
16✔
2107

2108
      int64_t iright, ileft;
16✔
2109
      if (nchars == 0) {
16✔
2110
         iright = index_left;
1✔
2111
         ileft = assume_int(tree_right(index_r));
1✔
2112
      }
2113
      else if (dir == RANGE_DOWNTO) {
15✔
UNCOV
2114
         ileft = index_left;
×
2115
         iright = MIN(nlits - 1, MAX(0, index_left - nchars + 1));
×
2116
      }
2117
      else {
2118
         ileft = index_left;
15✔
2119
         iright = MIN(nlits - 1, MAX(0, index_left + nchars - 1));
15✔
2120
      }
2121

2122
      left = get_enum_lit(str, index_type, ileft);
16✔
2123
      right = get_enum_lit(str, index_type, iright);
16✔
2124
   }
2125
   else {
2126
      left = tree_left(index_r);
28,907✔
2127

2128
      int64_t iright;
28,907✔
2129
      if (dir == RANGE_DOWNTO)
28,907✔
UNCOV
2130
         iright = assume_int(left) - nchars + 1;
×
2131
      else
2132
         iright = assume_int(left) + nchars - 1;
28,907✔
2133

2134
      right = get_int_lit(str, index_type, iright);
28,907✔
2135
   }
2136

2137
   tree_t r = tree_new(T_RANGE);
28,923✔
2138
   tree_set_subkind(r, dir);
28,923✔
2139
   tree_set_left(r, left);
28,923✔
2140
   tree_set_right(r, right);
28,923✔
2141
   tree_set_loc(r, tree_loc(str));
28,923✔
2142
   tree_set_type(r, index_type);
28,923✔
2143

2144
   tree_t c = tree_new(T_CONSTRAINT);
28,923✔
2145
   tree_set_subkind(c, C_INDEX);
28,923✔
2146
   tree_add_range(c, r);
28,923✔
2147
   tree_set_loc(c, tree_loc(str));
28,923✔
2148

2149
   type_set_constraint(sub, c);
28,923✔
2150

2151
   return sub;
28,923✔
2152
}
2153

2154
tree_t change_ref(tree_t name, tree_t new)
2,841✔
2155
{
2156
   switch (tree_kind(name)) {
2,841✔
2157
   case T_REF:
1,879✔
2158
      {
2159
         tree_t ref = make_ref(new);
1,879✔
2160
         tree_set_loc(ref, tree_loc(name));
1,879✔
2161
         return ref;
1,879✔
2162
      }
2163

2164
   case T_ARRAY_REF:
146✔
2165
      {
2166
         tree_t value = change_ref(tree_value(name), new);
146✔
2167

2168
         tree_t t = tree_new(T_ARRAY_REF);
146✔
2169
         tree_set_loc(t, tree_loc(name));
146✔
2170
         tree_set_value(t, value);
146✔
2171
         tree_set_type(t, type_elem(tree_type(value)));
146✔
2172

2173
         const int nparams = tree_params(name);
146✔
2174
         for (int i = 0; i < nparams; i++)
292✔
2175
            tree_add_param(t, tree_param(name, i));
146✔
2176

2177
         return t;
2178
      }
2179

2180
   case T_ARRAY_SLICE:
61✔
2181
      {
2182
         tree_t value = change_ref(tree_value(name), new);
61✔
2183
         tree_t r = tree_range(name, 0);
61✔
2184

2185
         tree_t constraint = tree_new(T_CONSTRAINT);
61✔
2186
         tree_set_subkind(constraint, C_INDEX);
61✔
2187
         tree_add_range(constraint, r);
61✔
2188

2189
         type_t slice_type = type_new(T_SUBTYPE);
61✔
2190
         type_set_constraint(slice_type, constraint);
61✔
2191
         type_set_base(slice_type, tree_type(value));
61✔
2192

2193
         tree_t t = tree_new(T_ARRAY_SLICE);
61✔
2194
         tree_set_loc(t, tree_loc(name));
61✔
2195
         tree_set_value(t, value);
61✔
2196
         tree_set_type(t, slice_type);
61✔
2197
         tree_add_range(t, r);
61✔
2198

2199
         return t;
61✔
2200
      }
2201

2202
   case T_RECORD_REF:
534✔
2203
      {
2204
         tree_t t = tree_new(T_RECORD_REF);
534✔
2205
         tree_set_loc(t, tree_loc(name));
534✔
2206
         tree_set_value(t, change_ref(tree_value(name), new));
534✔
2207
         tree_set_type(t, tree_type(name));
534✔
2208
         tree_set_ident(t, tree_ident(name));
534✔
2209
         tree_set_ref(t, tree_ref(name));
534✔
2210

2211
         return t;
534✔
2212
      }
2213

2214
   case T_CONV_FUNC:
209✔
2215
      {
2216
         tree_t t = tree_new(T_CONV_FUNC);
209✔
2217
         tree_set_loc(t, tree_loc(name));
209✔
2218
         tree_set_value(t, change_ref(tree_value(name), new));
209✔
2219
         tree_set_ident(t, tree_ident(name));
209✔
2220
         tree_set_type(t, tree_type(name));
209✔
2221
         tree_set_ref(t, tree_ref(name));
209✔
2222

2223
         return t;
209✔
2224
      }
2225

2226
   case T_TYPE_CONV:
12✔
2227
      {
2228
         tree_t t = tree_new(T_TYPE_CONV);
12✔
2229
         tree_set_loc(t, tree_loc(name));
12✔
2230
         tree_set_type(t, tree_type(name));
12✔
2231
         tree_set_value(t, change_ref(tree_value(name), new));
12✔
2232

2233
         return t;
12✔
2234
      }
2235

UNCOV
2236
   default:
×
2237
      fatal_trace("cannot handle tree kind %s in elab_change_ref",
2238
                  tree_kind_str(tree_kind(name)));
2239
   }
2240
}
2241

2242
static void build_wait_for_target(tree_t expr, build_wait_fn_t fn, void *ctx)
3,983✔
2243
{
2244
   switch (tree_kind(expr)) {
3,983✔
2245
   case T_ARRAY_SLICE:
108✔
2246
      build_wait(tree_range(expr, 0), fn, ctx);
108✔
2247
      break;
108✔
2248

2249
   case T_ARRAY_REF:
756✔
2250
      {
2251
         const int nparams = tree_params(expr);
756✔
2252
         for (int i = 0; i < nparams; i++)
1,512✔
2253
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
756✔
2254
      }
2255
      break;
2256

2257
   default:
2258
      break;
2259
   }
2260
}
3,983✔
2261

2262
void build_wait(tree_t expr, build_wait_fn_t fn, void *ctx)
18,680✔
2263
{
2264
   // LRM 08 section 10.2 has rules for building a wait statement from a
2265
   // sensitivity list. LRM 08 section 11.3 extends these rules to
2266
   // all-sensitised processes.
2267

2268
   switch (tree_kind(expr)) {
23,181✔
2269
   case T_REF:
5,936✔
2270
      if (class_of(tree_ref(expr)) == C_SIGNAL)
5,936✔
2271
         (*fn)(expr, ctx);
3,486✔
2272
      break;
2273

2274
   case T_EXTERNAL_NAME:
10✔
2275
      if (tree_class(expr) == C_SIGNAL)
10✔
2276
         (*fn)(expr, ctx);
10✔
2277
      break;
2278

2279
   case T_WAVEFORM:
3,902✔
2280
   case T_QUALIFIED:
2281
   case T_TYPE_CONV:
2282
   case T_INERTIAL:
2283
      if (tree_has_value(expr))
3,902✔
2284
         build_wait(tree_value(expr), fn, ctx);
3,890✔
2285
      break;
2286

2287
   case T_ASSERT:
530✔
2288
      build_wait(tree_value(expr), fn, ctx);
530✔
2289
      // Fall-through
2290
   case T_REPORT:
535✔
2291
      if (tree_has_message(expr))
535✔
2292
         build_wait(tree_message(expr), fn, ctx);
371✔
2293
      break;
2294

2295
   case T_ARRAY_REF:
1,077✔
2296
   case T_ARRAY_SLICE:
2297
   case T_RECORD_REF:
2298
      {
2299
         tree_t ref = name_to_ref(expr);
1,077✔
2300
         if (ref != NULL && class_of(ref) == C_SIGNAL
1,077✔
2301
             && longest_static_prefix(expr) == expr)
685✔
2302
            (*fn)(expr, ctx);
587✔
2303
         else {
2304
            build_wait(tree_value(expr), fn, ctx);
490✔
2305
            build_wait_for_target(expr, fn, ctx);
490✔
2306
         }
2307
      }
2308
      break;
2309

2310
   case T_FCALL:
2,697✔
2311
   case T_PCALL:
2312
   case T_PROT_FCALL:
2313
   case T_PROT_PCALL:
2314
      {
2315
         tree_t decl = tree_ref(expr);
2,697✔
2316
         const int nparams = tree_params(expr);
2,697✔
2317
         for (int i = 0; i < nparams; i++) {
7,322✔
2318
            tree_t p = tree_param(expr, i), port;
4,625✔
2319
            switch (tree_subkind(p)) {
4,625✔
2320
            case P_POS:
4,625✔
2321
               port = tree_port(decl, tree_pos(p));
4,625✔
2322
               break;
4,625✔
UNCOV
2323
            case P_NAMED:
×
2324
               port = tree_ref(name_to_ref(tree_name(p)));
×
2325
               break;
×
2326
            default:
×
2327
               should_not_reach_here();
2328
            }
2329
            assert(tree_kind(port) == T_PARAM_DECL);
4,625✔
2330

2331
            switch (tree_subkind(port)) {
4,625✔
2332
            case PORT_IN:
4,616✔
2333
            case PORT_INOUT:
2334
            case PORT_ARRAY_VIEW:
2335
            case PORT_RECORD_VIEW:
2336
               build_wait(tree_value(p), fn, ctx);
4,616✔
2337
               break;
4,616✔
2338
            default:
2339
               break;
2340
            }
2341
         }
2342
      }
2343
      break;
2344

2345
   case T_AGGREGATE:
508✔
2346
      {
2347
         const int nassocs = tree_assocs(expr);
508✔
2348
         for (int i = 0; i < nassocs; i++) {
1,844✔
2349
            tree_t a = tree_assoc(expr, i);
1,336✔
2350
            build_wait(tree_value(a), fn, ctx);
1,336✔
2351

2352
            switch (tree_subkind(a)) {
1,336✔
2353
            case A_RANGE:
70✔
2354
            case A_SLICE:
2355
               build_wait(tree_range(a, 0), fn, ctx);
70✔
2356
               break;
70✔
2357
            case A_NAMED:
124✔
2358
               build_wait(tree_name(a), fn, ctx);
124✔
2359
               break;
124✔
2360
            }
2361
         }
2362
      }
2363
      break;
2364

2365
   case T_ATTR_REF:
494✔
2366
      {
2367
         const attr_kind_t predef = tree_subkind(expr);
494✔
2368
         if (predef == ATTR_EVENT || predef == ATTR_ACTIVE)
494✔
2369
            build_wait(tree_name(expr), fn, ctx);
230✔
2370

2371
         const int nparams = tree_params(expr);
494✔
2372
         for (int i = 0; i < nparams; i++)
518✔
2373
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
24✔
2374
      }
2375
      break;
2376

2377
   case T_LITERAL:
2378
   case T_STRING:
2379
   case T_DUMMY_DRIVER:
2380
      break;
2381

2382
   case T_IF:
310✔
2383
      {
2384
         const int nconds = tree_conds(expr);
310✔
2385
         for (int i = 0; i < nconds; i++)
846✔
2386
            build_wait(tree_cond(expr, i), fn, ctx);
536✔
2387
      }
2388
      break;
2389

2390
   case T_COND_STMT:
540✔
2391
      {
2392
         if (tree_has_value(expr))
540✔
2393
            build_wait(tree_value(expr), fn, ctx);
362✔
2394

2395
         const int nstmts = tree_stmts(expr);
540✔
2396
         for (int i = 0; i < nstmts; i++)
1,082✔
2397
            build_wait(tree_stmt(expr, i), fn, ctx);
542✔
2398
      }
2399
      break;
2400

2401
   case T_COND_VALUE:
4✔
2402
      {
2403
         const int nconds = tree_conds(expr);
4✔
2404
         for (int i = 0; i < nconds; i++)
16✔
2405
            build_wait(tree_cond(expr, i), fn, ctx);
12✔
2406
         break;
2407
      }
2408

2409
   case T_COND_EXPR:
12✔
2410
      {
2411
         if (tree_has_value(expr))
12✔
2412
            build_wait(tree_value(expr), fn, ctx);
8✔
2413

2414
         build_wait(tree_result(expr), fn, ctx);
12✔
2415
         break;
12✔
2416
      }
2417

2418
   case T_PROCESS:
70✔
2419
   case T_SEQUENCE:
2420
   case T_PROC_BODY:
2421
      {
2422
         const int ndecls = tree_decls(expr);
70✔
2423
         for (int i = 0; i < ndecls; i++) {
93✔
2424
            tree_t d = tree_decl(expr, i);
23✔
2425
            if (tree_kind(d) == T_PROC_BODY)
23✔
2426
               build_wait(d, fn, ctx);
2✔
2427
         }
2428

2429
         const int nstmts = tree_stmts(expr);
70✔
2430
         for (int i = 0; i < nstmts; i++)
159✔
2431
            build_wait(tree_stmt(expr, i), fn, ctx);
89✔
2432
      }
2433
      break;
2434

2435
   case T_SIGNAL_ASSIGN:
3,468✔
2436
      {
2437
         build_wait_for_target(tree_target(expr), fn, ctx);
3,468✔
2438

2439
         const int nwaves = tree_waveforms(expr);
3,468✔
2440
         for (int i = 0; i < nwaves; i++)
7,167✔
2441
            build_wait(tree_waveform(expr, i), fn, ctx);
3,699✔
2442
      }
2443
      break;
2444

2445
   case T_VAR_ASSIGN:
21✔
2446
   case T_FORCE:
2447
      build_wait_for_target(tree_target(expr), fn, ctx);
21✔
2448
      build_wait(tree_value(expr), fn, ctx);
21✔
2449
      break;
21✔
2450

2451
   case T_RELEASE:
4✔
2452
      build_wait_for_target(tree_target(expr), fn, ctx);
4✔
2453
      break;
4✔
2454

2455
   case T_CASE:
61✔
2456
   case T_MATCH_CASE:
2457
      {
2458
         build_wait(tree_value(expr), fn, ctx);
61✔
2459

2460
         const int nstmts = tree_stmts(expr);
61✔
2461
         for (int i = 0; i < nstmts; i++) {
374✔
2462
            tree_t alt = tree_stmt(expr, i);
313✔
2463

2464
            const int nstmts = tree_stmts(alt);
313✔
2465
            for (int j = 0; j < nstmts; j++)
626✔
2466
               build_wait(tree_stmt(alt, j), fn, ctx);
313✔
2467
         }
2468
      }
2469
      break;
2470

2471
   case T_FOR:
21✔
2472
      {
2473
         build_wait(tree_range(expr, 0), fn, ctx);
21✔
2474

2475
         const int nstmts = tree_stmts(expr);
21✔
2476
         for (int i = 0; i < nstmts; i++)
46✔
2477
            build_wait(tree_stmt(expr, i), fn, ctx);
25✔
2478
      }
2479
      break;
2480

2481
   case T_WHILE:
1✔
2482
      build_wait(tree_value(expr), fn, ctx);
1✔
2483
      // Fall-through
2484
   case T_LOOP:
5✔
2485
      {
2486
         const int nstmts = tree_stmts(expr);
5✔
2487
         for (int i = 0; i < nstmts; i++)
26✔
2488
            build_wait(tree_stmt(expr, i), fn, ctx);
21✔
2489
      }
2490
      break;
2491

2492
   case T_NEXT:
12✔
2493
   case T_EXIT:
2494
      if (tree_has_value(expr))
12✔
2495
         build_wait(tree_value(expr), fn, ctx);
8✔
2496
      break;
2497

2498
   case T_RANGE:
199✔
2499
      if (tree_subkind(expr) == RANGE_EXPR)
199✔
2500
         build_wait(tree_value(expr), fn, ctx);
21✔
2501
      else {
2502
         build_wait(tree_left(expr), fn, ctx);
178✔
2503
         build_wait(tree_right(expr), fn, ctx);
178✔
2504
      }
2505
      break;
2506

2507
   case T_OPEN:
2508
      break;
2509

UNCOV
2510
   default:
×
2511
      fatal_trace("Cannot handle tree kind %s in wait expression",
2512
                  tree_kind_str(tree_kind(expr)));
2513
   }
2514
}
18,680✔
2515

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

2575
   if (highlighting)
2,128✔
2576
      tb_cat(tb, "$$");
56✔
2577

2578
   va_list ap;
2,128✔
2579
   va_start(ap, fmt);
2,128✔
2580

2581
   if (syntax_buf != NULL) {
2,128✔
2582
      ostream_t os = { tb_ostream_write, syntax_buf, CHARSET_ISO88591 };
2,128✔
2583
      nvc_vfprintf(&os, tb_get(tb), ap);
2,128✔
2584
   }
2585
   else
UNCOV
2586
      nvc_vprintf(tb_get(tb), ap);
×
2587

2588
   va_end(ap);
2,128✔
2589
}
2,128✔
2590

2591
void capture_syntax(text_buf_t *tb)
9✔
2592
{
2593
   assert(tb == NULL || syntax_buf == NULL);
9✔
2594
   syntax_buf = tb;
9✔
2595
}
9✔
2596

2597
void analyse_file(const char *file, jit_t *jit, unit_registry_t *ur,
5,527✔
2598
                  mir_context_t *mc)
2599
{
2600
   input_from_file(file);
5,527✔
2601

2602
   switch (source_kind()) {
5,527✔
2603
   case SOURCE_VHDL:
5,027✔
2604
      {
2605
         lib_t work = lib_work();
5,027✔
2606
         int base_errors = 0;
5,027✔
2607
         tree_t unit;
5,027✔
2608
         while (base_errors = error_count(), (unit = parse())) {
19,398✔
2609
            if (error_count() == base_errors) {
14,371✔
2610
               lib_put(work, unit);
14,363✔
2611

2612
               simplify_local(unit, jit, ur, mc);
14,363✔
2613
               bounds_check(unit);
14,363✔
2614
            }
2615
            else
2616
               lib_put_error(work, unit);
8✔
2617
         }
2618
      }
2619
      break;
2620

2621
   case SOURCE_VERILOG:
500✔
2622
      {
2623
         LOCAL_TEXT_BUF tb = tb_new();
1,000✔
2624
         vlog_preprocess(tb, true);
500✔
2625

2626
         file_ref_t file_ref = loc_file_ref(file, NULL);
500✔
2627
         input_from_buffer(tb_get(tb), tb_len(tb), file_ref, SOURCE_VERILOG);
500✔
2628

2629
         lib_t work = lib_work();
500✔
2630
         vlog_node_t module;
500✔
2631
         while ((module = vlog_parse())) {
1,597✔
2632
            if (error_count() == 0) {
597✔
2633
               vlog_check(module);
597✔
2634

2635
               if (error_count() == 0) {
597✔
2636
                  vlog_simp(module);
597✔
2637
                  lib_put_vlog(work, module);
597✔
2638
               }
2639
            }
2640
         }
2641
      }
2642
      break;
500✔
2643

UNCOV
2644
   case SOURCE_SDF:
×
2645
      {
UNCOV
2646
         sdf_file_t *sdf_file = sdf_parse(file, 0);
×
2647
         progress("analysed SDF file: %s", file);
×
2648

UNCOV
2649
         if (sdf_file != NULL) {
×
2650
            warnf("SDF is not yet supported");
×
2651
            sdf_file_free(sdf_file);
×
2652
         }
2653
      }
2654
      break;
2655
   }
2656
}
5,523✔
2657

2658
bool all_character_literals(type_t type)
29,798✔
2659
{
2660
   assert(type_is_enum(type));
29,798✔
2661

2662
   type_t base = type_base_recur(type);
29,798✔
2663
   const int nlits = type_enum_literals(base);
29,798✔
2664
   for (int i = 0; i < nlits; i++) {
155,476✔
2665
      if (ident_char(tree_ident(type_enum_literal(base, i)), 0) != '\'')
136,154✔
2666
         return false;
2667
   }
2668

2669
   return true;
2670
}
2671

2672
bool is_operator_symbol(ident_t ident)
31,787✔
2673
{
2674
   const well_known_t wk = is_well_known(ident);
31,787✔
2675
   if (wk >= NUM_WELL_KNOWN)
31,787✔
2676
      return false;
2677
   else if (standard() < STD_08)
17,163✔
2678
      return wk >= W_OP_AND && wk <= W_OP_NOT;
3,305✔
2679
   else
2680
      return wk >= W_OP_AND && wk <= W_OP_MATCH_GREATER_EQUAL;
13,858✔
2681
}
2682

2683
bool same_tree(tree_t a, tree_t b)
8,101✔
2684
{
2685
   const tree_kind_t akind = tree_kind(a);
8,101✔
2686
   if (akind != tree_kind(b))
8,101✔
2687
      return false;
2688

2689
   switch (akind) {
7,942✔
2690
   case T_REF:
3,590✔
2691
      return tree_has_ref(a) && tree_has_ref(b) && tree_ref(a) == tree_ref(b);
4,698✔
2692
   case T_ARRAY_REF:
1,258✔
2693
      {
2694
         if (!same_tree(tree_value(a), tree_value(b)))
1,258✔
2695
            return false;
2696

2697
         const int nparams = tree_params(a);
1,229✔
2698
         assert(nparams == tree_params(b));
1,229✔
2699

2700
         for (int i = 0; i < nparams; i++) {
2,327✔
2701
            tree_t pa = tree_value(tree_param(a, i));
2,129✔
2702
            tree_t pb = tree_value(tree_param(b, i));
2,129✔
2703
            if (!same_tree(pa, pb))
2,129✔
2704
               return false;
2705
         }
2706

2707
         return true;
2708
      }
2709
   case T_ARRAY_SLICE:
42✔
2710
      {
2711
         if (!same_tree(tree_value(a), tree_value(b)))
42✔
2712
            return false;
2713

2714
         tree_t ra = tree_range(a, 0);
42✔
2715
         tree_t rb = tree_range(b, 0);
42✔
2716

2717
         const range_kind_t rakind = tree_subkind(ra);
42✔
2718
         if (rakind != tree_subkind(rb) || rakind == RANGE_EXPR)
42✔
2719
            return false;
2720

2721
         return same_tree(tree_left(ra), tree_left(rb))
42✔
2722
            && same_tree(tree_right(ra), tree_right(rb));
46✔
2723
      }
2724
   case T_RECORD_REF:
298✔
2725
      return ident_casecmp(tree_ident(a), tree_ident(b))
298✔
2726
         && same_tree(tree_value(a), tree_value(b));
306✔
2727
   case T_LITERAL:
2,468✔
2728
      {
2729
         const literal_kind_t lkind = tree_subkind(a);
2,468✔
2730
         if (lkind != tree_subkind(b))
2,468✔
2731
            return false;
2732

2733
         switch (lkind) {
2,468✔
2734
         case L_PHYSICAL:
36✔
2735
            {
2736
               ident_t aid = tree_has_ident(a) ? tree_ident(a) : NULL;
36✔
2737
               ident_t bid = tree_has_ident(b) ? tree_ident(b) : NULL;
36✔
2738
               if (aid == bid)
36✔
2739
                  return tree_ival(a) == tree_ival(b);
36✔
2740
               else
2741
                  return false;
2742
            }
2743
         case L_INT:
2,401✔
2744
            return tree_ival(a) == tree_ival(b);
2,401✔
2745
         case L_REAL:
31✔
2746
            return tree_dval(a) == tree_dval(b);
31✔
2747
         case L_NULL:
2748
            return true;
UNCOV
2749
         default:
×
2750
            return false;
×
2751
         }
2752
      }
2753
   case T_STRING:
254✔
2754
      {
2755
         const int nchars = tree_chars(a);
254✔
2756
         if (tree_chars(b) != nchars)
254✔
2757
            return false;
2758

2759
         for (int i = 0; i < nchars; i++) {
309✔
2760
            if (!same_tree(tree_char(a, i), tree_char(b, i)))
56✔
2761
               return false;
2762
         }
2763

2764
         return true;
2765
      }
2766
   case T_OPEN:
2767
      return true;
2768
   default:
32✔
2769
      return false;
32✔
2770
   }
2771
}
2772

2773
static range_kind_t get_range_direction(tree_t r)
406✔
2774
{
2775
   assert(tree_kind(r) == T_RANGE);
406✔
2776

2777
   const range_kind_t dir = tree_subkind(r);
406✔
2778
   if (dir != RANGE_EXPR)
406✔
2779
      return dir;
2780

2781
   // Handle ranges like X'RANGE where X has known direction
2782

2783
   tree_t aref = tree_value(r);
59✔
2784
   assert(tree_kind(aref) == T_ATTR_REF);
59✔
2785

2786
   const attr_kind_t kind = tree_subkind(aref);
59✔
2787
   assert(kind == ATTR_RANGE || kind == ATTR_REVERSE_RANGE);
59✔
2788

2789
   type_t prefix_type = tree_type(tree_name(aref));
59✔
2790
   if (type_is_none(prefix_type))
59✔
2791
      return RANGE_ERROR;
2792
   if (type_is_unconstrained(prefix_type))
58✔
2793
      return RANGE_EXPR;
2794

2795
   tree_t prefix_r = range_of(prefix_type, 0);
54✔
2796

2797
   const range_kind_t prefix_dir = get_range_direction(prefix_r);
54✔
2798
   if (prefix_dir != RANGE_TO && prefix_dir != RANGE_DOWNTO)
54✔
2799
      return prefix_dir;
2800
   else if (kind == ATTR_REVERSE_RANGE)
54✔
2801
      return prefix_dir == RANGE_TO ? RANGE_DOWNTO : RANGE_TO;
4✔
2802
   else
2803
      return prefix_dir;
2804
}
2805

2806
bool calculate_aggregate_bounds(tree_t expr, range_kind_t *kind,
8,894✔
2807
                                int64_t *left, int64_t *right)
2808
{
2809
   // Calculate the direction and bounds of an array aggregate using the
2810
   // rules in LRM 93 7.3.2.2
2811

2812
   type_t type = tree_type(expr);
8,894✔
2813
   if (type_is_none(type))
8,894✔
2814
      return false;
2815

2816
   type_t index_type = index_type_of(type, 0);
8,894✔
2817
   if (index_type == NULL)
8,894✔
2818
      return false;
2819
   else if (type_is_none(index_type) || type_is_generic(index_type))
8,894✔
2820
      return false;
3✔
2821

2822
   tree_t index_r = range_of(index_type, 0), base_r = index_r;
8,891✔
2823

2824
   int64_t low, high;
8,891✔
2825
   if (!folded_bounds(index_r, &low, &high))
8,891✔
2826
      return false;
2827

2828
   int64_t clow = INT64_MAX, chigh = INT64_MIN;  // Actual bounds computed below
8,871✔
2829

2830
   range_kind_t dir;
8,871✔
2831
   if (type_is_unconstrained(type))
8,871✔
2832
      dir = tree_subkind(index_r);
8,519✔
2833
   else {
2834
      base_r = range_of(type, 0);
352✔
2835
      dir = get_range_direction(base_r);
352✔
2836
   }
2837

2838
   const int nassocs = tree_assocs(expr);
8,871✔
2839

2840
   if (standard() >= STD_08) {
8,871✔
2841
      // VHDL-2008 range association determines index direction for
2842
      // unconstrained aggregate when the expression type matches the
2843
      // array type
2844
      for (int i = 0; i < nassocs; i++) {
18,335✔
2845
         tree_t a = tree_assoc(expr, i);
13,266✔
2846
         if (tree_subkind(a) == A_SLICE)
13,266✔
2847
            dir = tree_subkind(tree_range(a, 0));
71✔
2848
      }
2849
   }
2850

2851
   if (dir != RANGE_TO && dir != RANGE_DOWNTO)
8,871✔
2852
      return false;
2853

2854
   int64_t pos = 0;
2855
   for (int i = 0; i < nassocs; i++) {
25,142✔
2856
      tree_t a = tree_assoc(expr, i);
22,192✔
2857
      int64_t ilow = 0, ihigh = 0;
22,192✔
2858
      const assoc_kind_t akind = tree_subkind(a);
22,192✔
2859

2860
      switch (akind) {
22,192✔
2861
      case A_NAMED:
797✔
2862
         {
2863
            tree_t name = tree_name(a);
797✔
2864
            if (folded_int(name, &ilow))
797✔
2865
               ihigh = ilow;
784✔
2866
            else
2867
               return false;
5,904✔
2868
         }
2869
         break;
784✔
2870

2871
      case A_RANGE:
1,469✔
2872
      case A_SLICE:
2873
         {
2874
            tree_t r = tree_range(a, 0);
1,469✔
2875
            const range_kind_t rkind = tree_subkind(r);
1,469✔
2876
            if (rkind == RANGE_TO || rkind == RANGE_DOWNTO) {
1,469✔
2877
               tree_t left = tree_left(r), right = tree_right(r);
1,100✔
2878

2879
               int64_t ileft, iright;
1,100✔
2880
               if (folded_int(left, &ileft) && folded_int(right, &iright)) {
1,100✔
2881
                  ilow = (rkind == RANGE_TO ? ileft : iright);
573✔
2882
                  ihigh = (rkind == RANGE_TO ? iright : ileft);
573✔
2883
               }
2884
               else
2885
                  return false;
527✔
2886
            }
2887
            else
2888
               return false;
2889
         }
2890
         break;
2891

2892
      case A_OTHERS:
2893
         return false;
2894

2895
      case A_POS:
12,584✔
2896
         if (i == 0) {
12,584✔
2897
            int64_t ileft;
2,035✔
2898
            if (folded_int(tree_left(base_r), &ileft))
2,035✔
2899
               ilow = ihigh = ileft;
2,035✔
2900
            else
UNCOV
2901
               return false;
×
2902
         }
2903
         else if (dir == RANGE_TO)
10,549✔
2904
            ilow = ihigh = clow + pos;
10,547✔
2905
         else
2906
            ilow = ihigh = chigh - pos;
2✔
2907
         pos++;
12,584✔
2908
         break;
12,584✔
2909

2910
      case A_CONCAT:
7,328✔
2911
         {
2912
            type_t value_type = tree_type(tree_value(a));
7,328✔
2913

2914
            int64_t length;
7,328✔
2915
            if (type_is_unconstrained(value_type))
7,328✔
2916
               return false;
4,981✔
2917
            else if (folded_length(range_of(value_type, 0), &length)) {
2,710✔
2918
               if (i == 0) {
2,347✔
2919
                  int64_t ileft;
1,959✔
2920
                  if (folded_int(tree_left(base_r), &ileft))
1,959✔
2921
                     ilow = ihigh = ileft;
1,959✔
2922
                  else
UNCOV
2923
                     return false;
×
2924
               }
2925
               else if (dir == RANGE_TO) {
388✔
2926
                  ilow = clow + pos;
364✔
2927
                  ihigh = ilow + length - 1;
364✔
2928
               }
2929
               else {
2930
                  ihigh = chigh - pos;
24✔
2931
                  ilow = ihigh - length + 1;
24✔
2932
               }
2933
               pos += length;
2,347✔
2934
            }
2935
            else
2936
               return false;
2937
         }
2938
         break;
2939
      }
2940

2941
      clow = MIN(clow, ilow);
16,288✔
2942
      chigh = MAX(chigh, ihigh);
16,288✔
2943
   }
2944

2945
   if (clow < low || chigh > high)
2,950✔
2946
      return false;   // Will raise a bounds check error later
2947

2948
   *kind = dir;
2,944✔
2949
   *left = dir == RANGE_TO ? clow : chigh;
2,944✔
2950
   *right = dir == RANGE_TO ? chigh : clow;
2,944✔
2951

2952
   return true;
2,944✔
2953
}
2954

2955
type_t calculate_aggregate_subtype(tree_t expr)
7,378✔
2956
{
2957
   range_kind_t dir;
7,378✔
2958
   int64_t ileft, iright;
7,378✔
2959
   if (!calculate_aggregate_bounds(expr, &dir, &ileft, &iright))
7,378✔
2960
      return NULL;
2961

2962
   type_t type = tree_type(expr);
2,923✔
2963

2964
   const int ndims = dimension_of(type);
2,923✔
2965
   type_t a0_type = NULL;
2,923✔
2966
   if (ndims > 1) {
2,923✔
2967
      a0_type = tree_type(tree_value(tree_assoc(expr, 0)));
87✔
2968
      if (type_is_unconstrained(a0_type))
87✔
2969
         return NULL;
2970

2971
      assert(dimension_of(a0_type) == ndims - 1);
87✔
2972
   }
2973

2974
   type_t index_type = index_type_of(type, 0);
2,923✔
2975

2976
   tree_t left = get_discrete_lit(expr, index_type, ileft);
2,923✔
2977
   tree_t right = get_discrete_lit(expr, index_type, iright);
2,923✔
2978
   assert(left != NULL && right != NULL);
2,923✔
2979

2980
   type_t sub = type_new(T_SUBTYPE);
2,923✔
2981
   type_set_base(sub, type_base_recur(type));
2,923✔
2982

2983
   type_t elem = type_elem(type);
2,923✔
2984
   if (type_is_unconstrained(elem)) {
2,923✔
2985
      tree_t a0 = tree_assoc(expr, 0);
137✔
2986
      switch (tree_subkind(a0)) {
137✔
2987
      case A_CONCAT:
7✔
2988
      case A_SLICE:
2989
         a0_type = type_elem(tree_type(tree_value(a0)));
7✔
2990
         break;
7✔
2991
      default:
130✔
2992
         a0_type = tree_type(tree_value(a0));
130✔
2993
         break;
130✔
2994
      }
2995

2996
      if (!type_is_unconstrained(a0_type))
137✔
2997
         elem = a0_type;
81✔
2998
   }
2999

3000
   type_set_elem(sub, elem);
2,923✔
3001

3002
   tree_t cons = tree_new(T_CONSTRAINT);
2,923✔
3003
   tree_set_subkind(cons, C_INDEX);
2,923✔
3004

3005
   tree_t r = tree_new(T_RANGE);
2,923✔
3006
   tree_set_subkind(r, dir);
2,923✔
3007
   tree_set_type(r, index_type);
2,923✔
3008
   tree_set_left(r, left);
2,923✔
3009
   tree_set_right(r, right);
2,923✔
3010

3011
   tree_add_range(cons, r);
2,923✔
3012

3013
   for (int i = 1; i < ndims; i++)
3,010✔
3014
      tree_add_range(cons, range_of(a0_type, i - 1));
87✔
3015

3016
   type_set_constraint(sub, cons);
2,923✔
3017

3018
   return sub;
2,923✔
3019
}
3020

3021
bool can_be_signal(type_t type)
26,510✔
3022
{
3023
   switch (type_kind(type)) {
46,633✔
3024
   case T_RECORD:
5,480✔
3025
      {
3026
         const int nfields = type_fields(type);
5,480✔
3027
         for (int i = 0; i < nfields; i++) {
21,338✔
3028
            if (!can_be_signal(tree_type(type_field(type, i))))
17,172✔
3029
               return false;
3030
         }
3031

3032
         return true;
3033
      }
3034
   case T_ARRAY:
7,841✔
3035
      return can_be_signal(type_elem(type));
7,841✔
3036
   case T_SUBTYPE:
12,282✔
3037
      return can_be_signal(type_base(type));
12,282✔
3038
   case T_ACCESS:
3039
   case T_FILE:
3040
   case T_PROTECTED:
3041
   case T_INCOMPLETE:
3042
      return false;
3043
   default:
15,020✔
3044
      return true;
15,020✔
3045
   }
3046
}
3047

3048
type_t merge_constraints(type_t to, type_t from)
1,293✔
3049
{
3050
   assert(type_is_unconstrained(to));
1,293✔
3051
   assert(type_eq(to, from));
1,293✔
3052

3053
   tree_t cto = NULL;
1,293✔
3054
   if (type_kind(to) == T_SUBTYPE && type_has_constraint(to))
1,293✔
3055
      cto = type_constraint(to);
30✔
3056

3057
   tree_t cfrom = NULL;
1,293✔
3058
   if (type_kind(from) == T_SUBTYPE && type_has_constraint(from))
1,293✔
3059
      cfrom = type_constraint(from);
1,011✔
3060

3061
   if (cfrom == NULL)
1,011✔
3062
      return to;
282✔
3063

3064
   type_t sub = type_new(T_SUBTYPE);
1,011✔
3065
   type_set_base(sub, type_base_recur(to));
1,011✔
3066

3067
   if (type_is_array(to)) {
1,011✔
3068
      type_set_constraint(sub, cto ?: cfrom);
1,997✔
3069

3070
      type_t elem = type_elem(to);
1,005✔
3071
      if (type_is_unconstrained(elem))
1,005✔
3072
         type_set_elem(sub, merge_constraints(elem, type_elem(from)));
18✔
3073
      else
3074
         type_set_elem(sub, elem);
987✔
3075
   }
3076
   else {
3077
      tree_t cnew = tree_new(T_CONSTRAINT);
6✔
3078
      tree_set_subkind(cnew, C_RECORD);
6✔
3079
      tree_set_loc(cnew, tree_loc(cto ?: cfrom));
7✔
3080

3081
      type_set_constraint(sub, cnew);
6✔
3082

3083
      if (cto != NULL) {
6✔
3084
         const int nto = tree_ranges(cto);
5✔
3085
         for (int i = 0; i < nto; i++)
10✔
3086
            tree_add_range(cnew, tree_range(cto, i));
5✔
3087
      }
3088

3089
      const int nfrom = tree_ranges(cfrom), base = tree_ranges(cnew);
6✔
3090
      for (int i = 0; i < nfrom; i++) {
16✔
3091
         tree_t ec = tree_range(cfrom, i);
10✔
3092
         assert(tree_kind(ec) == T_ELEM_CONSTRAINT);
10✔
3093

3094
         ident_t id = tree_ident(ec);
10✔
3095
         bool found = false;
10✔
3096
         for (int j = 0; j < base && !found; j++)
19✔
3097
            found |= tree_ident(tree_range(cnew, j)) == id;
9✔
3098

3099
         if (!found)
10✔
3100
            tree_add_range(cnew, ec);
5✔
3101
      }
3102
   }
3103

3104
   return sub;
3105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc