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

nickg / nvc / 6599333226

21 Oct 2023 07:46PM UTC coverage: 90.886% (-0.004%) from 90.89%
6599333226

push

github

nickg
Create implicit aliases for predefined operators of a type

120 of 120 new or added lines in 5 files covered. (100.0%)

49594 of 54567 relevant lines covered (90.89%)

583373.64 hits per line

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

92.85
/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,444✔
49
{
50
   int64_t value;
130,444✔
51
   if (folded_int(t, &value))
130,444✔
52
      return value;
130,444✔
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,927✔
59
{
60
   assert(tree_kind(r) == T_RANGE);
42,927✔
61

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

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

69
bool folded_int(tree_t t, int64_t *l)
2,412,020✔
70
{
71
   switch (tree_kind(t)) {
2,445,090✔
72
   case T_LITERAL:
1,864,330✔
73
      switch (tree_subkind(t)) {
1,864,330✔
74
      case L_PHYSICAL:
12,922✔
75
         if (tree_has_ref(t))
12,922✔
76
            return false;
77
         // Fall-through
78
      case L_INT:
79
         *l = tree_ival(t);
1,846,950✔
80
         return true;
1,846,950✔
81
      default:
82
         return false;
83
      }
84
   case T_QUALIFIED:
289✔
85
      return folded_int(tree_value(t), l);
289✔
86
   case T_REF:
395,660✔
87
      {
88
         tree_t decl = tree_ref(t);
395,660✔
89
         switch (tree_kind(decl)) {
395,660✔
90
         case T_CONST_DECL:
40,315✔
91
            if (tree_has_value(decl))
40,315✔
92
               return folded_int(tree_value(decl), l);
32,619✔
93
            else
94
               return false;
95
         case T_ENUM_LIT:
291,638✔
96
            *l = tree_pos(decl);
291,638✔
97
            return true;
291,638✔
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)
130,867✔
110
{
111
   switch (tree_kind(t)) {
130,888✔
112
   case T_LITERAL:
123,773✔
113
      if (tree_subkind(t) == L_REAL) {
123,773✔
114
         *l = tree_dval(t);
123,771✔
115
         return true;
123,771✔
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,156✔
127
{
128
   int64_t low, high;
40,156✔
129
   if (folded_bounds(r, &low, &high)) {
40,156✔
130
      *l = MAX(high - low + 1, 0);
37,839✔
131
      return true;
37,839✔
132
   }
133
   else
134
      return false;
135
}
136

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

141
   const range_kind_t rkind = tree_subkind(r);
1,046,250✔
142

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

146
   int64_t left, right;
1,035,360✔
147
   if (!folded_int(tree_left(r), &left))
1,035,360✔
148
      return false;
149
   else if (!folded_int(tree_right(r), &right))
950,068✔
150
      return false;
151

152
   switch (rkind) {
932,627✔
153
   case RANGE_TO:
840,965✔
154
      *low  = left;
840,965✔
155
      *high = right;
840,965✔
156
      return true;
840,965✔
157
   case RANGE_DOWNTO:
91,662✔
158
      *low  = right;
91,662✔
159
      *high = left;
91,662✔
160
      return true;
91,662✔
161
   default:
162
      return false;
163
   }
164
}
165

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

170
   const range_kind_t rkind = tree_subkind(r);
60,466✔
171

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

175
   double left, right;
60,463✔
176
   if (folded_real(tree_left(r), &left) && folded_real(tree_right(r), &right)) {
60,463✔
177
      switch (rkind) {
60,452✔
178
      case RANGE_TO:
60,452✔
179
         *low  = left;
60,452✔
180
         *high = right;
60,452✔
181
         return true;
60,452✔
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,120✔
195
{
196
   if (tree_kind(t) == T_REF) {
36,120✔
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,202✔
209
{
210
   type_t enum_type = type_base_recur(type ?: tree_type(t));
7,202✔
211
   tree_t lit = type_enum_literal(enum_type, pos);
7,202✔
212

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

219
   return b;
7,202✔
220
}
221

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

230
   return f;
32,805✔
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)
14,931✔
408
{
409
   tree_t t = tree_new(T_REF);
14,931✔
410
   tree_set_ident(t, tree_ident(to));
14,931✔
411
   tree_set_ref(t, to);
14,931✔
412
   tree_set_type(t, tree_type(to));
14,931✔
413
   return t;
14,931✔
414
}
415

416
vhdl_standard_t standard(void)
6,615,860✔
417
{
418
   return current_std;
6,615,860✔
419
}
420

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

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

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

439
   if ((unsigned)s < ARRAY_LEN(text))
3,604✔
440
      return text[s];
3,604✔
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,465✔
455
{
456
   ident_t fname = tree_ident(rref);
8,465✔
457
   type_t value_type = tree_type(tree_value(rref));
8,465✔
458

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

462
   const int nfields = type_fields(value_type);
8,465✔
463
   for (int i = 0; i < nfields; i++) {
27,878✔
464
      tree_t field = type_field(value_type, i);
27,873✔
465
      if (tree_ident(field) == fname)
27,873✔
466
         return field;
8,460✔
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,458,170✔
518
{
519
   switch (tree_kind(t)) {
1,503,720✔
520
   case T_VAR_DECL:
521
   case T_PROT_REF:
522
      return C_VARIABLE;
523
   case T_SIGNAL_DECL:
50,170✔
524
   case T_IMPLICIT_SIGNAL:
525
      return C_SIGNAL;
50,170✔
526
   case T_CONST_DECL:
44,570✔
527
      return C_CONSTANT;
44,570✔
528
   case T_PORT_DECL:
176,835✔
529
   case T_GENERIC_DECL:
530
   case T_PARAM_DECL:
531
   case T_EXTERNAL_NAME:
532
      return tree_class(t);
176,835✔
533
   case T_ENUM_LIT:
915,928✔
534
   case T_LITERAL:
535
   case T_STRING:
536
      return C_LITERAL;
915,928✔
537
   case T_FIELD_DECL:
1,640✔
538
   case T_ATTR_DECL:
539
      return C_DEFAULT;
1,640✔
540
   case T_VIEW_DECL:
131✔
541
      return C_VIEW;
131✔
542
   case T_UNIT_DECL:
6,061✔
543
      return C_UNITS;
6,061✔
544
   case T_ARCH:
40✔
545
      return C_ARCHITECTURE;
40✔
546
   case T_FUNC_DECL:
2,243✔
547
   case T_FUNC_BODY:
548
   case T_FUNC_INST:
549
   case T_FCALL:
550
      return C_FUNCTION;
2,243✔
551
   case T_PROC_DECL:
353✔
552
   case T_PROC_BODY:
553
   case T_PROC_INST:
554
   case T_PCALL:
555
      return C_PROCEDURE;
353✔
556
   case T_ENTITY:
132✔
557
      return C_ENTITY;
132✔
558
   case T_SUBTYPE_DECL:
27,032✔
559
      return C_SUBTYPE;
27,032✔
560
   case T_TYPE_DECL:
130,385✔
561
   case T_PROT_DECL:
562
      return C_TYPE;
130,385✔
563
   case T_FILE_DECL:
1,856✔
564
      return C_FILE;
1,856✔
565
   case T_PROCESS:
73✔
566
   case T_BLOCK:
567
   case T_FOR:
568
   case T_INSTANCE:
569
   case T_CONCURRENT:
570
   case T_ELAB:
571
      return C_LABEL;
73✔
572
   case T_COMPONENT:
1✔
573
      return C_COMPONENT;
1✔
574
   case T_REF:
38,310✔
575
      return tree_has_ref(t) ? class_of(tree_ref(t)) : C_DEFAULT;
38,310✔
576
   case T_ARRAY_REF:
7,255✔
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));
7,255✔
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)
813,515✔
609
{
610
   switch (c) {
813,515✔
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:
812,297✔
620
      return true;
812,297✔
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,719,460✔
648
{
649
   switch (tree_kind(t)) {
1,719,460✔
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,506✔
658
      {
659
         const class_t class = tree_class(t);
1,506✔
660
         return class == C_FUNCTION || class == C_PROCEDURE;
1,506✔
661
      }
662
   default:
1,243,520✔
663
      return false;
1,243,520✔
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,580✔
674
{
675
   switch (tree_kind(t)) {
41,580✔
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,257✔
710
{
711
   switch (tree_kind(t)) {
39,257✔
712
   case T_PACKAGE:
713
   case T_PACK_BODY:
714
   case T_PACK_INST:
715
      return true;
716
   default:
3,306✔
717
      return false;
3,306✔
718
   }
719
}
720

721
bool is_design_unit(tree_t t)
19,349✔
722
{
723
   switch (tree_kind(t)) {
19,349✔
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,870✔
733
      return false;
2,870✔
734
   }
735
}
736

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

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

754
tree_t aliased_type_decl(tree_t decl)
104,618✔
755
{
756
   switch (tree_kind(decl)) {
104,861✔
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:
370✔
770
      if (tree_class(decl) == C_TYPE)
370✔
771
         return decl;
772
      else
773
         return NULL;
44✔
774
   default:
19,587✔
775
      return NULL;
19,587✔
776
   }
777
}
778

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

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

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

800
type_t array_aggregate_type(type_t array, int from_dim)
261✔
801
{
802
   if (type_is_none(array))
261✔
803
      return type_new(T_NONE);
1✔
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,490,780✔
844
{
845
   assert(low <= high);
1,490,780✔
846

847
   if (low < 0) {
1,490,780✔
848
      // Signed integers
849
      if (low >= INT8_MIN && high <= INT8_MAX)
404,946✔
850
         return 8;
851
      else if (low >= INT16_MIN && high <= INT16_MAX)
398,365✔
852
         return 16;
853
      else if (low >= INT32_MIN && high <= INT32_MAX)
398,158✔
854
         return 32;
855
      else
856
         return 64;
102,310✔
857
   }
858
   else {
859
      // Unsigned integers
860
      if (high <= 1)
1,085,830✔
861
         return 1;
862
      else if (high <= UINT8_MAX)
487,226✔
863
         return 8;
864
      else if (high <= UINT16_MAX)
66,415✔
865
         return 16;
866
      else if (high <= UINT32_MAX)
64,165✔
867
         return 32;
868
      else
869
         return 64;
13,117✔
870
   }
871
}
872

873
unsigned dimension_of(type_t type)
1,112,590✔
874
{
875
   switch (type_kind(type)) {
2,023,860✔
876
   case T_SUBTYPE:
911,271✔
877
      return dimension_of(type_base(type));
911,271✔
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,109,330✔
883
   case T_NONE:
884
   case T_ACCESS:
885
   case T_RECORD:
886
      return 0;
887
   case T_INTEGER:
3,215✔
888
   case T_REAL:
889
   case T_PHYSICAL:
890
   case T_ENUM:
891
      return type_dims(type);
3,215✔
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,246,690✔
899
{
900
   switch (type_kind(type)) {
1,276,470✔
901
   case T_SUBTYPE:
1,042,200✔
902
      if (type_constraints(type) > 0) {
1,042,200✔
903
         tree_t c0 = type_constraint(type, 0);
1,012,540✔
904
         switch (tree_subkind(c0)) {
1,012,540✔
905
         case C_OPEN:
110✔
906
            return range_of(type_base(type), dim);
110✔
907
         case C_INDEX:
1,012,430✔
908
         case C_RANGE:
909
            return tree_range(type_constraint(type, 0), dim);
1,012,430✔
910
         default:
×
911
            fatal_trace("invalid constraint in range_of");
×
912
         }
913
      }
914
      else
915
         return range_of(type_base(type), dim);
29,664✔
916
   case T_INTEGER:
234,261✔
917
   case T_REAL:
918
   case T_PHYSICAL:
919
   case T_ENUM:
920
      return type_dim(type, dim);
234,261✔
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,134✔
928
{
929
   switch (type_kind(type)) {
23,178✔
930
   case T_ENUM:
931
      return RANGE_TO;
932
   case T_NONE:
×
933
      return RANGE_ERROR;
×
934
   case T_INTEGER:
23,104✔
935
   case T_REAL:
936
   case T_PHYSICAL:
937
   case T_SUBTYPE:
938
      {
939
         tree_t r = range_of(type, dim);
23,104✔
940
         const range_kind_t rkind = tree_subkind(r);
23,104✔
941
         if (rkind == RANGE_EXPR) {
23,104✔
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)
194,981✔
968
{
969
   if (dim >= dimension_of(type))
194,981✔
970
      return NULL;
971

972
   type_t base = type_base_recur(type);
194,967✔
973
   type_kind_t base_kind = type_kind(base);
194,967✔
974
   if (base_kind == T_ARRAY)
194,967✔
975
      return type_index(base, dim);
192,071✔
976
   else if (base_kind == T_ENUM || base_kind == T_NONE)
2,896✔
977
      return type;
978
   else if (base_kind == T_GENERIC && type_subkind(base) == GTYPE_ARRAY)
2,749✔
979
      return type_index(base, dim);
9✔
980
   else if (base_kind == T_RECORD || base_kind == T_GENERIC)
2,740✔
981
      return NULL;
982
   else
983
      return tree_type(range_of(base, dim));
2,740✔
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)
230,248✔
995
{
996
   assert(id < NUM_WELL_KNOWN);
230,248✔
997
   return id_cache[id];
230,248✔
998
}
999

1000
well_known_t is_well_known(ident_t ident)
216,596✔
1001
{
1002
   well_known_t pos = 0;
216,596✔
1003
   for (; pos < NUM_WELL_KNOWN; pos++) {
6,704,010✔
1004
      if (id_cache[pos] == ident)
6,579,970✔
1005
         break;
1006
   }
1007

1008
   return pos;
216,596✔
1009
}
1010

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

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

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

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

1066
bool is_uninstantiated_package(tree_t pack)
26,631✔
1067
{
1068
   return tree_kind(pack) == T_PACKAGE
26,631✔
1069
      && tree_generics(pack) > 0
26,305✔
1070
      && tree_genmaps(pack) == 0;
27,310✔
1071
}
1072

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

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

1103
bool package_needs_body(tree_t pack)
1,453✔
1104
{
1105
   assert(tree_kind(pack) == T_PACKAGE);
1,453✔
1106

1107
   const int ndecls = tree_decls(pack);
1,453✔
1108
   for (int i = 0; i < ndecls; i++) {
106,725✔
1109
      tree_t d = tree_decl(pack, i);
105,938✔
1110

1111
      switch (tree_kind(d)) {
105,938✔
1112
      case T_FUNC_DECL:
93,761✔
1113
      case T_PROC_DECL:
1114
         if (tree_flags(d) & TREE_F_PREDEFINED)
93,761✔
1115
            continue;
92,803✔
1116
         else if (tree_subkind(d) == S_FOREIGN)
958✔
1117
            continue;
429✔
1118
         return true;
1119

1120
      case T_CONST_DECL:
660✔
1121
         if (!tree_has_value(d))
660✔
1122
            return true;
1123
         continue;
601✔
1124

1125
      case T_PROT_DECL:
1126
         return true;
1127

1128
      default:
11,439✔
1129
         continue;
11,439✔
1130
      }
1131
   }
1132

1133
   return false;
1134
}
1135

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

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

1162
   for (int i = 0; i < ndecls; i++) {
1,799,870✔
1163
      tree_t d = tree_decl(container, i);
1,799,710✔
1164
      if (!tree_has_ident(d))
1,799,710✔
1165
         continue;
×
1166
      else if (tree_ident(d) == name) {
1,799,710✔
1167
         if (tree_kind(d) == T_TYPE_DECL
20,702✔
1168
             && type_kind(tree_type(d)) == T_INCOMPLETE)
19,466✔
1169
            best = d;
1170
         else if (nth-- == 0)
20,670✔
1171
            return d;
20,495✔
1172
      }
1173
      else if (tree_kind(d) == T_TYPE_DECL) {
1,779,000✔
1174
         type_t type = tree_type(d);
139,702✔
1175
         switch (type_kind(type)) {
139,702✔
1176
         case T_ENUM:
105,302✔
1177
            {
1178
               const int nlits = type_enum_literals(type);
105,302✔
1179
               for (int j = 0; j < nlits; j++) {
7,080,540✔
1180
                  tree_t lit = type_enum_literal(type, j);
6,986,880✔
1181
                  if (tree_ident(lit) == name && nth-- == 0)
6,986,880✔
1182
                     return lit;
11,647✔
1183
               }
1184
            }
1185
            break;
1186
         default:
1187
            break;
1188
         }
1189
      }
1,639,300✔
1190
   }
1191

1192
   return best;
1193
}
1194

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

1200
   if (cache[curr] == NULL) {
19,284✔
1201
      if (hint != NULL)
3,465✔
1202
         cache[curr] = hint;
963✔
1203
      else {
1204
         lib_t std = lib_require(well_known(lib_name));
2,502✔
1205
         cache[curr] = lib_get(std, well_known(unit_name));
2,502✔
1206
         assert(cache[curr] != NULL);
2,502✔
1207
      }
1208
   }
1209

1210
   assert(hint == NULL || hint == cache[curr]);
19,284✔
1211
   return cache[curr];
19,284✔
1212
}
1213

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

1220
type_t std_type(tree_t std, std_type_t which)
2,123,570✔
1221
{
1222
   static type_t cache[STD_FILE_OPEN_STATE + 1] = {};
2,123,570✔
1223
   assert(which < ARRAY_LEN(cache));
2,123,570✔
1224

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

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

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

1253
      if (can_cache)
19,203✔
1254
         return (cache[which] = tree_type(d));
18,453✔
1255
      else
1256
         return tree_type(d);
750✔
1257
   }
1258
   else
1259
      return cache[which];
1260
}
1261

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

1267
   if (cache[which] == NULL) {
374✔
1268
      const char *names[] = {
49✔
1269
         "STD_ULOGIC",
1270
         "STD_LOGIC",
1271
      };
1272

1273
      static tree_t ieee_cache[STD_19 + 1] = {};
49✔
1274
      tree_t unit = cached_unit(NULL, ieee_cache, W_IEEE, W_IEEE_1164);
49✔
1275

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

1280
      // STD.STANDARD cannot depend on IEEE
1281
      assert(!opt_get_int(OPT_BOOTSTRAP));
49✔
1282

1283
      return (cache[which] = tree_type(d));
49✔
1284
   }
1285
   else
1286
      return cache[which];
1287
}
1288

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

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

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

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

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

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

1316
bool is_builtin(subprogram_kind_t kind)
12,846,200✔
1317
{
1318
   return kind != S_USER && kind != S_FOREIGN;
12,846,200✔
1319
}
1320

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

1374
tree_t std_func(ident_t mangled)
×
1375
{
1376
   tree_t std = cached_std(NULL);
×
1377

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

1385
   return NULL;
1386
}
1387

1388
tree_t name_to_ref(tree_t name)
78,859✔
1389
{
1390
   tree_kind_t kind;
78,859✔
1391
   while ((kind = tree_kind(name)) != T_REF) {
89,153✔
1392
      switch (kind) {
11,633✔
1393
      case T_ARRAY_REF:
10,294✔
1394
      case T_ARRAY_SLICE:
1395
      case T_RECORD_REF:
1396
      case T_ALL:
1397
         name = tree_value(name);
10,294✔
1398
         break;
10,294✔
1399
      default:
1400
         return NULL;
1401
      }
1402
   }
1403

1404
   return name;
1405
}
1406

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

1416
void mangle_one_type(text_buf_t *buf, type_t type)
155,917✔
1417
{
1418
   ident_t ident = type_ident(type);
155,917✔
1419

1420
   char code = 0;
155,917✔
1421
   switch (is_well_known(ident)) {
155,917✔
1422
   case W_STD_INTEGER:        code = 'I'; break;
1423
   case W_STD_STRING:         code = 'S'; break;
3,044✔
1424
   case W_STD_REAL:           code = 'R'; break;
3,278✔
1425
   case W_STD_BOOL:           code = 'B'; break;
20,936✔
1426
   case W_STD_CHAR:           code = 'C'; break;
651✔
1427
   case W_STD_TIME:           code = 'T'; break;
891✔
1428
   case W_STD_NATURAL:        code = 'N'; break;
3,941✔
1429
   case W_STD_POSITIVE:       code = 'P'; break;
324✔
1430
   case W_STD_BIT:            code = 'J'; break;
2,353✔
1431
   case W_STD_BIT_VECTOR:     code = 'Q'; break;
2,279✔
1432
   case W_IEEE_LOGIC:         code = 'L'; break;
244✔
1433
   case W_IEEE_ULOGIC:        code = 'U'; break;
4,722✔
1434
   case W_IEEE_LOGIC_VECTOR:  code = 'V'; break;
1,539✔
1435
   case W_IEEE_ULOGIC_VECTOR: code = 'Y'; break;
1,398✔
1436
   default: break;
1437
   }
1438

1439
   if (code)
45,600✔
1440
      tb_append(buf, code);
57,402✔
1441
   else {
1442
      tb_printf(buf, "%zu", ident_len(ident));
98,515✔
1443
      tb_istr(buf, ident);
98,515✔
1444
   }
1445
}
155,917✔
1446

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

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

1476
   case T_AGGREGATE:
14✔
1477
      {
1478
         const int nassocs = tree_assocs(value);
14✔
1479
         type_t type = tree_type(value);
14✔
1480

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

1489
            case A_POS:
23✔
1490
               if (tree_pos(a) == (unsigned)depth)
23✔
1491
                  return assume_int(tree_value(a));
13✔
1492
               break;
1493

1494
            case A_OTHERS:
×
1495
               return assume_int(tree_value(a));
×
1496
            }
1497
         }
10✔
1498

1499
         // This will produce an error during bounds checking
1500
         return ~0;
1501
      }
1502

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

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

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

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

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

1537
            if (depth < left_len || i + 1 == nparams)
156✔
1538
               return get_case_choice_char(left, depth);
104✔
1539

1540
            depth -= left_len;
52✔
1541
         }
1542
      }
1543
      // Fall-through
1544

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

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

1565
   return enc;
981✔
1566
}
1567

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

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

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

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

1631
   case T_LITERAL:
1632
   case T_STRING:
1633
      return true;
1634

1635
   case T_FCALL:
312✔
1636
      return !!(tree_flags(expr) & (TREE_F_LOCALLY_STATIC
312✔
1637
                                    | TREE_F_GLOBALLY_STATIC));
1638

1639
   case T_RECORD_REF:
101✔
1640
      return is_static(tree_value(expr));
101✔
1641

1642
   default:
×
1643
      return false;
×
1644
   }
1645
}
1646

1647
tree_t longest_static_prefix(tree_t expr)
17,226✔
1648
{
1649
   switch (tree_kind(expr)) {
17,226✔
1650
   case T_ARRAY_REF:
3,382✔
1651
      {
1652
         tree_t value = tree_value(expr);
3,382✔
1653
         tree_t prefix = longest_static_prefix(value);
3,382✔
1654

1655
         if (prefix != value)
3,382✔
1656
            return prefix;
1657

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

1664
         return expr;
1665
      }
1666

1667
   case T_ARRAY_SLICE:
552✔
1668
      {
1669
         tree_t value = tree_value(expr);
552✔
1670
         tree_t prefix = longest_static_prefix(value);
552✔
1671

1672
         if (prefix != value)
552✔
1673
            return prefix;
1674

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

1684
         return expr;
1685
      }
1686

1687
   case T_RECORD_REF:
938✔
1688
      {
1689
         tree_t value = tree_value(expr);
938✔
1690
         tree_t prefix = longest_static_prefix(value);
938✔
1691

1692
         if (prefix != value)
938✔
1693
            return prefix;
×
1694

1695
         return expr;
1696
      }
1697

1698
   default:
1699
      return expr;
1700
   }
1701
}
1702

1703
tree_t body_of(tree_t pack)
679✔
1704
{
1705
   const tree_kind_t kind = tree_kind(pack);
679✔
1706
   if (kind == T_PACK_INST)
679✔
1707
      return NULL;
1708

1709
   assert(tree_kind(pack) == T_PACKAGE);
679✔
1710

1711
   ident_t body_i = well_known(W_BODY);
679✔
1712
   ident_t body_name = ident_prefix(tree_ident(pack), body_i, '-');
679✔
1713
   return lib_get_qualified(body_name);
679✔
1714
}
1715

1716
tree_t find_generic_map(tree_t unit, int pos, tree_t g)
1,105✔
1717
{
1718
   const int ngenmaps = tree_genmaps(unit);
1,105✔
1719

1720
   if (pos < ngenmaps) {
1,105✔
1721
      tree_t m = tree_genmap(unit, pos);
1,102✔
1722
      if (tree_subkind(m) == P_POS && tree_pos(m) == pos)
1,102✔
1723
         return tree_value(m);
593✔
1724
   }
1725

1726
   for (int j = 0; j < ngenmaps; j++) {
1,345✔
1727
      tree_t m = tree_genmap(unit, j);
1,342✔
1728
      switch (tree_subkind(m)) {
1,342✔
1729
      case P_NAMED:
979✔
1730
         {
1731
            tree_t name = tree_name(m);
979✔
1732
            assert(tree_kind(name) == T_REF);
979✔
1733

1734
            if (tree_ref(name) == g)
979✔
1735
               return tree_value(m);
313✔
1736
         }
1737
         break;
1738

1739
      case P_POS:
363✔
1740
         if (tree_pos(m) == pos)
363✔
1741
            return tree_value(m);
196✔
1742
         break;
1743

1744
      default:
1745
         break;
1746
      }
1747
   }
1748

1749
   return NULL;
1750
}
1751

1752
bool relaxed_rules(void)
827,195✔
1753
{
1754
   return opt_get_int(OPT_RELAXED);
827,195✔
1755
}
1756

1757
bool is_type_attribute(attr_kind_t kind)
61,382✔
1758
{
1759
   switch (kind) {
61,382✔
1760
   case ATTR_SUBTYPE:
1761
   case ATTR_BASE:
1762
   case ATTR_ELEMENT:
1763
   case ATTR_DESIGNATED_SUBTYPE:
1764
   case ATTR_INDEX:
1765
      return true;
1766
   default:
61,055✔
1767
      return false;
61,055✔
1768
   }
1769
}
1770

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

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

1834
type_t subtype_for_string(tree_t str, type_t base)
24,023✔
1835
{
1836
   if (!type_is_unconstrained(base))
24,023✔
1837
      return base;
1838

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

1844
   type_t index_type = index_type_of(base, 0);
21,288✔
1845
   const bool is_enum = type_is_enum(index_type);
21,288✔
1846

1847
   // The direction is determined by the index type
1848
   range_kind_t dir = direction_of(index_type, 0);
21,288✔
1849

1850
   // The left bound is the left of the index type and the right bound
1851
   // is determined by the number of elements
1852

1853
   tree_t left = NULL, right = NULL;
21,288✔
1854

1855
   if (is_enum)
21,288✔
1856
      left = make_ref(type_enum_literal(type_base_recur(index_type), 0));
1✔
1857
   else
1858
      left = tree_left(range_of(index_type, 0));
21,287✔
1859

1860
   const int nchars = tree_chars(str);
21,288✔
1861
   int64_t iright;
21,288✔
1862
   if (dir == RANGE_DOWNTO)
21,288✔
1863
      iright = assume_int(left) - nchars + 1;
×
1864
   else
1865
      iright = assume_int(left) + nchars - 1;
21,288✔
1866

1867
   if (is_enum)
21,288✔
1868
      right = get_enum_lit(str, index_type, iright);
1✔
1869
   else
1870
      right = get_int_lit(str, index_type, iright);
21,287✔
1871

1872
   tree_t r = tree_new(T_RANGE);
21,288✔
1873
   tree_set_subkind(r, dir);
21,288✔
1874
   tree_set_left(r, left);
21,288✔
1875
   tree_set_right(r, right);
21,288✔
1876
   tree_set_loc(r, tree_loc(str));
21,288✔
1877
   tree_set_type(r, index_type);
21,288✔
1878

1879
   tree_t c = tree_new(T_CONSTRAINT);
21,288✔
1880
   tree_set_subkind(c, C_INDEX);
21,288✔
1881
   tree_add_range(c, r);
21,288✔
1882
   tree_set_loc(c, tree_loc(str));
21,288✔
1883

1884
   type_add_constraint(sub, c);
21,288✔
1885

1886
   return sub;
21,288✔
1887
}
1888

1889
tree_t change_ref(tree_t name, tree_t new)
1,678✔
1890
{
1891
   switch (tree_kind(name)) {
1,678✔
1892
   case T_REF:
1,138✔
1893
      return make_ref(new);
1,138✔
1894

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

1902
         const int nparams = tree_params(name);
89✔
1903
         for (int i = 0; i < nparams; i++)
178✔
1904
            tree_add_param(t, tree_param(name, i));
89✔
1905

1906
         return t;
1907
      }
1908

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

1917
         return t;
43✔
1918
      }
1919

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

1929
         return t;
351✔
1930
      }
1931

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

1941
         return t;
54✔
1942
      }
1943

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

1951
         return t;
3✔
1952
      }
1953

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

1960
static void build_wait_for_target(tree_t expr, build_wait_fn_t fn, void *ctx)
2,110✔
1961
{
1962
   switch (tree_kind(expr)) {
2,110✔
1963
   case T_ARRAY_SLICE:
60✔
1964
      build_wait(tree_range(expr, 0), fn, ctx);
60✔
1965
      break;
60✔
1966

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

1975
   default:
1976
      break;
1977
   }
1978
}
2,110✔
1979

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

1986
   switch (tree_kind(expr)) {
12,151✔
1987
   case T_REF:
3,072✔
1988
      if (class_of(tree_ref(expr)) == C_SIGNAL)
3,072✔
1989
         (*fn)(expr, ctx);
1,705✔
1990
      break;
1991

1992
   case T_EXTERNAL_NAME:
3✔
1993
      if (tree_class(expr) == C_SIGNAL)
3✔
1994
         (*fn)(expr, ctx);
3✔
1995
      break;
1996

1997
   case T_WAVEFORM:
2,467✔
1998
   case T_QUALIFIED:
1999
   case T_TYPE_CONV:
2000
   case T_ASSERT:
2001
      if (tree_has_value(expr))
2,467✔
2002
         build_wait(tree_value(expr), fn, ctx);
2,467✔
2003
      break;
2004

2005
   case T_ARRAY_REF:
583✔
2006
   case T_ARRAY_SLICE:
2007
   case T_RECORD_REF:
2008
      {
2009
         tree_t ref = name_to_ref(expr);
583✔
2010
         if (ref != NULL && class_of(ref) == C_SIGNAL
583✔
2011
             && longest_static_prefix(expr) == expr)
419✔
2012
            (*fn)(expr, ctx);
385✔
2013
         else {
2014
            build_wait(tree_value(expr), fn, ctx);
198✔
2015
            build_wait_for_target(expr, fn, ctx);
198✔
2016
         }
2017
      }
2018
      break;
2019

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

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

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

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

2057
   case T_LITERAL:
2058
   case T_STRING:
2059
      break;
2060

2061
   case T_IF:
150✔
2062
      {
2063
         const int nconds = tree_conds(expr);
150✔
2064
         for (int i = 0; i < nconds; i++)
438✔
2065
            build_wait(tree_cond(expr, i), fn, ctx);
288✔
2066
      }
2067
      break;
2068

2069
   case T_COND_STMT:
288✔
2070
      {
2071
         if (tree_has_value(expr))
288✔
2072
            build_wait(tree_value(expr), fn, ctx);
172✔
2073

2074
         const int nstmts = tree_stmts(expr);
288✔
2075
         for (int i = 0; i < nstmts; i++)
576✔
2076
            build_wait(tree_stmt(expr, i), fn, ctx);
288✔
2077
      }
2078
      break;
2079

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

2091
         const int nstmts = tree_stmts(expr);
18✔
2092
         for (int i = 0; i < nstmts; i++)
40✔
2093
            build_wait(tree_stmt(expr, i), fn, ctx);
22✔
2094
      }
2095
      break;
2096

2097
   case T_SIGNAL_ASSIGN:
1,905✔
2098
      {
2099
         build_wait_for_target(tree_target(expr), fn, ctx);
1,905✔
2100

2101
         const int nwaves = tree_waveforms(expr);
1,905✔
2102
         for (int i = 0; i < nwaves; i++)
3,878✔
2103
            build_wait(tree_waveform(expr, i), fn, ctx);
1,973✔
2104
      }
2105
      break;
2106

2107
   case T_VAR_ASSIGN:
7✔
2108
      build_wait_for_target(tree_target(expr), fn, ctx);
7✔
2109
      build_wait(tree_value(expr), fn, ctx);
7✔
2110
      break;
7✔
2111

2112
   case T_CASE:
33✔
2113
   case T_MATCH_CASE:
2114
      {
2115
         build_wait(tree_value(expr), fn, ctx);
33✔
2116

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

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

2128
   case T_FOR:
1✔
2129
      {
2130
         build_wait(tree_range(expr, 0), fn, ctx);
1✔
2131

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

2138
   case T_WHILE:
4✔
2139
      {
2140
         build_wait(tree_value(expr), fn, ctx);
4✔
2141

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

2148
   case T_NEXT:
9✔
2149
   case T_EXIT:
2150
      if (tree_has_value(expr))
9✔
2151
         build_wait(tree_value(expr), fn, ctx);
6✔
2152
      break;
2153

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

2163
   default:
×
2164
      fatal_trace("Cannot handle tree kind %s in wait expression",
×
2165
                  tree_kind_str(tree_kind(expr)));
2166
   }
2167
}
9,610✔
2168

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

2226
   if (highlighting)
1,369✔
2227
      tb_cat(tb, "$$");
20✔
2228

2229
   va_list ap;
1,369✔
2230
   va_start(ap, fmt);
1,369✔
2231

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

2239
   va_end(ap);
1,369✔
2240
}
1,369✔
2241

2242
void capture_syntax(text_buf_t *tb)
7✔
2243
{
2244
   assert(tb == NULL || syntax_buf == NULL);
7✔
2245
   syntax_buf = tb;
7✔
2246
}
7✔
2247

2248
void analyse_file(const char *file, jit_t *jit, unit_registry_t *ur)
2,874✔
2249
{
2250
   input_from_file(file);
2,874✔
2251

2252
   switch (source_kind()) {
2,874✔
2253
   case SOURCE_VHDL:
2,858✔
2254
      {
2255
         lib_t work = lib_work();
2,858✔
2256
         int base_errors = 0;
2,858✔
2257
         tree_t unit;
2,858✔
2258
         while (base_errors = error_count(), (unit = parse())) {
10,968✔
2259
            if (error_count() == base_errors) {
8,110✔
2260
               lib_put(work, unit);
8,109✔
2261
               unit_registry_purge(ur, tree_ident(unit));
8,109✔
2262

2263
               simplify_local(unit, jit, ur);
8,109✔
2264
               bounds_check(unit);
8,109✔
2265
            }
2266
            else
2267
               lib_put_error(work, unit);
1✔
2268
         }
2269
      }
2270
      break;
2271

2272
   case SOURCE_VERILOG:
16✔
2273
#ifdef ENABLE_VERILOG
2274
      {
2275
         lib_t work = lib_work();
16✔
2276
         vlog_node_t module;
16✔
2277
         while ((module = vlog_parse())) {
48✔
2278
            if (error_count() == 0) {
16✔
2279
               vlog_check(module);
16✔
2280

2281
               if (error_count() == 0)
16✔
2282
                  lib_put_vlog(work, module);
16✔
2283
            }
2284
         }
2285
      }
2286
#else
2287
      fatal("Verilog is not currently supported");
2288
#endif
2289
      break;
2290
   }
2291
}
2,871✔
2292

2293
static void tree_copy_cb(tree_t t, void *__ctx)
277,538✔
2294
{
2295
   copy_ctx_t *ctx = __ctx;
277,538✔
2296

2297
   if (is_subprogram(t))
277,538✔
2298
      list_add(&ctx->copied_subs, t);
11,723✔
2299
}
277,538✔
2300

2301
static void type_copy_cb(type_t type, void *__ctx)
16,510✔
2302
{
2303
   copy_ctx_t *ctx = __ctx;
16,510✔
2304

2305
   if (type_has_ident(type))
16,510✔
2306
      list_add(&ctx->copied_types, type);
10,520✔
2307
}
16,510✔
2308

2309
void copy_with_renaming(tree_t *roots, int nroots, tree_copy_pred_t tree_pred,
6,582✔
2310
                        type_copy_pred_t type_pred, void *context,
2311
                        ident_t dotted, ident_t *prefixes, int nprefix)
2312
{
2313
   copy_ctx_t copy_ctx = {};
6,582✔
2314

2315
   tree_copy(roots, nroots, tree_pred, type_pred, context,
6,582✔
2316
             tree_copy_cb, type_copy_cb, &copy_ctx);
2317

2318
   // Change the name of any copied types to reflect the new hiearchy
2319
   for (list_iter(type_t, type, copy_ctx.copied_types)) {
17,102✔
2320
      ident_t orig = type_ident(type);
10,520✔
2321
      for (int j = 0; j < nprefix; j++) {
31,729✔
2322
         if (ident_starts_with(orig, prefixes[j])) {
11,340✔
2323
            LOCAL_TEXT_BUF tb = tb_new();
651✔
2324
            tb_cat(tb, istr(dotted));
651✔
2325
            tb_cat(tb, istr(orig) + ident_len(prefixes[j]));
651✔
2326

2327
            type_set_ident(type, ident_new(tb_get(tb)));
651✔
2328
            break;
651✔
2329
         }
2330
      }
2331
   }
2332
   list_free(&copy_ctx.copied_types);
6,582✔
2333

2334
   // Change the mangled name of copied subprograms so that copies in
2335
   // different instances do not collide
2336
   for (list_iter(tree_t, decl, copy_ctx.copied_subs)) {
18,305✔
2337
      if (tree_kind(decl) == T_GENERIC_DECL)
11,723✔
2338
         continue;   // Does not yet have mangled name
374✔
2339

2340
      ident_t orig = tree_ident2(decl);
11,349✔
2341
      for (int j = 0; j < nprefix; j++) {
12,670✔
2342
         if (ident_starts_with(orig, prefixes[j])) {
11,423✔
2343
            ident_t prefix = ident_runtil(orig, '(');
10,102✔
2344

2345
            LOCAL_TEXT_BUF tb = tb_new();
10,102✔
2346
            tb_cat(tb, istr(dotted));
10,102✔
2347
            tb_cat(tb, istr(prefix) + ident_len(prefixes[j]));
10,102✔
2348

2349
            const tree_kind_t kind = tree_kind(decl);
10,102✔
2350
            const bool is_func = kind == T_FUNC_BODY || kind == T_FUNC_DECL;
10,102✔
2351
            const int nports = tree_ports(decl);
10,102✔
2352
            if (nports > 0 || is_func)
10,102✔
2353
               tb_append(tb, '(');
9,924✔
2354

2355
            for (int k = 0; k < nports; k++) {
29,772✔
2356
               tree_t p = tree_port(decl, k);
19,670✔
2357
               if (tree_class(p) == C_SIGNAL)
19,670✔
2358
                  tb_printf(tb, "s");
187✔
2359
               mangle_one_type(tb, tree_type(p));
19,670✔
2360
            }
2361

2362
            if (nports > 0 || is_func)
10,102✔
2363
               tb_printf(tb, ")");
9,924✔
2364

2365
            if (is_func)
10,102✔
2366
               mangle_one_type(tb, type_result(tree_type(decl)));
8,711✔
2367

2368
            ident_t mangled = ident_new(tb_get(tb));
10,102✔
2369
            tree_set_ident2(decl, mangled);
10,102✔
2370

2371
            break;
10,102✔
2372
         }
2373
      }
2374
   }
2375
   list_free(&copy_ctx.copied_subs);
6,582✔
2376
}
6,582✔
2377

2378
bool all_character_literals(type_t type)
192✔
2379
{
2380
   assert(type_is_enum(type));
192✔
2381

2382
   type_t base = type_base_recur(type);
192✔
2383
   const int nlits = type_enum_literals(base);
192✔
2384
   for (int i = 0; i < nlits; i++) {
1,017✔
2385
      if (ident_char(tree_ident(type_enum_literal(base, i)), 0) != '\'')
886✔
2386
         return false;
2387
   }
2388

2389
   return true;
2390
}
2391

2392
bool is_operator_symbol(ident_t ident)
9,010✔
2393
{
2394
   const int len = ident_len(ident);
9,010✔
2395
   if (len < 3)
9,010✔
2396
      return false;
2397
   else if (ident_char(ident, 0) != '"')
9,010✔
2398
      return false;
2399
   else if (ident_char(ident, len - 1) != '"')
8,710✔
2400
      return false;
2401
   else if (ident_char(ident, 1) == '?' && standard() < STD_08)
8,710✔
2402
      return false;
2403

2404
   static const char *const strings[] = {
8,710✔
2405
      "\"??\"", "\"and\"", "\"or\"", "\"nand\"", "\"nor\"",
2406
      "\"xor\"", "\"xnor\"", "\"=\"", "\"/=\"", "\"<\"", "\"<=\"",
2407
      "\">\"", "\">=\"", "\"?=\"", "\"?/=\"", "\"?<\"", "\"?<=\"",
2408
      "\"?>\"", "\"?>=\"", "\"sll\"", "\"srl\"", "\"sla\"", "\"sra\"",
2409
      "\"rol\"", "\"ror\"", "\"+\"", "\"-\"", "\"&\"", "\"*\"",
2410
      "\"/\"", "\"mod\"", "\"rem\"", "\"**\"", "\"abs\"", "\"not\""
2411
   };
2412

2413
   static ident_t operators[ARRAY_LEN(strings)];
8,710✔
2414
   INIT_ONCE({
14,730✔
2415
         for (int i = 0; i < ARRAY_LEN(strings); i++)
2416
            operators[i] = ident_new(strings[i]);
2417
      });
2418

2419
   for (int i = 0; i < ARRAY_LEN(operators); i++) {
148,412✔
2420
      if (ident == operators[i])
148,411✔
2421
         return true;
2422
   }
2423

2424
   return false;
2425
}
2426

2427
bool same_tree(tree_t a, tree_t b)
2,689✔
2428
{
2429
   const tree_kind_t akind = tree_kind(a);
2,689✔
2430
   if (akind != tree_kind(b))
2,689✔
2431
      return false;
2432

2433
   switch (akind) {
2,635✔
2434
   case T_REF:
1,389✔
2435
      return tree_ref(a) == tree_ref(b);
1,389✔
2436
   case T_ARRAY_REF:
228✔
2437
      {
2438
         if (!same_tree(tree_value(a), tree_value(b)))
228✔
2439
            return false;
2440

2441
         const int nparams = tree_params(a);
216✔
2442
         assert(nparams == tree_params(b));
216✔
2443

2444
         for (int i = 0; i < nparams; i++) {
383✔
2445
            tree_t pa = tree_value(tree_param(a, i));
219✔
2446
            tree_t pb = tree_value(tree_param(b, i));
219✔
2447
            if (!same_tree(pa, pb))
219✔
2448
               return false;
2449
         }
2450

2451
         return true;
2452
      }
2453
   case T_ARRAY_SLICE:
32✔
2454
      {
2455
         if (!same_tree(tree_value(a), tree_value(b)))
32✔
2456
            return false;
2457

2458
         tree_t ra = tree_range(a, 0);
32✔
2459
         tree_t rb = tree_range(b, 0);
32✔
2460

2461
         const range_kind_t rakind = tree_subkind(ra);
32✔
2462
         if (rakind != tree_subkind(rb) || rakind == RANGE_EXPR)
32✔
2463
            return false;
2464

2465
         return same_tree(tree_left(ra), tree_left(rb))
32✔
2466
            && same_tree(tree_right(ra), tree_right(rb));
32✔
2467
      }
2468

2469
   case T_RECORD_REF:
729✔
2470
      return tree_ident(a) == tree_ident(b)
729✔
2471
         && same_tree(tree_value(a), tree_value(b));
729✔
2472

2473
   case T_LITERAL:
257✔
2474
      {
2475
         const literal_kind_t alkind = tree_subkind(a);
257✔
2476
         if (alkind != tree_subkind(b) || alkind != L_INT)
257✔
2477
            return false;
2478
         else
2479
            return tree_ival(a) == tree_ival(b);
257✔
2480
      }
2481
   default:
2482
      return false;
2483
   }
2484
}
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