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

nickg / nvc / 6597772503

21 Oct 2023 01:55PM UTC coverage: 90.89%. Remained the same
6597772503

push

github

nickg
Create implicit aliases for predefined operators of a type

92 of 92 new or added lines in 3 files covered. (100.0%)

49588 of 54558 relevant lines covered (90.89%)

578480.59 hits per line

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

92.84
/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,442✔
49
{
50
   int64_t value;
130,442✔
51
   if (folded_int(t, &value))
130,442✔
52
      return value;
130,442✔
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,150✔
70
{
71
   switch (tree_kind(t)) {
2,445,060✔
72
   case T_LITERAL:
1,864,410✔
73
      switch (tree_subkind(t)) {
1,864,410✔
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,847,030✔
80
         return true;
1,847,030✔
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,620✔
87
      {
88
         tree_t decl = tree_ref(t);
395,620✔
89
         switch (tree_kind(decl)) {
395,620✔
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,633✔
96
            *l = tree_pos(decl);
291,633✔
97
            return true;
291,633✔
98
         default:
99
            return false;
100
         }
101
      }
102
   default:
103
      return false;
104
   }
105
}
106

107
bool folded_real(tree_t t, double *l)
130,867✔
108
{
109
   switch (tree_kind(t)) {
130,888✔
110
   case T_LITERAL:
123,773✔
111
      if (tree_subkind(t) == L_REAL) {
123,773✔
112
         *l = tree_dval(t);
123,771✔
113
         return true;
123,771✔
114
      }
115
      else
116
         return false;
117
   case T_QUALIFIED:
21✔
118
      return folded_real(tree_value(t), l);
21✔
119
   default:
120
      return false;
121
   }
122
}
123

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

135
bool folded_bounds(tree_t r, int64_t *low, int64_t *high)
1,046,290✔
136
{
137
   assert(tree_kind(r) == T_RANGE);
1,046,290✔
138

139
   const range_kind_t rkind = tree_subkind(r);
1,046,290✔
140

141
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
1,046,290✔
142
      return false;
143

144
   int64_t left, right;
1,035,400✔
145
   if (!folded_int(tree_left(r), &left))
1,035,400✔
146
      return false;
147
   else if (!folded_int(tree_right(r), &right))
950,111✔
148
      return false;
149

150
   switch (rkind) {
932,670✔
151
   case RANGE_TO:
841,008✔
152
      *low  = left;
841,008✔
153
      *high = right;
841,008✔
154
      return true;
841,008✔
155
   case RANGE_DOWNTO:
91,662✔
156
      *low  = right;
91,662✔
157
      *high = left;
91,662✔
158
      return true;
91,662✔
159
   default:
160
      return false;
161
   }
162
}
163

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

168
   const range_kind_t rkind = tree_subkind(r);
60,466✔
169

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

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

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

203
   return false;
204
}
205

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

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

217
   return b;
7,187✔
218
}
219

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

228
   return f;
32,805✔
229
}
230

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

239
   return f;
×
240
}
241

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

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

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

254
      type_t elem = type_elem(base);
17✔
255

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

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

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

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

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

276
      assert(n <= max);
17✔
277
      array->count = n;
17✔
278

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

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

291
      value->enums = array;
15✔
292
      return true;
15✔
293
   }
294

295
   while (isspace_iso88591(*str))
79✔
296
      ++str;
10✔
297

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

304
         if (is_negative) ++str;
44✔
305

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

315
         value->integer = is_negative ? -sum : sum;
44✔
316

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

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

334
         ident_t id = ident_new(copy);
14✔
335

336
         value->integer = -1;
14✔
337

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

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

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

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

365
         while (isspace_iso88591(*str)) ++str;
10✔
366

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

372
         if (p == copy)
5✔
373
            return false;
374

375
         ident_t id = ident_new(copy);
4✔
376

377
         value->integer = -1;
4✔
378

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

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

393
   default:
394
      return false;
395
   }
396

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

402
   return true;
403
}
404

405
tree_t make_ref(tree_t to)
14,893✔
406
{
407
   tree_t t = tree_new(T_REF);
14,893✔
408
   tree_set_ident(t, tree_ident(to));
14,893✔
409
   tree_set_ref(t, to);
14,893✔
410
   tree_set_type(t, tree_type(to));
14,893✔
411
   return t;
14,893✔
412
}
413

414
vhdl_standard_t standard(void)
6,614,270✔
415
{
416
   return current_std;
6,614,270✔
417
}
418

419
void set_standard(vhdl_standard_t s)
3,948✔
420
{
421
   current_std = s;
3,948✔
422
   have_set_std = true;
3,948✔
423
}
3,948✔
424

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

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

437
   if ((unsigned)s < ARRAY_LEN(text))
3,601✔
438
      return text[s];
3,601✔
439
   else
440
      return "????";
441
}
442

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

449
   return offset;
×
450
}
451

452
tree_t find_record_field(tree_t rref)
8,465✔
453
{
454
   ident_t fname = tree_ident(rref);
8,465✔
455
   type_t value_type = tree_type(tree_value(rref));
8,465✔
456

457
   if (!type_is_record(value_type))
8,465✔
458
      return NULL;
459

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

467
   return NULL;
468
}
469

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

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

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

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

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

496
         return NULL;
497
      }
498

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

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

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

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

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

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

645
bool is_subprogram(tree_t t)
1,719,410✔
646
{
647
   switch (tree_kind(t)) {
1,719,410✔
648
   case T_FUNC_DECL:
649
   case T_FUNC_BODY:
650
   case T_FUNC_INST:
651
   case T_PROC_DECL:
652
   case T_PROC_BODY:
653
   case T_PROC_INST:
654
      return true;
655
   case T_GENERIC_DECL:
1,503✔
656
      {
657
         const class_t class = tree_class(t);
1,503✔
658
         return class == C_FUNCTION || class == C_PROCEDURE;
1,503✔
659
      }
660
   default:
1,243,520✔
661
      return false;
1,243,520✔
662
   }
663
}
664

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

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

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

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

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

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

740
bool is_type_decl(tree_t t)
2,206,900✔
741
{
742
   switch (tree_kind(t)) {
2,206,900✔
743
   case T_TYPE_DECL:
744
   case T_SUBTYPE_DECL:
745
   case T_PROT_DECL:
746
      return true;
747
   default:
2,126,230✔
748
      return false;
2,126,230✔
749
   }
750
}
751

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

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

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

794
   tree_add_param(call, p);
142,518✔
795
   return p;
142,518✔
796
}
797

798
type_t array_aggregate_type(type_t array, int from_dim)
261✔
799
{
800
   if (type_is_none(array))
261✔
801
      return type_new(T_NONE);
1✔
802

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

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

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

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

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

824
      tree_t constraint = tree_new(T_CONSTRAINT);
223✔
825
      tree_set_subkind(constraint, C_INDEX);
223✔
826

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

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

837
      return sub;
838
   }
839
}
840

841
unsigned bits_for_range(int64_t low, int64_t high)
1,490,810✔
842
{
843
   assert(low <= high);
1,490,810✔
844

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

871
unsigned dimension_of(type_t type)
1,112,590✔
872
{
873
   switch (type_kind(type)) {
2,023,860✔
874
   case T_SUBTYPE:
911,271✔
875
      return dimension_of(type_base(type));
911,271✔
876
   case T_GENERIC:
15✔
877
      assert(type_subkind(type) == GTYPE_ARRAY);
15✔
878
      // Fall-through
879
   case T_ARRAY:
880
      return type_indexes(type);
1,109,330✔
881
   case T_NONE:
882
   case T_ACCESS:
883
   case T_RECORD:
884
      return 0;
885
   case T_INTEGER:
3,215✔
886
   case T_REAL:
887
   case T_PHYSICAL:
888
   case T_ENUM:
889
      return type_dims(type);
3,215✔
890
   default:
×
891
      fatal_trace("invalid type kind %s in dimension_of",
×
892
                  type_kind_str(type_kind(type)));
893
   }
894
}
895

896
tree_t range_of(type_t type, unsigned dim)
1,246,720✔
897
{
898
   switch (type_kind(type)) {
1,276,490✔
899
   case T_SUBTYPE:
1,042,200✔
900
      if (type_constraints(type) > 0) {
1,042,200✔
901
         tree_t c0 = type_constraint(type, 0);
1,012,540✔
902
         switch (tree_subkind(c0)) {
1,012,540✔
903
         case C_OPEN:
110✔
904
            return range_of(type_base(type), dim);
110✔
905
         case C_INDEX:
1,012,430✔
906
         case C_RANGE:
907
            return tree_range(type_constraint(type, 0), dim);
1,012,430✔
908
         default:
×
909
            fatal_trace("invalid constraint in range_of");
×
910
         }
911
      }
912
      else
913
         return range_of(type_base(type), dim);
29,664✔
914
   case T_INTEGER:
234,286✔
915
   case T_REAL:
916
   case T_PHYSICAL:
917
   case T_ENUM:
918
      return type_dim(type, dim);
234,286✔
919
   default:
×
920
      fatal_trace("invalid type kind %s in range_of",
×
921
                  type_kind_str(type_kind(type)));
922
   }
923
}
924

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

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

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

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

965
type_t index_type_of(type_t type, unsigned dim)
194,981✔
966
{
967
   if (dim >= dimension_of(type))
194,981✔
968
      return NULL;
969

970
   type_t base = type_base_recur(type);
194,967✔
971
   type_kind_t base_kind = type_kind(base);
194,967✔
972
   if (base_kind == T_ARRAY)
194,967✔
973
      return type_index(base, dim);
192,071✔
974
   else if (base_kind == T_ENUM || base_kind == T_NONE)
2,896✔
975
      return type;
976
   else if (base_kind == T_GENERIC && type_subkind(base) == GTYPE_ARRAY)
2,749✔
977
      return type_index(base, dim);
9✔
978
   else if (base_kind == T_RECORD || base_kind == T_GENERIC)
2,740✔
979
      return NULL;
980
   else
981
      return tree_type(range_of(base, dim));
2,740✔
982
}
983

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

992
ident_t well_known(well_known_t id)
230,165✔
993
{
994
   assert(id < NUM_WELL_KNOWN);
230,165✔
995
   return id_cache[id];
230,165✔
996
}
997

998
well_known_t is_well_known(ident_t ident)
216,480✔
999
{
1000
   well_known_t pos = 0;
216,480✔
1001
   for (; pos < NUM_WELL_KNOWN; pos++) {
6,700,280✔
1002
      if (id_cache[pos] == ident)
6,576,320✔
1003
         break;
1004
   }
1005

1006
   return pos;
216,480✔
1007
}
1008

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

1053
   id_cache[W_NVC_PSL_SUPPORT] = ident_new("NVC.PSL_SUPPORT");
4,082✔
1054

1055
   id_cache[W_IEEE_LOGIC_VECTOR] =
8,164✔
1056
      ident_new("IEEE.STD_LOGIC_1164.STD_LOGIC_VECTOR");
4,082✔
1057
   id_cache[W_IEEE_ULOGIC_VECTOR] =
8,164✔
1058
      ident_new("IEEE.STD_LOGIC_1164.STD_ULOGIC_VECTOR");
4,082✔
1059

1060
   id_cache[W_NUMERIC_STD_UNSIGNED] = ident_new("IEEE.NUMERIC_STD_UNSIGNED");
4,082✔
1061
   id_cache[W_NUMERIC_BIT_UNSIGNED] = ident_new("IEEE.NUMERIC_BIT_UNSIGNED");
4,082✔
1062
}
4,082✔
1063

1064
bool is_uninstantiated_package(tree_t pack)
26,624✔
1065
{
1066
   return tree_kind(pack) == T_PACKAGE
26,624✔
1067
      && tree_generics(pack) > 0
26,298✔
1068
      && tree_genmaps(pack) == 0;
27,302✔
1069
}
1070

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

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

1101
bool package_needs_body(tree_t pack)
1,452✔
1102
{
1103
   assert(tree_kind(pack) == T_PACKAGE);
1,452✔
1104

1105
   const int ndecls = tree_decls(pack);
1,452✔
1106
   for (int i = 0; i < ndecls; i++) {
106,724✔
1107
      tree_t d = tree_decl(pack, i);
105,938✔
1108

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

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

1123
      case T_PROT_DECL:
1124
         return true;
1125

1126
      default:
11,439✔
1127
         continue;
11,439✔
1128
      }
1129
   }
1130

1131
   return false;
1132
}
1133

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

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

1160
   for (int i = 0; i < ndecls; i++) {
1,799,600✔
1161
      tree_t d = tree_decl(container, i);
1,799,440✔
1162
      if (!tree_has_ident(d))
1,799,440✔
1163
         continue;
×
1164
      else if (tree_ident(d) == name) {
1,799,440✔
1165
         if (tree_kind(d) == T_TYPE_DECL
20,697✔
1166
             && type_kind(tree_type(d)) == T_INCOMPLETE)
19,461✔
1167
            best = d;
1168
         else if (nth-- == 0)
20,665✔
1169
            return d;
20,490✔
1170
      }
1171
      else if (tree_kind(d) == T_TYPE_DECL) {
1,778,740✔
1172
         type_t type = tree_type(d);
139,685✔
1173
         switch (type_kind(type)) {
139,685✔
1174
         case T_ENUM:
105,294✔
1175
            {
1176
               const int nlits = type_enum_literals(type);
105,294✔
1177
               for (int j = 0; j < nlits; j++) {
7,080,000✔
1178
                  tree_t lit = type_enum_literal(type, j);
6,986,360✔
1179
                  if (tree_ident(lit) == name && nth-- == 0)
6,986,360✔
1180
                     return lit;
11,647✔
1181
               }
1182
            }
1183
            break;
1184
         default:
1185
            break;
1186
         }
1187
      }
1,639,060✔
1188
   }
1189

1190
   return best;
1191
}
1192

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

1198
   if (cache[curr] == NULL) {
19,279✔
1199
      if (hint != NULL)
3,462✔
1200
         cache[curr] = hint;
960✔
1201
      else {
1202
         lib_t std = lib_require(well_known(lib_name));
2,502✔
1203
         cache[curr] = lib_get(std, well_known(unit_name));
2,502✔
1204
         assert(cache[curr] != NULL);
2,502✔
1205
      }
1206
   }
1207

1208
   assert(hint == NULL || hint == cache[curr]);
19,279✔
1209
   return cache[curr];
19,279✔
1210
}
1211

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

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

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

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

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

1251
      if (can_cache)
19,198✔
1252
         return (cache[which] = tree_type(d));
18,448✔
1253
      else
1254
         return tree_type(d);
750✔
1255
   }
1256
   else
1257
      return cache[which];
1258
}
1259

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1372
tree_t std_func(ident_t mangled)
×
1373
{
1374
   tree_t std = cached_std(NULL);
×
1375

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

1383
   return NULL;
1384
}
1385

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

1402
   return name;
1403
}
1404

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

1414
void mangle_one_type(text_buf_t *buf, type_t type)
155,822✔
1415
{
1416
   ident_t ident = type_ident(type);
155,822✔
1417

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

1437
   if (code)
45,573✔
1438
      tb_append(buf, code);
57,374✔
1439
   else {
1440
      tb_printf(buf, "%zu", ident_len(ident));
98,448✔
1441
      tb_istr(buf, ident);
98,448✔
1442
   }
1443
}
155,822✔
1444

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

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

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

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

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

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

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

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

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

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

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

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

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

1538
            depth -= left_len;
52✔
1539
         }
1540
      }
1541
      // Fall-through
1542

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

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

1563
   return enc;
981✔
1564
}
1565

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

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

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

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

1629
   case T_LITERAL:
1630
   case T_STRING:
1631
      return true;
1632

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

1637
   case T_RECORD_REF:
101✔
1638
      return is_static(tree_value(expr));
101✔
1639

1640
   default:
×
1641
      return false;
×
1642
   }
1643
}
1644

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

1653
         if (prefix != value)
3,382✔
1654
            return prefix;
1655

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

1662
         return expr;
1663
      }
1664

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

1670
         if (prefix != value)
552✔
1671
            return prefix;
1672

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

1682
         return expr;
1683
      }
1684

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

1690
         if (prefix != value)
938✔
1691
            return prefix;
×
1692

1693
         return expr;
1694
      }
1695

1696
   default:
1697
      return expr;
1698
   }
1699
}
1700

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

1707
   assert(tree_kind(pack) == T_PACKAGE);
679✔
1708

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

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

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

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

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

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

1742
      default:
1743
         break;
1744
      }
1745
   }
1746

1747
   return NULL;
1748
}
1749

1750
bool relaxed_rules(void)
827,194✔
1751
{
1752
   return opt_get_int(OPT_RELAXED);
827,194✔
1753
}
1754

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

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

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

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

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

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

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

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

1851
   tree_t left = NULL, right = NULL;
21,288✔
1852

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

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

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

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

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

1882
   type_add_constraint(sub, c);
21,288✔
1883

1884
   return sub;
21,288✔
1885
}
1886

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

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

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

1904
         return t;
1905
      }
1906

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

1915
         return t;
43✔
1916
      }
1917

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

1927
         return t;
351✔
1928
      }
1929

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

1939
         return t;
54✔
1940
      }
1941

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

1949
         return t;
3✔
1950
      }
1951

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

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

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

1973
   default:
1974
      break;
1975
   }
1976
}
2,110✔
1977

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

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

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

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

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

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

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

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

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

2055
   case T_LITERAL:
2056
   case T_STRING:
2057
      break;
2058

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2224
   if (highlighting)
1,369✔
2225
      tb_cat(tb, "$$");
20✔
2226

2227
   va_list ap;
1,369✔
2228
   va_start(ap, fmt);
1,369✔
2229

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

2237
   va_end(ap);
1,369✔
2238
}
1,369✔
2239

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

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

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

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

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

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

2291
static void tree_copy_cb(tree_t t, void *__ctx)
277,560✔
2292
{
2293
   copy_ctx_t *ctx = __ctx;
277,560✔
2294

2295
   if (is_subprogram(t))
277,560✔
2296
      list_add(&ctx->copied_subs, t);
11,721✔
2297
}
277,560✔
2298

2299
static void type_copy_cb(type_t type, void *__ctx)
16,507✔
2300
{
2301
   copy_ctx_t *ctx = __ctx;
16,507✔
2302

2303
   if (type_has_ident(type))
16,507✔
2304
      list_add(&ctx->copied_types, type);
10,517✔
2305
}
16,507✔
2306

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

2313
   tree_copy(roots, nroots, tree_pred, type_pred, context,
6,581✔
2314
             tree_copy_cb, type_copy_cb, &copy_ctx);
2315

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

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

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

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

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

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

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

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

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

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

2369
            break;
10,102✔
2370
         }
2371
      }
2372
   }
2373
   list_free(&copy_ctx.copied_subs);
6,581✔
2374
}
6,581✔
2375

2376
bool all_character_literals(type_t type)
192✔
2377
{
2378
   assert(type_is_enum(type));
192✔
2379

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

2387
   return true;
2388
}
2389

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

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

2411
   static ident_t operators[ARRAY_LEN(strings)];
8,691✔
2412
   INIT_ONCE({
14,606✔
2413
         for (int i = 0; i < ARRAY_LEN(strings); i++)
2414
            operators[i] = ident_new(strings[i]);
2415
      });
2416

2417
   for (int i = 0; i < ARRAY_LEN(operators); i++) {
148,215✔
2418
      if (ident == operators[i])
148,214✔
2419
         return true;
2420
   }
2421

2422
   return false;
2423
}
2424

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

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

2439
         const int nparams = tree_params(a);
216✔
2440
         assert(nparams == tree_params(b));
216✔
2441

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

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

2456
         tree_t ra = tree_range(a, 0);
32✔
2457
         tree_t rb = tree_range(b, 0);
32✔
2458

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

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

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

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