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

nickg / nvc / 6672403716

27 Oct 2023 09:27PM UTC coverage: 91.224% (+0.004%) from 91.22%
6672403716

push

github

nickg
Visibility issue with block name in configuration

Fixes #783

2 of 2 new or added lines in 1 file covered. (100.0%)

49635 of 54410 relevant lines covered (91.22%)

582228.89 hits per line

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

92.89
/src/common.c
1
//
2
//  Copyright (C) 2013-2023  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 "scan.h"
28
#include "thread.h"
29
#include "type.h"
30
#include "vlog/vlog-phase.h"
31

32
#include <assert.h>
33
#include <ctype.h>
34
#include <string.h>
35
#include <stdlib.h>
36
#include <inttypes.h>
37

38
typedef struct {
39
   ptr_list_t copied_subs;
40
   ptr_list_t copied_types;
41
} copy_ctx_t;
42

43
static vhdl_standard_t  current_std  = STD_02;
44
static bool             have_set_std = false;
45
static ident_t          id_cache[NUM_WELL_KNOWN];
46
static text_buf_t      *syntax_buf = NULL;
47

48
int64_t assume_int(tree_t t)
130,676✔
49
{
50
   int64_t value;
130,676✔
51
   if (folded_int(t, &value))
130,676✔
52
      return value;
130,676✔
53

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

58
void range_bounds(tree_t r, int64_t *low, int64_t *high)
42,975✔
59
{
60
   assert(tree_kind(r) == T_RANGE);
42,975✔
61

62
   const int64_t left = assume_int(tree_left(r));
42,975✔
63
   const int64_t right = assume_int(tree_right(r));
42,975✔
64

65
   *low  = tree_subkind(r) == RANGE_TO ? left : right;
42,975✔
66
   *high = tree_subkind(r) == RANGE_TO ? right : left;
42,975✔
67
}
42,975✔
68

69
bool folded_int(tree_t t, int64_t *l)
2,418,720✔
70
{
71
   switch (tree_kind(t)) {
2,451,790✔
72
   case T_LITERAL:
1,868,210✔
73
      switch (tree_subkind(t)) {
1,868,210✔
74
      case L_PHYSICAL:
13,060✔
75
         if (tree_has_ref(t))
13,060✔
76
            return false;
77
         // Fall-through
78
      case L_INT:
79
         *l = tree_ival(t);
1,850,820✔
80
         return true;
1,850,820✔
81
      default:
82
         return false;
83
      }
84
   case T_QUALIFIED:
289✔
85
      return folded_int(tree_value(t), l);
289✔
86
   case T_REF:
398,405✔
87
      {
88
         tree_t decl = tree_ref(t);
398,405✔
89
         switch (tree_kind(decl)) {
398,405✔
90
         case T_CONST_DECL:
40,319✔
91
            if (tree_has_value(decl))
40,319✔
92
               return folded_int(tree_value(decl), l);
32,623✔
93
            else
94
               return false;
95
         case T_ENUM_LIT:
294,288✔
96
            *l = tree_pos(decl);
294,288✔
97
            return true;
294,288✔
98
         case T_ALIAS:
157✔
99
            return folded_int(tree_value(decl), l);
157✔
100
         default:
101
            return false;
102
         }
103
      }
104
   default:
105
      return false;
106
   }
107
}
108

109
bool folded_real(tree_t t, double *l)
132,328✔
110
{
111
   switch (tree_kind(t)) {
132,349✔
112
   case T_LITERAL:
125,230✔
113
      if (tree_subkind(t) == L_REAL) {
125,230✔
114
         *l = tree_dval(t);
125,228✔
115
         return true;
125,228✔
116
      }
117
      else
118
         return false;
119
   case T_QUALIFIED:
21✔
120
      return folded_real(tree_value(t), l);
21✔
121
   default:
122
      return false;
123
   }
124
}
125

126
bool folded_length(tree_t r, int64_t *l)
40,192✔
127
{
128
   int64_t low, high;
40,192✔
129
   if (folded_bounds(r, &low, &high)) {
40,192✔
130
      *l = MAX(high - low + 1, 0);
37,872✔
131
      return true;
37,872✔
132
   }
133
   else
134
      return false;
135
}
136

137
bool folded_bounds(tree_t r, int64_t *low, int64_t *high)
1,049,340✔
138
{
139
   assert(tree_kind(r) == T_RANGE);
1,049,340✔
140

141
   const range_kind_t rkind = tree_subkind(r);
1,049,340✔
142

143
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
1,049,340✔
144
      return false;
145

146
   int64_t left, right;
1,038,450✔
147
   if (!folded_int(tree_left(r), &left))
1,038,450✔
148
      return false;
149
   else if (!folded_int(tree_right(r), &right))
953,084✔
150
      return false;
151

152
   switch (rkind) {
935,624✔
153
   case RANGE_TO:
843,574✔
154
      *low  = left;
843,574✔
155
      *high = right;
843,574✔
156
      return true;
843,574✔
157
   case RANGE_DOWNTO:
92,050✔
158
      *low  = right;
92,050✔
159
      *high = left;
92,050✔
160
      return true;
92,050✔
161
   default:
162
      return false;
163
   }
164
}
165

166
bool folded_bounds_real(tree_t r, double *low, double *high)
61,194✔
167
{
168
   assert(tree_kind(r) == T_RANGE);
61,194✔
169

170
   const range_kind_t rkind = tree_subkind(r);
61,194✔
171

172
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
61,194✔
173
      return false;
174

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

194
bool folded_bool(tree_t t, bool *b)
36,156✔
195
{
196
   if (tree_kind(t) == T_REF) {
36,156✔
197
      tree_t decl = tree_ref(t);
8,436✔
198
      if (tree_kind(decl) == T_ENUM_LIT
8,436✔
199
          && type_ident(tree_type(decl)) == well_known(W_STD_BOOL)) {
6,185✔
200
         *b = (tree_pos(decl) == 1);
6,185✔
201
         return true;
6,185✔
202
      }
203
   }
204

205
   return false;
206
}
207

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

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

219
   return b;
7,193✔
220
}
221

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

230
   return f;
32,849✔
231
}
232

233
tree_t get_real_lit(tree_t t, type_t type, double r)
×
234
{
235
   tree_t f = tree_new(T_LITERAL);
×
236
   tree_set_loc(f, tree_loc(t));
×
237
   tree_set_subkind(f, L_REAL);
×
238
   tree_set_dval(f, r);
×
239
   tree_set_type(f, type ?: tree_type(t));
×
240

241
   return f;
×
242
}
243

244
bool parse_value(type_t type, const char *str, parsed_value_t *value)
86✔
245
{
246
   type_t base = type_base_recur(type);
86✔
247
   const type_kind_t basek = type_kind(base);
86✔
248

249
   if (basek == T_ARRAY && type_is_character_array(base)) {
86✔
250
      value->enums = NULL;
17✔
251

252
      int map[256];
17✔
253
      for (int i = 0; i < ARRAY_LEN(map); i++)
4,369✔
254
         map[i] = INT_MAX;
4,352✔
255

256
      type_t elem = type_elem(base);
17✔
257

258
      const int nlits = type_enum_literals(elem);
17✔
259
      for (int i = 0; i < nlits; i++) {
2,591✔
260
         ident_t id = tree_ident(type_enum_literal(elem, i));
2,574✔
261
         if (ident_char(id, 0) == '\'')
2,574✔
262
            map[(uint8_t)ident_char(id, 1)] = i;
1,924✔
263
      }
264

265
      while (map[(uint8_t)*str] == INT_MAX && isspace_iso88591(*str))
20✔
266
         ++str;
3✔
267

268
      const bool quoted = map['\"'] == INT_MAX && *str == '\"';
17✔
269
      if (quoted) str++;
17✔
270

271
      const size_t max = strlen(str);
17✔
272
      enum_array_t *array = xmalloc_flex(sizeof(enum_array_t), max, 1);
17✔
273

274
      int n = 0, m;
17✔
275
      for (; *str != '\0' && (m = map[(uint8_t)*str]) != INT_MAX; str++, n++)
99✔
276
         array->values[n] = m;
65✔
277

278
      assert(n <= max);
17✔
279
      array->count = n;
17✔
280

281
      if (quoted && *str++ != '\"') {
17✔
282
         free(array);
1✔
283
         return false;
1✔
284
      }
285

286
      for (; *str; str++) {
20✔
287
         if (!isspace_iso88591(*str)) {
5✔
288
            free(array);
1✔
289
            return false;
1✔
290
         }
291
      }
292

293
      value->enums = array;
15✔
294
      return true;
15✔
295
   }
296

297
   while (isspace_iso88591(*str))
79✔
298
      ++str;
10✔
299

300
   switch (basek) {
69✔
301
   case T_INTEGER:
44✔
302
      {
303
         const bool is_negative = *str == '-';
44✔
304
         int num_digits = 0;
44✔
305

306
         if (is_negative) ++str;
44✔
307

308
         int64_t sum = 0;
309
         for (; isdigit_iso88591(*str) || (*str == '_'); str++) {
132✔
310
            if (*str != '_') {
88✔
311
               sum *= 10;
86✔
312
               sum += (*str - '0');
86✔
313
               num_digits++;
86✔
314
            }
315
         }
316

317
         value->integer = is_negative ? -sum : sum;
44✔
318

319
         if (num_digits == 0)
44✔
320
            return false;
321
      }
322
      break;
323

324
   case T_ENUM:
14✔
325
      {
326
         bool upcase = true;
14✔
327
         char *copy LOCAL = xstrdup(str), *p;
27✔
328
         for (p = copy; (*p != '\0') && !isspace_iso88591(*p); p++, str++) {
55✔
329
            if (*p == '\'')
41✔
330
               upcase = false;
331
            if (upcase)
23✔
332
               *p = toupper_iso88591(*p);
14✔
333
         }
334
         *p = '\0';
14✔
335

336
         ident_t id = ident_new(copy);
14✔
337

338
         value->integer = -1;
14✔
339

340
         const int nlits = type_enum_literals(base);
14✔
341
         for (int i = 0; i < nlits; i++) {
38✔
342
            if (tree_ident(type_enum_literal(base, i)) == id) {
37✔
343
               value->integer = i;
13✔
344
               break;
13✔
345
            }
346
         }
347

348
         if (value->integer == -1)
14✔
349
            return false;
1✔
350
      }
351
      break;
352

353
   case T_REAL:
6✔
354
      {
355
         char *eptr = NULL;
6✔
356
         value->real = strtod(str, &eptr);
6✔
357
         str = eptr;
6✔
358
      }
359
      break;
6✔
360

361
   case T_PHYSICAL:
5✔
362
      {
363
         char *eptr = NULL;
5✔
364
         double scale = strtod(str, &eptr);
5✔
365
         str = eptr;
5✔
366

367
         while (isspace_iso88591(*str)) ++str;
10✔
368

369
         char *copy LOCAL = xstrdup(str), *p;
10✔
370
         for (p = copy; *p && !isspace_iso88591(*p); p++, str++)
13✔
371
            *p = toupper_iso88591(*p);
8✔
372
         *p = '\0';
5✔
373

374
         if (p == copy)
5✔
375
            return false;
376

377
         ident_t id = ident_new(copy);
4✔
378

379
         value->integer = -1;
4✔
380

381
         const int nunits = type_units(base);
4✔
382
         for (int i = 0; i < nunits; i++) {
11✔
383
            tree_t u = type_unit(base, i);
11✔
384
            if (tree_ident(u) == id) {
11✔
385
               value->integer = scale * assume_int(tree_value(u));
4✔
386
               break;
4✔
387
            }
388
         }
389

390
         if (value->integer == -1)
4✔
391
            return false;
392
      }
393
      break;
394

395
   default:
396
      return false;
397
   }
398

399
   for (; *str; str++) {
78✔
400
      if (!isspace_iso88591(*str))
11✔
401
         return false;
402
   }
403

404
   return true;
405
}
406

407
tree_t make_ref(tree_t to)
15,254✔
408
{
409
   tree_t t = tree_new(T_REF);
15,254✔
410
   tree_set_ident(t, tree_ident(to));
15,254✔
411
   tree_set_ref(t, to);
15,254✔
412
   tree_set_type(t, tree_type(to));
15,254✔
413
   return t;
15,254✔
414
}
415

416
vhdl_standard_t standard(void)
2,394,330✔
417
{
418
   return current_std;
2,394,330✔
419
}
420

421
void set_standard(vhdl_standard_t s)
3,972✔
422
{
423
   current_std = s;
3,972✔
424
   have_set_std = true;
3,972✔
425
}
3,972✔
426

427
void set_default_standard(vhdl_standard_t s)
332✔
428
{
429
   if (!have_set_std)
332✔
430
      set_standard(s);
57✔
431
}
332✔
432

433
const char *standard_text(vhdl_standard_t s)
3,626✔
434
{
435
   static const char *text[] = {
3,626✔
436
      "1987", "1993", "2000", "2002", "2008", "2019"
437
   };
438

439
   if ((unsigned)s < ARRAY_LEN(text))
3,626✔
440
      return text[s];
3,626✔
441
   else
442
      return "????";
443
}
444

445
int record_field_to_net(type_t type, unsigned pos)
×
446
{
447
   int offset = 0;
×
448
   for (int i = 0; i < pos; i++)
×
449
      offset += type_width(tree_type(type_field(type, i)));
×
450

451
   return offset;
×
452
}
453

454
tree_t find_record_field(tree_t rref)
8,466✔
455
{
456
   ident_t fname = tree_ident(rref);
8,466✔
457
   type_t value_type = tree_type(tree_value(rref));
8,466✔
458

459
   if (!type_is_record(value_type))
8,466✔
460
      return NULL;
461

462
   const int nfields = type_fields(value_type);
8,466✔
463
   for (int i = 0; i < nfields; i++) {
27,880✔
464
      tree_t field = type_field(value_type, i);
27,875✔
465
      if (tree_ident(field) == fname)
27,875✔
466
         return field;
8,461✔
467
   }
468

469
   return NULL;
470
}
471

472
tree_t find_element_mode_indication(tree_t view, tree_t field, bool *converse)
277✔
473
{
474
   switch (tree_kind(view)) {
915✔
475
   case T_REF:
397✔
476
      return find_element_mode_indication(tree_ref(view), field, converse);
397✔
477

478
   case T_ALIAS:
120✔
479
      return find_element_mode_indication(tree_value(view), field, converse);
120✔
480

481
   case T_ATTR_REF:
121✔
482
      assert(tree_subkind(view) == ATTR_CONVERSE);
121✔
483
      *converse = !*converse;
121✔
484
      return find_element_mode_indication(tree_name(view), field, converse);
121✔
485

486
   case T_VIEW_DECL:
277✔
487
      {
488
         type_t view_type = tree_type(view);
277✔
489
         assert(type_kind(view_type) == T_VIEW);
277✔
490

491
         const int nelems = type_fields(view_type);
277✔
492
         for (int i = 0; i < nelems; i++) {
491✔
493
            tree_t e = type_field(view_type, i);
491✔
494
            if (tree_ref(e) == field)
491✔
495
               return e;
277✔
496
         }
497

498
         return NULL;
499
      }
500

501
   default:
×
502
      fatal_trace("unhandled tree kind %s in find_element_mode_indication",
×
503
                  tree_kind_str(tree_kind(view)));
504
   }
505
}
506

507
port_mode_t converse_mode(tree_t port, bool converse)
181✔
508
{
509
   const port_mode_t mode = tree_subkind(port);
181✔
510
   switch (mode) {
181✔
511
   case PORT_IN: return converse ? PORT_OUT : PORT_IN;
101✔
512
   case PORT_OUT: return converse ? PORT_IN : PORT_OUT;
71✔
513
   default: return mode;
514
   }
515
}
516

517
class_t class_of(tree_t t)
1,495,170✔
518
{
519
   switch (tree_kind(t)) {
1,577,990✔
520
   case T_VAR_DECL:
521
      return C_VARIABLE;
522
   case T_SIGNAL_DECL:
50,452✔
523
   case T_IMPLICIT_SIGNAL:
524
      return C_SIGNAL;
50,452✔
525
   case T_CONST_DECL:
44,604✔
526
      return C_CONSTANT;
44,604✔
527
   case T_PORT_DECL:
177,043✔
528
   case T_GENERIC_DECL:
529
   case T_PARAM_DECL:
530
   case T_EXTERNAL_NAME:
531
      return tree_class(t);
177,043✔
532
   case T_ENUM_LIT:
917,767✔
533
   case T_LITERAL:
534
   case T_STRING:
535
      return C_LITERAL;
917,767✔
536
   case T_FIELD_DECL:
1,644✔
537
   case T_ATTR_DECL:
538
      return C_DEFAULT;
1,644✔
539
   case T_VIEW_DECL:
131✔
540
      return C_VIEW;
131✔
541
   case T_UNIT_DECL:
6,127✔
542
      return C_UNITS;
6,127✔
543
   case T_ARCH:
40✔
544
      return C_ARCHITECTURE;
40✔
545
   case T_FUNC_DECL:
25,701✔
546
   case T_FUNC_BODY:
547
   case T_FUNC_INST:
548
   case T_FCALL:
549
      return C_FUNCTION;
25,701✔
550
   case T_PROC_DECL:
11,168✔
551
   case T_PROC_BODY:
552
   case T_PROC_INST:
553
   case T_PCALL:
554
      return C_PROCEDURE;
11,168✔
555
   case T_ENTITY:
132✔
556
      return C_ENTITY;
132✔
557
   case T_SUBTYPE_DECL:
27,133✔
558
      return C_SUBTYPE;
27,133✔
559
   case T_TYPE_DECL:
130,593✔
560
   case T_PROT_DECL:
561
      return C_TYPE;
130,593✔
562
   case T_FILE_DECL:
1,856✔
563
      return C_FILE;
1,856✔
564
   case T_PROCESS:
73✔
565
   case T_BLOCK:
566
   case T_FOR:
567
   case T_INSTANCE:
568
   case T_CONCURRENT:
569
   case T_ELAB:
570
      return C_LABEL;
73✔
571
   case T_COMPONENT:
1✔
572
      return C_COMPONENT;
1✔
573
   case T_REF:
74,521✔
574
   case T_PROT_REF:
575
      return tree_has_ref(t) ? class_of(tree_ref(t)) : C_DEFAULT;
74,521✔
576
   case T_ARRAY_REF:
8,319✔
577
   case T_ARRAY_SLICE:
578
   case T_RECORD_REF:
579
   case T_ALL:
580
   case T_ALIAS:
581
   case T_QUALIFIED:
582
      return class_of(tree_value(t));
8,319✔
583
   case T_PACKAGE:
1,222✔
584
   case T_PACK_BODY:
585
   case T_PACK_INST:
586
      return C_PACKAGE;
1,222✔
587
   case T_CONFIGURATION:
1✔
588
      return C_CONFIGURATION;
1✔
589
   case T_LIBRARY:
×
590
      return C_LIBRARY;
×
591
   case T_ATTR_REF:
161✔
592
      switch (tree_subkind(t)) {
161✔
593
      case ATTR_DELAYED:
594
      case ATTR_STABLE:
595
      case ATTR_QUIET:
596
      case ATTR_TRANSACTION:
597
         return C_SIGNAL;
598
      default:
158✔
599
         return C_DEFAULT;
158✔
600
      }
601
   case T_CONTEXT:
×
602
      return C_CONTEXT;
×
603
   default:
×
604
      fatal_trace("missing class_of for %s", tree_kind_str(tree_kind(t)));
×
605
   }
606
}
607

608
bool class_has_type(class_t c)
814,549✔
609
{
610
   switch (c) {
814,549✔
611
   case C_LABEL:
612
   case C_ENTITY:
613
   case C_ARCHITECTURE:
614
   case C_COMPONENT:
615
   case C_CONFIGURATION:
616
   case C_PACKAGE:
617
   case C_LIBRARY:
618
      return false;
619
   default:
813,331✔
620
      return true;
813,331✔
621
   }
622
}
623

624
const char *class_str(class_t c)
230✔
625
{
626
   static const char *strs[] = {
230✔
627
      "default", "signal", "variable", "constant", "file", "entity",
628
      "component", "configuration", "architecture", "function", "package",
629
      "type", "subtype", "label", "procedure", "literal", "units", "library",
630
      "context", "view",
631
   };
632
   assert(c < ARRAY_LEN(strs));
230✔
633
   return strs[c];
230✔
634
}
635

636
const char *assoc_kind_str(assoc_kind_t akind)
9✔
637
{
638
   switch (akind) {
9✔
639
   case A_NAMED:  return "named";
640
   case A_POS:    return "positional";
2✔
641
   case A_OTHERS: return "others";
3✔
642
   case A_RANGE:  return "range";
1✔
643
   default:       return "??";
×
644
   }
645
}
646

647
bool is_subprogram(tree_t t)
1,724,110✔
648
{
649
   switch (tree_kind(t)) {
1,724,110✔
650
   case T_FUNC_DECL:
651
   case T_FUNC_BODY:
652
   case T_FUNC_INST:
653
   case T_PROC_DECL:
654
   case T_PROC_BODY:
655
   case T_PROC_INST:
656
      return true;
657
   case T_GENERIC_DECL:
1,544✔
658
      {
659
         const class_t class = tree_class(t);
1,544✔
660
         return class == C_FUNCTION || class == C_PROCEDURE;
1,544✔
661
      }
662
   default:
1,245,570✔
663
      return false;
1,245,570✔
664
   }
665
}
666

667
bool is_loop_stmt(tree_t t)
407✔
668
{
669
   const tree_kind_t kind = tree_kind(t);
407✔
670
   return kind == T_WHILE || kind == T_FOR;
407✔
671
}
672

673
bool is_container(tree_t t)
41,743✔
674
{
675
   switch (tree_kind(t)) {
41,743✔
676
   case T_FUNC_BODY:
677
   case T_PROC_BODY:
678
   case T_ENTITY:
679
   case T_ARCH:
680
   case T_PACKAGE:
681
   case T_PACK_BODY:
682
   case T_CONFIGURATION:
683
   case T_BLOCK:
684
   case T_PROT_BODY:
685
   case T_ELAB:
686
   case T_FOR:
687
   case T_PROCESS:
688
   case T_PACK_INST:
689
      return true;
690
   default:
7,565✔
691
      return false;
7,565✔
692
   }
693
}
694

695
bool is_concurrent_block(tree_t t)
193✔
696
{
697
   switch (tree_kind(t)) {
193✔
698
   case T_ARCH:
699
   case T_ENTITY:
700
   case T_BLOCK:
701
   case T_IF_GENERATE:
702
   case T_FOR_GENERATE:
703
      return true;
704
   default:
95✔
705
      return false;
95✔
706
   }
707
}
708

709
bool is_package(tree_t t)
39,489✔
710
{
711
   switch (tree_kind(t)) {
39,489✔
712
   case T_PACKAGE:
713
   case T_PACK_BODY:
714
   case T_PACK_INST:
715
      return true;
716
   default:
3,309✔
717
      return false;
3,309✔
718
   }
719
}
720

721
bool is_design_unit(tree_t t)
19,513✔
722
{
723
   switch (tree_kind(t)) {
19,513✔
724
   case T_ENTITY:
725
   case T_ARCH:
726
   case T_PACKAGE:
727
   case T_PACK_BODY:
728
   case T_CONFIGURATION:
729
   case T_CONTEXT:
730
   case T_PACK_INST:
731
      return true;
732
   default:
2,878✔
733
      return false;
2,878✔
734
   }
735
}
736

737
bool is_guarded_signal(tree_t decl)
4,672✔
738
{
739
   return !!(tree_flags(decl) & (TREE_F_BUS | TREE_F_REGISTER));
4,672✔
740
}
741

742
bool is_type_decl(tree_t t)
2,225,810✔
743
{
744
   switch (tree_kind(t)) {
2,225,810✔
745
   case T_TYPE_DECL:
746
   case T_SUBTYPE_DECL:
747
   case T_PROT_DECL:
748
      return true;
749
   default:
2,145,660✔
750
      return false;
2,145,660✔
751
   }
752
}
753

754
tree_t aliased_type_decl(tree_t decl)
104,781✔
755
{
756
   switch (tree_kind(decl)) {
105,024✔
757
   case T_ALIAS:
243✔
758
      {
759
         tree_t value = tree_value(decl);
243✔
760
         if (tree_kind(value) == T_REF && tree_has_ref(value))
243✔
761
            return aliased_type_decl(tree_ref(value));
243✔
762
         else
763
            return NULL;
×
764
      }
765
   case T_TYPE_DECL:
766
   case T_SUBTYPE_DECL:
767
   case T_PROT_DECL:
768
      return decl;
769
   case T_GENERIC_DECL:
376✔
770
      if (tree_class(decl) == C_TYPE)
376✔
771
         return decl;
772
      else
773
         return NULL;
44✔
774
   default:
19,588✔
775
      return NULL;
19,588✔
776
   }
777
}
778

779
tree_t add_param(tree_t call, tree_t value, param_kind_t kind, tree_t name)
142,658✔
780
{
781
   tree_t p = tree_new(T_PARAM);
142,658✔
782
   tree_set_loc(p, tree_loc(value));
142,658✔
783
   tree_set_subkind(p, kind);
142,658✔
784
   tree_set_value(p, value);
142,658✔
785

786
   switch (kind) {
142,658✔
787
   case P_NAMED:
×
788
      assert(name != NULL);
×
789
      tree_set_name(p, name);
×
790
      break;
×
791
   case P_POS:
142,658✔
792
      tree_set_pos(p, tree_params(call));
142,658✔
793
      break;
142,658✔
794
   }
795

796
   tree_add_param(call, p);
142,658✔
797
   return p;
142,658✔
798
}
799

800
type_t array_aggregate_type(type_t array, int from_dim)
262✔
801
{
802
   if (type_is_none(array))
262✔
803
      return type_new(T_NONE);
2✔
804

805
   if (type_is_unconstrained(array)) {
260✔
806
      const int nindex = type_indexes(array);
37✔
807
      assert(from_dim < nindex);
37✔
808

809
      type_t type = type_new(T_ARRAY);
37✔
810
      type_set_ident(type, type_ident(array));
37✔
811
      type_set_elem(type, type_elem(array));
37✔
812

813
      for (int i = from_dim; i < nindex; i++)
74✔
814
         type_add_index(type, type_index(array, i));
37✔
815

816
      return type;
817
   }
818
   else {
819
      const int ndims = dimension_of(array);
223✔
820
      assert(from_dim < ndims);
223✔
821

822
      type_t base = type_new(T_ARRAY);
223✔
823
      type_set_ident(base, type_ident(array));
223✔
824
      type_set_elem(base, type_elem(array));
223✔
825

826
      tree_t constraint = tree_new(T_CONSTRAINT);
223✔
827
      tree_set_subkind(constraint, C_INDEX);
223✔
828

829
      type_t sub = type_new(T_SUBTYPE);
223✔
830
      type_set_base(sub, base);
223✔
831
      type_add_constraint(sub, constraint);
223✔
832

833
      for (int i = from_dim; i < ndims; i++) {
465✔
834
         tree_t r = range_of(array, i);
242✔
835
         type_add_index(base, tree_type(r));
242✔
836
         tree_add_range(constraint, r);
242✔
837
      }
838

839
      return sub;
840
   }
841
}
842

843
unsigned bits_for_range(int64_t low, int64_t high)
1,504,700✔
844
{
845
   assert(low <= high);
1,504,700✔
846

847
   if (low < 0) {
1,504,700✔
848
      // Signed integers
849
      if (low >= INT8_MIN && high <= INT8_MAX)
406,690✔
850
         return 8;
851
      else if (low >= INT16_MIN && high <= INT16_MAX)
400,104✔
852
         return 16;
853
      else if (low >= INT32_MIN && high <= INT32_MAX)
399,897✔
854
         return 32;
855
      else
856
         return 64;
103,300✔
857
   }
858
   else {
859
      // Unsigned integers
860
      if (high <= 1)
1,098,010✔
861
         return 1;
862
      else if (high <= UINT8_MAX)
491,934✔
863
         return 8;
864
      else if (high <= UINT16_MAX)
66,976✔
865
         return 16;
866
      else if (high <= UINT32_MAX)
64,584✔
867
         return 32;
868
      else
869
         return 64;
13,238✔
870
   }
871
}
872

873
unsigned dimension_of(type_t type)
1,115,030✔
874
{
875
   switch (type_kind(type)) {
2,027,590✔
876
   case T_SUBTYPE:
912,566✔
877
      return dimension_of(type_base(type));
912,566✔
878
   case T_GENERIC:
15✔
879
      assert(type_subkind(type) == GTYPE_ARRAY);
15✔
880
      // Fall-through
881
   case T_ARRAY:
882
      return type_indexes(type);
1,111,760✔
883
   case T_NONE:
884
   case T_ACCESS:
885
   case T_RECORD:
886
      return 0;
887
   case T_INTEGER:
3,219✔
888
   case T_REAL:
889
   case T_PHYSICAL:
890
   case T_ENUM:
891
      return type_dims(type);
3,219✔
892
   default:
×
893
      fatal_trace("invalid type kind %s in dimension_of",
×
894
                  type_kind_str(type_kind(type)));
895
   }
896
}
897

898
tree_t range_of(type_t type, unsigned dim)
1,249,490✔
899
{
900
   switch (type_kind(type)) {
1,279,540✔
901
   case T_SUBTYPE:
1,043,770✔
902
      if (type_constraints(type) > 0) {
1,043,770✔
903
         tree_t c0 = type_constraint(type, 0);
1,013,830✔
904
         switch (tree_subkind(c0)) {
1,013,830✔
905
         case C_OPEN:
110✔
906
            return range_of(type_base(type), dim);
110✔
907
         case C_INDEX:
1,013,720✔
908
         case C_RANGE:
909
            return tree_range(type_constraint(type, 0), dim);
1,013,720✔
910
         default:
×
911
            fatal_trace("invalid constraint in range_of");
×
912
         }
913
      }
914
      else
915
         return range_of(type_base(type), dim);
29,943✔
916
   case T_INTEGER:
235,775✔
917
   case T_REAL:
918
   case T_PHYSICAL:
919
   case T_ENUM:
920
      return type_dim(type, dim);
235,775✔
921
   default:
×
922
      fatal_trace("invalid type kind %s in range_of",
×
923
                  type_kind_str(type_kind(type)));
924
   }
925
}
926

927
range_kind_t direction_of(type_t type, unsigned dim)
23,156✔
928
{
929
   switch (type_kind(type)) {
23,200✔
930
   case T_ENUM:
931
      return RANGE_TO;
932
   case T_NONE:
×
933
      return RANGE_ERROR;
×
934
   case T_INTEGER:
23,126✔
935
   case T_REAL:
936
   case T_PHYSICAL:
937
   case T_SUBTYPE:
938
      {
939
         tree_t r = range_of(type, dim);
23,126✔
940
         const range_kind_t rkind = tree_subkind(r);
23,126✔
941
         if (rkind == RANGE_EXPR) {
23,126✔
942
            // Return a fixed direction if possible
943
            tree_t value = tree_value(r);
107✔
944
            assert(tree_kind(value) == T_ATTR_REF);
107✔
945

946
            DEBUG_ONLY({
107✔
947
                  const attr_kind_t attr = tree_subkind(value);
948
                  assert(attr == ATTR_RANGE || attr == ATTR_REVERSE_RANGE);
949
               });
107✔
950

951
            tree_t name = tree_name(value);
107✔
952
            if (tree_kind(name) == T_REF && tree_has_ref(name)) {
107✔
953
               tree_t decl = tree_ref(name);
106✔
954
               if (is_type_decl(decl))
106✔
955
                  return direction_of(tree_type(decl), 0);
44✔
956
            }
957
         }
958

959
         return rkind;
960
      }
961
   default:
×
962
      fatal_trace("invalid type kind %s in direction_of",
×
963
                  type_kind_str(type_kind(type)));
964
   }
965
}
966

967
type_t index_type_of(type_t type, unsigned dim)
195,467✔
968
{
969
   if (dim >= dimension_of(type))
195,467✔
970
      return NULL;
971

972
   type_t base = type_base_recur(type);
195,452✔
973
   type_kind_t base_kind = type_kind(base);
195,452✔
974
   if (base_kind == T_ARRAY)
195,452✔
975
      return type_index(base, dim);
192,552✔
976
   else if (base_kind == T_ENUM || base_kind == T_NONE)
2,900✔
977
      return type;
978
   else if (base_kind == T_GENERIC && type_subkind(base) == GTYPE_ARRAY)
2,750✔
979
      return type_index(base, dim);
9✔
980
   else if (base_kind == T_RECORD || base_kind == T_GENERIC)
2,741✔
981
      return NULL;
982
   else
983
      return tree_type(range_of(base, dim));
2,741✔
984
}
985

986
int64_t rebase_index(type_t array_type, int dim, int64_t value)
240✔
987
{
988
   // Convert value which is in the range of array_type to a zero-based index
989
   tree_t r = range_of(array_type, dim);
240✔
990
   const int64_t left = assume_int(tree_left(r));
240✔
991
   return (tree_subkind(r) == RANGE_TO) ? value - left : left - value;
240✔
992
}
993

994
ident_t well_known(well_known_t id)
231,666✔
995
{
996
   assert(id < NUM_WELL_KNOWN);
231,666✔
997
   return id_cache[id];
231,666✔
998
}
999

1000
well_known_t is_well_known(ident_t ident)
217,890✔
1001
{
1002
   well_known_t pos = 0;
217,890✔
1003
   for (; pos < NUM_WELL_KNOWN; pos++) {
6,871,100✔
1004
      if (id_cache[pos] == ident)
6,746,270✔
1005
         break;
1006
   }
1007

1008
   return pos;
217,890✔
1009
}
1010

1011
void intern_strings(void)
4,119✔
1012
{
1013
   id_cache[W_STD_STANDARD]   = ident_new("STD.STANDARD");
4,119✔
1014
   id_cache[W_ALL]            = ident_new("all");
4,119✔
1015
   id_cache[W_STD_BIT]        = ident_new("STD.STANDARD.BIT");
4,119✔
1016
   id_cache[W_STD_BOOL]       = ident_new("STD.STANDARD.BOOLEAN");
4,119✔
1017
   id_cache[W_STD_CHAR]       = ident_new("STD.STANDARD.CHARACTER");
4,119✔
1018
   id_cache[W_STD_NATURAL]    = ident_new("STD.STANDARD.NATURAL");
4,119✔
1019
   id_cache[W_STD_POSITIVE]   = ident_new("STD.STANDARD.POSITIVE");
4,119✔
1020
   id_cache[W_STD_INTEGER]    = ident_new("STD.STANDARD.INTEGER");
4,119✔
1021
   id_cache[W_STD_STRING]     = ident_new("STD.STANDARD.STRING");
4,119✔
1022
   id_cache[W_STD_REAL]       = ident_new("STD.STANDARD.REAL");
4,119✔
1023
   id_cache[W_STD_TIME]       = ident_new("STD.STANDARD.TIME");
4,119✔
1024
   id_cache[W_STD_BIT_VECTOR] = ident_new("STD.STANDARD.BIT_VECTOR");
4,119✔
1025
   id_cache[W_IEEE_SIGNED]    = ident_new("IEEE.NUMERIC_STD.SIGNED");
4,119✔
1026
   id_cache[W_IEEE_UNSIGNED]  = ident_new("IEEE.NUMERIC_STD.UNSIGNED");
4,119✔
1027
   id_cache[W_IEEE_LOGIC]     = ident_new("IEEE.STD_LOGIC_1164.STD_LOGIC");
4,119✔
1028
   id_cache[W_IEEE_ULOGIC]    = ident_new("IEEE.STD_LOGIC_1164.STD_ULOGIC");
4,119✔
1029
   id_cache[W_IEEE_1164_AND]  = ident_new("IEEE.STD_LOGIC_1164.\"and\"");
4,119✔
1030
   id_cache[W_IEEE_1164_NAND] = ident_new("IEEE.STD_LOGIC_1164.\"nand\"");
4,119✔
1031
   id_cache[W_IEEE_1164_OR]   = ident_new("IEEE.STD_LOGIC_1164.\"or\"");
4,119✔
1032
   id_cache[W_IEEE_1164_NOR]  = ident_new("IEEE.STD_LOGIC_1164.\"nor\"");
4,119✔
1033
   id_cache[W_IEEE_1164_XOR]  = ident_new("IEEE.STD_LOGIC_1164.\"xor\"");
4,119✔
1034
   id_cache[W_IEEE_1164_XNOR] = ident_new("IEEE.STD_LOGIC_1164.\"xnor\"");
4,119✔
1035
   id_cache[W_FOREIGN]        = ident_new("FOREIGN");
4,119✔
1036
   id_cache[W_WORK]           = ident_new("WORK");
4,119✔
1037
   id_cache[W_STD]            = ident_new("STD");
4,119✔
1038
   id_cache[W_THUNK]          = ident_new("thunk");
4,119✔
1039
   id_cache[W_BODY]           = ident_new("body");
4,119✔
1040
   id_cache[W_CARET]          = ident_new("^");
4,119✔
1041
   id_cache[W_IEEE]           = ident_new("IEEE");
4,119✔
1042
   id_cache[W_IEEE_1164]      = ident_new("IEEE.STD_LOGIC_1164");
4,119✔
1043
   id_cache[W_ERROR]          = ident_new("error");
4,119✔
1044
   id_cache[W_CCONV]          = ident_new("\"??\"");
4,119✔
1045
   id_cache[W_ELAB]           = ident_new("elab");
4,119✔
1046
   id_cache[W_NUMERIC_STD]    = ident_new("IEEE.NUMERIC_STD");
4,119✔
1047
   id_cache[W_NUMERIC_BIT]    = ident_new("IEEE.NUMERIC_BIT");
4,119✔
1048
   id_cache[W_NVC]            = ident_new("NVC");
4,119✔
1049
   id_cache[W_DEFAULT_CLOCK]  = ident_new("default clock");
4,119✔
1050
   id_cache[W_DOLLAR_DISPLAY] = ident_new("$display");
4,119✔
1051
   id_cache[W_DOLLAR_FINISH]  = ident_new("$finish");
4,119✔
1052
   id_cache[W_DOLLAR_WRITE]   = ident_new("$write");
4,119✔
1053
   id_cache[W_STD_REFLECTION] = ident_new("STD.REFLECTION");
4,119✔
1054

1055
   id_cache[W_NVC_PSL_SUPPORT] = ident_new("NVC.PSL_SUPPORT");
4,119✔
1056

1057
   id_cache[W_IEEE_LOGIC_VECTOR] =
8,238✔
1058
      ident_new("IEEE.STD_LOGIC_1164.STD_LOGIC_VECTOR");
4,119✔
1059
   id_cache[W_IEEE_ULOGIC_VECTOR] =
8,238✔
1060
      ident_new("IEEE.STD_LOGIC_1164.STD_ULOGIC_VECTOR");
4,119✔
1061

1062
   id_cache[W_NUMERIC_STD_UNSIGNED] = ident_new("IEEE.NUMERIC_STD_UNSIGNED");
4,119✔
1063
   id_cache[W_NUMERIC_BIT_UNSIGNED] = ident_new("IEEE.NUMERIC_BIT_UNSIGNED");
4,119✔
1064

1065
   id_cache[W_VITAL] = ident_new("VITAL");
4,119✔
1066
}
4,119✔
1067

1068
bool is_uninstantiated_package(tree_t pack)
26,863✔
1069
{
1070
   return tree_kind(pack) == T_PACKAGE
26,863✔
1071
      && tree_generics(pack) > 0
26,535✔
1072
      && tree_genmaps(pack) == 0;
27,554✔
1073
}
1074

1075
bool is_uninstantiated_subprogram(tree_t decl)
91,427✔
1076
{
1077
   switch (tree_kind(decl)) {
91,427✔
1078
   case T_FUNC_DECL:
91,238✔
1079
   case T_FUNC_BODY:
1080
   case T_PROC_DECL:
1081
   case T_PROC_BODY:
1082
      return tree_generics(decl) > 0;
91,238✔
1083
   default:
1084
      return false;
1085
   }
1086
}
1087

1088
bool unit_needs_cgen(tree_t unit)
139✔
1089
{
1090
   switch (tree_kind(unit)) {
139✔
1091
   case T_PACK_INST:
1092
      return true;
1093
   case T_PACK_BODY:
×
1094
      {
1095
         tree_t pack = tree_primary(unit);
×
1096
         return package_needs_body(pack) && !is_uninstantiated_package(pack);
×
1097
      }
1098
   case T_PACKAGE:
9✔
1099
      return !package_needs_body(unit) && !is_uninstantiated_package(unit);
18✔
1100
   default:
×
1101
      return false;
×
1102
   }
1103
}
1104

1105
bool package_needs_body(tree_t pack)
1,470✔
1106
{
1107
   assert(tree_kind(pack) == T_PACKAGE);
1,470✔
1108

1109
   const int ndecls = tree_decls(pack);
1,470✔
1110
   for (int i = 0; i < ndecls; i++) {
108,029✔
1111
      tree_t d = tree_decl(pack, i);
107,233✔
1112

1113
      switch (tree_kind(d)) {
107,233✔
1114
      case T_FUNC_DECL:
94,980✔
1115
      case T_PROC_DECL:
1116
         if (tree_flags(d) & TREE_F_PREDEFINED)
94,980✔
1117
            continue;
94,012✔
1118
         else if (tree_subkind(d) == S_FOREIGN)
968✔
1119
            continue;
431✔
1120
         return true;
1121

1122
      case T_CONST_DECL:
691✔
1123
         if (!tree_has_value(d))
691✔
1124
            return true;
1125
         continue;
632✔
1126

1127
      case T_PROT_DECL:
1128
         return true;
1129

1130
      default:
11,484✔
1131
         continue;
11,484✔
1132
      }
1133
   }
1134

1135
   return false;
1136
}
1137

1138
tree_t search_decls(tree_t container, ident_t name, int nth)
32,436✔
1139
{
1140
   tree_kind_t kind = tree_kind(container);
32,436✔
1141
   if (kind == T_LIBRARY) {
32,436✔
1142
      if (nth == 0) {
×
1143
         lib_t lib = lib_require(tree_ident(container));
×
1144
         return lib_get(lib, name);
×
1145
      }
1146
      else
1147
         return NULL;
1148
   }
1149
   else if (kind == T_ENTITY || kind == T_BLOCK) {
32,436✔
1150
      const int nports = tree_ports(container);
265✔
1151
      for (int i = 0; i < nports; i++) {
290✔
1152
         tree_t p = tree_port(container, i);
25✔
1153
         if (tree_ident(p) == name && nth-- == 0)
25✔
1154
            return p;
×
1155
      }
1156
   }
1157
   else if (!is_container(container))
32,171✔
1158
      return NULL;
1159

1160
   // TODO: how to improve this?
1161
   const int ndecls = tree_decls(container);
32,436✔
1162
   tree_t best = NULL;
32,436✔
1163

1164
   for (int i = 0; i < ndecls; i++) {
1,813,310✔
1165
      tree_t d = tree_decl(container, i);
1,813,150✔
1166
      if (!tree_has_ident(d))
1,813,150✔
1167
         continue;
×
1168
      else if (tree_ident(d) == name) {
1,813,150✔
1169
         if (tree_kind(d) == T_TYPE_DECL
20,904✔
1170
             && type_kind(tree_type(d)) == T_INCOMPLETE)
19,546✔
1171
            best = d;
1172
         else if (nth-- == 0)
20,872✔
1173
            return d;
20,597✔
1174
      }
1175
      else if (tree_kind(d) == T_TYPE_DECL) {
1,792,250✔
1176
         type_t type = tree_type(d);
140,532✔
1177
         switch (type_kind(type)) {
140,532✔
1178
         case T_ENUM:
105,787✔
1179
            {
1180
               const int nlits = type_enum_literals(type);
105,787✔
1181
               for (int j = 0; j < nlits; j++) {
7,109,140✔
1182
                  tree_t lit = type_enum_literal(type, j);
7,015,030✔
1183
                  if (tree_ident(lit) == name && nth-- == 0)
7,015,030✔
1184
                     return lit;
11,675✔
1185
               }
1186
            }
1187
            break;
1188
         default:
1189
            break;
1190
         }
1191
      }
1,651,710✔
1192
   }
1193

1194
   return best;
1195
}
1196

1197
static tree_t cached_unit(tree_t hint, tree_t *cache, well_known_t lib_name,
19,367✔
1198
                          well_known_t unit_name)
1199
{
1200
   const vhdl_standard_t curr = standard();
19,367✔
1201

1202
   if (cache[curr] == NULL) {
19,367✔
1203
      if (hint != NULL)
3,487✔
1204
         cache[curr] = hint;
972✔
1205
      else {
1206
         lib_t std = lib_require(well_known(lib_name));
2,515✔
1207
         cache[curr] = lib_get(std, well_known(unit_name));
2,515✔
1208
         assert(cache[curr] != NULL);
2,515✔
1209
      }
1210
   }
1211

1212
   assert(hint == NULL || hint == cache[curr]);
19,367✔
1213
   return cache[curr];
19,367✔
1214
}
1215

1216
static tree_t cached_std(tree_t hint)
19,283✔
1217
{
1218
   static tree_t standard_cache[STD_19 + 1] = {};
19,283✔
1219
   return cached_unit(hint, standard_cache, W_STD, W_STD_STANDARD);
19,283✔
1220
}
1221

1222
type_t std_type(tree_t std, std_type_t which)
2,125,820✔
1223
{
1224
   static type_t cache[STD_FILE_OPEN_STATE + 1] = {};
2,125,820✔
1225
   assert(which < ARRAY_LEN(cache));
2,125,820✔
1226

1227
   if (cache[which] == NULL) {
2,125,820✔
1228
      const char *names[] = {
19,283✔
1229
         "universal_integer",
1230
         "universal_real",
1231
         "INTEGER",
1232
         "REAL",
1233
         "BOOLEAN",
1234
         "STRING",
1235
         "TIME",
1236
         "BIT",
1237
         "FILE_OPEN_KIND",
1238
         "FILE_OPEN_STATUS",
1239
         "NATURAL",
1240
         "BIT_VECTOR",
1241
         "SEVERITY_LEVEL",
1242
         "FILE_ORIGIN_KIND",
1243
         "FILE_OPEN_STATE",
1244
      };
1245

1246
      tree_t d = search_decls(cached_std(std), ident_new(names[which]), 0);
19,283✔
1247
      if (d == NULL)
19,283✔
1248
         fatal_trace("cannot find standard type %s", names[which]);
×
1249

1250
      // Do not cache standard types while bootstrapping as the GC will
1251
      // move the objects after parsing
1252
      static int can_cache = -1;
19,283✔
1253
      if (can_cache == -1) can_cache = !opt_get_int(OPT_BOOTSTRAP);
19,283✔
1254

1255
      if (can_cache)
19,283✔
1256
         return (cache[which] = tree_type(d));
18,535✔
1257
      else
1258
         return tree_type(d);
748✔
1259
   }
1260
   else
1261
      return cache[which];
1262
}
1263

1264
type_t ieee_type(ieee_type_t which)
377✔
1265
{
1266
   static type_t cache[IEEE_STD_LOGIC + 1] = {};
377✔
1267
   assert(which < ARRAY_LEN(cache));
377✔
1268

1269
   if (cache[which] == NULL) {
377✔
1270
      const char *names[] = {
52✔
1271
         "STD_ULOGIC",
1272
         "STD_LOGIC",
1273
      };
1274

1275
      static tree_t ieee_cache[STD_19 + 1] = {};
52✔
1276
      tree_t unit = cached_unit(NULL, ieee_cache, W_IEEE, W_IEEE_1164);
52✔
1277

1278
      tree_t d = search_decls(unit, ident_new(names[which]), 0);
52✔
1279
      if (d == NULL)
52✔
1280
         fatal_trace("cannot find IEEE type %s", names[which]);
×
1281

1282
      // STD.STANDARD cannot depend on IEEE
1283
      assert(!opt_get_int(OPT_BOOTSTRAP));
52✔
1284

1285
      return (cache[which] = tree_type(d));
52✔
1286
   }
1287
   else
1288
      return cache[which];
1289
}
1290

1291
type_t reflection_type(reflect_type_t which)
183✔
1292
{
1293
   static type_t cache[REFLECT_SUBTYPE_MIRROR + 1] = {};
183✔
1294
   assert(which < ARRAY_LEN(cache));
183✔
1295

1296
   if (cache[which] == NULL) {
183✔
1297
      const char *names[] = {
32✔
1298
         "VALUE_MIRROR",
1299
         "SUBTYPE_MIRROR",
1300
      };
1301

1302
      static tree_t reflect_cache[STD_19 + 1] = {};
32✔
1303
      tree_t unit = cached_unit(NULL, reflect_cache, W_STD, W_STD_REFLECTION);
32✔
1304

1305
      tree_t d = search_decls(unit, ident_new(names[which]), 0);
32✔
1306
      if (d == NULL)
32✔
1307
         fatal_trace("cannot find REFLECTION type %s", names[which]);
×
1308

1309
      // STD.STANDARD cannot depend on REFLECTION
1310
      assert(!opt_get_int(OPT_BOOTSTRAP));
32✔
1311

1312
      return (cache[which] = tree_type(d));
32✔
1313
   }
1314
   else
1315
      return cache[which];
1316
}
1317

1318
bool is_builtin(subprogram_kind_t kind)
12,846,400✔
1319
{
1320
   return kind != S_USER && kind != S_FOREIGN;
12,846,400✔
1321
}
1322

1323
bool is_open_coded_builtin(subprogram_kind_t kind)
360,048✔
1324
{
1325
   switch (kind) {
360,048✔
1326
   case S_USER:
1327
   case S_FOREIGN:
1328
   case S_ARRAY_EQ:
1329
   case S_ARRAY_NEQ:
1330
   case S_ARRAY_LT:
1331
   case S_ARRAY_LE:
1332
   case S_ARRAY_GT:
1333
   case S_ARRAY_GE:
1334
   case S_RECORD_EQ:
1335
   case S_RECORD_NEQ:
1336
   case S_TO_STRING:
1337
   case S_SLL:
1338
   case S_SRL:
1339
   case S_SLA:
1340
   case S_SRA:
1341
   case S_ROL:
1342
   case S_ROR:
1343
   case S_ARRAY_NOT:
1344
   case S_ARRAY_AND:
1345
   case S_ARRAY_OR:
1346
   case S_ARRAY_XOR:
1347
   case S_ARRAY_XNOR:
1348
   case S_ARRAY_NAND:
1349
   case S_ARRAY_NOR:
1350
   case S_MIXED_AND:
1351
   case S_MIXED_OR:
1352
   case S_MIXED_XOR:
1353
   case S_MIXED_XNOR:
1354
   case S_MIXED_NAND:
1355
   case S_MIXED_NOR:
1356
   case S_REDUCE_OR:
1357
   case S_REDUCE_AND:
1358
   case S_REDUCE_NAND:
1359
   case S_REDUCE_NOR:
1360
   case S_REDUCE_XOR:
1361
   case S_REDUCE_XNOR:
1362
   case S_MATCH_EQ:
1363
   case S_MATCH_NEQ:
1364
   case S_MATCH_LT:
1365
   case S_MATCH_LE:
1366
   case S_MATCH_GT:
1367
   case S_MATCH_GE:
1368
   case S_MINIMUM:
1369
   case S_MAXIMUM:
1370
      return false;
1371
   default:
219,874✔
1372
      return true;
219,874✔
1373
   }
1374
}
1375

1376
tree_t std_func(ident_t mangled)
×
1377
{
1378
   tree_t std = cached_std(NULL);
×
1379

1380
   const int ndecls = tree_decls(std);
×
1381
   for (int i = 0; i < ndecls; i++) {
×
1382
      tree_t d = tree_decl(std, i);
×
1383
      if (is_subprogram(d) && tree_has_ident2(d) && tree_ident2(d) == mangled)
×
1384
         return d;
×
1385
   }
1386

1387
   return NULL;
1388
}
1389

1390
tree_t name_to_ref(tree_t name)
78,988✔
1391
{
1392
   tree_kind_t kind;
78,988✔
1393
   while ((kind = tree_kind(name)) != T_REF) {
89,285✔
1394
      switch (kind) {
11,639✔
1395
      case T_ARRAY_REF:
10,297✔
1396
      case T_ARRAY_SLICE:
1397
      case T_RECORD_REF:
1398
      case T_ALL:
1399
         name = tree_value(name);
10,297✔
1400
         break;
10,297✔
1401
      default:
1402
         return NULL;
1403
      }
1404
   }
1405

1406
   return name;
1407
}
1408

1409
const char *port_mode_str(port_mode_t mode)
22✔
1410
{
1411
   const char *mode_str[] = {
22✔
1412
      "INVALID", "IN", "OUT", "INOUT", "BUFFER", "LINKAGE", "VIEW", "VIEW"
1413
   };
1414
   assert(mode < ARRAY_LEN(mode_str));
22✔
1415
   return mode_str[mode];
22✔
1416
}
1417

1418
void mangle_one_type(text_buf_t *buf, type_t type)
156,888✔
1419
{
1420
   ident_t ident = type_ident(type);
156,888✔
1421

1422
   char code = 0;
156,888✔
1423
   switch (is_well_known(ident)) {
156,888✔
1424
   case W_STD_INTEGER:        code = 'I'; break;
1425
   case W_STD_STRING:         code = 'S'; break;
3,040✔
1426
   case W_STD_REAL:           code = 'R'; break;
3,278✔
1427
   case W_STD_BOOL:           code = 'B'; break;
21,181✔
1428
   case W_STD_CHAR:           code = 'C'; break;
651✔
1429
   case W_STD_TIME:           code = 'T'; break;
891✔
1430
   case W_STD_NATURAL:        code = 'N'; break;
3,945✔
1431
   case W_STD_POSITIVE:       code = 'P'; break;
324✔
1432
   case W_STD_BIT:            code = 'J'; break;
2,353✔
1433
   case W_STD_BIT_VECTOR:     code = 'Q'; break;
2,278✔
1434
   case W_IEEE_LOGIC:         code = 'L'; break;
244✔
1435
   case W_IEEE_ULOGIC:        code = 'U'; break;
4,734✔
1436
   case W_IEEE_LOGIC_VECTOR:  code = 'V'; break;
1,539✔
1437
   case W_IEEE_ULOGIC_VECTOR: code = 'Y'; break;
1,397✔
1438
   default: break;
1439
   }
1440

1441
   if (code)
45,855✔
1442
      tb_append(buf, code);
57,657✔
1443
   else {
1444
      tb_printf(buf, "%zu", ident_len(ident));
99,231✔
1445
      tb_istr(buf, ident);
99,231✔
1446
   }
1447
}
156,888✔
1448

1449
tree_t primary_unit_of(tree_t unit)
16,969✔
1450
{
1451
   switch (tree_kind(unit)) {
16,969✔
1452
   case T_ENTITY:
1453
   case T_COMPONENT:
1454
   case T_PACKAGE:
1455
   case T_BLOCK:
1456
   case T_ELAB:
1457
      return unit;
1458
   case T_ARCH:
7,013✔
1459
   case T_CONFIGURATION:
1460
   case T_PACK_BODY:
1461
      return tree_primary(unit);
7,013✔
1462
   default:
×
1463
      fatal_trace("invalid kind %s in primary_unit_of",
×
1464
                  tree_kind_str(tree_kind(unit)));
1465
   }
1466
}
1467

1468
unsigned get_case_choice_char(tree_t value, int depth)
7,573✔
1469
{
1470
   switch (tree_kind(value)) {
7,573✔
1471
   case T_STRING:
6,823✔
1472
      if (depth < tree_chars(value)) {
6,823✔
1473
         tree_t ch = tree_char(value, depth);
6,822✔
1474
         return tree_pos(tree_ref(ch));
6,822✔
1475
      }
1476
      else
1477
         return ~0;   // Out of bounds
1478

1479
   case T_AGGREGATE:
14✔
1480
      {
1481
         const int nassocs = tree_assocs(value);
14✔
1482
         type_t type = tree_type(value);
14✔
1483

1484
         for (int i = 0; i < nassocs; i++) {
24✔
1485
            tree_t a = tree_assoc(value, i);
23✔
1486
            switch (tree_subkind(a)) {
23✔
1487
            case A_NAMED:
×
1488
               if (rebase_index(type, 0, assume_int(tree_name(a))) == depth)
×
1489
                  return assume_int(tree_value(a));
×
1490
               break;
1491

1492
            case A_POS:
23✔
1493
               if (tree_pos(a) == (unsigned)depth)
23✔
1494
                  return assume_int(tree_value(a));
13✔
1495
               break;
1496

1497
            case A_OTHERS:
×
1498
               return assume_int(tree_value(a));
×
1499
            }
1500
         }
10✔
1501

1502
         // This will produce an error during bounds checking
1503
         return ~0;
1504
      }
1505

1506
   case T_REF:
392✔
1507
      {
1508
         tree_t decl = tree_ref(value);
392✔
1509
         assert(tree_kind(decl) == T_CONST_DECL || tree_kind(decl) == T_ALIAS);
392✔
1510
         assert(tree_has_value(decl));
392✔
1511
         return get_case_choice_char(tree_value(decl), depth);
392✔
1512
      }
1513

1514
   case T_ARRAY_SLICE:
240✔
1515
      {
1516
         tree_t base = tree_value(value);
240✔
1517
         tree_t r = tree_range(value, 0);
240✔
1518
         const int64_t rleft = assume_int(tree_left(r));
240✔
1519
         const int64_t offset = rebase_index(tree_type(base), 0, rleft);
240✔
1520
         return get_case_choice_char(base, depth + offset);
240✔
1521
      }
1522

1523
   case T_FCALL:
104✔
1524
      if (tree_subkind(tree_ref(value)) == S_CONCAT) {
104✔
1525
         const int nparams = tree_params(value);
104✔
1526
         for (int i = 0; i < nparams; i++) {
156✔
1527
            tree_t left = tree_value(tree_param(value, i));
156✔
1528

1529
            type_t left_type = tree_type(left);
156✔
1530
            if (type_is_unconstrained(left_type))
156✔
1531
               fatal_at(tree_loc(left), "sorry, this expression is not "
×
1532
                        "currently supported in a case choice");
1533

1534
            tree_t lr = range_of(tree_type(left), 0);
156✔
1535
            int64_t left_len;
156✔
1536
            if (!folded_length(lr, &left_len))
156✔
1537
               fatal_at(tree_loc(left), "cannot determine length of left hand "
×
1538
                        "side of concatenation");
1539

1540
            if (depth < left_len || i + 1 == nparams)
156✔
1541
               return get_case_choice_char(left, depth);
104✔
1542

1543
            depth -= left_len;
52✔
1544
         }
1545
      }
1546
      // Fall-through
1547

1548
   default:
1549
      fatal_at(tree_loc(value), "unsupported tree type %s in case choice",
×
1550
               tree_kind_str(tree_kind(value)));
1551
   }
1552
}
1553

1554
int64_t encode_case_choice(tree_t value, int length, int bits)
981✔
1555
{
1556
   uint64_t enc = 0;
981✔
1557
   for (int i = 0; i < length; i++) {
7,812✔
1558
      if (bits > 0) {
6,831✔
1559
         enc <<= bits;
1,631✔
1560
         enc |= get_case_choice_char(value, i);
1,631✔
1561
      }
1562
      else {
1563
         enc *= 0x27d4eb2d;
5,200✔
1564
         enc += get_case_choice_char(value, i);
5,200✔
1565
      }
1566
   }
1567

1568
   return enc;
981✔
1569
}
1570

1571
void to_string(text_buf_t *tb, type_t type, int64_t value)
553✔
1572
{
1573
   if (type_is_integer(type))
553✔
1574
      tb_printf(tb, "%"PRIi64, value);
419✔
1575
   else if (type_is_enum(type)) {
134✔
1576
      type_t base = type_base_recur(type);
62✔
1577
      if (value < 0 || value >= type_enum_literals(base))
62✔
1578
         tb_printf(tb, "%"PRIi64, value);
3✔
1579
      else
1580
         tb_cat(tb, istr(tree_ident(type_enum_literal(base, value))));
59✔
1581
   }
1582
   else if (type_is_physical(type)) {
72✔
1583
      type_t base = type_base_recur(type);
24✔
1584
      const unsigned nunits = type_units(base);
24✔
1585
      tree_t max_unit = NULL;
24✔
1586
      int64_t max_unit_value = 0;
24✔
1587

1588
      // Find the largest unit that evenly divides the given value
1589
      for (unsigned u = 0; u < nunits; u++) {
216✔
1590
         tree_t unit = type_unit(base, u);
192✔
1591
         const int64_t unit_value = assume_int(tree_value(unit));
192✔
1592
         if ((unit_value > max_unit_value) && (value % unit_value == 0)) {
192✔
1593
            max_unit = unit;
90✔
1594
            max_unit_value = unit_value;
90✔
1595
         }
1596
      }
1597
      assert(max_unit);
24✔
1598

1599
      tb_printf(tb, "%"PRIi64" %s", value / max_unit_value,
24✔
1600
                istr(tree_ident(max_unit)));
1601
   }
1602
   else if (type_is_real(type)) {
48✔
1603
      union { int64_t i; double r; } u = { .i = value };
42✔
1604
      tb_printf(tb, "%.17g", u.r);
42✔
1605
   }
1606
   else if (type_is_access(type)) {
6✔
1607
      if (value == 0)
6✔
1608
         tb_cat(tb, "NULL");
3✔
1609
      else
1610
         tb_printf(tb, "%p", (void *)value);
3✔
1611
   }
1612
}
553✔
1613

1614
static bool is_static(tree_t expr)
4,873✔
1615
{
1616
   switch (tree_kind(expr)) {
4,974✔
1617
   case T_REF:
432✔
1618
      {
1619
         tree_t decl = tree_ref(expr);
432✔
1620
         switch (tree_kind(decl)) {
432✔
1621
         case T_CONST_DECL:
119✔
1622
            return !(tree_flags(decl) & TREE_F_SEQ_BLOCK);
119✔
1623
         case T_UNIT_DECL:
1624
         case T_ENUM_LIT:
1625
         case T_GENERIC_DECL:
1626
            return true;
1627
         case T_ALIAS:
×
1628
            return is_static(tree_value(decl));
×
1629
         default:
113✔
1630
            return false;
113✔
1631
         }
1632
      }
1633

1634
   case T_LITERAL:
1635
   case T_STRING:
1636
      return true;
1637

1638
   case T_FCALL:
315✔
1639
      return !!(tree_flags(expr) & (TREE_F_LOCALLY_STATIC
315✔
1640
                                    | TREE_F_GLOBALLY_STATIC));
1641

1642
   case T_RECORD_REF:
101✔
1643
      return is_static(tree_value(expr));
101✔
1644

1645
   default:
×
1646
      return false;
×
1647
   }
1648
}
1649

1650
tree_t longest_static_prefix(tree_t expr)
17,310✔
1651
{
1652
   switch (tree_kind(expr)) {
17,310✔
1653
   case T_ARRAY_REF:
3,382✔
1654
      {
1655
         tree_t value = tree_value(expr);
3,382✔
1656
         tree_t prefix = longest_static_prefix(value);
3,382✔
1657

1658
         if (prefix != value)
3,382✔
1659
            return prefix;
1660

1661
         const int nparams = tree_params(expr);
3,369✔
1662
         for (int i = 0; i < nparams; i++) {
7,018✔
1663
            if (!is_static(tree_value(tree_param(expr, i))))
3,795✔
1664
               return prefix;
1665
         }
1666

1667
         return expr;
1668
      }
1669

1670
   case T_ARRAY_SLICE:
555✔
1671
      {
1672
         tree_t value = tree_value(expr);
555✔
1673
         tree_t prefix = longest_static_prefix(value);
555✔
1674

1675
         if (prefix != value)
555✔
1676
            return prefix;
1677

1678
         const int nranges = tree_ranges(expr);
548✔
1679
         for (int i = 0; i < nranges; i++) {
1,080✔
1680
            tree_t r = tree_range(expr, i);
548✔
1681
            if (tree_subkind(r) == RANGE_EXPR)
548✔
1682
               return prefix;
1683
            else if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
545✔
1684
               return prefix;
13✔
1685
         }
1686

1687
         return expr;
1688
      }
1689

1690
   case T_RECORD_REF:
938✔
1691
      {
1692
         tree_t value = tree_value(expr);
938✔
1693
         tree_t prefix = longest_static_prefix(value);
938✔
1694

1695
         if (prefix != value)
938✔
1696
            return prefix;
×
1697

1698
         return expr;
1699
      }
1700

1701
   default:
1702
      return expr;
1703
   }
1704
}
1705

1706
tree_t body_of(tree_t pack)
687✔
1707
{
1708
   const tree_kind_t kind = tree_kind(pack);
687✔
1709
   if (kind == T_PACK_INST)
687✔
1710
      return NULL;
1711

1712
   assert(tree_kind(pack) == T_PACKAGE);
687✔
1713

1714
   ident_t body_i = well_known(W_BODY);
687✔
1715
   ident_t body_name = ident_prefix(tree_ident(pack), body_i, '-');
687✔
1716
   return lib_get_qualified(body_name);
687✔
1717
}
1718

1719
tree_t find_generic_map(tree_t unit, int pos, tree_t g)
1,183✔
1720
{
1721
   const int ngenmaps = tree_genmaps(unit);
1,183✔
1722

1723
   if (pos < ngenmaps) {
1,183✔
1724
      tree_t m = tree_genmap(unit, pos);
1,180✔
1725
      if (tree_subkind(m) == P_POS && tree_pos(m) == pos)
1,180✔
1726
         return tree_value(m);
650✔
1727
   }
1728

1729
   for (int j = 0; j < ngenmaps; j++) {
1,402✔
1730
      tree_t m = tree_genmap(unit, j);
1,399✔
1731
      switch (tree_subkind(m)) {
1,399✔
1732
      case P_NAMED:
1,000✔
1733
         {
1734
            tree_t name = tree_name(m);
1,000✔
1735
            assert(tree_kind(name) == T_REF);
1,000✔
1736

1737
            if (tree_ref(name) == g)
1,000✔
1738
               return tree_value(m);
322✔
1739
         }
1740
         break;
1741

1742
      case P_POS:
399✔
1743
         if (tree_pos(m) == pos)
399✔
1744
            return tree_value(m);
208✔
1745
         break;
1746

1747
      default:
1748
         break;
1749
      }
1750
   }
1751

1752
   return NULL;
1753
}
1754

1755
bool relaxed_rules(void)
827,269✔
1756
{
1757
   return opt_get_int(OPT_RELAXED);
827,269✔
1758
}
1759

1760
bool is_type_attribute(attr_kind_t kind)
61,391✔
1761
{
1762
   switch (kind) {
61,391✔
1763
   case ATTR_SUBTYPE:
1764
   case ATTR_BASE:
1765
   case ATTR_ELEMENT:
1766
   case ATTR_DESIGNATED_SUBTYPE:
1767
   case ATTR_INDEX:
1768
      return true;
1769
   default:
61,064✔
1770
      return false;
61,064✔
1771
   }
1772
}
1773

1774
bool attribute_has_param(attr_kind_t attr)
20,441✔
1775
{
1776
   switch (attr) {
20,441✔
1777
   case ATTR_IMAGE:
1778
   case ATTR_SUCC:
1779
   case ATTR_PRED:
1780
   case ATTR_DELAYED:
1781
   case ATTR_LEFTOF:
1782
   case ATTR_RIGHTOF:
1783
   case ATTR_VALUE:
1784
   case ATTR_POS:
1785
   case ATTR_LOW:
1786
   case ATTR_HIGH:
1787
   case ATTR_LEFT:
1788
   case ATTR_RIGHT:
1789
   case ATTR_LENGTH:
1790
   case ATTR_RANGE:
1791
   case ATTR_REVERSE_RANGE:
1792
   case ATTR_VAL:
1793
   case ATTR_QUIET:
1794
   case ATTR_STABLE:
1795
   case ATTR_INDEX:
1796
   case ATTR_ASCENDING:
1797
      return true;
1798
   default:
1,814✔
1799
      return false;
1,814✔
1800
   }
1801
}
1802

1803
type_t get_type_or_null(tree_t t)
1,780,790✔
1804
{
1805
   switch (tree_kind(t)) {
1,780,790✔
1806
   case T_LIBRARY:
1807
   case T_ATTR_SPEC:
1808
   case T_PACKAGE:
1809
   case T_PACK_INST:
1810
   case T_PACK_BODY:
1811
   case T_ENTITY:
1812
   case T_ARCH:
1813
   case T_PROCESS:
1814
   case T_COMPONENT:
1815
   case T_INSTANCE:
1816
   case T_CONCURRENT:
1817
   case T_BLOCK:
1818
   case T_WHILE:
1819
   case T_FOR:
1820
   case T_GROUP_TEMPLATE:
1821
   case T_CONFIGURATION:
1822
   case T_GROUP:
1823
   case T_FOR_GENERATE:
1824
   case T_IF_GENERATE:
1825
   case T_USE:
1826
   case T_CONTEXT:
1827
   case T_PSL:
1828
      return NULL;
1829
   default:
1,711,650✔
1830
      if (tree_has_type(t))
1,711,650✔
1831
         return tree_type(t);
1,710,660✔
1832
      else
1833
         return NULL;
1834
   }
1835
}
1836

1837
type_t subtype_for_string(tree_t str, type_t base)
24,043✔
1838
{
1839
   if (!type_is_unconstrained(base))
24,043✔
1840
      return base;
1841

1842
   // Construct a new constrained array subtype: the direction and
1843
   // bounds are the same as those for a positional array aggregate
1844
   type_t sub = type_new(T_SUBTYPE);
21,307✔
1845
   type_set_base(sub, base);
21,307✔
1846

1847
   type_t index_type = index_type_of(base, 0);
21,307✔
1848
   const bool is_enum = type_is_enum(index_type);
21,307✔
1849

1850
   // The direction is determined by the index type
1851
   range_kind_t dir = direction_of(index_type, 0);
21,307✔
1852

1853
   // The left bound is the left of the index type and the right bound
1854
   // is determined by the number of elements
1855

1856
   tree_t left = NULL, right = NULL;
21,307✔
1857

1858
   if (is_enum)
21,307✔
1859
      left = make_ref(type_enum_literal(type_base_recur(index_type), 0));
1✔
1860
   else
1861
      left = tree_left(range_of(index_type, 0));
21,306✔
1862

1863
   const int nchars = tree_chars(str);
21,307✔
1864
   int64_t iright;
21,307✔
1865
   if (dir == RANGE_DOWNTO)
21,307✔
1866
      iright = assume_int(left) - nchars + 1;
×
1867
   else
1868
      iright = assume_int(left) + nchars - 1;
21,307✔
1869

1870
   if (is_enum)
21,307✔
1871
      right = get_enum_lit(str, index_type, iright);
1✔
1872
   else
1873
      right = get_int_lit(str, index_type, iright);
21,306✔
1874

1875
   tree_t r = tree_new(T_RANGE);
21,307✔
1876
   tree_set_subkind(r, dir);
21,307✔
1877
   tree_set_left(r, left);
21,307✔
1878
   tree_set_right(r, right);
21,307✔
1879
   tree_set_loc(r, tree_loc(str));
21,307✔
1880
   tree_set_type(r, index_type);
21,307✔
1881

1882
   tree_t c = tree_new(T_CONSTRAINT);
21,307✔
1883
   tree_set_subkind(c, C_INDEX);
21,307✔
1884
   tree_add_range(c, r);
21,307✔
1885
   tree_set_loc(c, tree_loc(str));
21,307✔
1886

1887
   type_add_constraint(sub, c);
21,307✔
1888

1889
   return sub;
21,307✔
1890
}
1891

1892
tree_t change_ref(tree_t name, tree_t new)
1,678✔
1893
{
1894
   switch (tree_kind(name)) {
1,678✔
1895
   case T_REF:
1,138✔
1896
      return make_ref(new);
1,138✔
1897

1898
   case T_ARRAY_REF:
89✔
1899
      {
1900
         tree_t t = tree_new(T_ARRAY_REF);
89✔
1901
         tree_set_loc(t, tree_loc(name));
89✔
1902
         tree_set_value(t, change_ref(tree_value(name), new));
89✔
1903
         tree_set_type(t, tree_type(name));
89✔
1904

1905
         const int nparams = tree_params(name);
89✔
1906
         for (int i = 0; i < nparams; i++)
178✔
1907
            tree_add_param(t, tree_param(name, i));
89✔
1908

1909
         return t;
1910
      }
1911

1912
   case T_ARRAY_SLICE:
43✔
1913
      {
1914
         tree_t t = tree_new(T_ARRAY_SLICE);
43✔
1915
         tree_set_loc(t, tree_loc(name));
43✔
1916
         tree_set_value(t, change_ref(tree_value(name), new));
43✔
1917
         tree_set_type(t, tree_type(name));
43✔
1918
         tree_add_range(t, tree_range(name, 0));
43✔
1919

1920
         return t;
43✔
1921
      }
1922

1923
   case T_RECORD_REF:
351✔
1924
      {
1925
         tree_t t = tree_new(T_RECORD_REF);
351✔
1926
         tree_set_loc(t, tree_loc(name));
351✔
1927
         tree_set_value(t, change_ref(tree_value(name), new));
351✔
1928
         tree_set_type(t, tree_type(name));
351✔
1929
         tree_set_ident(t, tree_ident(name));
351✔
1930
         tree_set_ref(t, tree_ref(name));
351✔
1931

1932
         return t;
351✔
1933
      }
1934

1935
   case T_CONV_FUNC:
54✔
1936
      {
1937
         tree_t t = tree_new(T_CONV_FUNC);
54✔
1938
         tree_set_loc(t, tree_loc(name));
54✔
1939
         tree_set_value(t, change_ref(tree_value(name), new));
54✔
1940
         tree_set_ident(t, tree_ident(name));
54✔
1941
         tree_set_type(t, tree_type(name));
54✔
1942
         tree_set_ref(t, tree_ref(name));
54✔
1943

1944
         return t;
54✔
1945
      }
1946

1947
   case T_TYPE_CONV:
3✔
1948
      {
1949
         tree_t t = tree_new(T_TYPE_CONV);
3✔
1950
         tree_set_loc(t, tree_loc(name));
3✔
1951
         tree_set_type(t, tree_type(name));
3✔
1952
         tree_set_value(t, change_ref(tree_value(name), new));
3✔
1953

1954
         return t;
3✔
1955
      }
1956

1957
   default:
×
1958
      fatal_trace("cannot handle tree kind %s in elab_change_ref",
×
1959
                  tree_kind_str(tree_kind(name)));
1960
   }
1961
}
1962

1963
static void build_wait_for_target(tree_t expr, build_wait_fn_t fn, void *ctx)
2,119✔
1964
{
1965
   switch (tree_kind(expr)) {
2,119✔
1966
   case T_ARRAY_SLICE:
60✔
1967
      build_wait(tree_range(expr, 0), fn, ctx);
60✔
1968
      break;
60✔
1969

1970
   case T_ARRAY_REF:
325✔
1971
      {
1972
         const int nparams = tree_params(expr);
325✔
1973
         for (int i = 0; i < nparams; i++)
650✔
1974
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
325✔
1975
      }
1976
      break;
1977

1978
   default:
1979
      break;
1980
   }
1981
}
2,119✔
1982

1983
void build_wait(tree_t expr, build_wait_fn_t fn, void *ctx)
9,685✔
1984
{
1985
   // LRM 08 section 10.2 has rules for building a wait statement from a
1986
   // sensitivity list. LRM 08 section 11.3 extends these rules to
1987
   // all-sensitised processes.
1988

1989
   switch (tree_kind(expr)) {
12,241✔
1990
   case T_REF:
3,096✔
1991
      if (class_of(tree_ref(expr)) == C_SIGNAL)
3,096✔
1992
         (*fn)(expr, ctx);
1,723✔
1993
      break;
1994

1995
   case T_EXTERNAL_NAME:
3✔
1996
      if (tree_class(expr) == C_SIGNAL)
3✔
1997
         (*fn)(expr, ctx);
3✔
1998
      break;
1999

2000
   case T_WAVEFORM:
2,485✔
2001
   case T_QUALIFIED:
2002
   case T_TYPE_CONV:
2003
   case T_ASSERT:
2004
      if (tree_has_value(expr))
2,485✔
2005
         build_wait(tree_value(expr), fn, ctx);
2,482✔
2006
      break;
2007

2008
   case T_ARRAY_REF:
586✔
2009
   case T_ARRAY_SLICE:
2010
   case T_RECORD_REF:
2011
      {
2012
         tree_t ref = name_to_ref(expr);
586✔
2013
         if (ref != NULL && class_of(ref) == C_SIGNAL
586✔
2014
             && longest_static_prefix(expr) == expr)
422✔
2015
            (*fn)(expr, ctx);
388✔
2016
         else {
2017
            build_wait(tree_value(expr), fn, ctx);
198✔
2018
            build_wait_for_target(expr, fn, ctx);
198✔
2019
         }
2020
      }
2021
      break;
2022

2023
   case T_FCALL:
1,748✔
2024
   case T_PCALL:
2025
   case T_PROT_FCALL:
2026
   case T_PROT_PCALL:
2027
      {
2028
         tree_t decl = tree_ref(expr);
1,748✔
2029
         const int nparams = tree_params(expr);
1,748✔
2030
         for (int i = 0; i < nparams; i++) {
4,851✔
2031
            tree_t p = tree_param(expr, i);
3,103✔
2032
            assert(tree_subkind(p) == P_POS);
3,103✔
2033
            const port_mode_t mode = tree_subkind(tree_port(decl, tree_pos(p)));
3,103✔
2034
            if (mode == PORT_IN || mode == PORT_INOUT)
3,103✔
2035
               build_wait(tree_value(p), fn, ctx);
3,096✔
2036
         }
2037
      }
2038
      break;
2039

2040
   case T_AGGREGATE:
173✔
2041
      {
2042
         const int nassocs = tree_assocs(expr);
173✔
2043
         for (int i = 0; i < nassocs; i++)
629✔
2044
            build_wait(tree_value(tree_assoc(expr, i)), fn, ctx);
456✔
2045
      }
2046
      break;
2047

2048
   case T_ATTR_REF:
111✔
2049
      {
2050
         const attr_kind_t predef = tree_subkind(expr);
111✔
2051
         if (predef == ATTR_EVENT || predef == ATTR_ACTIVE)
111✔
2052
            build_wait(tree_name(expr), fn, ctx);
60✔
2053

2054
         const int nparams = tree_params(expr);
111✔
2055
         for (int i = 0; i < nparams; i++)
112✔
2056
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
1✔
2057
      }
2058
      break;
2059

2060
   case T_LITERAL:
2061
   case T_STRING:
2062
      break;
2063

2064
   case T_IF:
156✔
2065
      {
2066
         const int nconds = tree_conds(expr);
156✔
2067
         for (int i = 0; i < nconds; i++)
450✔
2068
            build_wait(tree_cond(expr, i), fn, ctx);
294✔
2069
      }
2070
      break;
2071

2072
   case T_COND_STMT:
294✔
2073
      {
2074
         if (tree_has_value(expr))
294✔
2075
            build_wait(tree_value(expr), fn, ctx);
178✔
2076

2077
         const int nstmts = tree_stmts(expr);
294✔
2078
         for (int i = 0; i < nstmts; i++)
594✔
2079
            build_wait(tree_stmt(expr, i), fn, ctx);
300✔
2080
      }
2081
      break;
2082

2083
   case T_PROCESS:
21✔
2084
   case T_SEQUENCE:
2085
   case T_PROC_BODY:
2086
      {
2087
         const int ndecls = tree_decls(expr);
21✔
2088
         for (int i = 0; i < ndecls; i++) {
29✔
2089
            tree_t d = tree_decl(expr, i);
8✔
2090
            if (tree_kind(d) == T_PROC_BODY)
8✔
2091
               build_wait(d, fn, ctx);
1✔
2092
         }
2093

2094
         const int nstmts = tree_stmts(expr);
21✔
2095
         for (int i = 0; i < nstmts; i++)
46✔
2096
            build_wait(tree_stmt(expr, i), fn, ctx);
25✔
2097
      }
2098
      break;
2099

2100
   case T_SIGNAL_ASSIGN:
1,914✔
2101
      {
2102
         build_wait_for_target(tree_target(expr), fn, ctx);
1,914✔
2103

2104
         const int nwaves = tree_waveforms(expr);
1,914✔
2105
         for (int i = 0; i < nwaves; i++)
3,899✔
2106
            build_wait(tree_waveform(expr, i), fn, ctx);
1,985✔
2107
      }
2108
      break;
2109

2110
   case T_VAR_ASSIGN:
7✔
2111
      build_wait_for_target(tree_target(expr), fn, ctx);
7✔
2112
      build_wait(tree_value(expr), fn, ctx);
7✔
2113
      break;
7✔
2114

2115
   case T_CASE:
33✔
2116
   case T_MATCH_CASE:
2117
      {
2118
         build_wait(tree_value(expr), fn, ctx);
33✔
2119

2120
         const int nstmts = tree_stmts(expr);
33✔
2121
         for (int i = 0; i < nstmts; i++) {
237✔
2122
            tree_t alt = tree_stmt(expr, i);
204✔
2123

2124
            const int nstmts = tree_stmts(alt);
204✔
2125
            for (int j = 0; j < nstmts; j++)
408✔
2126
               build_wait(tree_stmt(alt, j), fn, ctx);
204✔
2127
         }
2128
      }
2129
      break;
2130

2131
   case T_FOR:
1✔
2132
      {
2133
         build_wait(tree_range(expr, 0), fn, ctx);
1✔
2134

2135
         const int nstmts = tree_stmts(expr);
1✔
2136
         for (int i = 0; i < nstmts; i++)
2✔
2137
            build_wait(tree_stmt(expr, i), fn, ctx);
1✔
2138
      }
2139
      break;
2140

2141
   case T_WHILE:
4✔
2142
      {
2143
         build_wait(tree_value(expr), fn, ctx);
4✔
2144

2145
         const int nstmts = tree_stmts(expr);
4✔
2146
         for (int i = 0; i < nstmts; i++)
20✔
2147
            build_wait(tree_stmt(expr, i), fn, ctx);
16✔
2148
      }
2149
      break;
2150

2151
   case T_NEXT:
9✔
2152
   case T_EXIT:
2153
      if (tree_has_value(expr))
9✔
2154
         build_wait(tree_value(expr), fn, ctx);
6✔
2155
      break;
2156

2157
   case T_RANGE:
61✔
2158
      if (tree_subkind(expr) == RANGE_EXPR)
61✔
2159
         build_wait(tree_value(expr), fn, ctx);
×
2160
      else {
2161
         build_wait(tree_left(expr), fn, ctx);
61✔
2162
         build_wait(tree_right(expr), fn, ctx);
61✔
2163
      }
2164
      break;
2165

2166
   default:
×
2167
      fatal_trace("Cannot handle tree kind %s in wait expression",
×
2168
                  tree_kind_str(tree_kind(expr)));
2169
   }
2170
}
9,685✔
2171

2172
void print_syntax(const char *fmt, ...)
1,369✔
2173
{
2174
   LOCAL_TEXT_BUF tb = tb_new();
1,369✔
2175
   bool highlighting = false;
1,369✔
2176
   static bool comment = false, last_was_newline = false;
1,369✔
2177
   for (const char *p = fmt; *p != '\0'; p++) {
7,731✔
2178
      if (comment) {
6,362✔
2179
         if (*p == '\n' || *p == '\r') {
2,954✔
2180
            comment = false;
90✔
2181
            last_was_newline = true;
90✔
2182
            tb_printf(tb, "$$\n");
90✔
2183
         }
2184
         else if (*p != '~' && *p != '#') {
2,864✔
2185
            tb_append(tb, *p);
2,704✔
2186
            last_was_newline = false;
2,704✔
2187
         }
2188
         if (p > fmt && *p == '/' && *(p - 1) == '*') {
2,954✔
2189
            tb_printf(tb, "$$");
1✔
2190
            comment = false;
1✔
2191
            last_was_newline = false;
1✔
2192
         }
2193
      }
2194
      else if (*p == '\r') {
3,408✔
2195
         if (!last_was_newline) {
18✔
2196
            tb_append(tb, '\n');
×
2197
            last_was_newline = true;
×
2198
         }
2199
      }
2200
      else if (*p == '#') {
3,390✔
2201
         tb_printf(tb, "$bold$$cyan$");
237✔
2202
         last_was_newline = false;
237✔
2203
         highlighting = true;
237✔
2204
      }
2205
      else if (*p == '~') {
3,153✔
2206
         tb_printf(tb, "$yellow$");
×
2207
         last_was_newline = false;
×
2208
         highlighting = true;
×
2209
      }
2210
      else if ((*p == '-' && *(p + 1) == '-')
3,153✔
2211
               || (*p == '/' && *(p + 1) == '/')
3,065✔
2212
               || (*p == '/' && *(p + 1) == '*')) {
3,063✔
2213
         tb_printf(tb, "$red$%c", *p);
91✔
2214
         last_was_newline = false;
91✔
2215
         comment = true;
91✔
2216
      }
2217
      else if (!isalnum_iso88591(*p) && *p != '_'
3,062✔
2218
               && *p != '%' && highlighting) {
1,520✔
2219
         tb_printf(tb, "$$%c", *p);
217✔
2220
         last_was_newline = false;
217✔
2221
         highlighting = false;
217✔
2222
      }
2223
      else {
2224
         tb_append(tb, *p);
2,845✔
2225
         last_was_newline = (*p == '\n');
2,845✔
2226
      }
2227
   }
2228

2229
   if (highlighting)
1,369✔
2230
      tb_cat(tb, "$$");
20✔
2231

2232
   va_list ap;
1,369✔
2233
   va_start(ap, fmt);
1,369✔
2234

2235
   if (syntax_buf != NULL) {
1,369✔
2236
      char *stripped LOCAL = strip_color(tb_get(tb), ap);
2,738✔
2237
      tb_cat(syntax_buf, stripped);
1,369✔
2238
   }
2239
   else
2240
      color_vprintf(tb_get(tb), ap);
×
2241

2242
   va_end(ap);
1,369✔
2243
}
1,369✔
2244

2245
void capture_syntax(text_buf_t *tb)
7✔
2246
{
2247
   assert(tb == NULL || syntax_buf == NULL);
7✔
2248
   syntax_buf = tb;
7✔
2249
}
7✔
2250

2251
void analyse_file(const char *file, jit_t *jit, unit_registry_t *ur)
2,892✔
2252
{
2253
   input_from_file(file);
2,892✔
2254

2255
   switch (source_kind()) {
2,892✔
2256
   case SOURCE_VHDL:
2,873✔
2257
      {
2258
         lib_t work = lib_work();
2,873✔
2259
         int base_errors = 0;
2,873✔
2260
         tree_t unit;
2,873✔
2261
         while (base_errors = error_count(), (unit = parse())) {
11,025✔
2262
            if (error_count() == base_errors) {
8,152✔
2263
               lib_put(work, unit);
8,151✔
2264
               unit_registry_purge(ur, tree_ident(unit));
8,151✔
2265

2266
               simplify_local(unit, jit, ur);
8,151✔
2267
               bounds_check(unit);
8,151✔
2268
            }
2269
            else
2270
               lib_put_error(work, unit);
1✔
2271
         }
2272
      }
2273
      break;
2274

2275
   case SOURCE_VERILOG:
19✔
2276
#ifdef ENABLE_VERILOG
2277
      {
2278
         LOCAL_TEXT_BUF tb = tb_new();
38✔
2279
         vlog_preprocess(tb);
19✔
2280

2281
         input_from_buffer(tb_get(tb), tb_len(tb), SOURCE_VERILOG);
19✔
2282

2283
         lib_t work = lib_work();
19✔
2284
         vlog_node_t module;
19✔
2285
         while ((module = vlog_parse())) {
57✔
2286
            if (error_count() == 0) {
19✔
2287
               vlog_check(module);
19✔
2288

2289
               if (error_count() == 0)
19✔
2290
                  lib_put_vlog(work, module);
19✔
2291
            }
2292
         }
2293
      }
2294
#else
2295
      fatal("Verilog is not currently supported");
2296
#endif
2297
      break;
19✔
2298
   }
2299
}
2,889✔
2300

2301
static void tree_copy_cb(tree_t t, void *__ctx)
277,928✔
2302
{
2303
   copy_ctx_t *ctx = __ctx;
277,928✔
2304

2305
   if (is_subprogram(t))
277,928✔
2306
      list_add(&ctx->copied_subs, t);
11,796✔
2307
}
277,928✔
2308

2309
static void type_copy_cb(type_t type, void *__ctx)
16,599✔
2310
{
2311
   copy_ctx_t *ctx = __ctx;
16,599✔
2312

2313
   if (type_has_ident(type))
16,599✔
2314
      list_add(&ctx->copied_types, type);
10,599✔
2315
}
16,599✔
2316

2317
void copy_with_renaming(tree_t *roots, int nroots, tree_copy_pred_t tree_pred,
6,601✔
2318
                        type_copy_pred_t type_pred, void *context,
2319
                        ident_t dotted, ident_t *prefixes, int nprefix)
2320
{
2321
   copy_ctx_t copy_ctx = {};
6,601✔
2322

2323
   tree_copy(roots, nroots, tree_pred, type_pred, context,
6,601✔
2324
             tree_copy_cb, type_copy_cb, &copy_ctx);
2325

2326
   // Change the name of any copied types to reflect the new hiearchy
2327
   for (list_iter(type_t, type, copy_ctx.copied_types)) {
17,200✔
2328
      ident_t orig = type_ident(type);
10,599✔
2329
      for (int j = 0; j < nprefix; j++) {
31,999✔
2330
         if (ident_starts_with(orig, prefixes[j])) {
11,461✔
2331
            LOCAL_TEXT_BUF tb = tb_new();
660✔
2332
            tb_cat(tb, istr(dotted));
660✔
2333
            tb_cat(tb, istr(orig) + ident_len(prefixes[j]));
660✔
2334

2335
            type_set_ident(type, ident_new(tb_get(tb)));
660✔
2336
            break;
660✔
2337
         }
2338
      }
2339
   }
2340
   list_free(&copy_ctx.copied_types);
6,601✔
2341

2342
   // Change the mangled name of copied subprograms so that copies in
2343
   // different instances do not collide
2344
   for (list_iter(tree_t, decl, copy_ctx.copied_subs)) {
18,397✔
2345
      if (tree_kind(decl) == T_GENERIC_DECL)
11,796✔
2346
         continue;   // Does not yet have mangled name
398✔
2347

2348
      ident_t orig = tree_ident2(decl);
11,398✔
2349
      for (int j = 0; j < nprefix; j++) {
12,717✔
2350
         if (ident_starts_with(orig, prefixes[j])) {
11,472✔
2351
            ident_t prefix = ident_runtil(orig, '(');
10,153✔
2352

2353
            LOCAL_TEXT_BUF tb = tb_new();
10,153✔
2354
            tb_cat(tb, istr(dotted));
10,153✔
2355
            tb_cat(tb, istr(prefix) + ident_len(prefixes[j]));
10,153✔
2356

2357
            const tree_kind_t kind = tree_kind(decl);
10,153✔
2358
            const bool is_func = kind == T_FUNC_BODY || kind == T_FUNC_DECL;
10,153✔
2359
            const int nports = tree_ports(decl);
10,153✔
2360
            if (nports > 0 || is_func)
10,153✔
2361
               tb_append(tb, '(');
9,975✔
2362

2363
            for (int k = 0; k < nports; k++) {
29,931✔
2364
               tree_t p = tree_port(decl, k);
19,778✔
2365
               if (tree_class(p) == C_SIGNAL)
19,778✔
2366
                  tb_printf(tb, "s");
190✔
2367
               mangle_one_type(tb, tree_type(p));
19,778✔
2368
            }
2369

2370
            if (nports > 0 || is_func)
10,153✔
2371
               tb_printf(tb, ")");
9,975✔
2372

2373
            if (is_func)
10,153✔
2374
               mangle_one_type(tb, type_result(tree_type(decl)));
8,759✔
2375

2376
            if (tree_flags(decl) & TREE_F_PREDEFINED)
10,153✔
2377
               tb_cat(tb, "$predef");
1,832✔
2378

2379
            ident_t mangled = ident_new(tb_get(tb));
10,153✔
2380
            tree_set_ident2(decl, mangled);
10,153✔
2381

2382
            break;
10,153✔
2383
         }
2384
      }
2385
   }
2386
   list_free(&copy_ctx.copied_subs);
6,601✔
2387
}
6,601✔
2388

2389
bool all_character_literals(type_t type)
1,695✔
2390
{
2391
   assert(type_is_enum(type));
1,695✔
2392

2393
   type_t base = type_base_recur(type);
1,695✔
2394
   const int nlits = type_enum_literals(base);
1,695✔
2395
   for (int i = 0; i < nlits; i++) {
6,733✔
2396
      if (ident_char(tree_ident(type_enum_literal(base, i)), 0) != '\'')
5,799✔
2397
         return false;
2398
   }
2399

2400
   return true;
2401
}
2402

2403
bool is_operator_symbol(ident_t ident)
11,017✔
2404
{
2405
   const int len = ident_len(ident);
11,017✔
2406
   if (len < 3)
11,017✔
2407
      return false;
2408
   else if (ident_char(ident, 0) != '"')
11,017✔
2409
      return false;
2410
   else if (ident_char(ident, len - 1) != '"')
10,728✔
2411
      return false;
2412
   else if (ident_char(ident, 1) == '?' && standard() < STD_08)
10,728✔
2413
      return false;
2414

2415
   static const char *const strings[] = {
10,728✔
2416
      "\"??\"", "\"and\"", "\"or\"", "\"nand\"", "\"nor\"",
2417
      "\"xor\"", "\"xnor\"", "\"=\"", "\"/=\"", "\"<\"", "\"<=\"",
2418
      "\">\"", "\">=\"", "\"?=\"", "\"?/=\"", "\"?<\"", "\"?<=\"",
2419
      "\"?>\"", "\"?>=\"", "\"sll\"", "\"srl\"", "\"sla\"", "\"sra\"",
2420
      "\"rol\"", "\"ror\"", "\"+\"", "\"-\"", "\"&\"", "\"*\"",
2421
      "\"/\"", "\"mod\"", "\"rem\"", "\"**\"", "\"abs\"", "\"not\""
2422
   };
2423

2424
   static ident_t operators[ARRAY_LEN(strings)];
10,728✔
2425
   INIT_ONCE({
18,148✔
2426
         for (int i = 0; i < ARRAY_LEN(strings); i++)
2427
            operators[i] = ident_new(strings[i]);
2428
      });
2429

2430
   for (int i = 0; i < ARRAY_LEN(operators); i++) {
166,702✔
2431
      if (ident == operators[i])
166,701✔
2432
         return true;
2433
   }
2434

2435
   return false;
2436
}
2437

2438
bool same_tree(tree_t a, tree_t b)
2,689✔
2439
{
2440
   const tree_kind_t akind = tree_kind(a);
2,689✔
2441
   if (akind != tree_kind(b))
2,689✔
2442
      return false;
2443

2444
   switch (akind) {
2,635✔
2445
   case T_REF:
1,389✔
2446
      return tree_ref(a) == tree_ref(b);
1,389✔
2447
   case T_ARRAY_REF:
228✔
2448
      {
2449
         if (!same_tree(tree_value(a), tree_value(b)))
228✔
2450
            return false;
2451

2452
         const int nparams = tree_params(a);
216✔
2453
         assert(nparams == tree_params(b));
216✔
2454

2455
         for (int i = 0; i < nparams; i++) {
383✔
2456
            tree_t pa = tree_value(tree_param(a, i));
219✔
2457
            tree_t pb = tree_value(tree_param(b, i));
219✔
2458
            if (!same_tree(pa, pb))
219✔
2459
               return false;
2460
         }
2461

2462
         return true;
2463
      }
2464
   case T_ARRAY_SLICE:
32✔
2465
      {
2466
         if (!same_tree(tree_value(a), tree_value(b)))
32✔
2467
            return false;
2468

2469
         tree_t ra = tree_range(a, 0);
32✔
2470
         tree_t rb = tree_range(b, 0);
32✔
2471

2472
         const range_kind_t rakind = tree_subkind(ra);
32✔
2473
         if (rakind != tree_subkind(rb) || rakind == RANGE_EXPR)
32✔
2474
            return false;
2475

2476
         return same_tree(tree_left(ra), tree_left(rb))
32✔
2477
            && same_tree(tree_right(ra), tree_right(rb));
32✔
2478
      }
2479

2480
   case T_RECORD_REF:
729✔
2481
      return tree_ident(a) == tree_ident(b)
729✔
2482
         && same_tree(tree_value(a), tree_value(b));
729✔
2483

2484
   case T_LITERAL:
257✔
2485
      {
2486
         const literal_kind_t alkind = tree_subkind(a);
257✔
2487
         if (alkind != tree_subkind(b) || alkind != L_INT)
257✔
2488
            return false;
2489
         else
2490
            return tree_ival(a) == tree_ival(b);
257✔
2491
      }
2492
   default:
2493
      return false;
2494
   }
2495
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc