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

nickg / nvc / 9008429933

08 May 2024 08:53PM UTC coverage: 91.621% (+0.01%) from 91.611%
9008429933

push

github

nickg
Generic actuals do not need to be globally static

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

87 existing lines in 2 files now uncovered.

53275 of 58147 relevant lines covered (91.62%)

667157.9 hits per line

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

96.81
/src/sem.c
1
//
2
//  Copyright (C) 2011-2024  Nick Gasson
3
//
4
//  This program is free software: you can redistribute it and/or modify
5
//  it under the terms of the GNU General Public License as published by
6
//  the Free Software Foundation, either version 3 of the License, or
7
//  (at your option) any later version.
8
//
9
//  This program is distributed in the hope that it will be useful,
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
//  GNU General Public License for more details.
13
//
14
//  You should have received a copy of the GNU General Public License
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

18
#include "util.h"
19
#include "common.h"
20
#include "diag.h"
21
#include "lib.h"
22
#include "mask.h"
23
#include "names.h"
24
#include "option.h"
25
#include "phase.h"
26
#include "type.h"
27

28
#include <assert.h>
29
#include <stdlib.h>
30
#include <string.h>
31

32
typedef bool (*static_fn_t)(tree_t t);
33

34
typedef struct {
35
   tree_t decl;
36
   bool   have;
37
   bool   partial;
38
} formal_map_t;
39

40
static bool sem_check_array_ref(tree_t t, nametab_t *tab);
41
static bool sem_locally_static(tree_t t);
42
static bool sem_globally_static(tree_t t);
43
static tree_t sem_check_lvalue(tree_t t);
44
static bool sem_check_same_type(tree_t left, tree_t right);
45
static bool sem_check_type(tree_t t, type_t expect, nametab_t *tab);
46
static bool sem_static_name(tree_t t, static_fn_t check_fn);
47
static bool sem_static_subtype(type_t type, static_fn_t fn);
48
static bool sem_check_attr_ref(tree_t t, bool allow_range, nametab_t *tab);
49
static bool sem_check_generic_map(tree_t t, tree_t unit, nametab_t *tab);
50
static bool sem_check_port_map(tree_t t, tree_t unit, nametab_t *tab);
51
static bool sem_check_subtype(tree_t decl, type_t type, nametab_t *tab);
52
static bool sem_check_incomplete(tree_t t, type_t type);
53

54
#define sem_error(t, ...) do {                        \
55
      error_at(t ? tree_loc(t) : NULL , __VA_ARGS__); \
56
      return false;                                   \
57
   } while (0)
58

59
#define pedantic_diag(t) ({                            \
60
         static int _warned = 0;                       \
61
         _pedantic_diag(tree_loc(t), &_warned, NULL);  \
62
      })
63

64
static diag_t *_pedantic_diag(const loc_t *loc, int *warned, bool *error)
59✔
65
{
66
   const bool relaxed = opt_get_int(OPT_RELAXED);
59✔
67
   if (!relaxed || !*warned) {
59✔
68
      const diag_level_t level = relaxed ? DIAG_WARN : DIAG_ERROR;
52✔
69
      diag_t *d = diag_new(level, loc);
52✔
70
      if (level == DIAG_ERROR)
52✔
71
         diag_hint(d, NULL, "the $bold$--relaxed$$ option downgrades this "
27✔
72
                   "to a warning");
73

74
      *warned = 1;
52✔
75
      if (error) *error = !relaxed;
52✔
76
      return d;
52✔
77
   }
78
   else
79
      return NULL;
80
}
81

82
static bool sem_check_resolution(type_t type, tree_t res)
334✔
83
{
84
   // Resolution functions are described in LRM 93 section 2.4
85

86
   if (tree_kind(res) == T_ELEM_RESOLUTION) {
363✔
87
      // VHDL-2008 element resolution
88
      assert(standard() >= STD_08);
30✔
89

90
      if (type_is_array(type)) {
30✔
91
         tree_t sub = tree_value(tree_assoc(res, 0));
29✔
92
         return sem_check_resolution(type_elem(type), sub);
29✔
93
      }
94
      else if (type_is_record(type))
1✔
95
         sem_error(res, "sorry, record element resolution functions are not"
×
96
                   "supported yet");
97
      else {
98
         // Should have been caught during name resolution
99
         assert(error_count() > 0);
1✔
100
         return false;
101
      }
102
   }
103

104
   assert(tree_kind(res) == T_REF);
333✔
105

106
   if (!tree_has_ref(res))
333✔
107
      return false;
108

109
   tree_t fdecl = tree_ref(res);
330✔
110
   assert(is_subprogram(fdecl));
330✔
111

112
   type_t ftype = tree_type(fdecl);
330✔
113

114
   if (type_kind(ftype) != T_FUNC)
330✔
115
      sem_error(res, "resolution function name %s is not a function",
1✔
116
                istr(tree_ident(res)));
117

118
   // Must take a single parameter of array of base type
119

120
   if (type_params(ftype) != 1)
329✔
121
      sem_error(res, "resolution function must have a single argument");
1✔
122

123
   type_t param = type_param(ftype, 0);
328✔
124
   if (type_kind(param) != T_ARRAY)
328✔
125
      sem_error(res, "parameter of resolution function must be "
1✔
126
                "an unconstrained array type");
127

128
   if (!type_eq(type_elem(param), type))
327✔
129
      sem_error(res, "parameter of resolution function must be "
1✔
130
                "an array of %s but found %s", type_pp(type),
131
                type_pp(type_elem(param)));
132

133
   // Return type must be the resolved type
134

135
   if (!type_eq(type_result(ftype), type))
326✔
136
      sem_error(res, "result of resolution function must be %s but have %s",
1✔
137
                type_pp(type), type_pp(type_result(ftype)));
138

139
   return true;
140
}
141

142
static bool sem_check_range(tree_t r, type_t expect, type_kind_t kind,
20,147✔
143
                            nametab_t *tab)
144
{
145
   if (expect != NULL && type_is_none(expect))
20,147✔
146
      return false;   // Prevent cascading errors
147

148
   switch (tree_subkind(r)) {
20,126✔
149
   case RANGE_EXPR:
4,560✔
150
      {
151
         tree_t expr = tree_value(r);
4,560✔
152

153
         type_t type = tree_type(expr);
4,560✔
154
         if (type_is_none(type))
4,560✔
155
            return false;   // Was earlier error
156

157
         if (tree_kind(expr) != T_ATTR_REF)
4,558✔
158
            sem_error(expr, "invalid expression in range constraint");
×
159

160
         if (!sem_check_attr_ref(expr, true, tab))
4,558✔
161
            return false;
162

163
         if (expect && !sem_check_type(expr, expect, tab))
4,553✔
164
            sem_error(expr, "expected type of range bound to be %s but is %s",
2✔
165
                      type_pp(expect), type_pp(type));
166

167
         if (kind != T_LAST_TYPE_KIND) {
4,551✔
168
            assert(kind == T_INTEGER || kind == T_REAL);
1✔
169
            if (type_base_kind(type) != kind)
1✔
170
               sem_error(expr, "type of range bounds must be of some %s type "
1✔
171
                         "but have %s", kind == T_INTEGER ? "integer" : "real",
172
                         type_pp(type));
173
         }
174
      }
175
      break;
176

177
   case RANGE_TO:
15,565✔
178
   case RANGE_DOWNTO:
179
      {
180
         tree_t left = tree_left(r);
15,565✔
181
         if (!sem_check(left, tab))
15,565✔
182
            return false;
183

184
         tree_t right = tree_right(r);
15,564✔
185
         if (!sem_check(right, tab))
15,564✔
186
            return false;
187

188
         if (expect != NULL) {
15,561✔
189
            if (!sem_check_same_type(left, right))
15,386✔
190
               sem_error(right, "type mismatch in range: left is %s,"
3✔
191
                         " right is %s", type_pp(tree_type(left)),
192
                         type_pp(tree_type(right)));
193

194
            if (!sem_check_type(left, expect, tab))
15,383✔
195
               sem_error(r, "expected type of range bounds to be %s but"
4✔
196
                         " have %s", type_pp(expect), type_pp(tree_type(left)));
197

198
            // This cannot fail because we know left and right have the
199
            // same type and left is equal to expect, but we still need
200
            // to call sem_check_type for the implicit conversion
201
            sem_check_type(right, expect, tab);
15,379✔
202
            sem_check_type(r, expect, tab);
15,379✔
203
         }
204

205
         if (kind != T_LAST_TYPE_KIND) {
15,554✔
206
            // See LRM 93 section 3.1.2: Each bound of a range
207
            // constraint that must be of some integer type, but the two
208
            // bounds need not have the same integer type.
209
            assert(kind == T_INTEGER || kind == T_REAL);
175✔
210
            if (type_base_kind(tree_type(left)) != kind)
175✔
211
               sem_error(left, "type of left bound must be of some %s type "
×
212
                         "but have %s", kind == T_INTEGER ? "integer" : "real",
213
                         type_pp(tree_type(left)));
214
            else if (type_base_kind(tree_type(right)) != kind)
175✔
215
               sem_error(right, "type of right bound must be of some %s type "
4✔
216
                         "but have %s", kind == T_INTEGER ? "integer" : "real",
217
                         type_pp(tree_type(right)));
218
         }
219
      }
220
      break;
221
   }
222

223
   return true;
224
}
225

226
static bool sem_check_discrete_range(tree_t r, type_t expect, nametab_t *tab)
16,962✔
227
{
228
   if (!sem_check_range(r, expect ?: tree_type(r), T_LAST_TYPE_KIND, tab))
16,962✔
229
      return false;
230

231
   const range_kind_t kind = tree_subkind(r);
16,925✔
232
   type_t type = tree_type(r);
16,925✔
233
   if (type_is_none(type) || kind == RANGE_ERROR)
16,925✔
234
      return false;
235

236
   if (!type_is_discrete(type))
16,924✔
237
      sem_error(r, "type of range bounds %s is not discrete", type_pp(type));
1✔
238

239
   // See LRM 93 section 3.2.1.1: universal integer bound must be a
240
   // numeric literal or attribute. Later LRMs relax the wording here.
241
   if (standard() < STD_00 && !relaxed_rules() && kind != RANGE_EXPR) {
16,923✔
242
      tree_t left  = tree_left(r);
6,963✔
243
      tree_t right = tree_right(r);
6,963✔
244

245
      type_t left_type  = tree_type(left);
6,963✔
246
      type_t right_type = tree_type(right);
6,963✔
247

248
      if (type_is_universal(left_type) && type_is_universal(right_type)) {
6,963✔
249
         tree_kind_t lkind = tree_kind(left);
1✔
250
         tree_kind_t rkind = tree_kind(right);
1✔
251

252
         const bool invalid =
2✔
253
            lkind != T_LITERAL && lkind != T_ATTR_REF
1✔
254
            && rkind != T_LITERAL && rkind != T_ATTR_REF;
1✔
255

256
         if (invalid)
1✔
257
            sem_error(r, "universal integer bound must be numeric"
×
258
                      " literal or attribute");
259
      }
260
   }
261

262
   return true;
263
}
264

265
static bool sem_check_constraint(tree_t constraint, type_t base, nametab_t *tab)
14,471✔
266
{
267
   const constraint_kind_t consk = tree_subkind(constraint);
14,471✔
268
   switch (consk) {
14,471✔
269
   case C_RANGE:
3,010✔
270
      if (!type_is_scalar(base))
3,010✔
271
         sem_error(constraint, "range constraint cannot be used with "
2✔
272
                   "non-scalar type %s", type_pp(base));
273
      break;
274

275
   case C_INDEX:
11,129✔
276
      if (!type_is_array(base))
11,129✔
277
         sem_error(constraint, "index constraint cannot be used with "
4✔
278
                   "non-array type %s", type_pp(base));
279
      break;
280

281
   case C_OPEN:
32✔
282
      if (!type_is_array(base))
32✔
283
         sem_error(constraint, "array constraint cannot be used with "
1✔
284
                   "non-array type %s", type_pp(base));
285
      return true;
286

287
   case C_RECORD:
300✔
288
      {
289
         if (!type_is_record(base))
300✔
290
            sem_error(constraint, "record element constraint cannot be used "
×
291
                      "with non-record type %s", type_pp(base));
292

293
         // Range list is overloaded to hold record element constraints
294
         const int nelem = tree_ranges(constraint);
300✔
295
         for (int i = 0; i < nelem; i++) {
819✔
296
            tree_t ei = tree_range(constraint, i);
525✔
297
            assert(tree_kind(ei) == T_ELEM_CONSTRAINT);
525✔
298

299
            if (!tree_has_ref(ei))
525✔
300
               return false;   // Was parse error
301

302
            tree_t decl = tree_ref(ei);
522✔
303
            assert(tree_kind(decl) == T_FIELD_DECL);  // Checked by parser
522✔
304

305
            type_t ftype = tree_type(decl);
522✔
306
            if (!type_is_unconstrained(ftype))
522✔
307
               sem_error(constraint, "field %s in record element constraint is "
1✔
308
                         "already constrained", istr(tree_ident(decl)));
309

310
            type_t sub = tree_type(ei);
521✔
311
            if (!sem_check_subtype(decl, sub, tab))
521✔
312
               return false;
313

314
            // Check for duplicate element constraints
315
            tree_t fi = tree_ref(ei);
520✔
316
            for (int j = 0; j < i; j++) {
919✔
317
               tree_t ej = tree_range(constraint, j);
400✔
318
               if (tree_pos(tree_ref(ej)) == tree_pos(fi))
400✔
319
                  sem_error(ei, "duplicate record element constraint for "
400✔
320
                            "field %s", istr(tree_ident(fi)));
321
            }
322

323
            if (type_kind(base) == T_SUBTYPE) {
519✔
324
               tree_t dup = type_constraint_for_field(base, fi);
3✔
325
               if (dup != NULL && !type_is_unconstrained(tree_type(dup))) {
3✔
326
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(ei));
×
327
                  diag_printf(d, "duplicate record element constraint for "
×
328
                              "field %s", istr(tree_ident(fi)));
329
                  diag_hint(d, tree_loc(dup), "constraint in subtype %s",
×
330
                            type_pp(base));
331
                  diag_hint(d, tree_loc(ei), "duplicate constraint here");
×
332
                  diag_emit(d);
×
333
                  return false;
×
334
               }
335
            }
336
         }
337

338
         // Code belows handles index and range constraints
339
         return true;
340
      }
341
   }
342

343
   if (type_is_array(base)) {
14,133✔
344
      if (type_kind(base) == T_SUBTYPE && !type_is_unconstrained(base))
11,125✔
345
         sem_error(constraint, "cannot change constraints of constrained "
5✔
346
                   "array type %s", type_pp(base));
347
   }
348
   else if (type_is_record(base) && standard() < STD_08)
3,008✔
349
      sem_error(constraint, "record subtype may not have constraints "
×
350
                "in VHDL-%s", standard_text(standard()));
351

352
   const int ndims_base = type_is_array(base) ? dimension_of(base) : 1;
14,128✔
353
   const int ndims = tree_ranges(constraint);
14,128✔
354

355
   if (ndims != ndims_base)
14,128✔
356
      sem_error(constraint, "expected %d constraints for type %s but found %d",
1✔
357
                ndims_base, type_pp(base), ndims);
358

359
   for (int i = 0; i < ndims; i++) {
28,569✔
360
      tree_t r = tree_range(constraint, i);
14,453✔
361
      type_t index = index_type_of(base, i);
14,453✔
362

363
      switch (consk) {
14,453✔
364
      case C_INDEX:
11,445✔
365
         if (!sem_check_discrete_range(r, index, tab))
11,445✔
366
            return false;
367
         break;
368

369
      case C_RANGE:
3,008✔
370
         if (!sem_check_range(r, index, T_LAST_TYPE_KIND, tab))
3,008✔
371
            return false;
372
         break;
373

374
      default:
375
         break;
376
      }
377
   }
378

379
   return true;
380
}
381

382
static bool sem_check_subtype_helper(tree_t decl, type_t type, nametab_t *tab)
14,918✔
383
{
384
   // Shared code for checking subtype declarations and implicit subtypes
385

386
   type_t base = type_base(type);
14,918✔
387
   if (type_is_none(base))
14,918✔
388
      return false;
389
   else if (type_is_access(base))
14,912✔
390
      base = type_designated(base);
2✔
391

392
   if (type_is_protected(base))
14,912✔
393
      sem_error(decl, "subtypes may not have protected base types");
1✔
394
   else if (!sem_check_incomplete(decl, type))
14,911✔
395
      return false;
396

397
   type_t elem = base;
14,910✔
398
   const int ncon = type_constraints(type);
14,910✔
399
   for (int i = 0; i < ncon; i++) {
29,351✔
400
      tree_t cons = type_constraint(type, i);
14,471✔
401
      if (!sem_check_constraint(cons, elem, tab))
14,471✔
402
         return false;
403

404
      const constraint_kind_t consk = tree_subkind(cons);
14,441✔
405
      if (i + 1 < ncon && (consk == C_INDEX || consk == C_OPEN))
14,441✔
406
         elem = type_elem(elem);
×
407
   }
408

409
   if (type_is_array(type) && type_has_elem(type)) {
14,880✔
410
      type_t elem = type_elem(type);
256✔
411
      if (type_kind(elem) == T_SUBTYPE && !type_has_ident(elem)) {
256✔
412
         // Anonymous subtype created for array element constraint
413
         assert(standard() >= STD_08);
204✔
414

415
         if (!sem_check_subtype_helper(decl, elem, tab))
204✔
416
            return false;
417
      }
418
   }
419

420
   if (type_has_resolution(type)) {
14,878✔
421
      if (!sem_check_resolution(type_base(type), type_resolution(type)))
334✔
422
         return false;
9✔
423
   }
424

425
   return true;
426
}
427

428
static bool sem_check_subtype(tree_t decl, type_t type, nametab_t *tab)
68,522✔
429
{
430
   // Check an anonymous subtype at the point of use
431

432
   if (type_kind(type) != T_SUBTYPE)
68,522✔
433
      return true;
434
   else if (type_has_ident(type))
25,572✔
435
      return true;   // Explicitly declared subtype
436

437
   return sem_check_subtype_helper(decl, type, tab);
12,681✔
438
}
439

440
static bool sem_check_use_clause(tree_t c, nametab_t *tab)
14,243✔
441
{
442
   if (standard() >= STD_08) {
14,243✔
443
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
4,968✔
444

445
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
4,968✔
446
         // LRM 08 section 13.3
447
         ident_t prefix = ident_until(tree_ident(c), '.');
35✔
448
         if (prefix == well_known(W_WORK))
35✔
449
            sem_error(c, "selected name in context declaration use clause "
1✔
450
                      "may not have WORK as a prefix");
451
      }
452
   }
453

454
   return true;
455
}
456

457
static bool sem_check_library_clause(tree_t t, nametab_t *tab)
23,829✔
458
{
459
   if (standard() >= STD_08) {
23,829✔
460
      ident_t name = tree_ident(t);
7,861✔
461
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
7,861✔
462

463
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
7,861✔
464
         // LRM 08 section 13.3
465
         if (name == well_known(W_WORK))
13✔
466
            sem_error(t, "library clause in a context declaration may not have "
1✔
467
                      "logical library name WORK");
468
      }
469
   }
470

471
   return true;
472
}
473

474
static bool sem_check_context_clause(tree_t t, nametab_t *tab)
11,444✔
475
{
476
   // Ignore the implicit WORK and STD with context declarations
477
   const int ignore = tree_kind(t) == T_CONTEXT ? 2 : 0;
11,444✔
478

479
   bool ok = true;
11,444✔
480
   const int ncontexts = tree_contexts(t);
11,444✔
481
   for (int n = ignore; n < ncontexts; n++)
49,537✔
482
      ok = sem_check(tree_context(t, n), tab) && ok;
38,096✔
483

484
   return ok;
11,444✔
485
}
486

487
static bool sem_readable(tree_t t)
192,422✔
488
{
489
   switch (tree_kind(t)) {
201,299✔
490
   case T_REF:
75,712✔
491
      {
492
         if (tree_flags(t) & TREE_F_FORMAL_NAME)
75,712✔
493
            return true;   // Name appearing in formal
494

495
         tree_t decl = tree_ref(t);
75,712✔
496
         switch (tree_kind(decl)) {
75,712✔
497
         case T_PORT_DECL:
1,569✔
498
            {
499
               const port_mode_t mode = tree_subkind(decl);
1,569✔
500
               if (mode == PORT_OUT && standard() < STD_08) {
1,569✔
501
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
502
                  diag_printf(d, "cannot read output port %s",
2✔
503
                              istr(tree_ident(t)));
504
                  diag_hint(d, tree_loc(decl), "%s declared with mode OUT",
2✔
505
                            istr(tree_ident(decl)));
506
                  diag_hint(d, tree_loc(t), "read here");
2✔
507
                  diag_hint(d, NULL, "outputs can be read with "
2✔
508
                            "$bold$--std=2008$$");
509
                  diag_emit(d);
2✔
510
                  return false;
2✔
511
               }
512
               else if (mode == PORT_LINKAGE)
1,567✔
513
                  sem_error(t, "linkage port %s may not be read except as "
1✔
514
                            "an actual corresponding to an interface of mode "
515
                            "linkage", istr(tree_ident(t)));
516
            }
517
            break;
518

519
         case T_PARAM_DECL:
21,322✔
520
            if (tree_subkind(decl) == PORT_OUT && standard() < STD_08)
21,322✔
521
               sem_error(t, "cannot read OUT parameter %s",
2✔
522
                         istr(tree_ident(t)));
523
            break;
524

525
         default:
526
            break;
527
         }
528

529
         return true;
530
      }
531

532
   case T_ARRAY_REF:
8,877✔
533
   case T_ARRAY_SLICE:
534
      return sem_readable(tree_value(t));
8,877✔
535

536
   default:
537
      return true;
538
   }
539
}
540

541
static bool sem_check_array_dims(type_t type, type_t constraint, nametab_t *tab)
277✔
542
{
543
   const int ndims = dimension_of(type);
277✔
544
   for (int i = 0; i < ndims; i++) {
569✔
545
      tree_t r = range_of(type, i);
292✔
546

547
      type_t index_type = NULL;
292✔
548
      if (constraint != NULL && i < dimension_of(constraint))
292✔
549
         index_type = index_type_of(constraint, i);
×
550

551
      if (!sem_check_discrete_range(r, index_type, tab))
292✔
552
         return false;
553

554
      if (index_type == NULL)
292✔
555
         index_type = tree_type(r);
292✔
556

557
      if (!sem_check_type(r, index_type, tab))
292✔
558
         sem_error(r, "type of bound %s does not match type of index %s",
292✔
559
                   type_pp(tree_type(r)),
560
                   type_pp(index_type));
561
   }
562

563
   return true;
564
}
565

566
static bool sem_check_type(tree_t t, type_t expect, nametab_t *tab)
372,821✔
567
{
568
   type_t actual = tree_type(t);
372,821✔
569

570
   if (type_eq_map(actual, expect, get_generic_map(tab)))
372,821✔
571
      return true;
572

573
   // Supress cascading errors
574
   if (type_is_none(actual) || type_is_none(expect))
244✔
575
      return true;
5✔
576

577
   return false;
578
}
579

580
static bool sem_check_same_type(tree_t left, tree_t right)
31,294✔
581
{
582
   type_t left_type  = tree_type(left);
31,294✔
583
   type_t right_type = tree_type(right);
31,294✔
584

585
   if (type_eq(left_type, right_type))
31,294✔
586
      return true;
587

588
   // Supress cascading errors
589
   if (type_is_none(left_type) || type_is_none(right_type))
18✔
590
      return true;
4✔
591

592
   return false;
593
}
594

595
static bool sem_has_access(type_t t)
63,949✔
596
{
597
   // returns true if the type is an access type or is a composite
598
   // type that contains a subelement of an access type
599
   type_t base = type_base_recur(t);
92,757✔
600

601
   if (type_is_access(base))
92,757✔
602
      return true;
603

604
   if (type_is_array(base))
91,974✔
605
      return sem_has_access(type_elem(base));
28,808✔
606

607
   if (type_is_record(base)) {
63,166✔
608
      const int nfields = type_fields(base);
4,309✔
609
      for (int i = 0; i < nfields; i++) {
20,746✔
610
         if (sem_has_access(tree_type(type_field(base, i))))
16,511✔
611
            return true;
612
      }
613
   }
614

615
   return false;
616
}
617

618
static bool sem_check_type_decl(tree_t t, nametab_t *tab)
4,470✔
619
{
620
   type_t type = tree_type(t);
4,470✔
621

622
   // Nothing more to do for incomplete types
623
   if (type_kind(type) == T_INCOMPLETE)
4,470✔
624
      return true;
625

626
   type_kind_t kind = type_kind(type);
4,428✔
627

628
   if (kind == T_SUBTYPE) {
4,428✔
629
      // Implicitly created subtype for a constrained array defintion
630
      if (!sem_check_subtype_helper(t, type, tab)) {
669✔
631
         // Prevent cascading errors
632
         // TODO: can we do this check in the parser and set T_NONE earlier?
633
         type_set_base(type, type_new(T_NONE));
4✔
634
         return false;
4✔
635
      }
636

637
      type = type_base(type);
665✔
638
      kind = type_kind(type);
665✔
639
      assert(kind == T_ARRAY);
665✔
640
   }
641

642
   switch (kind) {
4,424✔
643
   case T_ARRAY:
2,319✔
644
      {
645
         type_t elem_type = type_elem(type);
2,319✔
646
         if (!sem_check_subtype(t, elem_type, tab))
2,319✔
647
            return false;
648

649
         if (standard() < STD_08 && type_is_unconstrained(elem_type)) {
2,319✔
650
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
651
            diag_printf(d, "array %s cannot have unconstrained element type",
1✔
652
                        istr(tree_ident(t)));
653
            diag_hint(d, NULL, "this would be allowed with $bold$--std=2008$$");
1✔
654
            diag_emit(d);
1✔
655
            return false;
1✔
656
         }
657

658
         if (type_is_file(elem_type))
2,318✔
659
            sem_error(t, "array %s cannot have element of file type",
1✔
660
                      istr(tree_ident(t)));
661
         else if (type_is_protected(elem_type))
2,317✔
662
            sem_error(t, "array %s cannot have element of protected type",
1✔
663
                      istr(tree_ident(t)));
664
         else if (!sem_check_incomplete(t, elem_type))
2,316✔
665
            return false;
666

667
         const int nindex = type_indexes(type);
2,315✔
668
         for (int i = 0; i < nindex; i++) {
4,934✔
669
            type_t index_type = type_index(type, i);
2,623✔
670
            if (type_is_none(index_type))
2,623✔
671
               return false;
672
            else if (!type_is_discrete(index_type))
2,621✔
673
               sem_error(t, "index type %s is not discrete",
2,621✔
674
                         type_pp(index_type));
675
         }
676

677
         return true;
678
      }
679

680
   case T_ENUM:
681
      return true;
682

683
   case T_PHYSICAL:
45✔
684
      {
685
         const int nunits = type_units(type);
45✔
686
         for (int i = 0; i < nunits; i++) {
200✔
687
            tree_t u = type_unit(type, i);
157✔
688
            tree_set_type(u, type);
157✔
689
            if (!sem_check(u, tab))
157✔
690
               return false;
691

692
            tree_t value = tree_value(u);
157✔
693

694
            // LRM 08 section 5.2.4.1: the abstract literal portion
695
            // shall be an integer literal
696
            if (tree_ival(value) == 0 && tree_dval(value) != 0)
157✔
697
               sem_error(value, "the abstract literal portion of a secondary "
1✔
698
                         "unit declaration must be an integer literal");
699

700
            if (i > 0 && !sem_check_type(value, type, tab))
156✔
701
               sem_error(value, "secondary unit %s must have type %s",
156✔
702
                         istr(tree_ident(u)), type_pp(type));
703
         }
704
      }
705

706
      // Fall-through
707
   case T_INTEGER:
708
   case T_REAL:
709
      {
710
         tree_t r = type_dim(type, 0);
177✔
711

712
         const type_kind_t check_kind = kind == T_PHYSICAL ? T_INTEGER : kind;
177✔
713
         if (!sem_check_range(r, NULL, check_kind, tab))
177✔
714
            return false;
715

716
         // Standard specifies type of 'LEFT and 'RIGHT are same
717
         // as the declared type
718
         switch (tree_subkind(r)) {
172✔
719
         case RANGE_TO:
172✔
720
         case RANGE_DOWNTO:
721
            tree_set_type(tree_left(r), type);
172✔
722
            tree_set_type(tree_right(r), type);
172✔
723
            break;
172✔
724
         case RANGE_EXPR:
×
725
            tree_set_type(tree_value(r), type);
×
726
            break;
×
727
         }
728

729
         tree_set_type(r, type);
172✔
730
         return true;
172✔
731
      }
732

733
   case T_RECORD:
1,105✔
734
      {
735
         const int nfields = type_fields(type);
1,105✔
736
         for (int i = 0; i < nfields; i++) {
4,585✔
737
            tree_t f = type_field(type, i);
3,486✔
738

739
            if (!sem_check(f, tab))
3,486✔
740
               return false;
741

742
            // Each field name must be distinct
743
            ident_t f_name = tree_ident(f);
3,485✔
744
            for (int j = 0; j < i; j++) {
11,921✔
745
               tree_t fj = type_field(type, j);
8,437✔
746
               if (f_name == tree_ident(fj)) {
8,437✔
747
                  diag_t *d = diag_new(DIAG_ERROR, tree_loc(f));
1✔
748
                  diag_printf(d, "duplicate field name %s", istr(f_name));
1✔
749
                  diag_hint(d, tree_loc(fj), "previously declared here");
1✔
750
                  diag_hint(d, tree_loc(f), "declared again here");
1✔
751
                  diag_emit(d);
1✔
752
                  return false;
1✔
753
               }
754
            }
755

756
            type_t f_type = tree_type(f);
3,484✔
757

758
            if (!sem_check_subtype(f, f_type, tab))
3,484✔
759
               return false;
760

761
            // Recursive record types are not allowed
762
            if (type_eq(type, f_type))
3,483✔
763
               sem_error(f, "recursive record types are not allowed");
×
764

765
            // Element types may not be unconstrained before VHDL-2008
766
            if (standard() < STD_08 && type_is_unconstrained(f_type)) {
3,483✔
767
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(f));
1✔
768
               diag_printf(d, "record field %s cannot have unconstrained "
1✔
769
                           "array type in VHDL-%s", istr(f_name),
770
                           standard_text(standard()));
771
               diag_hint(d, NULL, "pass $bold$--std=2008$$ to enable this "
1✔
772
                         "feature");
773
               diag_emit(d);
1✔
774
               return false;
1✔
775
            }
776
            else if (type_is_file(f_type))
3,482✔
777
               sem_error(f, "record field %s cannot be of file type",
1✔
778
                         istr(f_name));
779
            else if (type_is_protected(f_type))
3,481✔
780
               sem_error(f, "record field %s cannot be of protected type",
3,481✔
781
                         istr(f_name));
782
         }
783

784
         return true;
785
      }
786

787
   case T_FILE:
88✔
788
      // Rules for file types are in LRM 93 section 3.4
789
      {
790
         type_t f = type_designated(type);
88✔
791

792
         switch (type_kind(f)) {
88✔
793
         case T_ACCESS:
1✔
794
            sem_error(t, "files may not be of access type");
1✔
795
            break;
1✔
796
         case T_FILE:
1✔
797
            sem_error(t, "files may not be of file type");
1✔
798
            break;
1✔
799
         case T_PROTECTED:
1✔
800
            sem_error(t, "files may not be of protected type");
1✔
801
            break;
85✔
802
         default:
803
            break;
85✔
804
         }
805

806
         if (sem_has_access(f))
85✔
807
            sem_error(t, "type %s has a subelement with an access type",
4✔
808
                      type_pp(f));
809

810
         type_t base_f = type_base_recur(f);
81✔
811
         if (type_is_array(base_f) && dimension_of(base_f) > 1)
81✔
812
            sem_error(t, "array type for file type must be one-dimensional");
2✔
813

814
         return true;
815
      }
816

817
   case T_ACCESS:
309✔
818
      // Rules for access types are in LRM 93 section 3.3
819
      {
820
         type_t designated = type_designated(type);
309✔
821

822
         if (!sem_check_subtype(t, designated, tab))
309✔
823
            return false;
824

825
         if (standard() < STD_19) {
308✔
826
            if (type_is_file(designated))
260✔
827
               sem_error(t, "access type %s cannot designate file type",
1✔
828
                         istr(tree_ident(t)));
829

830
            if (type_is_protected(designated))
259✔
831
               sem_error(t, "access type %s cannot designate protected type",
1✔
832
                         istr(tree_ident(t)));
833
         }
834

835
         return true;
836
      }
837

838
   case T_PROTECTED:
839
   default:
840
      return true;
841
   }
842
}
843

844
static bool sem_check_subtype_decl(tree_t t, nametab_t *tab)
1,364✔
845
{
846
   type_t type = tree_type(t);
1,364✔
847
   assert(type_kind(type) == T_SUBTYPE);
1,364✔
848
   assert(type_has_ident(type));
1,364✔
849

850
   return sem_check_subtype_helper(t, type, tab);
1,364✔
851
}
852

853
static bool sem_check_incomplete(tree_t t, type_t type)
111,139✔
854
{
855
   if (type_is_incomplete(type)) {
111,139✔
856
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
12✔
857
      diag_printf(d, "invalid use of incomplete type %s", type_pp(type));
12✔
858
      diag_hint(d, NULL, "prior to the end of the corresponding full type "
12✔
859
                "declaration, an incomplete type may only be used as the "
860
                "designated type of an access type declaration");
861
      diag_lrm(d, STD_08, "5.4.2");
12✔
862
      diag_emit(d);
12✔
863

864
      return false;
12✔
865
   }
866

867
   return true;
868
}
869

870
static bool sem_no_access_file_or_protected(tree_t t, type_t type, const char *what)
11,825✔
871
{
872
   // constants, signals, attributes, generics, ports
873
   // may not be of an access, file, or protected type, or
874
   // of a composite type with a subelement of an access type
875

876
   if (type_is_access(type))
11,825✔
877
      sem_error(t, "%s may not have access type", what);
4✔
878

879
   if (sem_has_access(type))
11,821✔
880
      sem_error(t, "%s may not have a type with a subelement of access type", what);
6✔
881

882
   if (type_is_protected(type))
11,815✔
883
      sem_error(t, "%s may not have protected type", what);
3✔
884

885
   if (type_is_file(type))
11,812✔
886
      sem_error(t, "%s may not have file type", what);
4✔
887

888
   return true;
889
}
890

891
static void sem_unconstrained_decl_hint(diag_t *d, type_t type)
14✔
892
{
893
   if (!type_is_record(type))
14✔
894
      return;
895

896
   // Tell the user which field is unconstrained
897

898
   type_t base = type_base_recur(type);
5✔
899
   const int nfields = type_fields(base);
5✔
900
   for (int i = 0; i < nfields; i++) {
15✔
901
      tree_t f = type_field(base, i);
10✔
902
      if (!type_is_unconstrained(tree_type(f)))
10✔
903
         continue;
2✔
904
      else if (type_constraint_for_field(type, f) == NULL)
8✔
905
         diag_hint(d, NULL, "missing record element constraint for field %s",
6✔
906
                   istr(tree_ident(f)));
907
   }
908
}
909

910
static bool sem_check_const_decl(tree_t t, nametab_t *tab)
6,367✔
911
{
912
   type_t type = tree_type(t);
6,367✔
913

914
   if (!sem_check_subtype(t, type, tab))
6,367✔
915
      return false;
916
   else if (type_is_none(type))
6,365✔
917
      return false;
918
   else if (!sem_check_incomplete(t, type))
6,349✔
919
      return false;
920

921
   if (!sem_no_access_file_or_protected(t, type, "constants"))
6,349✔
922
      return false;
923

924
   tree_t fwd = find_forward_decl(tab, t);
6,344✔
925

926
   if (tree_has_value(t)) {
6,344✔
927
      tree_t value = tree_value(t);
5,942✔
928
      if (!sem_check(value, tab))
5,942✔
929
         return false;
930

931
      if (!sem_check_type(value, type, tab))
5,919✔
932
         sem_error(value, "type of initial value %s does not match type "
5✔
933
                   "of declaration %s", type_pp2(tree_type(value), type),
934
                   type_pp2(type, tree_type(value)));
935

936
      if (fwd == NULL && type_is_unconstrained(type))
5,914✔
937
         tree_set_type(t, tree_type(value));
747✔
938
   }
939
   else if (tree_kind(find_enclosing(tab, S_DESIGN_UNIT)) != T_PACKAGE)
402✔
940
      sem_error(t, "deferred constant declarations are only permitted "
1✔
941
                "in packages");
942

943
   if (fwd != NULL && !type_strict_eq(tree_type(fwd), type)) {
6,315✔
944
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
945
      diag_printf(d, "expected type %s for deferred constant %s but "
1✔
946
                  "found %s", type_pp2(tree_type(fwd), type),
947
                  istr(tree_ident(t)), type_pp2(type, tree_type(fwd)));
948
      diag_hint(d, tree_loc(fwd), "originally declared with type %s",
1✔
949
                type_pp2(tree_type(fwd), type));
950
      diag_hint(d, tree_loc(t), "type here is %s",
1✔
951
                type_pp2(type, tree_type(fwd)));
952
      diag_emit(d);
1✔
953
      return false;
1✔
954
   }
955

956
   return true;
957
}
958

959
static bool sem_check_signal_decl(tree_t t, nametab_t *tab)
5,378✔
960
{
961
   type_t type = tree_type(t);
5,378✔
962

963
   if (!sem_check_subtype(t, type, tab))
5,378✔
964
      return false;
965
   else if (type_is_none(type))
5,371✔
966
      return false;
967

968
   if (type_is_unconstrained(type)) {
5,370✔
969
      if (standard() < STD_19) {
18✔
970
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
6✔
971
         diag_printf(d, "declaration of signal %s cannot have unconstrained "
6✔
972
                     "type %s", istr(tree_ident(t)), type_pp(type));
973
         sem_unconstrained_decl_hint(d, type);
6✔
974
         diag_emit(d);
6✔
975
         return false;
6✔
976
      }
977
      else if (!tree_has_value(t)) {
12✔
978
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
979
         diag_printf(d, "declaration of signal %s without an initial value "
1✔
980
                     "cannot have unconstrained type %s",
981
                     istr(tree_ident(t)), type_pp(type));
982
         sem_unconstrained_decl_hint(d, type);
1✔
983
         diag_emit(d);
1✔
984
         return false;
1✔
985
      }
986
   }
987
   else if (!sem_check_incomplete(t, type))
5,352✔
988
      return false;
989

990
   if (!sem_no_access_file_or_protected(t, type, "signals"))
5,363✔
991
      return false;
992

993
   if (is_guarded_signal(t) && !type_is_resolved(type))
5,356✔
994
      sem_error(t, "guarded signal must have resolved subtype");
1✔
995

996
   if (tree_has_value(t)) {
5,355✔
997
      tree_t value = tree_value(t);
1,563✔
998
      if (!sem_check(value, tab))
1,563✔
999
         return false;
1000

1001
      if (!sem_check_type(value, type, tab))
1,559✔
1002
         sem_error(value, "type of initial value %s does not match type "
1✔
1003
                   "of declaration %s", type_pp2(tree_type(value), type),
1004
                   type_pp2(type, tree_type(value)));
1005

1006
      if (standard() >= STD_19 && type_is_unconstrained(type))
1,558✔
1007
         tree_set_type(t, tree_type(value));
11✔
1008
   }
1009

1010
   return true;
1011
}
1012

1013
static bool sem_check_var_decl(tree_t t, nametab_t *tab)
10,803✔
1014
{
1015
   type_t type = tree_type(t);
10,803✔
1016

1017
   if (!sem_check_subtype(t, type, tab))
10,803✔
1018
      return false;
1019
   else if (type_is_none(type))
10,800✔
1020
      return false;
1021

1022
   if (type_is_unconstrained(type)) {
10,789✔
1023
      if (standard() < STD_19) {
18✔
1024
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
5✔
1025
         diag_printf(d, "declaration of variable %s cannot have unconstrained "
5✔
1026
                     "type %s", istr(tree_ident(t)), type_pp(type));
1027
         sem_unconstrained_decl_hint(d, type);
5✔
1028
         diag_emit(d);
5✔
1029
         return false;
5✔
1030
      }
1031
      else if (!tree_has_value(t)) {
13✔
1032
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
1033
         diag_printf(d, "declaration of variable %s without an initial value "
2✔
1034
                     "cannot have unconstrained type %s", istr(tree_ident(t)),
1035
                     type_pp(type));
1036
         sem_unconstrained_decl_hint(d, type);
2✔
1037
         diag_emit(d);
2✔
1038
         return false;
2✔
1039
      }
1040
   }
1041
   else if (!sem_check_incomplete(t, type))
10,771✔
1042
      return false;
1043

1044
   if (tree_has_value(t)) {
10,781✔
1045
      if (type_kind(type) == T_PROTECTED)
1,928✔
1046
         sem_error(t, "variable %s with protected type may not have an "
1✔
1047
                   "initial value", istr(tree_ident(t)));
1048

1049
      tree_t value = tree_value(t);
1,927✔
1050
      if (!sem_check(value, tab))
1,927✔
1051
         return false;
1052

1053
      if (!sem_check_type(value, type, tab))
1,913✔
1054
         sem_error(value, "type of initial value %s does not match type "
1✔
1055
                   "of declaration %s", type_pp2(tree_type(value), type),
1056
                   type_pp2(type, tree_type(value)));
1057

1058
      if (standard() >= STD_08 && type_is_unconstrained(type))
1,912✔
1059
         tree_set_type(t, tree_type(value));
11✔
1060
   }
1061

1062
   // From VHDL-2000 onwards shared variables must be protected types
1063
   if (standard() >= STD_00) {
10,765✔
1064
      if ((tree_flags(t) & TREE_F_SHARED) && type_kind(type) != T_PROTECTED) {
6,009✔
1065
         diag_t *d = pedantic_diag(t);
7✔
1066
         if (d != NULL) {
7✔
1067
            diag_printf(d, "shared variable %s must have protected type",
7✔
1068
                        istr(tree_ident(t)));
1069
            diag_emit(d);
7✔
1070
         }
1071
      }
1072
   }
1073

1074
   if (type_is_protected(type) && standard() >= STD_19)
10,765✔
1075
      tree_set_global_flags(t, TREE_GF_INSTANCE_NAME | TREE_GF_PATH_NAME);
30✔
1076

1077
   return true;
1078
}
1079

1080
static bool sem_check_param_decl(tree_t t, nametab_t *tab)
30,173✔
1081
{
1082
   type_t type = tree_type(t);
30,173✔
1083

1084
   if (!sem_check_subtype(t, type, tab))
30,173✔
1085
      return false;
1086
   else if (!sem_check_incomplete(t, type))
30,173✔
1087
      return false;
1088

1089
   // See LRM 93 section 3.3 for restrictions
1090

1091
   const type_kind_t kind = type_base_kind(type);
30,172✔
1092
   const class_t class = tree_class(t);
30,172✔
1093
   const port_mode_t mode = tree_subkind(t);
30,172✔
1094

1095
   switch (mode) {
30,172✔
1096
   case PORT_BUFFER:
1✔
1097
      sem_error(t, "subprogram formal parameters cannot have mode BUFFER");
1✔
1098
      break;
1✔
1099
   case PORT_LINKAGE:
1✔
1100
      sem_error(t, "subprogram formal parameters cannot have mode LINKAGE");
1✔
1101
      break;
30,170✔
1102
   default:
1103
      break;
30,170✔
1104
   }
1105

1106
   if (kind == T_FILE && class != C_FILE)
30,170✔
1107
      sem_error(t, "formal parameter %s with file type must have class FILE",
1✔
1108
                istr(tree_ident(t)));
1109

1110
   if (kind != T_FILE && class == C_FILE)
30,169✔
1111
      sem_error(t, "formal parameter %s with class FILE must have file type",
1✔
1112
                istr(tree_ident(t)));
1113

1114
   if ((kind == T_ACCESS || kind == T_PROTECTED) && class != C_VARIABLE)
30,168✔
1115
      sem_error(t, "formal parameter %s with %s type must have class VARIABLE",
6✔
1116
                istr(tree_ident(t)),
1117
                kind == T_ACCESS ? "access" : "protected");
1118

1119
   if (sem_has_access(type) && class != C_VARIABLE)
30,163✔
1120
      sem_error(t, "formal parameter %s with type containing an access type "
4✔
1121
                "must have class VARIABLE", istr(tree_ident(t)));
1122

1123
   if (class == C_CONSTANT && mode != PORT_IN)
30,159✔
1124
      sem_error(t, "parameter of class CONSTANT must have mode IN");
2✔
1125

1126
   // LRM 08 section 4.2.2.3
1127
   if (class == C_SIGNAL && tree_flags(t) & TREE_F_BUS)
30,157✔
1128
      sem_error(t, "formal signal parameter declaration may "
1✔
1129
                "not include the reserved word BUS");
1130

1131
   if (mode == PORT_RECORD_VIEW || mode == PORT_ARRAY_VIEW) {
30,156✔
1132
      tree_t name = tree_value(t);
20✔
1133
      type_t view_type = tree_type(name);
20✔
1134

1135
      if (type_is_none(view_type))
20✔
1136
         return false;
1137

1138
      if (type_kind(view_type) != T_VIEW)
20✔
1139
         sem_error(name, "name in mode view indication of parameter %s does "
1✔
1140
                   "not denote a mode view", istr(tree_ident(t)));
1141

1142
      type_t elem_type = type;
19✔
1143
      if (mode == PORT_ARRAY_VIEW) {
19✔
1144
         if (!type_is_array(type))
1✔
1145
            sem_error(t, "parameter %s with array mode view indication has "
1✔
1146
                      "non-array type %s", istr(tree_ident(t)), type_pp(type));
1147

1148
         elem_type = type_elem(type);
×
1149
      }
1150

1151
      if (!type_eq(elem_type, type_designated(view_type)))
18✔
1152
         sem_error(t, "subtype %s is not compatible with mode "
1✔
1153
                   "view %s", type_pp(elem_type), type_pp(view_type));
1154
   }
1155
   else if (tree_has_value(t)) {
30,136✔
1156
      tree_t value = tree_value(t);
4,122✔
1157
      if (!sem_check(value, tab))
4,122✔
1158
         return false;
1159

1160
      if (!sem_check_type(value, type, tab))
4,121✔
1161
         sem_error(value, "type of default value %s does not match type "
2✔
1162
                   "of declaration %s", type_pp(tree_type(value)),
1163
                   type_pp(type));
1164

1165
      switch (class) {
4,119✔
1166
      case C_SIGNAL:
1✔
1167
         sem_error(t, "parameter of class SIGNAL cannot have a "
1✔
1168
                   "default value");
1169
         break;
6✔
1170

1171
      case C_VARIABLE:
6✔
1172
         if (mode == PORT_OUT || mode == PORT_INOUT)
6✔
1173
            sem_error(t, "parameter of class VARIABLE with mode OUT or "
2✔
1174
                      "INOUT cannot have a default value");
1175
         break;
1176

1177
      default:
1178
         break;
1179
      }
1180

1181
      if (!sem_globally_static(value)) {
4,116✔
1182
         diag_t *d = pedantic_diag(value);
8✔
1183
         if (d != NULL) {
8✔
1184
            diag_printf(d, "default value must be a static expression");
5✔
1185
            diag_emit(d);
5✔
1186
         }
1187
      }
1188

1189
      if (kind == T_PROTECTED)
4,116✔
1190
         sem_error(t, "parameter with protected type cannot have "
1✔
1191
                   "a default value");
1192
   }
1193

1194
   return true;
1195
}
1196

1197
static bool sem_check_port_decl(tree_t t, nametab_t *tab)
3,303✔
1198
{
1199
   type_t type = tree_type(t);
3,303✔
1200

1201
   if (type_is_none(type))
3,303✔
1202
      return false;
1203
   else if (!sem_check_subtype(t, type, tab))
3,301✔
1204
      return false;
1205
   else if (!sem_check_incomplete(t, type))
3,300✔
1206
      return false;
1207

1208
   const class_t class = tree_class(t);
3,299✔
1209
   const port_mode_t mode = tree_subkind(t);
3,299✔
1210

1211
   if (class == C_VARIABLE) {
3,299✔
1212
      if (standard() < STD_19) {
7✔
1213
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1214
         diag_printf(d, "ports may not have variable class in VHDL-%s",
1✔
1215
                     standard_text(standard()));
1216
         diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
1217
                   "feature");
1218
         diag_emit(d);
1✔
1219
         return false;
1✔
1220
      }
1221

1222
      if (mode != PORT_INOUT)
6✔
1223
         sem_error(t, "formal variable port %s must have mode INOUT",
1✔
1224
                   istr(tree_ident(t)));
1225

1226
      if (!type_is_protected(type))
5✔
1227
         sem_error(t, "formal variable port %s must have protected type",
1✔
1228
                   istr(tree_ident(t)));
1229
   }
1230
   else if (class != C_SIGNAL)
3,292✔
1231
      sem_error(t, "invalid object class %s for port %s",
2✔
1232
                class_str(class), istr(tree_ident(t)));
1233

1234
   if (type_is_access(type))
3,294✔
1235
      sem_error(t, "port %s cannot be declared with access type %s",
1✔
1236
                istr(tree_ident(t)), type_pp(type));
1237

1238
   if (sem_has_access(type))
3,293✔
1239
      sem_error(t, "port %s cannot be declared with type %s which has a "
2✔
1240
                "subelement of access type", istr(tree_ident(t)),
1241
                type_pp(type));
1242

1243
   if (class != C_VARIABLE && type_is_protected(type)) {
3,291✔
1244
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1245
      diag_printf(d, "port %s with class %s cannot be declared with "
1✔
1246
                  "protected type %s", istr(tree_ident(t)),
1247
                  class_str(class), type_pp(type));
1248
      diag_hint(d, NULL, "ports with variable class can be of protected "
1✔
1249
                "type in VHDL-2019");
1250
      diag_hint(d, NULL, "pass $bold$--std=2019$$ to enable this "
1✔
1251
                "feature");
1252
      diag_emit(d);
1✔
1253
      return false;
1✔
1254
   }
1255

1256
   if (type_is_file(type))
3,290✔
1257
      sem_error(t, "port %s cannot be declared with file type %s",
1✔
1258
                istr(tree_ident(t)), type_pp(type));
1259

1260
   if (mode == PORT_RECORD_VIEW || mode == PORT_ARRAY_VIEW) {
3,289✔
1261
      tree_t name = tree_value(t);
63✔
1262
      type_t view_type = tree_type(name);
63✔
1263

1264
      if (type_is_none(view_type))
63✔
1265
         return false;
1266

1267
      if (type_kind(view_type) != T_VIEW)
63✔
1268
         sem_error(name, "name in mode view indication of port %s does not "
×
1269
                   "denote a mode view", istr(tree_ident(t)));
1270

1271
      type_t elem_type = type;
63✔
1272
      if (mode == PORT_ARRAY_VIEW) {
63✔
1273
         if (!type_is_array(type))
16✔
1274
            sem_error(t, "port %s with array mode view indication has "
1✔
1275
                      "non-array type %s", istr(tree_ident(t)), type_pp(type));
1276

1277
         elem_type = type_elem(type);
15✔
1278
      }
1279

1280
      if (!type_eq(elem_type, type_designated(view_type)))
62✔
1281
         sem_error(t, "subtype %s is not compatible with mode "
2✔
1282
                   "view %s", type_pp(elem_type), type_pp(view_type));
1283
   }
1284
   else if (tree_has_value(t)) {
3,226✔
1285
      tree_t value = tree_value(t);
316✔
1286
      if (!sem_check(value, tab))
316✔
1287
         return false;
1288

1289
      if (mode == PORT_LINKAGE)
316✔
1290
         sem_error(t, "port with mode LINKAGE cannot have a default value");
1✔
1291

1292
      if (!sem_check_type(value, type, tab))
315✔
1293
         sem_error(value, "type of default value %s does not match type "
×
1294
                   "of declaration %s", type_pp(tree_type(value)),
1295
                   type_pp(type));
1296
   }
1297

1298
   return true;
1299
}
1300

1301
static bool sem_check_generic_decl(tree_t t, nametab_t *tab)
2,038✔
1302
{
1303
   const class_t class = tree_class(t);
2,038✔
1304
   switch (class) {
2,038✔
1305
   case C_CONSTANT:
1306
   case C_TYPE:
1307
   case C_FUNCTION:
1308
   case C_PROCEDURE:
1309
      break;
1,984✔
1310

1311
   case C_PACKAGE:
48✔
1312
      {
1313
         tree_t map = tree_value(t);
48✔
1314
         if (!tree_has_ref(map))
48✔
1315
            return false;   // Was earlier error
1316

1317
         assert(tree_kind(map) == T_PACKAGE_MAP);
47✔
1318

1319
         tree_t pack = tree_ref(map);
47✔
1320
         assert(is_uninstantiated_package(pack));
47✔
1321

1322
         switch (tree_subkind(map)) {
47✔
1323
         case PACKAGE_MAP_DEFAULT:
6✔
1324
            {
1325
               // Check each generic in the uninstantiated package has a
1326
               // default value
1327
               const int ngenerics = tree_generics(pack);
6✔
1328
               for (int i = 0; i < ngenerics; i++) {
18✔
1329
                  tree_t g = tree_generic(pack, i);
13✔
1330
                  if (!tree_has_value(g)) {
13✔
1331
                     diag_t *d = diag_new(DIAG_ERROR, tree_loc(map));
1✔
1332
                     diag_printf(d, "generic %s in package %s does not have a "
1✔
1333
                                 "default value", istr(tree_ident(g)),
1334
                                 istr(tree_ident(pack)));
1335
                     diag_hint(d, tree_loc(g), "%s declared here",
1✔
1336
                               istr(tree_ident(g)));
1337
                     diag_lrm(d, STD_08, "6.5.5");
1✔
1338

1339
                     diag_emit(d);
1✔
1340
                     return false;
1✔
1341
                  }
1342
               }
1343
            }
1344
            break;
1345

1346
         case PACKAGE_MAP_MATCHING:
9✔
1347
            sem_check_generic_map(map, pack, tab);
9✔
1348
            break;
9✔
1349

1350
         case PACKAGE_MAP_BOX:
1351
            break;
1352
         }
1353

1354
         return true;
1355
      }
1356
   default:
6✔
1357
      sem_error(t, "invalid object class %s for generic %s",
6✔
1358
                class_str(tree_class(t)), istr(tree_ident(t)));
1359
   }
1360

1361
   type_t type = tree_type(t);
1,984✔
1362

1363
   if (!sem_check_subtype(t, type, tab))
1,984✔
1364
      return false;
1365
   else if (type_is_none(type))
1,984✔
1366
      return false;
1367
   else if (!sem_check_incomplete(t, type))
1,983✔
1368
      return false;
1369

1370
   if (class != C_TYPE) {
1,982✔
1371
      if (type_is_access(type))
1,804✔
1372
         sem_error(t, "generic %s may not have access type",
1✔
1373
                   istr(tree_ident(t)));
1374
      else if (sem_has_access(type))
1,803✔
1375
         sem_error(t, "generic %s may not have a type with a subelement of "
2✔
1376
                   "access type", istr(tree_ident(t)));
1377
      else if (type_is_protected(type))
1,801✔
1378
         sem_error(t, "generic %s may not have protected type",
1✔
1379
                   istr(tree_ident(t)));
1380
      else if (type_is_file(type))
1,800✔
1381
         sem_error(t, "generic %s may not have file type", istr(tree_ident(t)));
1✔
1382
   }
1383

1384
   if (tree_has_value(t)) {
1,977✔
1385
      tree_t value = tree_value(t);
1,087✔
1386
      if (!sem_check(value, tab))
1,087✔
1387
         return false;
1388

1389
      if (!sem_check_type(value, type, tab))
1,083✔
1390
         sem_error(value, "type of default value %s does not match type "
×
1391
                   "of declaration %s", type_pp(tree_type(value)),
1392
                   type_pp(type));
1393
   }
1394

1395
   return true;
1396
}
1397

1398
static bool sem_check_field_decl(tree_t t)
3,486✔
1399
{
1400
   type_t type = tree_type(t);
3,486✔
1401

1402
   if (!sem_check_incomplete(t, type))
3,486✔
1403
      return false;
1✔
1404

1405
   return true;
1406
}
1407

1408
static bool sem_check_unit_decl(tree_t t)
1409
{
1410
   return true;
1411
}
1412

1413
static bool sem_check_alias(tree_t t, nametab_t *tab)
1,494✔
1414
{
1415
   // Rules for aliases are given in LRM 93 section 4.3.3
1416

1417
   tree_t value = tree_value(t);
1,494✔
1418
   type_t type = get_type_or_null(t);
1,494✔
1419

1420
   const tree_kind_t value_kind = tree_kind(value);
1,494✔
1421

1422
   if (type != NULL && type_is_subprogram(type)) {
1,494✔
1423
      // Alias of subprogram or enumeration literal
1424
      // Rules for matching signatures are in LRM 93 section 2.3.2
1425
      assert(tree_kind(value) == T_REF || tree_kind(value) == T_PROT_REF);
525✔
1426
      return true;
525✔
1427
   }
1428
   else if (value_kind == T_REF && tree_has_ref(value)) {
969✔
1429
      tree_t decl = tree_ref(value);
829✔
1430
      if (aliased_type_decl(decl) != NULL)
829✔
1431
         return true;   // Alias of type
1432
      else if (tree_kind(decl) == T_VIEW_DECL)
764✔
1433
         return true;   // Alias of view declaration
1434
   }
1435
   else if (value_kind == T_ATTR_REF && is_type_attribute(tree_subkind(value)))
140✔
1436
      return true;   // Alias of type
1437

1438
   // Alias of object
1439
   if (!sem_check(value, tab))
898✔
1440
      return false;
1441

1442
   if (value_kind == T_ATTR_REF && tree_subkind(value) == ATTR_CONVERSE) {
891✔
1443
      // Special case handling for
1444
      //   https://gitlab.com/IEEE-P1076/VHDL-Issues/-/issues/293
1445
      return true;
1446
   }
1447
   else if (!sem_static_name(value, sem_globally_static)) {
871✔
1448
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
6✔
1449
      diag_printf(d, "aliased name is not static");
6✔
1450
      diag_lrm(d, STD_93, "6.1");
6✔
1451
      diag_emit(d);
6✔
1452
      return false;
6✔
1453
   }
1454

1455
   if (type != NULL) {
865✔
1456
      // Alias declaration had optional subtype indication
1457

1458
      if (!sem_check_subtype(t, type, tab))
797✔
1459
         return false;
1460

1461
      if (!sem_check_type(value, type, tab))
797✔
1462
         sem_error(t, "type of aliased object %s does not match expected "
1✔
1463
                   "type %s", type_pp2(tree_type(value), type),
1464
                   type_pp2(type, tree_type(value)));
1465

1466
      if (opt_get_int(OPT_RELAXED) && type_is_unconstrained(type)) {
796✔
1467
         // If the type of the aliased object is unconstrained then
1468
         // use its subtype instead of the subtype declared by the
1469
         // alias.  This is required for some UVVM sources.
1470
         type_t obj_type = tree_type(value);
1✔
1471
         if (!type_is_unconstrained(obj_type))
1✔
1472
            tree_set_type(t, obj_type);
1✔
1473
      }
1474
   }
1475
   else
1476
      type = tree_type(value);
68✔
1477

1478
   if (standard() < STD_08 && type_is_array(type) && dimension_of(type) > 1) {
864✔
1479
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1480
      diag_printf(d, "object alias may not have multidimensional array type "
1✔
1481
                  "in VHDL-%s", standard_text(standard()));
1482
      diag_lrm(d, STD_93, "4.3.3.1");
1✔
1483
      diag_emit(d);
1✔
1484
      return false;
1✔
1485
   }
1486

1487
   return true;
1488
}
1489

1490
static bool sem_check_func_ports(tree_t t, nametab_t *tab)
1491
{
1492
   const bool pure = !(tree_flags(t) & TREE_F_IMPURE);
1493
   const bool pre_std19 = (standard() < STD_19);
1494

1495
   if (!pure && !pre_std19)
1496
      return true;   // LCS2016_002 relaxed rules for impure functions
1497

1498
   const int nports = tree_ports(t);
1499
   for (int i = 0; i < nports; i++) {
1500
      tree_t p = tree_port(t, i);
1501
      if (tree_subkind(p) != PORT_IN) {
1502
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(p));
1503
         diag_printf(d, "%sfunction parameters must have mode IN",
1504
                     pre_std19 ? "" : "pure ");
1505
         diag_hint(d, tree_loc(p), "parameter %s has mode %s",
1506
                   istr(tree_ident(p)), port_mode_str(tree_subkind(p)));
1507
         diag_lrm(d, STD_08, "4.2.2.1");
1508
         diag_emit(d);
1509
      }
1510
      else if (tree_class(p) == C_VARIABLE) {
1511
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(p));
1512
         diag_printf(d, "class of %sfunction parameters must be CONSTANT, "
1513
                     "SIGNAL, or FILE", pre_std19 ? "" : "pure ");
1514
         diag_hint(d, tree_loc(p), "parameter %s has class %s",
1515
                   istr(tree_ident(p)), class_str(tree_class(p)));
1516
         diag_lrm(d, STD_08, "4.2.2.1");
1517
         diag_emit(d);
1518
      }
1519
   }
1520

1521
   return true;
1522
}
1523

1524
static bool sem_check_protected_method(tree_t t, nametab_t *tab)
1525
{
1526
   if (standard() >= STD_19)
1527
      return true;   // Relaxed in LCS2016_04
1528

1529
   const int nports = tree_ports(t);
1530
   for (int i = 0; i < nports; i++) {
1531
      tree_t p = tree_port(t, i);
1532
      type_t type = tree_type(p);
1533

1534
      if (sem_has_access(type)) {
1535
         diag_t *d = pedantic_diag(p);
1536
         if (d != NULL) {
1537
            diag_printf(d, "parameters of protected type methods cannot be of "
1538
                        "an access type or a composite type containing an "
1539
                        "access type");
1540
            if (type_is_access(type))
1541
               diag_hint(d, tree_loc(p), "type of %s is %s which is an "
1542
                         "access type", istr(tree_ident(p)), type_pp(type));
1543
            else
1544
               diag_hint(d, tree_loc(p), "type of %s is %s which has an "
1545
                         "access type as a subelement",
1546
                         istr(tree_ident(p)), type_pp(type));
1547
            diag_lrm(d, STD_08, "5.6.2");
1548
            diag_emit(d);
1549
         }
1550
      }
1551
      else if (type_is_file(type)) {
1552
         diag_t *d = pedantic_diag(p);
1553
         if (d != NULL) {
1554
            diag_printf(d, "parameters of protected type methods cannot be of "
1555
                        "a file type");
1556
            diag_hint(d, tree_loc(p), "type of %s is %s which is a file type",
1557
                      istr(tree_ident(p)), type_pp(type));
1558
            diag_lrm(d, STD_08, "5.6.2");
1559
            diag_emit(d);
1560
         }
1561
      }
1562
   }
1563

1564
   if (tree_kind(t) == T_FUNC_DECL) {
1565
      type_t result = type_result(tree_type(t));
1566
      if (sem_has_access(result) || type_is_file(result)) {
1567
         diag_t *d = pedantic_diag(t);
1568
         if (d != NULL) {
1569
            diag_printf(d, "return type of a protected type method cannot be "
1570
                        "of a file type, access type, or a composite type with "
1571
                        "a subelement that is an access type");
1572
            diag_lrm(d, STD_08, "5.6.2");
1573
            diag_emit(d);
1574
         }
1575
      }
1576
   }
1577

1578
   return true;
1579
}
1580

1581
static bool sem_check_func_result(tree_t t)
12,910✔
1582
{
1583
   type_t result = type_result(tree_type(t));
12,910✔
1584

1585
   if (type_is_protected(result))
12,910✔
1586
      sem_error(t, "function result subtype may not denote a protected type");
1✔
1587
   else if (type_is_file(result))
12,909✔
1588
      sem_error(t, "function result subtype may not denote a file type");
1✔
1589
   else if (!sem_check_incomplete(t, result))
12,908✔
1590
      return false;
1✔
1591

1592
   return true;
1593
}
1594

1595
static bool sem_check_func_decl(tree_t t, nametab_t *tab)
1596
{
1597
   const tree_flags_t flags = tree_flags(t);
1598
   if (flags & TREE_F_PREDEFINED)
1599
      return true;
1600

1601
   if (!sem_check_func_ports(t, tab))
1602
      return false;
1603

1604
   if (!sem_check_func_result(t))
1605
      return false;
1606

1607
   if ((flags & TREE_F_PROTECTED) && !sem_check_protected_method(t, tab))
1608
      return false;
1609

1610
   return true;
1611
}
1612

1613
static bool sem_compare_interfaces(tree_t dport, tree_t bport,
12,571✔
1614
                                   int nth, tree_t body, const char *what)
1615
{
1616
   tree_flags_t dflags = tree_flags(dport);
12,571✔
1617
   tree_flags_t bflags = tree_flags(bport);
12,571✔
1618

1619
   ident_t dname = tree_ident(dport);
12,571✔
1620
   ident_t bname = tree_ident(bport);
12,571✔
1621

1622
   if (dname != bname) {
12,571✔
1623
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1624
      diag_printf(d, "%s name %s in subprogram %s body does not match "
2✔
1625
                  "name %s in declaration", what,
1626
                  istr(bname), istr(tree_ident(body)), istr(dname));
1627
      diag_hint(d, tree_loc(dport), "%s %s has name %s in specification",
2✔
1628
                ordinal_str(nth + 1), what, istr(dname));
1629
      diag_hint(d, tree_loc(bport), "%s %s has name %s in body",
2✔
1630
                ordinal_str(nth + 1), what, istr(bname));
1631
      diag_emit(d);
2✔
1632
      return false;
2✔
1633
   }
1634

1635
   type_t dtype = tree_type(dport);
12,569✔
1636
   type_t btype = tree_type(bport);
12,569✔
1637

1638
   // Do not use type_eq here as subtype must exactly match
1639
   if (!type_strict_eq(btype, dtype)) {
12,569✔
1640
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1641
     diag_printf(d, "subtype of %s %s does not match type %s in "
2✔
1642
                 "specification", what, istr(bname), type_pp(dtype));
1643
     diag_hint(d, tree_loc(dport), "%s %s declared with type %s",
2✔
1644
               what, istr(dname), type_pp(dtype));
1645
     diag_hint(d, tree_loc(bport), "%s %s declared with type %s ",
2✔
1646
               what, istr(bname), type_pp(btype));
1647
     diag_emit(d);
2✔
1648
     return false;
2✔
1649
   }
1650

1651
   const port_mode_t dmode = tree_subkind(dport);
12,567✔
1652
   const port_mode_t bmode = tree_subkind(bport);
12,567✔
1653

1654
   if (dmode != bmode) {
12,567✔
1655
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
2✔
1656
     diag_printf(d, "%s %s of subprogram body %s with mode %s does not "
2✔
1657
                 "match mode %s in specification", what, istr(dname),
1658
                 istr(tree_ident(body)), port_mode_str(bmode),
1659
                 port_mode_str(dmode));
1660
     diag_hint(d, tree_loc(dport), "%s %s declared with mode %s",
2✔
1661
               what, istr(dname), port_mode_str(dmode));
1662
     diag_hint(d, tree_loc(bport), "%s %s declared with mode %s",
2✔
1663
               what, istr(bname), port_mode_str(bmode));
1664
     diag_emit(d);
2✔
1665
     return false;
2✔
1666
   }
1667

1668
   const bool bmode_explicit = !!(bflags & TREE_F_EXPLICIT_MODE);
12,565✔
1669
   const bool dmode_explicit = !!(dflags & TREE_F_EXPLICIT_MODE);
12,565✔
1670

1671
   if (bmode_explicit != dmode_explicit) {
12,565✔
1672
      diag_t *d = pedantic_diag(bport);
1✔
1673
      if (d != NULL) {
1✔
1674
         diag_printf(d, "mode (%s) of %s %s of subprogram %s not defined "
1✔
1675
                     "equally in subprogram specification and "
1676
                     "subprogram body", port_mode_str(dmode), what,
1677
                     istr(dname), istr(tree_ident(body)));
1678

1679
         diag_hint(d, tree_loc(dport), "%s mode %sdeclared explicitly",
2✔
1680
                   what, dmode_explicit ? "" : "not ");
1681
         diag_hint(d, tree_loc(bport), "%s mode %sdeclared explicitly",
1✔
1682
                   what, bmode_explicit ? "" : "not ");
1683
         diag_emit(d);
1✔
1684
      }
1685
      return false;
1✔
1686
   }
1687

1688
   const class_t dclass = tree_class(dport);
12,564✔
1689
   const class_t bclass = tree_class(bport);
12,564✔
1690

1691
   if (dclass != bclass) {
12,564✔
1692
     diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1693
     diag_printf(d, "class %s of subprogram body %s %s %s does not "
3✔
1694
                 "match class %s in specification", class_str(bclass),
1695
                 istr(tree_ident(body)), what, istr(dname), class_str(dclass));
1696
     diag_hint(d, tree_loc(dport), "%s %s declared with class %s in "
3✔
1697
               "subprogram specification", what, istr(dname),
1698
               class_str(dclass));
1699
     diag_hint(d, tree_loc(bport), "%s %s declared with class %s in "
3✔
1700
               "subprogram body", what, istr(bname), class_str(bclass));
1701
     diag_emit(d);
3✔
1702
     return false;
3✔
1703
   }
1704

1705
   const bool bclass_explicit = !!(bflags & TREE_F_EXPLICIT_CLASS);
12,561✔
1706
   const bool dclass_explicit = !!(dflags & TREE_F_EXPLICIT_CLASS);
12,561✔
1707

1708
   if (bclass_explicit != dclass_explicit) {
12,561✔
1709
      diag_t *d = pedantic_diag(bport);
3✔
1710
      if (d != NULL) {
3✔
1711
         diag_printf(d, "class (%s) of %s %s of subprogram %s not defined "
2✔
1712
                     "equally in subprogram specification and "
1713
                     "subprogram body", class_str(dclass), what,
1714
                     istr(dname), istr(tree_ident(body)));
1715

1716
         diag_hint(d, tree_loc(dport), "%s class %sdeclared explicitly in "
3✔
1717
                   "subprogram specification", what,
1718
                   dclass_explicit ? "" : "not ");
1719
         diag_hint(d, tree_loc(bport), "%s class %sdeclared explicitly in "
3✔
1720
                   "subprogram body", what, bclass_explicit ? "" : "not ");
1721
         diag_emit(d);
2✔
1722
      }
1723
      return false;
3✔
1724
   }
1725

1726
   tree_t bdef = tree_has_value(bport) ? tree_value(bport) : NULL;
12,558✔
1727
   tree_t ddef = tree_has_value(dport) ? tree_value(dport) : NULL;
12,558✔
1728

1729
   if (bdef == NULL && ddef == NULL)
12,558✔
1730
     return true;
1731

1732
   const tree_kind_t bkind = bdef ? tree_kind(bdef) : T_LAST_TREE_KIND;
1,915✔
1733
   const tree_kind_t dkind = ddef ? tree_kind(ddef) : T_LAST_TREE_KIND;
1,915✔
1734

1735
   // Work around some mismatches caused by folding
1736
   if (bdef != NULL && ddef != NULL && bkind != dkind)
1,915✔
1737
     return true;
1738

1739
   if (dkind == bkind) {
1,890✔
1740
     // This only covers a few simple cases
1741
     switch (dkind) {
1,889✔
1742
     case T_LITERAL: {
271✔
1743
       const literal_kind_t dsub = tree_subkind(ddef);
271✔
1744
       const literal_kind_t bsub = tree_subkind(bdef);
271✔
1745
       if (dsub == bsub) {
271✔
1746
         switch (dsub) {
271✔
1747
         case L_INT:
190✔
1748
           if (tree_ival(ddef) == tree_ival(bdef))
190✔
1749
             return true;
1750
           break;
1751
         case L_REAL:
23✔
1752
           if (tree_dval(ddef) == tree_dval(bdef))
23✔
1753
             return true;
1754
           break;
1755
         default:
1756
           return true;
1757
         }
1758
       }
×
1759
     } break;
1760

1761
     case T_REF:
1,434✔
1762
     case T_FCALL:
1763
       if (!tree_has_ref(bdef) || !tree_has_ref(ddef))
1,434✔
1764
         return true; // Was parse error, ignore it
×
1765

1766
       tree_t bref = tree_ref(bdef);
1,434✔
1767
       tree_t dref = tree_ref(ddef);
1,434✔
1768

1769
       if (bref == dref)
1,434✔
1770
         return true;
1771

1772
       // Work around mismatch introduced by folding
1773
       const tree_kind_t brefkind = tree_kind(bref);
147✔
1774
       if (brefkind == T_CONST_DECL || brefkind == T_GENERIC_DECL)
147✔
1775
         return true;
1776

1777
       break;
1778

1779
     default:
1780
       return true;
1781
     }
1782
   }
1✔
1783

1784
   diag_t *d = diag_new(DIAG_ERROR, tree_loc(bport));
3✔
1785
   diag_printf(d, "default value of %s %s in subprogram body %s does not "
3✔
1786
               "match declaration", what, istr(dname), istr(tree_ident(body)));
1787
   diag_hint(d, tree_loc(dport), "parameter was originally declared here");
3✔
1788
   diag_hint(d, tree_loc(bport), "body has different default value");
3✔
1789
   diag_emit(d);
3✔
1790

1791
   return false;
3✔
1792
}
1793

1794
static bool sem_check_conforming(tree_t decl, tree_t body)
6,472✔
1795
{
1796
   // Conformance rules are in LRM 08 section 4.10
1797
   // Note we don't implement strict lexical conformance here
1798

1799
   bool ok = true;
6,472✔
1800

1801
   const bool dimpure = !!(tree_flags(decl) & TREE_F_IMPURE);
6,472✔
1802
   const bool bimpure = !!(tree_flags(body) & TREE_F_IMPURE);
6,472✔
1803

1804
   if (dimpure != bimpure) {
6,472✔
1805
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
2✔
1806
      diag_printf(d, "function %s declaration was %s but body is %s",
4✔
1807
                  istr(tree_ident(body)), dimpure ? "impure" : "pure",
1808
                  bimpure ? "impure" : "pure");
1809
      diag_hint(d, tree_loc(decl), "declaration was %s",
2✔
1810
                dimpure ? "impure" : "pure");
1811
      diag_hint(d, tree_loc(body), "expecting keyword %s to match declaration",
3✔
1812
                bimpure ? "IMPURE" : "PURE");
1813
      diag_emit(d);
2✔
1814
      ok = false;
2✔
1815
   }
1816

1817
   // This must be true or they would be considered different overloads
1818
   assert(tree_ports(decl) == tree_ports(body));
6,472✔
1819

1820
   const int nports = tree_ports(decl);
6,472✔
1821
   for (int i = 0; i < nports; i++) {
18,976✔
1822
      tree_t dport = tree_port(decl, i);
12,504✔
1823
      tree_t bport = tree_port(body, i);
12,504✔
1824
      ok &= sem_compare_interfaces(dport, bport, i, body, "parameter");
12,504✔
1825
   }
1826

1827
   const int ngenerics = tree_generics(decl);
6,472✔
1828
   if (ngenerics != tree_generics(body)) {
6,472✔
1829
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(body));
1✔
1830
      diag_printf(d, "subprogram %s declaration has %d generic%s but body "
2✔
1831
                  "has %d", istr(tree_ident(body)), ngenerics,
1832
                  ngenerics > 1 ? "s" : "", tree_generics(body));
1833
      diag_hint(d, tree_loc(decl), "declaration with %d generics", ngenerics);
1✔
1834
      diag_hint(d, tree_loc(body), "body has %d generics", tree_generics(body));
1✔
1835
      diag_emit(d);
1✔
1836
      ok = false;
1✔
1837
   }
1838
   else {
1839
      for (int i = 0; i < ngenerics; i++) {
6,538✔
1840
         tree_t dgen = tree_generic(decl, i);
67✔
1841
         tree_t bgen = tree_generic(body, i);
67✔
1842
         ok &= sem_compare_interfaces(dgen, bgen, i, body, "generic");
67✔
1843
      }
1844
   }
1845

1846
   return ok;
6,472✔
1847
}
1848

1849
static bool sem_check_func_body(tree_t t, nametab_t *tab)
7,029✔
1850
{
1851
   if (!sem_check_func_ports(t, tab))
7,029✔
1852
      return false;
1853

1854
   if (!sem_check_func_result(t))
7,029✔
1855
      return false;
1856

1857
   tree_t fwd = find_forward_decl(tab, t);
7,028✔
1858
   if (fwd != NULL && !sem_check_conforming(fwd, t))
7,028✔
1859
      return false;
13✔
1860

1861
   return true;
1862
}
1863

1864
static bool sem_check_proc_decl(tree_t t, nametab_t *tab)
1865
{
1866
   const tree_flags_t flags = tree_flags(t);
1867
   if ((flags & TREE_F_PROTECTED) && !sem_check_protected_method(t, tab))
1868
      return false;
1869

1870
   return true;
1871
}
1872

1873
static bool sem_check_proc_body(tree_t t, nametab_t *tab)
2,018✔
1874
{
1875
   tree_t fwd = find_forward_decl(tab, t);
2,018✔
1876
   if (fwd != NULL && !sem_check_conforming(fwd, t))
2,018✔
1877
      return false;
1878

1879
   // Cleared by wait statement or pcall
1880
   tree_set_flag(t, TREE_F_NEVER_WAITS);
2,013✔
1881

1882
   return true;
2,013✔
1883
}
1884

1885
static bool sem_check_subprogram_inst(tree_t t, nametab_t *tab)
91✔
1886
{
1887
   if (tree_generics(t) == 0)
91✔
1888
      return false;   // Was a parse error
1889

1890
   if (!sem_check_generic_map(t, t, tab))
85✔
1891
      return false;
1✔
1892

1893
   // Other declarations were checked on the uninstantiated subprogram
1894

1895
   return true;
1896
}
1897

1898
static bool sem_check_sensitivity(tree_t t, nametab_t *tab)
12,657✔
1899
{
1900
   const int ntriggers = tree_triggers(t);
12,657✔
1901
   for (int i = 0; i < ntriggers; i++) {
13,744✔
1902
      tree_t r = tree_trigger(t, i);
1,092✔
1903
      if (tree_kind(r) == T_ALL)
1,092✔
1904
         continue;
40✔
1905
      else if (!sem_check(r, tab) || !sem_readable(r))
1,052✔
1906
         return false;
2✔
1907

1908
      if (!sem_static_name(r, sem_globally_static))
1,050✔
1909
         sem_error(r, "name in sensitivity list is not a static signal name");
2✔
1910

1911
      if (class_of(r) != C_SIGNAL) {
1,048✔
1912
         tree_t ref = name_to_ref(r);
1✔
1913
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(r));
1✔
1914
         if (ref != NULL) {
1✔
1915
            tree_t decl = tree_ref(ref);
1✔
1916
            diag_printf(d, "name %s in sensitivity list is not a signal",
1✔
1917
                        istr(tree_ident(decl)));
1918
            diag_hint(d, tree_loc(r), "%s is a %s", istr(tree_ident(decl)),
1✔
1919
                      class_str(class_of(decl)));
1920
         }
1921
         else
1922
            diag_printf(d, "name in sensitivity list is not a signal");
×
1923
         diag_emit(d);
1✔
1924
         return false;
1✔
1925
      }
1926
   }
1927

1928
   return true;
1929
}
1930

1931
static void sem_check_static_elab(tree_t t)
36,960✔
1932
{
1933
   // LRM 93 12.3 forbirds references to signals before the design has
1934
   // been elaborated: "The value of any object denoted by a primary in
1935
   // such an expression must be defined at the time the primary is read"
1936

1937
   const tree_kind_t kind = tree_kind(t);
47,441✔
1938

1939
   switch (kind) {
47,441✔
1940
   case T_REF:
5,904✔
1941
   case T_EXTERNAL_NAME:
1942
      {
1943
         if (class_of(t) != C_SIGNAL)
5,904✔
1944
            return;
1945

1946
         ident_t id;
19✔
1947
         diag_t *d;
19✔
1948
         if (kind == T_EXTERNAL_NAME) {
19✔
1949
            id = tree_ident(tree_part(t, tree_parts(t) - 1));
1✔
1950
            d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
1951
         }
1952
         else if (!tree_has_ref(t))
18✔
1953
            return;   // Was earlier error
1954
         else {
1955
            tree_t decl = tree_ref(t);
18✔
1956
            id = tree_ident(decl);
18✔
1957
            if (tree_has_value(decl) || !type_is_unconstrained(tree_type(decl)))
18✔
1958
               d = pedantic_diag(t);
18✔
1959
            else {
1960
               d = diag_new(DIAG_ERROR, tree_loc(t));
×
1961
               diag_hint(d, NULL, "the $bold$--relaxed$$ option would "
×
1962
                         "downgrade this to a warning if %s had an initial "
1963
                         "value or its type was fully constrained", istr(id));
1964
            }
1965
         }
1966

1967
         if (d != NULL) {
19✔
1968
            diag_printf(d, "cannot reference signal %s during static "
16✔
1969
                        "elaboration", istr(id));
1970
            diag_hint(d, NULL, "the value of a signal is not defined "
16✔
1971
                      "until after the design hierarchy is elaborated");
1972
            diag_lrm(d, STD_93, "12.3");
16✔
1973
            diag_emit(d);
16✔
1974
         }
1975
      }
1976
      break;
1977

1978
   case T_SIGNAL_DECL:
6,482✔
1979
   case T_VAR_DECL:
1980
   case T_CONST_DECL:
1981
      if (tree_has_value(t))
6,482✔
1982
         sem_check_static_elab(tree_value(t));
2,770✔
1983
      // Fall-through
1984
   case T_TYPE_DECL:
1985
   case T_SUBTYPE_DECL:
1986
      {
1987
         type_t type = tree_type(t);
8,817✔
1988
         if (type_is_generic(type))
8,817✔
1989
            ;  // Cannot check further
1990
         else if (type_is_scalar(type) && !type_is_none(type))
8,797✔
1991
            sem_check_static_elab(range_of(type, 0));
3,733✔
1992
         else if (type_is_array(type) && !type_is_unconstrained(type)) {
5,064✔
1993
            const int ndims = dimension_of(type);
2,772✔
1994
            for (int i = 0; i < ndims; i++)
5,704✔
1995
               sem_check_static_elab(range_of(type, i));
2,932✔
1996
         }
1997
      }
1998
      break;
1999

2000
   case T_ARRAY_REF:
4✔
2001
      {
2002
         sem_check_static_elab(tree_value(t));
4✔
2003

2004
         const int nparams = tree_params(t);
4✔
2005
         for (int i = 0; i < nparams; i++)
8✔
2006
            sem_check_static_elab(tree_value(tree_param(t, i)));
4✔
2007
      }
2008
      break;
2009

2010
   case T_FCALL:
777✔
2011
      if (!(tree_flags(t) & TREE_F_GLOBALLY_STATIC)) {
777✔
2012
         const int nparams = tree_params(t);
45✔
2013
         for (int i = 0; i < nparams; i++)
73✔
2014
            sem_check_static_elab(tree_value(tree_param(t, i)));
28✔
2015
      }
2016
      break;
2017

2018
   case T_ARRAY_SLICE:
12✔
2019
      sem_check_static_elab(tree_value(t));
12✔
2020
      sem_check_static_elab(tree_range(t, 0));
12✔
2021
      break;
12✔
2022

2023
   case T_RECORD_REF:
58✔
2024
   case T_TYPE_CONV:
2025
      sem_check_static_elab(tree_value(t));
58✔
2026
      break;
58✔
2027

2028
   case T_RANGE:
6,677✔
2029
      switch (tree_subkind(t)) {
6,677✔
2030
      case RANGE_TO:
6,314✔
2031
      case RANGE_DOWNTO:
2032
         sem_check_static_elab(tree_left(t));
6,314✔
2033
         sem_check_static_elab(tree_right(t));
6,314✔
2034
         break;
6,314✔
2035
      case RANGE_EXPR:
363✔
2036
         sem_check_static_elab(tree_value(t));
363✔
2037
         break;
363✔
2038
      }
2039
      break;
2040

2041
   case T_ATTR_REF:
423✔
2042
      {
2043
         // Same list of predefined attributes as sem_globally_static
2044
         const attr_kind_t predef = tree_subkind(t);
423✔
2045
         const bool non_static = predef == ATTR_EVENT || predef == ATTR_ACTIVE
846✔
2046
            || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
423✔
2047
            || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
422✔
2048
            || predef == ATTR_DRIVING_VALUE;
845✔
2049

2050
         if (non_static)
423✔
2051
            sem_check_static_elab(tree_name(t));
1✔
2052
      }
2053
      break;
2054

2055
   default:
2056
      break;
2057
   }
2058
}
2059

2060
static bool sem_check_process(tree_t t, nametab_t *tab)
4,315✔
2061
{
2062
   if (!sem_check_sensitivity(t, tab))
4,315✔
2063
      return false;
2✔
2064

2065
   return true;
2066
}
2067

2068
static bool sem_check_package(tree_t t, nametab_t *tab)
1,287✔
2069
{
2070
   if (!sem_check_context_clause(t, tab))
1,287✔
2071
      return false;
2072

2073
   if (tree_genmaps(t) > 0 && !sem_check_generic_map(t, t, tab))
1,287✔
2074
      return false;
2075

2076
   // Subprogram bodies are not allowed in package specification
2077
   const int ndecls = tree_decls(t);
1,287✔
2078
   for (int i = 0; i < ndecls; i++) {
28,269✔
2079
     tree_t d = tree_decl(t, i);
26,984✔
2080
     tree_kind_t kind = tree_kind(d);
26,984✔
2081
     if ((kind == T_FUNC_BODY) || (kind == T_PROC_BODY))
26,984✔
2082
       sem_error(d, "subprogram body is not allowed in package specification");
26,984✔
2083
   }
2084

2085
   return true;
2086
}
2087

2088
static bool sem_check_pack_inst(tree_t t, nametab_t *tab)
228✔
2089
{
2090
   if (tree_generics(t) == 0)
228✔
2091
      return false;   // Was a parse error
2092

2093
   if (!sem_check_generic_map(t, t, tab))
225✔
2094
      return false;
6✔
2095

2096
   // Other declarations were checked on the uninstantiated package
2097

2098
   return true;
2099
}
2100

2101
static bool sem_check_missing_body(tree_t body, tree_t spec)
6,439✔
2102
{
2103
   // Check for any subprogram declarations or protected types without bodies
2104
   bool ok = true;
6,439✔
2105
   const int ndecls = tree_decls(spec);
6,439✔
2106
   for (int i = 0; i < ndecls; i++) {
66,197✔
2107
      tree_t d = tree_decl(spec, i);
59,758✔
2108

2109
      const tree_kind_t dkind = tree_kind(d);
59,758✔
2110
      if (dkind != T_FUNC_DECL && dkind != T_PROC_DECL && dkind != T_PROT_DECL)
59,758✔
2111
         continue;
30,809✔
2112
      else if (dkind != T_PROT_DECL && (tree_flags(d) & TREE_F_PREDEFINED))
28,949✔
2113
         continue;
22,504✔
2114

2115
      type_t dtype = tree_type(d);
6,445✔
2116

2117
      bool found = false;
6,445✔
2118
      const int nbody_decls = tree_decls(body);
6,445✔
2119
      const int start = (body == spec ? i + 1 : 0);
6,445✔
2120
      for (int j = start; !found && (j < nbody_decls); j++) {
544,754✔
2121
         tree_t b = tree_decl(body, j);
538,309✔
2122
         const tree_kind_t bkind = tree_kind(b);
538,309✔
2123
         if (bkind == T_ATTR_SPEC && is_well_known(tree_ident(b)) == W_FOREIGN
538,309✔
2124
             && tree_has_ref(b) && tree_ref(b) == d)
202✔
2125
            found = true;
2126
         else if (bkind != T_FUNC_BODY && bkind != T_PROC_BODY
538,265✔
2127
                  && bkind != T_PROT_BODY)
147,568✔
2128
            continue;
147,108✔
2129
         else if (type_eq(dtype, tree_type(b)))
391,157✔
2130
            found = true;
6,267✔
2131
      }
2132

2133
      if (!found && (dkind == T_FUNC_DECL || dkind == T_PROC_DECL)) {
6,445✔
2134
         // Check if there was a later foreign attribute declaration
2135
         for (int j = i + 1; j < ndecls; j++) {
1,716✔
2136
            tree_t d2 = tree_decl(spec, j);
1,603✔
2137
            if (tree_kind(d2) != T_ATTR_SPEC || !tree_has_ref(d2))
1,603✔
2138
               continue;
1,571✔
2139
            else if (tree_ref(d2) == d) {
32✔
2140
               found = true;
2141
               break;
2142
            }
2143
         }
2144
      }
2145

2146
      if (!found && opt_get_int(OPT_MISSING_BODY)) {
6,445✔
2147
         error_at(tree_loc(d), "missing body for %s %s",
15✔
2148
                 (dkind == T_PROT_DECL) ? "protected type"
2149
                 : (dkind == T_PROC_DECL ? "procedure" : "function"),
7✔
2150
                 type_pp(dtype));
2151
      }
2152
   }
2153

2154
   if (body != spec)
6,439✔
2155
      ok = sem_check_missing_body(body, body) && ok;
684✔
2156

2157
   return ok;
6,439✔
2158
}
2159

2160
static bool sem_check_pack_body(tree_t t, nametab_t *tab)
689✔
2161
{
2162
   if (!tree_has_primary(t))
689✔
2163
      return false;
2164

2165
   tree_t pack = tree_primary(t);
684✔
2166

2167
   if (!sem_check_context_clause(pack, tab))
684✔
2168
      return false;
2169

2170
   if (!sem_check_context_clause(t, tab))
684✔
2171
      return false;
2172

2173
   if (!sem_check_missing_body(t, pack))
684✔
2174
      return false;
2175

2176
   if (!sem_check_missing_body(t, t))
684✔
2177
      return false;
2178

2179
   // Check for any deferred constants which were not given values
2180
   const int ndecls = tree_decls(pack);
684✔
2181
   for (int i = 0; i < ndecls; i++) {
17,331✔
2182
      tree_t d = tree_decl(pack, i);
16,649✔
2183
      if ((tree_kind(d) == T_CONST_DECL) && !tree_has_value(d)) {
16,649✔
2184
         tree_t d2 = search_decls(t, tree_ident(d), 0);
396✔
2185
         if (d2 == NULL || !tree_has_value(d2))
396✔
2186
            sem_error(d, "deferred constant %s was not given a value in the "
16,649✔
2187
                      "package body", istr(tree_ident(d)));
2188
      }
2189
   }
2190

2191
   return true;
2192
}
2193

2194
static bool sem_check_component(tree_t t, nametab_t *tab)
2195
{
2196
   return true;
2197
}
2198

2199
static void sem_passive_cb(tree_t t, void *context)
1✔
2200
{
2201
   tree_t s = context;
1✔
2202

2203
   diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
2204
   diag_printf(d, "signal assignment statement not allowed inside passive "
1✔
2205
               "process");
2206
   diag_hint(d, tree_loc(s), "process in entity statement part must "
1✔
2207
             "be passive");
2208
   diag_hint(d, tree_loc(t), "signal assignment statement");
1✔
2209
   diag_lrm(d, STD_93, "1.1.3");
1✔
2210
   diag_lrm(d, STD_93, "9.2");
1✔
2211

2212
   diag_emit(d);
1✔
2213
}
1✔
2214

2215
static bool sem_check_entity(tree_t t, nametab_t *tab)
4,383✔
2216
{
2217
   if (!sem_check_context_clause(t, tab))
4,383✔
2218
      return false;
2219

2220
   // All processes in entity statement part must be passive
2221
   const int nstmts = tree_stmts(t);
4,383✔
2222
   for (int i = 0; i < nstmts; i++) {
4,422✔
2223
      tree_t s = tree_stmt(t, i);
39✔
2224
      tree_visit_only(s, sem_passive_cb, s, T_SIGNAL_ASSIGN);
39✔
2225
   }
2226

2227
   return true;
2228
}
2229

2230
static bool sem_check_arch(tree_t t, nametab_t *tab)
4,390✔
2231
{
2232
   if (!tree_has_primary(t))
4,390✔
2233
      return false;
2234

2235
   if (!sem_check_context_clause(t, tab))
4,387✔
2236
      return false;
2237

2238
   const int ndecls = tree_decls(t);
4,387✔
2239
   for (int n = 0; n < ndecls; n++) {
27,524✔
2240
      tree_t d = tree_decl(t, n);
23,137✔
2241
      sem_check_static_elab(d);
23,137✔
2242
   }
2243

2244
   if (!sem_check_missing_body(t, t))
4,387✔
2245
      return false;
×
2246

2247
   return true;
2248
}
2249

2250
static tree_t sem_check_lvalue(tree_t t)
22,577✔
2251
{
2252
   switch (tree_kind(t)) {
52,059✔
2253
   case T_REF:
22,645✔
2254
      return sem_check_lvalue(tree_ref(t));
22,645✔
2255
   case T_ARRAY_SLICE:
6,837✔
2256
   case T_ARRAY_REF:
2257
   case T_ALIAS:
2258
   case T_RECORD_REF:
2259
   case T_ALL:
2260
      return sem_check_lvalue(tree_value(t));
6,837✔
2261
   case T_VAR_DECL:
2262
   case T_SIGNAL_DECL:
2263
   case T_PORT_DECL:
2264
   case T_CONST_DECL:
2265
   case T_IMPLICIT_SIGNAL:
2266
   case T_PARAM_DECL:
2267
   case T_EXTERNAL_NAME:
2268
      return t;
2269
   default:
5✔
2270
      return NULL;
5✔
2271
   }
2272
}
2273

2274
static bool sem_check_aggregate_target_element(tree_t a, type_t type)
336✔
2275
{
2276
   tree_t value = tree_value(a);
336✔
2277

2278
   if (tree_kind(value) != T_AGGREGATE) {
336✔
2279
      if (!sem_static_name(value, sem_locally_static))
318✔
2280
         sem_error(value, "aggregate element must be locally static name");
2✔
2281
   }
2282

2283
   const assoc_kind_t kind = tree_subkind(a);
334✔
2284
   switch (kind) {
334✔
2285
   case A_RANGE:
2✔
2286
      if (standard() >= STD_08) {
2✔
2287
         // LRM 08 section 10.6.2.1: it is an error if the element
2288
         // association contains a choice that is a discrete range and
2289
         // an expression of a type other than the aggregate type.
2290
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
2291
         diag_printf(d, "range choice expression must have same type "
1✔
2292
                     "as aggregate");
2293
         diag_hint(d, tree_loc(value), "expression type is %s but "
1✔
2294
                   "aggregate is %s", type_pp(tree_type(value)),
2295
                   type_pp(type));
2296
         diag_lrm(d, STD_08, "10.6.2");
1✔
2297
         diag_emit(d);
1✔
2298
         return false;
1✔
2299
      }
2300
      // Fall-through
2301
   case A_OTHERS:
2302
      sem_error(a, "%s association not allowed in aggregate target",
4✔
2303
                assoc_kind_str(kind));
2304
   case A_NAMED:
2305
   case A_POS:
2306
   case A_SLICE:
2307
   case A_CONCAT:
2308
      break;
2309
   }
2310

2311
   return true;
2312
}
2313

2314
static bool sem_check_variable_target(tree_t target)
16,076✔
2315
{
2316
   if (tree_kind(target) == T_AGGREGATE) {
16,076✔
2317
      // Rules for aggregate variable targets in LRM 93 section 8.5
2318

2319
      type_t type = tree_type(target);
70✔
2320
      if (!type_is_composite(type))
70✔
2321
         sem_error(target, "aggregate target of variable assignment has "
×
2322
                   "non-composite type %s", type_pp(tree_type(target)));
2323

2324
      const int nassocs = tree_assocs(target);
70✔
2325
      for (int i = 0; i < nassocs; i++) {
221✔
2326
         tree_t a = tree_assoc(target, i);
156✔
2327
         tree_t value = tree_value(a);
156✔
2328

2329
         if (!sem_check_variable_target(value))
156✔
2330
            return false;
2331

2332
         if (!sem_check_aggregate_target_element(a, type))
155✔
2333
            return false;
2334
      }
2335
   }
2336
   else {
2337
      tree_t decl = sem_check_lvalue(target);
16,006✔
2338
      const tree_kind_t kind = decl ? tree_kind(decl) : T_LAST_TREE_KIND;
16,006✔
2339

2340
      const bool suitable = kind == T_VAR_DECL
32,011✔
2341
         || (kind == T_PARAM_DECL && tree_class(decl) == C_VARIABLE)
2,573✔
2342
         || (kind == T_EXTERNAL_NAME && tree_class(decl) == C_VARIABLE);
16,012✔
2343

2344
      if (!suitable) {
16,006✔
2345
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
6✔
2346
         diag_printf(d, "target of variable assignment must be a variable "
6✔
2347
                     "name or aggregate");
2348

2349
         tree_t ref = name_to_ref(target);
6✔
2350
         if (ref != NULL && tree_has_ref(ref))
6✔
2351
            diag_hint(d, tree_loc(target), "%s is a %s", istr(tree_ident(ref)),
5✔
2352
                      class_str(class_of(tree_ref(ref))));
2353

2354
         diag_emit(d);
6✔
2355
         return false;
6✔
2356
      }
2357
      else if (kind == T_PARAM_DECL && tree_subkind(decl) == PORT_IN) {
16,000✔
2358
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2359
         diag_printf(d, "cannot assign to parameter %s with mode IN",
1✔
2360
                     istr(tree_ident(decl)));
2361
         diag_hint(d, tree_loc(decl), "%s declared with mode IN",
1✔
2362
                   istr(tree_ident(decl)));
2363
         diag_hint(d, tree_loc(target), "target of variable assignment");
1✔
2364
         diag_emit(d);
1✔
2365
         return false;
1✔
2366
      }
2367
      else if (type_is_protected(tree_type(target)))
15,999✔
2368
         sem_error(target, "may not assign to variable of a protected type");
1✔
2369
   }
2370

2371
   return true;
2372
}
2373

2374
static bool sem_check_var_assign(tree_t t, nametab_t *tab)
16,029✔
2375
{
2376
   tree_t target = tree_target(t);
16,029✔
2377
   tree_t value = tree_value(t);
16,029✔
2378

2379
   if (!sem_check(target, tab))
16,029✔
2380
      return false;
2381

2382
   if (!sem_check(value, tab))
16,008✔
2383
      return false;
2384

2385
   if (!sem_readable(value))
15,921✔
2386
      return false;
2387

2388
   if (!sem_check_variable_target(target))
15,920✔
2389
      return false;
2390

2391
   if (!sem_check_same_type(value, target)) {
15,908✔
2392
      type_t target_type = tree_type(target);
11✔
2393
      type_t value_type  = tree_type(value);
11✔
2394
      sem_error(t, "type of value %s does not match type of target %s",
11✔
2395
                type_pp2(value_type, target_type),
2396
                type_pp2(target_type, value_type));
2397
   }
2398

2399
   return true;
2400
}
2401

2402
static bool sem_check_waveforms(tree_t t, tree_t target, nametab_t *tab)
6,479✔
2403
{
2404
   type_t std_time = std_type(NULL, STD_TIME);
6,479✔
2405
   type_t expect = tree_type(target);
6,479✔
2406

2407
   const int nwaves = tree_waveforms(t);
6,479✔
2408
   for (int i = 0; i < nwaves; i++) {
13,109✔
2409
      tree_t waveform = tree_waveform(t, i);
6,646✔
2410

2411
      if (tree_has_value(waveform)) {
6,646✔
2412
         tree_t value = tree_value(waveform);
6,632✔
2413

2414
         if (!sem_check(value, tab))
6,632✔
2415
            return false;
2416

2417
         if (!sem_readable(value))
6,625✔
2418
            return false;
2419

2420
         if (!sem_check_type(value, expect, tab))
6,624✔
2421
            sem_error(t, "type of value %s does not match type of target %s",
6✔
2422
                      type_pp2(tree_type(value), expect),
2423
                      type_pp2(expect, tree_type(value)));
2424
      }
2425
      else {
2426
         tree_t decl = sem_check_lvalue(target);
14✔
2427
         if (decl != NULL && !is_guarded_signal(decl))
14✔
2428
            sem_error(waveform, "a null waveform element is only valid when "
2✔
2429
                      "the target is a guarded signal");
2430
      }
2431

2432
      if (tree_has_delay(waveform)) {
6,630✔
2433
         tree_t delay = tree_delay(waveform);
675✔
2434
         if (!sem_check(delay, tab))
675✔
2435
            return false;
2436

2437
         if (!sem_check_type(delay, std_time, tab))
675✔
2438
            sem_error(delay, "type of delay must be %s but have %s",
6,630✔
2439
                      type_pp(std_time), type_pp(tree_type(delay)));
2440
      }
2441
   }
2442

2443
   return true;
2444
}
2445

2446
static tree_t sem_check_view_target(tree_t target)
233✔
2447
{
2448
   switch (tree_kind(target)) {
233✔
2449
   case T_REF:
143✔
2450
      {
2451
         tree_t decl = tree_ref(target);
143✔
2452
         if (tree_kind(decl) == T_PORT_DECL) {
143✔
2453
            const port_mode_t mode = tree_subkind(decl);
68✔
2454
            if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
68✔
2455
               return tree_value(decl);
68✔
2456
         }
2457

2458
         return NULL;
2459
      }
2460

2461
   case T_ARRAY_REF:
17✔
2462
   case T_ARRAY_SLICE:
2463
      return sem_check_view_target(tree_value(target));
17✔
2464

2465
   case T_RECORD_REF:
73✔
2466
      {
2467
         tree_t view = sem_check_view_target(tree_value(target));
73✔
2468
         if (view == NULL)
73✔
2469
            return NULL;
2470

2471
         bool converse = false;
55✔
2472
         tree_t f = tree_ref(target);
55✔
2473
         tree_t e = find_element_mode_indication(view, f, &converse);
55✔
2474
         if (e == NULL)
55✔
2475
            return NULL;
2476

2477
         if (converse_mode(e, converse) == PORT_IN) {
55✔
2478
            tree_t port = tree_ref(name_to_ref(target));
2✔
2479
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
2✔
2480
            diag_printf(d, "cannot assign to element %s of port %s which has "
2✔
2481
                        "mode IN from mode view indication",
2482
                        istr(tree_ident(e)), istr(tree_ident(port)));
2483
            diag_hint(d, tree_loc(target), "target of signal assignment");
2✔
2484
            diag_hint(d, tree_loc(port), "sub-element %s of %s declared with "
2✔
2485
                      "mode IN due to mode view indication",
2486
                      istr(tree_ident(e)), istr(tree_ident(port)));
2487
            diag_emit(d);
2✔
2488
            return NULL;
2✔
2489
         }
2490

2491
         return NULL;
2492
      }
2493

2494
   default:
2495
      return NULL;
2496
   }
2497
}
2498

2499
static bool sem_check_signal_target(tree_t target, nametab_t *tab)
6,543✔
2500
{
2501
   if (tree_kind(target) == T_AGGREGATE) {
6,543✔
2502
      // Rules for aggregate signal targets in LRM 93 section 8.4
2503

2504
      type_t type = tree_type(target);
75✔
2505
      if (!type_is_composite(type))
75✔
2506
         sem_error(target, "aggregate target of signal assignment has "
×
2507
                   "non-composite type %s", type_pp(tree_type(target)));
2508

2509
      const int nassocs = tree_assocs(target);
75✔
2510
      for (int i = 0; i < nassocs; i++) {
253✔
2511
         tree_t a = tree_assoc(target, i);
185✔
2512
         tree_t value = tree_value(a);
185✔
2513

2514
         if (!sem_check_signal_target(value, tab))
185✔
2515
            return false;
2516

2517
         if (!sem_check_aggregate_target_element(a, type))
181✔
2518
            return false;
2519
      }
2520

2521
      return true;
2522
   }
2523
   else {
2524
      tree_t decl = sem_check_lvalue(target);
6,468✔
2525
      if (decl == NULL)
6,468✔
2526
         sem_error(target, "target of signal assignment must be a signal "
3✔
2527
                   "name or aggregate");
2528

2529
      switch (tree_kind(decl)) {
6,465✔
2530
      case T_SIGNAL_DECL:
4,963✔
2531
         {
2532
            tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
4,963✔
2533
            if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL) {
4,963✔
2534
               // LRM 08 section 10.5.2.2: if a signal assignment appears
2535
               // in a procedure not contained within a process then the
2536
               // target must be a formal parameter
2537
               sem_error(target, "signal %s is not a formal parameter and "
3✔
2538
                         "subprogram %s is not contained within a process "
2539
                         "statement", istr(tree_ident(decl)),
2540
                         type_pp(tree_type(sub)));
2541
            }
2542
         }
2543
         break;
2544

2545
      case T_IMPLICIT_SIGNAL:
1✔
2546
         sem_error(target, "implicit signal may not be assigned");
1✔
2547

2548
      case T_PORT_DECL:
1,476✔
2549
      case T_PARAM_DECL:
2550
         {
2551
            const port_mode_t mode = tree_subkind(decl);
1,476✔
2552
            if (mode == PORT_IN) {
1,476✔
2553
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
5✔
2554
               diag_printf(d, "cannot assign to input %s %s",
11✔
2555
                           tree_kind(decl) == T_PORT_DECL
5✔
2556
                           ? "port" : "parameter",
2557
                           istr(tree_ident(decl)));
2558
               diag_hint(d, tree_loc(target), "target of signal assignment");
5✔
2559
               diag_hint(d, tree_loc(decl), "%s declared with mode IN",
5✔
2560
                         istr(tree_ident(decl)));
2561
               diag_emit(d);
5✔
2562
               return false;
5✔
2563
            }
2564
            else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW) {
1,471✔
2565
               tree_t view = sem_check_view_target(target);
71✔
2566
               if (view != NULL) {
71✔
2567
                  tree_t inport = NULL;
1✔
2568
                  type_t view_type = tree_type(view);
1✔
2569
                  const int nelems = type_fields(view_type);
1✔
2570
                  for (int i = 0; i < nelems; i++) {
1✔
2571
                     tree_t e = type_field(view_type, i);
1✔
2572
                     const port_mode_t mode = tree_subkind(e);
1✔
2573
                     if (mode == PORT_IN || mode == PORT_ARRAY_VIEW
1✔
2574
                         || mode == PORT_RECORD_VIEW) {
×
2575
                        // This is not correct for nested mode view
2576
                        // indications but seems like a very obscure
2577
                        // corner case
2578
                        inport = e;
2579
                        break;
2580
                     }
2581
                  }
2582

2583
                  if (inport != NULL) {
1✔
2584
                     diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2585
                     diag_printf(d, "cannot assign to port %s with mode view "
1✔
2586
                                 "indication as one or more sub-elements have "
2587
                                 "mode IN", istr(tree_ident(decl)));
2588
                     diag_hint(d, tree_loc(target),
1✔
2589
                               "target of signal assignment");
2590
                     diag_hint(d, tree_loc(inport),
1✔
2591
                               "element %s declared with mode IN",
2592
                               istr(tree_ident(inport)));
2593
                     diag_emit(d);
1✔
2594
                     return false;
1✔
2595
                  }
2596
               }
2597

2598
               return true;
70✔
2599
            }
2600
            else if (mode == PORT_LINKAGE)
1,400✔
2601
               sem_error(target, "linkage port %s may not be updated except as "
1✔
2602
                         "an actual corresponding to an interface of mode "
2603
                         "linkage", istr(tree_ident(decl)));
2604
            else if (tree_class(decl) != C_SIGNAL) {
1,399✔
2605
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2606
               diag_printf(d, "%s is not a valid target of signal assignment",
1✔
2607
                           istr(tree_ident(decl)));
2608
               diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
2609
               diag_hint(d, tree_loc(decl), "declared with class %s",
1✔
2610
                         class_str(tree_class(decl)));
2611
               diag_emit(d);
1✔
2612
               return false;
1✔
2613
            }
2614
         }
2615
         break;
2616

2617
      case T_EXTERNAL_NAME:
24✔
2618
         {
2619
            if (tree_class(decl) != C_SIGNAL) {
24✔
2620
               tree_t tail = tree_part(decl, tree_parts(decl) - 1);
×
2621
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
×
UNCOV
2622
               diag_printf(d, "external name %s is not a valid target of "
×
2623
                           "signal assignment", istr(tree_ident(tail)));
2624
               diag_hint(d, tree_loc(target), "target of signal assignment");
×
UNCOV
2625
               diag_hint(d, tree_loc(decl), "declared with class %s",
×
2626
                         class_str(tree_class(decl)));
2627
               diag_emit(d);
×
UNCOV
2628
               return false;
×
2629
            }
2630

2631
            tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
24✔
2632
            if (sub != NULL && find_enclosing(tab, S_PROCESS) == NULL)
24✔
2633
               sem_error(target, "cannot create driver for external name as "
1✔
2634
                         "subprogram %s is not contained within a process "
2635
                         "statement", type_pp(tree_type(sub)));
2636
         }
2637
         break;
2638

2639
      case T_VAR_DECL:
1✔
2640
      case T_CONST_DECL:
2641
         {
2642
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
2643
            diag_printf(d, "%s %s is not a valid target of signal assignment",
1✔
2644
                        class_str(class_of(decl)), istr(tree_ident(decl)));
2645
            diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
2646
            diag_hint(d, tree_loc(decl), "declared as %s",
1✔
2647
                      class_str(class_of(decl)));
2648
            diag_emit(d);
1✔
2649
            return false;
1✔
2650
         }
2651

UNCOV
2652
      default:
×
UNCOV
2653
         sem_error(target, "invalid target of signal assignment");
×
2654
      }
2655

2656
      return true;
6,381✔
2657
   }
2658
}
2659

2660
static bool sem_check_reject(tree_t t, nametab_t *tab)
509✔
2661
{
2662
   if (!sem_check(t, tab))
509✔
2663
      return false;
2664

2665
   if (!type_eq(tree_type(t), std_type(NULL, STD_TIME)))
509✔
2666
      sem_error(t, "reject interval must have type TIME but have %s",
1✔
2667
                type_pp(tree_type(t)));
2668

2669
   return true;
2670
}
2671

2672
static bool sem_check_signal_assign(tree_t t, nametab_t *tab)
4,526✔
2673
{
2674
   tree_t target = tree_target(t);
4,526✔
2675

2676
   if (!sem_check(target, tab))
4,526✔
2677
      return false;
2678

2679
   if (!sem_check_signal_target(target, tab))
4,523✔
2680
      return false;
2681

2682
   if (!sem_check_waveforms(t, target, tab))
4,509✔
2683
      return false;
2684

2685
   if (tree_has_reject(t) && !sem_check_reject(tree_reject(t), tab))
4,497✔
UNCOV
2686
      return false;
×
2687

2688
   return true;
2689
}
2690

2691
static bool sem_check_guard(tree_t t, nametab_t *tab)
17✔
2692
{
2693
   assert(tree_kind(t) == T_GUARD);
17✔
2694

2695
   if (!sem_check_type(t, std_type(NULL, STD_BOOLEAN), tab))
17✔
2696
      sem_error(t, "guard signal must have BOOLEAN type but found %s",
1✔
2697
                type_pp(tree_type(t)));
2698

2699
   tree_t decl = tree_ref(t);
16✔
2700
   switch (tree_kind(decl)) {
16✔
2701
   case T_SIGNAL_DECL:
2702
   case T_IMPLICIT_SIGNAL:
2703
      break;
UNCOV
2704
   case T_PORT_DECL:
×
UNCOV
2705
      if (tree_class(decl) == C_SIGNAL)
×
2706
         break;
2707
      // Fall-through
2708
   default:
2709
      {
2710
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
2711
         diag_printf(d, "assignment guard must be a signal");
1✔
2712
         diag_hint(d, tree_loc(decl), "%s is a %s", istr(tree_ident(decl)),
1✔
2713
                   class_str(class_of(decl)));
2714
         diag_hint(d, tree_loc(t), "guarded statement");
1✔
2715
         diag_emit(d);
1✔
2716
         return false;
1✔
2717
      }
2718
   }
2719

UNCOV
2720
   return true;
×
2721
}
2722

2723
static bool sem_check_cond_assign(tree_t t, nametab_t *tab)
1,838✔
2724
{
2725
   tree_t target = tree_target(t);
1,838✔
2726

2727
   if (!sem_check(target, tab))
1,838✔
2728
      return false;
2729

2730
   if (!sem_check_signal_target(target, tab))
1,835✔
2731
      return false;
2732

2733
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
1,829✔
2734

2735
   const int nconds = tree_conds(t);
1,829✔
2736
   for (int i = 0; i < nconds; i++) {
3,795✔
2737
      tree_t c = tree_cond(t, i);
1,972✔
2738

2739
      if (tree_has_value(c)) {
1,972✔
2740
         tree_t test = tree_value(c);
190✔
2741

2742
         if (!sem_check(test, tab))
190✔
2743
            return false;
2744

2745
         if (!type_eq(tree_type(test), std_bool))
190✔
2746
            sem_error(test, "type of condition must be BOOLEAN");
1✔
2747
      }
2748

2749
      assert(tree_stmts(c) == 1);
1,971✔
2750
      tree_t a = tree_stmt(c, 0);
1,971✔
2751

2752
      assert(tree_kind(a) == T_SIGNAL_ASSIGN);
1,971✔
2753
      assert(tree_target(a) == target);
1,971✔
2754

2755
      if (tree_has_reject(a) && !sem_check_reject(tree_reject(a), tab))
1,971✔
2756
         return false;
2757

2758
      if (!sem_check_waveforms(a, target, tab))
1,970✔
2759
         return false;
2760
   }
2761

2762
   return true;
2763
}
2764

2765
static bool sem_check_closely_related(type_t from, type_t to, tree_t where)
8,726✔
2766
{
2767
   if (type_eq(to, from))
8,726✔
2768
      return true;
2769

2770
   // Conversions are allowed between any abstract numeric types
2771
   if (type_is_numeric(from) && type_is_numeric(to))
4,524✔
2772
      return true;
2773

2774
   // Suppress cascading errors
2775
   if (type_is_none(from) || type_is_none(to))
2,358✔
2776
      return true;
1✔
2777

2778
   char *reason = NULL;
2,357✔
2779

2780
   if (type_is_array(from) && type_is_array(to)) {
2,357✔
2781
      const int from_dims = dimension_of(from);
2,332✔
2782
      const int to_dims = dimension_of(to);
2,332✔
2783

2784
      // Types must have same dimensionality
2785
      if (from_dims != to_dims) {
2,332✔
2786
         reason = xasprintf("%s has %d dimension%s but %s has %d",
1✔
2787
                            type_pp2(from, to), from_dims,
2788
                            from_dims == 1 ? "" : "s",
2789
                            type_pp2(to, from), to_dims);
2790
         goto not_closely_related;
1✔
2791
      }
2792

2793
      // Index types the same or closely related
2794
      for (int i = 0; i < from_dims; i++) {
4,662✔
2795
         type_t from_index = index_type_of(from, i);
2,332✔
2796
         type_t to_index = index_type_of(to, i);
2,332✔
2797

2798
         if (!sem_check_closely_related(from_index, to_index, NULL)) {
2,332✔
2799
            reason = xasprintf("%s index type of %s is %s which is not closely "
1✔
2800
                               "related to the %s index type of %s",
2801
                               ordinal_str(i + 1), type_pp2(from, to),
2802
                               type_pp(from_index), ordinal_str(i + 1),
2803
                               type_pp2(to, from));
2804
            goto not_closely_related;
1✔
2805
         }
2806
      }
2807

2808
      type_t from_e = type_elem(from);
2,330✔
2809
      type_t to_e = type_elem(to);
2,330✔
2810

2811
      if (standard() >= STD_08) {
2,330✔
2812
         // Element types must be closely related
2813
         if (!sem_check_closely_related(from_e, to_e, NULL)) {
1,760✔
2814
            reason = xasprintf("element type %s is not closely related to %s",
1✔
2815
                               type_pp2(from_e, to_e), type_pp2(to_e, from_e));
2816
            goto not_closely_related;
1✔
2817
         }
2818
      }
2819
      else {
2820
         // Element types must be the same
2821
         if (!type_eq(from_e, to_e)) {
570✔
2822
            reason = xasprintf("element type %s does not match %s",
1✔
2823
                               type_pp2(from_e, to_e), type_pp2(to_e, from_e));
2824
            goto not_closely_related;
1✔
2825
         }
2826
      }
2827

2828
      return true;
2,328✔
2829
   }
2830

2831
   if (type_is_record(from) && type_is_record(to) && standard() >= STD_19) {
25✔
2832
      // Each element of the target type must have a matching element in
2833
      // the from type
2834
      const int from_nf = type_fields(from);
21✔
2835
      const int to_nf = type_fields(to);
21✔
2836

2837
      for (int i = 0; i < to_nf; i++) {
68✔
2838
         tree_t to_f = type_field(to, i), from_f = NULL;
48✔
2839
         type_t to_ftype = tree_type(to_f);
48✔
2840
         ident_t name = tree_ident(to_f);
48✔
2841

2842
         for (int j = 0; j < from_nf && from_f == NULL; j++) {
130✔
2843
            tree_t f = type_field(from, j);
82✔
2844
            if (tree_ident(f) != name)
82✔
2845
               continue;
35✔
2846

2847
            type_t from_ftype = tree_type(f);
47✔
2848
            if (!sem_check_closely_related(from_ftype, to_ftype, NULL))
47✔
2849
               break;
2850

2851
            from_f = f;
2852
         }
2853

2854
         if (from_f != NULL)
48✔
2855
            continue;
47✔
2856

2857
         reason = xasprintf("field %s in record type %s has no matching "
1✔
2858
                            "element in type %s", istr(tree_ident(to_f)),
2859
                            type_pp2(to, from), type_pp2(from, to));
2860
         goto not_closely_related;
1✔
2861
      }
2862

2863
      return true;
2864
   }
2865

2866
 not_closely_related:
4✔
2867
   if (where != NULL) {
9✔
2868
      const loc_t *loc = tree_loc(where);
7✔
2869
      diag_t *d = diag_new(DIAG_ERROR, loc);
7✔
2870
      diag_printf(d, "conversion only allowed between closely related types");
7✔
2871
      if (reason != NULL)
7✔
2872
         diag_hint(d, loc, "%s", reason);
5✔
2873
      else
2874
         diag_hint(d, loc, "%s and %s are not closely related",
2✔
2875
                   type_pp2(from, to), type_pp2(to, from));
2876
      diag_lrm(d, STD_93, "7.3.5");
7✔
2877
      diag_emit(d);
7✔
2878

2879
      free(reason);
7✔
2880
   }
2881

2882
   return false;
2883
}
2884

2885
static bool sem_check_conversion(tree_t t, nametab_t *tab)
4,587✔
2886
{
2887
   // Type conversions are described in LRM 93 section 7.3.5
2888

2889
   tree_t value = tree_value(t);
4,587✔
2890
   if (!sem_check(value, tab))
4,587✔
2891
      return false;
2892

2893
   return sem_check_closely_related(tree_type(value), tree_type(t), t);
4,587✔
2894
}
2895

2896
static bool sem_check_compatible_view(tree_t formal, tree_t actual)
72✔
2897
{
2898
   type_t type = tree_type(formal);
72✔
2899

2900
   type_t elem_type = type;
72✔
2901
   if (tree_subkind(formal) == PORT_ARRAY_VIEW)
72✔
2902
      elem_type = type_elem(type);
12✔
2903

2904
   tree_t formal_view = tree_value(formal);
72✔
2905

2906
   tree_t actual_view = sem_check_view_target(actual);
72✔
2907
   if (actual_view != NULL) {
72✔
2908
      // Associating an interface with another interface: check the
2909
      // mode of each element is compatible
2910
      const int nfields = type_fields(elem_type);
12✔
2911
      for (int i = 0; i < nfields; i++) {
40✔
2912
         tree_t f = type_field(elem_type, i);
30✔
2913

2914
         bool formal_converse = false;
30✔
2915
         tree_t formal_elem = find_element_mode_indication(formal_view, f,
30✔
2916
                                                           &formal_converse);
2917

2918
         bool actual_converse = false;
30✔
2919
         tree_t actual_elem = find_element_mode_indication(actual_view, f,
30✔
2920
                                                           &actual_converse);
2921

2922
         const port_mode_t formal_mode =
30✔
2923
            converse_mode(formal_elem, formal_converse);
30✔
2924

2925
         const port_mode_t actual_mode =
30✔
2926
            converse_mode(actual_elem, actual_converse);
30✔
2927

2928
         if (formal_mode != actual_mode) {
30✔
2929
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(actual));
2✔
2930
            diag_printf(d, "mode view indication of formal %s %s "
5✔
2931
                        "element %s is not compatible with actual",
2932
                        tree_kind(formal) == T_PORT_DECL ? "port" : "parameter",
2✔
2933
                        istr(tree_ident(formal)), istr(tree_ident(f)));
2934
            diag_hint(d, tree_loc(actual_view), "actual has mode %s from "
2✔
2935
                      "mode view indication on port %s",
2936
                      port_mode_str(actual_mode),
2937
                      istr(tree_ident(name_to_ref(actual))));
2938
            diag_hint(d, tree_loc(formal_view), "formal has mode %s",
2✔
2939
                      port_mode_str(formal_mode));
2940
            diag_emit(d);
2✔
2941
            return false;
2✔
2942
         }
2943
      }
2944
   }
2945

2946
   return true;
2947
}
2948

2949
static bool sem_check_call_args(tree_t t, tree_t decl, nametab_t *tab)
87,792✔
2950
{
2951
   const int nparams = tree_params(t);
87,792✔
2952
   const int nports  = tree_ports(decl);
87,792✔
2953

2954
   if (is_uninstantiated_subprogram(decl)) {
87,792✔
2955
      // Allow recursive calls to the same subprogram
2956
      tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
5✔
2957
      if (sub != decl)
5✔
2958
         sem_error(t, "cannot call uninstantiated %s %s",
2✔
2959
                   class_str(class_of(decl)), istr(tree_ident(decl)));
2960
   }
2961

2962
   tree_t *map LOCAL = xcalloc_array(nports, sizeof(tree_t));
175,580✔
2963

2964
   bool have_named = false;
87,790✔
2965
   for (int i = 0; i < nparams; i++) {
249,189✔
2966
      tree_t param = tree_param(t, i), port = NULL;
161,466✔
2967
      type_t port_type = NULL;
161,466✔
2968
      bool partial = false;
161,466✔
2969
      int index = -1;
161,466✔
2970
      switch (tree_subkind(param)) {
161,466✔
2971
      case P_POS:
158,019✔
2972
         if (have_named)
158,019✔
2973
            sem_error(param, "positional parameters must precede named "
3✔
2974
                      "parameters");
2975
         else if ((index = tree_pos(param)) >= nports) {
158,016✔
2976
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(param));
1✔
2977
            diag_printf(d, "too many positional parameters for subprogram %s",
1✔
2978
                        type_pp(tree_type(decl)));
2979
            diag_hint(d, tree_loc(param), "%s positional parameter",
1✔
2980
                      ordinal_str(index + 1));
2981
            diag_hint(d, tree_loc(decl), "%s %s has %d formal parameter%s",
2✔
2982
                      class_str(class_of(decl)), istr(tree_ident(decl)),
2983
                      nports, nports > 1 ? "s" : "");
2984
            diag_emit(d);
1✔
2985
            return false;
1✔
2986
         }
2987
         else {
2988
            port = tree_port(decl, index);
158,015✔
2989
            port_type = tree_type(port);
158,015✔
2990
         }
2991
         break;
158,015✔
2992

2993
      case P_NAMED:
3,447✔
2994
         {
2995
            have_named = true;
3,447✔
2996

2997
            tree_t name = tree_name(param);
3,447✔
2998
            tree_t ref = name_to_ref(name);
3,447✔
2999
            assert(ref != NULL);
3,447✔
3000

3001
            if ((partial = (ref != name))) {
3,447✔
3002
               tree_t value = tree_value(name);
58✔
3003
               if (tree_kind(value) != T_REF)
58✔
UNCOV
3004
                  sem_error(name, "sorry, this form of named parameter is "
×
3005
                            "not supported");
3006
            }
3007

3008
            ident_t id = tree_ident(ref);
3,447✔
3009
            for (int j = 0; j < nports; j++) {
13,202✔
3010
               tree_t p = tree_port(decl, j);
13,200✔
3011
               if (tree_ident(p) == id) {
13,200✔
3012
                  index = j;
3013
                  port = p;
3014
                  break;
3015
               }
3016
            }
3017

3018
            if (index == -1 || !tree_has_ref(ref)) {
3,447✔
3019
               // Should have generated an error during overload
3020
               // resolution
3021
               assert(error_count() > 0);
2✔
3022
               return false;
3023
            }
3024

3025
            // Set the ref again here because solve_types may have set it
3026
            // to the wrong overload
3027
            if (tree_ref(ref) != port)
3,445✔
3028
               tree_set_name(param, (name = change_ref(name, port)));
775✔
3029

3030
            port_type = tree_type(name);
3,445✔
3031
         }
3032
      }
3033

3034
      class_t class    = tree_class(port);
161,460✔
3035
      port_mode_t mode = tree_subkind(port);
161,460✔
3036

3037
      if (map[index] != NULL && (!partial || tree_kind(map[index]) == T_REF))
161,460✔
3038
         sem_error(param, "formal parameter %s already has an associated "
2✔
3039
                   "actual", istr(tree_ident(port)));
3040

3041
      map[index] = param;
161,458✔
3042

3043
      tree_t value = tree_value(param);
161,458✔
3044
      if (!sem_check(value, tab))
161,458✔
3045
         return false;
3046

3047
      if (!sem_check_type(value, port_type, tab))
161,419✔
3048
         sem_error(value, "type of actual %s does not match formal %s type %s",
4✔
3049
                   type_pp2(tree_type(value), port_type),
3050
                   istr(tree_ident(port)),
3051
                   type_pp2(port_type, tree_type(value)));
3052

3053
      // LRM 08 sections 4.2.2.2 and 4.2.2.3
3054
      if (class == C_VARIABLE || class == C_SIGNAL) {
161,415✔
3055
         tree_t ref = name_to_ref(value);
5,994✔
3056
         if (ref == NULL || class_of(ref) != class) {
5,994✔
3057
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
7✔
3058
            diag_printf(d, "actual for formal %s with class %s must be "
11✔
3059
                        "a name denoting a %s", istr(tree_ident(port)),
3060
                        class == C_VARIABLE ? "VARIABLE" : "SIGNAL",
3061
                        class_str(class));
3062
            if (ref == NULL)
7✔
3063
               diag_hint(d, tree_loc(value), "actual designator is not a name");
3✔
3064
            else if (tree_has_ref(ref))
4✔
3065
               diag_hint(d, tree_loc(value), "object %s has class %s",
4✔
3066
                         istr(tree_ident(ref)), class_str(class_of(ref)));
3067
            diag_lrm(d, STD_08, class == C_SIGNAL ? "4.2.2.3" : "4.2.2.2");
10✔
3068
            diag_emit(d);
7✔
3069
            return false;
7✔
3070
         }
3071

3072
         // Check OUT and INOUT parameters can be assigned to
3073
         if (mode == PORT_OUT || mode == PORT_INOUT) {
5,987✔
3074
            tree_t decl = tree_ref(ref);
5,046✔
3075
            const tree_kind_t decl_kind = tree_kind(decl);
5,046✔
3076

3077
            if ((decl_kind == T_PARAM_DECL || decl_kind == T_PORT_DECL)
5,046✔
3078
                && tree_subkind(decl) == PORT_IN) {
932✔
3079
               const char *what =
2✔
3080
                  decl_kind == T_PARAM_DECL ? "parameter" : "port";
1✔
3081
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
3082
               diag_printf(d, "cannot associate %s %s of mode IN with "
1✔
3083
                           "formal %s of mode %s", what,
3084
                           istr(tree_ident(decl)), istr(tree_ident(port)),
3085
                           port_mode_str(mode));
3086
               diag_hint(d, tree_loc(decl), "%s declared with mode %s",
1✔
3087
                         istr(tree_ident(decl)),
3088
                         port_mode_str(tree_subkind(decl)));
1✔
3089
               diag_hint(d, tree_loc(value), "associated with %s %s %s here",
1✔
3090
                         port_mode_str(mode), what, istr(tree_ident(port)));
3091
               diag_emit(d);
1✔
3092
               return false;
1✔
3093
            }
3094
         }
3095
         else if ((mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
941✔
3096
                  && !sem_check_compatible_view(port, value))
17✔
3097
            return false;
3098
      }
3099

3100
      if (class == C_SIGNAL && !sem_static_name(value, sem_globally_static)) {
161,406✔
3101
         diag_t *d = pedantic_diag(value);
4✔
3102
         if (d != NULL) {
4✔
3103
            diag_printf(d, "actual associated with signal parameter %s must be "
4✔
3104
                        "denoted by a static signal name",
3105
                        istr(tree_ident(port)));
3106
            diag_hint(d, tree_loc(value), "not a static signal name");
4✔
3107
            diag_lrm(d, STD_08, "4.2.2.3");
4✔
3108
            diag_lrm(d, STD_08, "8.1");
4✔
3109
            diag_emit(d);
4✔
3110
            return false;
4✔
3111
         }
3112
      }
3113

3114
      // Check IN and INOUT parameters can be read
3115
      if (tree_kind(t) != T_ATTR_REF) {
161,402✔
3116
         const port_mode_t mode = tree_subkind(port);
161,402✔
3117
         if (mode == PORT_IN || mode == PORT_INOUT) {
161,402✔
3118
            if (!sem_readable(value))
159,268✔
3119
               return false;
3120
         }
3121
      }
3122
   }
3123

3124
   for (int i = 0; i < nports; i++) {
254,875✔
3125
      if (map[i] == NULL) {
167,155✔
3126
         tree_t port = tree_port(decl, i);
5,800✔
3127
         if (!tree_has_value(port))
5,800✔
3128
            sem_error(t, "missing actual for formal parameter %s without "
2✔
3129
                      "default value", istr(tree_ident(port)));
3130
         else {
3131
            const port_mode_t mode = tree_subkind(port);
5,798✔
3132
            if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
5,798✔
3133
               sem_error(t, "missing actual for formal parameter %s with "
167,153✔
3134
                         "mode view indication %s", istr(tree_ident(port)),
3135
                         type_pp(tree_type(tree_value(port))));
3136
         }
3137
      }
3138
   }
3139

3140
   return true;
3141
}
3142

3143
static bool sem_check_fcall(tree_t t, nametab_t *tab)
80,263✔
3144
{
3145
   if (!tree_has_ref(t))
80,263✔
3146
      return false;
3147

3148
   if (tree_kind(t) == T_PROT_FCALL && tree_has_name(t)) {
80,182✔
3149
      tree_t name = tree_name(t);
776✔
3150
      if (!sem_check(name, tab))
776✔
3151
         return false;
3152
   }
3153

3154
   tree_t decl = tree_ref(t), sub;
80,182✔
3155
   const tree_flags_t flags = tree_flags(decl);
80,182✔
3156

3157
   if ((flags & TREE_F_IMPURE) && (sub = find_enclosing(tab, S_SUBPROGRAM))) {
80,182✔
3158
      // Pure function may not call an impure function
3159
      if (tree_kind(sub) == T_FUNC_BODY && !(tree_flags(sub) & TREE_F_IMPURE)) {
1,035✔
3160
         diag_t *d = pedantic_diag(t);
2✔
3161
         if (d != NULL) {
2✔
3162
            diag_printf(d, "pure function %s cannot call impure function %s",
2✔
3163
                        istr(tree_ident(sub)), istr(tree_ident(decl)));
3164
            diag_emit(d);
2✔
3165
         }
3166
      }
3167

3168
      // Propagate impurity flags
3169
      tree_set_flag(sub, flags & (TREE_F_IMPURE_FILE | TREE_F_IMPURE_SHARED));
1,035✔
3170
   }
3171

3172
   if (!sem_check_call_args(t, decl, tab))
80,182✔
3173
      return false;
3174

3175
   if (sem_locally_static(t))
80,138✔
3176
      tree_set_flag(t, TREE_F_LOCALLY_STATIC | TREE_F_GLOBALLY_STATIC);
12,539✔
3177
   else if (sem_globally_static(t))
67,599✔
3178
      tree_set_flag(t, TREE_F_GLOBALLY_STATIC);
13,393✔
3179

3180
   return true;
3181
}
3182

3183
static bool sem_check_pcall(tree_t t, nametab_t *tab)
7,647✔
3184
{
3185
   if (!tree_has_ref(t))
7,647✔
3186
      return false;
3187

3188
   const bool is_protected = (tree_kind(t) == T_PROT_PCALL);
7,612✔
3189
   if (is_protected && tree_has_name(t)
7,612✔
3190
       && !sem_check(tree_name(t), tab))
191✔
3191
      return false;
3192

3193
   tree_t decl = tree_ref(t);
7,611✔
3194

3195
   switch (class_of(decl)) {
7,611✔
3196
   case C_PROCEDURE:
3197
      break;
7,610✔
3198
   case C_FUNCTION:
1✔
3199
      sem_error(t, "function %s cannot be called as a procedure",
1✔
3200
                type_pp(tree_type(decl)));
UNCOV
3201
   default:
×
3202
      // All other errors should be caught at parsing stage
UNCOV
3203
      assert(error_count() > 0);
×
3204
      return false;
3205
   }
3206

3207
   if (!sem_check_call_args(t, decl, tab))
7,610✔
3208
      return false;
3209

3210
   const tree_flags_t flags = tree_flags(decl);
7,582✔
3211

3212
   const bool never_waits = is_protected || !!(flags & TREE_F_NEVER_WAITS);
7,582✔
3213
   const bool has_wait = !is_protected && !!(flags & TREE_F_HAS_WAIT);
7,582✔
3214

3215
   assert(!never_waits || !has_wait);
7,582✔
3216

3217
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
7,582✔
3218
   if (sub != NULL) {
7,582✔
3219
      if (!never_waits)
3,166✔
3220
         tree_clear_flag(sub, TREE_F_NEVER_WAITS);
166✔
3221

3222
      if (has_wait)
3,166✔
3223
         tree_set_flag(sub, TREE_F_HAS_WAIT);
26✔
3224

3225
      if (flags & TREE_F_IMPURE_FILE)
3,166✔
3226
         tree_set_flag(sub, TREE_F_IMPURE_FILE);
168✔
3227

3228
      if (flags & TREE_F_IMPURE_SHARED)
3,166✔
3229
         tree_set_flag(sub, TREE_F_IMPURE_SHARED);
6✔
3230

3231
      const bool in_func = tree_kind(sub) == T_FUNC_BODY;
3,166✔
3232
      const bool in_pure_func = in_func && !(tree_flags(sub) & TREE_F_IMPURE);
3,166✔
3233

3234
      if (has_wait && in_func)
3,166✔
3235
         sem_error(t, "function %s cannot call procedure %s which contains "
2✔
3236
                   "a wait statement", istr(tree_ident(sub)),
3237
                   istr(tree_ident(decl)));
3238
      else if ((flags & TREE_F_IMPURE_FILE) && in_pure_func) {
3,164✔
3239
         diag_t *d = pedantic_diag(t);
1✔
3240
         if (d != NULL) {
1✔
3241
            diag_printf(d, "pure function %s cannot call procedure %s which "
1✔
3242
                        "references a file object", istr(tree_ident(sub)),
3243
                        istr(tree_ident(decl)));
3244
            diag_emit(d);
1✔
3245
         }
3246
      }
3247
      else if ((flags & TREE_F_IMPURE_SHARED) && in_pure_func) {
3,163✔
3248
         diag_t *d = pedantic_diag(t);
4✔
3249
         if (d != NULL) {
4✔
3250
            diag_printf(d, "pure function %s cannot call procedure %s which "
4✔
3251
                        "references a shared variable", istr(tree_ident(sub)),
3252
                        istr(tree_ident(decl)));
3253
            diag_emit(d);
4✔
3254
         }
3255
      }
3256
   }
3257

3258
   return true;
3259
}
3260

3261
static bool sem_check_wait(tree_t t, nametab_t *tab)
8,349✔
3262
{
3263
   if (tree_has_delay(t)) {
8,349✔
3264
      type_t std_time = std_type(NULL, STD_TIME);
4,386✔
3265
      tree_t delay = tree_delay(t);
4,386✔
3266

3267
      if (!sem_check(delay, tab))
4,386✔
3268
         return false;
3269

3270
      if (!sem_check_type(delay, std_time, tab))
4,385✔
3271
         sem_error(delay, "type of delay must be %s but have %s",
2✔
3272
                   type_pp(std_time), type_pp(tree_type(delay)));
3273
   }
3274

3275
   if (tree_has_value(t)) {
8,346✔
3276
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
241✔
3277
      tree_t value = tree_value(t);
241✔
3278

3279
      if (!sem_check(value, tab))
241✔
3280
         return false;
3281

3282
      if (!sem_check_type(value, std_bool, tab))
241✔
3283
         sem_error(value, "type of condition must be BOOLEAN but have %s",
1✔
3284
                   type_pp(tree_type(value)));
3285
   }
3286

3287
   if (find_enclosing(tab, S_PROTECTED))
8,345✔
3288
      sem_error(t, "wait statement not allowed in protected subprogram body");
1✔
3289

3290
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
8,344✔
3291
   if (sub != NULL) {
8,344✔
3292
      if (tree_kind(sub) == T_FUNC_BODY)
357✔
3293
         sem_error(t, "wait statement not allowed in function body");
1✔
3294

3295
      tree_clear_flag(sub, TREE_F_NEVER_WAITS);
356✔
3296
      tree_set_flag(sub, TREE_F_HAS_WAIT);
356✔
3297
   }
3298

3299
   tree_t proc = find_enclosing(tab, S_PROCESS);
8,343✔
3300
   if (proc != NULL && tree_triggers(proc) > 0) {
8,343✔
3301
      // No wait statements allowed in process with sensitivity list
3302
      sem_error(t, "wait statement not allowed in process with "
1✔
3303
                "sensitvity list");
3304
   }
3305

3306
   return sem_check_sensitivity(t, tab);
8,342✔
3307
}
3308

3309
static bool sem_check_assert(tree_t t, nametab_t *tab)
18,294✔
3310
{
3311
   // Rules for asserion statements are in LRM 93 section 8.2
3312

3313
   type_t std_bool     = std_type(NULL, STD_BOOLEAN);
18,294✔
3314
   type_t std_string   = std_type(NULL, STD_STRING);
18,294✔
3315
   type_t std_severity = std_type(NULL, STD_SEVERITY_LEVEL);
18,294✔
3316

3317
   tree_t value    = tree_has_value(t) ? tree_value(t) : NULL;
18,294✔
3318
   tree_t severity = tree_severity(t);
18,294✔
3319
   tree_t message  = tree_has_message(t) ? tree_message(t) : NULL;
18,294✔
3320

3321
   if (value != NULL && !sem_check(value, tab))
18,294✔
3322
      return false;
3323

3324
   if (!sem_check(severity, tab))
18,254✔
3325
      return false;
3326

3327
   if (message != NULL && !sem_check(message, tab))
18,254✔
3328
      return false;
3329

3330
   if (value != NULL && !sem_check_type(value, std_bool, tab))
18,243✔
3331
      sem_error(value, "type of assertion expression must "
1✔
3332
                "be %s but is %s", type_pp(std_bool),
3333
                type_pp(tree_type(value)));
3334

3335
   if (!sem_check_type(severity, std_severity, tab))
18,242✔
3336
      sem_error(severity, "type of severity must be %s but is %s",
1✔
3337
                type_pp(std_severity),
3338
                type_pp(tree_type(severity)));
3339

3340
   if (message != NULL && !sem_check_type(message, std_string, tab))
18,241✔
3341
      sem_error(message, "type of message be %s but is %s",
1✔
3342
                type_pp(std_string),
3343
                type_pp(tree_type(message)));
3344

3345
   return true;
3346
}
3347

3348
static bool sem_check_string_literal(tree_t t)
24,210✔
3349
{
3350
   // String literals are in LRM 93 section 7.3.1
3351

3352
   type_t type = tree_type(t);
24,210✔
3353
   type_t elem = type_base_recur(type_elem(type));
24,210✔
3354

3355
   if (type_is_none(elem))
24,210✔
3356
      return false;
3357

3358
   const int nlits = type_enum_literals(elem);
24,210✔
3359
   const int nchars = tree_chars(t);
24,210✔
3360
   for (int i = 0; i < nchars; i++) {
364,457✔
3361
      tree_t ch = tree_char(t, i);
340,248✔
3362

3363
      ident_t ch_i = tree_ident(ch);
340,248✔
3364
      bool valid = false;
340,248✔
3365
      for (int j = 0; !valid && (j < nlits); j++) {
24,625,900✔
3366
         tree_t lit = type_enum_literal(elem, j);
24,285,600✔
3367
         if (ch_i == tree_ident(lit))
24,285,600✔
3368
            valid = true;
340,247✔
3369
      }
3370

3371
      if (!valid)
340,248✔
3372
         sem_error(t, "invalid character %s in string literal of type %s",
340,248✔
3373
                   istr(ch_i), type_pp(type));
3374
   }
3375

3376
   return true;
3377
}
3378

3379
static bool sem_check_literal(tree_t t)
85,978✔
3380
{
3381
   type_t type = tree_type(t);
85,978✔
3382
   if (type_is_none(type))
85,978✔
3383
      return false;
2✔
3384

3385
   return true;
3386
}
3387

3388
static bool sem_check_array_aggregate(tree_t t, nametab_t *tab)
6,243✔
3389
{
3390
   type_t composite_type = tree_type(t);
6,243✔
3391
   type_t base_type = type_base_recur(composite_type);
6,243✔
3392

3393
   const bool unconstrained = type_is_unconstrained(composite_type);
6,243✔
3394

3395
   type_t elem_type = NULL;
6,243✔
3396
   const int ndims = dimension_of(composite_type);
6,243✔
3397
   if (ndims == 1)
6,243✔
3398
      elem_type = type_elem(base_type);
5,958✔
3399
   else {
3400
      // Higher dimensions must be specified with a sub-aggregate or
3401
      // string literal
3402
      tree_t a0 = tree_value(tree_assoc(t, 0));
285✔
3403
      const tree_kind_t a0_kind = tree_kind(a0);
285✔
3404
      if (a0_kind != T_AGGREGATE && a0_kind != T_STRING)
285✔
3405
         sem_error(a0, "second dimension of %d dimensional array type %s must "
1✔
3406
                   "be specified by a sub-aggregate, string, or bit-string "
3407
                   "literal", ndims, type_pp(composite_type));
3408

3409
      // The parser will have constructed a type with ndims - 1
3410
      // dimensions.
3411
      elem_type = tree_type(tree_value(tree_assoc(t, 0)));
284✔
3412

3413
      if (!type_is_unconstrained(elem_type)) {
284✔
3414
         if (!sem_check_array_dims(elem_type, NULL, tab))
277✔
3415
            return false;
3416
      }
3417
   }
3418

3419
   type_t index_type = index_type_of(composite_type, 0);
6,242✔
3420

3421
   bool have_named = false;
6,242✔
3422
   bool have_pos = false;
6,242✔
3423

3424
   const int nassocs = tree_assocs(t);
6,242✔
3425
   for (int i = 0; i < nassocs; i++) {
37,781✔
3426
      tree_t a = tree_assoc(t, i);
31,550✔
3427

3428
      const assoc_kind_t akind = tree_subkind(a);
31,550✔
3429
      switch (akind) {
31,550✔
3430
      case A_RANGE:
611✔
3431
      case A_SLICE:
3432
         {
3433
            tree_t r = tree_range(a, 0);
611✔
3434
            if (!sem_check_discrete_range(r, index_type, tab))
611✔
3435
               return false;
3436

3437
            have_named = true;
3438
         }
3439
         break;
3440

3441
      case A_NAMED:
1,246✔
3442
         {
3443
            tree_t name = tree_name(a);
1,246✔
3444

3445
            if (!sem_check(name, tab))
1,246✔
3446
               return false;
3447

3448
            if (!sem_check_type(name, index_type, tab))
1,246✔
3449
               sem_error(name, "type of array aggregate choice %s does not "
1✔
3450
                         "match %s index type %s", type_pp(tree_type(name)),
3451
                         type_pp(composite_type), type_pp(index_type));
3452

3453
            have_named = true;
3454
         }
3455
         break;
3456

3457
      case A_POS:
27,351✔
3458
      case A_CONCAT:
3459
         have_pos = true;
27,351✔
3460
         break;
27,351✔
3461

3462
      case A_OTHERS:
2,342✔
3463
         if (unconstrained)
2,342✔
3464
            sem_error(a, "index range of array aggregate with others choice "
2✔
3465
                      "cannot be determined from the context");
3466
         break;
3467
      }
3468

3469
      tree_t value = tree_value(a);
31,544✔
3470

3471
      if (!sem_check(value, tab))
31,544✔
3472
         return false;
3473

3474
      if (!sem_check_type(value, elem_type, tab)) {
31,542✔
3475
         // LRM 08 section 9.3.3.3 allows the association to be of the
3476
         // base aggregate type as well
3477
         const bool allow_slice =
342✔
3478
            (akind == A_CONCAT || akind == A_SLICE)
171✔
3479
            || (ndims == 1 && standard() >= STD_08
171✔
3480
                && (akind == A_POS || akind == A_RANGE));
3✔
3481

3482
         if (allow_slice && !sem_check_type(value, composite_type, tab))
171✔
3483
            sem_error(value, "type of %s association %s does not match "
1✔
3484
                      "aggregate element type %s or the aggregate type "
3485
                      "itself %s", assoc_kind_str(akind),
3486
                      type_pp(tree_type(value)), type_pp(elem_type),
3487
                      type_pp(composite_type));
3488
         else if (!allow_slice)
170✔
3489
            sem_error(value, "type of %s association %s does not match "
2✔
3490
                      "aggregate element type %s", assoc_kind_str(akind),
3491
                      type_pp(tree_type(value)), type_pp(elem_type));
3492
         else
3493
            assert(akind == A_CONCAT || akind == A_SLICE);
168✔
3494
      }
3495
   }
3496

3497
   // Named and positional associations cannot be mixed in array
3498
   // aggregates
3499

3500
   if (have_named && have_pos)
6,231✔
3501
      sem_error(t, "named and positional associations cannot be "
2✔
3502
                "mixed in array aggregates");
3503

3504
   // If a choice is not locally static then it must be the only element
3505

3506
   if (have_named && nassocs > 1) {
6,229✔
3507
      for (int i = 0; i < nassocs; i++) {
1,859✔
3508
         tree_t a = tree_assoc(t, i);
1,463✔
3509
         tree_t choice = NULL;
1,463✔
3510
         switch (tree_subkind(a)) {
1,463✔
3511
         case A_NAMED: choice = tree_name(a); break;
1,142✔
3512
         case A_SLICE:
155✔
3513
         case A_RANGE: choice = tree_range(a, 0); break;
155✔
3514
         }
3515

3516
         if (choice && !sem_locally_static(choice))
1,297✔
3517
            sem_error(choice, "a choice that is not locally static is allowed"
1,463✔
3518
                      " only if the array aggregate contains a single element"
3519
                      " association");
3520
      }
3521
   }
3522

3523
   return true;
3524
}
3525

3526
static bool sem_check_record_aggregate(tree_t t, nametab_t *tab)
2,238✔
3527
{
3528
   // Checks for record aggregates are given in LRM 93 section 7.3.2.1
3529

3530
   type_t composite_type = tree_type(t);
2,238✔
3531
   type_t base_type = type_base_recur(composite_type);
2,238✔
3532

3533
   const int nfields = type_fields(base_type);
2,238✔
3534
   int pos = 0;
2,238✔
3535

3536
   LOCAL_BIT_MASK have;
4,476✔
3537
   mask_init(&have, nfields);
2,238✔
3538

3539
   const int nassocs = tree_assocs(t);
2,238✔
3540
   for (int i = 0; i < nassocs; i++) {
7,792✔
3541
      tree_t a = tree_assoc(t, i);
5,567✔
3542
      int f = -1;
5,567✔
3543

3544
      switch (tree_subkind(a)) {
5,567✔
3545
      case A_NAMED:
957✔
3546
         {
3547
            tree_t name = tree_name(a);
957✔
3548
            if (tree_kind(name) != T_REF)
957✔
3549
               sem_error(name, "association choice must be a field name");
1✔
3550
            else if (!tree_has_ref(name))
956✔
3551
               return false;   // Was parse error
3552

3553
            tree_t fdecl = tree_ref(name);
954✔
3554
            if (tree_kind(fdecl) != T_FIELD_DECL)
954✔
3555
               return false;   // Was parse error
3556

3557
            f = tree_pos(fdecl);
952✔
3558
         }
3559
         break;
952✔
3560

3561
      case A_POS:
4,499✔
3562
         {
3563
            if (pos >= nfields)
4,499✔
3564
               sem_error(a, "%d positional associations given but record type"
1✔
3565
                         " %s only has %d fields", pos + 1,
3566
                         type_pp(composite_type), nfields);
3567

3568
            f = pos++;
4,498✔
3569
         }
3570
         break;
4,498✔
3571

3572
      case A_OTHERS:
3573
         f = -1;
3574
         break;
3575

3576
      case A_RANGE:
2✔
3577
         sem_error(a, "range association invalid in record aggregate");
2✔
3578

UNCOV
3579
      case A_SLICE:
×
3580
      case A_CONCAT:
UNCOV
3581
         fatal_trace("illegal association type in record aggregate");
×
3582
      }
3583

3584
      int nmatched = 0;
5,559✔
3585
      for (int j = 0; j < nfields; j++) {
26,489✔
3586
         if ((f != -1) && (f != j))
20,934✔
3587
            continue;
15,130✔
3588

3589
         tree_t field = type_field(base_type, j);
5,804✔
3590
         type_t field_type = tree_type(field);
5,804✔
3591

3592
         if (mask_test(&have, j)) {
5,804✔
3593
            if (f == -1)
42✔
3594
               continue;
40✔
3595

3596
            tree_t ak = NULL;
3597
            for (int k = 0; k < i; k++) {
2✔
3598
               ak = tree_assoc(t, k);
2✔
3599
               if (tree_subkind(ak) == A_POS && tree_pos(ak) == j)
2✔
3600
                  break;
3601
               else if (tree_pos(tree_ref(tree_name(ak))) == j)
1✔
3602
                  break;
3603
            }
3604
            assert(ak != NULL);
2✔
3605

3606
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(a));
2✔
3607
            diag_printf(d, "field %s was already given a value by earlier "
2✔
3608
                        "%s choice", istr(tree_ident(field)),
3609
                        assoc_kind_str(tree_subkind(ak)));
2✔
3610
            diag_hint(d, tree_loc(ak), "first choice associated with field %s",
2✔
3611
                      istr(tree_ident(field)));
3612
            diag_hint(d, tree_loc(a), "duplicate choice here");
2✔
3613
            diag_emit(d);
2✔
3614
            return false;
2✔
3615
         }
3616

3617
         tree_t value = tree_value(a);
5,762✔
3618

3619
         if (!sem_check(value, tab))
5,762✔
3620
            return false;
3621

3622
         if (!sem_check_type(value, field_type, tab))
5,762✔
3623
            sem_error(value, "type of value %s does not match type %s"
2✔
3624
                      " of field %s",
3625
                      type_pp2(tree_type(value), field_type),
3626
                      type_pp2(field_type, tree_type(value)),
3627
                      istr(tree_ident(field)));
3628

3629
         mask_set(&have, j);
5,760✔
3630
         nmatched++;
5,760✔
3631
      }
3632

3633
      if (f == -1 && nmatched == 0)
5,555✔
3634
         sem_error(a, "others association must represent at least one element");
5,555✔
3635
   }
3636

3637
   for (int i = 0; i < nfields; i++) {
7,975✔
3638
      if (!mask_test(&have, i)) {
5,752✔
3639
         tree_t field = type_field(base_type, i);
2✔
3640
         sem_error(t, "field %s does not have a value",
5,752✔
3641
                   istr(tree_ident(field)));
3642
      }
3643
   }
3644

3645
   return true;
3646
}
3647

3648
static bool sem_check_aggregate(tree_t t, nametab_t *tab)
8,490✔
3649
{
3650
   // Rules for aggregates are in LRM 93 section 7.3.2
3651

3652
   type_t composite_type = tree_type(t);
8,490✔
3653

3654
   if (type_is_none(composite_type))
8,490✔
3655
      return false;
3656
   assert(type_is_composite(composite_type));
8,484✔
3657

3658
   // All positional associations must appear before named associations
3659
   // and those must appear before any others association
3660

3661
   enum { POS, NAMED, OTHERS } state = POS;
8,484✔
3662

3663
   const int nassocs = tree_assocs(t);
8,484✔
3664
   for (int i = 0; i < nassocs; i++) {
45,612✔
3665
      tree_t a = tree_assoc(t, i);
37,131✔
3666

3667
      switch (tree_subkind(a)) {
37,131✔
3668
      case A_POS:
31,853✔
3669
      case A_CONCAT:
3670
         if (state > POS)
31,853✔
3671
            sem_error(a, "positional associations must appear "
1✔
3672
                      "first in aggregate");
3673
         break;
3674

3675
      case A_NAMED:
2,823✔
3676
      case A_RANGE:
3677
      case A_SLICE:
3678
         if (state > NAMED)
2,823✔
3679
            sem_error(a, "named association must not follow "
1✔
3680
                      "others association in aggregate");
3681
         state = NAMED;
3682
         break;
3683

3684
      case A_OTHERS:
2,455✔
3685
         if (state == OTHERS)
2,455✔
3686
            sem_error(a, "only a single others association "
1✔
3687
                      "allowed in aggregate");
3688
         state = OTHERS;
3689
         break;
3690
      }
3691
   }
37,128✔
3692

3693
   if (type_is_array(composite_type))
8,481✔
3694
      return sem_check_array_aggregate(t, tab);
6,243✔
3695
   else
3696
      return sem_check_record_aggregate(t, tab);
2,238✔
3697
}
3698

3699
static bool sem_check_ref(tree_t t, nametab_t *tab)
177,264✔
3700
{
3701
   if (!tree_has_ref(t))
177,264✔
3702
      return false;
3703

3704
   type_t type = get_type_or_null(t);
177,227✔
3705
   if (type != NULL && type_is_none(type))
177,227✔
3706
      return false;
3707

3708
   tree_t decl = tree_ref(t);
177,221✔
3709
   const tree_kind_t kind = tree_kind(decl);
177,221✔
3710

3711
   switch (kind) {
177,221✔
3712
   case T_PORT_DECL:
3713
   case T_VAR_DECL:
3714
   case T_SIGNAL_DECL:
3715
   case T_FILE_DECL:
3716
   case T_CONST_DECL:
3717
   case T_ENUM_LIT:
3718
   case T_UNIT_DECL:
3719
   case T_FUNC_DECL:
3720
   case T_FUNC_BODY:
3721
   case T_FUNC_INST:
3722
   case T_PROC_DECL:
3723
   case T_PROC_BODY:
3724
   case T_PROC_INST:
3725
   case T_IMPLICIT_SIGNAL:
3726
   case T_PARAM_DECL:
3727
      break;
3728

3729
   case T_ALIAS:
1,613✔
3730
      {
3731
         switch (class_of(decl)) {
1,613✔
3732
         case C_VARIABLE:
3733
         case C_SIGNAL:
3734
         case C_CONSTANT:
3735
         case C_LITERAL:
3736
            break;
3737

3738
         case C_DEFAULT:
3739
            return false;   // Must have been an earlier parse error
3740

3741
         default:
1✔
3742
            sem_error(t, "invalid use of alias %s", istr(tree_ident(decl)));
1✔
3743
         }
3744
      }
3745
      break;
3746

3747
   case T_GENERIC_DECL:
5,796✔
3748
      if (tree_class(decl) == C_CONSTANT)
5,796✔
3749
         break;
3750
      // Fall-through
3751

3752
   default:
3753
      {
3754
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
3✔
3755
         diag_printf(d, "invalid use of %s %s", class_str(class_of(decl)),
3✔
3756
                     istr(tree_ident(t)));
3757
         diag_hint(d, tree_loc(decl), "%s declared here",
3✔
3758
                   istr(tree_ident(decl)));
3759
         diag_emit(d);
3✔
3760
         return false;
3✔
3761
      }
3762
   }
3763

3764
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
177,217✔
3765
   if (sub != NULL) {
177,217✔
3766
      if (kind == T_FILE_DECL)
89,815✔
3767
         tree_set_flag(sub, TREE_F_IMPURE_FILE);
283✔
3768
      else if (kind == T_VAR_DECL && (tree_flags(decl) & TREE_F_SHARED))
89,532✔
3769
         tree_set_flag(sub, TREE_F_IMPURE_SHARED);
58✔
3770
   }
3771

3772
   return true;
3773
}
3774

3775
static bool sem_check_name_prefix(tree_t t, nametab_t *tab, const char *what)
27,012✔
3776
{
3777
   tree_t value = tree_value(t);
27,012✔
3778
   if (!sem_check(value, tab))
27,012✔
3779
      return false;
3780

3781
   // The prefix of a name may only be function call or another name
3782
   switch (tree_kind(value)) {
27,005✔
3783
   case T_FCALL:
3784
   case T_PROT_FCALL:
3785
   case T_REF:
3786
   case T_ATTR_REF:
3787
   case T_ALL:
3788
   case T_ARRAY_REF:
3789
   case T_ARRAY_SLICE:
3790
   case T_RECORD_REF:
3791
   case T_EXTERNAL_NAME:
3792
      break;
3793

3794
   default:
2✔
3795
      {
3796
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
3797
         diag_printf(d, "the prefix of %s must be a name or a "
2✔
3798
                     "function call", what);
3799
         diag_lrm(d, STD_08, "8.1");
2✔
3800
         diag_emit(d);
2✔
3801
         return false;
2✔
3802
      }
3803
   }
3804

3805
   return true;
3806
}
3807

3808
static bool sem_check_record_ref(tree_t t, nametab_t *tab)
8,587✔
3809
{
3810
   if (!sem_check_name_prefix(t, tab, "a selected name"))
8,587✔
3811
      return false;
3812

3813
   tree_t value = tree_value(t);
8,583✔
3814
   type_t value_type = tree_type(value);
8,583✔
3815

3816
   if (type_is_none(value_type))
8,583✔
3817
      return false;
3818
   else if (!type_is_record(value_type))
8,583✔
UNCOV
3819
      sem_error(value, "expected record type but found %s",
×
3820
                type_pp(value_type));
3821

3822
   return true;
3823
}
3824

3825
static bool sem_check_array_ref(tree_t t, nametab_t *tab)
14,079✔
3826
{
3827
   if (!sem_check_name_prefix(t, tab, "an indexed name"))
14,079✔
3828
      return false;
3829

3830
   type_t type = tree_type(tree_value(t));
14,077✔
3831

3832
   if (!type_is_array(type))
14,077✔
3833
      return false;  // Checked earlier
3834

3835
   const int nindex  = dimension_of(type);
14,073✔
3836
   const int nparams = tree_params(t);
14,073✔
3837

3838
   if (nparams != nindex)
14,073✔
3839
      sem_error(t, "prefix of indexed name has %d dimensions but %d "
3✔
3840
                "indices given", nindex, nparams);
3841

3842
   bool ok = true;
3843
   for (int i = 0; i < nparams; i++) {
29,769✔
3844
      tree_t p = tree_param(t, i);
15,701✔
3845
      assert(tree_subkind(p) == P_POS);
15,701✔
3846

3847
      type_t expect = index_type_of(type, i);
15,701✔
3848
      tree_t value = tree_value(p);
15,701✔
3849

3850
      ok = sem_check(value, tab) && ok;
15,701✔
3851

3852
      if (ok && !sem_check_type(value, expect, tab))
15,701✔
3853
         sem_error(value, "type of index %s does not match type of "
15,701✔
3854
                   "array dimension %s",
3855
                   type_pp(tree_type(value)),
3856
                   type_pp(expect));
3857
   }
3858

3859
   return ok;
3860
}
3861

3862
static bool sem_check_array_slice(tree_t t, nametab_t *tab)
2,010✔
3863
{
3864
   if (!sem_check_name_prefix(t, tab, "a slice name"))
2,010✔
3865
      return false;
3866

3867
   type_t array_type = tree_type(tree_value(t));
2,008✔
3868

3869
   if (type_is_none(array_type))
2,008✔
3870
      return false;
3871
   else if (!sem_check_incomplete(t, array_type))
2,008✔
3872
      return false;
3873
   else if (!type_is_array(array_type))
2,007✔
3874
      sem_error(t, "type of slice prefix %s is not an array",
2✔
3875
                type_pp(array_type));
3876

3877
   tree_t r = tree_range(t, 0);
2,005✔
3878
   if (!sem_check_discrete_range(r, index_type_of(array_type, 0), tab))
2,005✔
3879
      return false;
3880

3881
   const bool unconstrained = type_is_unconstrained(array_type);
1,995✔
3882
   const range_kind_t prefix_dir =
3,990✔
3883
      unconstrained ? RANGE_EXPR : direction_of(array_type, 0);
1,995✔
3884

3885
   const range_kind_t rkind = tree_subkind(r);
1,995✔
3886
   const bool wrong_dir =
3,990✔
3887
      !unconstrained
1,995✔
3888
      && rkind != prefix_dir
1,995✔
3889
      && (rkind == RANGE_TO || rkind == RANGE_DOWNTO)
130✔
3890
      && (prefix_dir == RANGE_TO || prefix_dir == RANGE_DOWNTO);
1,995✔
3891

3892
   if (wrong_dir) {
1,995✔
3893
      const char *text[] = { "TO", "DOWNTO", "?", "??", "???" };
2✔
3894
      sem_error(t, "range direction of slice %s does not match prefix %s",
2✔
3895
                text[rkind], text[prefix_dir]);
3896
   }
3897

3898
   return true;
3899
}
3900

3901
static bool sem_check_valid_implicit_signal(tree_t t, nametab_t *tab)
49✔
3902
{
3903
   // Certain attributes are illegal inside a subprogram according to LRM
3904
   // 93 section 2.1.1.2
3905

3906
   if (find_enclosing(tab, S_SUBPROGRAM) != NULL)
49✔
3907
      sem_error(t, "implicit signal %s cannot be used in a "
2✔
3908
                "subprogram body", istr(tree_ident(t)));
3909

3910
   return true;
3911
}
3912

3913
static bool sem_check_signal_attr(tree_t t)
775✔
3914
{
3915
   tree_t name = tree_name(t);
776✔
3916

3917
   if (tree_kind(name) == T_ATTR_REF)
776✔
3918
      return sem_check_signal_attr(name);
3919

3920
   tree_t ref = name_to_ref(name);
775✔
3921
   if (ref != NULL && class_of(ref) == C_SIGNAL)
775✔
3922
      return true;
3923

3924
   sem_error(t, "prefix of attribute %s must denote a signal",
2✔
3925
             istr(tree_ident(t)));
3926
}
3927

3928
static bool sem_check_driving(tree_t t)
58✔
3929
{
3930
   // See LRM 08 section 16.2.4 for special rules about 'DRIVING and
3931
   // 'DRIVING_VALUE
3932

3933
   if (!sem_check_signal_attr(t))
58✔
3934
      return false;
3935

3936
   tree_t ref = name_to_ref(tree_name(t));
57✔
3937
   if (ref == NULL || !tree_has_ref(ref))
57✔
UNCOV
3938
      return false;
×
3939

3940
   tree_t decl = tree_ref(ref);
57✔
3941
   if (tree_kind(decl) == T_PORT_DECL) {
57✔
3942
      const port_mode_t mode = tree_subkind(decl);
2✔
3943
      if (mode != PORT_OUT && mode != PORT_INOUT && mode != PORT_BUFFER)
2✔
3944
         sem_error(t, "prefix of attribute %s must denote a signal or a port "
1✔
3945
                   "with mode IN, INOUT, or BUFFER", istr(tree_ident(t)));
3946
   }
3947

3948
   // TODO: check within a process
3949

3950
   return true;
3951
}
3952

3953
static bool sem_check_attr_param(tree_t t, type_t expect, int min, int max,
1,283✔
3954
                                 nametab_t *tab)
3955
{
3956
   const int nparams = tree_params(t);
1,283✔
3957
   if (nparams == 0 && min > 0)
1,283✔
UNCOV
3958
      sem_error(t, "attribute %s requires a parameter", istr(tree_ident(t)));
×
3959
   else if (nparams > max)
1,283✔
3960
      sem_error(t, "too many parameters for attribute %s", istr(tree_ident(t)));
×
3961
   else if (nparams == 1) {
1,283✔
3962
      tree_t dim = tree_value(tree_param(t, 0));
1,224✔
3963
      if (!sem_check(dim, tab))
1,224✔
3964
         return false;
3965

3966
      tree_t value = tree_value(tree_param(t, 0));
1,220✔
3967
      if (!sem_check_type(value, expect, tab))
1,220✔
UNCOV
3968
         sem_error(t, "expected type %s for attribute %s parameter but "
×
3969
                   "have %s", type_pp(expect), istr(tree_ident(t)),
3970
                   type_pp(tree_type(value)));
3971
   }
3972

3973
   return true;
3974
}
3975

3976
static bool sem_check_dimension_attr(tree_t t, nametab_t *tab)
21,613✔
3977
{
3978
   const int nparams = tree_params(t);
21,613✔
3979
   if (nparams == 0)
21,613✔
3980
      return true;
3981

3982
   assert(nparams == 1);   // Enforced by parser
1,647✔
3983

3984
   tree_t dim = tree_value(tree_param(t, 0));
1,647✔
3985
   if (!sem_check(dim, tab))
1,647✔
3986
      return false;
3987

3988
   // The parameter must be a locally static expression of type
3989
   // universal_integer
3990

3991
   type_t uint = std_type(NULL, STD_UNIVERSAL_INTEGER);
1,647✔
3992
   type_t dimtype = tree_type(dim);
1,647✔
3993
   if (!type_eq(dimtype, uint)) {
1,647✔
3994
      diag_t *d;
4✔
3995
      if (type_is_integer(dimtype))
4✔
3996
         d = pedantic_diag(dim);
4✔
3997
      else
UNCOV
3998
         d = diag_new(DIAG_ERROR, tree_loc(dim));
×
3999

4000
      if (d != NULL) {
4✔
4001
         diag_printf(d, "dimension parameter of attribute %s must be a locally "
4✔
4002
                     "static expression of type universal_integer",
4003
                     istr(tree_ident(t)));
4004
         diag_hint(d, tree_loc(dim), "expression has type %s",
4✔
4005
                   type_pp(tree_type(dim)));
4006
         diag_emit(d);
4✔
4007
         return false;
4✔
4008
      }
4009
   }
4010

4011
   if (!sem_locally_static(dim))
1,643✔
UNCOV
4012
      sem_error(dim, "dimension parameter of attribute %s must be a locally "
×
4013
                "static expression", istr(tree_ident(t)));
4014

4015
   if (!type_is_array(tree_type(tree_name(t))))
1,643✔
UNCOV
4016
      sem_error(t, "prefix of attribute %s with dimension is not an array",
×
4017
                istr(tree_ident(t)));
4018

4019
   return true;
4020
}
4021

4022
static bool sem_is_named_entity(tree_t t)
748✔
4023
{
4024
   const tree_kind_t kind = tree_kind(t);
748✔
4025
   if (kind != T_REF)
748✔
4026
      return false;
4027

4028
   tree_t decl = tree_ref(t);
748✔
4029

4030
   switch (tree_kind(decl)) {
748✔
4031
   case T_SIGNAL_DECL:  case T_VAR_DECL:     case T_PORT_DECL:
4032
   case T_ALIAS:        case T_ENTITY:       case T_ARCH:
4033
   case T_PACKAGE:      case T_PACK_BODY:    case T_BLOCK:
4034
   case T_FILE_DECL:    case T_CONST_DECL:   case T_FUNC_DECL:
4035
   case T_FUNC_BODY:    case T_PROC_DECL:    case T_PROC_BODY:
4036
   case T_PROCESS:      case T_GENERIC_DECL: case T_PARAM_DECL:
4037
   case T_INSTANCE:     case T_PROT_DECL:
4038
      return true;
UNCOV
4039
   case T_IMPLICIT_SIGNAL:
×
UNCOV
4040
      return tree_subkind(decl) == IMPLICIT_GUARD;   // See LRM 93 section 4.3
×
4041
   default:
2✔
4042
      return false;
2✔
4043
   }
4044
}
4045

4046
static bool sem_check_attr_ref(tree_t t, bool allow_range, nametab_t *tab)
24,799✔
4047
{
4048
   // Attribute names are in LRM 93 section 6.6
4049

4050
   tree_t name = tree_name(t), decl = NULL, type_decl = NULL;
24,799✔
4051
   type_t named_type = NULL;
24,799✔
4052

4053
   ident_t attr = tree_ident(t);
24,799✔
4054
   const attr_kind_t predef = tree_subkind(t);
24,799✔
4055

4056
   switch (tree_kind(name)) {
24,799✔
4057
   case T_REF:
23,098✔
4058
      {
4059
         if (!tree_has_ref(name))
23,098✔
4060
            return false;
4061

4062
         decl = tree_ref(name);
23,096✔
4063

4064
         if ((type_decl = aliased_type_decl(decl)) != NULL)
23,096✔
4065
            named_type = tree_type(type_decl);
4,621✔
4066
      }
4067
      break;
4068

4069
   case T_ATTR_REF:
62✔
4070
      if (is_type_attribute(tree_subkind(name)))
62✔
4071
         named_type = tree_type(name);
37✔
4072
      else {
4073
         const bool prefix_can_be_range =
25✔
4074
            predef == ATTR_LOW || predef == ATTR_HIGH || predef == ATTR_LEFT
4075
            || predef == ATTR_RIGHT || predef == ATTR_ASCENDING;
25✔
4076

4077
         if (!sem_check_attr_ref(name, prefix_can_be_range, tab))
25✔
4078
            return false;
4079
      }
4080
      break;
4081

4082
   default:
1,639✔
4083
      if (!sem_check(name, tab))
1,639✔
4084
         return false;
4085
   }
4086

4087
   if (predef == ATTR_INSTANCE_NAME)
24,795✔
4088
      tree_set_global_flags(t, TREE_GF_INSTANCE_NAME);
490✔
4089
   else if (predef == ATTR_PATH_NAME)
24,305✔
4090
      tree_set_global_flags(t, TREE_GF_PATH_NAME);
208✔
4091

4092
   switch (predef) {
24,795✔
4093
   case ATTR_RANGE:
4,574✔
4094
   case ATTR_REVERSE_RANGE:
4095
      {
4096
         if (!allow_range)
4,574✔
UNCOV
4097
            sem_error(t, "range expression not allowed here");
×
4098

4099
         type_t name_type = tree_has_type(name) ? tree_type(name) : NULL;
4,574✔
4100
         const bool is_type = type_decl != NULL;
9,148✔
4101
         const bool is_discrete =
9,148✔
4102
            name_type != NULL && type_is_discrete(name_type);
4,574✔
4103
         const bool invalid =
9,148✔
4104
            name_type == NULL
4105
            || (!(is_discrete && is_type) && !type_is_array(name_type));
4,574✔
4106

4107
         if (invalid) {
4,574✔
4108
            if (name_type != NULL && type_is_none(name_type))
3✔
4109
               return false;
4110
            else if (decl != NULL && class_has_type(class_of(decl))) {
3✔
4111
               if (is_type)
3✔
4112
                  sem_error(t, "type %s does not have a range",
1✔
4113
                            type_pp(tree_type(decl)));
4114
               else
4115
                  sem_error(t, "object %s does not have a range",
2✔
4116
                            istr(tree_ident(decl)));
4117
            }
4118
            else {
UNCOV
4119
               assert(error_count() > 0);  // Checked in parser
×
4120
               return false;
4121
            }
4122
         }
4123

4124
         if (is_type && type_is_unconstrained(name_type))
4,571✔
4125
            sem_error(t, "cannot use attribute %s with unconstrained array "
1✔
4126
                      "type %s", istr(attr), type_pp(name_type));
4127

4128
         if (!sem_check_dimension_attr(t, tab))
4,570✔
UNCOV
4129
            return false;
×
4130

4131
         return true;
4132
      }
4133

4134
   case ATTR_LENGTH:
7,587✔
4135
      {
4136
         type_t type = get_type_or_null(name);
7,587✔
4137
         if (type == NULL)
7,587✔
4138
            sem_error(name, "prefix does not have LENGTH attribute");
1✔
4139
         else if (type_is_none(type))
7,586✔
4140
            return false;
4141
         else if (!sem_check_incomplete(t, type))
7,586✔
4142
            return false;
4143
         else if (!type_is_array(type)
7,585✔
4144
                  && !(standard() >= STD_19 && type_is_discrete(type)))
11✔
4145
            sem_error(name, "prefix of attribute LENGTH must be an array%s "
1✔
4146
                      "but have type %s",
4147
                      standard() >= STD_19 ? " or a discrete type" : "",
4148
                      type_pp(type));
4149

4150
         if (!sem_check_dimension_attr(t, tab))
7,584✔
4151
            return false;
3✔
4152

4153
         return true;
4154
      }
4155

4156
   case ATTR_LEFT:
9,460✔
4157
   case ATTR_RIGHT:
4158
   case ATTR_LOW:
4159
   case ATTR_HIGH:
4160
   case ATTR_ASCENDING:
4161
      {
4162
         type_t type = tree_type(name);
9,460✔
4163

4164
         if (type_is_none(type))
9,460✔
4165
            return false;
4166
         else if (!sem_check_incomplete(t, type))
9,460✔
4167
            return false;
4168
         else if (!sem_check_dimension_attr(t, tab))
9,459✔
4169
            return false;
4170

4171
         if (!type_is_array(type) && !type_is_scalar(type))
9,458✔
4172
            sem_error(t, "prefix does not have attribute %s", istr(attr));
1✔
4173

4174
         return true;
4175
      }
4176

4177
   case ATTR_LAST_EVENT:
59✔
4178
   case ATTR_LAST_ACTIVE:
4179
      if (!sem_check_attr_param(t, NULL, 0, 0, tab))
59✔
4180
         return false;
4181

4182
      if (!sem_check_signal_attr(t))
59✔
4183
         return false;
1✔
4184

4185
      return true;
4186

4187
   case ATTR_EVENT:
511✔
4188
   case ATTR_ACTIVE:
4189
   case ATTR_LAST_VALUE:
4190
      if (!sem_check_signal_attr(t))
511✔
UNCOV
4191
         return false;
×
4192

4193
      return true;
4194

4195
   case ATTR_PATH_NAME:
748✔
4196
   case ATTR_INSTANCE_NAME:
4197
   case ATTR_SIMPLE_NAME:
4198
      if (!sem_is_named_entity(name))
748✔
4199
         sem_error(t, "prefix of %s attribute must be a named entity",
2✔
4200
                   istr(attr));
4201

4202
      tree_set_flag(name, TREE_F_FORMAL_NAME);
746✔
4203
      return true;
746✔
4204

4205
   case ATTR_STABLE:
49✔
4206
   case ATTR_QUIET:
4207
      if (!sem_check_valid_implicit_signal(t, tab))
49✔
4208
         return false;
4209
      // Fall-through
4210
   case ATTR_DELAYED:
4211
      {
4212
         if (!sem_check_signal_attr(t))
130✔
4213
            return false;
4214

4215
         if (tree_params(t) > 0) {
130✔
4216
            tree_t value = tree_value(tree_param(t, 0));
48✔
4217

4218
            if (!sem_check(value, tab))
48✔
4219
               return false;
4220

4221
            type_t std_time = std_type(NULL, STD_TIME);
48✔
4222
            if (!sem_check_type(value, std_time, tab))
48✔
4223
               sem_error(value, "attribute %s parameter must have type %s",
1✔
4224
                         istr(attr), type_pp(std_time));
4225
         }
4226

4227
         return true;
4228
      }
4229
   case ATTR_TRANSACTION:
17✔
4230
      if (!sem_check_signal_attr(t))
17✔
UNCOV
4231
         return false;
×
4232

4233
      return true;
4234

4235
   case ATTR_DRIVING_VALUE:
58✔
4236
   case ATTR_DRIVING:
4237
      return sem_check_driving(t);
58✔
4238

4239
   case ATTR_IMAGE:
1,077✔
4240
   case ATTR_VALUE:
4241
      {
4242
         const bool std_2019 = standard() >= STD_19;
1,077✔
4243

4244
         if (named_type == NULL && std_2019 && tree_params(t) == 0) {
1,077✔
4245
            // LCS2016-18 allows attribute with object prefix
4246
            named_type = get_type_or_null(name);
5✔
4247
            add_param(t, name, P_POS, NULL);
5✔
4248
         }
4249

4250
         if (named_type == NULL)
1,077✔
4251
            sem_error(t, "prefix of attribute %s must be a type", istr(attr));
1✔
4252
         if (!std_2019 && !type_is_scalar(named_type))
1,076✔
4253
            sem_error(t, "cannot use attribute %s with non-scalar type %s",
2✔
4254
                      istr(attr), type_pp(named_type));
4255
         else if (std_2019 && !type_is_representable(named_type))
1,074✔
4256
            sem_error(t, "cannot use attribute %s with non-representable "
1✔
4257
                      "type %s", istr(attr), type_pp(named_type));
4258

4259
         type_t std_string = std_type(NULL, STD_STRING);
1,073✔
4260
         type_t arg_type = predef == ATTR_IMAGE ? named_type : std_string;
1,073✔
4261
         if (!sem_check_attr_param(t, arg_type, 1, 1, tab))
1,073✔
4262
            return false;
4✔
4263

4264
         return true;
4265
      }
4266

4267
   case ATTR_LEFTOF:
238✔
4268
   case ATTR_RIGHTOF:
4269
   case ATTR_PRED:
4270
   case ATTR_SUCC:
4271
   case ATTR_POS:
4272
   case ATTR_VAL:
4273
      {
4274
         if (named_type == NULL && standard() >= STD_19)
238✔
4275
            named_type = get_type_or_null(name);   // LCS2016-08 relaxation
3✔
4276

4277
         if (named_type == NULL)
238✔
UNCOV
4278
            sem_error(t, "prefix of attribute %s must be a type", istr(attr));
×
4279

4280
         type_t name_type = tree_type(name);
238✔
4281

4282
         if (!type_is_discrete(name_type) && !type_is_physical(name_type))
238✔
UNCOV
4283
            sem_error(t, "prefix of attribute %s must be a discrete or "
×
4284
                      "physical type", istr(attr));
4285

4286
         if (predef == ATTR_VAL) {
238✔
4287
            // Parameter may be any integer type
4288
            if (tree_params(t) != 1)
87✔
4289
               sem_error(t, "attribute VAL requires a parameter");
1✔
4290

4291
            type_t ptype = tree_type(tree_value(tree_param(t, 0)));
86✔
4292
            if (!type_is_integer(ptype))
86✔
4293
               sem_error(t, "parameter of attribute VAL must have an integer "
1✔
4294
                         "type but found %s", type_pp(ptype));
4295
         }
4296
         else if (!sem_check_attr_param(t, name_type, 1, 1, tab))
151✔
UNCOV
4297
            return false;
×
4298

4299
         return true;
4300
      }
4301

4302
   case ATTR_BASE:
1✔
4303
      sem_error(t, "BASE attribute is allowed only as the prefix of the name "
1✔
4304
                "of another attribute");
4305

4306
   case ATTR_ELEMENT:
1✔
4307
   case ATTR_SUBTYPE:
4308
   case ATTR_INDEX:
4309
      sem_error(t, "%s attribute is only allowed in a type mark", istr(attr));
1✔
4310

4311
   case ATTR_CONVERSE:
21✔
4312
      if (type_kind(tree_type(name)) != T_VIEW)
21✔
4313
         sem_error(t, "prefix of 'CONVERSE attribute must be a named mode "
1✔
4314
                   "view or alias thereof");
4315

4316
      return true;
4317

4318
   case ATTR_REFLECT:
96✔
4319
      if (get_type_or_null(name) == NULL)
96✔
4320
         sem_error(t, "prefix of attribute REFLECT is not a type mark or "
1✔
4321
                   "an object with a type");
4322
      else if (named_type != NULL && type_is_unconstrained(named_type))
95✔
4323
         sem_error(t, "prefix of 'REFLECT attribute must be a fully "
1✔
4324
                   "constrained subtype");
4325

4326
      return true;
4327

4328
   case ATTR_USER:
215✔
4329
      if (!tree_has_value(t))
215✔
4330
         return false;
4331

4332
      if (!sem_static_name(name, sem_globally_static)) {
213✔
4333
         if (tree_kind(name) == T_REF)
×
4334
            sem_error(name, "%s is not a static name", istr(tree_ident(name)));
×
4335
         else
UNCOV
4336
            sem_error(name, "invalid attribute reference");
×
4337
      }
4338

4339
      return true;
4340

UNCOV
4341
   default:
×
UNCOV
4342
      fatal_trace("unhandled attribute kind %d", predef);
×
4343
   }
4344
}
4345

4346
static bool sem_check_qualified(tree_t t, nametab_t *tab)
4,218✔
4347
{
4348
   if (tree_has_value(t)) {
4,218✔
4349
      tree_t value = tree_value(t);
3,930✔
4350

4351
      if (!sem_check(value, tab))
3,930✔
4352
         return false;
4353

4354
      // LRM 08 section 9.3.5 qualified expressions: the operand shall have
4355
      // the same type as the base type of the type mark
4356
      type_t base = type_base_recur(tree_type(t));
3,930✔
4357
      if (!sem_check_type(value, base, tab)) {
3,930✔
4358
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
4359
         diag_printf(d, "operand of qualified expression must have type %s",
1✔
4360
                     type_pp(base));
4361
         diag_hint(d, tree_loc(value), "operand has type %s",
1✔
4362
                   type_pp(tree_type(value)));
4363
         diag_lrm(d, STD_08, "9.3.5");
1✔
4364
         diag_emit(d);
1✔
4365
         return false;
1✔
4366
      }
4367
   }
4368

4369
   return true;
4370
}
4371

4372
static bool sem_static_signal_name(tree_t t)
1,261✔
4373
{
4374
   if (!sem_static_name(t, sem_globally_static))
1,261✔
4375
      return false;
4376

4377
   tree_t ref = name_to_ref(t);
1,259✔
4378
   if (ref != NULL && !tree_has_ref(ref))
1,259✔
4379
      return true;  // Suppress cascading error
4380

4381
   return ref != NULL && class_of(tree_ref(ref)) == C_SIGNAL;
1,262✔
4382
}
4383

4384
static bool sem_check_port_actual(formal_map_t *formals, int nformals,
3,188✔
4385
                                  tree_t param, tree_t unit, nametab_t *tab)
4386
{
4387
   tree_t value = tree_value(param);
3,188✔
4388
   tree_t decl = NULL;
3,188✔
4389
   type_t type = NULL;
3,188✔
4390

4391
   switch (tree_subkind(param)) {
3,188✔
4392
   case P_POS:
1,210✔
4393
      {
4394
         const int pos = tree_pos(param);
1,210✔
4395
         if (pos >= nformals)
1,210✔
4396
            sem_error(value, "found at least %d positional actuals but %s "
4✔
4397
                      "has only %d port%s", pos + 1, istr(tree_ident(unit)),
4398
                      nformals, nformals == 1 ? "" : "s");
4399
         if (formals[pos].have)
1,208✔
UNCOV
4400
            sem_error(value, "formal port %s already has an actual",
×
4401
                      istr(tree_ident(formals[pos].decl)));
4402
         formals[pos].have = true;
1,208✔
4403
         decl = formals[pos].decl;
1,208✔
4404
         type = tree_type(decl);
1,208✔
4405
      }
4406
      break;
1,208✔
4407

4408
   case P_NAMED:
1,978✔
4409
      {
4410
         tree_t name = tree_name(param);
1,978✔
4411
         tree_t ref = name;
1,978✔
4412
         tree_t conv = NULL;
1,978✔
4413

4414
         switch (tree_kind(name)) {
1,978✔
4415
         case T_FCALL:
1✔
4416
            if (tree_params(name) != 1)
1✔
UNCOV
4417
               sem_error(name, "output conversion function must have "
×
4418
                         "exactly one parameter");
4419

4420
            // The parser would have replaced any other valid conversion
4421
            // function with T_CONV_FUNC
4422
            sem_error(name, "invalid output conversion %s",
1✔
4423
                      istr(tree_ident(name)));
4424
            break;
72✔
4425

4426
         case T_CONV_FUNC:
72✔
4427
         case T_TYPE_CONV:
4428
            conv = name;
72✔
4429
            name = ref = tree_value(name);
72✔
4430
            break;
72✔
4431

4432
         default:
4433
            break;
4434
         }
4435

4436
         ref = name_to_ref(ref);
1,977✔
4437
         assert(ref != NULL && tree_kind(ref) == T_REF);
1,977✔
4438

4439
         for (int i = 0; i < nformals; i++) {
8,157✔
4440
            if (tree_ident(formals[i].decl) == tree_ident(ref)) {
8,155✔
4441
               if (formals[i].have && !formals[i].partial)
1,975✔
4442
                  sem_error(value, "formal port %s already has an actual",
1✔
4443
                            istr(tree_ident(formals[i].decl)));
4444
               formals[i].have    = true;
1,974✔
4445
               formals[i].partial = (tree_kind(name) != T_REF);
1,974✔
4446
               decl = formals[i].decl;
1,974✔
4447
               tree_set_flag(ref, TREE_F_FORMAL_NAME);
1,974✔
4448
               break;
1,974✔
4449
            }
4450
         }
4451

4452
         if (decl == NULL)
1,976✔
4453
            sem_error(value, "%s has no port named %s",
2✔
4454
                      istr(tree_ident(unit)), istr(tree_ident(ref)));
4455

4456
         if (!sem_static_name(name, sem_locally_static))
1,974✔
4457
            sem_error(name, "formal name must be locally static");
3✔
4458

4459
         if (conv != NULL) {
1,971✔
4460
            port_mode_t mode = tree_subkind(decl);
72✔
4461

4462
            type = tree_type((mode == PORT_INOUT) ? name : conv);
128✔
4463

4464
            if (mode == PORT_IN)
72✔
4465
               sem_error(name, "output conversion not allowed for formal "
1✔
4466
                         "%s with mode IN", istr(tree_ident(decl)));
4467

4468
            if (tree_kind(value) == T_OPEN)
71✔
4469
               sem_error(name, "output conversion for formal %s must not "
1✔
4470
                         "have OPEN actual", istr(tree_ident(decl)));
4471
         }
4472
         else
4473
            type = tree_type(name);
1,899✔
4474

4475
         break;
4476
      }
4477
   }
4478

4479
   assert(type != NULL);
3,177✔
4480

4481
   if (!sem_check(value, tab))
3,177✔
4482
      return false;
4483

4484
   const tree_kind_t kind = tree_kind(value);
3,176✔
4485
   tree_t expr = kind == T_INERTIAL ? tree_value(value) : value;
3,176✔
4486

4487
   type_t value_type = tree_type(expr);
3,176✔
4488

4489
   if (!sem_check_type(expr, type, tab))
3,176✔
4490
      sem_error(value, "type of actual %s does not match type %s of formal "
1✔
4491
                "port %s", type_pp2(value_type, type),
4492
                type_pp2(type, value_type), istr(tree_ident(decl)));
4493

4494
   const port_mode_t mode = tree_subkind(decl);
3,175✔
4495

4496
   if (tree_kind(value) == T_OPEN) {
3,175✔
4497
      if ((mode == PORT_IN) && !tree_has_value(decl))
50✔
4498
         sem_error(value, "unconnected port %s with mode IN must have a "
1✔
4499
                   "default value", istr(tree_ident(decl)));
4500

4501
      if ((mode != PORT_IN) && type_is_unconstrained(tree_type(decl)))
49✔
4502
         sem_error(value, "port %s of unconstrained type %s cannot "
1✔
4503
                   "be unconnected", istr(tree_ident(decl)), type_pp(type));
4504
   }
4505

4506
   // Check for type conversions and conversion functions
4507
   // These only apply if the class of the formal is not constant
4508

4509
   tree_t actual = NULL;
3,173✔
4510
   if (kind == T_TYPE_CONV || kind == T_CONV_FUNC) {
3,173✔
4511
      // Conversion functions are in LRM 93 section 4.3.2.2
4512
      actual = tree_value(value);
104✔
4513

4514
      // LRM 93 section 3.2.1.1 result of a type conversion in an
4515
      // association list cannot be an unconstrained array type
4516
      if (type_is_unconstrained(value_type)
104✔
4517
          && type_is_unconstrained(type))
35✔
4518
         sem_error(value, "result of conversion for unconstrained formal "
1✔
4519
                   "%s must be a constrained array type",
4520
                   istr(tree_ident(decl)));
4521

4522
      if (mode == PORT_OUT)
103✔
4523
         sem_error(value, "conversion not allowed for formal %s with "
1✔
4524
                   "mode OUT", istr(tree_ident(decl)));
4525
   }
4526
   else
4527
      actual = value;    // No conversion
4528

4529
   if (mode == PORT_IN && kind != T_INERTIAL) {
3,171✔
4530
      tree_t ref = name_to_ref(actual);
1,859✔
4531
      bool is_static = true;
1,859✔
4532
      if (ref != NULL && class_of(ref) == C_SIGNAL)
1,859✔
4533
         is_static = sem_static_name(actual, sem_globally_static);
1,697✔
4534
      else
4535
         is_static = sem_globally_static(actual);
162✔
4536

4537
      // LRM 08 section 6.5.6.3 the actual is converted to a concurrent
4538
      // signal assignment to an anonymous signal that is then
4539
      // associated with the formal
4540
      if (!is_static && standard() >= STD_08) {
1,874✔
4541
         // The rules listed for unconstrained ports in 6.5.6.3 should
4542
         // be covered by the check for a globally static subtype in
4543
         // addition to the checks above
4544
         if (type_is_unconstrained(type)
16✔
4545
             && !sem_static_subtype(value_type, sem_globally_static)) {
1✔
4546
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
4547
            diag_printf(d, "expression associated with unconstrained formal "
1✔
4548
                        "port %s must have a globally static subtype",
4549
                        istr(tree_ident(decl)));
4550
            diag_lrm(d, STD_08, "6.5.6.3");
1✔
4551
            diag_emit(d);
1✔
4552
            return false;
1✔
4553
         }
4554

4555
         tree_t w = tree_new(T_INERTIAL);
15✔
4556
         tree_set_loc(w, tree_loc(value));
15✔
4557
         tree_set_value(w, value);
15✔
4558

4559
         tree_set_value(param, w);
15✔
4560
      }
4561
      else if (!is_static)
1,843✔
4562
         sem_error(value, "actual associated with port %s of mode IN must be "
4✔
4563
                   "a globally static expression or static signal name",
4564
                   istr(tree_ident(decl)));
4565
   }
4566
   else if (mode == PORT_INOUT && tree_class(decl) == C_VARIABLE) {
1,312✔
4567
      // VHDL-2019 additions for shared variable ports
4568
      tree_t ref = name_to_ref(value);
5✔
4569
      if (ref == NULL || class_of(ref) != C_VARIABLE)
5✔
4570
         sem_error(value, "actual associated with formal variable port %s "
1✔
4571
                   "must either be a shared variable or a formal variable port "
4572
                   "of another design entity", istr(tree_ident(decl)));
4573
   }
4574
   else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW) {
1,307✔
4575
      if (!sem_static_signal_name(actual))
56✔
4576
         sem_error(value, "actual associated with port %s with mode view "
1✔
4577
                   "indication must be a static signal name",
4578
                   istr(tree_ident(decl)));
4579

4580
      if (!sem_check_compatible_view(decl, actual))
55✔
4581
         return false;
1✔
4582
   }
4583
   else if (mode != PORT_IN && tree_kind(actual) != T_OPEN
1,251✔
4584
            && !sem_static_signal_name(actual)) {
1,205✔
4585
      sem_error(value, "actual associated with port %s of mode %s must be "
4✔
4586
                "a static signal name or OPEN",
4587
                istr(tree_ident(decl)), port_mode_str(tree_subkind(decl)));
4588
   }
4589

4590
   return true;
4591
}
4592

4593
static bool sem_check_port_map(tree_t t, tree_t unit, nametab_t *tab)
1,562✔
4594
{
4595
   // Check there is an actual for each formal port generic
4596
   // Rules for maps are described in LRM 93 section 5.2.1.2
4597

4598
   const int nformals = tree_ports(unit);
1,562✔
4599
   const int nactuals = tree_params(t);
1,562✔
4600

4601
   bool ok = true;
1,562✔
4602

4603
   formal_map_t *formals LOCAL = xmalloc_array(nformals, sizeof(formal_map_t));
3,124✔
4604

4605
   for (int i = 0; i < nformals; i++) {
4,672✔
4606
      formals[i].decl    = tree_port(unit, i);
3,110✔
4607
      formals[i].have    = false;
3,110✔
4608
      formals[i].partial = false;
3,110✔
4609
   }
4610

4611
   for (int i = 0; i < nactuals; i++) {
4,760✔
4612
      tree_t p = tree_param(t, i);
3,198✔
4613
      if (tree_subkind(p) != P_NAMED)
3,198✔
4614
         continue;
1,211✔
4615

4616
      tree_t name = tree_name(p);
1,987✔
4617

4618
      ok &= sem_check(name, tab);
1,987✔
4619

4620
      const tree_kind_t name_kind = tree_kind(name);
1,987✔
4621
      if ((name_kind == T_ARRAY_REF || name_kind == T_ARRAY_SLICE)
1,987✔
4622
          && tree_kind(tree_value(p)) == T_OPEN && standard() < STD_19) {
182✔
4623
         diag_t *d = pedantic_diag(p);
1✔
4624
         if (d != NULL) {
1✔
4625
            diag_printf(d, "sub-elements of composite port cannot be "
1✔
4626
                        "associated with OPEN");
4627
            diag_emit(d);
1✔
4628
         }
4629
      }
4630
   }
4631

4632
   if (!ok)
1,562✔
4633
      return false;
4634

4635
   for (int i = 0; i < nactuals; i++) {
4,745✔
4636
      tree_t actual = tree_param(t, i);
3,188✔
4637
      ok &= sem_check_port_actual(formals, nformals, actual, unit, tab);
3,188✔
4638

4639
      if (!ok && tree_subkind(actual) == P_POS && i >= nformals)
3,188✔
4640
         break;   // Prevent useless repeated errors
4641
   }
4642

4643
   if (tree_kind(t) == T_BINDING)
1,559✔
4644
      return ok;
4645

4646
   for (int i = 0; i < nformals; i++) {
4,578✔
4647
      if (!formals[i].have) {
3,047✔
4648
         port_mode_t mode = tree_subkind(formals[i].decl);
313✔
4649

4650
         if (mode == PORT_IN && !tree_has_value(formals[i].decl)) {
313✔
4651
            error_at(tree_loc(t), "missing actual for port %s of "
4✔
4652
                     "mode IN without a default expression",
4653
                     istr(tree_ident(formals[i].decl)));
4✔
4654
         }
4655
         else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW)
309✔
4656
            error_at(tree_loc(t), "missing actual for port %s with "
2✔
4657
                     "mode view indication %s",
4658
                     istr(tree_ident(formals[i].decl)),
1✔
4659
                     type_pp(tree_type(tree_value(formals[i].decl))));
1✔
4660

4661
         type_t ftype = tree_type(formals[i].decl);
313✔
4662
         if (mode != PORT_IN && type_is_unconstrained(ftype)) {
313✔
4663
            error_at(tree_loc(t), "missing actual for port %s with "
1✔
4664
                     "unconstrained array type",
4665
                     istr(tree_ident(formals[i].decl)));
1✔
4666
         }
4667
      }
4668
   }
4669

4670
   return ok;
4671
}
4672

4673
static bool sem_check_generic_actual(formal_map_t *formals, int nformals,
1,424✔
4674
                                     tree_t param, tree_t unit, nametab_t *tab)
4675
{
4676
   tree_t value = tree_value(param), decl = NULL;
1,424✔
4677
   type_t type = NULL;
1,424✔
4678

4679
   switch (tree_subkind(param)) {
1,424✔
4680
   case P_POS:
430✔
4681
      {
4682
         const int pos = tree_pos(param);
430✔
4683
         if (pos >= nformals)
430✔
4684
            sem_error(value, "found at least %d positional actuals but %s "
1✔
4685
                      "has only %d generic%s", pos + 1, istr(tree_ident(unit)),
4686
                      nformals, nformals == 1 ? "" : "s");
4687
         else if (formals[pos].have)
429✔
UNCOV
4688
            sem_error(value, "formal generic %s already has an actual",
×
4689
                      istr(tree_ident(formals[pos].decl)));
4690
         else if (tree_flags(formals[pos].decl) & TREE_F_PREDEFINED) {
429✔
4691
            diag_t *d = diag_new(DIAG_WARN, tree_loc(param));
2✔
4692
            diag_printf(d, "positional generic actual is associated with "
2✔
4693
                        "implicit generic subprogram %s for type %s",
4694
                        istr(tree_ident(formals[pos].decl)),
4695
                        type_pp(tree_type(tree_port(formals[pos].decl, 0))));
4696
            diag_hint(d, NULL, "use a named association if this was intended");
2✔
4697
            diag_emit(d);
2✔
4698
         }
4699

4700
         formals[pos].have = true;
429✔
4701
         decl = formals[pos].decl;
429✔
4702
         type = get_type_or_null(decl);
429✔
4703
      }
4704
      break;
429✔
4705

4706
   case P_NAMED:
994✔
4707
      {
4708
         tree_t name = tree_name(param);
994✔
4709
         tree_t ref = name_to_ref(name);
994✔
4710

4711
         if (ref == NULL)
994✔
4712
            sem_error(name, "invalid name in generic map");
1✔
4713
         else if (!tree_has_ref(ref))
993✔
4714
            return false;
4715

4716
         tree_t d = tree_ref(ref);
990✔
4717
         for (int i = 0; i < nformals; i++) {
2,372✔
4718
            if (formals[i].decl == d) {
2,371✔
4719
               if (formals[i].have && !formals[i].partial)
989✔
UNCOV
4720
                  sem_error(value, "generic %s already has an actual",
×
4721
                            istr(tree_ident(formals[i].decl)));
4722
               formals[i].have    = true;
989✔
4723
               formals[i].partial = (tree_kind(name) != T_REF);
989✔
4724
               decl = d;
989✔
4725
               tree_set_flag(ref, TREE_F_FORMAL_NAME);
989✔
4726
               break;
989✔
4727
            }
4728
         }
4729

4730
         if (decl == NULL)
990✔
4731
            sem_error(name, "%s is not a formal generic of %s",
1✔
4732
                      istr(tree_ident(ref)), istr(tree_ident(unit)));
4733

4734
         if (tree_class(decl) == C_CONSTANT || tree_kind(name) != T_REF) {
989✔
4735
            // Do not check package or type for names here as that will
4736
            // throw an error
4737
            if (!sem_check(name, tab))
713✔
4738
               return false;
4739

4740
            if (!sem_static_name(name, sem_locally_static))
713✔
4741
               sem_error(name, "formal generic name must be a locally "
1✔
4742
                         "static name");
4743
         }
4744

4745
         type = get_type_or_null(name);
988✔
4746
         break;
988✔
4747
      }
4748
   }
4749

4750
   switch (tree_class(decl)) {
1,417✔
4751
   case C_TYPE:
200✔
4752
      // The parser already called map_generic_type
4753
      assert(tree_kind(value) == T_TYPE_REF);
200✔
4754
      assert(type_kind(type) == T_GENERIC);
200✔
4755

4756
      type_t map = tree_type(value);
200✔
4757
      if (type_is_none(map))
200✔
4758
         return false;
4759

4760
      static const char *class_strings[] = {
197✔
4761
         [GTYPE_SCALAR] = "a scalar",
4762
         [GTYPE_DISCRETE] = "a discrete",
4763
         [GTYPE_INTEGER] = "an integer",
4764
         [GTYPE_FLOATING] = "a floating-point",
4765
         [GTYPE_PHYSICAL] = "a physical",
4766
         [GTYPE_ACCESS] = "an access",
4767
         [GTYPE_ARRAY] = "an array",
4768
         [GTYPE_FILE] = "a file",
4769
      };
4770

4771
      const gtype_class_t class = type_subkind(type);
197✔
4772
      if (!type_matches_class(map, class))
197✔
4773
         sem_error(param, "cannot map type %s to generic interface type %s "
8✔
4774
                   "which requires %s type", type_pp(map),
4775
                   istr(tree_ident(decl)), class_strings[class]);
4776
      else if (class == GTYPE_ACCESS || class == GTYPE_FILE) {
189✔
4777
         type_t expect = type_designated(type);
9✔
4778
         type_t actual = type_designated(map);
9✔
4779

4780
         if (type_is_generic(expect)) {
9✔
4781
            const gtype_class_t expect_class = type_subkind(expect);
5✔
4782
            if (!type_matches_class(actual, expect_class))
5✔
4783
               sem_error(param, "cannot map type %s to generic interface type "
1✔
4784
                         "%s as the designated type %s is not %s type",
4785
                         type_pp(map), istr(tree_ident(decl)),
4786
                         type_pp(actual), class_strings[expect_class]);
4787
         }
4788
         else if (!type_eq(actual, expect))
4✔
4789
            sem_error(param, "cannot map type %s to generic interface type "
2✔
4790
                      "%s as the designated type %s is not %s",
4791
                      type_pp(map), istr(tree_ident(decl)),
4792
                      type_pp(actual), type_pp(expect));
4793
      }
4794
      else if (class == GTYPE_ARRAY) {
180✔
4795
         const int nindex = type_indexes(type);
14✔
4796
         if (nindex != dimension_of(map))
14✔
4797
            sem_error(param, "cannot map type %s to generic interface "
1✔
4798
                      "type %s as it has %d dimensions but the incomplete "
4799
                      "type definition has %d", type_pp(map),
4800
                      istr(tree_ident(decl)), dimension_of(map), nindex);
4801

4802
         for (int i = 0; i < nindex; i++) {
24✔
4803
            type_t itype = type_index(type, i);
13✔
4804
            type_t imap = index_type_of(map, i);
13✔
4805

4806
            if (type_is_generic(itype)) {
13✔
4807
               const gtype_class_t expect_class = type_subkind(itype);
10✔
4808
               if (!type_matches_class(imap, expect_class))
10✔
4809
                  sem_error(param, "cannot map type %s to generic interface "
1✔
4810
                            "type %s as the index type %s of the %s dimension "
4811
                            "is not %s type", type_pp(map),
4812
                            istr(tree_ident(decl)), type_pp(imap),
4813
                            ordinal_str(i + 1), class_strings[expect_class]);
4814
            }
4815
            else if (!type_eq(imap, itype))
3✔
4816
               sem_error(param, "cannot map type %s to generic interface type "
12✔
4817
                         "%s as the index type %s of the %s dimension is not "
4818
                         "%s", type_pp(map), istr(tree_ident(decl)),
4819
                         type_pp(imap), ordinal_str(i + 1), type_pp(itype));
4820
         }
4821

4822
         type_t expect = type_elem(type);
11✔
4823
         type_t actual = type_elem(map);
11✔
4824

4825
          if (type_is_generic(expect)) {
11✔
4826
            const gtype_class_t expect_class = type_subkind(expect);
7✔
4827
            if (!type_matches_class(actual, expect_class))
7✔
4828
               sem_error(param, "cannot map type %s to generic interface type "
1✔
4829
                         "%s as the element type %s is not %s type",
4830
                         type_pp(map), istr(tree_ident(decl)),
4831
                         type_pp(actual), class_strings[expect_class]);
4832
         }
4833
         else if (!type_eq(actual, expect))
4✔
4834
            sem_error(param, "cannot map type %s to generic interface type "
1✔
4835
                      "%s as the element type %s is not %s",
4836
                      type_pp(map), istr(tree_ident(decl)),
4837
                      type_pp(actual), type_pp(expect));
4838
      }
4839

4840
      break;
4841

4842
   case C_PACKAGE:
60✔
4843
      {
4844
         tree_t pack = NULL;
60✔
4845
         if (tree_kind(value) == T_REF && tree_has_ref(value))
60✔
4846
            pack = tree_ref(value);
59✔
4847

4848
         if (pack == NULL || tree_kind(pack) != T_PACK_INST)
59✔
4849
            sem_error(value, "actual for generic %s is not an "
2✔
4850
                      "instantiated package name", istr(tree_ident(decl)));
4851
         else if (!tree_has_ref(pack))
58✔
4852
            return false;   // Was parse error
4853

4854
         tree_t map = tree_value(decl);
58✔
4855
         if (!tree_has_ref(map))
58✔
4856
            return false;   // Was earlier error
4857

4858
         assert(tree_kind(map) == T_PACKAGE_MAP);
57✔
4859

4860
         tree_t base = tree_ref(pack);
57✔
4861
         tree_t expect = tree_ref(map);
57✔
4862

4863
         if (tree_ident(base) != tree_ident(expect))
57✔
4864
            sem_error(value, "expected an instance of package %s but have "
1✔
4865
                      "instance of %s for generic %s", istr(tree_ident(expect)),
4866
                      istr(tree_ident(base)), istr(tree_ident(decl)));
4867

4868
         map_generic_package(tab, expect, pack);
56✔
4869
      }
4870
      break;
56✔
4871

4872
   case C_FUNCTION:
50✔
4873
   case C_PROCEDURE:
4874
      if (!sem_check(value, tab))
50✔
4875
         return false;
4876

4877
      if (!type_eq_map(tree_type(value), type, get_generic_map(tab)))
47✔
UNCOV
4878
         sem_error(value, "type of actual %s does not match type %s of formal "
×
4879
                   "generic %s", type_pp(tree_type(value)), type_pp(type),
4880
                   istr(tree_ident(decl)));
4881

4882
      assert(tree_kind(value) == T_REF);
47✔
4883

4884
      if (!tree_has_ref(value))
47✔
4885
         return false;
4886

4887
      map_generic_subprogram(tab, decl, tree_ref(value));
47✔
4888
      break;
47✔
4889

4890
   case C_CONSTANT:
1,107✔
4891
      if (!sem_check(value, tab))
1,107✔
4892
         return false;
4893

4894
      if (!sem_check_type(value, type, tab))
1,104✔
4895
         sem_error(value, "type of actual %s does not match type %s of formal "
6✔
4896
                   "generic %s", type_pp(tree_type(value)), type_pp(type),
4897
                   istr(tree_ident(decl)));
4898

4899
      sem_check_static_elab(value);
1,098✔
4900
      break;
1,098✔
4901

4902
   default:
4903
      // Was an earlier error
4904
      break;
4905
   }
4906

4907
   return true;
4908
}
4909

4910
static bool sem_check_generic_map(tree_t t, tree_t unit, nametab_t *tab)
1,917✔
4911
{
4912
   // Check there is an actual for each formal generic
4913
   // Rules for maps are described in LRM 93 section 5.2.1.2
4914

4915
   const int nformals = tree_generics(unit);
1,917✔
4916
   const int nactuals = tree_genmaps(t);
1,917✔
4917

4918
   formal_map_t *formals LOCAL = xmalloc_array(nformals, sizeof(formal_map_t));
1,917✔
4919

4920
   for (int i = 0; i < nformals; i++) {
4,432✔
4921
      formals[i].decl    = tree_generic(unit, i);
2,515✔
4922
      formals[i].have    = false;
2,515✔
4923
      formals[i].partial = false;
2,515✔
4924
   }
4925

4926
   bool ok = true;
4927

4928
   for (int i = 0; i < nactuals; i++) {
3,340✔
4929
      tree_t actual = tree_genmap(t, i);
1,424✔
4930
      ok &= sem_check_generic_actual(formals, nformals, actual, unit, tab);
1,424✔
4931

4932
      if (!ok && tree_subkind(actual) == P_POS && i >= nformals)
1,424✔
4933
         break;   // Prevent useless repeated errors
4934
   }
4935

4936
   for (int i = 0; i < nformals; i++) {
4,432✔
4937
      if (formals[i].have)
2,515✔
4938
         continue;
1,412✔
4939
      else if (tree_has_value(formals[i].decl)) {
1,103✔
4940
         tree_t value = tree_value(formals[i].decl);
813✔
4941
         if (tree_kind(value) == T_BOX) {
813✔
4942
            // Need to look up the matching subprogram now while we still
4943
            // have the symbol table
4944
            map_generic_box(tab, t, formals[i].decl, i);
571✔
4945
         }
4946
      }
4947
      else if (tree_flags(formals[i].decl) & TREE_F_PREDEFINED)
290✔
4948
         map_generic_predef(tab, t, formals[i].decl, i);
284✔
4949
      else
4950
         error_at(tree_loc(t), "missing actual for generic %s without a "
6✔
4951
                  "default expression", istr(tree_ident(formals[i].decl)));
6✔
4952
   }
4953

4954
   return ok;
1,917✔
4955
}
4956

4957
static bool sem_check_instance(tree_t t, nametab_t *tab)
1,306✔
4958
{
4959
   if (!tree_has_ref(t))
1,306✔
4960
      return false;
4961

4962
   tree_t unit = primary_unit_of(tree_ref(t));
1,297✔
4963

4964
   if (tree_has_spec(t)) {
1,297✔
4965
      tree_t spec = tree_spec(t);
81✔
4966

4967
      if (tree_class(t) != C_COMPONENT) {
81✔
4968
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(spec));
2✔
4969
         diag_printf(d, "specification may only be used with component"
2✔
4970
                     " instances");
4971
         diag_hint(d, tree_loc(spec), "specification for %s",
2✔
4972
                   istr(tree_ident(t)));
4973
         diag_hint(d, tree_loc(t), "%s instance", class_str(tree_class(t)));
2✔
4974
         diag_emit(d);
2✔
4975
         return false;
2✔
4976
      }
4977

4978
      assert(tree_kind(unit) == T_COMPONENT);   // Checked by parser
79✔
4979

4980
      if (tree_has_ref(spec) && tree_ref(spec) != unit) {
79✔
4981
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(spec));
2✔
4982
         diag_printf(d, "component mismatch for instance %s: expected %s "
2✔
4983
                     "but specification has %s", istr(tree_ident(t)),
4984
                     istr(tree_ident(unit)), istr(tree_ident(tree_ref(spec))));
4985
         diag_hint(d, tree_loc(spec), "specification has component %s",
2✔
4986
                   istr(tree_ident(tree_ref(spec))));
4987
         diag_hint(d, tree_loc(t), "instance of component %s",
2✔
4988
                   istr(tree_ident(unit)));
4989
         diag_emit(d);
2✔
4990
         return false;
2✔
4991
      }
4992
   }
4993

4994
   if (!sem_check_generic_map(t, unit, tab))
1,293✔
4995
      return false;
4996

4997
   if (!sem_check_port_map(t, unit, tab))
1,262✔
4998
      return false;
29✔
4999

5000
   return true;
5001
}
5002

5003
static bool sem_check_cond(tree_t t, nametab_t *tab)
11,880✔
5004
{
5005
   if (tree_has_value(t)) {
11,880✔
5006
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
9,565✔
5007

5008
      tree_t value = tree_value(t);
9,565✔
5009
      if (!sem_check(value, tab))
9,565✔
5010
         return false;
5011

5012
      if (!sem_check_type(value, std_bool, tab))
9,560✔
5013
         sem_error(value, "type of condition must be %s but have %s",
2✔
5014
                   type_pp(std_bool), type_pp(tree_type(value)));
5015

5016
      if (!sem_readable(value))
9,558✔
UNCOV
5017
         return false;
×
5018
   }
5019

5020
   return true;
5021
}
5022

5023
static bool sem_check_if(tree_t t, nametab_t *tab)
8,568✔
5024
{
5025
   bool ok = true;
8,568✔
5026
   const int nconds = tree_conds(t);
8,568✔
5027
   for (int i = 0; i < nconds; i++)
20,286✔
5028
      ok &= sem_check_cond(tree_cond(t, i), tab);
11,718✔
5029

5030
   return ok;
8,568✔
5031
}
5032

5033
static bool sem_static_subtype(type_t type, static_fn_t fn)
267,022✔
5034
{
5035
   // Rules for locally static subtypes are in LRM 93 7.4.1
5036

5037
   if (type_is_unconstrained(type))
267,022✔
5038
      return false;
5039

5040
   if (type_is_scalar(type))
241,033✔
5041
      return true;
5042

5043
   if (type_is_record(type)) {
236,970✔
5044
      const int nfields = type_fields(type);
24✔
5045
      for (int i = 0; i < nfields; i++) {
78✔
5046
         if (!sem_static_subtype(tree_type(type_field(type, i)), fn))
54✔
5047
            return false;
5048
      }
5049

5050
      return true;
5051
   }
5052

5053
   switch (type_kind(type)) {
236,946✔
5054
   case T_SUBTYPE:
236,946✔
5055
      {
5056
         const int ndims = dimension_of(type);
236,946✔
5057
         for (int i = 0; i < ndims; i++) {
265,751✔
5058
            if (!(*fn)(range_of(type, i)))
236,988✔
5059
               return false;
5060
         }
5061

5062
         return true;
5063
      }
5064
   default:
5065
      return true;
5066
   }
5067
}
5068

5069
static bool sem_ieee_locally_static(tree_t decl)
61,057✔
5070
{
5071
   // Subprograms definined in certain IEEE packages are treated the
5072
   // same as builtin operators in VHDL-2008
5073

5074
   if (standard() < STD_08)
61,057✔
5075
      return false;
5076

5077
   ident_t unit_name = tree_ident(tree_container(decl));
33,944✔
5078

5079
   switch (is_well_known(unit_name)) {
33,944✔
5080
   case W_NUMERIC_STD:
5081
   case W_NUMERIC_BIT:
5082
   case W_IEEE_1164:
5083
   case W_NUMERIC_BIT_UNSIGNED:
5084
   case W_NUMERIC_STD_UNSIGNED:
5085
      return true;
5086
   default:
25,121✔
5087
      return false;
25,121✔
5088
   }
5089
}
5090

5091
static bool sem_locally_static(tree_t t)
14,350,700✔
5092
{
5093
   // Rules for locally static expressions are in LRM 93 7.4.1
5094

5095
   type_t type = tree_type(t);
14,407,600✔
5096
   tree_kind_t kind = tree_kind(t);
14,407,600✔
5097

5098
   if (type_is_none(type))
14,407,600✔
5099
      return true;   // Prevents further cascading errors
5100

5101
   // Any literal other than of type time
5102
   if (kind == T_LITERAL) {
14,407,600✔
5103
      if (tree_subkind(t) == L_PHYSICAL)
140,951✔
5104
         return !type_eq(type, std_type(NULL, STD_TIME));
1,605✔
5105
      else
5106
         return true;
5107
   }
5108
   else if (kind == T_STRING)
14,266,600✔
5109
      return true;
5110
   else if ((kind == T_REF) && (tree_kind(tree_ref(t)) == T_ENUM_LIT))
14,248,200✔
5111
      return true;
5112
   else if (kind == T_OPEN)
14,072,900✔
5113
      return true;
5114

5115
   if (kind == T_REF) {
14,072,900✔
5116
      tree_t decl = tree_ref(t);
444,482✔
5117
      const tree_kind_t dkind = tree_kind(decl);
444,482✔
5118

5119
      // A constant reference (other than a deferred constant) with a
5120
      // locally static value
5121
      if (dkind == T_CONST_DECL) {
444,482✔
5122
         if (tree_has_value(decl))
39,539✔
5123
            return sem_locally_static(tree_value(decl));
39,187✔
5124
         else
5125
            return false;
5126
      }
5127

5128
      // An alias of a locally static name
5129
      if (dkind == T_ALIAS)
404,943✔
5130
         return sem_locally_static(tree_value(decl));
1,628✔
5131

5132
      // [2008] A generic reference with a locally static subtype
5133
      if (dkind == T_GENERIC_DECL && (standard() >= STD_08 || relaxed_rules()))
403,315✔
5134
         return sem_static_subtype(tree_type(decl), sem_locally_static);
1,474✔
5135
   }
5136

5137
   // A locally static range
5138
   if (kind == T_RANGE) {
14,030,200✔
5139
      switch (tree_subkind(t)) {
255,983✔
5140
      case RANGE_TO:
255,365✔
5141
      case RANGE_DOWNTO:
5142
         return sem_locally_static(tree_left(t))
255,365✔
5143
            && sem_locally_static(tree_right(t));
255,831✔
5144

5145
      case RANGE_EXPR:
618✔
5146
         return sem_locally_static(tree_value(t));
618✔
5147

5148
      default:
5149
         return false;
5150
      }
5151
   }
5152

5153
   // A function call of an implicit operator or [2008] an operation
5154
   // defined in one of the packages STD_LOGIC_1164, NUMERIC_BIT,
5155
   // NUMERIC_STD, NUMERIC_BIT_UNSIGNED, or NUMERIC_STD_UNSIGNED in
5156
   // library IEEE whose actuals are locally static expressions.
5157
   if (kind == T_FCALL) {
13,774,300✔
5158
      if (!tree_has_ref(t))
12,984,900✔
5159
         return true;  // Suppress further errors
5160
      else if (tree_flags(t) & TREE_F_LOCALLY_STATIC)
12,984,900✔
5161
         return true;
5162

5163
      tree_t decl = tree_ref(t);
12,847,500✔
5164
      if (tree_kind(decl) == T_GENERIC_DECL)
12,847,500✔
5165
         return false;   // Not known at this point
5166
      else if (tree_subkind(decl) == S_USER && !sem_ieee_locally_static(decl))
12,846,700✔
5167
         return false;
5168

5169
      const int nparams = tree_params(t);
12,794,500✔
5170
      for (int i = 0; i < nparams; i++) {
12,820,000✔
5171
         if (!sem_locally_static(tree_value(tree_param(t, i))))
12,807,600✔
5172
            return false;
5173
      }
5174

5175
      return true;
5176
   }
5177

5178
   if (kind == T_ATTR_REF) {
789,344✔
5179
      // A predefined attribute other than those listed below whose prefix
5180
      // prefix is either a locally static subtype or is an object that is
5181
      // of a locally static subtype
5182
      const attr_kind_t predef = tree_subkind(t);
243,700✔
5183
      if (predef == ATTR_EVENT || predef == ATTR_ACTIVE
243,700✔
5184
          || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
243,700✔
5185
          || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
242,752✔
5186
          || predef == ATTR_DRIVING_VALUE || predef == ATTR_PATH_NAME
242,635✔
5187
          || predef == ATTR_INSTANCE_NAME || predef == ATTR_SIMPLE_NAME)
241,852✔
5188
         return false;
5189
      else if (!tree_has_value(t)) {
238,799✔
5190
         type_t type = tree_type(tree_name(t));
237,442✔
5191
         return sem_static_subtype(type, sem_locally_static);
237,442✔
5192
      }
5193

5194
      // Whose actual parameter (if any) is a locally static expression
5195
      const int nparams = tree_params(t);
1,357✔
5196
      for (int i = 0; i < nparams; i++) {
1,357✔
5197
         if (!sem_locally_static(tree_value(tree_param(t, i))))
75✔
5198
            return false;
5199
      }
5200

5201
      // A user-defined attribute whose value is a locally static expression
5202
      assert(tree_has_value(t));
1,282✔
5203
      return sem_locally_static(tree_value(t));
1,282✔
5204
   }
5205

5206
   // A qualified expression whose operand is locally static
5207
   if (kind == T_QUALIFIED)
545,644✔
5208
      return sem_locally_static(tree_value(t));
4,609✔
5209

5210
   // A type conversion whose expression is locally static
5211
   if (kind == T_TYPE_CONV)
541,035✔
5212
      return sem_locally_static(tree_value(t));
2,754✔
5213

5214
   // Aggregates must have locally static range and all elements
5215
   // must have locally static values
5216
   if (kind == T_AGGREGATE) {
538,281✔
5217
      if (type_is_unconstrained(type))
17,522✔
5218
         return false;
5219

5220
      if (type_is_array(type)) {
17,438✔
5221
         if (!sem_locally_static(range_of(type, 0)))
16,975✔
5222
            return false;
5223
      }
5224

5225
      const int nassocs = tree_assocs(t);
17,343✔
5226
      for (int i = 0; i < nassocs; i++) {
188,555✔
5227
         tree_t a = tree_assoc(t, i);
171,509✔
5228
         if ((tree_subkind(a) == A_NAMED) && !sem_locally_static(tree_name(a)))
171,509✔
5229
            return false;
5230

5231
         if (!sem_locally_static(tree_value(a)))
171,509✔
5232
            return false;
5233
      }
5234

5235
      return true;
5236
   }
5237

5238
   // A record field name
5239
   if ((kind == T_REF) && (tree_kind(tree_ref(t)) == T_FIELD_DECL))
520,759✔
5240
      return true;
5241

5242
   const bool std08_rules = standard() >= STD_08 || relaxed_rules();
520,543✔
5243

5244
   // [2008] An indexed name whose prefix and index expressions are
5245
   // locally static
5246
   if (std08_rules && kind == T_ARRAY_REF) {
520,543✔
5247
      const int nparams = tree_params(t);
6,107✔
5248
      for (int i = 0; i < nparams; i++) {
7,648✔
5249
         if (!sem_locally_static(tree_value(tree_param(t, i))))
6,161✔
5250
            return false;
5251
      }
5252

5253
      return sem_locally_static(tree_value(t));
1,487✔
5254
   }
5255

5256
   // [2008] A slice name whose prefix and range is locally static
5257
   if (std08_rules && kind == T_ARRAY_SLICE) {
514,436✔
5258
      if (!sem_locally_static(tree_range(t, 0)))
1,503✔
5259
         return false;
5260

5261
      return sem_locally_static(tree_value(t));
297✔
5262
   }
5263

5264
   // [2008] A selected name whose prefix is locally static
5265
   if (std08_rules && kind == T_RECORD_REF)
512,933✔
5266
      return sem_locally_static(tree_value(t));
4,984✔
5267

5268
   return false;
5269
}
5270

5271
static bool sem_static_name(tree_t t, static_fn_t check_fn)
10,124✔
5272
{
5273
   // Rules for static names are in LRM 93 6.1
5274

5275
   switch (tree_kind(t)) {
11,239✔
5276
   case T_REF:
9,123✔
5277
      {
5278
         tree_t decl = tree_ref(t);
9,123✔
5279
         switch (tree_kind(decl)) {
9,123✔
5280
         case T_SIGNAL_DECL:
5281
         case T_VAR_DECL:
5282
         case T_CONST_DECL:
5283
         case T_PORT_DECL:
5284
         case T_TYPE_DECL:
5285
         case T_SUBTYPE_DECL:
5286
         case T_ENTITY:
5287
         case T_ARCH:
5288
         case T_PACK_BODY:
5289
         case T_PACKAGE:
5290
         case T_FUNC_BODY:
5291
         case T_PROC_BODY:
5292
         case T_FUNC_DECL:
5293
         case T_PROC_DECL:
5294
         case T_PROCESS:
5295
         case T_BLOCK:
5296
         case T_ENUM_LIT:
5297
         case T_IMPLICIT_SIGNAL:
5298
         case T_GENERIC_DECL:
5299
         case T_PARAM_DECL:
5300
         case T_CONCURRENT:
5301
         case T_VIEW_DECL:
5302
            return true;
5303
         case T_ALIAS:
26✔
5304
            return sem_static_name(tree_value(decl), check_fn);
26✔
UNCOV
5305
         default:
×
UNCOV
5306
            return false;
×
5307
         }
5308
      }
5309

5310
   case T_EXTERNAL_NAME:
5311
      return true;
5312

5313
   case T_RECORD_REF:
1,086✔
5314
      return sem_static_name(tree_value(t), check_fn);
1,086✔
5315

5316
   case T_ARRAY_REF:
843✔
5317
      {
5318
         if (!sem_static_name(tree_value(t), check_fn))
843✔
5319
            return false;
5320

5321
         const int nparams = tree_params(t);
843✔
5322
         for (int i = 0; i < nparams; i++) {
1,678✔
5323
            if (!(*check_fn)(tree_value(tree_param(t, i))))
846✔
5324
               return false;
5325
         }
5326

5327
         return true;
5328
      }
5329

5330
   case T_ARRAY_SLICE:
152✔
5331
      {
5332
         if (!sem_static_name(tree_value(t), check_fn))
152✔
5333
            return false;
5334

5335
         return (*check_fn)(tree_range(t, 0));
152✔
5336
      }
5337

5338
   case T_ATTR_REF:
3✔
5339
      {
5340
         switch (tree_subkind(t)) {
3✔
5341
         case ATTR_DELAYED:
3✔
5342
         case ATTR_STABLE:
5343
         case ATTR_QUIET:
5344
         case ATTR_TRANSACTION:
5345
            return sem_static_name(tree_name(t), check_fn);
3✔
5346
         default:
5347
            return false;
5348
         }
5349
      }
5350

5351
   default:
5✔
5352
      return false;
5✔
5353
   }
5354
}
5355

5356
static bool sem_globally_static(tree_t t)
770,383✔
5357
{
5358
   // Rules for globally static expressions are in LRM 93 7.4.2
5359

5360
   type_t type = tree_type(t);
774,990✔
5361
   tree_kind_t kind = tree_kind(t);
774,990✔
5362

5363
   if (type_is_none(type))
774,990✔
5364
      return true;   // Prevents further cascading errors
5365

5366
   // A literal of type TIME
5367

5368
   if (type_eq(type, std_type(NULL, STD_TIME))) {
774,990✔
5369
      if (kind == T_REF && tree_kind(tree_ref(t)) == T_UNIT_DECL)
5,618✔
5370
         return true;
5371
      else if (kind == T_LITERAL && tree_subkind(t) == L_PHYSICAL)
5,566✔
5372
         return true;
5373
   }
5374

5375
   // A locally static primary
5376

5377
   if (sem_locally_static(t))
774,322✔
5378
      return true;
5379

5380
   // A generic constant, generate parameter, or constant
5381

5382
   if (kind == T_REF) {
524,866✔
5383
      tree_t decl = tree_ref(t);
101,942✔
5384
      const tree_kind_t decl_kind = tree_kind(decl);
101,942✔
5385
      return decl_kind == T_GENERIC_DECL || decl_kind == T_CONST_DECL;
101,942✔
5386
   }
5387
   else if (kind == T_EXTERNAL_NAME)
422,924✔
5388
      return tree_class(t) == C_CONSTANT;
61✔
5389

5390
   // An alias whose aliased name is globally static
5391

5392
   if (kind == T_ALIAS)
422,863✔
UNCOV
5393
      return sem_globally_static(tree_value(t));
×
5394

5395
   if (kind == T_RANGE) {
422,863✔
5396
      if (tree_subkind(t) == RANGE_EXPR)
26,604✔
5397
         return sem_globally_static(tree_value(t));
16✔
5398

5399
      if (!sem_globally_static(tree_left(t)))
26,588✔
5400
         return false;
5401

5402
      if (!sem_globally_static(tree_right(t)))
26,585✔
5403
         return false;
5404

5405
      return true;
26,575✔
5406
   }
5407

5408
   // Aggregates must have globally static range and all elements
5409
   // must have globally static values
5410
   if (kind == T_AGGREGATE) {
396,259✔
5411
      if (type_is_array(type)
60✔
5412
          && !type_is_unconstrained(type)
34✔
5413
          && !sem_globally_static(range_of(type, 0)))
16✔
5414
         return false;
5415

5416
      const int nassocs = tree_assocs(t);
60✔
5417
      for (int i = 0; i < nassocs; i++) {
88✔
5418
         tree_t a = tree_assoc(t, i);
65✔
5419
         if ((tree_subkind(a) == A_NAMED) && !sem_globally_static(tree_name(a)))
65✔
5420
            return false;
5421

5422
         if (!sem_globally_static(tree_value(a)))
65✔
5423
            return false;
5424
      }
5425

5426
      return true;
5427
   }
5428

5429
   // A function call of a pure function with globally static actuals
5430
   if (kind == T_FCALL) {
396,199✔
5431
      tree_t decl = tree_ref(t);
324,089✔
5432
      if (tree_flags(decl) & TREE_F_IMPURE)
324,089✔
5433
         return false;
5434

5435
      bool all_static = true;
320,820✔
5436
      const int nparams = tree_params(t);
320,820✔
5437
      for (int i = 0; i < nparams; i++) {
952,541✔
5438
         tree_t p = tree_param(t, i);
631,721✔
5439
         all_static = all_static && sem_globally_static(tree_value(p));
711,841✔
5440
      }
5441
      return all_static;
320,820✔
5442
   }
5443

5444
   if (kind == T_ATTR_REF) {
72,110✔
5445
      const attr_kind_t predef = tree_subkind(t);
34,552✔
5446

5447
      // A predefined attribute that is one of 'SIMPLE_NAME,
5448
      // 'INSTANCE_NAME, or 'PATH_NAME
5449
      if (predef == ATTR_SIMPLE_NAME || predef == ATTR_INSTANCE_NAME
34,552✔
5450
          || predef == ATTR_PATH_NAME)
33,703✔
5451
         return true;   // Clause j
5452

5453
      // A predefined attribute other than those listed below whose
5454
      // prefix is either a globally static subtype or is an object or
5455
      // function call that is of a globally static subtype, or in 2008,
5456
      // a prefix which is a appropriate for a globally static attribute
5457
      if (predef == ATTR_EVENT || predef == ATTR_ACTIVE
33,445✔
5458
          || predef == ATTR_LAST_EVENT || predef == ATTR_LAST_ACTIVE
33,445✔
5459
          || predef == ATTR_LAST_VALUE || predef == ATTR_DRIVING
33,151✔
5460
          || predef == ATTR_DRIVING_VALUE)
33,104✔
5461
         return false;   // Clause k
5462
      else if (predef == ATTR_USER) {
33,080✔
5463
         // A user-defined attribute whose value is a globally static
5464
         // expression
5465
         return sem_globally_static(tree_value(t));
120✔
5466
      }
5467

5468
      tree_t name = tree_name(t);
32,960✔
5469

5470
      if (standard() >= STD_08 || relaxed_rules()) {
32,960✔
5471
         // LRM 08 section 9.4.3: A prefix is appropriate for a globally
5472
         // static attribute if it denotes a signal, a constant, a type
5473
         // or subtype, a globally static function call, a variable that
5474
         // is not of an access type, or a variable of an access type
5475
         // whose designated subtype is fully constrained.
5476

5477
         if (tree_kind(name) == T_FCALL)
4,963✔
5478
            return sem_globally_static(name);
5479

5480
         tree_t ref = name_to_ref(name);
4,963✔
5481
         if (ref == NULL)
4,963✔
5482
            return false;
5483

5484
         tree_t decl = tree_ref(ref);
4,962✔
5485
         const tree_kind_t dkind = tree_kind(decl);
4,962✔
5486
         if (dkind == T_VAR_DECL && type_is_access(tree_type(name)))
4,962✔
5487
            return false;
5488

5489
         return dkind == T_CONST_DECL || dkind == T_SIGNAL_DECL
4,962✔
5490
            || dkind == T_TYPE_DECL || dkind == T_VAR_DECL
4,933✔
5491
            || dkind == T_SUBTYPE_DECL || dkind == T_PORT_DECL
4,266✔
5492
            || dkind == T_GENERIC_DECL;
14,096✔
5493
      }
5494

5495
      type_t type = get_type_or_null(name);
27,997✔
5496
      if (type == NULL)
27,997✔
5497
         return false;
5498

5499
      return sem_static_subtype(type, sem_globally_static);
27,997✔
5500
   }
5501

5502
   // A qualified expression whose operand is globally static
5503

5504
   if (kind == T_QUALIFIED)
37,558✔
5505
      return sem_globally_static(tree_value(t));
237✔
5506

5507
   // A type conversion whose operand is globally static
5508

5509
   if (kind == T_TYPE_CONV)
37,321✔
5510
      return sem_globally_static(tree_value(t));
1,009✔
5511

5512
   // TODO: clauses o, p
5513

5514
   // A sub-element or slice where indexes are globally static
5515

5516
   if (kind == T_ARRAY_REF) {
36,312✔
5517
      if (!sem_globally_static(tree_value(t)))
29,963✔
5518
         return false;
5519

5520
      const int nparams = tree_params(t);
24,952✔
5521
      for (int i = 0; i < nparams; i++) {
49,520✔
5522
         if (!sem_globally_static(tree_value(tree_param(t, i))))
24,990✔
5523
            return false;
5524
      }
5525

5526
      return true;
5527
   }
5528
   else if (kind == T_ARRAY_SLICE) {
6,349✔
5529
      if (!sem_globally_static(tree_value(t)))
705✔
5530
         return false;
5531

5532
      if (!sem_globally_static(tree_range(t, 0)))
42✔
5533
         return false;
5534

5535
      return true;
42✔
5536
   }
5537
   else if (kind == T_RECORD_REF)
5,644✔
5538
      return sem_globally_static(tree_value(t));
3,225✔
5539

5540
   return false;
5541
}
5542

5543
static bool sem_check_case(tree_t t, nametab_t *tab)
588✔
5544
{
5545
   tree_t test = tree_value(t);
588✔
5546
   if (!sem_check(test, tab))
588✔
5547
      return false;
5548

5549
   type_t type = tree_type(test);
584✔
5550

5551
   // LRM 93 8.8 if the type of the expression is an array then it must be
5552
   // a one dimensional character array type
5553

5554
   const bool is_1d_character_array = type_is_character_array(type);
584✔
5555
   const bool valid = is_1d_character_array || type_is_discrete(type);
584✔
5556

5557
   if (!valid) {
584✔
5558
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(test));
2✔
5559
      diag_printf(d, "case expression must have a discrete type or one "
2✔
5560
                  "dimensional character array type");
5561
      if (type_is_array(type) && dimension_of(type) != 1)
2✔
5562
         diag_hint(d, tree_loc(test), "array has %d dimensions",
1✔
5563
                   dimension_of(type));
5564
      else if (type_is_array(type))
1✔
5565
         diag_hint(d, tree_loc(test), "type %s is not a character array",
1✔
5566
                   type_pp(type));
5567
      else
UNCOV
5568
         diag_hint(d, tree_loc(test), "type is %s", type_pp(type));
×
5569
      diag_lrm(d, STD_08, "10.9");
2✔
5570
      diag_emit(d);
2✔
5571
      return false;
2✔
5572
   }
5573

5574
   if (is_1d_character_array && standard() < STD_08) {
582✔
5575
      // VHDL-93 requires a locally static subtype, relaxed in later
5576
      // revisions
5577
      if (!sem_static_subtype(type, sem_locally_static))
54✔
5578
         sem_error(test, "case expression must have locally static subtype");
2✔
5579
   }
5580

5581
   static_fn_t static_fn = sem_locally_static;
580✔
5582
   const char *static_str = "locally";
580✔
5583

5584
   if (tree_kind(t) == T_CASE_GENERATE) {
580✔
5585
      static_fn = sem_globally_static;
7✔
5586
      static_str = "globally";
7✔
5587
   }
5588

5589
   const int nstmts = tree_stmts(t);
580✔
5590
   for (int i = 0; i < nstmts; i++) {
3,227✔
5591
      tree_t alt = tree_stmt(t, i);
2,660✔
5592

5593
      const int nassocs = tree_assocs(alt);
2,660✔
5594
      for (int j = 0; j < nassocs; j++) {
6,024✔
5595
         tree_t a = tree_assoc(alt, j);
3,377✔
5596
         switch (tree_subkind(a)) {
3,377✔
5597
         case A_OTHERS:
413✔
5598
            if (j != nassocs - 1 || i != nstmts - 1) {
413✔
5599
               diag_t *d = diag_new(DIAG_ERROR, tree_loc(a));
1✔
5600
               diag_printf(d, "others choice must appear last");
1✔
5601
               diag_hint(d, tree_loc(a), "others choice");
1✔
5602

5603
               tree_t more = j + 1 < nassocs
2✔
5604
                  ? tree_assoc(alt, j + 1) : tree_assoc(tree_stmt(t, i + 1), 0);
1✔
5605
               diag_hint(d, tree_loc(more), "further choices follow this");
1✔
5606

5607
               diag_emit(d);
1✔
5608
               return false;
1✔
5609
            }
5610
            break;
5611

5612
         case A_NAMED:
2,921✔
5613
            {
5614
               tree_t name = tree_name(a);
2,921✔
5615
               if (!sem_check(name, tab))
2,921✔
5616
                  return false;
5617

5618
               if (!sem_check_type(name, type, tab))
2,921✔
5619
                  sem_error(name, "case choice must have type %s but found %s",
1✔
5620
                            type_pp(type), type_pp(tree_type(name)));
5621
               else if (!(*static_fn)(name))
2,920✔
5622
                  sem_error(name, "case choice must be %s static", static_str);
8✔
5623
            }
5624
            break;
5625

5626
         case A_RANGE:
43✔
5627
            {
5628
               tree_t r = tree_range(a, 0);
43✔
5629
               if (!sem_check_discrete_range(r, type, tab))
43✔
5630
                  return false;
5631

5632
               switch (tree_subkind(r)) {
41✔
5633
               case RANGE_TO:
33✔
5634
               case RANGE_DOWNTO:
5635
                  if (!(*static_fn)(tree_left(r)))
33✔
UNCOV
5636
                     sem_error(tree_left(r), "left index of case choice "
×
5637
                               "range is not %s static", static_str);
5638
                  else if (!(*static_fn)(tree_right(r)))
33✔
5639
                     sem_error(tree_right(r), "right index of case choice "
1✔
5640
                               "range is not %s static", static_str);
5641
                  break;
5642

5643
               case RANGE_EXPR:
8✔
5644
                  if (!(*static_fn)(tree_value(r)))
8✔
UNCOV
5645
                     sem_error(tree_value(r), "range expression is not %s "
×
5646
                               "static", static_str);
5647
                  break;
5648

5649
               default:
5650
                  return false;
5651
               }
5652
            }
5653
            break;
5654
         }
5655
      }
3,364✔
5656
   }
5657

5658
   return true;
5659
}
5660

5661
static bool sem_check_match_case(tree_t t, nametab_t *tab)
40✔
5662
{
5663
   // Matching case statement is in LRM 08 section 10.9
5664

5665
   if (!sem_check_case(t, tab))
40✔
5666
      return false;
5667

5668
   tree_t value = tree_value(t);
39✔
5669
   type_t type = tree_type(value);
39✔
5670

5671
   type_t std_bit = std_type(NULL, STD_BIT);
39✔
5672
   type_t std_logic = ieee_type(IEEE_STD_ULOGIC);
39✔
5673

5674
   type_t elem = type;
39✔
5675
   if (type_is_array(type) && dimension_of(type) == 1)
39✔
5676
      elem = type_elem(type);
31✔
5677

5678
   if (!type_eq(elem, std_bit) && !type_eq(elem, std_logic)) {
39✔
5679
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
1✔
5680
      diag_printf(d, "type of expression in a matching case statement must be "
1✔
5681
                  "BIT, STD_ULOGIC, or a one-dimensional array of these types");
5682
      diag_hint(d, tree_loc(value), "type is %s", type_pp(type));
1✔
5683
      diag_lrm(d, STD_08, "10.9");
1✔
5684
      diag_emit(d);
1✔
5685
      return false;
1✔
5686
   }
5687

5688
   return true;
5689
}
5690

5691
static bool sem_check_return(tree_t t, nametab_t *tab)
12,740✔
5692
{
5693
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
12,740✔
5694
   if (sub == NULL)
12,740✔
5695
      sem_error(t, "return statement not allowed outside subprogram");
3✔
5696

5697
   if (tree_has_value(t)) {
12,737✔
5698
      if (tree_kind(sub) == T_PROC_BODY)
12,393✔
5699
         sem_error(t, "cannot return a value from a procedure");
1✔
5700

5701
      type_t expect = type_result(tree_type(sub));
12,392✔
5702

5703
      if (!sem_check(tree_value(t), tab))
12,392✔
5704
         return false;
5705

5706
      if (!sem_check_type(tree_value(t), expect, tab))
12,389✔
5707
         sem_error(t, "expected return type %s but have %s",
2✔
5708
                   type_pp(expect), type_pp(tree_type(tree_value(t))));
5709
   }
5710

5711
   return true;
5712
}
5713

5714
static bool sem_check_cond_return(tree_t t, nametab_t *tab)
7✔
5715
{
5716
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
7✔
5717
   if (sub == NULL)
7✔
5718
      sem_error(t, "return statement not allowed outside subprogram");
1✔
5719

5720
   if (tree_kind(sub) != T_PROC_BODY)
6✔
5721
      sem_error(t, "conditional return statement without value is only "
1✔
5722
                "valid inside a procedure");
5723

5724
   tree_t value = tree_value(t);
5✔
5725
   if (!sem_check(value, tab))
5✔
5726
      return false;
5727

5728
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
5✔
5729

5730
   if (!sem_check_type(value, std_bool, tab))
5✔
5731
      sem_error(value, "type of condition must be %s but have %s",
1✔
5732
                type_pp(std_bool), type_pp(tree_type(value)));
5733

5734
   return true;
5735
}
5736

5737
static bool sem_check_while(tree_t t, nametab_t *tab)
458✔
5738
{
5739
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
458✔
5740

5741
   tree_t value = tree_value(t);
458✔
5742
   if (!sem_check(value, tab))
458✔
5743
      return false;
5744

5745
   if (!sem_check_type(value, std_bool, tab))
458✔
5746
      sem_error(value, "type of loop condition must be %s but is %s",
1✔
5747
                type_pp(std_bool), type_pp(tree_type(value)));
5748

5749
   return true;
5750
}
5751

5752
static bool sem_check_for(tree_t t, nametab_t *tab)
2,412✔
5753
{
5754
   if (!sem_check_discrete_range(tree_range(t, 0), NULL, tab))
2,412✔
5755
      return false;
5756

5757
   tree_t idecl = tree_decl(t, 0);
2,398✔
5758

5759
   if (!sem_check_subtype(idecl, tree_type(idecl), tab))
2,398✔
UNCOV
5760
      return false;
×
5761

5762
   return true;
5763
}
5764

5765
static bool sem_check_block(tree_t t, nametab_t *tab)
272✔
5766
{
5767
   if (!sem_check_generic_map(t, t, tab))
272✔
5768
      return false;
5769

5770
   if (!sem_check_port_map(t, t, tab))
272✔
5771
      return false;
5772

5773
   const int ndecls = tree_decls(t);
271✔
5774
   for (int i = 0; i < ndecls; i++) {
932✔
5775
      tree_t d = tree_decl(t, i);
661✔
5776
      sem_check_static_elab(d);
661✔
5777
   }
5778

5779
   return true;
5780
}
5781

5782
static bool sem_check_loop_control(tree_t t, nametab_t *tab)
409✔
5783
{
5784
   if (tree_has_value(t)) {
409✔
5785
      tree_t value = tree_value(t);
195✔
5786
      if (!sem_check(value, tab))
195✔
5787
         return false;
5788

5789
      type_t std_bool = std_type(NULL, STD_BOOLEAN);
195✔
5790
      if (!type_eq(tree_type(value), std_bool))
195✔
5791
         sem_error(value, "type of %s condition must be %s but is %s",
3✔
5792
                   (tree_kind(t) == T_EXIT) ? "exit" : "next",
5793
                   type_pp(std_bool), type_pp(tree_type(value)));
5794
   }
5795

5796
   return true;
5797
}
5798

5799
static bool sem_check_attr_decl(tree_t t)
113✔
5800
{
5801
   if (!sem_no_access_file_or_protected(t, tree_type(t), "attributes"))
113✔
5802
      return false;
5✔
5803

5804
   return true;
5805
}
5806

5807
static bool sem_check_attr_spec(tree_t t, nametab_t *tab)
313✔
5808
{
5809
   tree_t value = tree_value(t);
313✔
5810
   if (!sem_check(value, tab))
313✔
5811
      return false;
5812

5813
   type_t type = tree_type(t);
313✔
5814
   if (!sem_check_type(value, type, tab))
313✔
5815
      sem_error(t, "expected attribute specification for %s to have type %s "
2✔
5816
                "but found %s", istr(tree_ident(t)), type_pp(type),
5817
                type_pp(tree_type(value)));
5818

5819
   if (!tree_has_ref(t))
311✔
5820
      return false;
5821

5822
   tree_t decl = tree_ref(t);
299✔
5823
   const class_t class = tree_class(t);
299✔
5824

5825
   if (class_of(decl) != class)
299✔
5826
      sem_error(t, "class of object %s is %s not %s",
1✔
5827
                istr(tree_ident(decl)), class_str(class_of(decl)),
5828
                class_str(class));
5829

5830
   switch (is_well_known(tree_ident(t))) {
298✔
5831
   case W_NEVER_WAITS:
110✔
5832
      {
5833
         bool flag;
110✔
5834
         if (!folded_bool(value, &flag))
110✔
5835
            sem_error(value, "expression must be a BOOLEAN literal");
2✔
5836
         else if (class != C_PROCEDURE)
109✔
5837
            sem_error(t, "NEVER_WAITS attribute can only be applied to "
1✔
5838
                      "procedures");
5839
         else if (flag && !tree_frozen(decl))
108✔
5840
            tree_set_flag(decl, TREE_F_NEVER_WAITS);
107✔
5841
      }
5842
      break;
108✔
5843

5844
   case W_FOREIGN:
95✔
5845
      if (!tree_frozen(decl))
95✔
5846
         tree_set_flag(decl, TREE_F_NEVER_WAITS);
88✔
5847
      break;
5848

5849
   default:
5850
      break;
5851
   }
5852

5853
   return true;
5854
}
5855

5856
static bool sem_check_if_generate(tree_t t, nametab_t *tab)
152✔
5857
{
5858
   const int nconds = tree_conds(t);
152✔
5859
   for (int i = 0; i < nconds; i++) {
311✔
5860
      tree_t cond = tree_cond(t, i);
162✔
5861

5862
      if (!sem_check_cond(cond, tab))
162✔
5863
         return false;
5864

5865
      if (tree_has_value(cond)) {
160✔
5866
         tree_t value = tree_value(cond);
151✔
5867
         if (!sem_globally_static(value))
151✔
5868
            sem_error(value, "condition of generate statement must be static");
160✔
5869
      }
5870
   }
5871

5872
   return true;
5873
}
5874

5875
static bool sem_check_for_generate(tree_t t, nametab_t *tab)
154✔
5876
{
5877
   tree_t r = tree_range(t, 0);
154✔
5878
   if (!sem_check_discrete_range(r, NULL, tab))
154✔
5879
      return false;
5880

5881
   if (!sem_globally_static(r))
152✔
5882
      sem_error(r, "range of generate statement must be static");
1✔
5883

5884
   tree_t idecl = tree_decl(t, 0);
151✔
5885
   assert(tree_kind(idecl) == T_GENERIC_DECL);
151✔
5886

5887
   if (!sem_check_subtype(idecl, tree_type(idecl), tab))
151✔
UNCOV
5888
      return false;
×
5889

5890
   return true;
5891
}
5892

5893
static bool sem_check_open(tree_t t)
5894
{
5895
   return true;
5896
}
5897

5898
static bool sem_check_file_decl(tree_t t, nametab_t *tab)
142✔
5899
{
5900
   // Rules for file declarations are in LRM 93 section 4.3.1.4
5901

5902
   if (!type_is_file(tree_type(t)))
142✔
5903
      sem_error(t, "file declarations must have file type");
1✔
5904

5905
   if (tree_has_value(t)) {
141✔
5906
      tree_t value = tree_value(t);
43✔
5907
      if (!sem_check(value, tab))
43✔
5908
         return false;
5909

5910
      if (!sem_check_type(value, std_type(NULL, STD_STRING), tab))
43✔
5911
         sem_error(value, "file name must have type STRING");
1✔
5912

5913
      tree_t mode = tree_file_mode(t);
42✔
5914
      if (!sem_check(mode, tab))
42✔
5915
         return false;
5916

5917
      if (!sem_check_type(mode, std_type(NULL, STD_FILE_OPEN_KIND), tab))
41✔
5918
         sem_error(mode, "open mode must have type FILE_OPEN_KIND");
1✔
5919
   }
5920

5921
   tree_t sub = find_enclosing(tab, S_SUBPROGRAM);
138✔
5922
   const bool in_pure_func =
276✔
5923
      sub != NULL && tree_kind(sub) == T_FUNC_BODY
22✔
5924
      && !(tree_flags(sub) & TREE_F_IMPURE);
141✔
5925

5926
   if (in_pure_func) {
138✔
5927
      diag_t *d = pedantic_diag(t);
1✔
5928
      if (d != NULL) {
1✔
5929
         diag_printf(d, "cannot declare a file object in a pure function");
1✔
5930
         diag_emit(d);
1✔
5931
      }
5932
   }
5933

5934
   return true;
5935
}
5936

5937
static bool sem_check_new(tree_t t, nametab_t *tab)
541✔
5938
{
5939
   // Rules for allocators are in LRM 93 section 7.3.6
5940

5941
   tree_t value = tree_value(t);
541✔
5942
   type_t access_type = tree_type(t);
541✔
5943

5944
   if (type_is_none(access_type))
541✔
5945
      return false;
5946

5947
   assert(type_is_access(access_type));
540✔
5948
   assert(tree_kind(value) == T_QUALIFIED);
540✔
5949

5950
   if (!sem_check(value, tab))
540✔
5951
      return false;
5952

5953
   type_t type = tree_type(value);
540✔
5954

5955
   if (type_is_none(type))
540✔
5956
      return false;
5957

5958
   if (!sem_check_subtype(value, type, tab))
537✔
5959
      return false;
5960

5961
   const bool has_initial = tree_has_value(value);
537✔
5962

5963
   if (!has_initial && type_is_unconstrained(type))
537✔
5964
      sem_error(t, "unconstrained array type %s not allowed in allocator "
1✔
5965
                "expression", type_pp(type));
5966
   else if (!sem_check_incomplete(t, type))
536✔
5967
      return false;
5968
   else if (type_is_protected(type)) {
535✔
5969
      if (has_initial)
11✔
5970
         sem_error(t, "protected type %s cannot have initial value",
1✔
5971
                   type_pp(type));
5972
      else if (standard() >= STD_19)
10✔
5973
         tree_set_global_flags(t, TREE_GF_INSTANCE_NAME | TREE_GF_PATH_NAME);
10✔
5974
   }
5975

5976
   type_t designated = type_designated(access_type);
534✔
5977

5978
   if (!type_eq(type, designated))
534✔
5979
      sem_error(value, "type of allocator expresion %s does not match "
1✔
5980
                "access type %s", type_pp(type), type_pp(designated));
5981

5982
   return true;
5983
}
5984

5985
static bool sem_check_all(tree_t t, nametab_t *tab)
2,336✔
5986
{
5987
   if (!sem_check_name_prefix(t, tab, "a selected name"))
2,336✔
5988
      return false;
5989

5990
   tree_t value = tree_value(t);
2,335✔
5991
   type_t value_type = tree_type(value);
2,335✔
5992

5993
   if (type_is_none(value_type))
2,335✔
5994
      return false;
5995

5996
   if (!type_is_access(value_type)) {
2,334✔
5997
      diag_t *d = diag_new(DIAG_ERROR, tree_loc(value));
2✔
5998
      diag_printf(d, "prefix of a selected name with suffix ALL must "
2✔
5999
                  "have access type");
6000
      diag_hint(d, tree_loc(value), "prefix has type %s", type_pp(value_type));
2✔
6001
      diag_lrm(d, STD_08, "8.3");
2✔
6002
      diag_emit(d);
2✔
6003
      return false;
2✔
6004
   }
6005

6006
   return true;
6007
}
6008

6009
static bool sem_check_binding(tree_t t, nametab_t *tab)
163✔
6010
{
6011
   if (!tree_has_ref(t))
163✔
6012
      return false;
6013

6014
   tree_t unit = primary_unit_of(tree_ref(t));
160✔
6015
   if (tree_kind(unit) == T_ENTITY) {
160✔
6016
      if (tree_genmaps(t) > 0 && !sem_check_generic_map(t, unit, tab))
160✔
6017
         return false;
6018

6019
      if (tree_params(t) > 0 && !sem_check_port_map(t, unit, tab))
160✔
UNCOV
6020
         return false;
×
6021
   }
6022

6023
   return true;
6024
}
6025

6026
static bool sem_check_block_config(tree_t t, nametab_t *tab)
6027
{
6028
   return true;
6029
}
6030

6031
static bool sem_check_spec(tree_t t, nametab_t *tab)
173✔
6032
{
6033
   if (!tree_has_ref(t))
173✔
6034
      return false;
6035

6036
   tree_t comp = tree_ref(t);
169✔
6037
   assert(tree_kind(comp) == T_COMPONENT);
169✔
6038

6039
   if (!tree_has_value(t))
169✔
6040
      return true;
6041

6042
   tree_t bind = tree_value(t);
163✔
6043
   assert(tree_kind(bind) == T_BINDING);
163✔
6044

6045
   if (!sem_check(bind, tab))
163✔
6046
      return false;
6047

6048
   tree_t unit = tree_ref(bind);
160✔
6049
   tree_t entity = primary_unit_of(unit);
160✔
6050
   assert(tree_kind(entity) == T_ENTITY);
160✔
6051

6052
   bool ok = true;
160✔
6053

6054
   if (tree_genmaps(bind) == 0) {
160✔
6055
      const int c_ngenerics = tree_generics(comp);
128✔
6056
      const int e_ngenerics = tree_generics(entity);
128✔
6057

6058
      bit_mask_t have;
128✔
6059
      mask_init(&have, e_ngenerics);
128✔
6060

6061
      bool have_named = false;
128✔
6062
      for (int i = 0; i < c_ngenerics; i++) {
278✔
6063
         tree_t cg = tree_generic(comp, i);
150✔
6064

6065
         int epos = 0;
150✔
6066
         tree_t match = NULL;
150✔
6067
         for (; epos < e_ngenerics; epos++) {
2,340✔
6068
            tree_t eg = tree_generic(entity, epos);
2,188✔
6069
            if (tree_ident(eg) == tree_ident(cg)) {
2,188✔
6070
               match = eg;
148✔
6071
               mask_set(&have, epos);
148✔
6072
               break;
6073
            }
6074
         }
6075

6076
         if (match == NULL) {
150✔
6077
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
6078
            diag_printf(d, "generic %s in component %s has no corresponding "
2✔
6079
                        "generic in entity %s", istr(tree_ident(cg)),
6080
                        istr(tree_ident(comp)), istr(tree_ident(entity)));
6081
            diag_hint(d, tree_loc(cg), "generic %s declared here",
2✔
6082
                      istr(tree_ident(cg)));
6083
            diag_emit(d);
2✔
6084

6085
            ok = false;
2✔
6086
            continue;
2✔
6087
         }
6088

6089
         type_t ctype = tree_type(cg);
148✔
6090
         type_t etype = tree_type(match);
148✔
6091
         if (!type_eq(ctype, etype)) {
148✔
6092
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6093
            diag_printf(d, "generic %s in component %s has type %s which is "
1✔
6094
                        "incompatible with type %s in entity %s",
6095
                        istr(tree_ident(cg)), istr(tree_ident(comp)),
6096
                        type_pp2(ctype, etype), type_pp2(etype, ctype),
6097
                        istr(tree_ident(entity)));
6098
            diag_hint(d, tree_loc(cg), "declaration of generic %s in component",
1✔
6099
                      istr(tree_ident(cg)));
6100
            diag_hint(d, tree_loc(match), "declaration of generic %s in entity",
1✔
6101
                      istr(tree_ident(match)));
6102
            diag_emit(d);
1✔
6103

6104
            ok = false;
1✔
6105
            continue;
1✔
6106
         }
6107

6108
         tree_t map = tree_new(T_PARAM);
147✔
6109
         tree_set_loc(map, tree_loc(t));
147✔
6110
         tree_set_value(map, make_ref(cg));
147✔
6111

6112
         if (!have_named && epos == i) {
147✔
6113
            tree_set_subkind(map, P_POS);
68✔
6114
            tree_set_pos(map, epos);
68✔
6115
         }
6116
         else {
6117
            tree_set_subkind(map, P_NAMED);
79✔
6118
            tree_set_name(map, make_ref(match));
79✔
6119
            have_named = true;
79✔
6120
         }
6121

6122
         tree_add_genmap(bind, map);
147✔
6123
      }
6124

6125
      for (int i = 0; i < e_ngenerics; i++) {
307✔
6126
         tree_t eg = tree_generic(entity, i);
179✔
6127
         if (!mask_test(&have, i) && !tree_has_value(eg)) {
179✔
6128
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
3✔
6129
            diag_printf(d, "generic %s in entity %s without a default value "
3✔
6130
                        "has no corresponding generic in component %s",
6131
                        istr(tree_ident(eg)),  istr(tree_ident(entity)),
6132
                        istr(tree_ident(comp)));
6133
            diag_hint(d, tree_loc(eg), "generic %s declared here",
3✔
6134
                      istr(tree_ident(eg)));
6135
            diag_emit(d);
3✔
6136

6137
            ok = false;
3✔
6138
            continue;
3✔
6139
         }
6140
      }
6141

6142
      mask_free(&have);
128✔
6143
   }
6144

6145
   if (tree_params(bind) == 0) {
160✔
6146
      const int c_nports = tree_ports(comp);
132✔
6147
      const int e_nports = tree_ports(entity);
132✔
6148

6149
      bit_mask_t have;
132✔
6150
      mask_init(&have, e_nports);
132✔
6151

6152
      bool have_named = false;
132✔
6153
      for (int i = 0; i < c_nports; i++) {
975✔
6154
         tree_t cp = tree_port(comp, i);
843✔
6155

6156
         int epos = 0;
843✔
6157
         tree_t match = NULL;
843✔
6158
         for (; match == NULL && epos < e_nports; epos++) {
8,084✔
6159
            tree_t ep = tree_port(entity, epos);
7,236✔
6160
            if (tree_ident(ep) == tree_ident(cp)) {
7,236✔
6161
               match = ep;
838✔
6162
               mask_set(&have, epos);
838✔
6163
               break;
6164
            }
6165
         }
6166

6167
         if (match == NULL) {
843✔
6168
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
5✔
6169
            diag_printf(d, "port %s in component %s has no corresponding port "
5✔
6170
                        "in entity %s", istr(tree_ident(cp)),
6171
                        istr(tree_ident(comp)), istr(tree_ident(entity)));
6172
            diag_hint(d, tree_loc(cp), "port %s declared here",
5✔
6173
                      istr(tree_ident(cp)));
6174
            diag_emit(d);
5✔
6175

6176
            ok = false;
5✔
6177
            continue;
5✔
6178
         }
6179

6180
         type_t ctype = tree_type(cp);
838✔
6181
         type_t etype = tree_type(match);
838✔
6182
         if (!type_eq(ctype, etype)) {
838✔
6183
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6184
            diag_printf(d, "port %s in component %s has type %s which is "
1✔
6185
                        "incompatible with type %s in entity %s",
6186
                        istr(tree_ident(cp)), istr(tree_ident(comp)),
6187
                        type_pp2(ctype, etype), type_pp2(etype, ctype),
6188
                        istr(tree_ident(entity)));
6189
            diag_hint(d, tree_loc(cp), "declaration of port %s in component",
1✔
6190
                      istr(tree_ident(cp)));
6191
            diag_hint(d, tree_loc(match), "declaration of port %s in entity",
1✔
6192
                      istr(tree_ident(match)));
6193
            diag_emit(d);
1✔
6194

6195
            ok = false;
1✔
6196
            continue;
1✔
6197
         }
6198

6199
         if (!have_named && epos == i)
837✔
6200
            add_param(bind, make_ref(cp), P_POS, NULL);
819✔
6201
         else {
6202
            add_param(bind, make_ref(cp), P_NAMED, make_ref(match));
18✔
6203
            have_named = true;
18✔
6204
         }
6205
      }
6206

6207
      for (int i = 0; i < e_nports; i++) {
977✔
6208
         if (mask_test(&have, i))
845✔
6209
            continue;
838✔
6210

6211
         tree_t ep = tree_port(entity, i);
7✔
6212

6213
         const bool open_ok =
14✔
6214
            tree_has_value(ep)
7✔
6215
            || (tree_subkind(ep) == PORT_OUT
7✔
6216
                && !type_is_unconstrained(tree_type(ep)));
1✔
6217

6218
         if (!open_ok) {
7✔
6219
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
2✔
6220
            diag_printf(d, "port %s in entity %s without a default value "
2✔
6221
                        "has no corresponding port in component %s",
6222
                        istr(tree_ident(ep)), istr(tree_ident(entity)),
6223
                        istr(tree_ident(comp)));
6224
            diag_hint(d, tree_loc(ep), "port %s declared here",
2✔
6225
                      istr(tree_ident(ep)));
6226
            diag_emit(d);
2✔
6227

6228
            ok = false;
2✔
6229
            continue;
2✔
6230
         }
6231
      }
6232
   }
6233

6234
   return ok;
6235
}
6236

6237
static bool sem_check_configuration(tree_t t, nametab_t *tab)
6238
{
6239
   if (!tree_has_primary(t))
6240
      return false;   // Was parse error
6241

6242
   tree_t of = tree_primary(t);
6243

6244
   // LRM 08 section 3.4.1: for a configuration of a given design
6245
   // entity, both the configuration declaration and the corresponding
6246
   // entity declaration shall reside in the same library
6247
   ident_t elib = ident_until(tree_ident(of), '.');
6248
   if (standard() < STD_19 && elib != lib_name(lib_work())) {
6249
      diag_t *d = pedantic_diag(t);
6250
      if (d != NULL) {
6251
         ident_t ename = ident_rfrom(tree_ident(of), '.');
6252
         diag_printf(d, "configuration declaration %s must reside in the "
6253
                     "same library as entity %s", istr(tree_ident(t)),
6254
                     istr(ename));
6255
         diag_hint(d, NULL, "entity %s is in library %s which is not the same "
6256
                   "as the current working library %s",
6257
                   istr(ename), istr(elib), istr(lib_name(lib_work())));
6258
         diag_lrm(d, STD_08, "3.4");
6259
         diag_emit(d);
6260
         return false;
6261
      }
6262
   }
6263

6264
   return true;
6265
}
6266

6267
static bool sem_check_prot_body(tree_t t, nametab_t *tab)
6268
{
6269
   // Rules for protected type bodies are in LRM 00 section 3.5.2
6270

6271
   type_t type = tree_type(t);
6272
   if (type_is_none(type))
6273
      return false;
6274

6275
   return true;
6276
}
6277

6278
static bool sem_check_implicit_signal(tree_t t, nametab_t *tab)
23✔
6279
{
6280
   tree_t value = tree_value(t);
23✔
6281
   type_t type  = tree_type(t);
23✔
6282

6283
   if (!sem_check(value, tab))
23✔
6284
      return false;
6285

6286
   switch (tree_subkind(t)) {
23✔
6287
   case IMPLICIT_GUARD:
23✔
6288
      if (!sem_check_type(value, type, tab))
23✔
6289
         sem_error(value, "guard expression must have type %s but "
1✔
6290
                   "found %s", type_pp2(type, tree_type(value)),
6291
                   type_pp2(tree_type(value), type));
6292
      break;
6293
   }
6294

6295
   return true;
6296
}
6297

6298
static bool sem_check_context_decl(tree_t t, nametab_t *tab)
19✔
6299
{
6300
   // Context declarations are in LRM 08 section 13.3
6301

6302
   if (!sem_check_context_clause(t, tab))
19✔
6303
      return false;
1✔
6304

6305
   return true;
6306
}
6307

6308
static bool sem_check_context_ref(tree_t t, nametab_t *tab)
21✔
6309
{
6310
   if (standard() >= STD_08) {
21✔
6311
      tree_t unit = find_enclosing(tab, S_DESIGN_UNIT);
21✔
6312

6313
      if (unit != NULL && tree_kind(unit) == T_CONTEXT) {
21✔
6314
         // LRM 08 section 13.3
6315
         ident_t prefix = ident_until(tree_ident(t), '.');
6✔
6316
         if (prefix == well_known(W_WORK))
6✔
6317
            sem_error(t, "selected name in context declaration context "
1✔
6318
                      "reference may not have WORK as a prefix");
6319
      }
6320
   }
6321

6322
   return true;
6323
}
6324

6325
static bool sem_check_disconnect(tree_t t, nametab_t *tab)
11✔
6326
{
6327
   if (!tree_has_ref(t))
11✔
6328
      return false;
6329

6330
   tree_t decl = tree_ref(t);
11✔
6331
   if (class_of(decl) != C_SIGNAL || !is_guarded_signal(decl))
11✔
6332
      sem_error(t, "signal name %s in disconnection specification must denote "
3✔
6333
                "a guarded signal", istr(tree_ident(t)));
6334

6335
   type_t type = tree_type(t);
8✔
6336
   if (!type_eq(tree_type(decl), type))
8✔
6337
      sem_error(t, "type of declared signal %s does not match type %s in "
1✔
6338
                "disconnection specification", type_pp(tree_type(decl)),
6339
                type_pp(type));
6340

6341
   tree_t delay = tree_delay(t);
7✔
6342
   type_t std_time = std_type(NULL, STD_TIME);
7✔
6343
   if (!sem_check_type(delay, std_time, tab))
7✔
6344
      sem_error(delay, "time expression in disconnection specification must "
1✔
6345
                "have type %s but found %s", type_pp(std_time),
6346
                type_pp(tree_type(delay)));
6347

6348
   if (!sem_globally_static(delay))
6✔
6349
      sem_error(delay, "time expression in disconnection specificiation "
1✔
6350
                "must be static");
6351

6352
   return true;
6353
}
6354

6355
static bool sem_check_conv_func(tree_t t, nametab_t *tab)
141✔
6356
{
6357
   if (type_is_none(tree_type(t)))
141✔
6358
      return false;
6359
   else if (!tree_has_ref(t))
141✔
6360
      return false;
6361

6362
   if (!sem_check(tree_value(t), tab))
141✔
UNCOV
6363
      return false;
×
6364

6365
   return true;
6366
}
6367

6368
static bool sem_check_concurrent(tree_t t, nametab_t *tab)
2,958✔
6369
{
6370
   if (tree_has_guard(t) && !sem_check_guard(tree_guard(t), tab))
2,958✔
6371
      return false;
6372

6373
   if (tree_stmts(t) == 0)
2,956✔
6374
      return false;   // Was parse error
6375

6376
   return sem_check(tree_stmt(t, 0), tab);
2,956✔
6377
}
6378

6379
static bool sem_check_external_name(tree_t t, nametab_t *tab)
127✔
6380
{
6381
   const int nparts = tree_parts(t);
127✔
6382
   for (int i = 0; i < nparts; i++) {
585✔
6383
      tree_t pe = tree_part(t, i);
460✔
6384
      switch (tree_subkind(pe)) {
460✔
6385
      case PE_GENERATE:
31✔
6386
         {
6387
            tree_t value = tree_value(pe);
31✔
6388
            if (!sem_globally_static(value))
31✔
6389
               sem_error(value, "generate index must be a static expression");
1✔
6390
         }
6391
         break;
6392
      case PE_RELATIVE:
95✔
6393
         // Relative pathnames must have enclosing concurrent region
6394
         if (find_enclosing(tab, S_CONCURRENT_BLOCK) == NULL) {
95✔
6395
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(t));
1✔
6396
            diag_printf(d, "relative pathname has no enclosing "
1✔
6397
                        "concurrent region");
6398
            diag_lrm(d, STD_08, "8.7");
1✔
6399
            diag_emit(d);
1✔
6400
            return false;
1✔
6401
         }
6402
      }
6403
   }
6404

6405
   // Cannot do any more checking until elaboration
6406
   return true;
6407
}
6408

6409
static port_mode_t sem_default_force_mode(tree_t target)
78✔
6410
{
6411
   // Rules for default force mode in LRM 08 section 10.5.2.1
6412

6413
   tree_t ref = name_to_ref(target);
78✔
6414
   if (ref == NULL || !tree_has_ref(ref))
78✔
6415
      return PORT_IN;
14✔
6416

6417
   tree_t decl = tree_ref(ref);
64✔
6418
   const tree_kind_t dkind = tree_kind(decl);
64✔
6419
   if (dkind == T_PORT_DECL || dkind == T_PARAM_DECL) {
64✔
6420
      switch (tree_subkind(decl)) {
6✔
6421
      case PORT_OUT:
6422
      case PORT_INOUT:
6423
      case PORT_BUFFER:
6424
         return PORT_OUT;
UNCOV
6425
      default:
×
UNCOV
6426
         return PORT_IN;
×
6427
      }
6428
   }
6429

6430
   return PORT_IN;
6431
}
6432

6433
static bool sem_check_force_target(tree_t target, port_mode_t mode,
89✔
6434
                                   const char *what)
6435
{
6436
   tree_t decl = sem_check_lvalue(target);
89✔
6437
   if (decl == NULL)
89✔
6438
      sem_error(target, "target of simple %s assignment must be a "
1✔
6439
                "signal name", what);
6440

6441
   switch (tree_kind(decl)) {
88✔
6442
   case T_SIGNAL_DECL:
6443
      break;
6444

6445
   case T_PORT_DECL:
9✔
6446
   case T_PARAM_DECL:
6447
      if (tree_class(decl) != C_SIGNAL) {
9✔
UNCOV
6448
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
×
UNCOV
6449
         diag_printf(d, "%s is not a valid target of simple %s assignment",
×
6450
                     what, istr(tree_ident(decl)));
UNCOV
6451
         diag_hint(d, tree_loc(target), "target of simple %s assignment", what);
×
UNCOV
6452
         diag_hint(d, tree_loc(decl), "declared with class %s",
×
6453
                   class_str(tree_class(decl)));
UNCOV
6454
         diag_emit(d);
×
UNCOV
6455
         return false;
×
6456
      }
6457
      else if (mode == PORT_OUT && tree_subkind(decl) == PORT_IN)
9✔
6458
         sem_error(target, "force mode OUT may not be used with target "
1✔
6459
                   "of mode IN");
6460
      break;
6461

6462
      case T_EXTERNAL_NAME:
32✔
6463
         if (tree_class(decl) != C_SIGNAL) {
32✔
6464
            tree_t tail = tree_part(decl, tree_parts(decl) - 1);
1✔
6465
            diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
1✔
6466
            diag_printf(d, "external name %s is not a valid target of "
1✔
6467
                        "simple %s assignment", istr(tree_ident(tail)), what);
6468
            diag_hint(d, tree_loc(target), "target of signal assignment");
1✔
6469
            diag_hint(d, tree_loc(decl), "declared with class %s",
1✔
6470
                      class_str(tree_class(decl)));
6471
            diag_emit(d);
1✔
6472
            return false;
1✔
6473
         }
6474
         break;
6475

6476
   case T_VAR_DECL:
2✔
6477
   case T_CONST_DECL:
6478
      {
6479
         diag_t *d = diag_new(DIAG_ERROR, tree_loc(target));
2✔
6480
         diag_printf(d, "%s %s is not a valid target of simple %s assignment",
2✔
6481
                     class_str(class_of(decl)), istr(tree_ident(decl)), what);
6482
         diag_hint(d, tree_loc(target), "target of simple %s assignment", what);
2✔
6483
         diag_hint(d, tree_loc(decl), "declared as %s",
2✔
6484
                   class_str(class_of(decl)));
6485
         diag_emit(d);
2✔
6486
         return false;
2✔
6487
      }
6488

UNCOV
6489
   default:
×
UNCOV
6490
      sem_error(target, "invalid target of simple %s assignment", what);
×
6491
   }
6492

6493
   return true;
6494
}
6495

6496
static bool sem_check_force(tree_t t, nametab_t *tab)
55✔
6497
{
6498
   tree_t target = tree_target(t);
55✔
6499

6500
   if (!sem_check(target, tab))
55✔
6501
      return false;
6502

6503
   port_mode_t mode = tree_subkind(t);
54✔
6504
   if (mode == PORT_INVALID)
54✔
6505
      tree_set_subkind(t, (mode = sem_default_force_mode(target)));
45✔
6506

6507
   if (!sem_check_force_target(target, mode, "force"))
54✔
6508
      return false;
6509

6510
   tree_t value = tree_value(t);
51✔
6511
   if (!sem_check(value, tab))
51✔
6512
      return false;
6513

6514
   type_t expect = tree_type(target);
51✔
6515

6516
   if (!sem_check_type(value, expect, tab))
51✔
6517
      sem_error(t, "type of force expression %s does not match type of "
2✔
6518
                "target %s", type_pp2(tree_type(value), expect),
6519
                type_pp2(expect, tree_type(value)));
6520

6521
   return true;
6522
}
6523

6524
static bool sem_check_release(tree_t t, nametab_t *tab)
36✔
6525
{
6526
   tree_t target = tree_target(t);
36✔
6527

6528
   if (!sem_check(target, tab))
36✔
6529
      return false;
6530

6531
   port_mode_t mode = tree_subkind(t);
35✔
6532
   if (mode == PORT_INVALID)
35✔
6533
      tree_set_subkind(t, (mode = sem_default_force_mode(target)));
33✔
6534

6535
   if (!sem_check_force_target(target, mode, "release"))
35✔
6536
      return false;
2✔
6537

6538
   return true;
6539
}
6540

6541
static bool sem_check_prot_ref(tree_t t, nametab_t *tab)
6542
{
6543
   if (standard() >= STD_19)
6544
      return true;   // Alias of private variable method
6545

6546
   // There are no legal ways this can appear here and should always
6547
   // have been converted to a call
6548
   assert(error_count() > 0);
6549

6550
   return false;
6551
}
6552

6553
static bool sem_check_view_decl(tree_t t, nametab_t *tab)
6554
{
6555
   type_t type = tree_type(t);
6556
   if (type_is_none(type))
6557
      return false;
6558

6559
   assert(type_kind(type) == T_VIEW);
6560

6561
   type_t rtype = type_designated(type);
6562
   if (!type_is_record(rtype)) {
6563
      assert(error_count() > 0);   // Checked by parser
6564
      return false;
6565
   }
6566
   else if (type_is_resolved(rtype))
6567
      sem_error(t, "subtype indication of a mode view declaration "
6568
                "must denote an unresolved record type");
6569

6570
   const int nfields = type_fields(rtype);
6571

6572
   LOCAL_BIT_MASK have;
6573
   mask_init(&have, nfields);
6574

6575
   const int nelems = type_fields(type);
6576
   for (int i = 0; i < nelems; i++) {
6577
      tree_t e = type_field(type, i);
6578
      assert(tree_kind(e) == T_VIEW_ELEMENT);
6579

6580
      if (!tree_has_ref(e))
6581
         return false;
6582

6583
      tree_t f = tree_ref(e);
6584
      assert(tree_kind(f) == T_FIELD_DECL);
6585

6586
      const int pos = tree_pos(f);
6587
      if (mask_test(&have, pos))
6588
         sem_error(e, "duplicate mode view element definition for field %s",
6589
                   istr(tree_ident(e)));
6590

6591
      mask_set(&have, pos);
6592

6593
      switch (tree_subkind(e)) {
6594
      case PORT_LINKAGE:
6595
         sem_error(e, "element mode indication cannot have mode LINKAGE");
6596

6597
      case PORT_RECORD_VIEW:
6598
      case PORT_ARRAY_VIEW:
6599
         {
6600
            tree_t name = tree_value(e);
6601
            type_t type = tree_type(e);
6602
            type_t view_type = tree_type(name);
6603

6604
            if (type_is_none(view_type))
6605
               return false;
6606

6607
            if (type_kind(view_type) != T_VIEW)
6608
               sem_error(name, "name in element mode view indication of field "
6609
                         "%s does not denote a mode view", istr(tree_ident(f)));
6610

6611
            type_t elem_type = type;
6612
            if (tree_subkind(e) == PORT_ARRAY_VIEW) {
6613
               if (!type_is_array(type))
6614
                  sem_error(e, "field %s with array mode view indication has "
6615
                            "non-array type %s", istr(tree_ident(f)),
6616
                            type_pp(type));
6617

6618
               elem_type = type_elem(type);
6619
            }
6620

6621
            if (!type_eq(elem_type, type_designated(view_type)))
6622
               sem_error(e, "field %s subtype %s is not compatible with mode "
6623
                         "view %s", istr(tree_ident(f)), type_pp(elem_type),
6624
                         type_pp(view_type));
6625
         }
6626
         break;
6627
      }
6628
   }
6629

6630
   if (mask_popcount(&have) != nfields) {
6631
      LOCAL_TEXT_BUF tb = tb_new();
6632
      for (int i = 0, missing = 0; i < nfields; i++) {
6633
         if (!mask_test(&have, i))
6634
            tb_printf(tb, "%s%s", missing++ > 0 ? ", " : "",
6635
                      istr(tree_ident(type_field(rtype, i))));
6636
      }
6637

6638
      sem_error(t, "missing mode view element defintion for %s", tb_get(tb));
6639
   }
6640

6641
   return true;
6642
}
6643

6644
static bool sem_check_cond_value(tree_t t, nametab_t *tab)
45✔
6645
{
6646
   type_t type = tree_type(t);
45✔
6647
   if (type_is_none(type))
45✔
6648
      return false;
6649

6650
   type_t std_bool = std_type(NULL, STD_BOOLEAN);
45✔
6651

6652
   const int nconds = tree_conds(t);
45✔
6653
   for (int i = 0; i < nconds; i++) {
144✔
6654
      tree_t cond = tree_cond(t, i);
103✔
6655
      assert(tree_kind(cond) == T_COND_EXPR);
103✔
6656

6657
      if (tree_has_value(cond)) {
103✔
6658
         tree_t value = tree_value(cond);
64✔
6659
         if (!sem_check(value, tab))
64✔
6660
            return false;
6661

6662
         if (!sem_check_type(value, std_bool, tab))
64✔
6663
            sem_error(value, "type of condition must be %s but have %s",
2✔
6664
                      type_pp(std_bool), type_pp(tree_type(value)));
6665
      }
6666
      else
6667
         assert(i == nconds - 1);
39✔
6668

6669
      if (tree_has_result(cond)) {
101✔
6670
         tree_t result = tree_result(cond);
91✔
6671
         if (!sem_check(result, tab))
91✔
6672
            return false;
6673

6674
         if (!sem_check_type(result, type, tab))
91✔
6675
            sem_error(result, "expected type of conditional expression to be "
101✔
6676
                      "%s but is %s", type_pp(type),
6677
                      type_pp(tree_type(result)));
6678
      }
6679
   }
6680

6681
   return true;
6682
}
6683

6684
static bool sem_check_sequence(tree_t t, nametab_t *tab)
6685
{
6686
   const int ndecls = tree_decls(t);
6687
   for (int i = 0; i < ndecls; i++) {
6688
      // Mark all constant declarations as they need to be treated
6689
      // specially when calculating longest static prefix
6690
      tree_t d = tree_decl(t, i);
6691
      if (tree_kind(d) == T_CONST_DECL)
6692
         tree_set_flag(d, TREE_F_SEQ_BLOCK);
6693
   }
6694

6695
   return true;
6696
}
6697

6698
static bool sem_check_prot_decl(tree_t t, nametab_t *tab)
6699
{
6700
   const int ndecls = tree_decls(t);
6701
   for (int i = 0; i < ndecls; i++) {
6702
      tree_t d = tree_decl(t, i);
6703
      if (tree_kind(d) == T_ALIAS) {
6704
         // LRM 19 section 5.6.2: it is an error if an alias declared
6705
         // within a protected type declaration denotes anything other
6706
         // than a method of a protected type
6707
         tree_t value = tree_value(d);
6708
         if (tree_kind(value) != T_PROT_REF)
6709
            sem_error(d, "an alias declared within a protected type "
6710
                      "declaration must denote a protected type method");
6711
      }
6712
   }
6713

6714
   return true;
6715
}
6716

6717
static bool sem_check_inertial(tree_t t, nametab_t *tab)
5✔
6718
{
6719
   if (!sem_check(tree_value(t), tab))
5✔
UNCOV
6720
      return false;
×
6721

6722
   return true;
6723
}
6724

6725
bool sem_check(tree_t t, nametab_t *tab)
660,506✔
6726
{
6727
   switch (tree_kind(t)) {
660,506✔
6728
   case T_ARCH:
4,390✔
6729
      return sem_check_arch(t, tab);
4,390✔
6730
   case T_PACKAGE:
1,287✔
6731
      return sem_check_package(t, tab);
1,287✔
6732
   case T_ENTITY:
4,383✔
6733
      return sem_check_entity(t, tab);
4,383✔
6734
   case T_TYPE_DECL:
4,470✔
6735
      return sem_check_type_decl(t, tab);
4,470✔
6736
   case T_SUBTYPE_DECL:
1,364✔
6737
      return sem_check_subtype_decl(t, tab);
1,364✔
6738
   case T_PORT_DECL:
3,303✔
6739
      return sem_check_port_decl(t, tab);
3,303✔
6740
   case T_PARAM_DECL:
30,173✔
6741
      return sem_check_param_decl(t, tab);
30,173✔
6742
   case T_GENERIC_DECL:
2,038✔
6743
      return sem_check_generic_decl(t, tab);
2,038✔
6744
   case T_SIGNAL_DECL:
5,378✔
6745
      return sem_check_signal_decl(t, tab);
5,378✔
6746
   case T_VAR_DECL:
10,803✔
6747
      return sem_check_var_decl(t, tab);
10,803✔
6748
   case T_CONST_DECL:
6,367✔
6749
      return sem_check_const_decl(t, tab);
6,367✔
6750
   case T_PROCESS:
4,315✔
6751
      return sem_check_process(t, tab);
4,315✔
6752
   case T_VAR_ASSIGN:
16,029✔
6753
      return sem_check_var_assign(t, tab);
16,029✔
6754
   case T_SIGNAL_ASSIGN:
4,526✔
6755
      return sem_check_signal_assign(t, tab);
4,526✔
6756
   case T_FCALL:
80,263✔
6757
   case T_PROT_FCALL:
6758
      return sem_check_fcall(t, tab);
80,263✔
6759
   case T_LITERAL:
85,978✔
6760
      return sem_check_literal(t);
85,978✔
6761
   case T_STRING:
24,210✔
6762
      return sem_check_string_literal(t);
24,210✔
6763
   case T_REF:
177,264✔
6764
      return sem_check_ref(t, tab);
177,264✔
6765
   case T_WAIT:
8,349✔
6766
      return sem_check_wait(t, tab);
8,349✔
6767
   case T_ASSERT:
18,294✔
6768
      return sem_check_assert(t, tab);
18,294✔
6769
   case T_QUALIFIED:
4,218✔
6770
      return sem_check_qualified(t, tab);
4,218✔
6771
   case T_FUNC_DECL:
5,881✔
6772
      return sem_check_func_decl(t, tab);
5,881✔
6773
   case T_AGGREGATE:
8,490✔
6774
      return sem_check_aggregate(t, tab);
8,490✔
6775
   case T_ATTR_REF:
20,216✔
6776
      return sem_check_attr_ref(t, false, tab);
20,216✔
6777
   case T_ARRAY_REF:
14,079✔
6778
      return sem_check_array_ref(t, tab);
14,079✔
6779
   case T_ARRAY_SLICE:
2,010✔
6780
      return sem_check_array_slice(t, tab);
2,010✔
6781
   case T_INSTANCE:
1,306✔
6782
      return sem_check_instance(t, tab);
1,306✔
6783
   case T_IF:
8,568✔
6784
      return sem_check_if(t, tab);
8,568✔
6785
   case T_NULL:
6786
      return true;
6787
   case T_PACK_BODY:
689✔
6788
      return sem_check_pack_body(t, tab);
689✔
6789
   case T_FUNC_BODY:
7,029✔
6790
      return sem_check_func_body(t, tab);
7,029✔
6791
   case T_RETURN:
12,740✔
6792
      return sem_check_return(t, tab);
12,740✔
6793
   case T_COND_RETURN:
7✔
6794
      return sem_check_cond_return(t, tab);
7✔
6795
   case T_COND_ASSIGN:
1,838✔
6796
      return sem_check_cond_assign(t, tab);
1,838✔
6797
   case T_WHILE:
458✔
6798
      return sem_check_while(t, tab);
458✔
6799
   case T_ALIAS:
1,494✔
6800
      return sem_check_alias(t, tab);
1,494✔
6801
   case T_FOR:
2,412✔
6802
      return sem_check_for(t, tab);
2,412✔
6803
   case T_PROC_DECL:
1,077✔
6804
      return sem_check_proc_decl(t, tab);
1,077✔
6805
   case T_PROC_BODY:
2,018✔
6806
      return sem_check_proc_body(t, tab);
2,018✔
6807
   case T_BLOCK:
272✔
6808
      return sem_check_block(t, tab);
272✔
6809
   case T_CASE:
541✔
6810
   case T_SELECT:
6811
      return sem_check_case(t, tab);
541✔
6812
   case T_EXIT:
409✔
6813
   case T_NEXT:
6814
      return sem_check_loop_control(t, tab);
409✔
6815
   case T_PCALL:
7,647✔
6816
   case T_PROT_PCALL:
6817
      return sem_check_pcall(t, tab);
7,647✔
6818
   case T_ATTR_SPEC:
313✔
6819
      return sem_check_attr_spec(t, tab);
313✔
6820
   case T_ATTR_DECL:
113✔
6821
      return sem_check_attr_decl(t);
113✔
6822
   case T_COMPONENT:
265✔
6823
      return sem_check_component(t, tab);
265✔
6824
   case T_IF_GENERATE:
152✔
6825
      return sem_check_if_generate(t, tab);
152✔
6826
   case T_FOR_GENERATE:
154✔
6827
      return sem_check_for_generate(t, tab);
154✔
6828
   case T_CASE_GENERATE:
7✔
6829
      return sem_check_case(t, tab);
7✔
6830
   case T_OPEN:
75✔
6831
      return sem_check_open(t);
75✔
6832
   case T_FIELD_DECL:
3,486✔
6833
      return sem_check_field_decl(t);
3,486✔
6834
   case T_FILE_DECL:
142✔
6835
      return sem_check_file_decl(t, tab);
142✔
6836
   case T_NEW:
541✔
6837
      return sem_check_new(t, tab);
541✔
6838
   case T_ALL:
2,336✔
6839
      return sem_check_all(t, tab);
2,336✔
6840
   case T_RECORD_REF:
8,587✔
6841
      return sem_check_record_ref(t, tab);
8,587✔
6842
   case T_UNIT_DECL:
157✔
6843
      return sem_check_unit_decl(t);
157✔
6844
   case T_USE:
14,243✔
6845
      return sem_check_use_clause(t, tab);
14,243✔
6846
   case T_TYPE_CONV:
4,587✔
6847
      return sem_check_conversion(t, tab);
4,587✔
6848
   case T_SPEC:
173✔
6849
      return sem_check_spec(t, tab);
173✔
6850
   case T_BINDING:
163✔
6851
      return sem_check_binding(t, tab);
163✔
6852
   case T_LIBRARY:
23,829✔
6853
      return sem_check_library_clause(t, tab);
23,829✔
6854
   case T_CONFIGURATION:
98✔
6855
      return sem_check_configuration(t, tab);
98✔
6856
   case T_PROT_BODY:
190✔
6857
      return sem_check_prot_body(t, tab);
190✔
6858
   case T_CONTEXT:
19✔
6859
      return sem_check_context_decl(t, tab);
19✔
6860
   case T_CONTEXT_REF:
21✔
6861
      return sem_check_context_ref(t, tab);
21✔
6862
   case T_BLOCK_CONFIG:
140✔
6863
      return sem_check_block_config(t, tab);
140✔
6864
   case T_IMPLICIT_SIGNAL:
23✔
6865
      return sem_check_implicit_signal(t, tab);
23✔
6866
   case T_DISCONNECT:
11✔
6867
      return sem_check_disconnect(t, tab);
11✔
6868
   case T_GROUP:
6869
   case T_GROUP_TEMPLATE:
6870
   case T_BOX:
6871
   case T_PSL:
6872
      return true;
6873
   case T_CONV_FUNC:
141✔
6874
      return sem_check_conv_func(t, tab);
141✔
6875
   case T_CONCURRENT:
2,958✔
6876
      return sem_check_concurrent(t, tab);
2,958✔
6877
   case T_PACK_INST:
228✔
6878
      return sem_check_pack_inst(t, tab);
228✔
6879
   case T_EXTERNAL_NAME:
127✔
6880
      return sem_check_external_name(t, tab);
127✔
6881
   case T_FORCE:
55✔
6882
      return sem_check_force(t, tab);
55✔
6883
   case T_RELEASE:
36✔
6884
      return sem_check_release(t, tab);
36✔
6885
   case T_PROT_REF:
15✔
6886
      return sem_check_prot_ref(t, tab);
15✔
6887
   case T_MATCH_CASE:
40✔
6888
   case T_MATCH_SELECT:
6889
      return sem_check_match_case(t, tab);
40✔
6890
   case T_FUNC_INST:
91✔
6891
   case T_PROC_INST:
6892
      return sem_check_subprogram_inst(t, tab);
91✔
6893
   case T_VIEW_DECL:
53✔
6894
      return sem_check_view_decl(t, tab);
53✔
6895
   case T_COND_VALUE:
45✔
6896
      return sem_check_cond_value(t, tab);
45✔
6897
   case T_SEQUENCE:
8✔
6898
      return sem_check_sequence(t, tab);
8✔
6899
   case T_PROT_DECL:
196✔
6900
      return sem_check_prot_decl(t, tab);
196✔
6901
   case T_INERTIAL:
5✔
6902
      return sem_check_inertial(t, tab);
5✔
UNCOV
6903
   default:
×
UNCOV
6904
      sem_error(t, "cannot check %s", tree_kind_str(tree_kind(t)));
×
6905
   }
6906
}
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

© 2025 Coveralls, Inc