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

nickg / nvc / 6604271862

22 Oct 2023 02:03PM UTC coverage: 91.212% (-0.006%) from 91.218%
6604271862

push

github

nickg
Create implicit aliases for predefined operators of a type

Fixes #776

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

49583 of 54360 relevant lines covered (91.21%)

588557.74 hits per line

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

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

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

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

69
bool folded_int(tree_t t, int64_t *l)
2,415,440✔
70
{
71
   switch (tree_kind(t)) {
2,448,510✔
72
   case T_LITERAL:
1,865,470✔
73
      switch (tree_subkind(t)) {
1,865,470✔
74
      case L_PHYSICAL:
13,018✔
75
         if (tree_has_ref(t))
13,018✔
76
            return false;
77
         // Fall-through
78
      case L_INT:
79
         *l = tree_ival(t);
1,848,090✔
80
         return true;
1,848,090✔
81
      default:
82
         return false;
83
      }
84
   case T_QUALIFIED:
289✔
85
      return folded_int(tree_value(t), l);
289✔
86
   case T_REF:
397,949✔
87
      {
88
         tree_t decl = tree_ref(t);
397,949✔
89
         switch (tree_kind(decl)) {
397,949✔
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:
293,918✔
96
            *l = tree_pos(decl);
293,918✔
97
            return true;
293,918✔
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,891✔
110
{
111
   switch (tree_kind(t)) {
130,912✔
112
   case T_LITERAL:
123,797✔
113
      if (tree_subkind(t) == L_REAL) {
123,797✔
114
         *l = tree_dval(t);
123,795✔
115
         return true;
123,795✔
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,159✔
127
{
128
   int64_t low, high;
40,159✔
129
   if (folded_bounds(r, &low, &high)) {
40,159✔
130
      *l = MAX(high - low + 1, 0);
37,842✔
131
      return true;
37,842✔
132
   }
133
   else
134
      return false;
135
}
136

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

141
   const range_kind_t rkind = tree_subkind(r);
1,047,870✔
142

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

146
   int64_t left, right;
1,036,980✔
147
   if (!folded_int(tree_left(r), &left))
1,036,980✔
148
      return false;
149
   else if (!folded_int(tree_right(r), &right))
951,693✔
150
      return false;
151

152
   switch (rkind) {
934,252✔
153
   case RANGE_TO:
842,590✔
154
      *low  = left;
842,590✔
155
      *high = right;
842,590✔
156
      return true;
842,590✔
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,478✔
167
{
168
   assert(tree_kind(r) == T_RANGE);
60,478✔
169

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

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

175
   double left, right;
60,475✔
176
   if (folded_real(tree_left(r), &left) && folded_real(tree_right(r), &right)) {
60,475✔
177
      switch (rkind) {
60,464✔
178
      case RANGE_TO:
60,464✔
179
         *low  = left;
60,464✔
180
         *high = right;
60,464✔
181
         return true;
60,464✔
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,126✔
195
{
196
   if (tree_kind(t) == T_REF) {
36,126✔
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,809✔
223
{
224
   tree_t f = tree_new(T_LITERAL);
32,809✔
225
   tree_set_subkind(f, L_INT);
32,809✔
226
   tree_set_ival(f, i);
32,809✔
227
   tree_set_loc(f, tree_loc(t));
32,809✔
228
   tree_set_type(f, type ?: tree_type(t));
32,809✔
229

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

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

421
void set_standard(vhdl_standard_t s)
3,955✔
422
{
423
   current_std = s;
3,955✔
424
   have_set_std = true;
3,955✔
425
}
3,955✔
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,613✔
434
{
435
   static const char *text[] = {
3,613✔
436
      "1987", "1993", "2000", "2002", "2008", "2019"
437
   };
438

439
   if ((unsigned)s < ARRAY_LEN(text))
3,613✔
440
      return text[s];
3,613✔
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,493,120✔
518
{
519
   switch (tree_kind(t)) {
1,575,100✔
520
   case T_VAR_DECL:
521
      return C_VARIABLE;
522
   case T_SIGNAL_DECL:
50,266✔
523
   case T_IMPLICIT_SIGNAL:
524
      return C_SIGNAL;
50,266✔
525
   case T_CONST_DECL:
44,570✔
526
      return C_CONSTANT;
44,570✔
527
   case T_PORT_DECL:
176,940✔
528
   case T_GENERIC_DECL:
529
   case T_PARAM_DECL:
530
   case T_EXTERNAL_NAME:
531
      return tree_class(t);
176,940✔
532
   case T_ENUM_LIT:
917,005✔
533
   case T_LITERAL:
534
   case T_STRING:
535
      return C_LITERAL;
917,005✔
536
   case T_FIELD_DECL:
1,640✔
537
   case T_ATTR_DECL:
538
      return C_DEFAULT;
1,640✔
539
   case T_VIEW_DECL:
131✔
540
      return C_VIEW;
131✔
541
   case T_UNIT_DECL:
6,109✔
542
      return C_UNITS;
6,109✔
543
   case T_ARCH:
40✔
544
      return C_ARCHITECTURE;
40✔
545
   case T_FUNC_DECL:
25,205✔
546
   case T_FUNC_BODY:
547
   case T_FUNC_INST:
548
   case T_FCALL:
549
      return C_FUNCTION;
25,205✔
550
   case T_PROC_DECL:
10,940✔
551
   case T_PROC_BODY:
552
   case T_PROC_INST:
553
   case T_PCALL:
554
      return C_PROCEDURE;
10,940✔
555
   case T_ENTITY:
132✔
556
      return C_ENTITY;
132✔
557
   case T_SUBTYPE_DECL:
27,044✔
558
      return C_SUBTYPE;
27,044✔
559
   case T_TYPE_DECL:
130,469✔
560
   case T_PROT_DECL:
561
      return C_TYPE;
130,469✔
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:
73,713✔
574
   case T_PROT_REF:
575
      return tree_has_ref(t) ? class_of(tree_ref(t)) : C_DEFAULT;
73,713✔
576
   case T_ARRAY_REF:
8,283✔
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,283✔
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,758✔
609
{
610
   switch (c) {
813,758✔
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,540✔
620
      return true;
812,540✔
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,722,460✔
648
{
649
   switch (tree_kind(t)) {
1,722,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,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,244,330✔
663
      return false;
1,244,330✔
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,667✔
674
{
675
   switch (tree_kind(t)) {
41,667✔
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,364✔
710
{
711
   switch (tree_kind(t)) {
39,364✔
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,439✔
722
{
723
   switch (tree_kind(t)) {
19,439✔
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,652✔
738
{
739
   return !!(tree_flags(decl) & (TREE_F_BUS | TREE_F_REGISTER));
4,652✔
740
}
741

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

754
tree_t aliased_type_decl(tree_t decl)
104,671✔
755
{
756
   switch (tree_kind(decl)) {
104,914✔
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,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,524✔
780
{
781
   tree_t p = tree_new(T_PARAM);
142,524✔
782
   tree_set_loc(p, tree_loc(value));
142,524✔
783
   tree_set_subkind(p, kind);
142,524✔
784
   tree_set_value(p, value);
142,524✔
785

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

796
   tree_add_param(call, p);
142,524✔
797
   return p;
142,524✔
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,501,730✔
844
{
845
   assert(low <= high);
1,501,730✔
846

847
   if (low < 0) {
1,501,730✔
848
      // Signed integers
849
      if (low >= INT8_MIN && high <= INT8_MAX)
405,940✔
850
         return 8;
851
      else if (low >= INT16_MIN && high <= INT16_MAX)
399,359✔
852
         return 16;
853
      else if (low >= INT32_MIN && high <= INT32_MAX)
399,152✔
854
         return 32;
855
      else
856
         return 64;
103,152✔
857
   }
858
   else {
859
      // Unsigned integers
860
      if (high <= 1)
1,095,790✔
861
         return 1;
862
      else if (high <= UINT8_MAX)
490,861✔
863
         return 8;
864
      else if (high <= UINT16_MAX)
66,789✔
865
         return 16;
866
      else if (high <= UINT32_MAX)
64,416✔
867
         return 32;
868
      else
869
         return 64;
13,229✔
870
   }
871
}
872

873
unsigned dimension_of(type_t type)
1,113,730✔
874
{
875
   switch (type_kind(type)) {
2,025,000✔
876
   case T_SUBTYPE:
911,275✔
877
      return dimension_of(type_base(type));
911,275✔
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,110,470✔
883
   case T_NONE:
884
   case T_ACCESS:
885
   case T_RECORD:
886
      return 0;
887
   case T_INTEGER:
3,218✔
888
   case T_REAL:
889
   case T_PHYSICAL:
890
   case T_ENUM:
891
      return type_dims(type);
3,218✔
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,248,090✔
899
{
900
   switch (type_kind(type)) {
1,278,040✔
901
   case T_SUBTYPE:
1,042,600✔
902
      if (type_constraints(type) > 0) {
1,042,600✔
903
         tree_t c0 = type_constraint(type, 0);
1,012,760✔
904
         switch (tree_subkind(c0)) {
1,012,760✔
905
         case C_OPEN:
110✔
906
            return range_of(type_base(type), dim);
110✔
907
         case C_INDEX:
1,012,650✔
908
         case C_RANGE:
909
            return tree_range(type_constraint(type, 0), dim);
1,012,650✔
910
         default:
×
911
            fatal_trace("invalid constraint in range_of");
×
912
         }
913
      }
914
      else
915
         return range_of(type_base(type), dim);
29,841✔
916
   case T_INTEGER:
235,440✔
917
   case T_REAL:
918
   case T_PHYSICAL:
919
   case T_ENUM:
920
      return type_dim(type, dim);
235,440✔
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,138✔
928
{
929
   switch (type_kind(type)) {
23,182✔
930
   case T_ENUM:
931
      return RANGE_TO;
932
   case T_NONE:
×
933
      return RANGE_ERROR;
×
934
   case T_INTEGER:
23,108✔
935
   case T_REAL:
936
   case T_PHYSICAL:
937
   case T_SUBTYPE:
938
      {
939
         tree_t r = range_of(type, dim);
23,108✔
940
         const range_kind_t rkind = tree_subkind(r);
23,108✔
941
         if (rkind == RANGE_EXPR) {
23,108✔
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,188✔
968
{
969
   if (dim >= dimension_of(type))
195,188✔
970
      return NULL;
971

972
   type_t base = type_base_recur(type);
195,174✔
973
   type_kind_t base_kind = type_kind(base);
195,174✔
974
   if (base_kind == T_ARRAY)
195,174✔
975
      return type_index(base, dim);
192,275✔
976
   else if (base_kind == T_ENUM || base_kind == T_NONE)
2,899✔
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,992✔
995
{
996
   assert(id < NUM_WELL_KNOWN);
230,992✔
997
   return id_cache[id];
230,992✔
998
}
999

1000
well_known_t is_well_known(ident_t ident)
217,357✔
1001
{
1002
   well_known_t pos = 0;
217,357✔
1003
   for (; pos < NUM_WELL_KNOWN; pos++) {
6,851,560✔
1004
      if (id_cache[pos] == ident)
6,727,060✔
1005
         break;
1006
   }
1007

1008
   return pos;
217,357✔
1009
}
1010

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

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

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

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

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

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

1075
bool is_uninstantiated_subprogram(tree_t decl)
91,315✔
1076
{
1077
   switch (tree_kind(decl)) {
91,315✔
1078
   case T_FUNC_DECL:
91,126✔
1079
   case T_FUNC_BODY:
1080
   case T_PROC_DECL:
1081
   case T_PROC_BODY:
1082
      return tree_generics(decl) > 0;
91,126✔
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,459✔
1106
{
1107
   assert(tree_kind(pack) == T_PACKAGE);
1,459✔
1108

1109
   const int ndecls = tree_decls(pack);
1,459✔
1110
   for (int i = 0; i < ndecls; i++) {
107,438✔
1111
      tree_t d = tree_decl(pack, i);
106,651✔
1112

1113
      switch (tree_kind(d)) {
106,651✔
1114
      case T_FUNC_DECL:
94,476✔
1115
      case T_PROC_DECL:
1116
         if (tree_flags(d) & TREE_F_PREDEFINED)
94,476✔
1117
            continue;
93,512✔
1118
         else if (tree_subkind(d) == S_FOREIGN)
964✔
1119
            continue;
429✔
1120
         return true;
1121

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

1127
      case T_PROT_DECL:
1128
         return true;
1129

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

1135
   return false;
1136
}
1137

1138
tree_t search_decls(tree_t container, ident_t name, int nth)
32,360✔
1139
{
1140
   tree_kind_t kind = tree_kind(container);
32,360✔
1141
   if (kind == T_LIBRARY) {
32,360✔
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,360✔
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,095✔
1158
      return NULL;
1159

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

1164
   for (int i = 0; i < ndecls; i++) {
1,808,830✔
1165
      tree_t d = tree_decl(container, i);
1,808,670✔
1166
      if (!tree_has_ident(d))
1,808,670✔
1167
         continue;
×
1168
      else if (tree_ident(d) == name) {
1,808,670✔
1169
         if (tree_kind(d) == T_TYPE_DECL
20,850✔
1170
             && type_kind(tree_type(d)) == T_INCOMPLETE)
19,495✔
1171
            best = d;
1172
         else if (nth-- == 0)
20,818✔
1173
            return d;
20,543✔
1174
      }
1175
      else if (tree_kind(d) == T_TYPE_DECL) {
1,787,820✔
1176
         type_t type = tree_type(d);
140,206✔
1177
         switch (type_kind(type)) {
140,206✔
1178
         case T_ENUM:
105,543✔
1179
            {
1180
               const int nlits = type_enum_literals(type);
105,543✔
1181
               for (int j = 0; j < nlits; j++) {
7,092,840✔
1182
                  tree_t lit = type_enum_literal(type, j);
6,998,950✔
1183
                  if (tree_ident(lit) == name && nth-- == 0)
6,998,950✔
1184
                     return lit;
11,653✔
1185
               }
1186
            }
1187
            break;
1188
         default:
1189
            break;
1190
         }
1191
      }
1,647,610✔
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,313✔
1198
                          well_known_t unit_name)
1199
{
1200
   const vhdl_standard_t curr = standard();
19,313✔
1201

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

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

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

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

1227
   if (cache[which] == NULL) {
2,123,920✔
1228
      const char *names[] = {
19,232✔
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,232✔
1247
      if (d == NULL)
19,232✔
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,232✔
1253
      if (can_cache == -1) can_cache = !opt_get_int(OPT_BOOTSTRAP);
19,232✔
1254

1255
      if (can_cache)
19,232✔
1256
         return (cache[which] = tree_type(d));
18,484✔
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)
374✔
1265
{
1266
   static type_t cache[IEEE_STD_LOGIC + 1] = {};
374✔
1267
   assert(which < ARRAY_LEN(cache));
374✔
1268

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

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

1278
      tree_t d = search_decls(unit, ident_new(names[which]), 0);
49✔
1279
      if (d == NULL)
49✔
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));
49✔
1284

1285
      return (cache[which] = tree_type(d));
49✔
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,200✔
1319
{
1320
   return kind != S_USER && kind != S_FOREIGN;
12,846,200✔
1321
}
1322

1323
bool is_open_coded_builtin(subprogram_kind_t kind)
359,036✔
1324
{
1325
   switch (kind) {
359,036✔
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,202✔
1372
      return true;
219,202✔
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,940✔
1391
{
1392
   tree_kind_t kind;
78,940✔
1393
   while ((kind = tree_kind(name)) != T_REF) {
89,234✔
1394
      switch (kind) {
11,633✔
1395
      case T_ARRAY_REF:
10,294✔
1396
      case T_ARRAY_SLICE:
1397
      case T_RECORD_REF:
1398
      case T_ALL:
1399
         name = tree_value(name);
10,294✔
1400
         break;
10,294✔
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,558✔
1419
{
1420
   ident_t ident = type_ident(type);
156,558✔
1421

1422
   char code = 0;
156,558✔
1423
   switch (is_well_known(ident)) {
156,558✔
1424
   case W_STD_INTEGER:        code = 'I'; break;
1425
   case W_STD_STRING:         code = 'S'; break;
3,039✔
1426
   case W_STD_REAL:           code = 'R'; break;
3,278✔
1427
   case W_STD_BOOL:           code = 'B'; break;
21,147✔
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,941✔
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,722✔
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,804✔
1442
      tb_append(buf, code);
57,606✔
1443
   else {
1444
      tb_printf(buf, "%zu", ident_len(ident));
98,952✔
1445
      tb_istr(buf, ident);
98,952✔
1446
   }
1447
}
156,558✔
1448

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1567
   return enc;
981✔
1568
}
1569

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

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

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

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

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

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

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

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

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

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

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

1666
         return expr;
1667
      }
1668

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

1674
         if (prefix != value)
552✔
1675
            return prefix;
1676

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

1686
         return expr;
1687
      }
1688

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

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

1697
         return expr;
1698
      }
1699

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

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

1711
   assert(tree_kind(pack) == T_PACKAGE);
685✔
1712

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

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

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

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

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

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

1746
      default:
1747
         break;
1748
      }
1749
   }
1750

1751
   return NULL;
1752
}
1753

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

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

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

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

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

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

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

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

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

1855
   tree_t left = NULL, right = NULL;
21,292✔
1856

1857
   if (is_enum)
21,292✔
1858
      left = make_ref(type_enum_literal(type_base_recur(index_type), 0));
1✔
1859
   else
1860
      left = tree_left(range_of(index_type, 0));
21,291✔
1861

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

1869
   if (is_enum)
21,292✔
1870
      right = get_enum_lit(str, index_type, iright);
1✔
1871
   else
1872
      right = get_int_lit(str, index_type, iright);
21,291✔
1873

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

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

1886
   type_add_constraint(sub, c);
21,292✔
1887

1888
   return sub;
21,292✔
1889
}
1890

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

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

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

1908
         return t;
1909
      }
1910

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

1919
         return t;
43✔
1920
      }
1921

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

1931
         return t;
351✔
1932
      }
1933

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

1943
         return t;
54✔
1944
      }
1945

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

1953
         return t;
3✔
1954
      }
1955

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

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

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

1977
   default:
1978
      break;
1979
   }
1980
}
2,110✔
1981

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2254
   switch (source_kind()) {
2,883✔
2255
   case SOURCE_VHDL:
2,867✔
2256
      {
2257
         lib_t work = lib_work();
2,867✔
2258
         int base_errors = 0;
2,867✔
2259
         tree_t unit;
2,867✔
2260
         while (base_errors = error_count(), (unit = parse())) {
11,007✔
2261
            if (error_count() == base_errors) {
8,140✔
2262
               lib_put(work, unit);
8,139✔
2263
               unit_registry_purge(ur, tree_ident(unit));
8,139✔
2264

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

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

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

2295
static void tree_copy_cb(tree_t t, void *__ctx)
277,668✔
2296
{
2297
   copy_ctx_t *ctx = __ctx;
277,668✔
2298

2299
   if (is_subprogram(t))
277,668✔
2300
      list_add(&ctx->copied_subs, t);
11,754✔
2301
}
277,668✔
2302

2303
static void type_copy_cb(type_t type, void *__ctx)
16,538✔
2304
{
2305
   copy_ctx_t *ctx = __ctx;
16,538✔
2306

2307
   if (type_has_ident(type))
16,538✔
2308
      list_add(&ctx->copied_types, type);
10,548✔
2309
}
16,538✔
2310

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

2317
   tree_copy(roots, nroots, tree_pred, type_pred, context,
6,594✔
2318
             tree_copy_cb, type_copy_cb, &copy_ctx);
2319

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

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

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

2342
      ident_t orig = tree_ident2(decl);
11,356✔
2343
      for (int j = 0; j < nprefix; j++) {
12,675✔
2344
         if (ident_starts_with(orig, prefixes[j])) {
11,430✔
2345
            ident_t prefix = ident_runtil(orig, '(');
10,111✔
2346

2347
            LOCAL_TEXT_BUF tb = tb_new();
10,111✔
2348
            tb_cat(tb, istr(dotted));
10,111✔
2349
            tb_cat(tb, istr(prefix) + ident_len(prefixes[j]));
10,111✔
2350

2351
            const tree_kind_t kind = tree_kind(decl);
10,111✔
2352
            const bool is_func = kind == T_FUNC_BODY || kind == T_FUNC_DECL;
10,111✔
2353
            const int nports = tree_ports(decl);
10,111✔
2354
            if (nports > 0 || is_func)
10,111✔
2355
               tb_append(tb, '(');
9,933✔
2356

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

2364
            if (nports > 0 || is_func)
10,111✔
2365
               tb_printf(tb, ")");
9,933✔
2366

2367
            if (is_func)
10,111✔
2368
               mangle_one_type(tb, type_result(tree_type(decl)));
8,720✔
2369

2370
            if (tree_flags(decl) & TREE_F_PREDEFINED)
10,111✔
2371
               tb_cat(tb, "$predef");
1,796✔
2372

2373
            ident_t mangled = ident_new(tb_get(tb));
10,111✔
2374
            tree_set_ident2(decl, mangled);
10,111✔
2375

2376
            break;
10,111✔
2377
         }
2378
      }
2379
   }
2380
   list_free(&copy_ctx.copied_subs);
6,594✔
2381
}
6,594✔
2382

2383
bool all_character_literals(type_t type)
1,690✔
2384
{
2385
   assert(type_is_enum(type));
1,690✔
2386

2387
   type_t base = type_base_recur(type);
1,690✔
2388
   const int nlits = type_enum_literals(base);
1,690✔
2389
   for (int i = 0; i < nlits; i++) {
6,724✔
2390
      if (ident_char(tree_ident(type_enum_literal(base, i)), 0) != '\'')
5,792✔
2391
         return false;
2392
   }
2393

2394
   return true;
2395
}
2396

2397
bool is_operator_symbol(ident_t ident)
11,017✔
2398
{
2399
   const int len = ident_len(ident);
11,017✔
2400
   if (len < 3)
11,017✔
2401
      return false;
2402
   else if (ident_char(ident, 0) != '"')
11,017✔
2403
      return false;
2404
   else if (ident_char(ident, len - 1) != '"')
10,728✔
2405
      return false;
2406
   else if (ident_char(ident, 1) == '?' && standard() < STD_08)
10,728✔
2407
      return false;
2408

2409
   static const char *const strings[] = {
10,728✔
2410
      "\"??\"", "\"and\"", "\"or\"", "\"nand\"", "\"nor\"",
2411
      "\"xor\"", "\"xnor\"", "\"=\"", "\"/=\"", "\"<\"", "\"<=\"",
2412
      "\">\"", "\">=\"", "\"?=\"", "\"?/=\"", "\"?<\"", "\"?<=\"",
2413
      "\"?>\"", "\"?>=\"", "\"sll\"", "\"srl\"", "\"sla\"", "\"sra\"",
2414
      "\"rol\"", "\"ror\"", "\"+\"", "\"-\"", "\"&\"", "\"*\"",
2415
      "\"/\"", "\"mod\"", "\"rem\"", "\"**\"", "\"abs\"", "\"not\""
2416
   };
2417

2418
   static ident_t operators[ARRAY_LEN(strings)];
10,728✔
2419
   INIT_ONCE({
18,148✔
2420
         for (int i = 0; i < ARRAY_LEN(strings); i++)
2421
            operators[i] = ident_new(strings[i]);
2422
      });
2423

2424
   for (int i = 0; i < ARRAY_LEN(operators); i++) {
167,014✔
2425
      if (ident == operators[i])
167,013✔
2426
         return true;
2427
   }
2428

2429
   return false;
2430
}
2431

2432
bool same_tree(tree_t a, tree_t b)
2,689✔
2433
{
2434
   const tree_kind_t akind = tree_kind(a);
2,689✔
2435
   if (akind != tree_kind(b))
2,689✔
2436
      return false;
2437

2438
   switch (akind) {
2,635✔
2439
   case T_REF:
1,389✔
2440
      return tree_ref(a) == tree_ref(b);
1,389✔
2441
   case T_ARRAY_REF:
228✔
2442
      {
2443
         if (!same_tree(tree_value(a), tree_value(b)))
228✔
2444
            return false;
2445

2446
         const int nparams = tree_params(a);
216✔
2447
         assert(nparams == tree_params(b));
216✔
2448

2449
         for (int i = 0; i < nparams; i++) {
383✔
2450
            tree_t pa = tree_value(tree_param(a, i));
219✔
2451
            tree_t pb = tree_value(tree_param(b, i));
219✔
2452
            if (!same_tree(pa, pb))
219✔
2453
               return false;
2454
         }
2455

2456
         return true;
2457
      }
2458
   case T_ARRAY_SLICE:
32✔
2459
      {
2460
         if (!same_tree(tree_value(a), tree_value(b)))
32✔
2461
            return false;
2462

2463
         tree_t ra = tree_range(a, 0);
32✔
2464
         tree_t rb = tree_range(b, 0);
32✔
2465

2466
         const range_kind_t rakind = tree_subkind(ra);
32✔
2467
         if (rakind != tree_subkind(rb) || rakind == RANGE_EXPR)
32✔
2468
            return false;
2469

2470
         return same_tree(tree_left(ra), tree_left(rb))
32✔
2471
            && same_tree(tree_right(ra), tree_right(rb));
32✔
2472
      }
2473

2474
   case T_RECORD_REF:
729✔
2475
      return tree_ident(a) == tree_ident(b)
729✔
2476
         && same_tree(tree_value(a), tree_value(b));
729✔
2477

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